view.c 11.5 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
/*
 * "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)
 *
 */

 /**
  * @brief Implement PW3270 Action view widget and related tools.
  *
  */

 #include <config.h>
 #include <pw3270.h>
 #include <pw3270/actions.h>
 #include <lib3270/log.h>

 enum {
	COLUMN_PIXBUF,
	COLUMN_LABEL,
	COLUMN_ACTION_NAME,
	COLUMN_FLAGS
 };

 struct ListElement {
 	GAction		* action;
 	GdkPixbuf	* pixbuf;
 	gchar		  name[1];
 };

 static void list_element_free(struct ListElement *element);

 static gint view_sort(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer G_GNUC_UNUSED(user_data)) {

	gint rc = 0;
	GValue value[] = { G_VALUE_INIT, G_VALUE_INIT };

	gtk_tree_model_get_value(model, a, COLUMN_LABEL, &value[0]);
	gtk_tree_model_get_value(model, b, COLUMN_LABEL, &value[1]);

	rc = g_ascii_strcasecmp(g_value_get_string(&value[0]),g_value_get_string(&value[1]));

	g_value_unset(&value[0]);
	g_value_unset(&value[1]);

	return rc;

 }

 GtkWidget * pw3270_action_view_new() {

	GtkTreeModel * model = GTK_TREE_MODEL(gtk_list_store_new(4,G_TYPE_OBJECT,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_INT));
	GtkWidget * view = GTK_WIDGET(gtk_tree_view_new_with_model(model));

	gtk_widget_set_hexpand(view,TRUE);
	gtk_widget_set_vexpand(view,TRUE);
	gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW(view),FALSE);
	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view),FALSE);

	// Create Renderers
	GtkCellRenderer * text_renderer = gtk_cell_renderer_text_new();
	GtkCellRenderer * pixbuf_renderer = gtk_cell_renderer_pixbuf_new();

	gtk_tree_view_insert_column_with_attributes(
		GTK_TREE_VIEW(view),
		-1,
		_("Icon"),
		pixbuf_renderer,
			"pixbuf",COLUMN_PIXBUF,
			NULL
	);

	gtk_tree_view_insert_column_with_attributes(
		GTK_TREE_VIEW(view),
		-1,
		_("Label"),
		text_renderer,
			"text",COLUMN_LABEL,
			NULL
	);

	gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(model),view_sort,NULL,NULL);

	return view;
 }

 void pw3270_action_view_append(GtkWidget *widget, const gchar *label, GdkPixbuf *pixbuf, const gchar *action_name, PW3270ActionViewFlag flags) {

	GtkListStore * store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));

	GtkTreeIter iter;
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(
		store,
		&iter,
		COLUMN_PIXBUF,			pixbuf,
		COLUMN_LABEL, 			label,
		COLUMN_ACTION_NAME,		action_name,
		COLUMN_FLAGS,			(gint) flags,
		-1
	);


 }

 static void pw3270_action_view_append_element(GtkListStore * store, struct ListElement * element) {

	size_t ix;

	struct Properties {
		const gchar * name;
		GType g_type;
		GValue value;
	} properties[] = {
		{
			.name = "label",
			.g_type = G_TYPE_STRING,
			.value = G_VALUE_INIT
		}
	};

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

		g_value_init(&properties[ix].value, properties[ix].g_type);
		g_object_get_property(G_OBJECT(element->action), properties[ix].name, &properties[ix].value);

	}

	// Remove "_"
	g_autofree gchar * label = g_strdup(g_value_get_string(&properties[0].value));

	if(label) {

		gchar *from, *to;

		for(from=to=label;*from;from++) {
			if(*from != '_') {
				*(to++) = *from;
			}
		}
		*to = 0;

	}

	GtkTreeIter iter;
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(
		store,
		&iter,
		COLUMN_PIXBUF,		element->pixbuf,
		COLUMN_LABEL, 		(label ? label : g_action_get_name(element->action)),
		COLUMN_ACTION_NAME,	element->name,
		COLUMN_FLAGS,		(gint) PW3270_ACTION_VIEW_ALLOW_MOVE,
		-1
	);

	for(ix = 0; ix < G_N_ELEMENTS(properties); ix++) {
		g_value_unset(&properties[ix].value);
	}

 }

 Pw3270ActionList * pw3270_action_list_move_action(Pw3270ActionList *action_list, const gchar *action_name, GtkWidget *view) {

	GSList * item = (GSList *) action_list;
	GtkListStore * store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(view)));

	while(item) {

		struct ListElement * element = (struct ListElement *) item->data;

		if(!g_ascii_strcasecmp(action_name,element->name)) {

			pw3270_action_view_append_element(store, element);
			list_element_free(element);
			return (Pw3270ActionList *) g_slist_remove_link(action_list,item);
		}

		item = g_slist_next(item);

	}

	return action_list;

 }

 void pw3270_action_view_set_actions(GtkWidget *view, Pw3270ActionList *list) {

	GSList *item;
	GtkListStore * store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(view)));

	for(item = (GSList *) list; item; item = g_slist_next(item)) {

		pw3270_action_view_append_element(store, (struct ListElement *) item->data);

	}

 }

 static GSList * append_action(GSList * list, const gchar *type, GAction *action) {

	if(!action)
		return list;

	if(!g_object_class_find_property(G_OBJECT_GET_CLASS(action),"label")) {
		debug("Action \"%s\": Doesn't have a label",g_action_get_name(action));
		return list;
	}

	GdkPixbuf * pixbuf = g_action_get_pixbuf(action, GTK_ICON_SIZE_MENU, GTK_ICON_LOOKUP_GENERIC_FALLBACK);
	if(!pixbuf) {
		debug("Action \"%s\": Doesn't have a pixbuf",g_action_get_name(action));
		return list;
	}

	const gchar *name = g_action_get_name(action);

	struct ListElement * element = (struct ListElement *) g_malloc0(sizeof(struct ListElement) + strlen(type) + strlen(name));

	strcpy(element->name,type);
	strcat(element->name,name);

	element->action = action;

	element->pixbuf = pixbuf;
	g_object_ref_sink(G_OBJECT(element->pixbuf));

	return g_slist_prepend(list,element);

 }

 Pw3270ActionList * pw3270_action_list_new(GtkApplication *application) {

 	GSList * list = NULL;

 	gchar ** action_names;
 	size_t ix;

 	// Get application actions.

	action_names = g_action_group_list_actions(G_ACTION_GROUP(application));
	for(ix = 0; action_names[ix];ix++) {
		list = append_action(list,"app.",g_action_map_lookup_action(G_ACTION_MAP(application),action_names[ix]));
	}
	g_strfreev(action_names);

	// Get Windows actions.
	GtkWindow * window = gtk_application_get_active_window(application);

	if(window && G_IS_ACTION_GROUP(window)) {

		// Get window actions.
		action_names = g_action_group_list_actions(G_ACTION_GROUP(window));
		for(ix = 0; action_names[ix];ix++) {
			list = append_action(list,"win.",g_action_map_lookup_action(G_ACTION_MAP(window),action_names[ix]));
		}
		g_strfreev(action_names);

	}

 	return (Pw3270ActionList *) list;
 }

 void list_element_free(struct ListElement *element) {

 	if(element->pixbuf) {
		g_object_unref(element->pixbuf);
		element->pixbuf = NULL;
 	}

 	g_free(element);

 }

 void pw3270_action_list_free(Pw3270ActionList *action_list) {
	g_slist_free_full((GSList *) action_list, (GDestroyNotify) list_element_free);
 }

 void pw3270_action_view_move_selected(GtkWidget *from, GtkWidget *to) {

	size_t ix;

	//
	// Get list of selected references.
	//
	GtkTreeModel * fromModel = gtk_tree_view_get_model(GTK_TREE_VIEW(from));
	GList * list = gtk_tree_selection_get_selected_rows(
							gtk_tree_view_get_selection(GTK_TREE_VIEW(from)),
							&fromModel);

	guint rowCount = g_list_length(list);

	g_autofree GtkTreeRowReference ** rows = g_new0(GtkTreeRowReference *,rowCount);
	GList * item = list;
	for(ix=0;ix < rowCount && item; ix++) {
		rows[ix] = gtk_tree_row_reference_new(fromModel,(GtkTreePath *) item->data);
		item = g_list_next(item);
	}

	g_list_free_full(list, (GDestroyNotify) gtk_tree_path_free);

	//
	// Move references
	//
	for(ix = 0; ix < rowCount && rows[ix]; ix++) {

		GtkTreePath * path = gtk_tree_row_reference_get_path(rows[ix]);

		if(path) {
			GtkTreeIter	iter;

			if (gtk_tree_model_get_iter(GTK_TREE_MODEL(fromModel), &iter, path)) {

				GValue vFlags = G_VALUE_INIT;
				gtk_tree_model_get_value(fromModel,&iter,COLUMN_FLAGS,&vFlags);
				gint flags = g_value_get_int(&vFlags);
				g_value_unset(&vFlags);

				if(flags & PW3270_ACTION_VIEW_FLAG_ALLOW_ADD) {

					// Add on target widget.
					GValue pixbuf = G_VALUE_INIT;
					GValue label  = G_VALUE_INIT;
					GValue action_name = G_VALUE_INIT;

					gtk_tree_model_get_value(fromModel,&iter,COLUMN_PIXBUF,&pixbuf);
					gtk_tree_model_get_value(fromModel,&iter,COLUMN_LABEL,&label);
					gtk_tree_model_get_value(fromModel,&iter,COLUMN_ACTION_NAME,&action_name);

					GtkListStore * store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(to)));

					GtkTreeIter ins;
					gtk_list_store_append(store, &ins);
					gtk_list_store_set(
						store,
						&ins,
						COLUMN_PIXBUF,		g_value_get_object(&pixbuf),
						COLUMN_LABEL, 		g_value_get_string(&label),
						COLUMN_ACTION_NAME,	g_value_get_string(&action_name),
						COLUMN_FLAGS,		(flags == 3 ? 3 : 2),
						-1
					);

					g_value_unset(&pixbuf);
					g_value_unset(&label);
					g_value_unset(&action_name);

				}

				if(flags & PW3270_ACTION_VIEW_ALLOW_REMOVE) {

					// Remove from source widget.
					gtk_list_store_remove(GTK_LIST_STORE(fromModel), &iter);

				}


			}

        }

	}

 }

 gboolean get_action_name(GtkTreeModel *model, GtkTreePath G_GNUC_UNUSED(*path), GtkTreeIter *iter, GString *str) {

	GValue value = G_VALUE_INIT;
	gtk_tree_model_get_value(model,iter,COLUMN_ACTION_NAME,&value);

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

	g_string_append(str,g_value_get_string(&value));

	g_value_unset(&value);
	return FALSE;
 }

 gchar * pw3270_action_view_get_action_names(GtkWidget *widget) {

 	GString *str = g_string_new("");

 	gtk_tree_model_foreach(
		gtk_tree_view_get_model(GTK_TREE_VIEW(widget)),
		(GtkTreeModelForeachFunc) get_action_name,
		str );

	return g_string_free(str,FALSE);
 }

 static void check_4_sensitive(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gboolean *sensitive) {

	GValue value = { 0, };
	gtk_tree_model_get_value(model,iter,COLUMN_FLAGS,&value);

	if(!(g_value_get_int(&value) & PW3270_ACTION_VIEW_FLAG_ALLOW_ADD))
		*sensitive = FALSE;

	g_value_unset(&value);

 }

 static void selection_changed(GtkTreeSelection *selection, GtkWidget *button) {

	if(!gtk_tree_selection_count_selected_rows(selection)) {
		gtk_widget_set_sensitive(button,FALSE);
		return;
	}

	gboolean sensitive = TRUE;

	// Scan selected rows
	gtk_tree_selection_selected_foreach(selection,(GtkTreeSelectionForeachFunc) check_4_sensitive,&sensitive);
	gtk_widget_set_sensitive(button,sensitive);

 }

 GtkWidget * pw3270_action_view_extract_button_new(GtkWidget *widget, const gchar *icon_name) {

	GtkWidget * button = gtk_button_new_from_icon_name(icon_name,GTK_ICON_SIZE_DND);

	gtk_widget_set_focus_on_click(button,FALSE);
	gtk_button_set_relief(GTK_BUTTON(button),GTK_RELIEF_NONE);
	gtk_widget_set_sensitive(button,FALSE);

	g_signal_connect(
		gtk_tree_view_get_selection(GTK_TREE_VIEW(widget)),
		"changed",
		G_CALLBACK(selection_changed),
		button
	);

	return button;
 }