demo_view_edit.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * 程序清单:edit控件演示
  3. *
  4. * 这个例子会在conatiner上进行edit控件的演示
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/dc.h>
  8. #include <rtgui/filerw.h>
  9. #include <rtgui/rtgui_system.h>
  10. #include <rtgui/widgets/edit.h>
  11. #include <rtgui/widgets/label.h>
  12. #include <rtgui/widgets/button.h>
  13. #ifdef RTGUI_USING_DFS_FILERW
  14. void demo_edit_readin_file(struct rtgui_object *object, struct rtgui_event *event)
  15. {
  16. rtgui_button_t *button;
  17. struct rtgui_edit *edit;
  18. const char *filename = "/test_readin.txt";
  19. struct rtgui_filerw *file;
  20. RT_ASSERT(object != RT_NULL);
  21. button = RTGUI_BUTTON(object);
  22. /* 取得edit指针 */
  23. edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
  24. /* 判断文件是否存在 */
  25. file = rtgui_filerw_create_file(filename, "rb");
  26. if (file == RT_NULL)
  27. {
  28. /* 不存在存在,则创建它 */
  29. rt_kprintf("file:\"%s\" does not exist!\n", filename);
  30. return;
  31. }
  32. rtgui_filerw_close(file);
  33. rt_kprintf("read-in file:\"%s\"\n", filename);
  34. rtgui_edit_readin_file(edit, filename);
  35. }
  36. void demo_edit_saveas_file(struct rtgui_object *object, struct rtgui_event *event)
  37. {
  38. rtgui_button_t *button;
  39. struct rtgui_edit *edit;
  40. const char* filename = "/test_saveas.txt";
  41. struct rtgui_filerw *file;
  42. RT_ASSERT(object != RT_NULL);
  43. button = RTGUI_BUTTON(object);
  44. /* 取得edit指针 */
  45. edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
  46. /* 判断文件是否存在, 如果存在则删除之 */
  47. file = rtgui_filerw_create_file(filename, "rb");
  48. if (file != RT_NULL)
  49. {
  50. rtgui_filerw_close(file);
  51. /* 如果是在win32中调试, 请手工删除该文件吧, NT中文件是只读的,unlink删除不掉 */
  52. if (rtgui_filerw_unlink(filename) == -1)
  53. rt_kprintf("Could not delete %s\n", filename);
  54. }
  55. rt_kprintf("save as file:\"%s\"\n", filename);
  56. rtgui_edit_saveas_file(edit, filename);
  57. }
  58. void demo_edit_get_mem(struct rtgui_object* object, struct rtgui_event* event)
  59. {
  60. rtgui_button_t *button;
  61. struct rtgui_edit *edit;
  62. RT_ASSERT(object != RT_NULL);
  63. button = RTGUI_BUTTON(object);
  64. edit = RTGUI_EDIT( RTGUI_WIDGET(button)->user_data );
  65. rt_kprintf("edit mem consume: %d\n", rtgui_edit_get_mem_consume(edit));
  66. }
  67. rt_bool_t demo_edit_event_handler(struct rtgui_object* object, struct rtgui_event *event)
  68. {
  69. rt_bool_t result;
  70. char buf[32];
  71. rtgui_point_t p;
  72. struct rtgui_edit *edit = RTGUI_EDIT(object);
  73. struct rtgui_label *label = (struct rtgui_label*)RTGUI_WIDGET(edit)->user_data;
  74. result = rtgui_edit_event_handler(object, event);
  75. p = rtgui_edit_get_current_point(edit);
  76. rt_sprintf(buf, "TRACK: line:%d, col:%d", p.y, p.x);
  77. rtgui_label_set_text(label, buf);
  78. return result;
  79. }
  80. /* 创建用于演示edit控件的视图 */
  81. rtgui_container_t *demo_view_edit(void)
  82. {
  83. rtgui_rect_t rect;
  84. rtgui_container_t *container;
  85. struct rtgui_edit *edit;
  86. struct rtgui_label *label;
  87. struct rtgui_button *button;
  88. /* 先创建一个演示用的视图 */
  89. container = demo_view("Edit View");
  90. edit = rtgui_edit_create(container, 10, 35, 220, 200);
  91. rtgui_edit_set_text(edit,
  92. "Edit beta v0.1\n"
  93. "Hello RT-Thread\n"
  94. "this's edit demo\n"
  95. "rtgui_edit_create\n"
  96. "rtgui_edit_append_line\n"
  97. "rtgui_edit_insert_line\n"
  98. "rtgui_edit_delete_line\n"
  99. "rtgui_edit_connect_line\n"
  100. "双字节测试\n"
  101. "a\n"
  102. "b\n"
  103. "c\n"
  104. "d\n"
  105. "1234567890\n");
  106. /* 创建一个按钮, 读取某个文件 */
  107. demo_view_get_rect(container, &rect);
  108. rect.x1 += 10;
  109. rect.x2 = rect.x1 + 100;
  110. rect.y1 += 200;
  111. rect.y2 = rect.y1 + 22;
  112. button = rtgui_button_create("ReadIn File");
  113. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  114. rtgui_container_add_child(container, RTGUI_WIDGET(button));
  115. rtgui_button_set_onbutton(button, demo_edit_readin_file);
  116. /* 使用user_data传递edit指针 */
  117. RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
  118. /* 创建一个按钮, 保存为某个文件 */
  119. demo_view_get_rect(container, &rect);
  120. rect.x1 += 130;
  121. rect.x2 = rect.x1 + 100;
  122. rect.y1 += 200;
  123. rect.y2 = rect.y1 + 22;
  124. button = rtgui_button_create("SaveAs File");
  125. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  126. rtgui_container_add_child(container, RTGUI_WIDGET(button));
  127. rtgui_button_set_onbutton(button, demo_edit_saveas_file);
  128. /* 使用user_data传递edit指针 */
  129. RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
  130. /* 创建一个标签, 显示EDIT的主要参数 */
  131. demo_view_get_rect(container, &rect);
  132. rect.x1 += 10;
  133. rect.x2 = rect.x1 + 220;
  134. rect.y1 += 225;
  135. rect.y2 = rect.y1 + 18;
  136. label = rtgui_label_create("TRACK:");
  137. RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
  138. RTGUI_WIDGET_FOREGROUND(label) = blue;
  139. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  140. rtgui_container_add_child(container, RTGUI_WIDGET(label));
  141. RTGUI_WIDGET(edit)->user_data = (rt_uint32_t)label;
  142. rtgui_object_set_event_handler(RTGUI_OBJECT(edit), demo_edit_event_handler);
  143. /* 创建一个按钮, 读取EDIT的内存消耗 */
  144. demo_view_get_rect(container, &rect);
  145. rect.x1 += 150;
  146. rect.x2 = rect.x1 + 80;
  147. rect.y1 -= 42;
  148. rect.y2 = rect.y1 + 20;
  149. button = rtgui_button_create("Get Mem");
  150. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  151. rtgui_container_add_child(container, RTGUI_WIDGET(button));
  152. rtgui_button_set_onbutton(button, demo_edit_get_mem);
  153. RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
  154. return container;
  155. }
  156. #endif