PageRenderTime 364ms CodeModel.GetById 242ms app.highlight 8ms RepoModel.GetById 112ms app.codeStats 0ms

/src/demos/demo_entry.c

http://ftk.googlecode.com/
C | 95 lines | 79 code | 16 blank | 0 comment | 27 complexity | 6b5e251941c7baeb3526af81d0efa3b0 MD5 | raw file
 1#include "ftk.h"
 2
 3static Ret button_quit_clicked(void* ctx, void* obj)
 4{
 5	ftk_logd("%s: %s\n", __func__, ftk_widget_get_text(ftk_widget_lookup(ctx, 100)));
 6	ftk_widget_unref(ctx);
 7
 8	return RET_OK;
 9}
10
11static Ret ftk_digit_only_filter(void* ctx, void* data)
12{
13	FtkEvent* event = data;
14	if(event->type == FTK_EVT_KEY_UP || event->type == FTK_EVT_KEY_DOWN)
15	{
16		int code = event->u.key.code;
17		if(code >= FTK_KEY_0 && code <= FTK_KEY_9)
18		{
19			return RET_OK;
20		}
21		else if(code == FTK_KEY_UP 
22			|| code == FTK_KEY_DOWN
23			|| code == FTK_KEY_LEFT
24			|| code == FTK_KEY_RIGHT
25			|| code == FTK_KEY_BACKSPACE
26			|| code == FTK_KEY_DELETE
27			|| code == FTK_KEY_HOME
28			|| code == FTK_KEY_END
29			|| code == FTK_KEY_TAB)
30		{
31			return RET_OK;
32		}
33
34		return RET_REMOVE;
35	}
36
37	return RET_OK;
38}
39
40#ifdef FTK_AS_PLUGIN
41#include "ftk_app_demo.h"
42FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
43FtkApp* ftk_app_demo_entry_create()
44{
45	return ftk_app_demo_create(_("entry"), ftk_main);
46}
47#else
48#define FTK_HIDE extern
49#endif /*FTK_AS_PLUGIN*/
50
51#define STR_TEXT1 "Read only editor"
52#define STR_TEXT2 "Single line editor, ?????????????."
53
54FTK_HIDE int FTK_MAIN(int argc, char* argv[])
55{
56	int width = 0;
57	int height = 0;
58	FtkWidget* win = NULL;
59	FtkWidget* button = NULL;
60	FtkWidget* entry  = NULL;
61
62	FTK_INIT(argc, argv);
63
64	win = ftk_app_window_create();
65	ftk_window_set_animation_hint(win, "app_main_window");
66	width = ftk_widget_width(win);
67	height = ftk_widget_height(win);
68	entry = ftk_entry_create(win, 10, 30, ftk_widget_width(win) - 20, 30);
69	ftk_widget_set_id(entry, 100);
70	ftk_widget_set_text(entry, "1234");
71	ftk_widget_set_event_listener(entry, ftk_digit_only_filter, NULL);
72	ftk_entry_set_tips(entry, "Please input some digits.");
73	entry = ftk_entry_create(win, 10, 80, ftk_widget_width(win) - 20, 30);
74	ftk_widget_set_text(entry, STR_TEXT1);
75	ftk_entry_set_readonly(entry, 1);
76	assert(strcmp(STR_TEXT1, ftk_widget_get_text(entry)) == 0);
77
78	entry = ftk_entry_create(win, 10, 130, ftk_widget_width(win) - 20, 30);
79	ftk_widget_set_text(entry, STR_TEXT2);
80	assert(strcmp(STR_TEXT2, ftk_widget_get_text(entry)) == 0);
81	
82	button = ftk_button_create(win, width/4, 3*height/4, width/2, 60);
83	ftk_widget_set_text(button, "quit");
84	ftk_button_set_clicked_listener(button, button_quit_clicked, win);
85	ftk_window_set_focus(win, button);
86
87	ftk_widget_set_text(win, "entry demo");
88	ftk_widget_show_all(win, 1);
89	FTK_QUIT_WHEN_WIDGET_CLOSE(win);
90
91	FTK_RUN();
92
93	return 0;
94}
95