demo_view_edit.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * 程序清单:edit控件演示
  3. *
  4. * 这个例子会在conatiner上进行edit控件的演示
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/dc.h>
  8. #include <rtgui/rtgui_system.h>
  9. #include <rtgui/widgets/edit.h>
  10. #include <rtgui/widgets/button.h>
  11. void demo_edit_readin_file(struct rtgui_object *object, struct rtgui_event *event)
  12. {
  13. rtgui_button_t *button;
  14. struct rtgui_edit *edit;
  15. const char *filename = "/test_readin.txt";
  16. int fd;
  17. RT_ASSERT(object != RT_NULL);
  18. button = RTGUI_BUTTON(object);
  19. /* 取得edit指针 */
  20. edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
  21. /* 判断文件是否存在 */
  22. fd = open(filename, O_RDONLY, 0);
  23. if (fd < 0)
  24. {
  25. /* 不存在存在,则创建它 */
  26. rt_kprintf("file:\"%s\" does not exist!\n", filename);
  27. return;
  28. }
  29. close(fd);
  30. rt_kprintf("read-in file:\"%s\"\n", filename);
  31. rtgui_edit_readin_file(edit, filename);
  32. }
  33. void demo_edit_saveas_file(struct rtgui_object *object, struct rtgui_event *event)
  34. {
  35. rtgui_button_t *button;
  36. struct rtgui_edit *edit;
  37. const char* filename = "/test_saveas.txt";
  38. int fd;
  39. RT_ASSERT(object != RT_NULL);
  40. button = RTGUI_BUTTON(object);
  41. /* 取得edit指针 */
  42. edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
  43. /* 判断文件是否存在, 如果存在则删除之 */
  44. fd = open(filename, O_RDONLY, 0);
  45. if (fd > 0)
  46. {
  47. close(fd);
  48. /* 如果是在win32中调试, 请手工删除该文件吧, NT中文件是只读的,unlink删除不掉 */
  49. if (unlink(filename) == -1)
  50. rt_kprintf("Could not delete %s\n", filename);
  51. }
  52. rt_kprintf("save as file:\"%s\"\n", filename);
  53. rtgui_edit_saveas_file(edit, filename);
  54. }
  55. /* 创建用于演示edit控件的视图 */
  56. rtgui_container_t *demo_view_edit(void)
  57. {
  58. rtgui_rect_t rect;
  59. rtgui_container_t *container;
  60. struct rtgui_edit *edit;
  61. struct rtgui_button *button;
  62. /* 先创建一个演示用的视图 */
  63. container = demo_view("Edit View");
  64. edit = rtgui_edit_create(container, 10, 35, 220, 200);
  65. rtgui_edit_set_text(edit,
  66. "Edit beta v0.1\n"
  67. "Hello RT-Thread\n"
  68. "this's edit demo\n"
  69. "rtgui_edit_create\n"
  70. "rtgui_edit_append_line\n"
  71. "rtgui_edit_insert_line\n"
  72. "rtgui_edit_delete_line\n"
  73. "rtgui_edit_connect_line\n"
  74. "a\n"
  75. "b\n"
  76. "c\n"
  77. "d\n"
  78. "1234567890\n");
  79. /* 创建一个按钮, 读取某个文件 */
  80. demo_view_get_rect(container, &rect);
  81. rect.x1 += 10;
  82. rect.x2 = rect.x1 + 100;
  83. rect.y1 += 200;
  84. rect.y2 = rect.y1 + 22;
  85. button = rtgui_button_create("ReadIn File");
  86. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  87. rtgui_container_add_child(container, RTGUI_WIDGET(button));
  88. rtgui_button_set_onbutton(button, demo_edit_readin_file);
  89. /* 使用user_data传递edit指针 */
  90. RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
  91. /* 创建一个按钮, 保存为某个文件 */
  92. demo_view_get_rect(container, &rect);
  93. rect.x1 += 130;
  94. rect.x2 = rect.x1 + 100;
  95. rect.y1 += 200;
  96. rect.y2 = rect.y1 + 22;
  97. button = rtgui_button_create("SaveAs File");
  98. rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
  99. rtgui_container_add_child(container, RTGUI_WIDGET(button));
  100. rtgui_button_set_onbutton(button, demo_edit_saveas_file);
  101. /* 使用user_data传递edit指针 */
  102. RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
  103. return container;
  104. }