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