combo-flags.c 24.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*  combo-flags.c
 *  Copyright (C) 2006 Armin Burgmeier
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19 20 21
 */

#include "combo-flags.h"

22
#include <gtk/gtk.h>
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

#include <gdk/gdkkeysyms.h>

#include <libanjuta/anjuta-marshal.h>

typedef struct _CgComboFlagsCellInfo CgComboFlagsCellInfo;
struct _CgComboFlagsCellInfo
{
  GtkCellRenderer *cell;
  GSList *attributes;

  GtkCellLayoutDataFunc func;
  gpointer func_data;
  GDestroyNotify destroy;

  guint expand : 1;
  guint pack : 1;
};

typedef struct _CgComboFlagsPrivate CgComboFlagsPrivate;
struct _CgComboFlagsPrivate
{
	GtkTreeModel* model;

	GtkWidget* window;
	GtkWidget* treeview;
	GtkTreeViewColumn* column;
50 51 52 53

	GdkDevice* pointer_device;
	GdkDevice* keyboard_device;

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	GSList* cells;

	gboolean editing_started;
	gboolean editing_canceled;
};

#define CG_COMBO_FLAGS_PRIVATE(o) \
	(G_TYPE_INSTANCE_GET_PRIVATE( \
		(o), \
		CG_TYPE_COMBO_FLAGS, \
		CgComboFlagsPrivate \
	))

enum {
	PROP_0,
	PROP_MODEL
};

enum {
	SELECTED,
	LAST_SIGNAL
};
static guint combo_flags_signals[LAST_SIGNAL];

78 79 80 81 82 83 84 85 86
static void cg_combo_flags_cell_layout_init (GtkCellLayoutIface *iface);
static void cg_combo_flags_cell_editable_init (GtkCellEditableIface *iface);

G_DEFINE_TYPE_WITH_CODE (CgComboFlags, cg_combo_flags, GTK_TYPE_BOX,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
                                                cg_combo_flags_cell_layout_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_EDITABLE,
                                                cg_combo_flags_cell_editable_init));

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
static CgComboFlagsCellInfo *
cg_combo_flags_get_cell_info (CgComboFlags *combo,
                              GtkCellRenderer *cell)
{
	CgComboFlagsPrivate *priv;
	GSList *i;

	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	for (i = priv->cells; i != NULL; i = i->next)
	{
	    CgComboFlagsCellInfo *info = (CgComboFlagsCellInfo *) i->data;

	    if (info != NULL && info->cell == cell)
			return info;
	}

	return NULL;
}

static void
cg_combo_flags_cell_layout_pack_start (GtkCellLayout *layout,
                                       GtkCellRenderer *cell,
                                       gboolean expand)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	g_object_ref (cell);
119
	g_object_ref_sink (cell);
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

	info = g_new0 (CgComboFlagsCellInfo, 1);
	info->cell = cell;
	info->expand = expand;
	info->pack = GTK_PACK_START;

	priv->cells = g_slist_append (priv->cells, info);

	if (priv->column != NULL)
		gtk_tree_view_column_pack_start (priv->column, cell, expand);
}

static void
cg_combo_flags_cell_layout_pack_end (GtkCellLayout *layout,
                                     GtkCellRenderer *cell,
                                     gboolean expand)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	g_object_ref (cell);
145
	g_object_ref_sink (cell);
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

	info = g_new0 (CgComboFlagsCellInfo, 1);
	info->cell = cell;
	info->expand = expand;
	info->pack = GTK_PACK_END;

	priv->cells = g_slist_append (priv->cells, info);

	if (priv->column != NULL)
		gtk_tree_view_column_pack_end (priv->column, cell, expand);
}

static void
cg_combo_flags_cell_layout_clear_attributes (GtkCellLayout *layout,
                                             GtkCellRenderer *cell)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;
	GSList *list;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	info = cg_combo_flags_get_cell_info (combo, cell);
	g_return_if_fail (info != NULL);

	list = info->attributes;
	while (list && list->next)
	{
		g_free (list->data);
		list = list->next->next;
	}

	g_slist_free (info->attributes);
	info->attributes = NULL;

	if (priv->column != NULL)
	{
	    gtk_cell_layout_clear_attributes (GTK_CELL_LAYOUT (priv->column), 
	                                      cell);
	}

	gtk_widget_queue_resize (GTK_WIDGET (combo));
}

static void
cg_combo_flags_cell_layout_clear (GtkCellLayout *layout)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	GSList *i;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
 
	if (priv->column != NULL)
		gtk_tree_view_column_clear (priv->column);

	for (i = priv->cells; i; i = i->next)
	{
		CgComboFlagsCellInfo* info = (CgComboFlagsCellInfo*)i->data;

		cg_combo_flags_cell_layout_clear_attributes (layout, info->cell);
		g_object_unref (info->cell);
		g_free (info);
		i->data = NULL;
	}

	g_slist_free (priv->cells);
	priv->cells = NULL;
}

static void
cg_combo_flags_cell_layout_add_attribute (GtkCellLayout *layout,
                                          GtkCellRenderer *cell,
                                          const gchar *attribute,
                                          gint column)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE(combo);
	info = cg_combo_flags_get_cell_info (combo, cell);

	info->attributes = g_slist_prepend (info->attributes,
	                                    GINT_TO_POINTER (column));
	info->attributes = g_slist_prepend (info->attributes,
	                                    g_strdup (attribute));

	if (priv->column != NULL)
	{
		gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (priv->column),
                                       cell, attribute, column);
	}

	gtk_widget_queue_resize (GTK_WIDGET (combo));
}

static void
cg_combo_flags_cell_data_func (GtkCellLayout *cell_layout,
                               GtkCellRenderer *cell,
                               GtkTreeModel *tree_model,
                               GtkTreeIter *iter,
                               gpointer data)
{
  CgComboFlagsCellInfo *info;
  info = (CgComboFlagsCellInfo *) data;

  if (info->func == NULL)
    return;

  (*info->func) (cell_layout, cell, tree_model, iter, info->func_data);
}

static void
cg_combo_flags_cell_layout_set_cell_data_func (GtkCellLayout *layout,
                                               GtkCellRenderer *cell,
                                               GtkCellLayoutDataFunc func,
                                               gpointer func_data,
                                               GDestroyNotify destroy)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;
	GDestroyNotify old_destroy;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	info = cg_combo_flags_get_cell_info (combo, cell);
	g_return_if_fail (info != NULL);

	if (info->destroy)
	{
		old_destroy = info->destroy;

		info->destroy = NULL;
		old_destroy (info->func_data);
	}

	info->func = func;
	info->func_data = func_data;
	info->destroy = destroy;

	if (priv->column != NULL)
	{
    	gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
    	                                    cell, func, func_data, NULL);
	}

	gtk_widget_queue_resize (GTK_WIDGET (combo));
}

static void
cg_combo_flags_cell_layout_reorder (GtkCellLayout *layout,
                                    GtkCellRenderer *cell,
                                    gint position)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;
	GSList *link;

	combo = CG_COMBO_FLAGS (layout);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	info = cg_combo_flags_get_cell_info (combo, cell);
	g_return_if_fail (info != NULL);

	link = g_slist_find (priv->cells, info);
	g_return_if_fail (link != NULL);

	priv->cells = g_slist_remove_link (priv->cells, link);
	priv->cells = g_slist_insert (priv->cells, info, position);

	if (priv->column != NULL)
		gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);

	gtk_widget_queue_draw (GTK_WIDGET (combo));
}

static void
cg_combo_flags_cell_editable_start_editing (GtkCellEditable *cell_editable,
                                            G_GNUC_UNUSED GdkEvent *event)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;

	combo = CG_COMBO_FLAGS (cell_editable);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	
	priv->editing_started = TRUE;
	gtk_widget_grab_focus (GTK_WIDGET (combo));
	cg_combo_flags_popup (combo);
}

static void
cg_combo_flags_sync_cells (CgComboFlags *combo,
                           GtkCellLayout *cell_layout)
{
	CgComboFlagsPrivate *priv;
	CgComboFlagsCellInfo *info;
	GSList *j;
	GSList *k;

	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	for (k = priv->cells; k != NULL; k = k->next)
	{
		info = (CgComboFlagsCellInfo *) k->data;

		if (info->pack == GTK_PACK_START)
			gtk_cell_layout_pack_start (cell_layout, info->cell, info->expand);
		else if (info->pack == GTK_PACK_END)
			gtk_cell_layout_pack_end (cell_layout, info->cell, info->expand);

		gtk_cell_layout_set_cell_data_func (cell_layout, info->cell,
		                                    cg_combo_flags_cell_data_func,
		                                    info, NULL);

		for (j = info->attributes; j != NULL; j = j->next->next)
		{
			gtk_cell_layout_add_attribute (cell_layout, info->cell, j->data,
			                               GPOINTER_TO_INT (j->next->data));
		}
	}
}

static void
cg_combo_flags_get_position (CgComboFlags *combo,
                             gint *x, 
                             gint *y, 
                             gint *width,
                             gint *height)
{
	CgComboFlagsPrivate *priv;
383 384 385
	GtkAllocation allocation;
	GtkRequisition popup_req;
	GdkWindow *window;
386 387
	GdkScreen *screen;
	GdkRectangle monitor;
388
	gint monitor_num;
389 390 391 392 393
  
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	
	g_assert (priv->window != NULL);

394 395
	window = gtk_widget_get_window (GTK_WIDGET (combo));
	gdk_window_get_origin (window, x, y);
396

397 398 399
	gtk_widget_get_allocation (GTK_WIDGET (combo), &allocation);

	if (!gtk_widget_get_has_window (GTK_WIDGET (combo)))
400
	{
401 402
		*x += allocation.x;
		*y += allocation.y;
403 404
	}

405
	gtk_widget_get_preferred_size (priv->window, &popup_req, NULL);
406

407
	*width = allocation.width;
408 409 410 411
	if (popup_req.width > *width) *width = popup_req.width;
	*height = popup_req.height;

	screen = gtk_widget_get_screen (GTK_WIDGET(combo));
412
	monitor_num = gdk_screen_get_monitor_at_window (screen, window);
413 414 415 416 417 418 419 420 421 422 423 424

	gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

	if (*x < monitor.x)
	{
		*x = monitor.x;
	}
	else if (*x + *width > monitor.x + monitor.width)
	{
		*x = monitor.x + monitor.width - *width;
	}
  
425
	if (*y + allocation.height + *height <=
426 427
	    monitor.y + monitor.height)
	{
428
		*y += allocation.height;
429 430 431 432 433 434
	}
	else if (*y - *height >= monitor.y)
	{
		*y -= *height;
	}
	else if (monitor.y + monitor.height -
435
	         (*y + allocation.height) > *y - monitor.y)
436
	{
437
		*y += allocation.height;
438 439
		*height = monitor.y + monitor.height - *y;
	}
440 441
	else
	{
442 443
		*height = *y - monitor.y;
		*y = monitor.y;
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
}

static gboolean
cg_combo_flags_treeview_button_press_cb (G_GNUC_UNUSED GtkWidget *widget,
                                         GdkEventButton *event,
                                         gpointer data)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	
	combo = CG_COMBO_FLAGS(data);
	priv = CG_COMBO_FLAGS_PRIVATE(combo);

	switch (event->button)
	{
	case 1:
		selection =
			gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));

		if(gtk_tree_selection_get_selected (selection, NULL, &iter) == TRUE)
		{
			g_signal_emit (G_OBJECT (combo), combo_flags_signals[SELECTED],
469
			               0, &iter, CG_COMBO_FLAGS_SELECTION_TOGGLE);
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

			return TRUE;
		}

		return FALSE;
		break;
	case 3:
		priv->editing_canceled = FALSE;
		cg_combo_flags_popdown (combo);
		return TRUE;
		break;
	default:
		return FALSE;
		break;
	}
}

static gboolean
cg_combo_flags_treeview_key_press_cb (G_GNUC_UNUSED GtkWidget *widget,
                                      GdkEventKey *event,
                                      gpointer data)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	GtkTreeSelection *selection;
	GtkTreeIter iter;
	
	combo = CG_COMBO_FLAGS (data);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	
	switch (event->keyval)
	{
Johannes Schmid's avatar
Johannes Schmid committed
502 503
	case GDK_KEY_space:
	case GDK_KEY_KP_Space:
504 505 506 507 508 509
		selection =
			gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));

		if(gtk_tree_selection_get_selected (selection, NULL, &iter) == TRUE)
		{
			g_signal_emit (G_OBJECT (combo), combo_flags_signals[SELECTED],
510
			               0, &iter, CG_COMBO_FLAGS_SELECTION_TOGGLE);
511 512 513 514 515 516

			return TRUE;
		}

		return FALSE;
		break;
Johannes Schmid's avatar
Johannes Schmid committed
517 518
	case GDK_KEY_Return:
	case GDK_KEY_KP_Enter:
519 520 521 522 523 524 525 526 527
		selection =
			gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));

		if(gtk_tree_selection_get_selected (selection, NULL, &iter) == TRUE)
		{
			g_signal_emit (G_OBJECT (combo), combo_flags_signals[SELECTED],
			               0, &iter, CG_COMBO_FLAGS_SELECTION_SELECT);
		}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
		priv->editing_canceled = FALSE;
		cg_combo_flags_popdown (combo);
		return TRUE;
		break;
	default:
		return FALSE;
		break;
	}
}

static gboolean
cg_combo_flags_window_button_press_cb (G_GNUC_UNUSED GtkWidget *widget,
                                       G_GNUC_UNUSED GdkEventButton *event,
                                       gpointer data)
{
	/* Probably the mouse clicked somewhere else but not into the window,
	 * otherwise the treeview would have received the event. */
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	
	combo = CG_COMBO_FLAGS (data);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	
551
	priv->editing_canceled = FALSE;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	cg_combo_flags_popdown (combo);

	return TRUE;
}

static gboolean
cg_combo_flags_window_key_press_cb (G_GNUC_UNUSED GtkWidget *widget,
                                    GdkEventKey *event,
                                    gpointer data)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	
	combo = CG_COMBO_FLAGS (data);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);
	
	switch (event->keyval)
	{
Johannes Schmid's avatar
Johannes Schmid committed
570
	case GDK_KEY_Escape:
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
		priv->editing_canceled = TRUE;
		cg_combo_flags_popdown (combo);
		return TRUE;
		break;
	default:
		return FALSE;
		break;
	}
}

static void
cg_combo_flags_init (CgComboFlags *combo_flags)
{
	CgComboFlagsPrivate *priv;
	priv = CG_COMBO_FLAGS_PRIVATE (combo_flags);

	priv->model = NULL;	
	priv->window = NULL;
	priv->treeview = NULL;
	priv->column = NULL;
	priv->cells = NULL;
	
	priv->editing_started = FALSE;
	priv->editing_canceled = FALSE;
}

static void 
cg_combo_flags_finalize (GObject *object)
{
	CgComboFlags *combo_flags;
	CgComboFlagsPrivate *priv;

	combo_flags = CG_COMBO_FLAGS (object);
	priv = CG_COMBO_FLAGS_PRIVATE (combo_flags);

	if (priv->window != NULL)
		cg_combo_flags_popdown (combo_flags);

609
	G_OBJECT_CLASS (cg_combo_flags_parent_class)->finalize (object);
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
}

static void
cg_combo_flags_set_property (GObject *object,
                             guint prop_id,
                             const GValue *value,
                             GParamSpec *pspec)
{
	CgComboFlags *combo_flags;
	CgComboFlagsPrivate *priv;

	g_return_if_fail(CG_IS_COMBO_FLAGS (object));

	combo_flags = CG_COMBO_FLAGS (object);
	priv = CG_COMBO_FLAGS_PRIVATE (combo_flags);

	switch (prop_id)
	{
	case PROP_MODEL:
		if (priv->model != NULL) g_object_unref (G_OBJECT (priv->model));
		priv->model = GTK_TREE_MODEL (g_value_dup_object (value));

		if (priv->treeview != NULL)
		{
			gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
			                         priv->model);
		}

		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cg_combo_flags_get_property (GObject *object,
                             guint prop_id,
                             GValue *value, 
                             GParamSpec *pspec)
{
	CgComboFlags *combo_flags;
	CgComboFlagsPrivate *priv;

	g_return_if_fail (CG_IS_COMBO_FLAGS (object));

	combo_flags = CG_COMBO_FLAGS (object);
	priv = CG_COMBO_FLAGS_PRIVATE (combo_flags);

	switch (prop_id)
	{
	case PROP_MODEL:
		g_value_set_object (value, G_OBJECT (priv->model));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
cg_combo_flags_class_init (CgComboFlagsClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (CgComboFlagsPrivate));

	object_class->finalize = cg_combo_flags_finalize;
	object_class->set_property = cg_combo_flags_set_property;
	object_class->get_property = cg_combo_flags_get_property;

	g_object_class_install_property(object_class,
	                                PROP_MODEL,
	                                g_param_spec_object("model",
	                                                    "Model",
	                                                    "The model used by the CgComboFlags widget",
	                                                    GTK_TYPE_TREE_MODEL,
	                                                    G_PARAM_READWRITE));

	combo_flags_signals[SELECTED] =
		g_signal_new("selected",
		             G_OBJECT_CLASS_TYPE(object_class),
		             G_SIGNAL_RUN_LAST,
		             0, /* no default handler */
		             NULL, NULL,
695
		             anjuta_cclosure_marshal_VOID__BOXED_ENUM,
696
		             G_TYPE_NONE,
697 698 699
		             2,
		             GTK_TYPE_TREE_ITER,
	                     CG_TYPE_COMBO_FLAGS_SELECTION_TYPE);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
}

static void
cg_combo_flags_cell_layout_init (GtkCellLayoutIface *iface)
{
	iface->pack_start = cg_combo_flags_cell_layout_pack_start;
	iface->pack_end = cg_combo_flags_cell_layout_pack_end;
	iface->reorder = cg_combo_flags_cell_layout_reorder;
	iface->clear = cg_combo_flags_cell_layout_clear;
	iface->add_attribute = cg_combo_flags_cell_layout_add_attribute;
	iface->set_cell_data_func = cg_combo_flags_cell_layout_set_cell_data_func;
	iface->clear_attributes = cg_combo_flags_cell_layout_clear_attributes;
}

static void
cg_combo_flags_cell_editable_init (GtkCellEditableIface *iface)
{
	iface->start_editing = cg_combo_flags_cell_editable_start_editing;
}

static gboolean
cg_combo_flags_popup_idle (gpointer data)
{
	CgComboFlags *combo;
	CgComboFlagsPrivate *priv;
	GtkTreeSelection* selection;
	GtkWidget *toplevel;
	GtkWidget *scrolled;
728
	GdkWindow *window;
729
	GdkDeviceManager* device_manager;
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	gint height, width, x, y;

	combo = CG_COMBO_FLAGS (data);
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	g_assert (priv->window == NULL);
	priv->window = gtk_window_new (GTK_WINDOW_POPUP);

	g_object_ref (G_OBJECT (priv->window));
	gtk_window_set_resizable (GTK_WINDOW (priv->window), FALSE);

	g_signal_connect (G_OBJECT (priv->window), "key_press_event",
	                  G_CALLBACK (cg_combo_flags_window_key_press_cb),
	                  combo);

	g_signal_connect (G_OBJECT (priv->window), "button_press_event",
	                  G_CALLBACK (cg_combo_flags_window_button_press_cb),
	                  combo);

	scrolled = gtk_scrolled_window_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER (priv->window), scrolled);

	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
									     GTK_SHADOW_ETCHED_IN);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
  								    GTK_POLICY_NEVER, GTK_POLICY_NEVER);

	gtk_widget_show (scrolled);

	priv->treeview = gtk_tree_view_new_with_model (priv->model);
	gtk_widget_show (priv->treeview);
	gtk_container_add (GTK_CONTAINER (scrolled), priv->treeview);

	g_signal_connect (G_OBJECT (priv->treeview), "key_press_event",
	                  G_CALLBACK (cg_combo_flags_treeview_key_press_cb),
	                  combo);

	g_signal_connect (G_OBJECT (priv->treeview), "button_press_event",
	                  G_CALLBACK (cg_combo_flags_treeview_button_press_cb),
	                  combo);

	priv->column = gtk_tree_view_column_new ();
	g_object_ref (G_OBJECT (priv->column));
	cg_combo_flags_sync_cells (combo, GTK_CELL_LAYOUT (priv->column));
	gtk_tree_view_append_column (GTK_TREE_VIEW (priv->treeview), priv->column);

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
	gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);

	gtk_tree_view_set_enable_search (GTK_TREE_VIEW (priv->treeview), FALSE);
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->treeview), FALSE);
	gtk_tree_view_set_hover_selection (GTK_TREE_VIEW (priv->treeview), TRUE);

	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (combo));
	if (GTK_IS_WINDOW (toplevel))
	{
		gtk_window_group_add_window (gtk_window_get_group (
		                             GTK_WINDOW (toplevel)),
		                             GTK_WINDOW (priv->window));

		gtk_window_set_transient_for (GTK_WINDOW (priv->window),
		                              GTK_WINDOW (toplevel));

	}
	
	gtk_window_set_screen (GTK_WINDOW (priv->window),
                           gtk_widget_get_screen (GTK_WIDGET (combo)));

	cg_combo_flags_get_position (combo, &x, &y, &width, &height);
	gtk_widget_set_size_request (priv->window, width, height);
	gtk_window_move (GTK_WINDOW(priv->window), x, y);
	gtk_widget_show (priv->window);

	gtk_widget_grab_focus (priv->window);
805
	if (!gtk_widget_has_focus (priv->treeview))
806 807
		gtk_widget_grab_focus (priv->treeview);

808 809
	window = gtk_widget_get_window (priv->window);

810 811 812
	device_manager = gdk_display_get_device_manager (gdk_window_get_display (window));
	priv->pointer_device = gdk_device_manager_get_client_pointer (device_manager);
	priv->keyboard_device = gdk_device_get_associated_device (priv->pointer_device);
813 814 815

	gtk_grab_add (priv->window);

816 817 818 819 820 821 822
	gdk_device_grab (priv->pointer_device, window, GDK_OWNERSHIP_NONE, TRUE,
	                 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK,
	                 NULL, GDK_CURRENT_TIME);

	gdk_device_grab (priv->keyboard_device, window, GDK_OWNERSHIP_NONE, TRUE,
	                 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
	                 NULL, GDK_CURRENT_TIME);
823 824 825 826 827 828 829 830 831 832
	return FALSE;
}

static gboolean
cg_combo_flags_popdown_idle (gpointer data)
{
	gtk_widget_destroy (GTK_WIDGET (data));
	return FALSE;
}

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
GType
cg_combo_flags_selection_type_get_type (void)
{
	static GType our_type = 0;

	if(our_type == 0)
	{
		static const GEnumValue values[] =
		{
			{ CG_COMBO_FLAGS_SELECTION_NONE, "CG_COMBO_FLAGS_SELECTION_NONE", "none" },
			{ CG_COMBO_FLAGS_SELECTION_UNSELECT, "CG_COMBO_FLAGS_SELECTION_UNSELECT", "unselect" },
			{ CG_COMBO_FLAGS_SELECTION_SELECT, "CG_COMBO_FLAGS_SELECTION_SELECT", "select" },
			{ CG_COMBO_FLAGS_SELECTION_TOGGLE, "CG_COMBO_FLAGS_SELECTION_TOGGLE", "toggle" },
			{ 0, NULL, NULL }
		};

		our_type = g_enum_register_static("CgComboFlagsSelectionType", values);
	}

	return our_type;
}

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
GtkWidget *
cg_combo_flags_new (void)
{
	GObject *object;	
	object = g_object_new (CG_TYPE_COMBO_FLAGS, NULL);
	return GTK_WIDGET (object);
}

GtkWidget *
cg_combo_flags_new_with_model (GtkTreeModel *model)
{
	GObject *object;
	object = g_object_new (CG_TYPE_COMBO_FLAGS, "model", model, NULL);
	return GTK_WIDGET (object);
}

void
cg_combo_flags_popup(CgComboFlags *combo)
{
	g_idle_add(cg_combo_flags_popup_idle, combo);
}

void
cg_combo_flags_popdown(CgComboFlags *combo)
{
	CgComboFlagsPrivate *priv;
	priv = CG_COMBO_FLAGS_PRIVATE (combo);

	if (priv->window != NULL)
	{
		gtk_grab_remove (priv->window);
886 887
		gdk_device_ungrab (priv->pointer_device, GDK_CURRENT_TIME);
		gdk_device_ungrab (priv->keyboard_device, GDK_CURRENT_TIME);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
		gtk_widget_hide (priv->window);

		g_object_unref (priv->column);
		g_idle_add (cg_combo_flags_popdown_idle, priv->window);

		priv->window = NULL;
		priv->treeview = NULL;
		priv->column = NULL;

		if (priv->editing_started)
		{
			priv->editing_started = FALSE;
			gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (combo));

			/* Seems like someone already calls _remove_widget when the
			 * cell renderer emits its edited signal (which we rely on if
			 * the editing was not canceled). */
			if (priv->editing_canceled)
				gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (combo));
		}
	}
}

gboolean
cg_combo_flags_editing_canceled (CgComboFlags *combo)
{
	return CG_COMBO_FLAGS_PRIVATE (combo)->editing_canceled;
}