ftk学习记(对话框篇)


【声明:版权全部,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】 


    前面谈到了输入法,首先看一看效果。




    上面有4个输入框,大家能够分别试试,看看效果怎样。

    今天,我们要说的是对话框。直接看demo代码好了,

#include "ftk.h"

static Ret button_quit_clicked(void* ctx, void* obj)
{
	if(ctx != NULL)
	{
		/*modal*/
		*(int*)ctx = ftk_widget_id(obj);
	}
	else
	{
		/*modal-less*/
		ftk_widget_unref(ftk_widget_toplevel(obj));
	}

	return RET_QUIT;
}

static Ret button_close_clicked(void* ctx, void* obj)
{
	ftk_widget_unref(ctx);

	return RET_OK;
}

static Ret button_dialog_clicked(void* ctx, void* obj)
{
	int id = 0;
	int width = 0;
	int height = 0;
	FtkWidget* label = NULL;
	FtkWidget* button = NULL;
	FtkWidget* dialog = NULL;
	FtkBitmap* icon = NULL;
	int modal = (int)ctx;
	
	ftk_logd("%s:%d begin\n", __func__, __LINE__);
	dialog = ftk_dialog_create(0, 40, 320, 240);
	
	icon = ftk_theme_load_image(ftk_default_theme(), "info-icon"FTK_STOCK_IMG_SUFFIX); 
	ftk_dialog_set_icon(dialog, icon);

	width = ftk_widget_width(dialog);
	height = ftk_widget_height(dialog);
	label = ftk_label_create(dialog, width/6, height/4, 5*width/6, 20);
	ftk_widget_set_text(label, "Are you sure to quit?");

	button = ftk_button_create(dialog, width/6, height/2, width/3, 50);
	ftk_widget_set_text(button, "yes");
	ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
	
	button = ftk_button_create(dialog, width/2, height/2, width/3, 50);
	ftk_widget_set_text(button, "no");
	ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
	ftk_window_set_focus(dialog, button);

	ftk_widget_set_text(dialog, modal ? "model dialog" : "normal dialog");
	ftk_widget_show_all(dialog, 1);

	if(modal)
	{
		assert(ftk_dialog_run(dialog) == id);
		ftk_widget_unref(dialog);
	}
	else
	{
		ftk_widget_show_all(dialog, 1);
	}
	ftk_logd("%s:%d end\n", __func__, __LINE__);

	return RET_OK;
}

int main(int argc, char* argv[])
{
	int width = 0;
	int height = 0;
	FtkWidget* win = NULL;
	FtkWidget* button = NULL;
	
	ftk_init(argc, argv);

	win = ftk_app_window_create();
	width = ftk_widget_width(win);
	height = ftk_widget_height(win);

	button = ftk_button_create(win, 0, height/6, width/3, 50);
	ftk_widget_set_text(button, "Normal");
	ftk_button_set_clicked_listener(button, button_dialog_clicked, NULL);

	button = ftk_button_create(win, 2*width/3, height/6, width/3, 50);
	ftk_widget_set_text(button, "Modal");
	ftk_button_set_clicked_listener(button, button_dialog_clicked, (void*)1);

	button = ftk_button_create(win, width/4, height/2, width/2, 60);
	ftk_widget_set_text(button, "quit");
	ftk_button_set_clicked_listener(button, button_close_clicked, win);

	ftk_widget_set_text(win, "demo dialog");
	ftk_widget_show_all(win, 1);
	ftk_widget_set_attr(win, FTK_ATTR_QUIT_WHEN_CLOSE);

	ftk_run();

	return 0;
}

    这段对话框的代码略微长一点,可是理解上没有难度。首先,main代码中创建了3个button。除了一个quit的button之外,其它两个button都是用来弹出对话框使用的,两个button的回调函数都是button_dialog_clicked。那么这当中又做了些什么呢?我们继续往下看,首先它会调用ftk_create_dialog创建一个对话框,然后用ftk_dialog_set_icon设置一下icon,接着依次创建了label和button,就是这样。怎样退出对话框呢?仅仅须要单击一下dialog中的button就可以,它会调用button_close_clicked函数,此时ftk_widget_unref会帮我们退出当前dialog,返回之前的windows,就是这么简单。


    效果图,下次见。




posted @ 2014-08-26 09:35  hrhguanli  阅读(274)  评论(0编辑  收藏  举报