8197
|
1
|
|
2 #include "../config.h"
|
|
3
|
|
4 #include <stdlib.h>
|
|
5 #include <stdio.h>
|
|
6 #include <string.h>
|
|
7
|
|
8 #include "img_format.h"
|
|
9 #include "mp_image.h"
|
|
10
|
|
11 #include "../m_struct.h"
|
|
12 #include "../m_option.h"
|
|
13 #include "menu.h"
|
|
14
|
|
15 #include "../libvo/font_load.h"
|
9380
|
16 #include "../osdep/keycodes.h"
|
8197
|
17
|
|
18 struct menu_priv_s {
|
|
19 char** lines;
|
|
20 int num_lines;
|
|
21 int cur_line;
|
|
22 int disp_lines;
|
|
23 int minb;
|
|
24 int hspace;
|
|
25 char* file;
|
|
26 };
|
|
27
|
|
28 static struct menu_priv_s cfg_dflt = {
|
|
29 NULL,
|
|
30 0,
|
|
31 0,
|
|
32 0,
|
|
33 0,
|
|
34 3,
|
|
35 NULL
|
|
36 };
|
|
37
|
|
38 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
|
|
39
|
|
40 static m_option_t cfg_fields[] = {
|
|
41 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
|
|
42 { "hspace", ST_OFF(hspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
|
|
43 { "file", ST_OFF(file), CONF_TYPE_STRING, 0, 0, 0, NULL },
|
|
44 { NULL, NULL, NULL, 0,0,0,NULL }
|
|
45 };
|
|
46
|
|
47 #define mpriv (menu->priv)
|
|
48
|
|
49 static void read_cmd(menu_t* menu,int cmd) {
|
|
50 switch(cmd) {
|
|
51 case MENU_CMD_UP:
|
|
52 mpriv->cur_line -= mpriv->disp_lines / 2;
|
|
53 if(mpriv->cur_line < 0)
|
|
54 mpriv->cur_line = 0;
|
|
55 break;
|
|
56 case MENU_CMD_DOWN:
|
|
57 case MENU_CMD_OK:
|
|
58 mpriv->cur_line += mpriv->disp_lines / 2;
|
|
59 if(mpriv->cur_line >= mpriv->num_lines)
|
|
60 mpriv->cur_line = mpriv->num_lines - 1;
|
|
61 break;
|
|
62 case MENU_CMD_CANCEL:
|
|
63 menu->show = 0;
|
|
64 menu->cl = 1;
|
|
65 break;
|
|
66 }
|
|
67 }
|
|
68
|
|
69 static void read_key(menu_t* menu,int c) {
|
|
70 switch (c) {
|
|
71 case KEY_HOME:
|
|
72 mpriv->cur_line = 0;
|
|
73 break;
|
|
74 case KEY_END:
|
|
75 mpriv->cur_line = mpriv->num_lines - 1;
|
|
76 break;
|
|
77 case KEY_PAGE_UP:
|
|
78 mpriv->cur_line = mpriv->cur_line > mpriv->disp_lines ?
|
|
79 mpriv->cur_line - mpriv->disp_lines : 0;
|
|
80 break;
|
|
81 case KEY_PAGE_DOWN:
|
|
82 mpriv->cur_line = mpriv->cur_line + mpriv->disp_lines > mpriv->num_lines - 1 ? mpriv->num_lines - 1 : mpriv->cur_line + mpriv->disp_lines;
|
|
83 break;
|
|
84 default:
|
|
85 menu_dflt_read_key(menu,c);
|
|
86 }
|
|
87 }
|
|
88
|
|
89
|
|
90 static void draw(menu_t* menu,mp_image_t* mpi) {
|
|
91 int x = mpriv->minb;
|
|
92 int y = mpriv->minb;
|
|
93 //int th = 2*mpriv->hspace + vo_font->height;
|
|
94 int i,end;
|
|
95
|
|
96 if(x < 0) x = 8;
|
|
97 if(y < 0) y = 8;
|
|
98
|
|
99 mpriv->disp_lines = (mpi->h + mpriv->hspace - 2*mpriv->minb) / ( vo_font->height + mpriv->hspace);
|
|
100 if(mpriv->num_lines - mpriv->cur_line < mpriv->disp_lines) {
|
|
101 i = mpriv->num_lines - 1 - mpriv->disp_lines;
|
|
102 if(i < 0) i = 0;
|
|
103 end = mpriv->num_lines - 1;
|
|
104 } else {
|
|
105 i = mpriv->cur_line;
|
|
106 end = i + mpriv->disp_lines;
|
|
107 if(end >= mpriv->num_lines) end = mpriv->num_lines - 1;
|
|
108 }
|
|
109
|
|
110 for( ; i < end ; i++) {
|
|
111 menu_draw_text(mpi,mpriv->lines[i],x,y);
|
|
112 y += vo_font->height + mpriv->hspace;
|
|
113 }
|
|
114
|
|
115 }
|
|
116
|
|
117 #define BUF_SIZE 1024
|
|
118
|
|
119 static int open(menu_t* menu, char* args) {
|
|
120 FILE* fd;
|
|
121 char buf[BUF_SIZE];
|
|
122 char *l;
|
|
123 int s;
|
|
124 int pos = 0, r = 0;
|
|
125 args = NULL; // Warning kill
|
|
126
|
|
127 menu->draw = draw;
|
|
128 menu->read_cmd = read_cmd;
|
|
129 menu->read_key = read_key;
|
|
130
|
|
131 if(!mpriv->file) {
|
|
132 printf("Menu txt need a txt file name (param file)\n");
|
|
133 return 0;
|
|
134 }
|
|
135
|
|
136 fd = fopen(mpriv->file,"r");
|
|
137 if(!fd) {
|
|
138 printf("Menu txt can't open: %s\n",mpriv->file);
|
|
139 return 0;
|
|
140 }
|
|
141
|
|
142 while(1) {
|
|
143 r = fread(buf+pos,1,BUF_SIZE-pos-1,fd);
|
|
144 if(r <= 0) {
|
|
145 if(pos > 0) {
|
|
146 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
|
|
147 mpriv->lines[mpriv->num_lines] = strdup(buf);
|
|
148 mpriv->num_lines++;
|
|
149 }
|
|
150 fclose(fd);
|
|
151 break;
|
|
152 }
|
|
153 pos += r;
|
|
154 buf[pos] = '\0';
|
|
155
|
|
156 while((l = strchr(buf,'\n')) != NULL) {
|
|
157 s = l-buf;
|
|
158 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
|
|
159 mpriv->lines[mpriv->num_lines] = malloc(s+1);
|
|
160 memcpy(mpriv->lines[mpriv->num_lines],buf,s);
|
|
161 mpriv->lines[mpriv->num_lines][s] = '\0';
|
|
162 pos -= s + 1;
|
|
163 if(pos > 0)
|
|
164 memmove(buf,l+1,pos);
|
|
165 buf[pos] = '\0';
|
|
166 mpriv->num_lines++;
|
|
167 }
|
|
168 if(pos >= BUF_SIZE-1) {
|
|
169 printf("Warning too long line, splitting it\n");
|
|
170 mpriv->lines = realloc(mpriv->lines,(mpriv->num_lines + 1)*sizeof(char*));
|
|
171 mpriv->lines[mpriv->num_lines] = strdup(buf);
|
|
172 mpriv->num_lines++;
|
|
173 pos = 0;
|
|
174 }
|
|
175 }
|
|
176
|
|
177 printf("Parsed %d lines\n",mpriv->num_lines);
|
|
178
|
|
179 return 1;
|
|
180 }
|
|
181
|
|
182 const menu_info_t menu_info_txt = {
|
|
183 "Text file viewer",
|
|
184 "txt",
|
|
185 "Albeu",
|
|
186 "",
|
|
187 {
|
|
188 "txt_cfg",
|
|
189 sizeof(struct menu_priv_s),
|
|
190 &cfg_dflt,
|
|
191 cfg_fields
|
|
192 },
|
|
193 open,
|
|
194 };
|