picture.c 6.5 KB

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