Rename pointerOrder TMtag member to flags
[geany-mirror.git] / src / notebook.c
blobf4e3724dfc392011e20c92c86dac3a2e4800d73a
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 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, 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 void notebook_init(void)
549 g_signal_connect_after(main_widgets.notebook, "button-press-event",
550 G_CALLBACK(notebook_tab_bar_click_cb), NULL);
552 g_signal_connect(main_widgets.notebook, "drag-data-received",
553 G_CALLBACK(on_window_drag_data_received), NULL);
555 mru_docs = g_queue_new();
556 g_signal_connect(main_widgets.notebook, "switch-page",
557 G_CALLBACK(on_notebook_switch_page), NULL);
558 g_signal_connect(geany_object, "document-close",
559 G_CALLBACK(on_document_close), NULL);
561 /* in case the switch dialog misses an event while drawing the dialog */
562 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
564 setup_tab_dnd();
568 void notebook_free(void)
570 g_queue_free(mru_docs);
574 static void setup_tab_dnd(void)
576 GtkWidget *notebook = main_widgets.notebook;
578 g_signal_connect(notebook, "page-reordered", G_CALLBACK(notebook_page_reordered_cb), NULL);
582 static void
583 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
584 gpointer user_data)
586 /* Not necessary to update open files treeview if it's sorted.
587 * Note: if enabled, it's best to move the item instead of recreating all items. */
588 /*sidebar_openfiles_update_all();*/
592 /* call this after the number of tabs in main_widgets.notebook changes. */
593 static void tab_count_changed(void)
595 switch (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)))
597 case 0:
598 /* Enables DnD for dropping files into the empty notebook widget */
599 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_ALL,
600 files_drop_targets, G_N_ELEMENTS(files_drop_targets),
601 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK);
602 break;
604 case 1:
605 /* Disables DnD for dropping files into the notebook widget and enables the DnD for moving file
606 * tabs. Files can still be dropped into the notebook widget because it will be handled by the
607 * active Scintilla Widget (only dropping to the tab bar is not possible but it should be ok) */
608 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
609 drag_targets, G_N_ELEMENTS(drag_targets), GDK_ACTION_MOVE);
610 break;
615 static gboolean notebook_tab_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
617 guint state;
618 GeanyDocument *doc = (GeanyDocument *) data;
620 /* toggle additional widgets on double click */
621 if (event->type == GDK_2BUTTON_PRESS)
623 if (interface_prefs.notebook_double_click_hides_widgets)
624 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
626 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
628 /* close tab on middle click */
629 if (event->button == 2)
631 document_close(doc);
632 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
634 /* switch last used tab on ctrl-click */
635 state = keybindings_get_modifiers(event->state);
636 if (event->button == 1 && state == GEANY_PRIMARY_MOD_MASK)
638 keybindings_send_command(GEANY_KEY_GROUP_NOTEBOOK,
639 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED);
640 return TRUE;
642 /* right-click is first handled here if it happened on a notebook tab */
643 if (event->button == 3)
645 show_tab_bar_popup_menu(event, doc);
646 return TRUE;
649 return FALSE;
653 static void notebook_tab_close_button_style_set(GtkWidget *btn, GtkRcStyle *prev_style,
654 gpointer data)
656 gint w, h;
658 gtk_icon_size_lookup_for_settings(gtk_widget_get_settings(btn), GTK_ICON_SIZE_MENU, &w, &h);
659 gtk_widget_set_size_request(btn, w + 2, h + 2);
663 /* Returns page number of notebook page, or -1 on error
665 * Note: the widget added to the notebook is *not* shown by this function, so you have to call
666 * something like `gtk_widget_show(document_get_notebook_child(doc))` when finished setting up the
667 * document. This is necessary because when the notebook tab is added, the document isn't ready
668 * yet, and we need the notebook to emit ::switch-page after it actually is. Actually this
669 * doesn't prevent the signal to me emitted straight when we insert the page (this looks like a
670 * GTK bug), but it emits it again when showing the child, and it's all we need. */
671 gint notebook_new_tab(GeanyDocument *this)
673 GtkWidget *hbox, *ebox, *vbox;
674 gint tabnum;
675 GtkWidget *page;
676 gint cur_page;
678 g_return_val_if_fail(this != NULL, -1);
680 /* page is packed into a vbox so we can stack infobars above it */
681 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
682 page = GTK_WIDGET(this->editor->sci);
683 gtk_box_pack_start(GTK_BOX(vbox), page, TRUE, TRUE, 0);
685 this->priv->tab_label = gtk_label_new(NULL);
687 /* get button press events for the tab label and the space between it and
688 * the close button, if any */
689 ebox = gtk_event_box_new();
690 gtk_widget_set_has_window(ebox, FALSE);
691 g_signal_connect(ebox, "button-press-event", G_CALLBACK(notebook_tab_click), this);
692 /* focus the current document after clicking on a tab */
693 g_signal_connect_after(ebox, "button-release-event",
694 G_CALLBACK(focus_sci), NULL);
696 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
697 gtk_box_pack_start(GTK_BOX(hbox), this->priv->tab_label, FALSE, FALSE, 0);
698 gtk_container_add(GTK_CONTAINER(ebox), hbox);
700 if (file_prefs.show_tab_cross)
702 GtkWidget *image, *btn, *align;
704 btn = gtk_button_new();
705 gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
706 gtk_button_set_focus_on_click(GTK_BUTTON(btn), FALSE);
707 gtk_widget_set_name(btn, "geany-close-tab-button");
709 image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
710 gtk_container_add(GTK_CONTAINER(btn), image);
712 align = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
713 gtk_container_add(GTK_CONTAINER(align), btn);
714 gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0);
716 g_signal_connect(btn, "clicked", G_CALLBACK(notebook_tab_close_clicked_cb), this);
717 /* button overrides event box, so make middle click on button also close tab */
718 g_signal_connect(btn, "button-press-event", G_CALLBACK(notebook_tab_click), this);
719 /* handle style modification to keep button small as possible even when theme change */
720 g_signal_connect(btn, "style-set", G_CALLBACK(notebook_tab_close_button_style_set), NULL);
723 gtk_widget_show_all(ebox);
725 document_update_tab_label(this);
727 if (file_prefs.tab_order_beside)
728 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
729 else
730 cur_page = file_prefs.tab_order_ltr ? -2 /* hack: -2 + 1 = -1, last page */ : 0;
731 if (file_prefs.tab_order_ltr)
732 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
733 ebox, NULL, cur_page + 1);
734 else
735 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
736 ebox, NULL, cur_page);
738 tab_count_changed();
740 /* enable tab DnD */
741 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(main_widgets.notebook), vbox, TRUE);
743 return tabnum;
747 static void
748 notebook_tab_close_clicked_cb(GtkButton *button, gpointer data)
750 GeanyDocument *doc = (GeanyDocument *) data;
752 document_close(doc);
756 /* Always use this instead of gtk_notebook_remove_page(). */
757 void notebook_remove_page(gint page_num)
759 gint page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
761 if (page_num == page)
763 if (file_prefs.tab_order_ltr)
764 page += 1;
765 else if (page > 0) /* never go negative, it would select the last page */
766 page -= 1;
768 if (file_prefs.tab_close_switch_to_mru)
770 GeanyDocument *last_doc;
772 last_doc = g_queue_peek_nth(mru_docs, 0);
773 if (DOC_VALID(last_doc))
774 page = document_get_notebook_page(last_doc);
777 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
780 /* now remove the page (so we don't temporarily switch to the previous page) */
781 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
783 tab_count_changed();
787 static void
788 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
789 gint x, gint y, GtkSelectionData *data, guint target_type,
790 guint event_time, gpointer user_data)
792 gboolean success = FALSE;
793 gint length = gtk_selection_data_get_length(data);
795 if (length > 0 && gtk_selection_data_get_format(data) == 8)
797 document_open_file_list((const gchar *)gtk_selection_data_get_data(data), length);
799 success = TRUE;
801 gtk_drag_finish(drag_context, success, FALSE, event_time);