Mercurial > MadButterfly
comparison examples/menu/filebrowser.c @ 309:4dfb850dee92
Add filebrowser program and SVG file.
author | wycc |
---|---|
date | Sun, 22 Feb 2009 13:45:28 +0800 |
parents | |
children | 4453ea44a83d |
comparison
equal
deleted
inserted
replaced
308:9e1d72eca57b | 309:4dfb850dee92 |
---|---|
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> | |
17 #include "menu.h" | |
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 if (data->curDir) | |
97 free(data->curDir); | |
98 data->curDir = strdup(curdir); | |
99 dir = opendir(curdir); | |
100 if (dir == NULL) { | |
101 printf("We can not open the direftory %s\n", curdir); | |
102 return; | |
103 } | |
104 | |
105 if (data->files) { | |
106 for(i=0;i<data->nFiles;i++) { | |
107 fileinfo_free(data->files[i]); | |
108 free(data->titles[i]); | |
109 } | |
110 } | |
111 data->nFiles = 0; | |
112 while(e = readdir(dir)) { | |
113 if (strcmp(e->d_name,".")==0) continue; | |
114 if (e->d_type == DT_DIR) { | |
115 if (data->nFiles < MAX_ENTRY) { | |
116 f = fileinfo_new(); | |
117 data->files[data->nFiles] = f; | |
118 data->titles[data->nFiles++] = strdup(e->d_name); | |
119 printf("%s\n", e->d_name); | |
120 } | |
121 } | |
122 } | |
123 | |
124 closedir(dir); | |
125 dir = opendir(curdir); | |
126 while(e = readdir(dir)) { | |
127 if (strcmp(e->d_name,".")==0) continue; | |
128 if (e->d_type == DT_REG) { | |
129 if (data->nFiles < MAX_ENTRY) { | |
130 f = fileinfo_new(); | |
131 data->files[data->nFiles] = f; | |
132 data->titles[data->nFiles++] = strdup(e->d_name); | |
133 printf("%s\n", e->d_name); | |
134 } | |
135 } | |
136 } | |
137 closedir(dir); | |
138 data->titles[data->nFiles] = NULL; | |
139 mb_animated_menu_set_titles(data->m,data->titles); | |
140 } | |
141 | |
142 | |
143 MyApp_InitContent(char *dir) | |
144 { | |
145 MyAppData *data = MBAPP_DATA(myApp,MyAppData); | |
146 subject_t *key = MBAPP_keySubject(myApp); | |
147 char name[255]; | |
148 coord_t *l; | |
149 int i; | |
150 mb_sprite_t *sprite=myApp->rootsprite; | |
151 | |
152 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",NULL); | |
153 mb_animated_menu_set_callback(data->m, myselect); | |
154 data->curDir = NULL; | |
155 data->nFiles=0; | |
156 MyApp_fillDirInfo(myApp,dir); | |
157 mb_animated_menu_set_speed(data->m,300); | |
158 } | |
159 | |
160 int main(int argc, char * const argv[]) { | |
161 subject_t *subject; | |
162 mb_obj_t *button; | |
163 MyAppData data; | |
164 mb_timeval_t tmo,interval; | |
165 char *dir; | |
166 | |
167 if (argc > 1) | |
168 dir = argv[1]; | |
169 else | |
170 dir ="/tmp"; | |
171 myApp = MBApp_Init("browser"); | |
172 MBApp_setData(myApp,&data); | |
173 MyApp_InitContent(dir); | |
174 | |
175 MBApp_loop(myApp); | |
176 | |
177 return 0; | |
178 } | |
179 | |
180 /* vim: set ts=4 */ |