annotate examples/menu/filebrowser.c @ 325:4453ea44a83d

Remove the menu.h
author root@localhost.localdomain
date Thu, 05 Mar 2009 14:52:06 +0800
parents 4dfb850dee92
children 740844ee48c4
rev   line source
309
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
1 /*! \file
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
2 *
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
3 * This is the demo program for the animated menu. We will use to test the MBAF API.
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
4 * We need to have group item1-item9 in the SVG file. Initially, we will show
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
5 * item1-item8 only. When a up/down key is pressed, we will draw the next item in item9 and
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
6 * add two words to move item1-item9 smoothly. The first word move items to the 3/4 position
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
7 * fastly. The second will move it from 3/4 to the final position slowly to make retard effect.
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
8 *
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
9 * If we press another key before the second words finish, we will delete the word and replace
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
10 * it with a word to move it fastly to the final position and then we will repeat the procedure
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
11 * to add another two words to move it to the next position.
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
12 */
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
13 #include <stdio.h>
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
14 #include <mb.h>
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
15 #include <string.h>
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
16 #include <dirent.h>
325
4453ea44a83d Remove the menu.h
root@localhost.localdomain
parents: 309
diff changeset
17 //#include "menu.h"
309
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
18 #include "mbapp.h"
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
19 #include <animated_menu.h>
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
20
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
21
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
22
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
23 struct fileinfo {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
24 int height;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
25 int width;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
26 int depth;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
27 int bitrate;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
28 int duration;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
29 int year;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
30 char *comment;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
31 };
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
32 #define MAX_ENTRY 1000
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
33 typedef struct {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
34 mb_animated_menu_t *m;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
35 char *curDir;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
36 struct fileinfo *files[MAX_ENTRY];
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
37 char *titles[MAX_ENTRY];
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
38 int nFiles;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
39 }MyAppData;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
40
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
41 MBApp *myApp;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
42
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
43
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
44
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
45 void myselect(mb_animated_menu_t *m, int select)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
46 {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
47 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
48 char path[1024];
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
49 int len,i;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
50
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
51 if (strcmp(data->titles[select],"..")==0) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
52 strcpy(path, data->curDir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
53 len = strlen(path);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
54 for(i=len-1;i>0;i--) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
55 if (path[i] == '/') {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
56 path[i] = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
57 break;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
58 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
59 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
60 } else {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
61 snprintf(path,1024,"%s/%s", data->curDir,data->titles[select]);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
62 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
63
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
64 MyApp_fillDirInfo(myApp, path);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
65 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
66
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
67
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
68 struct fileinfo *fileinfo_new()
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
69 {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
70 struct fileinfo *f = (struct fileinfo *) malloc(sizeof(struct fileinfo));
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
71
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
72 f->width = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
73 f->height = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
74 f->depth = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
75 f->bitrate = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
76 f->duration = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
77 f->year = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
78 f->comment = NULL;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
79 return f;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
80 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
81
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
82 void fileinfo_free(struct fileinfo *f)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
83 {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
84 free(f);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
85 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
86
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
87
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
88 MyApp_fillDirInfo(MBApp *app,char *curdir)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
89 {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
90 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
91 DIR *dir;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
92 struct dirent *e;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
93 struct fileinfo *f;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
94 int i;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
95
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
96 if (data->curDir)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
97 free(data->curDir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
98 data->curDir = strdup(curdir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
99 dir = opendir(curdir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
100 if (dir == NULL) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
101 printf("We can not open the direftory %s\n", curdir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
102 return;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
103 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
104
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
105 if (data->files) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
106 for(i=0;i<data->nFiles;i++) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
107 fileinfo_free(data->files[i]);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
108 free(data->titles[i]);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
109 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
110 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
111 data->nFiles = 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
112 while(e = readdir(dir)) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
113 if (strcmp(e->d_name,".")==0) continue;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
114 if (e->d_type == DT_DIR) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
115 if (data->nFiles < MAX_ENTRY) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
116 f = fileinfo_new();
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
117 data->files[data->nFiles] = f;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
118 data->titles[data->nFiles++] = strdup(e->d_name);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
119 printf("%s\n", e->d_name);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
120 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
121 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
122 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
123
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
124 closedir(dir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
125 dir = opendir(curdir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
126 while(e = readdir(dir)) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
127 if (strcmp(e->d_name,".")==0) continue;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
128 if (e->d_type == DT_REG) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
129 if (data->nFiles < MAX_ENTRY) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
130 f = fileinfo_new();
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
131 data->files[data->nFiles] = f;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
132 data->titles[data->nFiles++] = strdup(e->d_name);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
133 printf("%s\n", e->d_name);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
134 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
135 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
136 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
137 closedir(dir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
138 data->titles[data->nFiles] = NULL;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
139 mb_animated_menu_set_titles(data->m,data->titles);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
140 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
141
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
142
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
143 MyApp_InitContent(char *dir)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
144 {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
145 MyAppData *data = MBAPP_DATA(myApp,MyAppData);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
146 subject_t *key = MBAPP_keySubject(myApp);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
147 char name[255];
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
148 coord_t *l;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
149 int i;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
150 mb_sprite_t *sprite=myApp->rootsprite;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
151
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
152 data->m = mb_animated_menu_new(myApp,myApp->rootsprite,"item",NULL);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
153 mb_animated_menu_set_callback(data->m, myselect);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
154 data->curDir = NULL;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
155 data->nFiles=0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
156 MyApp_fillDirInfo(myApp,dir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
157 mb_animated_menu_set_speed(data->m,300);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
158 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
159
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
160 int main(int argc, char * const argv[]) {
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
161 subject_t *subject;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
162 mb_obj_t *button;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
163 MyAppData data;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
164 mb_timeval_t tmo,interval;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
165 char *dir;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
166
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
167 if (argc > 1)
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
168 dir = argv[1];
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
169 else
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
170 dir ="/tmp";
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
171 myApp = MBApp_Init("browser");
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
172 MBApp_setData(myApp,&data);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
173 MyApp_InitContent(dir);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
174
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
175 MBApp_loop(myApp);
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
176
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
177 return 0;
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
178 }
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
179
4dfb850dee92 Add filebrowser program and SVG file.
wycc
parents:
diff changeset
180 /* vim: set ts=4 */