picture.c 6.9 KB

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