edit.c 48 KB

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