1
0

filelist_view.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * File : filelist_view.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-01-06 Bernard first version
  13. */
  14. #include <rtgui/rtgui_object.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <rtgui/rtgui_theme.h>
  17. #include <rtgui/list.h>
  18. #include <rtgui/image.h>
  19. #include <rtgui/widgets/view.h>
  20. #include <rtgui/widgets/workbench.h>
  21. #include <rtgui/widgets/filelist_view.h>
  22. #include <rtgui/widgets/listbox.h>
  23. #include <rtgui/widgets/window.h>
  24. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  25. #ifdef _WIN32
  26. #include <io.h>
  27. #include <dirent.h>
  28. #include <sys/stat.h>
  29. #define PATH_SEPARATOR '\\'
  30. #else
  31. #include <dfs_posix.h>
  32. #define PATH_SEPARATOR '/'
  33. #endif
  34. #include <string.h>
  35. #define RTGUI_FILELIST_MARGIN 5
  36. const static char * file_xpm[] = {
  37. "16 16 21 1",
  38. " c None",
  39. ". c #999999",
  40. "+ c #818181",
  41. "@ c #FFFFFF",
  42. "# c #ECECEC",
  43. "$ c #EAEAEA",
  44. "% c #EBEBEB",
  45. "& c #EDEDED",
  46. "* c #F0F0F0",
  47. "= c #C4C4C4",
  48. "- c #C5C5C5",
  49. "; c #C6C6C6",
  50. "> c #C7C7C7",
  51. ", c #EEEEEE",
  52. "' c #EDEDE5",
  53. ") c #EDEDE6",
  54. "! c #EFEFEF",
  55. "~ c #C8C8C8",
  56. "{ c #F1F1F1",
  57. "] c #F2F2F2",
  58. "^ c #959595",
  59. ".++++++++++++ ",
  60. "+@@@@@@@@@@@@+ ",
  61. "+@#$$%%%##&*@+ ",
  62. "+@$=--;;;;>*@+ ",
  63. "+@$%%###&&,*@+ ",
  64. "+@%-;;;;;;>*@+ ",
  65. "+@%%##&&'#,*@+ ",
  66. "+@%;;;;,,),*@+ ",
  67. "+@##&&,,!!!*@+ ",
  68. "+@#;;;>>~~~*@+ ",
  69. "+@#&,,!!*{{{@+ ",
  70. "+@&;>>~~~{{]@+ ",
  71. "+@&&,!!**{]]@+ ",
  72. "+@@@@@@@@@@@@+ ",
  73. "^++++++++++++^ ",
  74. " "};
  75. const static char * folder_xpm[] = {
  76. "16 16 121 2",
  77. " c None",
  78. ". c #D9B434",
  79. "+ c #E1C25E",
  80. "@ c #E2C360",
  81. "# c #E2C35F",
  82. "$ c #DBB63C",
  83. "% c #DAB336",
  84. "& c #FEFEFD",
  85. "* c #FFFFFE",
  86. "= c #FFFEFE",
  87. "- c #FFFEFD",
  88. "; c #FBF7EA",
  89. "> c #E4C76B",
  90. ", c #E3C76B",
  91. "' c #E6CD79",
  92. ") c #E5CA74",
  93. "! c #DAAF35",
  94. "~ c #FEFCF7",
  95. "{ c #F8E48E",
  96. "] c #F5DE91",
  97. "^ c #F5E09F",
  98. "/ c #F6E1AC",
  99. "( c #FEFBEF",
  100. "_ c #FEFDF4",
  101. ": c #FEFCF3",
  102. "< c #FEFCF1",
  103. "[ c #FEFBEE",
  104. "} c #FFFDFA",
  105. "| c #DAAF36",
  106. "1 c #DAAA36",
  107. "2 c #FDFAF1",
  108. "3 c #F5DE94",
  109. "4 c #F4DC93",
  110. "5 c #F2D581",
  111. "6 c #EDCA6A",
  112. "7 c #EACB6C",
  113. "8 c #EFD385",
  114. "9 c #EFD280",
  115. "0 c #EFD07A",
  116. "a c #EECF76",
  117. "b c #EECF72",
  118. "c c #FBF7E9",
  119. "d c #DAAE34",
  120. "e c #DAAB35",
  121. "f c #FBF6E8",
  122. "g c #EFD494",
  123. "h c #EECE88",
  124. "i c #E9C173",
  125. "j c #F6E9C9",
  126. "k c #FEFCF2",
  127. "l c #FEFCF0",
  128. "m c #DAAB36",
  129. "n c #DAA637",
  130. "o c #FFFDF8",
  131. "p c #FFFDF6",
  132. "q c #FFFCF5",
  133. "r c #FCF6D8",
  134. "s c #F8E694",
  135. "t c #F7E385",
  136. "u c #F6DF76",
  137. "v c #F5DB68",
  138. "w c #F4D85C",
  139. "x c #FCF4D7",
  140. "y c #DAA435",
  141. "z c #DAA136",
  142. "A c #FEFCF6",
  143. "B c #FCF2C8",
  144. "C c #FBEFB9",
  145. "D c #FAECAC",
  146. "E c #F9E89C",
  147. "F c #F7E38B",
  148. "G c #F6E07C",
  149. "H c #F6DC6C",
  150. "I c #F5D95D",
  151. "J c #F4D64F",
  152. "K c #F3D344",
  153. "L c #FCF3D0",
  154. "M c #DA9F35",
  155. "N c #DA9A36",
  156. "O c #FDFAF2",
  157. "P c #FAEDB3",
  158. "Q c #F9E9A4",
  159. "R c #F8E695",
  160. "S c #F7E285",
  161. "T c #F6DE76",
  162. "U c #F5DB65",
  163. "V c #F4D757",
  164. "W c #F3D449",
  165. "X c #F2D13B",
  166. "Y c #F1CE30",
  167. "Z c #FBF2CC",
  168. "` c #DA9835",
  169. " . c #DA9435",
  170. ".. c #FEFAEF",
  171. "+. c #F9E9A1",
  172. "@. c #F8E591",
  173. "#. c #F7E181",
  174. "$. c #F6DE72",
  175. "%. c #F5DA63",
  176. "&. c #F4D754",
  177. "*. c #F3D347",
  178. "=. c #F2D039",
  179. "-. c #F1CD2E",
  180. ";. c #F0CB26",
  181. ">. c #FBF2CA",
  182. ",. c #D98E33",
  183. "'. c #FAF0DC",
  184. "). c #F4DDA7",
  185. "!. c #F4DB9E",
  186. "~. c #F3DA96",
  187. "{. c #F3D88E",
  188. "]. c #F3D786",
  189. "^. c #F2D47F",
  190. "/. c #F2D379",
  191. "(. c #F1D272",
  192. "_. c #F1D06C",
  193. ":. c #F1CF69",
  194. "<. c #F8EAC2",
  195. "[. c #D8882D",
  196. "}. c #D8872D",
  197. "|. c #D8862C",
  198. " ",
  199. " ",
  200. " ",
  201. " . + @ @ @ # $ ",
  202. " % & * = - * ; > , , , ' ) ",
  203. " ! ~ { ] ^ / ( _ : < ( [ } | ",
  204. " 1 2 3 4 5 6 7 8 9 0 a b c d ",
  205. " e f g h i j k : k l ( [ * m ",
  206. " n * o p q : r s t u v w x y ",
  207. " z A B C D E F G H I J K L M ",
  208. " N O P Q R S T U V W X Y Z ` ",
  209. " ...+.@.#.$.%.&.*.=.-.;.>. . ",
  210. " ,.'.).!.~.{.].^./.(._.:.<.,. ",
  211. " [.}.[.[.[.[.[.[.[.[.}.[.|. ",
  212. " ",
  213. " "};
  214. /* image for file and folder */
  215. static rtgui_image_t *file_image, *folder_image;
  216. static struct rtgui_listbox_item items[] =
  217. {
  218. #ifdef RTGUI_USING_FONTHZ
  219. {"打开文件夹", RT_NULL},
  220. {"选择文件夹", RT_NULL},
  221. {"退出", RT_NULL}
  222. #else
  223. {"Open folder", RT_NULL},
  224. {"Select folder", RT_NULL},
  225. {"Cancel", RT_NULL}
  226. #endif
  227. };
  228. static void rtgui_filelist_view_clear(rtgui_filelist_view_t* view);
  229. static void rtgui_filelist_view_on_folder_item(rtgui_widget_t* widget, struct rtgui_event* event)
  230. {
  231. rtgui_win_t *menu;
  232. rtgui_listbox_t *listbox;
  233. rtgui_filelist_view_t *view;
  234. listbox = RTGUI_LISTBOX(widget);
  235. menu = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  236. view = RTGUI_FILELIST_VIEW(menu->user_data);
  237. /* hide window */
  238. rtgui_win_hiden(menu);
  239. switch (listbox->current_item)
  240. {
  241. case 0:
  242. {
  243. char* dir_ptr;
  244. /* destroy menu window */
  245. rtgui_win_destroy(menu);
  246. dir_ptr = (char*) rtgui_malloc (256);
  247. rtgui_filelist_view_get_fullpath(view, dir_ptr, 256);
  248. rtgui_filelist_view_set_directory(view, dir_ptr);
  249. rt_free(dir_ptr);
  250. }
  251. break;
  252. case 1:
  253. /* destroy menu window */
  254. rtgui_win_destroy(menu);
  255. if (RTGUI_VIEW(view)->modal_show == RT_TRUE)
  256. {
  257. rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_OK);
  258. }
  259. break;
  260. default:
  261. /* destroy menu window */
  262. rtgui_win_destroy(menu);
  263. break;
  264. }
  265. }
  266. static rt_bool_t rtgui_filelist_view_on_menu_deactivate(rtgui_widget_t* widget, struct rtgui_event* event)
  267. {
  268. rtgui_win_t *menu;
  269. menu = RTGUI_WIN(rtgui_widget_get_toplevel(widget));
  270. /* destroy menu window */
  271. rtgui_win_destroy(menu);
  272. return RT_TRUE;
  273. }
  274. static void rtgui_filelist_view_menu_pop(rtgui_widget_t *parent)
  275. {
  276. rtgui_win_t *menu;
  277. rtgui_listbox_t *listbox;
  278. rtgui_rect_t screen, rect = {0, 0, 140, 85};
  279. rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), &screen);
  280. rtgui_rect_moveto_align(&screen, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  281. menu = rtgui_win_create(RTGUI_TOPLEVEL(rtgui_widget_get_toplevel(parent)),
  282. "Folder Menu", &rect, RTGUI_WIN_STYLE_DEFAULT);
  283. if (menu != RT_NULL)
  284. {
  285. /* set user data on menu window */
  286. menu->user_data = (rt_uint32_t)parent;
  287. rtgui_win_set_ondeactivate(menu, rtgui_filelist_view_on_menu_deactivate);
  288. listbox = rtgui_listbox_create(items, sizeof(items)/sizeof(items[0]), &rect);
  289. rtgui_listbox_set_onitem(listbox, rtgui_filelist_view_on_folder_item);
  290. rtgui_container_add_child(RTGUI_CONTAINER(menu), RTGUI_WIDGET(listbox));
  291. rtgui_widget_focus(RTGUI_WIDGET(listbox));
  292. rtgui_win_show(menu, RT_FALSE);
  293. }
  294. }
  295. static void _rtgui_filelist_view_constructor(struct rtgui_filelist_view *view)
  296. {
  297. /* default rect */
  298. struct rtgui_rect rect = {0, 0, 200, 200};
  299. /* set default widget rect and set event handler */
  300. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), rtgui_filelist_view_event_handler);
  301. rtgui_widget_set_rect(RTGUI_WIDGET(view), &rect);
  302. RTGUI_WIDGET(view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  303. view->current_item = 0;
  304. view->items_count = 0;
  305. view->page_items = 0;
  306. view->current_directory = RT_NULL;
  307. view->pattern = RT_NULL;
  308. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = white;
  309. RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(view)) = RTGUI_ALIGN_CENTER_VERTICAL;
  310. file_image = rtgui_image_create_from_mem("xpm",
  311. (rt_uint8_t*)file_xpm, sizeof(file_xpm), RT_TRUE);
  312. folder_image = rtgui_image_create_from_mem("xpm",
  313. (rt_uint8_t*)folder_xpm, sizeof(folder_xpm), RT_TRUE);
  314. }
  315. static void _rtgui_filelist_view_destructor(struct rtgui_filelist_view *view)
  316. {
  317. /* delete all file items */
  318. rtgui_filelist_view_clear(view);
  319. /* delete current directory and pattern */
  320. rtgui_free(view->current_directory); view->current_directory = RT_NULL;
  321. rtgui_free(view->pattern); view->pattern = RT_NULL;
  322. /* delete image */
  323. rtgui_image_destroy(file_image);
  324. rtgui_image_destroy(folder_image);
  325. }
  326. rtgui_type_t *rtgui_filelist_view_type_get(void)
  327. {
  328. static rtgui_type_t *filelist_view_type = RT_NULL;
  329. if (!filelist_view_type)
  330. {
  331. filelist_view_type = rtgui_type_create("flview", RTGUI_VIEW_TYPE,
  332. sizeof(rtgui_filelist_view_t),
  333. RTGUI_CONSTRUCTOR(_rtgui_filelist_view_constructor),
  334. RTGUI_DESTRUCTOR(_rtgui_filelist_view_destructor));
  335. }
  336. return filelist_view_type;
  337. }
  338. void rtgui_filelist_view_ondraw(struct rtgui_filelist_view* view)
  339. {
  340. struct rtgui_dc* dc;
  341. rt_uint16_t page_index, index;
  342. struct rtgui_file_item* item;
  343. struct rtgui_rect rect, item_rect, image_rect;
  344. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view));
  345. if (dc == RT_NULL) return;
  346. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  347. rtgui_dc_fill_rect(dc, &rect);
  348. /* get item base rect */
  349. item_rect = rect;
  350. item_rect.y1 += 1;
  351. item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height());
  352. /* get image base rect */
  353. image_rect.x1 = RTGUI_FILELIST_MARGIN; image_rect.y1 = 0;
  354. image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; image_rect.y2 = file_image->h;
  355. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  356. /* get current page */
  357. page_index = (view->current_item / view->page_items) * view->page_items;
  358. for (index = 0; index < view->page_items; index ++)
  359. {
  360. if (page_index + index >= view->items_count) break;
  361. item = &(view->items[page_index + index]);
  362. if (page_index + index == view->current_item)
  363. {
  364. rtgui_theme_draw_selected(dc, &item_rect);
  365. }
  366. else
  367. {
  368. /* draw background */
  369. rtgui_dc_fill_rect(dc, &item_rect);
  370. }
  371. /* draw item */
  372. if (item->type == RTGUI_FITEM_FILE)
  373. rtgui_image_blit(file_image, dc, &image_rect);
  374. else
  375. rtgui_image_blit(folder_image, dc, &image_rect);
  376. /* draw text */
  377. item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2;
  378. rtgui_dc_draw_text(dc, item->name, &item_rect);
  379. item_rect.x1 -= RTGUI_FILELIST_MARGIN + file_image->w + 2;
  380. /* move to next item position */
  381. item_rect.y1 += (rtgui_theme_get_selected_height() + 1);
  382. item_rect.y2 += (rtgui_theme_get_selected_height() + 1);
  383. image_rect.y1 += (rtgui_theme_get_selected_height() + 1);
  384. image_rect.y2 += (rtgui_theme_get_selected_height() + 1);
  385. }
  386. rtgui_dc_end_drawing(dc);
  387. }
  388. void rtgui_filelist_view_update_current(struct rtgui_filelist_view* view, rt_uint16_t old_item)
  389. {
  390. struct rtgui_dc* dc;
  391. struct rtgui_file_item* item;
  392. rtgui_rect_t rect, item_rect, image_rect;
  393. if (old_item/view->page_items != view->current_item/view->page_items)
  394. {
  395. /* it's not a same page, update all */
  396. rtgui_widget_update(RTGUI_WIDGET(view));
  397. return;
  398. }
  399. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(view));
  400. if (dc == RT_NULL) return;
  401. rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect);
  402. /* get old item rect */
  403. item_rect = rect;
  404. item_rect.y1 += 1;
  405. item_rect.y1 += (old_item % view->page_items) * (1 + rtgui_theme_get_selected_height());
  406. item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height());
  407. /* get image rect */
  408. image_rect.x1 = RTGUI_FILELIST_MARGIN; image_rect.y1 = 0;
  409. image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; image_rect.y2 = file_image->h;
  410. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  411. /* draw old item */
  412. rtgui_dc_fill_rect(dc, &item_rect);
  413. item = &(view->items[old_item]);
  414. if (item->type == RTGUI_FITEM_FILE) /* draw item image */
  415. rtgui_image_blit(file_image, dc, &image_rect);
  416. else
  417. rtgui_image_blit(folder_image, dc, &image_rect);
  418. item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2;
  419. rtgui_dc_draw_text(dc, item->name, &item_rect);
  420. /* draw current item */
  421. item_rect = rect;
  422. item_rect.y1 += 1;
  423. item_rect.y1 += (view->current_item % view->page_items) * (1 + rtgui_theme_get_selected_height());
  424. item_rect.y2 = item_rect.y1 + (1 + rtgui_theme_get_selected_height());
  425. rtgui_theme_draw_selected(dc, &item_rect);
  426. /* get image base rect */
  427. image_rect.x1 = RTGUI_FILELIST_MARGIN; image_rect.y1 = 0;
  428. image_rect.x2 = RTGUI_FILELIST_MARGIN + file_image->w; image_rect.y2 = file_image->h;
  429. rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  430. item = &(view->items[view->current_item]);
  431. if (item->type == RTGUI_FITEM_FILE) /* draw item image */
  432. rtgui_image_blit(file_image, dc, &image_rect);
  433. else
  434. rtgui_image_blit(folder_image, dc, &image_rect);
  435. item_rect.x1 += RTGUI_FILELIST_MARGIN + file_image->w + 2;
  436. rtgui_dc_draw_text(dc, item->name, &item_rect);
  437. rtgui_dc_end_drawing(dc);
  438. }
  439. static void rtgui_filelist_view_onenturn(struct rtgui_filelist_view* view)
  440. {
  441. if (view->items[view->current_item].type == RTGUI_FITEM_DIR)
  442. {
  443. char new_path[64];
  444. if (strcmp(view->items[view->current_item].name, ".") == 0) return ;
  445. if (strcmp(view->items[view->current_item].name, "..") == 0)
  446. {
  447. char *ptr;
  448. ptr = strrchr(view->current_directory, PATH_SEPARATOR);
  449. if (ptr == RT_NULL) return ;
  450. if (ptr == &(view->current_directory[0]))
  451. {
  452. /* it's root directory */
  453. new_path[0] = PATH_SEPARATOR;
  454. new_path[1] = '\0';
  455. }
  456. else
  457. {
  458. strncpy(new_path, view->current_directory, ptr - view->current_directory + 1);
  459. new_path[ptr - view->current_directory] = '\0';
  460. }
  461. }
  462. else if (view->current_item == 0 &&
  463. #ifdef _WIN32
  464. (view->current_directory[1] == ':') && (view->current_directory[2] == '\\'))
  465. #else
  466. (view->current_directory[0] == '/') && (view->current_directory[1] == '\0'))
  467. #endif
  468. {
  469. if (RTGUI_VIEW(view)->modal_show == RT_TRUE)
  470. {
  471. rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_CANCEL);
  472. }
  473. else
  474. {
  475. rtgui_filelist_view_destroy(view);
  476. }
  477. return ;
  478. }
  479. else
  480. {
  481. rtgui_filelist_view_menu_pop(RTGUI_WIDGET(view));
  482. return ;
  483. }
  484. rtgui_filelist_view_set_directory(view, new_path);
  485. }
  486. else
  487. {
  488. if (RTGUI_VIEW(view)->modal_show == RT_TRUE)
  489. {
  490. rtgui_view_end_modal(RTGUI_VIEW(view), RTGUI_MODAL_OK);
  491. }
  492. }
  493. }
  494. rt_bool_t rtgui_filelist_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  495. {
  496. struct rtgui_filelist_view* view = RT_NULL;
  497. view = RTGUI_FILELIST_VIEW(widget);
  498. switch (event->type)
  499. {
  500. case RTGUI_EVENT_PAINT:
  501. rtgui_filelist_view_ondraw(view);
  502. return RT_FALSE;
  503. case RTGUI_EVENT_RESIZE:
  504. {
  505. struct rtgui_event_resize* resize;
  506. resize = (struct rtgui_event_resize*)event;
  507. /* recalculate page items */
  508. if (file_image != RT_NULL)
  509. view->page_items = resize->h / (1 + rtgui_theme_get_selected_height());
  510. else
  511. view->page_items = resize->h / (2 + 14);
  512. }
  513. break;
  514. case RTGUI_EVENT_MOUSE_BUTTON:
  515. {
  516. rtgui_rect_t rect;
  517. struct rtgui_event_mouse* emouse;
  518. emouse = (struct rtgui_event_mouse*)event;
  519. /* calculate selected item */
  520. /* get physical extent information */
  521. rtgui_widget_get_rect(widget, &rect);
  522. rtgui_widget_rect_to_device(widget, &rect);
  523. if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
  524. {
  525. rt_uint16_t index;
  526. rt_uint16_t current_page;
  527. rt_uint16_t old_item;
  528. index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height());
  529. /* get current page */
  530. current_page = view->current_item/view->page_items;
  531. old_item = view->current_item;
  532. if (index + current_page * view->page_items < view->items_count)
  533. {
  534. /* set selected item */
  535. view->current_item = index + current_page * view->page_items;
  536. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  537. {
  538. rtgui_filelist_view_update_current(view, old_item);
  539. }
  540. else
  541. {
  542. /* up event */
  543. rtgui_filelist_view_onenturn(view);
  544. }
  545. }
  546. return RT_TRUE;
  547. }
  548. }
  549. break;
  550. case RTGUI_EVENT_KBD:
  551. {
  552. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  553. if (ekbd->type == RTGUI_KEYDOWN)
  554. {
  555. rt_uint16_t old_item;
  556. old_item = view->current_item;
  557. switch (ekbd->key)
  558. {
  559. case RTGUIK_UP:
  560. if (view->current_item > 0)
  561. view->current_item --;
  562. rtgui_filelist_view_update_current(view, old_item);
  563. return RT_TRUE;
  564. case RTGUIK_DOWN:
  565. if (view->current_item < view->items_count - 1)
  566. view->current_item ++;
  567. rtgui_filelist_view_update_current(view, old_item);
  568. return RT_TRUE;
  569. case RTGUIK_LEFT:
  570. if (view->current_item - view->page_items >= 0)
  571. view->current_item -= view->page_items;
  572. rtgui_filelist_view_update_current(view, old_item);
  573. return RT_TRUE;
  574. case RTGUIK_RIGHT:
  575. if (view->current_item + view->page_items < view->items_count - 1)
  576. view->current_item += view->page_items;
  577. rtgui_filelist_view_update_current(view, old_item);
  578. return RT_TRUE;
  579. case RTGUIK_RETURN:
  580. rtgui_filelist_view_onenturn(view);
  581. return RT_TRUE;
  582. default:
  583. break;
  584. }
  585. }
  586. }
  587. return RT_FALSE;
  588. }
  589. /* use view event handler */
  590. return rtgui_view_event_handler(widget, event);
  591. }
  592. rtgui_filelist_view_t* rtgui_filelist_view_create(rtgui_workbench_t* workbench,
  593. const char* directory, const char* pattern, const rtgui_rect_t* rect)
  594. {
  595. struct rtgui_filelist_view* view = RT_NULL;
  596. /* create a new view */
  597. view = (struct rtgui_filelist_view*) rtgui_widget_create(RTGUI_FILELIST_VIEW_TYPE);
  598. if (view != RT_NULL)
  599. {
  600. view->items = RT_NULL;
  601. view->pattern = rt_strdup(pattern);
  602. view->page_items = rtgui_rect_height(*rect) / (1 + rtgui_theme_get_selected_height());
  603. rtgui_filelist_view_set_directory(view, directory);
  604. rtgui_workbench_add_view(workbench, RTGUI_VIEW(view));
  605. }
  606. return view;
  607. }
  608. void rtgui_filelist_view_destroy(rtgui_filelist_view_t* view)
  609. {
  610. /* destroy view */
  611. rtgui_widget_destroy(RTGUI_WIDGET(view));
  612. }
  613. /* clear all file items */
  614. static void rtgui_filelist_view_clear(rtgui_filelist_view_t* view)
  615. {
  616. rt_uint32_t index;
  617. struct rtgui_file_item* item;
  618. for (index = 0; index < view->items_count; index ++)
  619. {
  620. item = &(view->items[index]);
  621. /* release item name */
  622. rt_free(item->name);
  623. item->name = RT_NULL;
  624. }
  625. /* release items */
  626. rtgui_free(view->items);
  627. view->items = RT_NULL;
  628. view->items_count = 0;
  629. view->current_item = 0;
  630. }
  631. void rtgui_filelist_view_set_directory(rtgui_filelist_view_t* view, const char* directory)
  632. {
  633. struct rtgui_file_item *item;
  634. RT_ASSERT(view != RT_NULL);
  635. /* clear file items firstly */
  636. rtgui_filelist_view_clear(view);
  637. if (directory != RT_NULL)
  638. {
  639. DIR* dir;
  640. struct stat s;
  641. char* fullpath;
  642. rt_uint32_t index;
  643. struct dirent* dirent;
  644. view->items_count = 0;
  645. dir = opendir(directory);
  646. if (dir == RT_NULL) goto __return;
  647. /* current directory exists, set it */
  648. if (view->current_directory != RT_NULL) rt_free(view->current_directory);
  649. view->current_directory = rt_strdup(directory);
  650. do
  651. {
  652. dirent = readdir(dir);
  653. if (dirent == RT_NULL) break;
  654. if (strcmp(dirent->d_name, ".") == 0) continue;
  655. if (strcmp(dirent->d_name, "..") == 0) continue;
  656. view->items_count ++;
  657. } while (dirent != RT_NULL);
  658. closedir(dir);
  659. view->items_count ++; /* root directory for [x] exit, others for .. */
  660. view->items = (struct rtgui_file_item*) rtgui_malloc(sizeof(struct rtgui_file_item) * view->items_count);
  661. if (view->items == RT_NULL) return; /* no memory */
  662. index = 0;
  663. if (directory[0] == '/' && directory[1] != '\0')
  664. {
  665. item = &(view->items[0]);
  666. /* add .. directory */
  667. item->name = rt_strdup("..");
  668. item->type = RTGUI_FITEM_DIR;
  669. item->size = 0;
  670. index ++;
  671. }
  672. else
  673. {
  674. item = &(view->items[0]);
  675. /* add .. directory */
  676. item->name = rt_strdup("退出文件浏览");
  677. item->type = RTGUI_FITEM_DIR;
  678. item->size = 0;
  679. index ++;
  680. }
  681. /* reopen directory */
  682. dir = opendir(directory);
  683. fullpath = rtgui_malloc(256);
  684. while (index < view->items_count)
  685. {
  686. dirent = readdir(dir);
  687. if (dirent == RT_NULL) break;
  688. if (strcmp(dirent->d_name, ".") == 0) continue;
  689. if (strcmp(dirent->d_name, "..") == 0) continue;
  690. item = &(view->items[index]);
  691. item->name = rt_strdup(dirent->d_name);
  692. rt_memset(&s, 0, sizeof(struct stat));
  693. /* build full path for the file */
  694. if (directory[strlen(directory) - 1] != PATH_SEPARATOR)
  695. rt_snprintf(fullpath, 256, "%s%c%s", directory, PATH_SEPARATOR, dirent->d_name);
  696. else
  697. rt_snprintf(fullpath, 256, "%s%s", directory, dirent->d_name);
  698. stat(fullpath, &s);
  699. if ( s.st_mode & S_IFDIR )
  700. {
  701. item->type = RTGUI_FITEM_DIR;
  702. item->size = 0;
  703. }
  704. else
  705. {
  706. item->type = RTGUI_FITEM_FILE;
  707. item->size = s.st_size;
  708. }
  709. index ++;
  710. }
  711. rtgui_free(fullpath);
  712. closedir(dir);
  713. }
  714. view->current_item = 0;
  715. __return:
  716. /* update view */
  717. rtgui_widget_update(RTGUI_WIDGET(view));
  718. }
  719. void rtgui_filelist_view_get_fullpath(rtgui_filelist_view_t* view, char* path, rt_size_t len)
  720. {
  721. RT_ASSERT(view != RT_NULL);
  722. if (view->current_directory[strlen(view->current_directory) - 1] != PATH_SEPARATOR)
  723. rt_snprintf(path, len, "%s%c%s",view->current_directory, PATH_SEPARATOR,
  724. view->items[view->current_item].name);
  725. else
  726. rt_snprintf(path, len, "%s%s",view->current_directory,
  727. view->items[view->current_item].name);
  728. }
  729. #endif