rtt-f030/examples/gui/demo_view_edit.c

183 lines
5.5 KiB
C
Raw Normal View History

/*
2013-02-01 10:27:10 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>edit<EFBFBD>ؼ<EFBFBD><EFBFBD><EFBFBD>ʾ
*
2013-02-01 10:27:10 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><EFBFBD><EFBFBD>conatiner<EFBFBD>Ͻ<EFBFBD><EFBFBD><EFBFBD>edit<EFBFBD>ؼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
*/
#include "demo_view.h"
#include <rtgui/dc.h>
#include <rtgui/filerw.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/edit.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/button.h>
#ifdef RTGUI_USING_DFS_FILERW
void demo_edit_readin_file(struct rtgui_object *object, struct rtgui_event *event)
{
rtgui_button_t *button;
struct rtgui_edit *edit;
const char *filename = "/test_readin.txt";
struct rtgui_filerw *file;
RT_ASSERT(object != RT_NULL);
button = RTGUI_BUTTON(object);
2013-02-01 10:27:10 +08:00
/* ȡ<><C8A1>editָ<74><D6B8> */
edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
2013-02-01 10:27:10 +08:00
/* <20>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD> */
file = rtgui_filerw_create_file(filename, "rb");
if (file == RT_NULL)
{
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>,<2C>򴴽<EFBFBD><F2B4B4BD><EFBFBD> */
rt_kprintf("file:\"%s\" does not exist!\n", filename);
return;
}
rtgui_filerw_close(file);
rt_kprintf("read-in file:\"%s\"\n", filename);
rtgui_edit_readin_file(edit, filename);
}
void demo_edit_saveas_file(struct rtgui_object *object, struct rtgui_event *event)
{
rtgui_button_t *button;
struct rtgui_edit *edit;
const char* filename = "/test_saveas.txt";
struct rtgui_filerw *file;
RT_ASSERT(object != RT_NULL);
button = RTGUI_BUTTON(object);
2013-02-01 10:27:10 +08:00
/* ȡ<><C8A1>editָ<74><D6B8> */
edit = RTGUI_EDIT(RTGUI_WIDGET(button)->user_data);
2013-02-01 10:27:10 +08:00
/* <20>ж<EFBFBD><D0B6>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>֮ */
file = rtgui_filerw_create_file(filename, "rb");
if (file != RT_NULL)
{
rtgui_filerw_close(file);
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>win32<33>е<EFBFBD><D0B5><EFBFBD>, <20><><EFBFBD>ֹ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>, NT<4E><54><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>,unlinkɾ<6B><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (rtgui_filerw_unlink(filename) == -1)
rt_kprintf("Could not delete %s\n", filename);
}
rt_kprintf("save as file:\"%s\"\n", filename);
rtgui_edit_saveas_file(edit, filename);
}
void demo_edit_get_mem(struct rtgui_object* object, struct rtgui_event* event)
{
rtgui_button_t *button;
struct rtgui_edit *edit;
RT_ASSERT(object != RT_NULL);
button = RTGUI_BUTTON(object);
edit = RTGUI_EDIT( RTGUI_WIDGET(button)->user_data );
rt_kprintf("edit mem consume: %d\n", rtgui_edit_get_mem_consume(edit));
}
rt_bool_t demo_edit_event_handler(struct rtgui_object* object, struct rtgui_event *event)
{
rt_bool_t result;
char buf[32];
rtgui_point_t p;
struct rtgui_edit *edit = RTGUI_EDIT(object);
struct rtgui_label *label = (struct rtgui_label*)RTGUI_WIDGET(edit)->user_data;
result = rtgui_edit_event_handler(object, event);
p = rtgui_edit_get_current_point(edit);
rt_sprintf(buf, "TRACK: line:%d, col:%d", p.y, p.x);
rtgui_label_set_text(label, buf);
return result;
}
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾedit<69>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD><EFBFBD>ͼ */
rtgui_container_t *demo_view_edit(void)
{
rtgui_rect_t rect;
rtgui_container_t *container;
struct rtgui_edit *edit;
struct rtgui_label *label;
struct rtgui_button *button;
2013-02-01 10:27:10 +08:00
/* <20>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ʾ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>ͼ */
container = demo_view("Edit View");
edit = rtgui_edit_create(container, 10, 35, 220, 200);
rtgui_edit_set_text(edit,
"Edit beta v0.1\n"
"Hello RT-Thread\n"
"this's edit demo\n"
"rtgui_edit_create\n"
"rtgui_edit_append_line\n"
"rtgui_edit_insert_line\n"
"rtgui_edit_delete_line\n"
"rtgui_edit_connect_line\n"
2013-02-01 10:27:10 +08:00
"˫<EFBFBD>ֽڲ<EFBFBD><EFBFBD><EFBFBD>\n"
"a\n"
"b\n"
"c\n"
"d\n"
"1234567890\n");
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ť, <20><>ȡij<C8A1><C4B3><EFBFBD>ļ<EFBFBD> */
demo_view_get_rect(container, &rect);
rect.x1 += 10;
rect.x2 = rect.x1 + 100;
rect.y1 += 200;
rect.y2 = rect.y1 + 22;
button = rtgui_button_create("ReadIn File");
rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
rtgui_container_add_child(container, RTGUI_WIDGET(button));
rtgui_button_set_onbutton(button, demo_edit_readin_file);
2013-02-01 10:27:10 +08:00
/* ʹ<><CAB9>user_data<74><61><EFBFBD><EFBFBD>editָ<74><D6B8> */
RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ť, <20><><EFBFBD><EFBFBD>Ϊij<CEAA><C4B3><EFBFBD>ļ<EFBFBD> */
demo_view_get_rect(container, &rect);
rect.x1 += 130;
rect.x2 = rect.x1 + 100;
rect.y1 += 200;
rect.y2 = rect.y1 + 22;
button = rtgui_button_create("SaveAs File");
rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
rtgui_container_add_child(container, RTGUI_WIDGET(button));
rtgui_button_set_onbutton(button, demo_edit_saveas_file);
2013-02-01 10:27:10 +08:00
/* ʹ<><CAB9>user_data<74><61><EFBFBD><EFBFBD>editָ<74><D6B8> */
RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ǩ, <20><>ʾEDIT<49><54><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD> */
demo_view_get_rect(container, &rect);
rect.x1 += 10;
rect.x2 = rect.x1 + 220;
rect.y1 += 225;
rect.y2 = rect.y1 + 18;
label = rtgui_label_create("TRACK:");
RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
RTGUI_WIDGET_FOREGROUND(label) = blue;
rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
rtgui_container_add_child(container, RTGUI_WIDGET(label));
RTGUI_WIDGET(edit)->user_data = (rt_uint32_t)label;
rtgui_object_set_event_handler(RTGUI_OBJECT(edit), demo_edit_event_handler);
2013-02-01 10:27:10 +08:00
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ť, <20><>ȡEDIT<49><54><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD> */
demo_view_get_rect(container, &rect);
rect.x1 += 150;
rect.x2 = rect.x1 + 80;
rect.y1 -= 42;
rect.y2 = rect.y1 + 20;
button = rtgui_button_create("Get Mem");
rtgui_widget_set_rect(RTGUI_WIDGET(button), &rect);
rtgui_container_add_child(container, RTGUI_WIDGET(button));
rtgui_button_set_onbutton(button, demo_edit_get_mem);
RTGUI_WIDGET(button)->user_data = (rt_uint32_t)edit;
return container;
}
#endif