Mercurial > MadButterfly
annotate examples/menu/filebrowser.c @ 443:1f900a67bd38
Doc How to Define a Backend
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 31 Jul 2009 13:30:43 +0800 |
parents | 31b6633e3538 |
children | 84ce2d4a8c3f |
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" |
309 | 18 #include "mbapp.h" |
19 #include <animated_menu.h> | |
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; | |
39 }MyAppData; | |
40 | |
41 MBApp *myApp; | |
42 | |
43 | |
44 | |
45 void myselect(mb_animated_menu_t *m, int select) | |
46 { | |
47 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
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 | |
64 MyApp_fillDirInfo(myApp, path); | |
65 } | |
66 | |
67 | |
344 | 68 void mypreview(MyAppData *data, char *path) |
69 { | |
346
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
345
diff
changeset
|
70 redraw_man_t *rdman = MBAPP_RDMAN(myApp); |
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; |
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
|
72 paint_t *previewimg_paint; |
344 | 73 shape_t *obj = (shape_t *) MB_SPRITE_GET_OBJ(myApp->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
|
74 int w, h; |
344 | 75 |
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
|
76 previewimg_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
|
77 (paint_t *)MB_SPRITE_GET_OBJ(myApp->rootsprite, |
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
|
78 "previewimg_paint_img"); |
344 | 79 printf("Preview %s\n",path); |
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 paint = rdman_img_ldr_load_paint(rdman, path); |
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 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
|
82 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
|
83 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
|
84 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
|
85 rdman_paint_fill(rdman, paint, 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
|
86 if(old_paint != previewimg_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 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
|
88 |
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
|
89 rdman_shape_changed(MBAPP_RDMAN(myApp),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
|
90 rdman_redraw_changed(MBAPP_RDMAN(myApp)); |
344 | 91 } |
92 } | |
93 | |
94 int endWith(char *path, char *ext) | |
95 { | |
96 int i; | |
97 char *s; | |
98 | |
99 s = path+strlen(path)-1; | |
100 for(i=strlen(ext)-1;i>=0;i--) { | |
101 if (*s != ext[i]) return 0; | |
102 s--; | |
103 if (s < path) return 0; | |
104 } | |
105 return 1; | |
106 } | |
107 | |
108 void myupdate(mb_animated_menu_t *m, int select) | |
109 { | |
110 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
111 char *s = data->titles[select]; | |
112 char path[1024]; | |
113 | |
114 printf("check %s\n",s); | |
115 | |
345 | 116 if (endWith(s,".png")) { |
344 | 117 snprintf(path,1024,"%s%s", data->curDir,data->titles[select]); |
118 mypreview(data,path); | |
119 } | |
120 } | |
121 | |
122 | |
123 | |
309 | 124 struct fileinfo *fileinfo_new() |
125 { | |
126 struct fileinfo *f = (struct fileinfo *) malloc(sizeof(struct fileinfo)); | |
127 | |
128 f->width = 0; | |
129 f->height = 0; | |
130 f->depth = 0; | |
131 f->bitrate = 0; | |
132 f->duration = 0; | |
133 f->year = 0; | |
134 f->comment = NULL; | |
135 return f; | |
136 } | |
137 | |
138 void fileinfo_free(struct fileinfo *f) | |
139 { | |
140 free(f); | |
141 } | |
142 | |
143 | |
144 MyApp_fillDirInfo(MBApp *app,char *curdir) | |
145 { | |
146 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
147 DIR *dir; | |
148 struct dirent *e; | |
149 struct fileinfo *f; | |
150 int i; | |
151 | |
152 dir = opendir(curdir); | |
153 if (dir == NULL) { | |
154 printf("We can not open the direftory %s\n", curdir); | |
155 return; | |
156 } | |
157 | |
329
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
158 if (data->curDir) |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
159 free(data->curDir); |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
160 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
|
161 |
309 | 162 if (data->files) { |
163 for(i=0;i<data->nFiles;i++) { | |
164 fileinfo_free(data->files[i]); | |
165 free(data->titles[i]); | |
166 } | |
167 } | |
168 data->nFiles = 0; | |
169 while(e = readdir(dir)) { | |
170 if (strcmp(e->d_name,".")==0) continue; | |
171 if (e->d_type == DT_DIR) { | |
172 if (data->nFiles < MAX_ENTRY) { | |
173 f = fileinfo_new(); | |
174 data->files[data->nFiles] = f; | |
175 data->titles[data->nFiles++] = strdup(e->d_name); | |
176 printf("%s\n", e->d_name); | |
177 } | |
178 } | |
179 } | |
180 | |
181 closedir(dir); | |
182 dir = opendir(curdir); | |
183 while(e = readdir(dir)) { | |
184 if (strcmp(e->d_name,".")==0) continue; | |
185 if (e->d_type == DT_REG) { | |
186 if (data->nFiles < MAX_ENTRY) { | |
187 f = fileinfo_new(); | |
188 data->files[data->nFiles] = f; | |
189 data->titles[data->nFiles++] = strdup(e->d_name); | |
190 printf("%s\n", e->d_name); | |
191 } | |
192 } | |
193 } | |
194 closedir(dir); | |
195 data->titles[data->nFiles] = NULL; | |
196 mb_animated_menu_set_titles(data->m,data->titles); | |
197 } | |
198 | |
199 | |
200 MyApp_InitContent(char *dir) | |
201 { | |
202 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
203 subject_t *key = MBAPP_keySubject(myApp); | |
204 char name[255]; | |
205 coord_t *l; | |
206 int i; | |
207 mb_sprite_t *sprite=myApp->rootsprite; | |
208 | |
209 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",NULL); | |
210 mb_animated_menu_set_callback(data->m, myselect); | |
344 | 211 mb_animated_menu_set_update_callback(data->m, myupdate); |
309 | 212 data->curDir = NULL; |
213 data->nFiles=0; | |
214 MyApp_fillDirInfo(myApp,dir); | |
215 mb_animated_menu_set_speed(data->m,300); | |
216 } | |
217 | |
218 int main(int argc, char * const argv[]) { | |
219 subject_t *subject; | |
220 mb_obj_t *button; | |
221 MyAppData data; | |
222 mb_timeval_t tmo,interval; | |
223 char *dir; | |
224 | |
225 if (argc > 1) | |
226 dir = argv[1]; | |
227 else | |
228 dir ="/tmp"; | |
399
31b6633e3538
- Fix a minor error in src/sprite.c: should check *(s-1) instead of
john.cylee@gmail.com
parents:
356
diff
changeset
|
229 myApp = MBApp_Init(".libs/browser"); |
309 | 230 MBApp_setData(myApp,&data); |
231 MyApp_InitContent(dir); | |
232 | |
233 MBApp_loop(myApp); | |
234 | |
235 return 0; | |
236 } | |
237 | |
238 /* vim: set ts=4 */ |