2 * notebook.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Notebook tab Drag 'n' Drop reordering and tab management.
32 #include "documentprivate.h"
36 #include "callbacks.h"
39 #define GEANY_DND_NOTEBOOK_TAB_TYPE "geany_dnd_notebook_tab"
41 static const GtkTargetEntry drag_targets
[] =
43 {GEANY_DND_NOTEBOOK_TAB_TYPE
, GTK_TARGET_SAME_APP
| GTK_TARGET_SAME_WIDGET
, 0}
46 static GtkTargetEntry files_drop_targets
[] = {
48 { "UTF8_STRING", 0, 0 },
49 { "text/plain", 0, 0 },
50 { "text/uri-list", 0, 0 }
55 notebook_drag_motion_cb(GtkWidget
*widget
, GdkDragContext
*dc
,
56 gint x
, gint y
, guint event_time
, gpointer user_data
);
59 notebook_page_reordered_cb(GtkNotebook
*notebook
, GtkWidget
*child
, guint page_num
,
63 on_window_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
64 gint x
, gint y
, GtkSelectionData
*data
, guint info
,
65 guint event_time
, gpointer user_data
);
68 notebook_find_tab_num_at_pos(GtkNotebook
*notebook
, gint x
, gint y
);
71 notebook_tab_close_clicked_cb(GtkButton
*button
, gpointer user_data
);
73 static void setup_tab_dnd(void);
76 static gboolean
focus_sci(GtkWidget
*widget
, GdkEventButton
*event
, gpointer user_data
)
78 GeanyDocument
*doc
= document_get_current();
80 if (doc
!= NULL
&& event
->button
== 1)
81 gtk_widget_grab_focus(GTK_WIDGET(doc
->editor
->sci
));
87 static gboolean
gtk_notebook_show_arrows(GtkNotebook
*notebook
)
89 return notebook
->scrollable
;
91 /* To get this working we would need to define at least the first two fields of
92 * GtkNotebookPage since it is a private field. The better way would be to
93 * subclass GtkNotebook.
94 struct _FakeGtkNotebookPage
100 gboolean show_arrow
= FALSE
;
103 if (! notebook
->scrollable
)
106 children
= notebook
->children
;
109 struct _FakeGtkNotebookPage
*page
= children
->data
;
111 if (page
->tab_label
&& ! gtk_widget_get_child_visible(page
->tab_label
))
114 children
= children
->next
;
121 static gboolean
is_position_on_tab_bar(GtkNotebook
*notebook
, GdkEventButton
*event
)
126 GtkPositionType tab_pos
;
127 gint scroll_arrow_hlength
, scroll_arrow_vlength
;
130 page
= gtk_notebook_get_nth_page(notebook
, 0);
131 g_return_val_if_fail(page
!= NULL
, FALSE
);
133 tab
= gtk_notebook_get_tab_label(notebook
, page
);
134 g_return_val_if_fail(tab
!= NULL
, FALSE
);
136 tab_pos
= gtk_notebook_get_tab_pos(notebook
);
137 nb
= GTK_WIDGET(notebook
);
139 #if GTK_CHECK_VERSION(2, 10, 0)
140 gtk_widget_style_get(GTK_WIDGET(notebook
), "scroll-arrow-hlength", &scroll_arrow_hlength
,
141 "scroll-arrow-vlength", &scroll_arrow_vlength
, NULL
);
143 scroll_arrow_hlength
= scroll_arrow_vlength
= 16;
146 if (! gdk_event_get_coords((GdkEvent
*) event
, &x
, &y
))
157 if (event
->y
>= 0 && event
->y
<= tab
->allocation
.height
)
159 if (! gtk_notebook_show_arrows(notebook
) || (
160 x
> scroll_arrow_hlength
&&
161 x
< nb
->allocation
.width
- scroll_arrow_hlength
))
169 if (event
->x
>= 0 && event
->x
<= tab
->allocation
.width
)
171 if (! gtk_notebook_show_arrows(notebook
) || (
172 y
> scroll_arrow_vlength
&&
173 y
< nb
->allocation
.height
- scroll_arrow_vlength
))
183 static void tab_bar_menu_activate_cb(GtkMenuItem
*menuitem
, gpointer data
)
185 GeanyDocument
*doc
= data
;
187 if (! DOC_VALID(doc
))
190 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
),
191 document_get_notebook_page(doc
));
195 static void show_tab_bar_popup_menu(GdkEventButton
*event
)
197 GtkWidget
*menu_item
;
198 static GtkWidget
*menu
= NULL
;
201 menu
= gtk_menu_new();
203 /* clear the old menu items */
204 gtk_container_foreach(GTK_CONTAINER(menu
), (GtkCallback
) gtk_widget_destroy
, NULL
);
206 ui_menu_add_document_items(GTK_MENU(menu
), document_get_current(),
207 G_CALLBACK(tab_bar_menu_activate_cb
));
209 menu_item
= gtk_separator_menu_item_new();
210 gtk_widget_show(menu_item
);
211 gtk_container_add(GTK_CONTAINER(menu
), menu_item
);
213 menu_item
= ui_image_menu_item_new(GTK_STOCK_CLOSE
, _("Close Ot_her Documents"));
214 gtk_widget_show(menu_item
);
215 gtk_container_add(GTK_CONTAINER(menu
), menu_item
);
216 g_signal_connect(menu_item
, "activate", G_CALLBACK(on_close_other_documents1_activate
), NULL
);
218 menu_item
= ui_image_menu_item_new(GTK_STOCK_CLOSE
, _("C_lose All"));
219 gtk_widget_show(menu_item
);
220 gtk_container_add(GTK_CONTAINER(menu
), menu_item
);
221 g_signal_connect(menu_item
, "activate", G_CALLBACK(on_close_all1_activate
), NULL
);
223 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
227 static gboolean
notebook_tab_bar_click_cb(GtkWidget
*widget
, GdkEventButton
*event
,
230 if (event
->type
== GDK_2BUTTON_PRESS
)
232 /* accessing ::event_window is a little hacky but we need to make sure the click
233 * was in the tab bar and not inside the child */
234 if (event
->window
!= GTK_NOTEBOOK(main_widgets
.notebook
)->event_window
)
237 if (is_position_on_tab_bar(GTK_NOTEBOOK(widget
), event
))
239 document_new_file(NULL
, NULL
, NULL
);
243 else if (event
->button
== 3)
245 show_tab_bar_popup_menu(event
);
253 /* Individual style for the tab close buttons */
255 "style \"geany-close-tab-button-style\" {\n"
256 " GtkWidget::focus-padding = 0\n"
257 " GtkWidget::focus-line-width = 0\n"
261 "widget \"*.geany-close-tab-button\" style \"geany-close-tab-button-style\""
264 g_signal_connect_after(main_widgets
.notebook
, "button-press-event",
265 G_CALLBACK(notebook_tab_bar_click_cb
), NULL
);
267 g_signal_connect(main_widgets
.notebook
, "drag-data-received",
268 G_CALLBACK(on_window_drag_data_received
), NULL
);
274 static void setup_tab_dnd()
276 GtkWidget
*notebook
= main_widgets
.notebook
;
278 /* Due to a segfault with manual tab DnD setup on GTK 2.10, we must
279 * use the built in gtk_notebook_set_tab_reorderable from GTK 2.10.
280 * This means a binary compiled against < 2.10 but run on >= 2.10
281 * will not have tab DnD support, but this is necessary until
282 * there is a fix for the older tab DnD code or GTK 2.10. */
283 if (gtk_check_version(2, 10, 0) == NULL
) /* null means version ok */
285 #if GTK_CHECK_VERSION(2, 10, 0)
286 g_signal_connect(notebook
, "page-reordered", G_CALLBACK(notebook_page_reordered_cb
), NULL
);
291 /* Set up drag movement callback */
292 g_signal_connect(notebook
, "drag-motion", G_CALLBACK(notebook_drag_motion_cb
), NULL
);
294 /* set up drag motion for moving notebook pages */
295 gtk_drag_dest_set(notebook
, GTK_DEST_DEFAULT_MOTION
| GTK_DEST_DEFAULT_DROP
,
296 drag_targets
, G_N_ELEMENTS(drag_targets
), GDK_ACTION_MOVE
);
297 /* set drag source, but for GTK+2.6 it's changed in motion-notify-event handler */
298 gtk_drag_source_set(notebook
, GDK_BUTTON1_MASK
,
299 drag_targets
, G_N_ELEMENTS(drag_targets
), GDK_ACTION_MOVE
);
304 notebook_page_reordered_cb(GtkNotebook
*notebook
, GtkWidget
*child
, guint page_num
,
307 /* Not necessary to update open files treeview if it's sorted.
308 * Note: if enabled, it's best to move the item instead of recreating all items. */
309 /*sidebar_openfiles_update_all();*/
314 notebook_drag_motion_cb(GtkWidget
*widget
, GdkDragContext
*dc
,
315 gint x
, gint y
, guint event_time
, gpointer user_data
)
317 static gint oldx
, oldy
; /* for determining direction of mouse drag */
318 GtkNotebook
*notebook
= GTK_NOTEBOOK(widget
);
319 gint ndest
= notebook_find_tab_num_at_pos(notebook
, x
, y
);
320 gint ncurr
= gtk_notebook_get_current_page(notebook
);
322 if (ndest
>= 0 && ndest
!= ncurr
)
325 /* prevent oscillation between non-homogeneous sized tabs */
326 switch (gtk_notebook_get_tab_pos(notebook
))
330 ok
= ((ndest
> ncurr
) && (y
> oldy
)) || ((ndest
< ncurr
) && (y
< oldy
));
335 ok
= ((ndest
> ncurr
) && (x
> oldx
)) || ((ndest
< ncurr
) && (x
< oldx
));
341 gtk_notebook_reorder_child(notebook
,
342 gtk_notebook_get_nth_page(notebook
, ncurr
), ndest
);
343 notebook_page_reordered_cb(NULL
, NULL
, ndest
, NULL
);
352 /* Adapted from Epiphany absolute version in ephy-notebook.c, thanks.
353 * x,y are co-ordinates local to the notebook (not including border padding)
354 * notebook tab label widgets must not be NULL.
355 * N.B. This only checks the dimension that the tabs are in,
356 * e.g. for GTK_POS_TOP it does not check the y coordinate. */
358 notebook_find_tab_num_at_pos(GtkNotebook
*notebook
, gint x
, gint y
)
360 GtkPositionType tab_pos
;
364 /* deal with less than 2 pages */
365 switch (gtk_notebook_get_n_pages(notebook
))
366 {case 0: return -1; case 1: return 0;}
368 tab_pos
= gtk_notebook_get_tab_pos(notebook
); /* which edge */
370 while ((page
= gtk_notebook_get_nth_page(notebook
, page_num
)))
373 GtkWidget
*tab
= gtk_notebook_get_tab_label(notebook
, page
);
375 g_return_val_if_fail(tab
!= NULL
, -1);
377 if (!GTK_WIDGET_MAPPED(GTK_WIDGET(tab
)))
378 { /* skip hidden tabs, e.g. tabs scrolled out of view */
383 /* subtract notebook pos to remove possible border padding */
384 max_x
= tab
->allocation
.x
+ tab
->allocation
.width
- GTK_WIDGET(notebook
)->allocation
.x
;
385 max_y
= tab
->allocation
.y
+ tab
->allocation
.height
- GTK_WIDGET(notebook
)->allocation
.y
;
387 if (((tab_pos
== GTK_POS_TOP
) || (tab_pos
== GTK_POS_BOTTOM
)) && (x
<= max_x
))
389 else if (((tab_pos
== GTK_POS_LEFT
) || (tab_pos
== GTK_POS_RIGHT
)) && (y
<= max_y
))
398 /* call this after the number of tabs in main_widgets.notebook changes. */
399 static void tab_count_changed(void)
401 switch (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)))
404 /* Enables DnD for dropping files into the empty notebook widget */
405 gtk_drag_dest_set(main_widgets
.notebook
, GTK_DEST_DEFAULT_ALL
,
406 files_drop_targets
, G_N_ELEMENTS(files_drop_targets
),
407 GDK_ACTION_COPY
| GDK_ACTION_MOVE
| GDK_ACTION_LINK
| GDK_ACTION_ASK
);
411 /* Disables DnD for dropping files into the notebook widget and enables the DnD for moving file
412 * tabs. Files can still be dropped into the notebook widget because it will be handled by the
413 * active Scintilla Widget (only dropping to the tab bar is not possible but it should be ok) */
414 gtk_drag_dest_set(main_widgets
.notebook
, GTK_DEST_DEFAULT_MOTION
| GTK_DEST_DEFAULT_DROP
,
415 drag_targets
, G_N_ELEMENTS(drag_targets
), GDK_ACTION_MOVE
);
421 static gboolean
notebook_tab_click(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
423 /* toggle additional widgets on double click */
424 if (event
->type
== GDK_2BUTTON_PRESS
)
426 if (interface_prefs
.notebook_double_click_hides_widgets
)
427 on_menu_toggle_all_additional_widgets1_activate(NULL
, NULL
);
429 return TRUE
; /* stop other handlers like notebook_tab_bar_click_cb() */
431 /* close tab on middle click */
432 if (event
->button
== 2)
434 document_remove_page(gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
),
436 return TRUE
; /* stop other handlers like notebook_tab_bar_click_cb() */
443 static void notebook_tab_close_button_style_set(GtkWidget
*btn
, GtkRcStyle
*prev_style
,
448 gtk_icon_size_lookup_for_settings(gtk_widget_get_settings(btn
), GTK_ICON_SIZE_MENU
, &w
, &h
);
449 gtk_widget_set_size_request(btn
, w
+ 2, h
+ 2);
453 /* Returns page number of notebook page, or -1 on error */
454 gint
notebook_new_tab(GeanyDocument
*this)
456 GtkWidget
*hbox
, *ebox
;
460 g_return_val_if_fail(this != NULL
, -1);
462 page
= GTK_WIDGET(this->editor
->sci
);
464 this->priv
->tab_label
= gtk_label_new(NULL
);
466 /* get button press events for the tab label and the space between it and
467 * the close button, if any */
468 ebox
= gtk_event_box_new();
469 GTK_WIDGET_SET_FLAGS(ebox
, GTK_NO_WINDOW
);
470 g_signal_connect(ebox
, "button-press-event", G_CALLBACK(notebook_tab_click
), page
);
471 /* focus the current document after clicking on a tab */
472 g_signal_connect_after(ebox
, "button-release-event",
473 G_CALLBACK(focus_sci
), NULL
);
475 hbox
= gtk_hbox_new(FALSE
, 2);
476 gtk_box_pack_start(GTK_BOX(hbox
), this->priv
->tab_label
, FALSE
, FALSE
, 0);
477 gtk_container_add(GTK_CONTAINER(ebox
), hbox
);
479 if (file_prefs
.show_tab_cross
)
481 GtkWidget
*image
, *btn
, *align
;
483 btn
= gtk_button_new();
484 gtk_button_set_relief(GTK_BUTTON(btn
), GTK_RELIEF_NONE
);
485 gtk_button_set_focus_on_click(GTK_BUTTON(btn
), FALSE
);
486 gtk_widget_set_name(btn
, "geany-close-tab-button");
488 image
= gtk_image_new_from_stock(GTK_STOCK_CLOSE
, GTK_ICON_SIZE_MENU
);
489 gtk_container_add(GTK_CONTAINER(btn
), image
);
491 align
= gtk_alignment_new(1.0, 0.0, 0.0, 0.0);
492 gtk_container_add(GTK_CONTAINER(align
), btn
);
493 gtk_box_pack_start(GTK_BOX(hbox
), align
, TRUE
, TRUE
, 0);
495 g_signal_connect(btn
, "clicked", G_CALLBACK(notebook_tab_close_clicked_cb
), page
);
496 /* button overrides event box, so make middle click on button also close tab */
497 g_signal_connect(btn
, "button-press-event", G_CALLBACK(notebook_tab_click
), page
);
498 /* handle style modification to keep button small as possible even when theme change */
499 g_signal_connect(btn
, "style-set", G_CALLBACK(notebook_tab_close_button_style_set
), NULL
);
502 gtk_widget_show_all(ebox
);
504 document_update_tab_label(this);
506 if (file_prefs
.tab_order_ltr
)
507 tabnum
= gtk_notebook_append_page_menu(GTK_NOTEBOOK(main_widgets
.notebook
), page
,
510 tabnum
= gtk_notebook_insert_page_menu(GTK_NOTEBOOK(main_widgets
.notebook
), page
,
515 /* This is where tab DnD is enabled for GTK 2.10 and higher */
516 #if GTK_CHECK_VERSION(2, 10, 0)
517 if (gtk_check_version(2, 10, 0) == NULL
) /* null means version ok */
519 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(main_widgets
.notebook
), page
, TRUE
);
527 notebook_tab_close_clicked_cb(GtkButton
*button
, gpointer user_data
)
529 gint cur_page
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
),
530 GTK_WIDGET(user_data
));
532 document_remove_page(cur_page
);
536 /* Always use this instead of gtk_notebook_remove_page(). */
537 void notebook_remove_page(gint page_num
)
539 gint curpage
= gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets
.notebook
));
541 /* Focus the next page, not the previous */
542 if (curpage
== page_num
&& file_prefs
.tab_order_ltr
)
544 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), curpage
+ 1);
547 /* now remove the page (so we don't temporarily switch to the previous page) */
548 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
555 on_window_drag_data_received(GtkWidget
*widget
, GdkDragContext
*drag_context
,
556 gint x
, gint y
, GtkSelectionData
*data
, guint target_type
,
557 guint event_time
, gpointer user_data
)
559 gboolean success
= FALSE
;
561 if (data
->length
> 0 && data
->format
== 8)
563 if (drag_context
->action
== GDK_ACTION_ASK
)
565 drag_context
->action
= GDK_ACTION_COPY
;
568 document_open_file_list((const gchar
*)data
->data
, data
->length
);
572 gtk_drag_finish(drag_context
, success
, FALSE
, event_time
);