-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicture.c
283 lines (239 loc) · 6.26 KB
/
picture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <rtgui/rtgui.h>
#include <rtgui/image.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/view.h>
#include <rtgui/widgets/workbench.h>
#include <dfs_posix.h>
#include <string.h>
#define PICTURE_DIR "/"
enum picture_view_mode
{
VIEW_SINGLE_MODE,
VIEW_DIR_MODE,
VIEW_FN_LIST_MODE
};
static rtgui_view_t* picture_view = RT_NULL;
static enum picture_view_mode view_mode = VIEW_SINGLE_MODE;
/* current picture file name */
static char current_fn[32] = {0};
static const char** picture_fn_list;
static rt_uint8_t picture_fn_list_size, picture_fn_list_current;
static void picture_show_prev()
{
DIR* dir;
struct dirent* entry;
rt_bool_t is_last;
char fn[32];
fn[0] = '\0';
is_last = RT_FALSE;
dir = opendir(PICTURE_DIR);
if (dir == RT_NULL)
{
rt_kprintf("open directory failed\n");
return;
}
do
{
entry = readdir(dir);
if (entry != RT_NULL)
{
if (strstr(entry->d_name, ".hdc") != RT_NULL ||
strstr(entry->d_name, ".HDC") != RT_NULL)
{
/* it's a HDC image */
if ((strcmp(entry->d_name, current_fn) == 0) &&
is_last != RT_TRUE)
{
if (fn[0] == '\0')
{
/* it should be the last image */
is_last = RT_TRUE;
}
else
{
/* display image */
strcpy(current_fn, fn);
rtgui_widget_update(RTGUI_WIDGET(picture_view));
closedir(dir);
return;
}
}
strcpy(fn, entry->d_name);
}
}
} while(entry != RT_NULL);
/* close directory */
closedir(dir);
if ((is_last == RT_TRUE) && fn[0] != '\0')
{
strcpy(current_fn, fn);
rtgui_widget_update(RTGUI_WIDGET(picture_view));
}
}
static void picture_show_next()
{
DIR* dir;
struct dirent* entry;
rt_bool_t found, has_image;
found = RT_FALSE; has_image = RT_FALSE;
__restart:
dir = opendir(PICTURE_DIR);
if (dir == RT_NULL)
{
rt_kprintf("open directory failed\n");
return;
}
do
{
entry = readdir(dir);
if (entry != RT_NULL)
{
if (strstr(entry->d_name, ".hdc") != RT_NULL ||
strstr(entry->d_name, ".HDC") != RT_NULL)
{
/* this directory includes image */
has_image = RT_TRUE;
if (found == RT_TRUE || current_fn[0] == '\0')
{
strcpy(current_fn, entry->d_name);
rtgui_widget_update(RTGUI_WIDGET(picture_view));
closedir(dir);
return;
}
/* it's a HDC image */
if (strcmp(entry->d_name, current_fn) == 0)
found = RT_TRUE;
}
}
} while(entry != RT_NULL);
/* close directory */
closedir(dir);
if (has_image != RT_TRUE) return;
current_fn[0] = '\0';
goto __restart;
}
static rt_bool_t picture_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
if (event->type == RTGUI_EVENT_PAINT)
{
struct rtgui_dc* dc;
struct rtgui_rect rect;
struct rtgui_image* image;
char fn[32];
dc = rtgui_dc_begin_drawing(widget);
if (dc == RT_NULL) return RT_FALSE;
rtgui_widget_get_rect(widget, &rect);
/* open image */
rt_snprintf(fn, sizeof(fn), "%s/%s", PICTURE_DIR, current_fn);
image = rtgui_image_create_from_file("hdc",
fn, RT_FALSE);
if (image != RT_NULL)
{
/* blit image */
rtgui_image_blit(image, dc, &rect);
/* destroy image */
rtgui_image_destroy(image);
}
else
{
rtgui_dc_fill_rect(dc, &rect);
rtgui_dc_draw_text(dc, "ûÓÐÎļþ±»´ò¿ª", &rect);
}
rtgui_dc_end_drawing(dc);
return RT_FALSE;
}
else if (event->type == RTGUI_EVENT_KBD)
{
struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
if (ekbd->type == RTGUI_KEYDOWN)
{
switch (ekbd->key)
{
case RTGUIK_RIGHT:
if (view_mode == VIEW_DIR_MODE) picture_show_next();
else if (view_mode == VIEW_FN_LIST_MODE)
{
picture_fn_list_current ++;
if (picture_fn_list_current == picture_fn_list_size)
{
picture_fn_list_current = 0;
}
strcpy(current_fn, picture_fn_list[picture_fn_list_current]);
rtgui_widget_update(RTGUI_WIDGET(picture_view));
}
break;
case RTGUIK_LEFT:
if (view_mode == VIEW_DIR_MODE) picture_show_prev();
else if (view_mode == VIEW_FN_LIST_MODE)
{
if (picture_fn_list_current == 0)
{
picture_fn_list_current = picture_fn_list_size - 1;
}
else picture_fn_list_current --;
strcpy(current_fn, picture_fn_list[picture_fn_list_current]);
rtgui_widget_update(RTGUI_WIDGET(picture_view));
}
break;
case RTGUIK_RETURN:
{
rtgui_view_t* view;
view = RTGUI_VIEW(widget);
/* close this view */
current_fn[0] = '\0';
/* end of modal */
rtgui_view_end_modal(view, RTGUI_MODAL_OK);
picture_view = RT_NULL;
}
break;
}
}
return RT_FALSE;
}
return rtgui_view_event_handler(widget, event);
}
rtgui_view_t *picture_view_create(struct rtgui_workbench* workbench)
{
/* create picture view */
picture_view = rtgui_view_create("Picture Presentation");
rtgui_widget_set_event_handler(RTGUI_WIDGET(picture_view),
picture_view_event_handler);
rtgui_workbench_add_view(workbench, picture_view);
/* this view can be focused */
RTGUI_WIDGET(picture_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
/* show next picture */
picture_show_next();
view_mode = VIEW_DIR_MODE;
return picture_view;
}
rtgui_view_t *picture_view_create_view_file(struct rtgui_workbench* workbench,
const char* filename)
{
strcpy(current_fn, filename);
/* create picture view */
picture_view = rtgui_view_create("Picture Presentation");
rtgui_widget_set_event_handler(RTGUI_WIDGET(picture_view),
picture_view_event_handler);
rtgui_workbench_add_view(workbench, picture_view);
/* this view can be focused */
RTGUI_WIDGET(picture_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
view_mode = VIEW_SINGLE_MODE;
return picture_view;
}
rtgui_view_t *picture_view_create_view_list(struct rtgui_workbench* workbench,
const char* list[], rt_uint8_t size)
{
picture_fn_list = list;
picture_fn_list_size = size;
picture_fn_list_current = 0;
strcpy(current_fn, picture_fn_list[picture_fn_list_current]);
/* create picture view */
picture_view = rtgui_view_create("Picture Presentation");
rtgui_widget_set_event_handler(RTGUI_WIDGET(picture_view),
picture_view_event_handler);
rtgui_workbench_add_view(workbench, picture_view);
/* this view can be focused */
RTGUI_WIDGET(picture_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
view_mode = VIEW_FN_LIST_MODE;
return picture_view;
}