gedit-notebook.c 17.3 KB
Newer Older
Paolo Borelli's avatar
Paolo Borelli committed
1 2 3 4
/*
 * gedit-notebook.c
 * This file is part of gedit
 *
5
 * Copyright (C) 2005 - Paolo Maggi
6
 * Copyright (C) 2015 - Sébastien Wilmet
Paolo Borelli's avatar
Paolo Borelli committed
7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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 Borelli's avatar
Paolo Borelli committed
20
 */
21

Paolo Borelli's avatar
Paolo Borelli committed
22 23 24 25 26 27 28 29
/* This file is a modified version of the epiphany file ephy-notebook.c
 * Here the relevant copyright:
 *
 *  Copyright (C) 2002 Christophe Fergeau
 *  Copyright (C) 2003 Marco Pesenti Gritti
 *  Copyright (C) 2003, 2004 Christian Persch
 */

30
#include "gedit-notebook.h"
31
#include "gedit-tab-label.h"
32

33 34
#define GEDIT_NOTEBOOK_GROUP_NAME "GeditNotebookGroup"

35 36 37 38 39
/* The DND targets defined in GeditView start at 100.
 * Those defined in GtkSourceView start at 200.
 */
#define TARGET_TAB 150

Paolo Borelli's avatar
Paolo Borelli committed
40 41
struct _GeditNotebookPrivate
{
42 43 44
	/* History of focused pages. The first element contains the most recent
	 * one.
	 */
45
	GList *focused_pages;
46

47
	guint ignore_focused_page_update : 1;
Paolo Borelli's avatar
Paolo Borelli committed
48 49
};

50
G_DEFINE_TYPE_WITH_PRIVATE (GeditNotebook, gedit_notebook, GTK_TYPE_NOTEBOOK)
Paolo Borelli's avatar
Paolo Borelli committed
51 52 53 54

enum
{
	TAB_CLOSE_REQUEST,
55
	SHOW_POPUP_MENU,
56
	CHANGE_TO_PAGE,
Paolo Borelli's avatar
Paolo Borelli committed
57 58 59 60 61
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

62 63 64 65 66 67 68 69 70 71
static void
gedit_notebook_finalize (GObject *object)
{
	GeditNotebook *notebook = GEDIT_NOTEBOOK (object);

	g_list_free (notebook->priv->focused_pages);

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

72 73 74
static void
gedit_notebook_grab_focus (GtkWidget *widget)
{
75
	GtkNotebook *notebook = GTK_NOTEBOOK (widget);
76
	gint current_page;
77
	GtkWidget *tab;
78

79 80
	current_page = gtk_notebook_get_current_page (notebook);
	tab = gtk_notebook_get_nth_page (notebook, current_page);
81

82
	if (tab != NULL)
83 84 85
	{
		gtk_widget_grab_focus (tab);
	}
86 87
}

88 89 90 91 92 93 94
static gint
find_tab_num_at_pos (GtkNotebook *notebook,
                     gint         screen_x,
                     gint         screen_y)
{
	GtkPositionType tab_pos;
	GtkAllocation tab_allocation;
95
	gint page_num;
96

97
	tab_pos = gtk_notebook_get_tab_pos (notebook);
98

99
	for (page_num = 0; ; page_num++)
100
	{
101 102
		GtkWidget *page;
		GtkWidget *tab_label;
103 104
		gint max_x, max_y, x_root, y_root;

105 106 107 108 109 110 111 112 113
		page = gtk_notebook_get_nth_page (notebook, page_num);

		if (page == NULL)
		{
			break;
		}

		tab_label = gtk_notebook_get_tab_label (notebook, page);
		g_return_val_if_fail (tab_label != NULL, -1);
114

115
		if (!gtk_widget_get_mapped (tab_label))
116 117 118 119
		{
			continue;
		}

120
		gdk_window_get_origin (gtk_widget_get_window (tab_label), &x_root, &y_root);
121

122
		gtk_widget_get_allocation (tab_label, &tab_allocation);
123 124 125
		max_x = x_root + tab_allocation.x + tab_allocation.width;
		max_y = y_root + tab_allocation.y + tab_allocation.height;

126 127
		if ((tab_pos == GTK_POS_TOP || tab_pos == GTK_POS_BOTTOM) &&
		    screen_x <= max_x)
128 129 130 131
		{
			return page_num;
		}

132 133
		if ((tab_pos == GTK_POS_LEFT || tab_pos == GTK_POS_RIGHT) &&
		    screen_y <= max_y)
134 135 136 137 138 139 140 141 142
		{
			return page_num;
		}
	}

	return -1;
}

static gboolean
143 144
gedit_notebook_button_press_event (GtkWidget      *widget,
				   GdkEventButton *event)
145
{
146
	GtkNotebook *notebook = GTK_NOTEBOOK (widget);
147

148 149
	if (event->type == GDK_BUTTON_PRESS &&
	    (event->state & gtk_accelerator_get_default_mod_mask ()) == 0)
150
	{
151
		gint tab_clicked;
152

153
		tab_clicked = find_tab_num_at_pos (notebook, event->x_root, event->y_root);
154 155
		if (tab_clicked >= 0)
		{
156
			GtkWidget *tab;
157

158
			tab = gtk_notebook_get_nth_page (notebook, tab_clicked);
159 160 161 162 163 164 165 166 167 168 169 170 171
			switch (event->button)
			{
				case GDK_BUTTON_SECONDARY:
					g_signal_emit (G_OBJECT (widget), signals[SHOW_POPUP_MENU], 0, event, tab);
					return GDK_EVENT_STOP;

				case GDK_BUTTON_MIDDLE:
					g_signal_emit (G_OBJECT (notebook), signals[TAB_CLOSE_REQUEST], 0, tab);
					return GDK_EVENT_STOP;

				default:
					break;
			}
172 173
		}
	}
174

175
	return GTK_WIDGET_CLASS (gedit_notebook_parent_class)->button_press_event (widget, event);
176 177
}

178 179 180 181 182 183 184
/*
 * We need to override this because when we don't show the tabs, like in
 * fullscreen we need to have wrap around too
 */
static gboolean
gedit_notebook_change_current_page (GtkNotebook *notebook,
				    gint         offset)
Paolo Borelli's avatar
Paolo Borelli committed
185
{
186
	gint current;
Paolo Borelli's avatar
Paolo Borelli committed
187

188
	current = gtk_notebook_get_current_page (notebook);
Paolo Borelli's avatar
Paolo Borelli committed
189

190
	if (current != -1)
191
	{
192 193 194 195
		gint target;
		gboolean wrap_around;

		target = current + offset;
196

197 198 199 200 201
		g_object_get (gtk_widget_get_settings (GTK_WIDGET (notebook)),
			      "gtk-keynav-wrap-around", &wrap_around,
			      NULL);

		if (wrap_around)
202
		{
203
			if (target < 0)
204
			{
205
				target = gtk_notebook_get_n_pages (notebook) - 1;
206
			}
207
			else if (target >= gtk_notebook_get_n_pages (notebook))
208
			{
209
				target = 0;
210
			}
211 212
		}

213
		gtk_notebook_set_current_page (notebook, target);
214
	}
215
	else
Paolo Borelli's avatar
Paolo Borelli committed
216
	{
217
		gtk_widget_error_bell (GTK_WIDGET (notebook));
Paolo Borelli's avatar
Paolo Borelli committed
218 219
	}

220
	return TRUE;
Paolo Borelli's avatar
Paolo Borelli committed
221 222
}

223 224 225 226
static void
gedit_notebook_switch_page (GtkNotebook *notebook,
                            GtkWidget   *page,
                            guint        page_num)
Paolo Borelli's avatar
Paolo Borelli committed
227
{
228
	GeditNotebookPrivate *priv = GEDIT_NOTEBOOK (notebook)->priv;
Paolo Borelli's avatar
Paolo Borelli committed
229

230 231
	GTK_NOTEBOOK_CLASS (gedit_notebook_parent_class)->switch_page (notebook, page, page_num);

232
	if (!priv->ignore_focused_page_update)
Paolo Borelli's avatar
Paolo Borelli committed
233
	{
234
		gint page_num;
235

236 237
		/* Get again page_num and page, the signal handler may have
		 * changed them.
238
		 */
239 240 241 242 243
		page_num = gtk_notebook_get_current_page (notebook);
		if (page_num != -1)
		{
			GtkWidget *page = gtk_notebook_get_nth_page (notebook, page_num);
			g_assert (page != NULL);
Paolo Borelli's avatar
Paolo Borelli committed
244

245 246 247 248
			/* Remove the old page, we dont want to grow unnecessarily
			 * the list.
			 */
			priv->focused_pages = g_list_remove (priv->focused_pages, page);
Paolo Borelli's avatar
Paolo Borelli committed
249

250 251 252
			priv->focused_pages = g_list_prepend (priv->focused_pages, page);
		}
	}
253

254 255
	/* give focus to the tab */
	gtk_widget_grab_focus (page);
Paolo Borelli's avatar
Paolo Borelli committed
256 257
}

258
static void
259 260
close_button_clicked_cb (GeditTabLabel *tab_label,
			 GeditNotebook *notebook)
Paolo Borelli's avatar
Paolo Borelli committed
261
{
262
	GeditTab *tab;
Paolo Borelli's avatar
Paolo Borelli committed
263

264 265
	tab = gedit_tab_label_get_tab (tab_label);
	g_signal_emit (notebook, signals[TAB_CLOSE_REQUEST], 0, tab);
Paolo Borelli's avatar
Paolo Borelli committed
266 267
}

268
static void
269 270
switch_to_last_focused_page (GeditNotebook *notebook,
			     GeditTab      *tab)
Paolo Borelli's avatar
Paolo Borelli committed
271
{
272
	if (notebook->priv->focused_pages != NULL)
Paolo Borelli's avatar
Paolo Borelli committed
273
	{
274
		GList *node;
275
		GtkWidget *page;
276
		gint page_num;
277

278 279
		node = notebook->priv->focused_pages;
		page = GTK_WIDGET (node->data);
280

281 282
		page_num = gtk_notebook_page_num (GTK_NOTEBOOK (notebook), page);
		g_return_if_fail (page_num != -1);
283

284
		gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page_num);
Paolo Borelli's avatar
Paolo Borelli committed
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
static void
drag_data_received_cb (GtkWidget        *widget,
		       GdkDragContext   *context,
		       gint              x,
		       gint              y,
		       GtkSelectionData *selection_data,
		       guint             info,
		       guint             timestamp)
{
	GtkWidget *notebook;
	GtkWidget *new_notebook;
	GtkWidget *page;

	if (info != TARGET_TAB)
	{
		return;
	}

	notebook = gtk_drag_get_source_widget (context);

	if (!GTK_IS_WIDGET (notebook))
	{
		return;
	}

	page = *(GtkWidget **) gtk_selection_data_get_data (selection_data);
	g_return_if_fail (page != NULL);

	/* We need to iterate and get the notebook of the target view
	 * because we can have several notebooks per window.
	 */
319 320
	new_notebook = gtk_widget_get_ancestor (widget, GEDIT_TYPE_NOTEBOOK);
	g_return_if_fail (new_notebook != NULL);
321 322 323 324 325 326 327 328 329 330 331 332

	if (notebook != new_notebook)
	{
		gedit_notebook_move_tab (GEDIT_NOTEBOOK (notebook),
					 GEDIT_NOTEBOOK (new_notebook),
					 GEDIT_TAB (page),
					 0);
	}

	gtk_drag_finish (context, TRUE, TRUE, timestamp);
}

Paolo Borelli's avatar
Paolo Borelli committed
333
static void
334 335 336
gedit_notebook_page_removed (GtkNotebook *notebook,
                             GtkWidget   *page,
                             guint        page_num)
Paolo Borelli's avatar
Paolo Borelli committed
337
{
338
	GeditNotebookPrivate *priv = GEDIT_NOTEBOOK (notebook)->priv;
339
	gboolean current_page;
Paolo Borelli's avatar
Paolo Borelli committed
340

341 342 343
	/* The page removed was the current page. */
	current_page = (priv->focused_pages != NULL &&
			priv->focused_pages->data == page);
Paolo Borelli's avatar
Paolo Borelli committed
344

345
	priv->focused_pages = g_list_remove (priv->focused_pages, page);
Paolo Borelli's avatar
Paolo Borelli committed
346

347
	if (current_page)
348
	{
349 350
		switch_to_last_focused_page (GEDIT_NOTEBOOK (notebook),
					     GEDIT_TAB (page));
Paolo Borelli's avatar
Paolo Borelli committed
351 352 353 354
	}
}

static void
355 356 357
gedit_notebook_page_added (GtkNotebook *notebook,
                           GtkWidget   *page,
                           guint        page_num)
Paolo Borelli's avatar
Paolo Borelli committed
358
{
359
	GtkWidget *tab_label;
360 361 362
	GeditView *view;

	g_return_if_fail (GEDIT_IS_TAB (page));
Paolo Borelli's avatar
Paolo Borelli committed
363

364 365
	tab_label = gtk_notebook_get_tab_label (notebook, page);
	g_return_if_fail (GEDIT_IS_TAB_LABEL (tab_label));
Paolo Borelli's avatar
Paolo Borelli committed
366

367
	/* For a DND from one notebook to another, the same tab_label can be
368
	 * used, so we need to connect the signal here.
369 370 371
	 * More precisely, the same tab_label is used when the drop zone is in
	 * the tab labels (not the GeditView), that is, when the DND is handled
	 * by the GtkNotebook implementation.
372
	 */
373 374 375
	g_signal_connect (tab_label,
	                  "close-clicked",
	                  G_CALLBACK (close_button_clicked_cb),
376
	                  notebook);
377 378 379 380 381 382

	view = gedit_tab_get_view (GEDIT_TAB (page));
	g_signal_connect (view,
			  "drag-data-received",
			  G_CALLBACK (drag_data_received_cb),
			  NULL);
Paolo Borelli's avatar
Paolo Borelli committed
383 384
}

385 386 387 388
static void
gedit_notebook_remove (GtkContainer *container,
                       GtkWidget    *widget)
{
389
	GtkNotebook *notebook = GTK_NOTEBOOK (container);
390
	GeditNotebookPrivate *priv = GEDIT_NOTEBOOK (container)->priv;
391
	GtkWidget *tab_label;
392
	GeditView *view;
393 394 395 396 397 398

	g_return_if_fail (GEDIT_IS_TAB (widget));

	tab_label = gtk_notebook_get_tab_label (notebook, widget);
	g_return_if_fail (GEDIT_IS_TAB_LABEL (tab_label));

399
	/* For a DND from one notebook to another, the same tab_label can be
400 401 402 403 404
	 * used, so we need to disconnect the signal.
	 */
	g_signal_handlers_disconnect_by_func (tab_label,
					      G_CALLBACK (close_button_clicked_cb),
					      notebook);
405

406 407 408
	view = gedit_tab_get_view (GEDIT_TAB (widget));
	g_signal_handlers_disconnect_by_func (view, drag_data_received_cb, NULL);

409
	/* This is where GtkNotebook will remove the page. By doing so, it
410 411 412 413
	 * will also switch to a new page, messing up our focus list. So we
	 * set a flag here to ignore the switch temporarily.
	 */
	priv->ignore_focused_page_update = TRUE;
414

415 416 417 418 419
	if (GTK_CONTAINER_CLASS (gedit_notebook_parent_class)->remove != NULL)
	{
		GTK_CONTAINER_CLASS (gedit_notebook_parent_class)->remove (container,
									   widget);
	}
420

421
	priv->ignore_focused_page_update = FALSE;
422 423
}

424 425 426 427 428 429 430 431
static gboolean
gedit_notebook_change_to_page (GeditNotebook *notebook,
                               gint           page_num)
{
	gint n_pages;

	n_pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));

432
	if (page_num >= n_pages)
433 434 435 436 437 438 439 440 441 442
	{
		return FALSE;
	}

	gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook),
	                               page_num);

	return TRUE;
}

443 444
static void
gedit_notebook_class_init (GeditNotebookClass *klass)
Paolo Borelli's avatar
Paolo Borelli committed
445
{
446
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
447
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
448
	GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
449
	GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
450 451
	GtkBindingSet *binding_set;
	gint i;
Paolo Borelli's avatar
Paolo Borelli committed
452

453
	object_class->finalize = gedit_notebook_finalize;
Paolo Borelli's avatar
Paolo Borelli committed
454

455 456 457 458
	widget_class->grab_focus = gedit_notebook_grab_focus;
	widget_class->button_press_event = gedit_notebook_button_press_event;

	container_class->remove = gedit_notebook_remove;
Paolo Borelli's avatar
Paolo Borelli committed
459

460 461 462 463
	notebook_class->change_current_page = gedit_notebook_change_current_page;
	notebook_class->switch_page = gedit_notebook_switch_page;
	notebook_class->page_removed = gedit_notebook_page_removed;
	notebook_class->page_added = gedit_notebook_page_added;
Paolo Borelli's avatar
Paolo Borelli committed
464

465 466
	klass->change_to_page = gedit_notebook_change_to_page;

467 468 469 470 471
	signals[TAB_CLOSE_REQUEST] =
		g_signal_new ("tab-close-request",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GeditNotebookClass, tab_close_request),
Paolo Borelli's avatar
Paolo Borelli committed
472
			      NULL, NULL, NULL,
473 474 475
			      G_TYPE_NONE,
			      1,
			      GEDIT_TYPE_TAB);
Paolo Borelli's avatar
Paolo Borelli committed
476

477 478 479 480 481
	signals[SHOW_POPUP_MENU] =
		g_signal_new ("show-popup-menu",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (GeditNotebookClass, show_popup_menu),
Paolo Borelli's avatar
Paolo Borelli committed
482
			      NULL, NULL, NULL,
483
			      G_TYPE_NONE,
484 485 486
			      2,
			      GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE,
			      GEDIT_TYPE_TAB);
487

488 489 490 491 492
	signals[CHANGE_TO_PAGE] =
		g_signal_new ("change-to-page",
		              G_TYPE_FROM_CLASS (object_class),
		              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		              G_STRUCT_OFFSET (GeditNotebookClass, change_to_page),
Paolo Borelli's avatar
Paolo Borelli committed
493
		              NULL, NULL, NULL,
494 495 496 497 498 499 500 501 502 503 504
		              G_TYPE_BOOLEAN, 1,
		              G_TYPE_INT);

	binding_set = gtk_binding_set_by_class (klass);
	for (i = 1; i < 10; i++)
	{
		gtk_binding_entry_add_signal (binding_set,
		                              GDK_KEY_0 + i, GDK_MOD1_MASK,
		                              "change-to-page", 1,
		                              G_TYPE_INT, i - 1);
	}
Paolo Borelli's avatar
Paolo Borelli committed
505 506
}

507 508 509 510 511 512 513
/**
 * gedit_notebook_new:
 *
 * Creates a new #GeditNotebook object.
 *
 * Returns: a new #GeditNotebook
 */
Paolo Borelli's avatar
Paolo Borelli committed
514 515 516 517 518 519 520 521 522
GtkWidget *
gedit_notebook_new (void)
{
	return GTK_WIDGET (g_object_new (GEDIT_TYPE_NOTEBOOK, NULL));
}

static void
gedit_notebook_init (GeditNotebook *notebook)
{
523
	notebook->priv = gedit_notebook_get_instance_private (notebook);
524

Paolo Borelli's avatar
Paolo Borelli committed
525 526
	gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
	gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
527
	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
528 529
	gtk_notebook_set_group_name (GTK_NOTEBOOK (notebook),
	                             GEDIT_NOTEBOOK_GROUP_NAME);
530
	gtk_container_set_border_width (GTK_CONTAINER (notebook), 0);
Paolo Borelli's avatar
Paolo Borelli committed
531 532
}

533 534
/**
 * gedit_notebook_add_tab:
535
 * @notebook: a #GeditNotebook
536 537 538 539
 * @tab: a #GeditTab
 * @position: the position where the @tab should be added
 * @jump_to: %TRUE to set the @tab as active
 *
540
 * Adds the specified @tab to the @notebook.
541
 */
Paolo Borelli's avatar
Paolo Borelli committed
542
void
543
gedit_notebook_add_tab (GeditNotebook *notebook,
Paolo Borelli's avatar
Paolo Borelli committed
544 545 546 547
		        GeditTab      *tab,
		        gint           position,
		        gboolean       jump_to)
{
548
	GtkWidget *tab_label;
549 550
	GeditView *view;
	GtkTargetList *target_list;
Paolo Borelli's avatar
Paolo Borelli committed
551

552
	g_return_if_fail (GEDIT_IS_NOTEBOOK (notebook));
Paolo Borelli's avatar
Paolo Borelli committed
553 554
	g_return_if_fail (GEDIT_IS_TAB (tab));

555
	tab_label = gedit_tab_label_new (tab);
556

557
	gtk_notebook_insert_page (GTK_NOTEBOOK (notebook),
Paolo Borelli's avatar
Paolo Borelli committed
558
				  GTK_WIDGET (tab),
559
				  tab_label,
Paolo Borelli's avatar
Paolo Borelli committed
560
				  position);
561 562

	gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (notebook),
563 564
	                                  GTK_WIDGET (tab),
	                                  TRUE);
565 566

	gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (notebook),
567 568
	                                 GTK_WIDGET (tab),
	                                 TRUE);
569 570

	gtk_container_child_set (GTK_CONTAINER (notebook),
William Jon McCann's avatar
William Jon McCann committed
571 572 573
				 GTK_WIDGET (tab),
				 "tab-expand", TRUE,
				 NULL);
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
	/* Drag and drop support: move a tab to another notebook, with the drop
	 * zone in the GeditView. The drop zone in the tab labels is already
	 * implemented by GtkNotebook.
	 */
	view = gedit_tab_get_view (tab);
	target_list = gtk_drag_dest_get_target_list (GTK_WIDGET (view));

	if (target_list != NULL)
	{
		gtk_target_list_add (target_list,
		                     gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"),
		                     GTK_TARGET_SAME_APP,
		                     TARGET_TAB);
	}

Paolo Borelli's avatar
Paolo Borelli committed
590
	/* The signal handler may have reordered the tabs */
591
	position = gtk_notebook_page_num (GTK_NOTEBOOK (notebook),
Paolo Borelli's avatar
Paolo Borelli committed
592 593 594 595
					  GTK_WIDGET (tab));

	if (jump_to)
	{
596
		gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), position);
597
		gtk_widget_grab_focus (GTK_WIDGET (tab));
Paolo Borelli's avatar
Paolo Borelli committed
598 599 600
	}
}

601 602 603 604 605 606 607 608
/**
 * gedit_notebook_move_tab:
 * @src: a #GeditNotebook
 * @dest: a #GeditNotebook
 * @tab: a #GeditTab
 * @dest_position: the position for @tab
 *
 * Moves @tab from @src to @dest.
609
 * If @dest_position is greater than or equal to the number of tabs
610 611 612 613 614 615 616 617 618 619 620 621 622 623
 * of the destination nootebook or negative, tab will be moved to the
 * end of the tabs.
 */
void
gedit_notebook_move_tab (GeditNotebook *src,
                         GeditNotebook *dest,
                         GeditTab      *tab,
                         gint           dest_position)
{
	g_return_if_fail (GEDIT_IS_NOTEBOOK (src));
	g_return_if_fail (GEDIT_IS_NOTEBOOK (dest));
	g_return_if_fail (src != dest);
	g_return_if_fail (GEDIT_IS_TAB (tab));

624
	/* Make sure the tab isn't destroyed while we move it. */
625
	g_object_ref (tab);
626 627 628 629 630 631 632 633

	/* Make sure the @src notebook isn't destroyed during the tab
	 * detachment, to prevent a crash in gtk_notebook_detach_tab(). In fact,
	 * if @tab is the last tab of @src, and if @src is not the last notebook
	 * of the GeditMultiNotebook, then @src will be destroyed when
	 * gtk_container_remove() is called by gtk_notebook_detach_tab().
	 */
	g_object_ref (src);
634
	gtk_notebook_detach_tab (GTK_NOTEBOOK (src), GTK_WIDGET (tab));
635 636
	g_object_unref (src);

637
	gedit_notebook_add_tab (dest, tab, dest_position, TRUE);
638

639
	g_object_unref (tab);
Paolo Borelli's avatar
Paolo Borelli committed
640 641
}

642 643
/**
 * gedit_notebook_remove_all_tabs:
644
 * @notebook: a #GeditNotebook
645
 *
646
 * Removes all #GeditTab from @notebook.
647
 */
Paolo Borelli's avatar
Paolo Borelli committed
648
void
649
gedit_notebook_remove_all_tabs (GeditNotebook *notebook)
650
{
651 652
	GList *tabs;
	GList *t;
653

654
	g_return_if_fail (GEDIT_IS_NOTEBOOK (notebook));
655

656 657
	g_list_free (notebook->priv->focused_pages);
	notebook->priv->focused_pages = NULL;
Paolo Borelli's avatar
Paolo Borelli committed
658

659
	/* Remove tabs in reverse order since it is faster
660 661 662
	 * due to how GtkNotebook works.
	 */
	tabs = gtk_container_get_children (GTK_CONTAINER (notebook));
663 664
	for (t = g_list_last (tabs); t != NULL; t = t->prev)
	{
665 666
		GtkWidget *tab = t->data;
		gtk_container_remove (GTK_CONTAINER (notebook), tab);
667 668 669
	}

	g_list_free (tabs);
Paolo Borelli's avatar
Paolo Borelli committed
670 671
}

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