Mercurial > MadButterfly
annotate examples/menu/filebrowser.c @ 348:04d22dc38bc0
Change declaration of sh_image_set_img_data().
- x, y, w, h are not passed to sh_image_set_img_data() any more.
- Applications should manage life-cycle of mb_img_data_t, paint_image_t do
not manage it for applications any more.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 08 Mar 2009 22:24:54 +0800 |
parents | b247beaac4f0 |
children | 3e84458968ec |
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); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
345
diff
changeset
|
71 mb_img_ldr_t *ldr = rdman_img_ldr(rdman); |
b391722bf20e
sh_image_t::img_data is managed by paint_image_t.
Thinker K.F. Li <thinker@branda.to>
parents:
345
diff
changeset
|
72 mb_img_data_t *img = MB_IMG_LDR_LOAD(ldr, path); |
344 | 73 shape_t *obj = (shape_t *) MB_SPRITE_GET_OBJ(myApp->rootsprite, "previewimg"); |
348
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
74 mb_img_data_t *previewimg_img_data; |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
75 mb_img_data_t *old_img; |
344 | 76 |
348
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
77 previewimg_img_data = |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
78 (mb_img_data_t *)MB_SPRITE_GET_OBJ(myApp->rootsprite, |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
79 "previewimg_img_data"); |
344 | 80 printf("Preview %s\n",path); |
81 if (img) { | |
82 printf("image %d %d\n",img->w,img->h); | |
348
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
83 old_img = sh_image_get_img_data(obj); |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
84 sh_image_set_img_data(obj,img); |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
85 if(old_img != previewimg_img_data) |
04d22dc38bc0
Change declaration of sh_image_set_img_data().
Thinker K.F. Li <thinker@branda.to>
parents:
347
diff
changeset
|
86 MB_IMG_DATA_FREE(old_img); |
344 | 87 rdman_shape_changed(MBAPP_RDMAN(myApp),obj); |
88 rdman_redraw_changed(MBAPP_RDMAN(myApp)); | |
89 } | |
90 } | |
91 | |
92 int endWith(char *path, char *ext) | |
93 { | |
94 int i; | |
95 char *s; | |
96 | |
97 s = path+strlen(path)-1; | |
98 for(i=strlen(ext)-1;i>=0;i--) { | |
99 if (*s != ext[i]) return 0; | |
100 s--; | |
101 if (s < path) return 0; | |
102 } | |
103 return 1; | |
104 } | |
105 | |
106 void myupdate(mb_animated_menu_t *m, int select) | |
107 { | |
108 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
109 char *s = data->titles[select]; | |
110 char path[1024]; | |
111 | |
112 printf("check %s\n",s); | |
113 | |
345 | 114 if (endWith(s,".png")) { |
344 | 115 snprintf(path,1024,"%s%s", data->curDir,data->titles[select]); |
116 mypreview(data,path); | |
117 } | |
118 } | |
119 | |
120 | |
121 | |
309 | 122 struct fileinfo *fileinfo_new() |
123 { | |
124 struct fileinfo *f = (struct fileinfo *) malloc(sizeof(struct fileinfo)); | |
125 | |
126 f->width = 0; | |
127 f->height = 0; | |
128 f->depth = 0; | |
129 f->bitrate = 0; | |
130 f->duration = 0; | |
131 f->year = 0; | |
132 f->comment = NULL; | |
133 return f; | |
134 } | |
135 | |
136 void fileinfo_free(struct fileinfo *f) | |
137 { | |
138 free(f); | |
139 } | |
140 | |
141 | |
142 MyApp_fillDirInfo(MBApp *app,char *curdir) | |
143 { | |
144 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
145 DIR *dir; | |
146 struct dirent *e; | |
147 struct fileinfo *f; | |
148 int i; | |
149 | |
150 dir = opendir(curdir); | |
151 if (dir == NULL) { | |
152 printf("We can not open the direftory %s\n", curdir); | |
153 return; | |
154 } | |
155 | |
329
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
156 if (data->curDir) |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
157 free(data->curDir); |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
158 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
|
159 |
309 | 160 if (data->files) { |
161 for(i=0;i<data->nFiles;i++) { | |
162 fileinfo_free(data->files[i]); | |
163 free(data->titles[i]); | |
164 } | |
165 } | |
166 data->nFiles = 0; | |
167 while(e = readdir(dir)) { | |
168 if (strcmp(e->d_name,".")==0) continue; | |
169 if (e->d_type == DT_DIR) { | |
170 if (data->nFiles < MAX_ENTRY) { | |
171 f = fileinfo_new(); | |
172 data->files[data->nFiles] = f; | |
173 data->titles[data->nFiles++] = strdup(e->d_name); | |
174 printf("%s\n", e->d_name); | |
175 } | |
176 } | |
177 } | |
178 | |
179 closedir(dir); | |
180 dir = opendir(curdir); | |
181 while(e = readdir(dir)) { | |
182 if (strcmp(e->d_name,".")==0) continue; | |
183 if (e->d_type == DT_REG) { | |
184 if (data->nFiles < MAX_ENTRY) { | |
185 f = fileinfo_new(); | |
186 data->files[data->nFiles] = f; | |
187 data->titles[data->nFiles++] = strdup(e->d_name); | |
188 printf("%s\n", e->d_name); | |
189 } | |
190 } | |
191 } | |
192 closedir(dir); | |
193 data->titles[data->nFiles] = NULL; | |
194 mb_animated_menu_set_titles(data->m,data->titles); | |
195 } | |
196 | |
197 | |
198 MyApp_InitContent(char *dir) | |
199 { | |
200 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
201 subject_t *key = MBAPP_keySubject(myApp); | |
202 char name[255]; | |
203 coord_t *l; | |
204 int i; | |
205 mb_sprite_t *sprite=myApp->rootsprite; | |
206 | |
207 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",NULL); | |
208 mb_animated_menu_set_callback(data->m, myselect); | |
344 | 209 mb_animated_menu_set_update_callback(data->m, myupdate); |
309 | 210 data->curDir = NULL; |
211 data->nFiles=0; | |
212 MyApp_fillDirInfo(myApp,dir); | |
213 mb_animated_menu_set_speed(data->m,300); | |
214 } | |
215 | |
216 int main(int argc, char * const argv[]) { | |
217 subject_t *subject; | |
218 mb_obj_t *button; | |
219 MyAppData data; | |
220 mb_timeval_t tmo,interval; | |
221 char *dir; | |
222 | |
223 if (argc > 1) | |
224 dir = argv[1]; | |
225 else | |
226 dir ="/tmp"; | |
227 myApp = MBApp_Init("browser"); | |
228 MBApp_setData(myApp,&data); | |
229 MyApp_InitContent(dir); | |
230 | |
231 MBApp_loop(myApp); | |
232 | |
233 return 0; | |
234 } | |
235 | |
236 /* vim: set ts=4 */ |