Mercurial > MadButterfly
annotate examples/menu/filebrowser.c @ 1030:36a63287e1d4 refine_backend_if
Migrate examples/dynamic to new definition of backend
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 22 Nov 2010 17:43:22 +0800 |
parents | d7f224b97b7f |
children | 8863d23cea4b |
rev | line source |
---|---|
309 | 1 /*! \file |
2 * | |
3 * This is the demo program for the animated menu. We will use to test the MBAF API. | |
4 * We need to have group item1-item9 in the SVG file. Initially, we will show | |
5 * item1-item8 only. When a up/down key is pressed, we will draw the next item in item9 and | |
6 * add two words to move item1-item9 smoothly. The first word move items to the 3/4 position | |
7 * fastly. The second will move it from 3/4 to the final position slowly to make retard effect. | |
8 * | |
9 * If we press another key before the second words finish, we will delete the word and replace | |
10 * it with a word to move it fastly to the final position and then we will repeat the procedure | |
11 * to add another two words to move it to the next position. | |
12 */ | |
13 #include <stdio.h> | |
14 #include <mb.h> | |
15 #include <string.h> | |
16 #include <dirent.h> | |
325 | 17 //#include "menu.h" |
456
26c302b47de1
Change name of header files.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
18 #include "mb_af.h" |
26c302b47de1
Change name of header files.
Thinker K.F. Li <thinker@branda.to>
parents:
454
diff
changeset
|
19 #include <mb_ani_menu.h> |
309 | 20 |
21 | |
22 | |
23 struct fileinfo { | |
24 int height; | |
25 int width; | |
26 int depth; | |
27 int bitrate; | |
28 int duration; | |
29 int year; | |
30 char *comment; | |
31 }; | |
32 #define MAX_ENTRY 1000 | |
33 typedef struct { | |
34 mb_animated_menu_t *m; | |
35 char *curDir; | |
36 struct fileinfo *files[MAX_ENTRY]; | |
37 char *titles[MAX_ENTRY]; | |
38 int nFiles; | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
39 }app_data_t; |
309 | 40 |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
41 mbaf_t *app; |
309 | 42 |
43 | |
44 | |
45 void myselect(mb_animated_menu_t *m, int select) | |
46 { | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
47 app_data_t *data = MBAF_DATA(app,app_data_t); |
309 | 48 char path[1024]; |
49 int len,i; | |
50 | |
51 if (strcmp(data->titles[select],"..")==0) { | |
52 strcpy(path, data->curDir); | |
53 len = strlen(path); | |
54 for(i=len-1;i>0;i--) { | |
55 if (path[i] == '/') { | |
56 path[i] = 0; | |
57 break; | |
58 } | |
59 } | |
60 } else { | |
61 snprintf(path,1024,"%s/%s", data->curDir,data->titles[select]); | |
62 } | |
63 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
64 MyApp_fillDirInfo(app, path); |
309 | 65 } |
66 | |
67 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
68 void mypreview(app_data_t *data, char *path) |
344 | 69 { |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
70 redraw_man_t *rdman = MBAF_RDMAN(app); |
356
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
71 paint_t *paint, *old_paint; |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
72 shape_t *obj = (shape_t *) MB_SPRITE_GET_OBJ(app->rootsprite, "previewimg"); |
356
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
73 int w, h; |
344 | 74 |
75 printf("Preview %s\n",path); | |
542
d7f224b97b7f
Fix the wrong way of getting the paint of an image.
Thinker K.F. Li <thinker@branda.to>
parents:
456
diff
changeset
|
76 paint = rdman_img_ldr_load_paint(rdman, path); /* return a cached |
d7f224b97b7f
Fix the wrong way of getting the paint of an image.
Thinker K.F. Li <thinker@branda.to>
parents:
456
diff
changeset
|
77 * paint if the |
d7f224b97b7f
Fix the wrong way of getting the paint of an image.
Thinker K.F. Li <thinker@branda.to>
parents:
456
diff
changeset
|
78 * path was loaded |
d7f224b97b7f
Fix the wrong way of getting the paint of an image.
Thinker K.F. Li <thinker@branda.to>
parents:
456
diff
changeset
|
79 * before */ |
356
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
80 if (paint) { |
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
81 paint_image_get_size(paint, &w, &h); |
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
82 printf("image %d %d\n",w, h); |
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
83 old_paint = sh_get_fill(obj); |
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
84 rdman_paint_fill(rdman, paint, obj); |
542
d7f224b97b7f
Fix the wrong way of getting the paint of an image.
Thinker K.F. Li <thinker@branda.to>
parents:
456
diff
changeset
|
85 if(old_paint != paint) |
356
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
86 rdman_paint_free(rdman, old_paint); |
3e84458968ec
Move mb_img_data_t out from argument list of rdman_shape_image_new().
Thinker K.F. Li <thinker@branda.to>
parents:
348
diff
changeset
|
87 |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
88 rdman_shape_changed(MBAF_RDMAN(app),obj); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
89 rdman_redraw_changed(MBAF_RDMAN(app)); |
344 | 90 } |
91 } | |
92 | |
93 int endWith(char *path, char *ext) | |
94 { | |
95 int i; | |
96 char *s; | |
97 | |
98 s = path+strlen(path)-1; | |
99 for(i=strlen(ext)-1;i>=0;i--) { | |
100 if (*s != ext[i]) return 0; | |
101 s--; | |
102 if (s < path) return 0; | |
103 } | |
104 return 1; | |
105 } | |
106 | |
107 void myupdate(mb_animated_menu_t *m, int select) | |
108 { | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
109 app_data_t *data = MBAF_DATA(app,app_data_t); |
344 | 110 char *s = data->titles[select]; |
111 char path[1024]; | |
112 | |
113 printf("check %s\n",s); | |
114 | |
345 | 115 if (endWith(s,".png")) { |
344 | 116 snprintf(path,1024,"%s%s", data->curDir,data->titles[select]); |
117 mypreview(data,path); | |
118 } | |
119 } | |
120 | |
121 | |
122 | |
309 | 123 struct fileinfo *fileinfo_new() |
124 { | |
125 struct fileinfo *f = (struct fileinfo *) malloc(sizeof(struct fileinfo)); | |
126 | |
127 f->width = 0; | |
128 f->height = 0; | |
129 f->depth = 0; | |
130 f->bitrate = 0; | |
131 f->duration = 0; | |
132 f->year = 0; | |
133 f->comment = NULL; | |
134 return f; | |
135 } | |
136 | |
137 void fileinfo_free(struct fileinfo *f) | |
138 { | |
139 free(f); | |
140 } | |
141 | |
142 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
143 MyApp_fillDirInfo(mbaf_t *app,char *curdir) |
309 | 144 { |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
145 app_data_t *data = MBAF_DATA(app,app_data_t); |
309 | 146 DIR *dir; |
147 struct dirent *e; | |
148 struct fileinfo *f; | |
149 int i; | |
150 | |
151 dir = opendir(curdir); | |
152 if (dir == NULL) { | |
153 printf("We can not open the direftory %s\n", curdir); | |
154 return; | |
155 } | |
156 | |
329
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
157 if (data->curDir) |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
158 free(data->curDir); |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
159 data->curDir = strdup(curdir); |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
160 |
309 | 161 if (data->files) { |
162 for(i=0;i<data->nFiles;i++) { | |
163 fileinfo_free(data->files[i]); | |
164 free(data->titles[i]); | |
165 } | |
166 } | |
167 data->nFiles = 0; | |
168 while(e = readdir(dir)) { | |
169 if (strcmp(e->d_name,".")==0) continue; | |
170 if (e->d_type == DT_DIR) { | |
171 if (data->nFiles < MAX_ENTRY) { | |
172 f = fileinfo_new(); | |
173 data->files[data->nFiles] = f; | |
174 data->titles[data->nFiles++] = strdup(e->d_name); | |
175 printf("%s\n", e->d_name); | |
176 } | |
177 } | |
178 } | |
179 | |
180 closedir(dir); | |
181 dir = opendir(curdir); | |
182 while(e = readdir(dir)) { | |
183 if (strcmp(e->d_name,".")==0) continue; | |
184 if (e->d_type == DT_REG) { | |
185 if (data->nFiles < MAX_ENTRY) { | |
186 f = fileinfo_new(); | |
187 data->files[data->nFiles] = f; | |
188 data->titles[data->nFiles++] = strdup(e->d_name); | |
189 printf("%s\n", e->d_name); | |
190 } | |
191 } | |
192 } | |
193 closedir(dir); | |
194 data->titles[data->nFiles] = NULL; | |
195 mb_animated_menu_set_titles(data->m,data->titles); | |
196 } | |
197 | |
198 | |
199 MyApp_InitContent(char *dir) | |
200 { | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
201 app_data_t *data = MBAF_DATA(app,app_data_t); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
202 subject_t *key = MBAF_KB_SUBJECT(app); |
309 | 203 char name[255]; |
204 coord_t *l; | |
205 int i; | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
206 mb_sprite_t *sprite=app->rootsprite; |
309 | 207 |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
208 data->m = mb_animated_menu_new(app,app->rootsprite,"item",NULL); |
309 | 209 mb_animated_menu_set_callback(data->m, myselect); |
344 | 210 mb_animated_menu_set_update_callback(data->m, myupdate); |
309 | 211 data->curDir = NULL; |
212 data->nFiles=0; | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
213 MyApp_fillDirInfo(app,dir); |
309 | 214 mb_animated_menu_set_speed(data->m,300); |
215 } | |
216 | |
217 int main(int argc, char * const argv[]) { | |
218 subject_t *subject; | |
219 mb_obj_t *button; | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
220 app_data_t data; |
309 | 221 mb_timeval_t tmo,interval; |
222 char *dir; | |
223 | |
224 if (argc > 1) | |
225 dir = argv[1]; | |
226 else | |
227 dir ="/tmp"; | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
228 app = mbaf_init("browser", ".libs"); |
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
229 mbaf_set_data(app,&data); |
309 | 230 MyApp_InitContent(dir); |
231 | |
454
9b8dda201ccb
Make naming convention consistent with MadButterfly.
Thinker K.F. Li <thinker@branda.to>
parents:
453
diff
changeset
|
232 mbaf_loop(app); |
309 | 233 |
234 return 0; | |
235 } | |
236 | |
237 /* vim: set ts=4 */ |