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