Merge pull request #3823 from ntrel/msg-tree-search
[geany-mirror.git] / src / notebook.c
blob4b36726943c4e1ec7b1736c336fce3d68ef2548a
1 /*
2 * notebook.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * Notebook tab Drag 'n' Drop reordering and tab management.
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "notebook.h"
31 #include "callbacks.h"
32 #include "documentprivate.h"
33 #include "geanyobject.h"
34 #include "keybindings.h"
35 #include "main.h"
36 #include "support.h"
37 #include "ui_utils.h"
38 #include "utils.h"
40 #include <gtk/gtk.h>
41 #include <gdk/gdkkeysyms.h>
44 #define GEANY_DND_NOTEBOOK_TAB_TYPE "geany_dnd_notebook_tab"
46 static const GtkTargetEntry drag_targets[] =
48 { (gchar *) GEANY_DND_NOTEBOOK_TAB_TYPE, GTK_TARGET_SAME_APP | GTK_TARGET_SAME_WIDGET, 0 }
51 static const GtkTargetEntry files_drop_targets[] = {
52 { (gchar *) "STRING", 0, 0 },
53 { (gchar *) "UTF8_STRING", 0, 0 },
54 { (gchar *) "text/plain", 0, 0 },
55 { (gchar *) "text/uri-list", 0, 0 }
58 static const gsize MAX_MRU_DOCS = 20;
59 static GQueue *mru_docs = NULL;
60 static guint mru_pos = 0;
62 static gboolean switch_in_progress = FALSE;
63 static GtkWidget *switch_dialog = NULL;
64 static GtkWidget *switch_dialog_label = NULL;
67 static void
68 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
69 gpointer user_data);
71 static void
72 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
73 gint x, gint y, GtkSelectionData *data, guint target_type,
74 guint event_time, gpointer user_data);
76 static void
77 notebook_tab_close_clicked_cb(GtkButton *button, gpointer user_data);
79 static void setup_tab_dnd(void);
82 static void update_mru_docs_head(GeanyDocument *doc)
84 if (doc)
86 g_queue_remove(mru_docs, doc);
87 g_queue_push_head(mru_docs, doc);
89 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
90 g_queue_pop_tail(mru_docs);
95 /* before the tab changes, add the current document to the MRU list */
96 static void on_notebook_switch_page(GtkNotebook *notebook,
97 gpointer page, guint page_num, gpointer user_data)
99 GeanyDocument *new;
101 new = document_get_from_page(page_num);
103 /* insert the very first document (when adding the second document
104 * and switching to it) */
105 if (g_queue_get_length(mru_docs) == 0 && gtk_notebook_get_n_pages(notebook) == 2)
106 update_mru_docs_head(document_get_current());
108 if (!switch_in_progress)
109 update_mru_docs_head(new);
113 static void on_document_close(GObject *obj, GeanyDocument *doc)
115 if (! main_status.quitting)
117 g_queue_remove(mru_docs, doc);
118 /* this prevents the pop up window from showing when there's a single
119 * document */
120 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 2)
121 g_queue_clear(mru_docs);
126 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
128 GtkWidget *dialog;
130 dialog = gtk_window_new(GTK_WINDOW_POPUP);
132 if (parent)
134 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
135 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
137 gtk_window_set_title(GTK_WINDOW(dialog), title);
138 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
139 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
141 gtk_widget_set_name(dialog, "GeanyDialog");
142 return dialog;
146 static gboolean is_modifier_key(guint keyval)
148 switch (keyval)
150 case GDK_KEY_Shift_L:
151 case GDK_KEY_Shift_R:
152 case GDK_KEY_Control_L:
153 case GDK_KEY_Control_R:
154 case GDK_KEY_Meta_L:
155 case GDK_KEY_Meta_R:
156 case GDK_KEY_Alt_L:
157 case GDK_KEY_Alt_R:
158 case GDK_KEY_Super_L:
159 case GDK_KEY_Super_R:
160 case GDK_KEY_Hyper_L:
161 case GDK_KEY_Hyper_R:
162 return TRUE;
163 default:
164 return FALSE;
169 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
171 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
172 if (switch_in_progress && is_modifier_key(ev->keyval))
174 GeanyDocument *doc;
176 switch_in_progress = FALSE;
178 if (switch_dialog)
180 gtk_widget_destroy(switch_dialog);
181 switch_dialog = NULL;
184 doc = document_get_current();
185 update_mru_docs_head(doc);
186 mru_pos = 0;
187 document_check_disk_status(doc, TRUE);
189 return FALSE;
193 static GtkWidget *create_switch_dialog(void)
195 GtkWidget *dialog, *widget, *vbox;
197 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
198 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
199 gtk_window_set_default_size(GTK_WINDOW(dialog), 200, -1);
201 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
202 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
203 gtk_container_add(GTK_CONTAINER(dialog), vbox);
205 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
206 gtk_container_add(GTK_CONTAINER(vbox), widget);
208 widget = gtk_label_new(NULL);
209 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
210 gtk_container_add(GTK_CONTAINER(vbox), widget);
211 switch_dialog_label = widget;
213 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
214 return dialog;
218 static void update_filename_label(void)
220 guint i;
221 guint queue_length;
222 GeanyDocument *doc;
223 GString *markup = g_string_new(NULL);
225 if (!switch_dialog)
227 switch_dialog = create_switch_dialog();
228 gtk_widget_show_all(switch_dialog);
231 queue_length = g_queue_get_length(mru_docs);
232 for (i = mru_pos; (i <= mru_pos + 3) && (doc = g_queue_peek_nth(mru_docs, i % queue_length)); i++)
234 gchar *basename;
236 basename = g_path_get_basename(DOC_FILENAME(doc));
237 SETPTR(basename, g_markup_escape_text(basename, -1));
239 if (i == mru_pos)
240 g_string_printf(markup, "<b>%s</b>", basename);
241 else if (i % queue_length == mru_pos) /* && i != mru_pos */
243 /* We have wrapped around and got to the starting document again */
244 g_free(basename);
245 break;
247 else
249 g_string_append(markup, "\n");
250 if (doc->changed)
251 SETPTR(basename, g_strconcat("<span color='red'>", basename, "</span>", NULL));
252 g_string_append(markup, basename);
254 g_free(basename);
256 gtk_label_set_markup(GTK_LABEL(switch_dialog_label), markup->str);
257 g_string_free(markup, TRUE);
261 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
263 if (!switch_in_progress || switch_dialog)
265 return FALSE;
268 update_filename_label();
269 return FALSE;
273 void notebook_switch_tablastused(void)
275 GeanyDocument *last_doc;
276 gboolean switch_start = !switch_in_progress;
278 mru_pos += 1;
279 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
281 if (! DOC_VALID(last_doc))
283 utils_beep();
284 mru_pos = 0;
285 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
287 if (! DOC_VALID(last_doc))
288 return;
290 switch_in_progress = TRUE;
291 document_show_tab(last_doc);
293 /* if there's a modifier key, we can switch back in MRU order each time unless
294 * the key is released */
295 if (switch_start)
296 g_timeout_add(600, on_switch_timeout, NULL);
297 else
298 update_filename_label();
302 gboolean notebook_switch_in_progress(void)
304 return switch_in_progress;
308 static gboolean focus_sci(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
310 GeanyDocument *doc = document_get_current();
312 if (doc != NULL && event->button == 1)
313 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
315 return FALSE;
319 static gboolean gtk_notebook_show_arrows(GtkNotebook *notebook)
321 return gtk_notebook_get_scrollable(notebook);
322 #if 0
323 /* To get this working we would need to define at least the first two fields of
324 * GtkNotebookPage since it is a private field. The better way would be to
325 * subclass GtkNotebook.
326 struct _FakeGtkNotebookPage
328 GtkWidget *child;
329 GtkWidget *tab_label;
332 gboolean show_arrow = FALSE;
333 GList *children;
335 if (! notebook->scrollable)
336 return FALSE;
338 children = notebook->children;
339 while (children)
341 struct _FakeGtkNotebookPage *page = children->data;
343 if (page->tab_label && ! gtk_widget_get_child_visible(page->tab_label))
344 show_arrow = TRUE;
346 children = children->next;
348 return show_arrow;
349 #endif
353 static gboolean is_position_on_tab_bar(GtkNotebook *notebook, GdkEventButton *event)
355 GtkWidget *page;
356 GtkWidget *tab;
357 GtkWidget *nb;
358 GtkPositionType tab_pos;
359 gint scroll_arrow_hlength, scroll_arrow_vlength;
360 gdouble x, y;
362 page = gtk_notebook_get_nth_page(notebook, 0);
363 g_return_val_if_fail(page != NULL, FALSE);
365 tab = gtk_notebook_get_tab_label(notebook, page);
366 g_return_val_if_fail(tab != NULL, FALSE);
368 tab_pos = gtk_notebook_get_tab_pos(notebook);
369 nb = GTK_WIDGET(notebook);
371 gtk_widget_style_get(GTK_WIDGET(notebook), "scroll-arrow-hlength", &scroll_arrow_hlength,
372 "scroll-arrow-vlength", &scroll_arrow_vlength, NULL);
374 if (! gdk_event_get_coords((GdkEvent*) event, &x, &y))
376 x = event->x;
377 y = event->y;
380 switch (tab_pos)
382 case GTK_POS_TOP:
383 case GTK_POS_BOTTOM:
385 if (event->y >= 0 && event->y <= gtk_widget_get_allocated_height(tab))
387 if (! gtk_notebook_show_arrows(notebook) || (
388 x > scroll_arrow_hlength &&
389 x < gtk_widget_get_allocated_width(nb) - scroll_arrow_hlength))
390 return TRUE;
392 break;
394 case GTK_POS_LEFT:
395 case GTK_POS_RIGHT:
397 if (event->x >= 0 && event->x <= gtk_widget_get_allocated_width(tab))
399 if (! gtk_notebook_show_arrows(notebook) || (
400 y > scroll_arrow_vlength &&
401 y < gtk_widget_get_allocated_height(nb) - scroll_arrow_vlength))
402 return TRUE;
407 return FALSE;
411 static void tab_bar_menu_activate_cb(GtkMenuItem *menuitem, gpointer data)
413 GeanyDocument *doc = data;
415 if (! DOC_VALID(doc))
416 return;
418 document_show_tab(doc);
422 static void on_open_in_new_window_activate(GtkMenuItem *menuitem, gpointer user_data)
424 GeanyDocument *doc = user_data;
425 gchar *doc_path;
427 g_return_if_fail(doc->is_valid);
429 doc_path = utils_get_locale_from_utf8(doc->file_name);
430 utils_start_new_geany_instance(doc_path);
431 g_free(doc_path);
435 static gboolean has_tabs_on_right(GeanyDocument *doc)
437 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
438 gint total_pages = gtk_notebook_get_n_pages(nb);
439 gint doc_page = document_get_notebook_page(doc);
440 return total_pages > (doc_page + 1);
444 static void on_close_documents_right_activate(GtkMenuItem *menuitem, GeanyDocument *doc)
446 g_return_if_fail(has_tabs_on_right(doc));
447 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
448 gint current_page = gtk_notebook_get_current_page(nb);
449 gint doc_page = document_get_notebook_page(doc);
450 for (gint i = doc_page + 1; i < gtk_notebook_get_n_pages(nb); )
452 if (! document_close(document_get_from_page(i)))
453 i++; // only increment if tab wasn't closed
455 /* keep the current tab to the original one unless it has been closed, in
456 * which case use the activated one */
457 gtk_notebook_set_current_page(nb, MIN(current_page, doc_page));
461 static void show_tab_bar_popup_menu(GdkEventButton *event, GeanyDocument *doc)
463 GtkWidget *menu_item;
464 static GtkWidget *menu = NULL;
466 if (menu == NULL)
467 menu = gtk_menu_new();
469 /* clear the old menu items */
470 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) (void(*)(void)) gtk_widget_destroy, NULL);
472 ui_menu_add_document_items(GTK_MENU(menu), document_get_current(),
473 G_CALLBACK(tab_bar_menu_activate_cb));
475 menu_item = gtk_separator_menu_item_new();
476 gtk_widget_show(menu_item);
477 gtk_container_add(GTK_CONTAINER(menu), menu_item);
479 menu_item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in New _Window"));
480 gtk_widget_show(menu_item);
481 gtk_container_add(GTK_CONTAINER(menu), menu_item);
482 g_signal_connect(menu_item, "activate",
483 G_CALLBACK(on_open_in_new_window_activate), doc);
484 /* disable if not on disk */
485 if (doc == NULL || !doc->real_path)
486 gtk_widget_set_sensitive(menu_item, FALSE);
488 menu_item = gtk_separator_menu_item_new();
489 gtk_widget_show(menu_item);
490 gtk_container_add(GTK_CONTAINER(menu), menu_item);
492 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE, NULL);
493 gtk_widget_show(menu_item);
494 gtk_container_add(GTK_CONTAINER(menu), menu_item);
495 g_signal_connect(menu_item, "activate", G_CALLBACK(notebook_tab_close_clicked_cb), doc);
496 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
498 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Ot_her Documents"));
499 gtk_widget_show(menu_item);
500 gtk_container_add(GTK_CONTAINER(menu), menu_item);
501 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_other_documents1_activate), doc);
502 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
504 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Documents to the _Right"));
505 gtk_widget_show(menu_item);
506 gtk_container_add(GTK_CONTAINER(menu), menu_item);
507 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_documents_right_activate), doc);
508 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), doc != NULL && has_tabs_on_right(doc));
510 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("C_lose All"));
511 gtk_widget_show(menu_item);
512 gtk_container_add(GTK_CONTAINER(menu), menu_item);
513 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_all1_activate), NULL);
515 gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent *) event);
519 static gboolean notebook_tab_bar_click_cb(GtkWidget *widget, GdkEventButton *event,
520 gpointer user_data)
522 if (event->type == GDK_2BUTTON_PRESS)
524 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
525 GtkWidget *event_widget = gtk_get_event_widget((GdkEvent *) event);
526 GtkWidget *child = gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook));
528 /* ignore events from the content of the page (impl. stolen from GTK2 tab scrolling)
529 * TODO: we should also ignore notebook's action widgets, but that's more work and
530 * we don't have any of them yet anyway -- and GTK 2.16 don't have those actions. */
531 if (event_widget == NULL || event_widget == child || gtk_widget_is_ancestor(event_widget, child))
532 return FALSE;
534 if (is_position_on_tab_bar(notebook, event))
536 document_new_file(NULL, NULL, NULL);
537 return TRUE;
540 /* right-click is also handled here if it happened on the notebook tab bar but not
541 * on a tab directly */
542 else if (event->button == 3)
544 show_tab_bar_popup_menu(event, NULL);
545 return TRUE;
547 return FALSE;
551 static gboolean notebook_tab_bar_scroll_cb(GtkWidget *widget, GdkEventScroll *event)
553 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
554 GtkWidget *child;
556 child = gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook));
557 if (child == NULL)
558 return FALSE;
560 switch (event->direction)
562 case GDK_SCROLL_RIGHT:
563 case GDK_SCROLL_DOWN:
564 gtk_notebook_next_page(notebook);
565 break;
566 case GDK_SCROLL_LEFT:
567 case GDK_SCROLL_UP:
568 gtk_notebook_prev_page(notebook);
569 break;
570 default:
571 break;
574 return TRUE;
578 void notebook_init(void)
580 g_signal_connect_after(main_widgets.notebook, "button-press-event",
581 G_CALLBACK(notebook_tab_bar_click_cb), NULL);
583 g_signal_connect(main_widgets.notebook, "drag-data-received",
584 G_CALLBACK(on_window_drag_data_received), NULL);
586 mru_docs = g_queue_new();
587 g_signal_connect(main_widgets.notebook, "switch-page",
588 G_CALLBACK(on_notebook_switch_page), NULL);
589 g_signal_connect(geany_object, "document-close",
590 G_CALLBACK(on_document_close), NULL);
592 gtk_widget_add_events(main_widgets.notebook, GDK_SCROLL_MASK);
593 g_signal_connect(main_widgets.notebook, "scroll-event", G_CALLBACK(notebook_tab_bar_scroll_cb), NULL);
595 /* in case the switch dialog misses an event while drawing the dialog */
596 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
598 setup_tab_dnd();
602 void notebook_free(void)
604 g_queue_free(mru_docs);
608 static void setup_tab_dnd(void)
610 GtkWidget *notebook = main_widgets.notebook;
612 g_signal_connect(notebook, "page-reordered", G_CALLBACK(notebook_page_reordered_cb), NULL);
616 static void
617 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
618 gpointer user_data)
620 /* Not necessary to update open files treeview if it's sorted.
621 * Note: if enabled, it's best to move the item instead of recreating all items. */
622 /*sidebar_openfiles_update_all();*/
626 /* call this after the number of tabs in main_widgets.notebook changes. */
627 static void tab_count_changed(void)
629 switch (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)))
631 case 0:
632 /* Enables DnD for dropping files into the empty notebook widget */
633 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_ALL,
634 files_drop_targets, G_N_ELEMENTS(files_drop_targets),
635 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK);
636 break;
638 case 1:
639 /* Disables DnD for dropping files into the notebook widget and enables the DnD for moving file
640 * tabs. Files can still be dropped into the notebook widget because it will be handled by the
641 * active Scintilla Widget (only dropping to the tab bar is not possible but it should be ok) */
642 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
643 drag_targets, G_N_ELEMENTS(drag_targets), GDK_ACTION_MOVE);
644 break;
649 static gboolean notebook_tab_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
651 guint state;
652 GeanyDocument *doc = (GeanyDocument *) data;
654 /* toggle additional widgets on double click */
655 if (event->type == GDK_2BUTTON_PRESS)
657 if (interface_prefs.notebook_double_click_hides_widgets)
658 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
660 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
662 /* close tab on middle click */
663 if (event->button == 2)
665 document_close(doc);
666 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
668 /* switch last used tab on ctrl-click */
669 state = keybindings_get_modifiers(event->state);
670 if (event->button == 1 && state == GEANY_PRIMARY_MOD_MASK)
672 keybindings_send_command(GEANY_KEY_GROUP_NOTEBOOK,
673 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED);
674 return TRUE;
676 /* right-click is first handled here if it happened on a notebook tab */
677 if (event->button == 3)
679 show_tab_bar_popup_menu(event, doc);
680 return TRUE;
683 return FALSE;
687 static void notebook_tab_close_button_style_set(GtkWidget *btn, GtkRcStyle *prev_style,
688 gpointer data)
690 gint w, h;
692 gtk_icon_size_lookup_for_settings(gtk_widget_get_settings(btn), GTK_ICON_SIZE_MENU, &w, &h);
693 gtk_widget_set_size_request(btn, w + 2, h + 2);
697 /* Returns page number of notebook page, or -1 on error
699 * Note: the widget added to the notebook is *not* shown by this function, so you have to call
700 * something like `gtk_widget_show(document_get_notebook_child(doc))` when finished setting up the
701 * document. This is necessary because when the notebook tab is added, the document isn't ready
702 * yet, and we need the notebook to emit ::switch-page after it actually is. Actually this
703 * doesn't prevent the signal to me emitted straight when we insert the page (this looks like a
704 * GTK bug), but it emits it again when showing the child, and it's all we need. */
705 gint notebook_new_tab(GeanyDocument *this)
707 GtkWidget *hbox, *ebox, *vbox;
708 gint tabnum;
709 GtkWidget *page;
710 gint cur_page;
712 g_return_val_if_fail(this != NULL, -1);
714 /* page is packed into a vbox so we can stack infobars above it */
715 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
716 page = GTK_WIDGET(this->editor->sci);
717 gtk_box_pack_start(GTK_BOX(vbox), page, TRUE, TRUE, 0);
719 this->priv->tab_label = gtk_label_new(NULL);
721 /* get button press events for the tab label and the space between it and
722 * the close button, if any */
723 ebox = gtk_event_box_new();
724 gtk_widget_set_has_window(ebox, FALSE);
725 g_signal_connect(ebox, "button-press-event", G_CALLBACK(notebook_tab_click), this);
726 /* focus the current document after clicking on a tab */
727 g_signal_connect_after(ebox, "button-release-event",
728 G_CALLBACK(focus_sci), NULL);
730 /* switch tab by scrolling - GTK2 behaviour for GTK3 */
731 gtk_widget_add_events(GTK_WIDGET(ebox), GDK_SCROLL_MASK);
732 gtk_widget_add_events(GTK_WIDGET(this->priv->tab_label), GDK_SCROLL_MASK);
734 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
735 gtk_box_pack_start(GTK_BOX(hbox), this->priv->tab_label, FALSE, FALSE, 0);
736 gtk_container_add(GTK_CONTAINER(ebox), hbox);
738 if (file_prefs.show_tab_cross)
740 GtkWidget *image, *btn, *align;
742 btn = gtk_button_new();
743 gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
744 gtk_button_set_focus_on_click(GTK_BUTTON(btn), FALSE);
745 gtk_widget_set_name(btn, "geany-close-tab-button");
747 image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
748 gtk_container_add(GTK_CONTAINER(btn), image);
750 align = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
751 gtk_container_add(GTK_CONTAINER(align), btn);
752 gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0);
754 g_signal_connect(btn, "clicked", G_CALLBACK(notebook_tab_close_clicked_cb), this);
755 /* button overrides event box, so make middle click on button also close tab */
756 g_signal_connect(btn, "button-press-event", G_CALLBACK(notebook_tab_click), this);
757 /* handle style modification to keep button small as possible even when theme change */
758 g_signal_connect(btn, "style-set", G_CALLBACK(notebook_tab_close_button_style_set), NULL);
761 gtk_widget_show_all(ebox);
763 document_update_tab_label(this);
765 if (main_status.opening_session_files)
766 cur_page = -1; /* last page */
767 else if (file_prefs.tab_order_beside)
769 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
770 if (file_prefs.tab_order_ltr)
771 cur_page++;
773 else if (file_prefs.tab_order_ltr)
774 cur_page = -1; /* last page */
775 else
776 cur_page = 0;
778 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
779 ebox, NULL, cur_page);
781 tab_count_changed();
783 /* enable tab DnD */
784 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(main_widgets.notebook), vbox, TRUE);
786 return tabnum;
790 static void
791 notebook_tab_close_clicked_cb(GtkButton *button, gpointer data)
793 GeanyDocument *doc = (GeanyDocument *) data;
795 document_close(doc);
799 /* Always use this instead of gtk_notebook_remove_page(). */
800 void notebook_remove_page(gint page_num)
802 gint page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
804 if (page_num == page)
806 if (file_prefs.tab_order_ltr)
807 page += 1;
808 else if (page > 0) /* never go negative, it would select the last page */
809 page -= 1;
811 if (file_prefs.tab_close_switch_to_mru)
813 GeanyDocument *last_doc;
815 last_doc = g_queue_peek_nth(mru_docs, 0);
816 if (DOC_VALID(last_doc))
817 page = document_get_notebook_page(last_doc);
820 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
823 /* now remove the page (so we don't temporarily switch to the previous page) */
824 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
826 tab_count_changed();
830 static void
831 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
832 gint x, gint y, GtkSelectionData *data, guint target_type,
833 guint event_time, gpointer user_data)
835 gboolean success = FALSE;
836 gint length = gtk_selection_data_get_length(data);
838 if (length > 0 && gtk_selection_data_get_format(data) == 8)
840 document_open_file_list((const gchar *)gtk_selection_data_get_data(data), length);
842 success = TRUE;
844 gtk_drag_finish(drag_context, success, FALSE, event_time);