Minor update of Ukrainian translation
[geany-mirror.git] / src / notebook.c
blobe1ef6fe30b9ac8ac9913a9354cddc990b65a58ce
1 /*
2 * notebook.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Notebook tab Drag 'n' Drop reordering and tab management.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "notebook.h"
32 #include "callbacks.h"
33 #include "documentprivate.h"
34 #include "geanyobject.h"
35 #include "keybindings.h"
36 #include "main.h"
37 #include "support.h"
38 #include "ui_utils.h"
39 #include "utils.h"
41 #include "gtkcompat.h"
43 #include <gdk/gdkkeysyms.h>
46 #define GEANY_DND_NOTEBOOK_TAB_TYPE "geany_dnd_notebook_tab"
48 static const GtkTargetEntry drag_targets[] =
50 {GEANY_DND_NOTEBOOK_TAB_TYPE, GTK_TARGET_SAME_APP | GTK_TARGET_SAME_WIDGET, 0}
53 static GtkTargetEntry files_drop_targets[] = {
54 { "STRING", 0, 0 },
55 { "UTF8_STRING", 0, 0 },
56 { "text/plain", 0, 0 },
57 { "text/uri-list", 0, 0 }
60 static const gsize MAX_MRU_DOCS = 20;
61 static GQueue *mru_docs = NULL;
62 static guint mru_pos = 0;
64 static gboolean switch_in_progress = FALSE;
65 static GtkWidget *switch_dialog = NULL;
66 static GtkWidget *switch_dialog_label = NULL;
69 static void
70 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
71 gpointer user_data);
73 static void
74 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
75 gint x, gint y, GtkSelectionData *data, guint target_type,
76 guint event_time, gpointer user_data);
78 static void
79 notebook_tab_close_clicked_cb(GtkButton *button, gpointer user_data);
81 static void setup_tab_dnd(void);
84 static void update_mru_docs_head(GeanyDocument *doc)
86 if (doc)
88 g_queue_remove(mru_docs, doc);
89 g_queue_push_head(mru_docs, doc);
91 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
92 g_queue_pop_tail(mru_docs);
97 /* before the tab changes, add the current document to the MRU list */
98 static void on_notebook_switch_page(GtkNotebook *notebook,
99 gpointer page, guint page_num, gpointer user_data)
101 GeanyDocument *new;
103 new = document_get_from_page(page_num);
105 /* insert the very first document (when adding the second document
106 * and switching to it) */
107 if (g_queue_get_length(mru_docs) == 0 && gtk_notebook_get_n_pages(notebook) == 2)
108 update_mru_docs_head(document_get_current());
110 if (!switch_in_progress)
111 update_mru_docs_head(new);
115 static void on_document_close(GObject *obj, GeanyDocument *doc)
117 if (! main_status.quitting)
119 g_queue_remove(mru_docs, doc);
120 /* this prevents the pop up window from showing when there's a single
121 * document */
122 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 2)
123 g_queue_clear(mru_docs);
128 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
130 GtkWidget *dialog;
132 dialog = gtk_window_new(GTK_WINDOW_POPUP);
134 if (parent)
136 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
137 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
139 gtk_window_set_title(GTK_WINDOW(dialog), title);
140 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
141 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
143 gtk_widget_set_name(dialog, "GeanyDialog");
144 return dialog;
148 static gboolean is_modifier_key(guint keyval)
150 switch (keyval)
152 case GDK_Shift_L:
153 case GDK_Shift_R:
154 case GDK_Control_L:
155 case GDK_Control_R:
156 case GDK_Meta_L:
157 case GDK_Meta_R:
158 case GDK_Alt_L:
159 case GDK_Alt_R:
160 case GDK_Super_L:
161 case GDK_Super_R:
162 case GDK_Hyper_L:
163 case GDK_Hyper_R:
164 return TRUE;
165 default:
166 return FALSE;
171 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
173 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
174 if (switch_in_progress && is_modifier_key(ev->keyval))
176 GeanyDocument *doc;
178 switch_in_progress = FALSE;
180 if (switch_dialog)
182 gtk_widget_destroy(switch_dialog);
183 switch_dialog = NULL;
186 doc = document_get_current();
187 update_mru_docs_head(doc);
188 mru_pos = 0;
189 document_check_disk_status(doc, TRUE);
191 return FALSE;
195 static GtkWidget *create_switch_dialog(void)
197 GtkWidget *dialog, *widget, *vbox;
199 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
200 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
201 gtk_window_set_default_size(GTK_WINDOW(dialog), 200, -1);
203 vbox = gtk_vbox_new(FALSE, 6);
204 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
205 gtk_container_add(GTK_CONTAINER(dialog), vbox);
207 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
208 gtk_container_add(GTK_CONTAINER(vbox), widget);
210 widget = gtk_label_new(NULL);
211 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
212 gtk_container_add(GTK_CONTAINER(vbox), widget);
213 switch_dialog_label = widget;
215 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
216 return dialog;
220 static void update_filename_label(void)
222 guint i;
223 gchar *msg = NULL;
224 guint queue_length;
225 GeanyDocument *doc;
227 if (!switch_dialog)
229 switch_dialog = create_switch_dialog();
230 gtk_widget_show_all(switch_dialog);
233 queue_length = g_queue_get_length(mru_docs);
234 for (i = mru_pos; (i <= mru_pos + 3) && (doc = g_queue_peek_nth(mru_docs, i % queue_length)); i++)
236 gchar *basename;
238 basename = g_path_get_basename(DOC_FILENAME(doc));
239 if (i == mru_pos)
240 msg = g_markup_printf_escaped ("<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 SETPTR(basename, g_markup_printf_escaped ("\n%s", basename));
250 SETPTR(msg, g_strconcat(msg, basename, NULL));
252 g_free(basename);
254 gtk_label_set_markup(GTK_LABEL(switch_dialog_label), msg);
255 g_free(msg);
259 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
261 if (!switch_in_progress || switch_dialog)
263 return FALSE;
266 update_filename_label();
267 return FALSE;
271 void notebook_switch_tablastused(void)
273 GeanyDocument *last_doc;
274 gboolean switch_start = !switch_in_progress;
276 mru_pos += 1;
277 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
279 if (! DOC_VALID(last_doc))
281 utils_beep();
282 mru_pos = 0;
283 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
285 if (! DOC_VALID(last_doc))
286 return;
288 switch_in_progress = TRUE;
289 document_show_tab(last_doc);
291 /* if there's a modifier key, we can switch back in MRU order each time unless
292 * the key is released */
293 if (switch_start)
294 g_timeout_add(600, on_switch_timeout, NULL);
295 else
296 update_filename_label();
300 gboolean notebook_switch_in_progress(void)
302 return switch_in_progress;
306 static gboolean focus_sci(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
308 GeanyDocument *doc = document_get_current();
310 if (doc != NULL && event->button == 1)
311 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
313 return FALSE;
317 static gboolean gtk_notebook_show_arrows(GtkNotebook *notebook)
319 return gtk_notebook_get_scrollable(notebook);
320 #if 0
321 /* To get this working we would need to define at least the first two fields of
322 * GtkNotebookPage since it is a private field. The better way would be to
323 * subclass GtkNotebook.
324 struct _FakeGtkNotebookPage
326 GtkWidget *child;
327 GtkWidget *tab_label;
330 gboolean show_arrow = FALSE;
331 GList *children;
333 if (! notebook->scrollable)
334 return FALSE;
336 children = notebook->children;
337 while (children)
339 struct _FakeGtkNotebookPage *page = children->data;
341 if (page->tab_label && ! gtk_widget_get_child_visible(page->tab_label))
342 show_arrow = TRUE;
344 children = children->next;
346 return show_arrow;
347 #endif
351 static gboolean is_position_on_tab_bar(GtkNotebook *notebook, GdkEventButton *event)
353 GtkWidget *page;
354 GtkWidget *tab;
355 GtkWidget *nb;
356 GtkPositionType tab_pos;
357 gint scroll_arrow_hlength, scroll_arrow_vlength;
358 gdouble x, y;
360 page = gtk_notebook_get_nth_page(notebook, 0);
361 g_return_val_if_fail(page != NULL, FALSE);
363 tab = gtk_notebook_get_tab_label(notebook, page);
364 g_return_val_if_fail(tab != NULL, FALSE);
366 tab_pos = gtk_notebook_get_tab_pos(notebook);
367 nb = GTK_WIDGET(notebook);
369 gtk_widget_style_get(GTK_WIDGET(notebook), "scroll-arrow-hlength", &scroll_arrow_hlength,
370 "scroll-arrow-vlength", &scroll_arrow_vlength, NULL);
372 if (! gdk_event_get_coords((GdkEvent*) event, &x, &y))
374 x = event->x;
375 y = event->y;
378 switch (tab_pos)
380 case GTK_POS_TOP:
381 case GTK_POS_BOTTOM:
383 if (event->y >= 0 && event->y <= gtk_widget_get_allocated_height(tab))
385 if (! gtk_notebook_show_arrows(notebook) || (
386 x > scroll_arrow_hlength &&
387 x < gtk_widget_get_allocated_width(nb) - scroll_arrow_hlength))
388 return TRUE;
390 break;
392 case GTK_POS_LEFT:
393 case GTK_POS_RIGHT:
395 if (event->x >= 0 && event->x <= gtk_widget_get_allocated_width(tab))
397 if (! gtk_notebook_show_arrows(notebook) || (
398 y > scroll_arrow_vlength &&
399 y < gtk_widget_get_allocated_height(nb) - scroll_arrow_vlength))
400 return TRUE;
405 return FALSE;
409 static void tab_bar_menu_activate_cb(GtkMenuItem *menuitem, gpointer data)
411 GeanyDocument *doc = data;
413 if (! DOC_VALID(doc))
414 return;
416 document_show_tab(doc);
420 static void on_open_in_new_window_activate(GtkMenuItem *menuitem, gpointer user_data)
422 GeanyDocument *doc = user_data;
423 gchar *doc_path;
425 g_return_if_fail(doc->is_valid);
427 doc_path = utils_get_locale_from_utf8(doc->file_name);
428 utils_start_new_geany_instance(doc_path);
429 g_free(doc_path);
433 static gboolean has_tabs_on_right(GeanyDocument *doc)
435 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
436 gint total_pages = gtk_notebook_get_n_pages(nb);
437 gint doc_page = document_get_notebook_page(doc);
438 return total_pages > (doc_page + 1);
442 static void on_close_documents_right_activate(GtkMenuItem *menuitem, GeanyDocument *doc)
444 g_return_if_fail(has_tabs_on_right(doc));
445 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
446 gint current_page = gtk_notebook_get_current_page(nb);
447 gint doc_page = document_get_notebook_page(doc);
448 for (gint i = doc_page + 1; i < gtk_notebook_get_n_pages(nb); )
450 if (! document_close(document_get_from_page(i)))
451 i++; // only increment if tab wasn't closed
453 /* keep the current tab to the original one unless it has been closed, in
454 * which case use the activated one */
455 gtk_notebook_set_current_page(nb, MIN(current_page, doc_page));
459 static void show_tab_bar_popup_menu(GdkEventButton *event, GeanyDocument *doc)
461 GtkWidget *menu_item;
462 static GtkWidget *menu = NULL;
464 if (menu == NULL)
465 menu = gtk_menu_new();
467 /* clear the old menu items */
468 gtk_container_foreach(GTK_CONTAINER(menu), (GtkCallback) gtk_widget_destroy, NULL);
470 ui_menu_add_document_items(GTK_MENU(menu), document_get_current(),
471 G_CALLBACK(tab_bar_menu_activate_cb));
473 menu_item = gtk_separator_menu_item_new();
474 gtk_widget_show(menu_item);
475 gtk_container_add(GTK_CONTAINER(menu), menu_item);
477 menu_item = ui_image_menu_item_new(GTK_STOCK_OPEN, _("Open in New _Window"));
478 gtk_widget_show(menu_item);
479 gtk_container_add(GTK_CONTAINER(menu), menu_item);
480 g_signal_connect(menu_item, "activate",
481 G_CALLBACK(on_open_in_new_window_activate), doc);
482 /* disable if not on disk */
483 if (doc == NULL || !doc->real_path)
484 gtk_widget_set_sensitive(menu_item, FALSE);
486 menu_item = gtk_separator_menu_item_new();
487 gtk_widget_show(menu_item);
488 gtk_container_add(GTK_CONTAINER(menu), menu_item);
490 menu_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLOSE, NULL);
491 gtk_widget_show(menu_item);
492 gtk_container_add(GTK_CONTAINER(menu), menu_item);
493 g_signal_connect(menu_item, "activate", G_CALLBACK(notebook_tab_close_clicked_cb), doc);
494 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
496 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Ot_her Documents"));
497 gtk_widget_show(menu_item);
498 gtk_container_add(GTK_CONTAINER(menu), menu_item);
499 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_other_documents1_activate), doc);
500 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), (doc != NULL));
502 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("Close Documents to the _Right"));
503 gtk_widget_show(menu_item);
504 gtk_container_add(GTK_CONTAINER(menu), menu_item);
505 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_documents_right_activate), doc);
506 gtk_widget_set_sensitive(GTK_WIDGET(menu_item), doc != NULL && has_tabs_on_right(doc));
508 menu_item = ui_image_menu_item_new(GTK_STOCK_CLOSE, _("C_lose All"));
509 gtk_widget_show(menu_item);
510 gtk_container_add(GTK_CONTAINER(menu), menu_item);
511 g_signal_connect(menu_item, "activate", G_CALLBACK(on_close_all1_activate), NULL);
513 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button, event->time);
517 static gboolean notebook_tab_bar_click_cb(GtkWidget *widget, GdkEventButton *event,
518 gpointer user_data)
520 if (event->type == GDK_2BUTTON_PRESS)
522 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
523 GtkWidget *event_widget = gtk_get_event_widget((GdkEvent *) event);
524 GtkWidget *child = gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook));
526 /* ignore events from the content of the page (impl. stolen from GTK2 tab scrolling)
527 * TODO: we should also ignore notebook's action widgets, but that's more work and
528 * we don't have any of them yet anyway -- and GTK 2.16 don't have those actions. */
529 if (event_widget == NULL || event_widget == child || gtk_widget_is_ancestor(event_widget, child))
530 return FALSE;
532 if (is_position_on_tab_bar(notebook, event))
534 document_new_file(NULL, NULL, NULL);
535 return TRUE;
538 /* right-click is also handled here if it happened on the notebook tab bar but not
539 * on a tab directly */
540 else if (event->button == 3)
542 show_tab_bar_popup_menu(event, NULL);
543 return TRUE;
545 return FALSE;
549 void notebook_init(void)
551 g_signal_connect_after(main_widgets.notebook, "button-press-event",
552 G_CALLBACK(notebook_tab_bar_click_cb), NULL);
554 g_signal_connect(main_widgets.notebook, "drag-data-received",
555 G_CALLBACK(on_window_drag_data_received), NULL);
557 mru_docs = g_queue_new();
558 g_signal_connect(main_widgets.notebook, "switch-page",
559 G_CALLBACK(on_notebook_switch_page), NULL);
560 g_signal_connect(geany_object, "document-close",
561 G_CALLBACK(on_document_close), NULL);
563 /* in case the switch dialog misses an event while drawing the dialog */
564 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
566 setup_tab_dnd();
570 void notebook_free(void)
572 g_queue_free(mru_docs);
576 static void setup_tab_dnd(void)
578 GtkWidget *notebook = main_widgets.notebook;
580 g_signal_connect(notebook, "page-reordered", G_CALLBACK(notebook_page_reordered_cb), NULL);
584 static void
585 notebook_page_reordered_cb(GtkNotebook *notebook, GtkWidget *child, guint page_num,
586 gpointer user_data)
588 /* Not necessary to update open files treeview if it's sorted.
589 * Note: if enabled, it's best to move the item instead of recreating all items. */
590 /*sidebar_openfiles_update_all();*/
594 /* call this after the number of tabs in main_widgets.notebook changes. */
595 static void tab_count_changed(void)
597 switch (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)))
599 case 0:
600 /* Enables DnD for dropping files into the empty notebook widget */
601 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_ALL,
602 files_drop_targets, G_N_ELEMENTS(files_drop_targets),
603 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_ASK);
604 break;
606 case 1:
607 /* Disables DnD for dropping files into the notebook widget and enables the DnD for moving file
608 * tabs. Files can still be dropped into the notebook widget because it will be handled by the
609 * active Scintilla Widget (only dropping to the tab bar is not possible but it should be ok) */
610 gtk_drag_dest_set(main_widgets.notebook, GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
611 drag_targets, G_N_ELEMENTS(drag_targets), GDK_ACTION_MOVE);
612 break;
617 static gboolean notebook_tab_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
619 guint state;
620 GeanyDocument *doc = (GeanyDocument *) data;
622 /* toggle additional widgets on double click */
623 if (event->type == GDK_2BUTTON_PRESS)
625 if (interface_prefs.notebook_double_click_hides_widgets)
626 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
628 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
630 /* close tab on middle click */
631 if (event->button == 2)
633 document_close(doc);
634 return TRUE; /* stop other handlers like notebook_tab_bar_click_cb() */
636 /* switch last used tab on ctrl-click */
637 state = keybindings_get_modifiers(event->state);
638 if (event->button == 1 && state == GEANY_PRIMARY_MOD_MASK)
640 keybindings_send_command(GEANY_KEY_GROUP_NOTEBOOK,
641 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED);
642 return TRUE;
644 /* right-click is first handled here if it happened on a notebook tab */
645 if (event->button == 3)
647 show_tab_bar_popup_menu(event, doc);
648 return TRUE;
651 return FALSE;
655 static void notebook_tab_close_button_style_set(GtkWidget *btn, GtkRcStyle *prev_style,
656 gpointer data)
658 gint w, h;
660 gtk_icon_size_lookup_for_settings(gtk_widget_get_settings(btn), GTK_ICON_SIZE_MENU, &w, &h);
661 gtk_widget_set_size_request(btn, w + 2, h + 2);
665 /* Returns page number of notebook page, or -1 on error
667 * Note: the widget added to the notebook is *not* shown by this function, so you have to call
668 * something like `gtk_widget_show(document_get_notebook_child(doc))` when finished setting up the
669 * document. This is necessary because when the notebook tab is added, the document isn't ready
670 * yet, and we need the notebook to emit ::switch-page after it actually is. Actually this
671 * doesn't prevent the signal to me emitted straight when we insert the page (this looks like a
672 * GTK bug), but it emits it again when showing the child, and it's all we need. */
673 gint notebook_new_tab(GeanyDocument *this)
675 GtkWidget *hbox, *ebox, *vbox;
676 gint tabnum;
677 GtkWidget *page;
678 gint cur_page;
680 g_return_val_if_fail(this != NULL, -1);
682 /* page is packed into a vbox so we can stack infobars above it */
683 vbox = gtk_vbox_new(FALSE, 0);
684 page = GTK_WIDGET(this->editor->sci);
685 gtk_box_pack_start(GTK_BOX(vbox), page, TRUE, TRUE, 0);
687 this->priv->tab_label = gtk_label_new(NULL);
689 /* get button press events for the tab label and the space between it and
690 * the close button, if any */
691 ebox = gtk_event_box_new();
692 gtk_widget_set_has_window(ebox, FALSE);
693 g_signal_connect(ebox, "button-press-event", G_CALLBACK(notebook_tab_click), this);
694 /* focus the current document after clicking on a tab */
695 g_signal_connect_after(ebox, "button-release-event",
696 G_CALLBACK(focus_sci), NULL);
698 hbox = gtk_hbox_new(FALSE, 2);
699 gtk_box_pack_start(GTK_BOX(hbox), this->priv->tab_label, FALSE, FALSE, 0);
700 gtk_container_add(GTK_CONTAINER(ebox), hbox);
702 if (file_prefs.show_tab_cross)
704 GtkWidget *image, *btn, *align;
706 btn = gtk_button_new();
707 gtk_button_set_relief(GTK_BUTTON(btn), GTK_RELIEF_NONE);
708 gtk_button_set_focus_on_click(GTK_BUTTON(btn), FALSE);
709 gtk_widget_set_name(btn, "geany-close-tab-button");
711 image = gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU);
712 gtk_container_add(GTK_CONTAINER(btn), image);
714 align = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
715 gtk_container_add(GTK_CONTAINER(align), btn);
716 gtk_box_pack_start(GTK_BOX(hbox), align, TRUE, TRUE, 0);
718 g_signal_connect(btn, "clicked", G_CALLBACK(notebook_tab_close_clicked_cb), this);
719 /* button overrides event box, so make middle click on button also close tab */
720 g_signal_connect(btn, "button-press-event", G_CALLBACK(notebook_tab_click), this);
721 /* handle style modification to keep button small as possible even when theme change */
722 g_signal_connect(btn, "style-set", G_CALLBACK(notebook_tab_close_button_style_set), NULL);
725 gtk_widget_show_all(ebox);
727 document_update_tab_label(this);
729 if (file_prefs.tab_order_beside)
730 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
731 else
732 cur_page = file_prefs.tab_order_ltr ? -2 /* hack: -2 + 1 = -1, last page */ : 0;
733 if (file_prefs.tab_order_ltr)
734 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
735 ebox, NULL, cur_page + 1);
736 else
737 tabnum = gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets.notebook), vbox,
738 ebox, NULL, cur_page);
740 tab_count_changed();
742 /* enable tab DnD */
743 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(main_widgets.notebook), vbox, TRUE);
745 return tabnum;
749 static void
750 notebook_tab_close_clicked_cb(GtkButton *button, gpointer data)
752 GeanyDocument *doc = (GeanyDocument *) data;
754 document_close(doc);
758 /* Always use this instead of gtk_notebook_remove_page(). */
759 void notebook_remove_page(gint page_num)
761 gint page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
763 if (page_num == page)
765 if (file_prefs.tab_order_ltr)
766 page += 1;
767 else if (page > 0) /* never go negative, it would select the last page */
768 page -= 1;
770 if (file_prefs.tab_close_switch_to_mru)
772 GeanyDocument *last_doc;
774 last_doc = g_queue_peek_nth(mru_docs, 0);
775 if (DOC_VALID(last_doc))
776 page = document_get_notebook_page(last_doc);
779 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
782 /* now remove the page (so we don't temporarily switch to the previous page) */
783 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
785 tab_count_changed();
789 static void
790 on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
791 gint x, gint y, GtkSelectionData *data, guint target_type,
792 guint event_time, gpointer user_data)
794 gboolean success = FALSE;
795 gint length = gtk_selection_data_get_length(data);
797 if (length > 0 && gtk_selection_data_get_format(data) == 8)
799 document_open_file_list((const gchar *)gtk_selection_data_get_data(data), length);
801 success = TRUE;
803 gtk_drag_finish(drag_context, success, FALSE, event_time);