gth-window.c 21.7 KB
Newer Older
Paolo Bacchilega's avatar
Paolo Bacchilega committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  GThumb
 *
 *  Copyright (C) 2005-2008 Free Software Foundation, Inc.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
Paolo Bacchilega's avatar
Paolo Bacchilega committed
20 21 22 23
 */

#include <config.h>
#include <gtk/gtk.h>
24
#include "glib-utils.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
25
#include "gth-window.h"
26
#include "gth-window-title.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
27
#include "gtk-utils.h"
28
#include "main.h"
Paolo Bacchilega's avatar
Paolo Bacchilega committed
29

30

Paolo Bacchilega's avatar
Paolo Bacchilega committed
31
enum  {
32
	PROP_0,
33 34
	PROP_N_PAGES,
	PROP_USE_HEADER_BAR
Paolo Bacchilega's avatar
Paolo Bacchilega committed
35 36 37 38
};


struct _GthWindowPrivate {
39
	int              n_pages;
40
	gboolean         use_header_bar;
41
	int              current_page;
42
	GtkWidget       *overlay;
43
	GtkWidget       *grid;
44
	GtkWidget       *stack;
45
	GtkWidget       *headerbar_container;
46
	GtkWidget       *headerbar;
47
	GtkWidget       *title;
48 49 50 51 52 53
	GtkWidget       *menubar;
	GtkWidget       *toolbar;
	GtkWidget       *infobar;
	GtkWidget       *statusbar;
	GtkWidget      **toolbars;
	GtkWidget      **contents;
54
	GtkWidget      **pages;
55 56
	GthWindowSize   *window_size;
	GtkWindowGroup  *window_group;
57
	GtkAccelGroup   *accel_group;
58 59
	GHashTable      *shortcuts;
	GPtrArray       *shortcuts_v;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
60 61 62
};


63 64 65 66 67 68
G_DEFINE_TYPE_WITH_CODE (GthWindow,
			 gth_window,
			 GTK_TYPE_APPLICATION_WINDOW,
			 G_ADD_PRIVATE (GthWindow))


69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static gboolean
overlay_get_child_position_cb (GtkOverlay   *overlay,
			       GtkWidget    *widget,
			       GdkRectangle *allocation,
			       gpointer      user_data)
{
	GtkAllocation main_alloc;

	gtk_widget_get_allocation (gtk_bin_get_child (GTK_BIN (overlay)), &main_alloc);

	allocation->x = 0;
	allocation->y = 0;
	allocation->width = main_alloc.width;
	gtk_widget_get_preferred_height (widget, NULL, &allocation->height);

	return TRUE;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
88 89 90 91 92 93
static void
gth_window_set_n_pages (GthWindow *self,
			int        n_pages)
{
	int i;

94 95 96 97 98
	if (self->priv->n_pages != 0) {
		g_critical ("The number of pages of a GthWindow can be set only once.");
		return;
	}

Paolo Bacchilega's avatar
Paolo Bacchilega committed
99 100
	self->priv->n_pages = n_pages;

101 102 103 104 105 106 107 108 109 110
	self->priv->overlay = gtk_overlay_new ();
	gtk_style_context_add_class (gtk_widget_get_style_context (self->priv->overlay), "window-overlay");
	gtk_widget_show (self->priv->overlay);
	gtk_container_add (GTK_CONTAINER (self), self->priv->overlay);

	g_signal_connect (self->priv->overlay,
			  "get-child-position",
			  G_CALLBACK (overlay_get_child_position_cb),
			  self);

111 112
	self->priv->grid = gtk_grid_new ();
	gtk_widget_show (self->priv->grid);
113
	gtk_container_add (GTK_CONTAINER (self->priv->overlay), self->priv->grid);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
114

115 116 117
	self->priv->stack = gtk_stack_new ();
	gtk_stack_set_transition_type (GTK_STACK (self->priv->stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);
	gtk_widget_show (self->priv->stack);
118
	gtk_grid_attach (GTK_GRID (self->priv->grid),
119
			 self->priv->stack,
120 121
			 0, 2,
			 1, 1);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
122

Paolo Bacchilega's avatar
Paolo Bacchilega committed
123 124
	self->priv->toolbars = g_new0 (GtkWidget *, n_pages);
	self->priv->contents = g_new0 (GtkWidget *, n_pages);
125
	self->priv->pages = g_new0 (GtkWidget *, n_pages);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
126 127 128 129

	for (i = 0; i < n_pages; i++) {
		GtkWidget *page;

130
		self->priv->pages[i] = page = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
131
		gtk_widget_show (page);
132
		gtk_container_add (GTK_CONTAINER (self->priv->stack), page);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
133

134
		self->priv->toolbars[i] = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
135 136
		gtk_widget_show (self->priv->toolbars[i]);
		gtk_box_pack_start (GTK_BOX (page), self->priv->toolbars[i], FALSE, FALSE, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
137

138
		self->priv->contents[i] = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
139
		gtk_widget_hide (self->priv->contents[i]);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
140
		gtk_box_pack_start (GTK_BOX (page), self->priv->contents[i], TRUE, TRUE, 0);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
141
	}
142 143 144 145

	self->priv->window_size = g_new0 (GthWindowSize, n_pages);
	for (i = 0; i < n_pages; i++)
		self->priv->window_size[i].saved = FALSE;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
146 147 148
}


149 150 151 152 153 154
static void
_gth_window_add_header_bar (GthWindow *self)
{
	self->priv->headerbar = gtk_header_bar_new ();
	gtk_widget_show (self->priv->headerbar);
	gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->priv->headerbar), TRUE);
155

Paolo Bacchilega's avatar
Paolo Bacchilega committed
156
	g_object_add_weak_pointer (G_OBJECT (self->priv->headerbar), (gpointer *) &self->priv->headerbar);
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
	{
		gboolean  shell_shows_app_menu;
		char     *decoration_layout;

		g_object_get (gtk_settings_get_default (),
			      "gtk-shell-shows-app-menu", &shell_shows_app_menu,
			      "gtk-decoration-layout", &decoration_layout,
			      NULL);
		if (! shell_shows_app_menu && ((decoration_layout == NULL) || (strstr (decoration_layout, "menu") == NULL))) {
			gboolean  left_part_is_empty;
			char     *new_layout;

			/* add 'menu' to the left */

			left_part_is_empty = (decoration_layout == NULL) || (decoration_layout[0] == '\0') || (decoration_layout[0] == ':');
			new_layout = g_strconcat ("menu", (left_part_is_empty ? "" : ","), decoration_layout, NULL);
			gtk_header_bar_set_decoration_layout (GTK_HEADER_BAR (self->priv->headerbar), new_layout);

			g_free (new_layout);
		}

		g_free (decoration_layout);
	}

182 183 184
	self->priv->title = gth_window_title_new ();
	gtk_widget_show (self->priv->title);
	gtk_header_bar_set_custom_title (GTK_HEADER_BAR (self->priv->headerbar), self->priv->title);
185 186 187 188 189 190

	self->priv->headerbar_container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
	gtk_widget_show (self->priv->headerbar_container);
	gtk_box_pack_start (GTK_BOX (self->priv->headerbar_container), self->priv->headerbar, TRUE, TRUE, 0);

	gtk_window_set_titlebar (GTK_WINDOW (self), self->priv->headerbar_container);
191 192 193
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
194 195 196 197 198 199 200 201 202 203 204
static void
gth_window_set_property (GObject      *object,
			 guint         property_id,
			 const GValue *value,
			 GParamSpec   *pspec)
{
	GthWindow *self;

	self = GTH_WINDOW (object);

	switch (property_id) {
205
	case PROP_N_PAGES:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
206 207
		gth_window_set_n_pages (self, g_value_get_int (value));
		break;
208 209 210 211 212
	case PROP_USE_HEADER_BAR:
		self->priv->use_header_bar = g_value_get_boolean (value);
		if (self->priv->use_header_bar)
			_gth_window_add_header_bar (self);
		break;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
213 214 215 216 217 218 219
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


220 221 222 223 224 225 226 227 228 229 230 231 232 233
static void
gth_window_get_property (GObject    *object,
		         guint       property_id,
		         GValue     *value,
		         GParamSpec *pspec)
{
	GthWindow *self;

        self = GTH_WINDOW (object);

	switch (property_id) {
	case PROP_N_PAGES:
		g_value_set_int (value, self->priv->n_pages);
		break;
234 235 236
	case PROP_USE_HEADER_BAR:
		g_value_set_boolean (value, self->priv->use_header_bar);
		break;
237 238 239 240 241 242 243
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
244 245 246 247 248
static void
gth_window_finalize (GObject *object)
{
	GthWindow *window = GTH_WINDOW (object);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
249 250
	g_free (window->priv->toolbars);
	g_free (window->priv->contents);
251
	g_free (window->priv->pages);
252
	g_free (window->priv->window_size);
253
	g_object_unref (window->priv->window_group);
254
	g_object_unref (window->priv->accel_group);
255 256
	g_hash_table_unref (window->priv->shortcuts);
	g_ptr_array_free (window->priv->shortcuts_v, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
257

258
	G_OBJECT_CLASS (gth_window_parent_class)->finalize (object);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}


static gboolean
gth_window_delete_event (GtkWidget   *widget,
			 GdkEventAny *event)
{
	gth_window_close ((GthWindow*) widget);
	return TRUE;
}


static void
gth_window_real_close (GthWindow *window)
{
	/* virtual */
}


static void
gth_window_real_set_current_page (GthWindow *window,
				  int        page)
{
282 283
	int i;

Paolo Bacchilega's avatar
Paolo Bacchilega committed
284 285
	if (window->priv->current_page == page)
		return;
286

Paolo Bacchilega's avatar
Paolo Bacchilega committed
287
	window->priv->current_page = page;
288
	gtk_stack_set_visible_child (GTK_STACK (window->priv->stack), window->priv->pages[page]);
289 290 291 292 293 294

	for (i = 0; i < window->priv->n_pages; i++)
		if (i == page)
			gtk_widget_show (window->priv->contents[i]);
		else
			gtk_widget_hide (window->priv->contents[i]);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
295 296 297
}


298
static void
299 300
_gth_window_add_css_provider (GtkWidget  *widget,
			      const char *path)
301
{
302 303 304 305 306 307 308
	GBytes         *bytes;
	gconstpointer   css_data;
	gsize           css_data_size;
	GtkCssProvider *css_provider;
	GError         *error = NULL;


309
	bytes = g_resources_lookup_data (path, 0, &error);
310 311 312 313 314 315 316 317 318 319 320 321
	if (bytes == NULL) {
		g_warning ("%s", error->message);
		g_error_free (error);
		return;
	}

	css_data = g_bytes_get_data (bytes, &css_data_size);
	css_provider = gtk_css_provider_new ();
	if (! gtk_css_provider_load_from_data (css_provider, css_data, css_data_size, &error)) {
		g_warning ("%s", error->message);
		g_error_free (error);
	}
322
	gtk_style_context_add_provider_for_screen (gtk_widget_get_screen (widget),
323
						   GTK_STYLE_PROVIDER (css_provider),
324
						   GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
325 326

	g_object_unref (css_provider);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
327
	g_bytes_unref (bytes);
328 329 330
}


331 332 333 334 335 336 337 338
static void
gth_window_realize (GtkWidget *widget)
{
	GTK_WIDGET_CLASS (gth_window_parent_class)->realize (widget);

	gtk_icon_theme_append_search_path (gtk_icon_theme_get_for_screen (gtk_widget_get_screen (widget)), GTHUMB_ICON_DIR);

	_gth_window_add_css_provider (widget, "/org/gnome/gThumb/resources/gthumb.css");
339 340 341
	if ((gtk_major_version >= 3) && (gtk_minor_version >= 20))
		_gth_window_add_css_provider (widget, "/org/gnome/gThumb/resources/gthumb-gtk320.css");
	else if ((gtk_major_version >= 3) && (gtk_minor_version >= 14))
342 343 344 345 346 347
		_gth_window_add_css_provider (widget, "/org/gnome/gThumb/resources/gthumb-gtk314.css");
	else if ((gtk_major_version >= 3) && (gtk_minor_version >= 10))
		_gth_window_add_css_provider (widget, "/org/gnome/gThumb/resources/gthumb-gtk312.css");
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
348 349 350 351 352 353 354 355
static void
gth_window_class_init (GthWindowClass *klass)
{
	GObjectClass   *gobject_class;
	GtkWidgetClass *widget_class;

	gobject_class = (GObjectClass*) klass;
	gobject_class->set_property = gth_window_set_property;
356
	gobject_class->get_property = gth_window_get_property;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
357 358 359 360
	gobject_class->finalize = gth_window_finalize;

	widget_class = (GtkWidgetClass*) klass;
	widget_class->delete_event = gth_window_delete_event;
361
	widget_class->realize = gth_window_realize;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
362 363 364 365 366

	klass->close = gth_window_real_close;
	klass->set_current_page = gth_window_real_set_current_page;

	g_object_class_install_property (G_OBJECT_CLASS (klass),
367
					 PROP_N_PAGES,
Paolo Bacchilega's avatar
Paolo Bacchilega committed
368 369 370 371 372 373
					 g_param_spec_int ("n-pages",
							   "n-pages",
							   "n-pages",
							   0,
							   G_MAXINT,
							   1,
374
							   G_PARAM_READWRITE));
375 376 377 378 379 380 381
	g_object_class_install_property (G_OBJECT_CLASS (klass),
					 PROP_USE_HEADER_BAR,
					 g_param_spec_boolean ("use-header-bar",
							       "use-header-bar",
							       "use-header-bar",
							       FALSE,
							       G_PARAM_READWRITE));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
382 383 384 385 386 387
}


static void
gth_window_init (GthWindow *window)
{
388
	window->priv = gth_window_get_instance_private (window);
389
	window->priv->grid = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
390
	window->priv->contents = NULL;
391
	window->priv->pages = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
392
	window->priv->n_pages = 0;
393
	window->priv->current_page = GTH_WINDOW_PAGE_UNDEFINED;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
394 395
	window->priv->menubar = NULL;
	window->priv->toolbar = NULL;
396
	window->priv->infobar = NULL;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
397
	window->priv->statusbar = NULL;
398 399 400
	window->priv->headerbar = NULL;
	window->priv->use_header_bar = FALSE;

401 402
	window->priv->window_group = gtk_window_group_new ();
	gtk_window_group_add_window (window->priv->window_group, GTK_WINDOW (window));
Paolo Bacchilega's avatar
Paolo Bacchilega committed
403

404 405 406
	window->priv->accel_group = gtk_accel_group_new ();
	gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accel_group);

407 408 409
	window->priv->shortcuts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	window->priv->shortcuts_v = g_ptr_array_new_with_free_func ((GDestroyNotify) gth_shortcut_free);

410
	gtk_window_set_application (GTK_WINDOW (window), Main_Application);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}


void
gth_window_close (GthWindow *window)
{
	GTH_WINDOW_GET_CLASS (window)->close (window);
}


void
gth_window_attach (GthWindow     *window,
		   GtkWidget     *child,
		   GthWindowArea  area)
{
	int position;

	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (child != NULL);
	g_return_if_fail (GTK_IS_WIDGET (child));

	switch (area) {
	case GTH_WINDOW_MENUBAR:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
435
		window->priv->menubar = child;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
436 437 438
		position = 0;
		break;
	case GTH_WINDOW_TOOLBAR:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
439
		window->priv->toolbar = child;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
440 441
		position = 1;
		break;
442 443
	case GTH_WINDOW_INFOBAR:
		window->priv->infobar = child;
444
		position = 4;
445
		break;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
446
	case GTH_WINDOW_STATUSBAR:
Paolo Bacchilega's avatar
Paolo Bacchilega committed
447
		window->priv->statusbar = child;
448
		position = 3;
Paolo Bacchilega's avatar
Paolo Bacchilega committed
449 450 451 452 453
		break;
	default:
		return;
	}

454
	gtk_widget_set_vexpand (child, FALSE);
455
	gtk_grid_attach (GTK_GRID (window->priv->grid),
Paolo Bacchilega's avatar
Paolo Bacchilega committed
456
			  child,
457 458
			  0, position,
			  1, 1);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472
}


void
gth_window_attach_toolbar (GthWindow *window,
			   int        page,
			   GtkWidget *child)
{
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);
	g_return_if_fail (child != NULL);
	g_return_if_fail (GTK_IS_WIDGET (child));

Paolo Bacchilega's avatar
Paolo Bacchilega committed
473
	_gtk_container_remove_children (GTK_CONTAINER (window->priv->toolbars[page]), NULL, NULL);
474
	gtk_style_context_add_class (gtk_widget_get_style_context (child), GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
475
	gtk_widget_set_hexpand (child, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
476
	gtk_container_add (GTK_CONTAINER (window->priv->toolbars[page]), child);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490
}


void
gth_window_attach_content (GthWindow *window,
			   int        page,
			   GtkWidget *child)
{
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);
	g_return_if_fail (child != NULL);
	g_return_if_fail (GTK_IS_WIDGET (child));

Paolo Bacchilega's avatar
Paolo Bacchilega committed
491
	_gtk_container_remove_children (GTK_CONTAINER (window->priv->contents[page]), NULL, NULL);
492
	gtk_widget_set_hexpand (child, TRUE);
493
	gtk_widget_set_vexpand (child, TRUE);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
494
	gtk_container_add (GTK_CONTAINER (window->priv->contents[page]), child);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
495 496 497 498 499 500 501
}


void
gth_window_set_current_page (GthWindow *window,
			     int        page)
{
502 503 504 505
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);

Paolo Bacchilega's avatar
Paolo Bacchilega committed
506 507 508 509 510 511 512 513 514 515 516
	GTH_WINDOW_GET_CLASS (window)->set_current_page (window, page);
}


int
gth_window_get_current_page (GthWindow *window)
{
	return window->priv->current_page;
}


Paolo Bacchilega's avatar
Paolo Bacchilega committed
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
static void
hide_widget (GtkWidget *widget)
{
	if (widget != NULL)
		gtk_widget_hide (widget);
}


static void
show_widget (GtkWidget *widget)
{
	if (widget != NULL)
		gtk_widget_show (widget);
}


void
gth_window_show_only_content (GthWindow *window,
			      gboolean   only_content)
{
	int i;

	if (only_content) {
		for (i = 0; i < window->priv->n_pages; i++)
			hide_widget (window->priv->toolbars[i]);
		hide_widget (window->priv->menubar);
		hide_widget (window->priv->toolbar);
		hide_widget (window->priv->statusbar);
	}
	else {
		for (i = 0; i < window->priv->n_pages; i++)
			show_widget (window->priv->toolbars[i]);
		show_widget (window->priv->menubar);
		show_widget (window->priv->toolbar);
		show_widget (window->priv->statusbar);
	}
}


556 557 558 559 560 561 562 563 564 565 566
GtkWidget *
gth_window_get_area (GthWindow     *window,
		     GthWindowArea  area)
{
	switch (area) {
	case GTH_WINDOW_MENUBAR:
		return window->priv->menubar;
		break;
	case GTH_WINDOW_TOOLBAR:
		return window->priv->toolbar;
		break;
567 568 569
	case GTH_WINDOW_INFOBAR:
		return window->priv->infobar;
		break;
570 571 572 573 574 575 576 577 578 579 580
	case GTH_WINDOW_STATUSBAR:
		return window->priv->statusbar;
		break;
	default:
		break;
	}

	return NULL;
}


581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
void
gth_window_add_overlay (GthWindow *window,
			GtkWidget *widget)
{
	gtk_overlay_add_overlay (GTK_OVERLAY (window->priv->overlay), widget);
}


void
gth_window_set_header_bar (GthWindow *window,
			   GtkWidget *header_bar)
{
	if (window->priv->headerbar != header_bar) {
		if (window->priv->headerbar != NULL)
			gtk_widget_destroy (window->priv->headerbar);
		window->priv->headerbar = header_bar;
	}
	gtk_widget_show (window->priv->headerbar);
	gtk_box_pack_start (GTK_BOX (window->priv->headerbar_container), window->priv->headerbar, TRUE, TRUE, 0);
}


603 604 605 606 607 608 609
GtkWidget *
gth_window_get_header_bar (GthWindow *window)
{
	return window->priv->headerbar;
}


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
void
gth_window_save_page_size (GthWindow *window,
			   int        page,
			   int        width,
			   int        height)
{
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);

	window->priv->window_size[page].width = width;
	window->priv->window_size[page].height = height;
	window->priv->window_size[page].saved = TRUE;
}


void
gth_window_apply_saved_size (GthWindow *window,
			     int        page)
{
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);

	if (! window->priv->window_size[page].saved)
		return;

	gtk_window_resize (GTK_WINDOW (window),
			   window->priv->window_size[page].width,
			   window->priv->window_size[page].height);
}


643 644 645 646 647 648 649 650 651 652 653 654
void
gth_window_clear_saved_size (GthWindow *window,
			     int        page)
{
	g_return_if_fail (window != NULL);
	g_return_if_fail (GTH_IS_WINDOW (window));
	g_return_if_fail (page >= 0 && page < window->priv->n_pages);

	window->priv->window_size[page].saved = FALSE;
}


655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
gboolean
gth_window_get_page_size (GthWindow *window,
			  int        page,
			  int       *width,
			  int       *height)
{
	g_return_val_if_fail (window != NULL, FALSE);
	g_return_val_if_fail (GTH_IS_WINDOW (window), FALSE);
	g_return_val_if_fail (page >= 0 && page < window->priv->n_pages, FALSE);

	if (! window->priv->window_size[page].saved)
		return FALSE;

	if (width != NULL)
		*width = window->priv->window_size[page].width;
	if (height != NULL)
		*height = window->priv->window_size[page].height;

	return TRUE;
}
675 676 677


void
678 679
gth_window_set_title (GthWindow  *window,
		      const char *title,
680
		      GList	 *emblems)
681 682
{
	if (window->priv->use_header_bar) {
683 684
		gth_window_title_set_title (GTH_WINDOW_TITLE (window->priv->title), title);
		gth_window_title_set_emblems (GTH_WINDOW_TITLE (window->priv->title), emblems);
685
	}
686 687
	else
		gtk_window_set_title (GTK_WINDOW (window), title);
688
}
689 690


691 692
GtkAccelGroup *
gth_window_get_accel_group (GthWindow *window)
693
{
694 695 696 697 698 699 700
	if (window->priv->accel_group == NULL) {
		window->priv->accel_group = gtk_accel_group_new ();
		gtk_window_add_accel_group (GTK_WINDOW (window), window->priv->accel_group);
	}

	return window->priv->accel_group;
}
701

702

703 704 705 706 707 708
static void
_gth_window_add_shortcut (GthWindow   *window,
			  GthShortcut *shorcut)
{
	g_hash_table_insert (window->priv->shortcuts,
			     g_strdup (shorcut->action_name),
709
			     shorcut);
710 711 712 713
	g_ptr_array_add (window->priv->shortcuts_v, shorcut);
}


714 715 716 717 718 719 720 721 722 723 724
void
gth_window_add_accelerators (GthWindow			*window,
			     const GthAccelerator	*accelerators,
			     int		 	 n_accelerators)
{
	GtkAccelGroup *accel_group;
	int            i;

	accel_group = gth_window_get_accel_group (window);
	for (i = 0; i < n_accelerators; i++) {
		const GthAccelerator *acc = accelerators + i;
725
		GthShortcut          *shortcut;
726 727 728 729 730 731

		_gtk_window_add_accelerator_for_action (GTK_WINDOW (window),
							accel_group,
							acc->action_name,
							acc->accelerator,
							NULL);
732 733 734 735 736

		shortcut = gth_shortcut_new ();
		shortcut->action_name = g_strdup (acc->action_name);
		shortcut->context = GTH_SHORTCUT_CONTEXT_INTERNAL;
		shortcut->category = GTH_SHORTCUT_CATEGORY_HIDDEN;
737
		gth_shortcut_set_accelerator (shortcut, acc->accelerator);
738
		_gth_window_add_shortcut (window, shortcut);
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


void
gth_window_enable_action (GthWindow  *window,
			  const char *action_name,
			  gboolean    enabled)
{
	GAction *action;

	action = g_action_map_lookup_action (G_ACTION_MAP (window), action_name);
	g_object_set (action, "enabled", enabled, NULL);
}


gboolean
gth_window_get_action_state (GthWindow  *window,
			     const char *action_name)
{
	GAction  *action;
	GVariant *state;
	gboolean  value;

	action = g_action_map_lookup_action (G_ACTION_MAP (window), action_name);
	g_return_val_if_fail (action != NULL, FALSE);
	state = g_action_get_state (action);
	value = g_variant_get_boolean (state);

	g_variant_unref (state);

	return value;
}


void
gth_window_change_action_state (GthWindow  *window,
			        const char *action_name,
			        gboolean    value)
{
	GAction  *action;
	GVariant *old_state;
	GVariant *new_state;

	action = g_action_map_lookup_action (G_ACTION_MAP (window), action_name);
	g_return_if_fail (action != NULL);

	old_state = g_action_get_state (action);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
787
	new_state = g_variant_ref_sink (g_variant_new_boolean (value));
788 789 790 791 792
	if ((old_state == NULL) || ! g_variant_equal (old_state, new_state))
		g_action_change_state (action, new_state);

	if (old_state != NULL)
		g_variant_unref (old_state);
Paolo Bacchilega's avatar
Paolo Bacchilega committed
793
	g_variant_unref (new_state);
794
}
795 796 797 798 799 800 801 802 803 804 805 806 807 808


void
gth_window_add_shortcuts (GthWindow         *window,
			  const GthShortcut *shortcuts,
			  int                n_shortcuts)
{
	int i;

	for (i = 0; i < n_shortcuts; i++) {
		const GthShortcut *shortcut = shortcuts + i;
		GthShortcut       *new_shortcut;

		new_shortcut = gth_shortcut_dup (shortcut);
809
		gth_shortcut_set_accelerator (new_shortcut, shortcut->default_accelerator);
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

		_gth_window_add_shortcut (window, new_shortcut);
	}
}


GPtrArray *
gth_window_get_shortcuts (GthWindow *window)
{
	g_return_val_if_fail (GTH_IS_WINDOW (window), NULL);

	return window->priv->shortcuts_v;
}


gboolean
gth_window_activate_shortcut (GthWindow       *window,
			      int              context,
			      guint            keycode,
			      GdkModifierType  modifiers)
{
	gboolean     activated = FALSE;
	GthShortcut *shortcut;

	shortcut = gth_shortcut_array_find (window->priv->shortcuts_v, context, keycode, modifiers);
	if (shortcut != NULL) {
		GAction *action;

		action = g_action_map_lookup_action (G_ACTION_MAP (window), shortcut->action_name);
		if (action != NULL) {
			GVariant *variant;

			variant = g_action_get_state (action);
			g_action_activate (action, variant);
			activated = TRUE;

			if (variant != NULL)
				g_variant_unref (variant);
		}
	}

	return activated;
}
853 854 855 856 857 858 859 860 861 862 863


void
gth_window_load_shortcuts (GthWindow *window)
{
	g_return_if_fail (GTH_IS_WINDOW (window));

	gth_shortcuts_load_from_file (window->priv->shortcuts_v,
				      window->priv->shortcuts,
				      NULL);
}