PageRenderTime 82ms CodeModel.GetById 30ms app.highlight 14ms RepoModel.GetById 36ms app.codeStats 1ms

/src/demos/demo_draw.c

http://ftk.googlecode.com/
C | 119 lines | 95 code | 24 blank | 0 comment | 3 complexity | 82850eaa73c6189e19d2e558c9ae8927 MD5 | raw file
  1#include <math.h>
  2#include "ftk.h"
  3
  4#define IDC_PAINT 100
  5static int g_width = 10;
  6
  7static Ret on_paint(void* ctx, void* obj)
  8{
  9	FtkGc gc;
 10	int i = 0;
 11	int w = 0;
 12	int h = 0;
 13	FtkPoint p = {0};
 14	FtkWidget* thiz = obj;
 15	FTK_BEGIN_PAINT(x, y, width, height, canvas);
 16
 17	ftk_logd("%s:%d\n", __func__, __LINE__);
 18	gc.fg.a = 0xff;
 19	gc.fg.r = 0xff;
 20	gc.fg.g = 0;
 21	gc.fg.b = 0;
 22	gc.mask = FTK_GC_FG;
 23	
 24	w = g_width % width;
 25	ftk_canvas_set_gc(canvas, &gc);
 26	ftk_canvas_draw_line(canvas, x, y + height/2, x + w, y + height/2);
 27	for(i = 0; i < height; i+=10)
 28	{
 29		ftk_canvas_draw_line(canvas, x, y, x + width, y + i);
 30	}
 31
 32	for(i = width; i >= 0; i-=10)
 33	{
 34		ftk_canvas_draw_line(canvas, x, y, x + i, y + height);
 35	}
 36
 37	gc.fg.r = 0;
 38	gc.fg.g = 0xff;
 39	ftk_canvas_set_gc(canvas, &gc);
 40
 41	for(i = 0; i < w; i++)
 42	{
 43		double r =  2 * 3.14 * i / width;
 44		h = height/3 * sin(r);
 45		p.x = x + i;
 46		p.y = y + height/2 + h;
 47		ftk_canvas_draw_pixels(canvas, &p, 1);
 48	}
 49	
 50	ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz)); 
 51	ftk_canvas_draw_string(canvas, x + 100, y + height/2, "Hello World!", strlen("Hello World!"), 1);
 52
 53	ftk_logd("%s:%d\n", __func__, __LINE__);
 54	FTK_END_PAINT();
 55
 56	return RET_OK;
 57}
 58
 59static Ret button_step_clicked(void* ctx, void* obj)
 60{
 61
 62	g_width += 30;
 63	ftk_widget_invalidate(ftk_widget_lookup(ctx, IDC_PAINT));
 64
 65	return RET_OK;
 66}
 67
 68static Ret button_quit_clicked(void* ctx, void* obj)
 69{
 70	ftk_widget_unref(ctx);
 71
 72	return RET_OK;
 73}
 74
 75#ifdef FTK_AS_PLUGIN
 76#include "ftk_app_demo.h"
 77FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
 78FtkApp* ftk_app_demo_draw_create()
 79{
 80	return ftk_app_demo_create(_("draw"), ftk_main);
 81}
 82#else
 83#define FTK_HIDE extern
 84#endif /*FTK_AS_PLUGIN*/
 85
 86FTK_HIDE int FTK_MAIN(int argc, char* argv[])
 87{
 88	int width = 0;
 89	int height = 0;
 90	FtkWidget* win = NULL;
 91	FtkWidget* button = NULL;
 92	FtkWidget* painter = NULL;
 93	FTK_INIT(argc, argv);
 94
 95	win = ftk_app_window_create();
 96	ftk_window_set_animation_hint(win, "app_main_window");
 97	width = ftk_widget_width(win);
 98	height = ftk_widget_height(win);
 99
100	button = ftk_button_create(win, 0, 10, width/3, 60);
101	ftk_widget_set_text(button, "step");
102	ftk_button_set_clicked_listener(button, button_step_clicked, win);
103
104	button = ftk_button_create(win, width*2/3, 10, width/3, 60);
105	ftk_widget_set_text(button, "quit");
106	ftk_button_set_clicked_listener(button, button_quit_clicked, win);
107
108	painter = ftk_painter_create(win, 0, 70, width, height-70);
109	ftk_widget_set_id(painter, IDC_PAINT);
110	ftk_painter_set_paint_listener(painter, on_paint, NULL);
111
112	ftk_widget_set_text(win, "drawking kit demo");
113	ftk_widget_show_all(win, 1);
114	FTK_QUIT_WHEN_WIDGET_CLOSE(win);
115
116	FTK_RUN();
117
118	return 0;
119}