gedit-multi-notebook.c 28.6 KB
Newer Older
1
/*
2
 * gedit-multi-notebook.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This file is part of gedit
 *
 * Copyright (C) 2010 - Ignacio Casal Quinteiro
 *
 * gedit 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.
 *
 * gedit 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
 * along with gedit; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
20 21 22 23
 * Boston, MA  02110-1301  USA
 */

#include "gedit-multi-notebook.h"
24

25 26
#include "gedit-enum-types.h"
#include "gedit-settings.h"
27 28
#include "gedit-tab-private.h"
#include "gedit-tab.h"
29 30 31 32 33 34 35 36 37

struct _GeditMultiNotebookPrivate
{
	GtkWidget *active_notebook;
	GList     *notebooks;
	gint       total_tabs;

	GeditTab  *active_tab;

38 39 40 41
	GeditNotebookShowTabsModeType show_tabs_mode;
	GSettings *ui_settings;

	guint      show_tabs : 1;
42 43 44 45 46 47 48
	guint      removing_notebook : 1;
};

enum
{
	PROP_0,
	PROP_ACTIVE_NOTEBOOK,
49
	PROP_ACTIVE_TAB,
50 51
	PROP_SHOW_TABS_MODE,
	LAST_PROP
52 53
};

54 55
static GParamSpec *properties[LAST_PROP];

56 57 58 59 60 61
enum
{
	NOTEBOOK_ADDED,
	NOTEBOOK_REMOVED,
	TAB_ADDED,
	TAB_REMOVED,
62
	SWITCH_TAB,
63
	TAB_CLOSE_REQUEST,
64
	CREATE_WINDOW,
65
	PAGE_REORDERED,
66 67 68 69
	SHOW_POPUP_MENU,
	LAST_SIGNAL
};

70
static guint signals[LAST_SIGNAL];
71

72
G_DEFINE_TYPE_WITH_PRIVATE (GeditMultiNotebook, gedit_multi_notebook, GTK_TYPE_GRID)
73

74
static void	remove_notebook		(GeditMultiNotebook *mnb,
75 76
					 GtkWidget          *notebook);

77 78
static void	update_tabs_visibility	(GeditMultiNotebook *mnb);

79 80 81 82 83 84
static void
gedit_multi_notebook_get_property (GObject    *object,
				   guint       prop_id,
				   GValue     *value,
				   GParamSpec *pspec)
{
85
	GeditMultiNotebook *mnb = GEDIT_MULTI_NOTEBOOK (object);
86 87 88 89 90

	switch (prop_id)
	{
		case PROP_ACTIVE_NOTEBOOK:
			g_value_set_object (value,
91
					    mnb->priv->active_notebook);
92 93 94
			break;
		case PROP_ACTIVE_TAB:
			g_value_set_object (value,
95
					    mnb->priv->active_tab);
96
			break;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		case PROP_SHOW_TABS_MODE:
			g_value_set_enum (value,
					  mnb->priv->show_tabs_mode);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gedit_multi_notebook_set_property (GObject      *object,
				   guint         prop_id,
				   const GValue *value,
				   GParamSpec   *pspec)
{
	GeditMultiNotebook *mnb = GEDIT_MULTI_NOTEBOOK (object);

	switch (prop_id)
	{
		case PROP_SHOW_TABS_MODE:
			mnb->priv->show_tabs_mode = g_value_get_enum (value);
			update_tabs_visibility (mnb);
			break;
121 122 123 124 125 126
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

127 128 129 130 131 132 133 134 135 136
static void
gedit_multi_notebook_dispose (GObject *object)
{
	GeditMultiNotebook *mnb = GEDIT_MULTI_NOTEBOOK (object);

	g_clear_object (&mnb->priv->ui_settings);

	G_OBJECT_CLASS (gedit_multi_notebook_parent_class)->dispose (object);
}

137 138 139
static void
gedit_multi_notebook_finalize (GObject *object)
{
140
	GeditMultiNotebook *mnb = GEDIT_MULTI_NOTEBOOK (object);
141

142
	g_list_free (mnb->priv->notebooks);
143 144 145 146 147 148 149 150 151

	G_OBJECT_CLASS (gedit_multi_notebook_parent_class)->finalize (object);
}

static void
gedit_multi_notebook_class_init (GeditMultiNotebookClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

152
	object_class->dispose = gedit_multi_notebook_dispose;
153 154
	object_class->finalize = gedit_multi_notebook_finalize;
	object_class->get_property = gedit_multi_notebook_get_property;
155
	object_class->set_property = gedit_multi_notebook_set_property;
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	properties[PROP_ACTIVE_NOTEBOOK] =
		g_param_spec_object ("active-notebook",
		                     "Active Notebook",
		                     "The Active Notebook",
		                     GEDIT_TYPE_NOTEBOOK,
		                     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
	properties[PROP_ACTIVE_TAB] =
		g_param_spec_object ("active-tab",
		                     "Active Tab",
		                     "The Active Tab",
		                     GEDIT_TYPE_TAB,
		                     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
	properties[PROP_SHOW_TABS_MODE] =
		g_param_spec_enum ("show-tabs-mode",
		                   "Show Tabs Mode",
		                   "When tabs should be shown",
		                   GEDIT_TYPE_NOTEBOOK_SHOW_TABS_MODE_TYPE,
		                   GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS,
		                   G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, LAST_PROP, properties);

179 180 181 182 183
	signals[NOTEBOOK_ADDED] =
		g_signal_new ("notebook-added",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, notebook_added),
Paolo Borelli's avatar
Paolo Borelli committed
184
			      NULL, NULL, NULL,
185 186 187 188 189 190 191 192
			      G_TYPE_NONE,
			      1,
			      GEDIT_TYPE_NOTEBOOK);
	signals[NOTEBOOK_REMOVED] =
		g_signal_new ("notebook-removed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, notebook_removed),
Paolo Borelli's avatar
Paolo Borelli committed
193
			      NULL, NULL, NULL,
194 195 196 197 198 199 200 201
			      G_TYPE_NONE,
			      1,
			      GEDIT_TYPE_NOTEBOOK);
	signals[TAB_ADDED] =
		g_signal_new ("tab-added",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, tab_added),
Paolo Borelli's avatar
Paolo Borelli committed
202
			      NULL, NULL, NULL,
203 204 205 206 207 208 209 210 211
			      G_TYPE_NONE,
			      2,
			      GEDIT_TYPE_NOTEBOOK,
			      GEDIT_TYPE_TAB);
	signals[TAB_REMOVED] =
		g_signal_new ("tab-removed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, tab_removed),
Paolo Borelli's avatar
Paolo Borelli committed
212
			      NULL, NULL, NULL,
213 214 215 216
			      G_TYPE_NONE,
			      2,
			      GEDIT_TYPE_NOTEBOOK,
			      GEDIT_TYPE_TAB);
217 218 219 220 221
	signals[SWITCH_TAB] =
		g_signal_new ("switch-tab",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, switch_tab),
Paolo Borelli's avatar
Paolo Borelli committed
222
			      NULL, NULL, NULL,
223
			      G_TYPE_NONE,
224 225
			      4,
			      GEDIT_TYPE_NOTEBOOK,
226
			      GEDIT_TYPE_TAB,
227
			      GEDIT_TYPE_NOTEBOOK,
228
			      GEDIT_TYPE_TAB);
229 230 231 232 233
	signals[TAB_CLOSE_REQUEST] =
		g_signal_new ("tab-close-request",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, tab_close_request),
Paolo Borelli's avatar
Paolo Borelli committed
234
			      NULL, NULL, NULL,
235 236 237 238
			      G_TYPE_NONE,
			      2,
			      GEDIT_TYPE_NOTEBOOK,
			      GEDIT_TYPE_TAB);
239 240 241 242 243
	signals[CREATE_WINDOW] =
		g_signal_new ("create-window",
		              G_TYPE_FROM_CLASS (object_class),
		              G_SIGNAL_RUN_LAST,
		              G_STRUCT_OFFSET (GeditMultiNotebookClass, create_window),
Paolo Borelli's avatar
Paolo Borelli committed
244
		              NULL, NULL, NULL,
245 246 247
		              GTK_TYPE_NOTEBOOK, 4,
		              GEDIT_TYPE_NOTEBOOK, GTK_TYPE_WIDGET,
		              G_TYPE_INT, G_TYPE_INT);
248 249 250 251 252
	signals[PAGE_REORDERED] =
		g_signal_new ("page-reordered",
		              G_OBJECT_CLASS_TYPE (object_class),
		              G_SIGNAL_RUN_FIRST,
		              G_STRUCT_OFFSET (GeditMultiNotebookClass, page_reordered),
Paolo Borelli's avatar
Paolo Borelli committed
253
		              NULL, NULL, NULL,
254 255 256 257
		              G_TYPE_NONE,
		              3,
		              GEDIT_TYPE_NOTEBOOK, GTK_TYPE_WIDGET,
		              G_TYPE_INT);
258 259 260 261 262
	signals[SHOW_POPUP_MENU] =
		g_signal_new ("show-popup-menu",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditMultiNotebookClass, show_popup_menu),
Paolo Borelli's avatar
Paolo Borelli committed
263
			      NULL, NULL, NULL,
264
			      G_TYPE_NONE,
265 266 267
			      2,
			      GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE,
			      GEDIT_TYPE_TAB);
268 269
}

270 271 272
static void
notebook_show_popup_menu (GtkNotebook        *notebook,
                          GdkEvent           *event,
273
                          GeditTab           *tab,
274
                          GeditMultiNotebook *mnb)
275
{
276
	g_signal_emit (G_OBJECT (mnb), signals[SHOW_POPUP_MENU], 0, event, tab);
277 278
}

279
static void
280 281
notebook_tab_close_request (GeditNotebook      *notebook,
			    GeditTab           *tab,
282
			    GeditMultiNotebook *mnb)
283
{
284
	g_signal_emit (G_OBJECT (mnb), signals[TAB_CLOSE_REQUEST], 0,
285 286 287
		       notebook, tab);
}

288 289 290 291 292 293
static GtkNotebook *
notebook_create_window (GeditNotebook      *notebook,
			GtkWidget          *child,
			gint                x,
			gint                y,
			GeditMultiNotebook *mnb)
294
{
295 296 297 298 299 300
	GtkNotebook *dest_notebook;

	g_signal_emit (G_OBJECT (mnb), signals[CREATE_WINDOW], 0,
	               notebook, child, x, y, &dest_notebook);

	return dest_notebook;
301 302 303
}

static void
304 305 306 307
notebook_page_reordered (GeditNotebook      *notebook,
		         GtkWidget          *child,
		         guint               page_num,
		         GeditMultiNotebook *mnb)
308
{
309 310
	g_signal_emit (G_OBJECT (mnb), signals[PAGE_REORDERED], 0, notebook,
	               child, page_num);
311 312
}

313 314 315 316 317 318 319 320
static void
set_active_tab (GeditMultiNotebook *mnb,
                GeditTab           *tab)
{
	mnb->priv->active_tab = tab;
	g_object_notify_by_pspec (G_OBJECT (mnb), properties[PROP_ACTIVE_TAB]);
}

321 322 323 324
static void
notebook_page_removed (GtkNotebook        *notebook,
		       GtkWidget          *child,
		       guint               page_num,
325
		       GeditMultiNotebook *mnb)
326 327 328 329 330
{
	GeditTab *tab = GEDIT_TAB (child);
	guint num_tabs;
	gboolean last_notebook;

331
	--mnb->priv->total_tabs;
332
	num_tabs = gtk_notebook_get_n_pages (notebook);
333
	last_notebook = (mnb->priv->notebooks->next == NULL);
334

335
	if (mnb->priv->total_tabs == 0)
336
	{
337
		set_active_tab (mnb, NULL);
338 339
	}

340 341
	g_signal_emit (G_OBJECT (mnb), signals[TAB_REMOVED], 0, notebook, tab);

342 343
	/* Not last notebook but last tab of the notebook, this means we have
	   to remove the current notebook */
344
	if (num_tabs == 0 && !mnb->priv->removing_notebook &&
345 346
	    !last_notebook)
	{
347
		remove_notebook (mnb, GTK_WIDGET (notebook));
348
	}
349 350

	update_tabs_visibility (mnb);
351 352 353 354 355 356
}

static void
notebook_page_added (GtkNotebook        *notebook,
		     GtkWidget          *child,
		     guint               page_num,
357
		     GeditMultiNotebook *mnb)
358 359 360
{
	GeditTab *tab = GEDIT_TAB (child);

361
	++mnb->priv->total_tabs;
362

363 364
	update_tabs_visibility (mnb);

365
	g_signal_emit (G_OBJECT (mnb), signals[TAB_ADDED], 0, notebook, tab);
366 367
}

368
static void
369
notebook_switch_page (GtkNotebook        *book,
370
		      GtkWidget          *pg,
371
		      gint                page_num,
372
		      GeditMultiNotebook *mnb)
373 374 375
{
	GeditTab *tab;

376 377 378 379 380 381
	/* When we switch a tab from a notebook that it is not the active one
	   the switch page is emitted before the set focus, so we do this check
	   and we avoid to call switch page twice */
	if (GTK_WIDGET (book) != mnb->priv->active_notebook)
		return;

382 383 384
	/* CHECK: I don't know why but it seems notebook_switch_page is called
	two times every time the user change the active tab */
	tab = GEDIT_TAB (gtk_notebook_get_nth_page (book, page_num));
385
	if (tab != mnb->priv->active_tab)
386
	{
387 388 389
		GeditTab *old_tab;

		old_tab = mnb->priv->active_tab;
390
		set_active_tab (mnb, tab);
391 392 393
		g_signal_emit (G_OBJECT (mnb), signals[SWITCH_TAB], 0,
			       mnb->priv->active_notebook, old_tab,
			       book, tab);
394 395 396 397 398 399 400 401
	}
}

/* We need to figure out if the any of the internal widget of the notebook
   has got the focus to set the active notebook */
static void
notebook_set_focus (GtkContainer       *container,
		    GtkWidget          *widget,
402
		    GeditMultiNotebook *mnb)
403 404
{
	if (GEDIT_IS_NOTEBOOK (container) &&
405
	    GTK_WIDGET (container) != mnb->priv->active_notebook)
406 407 408
	{
		gint page_num;

409
		mnb->priv->active_notebook = GTK_WIDGET (container);
410 411 412

		page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (container));
		notebook_switch_page (GTK_NOTEBOOK (container), NULL,
413
				      page_num, mnb);
414

415
		g_object_notify_by_pspec (G_OBJECT (mnb), properties[PROP_ACTIVE_NOTEBOOK]);
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
static void
show_tabs_changed (GObject     *object,
		   GParamSpec  *pspec,
		   gpointer    *data)
{
	update_tabs_visibility (GEDIT_MULTI_NOTEBOOK (data));
}

static void
update_tabs_visibility (GeditMultiNotebook *mnb)
{
	gboolean show_tabs;
	GList *l;

	if (mnb->priv->notebooks == NULL)
		return;

	if (!mnb->priv->show_tabs)
	{
		show_tabs = FALSE;
	}
	else if (mnb->priv->notebooks->next == NULL) /* only one notebook */
	{
		switch (mnb->priv->show_tabs_mode)
		{
			case GEDIT_NOTEBOOK_SHOW_TABS_NEVER:
				show_tabs = FALSE;
				break;
			case GEDIT_NOTEBOOK_SHOW_TABS_AUTO:
				show_tabs = gtk_notebook_get_n_pages (GTK_NOTEBOOK (mnb->priv->notebooks->data)) > 1;
				break;
			case GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS:
			default:
				show_tabs = TRUE;
				break;
		}
	}
	else
	{
		show_tabs = (mnb->priv->show_tabs_mode != GEDIT_NOTEBOOK_SHOW_TABS_NEVER);
	}

	g_signal_handlers_block_by_func (mnb, show_tabs_changed, NULL);

	for (l = mnb->priv->notebooks; l != NULL; l = l->next)
	{
		gtk_notebook_set_show_tabs (GTK_NOTEBOOK (l->data), show_tabs);
	}

	g_signal_handlers_unblock_by_func (mnb, show_tabs_changed, NULL);
}

471
static void
472
connect_notebook_signals (GeditMultiNotebook *mnb,
473 474 475 476 477
			  GtkWidget          *notebook)
{
	g_signal_connect (notebook,
			  "set-focus-child",
			  G_CALLBACK (notebook_set_focus),
478
			  mnb);
479 480 481
	g_signal_connect (notebook,
			  "page-added",
			  G_CALLBACK (notebook_page_added),
482
			  mnb);
483 484 485
	g_signal_connect (notebook,
			  "page-removed",
			  G_CALLBACK (notebook_page_removed),
486
			  mnb);
487 488 489
	g_signal_connect (notebook,
			  "switch-page",
			  G_CALLBACK (notebook_switch_page),
490
			  mnb);
491
	g_signal_connect (notebook,
492 493
			  "page-reordered",
			  G_CALLBACK (notebook_page_reordered),
494
			  mnb);
495
	g_signal_connect (notebook,
496 497
			  "create-window",
			  G_CALLBACK (notebook_create_window),
498
			  mnb);
499 500 501
	g_signal_connect (notebook,
			  "tab-close-request",
			  G_CALLBACK (notebook_tab_close_request),
502
			  mnb);
503
	g_signal_connect (notebook,
504 505
			  "show-popup-menu",
			  G_CALLBACK (notebook_show_popup_menu),
506
			  mnb);
507 508 509 510
	g_signal_connect (notebook,
			  "notify::show-tabs",
			  G_CALLBACK (show_tabs_changed),
			  mnb);
511 512 513
}

static void
514
disconnect_notebook_signals (GeditMultiNotebook *mnb,
515 516 517
			     GtkWidget          *notebook)
{
	g_signal_handlers_disconnect_by_func (notebook, notebook_set_focus,
518
					      mnb);
519
	g_signal_handlers_disconnect_by_func (notebook, notebook_switch_page,
520
					      mnb);
521
	g_signal_handlers_disconnect_by_func (notebook, notebook_page_added,
522
					      mnb);
523
	g_signal_handlers_disconnect_by_func (notebook, notebook_page_removed,
524
					      mnb);
525
	g_signal_handlers_disconnect_by_func (notebook, notebook_page_reordered,
526
					      mnb);
527
	g_signal_handlers_disconnect_by_func (notebook, notebook_create_window,
528
					      mnb);
529
	g_signal_handlers_disconnect_by_func (notebook, notebook_tab_close_request,
530
					      mnb);
531
	g_signal_handlers_disconnect_by_func (notebook, notebook_show_popup_menu,
532
					      mnb);
533 534
	g_signal_handlers_disconnect_by_func (notebook, show_tabs_changed,
					      mnb);
535 536 537
}

static void
538
add_notebook (GeditMultiNotebook *mnb,
539 540 541
	      GtkWidget          *notebook,
	      gboolean            main_container)
{
542 543 544
	gtk_widget_set_hexpand (notebook, TRUE);
	gtk_widget_set_vexpand (notebook, TRUE);

545 546
	if (main_container)
	{
547
		gtk_container_add (GTK_CONTAINER (mnb), notebook);
548 549 550

		mnb->priv->notebooks = g_list_append (mnb->priv->notebooks,
		                                      notebook);
551 552 553 554 555 556
	}
	else
	{
		GtkWidget *paned;
		GtkWidget *parent;
		GtkAllocation allocation;
557
		GtkWidget *active_notebook = mnb->priv->active_notebook;
558
		gint active_nb_pos;
559

560
		paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
		gtk_widget_show (paned);

		/* First we remove the active container from its parent to make
		   this we add a ref to it*/
		g_object_ref (active_notebook);
		parent = gtk_widget_get_parent (active_notebook);
		gtk_widget_get_allocation (active_notebook, &allocation);

		gtk_container_remove (GTK_CONTAINER (parent), active_notebook);
		gtk_container_add (GTK_CONTAINER (parent), paned);

		gtk_paned_pack1 (GTK_PANED (paned), active_notebook, TRUE, FALSE);
		g_object_unref (active_notebook);

		gtk_paned_pack2 (GTK_PANED (paned), notebook, FALSE, FALSE);

		/* We need to set the new paned in the right place */
		gtk_paned_set_position (GTK_PANED (paned),
		                        allocation.width / 2);

581 582 583 584 585 586
		active_nb_pos = g_list_index (mnb->priv->notebooks,
		                              active_notebook);
		mnb->priv->notebooks = g_list_insert (mnb->priv->notebooks,
		                                      notebook,
		                                      active_nb_pos + 1);
	}
587 588 589

	gtk_widget_show (notebook);

590
	connect_notebook_signals (mnb, notebook);
591

592
	g_signal_emit (G_OBJECT (mnb), signals[NOTEBOOK_ADDED], 0, notebook);
593 594 595
}

static void
596
remove_notebook (GeditMultiNotebook *mnb,
597 598 599 600 601 602 603 604
		 GtkWidget          *notebook)
{
	GtkWidget *parent;
	GtkWidget *grandpa;
	GList *children;
	GtkWidget *new_notebook;
	GList *current;

605
	if (mnb->priv->notebooks->next == NULL)
606 607 608 609 610
	{
		g_warning ("You are trying to remove the main notebook");
		return;
	}

611
	current = g_list_find (mnb->priv->notebooks,
612 613 614
			       notebook);

	if (current->next != NULL)
615
	{
616
		new_notebook = GTK_WIDGET (current->next->data);
617
	}
618
	else
619
	{
620
		new_notebook = GTK_WIDGET (mnb->priv->notebooks->data);
621 622
	}

623 624 625 626 627 628
	parent = gtk_widget_get_parent (notebook);

	/* Now we destroy the widget, we get the children of parent and we destroy
	  parent too as the parent is an useless paned. Finally we add the child
	  into the grand parent */
	g_object_ref (notebook);
629
	mnb->priv->removing_notebook = TRUE;
630 631

	gtk_widget_destroy (notebook);
632

633 634
	mnb->priv->notebooks = g_list_remove (mnb->priv->notebooks,
					      notebook);
635

636
	mnb->priv->removing_notebook = FALSE;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

	children = gtk_container_get_children (GTK_CONTAINER (parent));
	if (children->next != NULL)
	{
		g_warning ("The parent is not a paned");
		return;
	}
	grandpa = gtk_widget_get_parent (parent);

	g_object_ref (children->data);
	gtk_container_remove (GTK_CONTAINER (parent),
			      GTK_WIDGET (children->data));
	gtk_widget_destroy (parent);
	gtk_container_add (GTK_CONTAINER (grandpa),
			   GTK_WIDGET (children->data));
	g_object_unref (children->data);
653
	g_list_free (children);
654

655
	disconnect_notebook_signals (mnb, notebook);
656

657
	g_signal_emit (G_OBJECT (mnb), signals[NOTEBOOK_REMOVED], 0, notebook);
658
	g_object_unref (notebook);
659 660 661

	/* Let's make the active notebook grab the focus */
	gtk_widget_grab_focus (new_notebook);
662 663 664
}

static void
665
gedit_multi_notebook_init (GeditMultiNotebook *mnb)
666
{
667 668
	GeditMultiNotebookPrivate *priv;

669
	mnb->priv = gedit_multi_notebook_get_instance_private (mnb);
670
	priv = mnb->priv;
671

672
	priv->removing_notebook = FALSE;
673

674 675 676
	gtk_orientable_set_orientation (GTK_ORIENTABLE (mnb),
	                                GTK_ORIENTATION_VERTICAL);

677 678 679 680 681 682 683 684 685 686 687 688
	priv->show_tabs_mode = GEDIT_NOTEBOOK_SHOW_TABS_ALWAYS;
	priv->show_tabs = TRUE;

	priv->ui_settings = g_settings_new ("org.gnome.gedit.preferences.ui");
	g_settings_bind (priv->ui_settings,
			 GEDIT_SETTINGS_SHOW_TABS_MODE,
			 mnb,
			 "show-tabs-mode",
			 G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET);

	priv->active_notebook = gedit_notebook_new ();
	add_notebook (mnb, priv->active_notebook, TRUE);
689 690 691 692 693 694 695 696 697
}

GeditMultiNotebook *
gedit_multi_notebook_new ()
{
	return g_object_new (GEDIT_TYPE_MULTI_NOTEBOOK, NULL);
}

GeditNotebook *
698
gedit_multi_notebook_get_active_notebook (GeditMultiNotebook *mnb)
699
{
700
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), NULL);
701

702
	return GEDIT_NOTEBOOK (mnb->priv->active_notebook);
703 704 705
}

gint
706
gedit_multi_notebook_get_n_notebooks (GeditMultiNotebook *mnb)
707
{
708
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), 0);
709

710
	return g_list_length (mnb->priv->notebooks);
711 712 713
}

GeditNotebook *
714
gedit_multi_notebook_get_nth_notebook (GeditMultiNotebook *mnb,
715 716
				       gint                notebook_num)
{
717
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), NULL);
718

719
	return g_list_nth_data (mnb->priv->notebooks, notebook_num);
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
GeditNotebook *
gedit_multi_notebook_get_notebook_for_tab (GeditMultiNotebook *mnb,
                                           GeditTab           *tab)
{
	GList *l;
	gint page_num;

	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), NULL);
	g_return_val_if_fail (GEDIT_IS_TAB (tab), NULL);

	l = mnb->priv->notebooks;

	do
	{
		page_num = gtk_notebook_page_num (GTK_NOTEBOOK (l->data),
		                                  GTK_WIDGET (tab));
		if (page_num != -1)
			break;

		l = g_list_next (l);
	} while (l != NULL && page_num == -1);

	g_return_val_if_fail (page_num != -1, NULL);

	return GEDIT_NOTEBOOK (l->data);
}

749 750 751 752 753 754 755 756 757 758
gint
gedit_multi_notebook_get_notebook_num (GeditMultiNotebook *mnb,
				       GeditNotebook      *notebook)
{
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), -1);
	g_return_val_if_fail (GEDIT_IS_NOTEBOOK (notebook), -1);

	return g_list_index (mnb->priv->notebooks, notebook);
}

759
gint
760
gedit_multi_notebook_get_n_tabs (GeditMultiNotebook *mnb)
761
{
762
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), 0);
763

764
	return mnb->priv->total_tabs;
765 766 767
}

gint
768
gedit_multi_notebook_get_page_num (GeditMultiNotebook *mnb,
769 770 771 772 773
				   GeditTab           *tab)
{
	GList *l;
	gint real_page_num = 0;

774
	for (l = mnb->priv->notebooks; l != NULL; l = g_list_next (l))
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	{
		gint page_num;

		page_num = gtk_notebook_page_num (GTK_NOTEBOOK (l->data),
						  GTK_WIDGET (tab));

		if (page_num != -1)
		{
			real_page_num += page_num;
			break;
		}

		real_page_num += gtk_notebook_get_n_pages (GTK_NOTEBOOK (l->data));
	}

	return real_page_num;
}

GeditTab *
794
gedit_multi_notebook_get_active_tab (GeditMultiNotebook *mnb)
795
{
796
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), NULL);
797

798
	return (mnb->priv->active_tab == NULL) ?
799
				NULL : GEDIT_TAB (mnb->priv->active_tab);
800 801 802
}

void
803
gedit_multi_notebook_set_active_tab (GeditMultiNotebook *mnb,
804 805 806 807
				     GeditTab           *tab)
{
	GList *l;
	gint page_num;
808

809
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
810 811
	g_return_if_fail (GEDIT_IS_TAB (tab) || tab == NULL);

812 813
	/* use plain C cast since the active tab can be null */
	if (tab == (GeditTab *) mnb->priv->active_tab)
814 815 816
	{
		return;
	}
817

818 819 820
	if (tab == NULL)
	{
		set_active_tab (mnb, NULL);
821
		return;
822
	}
823

824
	l = mnb->priv->notebooks;
825 826 827 828

	do
	{
		page_num = gtk_notebook_page_num (GTK_NOTEBOOK (l->data),
829
		                                  GTK_WIDGET (tab));
830 831 832 833 834 835 836 837
		if (page_num != -1)
			break;

		l = g_list_next (l);
	} while (l != NULL && page_num == -1);

	g_return_if_fail (page_num != -1);

838
	gtk_notebook_set_current_page (GTK_NOTEBOOK (l->data), page_num);
839

840
	if (GTK_WIDGET (l->data) != mnb->priv->active_notebook)
841 842 843 844 845 846
	{
		gtk_widget_grab_focus (GTK_WIDGET (l->data));
	}
}

void
847
gedit_multi_notebook_set_current_page (GeditMultiNotebook *mnb,
848 849 850 851 852 853
				       gint                page_num)
{
	GList *l;
	gint pages = 0;
	gint single_num = page_num;

854
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
855

856
	for (l = mnb->priv->notebooks; l != NULL; l = g_list_next (l))
857 858
	{
		gint p;
859

860 861
		p = gtk_notebook_get_n_pages (GTK_NOTEBOOK (l->data));
		pages += p;
862

863 864 865 866 867 868
		if ((pages - 1) >= page_num)
			break;

		single_num -= p;
	}

869 870 871
	if (l == NULL)
		return;

872
	if (GTK_WIDGET (l->data) != mnb->priv->active_notebook)
873 874 875 876 877 878 879 880
	{
		gtk_widget_grab_focus (GTK_WIDGET (l->data));
	}

	gtk_notebook_set_current_page (GTK_NOTEBOOK (l->data), single_num);
}

GList *
881
gedit_multi_notebook_get_all_tabs (GeditMultiNotebook *mnb)
882 883 884 885
{
	GList *nbs;
	GList *ret = NULL;

886
	g_return_val_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb), NULL);
887

888
	for (nbs = mnb->priv->notebooks; nbs != NULL; nbs = g_list_next (nbs))
889 890 891 892 893 894 895 896 897
	{
		GList *l, *children;

		children = gtk_container_get_children (GTK_CONTAINER (nbs->data));

		for (l = children; l != NULL; l = g_list_next (l))
		{
			ret = g_list_prepend (ret, l->data);
		}
Pavel Vasin's avatar
Pavel Vasin committed
898 899

		g_list_free (children);
900 901 902 903 904 905 906 907
	}

	ret = g_list_reverse (ret);

	return ret;
}

void
908
gedit_multi_notebook_close_tabs (GeditMultiNotebook *mnb,
909 910 911 912
				 const GList        *tabs)
{
	GList *l;

913
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
914 915 916 917 918

	for (l = (GList *)tabs; l != NULL; l = g_list_next (l))
	{
		GList *nbs;

919
		for (nbs = mnb->priv->notebooks; nbs != NULL; nbs = g_list_next (nbs))
920 921 922 923
		{
			gint n;

			n = gtk_notebook_page_num (GTK_NOTEBOOK (nbs->data),
924
			                           GTK_WIDGET (l->data));
925 926

			if (n != -1)
927
			{
928 929
				gtk_container_remove (GTK_CONTAINER (nbs->data),
				                      GTK_WIDGET (l->data));
930
				break;
931
			}
932 933 934 935 936 937
		}
	}
}

/**
 * gedit_multi_notebook_close_all_tabs:
938
 * @mnb: a #GeditMultiNotebook
939 940 941 942
 *
 * Closes all opened tabs.
 */
void
943
gedit_multi_notebook_close_all_tabs (GeditMultiNotebook *mnb)
944 945 946
{
	GList *nbs, *l;

947
	g_return_if_fail (GEDIT_MULTI_NOTEBOOK (mnb));
948 949 950

	/* We copy the list because the main one is going to have the items
	   removed */
951
	nbs = g_list_copy (mnb->priv->notebooks);
952 953 954 955 956 957 958 959 960 961

	for (l = nbs; l != NULL; l = g_list_next (l))
	{
		gedit_notebook_remove_all_tabs (GEDIT_NOTEBOOK (l->data));
	}

	g_list_free (nbs);
}

void
962
gedit_multi_notebook_add_new_notebook (GeditMultiNotebook *mnb)
963 964 965 966
{
	GtkWidget *notebook;
	GeditTab *tab;

967
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
968 969

	notebook = gedit_notebook_new ();
970
	add_notebook (mnb, notebook, FALSE);
971

972
	tab = _gedit_tab_new ();
973 974 975 976 977 978
	gtk_widget_show (GTK_WIDGET (tab));

	/* When gtk_notebook_insert_page is called the focus is set in
	   the notebook, we don't want this to happen until the page is added.
	   Also we don't want to call switch_page when we add the tab
	   but when we switch the notebook. */
979 980
	g_signal_handlers_block_by_func (notebook, notebook_set_focus, mnb);
	g_signal_handlers_block_by_func (notebook, notebook_switch_page, mnb);
981 982 983 984 985 986

	gedit_notebook_add_tab (GEDIT_NOTEBOOK (notebook),
				tab,
				-1,
				TRUE);

987 988
	g_signal_handlers_unblock_by_func (notebook, notebook_switch_page, mnb);
	g_signal_handlers_unblock_by_func (notebook, notebook_set_focus, mnb);
989

990
	notebook_set_focus (GTK_CONTAINER (notebook), NULL, mnb);
991 992
}

993 994 995 996 997
void
gedit_multi_notebook_add_new_notebook_with_tab (GeditMultiNotebook *mnb,
                                                GeditTab           *tab)
{
	GtkWidget *notebook;
998
	GeditNotebook *old_notebook;
999 1000 1001 1002 1003 1004 1005

	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
	g_return_if_fail (GEDIT_IS_TAB (tab));

	notebook = gedit_notebook_new ();
	add_notebook (mnb, notebook, FALSE);

1006
	old_notebook = gedit_multi_notebook_get_notebook_for_tab (mnb, tab);
1007

1008 1009 1010 1011
	/* When gtk_notebook_insert_page is called the focus is set in
	   the notebook, we don't want this to happen until the page is added.
	   Also we don't want to call switch_page when we add the tab
	   but when we switch the notebook. */
1012 1013
	g_signal_handlers_block_by_func (old_notebook, notebook_set_focus, mnb);
	g_signal_handlers_block_by_func (old_notebook, notebook_switch_page, mnb);
1014

1015
	gedit_notebook_move_tab (old_notebook,
1016 1017 1018
	                         GEDIT_NOTEBOOK (notebook),
	                         tab,
	                         -1);
1019

1020 1021
	g_signal_handlers_unblock_by_func (old_notebook, notebook_switch_page, mnb);
	g_signal_handlers_unblock_by_func (old_notebook, notebook_set_focus, mnb);
1022 1023

	notebook_set_focus (GTK_CONTAINER (notebook), NULL, mnb);
1024 1025
}

1026
void
1027
gedit_multi_notebook_remove_active_notebook (GeditMultiNotebook *mnb)
1028
{
1029
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
1030

1031
	gedit_notebook_remove_all_tabs (GEDIT_NOTEBOOK (mnb->priv->active_notebook));
1032 1033 1034
}

void
1035
gedit_multi_notebook_previous_notebook (GeditMultiNotebook *mnb)
1036 1037 1038 1039
{
	GList *current;
	GtkWidget *notebook;

1040
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
1041

1042 1043
	current = g_list_find (mnb->priv->notebooks,
			       mnb->priv->active_notebook);
1044 1045 1046 1047

	if (current->prev != NULL)
		notebook = GTK_WIDGET (current->prev->data);
	else
1048
		notebook = GTK_WIDGET (g_list_last (mnb->priv->notebooks)->data);
1049 1050 1051 1052 1053

	gtk_widget_grab_focus (notebook);
}

void
1054
gedit_multi_notebook_next_notebook (GeditMultiNotebook *mnb)
1055 1056 1057 1058
{
	GList *current;
	GtkWidget *notebook;

1059
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));
1060

1061 1062
	current = g_list_find (mnb->priv->notebooks,
			       mnb->priv->active_notebook);
1063 1064 1065 1066

	if (current->next != NULL)
		notebook = GTK_WIDGET (current->next->data);
	else
1067
		notebook = GTK_WIDGET (mnb->priv->notebooks->data);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109

	gtk_widget_grab_focus (notebook);
}

void
gedit_multi_notebook_foreach_notebook (GeditMultiNotebook *mnb,
				       GtkCallback         callback,
				       gpointer            callback_data)
{
	GList *l;

	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));

	for (l = mnb->priv->notebooks; l != NULL; l = g_list_next (l))
	{
		callback (GTK_WIDGET (l->data), callback_data);
	}
}

void
gedit_multi_notebook_foreach_tab (GeditMultiNotebook *mnb,
				  GtkCallback         callback,
				  gpointer            callback_data)
{
	GList *nb;

	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));

	for (nb = mnb->priv->notebooks; nb != NULL; nb = g_list_next (nb))
	{
		GList *l, *children;

		children = gtk_container_get_children (GTK_CONTAINER (nb->data));

		for (l = children; l != NULL; l = g_list_next (l))
		{
			callback (GTK_WIDGET (l->data), callback_data);
		}

		g_list_free (children);
	}
}
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

/* We only use this to hide tabs in fullscreen mode so for now
 * we do not have a real property etc.
 */
void
_gedit_multi_notebook_set_show_tabs (GeditMultiNotebook *mnb,
				     gboolean           show)
{
	g_return_if_fail (GEDIT_IS_MULTI_NOTEBOOK (mnb));

	mnb->priv->show_tabs = show != FALSE;

	update_tabs_visibility (mnb);
}

/* ex:set ts=8 noet: */