toolbar.c 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como - e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

 #include "private.h"
 #include <pw3270/application.h>
 #include <pw3270/settings.h>

 #define GTK_TOOLBAR_DEFAULT_STYLE ((GtkToolbarStyle) -1)

 static gboolean popup_context_menu(GtkToolbar *toolbar, gint x, gint y, gint button_number);
 static void finalize(GObject *object);
 static void get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
 static void set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);

 const struct icon_size {
	const gchar			* label;
	GtkIconSize			  icon_size;
 } icon_sizes[] = {

	{
		.label = N_( "System default" ),
		.icon_size = GTK_ICON_SIZE_INVALID

	},

	{
		.label = N_( "Small" ),
		.icon_size = GTK_ICON_SIZE_SMALL_TOOLBAR
	},

	{
		.label = N_( "Large" ),
		.icon_size = GTK_ICON_SIZE_LARGE_TOOLBAR
	},
 };

 static const struct style {
	const gchar * label;
	GtkToolbarStyle style;
 } styles[] = {

	{
		.label = N_( "System default" ),
		.style = GTK_TOOLBAR_DEFAULT_STYLE
	},

	{
		.label = N_( "Icons only" ),
		.style = GTK_TOOLBAR_ICONS
	},

	{
		.label = N_( "Text only" ),
		.style = GTK_TOOLBAR_TEXT
	},

	{
		.label = N_( "Icons & text" ),
		.style = GTK_TOOLBAR_BOTH
	},
 };

 enum {
	PROP_NONE,
	PROP_ACTION_NAMES,
	PROP_ICON_SIZE,
	PROP_ICON_TYPE,
	PROP_STYLE
 };

 struct _pw3270ToolBar {
 	GtkToolbar parent;
	GtkToolbarStyle style;
	int icon_type;

 	/// @brief Popup Menu
 	struct {
		GtkWidget * menu;
		GtkWidget * styles[G_N_ELEMENTS(styles)];
		GtkWidget * icon_sizes[G_N_ELEMENTS(icon_sizes)];
 	} popup;

 };

 struct _pw3270ToolBarClass {

	GtkToolbarClass parent_class;


 };

 G_DEFINE_TYPE(pw3270ToolBar, pw3270ToolBar, GTK_TYPE_TOOLBAR);

 static void pw3270ToolBar_class_init(pw3270ToolBarClass *klass) {

	GObjectClass *object_class = G_OBJECT_CLASS(klass);
 	GtkToolbarClass * toolbar = GTK_TOOLBAR_CLASS(klass);

 	toolbar->popup_context_menu = popup_context_menu;

 	G_OBJECT_CLASS(klass)->finalize = finalize;

	object_class->set_property	= set_property;
	object_class->get_property	= get_property;

	g_object_class_install_property(
		object_class,
		PROP_ACTION_NAMES,
		g_param_spec_string (
			I_("action-names"),
			"Action Names",
			_("The name of the actions in the toolbar"),
			NULL,
			G_PARAM_READABLE|G_PARAM_WRITABLE)
	);

	g_object_class_install_property(
		object_class,
		PROP_ICON_SIZE,
		g_param_spec_int(
			I_("icon-size"),
			"icon size",
			_("The toolbar icon size"),
			INT_MIN,
			INT_MAX,
			0,
			G_PARAM_READABLE|G_PARAM_WRITABLE)
	);

	g_object_class_install_property(
		object_class,
		PROP_STYLE,
		g_param_spec_int(
			I_("style"),
			"style",
			_("The toolbar style"),
			INT_MIN,
			INT_MAX,
			0,
			G_PARAM_READABLE|G_PARAM_WRITABLE)
	);

	g_object_class_install_property(
		object_class,
		PROP_ICON_TYPE,
		g_param_spec_int(
			I_("icon-type"),
			I_("icon-type"),
			_("The toolbar icon type"),
			0,
			1,
			0,
			G_PARAM_READABLE|G_PARAM_WRITABLE)
	);

 }

  void get_property(GObject *object, guint prop_id, GValue *value, GParamSpec G_GNUC_UNUSED(*pspec)) {

	switch (prop_id) {
    case PROP_ACTION_NAMES:
    	g_value_take_string(value,pw3270_toolbar_get_actions(GTK_WIDGET(object)));
		break;

	case PROP_ICON_SIZE:
		g_value_set_int(value,pw3270_toolbar_get_icon_size(GTK_TOOLBAR(object)));
		break;

	case PROP_STYLE:
		g_value_set_int(value,pw3270_toolbar_get_style(GTK_TOOLBAR(object)));
		break;

	case PROP_ICON_TYPE:
		g_value_set_int(value,pw3270_toolbar_get_icon_type(GTK_TOOLBAR(object)));
		break;

	default:
		g_assert_not_reached ();
	}

 }

 void set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec G_GNUC_UNUSED(*pspec)) {

	switch (prop_id)
	{
    case PROP_ACTION_NAMES:
		pw3270_toolbar_set_actions(GTK_WIDGET(object), g_value_get_string(value));
		break;

	case PROP_ICON_SIZE:
		pw3270_toolbar_set_icon_size(GTK_TOOLBAR(object),(GtkIconSize) g_value_get_int(value));
		break;

	case PROP_STYLE:
		pw3270_toolbar_set_style(GTK_TOOLBAR(object),(GtkToolbarStyle) g_value_get_int(value));
		break;

	case PROP_ICON_TYPE:
		pw3270_toolbar_set_icon_type(GTK_TOOLBAR(object),(GtkToolbarStyle) g_value_get_int(value));
		break;

	default:
		g_assert_not_reached ();
	}

 }

 static void detacher(GtkWidget *attach_widget, GtkMenu G_GNUC_UNUSED(*menu)) {

 	pw3270ToolBar * toolbar = PW3270_TOOLBAR(attach_widget);
 	toolbar->popup.menu = NULL;

 }

 static void set_icon_size(GtkCheckMenuItem *menuitem, GtkWidget *toolbar) {

	if(gtk_check_menu_item_get_active(menuitem)) {
		const struct icon_size * size = g_object_get_data(G_OBJECT(menuitem),"icon_size");
		pw3270_toolbar_set_icon_size(GTK_TOOLBAR(toolbar), size->icon_size);
	}

 }

 static void set_style(GtkCheckMenuItem *menuitem, GtkWidget *toolbar) {

	if(gtk_check_menu_item_get_active(menuitem)) {
		struct style * style = g_object_get_data(G_OBJECT(menuitem),"toolbar_style");
		pw3270_toolbar_set_style(GTK_TOOLBAR(toolbar), style->style);
	}

 }

 static void open_properties(GtkMenuItem G_GNUC_UNUSED(*menuitem), GtkWidget *toolbar) {

	GtkWidget * window = gtk_widget_get_toplevel(toolbar);
	GtkWidget * dialog = pw3270_settings_dialog_new(NULL);

	gtk_container_add(GTK_CONTAINER(dialog),pw3270_toolbar_settings_new());

	gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
	gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
	gtk_window_set_attached_to(GTK_WINDOW(dialog), window);
	gtk_window_set_transient_for(GTK_WINDOW(dialog),GTK_WINDOW(window));

	gtk_widget_show_all(dialog);

 }

 static void pw3270ToolBar_init(pw3270ToolBar *widget) {

	widget->popup.menu = gtk_menu_new();

	// Size options
	{
		size_t ix;

		GtkWidget * item = gtk_menu_item_new_with_mnemonic( _("Icon _size") );
		gtk_menu_shell_append(GTK_MENU_SHELL(widget->popup.menu),item);

		GtkWidget * submenu = gtk_menu_new();
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);

		for(ix = 0; ix < G_N_ELEMENTS(icon_sizes); ix++) {

			widget->popup.icon_sizes[ix] = item = gtk_check_menu_item_new_with_mnemonic(gettext(icon_sizes[ix].label));
			gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item),TRUE);

			g_object_set_data(G_OBJECT(item),"icon_size", (gpointer) &icon_sizes[ix]);
			g_signal_connect(item, "toggled", G_CALLBACK(set_icon_size), widget);

			gtk_menu_shell_append(GTK_MENU_SHELL(submenu),item);

		}

	}

	// Style option
	{
		size_t ix;

		GtkWidget * item = gtk_menu_item_new_with_mnemonic( _("S_tyle") );
		gtk_menu_shell_append(GTK_MENU_SHELL(widget->popup.menu),item);

		GtkWidget * submenu = gtk_menu_new();
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);

		for(ix = 0; ix < G_N_ELEMENTS(styles); ix++) {

			widget->popup.styles[ix] = item = gtk_check_menu_item_new_with_mnemonic(gettext(styles[ix].label));
			gtk_check_menu_item_set_draw_as_radio(GTK_CHECK_MENU_ITEM(item),TRUE);

			g_object_set_data(G_OBJECT(item),"toolbar_style", (gpointer) &styles[ix]);
			g_signal_connect(item, "toggled", G_CALLBACK(set_style), widget);

			gtk_menu_shell_append(GTK_MENU_SHELL(submenu),item);

		}

	}

	// Toolbar properties.
	{
		GtkWidget * item = gtk_menu_item_new_with_mnemonic( _("_Properties") );
		gtk_menu_shell_append(GTK_MENU_SHELL(widget->popup.menu),item);
		g_signal_connect(item, "activate", G_CALLBACK(open_properties), widget);
	}

	// gtk_container_set_border_width(GTK_CONTAINER(widget->popup_menu),6);
	gtk_widget_show_all(widget->popup.menu);
	gtk_menu_attach_to_widget(GTK_MENU(widget->popup.menu),GTK_WIDGET(widget),detacher);

 }

 static void finalize(GObject *object) {

	G_OBJECT_CLASS(pw3270ToolBar_parent_class)->finalize(object);

 }

 GtkWidget * pw3270_toolbar_new(void) {
	return g_object_new(PW3270_TYPE_TOOLBAR, NULL);
 }

 gboolean popup_context_menu(GtkToolbar *widget, gint G_GNUC_UNUSED(x), gint G_GNUC_UNUSED(y), gint button_number) {

 	pw3270ToolBar * toolbar = PW3270_TOOLBAR(widget);

	debug("%s button_number=%d",__FUNCTION__,button_number);

	if(toolbar->popup.menu) {
#if GTK_CHECK_VERSION(3,22,0)
		gtk_menu_popup_at_pointer(GTK_MENU(toolbar->popup.menu),NULL);
#else
		gtk_menu_popup(GTK_MENU(toolbar->popup.menu), NULL, NULL, NULL, NULL, 0, 0);
#endif
	}

	return TRUE;

 }

 void pw3270_toolbar_set_style(GtkToolbar *toolbar, GtkToolbarStyle style) {

	debug("%s(%d)",__FUNCTION__,(int) style);

	PW3270_TOOLBAR(toolbar)->style = style;

	if(style == GTK_TOOLBAR_DEFAULT_STYLE)
		gtk_toolbar_unset_style(GTK_TOOLBAR(toolbar));
	else
		gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),style);

	// Update menu
	pw3270ToolBar * tb = PW3270_TOOLBAR(toolbar);
	if(tb && tb->popup.menu) {
		size_t ix;
		for(ix = 0; ix < G_N_ELEMENTS(styles); ix++) {

			gtk_check_menu_item_set_active(
				GTK_CHECK_MENU_ITEM(tb->popup.styles[ix]),
				styles[ix].style == style
			);
		}
	}

	// Store value
//	pw3270_settings_set_int("toolbar-style",(int) style);

	g_object_notify(G_OBJECT(toolbar), "style");

 }

 GtkToolbarStyle pw3270_toolbar_get_style(GtkToolbar *toolbar) {
	return PW3270_TOOLBAR(toolbar)->style;
 }

 gint pw3270_toolbar_get_icon_type(GtkToolbar *toolbar) {
 	return PW3270_TOOLBAR(toolbar)->icon_type;
 }

 void pw3270_toolbar_set_icon_type(GtkToolbar *toolbar, gint icon_type) {
 	PW3270_TOOLBAR(toolbar)->icon_type = icon_type;
	g_object_notify(G_OBJECT(toolbar), "icon-type");
 }

 void pw3270_toolbar_set_icon_size(GtkToolbar *toolbar, GtkIconSize icon_size) {

	debug("%s(%d)",__FUNCTION__,(int) icon_size);

	if(icon_size == GTK_ICON_SIZE_INVALID)
		gtk_toolbar_unset_icon_size(GTK_TOOLBAR(toolbar));
	else
		gtk_toolbar_set_icon_size(GTK_TOOLBAR(toolbar),icon_size);

	// Update menu
	pw3270ToolBar * tb = PW3270_TOOLBAR(toolbar);
	if(tb && tb->popup.menu) {
		size_t ix;
		for(ix = 0; ix < G_N_ELEMENTS(icon_sizes); ix++) {

			gtk_check_menu_item_set_active(
				GTK_CHECK_MENU_ITEM(tb->popup.icon_sizes[ix]),
				icon_sizes[ix].icon_size == icon_size
			);
		}
	}

	// Store value
	g_object_notify(G_OBJECT(toolbar), "icon-size");

 }

 GtkIconSize pw3270_toolbar_get_icon_size(GtkToolbar *toolbar) {

	GValue value = G_VALUE_INIT;
	g_value_init(&value, G_TYPE_BOOLEAN);
	g_object_get_property(G_OBJECT(toolbar),"icon-size-set",&value);

	gboolean is_set = g_value_get_boolean(&value);

	g_value_unset(&value);

	if(is_set)
		return gtk_toolbar_get_icon_size(GTK_TOOLBAR(toolbar));

	return GTK_ICON_SIZE_INVALID;
 }

 void pw3270_toolbar_set_actions(GtkWidget *toolbar, const gchar *action_names) {

 	gchar ** actions = g_strsplit(action_names,",",-1);
 	size_t ix;

 	gtk_container_remove_all(GTK_CONTAINER(toolbar));

	for(ix = 0; actions[ix]; ix++) {
		pw3270_toolbar_insert_action(toolbar,actions[ix],-1);
	}

 	g_strfreev(actions);

	g_object_notify(G_OBJECT(toolbar), "action-names");

 }

 gchar * pw3270_toolbar_get_actions(GtkWidget *toolbar) {

	GString * str = g_string_new("");

 	GList * children = gtk_container_get_children(GTK_CONTAINER(toolbar));
 	GList * item;

 	for(item = children;item;item = g_list_next(item)) {

		if(*str->str)
			g_string_append(str,",");

		if(GTK_IS_SEPARATOR_TOOL_ITEM(item->data)) {
			g_string_append(str,"separator");
		} else if(GTK_IS_TOOL_BUTTON(item->data)) {
			g_string_append(str,gtk_actionable_get_action_name(GTK_ACTIONABLE(item->data)));
		}

 	}

 	g_list_free(children);

	return g_string_free(str,FALSE);
 }

 GtkTreeModel * pw3270_toolbar_style_model_new() {

 	size_t ix;
	GtkTreeIter	  iter;
	GtkListStore * model = gtk_list_store_new(2, G_TYPE_STRING,G_TYPE_UINT);

	for(ix = 0; ix < G_N_ELEMENTS(icon_sizes); ix++) {
		gtk_list_store_append(model,&iter);
		gtk_list_store_set(	model,
							&iter,
							0, gettext(styles[ix].label),
							1, (guint) styles[ix].style,
							-1);

	}

	return GTK_TREE_MODEL(model);

 }

 GtkTreeModel * pw3270_toolbar_icon_size_model_new() {

	size_t ix;
	GtkTreeIter	  iter;
	GtkListStore * model = gtk_list_store_new(2, G_TYPE_STRING,G_TYPE_UINT);

	for(ix = 0; ix < G_N_ELEMENTS(icon_sizes); ix++) {
		gtk_list_store_append(model,&iter);
		gtk_list_store_set(	model,
							&iter,
							0, gettext(icon_sizes[ix].label),
							1, (guint) icon_sizes[ix].icon_size,
							-1);

	}

	return GTK_TREE_MODEL(model);
 }

 void pw3270_model_get_iter_from_value(GtkTreeModel * model, GtkTreeIter *iter, guint value) {

	if(gtk_tree_model_get_iter_first(model,iter)) {

		do {

			GValue gVal = { 0, };
			gtk_tree_model_get_value(model,iter,1,&gVal);
			guint iVal = g_value_get_uint(&gVal);
			g_value_unset(&gVal);

			if(iVal == value) {
				return;
			}

		} while(gtk_tree_model_iter_next(model,iter));

	}

 }

 guint pw3270_model_get_value_from_iter(GtkTreeModel * model, GtkTreeIter *iter) {
	GValue gVal = { 0, };
	gtk_tree_model_get_value(model,iter,1,&gVal);
	guint iVal = g_value_get_uint(&gVal);
	g_value_unset(&gVal);
	return iVal;
}