edit.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * File : edit.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. * 2012-06-04 amsl firist version.
  13. * 2012-08-09 amsl beta 0.1
  14. */
  15. #include <rtgui/dc.h>
  16. #include <rtgui/widgets/edit.h>
  17. #include <rtgui/widgets/scrollbar.h>
  18. #include <rtgui/rtgui_system.h>
  19. static void rtgui_edit_draw_caret(struct rtgui_edit *edit);
  20. static void rtgui_edit_timeout(struct rtgui_timer* timer, void* parameter);
  21. static rt_bool_t rtgui_edit_onfocus(struct rtgui_object* object, rtgui_event_t* event);
  22. static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object* object, rtgui_event_t* event);
  23. static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event);
  24. static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event);
  25. void _rtgui_edit_constructor(struct rtgui_edit *edit)
  26. {
  27. rtgui_rect_t font_rect;
  28. RTGUI_WIDGET_FLAG(edit) |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  29. rtgui_object_set_event_handler(RTGUI_OBJECT(edit), rtgui_edit_event_handler);
  30. rtgui_widget_set_onfocus(RTGUI_WIDGET(edit), rtgui_edit_onfocus);
  31. rtgui_widget_set_onunfocus(RTGUI_WIDGET(edit), rtgui_edit_onunfocus);
  32. RTGUI_WIDGET_FOREGROUND(edit) = black;
  33. RTGUI_WIDGET_BACKGROUND(edit) = white;
  34. /* set default text align */
  35. RTGUI_WIDGET_TEXTALIGN(edit) = RTGUI_ALIGN_CENTER_VERTICAL;
  36. rtgui_widget_set_border(RTGUI_WIDGET(edit), RTGUI_BORDER_SUNKEN);
  37. /* set proper of control */
  38. edit->caret_timer = RT_NULL;
  39. edit->caret = RT_NULL;
  40. edit->tabsize = 4;
  41. edit->margin = 1;
  42. edit->max_rows = edit->max_cols = 0;
  43. edit->visual.x = edit->visual.y = 0;
  44. edit->upleft.x = edit->upleft.y = 0;
  45. edit->row_per_page = edit->col_per_page = 0;
  46. edit->update_buf = RT_NULL;
  47. edit->flag = RTGUI_EDIT_NONE;
  48. #ifdef RTGUI_EDIT_USING_SCROLL
  49. edit->flag |= RTGUI_EDIT_VSCROLL;
  50. edit->flag |= RTGUI_EDIT_HSCROLL;
  51. #endif
  52. /* allocate default line buffer */
  53. edit->bzsize = 16;
  54. rtgui_font_get_metrics(RTGUI_WIDGET_FONT(edit), "H", &font_rect);
  55. edit->font_width = rtgui_rect_width(font_rect);
  56. edit->font_height = rtgui_rect_height(font_rect);
  57. edit->head = RT_NULL;
  58. edit->tail = RT_NULL;
  59. edit->first_line = RT_NULL;
  60. #ifdef RTGUI_EDIT_USING_SCROLL
  61. edit->hscroll = RT_NULL;
  62. edit->vscroll = RT_NULL;
  63. #endif
  64. }
  65. void _rtgui_edit_deconstructor(struct rtgui_edit *edit)
  66. {
  67. if(edit->max_rows > 0)
  68. {
  69. while(edit->max_rows > 0)
  70. rtgui_edit_delete_line(edit, edit->head);
  71. edit->max_rows = 0;
  72. }
  73. if(edit->caret_timer != RT_NULL)
  74. rtgui_timer_destory(edit->caret_timer);
  75. edit->caret_timer = RT_NULL;
  76. if(edit->caret != RT_NULL)
  77. rtgui_free(edit->caret);
  78. edit->caret = RT_NULL;
  79. if(edit->update_buf != RT_NULL)
  80. rtgui_free(edit->update_buf);
  81. }
  82. DEFINE_CLASS_TYPE(edit, "edit",
  83. RTGUI_CONTAINER_TYPE,
  84. _rtgui_edit_constructor,
  85. _rtgui_edit_deconstructor,
  86. sizeof(struct rtgui_edit));
  87. #ifdef RTGUI_EDIT_USING_SCROLL
  88. void rtgui_edit_adjust_scroll(rtgui_scrollbar_t *bar)
  89. {
  90. struct rtgui_edit *edit;
  91. RT_ASSERT(bar != RT_NULL);
  92. if(bar->widget_link != RT_NULL)
  93. {
  94. rtgui_rect_t rect;
  95. rt_uint32_t _left=0,_top=0,_width=RTGUI_DEFAULT_SB_WIDTH,_len=0;
  96. edit = bar->widget_link;
  97. rtgui_widget_get_rect(edit, &rect);
  98. rtgui_widget_rect_to_device(edit,&rect);
  99. if(bar->orient==RTGUI_HORIZONTAL)
  100. {
  101. if(RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  102. {
  103. if(edit->max_rows > edit->row_per_page)
  104. {
  105. RTGUI_WIDGET_SHOW(edit->hscroll);
  106. rtgui_scrollbar_set_line_step(edit->hscroll, 1);
  107. rtgui_scrollbar_set_page_step(edit->hscroll, edit->row_per_page);
  108. rtgui_scrollbar_set_range(edit->hscroll, edit->max_rows);
  109. }
  110. else
  111. RTGUI_WIDGET_HIDE(edit->vscroll);
  112. rtgui_widget_update_clip(RTGUI_WIDGET(edit));
  113. }
  114. else
  115. {
  116. _left = RTGUI_WIDGET_BORDER(edit);
  117. _top = rtgui_rect_height(rect)-RTGUI_WIDGET_BORDER(edit)-_width;
  118. _len = rtgui_rect_width(rect)-RTGUI_WIDGET_BORDER(edit)*2;
  119. if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  120. _len -= _width;
  121. rect.x1 += _left;
  122. rect.y1 += _top;
  123. rect.x2 = rect.x1+_len;
  124. rect.y2 = rect.y1+_width;
  125. }
  126. }
  127. else if(bar->orient==RTGUI_VERTICAL)
  128. {
  129. _left = rtgui_rect_width(rect)-RTGUI_WIDGET_BORDER(edit)-_width;
  130. _top = RTGUI_WIDGET_BORDER(edit);
  131. _len = rtgui_rect_height(rect)-RTGUI_WIDGET_BORDER(edit)*2;
  132. if(!RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  133. _len -= _width;
  134. rect.x1 += _left;
  135. rect.y1 += _top;
  136. rect.x2 = rect.x1+_width;
  137. rect.y2 = rect.y1+_len;
  138. }
  139. rtgui_widget_set_rect(bar,&rect);
  140. }
  141. }
  142. #endif
  143. struct rtgui_edit* rtgui_edit_create(struct rtgui_container* container, int left, int top, int w, int h)
  144. {
  145. struct rtgui_edit* edit;
  146. RT_ASSERT(container != RT_NULL);
  147. edit = (struct rtgui_edit*)rtgui_widget_create(RTGUI_EDIT_TYPE);
  148. if(edit != RT_NULL)
  149. {
  150. rtgui_rect_t rect;
  151. int effe;
  152. rtgui_widget_get_rect(RTGUI_WIDGET(container), &rect);
  153. rtgui_widget_rect_to_device(RTGUI_WIDGET(container),&rect);
  154. rect.x1 += left;
  155. rect.y1 += top;
  156. rect.x2 = rect.x1+w;
  157. rect.y2 = rect.y1+h;
  158. rtgui_widget_set_rect(RTGUI_WIDGET(edit),&rect);
  159. rtgui_container_add_child(container, RTGUI_WIDGET(edit));
  160. /* set character number */
  161. edit->item_height = edit->font_height; /* the same height */
  162. effe = h-(edit->margin + RTGUI_WIDGET_BORDER(edit))*2;
  163. edit->row_per_page = effe / edit->item_height;
  164. if(effe % edit->item_height)
  165. edit->row_per_page += 1;
  166. effe = w-(edit->margin + RTGUI_WIDGET_BORDER(edit))*2;
  167. edit->col_per_page = effe / edit->font_width;
  168. if(effe % edit->font_width)
  169. edit->col_per_page += 1;
  170. edit->update_buf = rtgui_malloc(edit->col_per_page + 1);
  171. #ifdef RTGUI_EDIT_USING_SCROLL
  172. if(edit->hscroll == RT_NULL && edit->flag & RTGUI_EDIT_HSCROLL)
  173. {
  174. /* create horizontal scrollbar */
  175. rt_uint32_t _left,_top,_width=RTGUI_DEFAULT_SB_WIDTH,_len;
  176. _left = RTGUI_WIDGET_BORDER(edit);
  177. _top = rtgui_rect_height(rect)-RTGUI_WIDGET_BORDER(edit)-_width;
  178. _len = rtgui_rect_width(rect)-RTGUI_WIDGET_BORDER(edit)*2;
  179. if(edit->max_rows > edit->row_per_page) _len -= _width;
  180. edit->hscroll = rtgui_scrollbar_create(edit,_left,_top,_width,_len,RTGUI_HORIZONTAL);
  181. if(edit->hscroll != RT_NULL)
  182. {
  183. edit->hscroll->widget_link = (pvoid)edit;
  184. edit->hscroll->on_scroll = rtgui_edit_hscroll_handle;
  185. RTGUI_WIDGET_HIDE(edit->hscroll);
  186. }
  187. }
  188. if(edit->vscroll == RT_NULL && edit->flag & RTGUI_EDIT_VSCROLL)
  189. {
  190. /* create vertical scrollbar */
  191. rt_uint32_t _left,_top,_width=RTGUI_DEFAULT_SB_WIDTH,_len;
  192. _left = rtgui_rect_width(rect)-RTGUI_WIDGET_BORDER(edit)-_width;
  193. _top = RTGUI_WIDGET_BORDER(edit);
  194. _len = rtgui_rect_height(rect)-RTGUI_WIDGET_BORDER(edit)*2;
  195. if(edit->max_cols > edit->col_per_page) _len -= _width;
  196. edit->vscroll = rtgui_scrollbar_create(edit,_left,_top,_width,_len,RTGUI_VERTICAL);
  197. if(edit->vscroll != RT_NULL)
  198. {
  199. edit->vscroll->widget_link = (pvoid)edit;
  200. edit->vscroll->on_scroll = rtgui_edit_vscroll_handle;
  201. RTGUI_WIDGET_HIDE(edit->vscroll);
  202. }
  203. }
  204. #endif
  205. }
  206. return edit;
  207. }
  208. void rtgui_edit_destroy(struct rtgui_edit* edit)
  209. {
  210. rtgui_widget_destroy(RTGUI_WIDGET(edit));
  211. }
  212. /**
  213. * calc line buffer alloc length
  214. *
  215. * @param n a standard buffer value, please use edit->bzsize
  216. * @param m given a reference value
  217. *
  218. * @return get a proper standard values
  219. */
  220. rt_inline rt_size_t rtgui_edit_alloc_len(rt_size_t n, rt_size_t m)
  221. {
  222. if(n > m) return n;
  223. #ifndef RTGUI_USING_SMALL_SIZE
  224. return rtgui_edit_alloc_len(n*2, m);
  225. #else
  226. return rtgui_edit_alloc_len(n+16, m);
  227. #endif
  228. }
  229. /**
  230. * please use it to replace rt_strlen
  231. * especially in reading the source file.
  232. */
  233. rt_inline rt_size_t rtgui_edit_line_strlen(const char *s)
  234. {
  235. const char *sc;
  236. /* ascii text end of 0x0A or 0x0D-0x0A*/
  237. for(sc = s; *sc != 0x0D && *sc != 0x0A && *sc != 0x00; ++sc);
  238. return sc - s;
  239. }
  240. rt_bool_t rtgui_edit_append_line(struct rtgui_edit* edit, const char *text)
  241. {
  242. rt_size_t len;
  243. struct edit_line *line, *node;
  244. RT_ASSERT(edit != RT_NULL);
  245. line = rtgui_malloc(sizeof(struct edit_line));
  246. if(line == RT_NULL) return RT_FALSE;
  247. len = rtgui_edit_line_strlen(text);
  248. line->zsize = rtgui_edit_alloc_len(edit->bzsize, len+1);
  249. line->text = rtgui_malloc(line->zsize);
  250. rt_memcpy(line->text, text, len);
  251. *(line->text+len) = '\0';
  252. line->len = rtgui_edit_line_strlen(line->text);
  253. line->next = RT_NULL;
  254. edit->max_rows++;
  255. if(edit->max_cols < len) edit->max_cols = len;
  256. node = edit->head;
  257. if(node == RT_NULL)
  258. {
  259. edit->head = line;
  260. edit->tail = line;
  261. line->prev = RT_NULL;
  262. return RT_TRUE;
  263. }
  264. while(node->next != RT_NULL) node = node->next;
  265. /* to tail item on to queue */
  266. node->next = line;
  267. line->prev = node;
  268. /* re-fixed position tail */
  269. edit->tail = line;
  270. return RT_TRUE;
  271. }
  272. rt_bool_t rtgui_edit_insert_line(struct rtgui_edit *edit, struct edit_line *p, char *text)
  273. {
  274. rt_size_t len;
  275. struct edit_line *line;
  276. RT_ASSERT(edit != RT_NULL);
  277. RT_ASSERT(p != RT_NULL);
  278. if(p->next == RT_NULL)
  279. {
  280. rtgui_edit_append_line(edit, text);
  281. return RT_TRUE;
  282. }
  283. line = rtgui_malloc(sizeof(struct edit_line));
  284. if(line == RT_NULL) return RT_FALSE;
  285. line->prev = p;
  286. line->next = p->next;
  287. p->next = line;
  288. if(line->next != RT_NULL)
  289. {
  290. line->next->prev = line;
  291. }
  292. len = rtgui_edit_line_strlen(text);
  293. line->zsize = rtgui_edit_alloc_len(edit->bzsize, len+1);
  294. line->text = rtgui_malloc(line->zsize);
  295. rt_memset(line->text, 0, line->zsize);
  296. rt_memcpy(line->text, text, len);
  297. *(line->text+len) = '\0';
  298. edit->max_rows ++;
  299. line->len = rtgui_edit_line_strlen(line->text);
  300. return RT_TRUE;
  301. }
  302. rt_bool_t rtgui_edit_delete_line(struct rtgui_edit* edit, struct edit_line *line)
  303. {
  304. RT_ASSERT(edit != RT_NULL);
  305. RT_ASSERT(line != RT_NULL);
  306. if(edit->max_rows == 0) return RT_FALSE;
  307. if(line->prev == RT_NULL)
  308. {
  309. if(line->next == RT_NULL)
  310. {
  311. /* only one item */
  312. edit->head = RT_NULL;
  313. edit->tail = RT_NULL;
  314. }
  315. else
  316. {
  317. /* first item */
  318. line->next->prev = RT_NULL;
  319. edit->head = line->next;
  320. }
  321. }
  322. else
  323. {
  324. if(line->next == RT_NULL)
  325. {
  326. /* last item */
  327. line->prev->next = RT_NULL;
  328. edit->tail = line->prev;
  329. }
  330. else
  331. {
  332. /* middle item */
  333. line->prev->next = line->next;
  334. line->next->prev = line->prev;
  335. }
  336. }
  337. if(edit->max_rows > 0)edit->max_rows--;
  338. if(line->text)
  339. {
  340. rtgui_free(line->text);
  341. line->text = RT_NULL;
  342. }
  343. rtgui_free(line);
  344. line = RT_NULL;
  345. return RT_TRUE;
  346. }
  347. rt_bool_t rtgui_edit_connect_line(struct rtgui_edit* edit, struct edit_line *line, struct edit_line *connect)
  348. {
  349. rt_size_t len1,len2;
  350. RT_ASSERT(edit != RT_NULL);
  351. RT_ASSERT(line != RT_NULL);
  352. RT_ASSERT(connect != RT_NULL);
  353. len1 = rtgui_edit_line_strlen(line->text);
  354. len2 = rtgui_edit_line_strlen(connect->text);
  355. line->zsize = rtgui_edit_alloc_len(edit->bzsize, len1+len2+1);
  356. line->text = rt_realloc(line->text, line->zsize);
  357. rt_memcpy(line->text+len1, connect->text, len2);
  358. *(line->text+len1+len2) = '\0';
  359. line->len = rtgui_edit_line_strlen(line->text);
  360. return RT_TRUE;
  361. }
  362. static void rtgui_edit_get_caret_rect(struct rtgui_edit *edit, rtgui_rect_t *rect, rtgui_point_t visual)
  363. {
  364. RT_ASSERT(edit != RT_NULL);
  365. rtgui_widget_get_rect(RTGUI_WIDGET(edit), rect);
  366. rect->x1 += visual.x * edit->font_width + RTGUI_WIDGET_BORDER(edit) + edit->margin;
  367. rect->x2 = rect->x1+1; /* caret width: 1 */
  368. rect->y1 += visual.y * edit->item_height + RTGUI_WIDGET_BORDER(edit) + edit->margin;
  369. if((rect->y1 + edit->font_height) < (rect->y2 - RTGUI_WIDGET_BORDER(edit) - edit->margin))
  370. rect->y2 = rect->y1 + edit->font_height;
  371. else
  372. rect->y2 = rect->y2 - RTGUI_WIDGET_BORDER(edit) - edit->margin;
  373. }
  374. static void rtgui_edit_init_caret(struct rtgui_edit *edit, rtgui_point_t visual)
  375. {
  376. struct rtgui_graphic_driver *hw_driver = rtgui_graphic_driver_get_default();
  377. int x, y;
  378. rtgui_color_t color;
  379. rtgui_rect_t rect;
  380. int ofs=0;
  381. RT_ASSERT(edit != RT_NULL);
  382. if(!RTGUI_WIDGET_IS_FOCUSED(edit)) return;
  383. rtgui_edit_get_caret_rect(edit, &edit->caret_rect, visual);
  384. rect = edit->caret_rect;
  385. rtgui_widget_rect_to_device(RTGUI_WIDGET(edit), &rect);
  386. if(edit->caret == RT_NULL)
  387. edit->caret = (rtgui_color_t*)rtgui_malloc(rtgui_rect_width(rect) * rtgui_rect_height(rect)*sizeof(rtgui_color_t));
  388. rtgui_timer_stop(edit->caret_timer);
  389. for(x=rect.x1; x<rect.x2; x++)
  390. {
  391. for(y=rect.y1; y<rect.y2; y++)
  392. {
  393. hw_driver->ops->get_pixel(&color,x,y);
  394. *(edit->caret + ofs++) = color;
  395. }
  396. }
  397. rtgui_timer_start(edit->caret_timer);
  398. }
  399. /* draw caret */
  400. static void rtgui_edit_draw_caret(struct rtgui_edit *edit)
  401. {
  402. int x,y;
  403. rtgui_color_t color;
  404. rtgui_rect_t rect;
  405. int ofs=0;
  406. struct rtgui_dc *dc;
  407. RT_ASSERT(edit != RT_NULL);
  408. if(edit->caret == RT_NULL) return;
  409. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  410. if(dc == RT_NULL)return;
  411. rect = edit->caret_rect;
  412. for(x=rect.x1; x<rect.x2; x++)
  413. {
  414. for(y=rect.y1; y<rect.y2; y++)
  415. {
  416. color = *(edit->caret + ofs);
  417. ofs++;
  418. if(edit->flag & RTGUI_EDIT_CARET)
  419. {
  420. color = ~color;
  421. rtgui_dc_draw_color_point(dc, x,y, color);
  422. }
  423. else
  424. {
  425. rtgui_dc_draw_color_point(dc, x,y, color);
  426. }
  427. }
  428. }
  429. rtgui_dc_end_drawing(dc);
  430. }
  431. static void rtgui_edit_timeout(struct rtgui_timer* timer, void* parameter)
  432. {
  433. struct rtgui_edit* edit;
  434. edit = RTGUI_EDIT(parameter);
  435. /* set caret flag */
  436. if(edit->flag & RTGUI_EDIT_CARET)
  437. edit->flag &= ~RTGUI_EDIT_CARET;
  438. else
  439. edit->flag |= RTGUI_EDIT_CARET;
  440. rtgui_edit_draw_caret(edit);
  441. return;
  442. }
  443. struct edit_line* rtgui_edit_get_line_by_index(struct rtgui_edit *edit, rt_uint32_t index)
  444. {
  445. struct edit_line *line = RT_NULL;
  446. RT_ASSERT(edit != RT_NULL);
  447. if(edit->head != RT_NULL)
  448. {
  449. int i=0;
  450. line = edit->first_line;
  451. while(line)
  452. {
  453. if((edit->upleft.y + i++) == index)
  454. break;
  455. line = line->next;
  456. }
  457. }
  458. return line;
  459. }
  460. rt_uint32_t rtgui_edit_get_index_by_line(struct rtgui_edit *edit, struct edit_line *line)
  461. {
  462. rt_uint32_t index=0;
  463. struct edit_line *tmp;
  464. RT_ASSERT(edit != RT_NULL);
  465. RT_ASSERT(line != RT_NULL);
  466. if(edit->head != RT_NULL)
  467. {
  468. tmp = edit->first_line;
  469. index = edit->upleft.y;
  470. while(tmp)
  471. {
  472. if(tmp == line)
  473. break;
  474. index ++;
  475. tmp = tmp->next;
  476. }
  477. }
  478. return index;
  479. }
  480. static void rtgui_edit_onmouse(struct rtgui_edit* edit, struct rtgui_event_mouse* emouse)
  481. {
  482. rtgui_rect_t rect;
  483. RT_ASSERT(edit != RT_NULL);
  484. RT_ASSERT(emouse != RT_NULL);
  485. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  486. if((rtgui_region_contains_point(&(RTGUI_WIDGET(edit)->clip), emouse->x, emouse->y, &rect) == RT_EOK))
  487. {
  488. rt_uint16_t x, y;
  489. /* multiline text */
  490. x = (emouse->x - rect.x1) / (edit->font_width);
  491. y = (emouse->y - rect.y1) / (edit->item_height);
  492. if((x < edit->col_per_page) && (y < edit->row_per_page))
  493. {
  494. if(emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  495. {
  496. struct edit_line *line;
  497. edit->visual.x = x;
  498. edit->visual.y = y;
  499. line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
  500. if(line == RT_NULL)
  501. return;
  502. if(edit->visual.x > line->len)
  503. edit->visual.x = line->len;
  504. if(edit->flag & RTGUI_EDIT_CARET)
  505. {
  506. if(edit->caret_timer != RT_NULL)
  507. rtgui_timer_stop(edit->caret_timer);
  508. edit->flag &= ~RTGUI_EDIT_CARET;
  509. rtgui_edit_draw_caret(edit);
  510. if(edit->caret_timer != RT_NULL)
  511. rtgui_timer_start(edit->caret_timer);
  512. }
  513. /* set widget focus */
  514. rtgui_widget_focus(RTGUI_WIDGET(edit));
  515. if(RTGUI_WIDGET_IS_FOCUSED(edit))
  516. {
  517. rtgui_edit_init_caret(edit, edit->visual);
  518. edit->flag |= RTGUI_EDIT_CARET;
  519. rtgui_edit_draw_caret(edit);
  520. }
  521. }
  522. else if(emouse->button & RTGUI_MOUSE_BUTTON_UP)
  523. {
  524. /* please add codes at here. */
  525. }
  526. #ifdef RTGUI_EDIT_USING_SCROLL
  527. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  528. {
  529. if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  530. rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
  531. }
  532. if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  533. {
  534. if(!RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  535. rtgui_scrollbar_set_value(edit->hscroll,edit->upleft.x);
  536. }
  537. #endif
  538. }
  539. }
  540. }
  541. rt_inline rt_uint16_t query_shift_code(rt_uint16_t key)
  542. {
  543. if(key >= RTGUIK_a && key <= RTGUIK_z)
  544. return (key - ('a'-'A'));
  545. else
  546. {
  547. switch (key)
  548. {
  549. case '1': return '!';
  550. case '2': return '@';
  551. case '3': return '#';
  552. case '4': return '$';
  553. case '5': return '%';
  554. case '6': return '^';
  555. case '7': return '&';
  556. case '8': return '*';
  557. case '9': return '(';
  558. case '0': return ')';
  559. case '-': return '_';
  560. case '=': return '+';
  561. case '\\':return '|';
  562. case ';': return ':';
  563. case '\'':return '"';
  564. case ',': return '<';
  565. case '.': return '>';
  566. case '/': return '?';
  567. case '`': return '~';
  568. }
  569. }
  570. return key;
  571. }
  572. rt_inline rt_uint16_t query_caps_code(rt_uint16_t key)
  573. {
  574. if(key >= RTGUIK_a && key <= RTGUIK_z)
  575. return (key - ('a'-'A'));
  576. return key;
  577. }
  578. rt_inline rt_bool_t is_small_keyboard(rt_uint16_t *key)
  579. {
  580. if(*key >= RTGUIK_KP0 && *key <= RTGUIK_KP9)
  581. {
  582. *key = *key - (RTGUIK_KP0 - RTGUIK_0);
  583. return RT_TRUE;
  584. }
  585. else if(*key == RTGUIK_KP_PERIOD)
  586. {
  587. *key = '.';
  588. return RT_TRUE;
  589. }
  590. else if(*key == RTGUIK_KP_DIVIDE)
  591. {
  592. *key = '/';
  593. return RT_TRUE;
  594. }
  595. else if(*key == RTGUIK_KP_MULTIPLY)
  596. {
  597. *key = '*';
  598. return RT_TRUE;
  599. }
  600. else if(*key == RTGUIK_KP_MINUS)
  601. {
  602. *key = '-';
  603. return RT_TRUE;
  604. }
  605. else if(*key == RTGUIK_KP_PLUS)
  606. {
  607. *key = '+';
  608. return RT_TRUE;
  609. }
  610. else if(*key == RTGUIK_KP_ENTER)
  611. {
  612. *key = RTGUIK_RETURN;
  613. return RT_TRUE;
  614. }
  615. return RT_FALSE;
  616. }
  617. void kbd_event_set_key(struct rtgui_event_kbd *ekbd, rt_uint16_t key)
  618. {
  619. RTGUI_EVENT_KBD_INIT(ekbd);
  620. ekbd->mod = RTGUI_KMOD_NONE;
  621. ekbd->unicode = 0;
  622. ekbd->key = key;
  623. ekbd->type = RTGUI_KEYDOWN;
  624. }
  625. static rt_bool_t rtgui_edit_onkey(struct rtgui_object* object, rtgui_event_t* event)
  626. {
  627. enum { EDIT_NONE, EDIT_ONDRAW, EDIT_UPDATE };
  628. struct rtgui_edit *edit = RTGUI_EDIT(object);
  629. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*)event;
  630. struct edit_line *line=RT_NULL;
  631. rt_bool_t update_type = EDIT_NONE;
  632. RT_ASSERT(edit != RT_NULL);
  633. RT_ASSERT(ekbd != RT_NULL);
  634. if (RTGUI_KBD_IS_UP(ekbd))
  635. { /* reset function key */
  636. if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
  637. edit->flag &= ~RTGUI_EDIT_CTRL;
  638. else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
  639. edit->flag &= ~RTGUI_EDIT_ALT;
  640. else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
  641. edit->flag &= ~RTGUI_EDIT_SHIFT;
  642. else if(ekbd->key == RTGUIK_CAPSLOCK)
  643. edit->flag &= ~RTGUI_EDIT_CAPSLOCK;
  644. else if(ekbd->key == RTGUIK_NUMLOCK)
  645. edit->flag &= ~RTGUI_EDIT_NUMLOCK;
  646. return RT_TRUE;
  647. }
  648. line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  649. if(line == RT_NULL)
  650. return RT_FALSE;
  651. /* rt_kprintf("key=%04X ",ekbd->key); */
  652. if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
  653. { /* use CTRL key */
  654. edit->flag |= RTGUI_EDIT_CTRL;
  655. return RT_FALSE;
  656. }
  657. else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
  658. { /* use ALT key */
  659. edit->flag |= RTGUI_EDIT_ALT;
  660. return RT_FALSE;
  661. }
  662. else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
  663. { /* use SHIFT key */
  664. edit->flag |= RTGUI_EDIT_SHIFT;
  665. return RT_FALSE;
  666. }
  667. else if(ekbd->key == RTGUIK_CAPSLOCK)
  668. {
  669. edit->flag |= RTGUI_EDIT_CAPSLOCK;
  670. return RT_FALSE;
  671. }
  672. else if(ekbd->key == RTGUIK_NUMLOCK)
  673. {
  674. edit->flag |= RTGUI_EDIT_NUMLOCK;
  675. return RT_FALSE;
  676. }
  677. else if(ekbd->key == RTGUIK_DELETE)
  678. { /* delete latter character */
  679. int ofs = edit->upleft.x + edit->visual.x;
  680. if(ofs > line->len - 1 || (ofs==0 && line->len==0))
  681. { /* will the next line marges into the current line */
  682. struct edit_line* next_line = line->next;
  683. if(next_line != RT_NULL)
  684. {
  685. struct edit_line *update_end_line;
  686. update_type = EDIT_UPDATE;
  687. edit->update.start = edit->visual;
  688. rtgui_edit_connect_line(edit, line, next_line);
  689. rtgui_edit_delete_line(edit, next_line);
  690. if(edit->max_rows-edit->upleft.y > edit->row_per_page)
  691. {
  692. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
  693. if(update_end_line != RT_NULL)
  694. {
  695. edit->update.end.x = edit->col_per_page;
  696. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  697. }
  698. }
  699. else
  700. {
  701. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  702. edit->update.end.x = edit->col_per_page;
  703. edit->update.end.y = update_end_index+1;
  704. }
  705. }
  706. line->len = rtgui_edit_line_strlen(line->text);
  707. goto _edit_exit;
  708. }
  709. else if(ofs == line->len - 1)
  710. {
  711. line->text[ofs] = '\0';
  712. }
  713. else
  714. {
  715. char *c;
  716. /* remove character */
  717. for(c = &line->text[ofs]; c[1] != '\0'; c++)
  718. *c = c[1];
  719. *c = '\0';
  720. }
  721. update_type = EDIT_UPDATE;
  722. edit->update.start = edit->visual;
  723. edit->update.end.x = line->len-edit->upleft.x;
  724. if (edit->update.end.x > edit->col_per_page)
  725. edit->update.end.x = edit->col_per_page;
  726. edit->update.end.y = edit->visual.y;
  727. }
  728. else if(ekbd->key == RTGUIK_BACKSPACE)
  729. {
  730. if(edit->visual.x == 0)
  731. { /* incorporated into prev line */
  732. struct rtgui_event_kbd event_kbd;
  733. struct edit_line* prev_line = line->prev;
  734. if(prev_line != RT_NULL)
  735. {
  736. struct edit_line *update_end_line;
  737. update_type = EDIT_UPDATE;
  738. edit->visual.x = prev_line->len;
  739. rtgui_edit_connect_line(edit, prev_line, line);
  740. kbd_event_set_key(&event_kbd, RTGUIK_UP);
  741. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  742. rtgui_edit_delete_line(edit, line);
  743. edit->update.start = edit->visual; /* update.start.y is changed */
  744. if(edit->max_rows-edit->upleft.y > edit->row_per_page)
  745. {
  746. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
  747. if(update_end_line != RT_NULL)
  748. {
  749. edit->update.end.x = edit->col_per_page;
  750. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  751. }
  752. }
  753. else
  754. {
  755. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  756. edit->update.end.x = edit->col_per_page;
  757. edit->update.end.y = update_end_index+1;
  758. }
  759. }
  760. goto _edit_exit;
  761. }
  762. /* delete front character */
  763. if(edit->visual.x == line->len)
  764. {
  765. line->text[edit->visual.x-1] = '\0';
  766. edit->visual.x --;
  767. }
  768. else if(edit->visual.x != 0)
  769. { /* remove current character */
  770. char *c;
  771. /* remove character */
  772. for(c = &line->text[edit->visual.x - 1]; c[1] != '\0'; c++)
  773. {
  774. *c = c[1];
  775. }
  776. *c = '\0';
  777. edit->visual.x --;
  778. }
  779. /* adjusted line buffer length */
  780. if(rtgui_edit_alloc_len(edit->bzsize, line->len+2) < line->zsize)
  781. {
  782. line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
  783. line->text = rt_realloc(line->text, line->zsize);
  784. }
  785. update_type = EDIT_UPDATE;
  786. edit->update.start = edit->visual;
  787. edit->update.end.x = line->len;
  788. edit->update.end.y = edit->visual.y;
  789. }
  790. else if(ekbd->key == RTGUIK_UP)
  791. { /* move to prev line */
  792. struct edit_line* prev_line;
  793. if(edit->visual.y > 0)
  794. edit->visual.y --;
  795. else
  796. {
  797. /* change first row */
  798. if(edit->upleft.y > 0)
  799. {
  800. edit->upleft.y --;
  801. if(edit->first_line->prev != RT_NULL)
  802. edit->first_line = edit->first_line->prev;
  803. update_type = EDIT_ONDRAW;
  804. }
  805. }
  806. /* The position of the recount X */
  807. prev_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
  808. if(prev_line == RT_NULL)
  809. return RT_FALSE;
  810. if(edit->upleft.x > 0)
  811. {
  812. if(prev_line->len <= edit->upleft.x)
  813. {
  814. edit->upleft.x = 0;
  815. edit->visual.x = prev_line->len;
  816. update_type = EDIT_ONDRAW;
  817. }
  818. else if(prev_line->len - edit->upleft.x < edit->col_per_page)
  819. edit->visual.x = prev_line->len - edit->upleft.x;
  820. }
  821. else if(edit->visual.x > prev_line->len)
  822. edit->visual.x = prev_line->len;
  823. #ifdef RTGUI_EDIT_USING_SCROLL
  824. /* update vscroll */
  825. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  826. {
  827. if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  828. rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
  829. }
  830. #endif
  831. }
  832. else if(ekbd->key == RTGUIK_DOWN)
  833. {
  834. struct edit_line *tail_line, *next_line;
  835. tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  836. if(tail_line != RT_NULL)
  837. { /* it is tail line */
  838. if(tail_line == edit->tail) return RT_FALSE;
  839. }
  840. /* move to next line */
  841. if(edit->visual.y < edit->row_per_page - 2)
  842. {
  843. edit->visual.y ++;
  844. }
  845. else if(edit->visual.y+edit->upleft.y < edit->max_rows-1)
  846. {
  847. /* change first row */
  848. edit->upleft.y++;
  849. if(edit->first_line->next != RT_NULL)
  850. edit->first_line = edit->first_line->next;
  851. update_type = EDIT_ONDRAW;
  852. }
  853. /* adjust next line end position */
  854. next_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
  855. if(next_line == RT_NULL)
  856. return RT_FALSE;
  857. if(edit->upleft.x > 0)
  858. {
  859. if(next_line->len <= edit->upleft.x)
  860. {
  861. edit->upleft.x = 0;
  862. edit->visual.x = next_line->len;
  863. update_type = EDIT_ONDRAW;
  864. }
  865. else if(next_line->len - edit->upleft.x < edit->col_per_page)
  866. edit->visual.x = next_line->len - edit->upleft.x;
  867. }
  868. else if(edit->visual.x > next_line->len)
  869. edit->visual.x = next_line->len;
  870. #ifdef RTGUI_EDIT_USING_SCROLL
  871. /* update vscroll */
  872. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  873. {
  874. if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  875. rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
  876. }
  877. #endif
  878. }
  879. else if(ekbd->key == RTGUIK_LEFT)
  880. { /* move to prev char */
  881. if(edit->visual.x > 0)
  882. edit->visual.x --;
  883. else
  884. {
  885. if(edit->upleft.x > 0)
  886. {
  887. edit->upleft.x --;
  888. update_type = EDIT_ONDRAW;
  889. }
  890. else
  891. {
  892. struct rtgui_event_kbd event_kbd;
  893. struct edit_line* first_line;
  894. first_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  895. if(first_line != RT_NULL)
  896. { /* it is head line */
  897. if(first_line == edit->head) return RT_FALSE;
  898. }
  899. /* move the caret to the prev line end */
  900. kbd_event_set_key(&event_kbd, RTGUIK_UP);
  901. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  902. kbd_event_set_key(&event_kbd, RTGUIK_END);
  903. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  904. }
  905. }
  906. }
  907. else if(ekbd->key == RTGUIK_RIGHT)
  908. { /* move to next char */
  909. if(line->len >= edit->col_per_page)
  910. {
  911. if(edit->upleft.x+edit->col_per_page <= line->len)
  912. {
  913. if(edit->visual.x < edit->col_per_page-1)
  914. edit->visual.x ++;
  915. else if(edit->visual.x == edit->col_per_page-1)
  916. {
  917. if(edit->upleft.x+edit->col_per_page < line->len)
  918. edit->upleft.x ++;
  919. else
  920. edit->upleft.x = line->len - edit->col_per_page + 1;
  921. update_type = EDIT_ONDRAW;
  922. }
  923. }
  924. else
  925. {
  926. struct rtgui_event_kbd event_kbd;
  927. /* move to next head */
  928. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  929. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  930. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  931. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  932. }
  933. }
  934. else
  935. {
  936. if(edit->visual.x < line->len)
  937. edit->visual.x ++;
  938. else
  939. {
  940. struct rtgui_event_kbd event_kbd;
  941. struct edit_line* tail_line;
  942. tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  943. if(tail_line != RT_NULL)
  944. { /* it is tail line */
  945. if(tail_line == edit->tail) return RT_FALSE;
  946. }
  947. /* move the caret to the next line head */
  948. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  949. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  950. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  951. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  952. }
  953. }
  954. }
  955. else if(ekbd->key == RTGUIK_HOME)
  956. { /* move cursor to line head */
  957. edit->visual.x = 0;
  958. if(edit->upleft.x > 0)
  959. {
  960. edit->upleft.x = 0;
  961. update_type = EDIT_ONDRAW;
  962. }
  963. }
  964. else if(ekbd->key == RTGUIK_END)
  965. { /* move cursor to line tail */
  966. if(line->len >= edit->col_per_page)
  967. {
  968. edit->visual.x = edit->col_per_page - 1;
  969. edit->upleft.x = line->len - (edit->col_per_page-1);
  970. update_type = EDIT_ONDRAW;
  971. }
  972. else
  973. edit->visual.x = line->len;
  974. }
  975. else if(ekbd->key == RTGUIK_TAB)
  976. {
  977. int space_nums;
  978. struct rtgui_event_kbd event_kbd;
  979. /* using spaces to replace TAB */
  980. space_nums = edit->tabsize - (edit->upleft.x+edit->visual.x) % edit->tabsize;
  981. while(space_nums--)
  982. {
  983. kbd_event_set_key(&event_kbd, RTGUIK_SPACE);
  984. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  985. }
  986. }
  987. else if(ekbd->key == RTGUIK_RETURN)
  988. {
  989. struct edit_line *update_end_line;
  990. struct rtgui_event_kbd event_kbd;
  991. update_type = EDIT_UPDATE;
  992. edit->update.start = edit->visual;
  993. /* insert a new line buffer */
  994. rtgui_edit_insert_line(edit, line, line->text + edit->upleft.x + edit->visual.x);
  995. line->text[edit->upleft.x + edit->visual.x] = '\0';
  996. line->len = rtgui_edit_line_strlen(line->text);
  997. /* adjust update line end position */
  998. if((edit->max_rows-edit->upleft.y) >= edit->row_per_page)
  999. {
  1000. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page-1);
  1001. if(update_end_line != RT_NULL)
  1002. {
  1003. edit->update.end.x = update_end_line->len;
  1004. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  1005. }
  1006. }
  1007. else
  1008. {
  1009. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  1010. edit->update.end.x = edit->tail->len;
  1011. edit->update.end.y = update_end_index;
  1012. }
  1013. /* move the caret to the next line head */
  1014. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  1015. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  1016. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  1017. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  1018. }
  1019. else
  1020. {
  1021. if(isprint(ekbd->key))
  1022. { /* it's may print character */
  1023. update_type = EDIT_UPDATE;
  1024. edit->update.start = edit->visual;
  1025. if(edit->flag & RTGUI_EDIT_SHIFT)
  1026. ekbd->key = query_shift_code(ekbd->key);
  1027. if(edit->flag & RTGUI_EDIT_CAPSLOCK)
  1028. ekbd->key = query_caps_code(ekbd->key);
  1029. if(line->len < line->zsize-1)
  1030. {
  1031. int ofs = edit->upleft.x + edit->visual.x;
  1032. if(edit->visual.x >= edit->col_per_page-1)
  1033. {
  1034. edit->upleft.x ++;
  1035. update_type = EDIT_ONDRAW;
  1036. }
  1037. if(ofs < line->len)
  1038. {
  1039. char* c;
  1040. for(c = &line->text[line->len]; c != &line->text[ofs]; c--)
  1041. *c = *(c-1);
  1042. }
  1043. line->text[ofs] = ekbd->key;
  1044. if(edit->visual.x < edit->col_per_page-1)
  1045. edit->visual.x ++;
  1046. line->text[line->len+1] = '\0';
  1047. line->len = rtgui_edit_line_strlen(line->text);
  1048. edit->update.end.x = line->len;
  1049. if(edit->update.end.x > edit->col_per_page)
  1050. edit->update.end.x = edit->col_per_page;
  1051. edit->update.end.y = edit->visual.y;
  1052. }
  1053. else
  1054. { /* adjust line buffer's zone size */
  1055. line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
  1056. line->text = rt_realloc(line->text, line->zsize);
  1057. rtgui_edit_onkey(object, event); /* reentry */
  1058. }
  1059. }
  1060. else
  1061. {
  1062. /* Is small keyboard ? */
  1063. if(edit->flag & RTGUI_EDIT_NUMLOCK)
  1064. {
  1065. if(is_small_keyboard(&ekbd->key))
  1066. rtgui_edit_onkey(object, event);
  1067. /* small keyboard another value reserved */
  1068. }
  1069. }
  1070. }
  1071. line->len = rtgui_edit_line_strlen(line->text);
  1072. _edit_exit:
  1073. if(edit->flag & RTGUI_EDIT_CARET)
  1074. {
  1075. if(edit->caret_timer != RT_NULL)
  1076. rtgui_timer_stop(edit->caret_timer);
  1077. edit->flag &= ~RTGUI_EDIT_CARET;
  1078. rtgui_edit_draw_caret(edit);/* refresh it */
  1079. if(edit->caret_timer != RT_NULL)
  1080. rtgui_timer_start(edit->caret_timer);
  1081. }
  1082. /* re-draw edit widget */
  1083. if(update_type == EDIT_ONDRAW)
  1084. rtgui_edit_ondraw(edit);
  1085. else if(update_type == EDIT_UPDATE)
  1086. rtgui_edit_update(edit);
  1087. if(RTGUI_WIDGET_IS_FOCUSED(edit))
  1088. {
  1089. rtgui_edit_init_caret(edit, edit->visual);
  1090. edit->flag |= RTGUI_EDIT_CARET;
  1091. rtgui_edit_draw_caret(edit);
  1092. }
  1093. return RT_TRUE;
  1094. }
  1095. static rt_bool_t rtgui_edit_onfocus(struct rtgui_object* object, rtgui_event_t* event)
  1096. {
  1097. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1098. edit->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC,
  1099. rtgui_edit_timeout, (void*)edit);
  1100. /* set caret to show */
  1101. edit->flag |= RTGUI_EDIT_CARET;
  1102. /* start caret timer */
  1103. if(edit->caret_timer != RT_NULL)
  1104. rtgui_timer_start(edit->caret_timer);
  1105. return RT_TRUE;
  1106. }
  1107. static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object* object, rtgui_event_t* event)
  1108. {
  1109. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1110. /* stop caret timer */
  1111. if(edit->caret_timer != RT_NULL)
  1112. {
  1113. rtgui_timer_stop(edit->caret_timer);
  1114. rtgui_timer_destory(edit->caret_timer);
  1115. }
  1116. /* set caret to hide */
  1117. edit->flag &= ~RTGUI_EDIT_CARET;
  1118. rtgui_edit_draw_caret(edit);
  1119. return RT_TRUE;
  1120. }
  1121. #ifdef RTGUI_EDIT_USING_SCROLL
  1122. static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event)
  1123. {
  1124. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1125. /* adjust first display row when dragging */
  1126. edit->upleft.y = edit->hscroll->value;
  1127. rtgui_edit_ondraw(edit);
  1128. return RT_TRUE;
  1129. }
  1130. static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event)
  1131. {
  1132. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1133. /* adjust first display row when dragging */
  1134. edit->upleft.x = edit->vscroll->value;
  1135. rtgui_edit_ondraw(edit);
  1136. return RT_TRUE;
  1137. }
  1138. #endif
  1139. /* local area update */
  1140. void rtgui_edit_update(struct rtgui_edit *edit)
  1141. {
  1142. rt_uint32_t i,cpy_len=0,prev_len;
  1143. rtgui_rect_t rect, r;
  1144. struct rtgui_dc *dc;
  1145. char *src;
  1146. RT_ASSERT(edit != RT_NULL);
  1147. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1148. if(dc == RT_NULL) return;
  1149. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1150. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1151. if(edit->update_buf == RT_NULL)
  1152. { /* try again allocate */
  1153. edit->update_buf = rtgui_malloc(edit->col_per_page+1);
  1154. }
  1155. prev_len = edit->col_per_page;
  1156. for(i=edit->update.start.y; i<=edit->update.end.y; i++)
  1157. {
  1158. struct edit_line *line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + i);
  1159. if(i > edit->upleft.y+edit->row_per_page) break;
  1160. if(line == RT_NULL)
  1161. { /* when use "RTGUIK_BACKSPACE" backspace forward,
  1162. * the last line is blank, that only fill background. */
  1163. r.x1 = rect.x1;
  1164. r.x2 = rect.x1 + prev_len * edit->font_width;
  1165. r.y1 = rect.y1 + i * edit->font_height;
  1166. r.y2 = r.y1 + edit->font_height;
  1167. if(r.x2 > rect.x2) r.x2 = rect.x2;
  1168. if(r.y2 > rect.y2) r.y2 = rect.y2;
  1169. rtgui_dc_fill_rect(dc, &r);
  1170. break;
  1171. }
  1172. if(edit->update.start.y == edit->update.end.y)
  1173. {
  1174. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1175. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1176. r.y1 = rect.y1 + i * edit->font_height;
  1177. r.y2 = r.y1 + edit->font_height;
  1178. cpy_len = edit->update.end.x - edit->update.start.x;
  1179. src = line->text + edit->update.start.x + edit->upleft.x;
  1180. }
  1181. else
  1182. {
  1183. if(i == edit->update.start.y)
  1184. {
  1185. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1186. r.x2 = rect.x2;
  1187. r.y1 = rect.y1 + i * edit->font_height;
  1188. r.y2 = r.y1 + edit->font_height;
  1189. cpy_len = line->len - edit->update.start.x - edit->upleft.x;
  1190. if(cpy_len > (edit->col_per_page-edit->update.start.x))
  1191. cpy_len = edit->col_per_page-edit->update.start.x;
  1192. src = line->text + edit->update.start.x + edit->upleft.x;
  1193. }
  1194. else if(i == edit->update.end.y)
  1195. {
  1196. r.x1 = rect.x1;
  1197. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1198. cpy_len = edit->update.end.x;
  1199. r.y1 = rect.y1 + i * edit->font_height;
  1200. r.y2 = r.y1 + edit->font_height;
  1201. src = line->text + edit->upleft.x;
  1202. }
  1203. else
  1204. {
  1205. r.x1 = rect.x1;
  1206. r.x2 = rect.x2;
  1207. r.y1 = rect.y1 + i * edit->font_height;
  1208. r.y2 = r.y1 + edit->font_height;
  1209. cpy_len = line->len - edit->upleft.x;
  1210. if(cpy_len > edit->col_per_page)
  1211. cpy_len = edit->col_per_page;
  1212. src = line->text + edit->upleft.x;
  1213. }
  1214. }
  1215. if(r.y1 > rect.y2) break;
  1216. if(r.x2 > rect.x2) r.x2 = rect.x2;
  1217. if(r.y2 > rect.y2) r.y2 = rect.y2;
  1218. rt_memcpy(edit->update_buf, src, cpy_len);
  1219. *(edit->update_buf + cpy_len) = '\0';
  1220. rtgui_dc_fill_rect(dc, &r);
  1221. rtgui_dc_draw_text(dc, edit->update_buf, &r);
  1222. prev_len = line->len;
  1223. }
  1224. rtgui_dc_end_drawing(dc);
  1225. }
  1226. void rtgui_edit_ondraw(struct rtgui_edit *edit)
  1227. {
  1228. rtgui_rect_t rect, r;
  1229. struct rtgui_dc *dc;
  1230. int hscroll_flag=0;
  1231. int vscroll_flag=0;
  1232. RT_ASSERT(edit != RT_NULL);
  1233. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1234. if(dc == RT_NULL) return;
  1235. /* get widget rect */
  1236. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1237. rtgui_rect_inflate(&rect, -RTGUI_WIDGET_BORDER(edit));
  1238. /* fill widget rect with edit background color */
  1239. RTGUI_DC_BC(dc) = RTGUI_WIDGET_BACKGROUND(edit);
  1240. rtgui_dc_fill_rect(dc, &rect);
  1241. rtgui_rect_inflate(&rect, RTGUI_WIDGET_BORDER(edit));
  1242. /* draw border */
  1243. rtgui_dc_draw_border(dc, &rect, RTGUI_WIDGET_BORDER_STYLE(edit));
  1244. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1245. #ifdef RTGUI_EDIT_USING_SCROLL
  1246. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1247. {
  1248. rect.x2 = rect.x2 - rtgui_rect_width(edit->vscroll->parent.extent);
  1249. }
  1250. if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1251. {
  1252. rect.y2 = rect.y2 - rtgui_rect_height(edit->hscroll->parent.extent);
  1253. }
  1254. #endif
  1255. r = rect;
  1256. /* draw text */
  1257. if(edit->head != RT_NULL)
  1258. {
  1259. struct edit_line *line = edit->first_line;
  1260. int num=0;
  1261. rect.y2 = rect.y1 + edit->item_height;
  1262. while(line)
  1263. {
  1264. if(edit->upleft.x < line->len)
  1265. rtgui_dc_draw_text(dc, line->text+edit->upleft.x, &rect);
  1266. line = line->next;
  1267. rect.y1 += edit->item_height;
  1268. if((rect.y1 + edit->item_height) < r.y2)
  1269. rect.y2 = rect.y1 + edit->item_height;
  1270. else
  1271. rect.y2 = r.y2;
  1272. if(num++ >= edit->row_per_page)
  1273. break;
  1274. }
  1275. }
  1276. #ifdef RTGUI_EDIT_USING_SCROLL
  1277. if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1278. {
  1279. hscroll_flag = 1;
  1280. rtgui_scrollbar_ondraw(edit->hscroll);
  1281. }
  1282. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1283. {
  1284. vscroll_flag = 1;
  1285. rtgui_scrollbar_ondraw(edit->vscroll);
  1286. }
  1287. if(hscroll_flag && vscroll_flag)
  1288. {
  1289. rtgui_color_t _bc;
  1290. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1291. rect.x1 = rect.x2-RTGUI_WIDGET_BORDER(edit);
  1292. rect.y1 = rect.y2-RTGUI_WIDGET_BORDER(edit);
  1293. _bc = RTGUI_DC_BC(dc);
  1294. RTGUI_DC_BC(dc) = default_background;
  1295. rtgui_dc_fill_rect(dc,&rect);
  1296. RTGUI_DC_BC(dc) = _bc;
  1297. }
  1298. #endif
  1299. rtgui_dc_end_drawing(dc);
  1300. }
  1301. /* set edit text */
  1302. void rtgui_edit_set_text(struct rtgui_edit* edit, const char* text)
  1303. {
  1304. const char *begin, *ptr;
  1305. int hscroll_flag=0;
  1306. int vscroll_flag=0;
  1307. RT_ASSERT(edit != RT_NULL);
  1308. while(edit->max_rows > 0)
  1309. rtgui_edit_delete_line(edit, edit->head);
  1310. edit->max_rows = 0;
  1311. begin = text;
  1312. for(ptr=begin; *ptr != '\0'; ptr++)
  1313. {
  1314. if(*ptr == 0x0A)
  1315. { /* unix style */
  1316. rtgui_edit_append_line(edit, begin);
  1317. begin = ptr+1;
  1318. }
  1319. else if(*ptr == 0x0D && *(ptr+1) == 0x0A)
  1320. { /* windows style */
  1321. rtgui_edit_append_line(edit, begin);
  1322. begin = ptr+2;
  1323. }
  1324. }
  1325. if(*ptr == '\0')
  1326. {
  1327. if(begin < ptr)
  1328. rtgui_edit_append_line(edit, begin);
  1329. }
  1330. edit->first_line = edit->head;
  1331. #ifdef RTGUI_EDIT_USING_SCROLL
  1332. if(edit->hscroll != RT_NULL)
  1333. {
  1334. if(edit->max_cols > edit->col_per_page)
  1335. {
  1336. RTGUI_WIDGET_SHOW(edit->hscroll);
  1337. rtgui_scrollbar_set_line_step(edit->hscroll, 1);
  1338. rtgui_scrollbar_set_page_step(edit->hscroll, edit->col_per_page);
  1339. rtgui_scrollbar_set_range(edit->hscroll, edit->max_cols);
  1340. hscroll_flag = 1;
  1341. }
  1342. else
  1343. {
  1344. RTGUI_WIDGET_HIDE(edit->hscroll);
  1345. }
  1346. }
  1347. if(edit->vscroll != RT_NULL)
  1348. {
  1349. if(edit->max_rows > edit->row_per_page)
  1350. {
  1351. RTGUI_WIDGET_SHOW(edit->vscroll);
  1352. rtgui_scrollbar_set_line_step(edit->vscroll, 1);
  1353. rtgui_scrollbar_set_page_step(edit->vscroll, edit->row_per_page);
  1354. rtgui_scrollbar_set_range(edit->vscroll, edit->max_rows);
  1355. vscroll_flag = 1;
  1356. }
  1357. else
  1358. {
  1359. RTGUI_WIDGET_HIDE(edit->vscroll);
  1360. }
  1361. }
  1362. if(edit->hscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1363. {
  1364. rtgui_edit_adjust_scroll(edit->hscroll);
  1365. }
  1366. if(edit->vscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1367. {
  1368. rtgui_edit_adjust_scroll(edit->vscroll);
  1369. }
  1370. #endif
  1371. if(hscroll_flag || vscroll_flag)
  1372. {
  1373. rtgui_widget_update_clip(RTGUI_WIDGET(edit));
  1374. }
  1375. }
  1376. rt_bool_t rtgui_edit_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  1377. {
  1378. rtgui_widget_t *widget = RTGUI_WIDGET(object);
  1379. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1380. switch(event->type)
  1381. {
  1382. case RTGUI_EVENT_PAINT:
  1383. #ifndef RTGUI_USING_SMALL_SIZE
  1384. if(widget->on_draw != RT_NULL)
  1385. widget->on_draw(object, event);
  1386. else
  1387. #endif
  1388. rtgui_edit_ondraw(edit);
  1389. break;
  1390. case RTGUI_EVENT_MOUSE_BUTTON:
  1391. #ifndef RTGUI_USING_SMALL_SIZE
  1392. if(widget->on_mouseclick != RT_NULL)
  1393. widget->on_mouseclick(object, event);
  1394. else
  1395. #endif
  1396. rtgui_edit_onmouse(edit, (struct rtgui_event_mouse*)event);
  1397. return RT_TRUE;
  1398. case RTGUI_EVENT_KBD:
  1399. #ifndef RTGUI_USING_SMALL_SIZE
  1400. if(widget->on_key != RT_NULL)
  1401. widget->on_key(object, event);
  1402. else
  1403. #endif
  1404. rtgui_edit_onkey(object, event);
  1405. return RT_TRUE;
  1406. default:
  1407. return rtgui_widget_event_handler(object, event);
  1408. }
  1409. return RT_FALSE;
  1410. }
  1411. /**
  1412. * File access component, General File Access Interface
  1413. */
  1414. rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename)
  1415. {
  1416. int fd, num=0, read_bytes, size ,len=0;
  1417. char *text ,ch;
  1418. fd = open(filename, O_RDONLY, 0);
  1419. if (fd < 0)
  1420. {
  1421. return RT_FALSE;
  1422. }
  1423. while(edit->max_rows > 0)
  1424. rtgui_edit_delete_line(edit, edit->head);
  1425. edit->max_rows = 0;
  1426. size = edit->bzsize;
  1427. text = rtgui_malloc(size);
  1428. if(text == RT_NULL) return RT_FALSE;
  1429. do {
  1430. if ( (read_bytes = read(fd, &ch, 1)) > 0 )
  1431. {
  1432. if(num >= size - 1)
  1433. text = rt_realloc(text, rtgui_edit_alloc_len(size, num));
  1434. if(ch == 0x09) //Tab
  1435. {
  1436. len = edit->tabsize - num%edit->tabsize;
  1437. while(len--)
  1438. *(text + num++) = ' ';
  1439. }
  1440. else
  1441. *(text + num++) = ch;
  1442. if(ch == 0x0A)
  1443. {
  1444. rtgui_edit_append_line(edit, text);
  1445. num = 0;
  1446. }
  1447. }
  1448. } while(read_bytes);
  1449. close(fd);
  1450. rtgui_free(text);
  1451. rtgui_edit_ondraw(edit);
  1452. return RT_TRUE;
  1453. }
  1454. rt_bool_t rtgui_edit_saveas_file(struct rtgui_edit *edit, const char *filename)
  1455. {
  1456. int fd;
  1457. char ch_tailed = 0x0A;
  1458. struct edit_line *line;
  1459. fd = open(filename, O_WRONLY | O_CREAT, 0);
  1460. if (fd < 0)
  1461. {
  1462. return RT_FALSE;
  1463. }
  1464. line = edit->head;
  1465. while(line)
  1466. {
  1467. write(fd, line->text, line->len);
  1468. if(line != edit->tail)
  1469. write(fd, &ch_tailed, 1);
  1470. line = line->next;
  1471. }
  1472. close(fd);
  1473. return RT_TRUE;
  1474. }