PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 15ms app.codeStats 1ms

/src/ftk_message_box.c

http://ftk.googlecode.com/
C | 216 lines | 145 code | 39 blank | 32 comment | 15 complexity | cc1502ca84a0d52bd3d4e4b09424c05d MD5 | raw file
  1/*
  2 * File: ftk_messagebox.c
  3 * Author: Li XianJing <xianjimli@hotmail.com>
  4 * Brief:  messagebox
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2010-01-23 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_label.h"
 34#include "ftk_button.h"
 35#include "ftk_globals.h"
 36#include "ftk_message_box.h"
 37
 38static Ret message_box_on_button_clicked(void* ctx, void* obj)
 39{
 40	*(int*)ctx = ftk_widget_id((FtkWidget*)obj);
 41
 42	ftk_logd("%s: the %dth button is clicked.\n", __func__, ftk_widget_id((FtkWidget*)obj));
 43
 44	return RET_QUIT;
 45}
 46
 47#define FTK_BUTTON_DEFAULT_WIDTH   80
 48#define FTK_BUTTON_DEFAULT_HEIGHT  48
 49#define FTK_MESSAGE_BOX_MIN_HEIGHT 30
 50
 51static Ret ftk_message_box_size(int has_title, int has_button, const char* text, int* w, int* h)
 52{
 53	int start  = 0;
 54	int width  = 0;
 55	int height = 0;
 56	int font_h = 0;
 57	FtkRect rect = {0};
 58	const char* end = text;
 59	FtkCanvas* canvas = ftk_shared_canvas();
 60	
 61	ftk_wnd_manager_get_work_area(ftk_default_wnd_manager(), &rect);
 62
 63	width  = rect.width - 2 * (FTK_DIALOG_MARGIN + FTK_LABEL_LEFT_MARGIN + FTK_DIALOG_BORDER);
 64
 65	height = 4 * FTK_V_MARGIN + FTK_DIALOG_BORDER;
 66	height += has_title ? ftk_dialog_get_title_height() : 0;
 67	height += has_button ? FTK_BUTTON_DEFAULT_HEIGHT : 0;
 68
 69	font_h = ftk_font_desc_get_size(ftk_default_font());
 70	while(*end != '\0')
 71	{
 72		height += font_h + FTK_LABEL_TOP_MARGIN;
 73		end = ftk_canvas_calc_str_visible_range(canvas, text, start, -1, width, NULL);
 74		start = end - text;
 75	}
 76
 77	height = height < FTK_MESSAGE_BOX_MIN_HEIGHT ? FTK_MESSAGE_BOX_MIN_HEIGHT : height;
 78	height = height < rect.height ? height : rect.height;
 79
 80	*h = (height + 1) & 0xfffffffe;
 81	*w = (rect.width + 1)  & 0xfffffffe;
 82
 83	return RET_OK;
 84}
 85
 86static int ftk_count_strings(const char* buttons[FTK_MSGBOX_MAX_BUTTONS])
 87{
 88	int i = 0;
 89	int buttons_nr = 0;
 90
 91	if(buttons != NULL)
 92	{
 93		for(i = 0; buttons[i] != NULL && i < FTK_MSGBOX_MAX_BUTTONS; i++)
 94		{
 95			buttons_nr++;
 96		}
 97	}
 98
 99	return buttons_nr;
100}
101
102int ftk_message_box(FtkBitmap* icon, const char* title, const char* text, const char* buttons[FTK_MSGBOX_MAX_BUTTONS])
103{
104	int i = 0;
105	int w = 0;
106	int h = 0;
107	int id = 0;
108	int ret = 0;
109	int width = 0;
110	int height = 0;
111	int xoffset = 0;
112	int yoffset = 0;
113	int h_margin = 0;
114	int has_title = icon != NULL || title != NULL;
115	int buttons_nr = ftk_count_strings(buttons);
116
117	FtkWidget* label = NULL;
118	FtkWidget* button = NULL;
119	FtkWidget* dialog = NULL;
120	
121	return_val_if_fail(text != NULL, -1);
122
123	ftk_message_box_size(has_title, buttons_nr, text, &width, &height);
124	dialog = ftk_dialog_create(0, 0, width, height);
125	ftk_window_set_animation_hint(dialog, "msgbox");
126	if(has_title) 
127	{
128		ftk_dialog_set_icon(dialog, icon);
129		ftk_widget_set_text(dialog, title);
130	}
131	else
132	{
133		ftk_dialog_hide_title(dialog);
134		ftk_widget_set_attr(dialog, FTK_ATTR_POPUP);
135	}
136
137	width  = ftk_widget_width(dialog);
138	height = ftk_widget_height(dialog);
139
140	/*create label.*/
141	xoffset = FTK_H_MARGIN;
142	yoffset = FTK_V_MARGIN;
143	w = width - 2 * (FTK_DIALOG_BORDER + FTK_H_MARGIN);
144	h = height - FTK_DIALOG_BORDER - 4 * FTK_V_MARGIN;
145	h -= has_title ? ftk_dialog_get_title_height() : 0;
146	h -= buttons_nr > 0 ? FTK_BUTTON_DEFAULT_HEIGHT : 0;
147
148	label = ftk_label_create(dialog, xoffset, yoffset, w, h);
149	ftk_widget_set_text(label, text);
150
151	/*create buttons*/
152
153	if(buttons_nr > 0)
154	{
155		xoffset = 0;
156		w = FTK_BUTTON_DEFAULT_WIDTH;
157		h = FTK_BUTTON_DEFAULT_HEIGHT;
158		w = ((buttons_nr + 1) * w) < width ? w : (width / (buttons_nr + 1));
159		yoffset = height - h - FTK_V_MARGIN - (has_title ? ftk_dialog_get_title_height() : 0);
160		h_margin = (width - buttons_nr * w) / (buttons_nr + 1);
161
162		for(i = 0; i < buttons_nr; i++)
163		{
164			xoffset += h_margin;
165			button = ftk_button_create(dialog, xoffset, yoffset, w, h);
166			ftk_widget_set_id(button, i + 1);
167			ftk_widget_set_text(button, buttons[i]);
168			ftk_button_set_clicked_listener(button, message_box_on_button_clicked, &id);
169			xoffset += w;
170		}
171	}
172	else
173	{
174		/*if no buttons, quit when timeout.*/
175		ftk_dialog_quit_after(dialog, 3000);
176	}
177
178	ftk_widget_show_all(dialog, 1);
179	ret = ftk_dialog_run(dialog);
180	ftk_widget_unref(dialog);
181	
182	if(icon != NULL)
183	{
184		ftk_bitmap_unref(icon);
185	}
186
187	return ret;
188}
189
190int ftk_tips(const char* text)
191{
192	return ftk_message_box(NULL, NULL, text, NULL);
193}
194
195int ftk_warning(const char* title, const char* text, const char* buttons[FTK_MSGBOX_MAX_BUTTONS + 1])
196{
197	FtkBitmap* icon = ftk_theme_load_image(ftk_default_theme(), "warning"FTK_STOCK_IMG_SUFFIX); 
198
199	return ftk_message_box(icon, title, text, buttons);
200}
201
202int ftk_question(const char* title, const char* text, const char* buttons[FTK_MSGBOX_MAX_BUTTONS + 1])
203{
204	FtkBitmap* icon = ftk_theme_load_image(ftk_default_theme(), "question"FTK_STOCK_IMG_SUFFIX); 
205
206	return ftk_message_box(icon, title, text, buttons);
207}
208
209int ftk_infomation(const char* title, const char* text, const char* buttons[FTK_MSGBOX_MAX_BUTTONS + 1])
210{
211	FtkBitmap* icon = ftk_theme_load_image(ftk_default_theme(), "info"FTK_STOCK_IMG_SUFFIX); 
212
213	return ftk_message_box(icon, title, text, buttons);
214}
215
216