1
0

edit.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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. {
  498. EDIT_IDENT_DIR_BOTH,
  499. EDIT_IDENT_DIR_LEFT,
  500. EDIT_IDENT_DIR_RIGHT
  501. };
  502. /*
  503. * identify a byte is double byte
  504. * @param dir set direction.
  505. * @param *p record the position of the effective.
  506. * @return RT_TRUE is Got it, else not found.
  507. */
  508. static rt_bool_t identify_double_byte(struct rtgui_edit *edit, struct edit_line *line,
  509. rt_uint32_t dir, rt_int16_t *p)
  510. {
  511. int index, effe_nums;
  512. RT_ASSERT(edit != RT_NULL);
  513. RT_ASSERT(line != RT_NULL);
  514. if (dir == EDIT_IDENT_DIR_BOTH)
  515. {
  516. }
  517. else if (dir == EDIT_IDENT_DIR_LEFT)
  518. {
  519. if (edit->upleft.x == 0 && edit->visual.x == 0)
  520. return RT_FALSE;
  521. index = edit->upleft.x + edit->visual.x;
  522. effe_nums = 0;
  523. while (index--)
  524. {
  525. if ((rt_uint8_t)(*(line->text + index)) >= 0x80)
  526. effe_nums ++;
  527. else
  528. break;
  529. }
  530. if (effe_nums > 0)
  531. {
  532. *p = 2 - effe_nums % 2;
  533. return RT_TRUE;
  534. }
  535. }
  536. else if (dir == EDIT_IDENT_DIR_RIGHT)
  537. {
  538. if (edit->upleft.x + edit->visual.x == line->len)
  539. return RT_FALSE;
  540. index = edit->upleft.x + edit->visual.x;
  541. effe_nums = 0;
  542. while (index < line->len)
  543. {
  544. if ((rt_uint8_t)(*(line->text + index)) >= 0x80)
  545. effe_nums ++;
  546. else
  547. break;
  548. index ++;
  549. }
  550. if (effe_nums > 0)
  551. {
  552. *p = 2 - effe_nums % 2;
  553. return RT_TRUE;
  554. }
  555. }
  556. return RT_FALSE;
  557. }
  558. static void rtgui_edit_onmouse(struct rtgui_edit *edit, struct rtgui_event_mouse *emouse)
  559. {
  560. rtgui_rect_t rect;
  561. RT_ASSERT(edit != RT_NULL);
  562. RT_ASSERT(emouse != RT_NULL);
  563. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  564. if ((rtgui_region_contains_point(&(RTGUI_WIDGET(edit)->clip), emouse->x, emouse->y, &rect) == RT_EOK))
  565. {
  566. rt_uint16_t x, y;
  567. /* multiline text */
  568. x = (emouse->x - rect.x1) / (edit->font_width);
  569. y = (emouse->y - rect.y1) / (edit->item_height);
  570. if ((x < edit->col_per_page) && (y < edit->row_per_page))
  571. {
  572. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  573. {
  574. struct edit_line *line;
  575. rt_int16_t tmp_pos = 0;
  576. edit->visual.x = x;
  577. edit->visual.y = y;
  578. line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  579. if (line == RT_NULL)
  580. return;
  581. if (edit->visual.x > line->len)
  582. edit->visual.x = line->len;
  583. if (edit->upleft.x > 0)
  584. {
  585. if (edit->upleft.x >= line->len)
  586. edit->upleft.x = 0;
  587. else
  588. edit->visual.x -= edit->upleft.x;
  589. rtgui_edit_ondraw(edit);
  590. }
  591. if (identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
  592. edit->visual.x -= (2 - tmp_pos);
  593. if (edit->flag & RTGUI_EDIT_CARET)
  594. {
  595. if (edit->caret_timer != RT_NULL)
  596. rtgui_timer_stop(edit->caret_timer);
  597. edit->flag &= ~RTGUI_EDIT_CARET;
  598. rtgui_edit_draw_caret(edit);
  599. if (edit->caret_timer != RT_NULL)
  600. rtgui_timer_start(edit->caret_timer);
  601. }
  602. /* set widget focus */
  603. rtgui_widget_focus(RTGUI_WIDGET(edit));
  604. if (RTGUI_WIDGET_IS_FOCUSED(edit))
  605. {
  606. rtgui_edit_init_caret(edit, edit->visual);
  607. edit->flag |= RTGUI_EDIT_CARET;
  608. rtgui_edit_draw_caret(edit);
  609. }
  610. }
  611. else if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
  612. {
  613. /* please add codes at here. */
  614. }
  615. #ifdef RTGUI_EDIT_USING_SCROLL
  616. if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  617. {
  618. if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  619. rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y);
  620. }
  621. if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  622. {
  623. if (!RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  624. rtgui_scrollbar_set_value(edit->hscroll, edit->upleft.x);
  625. }
  626. #endif
  627. }
  628. }
  629. }
  630. rt_inline rt_uint16_t query_shift_code(rt_uint16_t key)
  631. {
  632. if (key >= RTGUIK_a && key <= RTGUIK_z)
  633. return (key - ('a' - 'A'));
  634. else
  635. {
  636. switch (key)
  637. {
  638. case '1':
  639. return '!';
  640. case '2':
  641. return '@';
  642. case '3':
  643. return '#';
  644. case '4':
  645. return '$';
  646. case '5':
  647. return '%';
  648. case '6':
  649. return '^';
  650. case '7':
  651. return '&';
  652. case '8':
  653. return '*';
  654. case '9':
  655. return '(';
  656. case '0':
  657. return ')';
  658. case '-':
  659. return '_';
  660. case '=':
  661. return '+';
  662. case '\\':
  663. return '|';
  664. case ';':
  665. return ':';
  666. case '\'':
  667. return '"';
  668. case ',':
  669. return '<';
  670. case '.':
  671. return '>';
  672. case '/':
  673. return '?';
  674. case '`':
  675. return '~';
  676. }
  677. }
  678. return key;
  679. }
  680. rt_inline rt_uint16_t query_caps_code(rt_uint16_t key)
  681. {
  682. if (key >= RTGUIK_a && key <= RTGUIK_z)
  683. return (key - ('a' - 'A'));
  684. return key;
  685. }
  686. rt_inline rt_bool_t is_small_keyboard(rt_uint16_t *key)
  687. {
  688. if (*key >= RTGUIK_KP0 && *key <= RTGUIK_KP9)
  689. {
  690. *key = *key - (RTGUIK_KP0 - RTGUIK_0);
  691. return RT_TRUE;
  692. }
  693. else if (*key == RTGUIK_KP_PERIOD)
  694. {
  695. *key = '.';
  696. return RT_TRUE;
  697. }
  698. else if (*key == RTGUIK_KP_DIVIDE)
  699. {
  700. *key = '/';
  701. return RT_TRUE;
  702. }
  703. else if (*key == RTGUIK_KP_MULTIPLY)
  704. {
  705. *key = '*';
  706. return RT_TRUE;
  707. }
  708. else if (*key == RTGUIK_KP_MINUS)
  709. {
  710. *key = '-';
  711. return RT_TRUE;
  712. }
  713. else if (*key == RTGUIK_KP_PLUS)
  714. {
  715. *key = '+';
  716. return RT_TRUE;
  717. }
  718. else if (*key == RTGUIK_KP_ENTER)
  719. {
  720. *key = RTGUIK_RETURN;
  721. return RT_TRUE;
  722. }
  723. return RT_FALSE;
  724. }
  725. void kbd_event_set_key(struct rtgui_event_kbd *ekbd, rt_uint16_t key)
  726. {
  727. RTGUI_EVENT_KBD_INIT(ekbd);
  728. ekbd->mod = RTGUI_KMOD_NONE;
  729. ekbd->unicode = 0;
  730. ekbd->key = key;
  731. ekbd->type = RTGUI_KEYDOWN;
  732. }
  733. static rt_bool_t rtgui_edit_onkey(struct rtgui_object *object, rtgui_event_t *event)
  734. {
  735. enum { EDIT_NONE, EDIT_ONDRAW, EDIT_UPDATE };
  736. struct rtgui_edit *edit = RTGUI_EDIT(object);
  737. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event;
  738. struct edit_line *line = RT_NULL;
  739. rt_bool_t update_type = EDIT_NONE;
  740. RT_ASSERT(edit != RT_NULL);
  741. RT_ASSERT(ekbd != RT_NULL);
  742. if (RTGUI_KBD_IS_UP(ekbd))
  743. {
  744. /* reset function key */
  745. if (ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
  746. edit->flag &= ~RTGUI_EDIT_CTRL;
  747. else if (ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
  748. edit->flag &= ~RTGUI_EDIT_ALT;
  749. else if (ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
  750. edit->flag &= ~RTGUI_EDIT_SHIFT;
  751. else if (ekbd->key == RTGUIK_CAPSLOCK)
  752. edit->flag &= ~RTGUI_EDIT_CAPSLOCK;
  753. else if (ekbd->key == RTGUIK_NUMLOCK)
  754. edit->flag &= ~RTGUI_EDIT_NUMLOCK;
  755. return RT_TRUE;
  756. }
  757. line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  758. if (line == RT_NULL)
  759. return RT_FALSE;
  760. /* rt_kprintf("key=%04X ",ekbd->key); */
  761. if (ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
  762. {
  763. /* use CTRL key */
  764. edit->flag |= RTGUI_EDIT_CTRL;
  765. return RT_FALSE;
  766. }
  767. else if (ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
  768. {
  769. /* use ALT key */
  770. edit->flag |= RTGUI_EDIT_ALT;
  771. return RT_FALSE;
  772. }
  773. else if (ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
  774. {
  775. /* use SHIFT key */
  776. edit->flag |= RTGUI_EDIT_SHIFT;
  777. return RT_FALSE;
  778. }
  779. else if (ekbd->key == RTGUIK_CAPSLOCK)
  780. {
  781. edit->flag |= RTGUI_EDIT_CAPSLOCK;
  782. return RT_FALSE;
  783. }
  784. else if (ekbd->key == RTGUIK_NUMLOCK)
  785. {
  786. edit->flag |= RTGUI_EDIT_NUMLOCK;
  787. return RT_FALSE;
  788. }
  789. else if (ekbd->key == RTGUIK_DELETE)
  790. {
  791. /* delete latter character */
  792. int ofs = edit->upleft.x + edit->visual.x;
  793. if (ofs > line->len - 1 || (ofs == 0 && line->len == 0))
  794. {
  795. /* will the next line marges into the current line */
  796. struct edit_line *next_line = line->next;
  797. if (next_line != RT_NULL)
  798. {
  799. struct edit_line *update_end_line;
  800. update_type = EDIT_UPDATE;
  801. edit->update.start = edit->visual;
  802. rtgui_edit_connect_line(edit, line, next_line);
  803. rtgui_edit_delete_line(edit, next_line);
  804. if (edit->max_rows - edit->upleft.y > edit->row_per_page)
  805. {
  806. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page);
  807. if (update_end_line != RT_NULL)
  808. {
  809. edit->update.end.x = edit->col_per_page;
  810. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  811. }
  812. }
  813. else
  814. {
  815. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  816. edit->update.end.x = edit->col_per_page;
  817. edit->update.end.y = update_end_index + 1;
  818. }
  819. }
  820. line->len = rtgui_edit_line_strlen(line->text);
  821. goto _edit_exit;
  822. }
  823. else if (ofs == line->len - 1)
  824. {
  825. line->text[ofs] = '\0';
  826. }
  827. else
  828. {
  829. char *c;
  830. rt_int16_t tmp_pos = 1;
  831. identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
  832. /* remove character */
  833. for (c = &line->text[ofs]; c[tmp_pos] != '\0'; c++)
  834. *c = c[tmp_pos];
  835. *c = '\0';
  836. }
  837. update_type = EDIT_UPDATE;
  838. edit->update.start = edit->visual;
  839. edit->update.end.x = line->len - edit->upleft.x;
  840. if (edit->update.end.x > edit->col_per_page)
  841. edit->update.end.x = edit->col_per_page;
  842. edit->update.end.y = edit->visual.y;
  843. }
  844. else if (ekbd->key == RTGUIK_BACKSPACE)
  845. {
  846. if (edit->visual.x == 0)
  847. {
  848. /* incorporated into prev line */
  849. struct rtgui_event_kbd event_kbd;
  850. struct edit_line *prev_line = line->prev;
  851. if (prev_line != RT_NULL)
  852. {
  853. struct edit_line *update_end_line;
  854. update_type = EDIT_UPDATE;
  855. edit->visual.x = prev_line->len;
  856. rtgui_edit_connect_line(edit, prev_line, line);
  857. kbd_event_set_key(&event_kbd, RTGUIK_UP);
  858. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  859. rtgui_edit_delete_line(edit, line);
  860. edit->update.start = edit->visual; /* update.start.y is changed */
  861. if (edit->max_rows - edit->upleft.y > edit->row_per_page)
  862. {
  863. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page);
  864. if (update_end_line != RT_NULL)
  865. {
  866. edit->update.end.x = edit->col_per_page;
  867. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  868. }
  869. }
  870. else
  871. {
  872. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  873. edit->update.end.x = edit->col_per_page;
  874. edit->update.end.y = update_end_index + 1;
  875. }
  876. }
  877. goto _edit_exit;
  878. }
  879. /* delete front character */
  880. if (edit->visual.x == line->len)
  881. {
  882. rt_int16_t tmp_pos = 1;
  883. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  884. line->text[edit->visual.x - tmp_pos] = '\0';
  885. edit->visual.x -= tmp_pos;
  886. }
  887. else if (edit->visual.x != 0)
  888. {
  889. /* remove current character */
  890. char *c;
  891. rt_int16_t tmp_pos = 1;
  892. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  893. /* remove character */
  894. for (c = &line->text[edit->visual.x - tmp_pos]; c[tmp_pos] != '\0'; c++)
  895. {
  896. *c = c[tmp_pos];
  897. }
  898. *c = '\0';
  899. edit->visual.x -= tmp_pos;
  900. }
  901. /* adjusted line buffer length */
  902. if (rtgui_edit_alloc_len(edit->bzsize, line->len + 2) < line->zsize)
  903. {
  904. line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len + 1);
  905. line->text = rt_realloc(line->text, line->zsize);
  906. }
  907. update_type = EDIT_UPDATE;
  908. edit->update.start = edit->visual;
  909. edit->update.end.x = line->len;
  910. edit->update.end.y = edit->visual.y;
  911. }
  912. else if (ekbd->key == RTGUIK_UP)
  913. {
  914. /* move to prev line */
  915. struct edit_line *prev_line;
  916. if (edit->visual.y > 0)
  917. edit->visual.y --;
  918. else
  919. {
  920. /* change first row */
  921. if (edit->upleft.y > 0)
  922. {
  923. edit->upleft.y --;
  924. if (edit->first_line->prev != RT_NULL)
  925. edit->first_line = edit->first_line->prev;
  926. update_type = EDIT_ONDRAW;
  927. }
  928. }
  929. /* The position of the recount X */
  930. prev_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  931. if (prev_line == RT_NULL)
  932. return RT_FALSE;
  933. if (edit->upleft.x > 0)
  934. {
  935. if (prev_line->len <= edit->upleft.x)
  936. {
  937. if (prev_line->len <= edit->col_per_page)
  938. {
  939. edit->upleft.x = 0;
  940. edit->visual.x = prev_line->len;
  941. }
  942. else
  943. {
  944. edit->upleft.x = prev_line->len - (edit->col_per_page - 1);
  945. edit->visual.x = edit->col_per_page - 1;
  946. }
  947. update_type = EDIT_ONDRAW;
  948. }
  949. else if (prev_line->len - edit->upleft.x < edit->col_per_page)
  950. {
  951. if (edit->visual.x > prev_line->len - edit->upleft.x)
  952. edit->visual.x = prev_line->len - edit->upleft.x;
  953. else
  954. {
  955. rt_int16_t tmp_pos = 0;
  956. if (identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
  957. edit->visual.x -= (2 - tmp_pos);
  958. }
  959. }
  960. }
  961. else if (edit->visual.x > prev_line->len)
  962. edit->visual.x = prev_line->len;
  963. else if (prev_line->len >= 2)
  964. {
  965. rt_int16_t tmp_pos = 0;
  966. if (identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
  967. edit->visual.x -= (2 - tmp_pos);
  968. }
  969. #ifdef RTGUI_EDIT_USING_SCROLL
  970. /* update vscroll */
  971. if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  972. {
  973. if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  974. rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y);
  975. }
  976. #endif
  977. }
  978. else if (ekbd->key == RTGUIK_DOWN)
  979. {
  980. struct edit_line *tail_line, *next_line;
  981. tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  982. if (tail_line != RT_NULL)
  983. {
  984. /* it is tail line */
  985. if (tail_line == edit->tail) return RT_FALSE;
  986. }
  987. /* move to next line */
  988. if (edit->visual.y < edit->row_per_page - 2)
  989. {
  990. edit->visual.y ++;
  991. }
  992. else if (edit->visual.y + edit->upleft.y < edit->max_rows - 1)
  993. {
  994. /* change first row */
  995. edit->upleft.y++;
  996. if (edit->first_line->next != RT_NULL)
  997. edit->first_line = edit->first_line->next;
  998. update_type = EDIT_ONDRAW;
  999. }
  1000. /* adjust next line end position */
  1001. next_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  1002. if (next_line == RT_NULL)
  1003. return RT_FALSE;
  1004. if (edit->upleft.x > 0)
  1005. {
  1006. if (next_line->len <= edit->upleft.x)
  1007. {
  1008. if (next_line->len <= edit->col_per_page)
  1009. {
  1010. edit->upleft.x = 0;
  1011. edit->visual.x = next_line->len;
  1012. }
  1013. else
  1014. {
  1015. edit->upleft.x = next_line->len - (edit->col_per_page - 1);
  1016. edit->visual.x = edit->col_per_page - 1;
  1017. }
  1018. update_type = EDIT_ONDRAW;
  1019. }
  1020. else if (next_line->len - edit->upleft.x < edit->col_per_page)
  1021. {
  1022. if (edit->visual.x > next_line->len - edit->upleft.x)
  1023. edit->visual.x = next_line->len - edit->upleft.x;
  1024. else
  1025. {
  1026. rt_int16_t tmp_pos = 0;
  1027. if (identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
  1028. edit->visual.x -= (2 - tmp_pos);
  1029. }
  1030. }
  1031. }
  1032. else if (edit->visual.x > next_line->len)
  1033. edit->visual.x = next_line->len;
  1034. else if (next_line->len >= 2)
  1035. {
  1036. rt_int16_t tmp_pos = 0;
  1037. if (identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
  1038. edit->visual.x -= (2 - tmp_pos);
  1039. }
  1040. #ifdef RTGUI_EDIT_USING_SCROLL
  1041. /* update vscroll */
  1042. if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
  1043. {
  1044. if (!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1045. rtgui_scrollbar_set_value(edit->vscroll, edit->upleft.y);
  1046. }
  1047. #endif
  1048. }
  1049. else if (ekbd->key == RTGUIK_LEFT)
  1050. {
  1051. /* move to prev char */
  1052. if (edit->visual.x > 0)
  1053. {
  1054. rt_int16_t tmp_pos = 1;
  1055. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  1056. edit->visual.x -= tmp_pos;
  1057. if (edit->visual.x == -1)
  1058. {
  1059. edit->visual.x = 0;
  1060. edit->upleft.x --;
  1061. update_type = EDIT_ONDRAW;
  1062. }
  1063. }
  1064. else
  1065. {
  1066. if (edit->upleft.x > 0)
  1067. {
  1068. rt_int16_t tmp_pos = 1;
  1069. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  1070. edit->upleft.x -= tmp_pos;
  1071. update_type = EDIT_ONDRAW;
  1072. }
  1073. else
  1074. {
  1075. struct rtgui_event_kbd event_kbd;
  1076. struct edit_line *first_line;
  1077. first_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  1078. if (first_line != RT_NULL)
  1079. {
  1080. /* it is head line */
  1081. if (first_line == edit->head) return RT_FALSE;
  1082. }
  1083. /* move the caret to the prev line end */
  1084. kbd_event_set_key(&event_kbd, RTGUIK_UP);
  1085. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1086. kbd_event_set_key(&event_kbd, RTGUIK_END);
  1087. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1088. }
  1089. }
  1090. }
  1091. else if (ekbd->key == RTGUIK_RIGHT)
  1092. {
  1093. /* move to next char */
  1094. if (line->len >= edit->col_per_page)
  1095. {
  1096. if (edit->upleft.x + edit->col_per_page <= line->len)
  1097. {
  1098. if (edit->visual.x < edit->col_per_page - 1)
  1099. {
  1100. rt_int16_t tmp_pos = 1;
  1101. identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
  1102. edit->visual.x += tmp_pos;
  1103. }
  1104. else if (edit->visual.x == edit->col_per_page - 1)
  1105. {
  1106. if (edit->upleft.x + edit->col_per_page < line->len)
  1107. edit->upleft.x ++;
  1108. else
  1109. edit->upleft.x = line->len - edit->col_per_page + 1;
  1110. update_type = EDIT_ONDRAW;
  1111. }
  1112. }
  1113. else
  1114. {
  1115. struct rtgui_event_kbd event_kbd;
  1116. /* move to next head */
  1117. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  1118. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1119. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  1120. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1121. }
  1122. }
  1123. else
  1124. {
  1125. if (edit->visual.x < line->len)
  1126. {
  1127. rt_int16_t tmp_pos = 1;
  1128. identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
  1129. edit->visual.x += tmp_pos;
  1130. }
  1131. else
  1132. {
  1133. struct rtgui_event_kbd event_kbd;
  1134. struct edit_line *tail_line;
  1135. tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
  1136. if (tail_line != RT_NULL)
  1137. {
  1138. /* it is tail line */
  1139. if (tail_line == edit->tail) return RT_FALSE;
  1140. }
  1141. /* move the caret to the next line head */
  1142. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  1143. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1144. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  1145. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1146. }
  1147. }
  1148. }
  1149. else if (ekbd->key == RTGUIK_HOME)
  1150. {
  1151. /* move cursor to line head */
  1152. edit->visual.x = 0;
  1153. if (edit->upleft.x > 0)
  1154. {
  1155. edit->upleft.x = 0;
  1156. update_type = EDIT_ONDRAW;
  1157. }
  1158. }
  1159. else if (ekbd->key == RTGUIK_END)
  1160. {
  1161. /* move cursor to line tail */
  1162. if (line->len >= edit->col_per_page)
  1163. {
  1164. edit->visual.x = edit->col_per_page - 1;
  1165. edit->upleft.x = line->len - (edit->col_per_page - 1);
  1166. update_type = EDIT_ONDRAW;
  1167. }
  1168. else
  1169. edit->visual.x = line->len;
  1170. }
  1171. else if (ekbd->key == RTGUIK_TAB)
  1172. {
  1173. int space_nums;
  1174. struct rtgui_event_kbd event_kbd;
  1175. /* using spaces to replace TAB */
  1176. space_nums = edit->tabsize - (edit->upleft.x + edit->visual.x) % edit->tabsize;
  1177. while (space_nums--)
  1178. {
  1179. kbd_event_set_key(&event_kbd, RTGUIK_SPACE);
  1180. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1181. }
  1182. }
  1183. else if (ekbd->key == RTGUIK_RETURN)
  1184. {
  1185. struct edit_line *update_end_line;
  1186. struct rtgui_event_kbd event_kbd;
  1187. /* insert a new line buffer */
  1188. rtgui_edit_insert_line(edit, line, line->text + edit->upleft.x + edit->visual.x);
  1189. line->text[edit->upleft.x + edit->visual.x] = '\0';
  1190. line->len = rtgui_edit_line_strlen(line->text);
  1191. if ((edit->max_rows - edit->upleft.y) < edit->row_per_page)
  1192. {
  1193. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  1194. update_type = EDIT_UPDATE;
  1195. edit->update.start = edit->visual;
  1196. edit->update.end.x = edit->tail->len;
  1197. edit->update.end.y = update_end_index;
  1198. }
  1199. else
  1200. {
  1201. /* nothing */
  1202. /* it will be adjusted upleft.y when entering DOWN case */
  1203. }
  1204. /* move the caret to the next line head */
  1205. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  1206. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1207. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  1208. rtgui_edit_onkey(object, (rtgui_event_t *)&event_kbd);
  1209. /* adjust update line end position */
  1210. if ((edit->max_rows - edit->upleft.y) >= edit->row_per_page)
  1211. {
  1212. update_type = EDIT_UPDATE;
  1213. edit->update.start = edit->visual;
  1214. edit->update.start.y -= 1;
  1215. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->row_per_page - 1);
  1216. if (update_end_line != RT_NULL)
  1217. {
  1218. edit->update.end.x = update_end_line->len;
  1219. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  1220. }
  1221. }
  1222. }
  1223. else
  1224. {
  1225. if (isprint((unsigned char)ekbd->key))
  1226. {
  1227. /* it's may print character */
  1228. update_type = EDIT_UPDATE;
  1229. edit->update.start = edit->visual;
  1230. if (edit->flag & RTGUI_EDIT_SHIFT)
  1231. ekbd->key = query_shift_code(ekbd->key);
  1232. if (edit->flag & RTGUI_EDIT_CAPSLOCK)
  1233. ekbd->key = query_caps_code(ekbd->key);
  1234. if (line->len < line->zsize - 1)
  1235. {
  1236. int ofs = edit->upleft.x + edit->visual.x;
  1237. if (edit->visual.x >= edit->col_per_page - 1)
  1238. {
  1239. edit->upleft.x ++;
  1240. update_type = EDIT_ONDRAW;
  1241. }
  1242. if (ofs < line->len)
  1243. {
  1244. char *c;
  1245. for (c = &line->text[line->len]; c != &line->text[ofs]; c--)
  1246. *c = *(c - 1);
  1247. }
  1248. line->text[ofs] = (char)ekbd->key;
  1249. if (edit->visual.x < edit->col_per_page - 1)
  1250. edit->visual.x ++;
  1251. line->text[line->len + 1] = '\0';
  1252. line->len = rtgui_edit_line_strlen(line->text);
  1253. edit->update.end.x = line->len;
  1254. if (edit->update.end.x > edit->col_per_page)
  1255. edit->update.end.x = edit->col_per_page;
  1256. edit->update.end.y = edit->visual.y;
  1257. }
  1258. else
  1259. {
  1260. /* adjust line buffer's zone size */
  1261. line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len + 1);
  1262. line->text = rt_realloc(line->text, line->zsize);
  1263. rtgui_edit_onkey(object, event); /* reentry */
  1264. }
  1265. }
  1266. else
  1267. {
  1268. /* Is small keyboard ? */
  1269. if (edit->flag & RTGUI_EDIT_NUMLOCK)
  1270. {
  1271. if (is_small_keyboard(&ekbd->key))
  1272. rtgui_edit_onkey(object, event);
  1273. /* small keyboard another value reserved */
  1274. }
  1275. }
  1276. }
  1277. line->len = rtgui_edit_line_strlen(line->text);
  1278. _edit_exit:
  1279. if (edit->flag & RTGUI_EDIT_CARET)
  1280. {
  1281. if (edit->caret_timer != RT_NULL)
  1282. rtgui_timer_stop(edit->caret_timer);
  1283. edit->flag &= ~RTGUI_EDIT_CARET;
  1284. rtgui_edit_draw_caret(edit);/* refresh it */
  1285. if (edit->caret_timer != RT_NULL)
  1286. rtgui_timer_start(edit->caret_timer);
  1287. }
  1288. /* re-draw edit widget */
  1289. if (update_type == EDIT_ONDRAW)
  1290. rtgui_edit_ondraw(edit);
  1291. else if (update_type == EDIT_UPDATE)
  1292. rtgui_edit_update(edit);
  1293. if (RTGUI_WIDGET_IS_FOCUSED(edit))
  1294. {
  1295. rtgui_edit_init_caret(edit, edit->visual);
  1296. edit->flag |= RTGUI_EDIT_CARET;
  1297. rtgui_edit_draw_caret(edit);
  1298. }
  1299. return RT_TRUE;
  1300. }
  1301. static rt_bool_t rtgui_edit_onfocus(struct rtgui_object *object, rtgui_event_t *event)
  1302. {
  1303. struct rtgui_edit *edit = RTGUI_EDIT(object);
  1304. edit->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC,
  1305. rtgui_edit_timeout, (void *)edit);
  1306. /* set caret to show */
  1307. edit->flag |= RTGUI_EDIT_CARET;
  1308. /* start caret timer */
  1309. if (edit->caret_timer != RT_NULL)
  1310. rtgui_timer_start(edit->caret_timer);
  1311. return RT_TRUE;
  1312. }
  1313. static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object *object, rtgui_event_t *event)
  1314. {
  1315. struct rtgui_edit *edit = RTGUI_EDIT(object);
  1316. /* stop caret timer */
  1317. if (edit->caret_timer != RT_NULL)
  1318. {
  1319. rtgui_timer_stop(edit->caret_timer);
  1320. rtgui_timer_destory(edit->caret_timer);
  1321. }
  1322. /* set caret to hide */
  1323. edit->flag &= ~RTGUI_EDIT_CARET;
  1324. rtgui_edit_draw_caret(edit);
  1325. return RT_TRUE;
  1326. }
  1327. #ifdef RTGUI_EDIT_USING_SCROLL
  1328. static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event)
  1329. {
  1330. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1331. /* adjust first display row when dragging */
  1332. edit->upleft.y = edit->hscroll->value;
  1333. rtgui_edit_ondraw(edit);
  1334. return RT_TRUE;
  1335. }
  1336. static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget *widget, rtgui_event_t *event)
  1337. {
  1338. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1339. /* adjust first display row when dragging */
  1340. edit->upleft.x = edit->vscroll->value;
  1341. rtgui_edit_ondraw(edit);
  1342. return RT_TRUE;
  1343. }
  1344. #endif
  1345. /* local area update */
  1346. void rtgui_edit_update(struct rtgui_edit *edit)
  1347. {
  1348. rt_int16_t i, cpy_len = 0, prev_len;
  1349. rtgui_rect_t rect, r;
  1350. struct rtgui_dc *dc;
  1351. char *src;
  1352. RT_ASSERT(edit != RT_NULL);
  1353. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1354. if (dc == RT_NULL) return;
  1355. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1356. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1357. if (edit->update_buf == RT_NULL)
  1358. {
  1359. /* try again allocate */
  1360. edit->update_buf = rtgui_malloc(edit->col_per_page + 1);
  1361. }
  1362. prev_len = edit->col_per_page;
  1363. for (i = edit->update.start.y; i <= edit->update.end.y; i++)
  1364. {
  1365. struct edit_line *line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + i);
  1366. if (i > edit->upleft.y + edit->row_per_page) break;
  1367. if (line == RT_NULL)
  1368. {
  1369. /* when use "RTGUIK_BACKSPACE" backspace forward,
  1370. * the last line is blank, that only fill background. */
  1371. r.x1 = rect.x1;
  1372. r.x2 = rect.x1 + prev_len * edit->font_width;
  1373. r.y1 = rect.y1 + i * edit->font_height;
  1374. r.y2 = r.y1 + edit->font_height;
  1375. if (r.x2 > rect.x2) r.x2 = rect.x2;
  1376. if (r.y2 > rect.y2) r.y2 = rect.y2;
  1377. rtgui_dc_fill_rect(dc, &r);
  1378. break;
  1379. }
  1380. if (edit->update.start.y == edit->update.end.y)
  1381. {
  1382. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1383. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1384. r.y1 = rect.y1 + i * edit->font_height;
  1385. r.y2 = r.y1 + edit->font_height;
  1386. cpy_len = edit->update.end.x - edit->update.start.x;
  1387. src = line->text + edit->update.start.x + edit->upleft.x;
  1388. }
  1389. else
  1390. {
  1391. if (i == edit->update.start.y)
  1392. {
  1393. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1394. r.x2 = rect.x2;
  1395. r.y1 = rect.y1 + i * edit->font_height;
  1396. r.y2 = r.y1 + edit->font_height;
  1397. cpy_len = line->len - edit->update.start.x - edit->upleft.x;
  1398. if (cpy_len > (edit->col_per_page - edit->update.start.x))
  1399. cpy_len = edit->col_per_page - edit->update.start.x;
  1400. src = line->text + edit->update.start.x + edit->upleft.x;
  1401. }
  1402. else if (i == edit->update.end.y)
  1403. {
  1404. r.x1 = rect.x1;
  1405. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1406. cpy_len = edit->update.end.x;
  1407. r.y1 = rect.y1 + i * edit->font_height;
  1408. r.y2 = r.y1 + edit->font_height;
  1409. src = line->text + edit->upleft.x;
  1410. }
  1411. else
  1412. {
  1413. r.x1 = rect.x1;
  1414. r.x2 = rect.x2;
  1415. r.y1 = rect.y1 + i * edit->font_height;
  1416. r.y2 = r.y1 + edit->font_height;
  1417. cpy_len = line->len - edit->upleft.x;
  1418. if (cpy_len > edit->col_per_page)
  1419. cpy_len = edit->col_per_page;
  1420. src = line->text + edit->upleft.x;
  1421. }
  1422. }
  1423. if (r.y1 > rect.y2) break;
  1424. if (r.x2 > rect.x2) r.x2 = rect.x2;
  1425. if (r.y2 > rect.y2) r.y2 = rect.y2;
  1426. rt_memcpy(edit->update_buf, src, cpy_len);
  1427. *(edit->update_buf + cpy_len) = '\0';
  1428. rtgui_dc_fill_rect(dc, &r);
  1429. rtgui_dc_draw_text(dc, edit->update_buf, &r);
  1430. prev_len = line->len;
  1431. }
  1432. rtgui_dc_end_drawing(dc);
  1433. }
  1434. void rtgui_edit_ondraw(struct rtgui_edit *edit)
  1435. {
  1436. rtgui_rect_t rect, r;
  1437. struct rtgui_dc *dc;
  1438. #ifdef RTGUI_EDIT_USING_SCROLL
  1439. int hscroll_flag = 0;
  1440. int vscroll_flag = 0;
  1441. #endif
  1442. RT_ASSERT(edit != RT_NULL);
  1443. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1444. if (dc == RT_NULL) return;
  1445. /* get widget rect */
  1446. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1447. rtgui_rect_inflate(&rect, -RTGUI_WIDGET_BORDER(edit));
  1448. /* fill widget rect with edit background color */
  1449. RTGUI_DC_BC(dc) = RTGUI_WIDGET_BACKGROUND(edit);
  1450. rtgui_dc_fill_rect(dc, &rect);
  1451. rtgui_rect_inflate(&rect, RTGUI_WIDGET_BORDER(edit));
  1452. /* draw border */
  1453. rtgui_dc_draw_border(dc, &rect, RTGUI_WIDGET_BORDER_STYLE(edit));
  1454. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1455. #ifdef RTGUI_EDIT_USING_SCROLL
  1456. if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1457. {
  1458. rect.x2 = rect.x2 - rtgui_rect_width(edit->vscroll->parent.extent);
  1459. }
  1460. if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1461. {
  1462. rect.y2 = rect.y2 - rtgui_rect_height(edit->hscroll->parent.extent);
  1463. }
  1464. #endif
  1465. r = rect;
  1466. /* draw text */
  1467. if (edit->head != RT_NULL)
  1468. {
  1469. struct edit_line *line = edit->first_line;
  1470. int num = 0;
  1471. rect.y2 = rect.y1 + edit->item_height;
  1472. while (line)
  1473. {
  1474. rt_int16_t tmp_pos = 0, ofs;
  1475. char *str = line->text + edit->upleft.x;
  1476. if (edit->upleft.x < line->len)
  1477. {
  1478. rtgui_point_t p = edit->visual; /* backup */
  1479. edit->visual.x = 0;
  1480. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  1481. ofs = tmp_pos % 2;
  1482. if (ofs == 1)
  1483. {
  1484. /* use dc_buffer draw the left half of double byte */
  1485. char dbl_bmp[3];
  1486. rtgui_point_t pot = {0};
  1487. rtgui_rect_t r = {0};
  1488. pot.x = edit->font_width;
  1489. r.x2 = edit->font_width * 2;
  1490. r.y2 = edit->font_height;
  1491. dbl_bmp[0] = *(str - 1);
  1492. dbl_bmp[1] = *str;
  1493. dbl_bmp[2] = '\0';
  1494. RTGUI_DC_BC(edit->dbl_buf) = RTGUI_WIDGET_BACKGROUND(edit);
  1495. rtgui_dc_fill_rect(edit->dbl_buf, &r);
  1496. RTGUI_DC_FC(edit->dbl_buf) = RTGUI_WIDGET_FOREGROUND(edit);
  1497. rtgui_dc_draw_text(edit->dbl_buf, dbl_bmp, &r);
  1498. rtgui_dc_blit(edit->dbl_buf, &pot, dc, &rect);
  1499. }
  1500. rect.x1 += ofs * edit->font_width;
  1501. rtgui_dc_draw_text(dc, line->text + edit->upleft.x + ofs, &rect);
  1502. rect.x1 -= ofs * edit->font_width;
  1503. edit->visual = p; /* restore */
  1504. }
  1505. line = line->next;
  1506. rect.y1 += edit->item_height;
  1507. if ((rect.y1 + edit->item_height) < r.y2)
  1508. rect.y2 = rect.y1 + edit->item_height;
  1509. else
  1510. rect.y2 = r.y2;
  1511. if (num++ >= edit->row_per_page)
  1512. break;
  1513. }
  1514. }
  1515. #ifdef RTGUI_EDIT_USING_SCROLL
  1516. if (edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1517. {
  1518. hscroll_flag = 1;
  1519. rtgui_scrollbar_ondraw(edit->hscroll);
  1520. }
  1521. if (edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1522. {
  1523. vscroll_flag = 1;
  1524. rtgui_scrollbar_ondraw(edit->vscroll);
  1525. }
  1526. if (hscroll_flag && vscroll_flag)
  1527. {
  1528. rtgui_color_t _bc;
  1529. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1530. rect.x1 = rect.x2 - RTGUI_WIDGET_BORDER(edit);
  1531. rect.y1 = rect.y2 - RTGUI_WIDGET_BORDER(edit);
  1532. _bc = RTGUI_DC_BC(dc);
  1533. RTGUI_DC_BC(dc) = default_background;
  1534. rtgui_dc_fill_rect(dc, &rect);
  1535. RTGUI_DC_BC(dc) = _bc;
  1536. }
  1537. #endif
  1538. rtgui_dc_end_drawing(dc);
  1539. }
  1540. /* set edit text */
  1541. void rtgui_edit_set_text(struct rtgui_edit *edit, const char *text)
  1542. {
  1543. const char *begin, *ptr;
  1544. #ifdef RTGUI_EDIT_USING_SCROLL
  1545. int hscroll_flag = 0;
  1546. int vscroll_flag = 0;
  1547. #endif
  1548. RT_ASSERT(edit != RT_NULL);
  1549. while (edit->max_rows > 0)
  1550. rtgui_edit_delete_line(edit, edit->head);
  1551. edit->max_rows = 0;
  1552. begin = text;
  1553. for (ptr = begin; *ptr != '\0'; ptr++)
  1554. {
  1555. if (*ptr == 0x0A)
  1556. {
  1557. /* unix style */
  1558. rtgui_edit_append_line(edit, begin);
  1559. begin = ptr + 1;
  1560. }
  1561. else if (*ptr == 0x0D && *(ptr + 1) == 0x0A)
  1562. {
  1563. /* windows style */
  1564. rtgui_edit_append_line(edit, begin);
  1565. begin = ptr + 2;
  1566. }
  1567. }
  1568. if (*ptr == '\0')
  1569. {
  1570. if (begin < ptr)
  1571. rtgui_edit_append_line(edit, begin);
  1572. }
  1573. #ifdef RTGUI_EDIT_USING_SCROLL
  1574. if (edit->hscroll != RT_NULL)
  1575. {
  1576. if (edit->max_cols > edit->col_per_page)
  1577. {
  1578. RTGUI_WIDGET_SHOW(edit->hscroll);
  1579. rtgui_scrollbar_set_line_step(edit->hscroll, 1);
  1580. rtgui_scrollbar_set_page_step(edit->hscroll, edit->col_per_page);
  1581. rtgui_scrollbar_set_range(edit->hscroll, edit->max_cols);
  1582. hscroll_flag = 1;
  1583. }
  1584. else
  1585. {
  1586. RTGUI_WIDGET_HIDE(edit->hscroll);
  1587. }
  1588. }
  1589. if (edit->vscroll != RT_NULL)
  1590. {
  1591. if (edit->max_rows > edit->row_per_page)
  1592. {
  1593. RTGUI_WIDGET_SHOW(edit->vscroll);
  1594. rtgui_scrollbar_set_line_step(edit->vscroll, 1);
  1595. rtgui_scrollbar_set_page_step(edit->vscroll, edit->row_per_page);
  1596. rtgui_scrollbar_set_range(edit->vscroll, edit->max_rows);
  1597. vscroll_flag = 1;
  1598. }
  1599. else
  1600. {
  1601. RTGUI_WIDGET_HIDE(edit->vscroll);
  1602. }
  1603. }
  1604. if (edit->hscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1605. {
  1606. rtgui_edit_adjust_scroll(edit->hscroll);
  1607. }
  1608. if (edit->vscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1609. {
  1610. rtgui_edit_adjust_scroll(edit->vscroll);
  1611. }
  1612. if (hscroll_flag || vscroll_flag)
  1613. {
  1614. rtgui_widget_update_clip(RTGUI_WIDGET(edit));
  1615. }
  1616. #endif
  1617. }
  1618. rt_bool_t rtgui_edit_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  1619. {
  1620. rtgui_widget_t *widget = RTGUI_WIDGET(object);
  1621. struct rtgui_edit *edit = RTGUI_EDIT(object);
  1622. switch (event->type)
  1623. {
  1624. case RTGUI_EVENT_PAINT:
  1625. #ifndef RTGUI_USING_SMALL_SIZE
  1626. if (widget->on_draw != RT_NULL)
  1627. widget->on_draw(object, event);
  1628. else
  1629. #endif
  1630. rtgui_edit_ondraw(edit);
  1631. break;
  1632. case RTGUI_EVENT_MOUSE_BUTTON:
  1633. #ifndef RTGUI_USING_SMALL_SIZE
  1634. if (widget->on_mouseclick != RT_NULL)
  1635. widget->on_mouseclick(object, event);
  1636. else
  1637. #endif
  1638. rtgui_edit_onmouse(edit, (struct rtgui_event_mouse *)event);
  1639. return RT_TRUE;
  1640. case RTGUI_EVENT_KBD:
  1641. #ifndef RTGUI_USING_SMALL_SIZE
  1642. if (widget->on_key != RT_NULL)
  1643. widget->on_key(object, event);
  1644. else
  1645. #endif
  1646. rtgui_edit_onkey(object, event);
  1647. return RT_TRUE;
  1648. default:
  1649. return rtgui_widget_event_handler(object, event);
  1650. }
  1651. return RT_FALSE;
  1652. }
  1653. rtgui_point_t rtgui_edit_get_current_point(struct rtgui_edit *edit)
  1654. {
  1655. rtgui_point_t p;
  1656. RT_ASSERT(edit != RT_NULL);
  1657. p.x = edit->upleft.x + edit->visual.x;
  1658. p.y = edit->upleft.y + edit->visual.y;
  1659. return p;
  1660. }
  1661. rt_uint32_t rtgui_edit_get_mem_consume(struct rtgui_edit *edit)
  1662. {
  1663. rt_uint32_t mem_size;
  1664. struct edit_line *line;
  1665. mem_size = sizeof(struct rtgui_edit);
  1666. mem_size += edit->col_per_page + 1; /* update_buf */
  1667. if (edit->head != RT_NULL)
  1668. {
  1669. line = edit->head;
  1670. while (line)
  1671. {
  1672. mem_size += line->zsize;
  1673. mem_size += sizeof(struct edit_line);
  1674. line = line->next;
  1675. }
  1676. }
  1677. return mem_size;
  1678. }
  1679. #ifdef RTGUI_USING_DFS_FILERW
  1680. /**
  1681. * File access component, General File Access Interface
  1682. */
  1683. rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename)
  1684. {
  1685. struct rtgui_filerw *filerw;
  1686. int num = 0, read_bytes, size , len = 0;
  1687. char *text , ch;
  1688. filerw = rtgui_filerw_create_file(filename, "rb");
  1689. if (filerw == RT_NULL) return RT_FALSE;
  1690. /**
  1691. * If it was in the debug of the win32, If document encode is UTF-8 or Unicode,
  1692. * Will read to garbled code when using the function read documents.
  1693. * You can Change of the document contains the source code for ANSI.
  1694. */
  1695. while (edit->max_rows > 0)
  1696. rtgui_edit_delete_line(edit, edit->head);
  1697. edit->max_rows = 0;
  1698. size = edit->bzsize;
  1699. text = rtgui_malloc(size);
  1700. if (text == RT_NULL) return RT_FALSE;
  1701. do
  1702. {
  1703. if ((read_bytes = rtgui_filerw_read(filerw, &ch, 1, 1)) > 0)
  1704. {
  1705. /* rt_kprintf("ch=%02X ",ch); DEBUG */
  1706. if (num >= size - 1)
  1707. text = rt_realloc(text, rtgui_edit_alloc_len(size, num));
  1708. if (ch == 0x09) //Tab
  1709. {
  1710. len = edit->tabsize - num % edit->tabsize;
  1711. while (len--)
  1712. *(text + num++) = ' ';
  1713. }
  1714. else
  1715. *(text + num++) = ch;
  1716. if (ch == 0x0A)
  1717. {
  1718. rtgui_edit_append_line(edit, text);
  1719. num = 0;
  1720. }
  1721. }
  1722. else if (num > 0)
  1723. {
  1724. /* last line does not exist the end operator */
  1725. *(text + num) = '\0';
  1726. rtgui_edit_append_line(edit, text);
  1727. }
  1728. }
  1729. while (read_bytes);
  1730. rtgui_filerw_close(filerw);
  1731. rtgui_free(text);
  1732. rtgui_edit_ondraw(edit);
  1733. return RT_TRUE;
  1734. }
  1735. rt_bool_t rtgui_edit_saveas_file(struct rtgui_edit *edit, const char *filename)
  1736. {
  1737. struct rtgui_filerw *filerw;
  1738. char ch_tailed = 0x0A;
  1739. struct edit_line *line;
  1740. filerw = rtgui_filerw_create_file(filename, "wb");
  1741. if (filerw == RT_NULL) return RT_FALSE;
  1742. line = edit->head;
  1743. while (line)
  1744. {
  1745. rtgui_filerw_write(filerw, line->text, line->len, 1);
  1746. if (line != edit->tail)
  1747. rtgui_filerw_write(filerw, &ch_tailed, 1, 1);
  1748. line = line->next;
  1749. }
  1750. rtgui_filerw_close(filerw);
  1751. return RT_TRUE;
  1752. }
  1753. #endif