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