PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/src/demos/demo_progress_bar.c

http://ftk.googlecode.com/
C | 89 lines | 71 code | 18 blank | 0 comment | 2 complexity | 2d39ad6579669c6b39ffe92e2644f511 MD5 | raw file
 1#include "ftk.h"
 2#include "ftk_util.h"
 3
 4static void on_window_destroy(void* user_data)
 5{
 6	FtkSource* timer = user_data;
 7
 8	ftk_source_disable(timer);
 9	ftk_main_loop_remove_source(ftk_default_main_loop(), timer);
10
11	return;
12}
13
14static Ret update_progress(void* ctx)
15{
16	int percent = 0;
17	char text[32] = {0};
18	FtkWidget* progress_bar = ctx;
19
20	percent = ftk_progress_bar_get_percent(progress_bar);
21	if(percent == 100)
22	{
23		return RET_REMOVE;
24	}
25	ftk_progress_bar_set_percent(progress_bar, percent + 10);
26
27	ftk_itoa(text, sizeof(text), ftk_progress_bar_get_percent(progress_bar));
28	strcat(text, "%");
29	ftk_widget_set_text(progress_bar, text);
30
31	return RET_OK;
32}
33
34#ifdef FTK_AS_PLUGIN
35#include "ftk_app_demo.h"
36FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
37FtkApp* ftk_app_demo_progress_bar_create()
38{
39	return ftk_app_demo_create(_("progress_bar"), ftk_main);
40}
41#else
42#define FTK_HIDE extern
43#endif /*FTK_AS_PLUGIN*/
44
45FTK_HIDE int FTK_MAIN(int argc, char* argv[])
46{
47	int width = 0;
48	int height = 0;
49	FtkWidget* win = NULL;
50	FtkWidget* progress_bar = NULL;
51	FtkSource* timer = NULL;
52
53	FTK_INIT(argc, argv);
54	
55	win = ftk_app_window_create();
56	width = ftk_widget_width(win);
57	height = ftk_widget_height(win);
58	
59	width = width - 20;
60	progress_bar = ftk_progress_bar_create(win, 10, height/6, width, 32);
61	ftk_progress_bar_set_percent(progress_bar, 20);
62	ftk_widget_set_text(progress_bar, "20%");
63	timer = ftk_source_timer_create(1000, update_progress, progress_bar);
64	ftk_main_loop_add_source(ftk_default_main_loop(), timer);
65	ftk_widget_set_user_data(progress_bar, on_window_destroy, timer);
66
67	progress_bar = ftk_progress_bar_create(win, 10, height/3, width, 20);
68	ftk_progress_bar_set_percent(progress_bar, 20);
69	timer = ftk_source_timer_create(1000, update_progress, progress_bar);
70	ftk_main_loop_add_source(ftk_default_main_loop(), timer);
71	ftk_widget_set_user_data(progress_bar, on_window_destroy, timer);
72	ftk_progress_bar_set_interactive(progress_bar, 1);
73
74	progress_bar = ftk_progress_bar_create(win, 10, height/2, width, 32);
75	ftk_progress_bar_set_percent(progress_bar, 20);
76	ftk_widget_set_text(progress_bar, "20%");
77	timer = ftk_source_timer_create(1000, update_progress, progress_bar);
78	ftk_main_loop_add_source(ftk_default_main_loop(), timer);
79	ftk_widget_set_user_data(progress_bar, on_window_destroy, timer);
80
81	ftk_widget_set_text(win, "progress_bar demo");
82	ftk_widget_show_all(win, 1);
83	FTK_QUIT_WHEN_WIDGET_CLOSE(win);
84
85	FTK_RUN();
86
87	return 0;
88}
89