Mercurial > MadButterfly
annotate examples/menu/filebrowser.c @ 329:740844ee48c4
Update position of light-bar after update menu item
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 06 Mar 2009 20:39:25 +0800 |
parents | 4453ea44a83d |
children | ab7f3c00fd05 |
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 | |
68 struct fileinfo *fileinfo_new() | |
69 { | |
70 struct fileinfo *f = (struct fileinfo *) malloc(sizeof(struct fileinfo)); | |
71 | |
72 f->width = 0; | |
73 f->height = 0; | |
74 f->depth = 0; | |
75 f->bitrate = 0; | |
76 f->duration = 0; | |
77 f->year = 0; | |
78 f->comment = NULL; | |
79 return f; | |
80 } | |
81 | |
82 void fileinfo_free(struct fileinfo *f) | |
83 { | |
84 free(f); | |
85 } | |
86 | |
87 | |
88 MyApp_fillDirInfo(MBApp *app,char *curdir) | |
89 { | |
90 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
91 DIR *dir; | |
92 struct dirent *e; | |
93 struct fileinfo *f; | |
94 int i; | |
95 | |
96 dir = opendir(curdir); | |
97 if (dir == NULL) { | |
98 printf("We can not open the direftory %s\n", curdir); | |
99 return; | |
100 } | |
101 | |
329
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
102 if (data->curDir) |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
103 free(data->curDir); |
740844ee48c4
Update position of light-bar after update menu item
Thinker K.F. Li <thinker@branda.to>
parents:
325
diff
changeset
|
104 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
|
105 |
309 | 106 if (data->files) { |
107 for(i=0;i<data->nFiles;i++) { | |
108 fileinfo_free(data->files[i]); | |
109 free(data->titles[i]); | |
110 } | |
111 } | |
112 data->nFiles = 0; | |
113 while(e = readdir(dir)) { | |
114 if (strcmp(e->d_name,".")==0) continue; | |
115 if (e->d_type == DT_DIR) { | |
116 if (data->nFiles < MAX_ENTRY) { | |
117 f = fileinfo_new(); | |
118 data->files[data->nFiles] = f; | |
119 data->titles[data->nFiles++] = strdup(e->d_name); | |
120 printf("%s\n", e->d_name); | |
121 } | |
122 } | |
123 } | |
124 | |
125 closedir(dir); | |
126 dir = opendir(curdir); | |
127 while(e = readdir(dir)) { | |
128 if (strcmp(e->d_name,".")==0) continue; | |
129 if (e->d_type == DT_REG) { | |
130 if (data->nFiles < MAX_ENTRY) { | |
131 f = fileinfo_new(); | |
132 data->files[data->nFiles] = f; | |
133 data->titles[data->nFiles++] = strdup(e->d_name); | |
134 printf("%s\n", e->d_name); | |
135 } | |
136 } | |
137 } | |
138 closedir(dir); | |
139 data->titles[data->nFiles] = NULL; | |
140 mb_animated_menu_set_titles(data->m,data->titles); | |
141 } | |
142 | |
143 | |
144 MyApp_InitContent(char *dir) | |
145 { | |
146 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
147 subject_t *key = MBAPP_keySubject(myApp); | |
148 char name[255]; | |
149 coord_t *l; | |
150 int i; | |
151 mb_sprite_t *sprite=myApp->rootsprite; | |
152 | |
153 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",NULL); | |
154 mb_animated_menu_set_callback(data->m, myselect); | |
155 data->curDir = NULL; | |
156 data->nFiles=0; | |
157 MyApp_fillDirInfo(myApp,dir); | |
158 mb_animated_menu_set_speed(data->m,300); | |
159 } | |
160 | |
161 int main(int argc, char * const argv[]) { | |
162 subject_t *subject; | |
163 mb_obj_t *button; | |
164 MyAppData data; | |
165 mb_timeval_t tmo,interval; | |
166 char *dir; | |
167 | |
168 if (argc > 1) | |
169 dir = argv[1]; | |
170 else | |
171 dir ="/tmp"; | |
172 myApp = MBApp_Init("browser"); | |
173 MBApp_setData(myApp,&data); | |
174 MyApp_InitContent(dir); | |
175 | |
176 MBApp_loop(myApp); | |
177 | |
178 return 0; | |
179 } | |
180 | |
181 /* vim: set ts=4 */ |