gedit-documents-panel.c 49.9 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 - Sebastien Lafargue <slaf66@gmail.com>
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 <glib/gi18n.h>

Paolo Borelli's avatar
Paolo Borelli committed
27
#include "gedit-documents-panel.h"
28
#include "gedit-debug.h"
29
#include "gedit-document.h"
30
#include "gedit-multi-notebook.h"
31
#include "gedit-notebook.h"
32
#include "gedit-notebook-popup-menu.h"
33 34
#include "gedit-small-button.h"
#include "gedit-utils.h"
35
#include "gedit-commands.h"
Paolo Borelli's avatar
Paolo Borelli committed
36

37 38 39 40 41 42 43 44 45 46 47
typedef struct _GeditDocumentsGenericRow GeditDocumentsGenericRow;
typedef struct _GeditDocumentsGenericRow GeditDocumentsGroupRow;
typedef struct _GeditDocumentsGenericRow GeditDocumentsDocumentRow;

struct _GeditDocumentsGenericRow
{
	GtkListBoxRow        parent;

	GeditDocumentsPanel *panel;
	GtkWidget           *ref;

48
	GtkWidget           *box;
49
	GtkWidget           *label;
50
	GtkWidget           *close_button;
51 52 53

	/* Not used in GeditDocumentsGroupRow */
	GtkWidget           *image;
54
	GtkWidget           *status_label;
55 56 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
};

#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)

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

Paolo Borelli's avatar
Paolo Borelli committed
98 99
struct _GeditDocumentsPanelPrivate
{
100 101
	GeditWindow        *window;
	GeditMultiNotebook *mnb;
102
	GtkWidget          *listbox;
103

104
	guint               selection_changed_handler_id;
105 106 107 108 109
	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;
110
	GtkWidget          *current_selection;
111

112 113 114 115
	GtkAdjustment      *adjustment;

	guint               nb_row_notebook;
	guint               nb_row_tab;
116 117 118 119 120 121 122 123 124 125 126 127 128 129

	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
130 131
};

132
G_DEFINE_TYPE_WITH_PRIVATE (GeditDocumentsPanel, gedit_documents_panel, GTK_TYPE_BOX)
Paolo Borelli's avatar
Paolo Borelli committed
133 134 135 136

enum
{
	PROP_0,
137 138 139
	PROP_WINDOW
};

140 141 142 143
static const GtkTargetEntry panel_targets [] = {
	{"GEDIT_DOCUMENTS_DOCUMENT_ROW", GTK_TARGET_SAME_APP, 0},
};

144
#define MAX_DOC_NAME_LENGTH 60
Paolo Borelli's avatar
Paolo Borelli committed
145

146 147
#define ROW_OUTSIDE_LISTBOX -1

148 149
static guint
get_nb_visible_rows (GeditDocumentsPanel *panel)
150
{
151
	guint nb = 0;
152

153 154 155 156 157 158
	if (panel->priv->nb_row_notebook > 1)
	{
		nb += panel->priv->nb_row_notebook;
	}

	nb += panel->priv->nb_row_tab;
159

160
	return nb;
161
}
Paolo Borelli's avatar
Paolo Borelli committed
162

163 164 165
static guint
get_row_visible_index (GeditDocumentsPanel *panel,
                       GtkWidget           *searched_row)
Paolo Borelli's avatar
Paolo Borelli committed
166
{
167 168 169 170
	GList *children;
	GList *l;
	guint nb_notebook_row = 0;
	guint nb_tab_row = 0;
Paolo Borelli's avatar
Paolo Borelli committed
171

172
	children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));
Paolo Borelli's avatar
Paolo Borelli committed
173

174
	for (l = children; l != NULL; l = g_list_next (l))
Paolo Borelli's avatar
Paolo Borelli committed
175
	{
176 177 178
		GtkWidget *row = l->data;

		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
Paolo Borelli's avatar
Paolo Borelli committed
179
		{
180
			nb_notebook_row += 1;
Paolo Borelli's avatar
Paolo Borelli committed
181 182 183
		}
		else
		{
184
			nb_tab_row += 1;
Paolo Borelli's avatar
Paolo Borelli committed
185
		}
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

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

	g_list_free (children);

	if (panel->priv->nb_row_notebook == 1)
	{
		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;

	adjustment_value = gtk_adjustment_get_value (panel->priv->adjustment);
	adjustment_lower = gtk_adjustment_get_lower (panel->priv->adjustment);
	adjustment_upper = gtk_adjustment_get_upper (panel->priv->adjustment);
	adjustment_page_size = gtk_adjustment_get_page_size (panel->priv->adjustment);

	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
238 239 240
	}
	else
	{
241
		new_adjustment_value = adjustment_value;
Paolo Borelli's avatar
Paolo Borelli committed
242 243
	}

244 245
	gtk_adjustment_set_value (panel->priv->adjustment, new_adjustment_value);
}
Paolo Borelli's avatar
Paolo Borelli committed
246

247 248 249 250 251 252 253 254
/* 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;

255
	return searched_item == item ? 0 : -1;
Paolo Borelli's avatar
Paolo Borelli committed
256 257
}

258
static GtkListBoxRow *
259 260
get_row_from_widget (GeditDocumentsPanel *panel,
                     GtkWidget           *widget)
261
{
262 263 264
	GList *children;
	GList *item;
	GtkListBoxRow *row;
265

266
	children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));
267

268 269
	item = g_list_find_custom (children, widget, listbox_search_function);
	row = item ? item->data : NULL;
270

271
	g_list_free (children);
272

273 274
	return row;
}
275

276 277 278 279 280 281 282 283
static void
row_select (GeditDocumentsPanel *panel,
            GtkListBox          *listbox,
            GtkListBoxRow       *row)
{
	GtkListBoxRow *selected_row = gtk_list_box_get_selected_row (listbox);

	if (row != selected_row)
284
	{
285 286 287 288
		g_signal_handler_block (listbox, panel->priv->selection_changed_handler_id);
		gtk_list_box_select_row (listbox, row);
		g_signal_handler_unblock (listbox, panel->priv->selection_changed_handler_id);
	}
289

290
	panel->priv->current_selection = GTK_WIDGET (row);
291 292
	make_row_visible (panel, GTK_WIDGET (row));
}
293

294 295 296 297 298
static void
row_state_changed (GtkWidget           *row,
                   GtkStateFlags        previous_flags,
                   GeditDocumentsPanel *panel)
{
299
	GtkStateFlags flags;
300 301 302

	flags = gtk_widget_get_state_flags (row);

303 304 305 306 307 308 309 310
	if (flags & GTK_STATE_FLAG_PRELIGHT)
	{
		gtk_style_context_add_class (gtk_widget_get_style_context (row), "prelight-row");
	}
	else
	{
		gtk_style_context_remove_class (gtk_widget_get_style_context (row), "prelight-row");
	}
311 312 313 314 315 316 317 318 319

	if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
	{
		GTK_WIDGET_CLASS (gedit_documents_group_row_parent_class)->state_flags_changed (row, previous_flags);
	}
	else
	{
		GTK_WIDGET_CLASS (gedit_documents_document_row_parent_class)->state_flags_changed (row, previous_flags);
	}
320 321
}

322
static void
323 324 325 326
insert_row (GeditDocumentsPanel *panel,
            GtkListBox          *listbox,
            GtkWidget           *row,
            gint                 position)
Paolo Borelli's avatar
Paolo Borelli committed
327
{
328
	g_signal_handler_block (listbox, panel->priv->selection_changed_handler_id);
Paolo Borelli's avatar
Paolo Borelli committed
329

330
	gtk_list_box_insert (listbox, row, position);
331

332 333 334 335
	g_signal_connect (row,
	                  "state-flags-changed",
	                  G_CALLBACK (row_state_changed),
	                  panel);
Paolo Borelli's avatar
Paolo Borelli committed
336

337
	g_signal_handler_unblock (listbox, panel->priv->selection_changed_handler_id);
Paolo Borelli's avatar
Paolo Borelli committed
338 339 340
}

static void
341 342 343
select_active_tab (GeditDocumentsPanel *panel)
{
	GeditNotebook *notebook;
344
	gboolean have_tabs;
345
	GeditTab *tab;
346 347

	notebook = gedit_multi_notebook_get_active_notebook (panel->priv->mnb);
348
	have_tabs = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook)) > 0;
349
	tab = gedit_multi_notebook_get_active_tab (panel->priv->mnb);
Paolo Borelli's avatar
Paolo Borelli committed
350

351
	if (notebook != NULL && tab != NULL && have_tabs)
Paolo Borelli's avatar
Paolo Borelli committed
352
	{
353
		GtkListBoxRow *row = get_row_from_widget (panel, GTK_WIDGET (tab));
Paolo Borelli's avatar
Paolo Borelli committed
354

355 356 357 358
		if (row)
		{
			row_select (panel, GTK_LIST_BOX (panel->priv->listbox), row);
		}
359 360
	}
}
Paolo Borelli's avatar
Paolo Borelli committed
361

362 363
static GtkListBoxRow *
get_first_notebook_found (GeditDocumentsPanel *panel)
364
{
365 366 367
	GList *children;
	GList *l;
	GtkListBoxRow *row = NULL;
368

369
	children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));
370

371 372 373
	for (l = children; l != NULL; l = g_list_next (l))
	{
		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (l->data))
374
		{
375 376
			row = l->data;
			break;
Paolo Borelli's avatar
Paolo Borelli committed
377 378
		}
	}
379 380 381 382

	g_list_free (children);

	return row;
Paolo Borelli's avatar
Paolo Borelli committed
383 384 385
}

static void
386 387 388 389 390 391
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
392
{
393
	gedit_debug (DEBUG_PANEL);
Paolo Borelli's avatar
Paolo Borelli committed
394

395
	if (!_gedit_window_is_removing_tabs (panel->priv->window) &&
396
	    panel->priv->is_in_tab_switched == FALSE)
397 398
	{
		GtkListBoxRow *row;
Paolo Borelli's avatar
Paolo Borelli committed
399

400
		panel->priv->is_in_tab_switched = TRUE;
Paolo Borelli's avatar
Paolo Borelli committed
401

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

404
		if (row)
405
		{
406
			row_select (panel, GTK_LIST_BOX (panel->priv->listbox), row);
407
		}
Paolo Borelli's avatar
Paolo Borelli committed
408

409
		panel->priv->is_in_tab_switched = FALSE;
410 411
	}
}
Paolo Borelli's avatar
Paolo Borelli committed
412

413
static void
414
group_row_set_notebook_name (GtkWidget *row)
415
{
416 417 418 419 420
	GeditNotebook *notebook;
	GeditMultiNotebook *mnb;
	guint num;
	gchar *name;
	GeditDocumentsGroupRow *group_row = GEDIT_DOCUMENTS_GROUP_ROW (row);
421

422
	notebook = GEDIT_NOTEBOOK (group_row->ref);
423

424 425
	mnb = group_row->panel->priv->mnb;
	num = gedit_multi_notebook_get_notebook_num (mnb, notebook);
426

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

429
	gtk_label_set_text (GTK_LABEL (group_row->label), name);
430

431
	g_free (name);
432
}
433

434 435 436 437 438 439 440 441 442 443 444
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))
445
	{
446 447 448 449 450 451
		row = l->data;

		if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
		{
			group_row_set_notebook_name (row);
		}
452
	}
453 454

	g_list_free (children);
455 456
}

457 458
static void
group_row_refresh_visibility (GeditDocumentsPanel *panel)
459
{
460
	gboolean notebook_is_unique;
461
	GtkWidget *first_group_row;
462

463 464
	notebook_is_unique = gedit_multi_notebook_get_n_notebooks (panel->priv->mnb) <= 1;
	first_group_row = GTK_WIDGET (get_first_notebook_found (panel));
465

466 467 468
	gtk_widget_set_no_show_all (first_group_row, notebook_is_unique);
	gtk_widget_set_visible (first_group_row, !notebook_is_unique);
}
469

470 471 472 473 474 475
static void
refresh_notebook (GeditDocumentsPanel *panel,
                  GeditNotebook       *notebook)
{
	GList *tabs;
	GList *l;
476

477
	tabs = gtk_container_get_children (GTK_CONTAINER (notebook));
Paolo Borelli's avatar
Paolo Borelli committed
478

479 480 481
	for (l = tabs; l != NULL; l = g_list_next (l))
	{
		GtkWidget *row;
482

483 484 485 486
		row = gedit_documents_document_row_new (panel, GEDIT_TAB (l->data));
		insert_row (panel, GTK_LIST_BOX (panel->priv->listbox), row, -1);
		panel->priv->nb_row_tab += 1;
	}
487

488 489
	g_list_free (tabs);
}
490

491 492 493 494 495
static void
refresh_notebook_foreach (GeditNotebook       *notebook,
                          GeditDocumentsPanel *panel)
{
	GtkWidget *row;
496

497 498 499 500 501 502
	row = gedit_documents_group_row_new (panel, notebook);
	insert_row (panel, GTK_LIST_BOX (panel->priv->listbox), row, -1);
	panel->priv->nb_row_notebook += 1;

	group_row_refresh_visibility (panel);
	refresh_notebook (panel, notebook);
503 504 505 506 507
}

static void
refresh_list (GeditDocumentsPanel *panel)
{
508 509 510 511 512 513 514
	GList *children;
	GList *l;

	/* Clear the listbox */
	children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));

	for (l = children; l != NULL; l = g_list_next (l))
515
	{
516
		gtk_widget_destroy (GTK_WIDGET (l->data));
517
	}
518

519 520 521 522 523 524
	g_list_free (children);

	gedit_multi_notebook_foreach_notebook (panel->priv->mnb,
	                                       (GtkCallback)refresh_notebook_foreach,
	                                       panel);
	select_active_tab (panel);
525 526
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
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;
	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);

	/* The status has as separate label to prevent ellipsizing */
	if (!gedit_document_get_readonly (doc))
	{
		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));
	}
}

602
static void
603 604 605 606
multi_notebook_tab_removed (GeditMultiNotebook  *mnb,
                            GeditNotebook       *notebook,
                            GeditTab            *tab,
                            GeditDocumentsPanel *panel)
607
{
608 609
	GtkListBoxRow *row;

610 611
	gedit_debug (DEBUG_PANEL);

612
	row = get_row_from_widget (panel, GTK_WIDGET (tab));
613 614 615 616 617 618 619

	/* 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);

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	gtk_widget_destroy (GTK_WIDGET (row));
	panel->priv->nb_row_tab -= 1;
}

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 */
636

637 638 639 640
	page_num = gtk_notebook_page_num (GTK_NOTEBOOK (notebook), GTK_WIDGET (tab));

	children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));
	item = g_list_find_custom (children, notebook, listbox_search_function);
641

642 643 644 645 646 647 648 649
	if (item)
	{
		res = 1 + page_num + g_list_position (children, item);
	}

	g_list_free (children);

	return res;
Paolo Borelli's avatar
Paolo Borelli committed
650 651 652
}

static void
653 654 655 656
multi_notebook_tab_added (GeditMultiNotebook  *mnb,
                          GeditNotebook       *notebook,
                          GeditTab            *tab,
                          GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
657
{
658 659
	gint position;
	GtkWidget *row;
Paolo Borelli's avatar
Paolo Borelli committed
660

661 662
	gedit_debug (DEBUG_PANEL);

663 664 665
	position = get_dest_position_for_tab (panel, notebook, tab);

	if (position == -1)
666
	{
667 668
		panel->priv->nb_row_tab = 0;
		panel->priv->nb_row_notebook = 0;
Paolo Borelli's avatar
Paolo Borelli committed
669

670
		refresh_list (panel);
671
	}
672 673 674 675 676
	else
	{
		/* Add a new tab's row to the listbox */
		row = gedit_documents_document_row_new (panel, tab);
		insert_row (panel, GTK_LIST_BOX (panel->priv->listbox), row, position);
677

678 679 680
		panel->priv->nb_row_tab += 1;
		row_select (panel, GTK_LIST_BOX (panel->priv->listbox), GTK_LIST_BOX_ROW (row));
	}
Paolo Borelli's avatar
Paolo Borelli committed
681 682 683
}

static void
684 685 686
multi_notebook_notebook_removed (GeditMultiNotebook  *mnb,
                                 GeditNotebook       *notebook,
                                 GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
687
{
688 689
	GtkListBoxRow *row;

690 691
	gedit_debug (DEBUG_PANEL);

692
	row = get_row_from_widget (panel, GTK_WIDGET (notebook));
693
	gtk_container_remove (GTK_CONTAINER (panel->priv->listbox), GTK_WIDGET (row));
694

695
	panel->priv->nb_row_notebook -= 1;
Paolo Borelli's avatar
Paolo Borelli committed
696

697 698
	group_row_refresh_visibility (panel);
	group_row_update_names (panel, panel->priv->listbox);
Paolo Borelli's avatar
Paolo Borelli committed
699 700 701
}

static void
702 703 704 705
row_move (GeditDocumentsPanel *panel,
          GeditNotebook       *notebook,
          GtkWidget           *tab,
          GtkWidget           *row)
Paolo Borelli's avatar
Paolo Borelli committed
706
{
707
	gint position;
708

709
	g_object_ref (row);
Paolo Borelli's avatar
Paolo Borelli committed
710

711 712
	gtk_container_remove (GTK_CONTAINER (panel->priv->listbox), GTK_WIDGET (row));
	position = get_dest_position_for_tab (panel, notebook, GEDIT_TAB (tab));
713

714 715 716 717 718 719 720 721 722
	g_signal_handler_block (panel->priv->listbox,
	                        panel->priv->selection_changed_handler_id);

	gtk_list_box_insert (GTK_LIST_BOX (panel->priv->listbox), row, position);

	g_object_unref (row);

	g_signal_handler_unblock (GTK_LIST_BOX (panel->priv->listbox),
	                          panel->priv->selection_changed_handler_id);
723 724
}

Paolo Borelli's avatar
Paolo Borelli committed
725
static void
726 727 728 729 730
multi_notebook_tabs_reordered (GeditMultiNotebook  *mnb,
                               GeditNotebook       *notebook,
                               GtkWidget           *page,
                               gint                 page_num,
                               GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
731
{
732 733
	GtkListBoxRow *row;

734 735
	gedit_debug (DEBUG_PANEL);

736
	row = get_row_from_widget (panel, GTK_WIDGET (page));
737

738 739 740
	row_move (panel, notebook, page, GTK_WIDGET (row));

	row_select (panel, GTK_LIST_BOX (panel->priv->listbox), GTK_LIST_BOX_ROW (row));
741
}
Paolo Borelli's avatar
Paolo Borelli committed
742 743 744

static void
set_window (GeditDocumentsPanel *panel,
745
            GeditWindow         *window)
Paolo Borelli's avatar
Paolo Borelli committed
746
{
747
	panel->priv->window = g_object_ref (window);
748
	panel->priv->mnb = GEDIT_MULTI_NOTEBOOK (_gedit_window_get_multi_notebook (window));
Paolo Borelli's avatar
Paolo Borelli committed
749

750
	g_signal_connect (panel->priv->mnb,
751 752 753
	                  "notebook-removed",
	                  G_CALLBACK (multi_notebook_notebook_removed),
	                  panel);
754
	g_signal_connect (panel->priv->mnb,
755 756 757
	                  "tab-added",
	                  G_CALLBACK (multi_notebook_tab_added),
	                  panel);
758
	g_signal_connect (panel->priv->mnb,
759 760 761
	                  "tab-removed",
	                  G_CALLBACK (multi_notebook_tab_removed),
	                  panel);
762
	g_signal_connect (panel->priv->mnb,
763 764 765 766 767 768 769 770 771 772
	                  "page-reordered",
	                  G_CALLBACK (multi_notebook_tabs_reordered),
	                  panel);

	panel->priv->tab_switched_handler_id = g_signal_connect (panel->priv->mnb,
	                                                         "switch-tab",
	                                                         G_CALLBACK (multi_notebook_tab_switched),
	                                                         panel);

	panel->priv->first_selection = TRUE;
773 774

	refresh_list (panel);
775
	group_row_refresh_visibility (panel);
Paolo Borelli's avatar
Paolo Borelli committed
776 777
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
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 */

	if (panel->priv->first_selection)
	{
		panel->priv->first_selection = FALSE;
		group_row_refresh_visibility (panel);
	}

798 799 800
	g_signal_handler_block (panel->priv->mnb,
	                        panel->priv->tab_switched_handler_id);

801 802 803 804
	if (GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
	{
		gedit_multi_notebook_set_active_tab (panel->priv->mnb,
		                                     GEDIT_TAB (GEDIT_DOCUMENTS_DOCUMENT_ROW (row)->ref));
805 806

		panel->priv->current_selection = GTK_WIDGET (row);
807
	}
808
	else if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row) && panel->priv->current_selection)
Paolo Borelli's avatar
Paolo Borelli committed
809
	{
810 811 812 813
		row_select (panel,
		            GTK_LIST_BOX (panel->priv->listbox),
		            GTK_LIST_BOX_ROW (panel->priv->current_selection));

814 815 816 817
	}
	else
	{
		g_assert_not_reached ();
818
	}
819 820 821

	g_signal_handler_unblock (panel->priv->mnb,
	                          panel->priv->tab_switched_handler_id);
Paolo Borelli's avatar
Paolo Borelli committed
822 823 824 825
}

static void
gedit_documents_panel_set_property (GObject      *object,
826 827 828
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
Paolo Borelli's avatar
Paolo Borelli committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
{
	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,
846 847 848
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
Paolo Borelli's avatar
Paolo Borelli committed
849 850 851 852 853 854
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);

	switch (prop_id)
	{
		case PROP_WINDOW:
855
			g_value_set_object (value, panel->priv->window);
Paolo Borelli's avatar
Paolo Borelli committed
856
			break;
857

Paolo Borelli's avatar
Paolo Borelli committed
858 859
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860
			break;
Paolo Borelli's avatar
Paolo Borelli committed
861 862 863 864 865 866
	}
}

static void
gedit_documents_panel_finalize (GObject *object)
{
867
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	g_signal_handlers_disconnect_by_func (panel->priv->mnb,
	                                      G_CALLBACK (multi_notebook_notebook_removed),
	                                      panel);
	g_signal_handlers_disconnect_by_func (panel->priv->mnb,
	                                      G_CALLBACK (multi_notebook_tab_added),
	                                      panel);
	g_signal_handlers_disconnect_by_func (panel->priv->mnb,
	                                      G_CALLBACK (multi_notebook_tab_removed),
	                                      panel);
	g_signal_handlers_disconnect_by_func (panel->priv->mnb,
	                                      G_CALLBACK (multi_notebook_tabs_reordered),
	                                      panel);
	g_signal_handlers_disconnect_by_func (panel->priv->mnb,
	                                      G_CALLBACK (multi_notebook_tab_switched),
	                                      panel);
Paolo Borelli's avatar
Paolo Borelli committed
884 885 886 887

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

888 889 890 891 892
static void
gedit_documents_panel_dispose (GObject *object)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (object);

893
	g_clear_object (&panel->priv->window);
894

895 896 897 898 899 900
	if (panel->priv->source_targets)
	{
		gtk_target_list_unref (panel->priv->source_targets);
		panel->priv->source_targets = NULL;
	}

901 902 903
	G_OBJECT_CLASS (gedit_documents_panel_parent_class)->dispose (object);
}

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 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 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
static GtkWidget *
create_placerholder_row (gint height)
{
	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);
	GeditDocumentsPanelPrivate *priv = panel->priv;
	GtkWidget *drag_document_row;
	GtkAllocation allocation;
	const gchar *name;
	GtkWidget *label;
	gint width, height;
	GtkWidget *image_box;
	GtkWidget *box;
	GtkStyleContext *style_context;

	drag_document_row = priv->drag_document_row;
	gtk_widget_get_allocation (drag_document_row, &allocation);
	gtk_widget_hide (drag_document_row);

	priv->document_row_height = allocation.height;

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

	label = gtk_label_new (name);
	gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
	gtk_widget_set_halign (label, GTK_ALIGN_START);
	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);

	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);

	priv->dnd_window = gtk_window_new (GTK_WINDOW_POPUP);
	gtk_widget_set_size_request (priv->dnd_window, allocation.width, allocation.height);
	gtk_window_set_screen (GTK_WINDOW (priv->dnd_window),
	                       gtk_widget_get_screen (drag_document_row));

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

	gtk_container_add (GTK_CONTAINER (priv->dnd_window), box);
	gtk_widget_show_all (priv->dnd_window);
	gtk_widget_set_opacity (priv->dnd_window, 0.8);

	gtk_drag_set_icon_widget (context,
	                          priv->dnd_window,
	                          priv->drag_document_row_x,
	                          priv->drag_document_row_y);
}

static gboolean
panel_on_drag_motion (GtkWidget      *widget,
                      GdkDragContext *context,
                      gint            x,
                      gint            y,
                      guint           time)
{
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
	GeditDocumentsPanelPrivate *priv = panel->priv;
	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;
	}

	gtk_widget_translate_coordinates (widget, priv->listbox,
	                                  x, y,
	                                  &dest_x, &dest_y);

	generic_row = (GeditDocumentsGenericRow *)gtk_list_box_get_row_at_y (GTK_LIST_BOX (priv->listbox), dest_y);
	source_panel = gtk_drag_get_source_widget (context);

	if (!priv->row_placeholder)
	{
		if (!generic_row)
		{
			/* We don't have a row height to use, so use the source one */
			priv->document_row_height = GEDIT_DOCUMENTS_PANEL (source_panel)->priv->document_row_height;
		}
		else
		{
			GtkAllocation allocation;

			gtk_widget_get_allocation (GTK_WIDGET (generic_row), &allocation);
			priv->document_row_height = allocation.height;
		}

		priv->row_placeholder = create_placerholder_row (priv->document_row_height);
		gtk_widget_show (priv->row_placeholder);
		g_object_ref_sink (priv->row_placeholder);
	}
	else if (GTK_WIDGET (generic_row) == priv->row_placeholder)
	{
		/* 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 */
		GList *children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));

		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);

		if (dest_y <= priv->document_row_height / 2 && row_index > 0)
		{
			row_placeholder_index = row_index;
		}
		else
		{
			row_placeholder_index = row_index + 1;
		}
	}

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

	if (priv->row_placeholder_index != row_placeholder_index)
	{
		if (priv->row_placeholder_index != ROW_OUTSIDE_LISTBOX)
		{
			gtk_container_remove (GTK_CONTAINER (priv->listbox),
			                      priv->row_placeholder);

			if (priv->row_placeholder_index < row_placeholder_index)
			{
				/* Ajustment because of existing placeholder row */
				row_placeholder_index -= 1;
			}
		}

		priv->row_destination_index = priv->row_placeholder_index = row_placeholder_index;

		gtk_list_box_insert (GTK_LIST_BOX (priv->listbox),
		                     priv->row_placeholder,
		                     priv->row_placeholder_index);
	}

	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);
	GeditDocumentsPanelPrivate *priv = panel->priv;

	if (priv->row_placeholder_index != ROW_OUTSIDE_LISTBOX)
	{
		gtk_container_remove (GTK_CONTAINER (priv->listbox), priv->row_placeholder);
		priv->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
	}
}

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

	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))
	{
		gtk_widget_show (GEDIT_DOCUMENTS_PANEL (source_widget)->priv->drag_document_row);
	}

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

	priv->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
	return FALSE;
}

static void
panel_on_drag_data_get (GtkWidget        *widget,
                        GdkDragContext   *context,
                        GtkSelectionData *data,
                        guint             info,
                        guint             time)
{
1140 1141
	GeditDocumentsPanel *panel = GEDIT_DOCUMENTS_PANEL (widget);
	GeditDocumentsPanelPrivate *priv = panel->priv;
1142
	GdkAtom target = gtk_selection_data_get_target (data);
1143
	GdkAtom result;
1144 1145 1146 1147 1148 1149 1150 1151

	if (target == gdk_atom_intern_static_string ("GEDIT_DOCUMENTS_DOCUMENT_ROW"))
	{
		gtk_selection_data_set (data,
		                        target,
		                        8,
		                        (void*)&priv->drag_document_row,
		                        sizeof (gpointer));
1152
		return;
1153
	}
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

	result = gtk_drag_dest_find_target (widget, context, priv->source_targets);

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

		tab = GEDIT_TAB (GEDIT_DOCUMENTS_DOCUMENT_ROW (priv->drag_document_row)->ref);
		doc = gedit_tab_get_document (tab);

		if (!gedit_document_is_untitled (doc))
		{
			GFile *file = gedit_document_get_location (doc);
			full_name = g_file_get_parse_name (file);
			g_object_unref (file);

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

	gtk_widget_show (priv->drag_document_row);
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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
}

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

	GList *children = gtk_container_get_children (GTK_CONTAINER (panel->priv->listbox));
	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);
	GeditDocumentsPanelPrivate *priv = panel->priv;
	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 ||
		    (priv->row_destination_index != source_index &&
		    priv->row_destination_index != source_index + 1))
		{
			GeditNotebook *old_notebook, *new_notebook;
			gint position;

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

			old_notebook = gedit_multi_notebook_get_notebook_for_tab (source_panel->priv->mnb, tab);
			new_notebook = get_notebook_and_position_from_document_row (panel,
			                                                            priv->row_destination_index,
			                                                            &position);
			if (old_notebook == new_notebook)
			{
				gtk_widget_show (*source_row);

				gtk_notebook_reorder_child (GTK_NOTEBOOK (new_notebook),
				                            GTK_WIDGET (tab),
				                            position + priv->row_source_row_offset);
			}
			else
			{
				gedit_notebook_move_tab (old_notebook, new_notebook, tab, position);
			}
1277 1278 1279 1280 1281 1282 1283

			if (tab != gedit_multi_notebook_get_active_tab (panel->priv->mnb))
			{
				g_signal_handler_block (panel->priv->mnb, panel->priv->tab_switched_handler_id);
				gedit_multi_notebook_set_active_tab (panel->priv->mnb, tab);
				g_signal_handler_unblock (panel->priv->mnb, panel->priv->tab_switched_handler_id);
			}
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
		}

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

	priv->row_destination_index = priv->row_placeholder_index = ROW_OUTSIDE_LISTBOX;

	if (priv->row_placeholder)
	{
		gtk_widget_destroy (priv->row_placeholder);
		priv->row_placeholder = NULL;
	}
}

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

	priv->drag_document_row = NULL;
	priv->is_on_drag = FALSE;

	gtk_widget_destroy (priv->dnd_window);
	priv->dnd_window = NULL;
}

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))
	{
		gtk_widget_show (GEDIT_DOCUMENTS_PANEL (source_widget)->priv->drag_document_row);
	}

	return FALSE;
}

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

	if (priv->drag_document_row == NULL || priv->is_on_drag)
	{
		return FALSE;
	}

	if (!(event->state & GDK_BUTTON1_MASK))
	{
		priv->drag_document_row = NULL;

		return FALSE;
	}

	if (gtk_drag_check_threshold (widget,
	                              priv->drag_root_x, priv->drag_root_y,
	                              event->x_root, event->y_root))
	{
		priv->is_on_drag = TRUE;

		gtk_drag_begin_with_coordinates (widget, priv->source_targets, GDK_ACTION_MOVE,
		                                 GDK_BUTTON_PRIMARY, (GdkEvent*)event,
		                                 -1, -1);
	}

	return FALSE;
}

1364
static void
Paolo Borelli's avatar
Paolo Borelli committed
1365 1366 1367
gedit_documents_panel_class_init (GeditDocumentsPanelClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
1368
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
Paolo Borelli's avatar
Paolo Borelli committed
1369 1370

	object_class->finalize = gedit_documents_panel_finalize;
1371
	object_class->dispose = gedit_documents_panel_dispose;
Paolo Borelli's avatar
Paolo Borelli committed
1372 1373 1374
	object_class->get_property = gedit_documents_panel_get_property;
	object_class->set_property = gedit_documents_panel_set_property;

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	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;

Paolo Borelli's avatar
Paolo Borelli committed
1386
	g_object_class_install_property (object_class,
1387 1388 1389 1390 1391 1392 1393 1394
	                                 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));
Paolo Borelli's avatar
Paolo Borelli committed
1395 1396
}

1397 1398
static void
gedit_documents_panel_init (GeditDocumentsPanel *panel)
Paolo Borelli's avatar
Paolo Borelli committed
1399
{
1400 1401
	GtkWidget *sw;
	GtkStyleContext *context;
1402 1403

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

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
	panel->priv = gedit_documents_panel_get_instance_private (panel);

	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 */
	panel->priv->listbox = gtk_list_box_new ();
Paolo Borelli's avatar
Paolo Borelli committed
1421

1422
	gtk_container_add (GTK_CONTAINER (sw), panel->priv->listbox);
Paolo Borelli's avatar
Paolo Borelli committed
1423

1424 1425 1426 1427 1428 1429 1430
	panel->priv->adjustment = gtk_list_box_get_adjustment (GTK_LIST_BOX (panel->priv->listbox));

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

	/* Css style */
	context = gtk_widget_get_style_context (panel->priv->listbox);
1431
	gtk_style_context_add_class (context, "gedit-document-panel");
1432 1433 1434 1435 1436 1437

	panel->priv->selection_changed_handler_id = g_signal_connect (panel->priv->listbox,
	                                                              "row-selected",
	                                                              G_CALLBACK (listbox_selection_changed),
	                                                              panel);
	panel->priv->is_in_tab_switched = FALSE;
1438
	panel->priv->current_selection = NULL;
1439 1440
	panel->priv->nb_row_notebook = 0;
	panel->priv->nb_row_tab = 0;
1441 1442 1443

	/* Drag and drop support */
	panel->priv->source_targets = gtk_target_list_new (panel_targets, G_N_ELEMENTS (panel_targets));
1444
	gtk_target_list_add_text_targets (panel->priv->source_targets, 0);
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457

	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);

	panel->priv->drag_document_row = NULL;
	panel->priv->row_placeholder = NULL;
	panel->priv->row_placeholder_index = ROW_OUTSIDE_LISTBOX;
	panel->priv->row_destination_index = ROW_OUTSIDE_LISTBOX;
	panel->priv->row_source_row_offset = 0;
	panel->priv->is_on_drag = FALSE;
Paolo Borelli's avatar
Paolo Borelli committed
1458 1459
}

1460 1461
GtkWidget *
gedit_documents_panel_new (GeditWindow *window)
Paolo Borelli's avatar
Paolo Borelli committed
1462
{
1463
	g_return_val_if_fail (GEDIT_IS_WINDOW (window), NULL);
Paolo Borelli's avatar
Paolo Borelli committed
1464

1465 1466 1467 1468
	return g_object_new (GEDIT_TYPE_DOCUMENTS_PANEL,
	                     "window", window,
	                     NULL);
}
1469

1470 1471
static void
row_on_close_button_clicked (GtkWidget *close_button,
1472 1473
                             GtkWidget *row)
{
1474 1475
	GeditDocumentsGenericRow *generic_row = (GeditDocumentsGenericRow *)row;
	GeditWindow *window = generic_row->panel->priv->window;
1476
	GtkWidget *ref;
Paolo Borelli's avatar
Paolo Borelli committed
1477

1478
	if (GEDIT_IS_DOCUMENTS_GROUP_ROW (row))
1479
	{
1480
		ref = GEDIT_DOCUMENTS_GROUP_ROW (row)->ref;
1481
		_gedit_cmd_file_close_notebook (window, GEDIT_NOTEBOOK (ref));
1482 1483
	}
	else if (GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
1484
	{
1485
		ref = GEDIT_DOCUMENTS_DOCUMENT_ROW (row)->ref;
1486
		_gedit_cmd_file_close_tab (GEDIT_TAB (ref), window);
1487 1488 1489 1490
	}
	else
	{
		g_assert_not_reached ();
1491
	}
Paolo Borelli's avatar
Paolo Borelli committed
1492 1493 1494
}

static gboolean
1495 1496 1497
row_on_button_pressed (GtkWidget      *row_event_box,
                       GdkEventButton *event,
                       GtkWidget      *row)
Paolo Borelli's avatar
Paolo Borelli committed
1498
{
1499
	if (gdk_event_get_event_type ((GdkEvent *)event) == GDK_BUTTON_PRESS &&
1500
	    GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
1501
	{
1502 1503
		GeditDocumentsDocumentRow *document_row = GEDIT_DOCUMENTS_DOCUMENT_ROW (row);
		GeditDocumentsPanelPrivate *panel_priv = document_row->panel->priv;
1504

1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
		if (event->button == GDK_BUTTON_PRIMARY)
		{
			/* memorize row and clicked position for possible drag'n drop */
			panel_priv->drag_document_row = row;
			panel_priv->drag_document_row_x = (gint)event->x;
			panel_priv->drag_document_row_y = (gint)event->y;

			panel_priv->drag_root_x = event->x_root;
			panel_priv->drag_root_y = event->y_root;

			return FALSE;
		}

		panel_priv->drag_document_row = NULL;

		if (gdk_event_triggers_context_menu ((GdkEvent *)event))
		{
			GeditWindow *window = panel_priv->window;
			GeditTab *tab = GEDIT_TAB (document_row->ref);
			GtkWidget *menu = gedit_notebook_popup_menu_new (window, tab);

			gtk_menu_popup_for_device (GTK_MENU (menu),
			                           gdk_event_get_device ((GdkEvent *)event),
			                           NULL, NULL,
			                           NULL, NULL, NULL,
			                           event->button,
			                           event->time);

			return TRUE;
		}
1535 1536 1537 1538 1539 1540 1541 1542 1543
	}

	return FALSE;
}

static void
document_row_create_header (GtkWidget *row)
{
	GeditDocumentsDocumentRow *document_row = GEDIT_DOCUMENTS_DOCUMENT_ROW (row);
1544 1545
	GtkWidget *image_box;
	gint width, height;
1546

1547 1548 1549 1550
	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);
1551 1552 1553

	document_row->image = gtk_image_new ();

1554
	gtk_container_add (GTK_CONTAINER (image_box), document_row->image);
1555

1556 1557
	gtk_box_pack_start (GTK_BOX (document_row->box),
	                    image_box, FALSE, FALSE, 0);
1558 1559

	/* Set the header on front of all other widget in the row */
1560 1561
	gtk_box_reorder_child (GTK_BOX (document_row->box),
	                       image_box, 0);
1562

1563
	gtk_widget_show_all (image_box);
Paolo Borelli's avatar
Paolo Borelli committed
1564 1565
}

1566 1567
static GtkWidget *
row_create (GtkWidget *row)
Paolo Borelli's avatar
Paolo Borelli committed
1568
{
1569
	GeditDocumentsGenericRow *generic_row = (GeditDocumentsGenericRow *)row;
1570
	GtkWidget *event_box;
1571 1572

	gedit_debug (DEBUG_PANEL);
1573

1574 1575
	event_box = gtk_event_box_new ();
	generic_row->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
1576

1577
	gtk_container_add (GTK_CONTAINER (event_box), generic_row->box);
1578 1579 1580

	generic_row->label = gtk_label_new (NULL);
	gtk_label_set_ellipsize (GTK_LABEL (generic_row->label), PANGO_ELLIPSIZE_END);
1581 1582
	gtk_widget_set_halign (generic_row->label, GTK_ALIGN_START);
	gtk_misc_set_alignment (GTK_MISC (generic_row->label), 0.0, 0.5);
1583

1584 1585 1586
	generic_row->status_label = gtk_label_new (NULL);
	gtk_widget_set_halign (generic_row->status_label, GTK_ALIGN_END);
	gtk_misc_set_alignment (GTK_MISC (generic_row->status_label), 1.0, 0.5);
1587

1588
	generic_row->close_button = gedit_close_button_new ();
1589

1590
	gtk_box_pack_start (GTK_BOX (generic_row->box),
1591
	                    generic_row->label, FALSE, FALSE, 0);
1592 1593
	gtk_box_pack_start (GTK_BOX (generic_row->box),
	                    generic_row->status_label, FALSE, FALSE, 0);
1594

1595 1596
	gtk_box_pack_end (GTK_BOX (generic_row->box),
	                  generic_row->close_button, FALSE, FALSE, 0);
1597

1598
	g_signal_connect (event_box,
1599 1600 1601
	                  "button-press-event",
	                  G_CALLBACK (row_on_button_pressed),
	                  row);
1602 1603
	g_signal_connect (generic_row->close_button,
	                  "clicked",
1604 1605 1606
	                  G_CALLBACK (row_on_close_button_clicked),
	                  row);

1607 1608
	gtk_widget_set_no_show_all (generic_row->status_label, TRUE);
	gtk_widget_show_all (event_box);
1609

1610
	return event_box;
Paolo Borelli's avatar
Paolo Borelli committed
1611 1612
}

1613
static gboolean
1614 1615 1616 1617 1618
document_row_query_tooltip (GtkWidget   *row,
                            gint         x,
                            gint         y,
                            gboolean     keyboard_tip,
                            GtkTooltip  *tooltip)
1619
{
1620 1621
	GeditDocumentsGenericRow *generic_row = (GeditDocumentsGenericRow *)row;
	gchar *markup;
1622

1623
	if (!GEDIT_IS_DOCUMENTS_DOCUMENT_ROW (row))
1624
	{
1625
		return FALSE;
1626 1627
	}

1628 1629
	markup = _gedit_tab_get_tooltip (GEDIT_TAB (generic_row->ref));
	gtk_tooltip_set_markup (tooltip, markup);
1630

1631
	g_free (markup);
1632 1633 1634 1635

	return TRUE;
}

1636 1637 1638 1639
/* Gedit Document Row */
static void
gedit_documents_document_row_class_init (GeditDocumentsDocumentRowClass *klass)
{
1640 1641
}

1642
static void
1643
gedit_documents_document_row_init (GeditDocumentsDocumentRow *row)