Update sinhala translation
[geany-mirror.git] / src / notebook.c
blob02216fa827aa07aec875eb36d59d10420cf583d8
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 {GEANY_DND_NOTEBOOK_TAB_TYPE, GTK_TARGET_SAME_APP | GTK_TARGET_SAME_WIDGET, 0}
51 static GtkTargetEntry files_drop_targets[] = {
52 { "STRING", 0, 0 },
53 { "UTF8_STRING", 0, 0 },
54 { "text/plain", 0, 0 },
55 { "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 gchar *msg = NULL;
222 guint queue_length;
223 GeanyDocument *doc;
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 if (i == mru_pos)
238 msg = g_markup_printf_escaped ("<b>%s</b>", basename);
239 else if (i % queue_length == mru_pos) /* && i != mru_pos */
241 /* We have wrapped around and got to the starting document again */
242 g_free(basename);
243 break;
245 else
247 SETPTR(basename, g_markup_printf_escaped ("\n%s", basename));
248 SETPTR(msg, g_strconcat(msg, basename, NULL));
250 g_free(basename);
252 gtk_label_set_markup(GTK_LABEL(switch_dialog_label), msg);
253 g_free(msg);
257 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
259 if (!switch_in_progress || switch_dialog)
261 return FALSE;
264 update_filename_label();
265 return FALSE;
269 void notebook_switch_tablastused(void)
271 GeanyDocument *last_doc;
272 gboolean switch_start = !switch_in_progress;
274 mru_pos += 1;
275 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
277 if (! DOC_VALID(last_doc))
279 utils_beep();
280 mru_pos = 0;
281 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
283 if (! DOC_VALID(last_doc))
284 return;
286 switch_in_progress = TRUE;
287 document_show_tab(last_doc);
289 /* if there's a modifier key, we can switch back in MRU order each time unless
290 * the key is released */
291 if (switch_start)
292 g_timeout_add(600, on_switch_timeout, NULL);
293 else
294 update_filename_label();
298 gboolean notebook_switch_in_progress(void)
300 return switch_in_progress;
304 static gboolean focus_sci(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
306 GeanyDocument *doc = document_get_current();
308 if (doc != NULL && event->button == 1)
309 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
311 return FALSE;
315 static gboolean gtk_notebook_show_arrows(GtkNotebook *notebook)
317 return gtk_notebook_get_scrollable(notebook);
318 #if 0
319 /* To get this working we would need to define at least the first two fields of
320 * GtkNotebookPage since it is a private field. The better way would be to
321 * subclass GtkNotebook.
322 struct _FakeGtkNotebookPage
324 GtkWidget *child;
325 GtkWidget *tab_label;
328 gboolean show_arrow = FALSE;
329 GList *children;
331 if (! notebook->scrollable)
332 return FALSE;
334 children = notebook->children;
335 while (children)
337 struct _FakeGtkNotebookPage *page = children->data;
339 if (page->tab_label && ! gtk_widget_get_child_visible(page->tab_label))
340 show_arrow = TRUE;
342 children = children->next;
344 return show_arrow;
345 #endif
349 static gboolean is_position_on_tab_bar(GtkNotebook *notebook, GdkEventButton *event)
351 GtkWidget *page;
352 GtkWidget *tab;
353 GtkWidget *nb;
354 GtkPositionType tab_pos;
355 gint scroll_arrow_hlength, scroll_arrow_vlength;
356 gdouble x, y;
358 page = gtk_notebook_get_nth_page(notebook, 0);
359 g_return_val_if_fail(page != NULL, FALSE);
361 tab = gtk_notebook_get_tab_label(notebook, page);
362 g_return_val_if_fail(tab != NULL, FALSE);
364 tab_pos = gtk_notebook_get_tab_pos(notebook);
365 nb = GTK_WIDGET(notebook);
367 gtk_widget_style_get(GTK_WIDGET(notebook), "scroll-arrow-hlength", &scroll_arrow_hlength,
368 "scroll-arrow-vlength", &scroll_arrow_vlength, NULL);
370 if (! gdk_event_get_coords((GdkEvent*) event, &x, &y))
372 x = event->x;
373 y = event->y;
376 switch (tab_pos)
378 case GTK_POS_TOP:
379 case GTK_POS_BOTTOM:
381 if (event->y >= 0 && event->y <= gtk_widget_get_allocated_height(tab))
383 if (! gtk_notebook_show_arrows(notebook) || (
384 x > scroll_arrow_hlength &&
385 x < gtk_widget_get_allocated_width(nb) - scroll_arrow_hlength))
386 return TRUE;
388 break;
390 case GTK_POS_LEFT:
391 case GTK_POS_RIGHT:
393 if (event->x >= 0 && event->x <= gtk_widget_get_allocated_width(tab))
395 if (! gtk_notebook_show_arrows(notebook) || (
396 y > scroll_arrow_vlength &&
397 y < gtk_widget_get_allocated_height(nb) - scroll_arrow_vlength))
398 return TRUE;
403 return FALSE;
407 static void tab_bar_menu_activate_cb(GtkMenuItem *menuitem, gpointer data)
409 GeanyDocument *doc = data;
411 if (! DOC_VALID(doc))
412 return;
414 document_show_tab(doc);
418 static void on_open_in_new_window_activate(GtkMenuItem *menuitem, gpointer user_data)
420 GeanyDocument *doc = user_data;
421 gchar *doc_path;
423 g_return_if_fail(doc->is_valid);
425 doc_path = utils_get_locale_from_utf8(doc->file_name);
426 utils_start_new_geany_instance(doc_path);
427 g_free(doc_path);
431 static gboolean has_tabs_on_right(GeanyDocument *doc)
433 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
434 gint total_pages = gtk_notebook_get_n_pages(nb);
435 gint doc_page = document_get_notebook_page(doc);
436 return total_pages > (doc_page + 1);
440 static void on_close_documents_right_activate(GtkMenuItem *menuitem, GeanyDocument *doc)
442 g_return_if_fail(has_tabs_on_right(doc));
443 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
444 gint current_page = gtk_notebook_get_current_page(nb);
445 gint doc_page = document_get_notebook_page(doc);
446 for (gint i = doc_page + 1; i < gtk_notebook_get_n_pages(nb); )
448 if (! document_close(document_get_from_page(i)))
449 i++; // only increment if tab wasn't closed
451 /* keep the current tab to the original one unless it has been closed, in
452 * which case use the activated one */
453 gtk_notebook_set_current_page(nb, MIN(current_page, doc_page));
457 static void show_tab_bar_popup_menu(GdkEventButton *event, GeanyDocument *doc)
459 GtkWidget *menu_item;
460 static GtkWidget *menu = NULL;
462 if (menu == NULL)
463 menu = gtk_menu_new();
465 /* clear the old menu items */
466 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
468 ui_menu_add_document_items(GTK_MENU(menu), document_get_current(),
469 G_CALLBACK(tab_bar_menu_activate_cb));
471 menu_item = gtk_separator_menu_item_new();
472 gtk_widget_show(menu_item);
473 gtk_container_add(GTK_CONTAINER(menu), menu_item);
475 menu_item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in New _Window"));
476 gtk_widget_show(menu_item);
477 gtk_container_add(GTK_CONTAINER(menu), menu_item);
478 g_signal_connect(menu_item, "activate",
479 G_CALLBACK(on_open_in_new_window_activate), doc);
480 /* disable if not on disk */
481 if (doc == NULL || !doc->real_path)
482 gtk_widget_set_sensitive(menu_item, FALSE);
484 menu_item = gtk_separator_menu_item_new();
485 gtk_widget_show(menu_item);
486 gtk_container_add(GTK_CONTAINER(menu), menu_item);
488 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE, NULL);
489 gtk_widget_show(menu_item);
490 gtk_container_add(GTK_CONTAINER(menu), menu_item);
491 g_signal_connect(menu_item, "activate", G_CALLBACK(notebook_tab_close_clicked_cb), doc);
492 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
494 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Ot_her Documents"));
495 gtk_widget_show(menu_item);
496 gtk_container_add(GTK_CONTAINER(menu), menu_item);
497 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_other_documents1_activate), doc);
498 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
500 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Documents to the _Right"));
501 gtk_widget_show(menu_item);
502 gtk_container_add(GTK_CONTAINER(menu), menu_item);
503 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_documents_right_activate), doc);
504 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), doc != NULL && has_tabs_on_right(doc));
506 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("C_lose All"));
507 gtk_widget_show(menu_item);
508 gtk_container_add(GTK_CONTAINER(menu), menu_item);
509 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_all1_activate), NULL);
511 ui_menu_popup(GTK_MENU(menu), NULL, NULL, event->button, event->time);
515 static gboolean notebook_tab_bar_click_cb(GtkWidget *widget, GdkEventButton *event,
516 gpointer user_data)
518 if (event->type == GDK_2BUTTON_PRESS)
520 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
521 GtkWidget *event_widget = gtk_get_event_widget((GdkEvent *) event);
522 GtkWidget *child = gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook));
524 /* ignore events from the content of the page (impl. stolen from GTK2 tab scrolling)
525 * TODO: we should also ignore notebook's action widgets, but that's more work and
526 * we don't have any of them yet anyway -- and GTK 2.16 don't have those actions. */
527 if (event_widget == NULL || event_widget == child || gtk_widget_is_ancestor(event_widget, child))
528 return FALSE;
530 if (is_position_on_tab_bar(notebook, event))
532 document_new_file(NULL, NULL, NULL);
533 return TRUE;
536 /* right-click is also handled here if it happened on the notebook tab bar but not
537 * on a tab directly */
538 else if (event->button == 3)
540 show_tab_bar_popup_menu(event, NULL);
541 return TRUE;
543 return FALSE;
547 static gboolean notebook_tab_bar_scroll_cb(GtkWidget *widget, GdkEventScroll *event)
549 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
550 GtkWidget *child;
552 child = gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook));
553 if (child == NULL)
554 return FALSE;
556 switch (event->direction)
558 case GDK_SCROLL_RIGHT:
559 case GDK_SCROLL_DOWN:
560 gtk_notebook_next_page(notebook);
561 break;
562 case GDK_SCROLL_LEFT:
563 case GDK_SCROLL_UP:
564 gtk_notebook_prev_page(notebook);
565 break;
566 default:
567 break;
570 return TRUE;
574 void notebook_init(void)
576 g_signal_connect_after(main_widgets.notebook, "button-press-event",
577 G_CALLBACK(notebook_tab_bar_click_cb), NULL);
579 g_signal_connect(main_widgets.notebook, "drag-data-received",
580 G_CALLBACK(on_window_drag_data_received), NULL);
582 mru_docs = g_queue_new();
583 g_signal_connect(main_widgets.notebook, "switch-page",
584 G_CALLBACK(on_notebook_switch_page), NULL);
585 g_signal_connect(geany_object, "document-close",
586 G_CALLBACK(on_document_close), NULL);
588 gtk_widget_add_events(main_widgets.notebook, GDK_SCROLL_MASK);
589 g_signal_connect(main_widgets.notebook, "scroll-event", G_CALLBACK(notebook_tab_bar_scroll_cb), NULL);
591 /* in case the switch dialog misses an event while drawing the dialog */
592 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
594 setup_tab_dnd();
598 void notebook_free(void)
600 g_queue_free(mru_docs);
604 static void setup_tab_dnd(void)
606 GtkWidget *notebook = main_widgets.notebook;
608 g_signal_connect(notebook, "page-reordered", G_CALLBACK(notebook_page_reordered_cb), NULL);
612 static void
613 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
614 gpointer user_data)
616 /* Not necessary to update open files treeview if it's sorted.
617 * Note: if enabled, it's best to move the item instead of recreating all items. */
618 /*sidebar_openfiles_update_all();*/
622 /* call this after the number of tabs in main_widgets.notebook changes. */
623 static void tab_count_changed(void)
625 switch (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)))
627 case 0:
628 /* Enables DnD for dropping files into the empty notebook widget */
629 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_ALL,
630 files_drop_targets, G_N_ELEMENTS(files_drop_targets),
631 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK);
632 break;
634 case 1:
635 /* Disables DnD for dropping files into the notebook widget and enables the DnD for moving file
636 * tabs. Files can still be dropped into the notebook widget because it will be handled by the
637 * active Scintilla Widget (only dropping to the tab bar is not possible but it should be ok) */
638 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
639 drag_targets, G_N_ELEMENTS(drag_targets), GDK_ACTION_MOVE);
640 break;
645 static gboolean notebook_tab_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
647 guint state;
648 GeanyDocument *doc = (GeanyDocument *) data;
650 /* toggle additional widgets on double click */
651 if (event->type == GDK_2BUTTON_PRESS)
653 if (interface_prefs.notebook_double_click_hides_widgets)
654 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
656 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
658 /* close tab on middle click */
659 if (event->button == 2)
661 document_close(doc);
662 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
664 /* switch last used tab on ctrl-click */
665 state = keybindings_get_modifiers(event->state);
666 if (event->button == 1 && state == GEANY_PRIMARY_MOD_MASK)
668 keybindings_send_command(GEANY_KEY_GROUP_NOTEBOOK,
669 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED);
670 return TRUE;
672 /* right-click is first handled here if it happened on a notebook tab */
673 if (event->button == 3)
675 show_tab_bar_popup_menu(event, doc);
676 return TRUE;
679 return FALSE;
683 static void notebook_tab_close_button_style_set(GtkWidget *btn, GtkRcStyle *prev_style,
684 gpointer data)
686 gint w, h;
688 gtk_icon_size_lookup_for_settings(gtk_widget_get_settings(btn), GTK_ICON_SIZE_MENU, &w, &h);
689 gtk_widget_set_size_request(btn, w + 2, h + 2);
693 /* Returns page number of notebook page, or -1 on error
695 * Note: the widget added to the notebook is *not* shown by this function, so you have to call
696 * something like `gtk_widget_show(document_get_notebook_child(doc))` when finished setting up the
697 * document. This is necessary because when the notebook tab is added, the document isn't ready
698 * yet, and we need the notebook to emit ::switch-page after it actually is. Actually this
699 * doesn't prevent the signal to me emitted straight when we insert the page (this looks like a
700 * GTK bug), but it emits it again when showing the child, and it's all we need. */
701 gint notebook_new_tab(GeanyDocument *this)
703 GtkWidget *hbox, *ebox, *vbox;
704 gint tabnum;
705 GtkWidget *page;
706 gint cur_page;
708 g_return_val_if_fail(this != NULL, -1);
710 /* page is packed into a vbox so we can stack infobars above it */
711 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
712 page = GTK_WIDGET(this->editor->sci);
713 gtk_box_pack_start(GTK_BOX(vbox), page, TRUE, TRUE, 0);
715 this->priv->tab_label = gtk_label_new(NULL);
717 /* get button press events for the tab label and the space between it and
718 * the close button, if any */
719 ebox = gtk_event_box_new();
720 gtk_widget_set_has_window(ebox, FALSE);
721 g_signal_connect(ebox, "button-press-event", G_CALLBACK(notebook_tab_click), this);
722 /* focus the current document after clicking on a tab */
723 g_signal_connect_after(ebox, "button-release-event",
724 G_CALLBACK(focus_sci), NULL);
726 /* switch tab by scrolling - GTK2 behaviour for GTK3 */
727 gtk_widget_add_events(GTK_WIDGET(ebox), GDK_SCROLL_MASK);
728 gtk_widget_add_events(GTK_WIDGET(this->priv->tab_label), GDK_SCROLL_MASK);
730 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
731 gtk_box_pack_start(GTK_BOX(hbox), this->priv->tab_label, FALSE, FALSE, 0);
732 gtk_container_add(GTK_CONTAINER(ebox), hbox);
734 if (file_prefs.show_tab_cross)
736 GtkWidget *image, *btn, *align;
738 btn = gtk_button_new();
739 gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
740 gtk_button_set_focus_on_click(GTK_BUTTON(btn), FALSE);
741 gtk_widget_set_name(btn, "geany-close-tab-button");
743 image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
744 gtk_container_add(GTK_CONTAINER(btn), image);
746 align = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
747 gtk_container_add(GTK_CONTAINER(align), btn);
748 gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0);
750 g_signal_connect(btn, "clicked", G_CALLBACK(notebook_tab_close_clicked_cb), this);
751 /* button overrides event box, so make middle click on button also close tab */
752 g_signal_connect(btn, "button-press-event", G_CALLBACK(notebook_tab_click), this);
753 /* handle style modification to keep button small as possible even when theme change */
754 g_signal_connect(btn, "style-set", G_CALLBACK(notebook_tab_close_button_style_set), NULL);
757 gtk_widget_show_all(ebox);
759 document_update_tab_label(this);
761 if (file_prefs.tab_order_beside)
762 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
763 else
764 cur_page = file_prefs.tab_order_ltr ? -2 /* hack: -2 + 1 = -1, last page */ : 0;
765 if (file_prefs.tab_order_ltr)
766 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
767 ebox, NULL, cur_page + 1);
768 else
769 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
770 ebox, NULL, cur_page);
772 tab_count_changed();
774 /* enable tab DnD */
775 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(main_widgets.notebook), vbox, TRUE);
777 return tabnum;
781 static void
782 notebook_tab_close_clicked_cb(GtkButton *button, gpointer data)
784 GeanyDocument *doc = (GeanyDocument *) data;
786 document_close(doc);
790 /* Always use this instead of gtk_notebook_remove_page(). */
791 void notebook_remove_page(gint page_num)
793 gint page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
795 if (page_num == page)
797 if (file_prefs.tab_order_ltr)
798 page += 1;
799 else if (page > 0) /* never go negative, it would select the last page */
800 page -= 1;
802 if (file_prefs.tab_close_switch_to_mru)
804 GeanyDocument *last_doc;
806 last_doc = g_queue_peek_nth(mru_docs, 0);
807 if (DOC_VALID(last_doc))
808 page = document_get_notebook_page(last_doc);
811 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
814 /* now remove the page (so we don't temporarily switch to the previous page) */
815 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
817 tab_count_changed();
821 static void
822 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
823 gint x, gint y, GtkSelectionData *data, guint target_type,
824 guint event_time, gpointer user_data)
826 gboolean success = FALSE;
827 gint length = gtk_selection_data_get_length(data);
829 if (length > 0 && gtk_selection_data_get_format(data) == 8)
831 document_open_file_list((const gchar *)gtk_selection_data_get_data(data), length);
833 success = TRUE;
835 gtk_drag_finish(drag_context, success, FALSE, event_time);