edit.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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(*(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(*(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. /* adjust update line end position */
  1156. if((edit->max_rows-edit->upleft.y) > edit->row_per_page)
  1157. {
  1158. update_type = EDIT_UPDATE;
  1159. edit->update.start = edit->visual;
  1160. update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page-1);
  1161. if(update_end_line != RT_NULL)
  1162. {
  1163. edit->update.end.x = update_end_line->len;
  1164. edit->update.end.y = edit->upleft.y + edit->row_per_page;
  1165. }
  1166. }
  1167. else if((edit->max_rows-edit->upleft.y) < edit->row_per_page)
  1168. {
  1169. int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
  1170. update_type = EDIT_UPDATE;
  1171. edit->update.start = edit->visual;
  1172. edit->update.end.x = edit->tail->len;
  1173. edit->update.end.y = update_end_index;
  1174. }
  1175. else
  1176. {
  1177. /* nothing */
  1178. /* it will be adjusted upleft.y when entering DOWN case */
  1179. }
  1180. /* move the caret to the next line head */
  1181. kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
  1182. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  1183. kbd_event_set_key(&event_kbd, RTGUIK_HOME);
  1184. rtgui_edit_onkey(object, (rtgui_event_t*)&event_kbd);
  1185. }
  1186. else
  1187. {
  1188. if(isprint((unsigned char)ekbd->key))
  1189. { /* it's may print character */
  1190. update_type = EDIT_UPDATE;
  1191. edit->update.start = edit->visual;
  1192. if(edit->flag & RTGUI_EDIT_SHIFT)
  1193. ekbd->key = query_shift_code(ekbd->key);
  1194. if(edit->flag & RTGUI_EDIT_CAPSLOCK)
  1195. ekbd->key = query_caps_code(ekbd->key);
  1196. if(line->len < line->zsize-1)
  1197. {
  1198. int ofs = edit->upleft.x + edit->visual.x;
  1199. if(edit->visual.x >= edit->col_per_page-1)
  1200. {
  1201. edit->upleft.x ++;
  1202. update_type = EDIT_ONDRAW;
  1203. }
  1204. if(ofs < line->len)
  1205. {
  1206. char* c;
  1207. for(c = &line->text[line->len]; c != &line->text[ofs]; c--)
  1208. *c = *(c-1);
  1209. }
  1210. line->text[ofs] = (char)ekbd->key;
  1211. if(edit->visual.x < edit->col_per_page-1)
  1212. edit->visual.x ++;
  1213. line->text[line->len+1] = '\0';
  1214. line->len = rtgui_edit_line_strlen(line->text);
  1215. edit->update.end.x = line->len;
  1216. if(edit->update.end.x > edit->col_per_page)
  1217. edit->update.end.x = edit->col_per_page;
  1218. edit->update.end.y = edit->visual.y;
  1219. }
  1220. else
  1221. { /* adjust line buffer's zone size */
  1222. line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
  1223. line->text = rt_realloc(line->text, line->zsize);
  1224. rtgui_edit_onkey(object, event); /* reentry */
  1225. }
  1226. }
  1227. else
  1228. {
  1229. /* Is small keyboard ? */
  1230. if(edit->flag & RTGUI_EDIT_NUMLOCK)
  1231. {
  1232. if(is_small_keyboard(&ekbd->key))
  1233. rtgui_edit_onkey(object, event);
  1234. /* small keyboard another value reserved */
  1235. }
  1236. }
  1237. }
  1238. line->len = rtgui_edit_line_strlen(line->text);
  1239. _edit_exit:
  1240. if(edit->flag & RTGUI_EDIT_CARET)
  1241. {
  1242. if(edit->caret_timer != RT_NULL)
  1243. rtgui_timer_stop(edit->caret_timer);
  1244. edit->flag &= ~RTGUI_EDIT_CARET;
  1245. rtgui_edit_draw_caret(edit);/* refresh it */
  1246. if(edit->caret_timer != RT_NULL)
  1247. rtgui_timer_start(edit->caret_timer);
  1248. }
  1249. /* re-draw edit widget */
  1250. if(update_type == EDIT_ONDRAW)
  1251. rtgui_edit_ondraw(edit);
  1252. else if(update_type == EDIT_UPDATE)
  1253. rtgui_edit_update(edit);
  1254. if(RTGUI_WIDGET_IS_FOCUSED(edit))
  1255. {
  1256. rtgui_edit_init_caret(edit, edit->visual);
  1257. edit->flag |= RTGUI_EDIT_CARET;
  1258. rtgui_edit_draw_caret(edit);
  1259. }
  1260. return RT_TRUE;
  1261. }
  1262. static rt_bool_t rtgui_edit_onfocus(struct rtgui_object* object, rtgui_event_t* event)
  1263. {
  1264. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1265. edit->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC,
  1266. rtgui_edit_timeout, (void*)edit);
  1267. /* set caret to show */
  1268. edit->flag |= RTGUI_EDIT_CARET;
  1269. /* start caret timer */
  1270. if(edit->caret_timer != RT_NULL)
  1271. rtgui_timer_start(edit->caret_timer);
  1272. return RT_TRUE;
  1273. }
  1274. static rt_bool_t rtgui_edit_onunfocus(struct rtgui_object* object, rtgui_event_t* event)
  1275. {
  1276. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1277. /* stop caret timer */
  1278. if(edit->caret_timer != RT_NULL)
  1279. {
  1280. rtgui_timer_stop(edit->caret_timer);
  1281. rtgui_timer_destory(edit->caret_timer);
  1282. }
  1283. /* set caret to hide */
  1284. edit->flag &= ~RTGUI_EDIT_CARET;
  1285. rtgui_edit_draw_caret(edit);
  1286. return RT_TRUE;
  1287. }
  1288. #ifdef RTGUI_EDIT_USING_SCROLL
  1289. static rt_bool_t rtgui_edit_hscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event)
  1290. {
  1291. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1292. /* adjust first display row when dragging */
  1293. edit->upleft.y = edit->hscroll->value;
  1294. rtgui_edit_ondraw(edit);
  1295. return RT_TRUE;
  1296. }
  1297. static rt_bool_t rtgui_edit_vscroll_handle(struct rtgui_widget* widget, rtgui_event_t* event)
  1298. {
  1299. struct rtgui_edit *edit = RTGUI_EDIT(widget);
  1300. /* adjust first display row when dragging */
  1301. edit->upleft.x = edit->vscroll->value;
  1302. rtgui_edit_ondraw(edit);
  1303. return RT_TRUE;
  1304. }
  1305. #endif
  1306. /* local area update */
  1307. void rtgui_edit_update(struct rtgui_edit *edit)
  1308. {
  1309. rt_int16_t i,cpy_len=0,prev_len;
  1310. rtgui_rect_t rect, r;
  1311. struct rtgui_dc *dc;
  1312. char *src;
  1313. RT_ASSERT(edit != RT_NULL);
  1314. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1315. if(dc == RT_NULL) return;
  1316. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1317. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1318. if(edit->update_buf == RT_NULL)
  1319. { /* try again allocate */
  1320. edit->update_buf = rtgui_malloc(edit->col_per_page+1);
  1321. }
  1322. prev_len = edit->col_per_page;
  1323. for(i=edit->update.start.y; i<=edit->update.end.y; i++)
  1324. {
  1325. struct edit_line *line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + i);
  1326. if(i > edit->upleft.y+edit->row_per_page) break;
  1327. if(line == RT_NULL)
  1328. { /* when use "RTGUIK_BACKSPACE" backspace forward,
  1329. * the last line is blank, that only fill background. */
  1330. r.x1 = rect.x1;
  1331. r.x2 = rect.x1 + prev_len * edit->font_width;
  1332. r.y1 = rect.y1 + i * edit->font_height;
  1333. r.y2 = r.y1 + edit->font_height;
  1334. if(r.x2 > rect.x2) r.x2 = rect.x2;
  1335. if(r.y2 > rect.y2) r.y2 = rect.y2;
  1336. rtgui_dc_fill_rect(dc, &r);
  1337. break;
  1338. }
  1339. if(edit->update.start.y == edit->update.end.y)
  1340. {
  1341. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1342. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1343. r.y1 = rect.y1 + i * edit->font_height;
  1344. r.y2 = r.y1 + edit->font_height;
  1345. cpy_len = edit->update.end.x - edit->update.start.x;
  1346. src = line->text + edit->update.start.x + edit->upleft.x;
  1347. }
  1348. else
  1349. {
  1350. if(i == edit->update.start.y)
  1351. {
  1352. r.x1 = rect.x1 + edit->update.start.x * edit->font_width;
  1353. r.x2 = rect.x2;
  1354. r.y1 = rect.y1 + i * edit->font_height;
  1355. r.y2 = r.y1 + edit->font_height;
  1356. cpy_len = line->len - edit->update.start.x - edit->upleft.x;
  1357. if(cpy_len > (edit->col_per_page-edit->update.start.x))
  1358. cpy_len = edit->col_per_page-edit->update.start.x;
  1359. src = line->text + edit->update.start.x + edit->upleft.x;
  1360. }
  1361. else if(i == edit->update.end.y)
  1362. {
  1363. r.x1 = rect.x1;
  1364. r.x2 = rect.x1 + edit->update.end.x * edit->font_width;
  1365. cpy_len = edit->update.end.x;
  1366. r.y1 = rect.y1 + i * edit->font_height;
  1367. r.y2 = r.y1 + edit->font_height;
  1368. src = line->text + edit->upleft.x;
  1369. }
  1370. else
  1371. {
  1372. r.x1 = rect.x1;
  1373. r.x2 = rect.x2;
  1374. r.y1 = rect.y1 + i * edit->font_height;
  1375. r.y2 = r.y1 + edit->font_height;
  1376. cpy_len = line->len - edit->upleft.x;
  1377. if(cpy_len > edit->col_per_page)
  1378. cpy_len = edit->col_per_page;
  1379. src = line->text + edit->upleft.x;
  1380. }
  1381. }
  1382. if(r.y1 > rect.y2) break;
  1383. if(r.x2 > rect.x2) r.x2 = rect.x2;
  1384. if(r.y2 > rect.y2) r.y2 = rect.y2;
  1385. rt_memcpy(edit->update_buf, src, cpy_len);
  1386. *(edit->update_buf + cpy_len) = '\0';
  1387. rtgui_dc_fill_rect(dc, &r);
  1388. rtgui_dc_draw_text(dc, edit->update_buf, &r);
  1389. prev_len = line->len;
  1390. }
  1391. rtgui_dc_end_drawing(dc);
  1392. }
  1393. void rtgui_edit_ondraw(struct rtgui_edit *edit)
  1394. {
  1395. rtgui_rect_t rect, r;
  1396. struct rtgui_dc *dc;
  1397. #ifdef RTGUI_EDIT_USING_SCROLL
  1398. int hscroll_flag=0;
  1399. int vscroll_flag=0;
  1400. #endif
  1401. RT_ASSERT(edit != RT_NULL);
  1402. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(edit));
  1403. if(dc == RT_NULL) return;
  1404. /* get widget rect */
  1405. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1406. rtgui_rect_inflate(&rect, -RTGUI_WIDGET_BORDER(edit));
  1407. /* fill widget rect with edit background color */
  1408. RTGUI_DC_BC(dc) = RTGUI_WIDGET_BACKGROUND(edit);
  1409. rtgui_dc_fill_rect(dc, &rect);
  1410. rtgui_rect_inflate(&rect, RTGUI_WIDGET_BORDER(edit));
  1411. /* draw border */
  1412. rtgui_dc_draw_border(dc, &rect, RTGUI_WIDGET_BORDER_STYLE(edit));
  1413. rtgui_rect_inflate(&rect, -(edit->margin + RTGUI_WIDGET_BORDER(edit)));
  1414. #ifdef RTGUI_EDIT_USING_SCROLL
  1415. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1416. {
  1417. rect.x2 = rect.x2 - rtgui_rect_width(edit->vscroll->parent.extent);
  1418. }
  1419. if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1420. {
  1421. rect.y2 = rect.y2 - rtgui_rect_height(edit->hscroll->parent.extent);
  1422. }
  1423. #endif
  1424. r = rect;
  1425. /* draw text */
  1426. if(edit->head != RT_NULL)
  1427. {
  1428. struct edit_line *line = edit->first_line;
  1429. int num=0;
  1430. rect.y2 = rect.y1 + edit->item_height;
  1431. while(line)
  1432. {
  1433. rt_int16_t tmp_pos=0, ofs;
  1434. char *str = line->text+edit->upleft.x;
  1435. if(edit->upleft.x < line->len)
  1436. {
  1437. rtgui_point_t p = edit->visual; /* backup */
  1438. edit->visual.x = 0;
  1439. identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
  1440. ofs = tmp_pos % 2;
  1441. if(ofs == 1)
  1442. { /* use dc_buffer draw the left half of double byte */
  1443. char dbl_bmp[3];
  1444. rtgui_point_t pot = {0};
  1445. rtgui_rect_t r = {0};
  1446. pot.x = edit->font_width;
  1447. r.x2 = edit->font_width*2;
  1448. r.y2 = edit->font_height;
  1449. dbl_bmp[0] = *(str-1);
  1450. dbl_bmp[1] = *str;
  1451. dbl_bmp[2] = '\0';
  1452. RTGUI_DC_BC(edit->dbl_buf) = RTGUI_WIDGET_BACKGROUND(edit);
  1453. rtgui_dc_fill_rect(edit->dbl_buf, &r);
  1454. RTGUI_DC_FC(edit->dbl_buf) = RTGUI_WIDGET_FOREGROUND(edit);
  1455. rtgui_dc_draw_text(edit->dbl_buf, dbl_bmp, &r);
  1456. rtgui_dc_blit(edit->dbl_buf, &pot, dc, &rect);
  1457. }
  1458. rect.x1 += ofs * edit->font_width;
  1459. rtgui_dc_draw_text(dc, line->text+edit->upleft.x+ofs, &rect);
  1460. rect.x1 -= ofs * edit->font_width;
  1461. edit->visual = p; /* restore */
  1462. }
  1463. line = line->next;
  1464. rect.y1 += edit->item_height;
  1465. if((rect.y1 + edit->item_height) < r.y2)
  1466. rect.y2 = rect.y1 + edit->item_height;
  1467. else
  1468. rect.y2 = r.y2;
  1469. if(num++ >= edit->row_per_page)
  1470. break;
  1471. }
  1472. }
  1473. #ifdef RTGUI_EDIT_USING_SCROLL
  1474. if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1475. {
  1476. hscroll_flag = 1;
  1477. rtgui_scrollbar_ondraw(edit->hscroll);
  1478. }
  1479. if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1480. {
  1481. vscroll_flag = 1;
  1482. rtgui_scrollbar_ondraw(edit->vscroll);
  1483. }
  1484. if(hscroll_flag && vscroll_flag)
  1485. {
  1486. rtgui_color_t _bc;
  1487. rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
  1488. rect.x1 = rect.x2-RTGUI_WIDGET_BORDER(edit);
  1489. rect.y1 = rect.y2-RTGUI_WIDGET_BORDER(edit);
  1490. _bc = RTGUI_DC_BC(dc);
  1491. RTGUI_DC_BC(dc) = default_background;
  1492. rtgui_dc_fill_rect(dc,&rect);
  1493. RTGUI_DC_BC(dc) = _bc;
  1494. }
  1495. #endif
  1496. rtgui_dc_end_drawing(dc);
  1497. }
  1498. /* set edit text */
  1499. void rtgui_edit_set_text(struct rtgui_edit* edit, const char* text)
  1500. {
  1501. const char *begin, *ptr;
  1502. #ifdef RTGUI_EDIT_USING_SCROLL
  1503. int hscroll_flag=0;
  1504. int vscroll_flag=0;
  1505. #endif
  1506. RT_ASSERT(edit != RT_NULL);
  1507. while(edit->max_rows > 0)
  1508. rtgui_edit_delete_line(edit, edit->head);
  1509. edit->max_rows = 0;
  1510. begin = text;
  1511. for(ptr=begin; *ptr != '\0'; ptr++)
  1512. {
  1513. if(*ptr == 0x0A)
  1514. { /* unix style */
  1515. rtgui_edit_append_line(edit, begin);
  1516. begin = ptr+1;
  1517. }
  1518. else if(*ptr == 0x0D && *(ptr+1) == 0x0A)
  1519. { /* windows style */
  1520. rtgui_edit_append_line(edit, begin);
  1521. begin = ptr+2;
  1522. }
  1523. }
  1524. if(*ptr == '\0')
  1525. {
  1526. if(begin < ptr)
  1527. rtgui_edit_append_line(edit, begin);
  1528. }
  1529. #ifdef RTGUI_EDIT_USING_SCROLL
  1530. if(edit->hscroll != RT_NULL)
  1531. {
  1532. if(edit->max_cols > edit->col_per_page)
  1533. {
  1534. RTGUI_WIDGET_SHOW(edit->hscroll);
  1535. rtgui_scrollbar_set_line_step(edit->hscroll, 1);
  1536. rtgui_scrollbar_set_page_step(edit->hscroll, edit->col_per_page);
  1537. rtgui_scrollbar_set_range(edit->hscroll, edit->max_cols);
  1538. hscroll_flag = 1;
  1539. }
  1540. else
  1541. {
  1542. RTGUI_WIDGET_HIDE(edit->hscroll);
  1543. }
  1544. }
  1545. if(edit->vscroll != RT_NULL)
  1546. {
  1547. if(edit->max_rows > edit->row_per_page)
  1548. {
  1549. RTGUI_WIDGET_SHOW(edit->vscroll);
  1550. rtgui_scrollbar_set_line_step(edit->vscroll, 1);
  1551. rtgui_scrollbar_set_page_step(edit->vscroll, edit->row_per_page);
  1552. rtgui_scrollbar_set_range(edit->vscroll, edit->max_rows);
  1553. vscroll_flag = 1;
  1554. }
  1555. else
  1556. {
  1557. RTGUI_WIDGET_HIDE(edit->vscroll);
  1558. }
  1559. }
  1560. if(edit->hscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->hscroll))
  1561. {
  1562. rtgui_edit_adjust_scroll(edit->hscroll);
  1563. }
  1564. if(edit->vscroll != RT_NULL && !RTGUI_WIDGET_IS_HIDE(edit->vscroll))
  1565. {
  1566. rtgui_edit_adjust_scroll(edit->vscroll);
  1567. }
  1568. if(hscroll_flag || vscroll_flag)
  1569. {
  1570. rtgui_widget_update_clip(RTGUI_WIDGET(edit));
  1571. }
  1572. #endif
  1573. }
  1574. rt_bool_t rtgui_edit_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  1575. {
  1576. rtgui_widget_t *widget = RTGUI_WIDGET(object);
  1577. struct rtgui_edit* edit = RTGUI_EDIT(object);
  1578. switch(event->type)
  1579. {
  1580. case RTGUI_EVENT_PAINT:
  1581. #ifndef RTGUI_USING_SMALL_SIZE
  1582. if(widget->on_draw != RT_NULL)
  1583. widget->on_draw(object, event);
  1584. else
  1585. #endif
  1586. rtgui_edit_ondraw(edit);
  1587. break;
  1588. case RTGUI_EVENT_MOUSE_BUTTON:
  1589. #ifndef RTGUI_USING_SMALL_SIZE
  1590. if(widget->on_mouseclick != RT_NULL)
  1591. widget->on_mouseclick(object, event);
  1592. else
  1593. #endif
  1594. rtgui_edit_onmouse(edit, (struct rtgui_event_mouse*)event);
  1595. return RT_TRUE;
  1596. case RTGUI_EVENT_KBD:
  1597. #ifndef RTGUI_USING_SMALL_SIZE
  1598. if(widget->on_key != RT_NULL)
  1599. widget->on_key(object, event);
  1600. else
  1601. #endif
  1602. rtgui_edit_onkey(object, event);
  1603. return RT_TRUE;
  1604. default:
  1605. return rtgui_widget_event_handler(object, event);
  1606. }
  1607. return RT_FALSE;
  1608. }
  1609. rtgui_point_t rtgui_edit_get_current_point(struct rtgui_edit *edit)
  1610. {
  1611. rtgui_point_t p;
  1612. RT_ASSERT(edit != RT_NULL);
  1613. p.x = edit->upleft.x + edit->visual.x;
  1614. p.y = edit->upleft.y + edit->visual.y;
  1615. return p;
  1616. }
  1617. rt_uint32_t rtgui_edit_get_mem_consume(struct rtgui_edit *edit)
  1618. {
  1619. rt_uint32_t mem_size;
  1620. struct edit_line *line;
  1621. mem_size = sizeof(struct rtgui_edit);
  1622. mem_size += edit->col_per_page + 1; /* update_buf */
  1623. if(edit->head != RT_NULL)
  1624. {
  1625. line = edit->head;
  1626. while(line)
  1627. {
  1628. mem_size += line->zsize;
  1629. mem_size += sizeof(struct edit_line);
  1630. line = line->next;
  1631. }
  1632. }
  1633. return mem_size;
  1634. }
  1635. /**
  1636. * File access component, General File Access Interface
  1637. */
  1638. rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename)
  1639. {
  1640. struct rtgui_filerw *filerw;
  1641. int num=0, read_bytes, size ,len=0;
  1642. char *text ,ch;
  1643. filerw = rtgui_filerw_create_file(filename, "rb");
  1644. if (filerw == RT_NULL) return RT_FALSE;
  1645. /**
  1646. * If it was in the debug of the win32, If document encode is UTF-8 or Unicode,
  1647. * Will read to garbled code when using the function read documents.
  1648. * You can Change of the document contains the source code for ANSI.
  1649. */
  1650. while(edit->max_rows > 0)
  1651. rtgui_edit_delete_line(edit, edit->head);
  1652. edit->max_rows = 0;
  1653. size = edit->bzsize;
  1654. text = rtgui_malloc(size);
  1655. if(text == RT_NULL) return RT_FALSE;
  1656. do {
  1657. if ( (read_bytes = rtgui_filerw_read(filerw, &ch, 1, 1)) > 0 )
  1658. { /* rt_kprintf("ch=%02X ",ch); DEBUG */
  1659. if(num >= size - 1)
  1660. text = rt_realloc(text, rtgui_edit_alloc_len(size, num));
  1661. if(ch == 0x09) //Tab
  1662. {
  1663. len = edit->tabsize - num%edit->tabsize;
  1664. while(len--)
  1665. *(text + num++) = ' ';
  1666. }
  1667. else
  1668. *(text + num++) = ch;
  1669. if(ch == 0x0A)
  1670. {
  1671. rtgui_edit_append_line(edit, text);
  1672. num = 0;
  1673. }
  1674. }
  1675. else if(num > 0)
  1676. { /* last line does not exist the end operator */
  1677. *(text + num) = '\0';
  1678. rtgui_edit_append_line(edit, text);
  1679. }
  1680. } while(read_bytes);
  1681. rtgui_filerw_close(filerw);
  1682. rtgui_free(text);
  1683. rtgui_edit_ondraw(edit);
  1684. return RT_TRUE;
  1685. }
  1686. rt_bool_t rtgui_edit_saveas_file(struct rtgui_edit *edit, const char *filename)
  1687. {
  1688. struct rtgui_filerw *filerw;
  1689. char ch_tailed = 0x0A;
  1690. struct edit_line *line;
  1691. filerw = rtgui_filerw_create_file(filename, "wb");
  1692. if (filerw == RT_NULL) return RT_FALSE;
  1693. line = edit->head;
  1694. while(line)
  1695. {
  1696. rtgui_filerw_write(filerw, line->text, line->len, 1);
  1697. if(line != edit->tail)
  1698. rtgui_filerw_write(filerw, &ch_tailed, 1, 1);
  1699. line = line->next;
  1700. }
  1701. rtgui_filerw_close(filerw);
  1702. return RT_TRUE;
  1703. }