gedit-documents-panel.c 49 KB
Newer Older
Paolo Borelli's avatar
Paolo Borelli committed
1 2 3 4
/*
 * gedit-documents-panel.c
 * This file is part of gedit
 *
5
 * Copyright (C) 2014 - Sébastien Lafargue <slafargue@gnome.org>
Paolo Borelli's avatar
Paolo Borelli committed
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paolo Borelli's avatar
Paolo Borelli committed
19
 */
20

Paolo Borelli's avatar
Paolo Borelli committed
21 22 23 24
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

25 26
#include "gedit-documents-panel.h"

27 28
#include <glib/gi18n.h>

29
#include "gedit-debug.h"
30
#include "gedit-document.h"
31
#include "gedit-multi-notebook.h"
32
#include "gedit-notebook.h"
33
#include "gedit-notebook-popup-menu.h"
34 35
#include "gedit-tab.h"
#include "gedit-tab-private.h"
36
#include "gedit-utils.h"
37
#include "gedit-commands-private.h"
Paolo Borelli's avatar
Paolo Borelli committed
38

39 40 41 42 43 44
typedef struct _GeditDocumentsGenericRow GeditDocumentsGenericRow;
typedef struct _GeditDocumentsGenericRow GeditDocumentsGroupRow;
typedef struct _GeditDocumentsGenericRow GeditDocumentsDocumentRow;

struct _GeditDocumentsGenericRow
{
45
	GtkListBoxRow        parent_instance;
46 47 48 49

	GeditDocumentsPanel *panel;
	GtkWidget           *ref;

50
	GtkWidget           *box;
51
	GtkWidget           *label;
52
	GtkWidget           *close_button;
53 54 55

	/* Not used in GeditDocumentsGroupRow */
	GtkWidget           *image;
56
	GtkWidget           *status_label;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
};

#define GEDIT_TYPE_DOCUMENTS_GROUP_ROW            (gedit_documents_group_row_get_type ())
#define GEDIT_DOCUMENTS_GROUP_ROW(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_DOCUMENTS_GROUP_ROW, GeditDocumentsGroupRow))
#define GEDIT_DOCUMENTS_GROUP_ROW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GEDIT_TYPE_DOCUMENTS_GROUP_ROW, GeditDocumentsGroupRowClass))
#define GEDIT_IS_DOCUMENTS_GROUP_ROW(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_DOCUMENTS_GROUP_ROW))
#define GEDIT_IS_DOCUMENTS_GROUP_ROW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GEDIT_TYPE_DOCUMENTS_GROUP_ROW))
#define GEDIT_DOCUMENTS_GROUP_ROW_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GEDIT_TYPE_DOCUMENTS_GROUP_ROW, GeditDocumentsGroupRowClass))

typedef struct _GeditDocumentsGroupRowClass GeditDocumentsGroupRowClass;

struct _GeditDocumentsGroupRowClass
{
	GtkListBoxRowClass parent_class;
};

GType gedit_documents_group_row_get_type (void) G_GNUC_CONST;

G_DEFINE_TYPE (GeditDocumentsGroupRow, gedit_documents_group_row, GTK_TYPE_LIST_BOX_ROW)

#define GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW            (gedit_documents_document_row_get_type ())
#define GEDIT_DOCUMENTS_DOCUMENT_ROW(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW, GeditDocumentsDocumentRow))
#define GEDIT_DOCUMENTS_DOCUMENT_ROW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW, GeditDocumentsDocumentRowClass))
#define GEDIT_IS_DOCUMENTS_DOCUMENT_ROW(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW))
#define GEDIT_IS_DOCUMENTS_DOCUMENT_ROW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW))
#define GEDIT_DOCUMENTS_DOCUMENT_ROW_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GEDIT_TYPE_DOCUMENTS_DOCUMENT_ROW, GeditDocumentsDocumentRowClass))

typedef struct _GeditDocumentsDocumentRowClass GeditDocumentsDocumentRowClass;

struct _GeditDocumentsDocumentRowClass
{
	GtkListBoxRowClass parent_class;
};

GType gedit_documents_document_row_get_type (void) G_GNUC_CONST;

G_DEFINE_TYPE (GeditDocumentsDocumentRow, gedit_documents_document_row, GTK_TYPE_LIST_BOX_ROW)

95 96 97 98
static GtkWidget *gedit_documents_document_row_new (GeditDocumentsPanel *panel,
                                                    GeditTab            *tab);
static GtkWidget *gedit_documents_group_row_new    (GeditDocumentsPanel *panel,
                                                    GeditNotebook       *notebook);
99

100
struct _GeditDocumentsPanel
Paolo Borelli's avatar
Paolo Borelli committed
101
{
102 103
	GtkBox              parent_instance;

104 105
	GeditWindow        *window;
	GeditMultiNotebook *mnb;
106
	GtkWidget          *listbox;
107

108
	guint               selection_changed_handler_id;
109 110 111 112 113
	guint               tab_switched_handler_id;
	gboolean            is_in_tab_switched;

	/* Flag to workaround first GroupRow selection at start ( we don't want to show it ) */
	gboolean            first_selection;
114
	GtkWidget          *current_selection;
115

116 117 118 119
	GtkAdjustment      *adjustment;

	guint               nb_row_notebook;
	guint               nb_row_tab;
120 121 122 123 124 125 126 127 128 129 130 131 132 133

	GtkTargetList      *source_targets;
	GtkWidget          *dnd_window;
	GtkWidget          *row_placeholder;
	guint               row_placeholder_index;
	guint               row_destination_index;
	GtkWidget          *drag_document_row;
	gint                row_source_row_offset;
	gint                document_row_height;
	gint                drag_document_row_x;
	gint                drag_document_row_y;
	gint                drag_root_x;
	gint                drag_root_y;
	gboolean            is_on_drag;
Paolo Borelli's avatar
Paolo Borelli committed
134 135 136 137 138
};

enum
{
	PROP_0,
139 140
	PROP_WINDOW,
	LAST_PROP
141 142
};

143 144 145 146
static GParamSpec *properties[LAST_PROP];

G_DEFINE_TYPE (GeditDocumentsPanel, gedit_documents_panel, GTK_TYPE_BOX)

147 148 149 150
static const GtkTargetEntry panel_targets [] = {
	{"GEDIT_DOCUMENTS_DOCUMENT_ROW", GTK_TARGET_SAME_APP, 0},
};

151
#define MAX_DOC_NAME_LENGTH 60
Paolo Borelli's avatar
Paolo Borelli committed
152

153 154
#define ROW_OUTSIDE_LISTBOX -1

155 156
static guint
get_nb_visible_rows (GeditDocumentsPanel *panel)
157
{
158
	guint nb = 0;
159

160
	if (panel->nb_row_notebook > 1)
161
	{
162
		nb += panel->nb_row_notebook;
163 164
	}

165
	nb += panel->nb_row_tab;
166

167
	return nb;
168
}
Paolo Borelli's avatar
Paolo Borelli committed
169

170 171 172
static guint
get_row_visible_index (GeditDocumentsPanel *panel,
                       GtkWidget           *searched_row)
Paolo Borelli's avatar
Paolo Borelli committed
173
{
174 175 176 177
	GList *children;
	GList *l;
	guint nb_notebook_row = 0;
	guint nb_tab_row = 0;
Paolo Borelli's avatar
Paolo Borelli committed
178

179
	children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
Paolo Borelli's avatar
Paolo Borelli committed
180

181
	for (l = children; l != NULL; l = g_list_next (l))
Paolo Borelli's avatar
Paolo Borelli committed
182
	{
183 184 185
		GtkWidget *row = l->data;

		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
Paolo Borelli's avatar
Paolo Borelli committed
186
		{
187
			nb_notebook_row += 1;
Paolo Borelli's avatar
Paolo Borelli committed
188 189 190
		}
		else
		{
191
			nb_tab_row += 1;
Paolo Borelli's avatar
Paolo Borelli committed
192
		}
193 194 195 196 197 198 199 200 201

		if (row == searched_row)
		{
			break;
		}
	}

	g_list_free (children);

202
	if (panel->nb_row_notebook == 1)
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	{
		nb_notebook_row = 0;
	}

	return nb_tab_row + nb_notebook_row - 1;
}

/* We do not grab focus on the row, so scroll it into view manually */
static void
make_row_visible (GeditDocumentsPanel *panel,
                  GtkWidget           *row)
{
	gdouble adjustment_value;
	gdouble adjustment_lower;
	gdouble adjustment_upper;
	gdouble adjustment_page_size;
	gdouble nb_visible_rows;
	gdouble row_visible_index;
	gdouble row_size;
	gdouble row_position;
	gdouble offset;
	gdouble new_adjustment_value;

226 227 228 229
	adjustment_value = gtk_adjustment_get_value (panel->adjustment);
	adjustment_lower = gtk_adjustment_get_lower (panel->adjustment);
	adjustment_upper = gtk_adjustment_get_upper (panel->adjustment);
	adjustment_page_size = gtk_adjustment_get_page_size (panel->adjustment);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

	nb_visible_rows = get_nb_visible_rows (panel);
	row_visible_index = get_row_visible_index (panel, GTK_WIDGET (row));

	row_size = (adjustment_upper - adjustment_lower) / nb_visible_rows;
	row_position =  row_size * row_visible_index;

	if (row_position < adjustment_value)
	{
		new_adjustment_value = row_position;
	}
	else if ((row_position + row_size) > (adjustment_value + adjustment_page_size))
	{
		offset = (row_position + row_size) - (adjustment_value + adjustment_page_size);
		new_adjustment_value = adjustment_value + offset;
Paolo Borelli's avatar
Paolo Borelli committed
245 246 247
	}
	else
	{
248
		new_adjustment_value = adjustment_value;
Paolo Borelli's avatar
Paolo Borelli committed
249 250
	}

251
	gtk_adjustment_set_value (panel->adjustment, new_adjustment_value);
252
}
Paolo Borelli's avatar
Paolo Borelli committed
253

254 255 256 257 258 259 260 261
/* This function is a GCompareFunc to use with g_list_find_custom */
static gint
listbox_search_function (gconstpointer row,
                         gconstpointer item)
{
	GeditDocumentsGenericRow *generic_row = (GeditDocumentsGenericRow *)row;
	gpointer *searched_item = (gpointer *)generic_row->ref;

262
	return searched_item == item ? 0 : -1;
Paolo Borelli's avatar
Paolo Borelli committed
263 264
}

265
static GtkListBoxRow *
266 267
get_row_from_widget (GeditDocumentsPanel *panel,
                     GtkWidget           *widget)
268
{
269 270 271
	GList *children;
	GList *item;
	GtkListBoxRow *row;
272

273
	children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
274

275 276
	item = g_list_find_custom (children, widget, listbox_search_function);
	row = item ? item->data : NULL;
277

278
	g_list_free (children);
279

280 281
	return row;
}
282

283 284 285 286 287 288 289 290
static void
row_select (GeditDocumentsPanel *panel,
            GtkListBox          *listbox,
            GtkListBoxRow       *row)
{
	GtkListBoxRow *selected_row = gtk_list_box_get_selected_row (listbox);

	if (row != selected_row)
291
	{
292
		g_signal_handler_block (listbox, panel->selection_changed_handler_id);
293
		gtk_list_box_select_row (listbox, row);
294
		g_signal_handler_unblock (listbox, panel->selection_changed_handler_id);
295
	}
296

297
	panel->current_selection = GTK_WIDGET (row);
298 299
	make_row_visible (panel, GTK_WIDGET (row));
}
300

301
static void
302 303 304 305
insert_row (GeditDocumentsPanel *panel,
            GtkListBox          *listbox,
            GtkWidget           *row,
            gint                 position)
Paolo Borelli's avatar
Paolo Borelli committed
306
{
307
	g_signal_handler_block (listbox, panel->selection_changed_handler_id);
308
	gtk_list_box_insert (listbox, row, position);
309
	g_signal_handler_unblock (listbox, panel->selection_changed_handler_id);
Paolo Borelli's avatar
Paolo Borelli committed
310 311 312
}

static void
313 314 315
select_active_tab (GeditDocumentsPanel *panel)
{
	GeditNotebook *notebook;
316
	gboolean have_tabs;
317
	GeditTab *tab;
318

319
	notebook = gedit_multi_notebook_get_active_notebook (panel->mnb);
320
	have_tabs = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook)) > 0;
321
	tab = gedit_multi_notebook_get_active_tab (panel->mnb);
Paolo Borelli's avatar
Paolo Borelli committed
322

323
	if (notebook != NULL && tab != NULL && have_tabs)
Paolo Borelli's avatar
Paolo Borelli committed
324
	{
325
		GtkListBoxRow *row = get_row_from_widget (panel, GTK_WIDGET (tab));
Paolo Borelli's avatar
Paolo Borelli committed
326

327 328
		if (row)
		{
329
			row_select (panel, GTK_LIST_BOX (panel->listbox), row);
330
		}
331 332
	}
}
Paolo Borelli's avatar
Paolo Borelli committed
333

334 335
static GtkListBoxRow *
get_first_notebook_found (GeditDocumentsPanel *panel)
336
{
337 338 339
	GList *children;
	GList *l;
	GtkListBoxRow *row = NULL;
340

341
	children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
342

343 344 345
	for (l = children; l != NULL; l = g_list_next (l))
	{
		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (l->data))
346
		{
347 348
			row = l->data;
			break;
Paolo Borelli's avatar
Paolo Borelli committed
349 350
		}
	}
351 352 353 354

	g_list_free (children);

	return row;
Paolo Borelli's avatar
Paolo Borelli committed
355 356 357
}

static void
358 359 360 361 362 363
multi_notebook_tab_switched (GeditMultiNotebook  *mnb,
                             GeditNotebook       *old_notebook,
                             GeditTab            *old_tab,
                             GeditNotebook       *new_notebook,
                             GeditTab            *new_tab,
                             GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
364
{
365
	gedit_debug (DEBUG_PANEL);
Paolo Borelli's avatar
Paolo Borelli committed
366

367 368
	if (!_gedit_window_is_removing_tabs (panel->window) &&
	    panel->is_in_tab_switched == FALSE)
369 370
	{
		GtkListBoxRow *row;
Paolo Borelli's avatar
Paolo Borelli committed
371

372
		panel->is_in_tab_switched = TRUE;
Paolo Borelli's avatar
Paolo Borelli committed
373

374
		row = get_row_from_widget (panel, GTK_WIDGET (new_tab));
Paolo Borelli's avatar
Paolo Borelli committed
375

376
		if (row)
377
		{
378
			row_select (panel, GTK_LIST_BOX (panel->listbox), row);
379
		}
Paolo Borelli's avatar
Paolo Borelli committed
380

381
		panel->is_in_tab_switched = FALSE;
382 383
	}
}
Paolo Borelli's avatar
Paolo Borelli committed
384

385
static void
386
group_row_set_notebook_name (GtkWidget *row)
387
{
388 389 390 391 392
	GeditNotebook *notebook;
	GeditMultiNotebook *mnb;
	guint num;
	gchar *name;
	GeditDocumentsGroupRow *group_row = GEDIT_DOCUMENTS_GROUP_ROW (row);
393

394
	notebook = GEDIT_NOTEBOOK (group_row->ref);
395

396
	mnb = group_row->panel->mnb;
397
	num = gedit_multi_notebook_get_notebook_num (mnb, notebook);
398

399
	name = g_strdup_printf (_("Tab Group %i"), num + 1);
400

401
	gtk_label_set_text (GTK_LABEL (group_row->label), name);
402

403
	g_free (name);
404
}
405

406 407 408 409 410 411 412 413 414 415 416
static void
group_row_update_names (GeditDocumentsPanel *panel,
                        GtkWidget           *listbox)
{
	GList *children;
	GList *l;
	GtkWidget *row;

	children = gtk_container_get_children (GTK_CONTAINER (listbox));

	for (l = children; l != NULL; l = g_list_next (l))
417
	{
418 419 420 421 422 423
		row = l->data;

		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
		{
			group_row_set_notebook_name (row);
		}
424
	}
425 426

	g_list_free (children);
427 428
}

429 430
static void
group_row_refresh_visibility (GeditDocumentsPanel *panel)
431
{
432
	gboolean notebook_is_unique;
433
	GtkWidget *first_group_row;
434

435
	notebook_is_unique = gedit_multi_notebook_get_n_notebooks (panel->mnb) <= 1;
436
	first_group_row = GTK_WIDGET (get_first_notebook_found (panel));
437

438 439 440
	gtk_widget_set_no_show_all (first_group_row, notebook_is_unique);
	gtk_widget_set_visible (first_group_row, !notebook_is_unique);
}
441

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
static gchar *
doc_get_name (GeditDocument *doc)
{
	gchar *name;
	gchar *docname;

	name = gedit_document_get_short_name_for_display (doc);

	/* Truncate the name so it doesn't get insanely wide. */
	docname = gedit_utils_str_middle_truncate (name, MAX_DOC_NAME_LENGTH);

	g_free (name);

	return docname;
}

static void
document_row_sync_tab_name_and_icon (GeditTab   *tab,
                                     GParamSpec *pspec,
                                     GtkWidget  *row)
{
	GeditDocumentsDocumentRow *document_row = GEDIT_DOCUMENTS_DOCUMENT_ROW (row);
	GeditDocument *doc;
465
	GtkSourceFile *file;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	gchar *name;
	GdkPixbuf *pixbuf;

	doc = gedit_tab_get_document (tab);
	name = doc_get_name (doc);

	if (!gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)))
	{
		gtk_label_set_text (GTK_LABEL (document_row->label), name);
	}
	else
	{
		gchar *markup;

		markup = g_markup_printf_escaped ("<b>%s</b>", name);
		gtk_label_set_markup (GTK_LABEL (document_row->label), markup);

		g_free (markup);
	}

	g_free (name);

488 489
	file = gedit_document_get_file (doc);

490
	/* The status has as separate label to prevent ellipsizing */
491
	if (!gtk_source_file_is_readonly (file))
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	{
		gtk_widget_hide (GTK_WIDGET (document_row->status_label));
	}
	else
	{
		gchar *status;

		status = g_strdup_printf ("[%s]", _("Read-Only"));

		gtk_label_set_text (GTK_LABEL (document_row->status_label), status);
		gtk_widget_show (GTK_WIDGET (document_row->status_label));

		g_free (status);
	}

	/* Update header of the row */
	pixbuf = _gedit_tab_get_icon (tab);

	if (pixbuf)
	{
		gtk_image_set_from_pixbuf (GTK_IMAGE (document_row->image), pixbuf);
	}
	else
	{
		gtk_image_clear (GTK_IMAGE (document_row->image));
	}
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533
static void
refresh_notebook (GeditDocumentsPanel *panel,
                  GeditNotebook       *notebook)
{
	GList *tabs;
	GList *l;

	tabs = gtk_container_get_children (GTK_CONTAINER (notebook));

	for (l = tabs; l != NULL; l = g_list_next (l))
	{
		GtkWidget *row;

		row = gedit_documents_document_row_new (panel, GEDIT_TAB (l->data));
534 535
		insert_row (panel, GTK_LIST_BOX (panel->listbox), row, -1);
		panel->nb_row_tab += 1;
536 537 538 539 540 541 542 543 544 545 546 547
	}

	g_list_free (tabs);
}

static void
refresh_notebook_foreach (GeditNotebook       *notebook,
                          GeditDocumentsPanel *panel)
{
	GtkWidget *row;

	row = gedit_documents_group_row_new (panel, notebook);
548 549
	insert_row (panel, GTK_LIST_BOX (panel->listbox), row, -1);
	panel->nb_row_notebook += 1;
550 551 552 553 554 555 556 557 558 559 560 561

	group_row_refresh_visibility (panel);
	refresh_notebook (panel, notebook);
}

static void
refresh_list (GeditDocumentsPanel *panel)
{
	GList *children;
	GList *l;

	/* Clear the listbox */
562
	children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

	for (l = children; l != NULL; l = g_list_next (l))
	{
		GeditDocumentsGenericRow *row = l->data;

		if (GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
		{
			GeditTab *tab = GEDIT_TAB (row->ref);
			g_signal_handlers_disconnect_matched (tab,
			                                      G_SIGNAL_MATCH_FUNC,
			                                      0,
			                                      0,
			                                      NULL,
			                                      G_CALLBACK (document_row_sync_tab_name_and_icon),
			                                      NULL);
		}

		gtk_widget_destroy (GTK_WIDGET (row));
	}

	g_list_free (children);

585
	gedit_multi_notebook_foreach_notebook (panel->mnb,
586 587 588 589 590
	                                       (GtkCallback)refresh_notebook_foreach,
	                                       panel);
	select_active_tab (panel);
}

591
static void
592 593 594 595
multi_notebook_tab_removed (GeditMultiNotebook  *mnb,
                            GeditNotebook       *notebook,
                            GeditTab            *tab,
                            GeditDocumentsPanel *panel)
596
{
597 598
	GtkListBoxRow *row;

599 600
	gedit_debug (DEBUG_PANEL);

601
	row = get_row_from_widget (panel, GTK_WIDGET (tab));
602 603 604 605 606 607 608

	/* Disconnect before destroy it so document_row_sync_tab_name_and_icon()
	 * don't get invalid data */
	g_signal_handlers_disconnect_by_func (GEDIT_DOCUMENTS_DOCUMENT_ROW (row)->ref,
	                                      G_CALLBACK (document_row_sync_tab_name_and_icon),
	                                      row);

609
	gtk_widget_destroy (GTK_WIDGET (row));
610
	panel->nb_row_tab -= 1;
611 612 613 614 615 616 617 618 619 620 621 622 623 624
}

static gint
get_dest_position_for_tab (GeditDocumentsPanel *panel,
                           GeditNotebook       *notebook,
                           GeditTab            *tab)
{
	gint page_num;
	GList *children;
	GList *item;
	gint res = -1;

	/* Get tab's position in notebook and notebook's position in GtkListBox
	 * then return future tab's position in GtkListBox */
625

626 627
	page_num = gtk_notebook_page_num (GTK_NOTEBOOK (notebook), GTK_WIDGET (tab));

628
	children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
629
	item = g_list_find_custom (children, notebook, listbox_search_function);
630

631 632 633 634 635 636 637 638
	if (item)
	{
		res = 1 + page_num + g_list_position (children, item);
	}

	g_list_free (children);

	return res;
Paolo Borelli's avatar
Paolo Borelli committed
639 640 641
}

static void
642 643 644 645
multi_notebook_tab_added (GeditMultiNotebook  *mnb,
                          GeditNotebook       *notebook,
                          GeditTab            *tab,
                          GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
646
{
647 648
	gint position;
	GtkWidget *row;
Paolo Borelli's avatar
Paolo Borelli committed
649

650 651
	gedit_debug (DEBUG_PANEL);

652 653 654
	position = get_dest_position_for_tab (panel, notebook, tab);

	if (position == -1)
655
	{
656 657
		panel->nb_row_tab = 0;
		panel->nb_row_notebook = 0;
Paolo Borelli's avatar
Paolo Borelli committed
658

659
		refresh_list (panel);
660
	}
661 662 663 664
	else
	{
		/* Add a new tab's row to the listbox */
		row = gedit_documents_document_row_new (panel, tab);
665
		insert_row (panel, GTK_LIST_BOX (panel->listbox), row, position);
666

667
		panel->nb_row_tab += 1;
668 669 670

		if (tab == gedit_multi_notebook_get_active_tab (mnb))
		{
671
			row_select (panel, GTK_LIST_BOX (panel->listbox), GTK_LIST_BOX_ROW (row));
672
		}
673
	}
Paolo Borelli's avatar
Paolo Borelli committed
674 675 676
}

static void
677 678 679
multi_notebook_notebook_removed (GeditMultiNotebook  *mnb,
                                 GeditNotebook       *notebook,
                                 GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
680
{
681 682
	GtkListBoxRow *row;

683 684
	gedit_debug (DEBUG_PANEL);

685
	row = get_row_from_widget (panel, GTK_WIDGET (notebook));
686
	gtk_container_remove (GTK_CONTAINER (panel->listbox), GTK_WIDGET (row));
687

688
	panel->nb_row_notebook -= 1;
Paolo Borelli's avatar
Paolo Borelli committed
689

690
	group_row_refresh_visibility (panel);
691
	group_row_update_names (panel, panel->listbox);
Paolo Borelli's avatar
Paolo Borelli committed
692 693 694
}

static void
695 696 697 698
row_move (GeditDocumentsPanel *panel,
          GeditNotebook       *notebook,
          GtkWidget           *tab,
          GtkWidget           *row)
Paolo Borelli's avatar
Paolo Borelli committed
699
{
700
	gint position;
701

702
	g_object_ref (row);
Paolo Borelli's avatar
Paolo Borelli committed
703

704
	gtk_container_remove (GTK_CONTAINER (panel->listbox), GTK_WIDGET (row));
705
	position = get_dest_position_for_tab (panel, notebook, GEDIT_TAB (tab));
706

707
	g_signal_handler_block (panel->listbox, panel->selection_changed_handler_id);
708

709
	gtk_list_box_insert (GTK_LIST_BOX (panel->listbox), row, position);
710 711
	g_object_unref (row);

712
	g_signal_handler_unblock (GTK_LIST_BOX (panel->listbox), panel->selection_changed_handler_id);
713 714
}

Paolo Borelli's avatar
Paolo Borelli committed
715
static void
716 717 718 719 720
multi_notebook_tabs_reordered (GeditMultiNotebook  *mnb,
                               GeditNotebook       *notebook,
                               GtkWidget           *page,
                               gint                 page_num,
                               GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
721
{
722 723
	GtkListBoxRow *row;

724 725
	gedit_debug (DEBUG_PANEL);

726
	row = get_row_from_widget (panel, GTK_WIDGET (page));
727

728 729
	row_move (panel, notebook, page, GTK_WIDGET (row));

730
	row_select (panel, GTK_LIST_BOX (panel->listbox), GTK_LIST_BOX_ROW (row));
731
}
Paolo Borelli's avatar
Paolo Borelli committed
732 733 734

static void
set_window (GeditDocumentsPanel *panel,
735
            GeditWindow         *window)
Paolo Borelli's avatar
Paolo Borelli committed
736
{
737 738
	panel->window = g_object_ref (window);
	panel->mnb = GEDIT_MULTI_NOTEBOOK (_gedit_window_get_multi_notebook (window));
Paolo Borelli's avatar
Paolo Borelli committed
739

740
	g_signal_connect (panel->mnb,
741 742 743
	                  "notebook-removed",
	                  G_CALLBACK (multi_notebook_notebook_removed),
	                  panel);
744
	g_signal_connect (panel->mnb,
745 746 747
	                  "tab-added",
	                  G_CALLBACK (multi_notebook_tab_added),
	                  panel);
748
	g_signal_connect (panel->mnb,
749 750 751
	                  "tab-removed",
	                  G_CALLBACK (multi_notebook_tab_removed),
	                  panel);
752
	g_signal_connect (panel->mnb,
753 754 755 756
	                  "page-reordered",
	                  G_CALLBACK (multi_notebook_tabs_reordered),
	                  panel);

757 758 759 760
	panel->tab_switched_handler_id = g_signal_connect (panel->mnb,
                                                           "switch-tab",
                                                           G_CALLBACK (multi_notebook_tab_switched),
                                                           panel);
761

762
	panel->first_selection = TRUE;
763 764

	refresh_list (panel);
765
	group_row_refresh_visibility (panel);
Paolo Borelli's avatar
Paolo Borelli committed
766 767
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781
static void
listbox_selection_changed (GtkListBox          *listbox,
                           GtkListBoxRow       *row,
                           GeditDocumentsPanel *panel)
{
	if (row == NULL)
	{
		/* No selection on document panel */
		return;
	}

	/* When the window is shown, the first notebook row is selected
	 * and therefore also shown - we don't want this */

782
	if (panel->first_selection)
783
	{
784
		panel->first_selection = FALSE;
785 786 787
		group_row_refresh_visibility (panel);
	}

788
	g_signal_handler_block (panel->mnb, panel->tab_switched_handler_id);
789

790 791
	if (GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
	{
792
		gedit_multi_notebook_set_active_tab (panel->mnb,
793
		                                     GEDIT_TAB (GEDIT_DOCUMENTS_DOCUMENT_ROW (row)->ref));
794

795
		panel->current_selection = GTK_WIDGET (row);
796
	}
797
	else if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row) && panel->current_selection)
Paolo Borelli's avatar
Paolo Borelli committed
798
	{
799
		row_select (panel,
800 801
		            GTK_LIST_BOX (panel->listbox),
		            GTK_LIST_BOX_ROW (panel->current_selection));
802

803 804 805 806
	}
	else
	{
		g_assert_not_reached ();
807
	}
808

809
	g_signal_handler_unblock (panel->mnb, panel->tab_switched_handler_id);
Paolo Borelli's avatar
Paolo Borelli committed
810 811 812 813
}

static void
gedit_documents_panel_set_property (GObject      *object,
814 815 816
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
Paolo Borelli's avatar
Paolo Borelli committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);

	switch (prop_id)
	{
		case PROP_WINDOW:
			set_window (panel, g_value_get_object (value));
			break;

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

static void
gedit_documents_panel_get_property (GObject    *object,
834 835 836
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
Paolo Borelli's avatar
Paolo Borelli committed
837 838 839 840 841 842
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);

	switch (prop_id)
	{
		case PROP_WINDOW:
843
			g_value_set_object (value, panel->window);
Paolo Borelli's avatar
Paolo Borelli committed
844
			break;
845

Paolo Borelli's avatar
Paolo Borelli committed
846 847
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
848
			break;
Paolo Borelli's avatar
Paolo Borelli committed
849 850 851 852 853 854
	}
}

static void
gedit_documents_panel_finalize (GObject *object)
{
855
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);
856

857
	g_signal_handlers_disconnect_by_func (panel->mnb,
858 859
	                                      G_CALLBACK (multi_notebook_notebook_removed),
	                                      panel);
860
	g_signal_handlers_disconnect_by_func (panel->mnb,
861 862
	                                      G_CALLBACK (multi_notebook_tab_added),
	                                      panel);
863
	g_signal_handlers_disconnect_by_func (panel->mnb,
864 865
	                                      G_CALLBACK (multi_notebook_tab_removed),
	                                      panel);
866
	g_signal_handlers_disconnect_by_func (panel->mnb,
867 868
	                                      G_CALLBACK (multi_notebook_tabs_reordered),
	                                      panel);
869
	g_signal_handlers_disconnect_by_func (panel->mnb,
870 871
	                                      G_CALLBACK (multi_notebook_tab_switched),
	                                      panel);
Paolo Borelli's avatar
Paolo Borelli committed
872 873 874 875

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

876 877 878 879 880
static void
gedit_documents_panel_dispose (GObject *object)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);

881
	g_clear_object (&panel->window);
882

883
	if (panel->source_targets)
884
	{
885 886
		gtk_target_list_unref (panel->source_targets);
		panel->source_targets = NULL;
887 888
	}

889 890 891
	G_OBJECT_CLASS (gedit_documents_panel_parent_class)->dispose (object);
}

892
static GtkWidget *
893
create_placeholder_row (gint height)
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
{
	GtkStyleContext *context;

	GtkWidget *placeholder_row = gtk_list_box_row_new ();

	context = gtk_widget_get_style_context (placeholder_row);
	gtk_style_context_add_class (context, "gedit-document-panel-placeholder-row");

	gtk_widget_set_size_request (placeholder_row, -1, height);

	return placeholder_row;
}

static void
panel_on_drag_begin (GtkWidget      *widget,
                     GdkDragContext *context)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
	GtkWidget *drag_document_row;
	GtkAllocation allocation;
	const gchar *name;
	GtkWidget *label;
	gint width, height;
	GtkWidget *image_box;
	GtkWidget *box;
	GtkStyleContext *style_context;

921
	drag_document_row = panel->drag_document_row;
922 923 924
	gtk_widget_get_allocation (drag_document_row, &allocation);
	gtk_widget_hide (drag_document_row);

925
	panel->document_row_height = allocation.height;
926 927 928

	name = gtk_label_get_label (GTK_LABEL (GEDIT_DOCUMENTS_DOCUMENT_ROW (drag_document_row)->label));

929
	label = gtk_label_new (NULL);
930
	gtk_label_set_markup (GTK_LABEL (label), name);
931 932
	gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
	gtk_widget_set_halign (label, GTK_ALIGN_START);
Paolo Borelli's avatar
Paolo Borelli committed
933
	gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
934 935 936 937 938 939 940 941 942 943

	gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
	image_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
	gtk_widget_set_size_request (image_box, width, height);

	box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);

	gtk_box_pack_start (GTK_BOX (box), image_box, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);

944 945 946
	panel->dnd_window = gtk_window_new (GTK_WINDOW_POPUP);
	gtk_widget_set_size_request (panel->dnd_window, allocation.width, allocation.height);
	gtk_window_set_screen (GTK_WINDOW (panel->dnd_window),
947 948
	                       gtk_widget_get_screen (drag_document_row));

949
	style_context = gtk_widget_get_style_context (panel->dnd_window);
950 951
	gtk_style_context_add_class (style_context, "gedit-document-panel-dragged-row");

952 953 954
	gtk_container_add (GTK_CONTAINER (panel->dnd_window), box);
	gtk_widget_show_all (panel->dnd_window);
	gtk_widget_set_opacity (panel->dnd_window, 0.8);
955 956

	gtk_drag_set_icon_widget (context,
957 958 959
	                          panel->dnd_window,
	                          panel->drag_document_row_x,
	                          panel->drag_document_row_y);
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
}

static gboolean
panel_on_drag_motion (GtkWidget      *widget,
                      GdkDragContext *context,
                      gint            x,
                      gint            y,
                      guint           time)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
	GeditDocumentsGenericRow *generic_row;
	GtkWidget *source_panel;
	gint dest_x, dest_y;
	gint row_placeholder_index;
	gint row_index;

	GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);

	if (target != gdk_atom_intern_static_string ("GEDIT_DOCUMENTS_DOCUMENT_ROW"))
	{
		gdk_drag_status (context, 0, time);
		return FALSE;
	}

984
	gtk_widget_translate_coordinates (widget, panel->listbox,
985 986 987
	                                  x, y,
	                                  &dest_x, &dest_y);

988
	generic_row = (GeditDocumentsGenericRow *)gtk_list_box_get_row_at_y (GTK_LIST_BOX (panel->listbox), dest_y);
989 990
	source_panel = gtk_drag_get_source_widget (context);

991
	if (!panel->row_placeholder)
992 993 994 995
	{
		if (!generic_row)
		{
			/* We don't have a row height to use, so use the source one */
996
			panel->document_row_height = GEDIT_DOCUMENTS_PANEL (source_panel)->document_row_height;
997 998 999 1000 1001 1002
		}
		else
		{
			GtkAllocation allocation;

			gtk_widget_get_allocation (GTK_WIDGET (generic_row), &allocation);
1003
			panel->document_row_height = allocation.height;
1004 1005
		}

1006
		panel->row_placeholder = create_placeholder_row (panel->document_row_height);
1007 1008
		gtk_widget_show (panel->row_placeholder);
		g_object_ref_sink (panel->row_placeholder);
1009
	}
1010
	else if (GTK_WIDGET (generic_row) == panel->row_placeholder)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	{
		/* cursor on placeholder */
		gdk_drag_status (context, GDK_ACTION_MOVE, time);

		return TRUE;
	}

	if (!generic_row)
	{
		/* cursor on empty space => put the placeholder at end of list */
1021
		GList *children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

		row_placeholder_index = g_list_length (children);
		g_list_free (children);
	}
	else
	{
		row_index = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (generic_row));

		gtk_widget_translate_coordinates (widget, GTK_WIDGET (generic_row),
		                                  x, y,
		                                  &dest_x, &dest_y);

1034
		if (dest_y <= panel->document_row_height / 2 && row_index > 0)
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
		{
			row_placeholder_index = row_index;
		}
		else
		{
			row_placeholder_index = row_index + 1;
		}
	}

	if (source_panel == widget)
	{
		/* Adjustment because of hidden source row */
1047 1048
		gint source_row_index = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (panel->drag_document_row));
		panel->row_source_row_offset = source_row_index <  row_placeholder_index ? -1 : 0;
1049 1050
	}

1051
	if (panel->row_placeholder_index != row_placeholder_index)
1052
	{
1053
		if (panel->row_placeholder_index != ROW_OUTSIDE_LISTBOX)
1054
		{
1055 1056
			gtk_container_remove (GTK_CONTAINER (panel->listbox),
			                      panel->row_placeholder);
1057

1058
			if (panel->row_placeholder_index < row_placeholder_index)
1059 1060 1061 1062 1063 1064
			{
				/* Ajustment because of existing placeholder row */
				row_placeholder_index -= 1;
			}
		}

1065
		panel->row_destination_index = panel->row_placeholder_index = row_placeholder_index;
1066

1067 1068 1069
		gtk_list_box_insert (GTK_LIST_BOX (panel->listbox),
		                     panel->row_placeholder,
		                     panel->row_placeholder_index);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	}

	gdk_drag_status (context, GDK_ACTION_MOVE, time);

	return TRUE;
}

static void
panel_on_drag_leave (GtkWidget      *widget,
                     GdkDragContext *context,
                     guint           time)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);

1084
	if (panel->row_placeholder_index != ROW_OUTSIDE_LISTBOX)
1085
	{
1086 1087
		gtk_container_remove (GTK_CONTAINER (panel->listbox), panel->row_placeholder);
		panel->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	}
}

static gboolean
panel_on_drag_drop (GtkWidget        *widget,
                    GdkDragContext   *context,
                    gint              x,
                    gint              y,
                    guint             time)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);

	GdkAtom target = gtk_drag_dest_find_target (widget, context, NULL);
	GtkWidget *source_widget = gtk_drag_get_source_widget (context);

	if (GEDIT_IS_DOCUMENTS_PANEL (source_widget))
	{
1105
		gtk_widget_show (GEDIT_DOCUMENTS_PANEL (source_widget)->drag_document_row);
1106 1107 1108 1109 1110 1111 1112 1113
	}

	if (target == gdk_atom_intern_static_string ("GEDIT_DOCUMENTS_DOCUMENT_ROW"))
	{
		gtk_drag_get_data (widget, context, target, time);
		return TRUE;
	}

1114
	panel->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	return FALSE;
}

static void
panel_on_drag_data_get (GtkWidget        *widget,
                        GdkDragContext   *context,
                        GtkSelectionData *data,
                        guint             info,
                        guint             time)
{
1125
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
1126
	GdkAtom target = gtk_selection_data_get_target (data);
1127
	GdkAtom result;
1128 1129 1130 1131 1132 1133

	if (target == gdk_atom_intern_static_string ("GEDIT_DOCUMENTS_DOCUMENT_ROW"))
	{
		gtk_selection_data_set (data,
		                        target,
		                        8,
1134
		                        (void*)&panel->drag_document_row,
1135
		                        sizeof (gpointer));
1136
		return;
1137
	}
1138

1139
	result = gtk_drag_dest_find_target (widget, context, panel->source_targets);
1140 1141 1142 1143 1144 1145 1146

	if (result != GDK_NONE)
	{
		GeditTab *tab;
		GeditDocument *doc;
		gchar *full_name;

1147
		tab = GEDIT_TAB (GEDIT_DOCUMENTS_DOCUMENT_ROW (panel->drag_document_row)->ref);
1148 1149 1150 1151
		doc = gedit_tab_get_document (tab);

		if (!gedit_document_is_untitled (doc))
		{
1152 1153 1154
			GtkSourceFile *file = gedit_document_get_file (doc);
			GFile *location = gtk_source_file_get_location (file);
			full_name = g_file_get_parse_name (location);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

			gtk_selection_data_set (data,
			                        target,
			                        8,
			                        (guchar *)full_name,
			                        strlen (full_name));
			g_free (full_name);
		}
	}

1165
	gtk_widget_show (panel->drag_document_row);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
}

static GeditNotebook *
get_notebook_and_position_from_document_row (GeditDocumentsPanel *panel,
                                             gint                 row_index,
                                             gint                *position)
{
	GList *l;
	gint index = 0;
	GeditDocumentsGroupRow *row;

1177
	GList *children = gtk_container_get_children (GTK_CONTAINER (panel->listbox));
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
	gint nb_elements = g_list_length (children);

	if (nb_elements == 1)
	{
		row = children->data;
	}
	else
	{
		l = g_list_nth (children, row_index - 1);

		while (TRUE)
		{
			row = l->data;

			if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
			{
				break;
			}

			l = g_list_previous (l);
			index += 1;
		}
	}

	g_list_free (children);

	*position = index;
	return GEDIT_NOTEBOOK (row->ref);
}

static void
panel_on_drag_data_received (GtkWidget        *widget,
                             GdkDragContext   *context,
                             gint              x,
                             gint              y,
                             GtkSelectionData *data,
                             guint             info,
                             guint             time)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
	GeditDocumentsPanel *source_panel = NULL;

	GtkWidget *source_widget = gtk_drag_get_source_widget (context);

	if (GEDIT_IS_DOCUMENTS_PANEL (source_widget))
	{
		source_panel = GEDIT_DOCUMENTS_PANEL (source_widget);
	}

	GtkWidget **source_row = (void*) gtk_selection_data_get_data (data);

	if (source_panel &&
	    gtk_selection_data_get_target (data) == gdk_atom_intern_static_string ("GEDIT_DOCUMENTS_DOCUMENT_ROW"))
	{
		gint source_index = gtk_list_box_row_get_index (GTK_LIST_BOX_ROW (*source_row));

		/* And finally, we can move the row */
		if (source_panel != panel ||
1236 1237
		    (panel->row_destination_index != source_index &&
		     panel->row_destination_index != source_index + 1))
1238 1239 1240 1241 1242 1243
		{
			GeditNotebook *old_notebook, *new_notebook;
			gint position;

			GeditTab *tab = GEDIT_TAB (GEDIT_DOCUMENTS_DOCUMENT_ROW (*source_row)->ref);

1244
			old_notebook = gedit_multi_notebook_get_notebook_for_tab (source_panel->mnb, tab);
1245
			new_notebook = get_notebook_and_position_from_document_row (panel,
1246
			                                                            panel->row_destination_index,
1247 1248 1249 1250 1251 1252 1253
			                                                            &position);
			if (old_notebook == new_notebook)
			{
				gtk_widget_show (*source_row);

				gtk_notebook_reorder_child (GTK_NOTEBOOK (new_notebook),
				                            GTK_WIDGET (tab),
1254
				                            position + panel->row_source_row_offset);
1255 1256 1257 1258 1259
			}
			else
			{
				gedit_notebook_move_tab (old_notebook, new_notebook, tab, position);
			}
1260

1261
			if (tab != gedit_multi_notebook_get_active_tab (panel->mnb))
1262
			{
1263 1264 1265
				g_signal_handler_block (panel->mnb, panel->tab_switched_handler_id);
				gedit_multi_notebook_set_active_tab (panel->mnb, tab);
				g_signal_handler_unblock (panel->mnb, panel->tab_switched_handler_id);
1266
			}
1267 1268 1269 1270 1271 1272 1273 1274 1275
		}

		gtk_drag_finish (context, TRUE, FALSE, time);
	}
	else
	{
		gtk_drag_finish (context, FALSE, FALSE, time);
	}

1276
	panel->row_destination_index = panel->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
1277

1278
	if (panel->row_placeholder)
1279
	{
1280 1281
		gtk_widget_destroy (panel->row_placeholder);
		panel->row_placeholder = NULL;
1282 1283 1284 1285 1286 1287 1288 1289 1290
	}
}

static void
panel_on_drag_end (GtkWidget      *widget,
                   GdkDragContext *context)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);

1291 1292
	panel->drag_document_row = NULL;
	panel->is_on_drag = FALSE;
1293

1294 1295
	gtk_widget_destroy (panel->dnd_window);
	panel->dnd_window = NULL;
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
}

static gboolean
panel_on_drag_failed (GtkWidget      *widget,
                      GdkDragContext *context,
                      GtkDragResult   result)
{
	GtkWidget *source_widget = gtk_drag_get_source_widget (context);

	if (GEDIT_IS_DOCUMENTS_PANEL (source_widget))
	{
1307
		gtk_widget_show (GEDIT_DOCUMENTS_PANEL (source_widget)->drag_document_row);
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	}

	return FALSE;
}

static gboolean
panel_on_motion_notify (GtkWidget      *widget,
                        GdkEventMotion *event)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);

1319
	if (panel->drag_document_row == NULL || panel->is_on_drag)
1320 1321 1322 1323 1324 1325
	{
		return FALSE;
	}

	if (!(event->state & GDK_BUTTON1_MASK))
	{
1326
		panel->drag_document_row = NULL;
1327 1328 1329 1330 1331

		return FALSE;
	}

	if (gtk_drag_check_threshold (widget,
1332
	                              panel->drag_root_x, panel->drag_root_y,
1333 1334
	                              event->x_root, event->y_root))
	{
1335
		panel->is_on_drag = TRUE;
1336

1337
		gtk_drag_begin_with_coordinates (widget, panel->source_targets, GDK_ACTION_MOVE,
1338 1339 1340 1341 1342 1343 1344
		                                 GDK_BUTTON_PRIMARY, (GdkEvent*)event,
		                                 -1, -1);
	}

	return FALSE;
}

1345
static void
Paolo Borelli's avatar
Paolo Borelli committed
1346 1347 1348
gedit_documents_panel_class_init (GeditDocumentsPanelClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
1349
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
Paolo Borelli's avatar
Paolo Borelli committed
1350 1351

	object_class->finalize = gedit_documents_panel_finalize;
1352
	object_class->dispose = gedit_documents_panel_dispose;
Paolo Borelli's avatar
Paolo Borelli committed
1353 1354 1355
	object_class->get_property = gedit_documents_panel_get_property;
	object_class->set_property = gedit_documents_panel_set_property;

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	widget_class->motion_notify_event = panel_on_motion_notify;

	widget_class->drag_begin = panel_on_drag_begin;
	widget_class->drag_end = panel_on_drag_end;
	widget_class->drag_failed = panel_on_drag_failed;
	widget_class->drag_motion = panel_on_drag_motion;
	widget_class->drag_leave = panel_on_drag_leave;
	widget_class->drag_drop = panel_on_drag_drop;
	widget_class->drag_data_get = panel_on_drag_data_get;
	widget_class->drag_data_received = panel_on_drag_data_received;

1367 1368 1369 1370 1371 1372 1373 1374
	properties[PROP_WINDOW] =
		g_param_spec_object ("window",
		                     "Window",
		                     "The GeditWindow this GeditDocumentsPanel is associated with",
		                     GEDIT_TYPE_WINDOW,
		                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties (object_class, LAST_PROP, properties);
Paolo Borelli's avatar
Paolo Borelli committed
1375 1376
}

1377 1378
static void
gedit_documents_panel_init (GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
1379
{
1380 1381
	GtkWidget *sw;
	GtkStyleContext *context;
1382 1383

	gedit_debug (DEBUG_PANEL);
Paolo Borelli's avatar
Paolo Borelli committed
1384

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
	gtk_orientable_set_orientation (GTK_ORIENTABLE (panel),
	                                GTK_ORIENTATION_VERTICAL);

	/* Create the scrolled window */
	sw = gtk_scrolled_window_new (NULL, NULL);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
	                                GTK_POLICY_AUTOMATIC,
	                                GTK_POLICY_AUTOMATIC);
	gtk_widget_show (sw);
	gtk_box_pack_start (GTK_BOX (panel), sw, TRUE, TRUE, 0);

	/* Create the listbox */
1398
	panel->listbox = gtk_list_box_new ();
Paolo Borelli's avatar
Paolo Borelli committed
1399

1400
	gtk_container_add (GTK_CONTAINER (sw), panel->listbox);
Paolo Borelli's avatar
Paolo Borelli committed
1401

1402
	panel->adjustment = gtk_list_box_get_adjustment (GTK_LIST_BOX (panel->listbox));
1403 1404

	/* Disable focus so it doesn't steal focus each time from the view */
1405
	gtk_widget_set_can_focus (panel->listbox, FALSE);
1406 1407

	/* Css style */
1408
	context = gtk_widget_get_style_context (panel->listbox);
1409
	gtk_style_context_add_class (context, "gedit-document-panel");
1410

1411 1412 1413 1414 1415 1416 1417 1418
	panel->selection_changed_handler_id = g_signal_connect (panel->listbox,
	                                                        "row-selected",
	                                                        G_CALLBACK (listbox_selection_changed),
	                                                        panel);
	panel->is_in_tab_switched = FALSE;
	panel->current_selection = NULL;
	panel->nb_row_notebook = 0;
	panel->nb_row_tab = 0;
1419 1420

	/* Drag and drop support */
1421 1422
	panel->source_targets = gtk_target_list_new (panel_targets, G_N_ELEMENTS (panel_targets));
	gtk_target_list_add_text_targets (panel->source_targets, 0);
1423 1424 1425 1426 1427 1428 1429

	gtk_drag_dest_set (GTK_WIDGET (panel), 0,
	                   panel_targets, G_N_ELEMENTS (panel_targets),
	                   GDK_ACTION_MOVE);

	gtk_drag_dest_set_track_motion (GTK_WIDGET (panel), TRUE);

1430 1431 1432 1433 1434 1435
	panel->drag_document_row = NULL;
	panel->row_placeholder = NULL;
	panel->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
	panel->row_destination_index = ROW_OUTSIDE_LISTBOX;
	panel->row_source_row_offset = 0;
	panel->is_on_drag = FALSE;
Paolo Borelli's avatar
Paolo Borelli committed
1436 1437
}

1438 1439
GtkWidget *
gedit_documents_panel_new (GeditWindow *window)
Paolo Borelli's avatar
Paolo Borelli committed
1440
{