picture.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/image.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/window.h>
  5. #include <rtgui/rtgui_app.h>
  6. #include <dfs_posix.h>
  7. #include <string.h>
  8. #define PICTURE_DIR "/picture"
  9. /* current picture file name */
  10. rt_bool_t key_pressed = RT_FALSE;
  11. static char current_fn[32] = {0};
  12. static struct rtgui_win *win;
  13. static void picture_show_prev(struct rtgui_widget* widget)
  14. {
  15. DIR* dir;
  16. struct dirent* entry;
  17. rt_bool_t is_last;
  18. char fn[32];
  19. struct rtgui_image_engine* engine;
  20. fn[0] = '\0';
  21. is_last = RT_FALSE;
  22. dir = opendir(PICTURE_DIR);
  23. if (dir == RT_NULL)
  24. {
  25. rt_kprintf("open directory failed\n");
  26. return;
  27. }
  28. do
  29. {
  30. entry = readdir(dir);
  31. if (entry != RT_NULL)
  32. {
  33. engine = rtgui_image_get_engine_by_filename(entry->d_name);
  34. if (engine != RT_NULL)
  35. {
  36. /* it's a HDC image */
  37. if ((strcmp(entry->d_name, current_fn) == 0) &&
  38. is_last != RT_TRUE)
  39. {
  40. if (fn[0] == '\0')
  41. {
  42. /* it should be the last image */
  43. is_last = RT_TRUE;
  44. }
  45. else
  46. {
  47. /* display image */
  48. strcpy(current_fn, fn);
  49. rtgui_widget_update(widget);
  50. closedir(dir);
  51. return;
  52. }
  53. }
  54. strcpy(fn, entry->d_name);
  55. }
  56. }
  57. } while(entry != RT_NULL);
  58. /* close directory */
  59. closedir(dir);
  60. if ((is_last == RT_TRUE) && fn[0] != '\0')
  61. {
  62. strcpy(current_fn, fn);
  63. rtgui_widget_update(widget);
  64. }
  65. }
  66. static void picture_show_next(struct rtgui_widget* widget)
  67. {
  68. DIR* dir;
  69. struct dirent* entry;
  70. rt_bool_t found, has_image;
  71. struct rtgui_image_engine* engine;
  72. found = RT_FALSE; has_image = RT_FALSE;
  73. __restart:
  74. dir = opendir(PICTURE_DIR);
  75. if (dir == RT_NULL)
  76. {
  77. rt_kprintf("open directory failed\n");
  78. return;
  79. }
  80. do
  81. {
  82. entry = readdir(dir);
  83. if (entry != RT_NULL)
  84. {
  85. engine = rtgui_image_get_engine_by_filename(entry->d_name);
  86. if (engine != RT_NULL)
  87. {
  88. /* this directory includes image */
  89. has_image = RT_TRUE;
  90. if (found == RT_TRUE || current_fn[0] == '\0')
  91. {
  92. strcpy(current_fn, entry->d_name);
  93. rtgui_widget_update(widget);
  94. closedir(dir);
  95. return;
  96. }
  97. /* it's a HDC image */
  98. if (strcmp(entry->d_name, current_fn) == 0)
  99. found = RT_TRUE;
  100. }
  101. }
  102. } while(entry != RT_NULL);
  103. /* close directory */
  104. closedir(dir);
  105. if (has_image != RT_TRUE) return;
  106. current_fn[0] = '\0';
  107. goto __restart;
  108. }
  109. static rt_bool_t onkey_handle(struct rtgui_object* object, struct rtgui_event* event)
  110. {
  111. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  112. if (ekbd->type == RTGUI_KEYDOWN)
  113. {
  114. if (ekbd->key == RTGUIK_RIGHT)
  115. {
  116. key_pressed = RT_TRUE;
  117. picture_show_next(RTGUI_WIDGET(object));
  118. return RT_TRUE;
  119. }
  120. else if (ekbd->key == RTGUIK_LEFT)
  121. {
  122. key_pressed = RT_TRUE;
  123. picture_show_prev(RTGUI_WIDGET(object));
  124. return RT_TRUE;
  125. }
  126. }
  127. return RT_TRUE;
  128. }
  129. static rt_bool_t picture_view_event_handler(rtgui_object_t *object, rtgui_event_t *event)
  130. {
  131. if (event->type == RTGUI_EVENT_PAINT)
  132. {
  133. struct rtgui_dc* dc;
  134. struct rtgui_rect rect;
  135. struct rtgui_image* image = RT_NULL;
  136. char fn[32];
  137. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object));
  138. if (dc == RT_NULL) return RT_FALSE;
  139. rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect);
  140. /* open image */
  141. rt_snprintf(fn, sizeof(fn), "%s/%s", PICTURE_DIR, current_fn);
  142. rt_kprintf("pic fn: %s\n", fn);
  143. image = rtgui_image_create(fn, RT_FALSE);
  144. if (image != RT_NULL)
  145. {
  146. /* blit image */
  147. rtgui_image_blit(image, dc, &rect);
  148. /* destroy image */
  149. rtgui_image_destroy(image);
  150. }
  151. else
  152. {
  153. rtgui_dc_fill_rect(dc, &rect);
  154. rtgui_dc_draw_text(dc, "ĂťÓĐÎÄźţąť´ňżŞ", &rect);
  155. }
  156. rtgui_dc_end_drawing(dc);
  157. return RT_FALSE;
  158. }
  159. return rtgui_win_event_handler(object, event);
  160. }
  161. static void timeout(struct rtgui_timer* timer, void* parameter)
  162. {
  163. struct rtgui_widget* widget;
  164. widget = (struct rtgui_widget*)parameter;
  165. if (key_pressed == RT_TRUE)
  166. key_pressed = RT_FALSE;
  167. else
  168. picture_show_next(widget);
  169. }
  170. void picture_show(void* parameter)
  171. {
  172. /* create application */
  173. struct rtgui_app *app;
  174. struct rtgui_rect rect1;
  175. rtgui_timer_t *timer;
  176. app = rtgui_app_create(rt_thread_self(), "picture");
  177. if (app == RT_NULL)
  178. {
  179. rt_kprintf("Create application \"picture\" failed!\n");
  180. return;
  181. }
  182. rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &rect1);
  183. /* create main window */
  184. win = rtgui_mainwin_create(RT_NULL, "main",
  185. RTGUI_WIN_STYLE_NO_BORDER | RTGUI_WIN_STYLE_NO_TITLE);
  186. if (win == RT_NULL)
  187. {
  188. rt_kprintf("Create window \"main\" failed!\n");
  189. rtgui_app_destroy(app);
  190. return;
  191. }
  192. timer = rtgui_timer_create(500, RT_TIMER_FLAG_PERIODIC, timeout, (void*)win);
  193. rtgui_timer_start(timer);
  194. rtgui_object_set_event_handler(RTGUI_OBJECT(win), picture_view_event_handler);
  195. rtgui_win_set_onkey(win, onkey_handle);
  196. rtgui_win_show(win, RT_FALSE);
  197. /* show next picture */
  198. picture_show_next(RTGUI_WIDGET(win));
  199. rtgui_app_run(app);
  200. rtgui_app_destroy(app);
  201. }
  202. void picture_app_create(void)
  203. {
  204. rt_thread_t tid;
  205. tid = rt_thread_create("pic", picture_show, RT_NULL,
  206. 2048, 20, 8);
  207. if (tid != RT_NULL) rt_thread_startup(tid);
  208. }