]> Pileus Git - ~andy/gtk/blob - gtk/gtknotebook.c
Remove GtkObject completely
[~andy/gtk] / gtk / gtknotebook.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #include "gtknotebook.h"
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <gdk/gdkkeysyms.h>
36
37 #include "gtkmain.h"
38 #include "gtkmenu.h"
39 #include "gtkmenuitem.h"
40 #include "gtklabel.h"
41 #include "gtkintl.h"
42 #include "gtkmarshalers.h"
43 #include "gtkbindings.h"
44 #include "gtkprivate.h"
45 #include "gtkdnd.h"
46 #include "gtkbuildable.h"
47
48 #define SCROLL_DELAY_FACTOR   5
49 #define SCROLL_THRESHOLD      12
50 #define DND_THRESHOLD_MULTIPLIER 4
51 #define FRAMES_PER_SECOND     45
52 #define MSECS_BETWEEN_UPDATES (1000 / FRAMES_PER_SECOND)
53
54 typedef struct _GtkNotebookPage GtkNotebookPage;
55
56 typedef enum
57 {
58   DRAG_OPERATION_NONE,
59   DRAG_OPERATION_REORDER,
60   DRAG_OPERATION_DETACH
61 } GtkNotebookDragOperation;
62
63 enum {
64   ACTION_WIDGET_START,
65   ACTION_WIDGET_END,
66   N_ACTION_WIDGETS
67 };
68
69 struct _GtkNotebookPrivate
70 {
71   GtkNotebookDragOperation   operation;
72   GtkNotebookPage           *cur_page;
73   GtkNotebookPage           *detached_tab;
74   GtkTargetList             *source_targets;
75   GtkWidget                 *action_widget[N_ACTION_WIDGETS];
76   GtkWidget                 *dnd_window;
77   GtkWidget                 *menu;
78
79   GdkWindow               *drag_window;
80   GdkWindow               *event_window;
81
82   GList         *children;
83   GList         *first_tab;             /* The first tab visible (for scrolling notebooks) */
84   GList         *focus_tab;
85
86   gint           drag_begin_x;
87   gint           drag_begin_y;
88   gint           drag_offset_x;
89   gint           drag_offset_y;
90   gint           drag_window_x;
91   gint           drag_window_y;
92   gint           mouse_x;
93   gint           mouse_y;
94   gint           pressed_button;
95
96   GQuark         group;
97
98   guint          dnd_timer;
99   guint          switch_tab_timer;
100
101   guint16        tab_hborder;
102   guint16        tab_vborder;
103
104   guint32        timer;
105   guint32        timestamp;
106
107   guint          button             : 2;
108   guint          child_has_focus    : 1;
109   guint          click_child        : 3;
110   guint          during_detach      : 1;
111   guint          during_reorder     : 1;
112   guint          focus_out          : 1; /* Flag used by ::move-focus-out implementation */
113   guint          has_scrolled       : 1;
114   guint          have_visible_child : 1;
115   guint          homogeneous        : 1;
116   guint          in_child           : 3;
117   guint          need_timer         : 1;
118   guint          show_border        : 1;
119   guint          show_tabs          : 1;
120   guint          scrollable         : 1;
121   guint          tab_pos            : 2;
122
123   guint          has_before_previous : 1;
124   guint          has_before_next     : 1;
125   guint          has_after_previous  : 1;
126   guint          has_after_next      : 1;
127 };
128
129 enum {
130   SWITCH_PAGE,
131   FOCUS_TAB,
132   SELECT_PAGE,
133   CHANGE_CURRENT_PAGE,
134   MOVE_FOCUS_OUT,
135   REORDER_TAB,
136   PAGE_REORDERED,
137   PAGE_REMOVED,
138   PAGE_ADDED,
139   CREATE_WINDOW,
140   LAST_SIGNAL
141 };
142
143 enum {
144   STEP_PREV,
145   STEP_NEXT
146 };
147
148 typedef enum
149 {
150   ARROW_NONE,
151   ARROW_LEFT_BEFORE,
152   ARROW_RIGHT_BEFORE,
153   ARROW_LEFT_AFTER,
154   ARROW_RIGHT_AFTER
155 } GtkNotebookArrow;
156
157 typedef enum
158 {
159   POINTER_BEFORE,
160   POINTER_AFTER,
161   POINTER_BETWEEN
162 } GtkNotebookPointerPosition;
163
164 #define ARROW_IS_LEFT(arrow)  ((arrow) == ARROW_LEFT_BEFORE || (arrow) == ARROW_LEFT_AFTER)
165 #define ARROW_IS_BEFORE(arrow) ((arrow) == ARROW_LEFT_BEFORE || (arrow) == ARROW_RIGHT_BEFORE)
166
167 enum {
168   PROP_0,
169   PROP_TAB_POS,
170   PROP_SHOW_TABS,
171   PROP_SHOW_BORDER,
172   PROP_SCROLLABLE,
173   PROP_PAGE,
174   PROP_ENABLE_POPUP,
175   PROP_GROUP_NAME
176 };
177
178 enum {
179   CHILD_PROP_0,
180   CHILD_PROP_TAB_LABEL,
181   CHILD_PROP_MENU_LABEL,
182   CHILD_PROP_POSITION,
183   CHILD_PROP_TAB_EXPAND,
184   CHILD_PROP_TAB_FILL,
185   CHILD_PROP_TAB_PACK,
186   CHILD_PROP_REORDERABLE,
187   CHILD_PROP_DETACHABLE
188 };
189
190 #define GTK_NOTEBOOK_PAGE(_glist_)         ((GtkNotebookPage *)((GList *)(_glist_))->data)
191
192 /* some useful defines for calculating coords */
193 #define PAGE_LEFT_X(_page_)   (((GtkNotebookPage *) (_page_))->allocation.x)
194 #define PAGE_RIGHT_X(_page_)  (((GtkNotebookPage *) (_page_))->allocation.x + ((GtkNotebookPage *) (_page_))->allocation.width)
195 #define PAGE_MIDDLE_X(_page_) (((GtkNotebookPage *) (_page_))->allocation.x + ((GtkNotebookPage *) (_page_))->allocation.width / 2)
196 #define PAGE_TOP_Y(_page_)    (((GtkNotebookPage *) (_page_))->allocation.y)
197 #define PAGE_BOTTOM_Y(_page_) (((GtkNotebookPage *) (_page_))->allocation.y + ((GtkNotebookPage *) (_page_))->allocation.height)
198 #define PAGE_MIDDLE_Y(_page_) (((GtkNotebookPage *) (_page_))->allocation.y + ((GtkNotebookPage *) (_page_))->allocation.height / 2)
199 #define NOTEBOOK_IS_TAB_LABEL_PARENT(_notebook_,_page_) (gtk_widget_get_parent (((GtkNotebookPage *) (_page_))->tab_label) == ((GtkWidget *) (_notebook_)))
200
201 struct _GtkNotebookPage
202 {
203   GtkWidget *child;
204   GtkWidget *tab_label;
205   GtkWidget *menu_label;
206   GtkWidget *last_focus_child;  /* Last descendant of the page that had focus */
207
208   guint default_menu : 1;       /* If true, we create the menu label ourself */
209   guint default_tab  : 1;       /* If true, we create the tab label ourself */
210   guint expand       : 1;
211   guint fill         : 1;
212   guint pack         : 1;
213   guint reorderable  : 1;
214   guint detachable   : 1;
215
216   /* if true, the tab label was visible on last allocation; we track this so
217    * that we know to redraw the tab area if a tab label was hidden then shown
218    * without changing position */
219   guint tab_allocated_visible : 1;
220
221   GtkRequisition requisition;
222   GtkAllocation allocation;
223
224   gulong mnemonic_activate_signal;
225   gulong notify_visible_handler;
226 };
227
228 static const GtkTargetEntry notebook_targets [] = {
229   { "GTK_NOTEBOOK_TAB", GTK_TARGET_SAME_APP, 0 },
230 };
231
232 #ifdef G_DISABLE_CHECKS
233 #define CHECK_FIND_CHILD(notebook, child)                           \
234  gtk_notebook_find_child (notebook, child, G_STRLOC)
235 #else
236 #define CHECK_FIND_CHILD(notebook, child)                           \
237  gtk_notebook_find_child (notebook, child, NULL)
238 #endif
239  
240 /*** GtkNotebook Methods ***/
241 static gboolean gtk_notebook_select_page         (GtkNotebook      *notebook,
242                                                   gboolean          move_focus);
243 static gboolean gtk_notebook_focus_tab           (GtkNotebook      *notebook,
244                                                   GtkNotebookTab    type);
245 static gboolean gtk_notebook_change_current_page (GtkNotebook      *notebook,
246                                                   gint              offset);
247 static void     gtk_notebook_move_focus_out      (GtkNotebook      *notebook,
248                                                   GtkDirectionType  direction_type);
249 static gboolean gtk_notebook_reorder_tab         (GtkNotebook      *notebook,
250                                                   GtkDirectionType  direction_type,
251                                                   gboolean          move_to_last);
252 static void     gtk_notebook_remove_tab_label    (GtkNotebook      *notebook,
253                                                   GtkNotebookPage  *page);
254 static void     gtk_notebook_set_tab_label_packing   (GtkNotebook  *notebook,
255                                                       GtkWidget    *child,
256                                                       gboolean      expand,
257                                                       gboolean      fill,
258                                                       GtkPackType   pack_type);
259 static void     gtk_notebook_query_tab_label_packing (GtkNotebook  *notebook,
260                                                       GtkWidget    *child,
261                                                       gboolean     *expand,
262                                                       gboolean     *fill,
263                                                       GtkPackType  *pack_type);
264
265 /*** GObject Methods ***/
266 static void gtk_notebook_set_property        (GObject         *object,
267                                               guint            prop_id,
268                                               const GValue    *value,
269                                               GParamSpec      *pspec);
270 static void gtk_notebook_get_property        (GObject         *object,
271                                               guint            prop_id,
272                                               GValue          *value,
273                                               GParamSpec      *pspec);
274
275 /*** GtkWidget Methods ***/
276 static void gtk_notebook_destroy             (GtkWidget        *widget);
277 static void gtk_notebook_map                 (GtkWidget        *widget);
278 static void gtk_notebook_unmap               (GtkWidget        *widget);
279 static void gtk_notebook_realize             (GtkWidget        *widget);
280 static void gtk_notebook_unrealize           (GtkWidget        *widget);
281 static void gtk_notebook_size_request        (GtkWidget        *widget,
282                                               GtkRequisition   *requisition);
283 static void gtk_notebook_size_allocate       (GtkWidget        *widget,
284                                               GtkAllocation    *allocation);
285 static gint gtk_notebook_draw                (GtkWidget        *widget,
286                                               cairo_t          *cr);
287 static gint gtk_notebook_button_press        (GtkWidget        *widget,
288                                               GdkEventButton   *event);
289 static gint gtk_notebook_button_release      (GtkWidget        *widget,
290                                               GdkEventButton   *event);
291 static gboolean gtk_notebook_popup_menu      (GtkWidget        *widget);
292 static gint gtk_notebook_leave_notify        (GtkWidget        *widget,
293                                               GdkEventCrossing *event);
294 static gint gtk_notebook_motion_notify       (GtkWidget        *widget,
295                                               GdkEventMotion   *event);
296 static gint gtk_notebook_focus_in            (GtkWidget        *widget,
297                                               GdkEventFocus    *event);
298 static gint gtk_notebook_focus_out           (GtkWidget        *widget,
299                                               GdkEventFocus    *event);
300 static void gtk_notebook_grab_notify         (GtkWidget          *widget,
301                                               gboolean            was_grabbed);
302 static void gtk_notebook_state_changed       (GtkWidget          *widget,
303                                               GtkStateType        previous_state);
304 static gint gtk_notebook_focus               (GtkWidget        *widget,
305                                               GtkDirectionType  direction);
306 static void gtk_notebook_style_set           (GtkWidget        *widget,
307                                               GtkStyle         *previous);
308
309 /*** Drag and drop Methods ***/
310 static void gtk_notebook_drag_begin          (GtkWidget        *widget,
311                                               GdkDragContext   *context);
312 static void gtk_notebook_drag_end            (GtkWidget        *widget,
313                                               GdkDragContext   *context);
314 static gboolean gtk_notebook_drag_failed     (GtkWidget        *widget,
315                                               GdkDragContext   *context,
316                                               GtkDragResult     result,
317                                               gpointer          data);
318 static gboolean gtk_notebook_drag_motion     (GtkWidget        *widget,
319                                               GdkDragContext   *context,
320                                               gint              x,
321                                               gint              y,
322                                               guint             time);
323 static void gtk_notebook_drag_leave          (GtkWidget        *widget,
324                                               GdkDragContext   *context,
325                                               guint             time);
326 static gboolean gtk_notebook_drag_drop       (GtkWidget        *widget,
327                                               GdkDragContext   *context,
328                                               gint              x,
329                                               gint              y,
330                                               guint             time);
331 static void gtk_notebook_drag_data_get       (GtkWidget        *widget,
332                                               GdkDragContext   *context,
333                                               GtkSelectionData *data,
334                                               guint             info,
335                                               guint             time);
336 static void gtk_notebook_drag_data_received  (GtkWidget        *widget,
337                                               GdkDragContext   *context,
338                                               gint              x,
339                                               gint              y,
340                                               GtkSelectionData *data,
341                                               guint             info,
342                                               guint             time);
343
344 /*** GtkContainer Methods ***/
345 static void gtk_notebook_set_child_property  (GtkContainer     *container,
346                                               GtkWidget        *child,
347                                               guint             property_id,
348                                               const GValue     *value,
349                                               GParamSpec       *pspec);
350 static void gtk_notebook_get_child_property  (GtkContainer     *container,
351                                               GtkWidget        *child,
352                                               guint             property_id,
353                                               GValue           *value,
354                                               GParamSpec       *pspec);
355 static void gtk_notebook_add                 (GtkContainer     *container,
356                                               GtkWidget        *widget);
357 static void gtk_notebook_remove              (GtkContainer     *container,
358                                               GtkWidget        *widget);
359 static void gtk_notebook_set_focus_child     (GtkContainer     *container,
360                                               GtkWidget        *child);
361 static GType gtk_notebook_child_type       (GtkContainer     *container);
362 static void gtk_notebook_forall              (GtkContainer     *container,
363                                               gboolean          include_internals,
364                                               GtkCallback       callback,
365                                               gpointer          callback_data);
366
367 /*** GtkNotebook Methods ***/
368 static gint gtk_notebook_real_insert_page    (GtkNotebook      *notebook,
369                                               GtkWidget        *child,
370                                               GtkWidget        *tab_label,
371                                               GtkWidget        *menu_label,
372                                               gint              position);
373
374 static GtkNotebook *gtk_notebook_create_window (GtkNotebook    *notebook,
375                                                 GtkWidget      *page,
376                                                 gint            x,
377                                                 gint            y);
378
379 /*** GtkNotebook Private Functions ***/
380 static void gtk_notebook_redraw_tabs         (GtkNotebook      *notebook);
381 static void gtk_notebook_redraw_arrows       (GtkNotebook      *notebook);
382 static void gtk_notebook_real_remove         (GtkNotebook      *notebook,
383                                               GList            *list);
384 static void gtk_notebook_update_labels       (GtkNotebook      *notebook);
385 static gint gtk_notebook_timer               (GtkNotebook      *notebook);
386 static void gtk_notebook_set_scroll_timer    (GtkNotebook *notebook);
387 static gint gtk_notebook_page_compare        (gconstpointer     a,
388                                               gconstpointer     b);
389 static GList* gtk_notebook_find_child        (GtkNotebook      *notebook,
390                                               GtkWidget        *child,
391                                               const gchar      *function);
392 static gint  gtk_notebook_real_page_position (GtkNotebook      *notebook,
393                                               GList            *list);
394 static GList * gtk_notebook_search_page      (GtkNotebook      *notebook,
395                                               GList            *list,
396                                               gint              direction,
397                                               gboolean          find_visible);
398 static void  gtk_notebook_child_reordered    (GtkNotebook      *notebook,
399                                               GtkNotebookPage  *page);
400
401 /*** GtkNotebook Drawing Functions ***/
402 static void gtk_notebook_paint               (GtkWidget        *widget,
403                                               cairo_t          *cr);
404 static void gtk_notebook_draw_tab            (GtkNotebook      *notebook,
405                                               GtkNotebookPage  *page,
406                                               cairo_t          *cr);
407 static void gtk_notebook_draw_arrow          (GtkNotebook      *notebook,
408                                               cairo_t          *cr,
409                                               GtkNotebookArrow  arrow);
410
411 /*** GtkNotebook Size Allocate Functions ***/
412 static void gtk_notebook_pages_allocate      (GtkNotebook      *notebook);
413 static gboolean gtk_notebook_page_allocate   (GtkNotebook      *notebook,
414                                               GtkNotebookPage  *page);
415 static void gtk_notebook_calc_tabs           (GtkNotebook      *notebook,
416                                               GList            *start,
417                                               GList           **end,
418                                               gint             *tab_space,
419                                               guint             direction);
420
421 /*** GtkNotebook Page Switch Methods ***/
422 static void gtk_notebook_real_switch_page    (GtkNotebook      *notebook,
423                                               GtkWidget        *child,
424                                               guint             page_num);
425
426 /*** GtkNotebook Page Switch Functions ***/
427 static void gtk_notebook_switch_page         (GtkNotebook      *notebook,
428                                               GtkNotebookPage  *page);
429 static gint gtk_notebook_page_select         (GtkNotebook      *notebook,
430                                               gboolean          move_focus);
431 static void gtk_notebook_switch_focus_tab    (GtkNotebook      *notebook,
432                                               GList            *new_child);
433 static void gtk_notebook_menu_switch_page    (GtkWidget        *widget,
434                                               GtkNotebookPage  *page);
435
436 /*** GtkNotebook Menu Functions ***/
437 static void gtk_notebook_menu_item_create    (GtkNotebook      *notebook,
438                                               GList            *list);
439 static void gtk_notebook_menu_label_unparent (GtkWidget        *widget,
440                                               gpointer          data);
441 static void gtk_notebook_menu_detacher       (GtkWidget        *widget,
442                                               GtkMenu          *menu);
443
444 /*** GtkNotebook Private Setters ***/
445 static void gtk_notebook_update_tab_states             (GtkNotebook *notebook);
446 static gboolean gtk_notebook_mnemonic_activate_switch_page (GtkWidget *child,
447                                                             gboolean overload,
448                                                             gpointer data);
449
450 static gboolean focus_tabs_in  (GtkNotebook      *notebook);
451 static gboolean focus_child_in (GtkNotebook      *notebook,
452                                 GtkDirectionType  direction);
453
454 static void stop_scrolling (GtkNotebook *notebook);
455 static void do_detach_tab  (GtkNotebook *from,
456                             GtkNotebook *to,
457                             GtkWidget   *child,
458                             gint         x,
459                             gint         y);
460
461 /* GtkBuildable */
462 static void gtk_notebook_buildable_init           (GtkBuildableIface *iface);
463 static void gtk_notebook_buildable_add_child      (GtkBuildable *buildable,
464                                                    GtkBuilder   *builder,
465                                                    GObject      *child,
466                                                    const gchar  *type);
467
468 static guint notebook_signals[LAST_SIGNAL] = { 0 };
469
470 G_DEFINE_TYPE_WITH_CODE (GtkNotebook, gtk_notebook, GTK_TYPE_CONTAINER,
471                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
472                                                 gtk_notebook_buildable_init))
473
474 static void
475 add_tab_bindings (GtkBindingSet    *binding_set,
476                   GdkModifierType   modifiers,
477                   GtkDirectionType  direction)
478 {
479   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
480                                 "move_focus_out", 1,
481                                 GTK_TYPE_DIRECTION_TYPE, direction);
482   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
483                                 "move_focus_out", 1,
484                                 GTK_TYPE_DIRECTION_TYPE, direction);
485 }
486
487 static void
488 add_arrow_bindings (GtkBindingSet    *binding_set,
489                     guint             keysym,
490                     GtkDirectionType  direction)
491 {
492   guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
493   
494   gtk_binding_entry_add_signal (binding_set, keysym, GDK_CONTROL_MASK,
495                                 "move_focus_out", 1,
496                                 GTK_TYPE_DIRECTION_TYPE, direction);
497   gtk_binding_entry_add_signal (binding_set, keypad_keysym, GDK_CONTROL_MASK,
498                                 "move_focus_out", 1,
499                                 GTK_TYPE_DIRECTION_TYPE, direction);
500 }
501
502 static void
503 add_reorder_bindings (GtkBindingSet    *binding_set,
504                       guint             keysym,
505                       GtkDirectionType  direction,
506                       gboolean          move_to_last)
507 {
508   guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
509
510   gtk_binding_entry_add_signal (binding_set, keysym, GDK_MOD1_MASK,
511                                 "reorder_tab", 2,
512                                 GTK_TYPE_DIRECTION_TYPE, direction,
513                                 G_TYPE_BOOLEAN, move_to_last);
514   gtk_binding_entry_add_signal (binding_set, keypad_keysym, GDK_MOD1_MASK,
515                                 "reorder_tab", 2,
516                                 GTK_TYPE_DIRECTION_TYPE, direction,
517                                 G_TYPE_BOOLEAN, move_to_last);
518 }
519
520 static gboolean
521 gtk_object_handled_accumulator (GSignalInvocationHint *ihint,
522                                 GValue                *return_accu,
523                                 const GValue          *handler_return,
524                                 gpointer               dummy)
525 {
526   gboolean continue_emission;
527   GObject *object;
528
529   object = g_value_get_object (handler_return);
530   g_value_set_object (return_accu, object);
531   continue_emission = !object;
532
533   return continue_emission;
534 }
535
536 static void
537 gtk_notebook_class_init (GtkNotebookClass *class)
538 {
539   GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
540   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
541   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
542   GtkBindingSet *binding_set;
543   
544   gobject_class->set_property = gtk_notebook_set_property;
545   gobject_class->get_property = gtk_notebook_get_property;
546
547   widget_class->destroy = gtk_notebook_destroy;
548   widget_class->map = gtk_notebook_map;
549   widget_class->unmap = gtk_notebook_unmap;
550   widget_class->realize = gtk_notebook_realize;
551   widget_class->unrealize = gtk_notebook_unrealize;
552   widget_class->size_request = gtk_notebook_size_request;
553   widget_class->size_allocate = gtk_notebook_size_allocate;
554   widget_class->draw = gtk_notebook_draw;
555   widget_class->button_press_event = gtk_notebook_button_press;
556   widget_class->button_release_event = gtk_notebook_button_release;
557   widget_class->popup_menu = gtk_notebook_popup_menu;
558   widget_class->leave_notify_event = gtk_notebook_leave_notify;
559   widget_class->motion_notify_event = gtk_notebook_motion_notify;
560   widget_class->grab_notify = gtk_notebook_grab_notify;
561   widget_class->state_changed = gtk_notebook_state_changed;
562   widget_class->focus_in_event = gtk_notebook_focus_in;
563   widget_class->focus_out_event = gtk_notebook_focus_out;
564   widget_class->focus = gtk_notebook_focus;
565   widget_class->style_set = gtk_notebook_style_set;
566   widget_class->drag_begin = gtk_notebook_drag_begin;
567   widget_class->drag_end = gtk_notebook_drag_end;
568   widget_class->drag_motion = gtk_notebook_drag_motion;
569   widget_class->drag_leave = gtk_notebook_drag_leave;
570   widget_class->drag_drop = gtk_notebook_drag_drop;
571   widget_class->drag_data_get = gtk_notebook_drag_data_get;
572   widget_class->drag_data_received = gtk_notebook_drag_data_received;
573
574   container_class->add = gtk_notebook_add;
575   container_class->remove = gtk_notebook_remove;
576   container_class->forall = gtk_notebook_forall;
577   container_class->set_focus_child = gtk_notebook_set_focus_child;
578   container_class->get_child_property = gtk_notebook_get_child_property;
579   container_class->set_child_property = gtk_notebook_set_child_property;
580   container_class->child_type = gtk_notebook_child_type;
581
582   class->switch_page = gtk_notebook_real_switch_page;
583   class->insert_page = gtk_notebook_real_insert_page;
584
585   class->focus_tab = gtk_notebook_focus_tab;
586   class->select_page = gtk_notebook_select_page;
587   class->change_current_page = gtk_notebook_change_current_page;
588   class->move_focus_out = gtk_notebook_move_focus_out;
589   class->reorder_tab = gtk_notebook_reorder_tab;
590   class->create_window = gtk_notebook_create_window;
591   
592   g_object_class_install_property (gobject_class,
593                                    PROP_PAGE,
594                                    g_param_spec_int ("page",
595                                                      P_("Page"),
596                                                      P_("The index of the current page"),
597                                                      -1,
598                                                      G_MAXINT,
599                                                      -1,
600                                                      GTK_PARAM_READWRITE));
601   g_object_class_install_property (gobject_class,
602                                    PROP_TAB_POS,
603                                    g_param_spec_enum ("tab-pos",
604                                                       P_("Tab Position"),
605                                                       P_("Which side of the notebook holds the tabs"),
606                                                       GTK_TYPE_POSITION_TYPE,
607                                                       GTK_POS_TOP,
608                                                       GTK_PARAM_READWRITE));
609   g_object_class_install_property (gobject_class,
610                                    PROP_SHOW_TABS,
611                                    g_param_spec_boolean ("show-tabs",
612                                                          P_("Show Tabs"),
613                                                          P_("Whether tabs should be shown"),
614                                                          TRUE,
615                                                          GTK_PARAM_READWRITE));
616   g_object_class_install_property (gobject_class,
617                                    PROP_SHOW_BORDER,
618                                    g_param_spec_boolean ("show-border",
619                                                          P_("Show Border"),
620                                                          P_("Whether the border should be shown"),
621                                                          TRUE,
622                                                          GTK_PARAM_READWRITE));
623   g_object_class_install_property (gobject_class,
624                                    PROP_SCROLLABLE,
625                                    g_param_spec_boolean ("scrollable",
626                                                          P_("Scrollable"),
627                                                          P_("If TRUE, scroll arrows are added if there are too many tabs to fit"),
628                                                          FALSE,
629                                                          GTK_PARAM_READWRITE));
630   g_object_class_install_property (gobject_class,
631                                    PROP_ENABLE_POPUP,
632                                    g_param_spec_boolean ("enable-popup",
633                                                          P_("Enable Popup"),
634                                                          P_("If TRUE, pressing the right mouse button on the notebook pops up a menu that you can use to go to a page"),
635                                                          FALSE,
636                                                          GTK_PARAM_READWRITE));
637
638   /**
639    * GtkNotebook:group-name:
640    *
641    * Group name for tab drag and drop.
642    *
643    * Since: 2.24
644    */
645   g_object_class_install_property (gobject_class,
646                                    PROP_GROUP_NAME,
647                                    g_param_spec_string ("group-name",
648                                                         P_("Group Name"),
649                                                         P_("Group name for tab drag and drop"),
650                                                         NULL,
651                                                         GTK_PARAM_READWRITE));
652
653   gtk_container_class_install_child_property (container_class,
654                                               CHILD_PROP_TAB_LABEL,
655                                               g_param_spec_string ("tab-label", 
656                                                                    P_("Tab label"),
657                                                                    P_("The string displayed on the child's tab label"),
658                                                                    NULL,
659                                                                    GTK_PARAM_READWRITE));
660   gtk_container_class_install_child_property (container_class,
661                                               CHILD_PROP_MENU_LABEL,
662                                               g_param_spec_string ("menu-label", 
663                                                                    P_("Menu label"), 
664                                                                    P_("The string displayed in the child's menu entry"),
665                                                                    NULL,
666                                                                    GTK_PARAM_READWRITE));
667   gtk_container_class_install_child_property (container_class,
668                                               CHILD_PROP_POSITION,
669                                               g_param_spec_int ("position", 
670                                                                 P_("Position"), 
671                                                                 P_("The index of the child in the parent"),
672                                                                 -1, G_MAXINT, 0,
673                                                                 GTK_PARAM_READWRITE));
674   gtk_container_class_install_child_property (container_class,
675                                               CHILD_PROP_TAB_EXPAND,
676                                               g_param_spec_boolean ("tab-expand", 
677                                                                     P_("Tab expand"), 
678                                                                     P_("Whether to expand the child's tab"),
679                                                                     FALSE,
680                                                                     GTK_PARAM_READWRITE));
681   gtk_container_class_install_child_property (container_class,
682                                               CHILD_PROP_TAB_FILL,
683                                               g_param_spec_boolean ("tab-fill", 
684                                                                     P_("Tab fill"), 
685                                                                     P_("Whether the child's tab should fill the allocated area"),
686                                                                     TRUE,
687                                                                     GTK_PARAM_READWRITE));
688   gtk_container_class_install_child_property (container_class,
689                                               CHILD_PROP_TAB_PACK,
690                                               g_param_spec_enum ("tab-pack", 
691                                                                  P_("Tab pack type"),
692                                                                  P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
693                                                                  GTK_TYPE_PACK_TYPE, GTK_PACK_START,
694                                                                  GTK_PARAM_READWRITE));
695   gtk_container_class_install_child_property (container_class,
696                                               CHILD_PROP_REORDERABLE,
697                                               g_param_spec_boolean ("reorderable",
698                                                                     P_("Tab reorderable"),
699                                                                     P_("Whether the tab is reorderable by user action"),
700                                                                     FALSE,
701                                                                     GTK_PARAM_READWRITE));
702   gtk_container_class_install_child_property (container_class,
703                                               CHILD_PROP_DETACHABLE,
704                                               g_param_spec_boolean ("detachable",
705                                                                     P_("Tab detachable"),
706                                                                     P_("Whether the tab is detachable"),
707                                                                     FALSE,
708                                                                     GTK_PARAM_READWRITE));
709
710 /**
711  * GtkNotebook:has-secondary-backward-stepper:
712  *
713  * The "has-secondary-backward-stepper" property determines whether 
714  * a second backward arrow button is displayed on the opposite end 
715  * of the tab area.
716  *
717  * Since: 2.4
718  */  
719   gtk_widget_class_install_style_property (widget_class,
720                                            g_param_spec_boolean ("has-secondary-backward-stepper",
721                                                                  P_("Secondary backward stepper"),
722                                                                  P_("Display a second backward arrow button on the opposite end of the tab area"),
723                                                                  FALSE,
724                                                                  GTK_PARAM_READABLE));
725
726 /**
727  * GtkNotebook:has-secondary-forward-stepper:
728  *
729  * The "has-secondary-forward-stepper" property determines whether 
730  * a second forward arrow button is displayed on the opposite end 
731  * of the tab area.
732  *
733  * Since: 2.4
734  */  
735   gtk_widget_class_install_style_property (widget_class,
736                                            g_param_spec_boolean ("has-secondary-forward-stepper",
737                                                                  P_("Secondary forward stepper"),
738                                                                  P_("Display a second forward arrow button on the opposite end of the tab area"),
739                                                                  FALSE,
740                                                                  GTK_PARAM_READABLE));
741
742 /**
743  * GtkNotebook:has-backward-stepper:
744  *
745  * The "has-backward-stepper" property determines whether 
746  * the standard backward arrow button is displayed.
747  *
748  * Since: 2.4
749  */  
750   gtk_widget_class_install_style_property (widget_class,
751                                            g_param_spec_boolean ("has-backward-stepper",
752                                                                  P_("Backward stepper"),
753                                                                  P_("Display the standard backward arrow button"),
754                                                                  TRUE,
755                                                                  GTK_PARAM_READABLE));
756
757 /**
758  * GtkNotebook:has-forward-stepper:
759  *
760  * The "has-forward-stepper" property determines whether 
761  * the standard forward arrow button is displayed.
762  *
763  * Since: 2.4
764  */  
765   gtk_widget_class_install_style_property (widget_class,
766                                            g_param_spec_boolean ("has-forward-stepper",
767                                                                  P_("Forward stepper"),
768                                                                  P_("Display the standard forward arrow button"),
769                                                                  TRUE,
770                                                                  GTK_PARAM_READABLE));
771   
772 /**
773  * GtkNotebook:tab-overlap:
774  *
775  * The "tab-overlap" property defines size of tab overlap
776  * area.
777  *
778  * Since: 2.10
779  */  
780   gtk_widget_class_install_style_property (widget_class,
781                                            g_param_spec_int ("tab-overlap",
782                                                              P_("Tab overlap"),
783                                                              P_("Size of tab overlap area"),
784                                                              G_MININT,
785                                                              G_MAXINT,
786                                                              2,
787                                                              GTK_PARAM_READABLE));
788
789 /**
790  * GtkNotebook:tab-curvature:
791  *
792  * The "tab-curvature" property defines size of tab curvature.
793  *
794  * Since: 2.10
795  */  
796   gtk_widget_class_install_style_property (widget_class,
797                                            g_param_spec_int ("tab-curvature",
798                                                              P_("Tab curvature"),
799                                                              P_("Size of tab curvature"),
800                                                              0,
801                                                              G_MAXINT,
802                                                              1,
803                                                              GTK_PARAM_READABLE));
804
805   /**
806    * GtkNotebook:arrow-spacing:
807    *
808    * The "arrow-spacing" property defines the spacing between the scroll
809    * arrows and the tabs.
810    *
811    * Since: 2.10
812    */
813   gtk_widget_class_install_style_property (widget_class,
814                                            g_param_spec_int ("arrow-spacing",
815                                                              P_("Arrow spacing"),
816                                                              P_("Scroll arrow spacing"),
817                                                              0,
818                                                              G_MAXINT,
819                                                              0,
820                                                              GTK_PARAM_READABLE));
821
822   notebook_signals[SWITCH_PAGE] =
823     g_signal_new (I_("switch-page"),
824                   G_TYPE_FROM_CLASS (gobject_class),
825                   G_SIGNAL_RUN_LAST,
826                   G_STRUCT_OFFSET (GtkNotebookClass, switch_page),
827                   NULL, NULL,
828                   _gtk_marshal_VOID__OBJECT_UINT,
829                   G_TYPE_NONE, 2,
830                   GTK_TYPE_WIDGET,
831                   G_TYPE_UINT);
832   notebook_signals[FOCUS_TAB] = 
833     g_signal_new (I_("focus-tab"),
834                   G_TYPE_FROM_CLASS (gobject_class),
835                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
836                   G_STRUCT_OFFSET (GtkNotebookClass, focus_tab),
837                   NULL, NULL,
838                   _gtk_marshal_BOOLEAN__ENUM,
839                   G_TYPE_BOOLEAN, 1,
840                   GTK_TYPE_NOTEBOOK_TAB);
841   notebook_signals[SELECT_PAGE] = 
842     g_signal_new (I_("select-page"),
843                   G_TYPE_FROM_CLASS (gobject_class),
844                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
845                   G_STRUCT_OFFSET (GtkNotebookClass, select_page),
846                   NULL, NULL,
847                   _gtk_marshal_BOOLEAN__BOOLEAN,
848                   G_TYPE_BOOLEAN, 1,
849                   G_TYPE_BOOLEAN);
850   notebook_signals[CHANGE_CURRENT_PAGE] = 
851     g_signal_new (I_("change-current-page"),
852                   G_TYPE_FROM_CLASS (gobject_class),
853                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
854                   G_STRUCT_OFFSET (GtkNotebookClass, change_current_page),
855                   NULL, NULL,
856                   _gtk_marshal_BOOLEAN__INT,
857                   G_TYPE_BOOLEAN, 1,
858                   G_TYPE_INT);
859   notebook_signals[MOVE_FOCUS_OUT] =
860     g_signal_new (I_("move-focus-out"),
861                   G_TYPE_FROM_CLASS (gobject_class),
862                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
863                   G_STRUCT_OFFSET (GtkNotebookClass, move_focus_out),
864                   NULL, NULL,
865                   _gtk_marshal_VOID__ENUM,
866                   G_TYPE_NONE, 1,
867                   GTK_TYPE_DIRECTION_TYPE);
868   notebook_signals[REORDER_TAB] =
869     g_signal_new (I_("reorder-tab"),
870                   G_TYPE_FROM_CLASS (gobject_class),
871                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
872                   G_STRUCT_OFFSET (GtkNotebookClass, reorder_tab),
873                   NULL, NULL,
874                   _gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
875                   G_TYPE_BOOLEAN, 2,
876                   GTK_TYPE_DIRECTION_TYPE,
877                   G_TYPE_BOOLEAN);
878   /**
879    * GtkNotebook::page-reordered:
880    * @notebook: the #GtkNotebook
881    * @child: the child #GtkWidget affected
882    * @page_num: the new page number for @child
883    *
884    * the ::page-reordered signal is emitted in the notebook
885    * right after a page has been reordered.
886    *
887    * Since: 2.10
888    **/
889   notebook_signals[PAGE_REORDERED] =
890     g_signal_new (I_("page-reordered"),
891                   G_TYPE_FROM_CLASS (gobject_class),
892                   G_SIGNAL_RUN_LAST,
893                   0, NULL, NULL,
894                   _gtk_marshal_VOID__OBJECT_UINT,
895                   G_TYPE_NONE, 2,
896                   GTK_TYPE_WIDGET,
897                   G_TYPE_UINT);
898   /**
899    * GtkNotebook::page-removed:
900    * @notebook: the #GtkNotebook
901    * @child: the child #GtkWidget affected
902    * @page_num: the @child page number
903    *
904    * the ::page-removed signal is emitted in the notebook
905    * right after a page is removed from the notebook.
906    *
907    * Since: 2.10
908    **/
909   notebook_signals[PAGE_REMOVED] =
910     g_signal_new (I_("page-removed"),
911                   G_TYPE_FROM_CLASS (gobject_class),
912                   G_SIGNAL_RUN_LAST,
913                   0, NULL, NULL,
914                   _gtk_marshal_VOID__OBJECT_UINT,
915                   G_TYPE_NONE, 2,
916                   GTK_TYPE_WIDGET,
917                   G_TYPE_UINT);
918   /**
919    * GtkNotebook::page-added:
920    * @notebook: the #GtkNotebook
921    * @child: the child #GtkWidget affected
922    * @page_num: the new page number for @child
923    *
924    * the ::page-added signal is emitted in the notebook
925    * right after a page is added to the notebook.
926    *
927    * Since: 2.10
928    **/
929   notebook_signals[PAGE_ADDED] =
930     g_signal_new (I_("page-added"),
931                   G_TYPE_FROM_CLASS (gobject_class),
932                   G_SIGNAL_RUN_LAST,
933                   0, NULL, NULL,
934                   _gtk_marshal_VOID__OBJECT_UINT,
935                   G_TYPE_NONE, 2,
936                   GTK_TYPE_WIDGET,
937                   G_TYPE_UINT);
938
939   /**
940    * GtkNotebook::create-window:
941    * @notebook: the #GtkNotebook emitting the signal
942    * @page: the tab of @notebook that is being detached
943    * @x: the X coordinate where the drop happens
944    * @y: the Y coordinate where the drop happens
945    *
946    * The ::create-window signal is emitted when a detachable
947    * tab is dropped on the root window. 
948    *
949    * A handler for this signal can create a window containing 
950    * a notebook where the tab will be attached. It is also 
951    * responsible for moving/resizing the window and adding the 
952    * necessary properties to the notebook (e.g. the 
953    * #GtkNotebook:group ).
954    *
955    * Returns: a #GtkNotebook that @page should be added to, or %NULL.
956    *
957    * Since: 2.12
958    */
959   notebook_signals[CREATE_WINDOW] = 
960     g_signal_new (I_("create-window"),
961                   G_TYPE_FROM_CLASS (gobject_class),
962                   G_SIGNAL_RUN_LAST,
963                   G_STRUCT_OFFSET (GtkNotebookClass, create_window),
964                   gtk_object_handled_accumulator, NULL,
965                   _gtk_marshal_OBJECT__OBJECT_INT_INT,
966                   GTK_TYPE_NOTEBOOK, 3,
967                   GTK_TYPE_WIDGET, G_TYPE_INT, G_TYPE_INT);
968  
969   binding_set = gtk_binding_set_by_class (class);
970   gtk_binding_entry_add_signal (binding_set,
971                                 GDK_KEY_space, 0,
972                                 "select-page", 1, 
973                                 G_TYPE_BOOLEAN, FALSE);
974   gtk_binding_entry_add_signal (binding_set,
975                                 GDK_KEY_KP_Space, 0,
976                                 "select-page", 1, 
977                                 G_TYPE_BOOLEAN, FALSE);
978   
979   gtk_binding_entry_add_signal (binding_set,
980                                 GDK_KEY_Home, 0,
981                                 "focus-tab", 1, 
982                                 GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_FIRST);
983   gtk_binding_entry_add_signal (binding_set,
984                                 GDK_KEY_KP_Home, 0,
985                                 "focus-tab", 1, 
986                                 GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_FIRST);
987   gtk_binding_entry_add_signal (binding_set,
988                                 GDK_KEY_End, 0,
989                                 "focus-tab", 1, 
990                                 GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_LAST);
991   gtk_binding_entry_add_signal (binding_set,
992                                 GDK_KEY_KP_End, 0,
993                                 "focus-tab", 1, 
994                                 GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_LAST);
995
996   gtk_binding_entry_add_signal (binding_set,
997                                 GDK_KEY_Page_Up, GDK_CONTROL_MASK,
998                                 "change-current-page", 1,
999                                 G_TYPE_INT, -1);
1000   gtk_binding_entry_add_signal (binding_set,
1001                                 GDK_KEY_Page_Down, GDK_CONTROL_MASK,
1002                                 "change-current-page", 1,
1003                                 G_TYPE_INT, 1);
1004
1005   gtk_binding_entry_add_signal (binding_set,
1006                                 GDK_KEY_Page_Up, GDK_CONTROL_MASK | GDK_MOD1_MASK,
1007                                 "change-current-page", 1,
1008                                 G_TYPE_INT, -1);
1009   gtk_binding_entry_add_signal (binding_set,
1010                                 GDK_KEY_Page_Down, GDK_CONTROL_MASK | GDK_MOD1_MASK,
1011                                 "change-current-page", 1,
1012                                 G_TYPE_INT, 1);
1013
1014   add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
1015   add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
1016   add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
1017   add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
1018
1019   add_reorder_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP, FALSE);
1020   add_reorder_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN, FALSE);
1021   add_reorder_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT, FALSE);
1022   add_reorder_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT, FALSE);
1023   add_reorder_bindings (binding_set, GDK_KEY_Home, GTK_DIR_LEFT, TRUE);
1024   add_reorder_bindings (binding_set, GDK_KEY_Home, GTK_DIR_UP, TRUE);
1025   add_reorder_bindings (binding_set, GDK_KEY_End, GTK_DIR_RIGHT, TRUE);
1026   add_reorder_bindings (binding_set, GDK_KEY_End, GTK_DIR_DOWN, TRUE);
1027
1028   add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
1029   add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
1030
1031   g_type_class_add_private (class, sizeof (GtkNotebookPrivate));
1032 }
1033
1034 static void
1035 gtk_notebook_init (GtkNotebook *notebook)
1036 {
1037   GtkNotebookPrivate *priv;
1038
1039   gtk_widget_set_can_focus (GTK_WIDGET (notebook), TRUE);
1040   gtk_widget_set_has_window (GTK_WIDGET (notebook), FALSE);
1041
1042   notebook->priv = G_TYPE_INSTANCE_GET_PRIVATE (notebook,
1043                                                 GTK_TYPE_NOTEBOOK,
1044                                                 GtkNotebookPrivate);
1045   priv = notebook->priv;
1046
1047   priv->cur_page = NULL;
1048   priv->children = NULL;
1049   priv->first_tab = NULL;
1050   priv->focus_tab = NULL;
1051   priv->event_window = NULL;
1052   priv->menu = NULL;
1053
1054   priv->tab_hborder = 2;
1055   priv->tab_vborder = 2;
1056
1057   priv->show_tabs = TRUE;
1058   priv->show_border = TRUE;
1059   priv->tab_pos = GTK_POS_TOP;
1060   priv->scrollable = FALSE;
1061   priv->in_child = 0;
1062   priv->click_child = 0;
1063   priv->button = 0;
1064   priv->need_timer = 0;
1065   priv->child_has_focus = FALSE;
1066   priv->have_visible_child = FALSE;
1067   priv->focus_out = FALSE;
1068
1069   priv->has_before_previous = 1;
1070   priv->has_before_next     = 0;
1071   priv->has_after_previous  = 0;
1072   priv->has_after_next      = 1;
1073
1074   priv->group = 0;
1075   priv->pressed_button = -1;
1076   priv->dnd_timer = 0;
1077   priv->switch_tab_timer = 0;
1078   priv->source_targets = gtk_target_list_new (notebook_targets,
1079                                               G_N_ELEMENTS (notebook_targets));
1080   priv->operation = DRAG_OPERATION_NONE;
1081   priv->detached_tab = NULL;
1082   priv->during_detach = FALSE;
1083   priv->has_scrolled = FALSE;
1084
1085   gtk_drag_dest_set (GTK_WIDGET (notebook), 0,
1086                      notebook_targets, G_N_ELEMENTS (notebook_targets),
1087                      GDK_ACTION_MOVE);
1088
1089   g_signal_connect (G_OBJECT (notebook), "drag-failed",
1090                     G_CALLBACK (gtk_notebook_drag_failed), NULL);
1091
1092   gtk_drag_dest_set_track_motion (GTK_WIDGET (notebook), TRUE);
1093 }
1094
1095 static void
1096 gtk_notebook_buildable_init (GtkBuildableIface *iface)
1097 {
1098   iface->add_child = gtk_notebook_buildable_add_child;
1099 }
1100
1101 static void
1102 gtk_notebook_buildable_add_child (GtkBuildable  *buildable,
1103                                   GtkBuilder    *builder,
1104                                   GObject       *child,
1105                                   const gchar   *type)
1106 {
1107   GtkNotebook *notebook = GTK_NOTEBOOK (buildable);
1108
1109   if (type && strcmp (type, "tab") == 0)
1110     {
1111       GtkWidget * page;
1112
1113       page = gtk_notebook_get_nth_page (notebook, -1);
1114       /* To set the tab label widget, we must have already a child
1115        * inside the tab container. */
1116       g_assert (page != NULL);
1117       gtk_notebook_set_tab_label (notebook, page, GTK_WIDGET (child));
1118     }
1119   else if (type && strcmp (type, "action-start") == 0)
1120     {
1121       gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_START);
1122     }
1123   else if (type && strcmp (type, "action-end") == 0)
1124     {
1125       gtk_notebook_set_action_widget (notebook, GTK_WIDGET (child), GTK_PACK_END);
1126     }
1127   else if (!type)
1128     gtk_notebook_append_page (notebook, GTK_WIDGET (child), NULL);
1129   else
1130     GTK_BUILDER_WARN_INVALID_CHILD_TYPE (notebook, type);
1131 }
1132
1133 static gboolean
1134 gtk_notebook_select_page (GtkNotebook *notebook,
1135                           gboolean     move_focus)
1136 {
1137   GtkNotebookPrivate *priv = notebook->priv;
1138
1139   if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && priv->show_tabs)
1140     {
1141       gtk_notebook_page_select (notebook, move_focus);
1142       return TRUE;
1143     }
1144   else
1145     return FALSE;
1146 }
1147
1148 static gboolean
1149 gtk_notebook_focus_tab (GtkNotebook       *notebook,
1150                         GtkNotebookTab     type)
1151 {
1152   GtkNotebookPrivate *priv = notebook->priv;
1153   GList *list;
1154
1155   if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && priv->show_tabs)
1156     {
1157       switch (type)
1158         {
1159         case GTK_NOTEBOOK_TAB_FIRST:
1160           list = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, TRUE);
1161           if (list)
1162             gtk_notebook_switch_focus_tab (notebook, list);
1163           break;
1164         case GTK_NOTEBOOK_TAB_LAST:
1165           list = gtk_notebook_search_page (notebook, NULL, STEP_PREV, TRUE);
1166           if (list)
1167             gtk_notebook_switch_focus_tab (notebook, list);
1168           break;
1169         }
1170
1171       return TRUE;
1172     }
1173   else
1174     return FALSE;
1175 }
1176
1177 static gboolean
1178 gtk_notebook_change_current_page (GtkNotebook *notebook,
1179                                   gint         offset)
1180 {
1181   GtkNotebookPrivate *priv = notebook->priv;
1182   GList *current = NULL;
1183
1184   if (!priv->show_tabs)
1185     return FALSE;
1186
1187   if (priv->cur_page)
1188     current = g_list_find (priv->children, priv->cur_page);
1189
1190   while (offset != 0)
1191     {
1192       current = gtk_notebook_search_page (notebook, current,
1193                                           offset < 0 ? STEP_PREV : STEP_NEXT,
1194                                           TRUE);
1195
1196       if (!current)
1197         {
1198           gboolean wrap_around;
1199
1200           g_object_get (gtk_widget_get_settings (GTK_WIDGET (notebook)),
1201                         "gtk-keynav-wrap-around", &wrap_around,
1202                         NULL);
1203
1204           if (wrap_around)
1205             current = gtk_notebook_search_page (notebook, NULL,
1206                                                 offset < 0 ? STEP_PREV : STEP_NEXT,
1207                                                 TRUE);
1208           else
1209             break;
1210         }
1211
1212       offset += offset < 0 ? 1 : -1;
1213     }
1214
1215   if (current)
1216     gtk_notebook_switch_page (notebook, current->data);
1217   else
1218     gtk_widget_error_bell (GTK_WIDGET (notebook));
1219
1220   return TRUE;
1221 }
1222
1223 static GtkDirectionType
1224 get_effective_direction (GtkNotebook      *notebook,
1225                          GtkDirectionType  direction)
1226 {
1227   GtkNotebookPrivate *priv = notebook->priv;
1228
1229   /* Remap the directions into the effective direction it would be for a
1230    * GTK_POS_TOP notebook
1231    */
1232
1233 #define D(rest) GTK_DIR_##rest
1234
1235   static const GtkDirectionType translate_direction[2][4][6] = {
1236     /* LEFT */   {{ D(TAB_FORWARD),  D(TAB_BACKWARD), D(LEFT), D(RIGHT), D(UP),   D(DOWN) },
1237     /* RIGHT */  { D(TAB_BACKWARD), D(TAB_FORWARD),  D(LEFT), D(RIGHT), D(DOWN), D(UP)   },
1238     /* TOP */    { D(TAB_FORWARD),  D(TAB_BACKWARD), D(UP),   D(DOWN),  D(LEFT), D(RIGHT) },
1239     /* BOTTOM */ { D(TAB_BACKWARD), D(TAB_FORWARD),  D(DOWN), D(UP),    D(LEFT), D(RIGHT) }},
1240     /* LEFT */  {{ D(TAB_BACKWARD), D(TAB_FORWARD),  D(LEFT), D(RIGHT), D(DOWN), D(UP)   },
1241     /* RIGHT */  { D(TAB_FORWARD),  D(TAB_BACKWARD), D(LEFT), D(RIGHT), D(UP),   D(DOWN) },
1242     /* TOP */    { D(TAB_FORWARD),  D(TAB_BACKWARD), D(UP),   D(DOWN),  D(RIGHT), D(LEFT) },
1243     /* BOTTOM */ { D(TAB_BACKWARD), D(TAB_FORWARD),  D(DOWN), D(UP),    D(RIGHT), D(LEFT) }},
1244   };
1245
1246 #undef D
1247
1248   int text_dir = gtk_widget_get_direction (GTK_WIDGET (notebook)) == GTK_TEXT_DIR_RTL ? 1 : 0;
1249
1250   return translate_direction[text_dir][priv->tab_pos][direction];
1251 }
1252
1253 static gint
1254 get_effective_tab_pos (GtkNotebook *notebook)
1255 {
1256   GtkNotebookPrivate *priv = notebook->priv;
1257
1258   if (gtk_widget_get_direction (GTK_WIDGET (notebook)) == GTK_TEXT_DIR_RTL)
1259     {
1260       switch (priv->tab_pos)
1261         {
1262         case GTK_POS_LEFT:
1263           return GTK_POS_RIGHT;
1264         case GTK_POS_RIGHT:
1265           return GTK_POS_LEFT;
1266         default: ;
1267         }
1268     }
1269
1270   return priv->tab_pos;
1271 }
1272
1273 static gint
1274 get_tab_gap_pos (GtkNotebook *notebook)
1275 {
1276   gint tab_pos = get_effective_tab_pos (notebook);
1277   gint gap_side = GTK_POS_BOTTOM;
1278   
1279   switch (tab_pos)
1280     {
1281     case GTK_POS_TOP:
1282       gap_side = GTK_POS_BOTTOM;
1283       break;
1284     case GTK_POS_BOTTOM:
1285       gap_side = GTK_POS_TOP;
1286       break;
1287     case GTK_POS_LEFT:
1288       gap_side = GTK_POS_RIGHT;
1289       break;
1290     case GTK_POS_RIGHT:
1291       gap_side = GTK_POS_LEFT;
1292       break;
1293     }
1294
1295   return gap_side;
1296 }
1297
1298 static void
1299 gtk_notebook_move_focus_out (GtkNotebook      *notebook,
1300                              GtkDirectionType  direction_type)
1301 {
1302   GtkNotebookPrivate *priv = notebook->priv;
1303   GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type);
1304   GtkWidget *toplevel;
1305   
1306   if (gtk_container_get_focus_child (GTK_CONTAINER (notebook)) && effective_direction == GTK_DIR_UP)
1307     if (focus_tabs_in (notebook))
1308       return;
1309   if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && effective_direction == GTK_DIR_DOWN)
1310     if (focus_child_in (notebook, GTK_DIR_TAB_FORWARD))
1311       return;
1312
1313   /* At this point, we know we should be focusing out of the notebook entirely. We
1314    * do this by setting a flag, then propagating the focus motion to the notebook.
1315    */
1316   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (notebook));
1317   if (!gtk_widget_is_toplevel (toplevel))
1318     return;
1319
1320   g_object_ref (notebook);
1321
1322   priv->focus_out = TRUE;
1323   g_signal_emit_by_name (toplevel, "move-focus", direction_type);
1324   priv->focus_out = FALSE;
1325
1326   g_object_unref (notebook);
1327 }
1328
1329 static gint
1330 reorder_tab (GtkNotebook *notebook, GList *position, GList *tab)
1331 {
1332   GtkNotebookPrivate *priv = notebook->priv;
1333   GList *elem;
1334
1335   if (position == tab)
1336     return g_list_position (priv->children, tab);
1337
1338   /* check that we aren't inserting the tab in the
1339    * same relative position, taking packing into account */
1340   elem = (position) ? position->prev : g_list_last (priv->children);
1341
1342   while (elem && elem != tab && GTK_NOTEBOOK_PAGE (elem)->pack != GTK_NOTEBOOK_PAGE (tab)->pack)
1343     elem = elem->prev;
1344
1345   if (elem == tab)
1346     return g_list_position (priv->children, tab);
1347
1348   /* now actually reorder the tab */
1349   if (priv->first_tab == tab)
1350     priv->first_tab = gtk_notebook_search_page (notebook, priv->first_tab,
1351                                                     STEP_NEXT, TRUE);
1352
1353   priv->children = g_list_remove_link (priv->children, tab);
1354
1355   if (!position)
1356     elem = g_list_last (priv->children);
1357   else
1358     {
1359       elem = position->prev;
1360       position->prev = tab;
1361     }
1362
1363   if (elem)
1364     elem->next = tab;
1365   else
1366     priv->children = tab;
1367
1368   tab->prev = elem;
1369   tab->next = position;
1370
1371   return g_list_position (priv->children, tab);
1372 }
1373
1374 static gboolean
1375 gtk_notebook_reorder_tab (GtkNotebook      *notebook,
1376                           GtkDirectionType  direction_type,
1377                           gboolean          move_to_last)
1378 {
1379   GtkNotebookPrivate *priv = notebook->priv;
1380   GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type);
1381   GtkNotebookPage *page;
1382   GList *last, *child;
1383   gint page_num;
1384
1385   if (!gtk_widget_is_focus (GTK_WIDGET (notebook)) || !priv->show_tabs)
1386     return FALSE;
1387
1388   if (!priv->cur_page ||
1389       !priv->cur_page->reorderable)
1390     return FALSE;
1391
1392   if (effective_direction != GTK_DIR_LEFT &&
1393       effective_direction != GTK_DIR_RIGHT)
1394     return FALSE;
1395
1396   if (move_to_last)
1397     {
1398       child = priv->focus_tab;
1399
1400       do
1401         {
1402           last = child;
1403           child = gtk_notebook_search_page (notebook, last, 
1404                                             (effective_direction == GTK_DIR_RIGHT) ? STEP_NEXT : STEP_PREV,
1405                                             TRUE);
1406         }
1407       while (child && GTK_NOTEBOOK_PAGE (last)->pack == GTK_NOTEBOOK_PAGE (child)->pack);
1408
1409       child = last;
1410     }
1411   else
1412     child = gtk_notebook_search_page (notebook, priv->focus_tab,
1413                                       (effective_direction == GTK_DIR_RIGHT) ? STEP_NEXT : STEP_PREV,
1414                                       TRUE);
1415
1416   if (!child || child->data == priv->cur_page)
1417     return FALSE;
1418
1419   page = child->data;
1420
1421   if (page->pack == priv->cur_page->pack)
1422     {
1423       if (effective_direction == GTK_DIR_RIGHT)
1424         page_num = reorder_tab (notebook, (page->pack == GTK_PACK_START) ? child->next : child, priv->focus_tab);
1425       else
1426         page_num = reorder_tab (notebook, (page->pack == GTK_PACK_START) ? child : child->next, priv->focus_tab);
1427
1428       gtk_notebook_pages_allocate (notebook);
1429
1430       g_signal_emit (notebook,
1431                      notebook_signals[PAGE_REORDERED],
1432                      0,
1433                      ((GtkNotebookPage *) priv->focus_tab->data)->child,
1434                      page_num);
1435
1436       return TRUE;
1437     }
1438
1439   return FALSE;
1440 }
1441
1442 /**
1443  * gtk_notebook_new:
1444  * 
1445  * Creates a new #GtkNotebook widget with no pages.
1446
1447  * Return value: the newly created #GtkNotebook
1448  **/
1449 GtkWidget*
1450 gtk_notebook_new (void)
1451 {
1452   return g_object_new (GTK_TYPE_NOTEBOOK, NULL);
1453 }
1454
1455 /* Private GObject Methods :
1456  *
1457  * gtk_notebook_set_property
1458  * gtk_notebook_get_property
1459  */
1460 static void
1461 gtk_notebook_set_property (GObject         *object,
1462                            guint            prop_id,
1463                            const GValue    *value,
1464                            GParamSpec      *pspec)
1465 {
1466   GtkNotebook *notebook;
1467
1468   notebook = GTK_NOTEBOOK (object);
1469
1470   switch (prop_id)
1471     {
1472     case PROP_SHOW_TABS:
1473       gtk_notebook_set_show_tabs (notebook, g_value_get_boolean (value));
1474       break;
1475     case PROP_SHOW_BORDER:
1476       gtk_notebook_set_show_border (notebook, g_value_get_boolean (value));
1477       break;
1478     case PROP_SCROLLABLE:
1479       gtk_notebook_set_scrollable (notebook, g_value_get_boolean (value));
1480       break;
1481     case PROP_ENABLE_POPUP:
1482       if (g_value_get_boolean (value))
1483         gtk_notebook_popup_enable (notebook);
1484       else
1485         gtk_notebook_popup_disable (notebook);
1486       break;
1487     case PROP_PAGE:
1488       gtk_notebook_set_current_page (notebook, g_value_get_int (value));
1489       break;
1490     case PROP_TAB_POS:
1491       gtk_notebook_set_tab_pos (notebook, g_value_get_enum (value));
1492       break;
1493     case PROP_GROUP_NAME:
1494       gtk_notebook_set_group_name (notebook, g_value_get_string (value));
1495       break;
1496     default:
1497       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1498       break;
1499     }
1500 }
1501
1502 static void
1503 gtk_notebook_get_property (GObject         *object,
1504                            guint            prop_id,
1505                            GValue          *value,
1506                            GParamSpec      *pspec)
1507 {
1508   GtkNotebook *notebook = GTK_NOTEBOOK (object);
1509   GtkNotebookPrivate *priv = notebook->priv;
1510
1511   switch (prop_id)
1512     {
1513     case PROP_SHOW_TABS:
1514       g_value_set_boolean (value, priv->show_tabs);
1515       break;
1516     case PROP_SHOW_BORDER:
1517       g_value_set_boolean (value, priv->show_border);
1518       break;
1519     case PROP_SCROLLABLE:
1520       g_value_set_boolean (value, priv->scrollable);
1521       break;
1522     case PROP_ENABLE_POPUP:
1523       g_value_set_boolean (value, priv->menu != NULL);
1524       break;
1525     case PROP_PAGE:
1526       g_value_set_int (value, gtk_notebook_get_current_page (notebook));
1527       break;
1528     case PROP_TAB_POS:
1529       g_value_set_enum (value, priv->tab_pos);
1530       break;
1531     case PROP_GROUP_NAME:
1532       g_value_set_string (value, gtk_notebook_get_group_name (notebook));
1533       break;
1534     default:
1535       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1536       break;
1537     }
1538 }
1539
1540 /* Private GtkWidget Methods :
1541  *
1542  * gtk_notebook_destroy
1543  * gtk_notebook_map
1544  * gtk_notebook_unmap
1545  * gtk_notebook_realize
1546  * gtk_notebook_size_request
1547  * gtk_notebook_size_allocate
1548  * gtk_notebook_draw
1549  * gtk_notebook_scroll
1550  * gtk_notebook_button_press
1551  * gtk_notebook_button_release
1552  * gtk_notebook_popup_menu
1553  * gtk_notebook_leave_notify
1554  * gtk_notebook_motion_notify
1555  * gtk_notebook_focus_in
1556  * gtk_notebook_focus_out
1557  * gtk_notebook_style_set
1558  * gtk_notebook_drag_begin
1559  * gtk_notebook_drag_end
1560  * gtk_notebook_drag_failed
1561  * gtk_notebook_drag_motion
1562  * gtk_notebook_drag_drop
1563  * gtk_notebook_drag_data_get
1564  * gtk_notebook_drag_data_received
1565  */
1566 static void
1567 gtk_notebook_destroy (GtkWidget *widget)
1568 {
1569   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1570   GtkNotebookPrivate *priv = notebook->priv;
1571
1572   if (priv->menu)
1573     gtk_notebook_popup_disable (notebook);
1574
1575   if (priv->source_targets)
1576     {
1577       gtk_target_list_unref (priv->source_targets);
1578       priv->source_targets = NULL;
1579     }
1580
1581   if (priv->switch_tab_timer)
1582     {
1583       g_source_remove (priv->switch_tab_timer);
1584       priv->switch_tab_timer = 0;
1585     }
1586
1587   GTK_WIDGET_CLASS (gtk_notebook_parent_class)->destroy (widget);
1588 }
1589
1590 static gboolean
1591 gtk_notebook_get_event_window_position (GtkNotebook  *notebook,
1592                                         GdkRectangle *rectangle)
1593 {
1594   GtkNotebookPrivate *priv = notebook->priv;
1595   GtkAllocation allocation, action_allocation;
1596   GtkWidget *widget = GTK_WIDGET (notebook);
1597   guint border_width = gtk_container_get_border_width (GTK_CONTAINER (notebook));
1598   GtkNotebookPage *visible_page = NULL;
1599   GList *tmp_list;
1600   gint tab_pos = get_effective_tab_pos (notebook);
1601   gboolean is_rtl;
1602   gint i;
1603
1604   for (tmp_list = priv->children; tmp_list; tmp_list = tmp_list->next)
1605     {
1606       GtkNotebookPage *page = tmp_list->data;
1607       if (gtk_widget_get_visible (page->child))
1608         {
1609           visible_page = page;
1610           break;
1611         }
1612     }
1613
1614   if (priv->show_tabs && visible_page)
1615     {
1616       if (rectangle)
1617         {
1618           gtk_widget_get_allocation (widget, &allocation);
1619
1620           is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
1621           rectangle->x = allocation.x + border_width;
1622           rectangle->y = allocation.y + border_width;
1623
1624           switch (tab_pos)
1625             {
1626             case GTK_POS_TOP:
1627             case GTK_POS_BOTTOM:
1628               rectangle->width = allocation.width - 2 * border_width;
1629               rectangle->height = visible_page->requisition.height;
1630               if (tab_pos == GTK_POS_BOTTOM)
1631                 rectangle->y += allocation.height - 2 * border_width - rectangle->height;
1632
1633               for (i = 0; i < N_ACTION_WIDGETS; i++)
1634                 {
1635                   if (priv->action_widget[i] &&
1636                       gtk_widget_get_visible (priv->action_widget[i]))
1637                     {
1638                       gtk_widget_get_allocation (priv->action_widget[i], &action_allocation);
1639
1640                       rectangle->width -= action_allocation.width;
1641                       if ((!is_rtl && i == ACTION_WIDGET_START) ||
1642                           (is_rtl && i == ACTION_WIDGET_END))
1643                         rectangle->x += action_allocation.width;
1644                     }
1645                 }
1646               break;
1647             case GTK_POS_LEFT:
1648             case GTK_POS_RIGHT:
1649               rectangle->width = visible_page->requisition.width;
1650               rectangle->height = allocation.height - 2 * border_width;
1651               if (tab_pos == GTK_POS_RIGHT)
1652                 rectangle->x += allocation.width - 2 * border_width - rectangle->width;
1653
1654               for (i = 0; i < N_ACTION_WIDGETS; i++)
1655                 {
1656                   if (priv->action_widget[i] &&
1657                       gtk_widget_get_visible (priv->action_widget[i]))
1658                     {
1659                       gtk_widget_get_allocation (priv->action_widget[i], &action_allocation);
1660
1661                       rectangle->height -= action_allocation.height;
1662
1663                       if (i == ACTION_WIDGET_START)
1664                         rectangle->y += action_allocation.height;
1665                     }
1666                 }
1667               break;
1668             }
1669         }
1670
1671       return TRUE;
1672     }
1673   else
1674     {
1675       if (rectangle)
1676         {
1677           rectangle->x = rectangle->y = 0;
1678           rectangle->width = rectangle->height = 10;
1679         }
1680     }
1681
1682   return FALSE;
1683 }
1684
1685 static void
1686 gtk_notebook_map (GtkWidget *widget)
1687 {
1688   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1689   GtkNotebookPrivate *priv = notebook->priv;
1690   GtkNotebookPage *page;
1691   GList *children;
1692   gint i;
1693
1694   gtk_widget_set_mapped (widget, TRUE);
1695
1696   if (priv->cur_page &&
1697       gtk_widget_get_visible (priv->cur_page->child) &&
1698       !gtk_widget_get_mapped (priv->cur_page->child))
1699     gtk_widget_map (priv->cur_page->child);
1700
1701   for (i = 0; i < N_ACTION_WIDGETS; i++)
1702     {
1703       if (priv->action_widget[i] &&
1704           gtk_widget_get_visible (priv->action_widget[i]) &&
1705           gtk_widget_get_child_visible (priv->action_widget[i]) &&
1706           !gtk_widget_get_mapped (priv->action_widget[i]))
1707         gtk_widget_map (priv->action_widget[i]);
1708     }
1709
1710   if (priv->scrollable)
1711     gtk_notebook_pages_allocate (notebook);
1712   else
1713     {
1714       children = priv->children;
1715
1716       while (children)
1717         {
1718           page = children->data;
1719           children = children->next;
1720
1721           if (page->tab_label &&
1722               gtk_widget_get_visible (page->tab_label) &&
1723               !gtk_widget_get_mapped (page->tab_label))
1724             gtk_widget_map (page->tab_label);
1725         }
1726     }
1727
1728   if (gtk_notebook_get_event_window_position (notebook, NULL))
1729     gdk_window_show_unraised (priv->event_window);
1730 }
1731
1732 static void
1733 gtk_notebook_unmap (GtkWidget *widget)
1734 {
1735   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1736   GtkNotebookPrivate *priv = notebook->priv;
1737
1738   stop_scrolling (notebook);
1739
1740   gtk_widget_set_mapped (widget, FALSE);
1741
1742   gdk_window_hide (priv->event_window);
1743
1744   GTK_WIDGET_CLASS (gtk_notebook_parent_class)->unmap (widget);
1745 }
1746
1747 static void
1748 gtk_notebook_realize (GtkWidget *widget)
1749 {
1750   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1751   GtkNotebookPrivate *priv = notebook->priv;
1752   GdkWindow *window;
1753   GdkWindowAttr attributes;
1754   gint attributes_mask;
1755   GdkRectangle event_window_pos;
1756
1757   gtk_widget_set_realized (widget, TRUE);
1758
1759   gtk_notebook_get_event_window_position (notebook, &event_window_pos);
1760
1761   window = gtk_widget_get_parent_window (widget);
1762   gtk_widget_set_window (widget, window);
1763   g_object_ref (window);
1764
1765   attributes.window_type = GDK_WINDOW_CHILD;
1766   attributes.x = event_window_pos.x;
1767   attributes.y = event_window_pos.y;
1768   attributes.width = event_window_pos.width;
1769   attributes.height = event_window_pos.height;
1770   attributes.wclass = GDK_INPUT_ONLY;
1771   attributes.event_mask = gtk_widget_get_events (widget);
1772   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1773                             GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK |
1774                             GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1775   attributes_mask = GDK_WA_X | GDK_WA_Y;
1776
1777   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1778                                            &attributes, attributes_mask);
1779   gdk_window_set_user_data (priv->event_window, notebook);
1780
1781   gtk_widget_style_attach (widget);
1782 }
1783
1784 static void
1785 gtk_notebook_unrealize (GtkWidget *widget)
1786 {
1787   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1788   GtkNotebookPrivate *priv = notebook->priv;
1789
1790   gdk_window_set_user_data (priv->event_window, NULL);
1791   gdk_window_destroy (priv->event_window);
1792   priv->event_window = NULL;
1793
1794   if (priv->drag_window)
1795     {
1796       gdk_window_set_user_data (priv->drag_window, NULL);
1797       gdk_window_destroy (priv->drag_window);
1798       priv->drag_window = NULL;
1799     }
1800
1801   GTK_WIDGET_CLASS (gtk_notebook_parent_class)->unrealize (widget);
1802 }
1803
1804 static void
1805 gtk_notebook_size_request (GtkWidget      *widget,
1806                            GtkRequisition *requisition)
1807 {
1808   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
1809   GtkNotebookPrivate *priv = notebook->priv;
1810   GtkNotebookPage *page;
1811   GList *children;
1812   GtkRequisition child_requisition;
1813   GtkRequisition action_widget_requisition[2] = { { 0 }, { 0 } };
1814   gboolean switch_page = FALSE;
1815   gint vis_pages;
1816   gint focus_width;
1817   gint tab_overlap;
1818   gint tab_curvature;
1819   gint arrow_spacing;
1820   gint scroll_arrow_hlength;
1821   gint scroll_arrow_vlength;
1822   guint border_width;
1823
1824   gtk_widget_style_get (widget,
1825                         "focus-line-width", &focus_width,
1826                         "tab-overlap", &tab_overlap,
1827                         "tab-curvature", &tab_curvature,
1828                         "arrow-spacing", &arrow_spacing,
1829                         "scroll-arrow-hlength", &scroll_arrow_hlength,
1830                         "scroll-arrow-vlength", &scroll_arrow_vlength,
1831                         NULL);
1832
1833   requisition->width = 0;
1834   requisition->height = 0;
1835
1836   for (children = priv->children, vis_pages = 0; children;
1837        children = children->next)
1838     {
1839       GtkWidget *parent;
1840       page = children->data;
1841
1842       if (gtk_widget_get_visible (page->child))
1843         {
1844           vis_pages++;
1845           gtk_widget_get_preferred_size (page->child,
1846                                          &child_requisition, NULL);
1847
1848           requisition->width = MAX (requisition->width,
1849                                            child_requisition.width);
1850           requisition->height = MAX (requisition->height,
1851                                             child_requisition.height);
1852
1853           if (priv->menu && page->menu_label)
1854             {
1855               parent = gtk_widget_get_parent (page->menu_label);
1856               if (parent && !gtk_widget_get_visible (parent))
1857                 gtk_widget_show (parent);
1858             }
1859         }
1860       else
1861         {
1862           if (page == priv->cur_page)
1863             switch_page = TRUE;
1864
1865           if (priv->menu && page->menu_label)
1866             {
1867               parent = gtk_widget_get_parent (page->menu_label);
1868               if (parent && gtk_widget_get_visible (parent))
1869                 gtk_widget_hide (parent);
1870             }
1871         }
1872     }
1873
1874   if (priv->show_border || priv->show_tabs)
1875     {
1876       GtkStyle *style;
1877
1878       style = gtk_widget_get_style (widget);
1879
1880       requisition->width += style->xthickness * 2;
1881       requisition->height += style->ythickness * 2;
1882
1883       if (priv->show_tabs)
1884         {
1885           gint tab_width = 0;
1886           gint tab_height = 0;
1887           gint tab_max = 0;
1888           gint padding;
1889           gint i;
1890           gint action_width = 0;
1891           gint action_height = 0;
1892           
1893           for (children = priv->children; children;
1894                children = children->next)
1895             {
1896               page = children->data;
1897               
1898               if (gtk_widget_get_visible (page->child))
1899                 {
1900                   if (!gtk_widget_get_visible (page->tab_label))
1901                     gtk_widget_show (page->tab_label);
1902
1903                   gtk_widget_get_preferred_size (page->tab_label,
1904                                                  &child_requisition, NULL);
1905
1906                   page->requisition.width = child_requisition.width + 2 * style->xthickness;
1907                   page->requisition.height = child_requisition.height + 2 * style->ythickness;
1908
1909                   switch (priv->tab_pos)
1910                     {
1911                     case GTK_POS_TOP:
1912                     case GTK_POS_BOTTOM:
1913                       page->requisition.height += 2 * (priv->tab_vborder +
1914                                                        focus_width);
1915                       tab_height = MAX (tab_height, page->requisition.height);
1916                       tab_max = MAX (tab_max, page->requisition.width);
1917                       break;
1918                     case GTK_POS_LEFT:
1919                     case GTK_POS_RIGHT:
1920                       page->requisition.width += 2 * (priv->tab_hborder +
1921                                                       focus_width);
1922                       tab_width = MAX (tab_width, page->requisition.width);
1923                       tab_max = MAX (tab_max, page->requisition.height);
1924                       break;
1925                     }
1926                 }
1927               else if (gtk_widget_get_visible (page->tab_label))
1928                 gtk_widget_hide (page->tab_label);
1929             }
1930
1931           children = priv->children;
1932
1933           if (vis_pages)
1934             {
1935               for (i = 0; i < N_ACTION_WIDGETS; i++)
1936                 {
1937                   if (priv->action_widget[i])
1938                     {
1939                       gtk_widget_get_preferred_size (priv->action_widget[i],
1940                                                      &action_widget_requisition[i], NULL);
1941                       action_widget_requisition[i].width += style->xthickness;
1942                       action_widget_requisition[i].height += style->ythickness;
1943                     }
1944                 }
1945
1946               switch (priv->tab_pos)
1947                 {
1948                 case GTK_POS_TOP:
1949                 case GTK_POS_BOTTOM:
1950                   if (tab_height == 0)
1951                     break;
1952
1953                   if (priv->scrollable && vis_pages > 1 &&
1954                       requisition->width < tab_width)
1955                     tab_height = MAX (tab_height, scroll_arrow_hlength);
1956
1957                   tab_height = MAX (tab_height, action_widget_requisition[ACTION_WIDGET_START].height);
1958                   tab_height = MAX (tab_height, action_widget_requisition[ACTION_WIDGET_END].height);
1959
1960                   padding = 2 * (tab_curvature + focus_width +
1961                                  priv->tab_hborder) - tab_overlap;
1962                   tab_max += padding;
1963                   while (children)
1964                     {
1965                       page = children->data;
1966                       children = children->next;
1967                   
1968                       if (!gtk_widget_get_visible (page->child))
1969                         continue;
1970
1971                       if (priv->homogeneous)
1972                         page->requisition.width = tab_max;
1973                       else
1974                         page->requisition.width += padding;
1975
1976                       tab_width += page->requisition.width;
1977                       page->requisition.height = tab_height;
1978                     }
1979
1980                   if (priv->scrollable && vis_pages > 1 &&
1981                       requisition->width < tab_width)
1982                     tab_width = tab_max + 2 * (scroll_arrow_hlength + arrow_spacing);
1983
1984                   action_width += action_widget_requisition[ACTION_WIDGET_START].width;
1985                   action_width += action_widget_requisition[ACTION_WIDGET_END].width;
1986                   if (priv->homogeneous && !priv->scrollable)
1987                     requisition->width = MAX (requisition->width,
1988                                                      vis_pages * tab_max +
1989                                                      tab_overlap + action_width);
1990                   else
1991                     requisition->width = MAX (requisition->width,
1992                                                      tab_width + tab_overlap + action_width);
1993
1994                   requisition->height += tab_height;
1995                   break;
1996                 case GTK_POS_LEFT:
1997                 case GTK_POS_RIGHT:
1998                   if (tab_width == 0)
1999                     break;
2000
2001                   if (priv->scrollable && vis_pages > 1 &&
2002                       requisition->height < tab_height)
2003                     tab_width = MAX (tab_width,
2004                                      arrow_spacing + 2 * scroll_arrow_vlength);
2005
2006                   tab_width = MAX (tab_width, action_widget_requisition[ACTION_WIDGET_START].width);
2007                   tab_width = MAX (tab_width, action_widget_requisition[ACTION_WIDGET_END].width);
2008
2009                   padding = 2 * (tab_curvature + focus_width +
2010                                  priv->tab_vborder) - tab_overlap;
2011                   tab_max += padding;
2012
2013                   while (children)
2014                     {
2015                       page = children->data;
2016                       children = children->next;
2017
2018                       if (!gtk_widget_get_visible (page->child))
2019                         continue;
2020
2021                       page->requisition.width = tab_width;
2022
2023                       if (priv->homogeneous)
2024                         page->requisition.height = tab_max;
2025                       else
2026                         page->requisition.height += padding;
2027
2028                       tab_height += page->requisition.height;
2029                     }
2030
2031                   if (priv->scrollable && vis_pages > 1 &&
2032                       requisition->height < tab_height)
2033                     tab_height = tab_max + (2 * scroll_arrow_vlength + arrow_spacing);
2034                   action_height += action_widget_requisition[ACTION_WIDGET_START].height;
2035                   action_height += action_widget_requisition[ACTION_WIDGET_END].height;
2036
2037                   if (priv->homogeneous && !priv->scrollable)
2038                     requisition->height =
2039                       MAX (requisition->height,
2040                            vis_pages * tab_max + tab_overlap + action_height);
2041                   else
2042                     requisition->height =
2043                       MAX (requisition->height,
2044                            tab_height + tab_overlap + action_height);
2045
2046                   if (!priv->homogeneous || priv->scrollable)
2047                     vis_pages = 1;
2048                   requisition->height = MAX (requisition->height,
2049                                              vis_pages * tab_max +
2050                                              tab_overlap);
2051
2052                   requisition->width += tab_width;
2053                   break;
2054                 }
2055             }
2056         }
2057       else
2058         {
2059           for (children = priv->children; children;
2060                children = children->next)
2061             {
2062               page = children->data;
2063               
2064               if (page->tab_label && gtk_widget_get_visible (page->tab_label))
2065                 gtk_widget_hide (page->tab_label);
2066             }
2067         }
2068     }
2069
2070   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2071
2072   requisition->width += border_width * 2;
2073   requisition->height += border_width * 2;
2074
2075   if (switch_page)
2076     {
2077       if (vis_pages)
2078         {
2079           for (children = priv->children; children;
2080                children = children->next)
2081             {
2082               page = children->data;
2083               if (gtk_widget_get_visible (page->child))
2084                 {
2085                   gtk_notebook_switch_page (notebook, page);
2086                   break;
2087                 }
2088             }
2089         }
2090       else if (gtk_widget_get_visible (widget))
2091         {
2092           requisition->width  = border_width * 2;
2093           requisition->height = border_width * 2;
2094         }
2095     }
2096   if (vis_pages && !priv->cur_page)
2097     {
2098       children = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, TRUE);
2099       if (children)
2100         {
2101           priv->first_tab = children;
2102           gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (children));
2103         }
2104     }
2105 }
2106
2107 static void
2108 gtk_notebook_size_allocate (GtkWidget     *widget,
2109                             GtkAllocation *allocation)
2110 {
2111   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2112   GtkNotebookPrivate *priv = notebook->priv;
2113   GtkStyle *style;
2114   gint tab_pos = get_effective_tab_pos (notebook);
2115   gboolean is_rtl;
2116   gint focus_width;
2117
2118   style = gtk_widget_get_style (widget);
2119
2120   gtk_widget_style_get (widget, "focus-line-width", &focus_width, NULL);
2121
2122   gtk_widget_set_allocation (widget, allocation);
2123
2124   if (gtk_widget_get_realized (widget))
2125     {
2126       GdkRectangle position;
2127
2128       if (gtk_notebook_get_event_window_position (notebook, &position))
2129         {
2130           gdk_window_move_resize (priv->event_window,
2131                                   position.x, position.y,
2132                                   position.width, position.height);
2133           if (gtk_widget_get_mapped (GTK_WIDGET (notebook)))
2134             gdk_window_show_unraised (priv->event_window);
2135         }
2136       else
2137         gdk_window_hide (priv->event_window);
2138     }
2139
2140   if (priv->children)
2141     {
2142       guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2143       GtkNotebookPage *page;
2144       GtkAllocation child_allocation;
2145       GList *children;
2146       gint i;
2147
2148       child_allocation.x = allocation->x + border_width;
2149       child_allocation.y = allocation->y + border_width;
2150       child_allocation.width = MAX (1, allocation->width - border_width * 2);
2151       child_allocation.height = MAX (1, allocation->height - border_width * 2);
2152
2153       if (priv->show_tabs || priv->show_border)
2154         {
2155           child_allocation.x += style->xthickness;
2156           child_allocation.y += style->ythickness;
2157           child_allocation.width = MAX (1, child_allocation.width - style->xthickness * 2);
2158           child_allocation.height = MAX (1, child_allocation.height - style->ythickness * 2);
2159
2160           if (priv->show_tabs && priv->children && priv->cur_page)
2161             {
2162               switch (tab_pos)
2163                 {
2164                 case GTK_POS_TOP:
2165                   child_allocation.y += priv->cur_page->requisition.height;
2166                 case GTK_POS_BOTTOM:
2167                   child_allocation.height =
2168                     MAX (1, child_allocation.height -
2169                          priv->cur_page->requisition.height);
2170                   break;
2171                 case GTK_POS_LEFT:
2172                   child_allocation.x += priv->cur_page->requisition.width;
2173                 case GTK_POS_RIGHT:
2174                   child_allocation.width =
2175                     MAX (1, child_allocation.width -
2176                          priv->cur_page->requisition.width);
2177                   break;
2178                 }
2179
2180               for (i = 0; i < N_ACTION_WIDGETS; i++)
2181                 {
2182                   GtkAllocation widget_allocation;
2183                   GtkRequisition requisition;
2184                   
2185                   if (!priv->action_widget[i])
2186                     continue;
2187
2188                   widget_allocation.x = allocation->x + border_width;
2189                   widget_allocation.y = allocation->y + border_width;
2190                   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2191
2192                   gtk_widget_get_preferred_size (priv->action_widget[i],
2193                                                  &requisition, NULL);
2194
2195                   switch (tab_pos)
2196                     {
2197                     case GTK_POS_BOTTOM:
2198                       widget_allocation.y += allocation->height - 2 * border_width - priv->cur_page->requisition.height;
2199                       /* fall through */
2200                     case GTK_POS_TOP:
2201                       widget_allocation.width = requisition.width;
2202                       widget_allocation.height = priv->cur_page->requisition.height - style->ythickness;
2203
2204                       if ((i == ACTION_WIDGET_START && is_rtl) ||
2205                           (i == ACTION_WIDGET_END && !is_rtl))
2206                         widget_allocation.x += allocation->width - 2 * border_width - requisition.width;
2207                       if (tab_pos == GTK_POS_TOP) /* no fall through */
2208                           widget_allocation.y += 2 * focus_width;
2209                       break;
2210                     case GTK_POS_RIGHT:
2211                       widget_allocation.x += allocation->width - 2 * border_width - priv->cur_page->requisition.width;
2212                       /* fall through */
2213                     case GTK_POS_LEFT:
2214                       widget_allocation.height = requisition.height;
2215                       widget_allocation.width = priv->cur_page->requisition.width - style->xthickness;
2216
2217                       if (i == ACTION_WIDGET_END)
2218                         widget_allocation.y += allocation->height - 2 * border_width - requisition.height;
2219                       if (tab_pos == GTK_POS_LEFT) /* no fall through */  
2220                         widget_allocation.x += 2 * focus_width;
2221                       break;
2222                     }
2223
2224                   gtk_widget_size_allocate (priv->action_widget[i], &widget_allocation);
2225                 }
2226             }
2227         }
2228
2229       children = priv->children;
2230       while (children)
2231         {
2232           page = children->data;
2233           children = children->next;
2234           
2235           if (gtk_widget_get_visible (page->child))
2236             gtk_widget_size_allocate (page->child, &child_allocation);
2237         }
2238
2239       gtk_notebook_pages_allocate (notebook);
2240     }
2241 }
2242
2243 static gint
2244 gtk_notebook_draw (GtkWidget *widget,
2245                    cairo_t   *cr)
2246 {
2247   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2248   GtkNotebookPrivate *priv = notebook->priv;
2249   GtkAllocation allocation;
2250   GdkWindow *window;
2251   gint i;
2252
2253   gtk_widget_get_allocation (widget, &allocation);
2254
2255   window = gtk_widget_get_window (widget);
2256   if (gtk_cairo_should_draw_window (cr, window))
2257     {
2258       cairo_save (cr);
2259
2260       cairo_translate (cr, -allocation.x, -allocation.y);
2261       gtk_notebook_paint (widget, cr);
2262
2263       cairo_restore (cr);
2264
2265       if (priv->show_tabs)
2266         {
2267           GtkNotebookPage *page;
2268           GList *pages;
2269
2270           for (pages = priv->children; pages; pages = pages->next)
2271             {
2272               page = GTK_NOTEBOOK_PAGE (pages);
2273
2274               if (gtk_widget_get_parent (page->tab_label) == widget)
2275                 gtk_container_propagate_draw (GTK_CONTAINER (notebook),
2276                                               page->tab_label, cr);
2277             }
2278         }
2279
2280       if (priv->cur_page && priv->operation != DRAG_OPERATION_REORDER)
2281         gtk_container_propagate_draw (GTK_CONTAINER (notebook),
2282                                       priv->cur_page->child,
2283                                       cr);
2284       if (priv->show_tabs)
2285       {
2286         for (i = 0; i < N_ACTION_WIDGETS; i++)
2287         {
2288           if (priv->action_widget[i])
2289             gtk_container_propagate_draw (GTK_CONTAINER (notebook),
2290                                           priv->action_widget[i], cr);
2291         }
2292       }
2293     }
2294
2295   if (priv->operation == DRAG_OPERATION_REORDER &&
2296       gtk_cairo_should_draw_window (cr, priv->drag_window))
2297     {
2298       cairo_save (cr);
2299       gtk_cairo_transform_to_window (cr, widget, priv->drag_window);
2300
2301       /* FIXME: This is a workaround to make tabs reordering work better
2302        * with engines with rounded tabs. If the drag window background
2303        * isn't set, the rounded corners would be black.
2304        *
2305        * Ideally, these corners should be made transparent, Either by using
2306        * ARGB visuals or shape windows.
2307        */
2308       gdk_cairo_set_source_color (cr, &gtk_widget_get_style (widget)->bg [GTK_STATE_NORMAL]);
2309       cairo_paint (cr);
2310
2311       gtk_notebook_draw_tab (notebook,
2312                              priv->cur_page,
2313                              cr);
2314
2315       cairo_restore (cr);
2316
2317       gtk_container_propagate_draw (GTK_CONTAINER (notebook),
2318                                     priv->cur_page->tab_label, cr);
2319     }
2320   
2321   return FALSE;
2322 }
2323
2324 static gboolean
2325 gtk_notebook_show_arrows (GtkNotebook *notebook)
2326 {
2327   GtkNotebookPrivate *priv = notebook->priv;
2328   gboolean show_arrow = FALSE;
2329   GList *children;
2330
2331   if (!priv->scrollable)
2332     return FALSE;
2333
2334   children = priv->children;
2335   while (children)
2336     {
2337       GtkNotebookPage *page = children->data;
2338
2339       if (page->tab_label && !gtk_widget_get_child_visible (page->tab_label))
2340         show_arrow = TRUE;
2341
2342       children = children->next;
2343     }
2344
2345   return show_arrow;
2346 }
2347
2348 static void
2349 gtk_notebook_get_arrow_rect (GtkNotebook     *notebook,
2350                              GdkRectangle    *rectangle,
2351                              GtkNotebookArrow arrow)
2352 {
2353   GtkNotebookPrivate *priv = notebook->priv;
2354   GdkRectangle event_window_pos;
2355   gboolean before = ARROW_IS_BEFORE (arrow);
2356   gboolean left = ARROW_IS_LEFT (arrow);
2357
2358   if (gtk_notebook_get_event_window_position (notebook, &event_window_pos))
2359     {
2360       gint scroll_arrow_hlength;
2361       gint scroll_arrow_vlength;
2362
2363       gtk_widget_style_get (GTK_WIDGET (notebook),
2364                             "scroll-arrow-hlength", &scroll_arrow_hlength,
2365                             "scroll-arrow-vlength", &scroll_arrow_vlength,
2366                             NULL);
2367
2368       switch (priv->tab_pos)
2369         {
2370         case GTK_POS_LEFT:
2371         case GTK_POS_RIGHT:
2372           rectangle->width = scroll_arrow_vlength;
2373           rectangle->height = scroll_arrow_vlength;
2374
2375           if ((before && (priv->has_before_previous != priv->has_before_next)) ||
2376               (!before && (priv->has_after_previous != priv->has_after_next)))
2377           rectangle->x = event_window_pos.x + (event_window_pos.width - rectangle->width) / 2;
2378           else if (left)
2379             rectangle->x = event_window_pos.x + event_window_pos.width / 2 - rectangle->width;
2380           else 
2381             rectangle->x = event_window_pos.x + event_window_pos.width / 2;
2382           rectangle->y = event_window_pos.y;
2383           if (!before)
2384             rectangle->y += event_window_pos.height - rectangle->height;
2385           break;
2386
2387         case GTK_POS_TOP:
2388         case GTK_POS_BOTTOM:
2389           rectangle->width = scroll_arrow_hlength;
2390           rectangle->height = scroll_arrow_hlength;
2391
2392           if (before)
2393             {
2394               if (left || !priv->has_before_previous)
2395                 rectangle->x = event_window_pos.x;
2396               else
2397                 rectangle->x = event_window_pos.x + rectangle->width;
2398             }
2399           else
2400             {
2401               if (!left || !priv->has_after_next)
2402                 rectangle->x = event_window_pos.x + event_window_pos.width - rectangle->width;
2403               else
2404                 rectangle->x = event_window_pos.x + event_window_pos.width - 2 * rectangle->width;
2405             }
2406           rectangle->y = event_window_pos.y + (event_window_pos.height - rectangle->height) / 2;
2407           break;
2408         }
2409     }
2410 }
2411
2412 static GtkNotebookArrow
2413 gtk_notebook_get_arrow (GtkNotebook *notebook,
2414                         gint         x,
2415                         gint         y)
2416 {
2417   GtkNotebookPrivate *priv = notebook->priv;
2418   GdkRectangle arrow_rect;
2419   GdkRectangle event_window_pos;
2420   gint i;
2421   gint x0, y0;
2422   GtkNotebookArrow arrow[4];
2423
2424   arrow[0] = priv->has_before_previous ? ARROW_LEFT_BEFORE : ARROW_NONE;
2425   arrow[1] = priv->has_before_next ? ARROW_RIGHT_BEFORE : ARROW_NONE;
2426   arrow[2] = priv->has_after_previous ? ARROW_LEFT_AFTER : ARROW_NONE;
2427   arrow[3] = priv->has_after_next ? ARROW_RIGHT_AFTER : ARROW_NONE;
2428
2429   if (gtk_notebook_show_arrows (notebook))
2430     {
2431       gtk_notebook_get_event_window_position (notebook, &event_window_pos);
2432       for (i = 0; i < 4; i++) 
2433         { 
2434           if (arrow[i] == ARROW_NONE)
2435             continue;
2436       
2437           gtk_notebook_get_arrow_rect (notebook, &arrow_rect, arrow[i]);
2438       
2439           x0 = x - arrow_rect.x;
2440           y0 = y - arrow_rect.y;
2441
2442           if (y0 >= 0 && y0 < arrow_rect.height &&
2443               x0 >= 0 && x0 < arrow_rect.width)
2444             return arrow[i];
2445         }
2446     }
2447
2448   return ARROW_NONE;
2449 }
2450
2451 static void
2452 gtk_notebook_do_arrow (GtkNotebook     *notebook,
2453                        GtkNotebookArrow arrow)
2454 {
2455   GtkNotebookPrivate *priv = notebook->priv;
2456   GtkWidget *widget = GTK_WIDGET (notebook);
2457   gboolean is_rtl, left;
2458
2459   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2460   left = (ARROW_IS_LEFT (arrow) && !is_rtl) || 
2461          (!ARROW_IS_LEFT (arrow) && is_rtl);
2462
2463   if (!priv->focus_tab ||
2464       gtk_notebook_search_page (notebook, priv->focus_tab,
2465                                 left ? STEP_PREV : STEP_NEXT,
2466                                 TRUE))
2467     {
2468       gtk_notebook_change_current_page (notebook, left ? -1 : 1);
2469       gtk_widget_grab_focus (widget);
2470     }
2471 }
2472
2473 static gboolean
2474 gtk_notebook_arrow_button_press (GtkNotebook      *notebook,
2475                                  GtkNotebookArrow  arrow,
2476                                  gint              button)
2477 {
2478   GtkNotebookPrivate *priv = notebook->priv;
2479   GtkWidget *widget = GTK_WIDGET (notebook);
2480   gboolean is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2481   gboolean left = (ARROW_IS_LEFT (arrow) && !is_rtl) || 
2482                   (!ARROW_IS_LEFT (arrow) && is_rtl);
2483
2484   if (!gtk_widget_has_focus (widget))
2485     gtk_widget_grab_focus (widget);
2486
2487   priv->button = button;
2488   priv->click_child = arrow;
2489
2490   if (button == 1)
2491     {
2492       gtk_notebook_do_arrow (notebook, arrow);
2493       gtk_notebook_set_scroll_timer (notebook);
2494     }
2495   else if (button == 2)
2496     gtk_notebook_page_select (notebook, TRUE);
2497   else if (button == 3)
2498     gtk_notebook_switch_focus_tab (notebook,
2499                                    gtk_notebook_search_page (notebook,
2500                                                              NULL,
2501                                                              left ? STEP_NEXT : STEP_PREV,
2502                                                              TRUE));
2503   gtk_notebook_redraw_arrows (notebook);
2504
2505   return TRUE;
2506 }
2507
2508 static gboolean
2509 get_widget_coordinates (GtkWidget *widget,
2510                         GdkEvent  *event,
2511                         gint      *x,
2512                         gint      *y)
2513 {
2514   GdkWindow *window = ((GdkEventAny *)event)->window;
2515   gdouble tx, ty;
2516
2517   if (!gdk_event_get_coords (event, &tx, &ty))
2518     return FALSE;
2519
2520   while (window && window != gtk_widget_get_window (widget))
2521     {
2522       gint window_x, window_y;
2523
2524       gdk_window_get_position (window, &window_x, &window_y);
2525       tx += window_x;
2526       ty += window_y;
2527
2528       window = gdk_window_get_parent (window);
2529     }
2530
2531   if (window)
2532     {
2533       *x = tx;
2534       *y = ty;
2535
2536       return TRUE;
2537     }
2538   else
2539     return FALSE;
2540 }
2541
2542 static GList*
2543 get_tab_at_pos (GtkNotebook *notebook, gint x, gint y)
2544 {
2545    GtkNotebookPrivate *priv = notebook->priv;
2546   GtkNotebookPage *page;
2547   GList *children;
2548
2549   children = priv->children;
2550   while (children)
2551     {
2552       page = children->data;
2553       
2554       if (gtk_widget_get_visible (page->child) &&
2555           page->tab_label && gtk_widget_get_mapped (page->tab_label) &&
2556           (x >= page->allocation.x) &&
2557           (y >= page->allocation.y) &&
2558           (x <= (page->allocation.x + page->allocation.width)) &&
2559           (y <= (page->allocation.y + page->allocation.height)))
2560         return children;
2561
2562       children = children->next;
2563     }
2564
2565   return NULL;
2566 }
2567
2568 static gboolean
2569 gtk_notebook_button_press (GtkWidget      *widget,
2570                            GdkEventButton *event)
2571 {
2572   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2573   GtkNotebookPrivate *priv = notebook->priv;
2574   GtkNotebookPage *page;
2575   GList *tab;
2576   GtkNotebookArrow arrow;
2577   gint x, y;
2578
2579   if (event->type != GDK_BUTTON_PRESS || !priv->children ||
2580       priv->button)
2581     return FALSE;
2582
2583   if (!get_widget_coordinates (widget, (GdkEvent *)event, &x, &y))
2584     return FALSE;
2585
2586   arrow = gtk_notebook_get_arrow (notebook, x, y);
2587   if (arrow)
2588     return gtk_notebook_arrow_button_press (notebook, arrow, event->button);
2589
2590   if (event->button == 3 && priv->menu)
2591     {
2592       gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL,
2593                       NULL, NULL, 3, event->time);
2594       return TRUE;
2595     }
2596
2597   if (event->button != 1)
2598     return FALSE;
2599
2600   priv->button = event->button;
2601
2602   if ((tab = get_tab_at_pos (notebook, x, y)) != NULL)
2603     {
2604       gboolean page_changed, was_focus;
2605
2606       page = tab->data;
2607       page_changed = page != priv->cur_page;
2608       was_focus = gtk_widget_is_focus (widget);
2609
2610       gtk_notebook_switch_focus_tab (notebook, tab);
2611       gtk_widget_grab_focus (widget);
2612
2613       if (page_changed && !was_focus)
2614         gtk_widget_child_focus (page->child, GTK_DIR_TAB_FORWARD);
2615
2616       /* save press to possibly begin a drag */
2617       if (page->reorderable || page->detachable)
2618         {
2619           priv->during_detach = FALSE;
2620           priv->during_reorder = FALSE;
2621           priv->pressed_button = event->button;
2622
2623           priv->mouse_x = x;
2624           priv->mouse_y = y;
2625
2626           priv->drag_begin_x = priv->mouse_x;
2627           priv->drag_begin_y = priv->mouse_y;
2628           priv->drag_offset_x = priv->drag_begin_x - page->allocation.x;
2629           priv->drag_offset_y = priv->drag_begin_y - page->allocation.y;
2630         }
2631     }
2632
2633   return TRUE;
2634 }
2635
2636 static void
2637 popup_position_func (GtkMenu  *menu,
2638                      gint     *x,
2639                      gint     *y,
2640                      gboolean *push_in,
2641                      gpointer  data)
2642 {
2643   GtkNotebook *notebook = data;
2644   GtkNotebookPrivate *priv = notebook->priv;
2645   GtkAllocation allocation;
2646   GtkWidget *w;
2647   GtkRequisition requisition;
2648
2649   if (priv->focus_tab)
2650     {
2651       GtkNotebookPage *page;
2652
2653       page = priv->focus_tab->data;
2654       w = page->tab_label;
2655     }
2656   else
2657    {
2658      w = GTK_WIDGET (notebook);
2659    }
2660
2661   gdk_window_get_origin (gtk_widget_get_window (w), x, y);
2662
2663   gtk_widget_get_allocation (w, &allocation);
2664   gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2665                                  &requisition, NULL);
2666
2667   if (gtk_widget_get_direction (w) == GTK_TEXT_DIR_RTL)
2668     *x += allocation.x + allocation.width - requisition.width;
2669   else
2670     *x += allocation.x;
2671
2672   *y += allocation.y + allocation.height;
2673
2674   *push_in = FALSE;
2675 }
2676
2677 static gboolean
2678 gtk_notebook_popup_menu (GtkWidget *widget)
2679 {
2680   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2681   GtkNotebookPrivate *priv = notebook->priv;
2682
2683   if (priv->menu)
2684     {
2685       gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL,
2686                       popup_position_func, notebook,
2687                       0, gtk_get_current_event_time ());
2688       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2689       return TRUE;
2690     }
2691
2692   return FALSE;
2693 }
2694
2695 static void 
2696 stop_scrolling (GtkNotebook *notebook)
2697 {
2698   GtkNotebookPrivate *priv = notebook->priv;
2699
2700   if (priv->timer)
2701     {
2702       g_source_remove (priv->timer);
2703       priv->timer = 0;
2704       priv->need_timer = FALSE;
2705     }
2706   priv->click_child = 0;
2707   priv->button = 0;
2708   gtk_notebook_redraw_arrows (notebook);
2709 }
2710
2711 static GList*
2712 get_drop_position (GtkNotebook *notebook,
2713                    guint        pack)
2714 {
2715   GtkNotebookPrivate *priv = notebook->priv;
2716   GList *children, *last_child;
2717   GtkNotebookPage *page;
2718   gboolean is_rtl;
2719   gint x, y;
2720
2721   x = priv->mouse_x;
2722   y = priv->mouse_y;
2723
2724   is_rtl = gtk_widget_get_direction ((GtkWidget *) notebook) == GTK_TEXT_DIR_RTL;
2725   children = priv->children;
2726   last_child = NULL;
2727
2728   while (children)
2729     {
2730       page = children->data;
2731
2732       if ((priv->operation != DRAG_OPERATION_REORDER || page != priv->cur_page) &&
2733           gtk_widget_get_visible (page->child) &&
2734           page->tab_label &&
2735           gtk_widget_get_mapped (page->tab_label) &&
2736           page->pack == pack)
2737         {
2738           switch (priv->tab_pos)
2739             {
2740             case GTK_POS_TOP:
2741             case GTK_POS_BOTTOM:
2742               if (!is_rtl)
2743                 {
2744                   if ((page->pack == GTK_PACK_START && PAGE_MIDDLE_X (page) > x) ||
2745                       (page->pack == GTK_PACK_END && PAGE_MIDDLE_X (page) < x))
2746                     return children;
2747                 }
2748               else
2749                 {
2750                   if ((page->pack == GTK_PACK_START && PAGE_MIDDLE_X (page) < x) ||
2751                       (page->pack == GTK_PACK_END && PAGE_MIDDLE_X (page) > x))
2752                     return children;
2753                 }
2754
2755               break;
2756             case GTK_POS_LEFT:
2757             case GTK_POS_RIGHT:
2758               if ((page->pack == GTK_PACK_START && PAGE_MIDDLE_Y (page) > y) ||
2759                   (page->pack == GTK_PACK_END && PAGE_MIDDLE_Y (page) < y))
2760                 return children;
2761
2762               break;
2763             }
2764
2765           last_child = children->next;
2766         }
2767
2768       children = children->next;
2769     }
2770
2771   return last_child;
2772 }
2773
2774 static void
2775 show_drag_window (GtkNotebook        *notebook,
2776                   GtkNotebookPrivate    *priv,
2777                   GtkNotebookPage    *page,
2778                   GdkDevice          *device)
2779 {
2780   GtkWidget *widget = GTK_WIDGET (notebook);
2781
2782   if (!priv->drag_window)
2783     {
2784       GdkWindowAttr attributes;
2785       guint attributes_mask;
2786
2787       attributes.x = page->allocation.x;
2788       attributes.y = page->allocation.y;
2789       attributes.width = page->allocation.width;
2790       attributes.height = page->allocation.height;
2791       attributes.window_type = GDK_WINDOW_CHILD;
2792       attributes.wclass = GDK_INPUT_OUTPUT;
2793       attributes.visual = gtk_widget_get_visual (widget);
2794       attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
2795       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2796
2797       priv->drag_window = gdk_window_new (gtk_widget_get_parent_window (widget),
2798                                           &attributes,
2799                                           attributes_mask);
2800       gdk_window_set_user_data (priv->drag_window, widget);
2801     }
2802
2803   g_object_ref (page->tab_label);
2804   gtk_widget_unparent (page->tab_label);
2805   gtk_widget_set_parent_window (page->tab_label, priv->drag_window);
2806   gtk_widget_set_parent (page->tab_label, widget);
2807   g_object_unref (page->tab_label);
2808
2809   gdk_window_show (priv->drag_window);
2810
2811   /* the grab will dissapear when the window is hidden */
2812   gdk_device_grab (device, priv->drag_window,
2813                    GDK_OWNERSHIP_WINDOW, FALSE,
2814                    GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
2815                    NULL, GDK_CURRENT_TIME);
2816 }
2817
2818 /* This function undoes the reparenting that happens both when drag_window
2819  * is shown for reordering and when the DnD icon is shown for detaching
2820  */
2821 static void
2822 hide_drag_window (GtkNotebook        *notebook,
2823                   GtkNotebookPrivate    *priv,
2824                   GtkNotebookPage    *page)
2825 {
2826   GtkWidget *widget = GTK_WIDGET (notebook);
2827   GtkWidget *parent = gtk_widget_get_parent (page->tab_label);
2828
2829   if (gtk_widget_get_window (page->tab_label) != gtk_widget_get_window (widget) ||
2830       !NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page))
2831     {
2832       g_object_ref (page->tab_label);
2833
2834       if (GTK_IS_WINDOW (parent))
2835         {
2836           /* parent widget is the drag window */
2837           gtk_container_remove (GTK_CONTAINER (parent), page->tab_label);
2838         }
2839       else
2840         gtk_widget_unparent (page->tab_label);
2841
2842       gtk_widget_set_parent (page->tab_label, widget);
2843       g_object_unref (page->tab_label);
2844     }
2845
2846   if (priv->drag_window &&
2847       gdk_window_is_visible (priv->drag_window))
2848     gdk_window_hide (priv->drag_window);
2849 }
2850
2851 static void
2852 gtk_notebook_stop_reorder (GtkNotebook *notebook)
2853 {
2854   GtkNotebookPrivate *priv = notebook->priv;
2855   GtkNotebookPage *page;
2856
2857   if (priv->operation == DRAG_OPERATION_DETACH)
2858     page = priv->detached_tab;
2859   else
2860     page = priv->cur_page;
2861
2862   if (!page || !page->tab_label)
2863     return;
2864
2865   priv->pressed_button = -1;
2866
2867   if (page->reorderable || page->detachable)
2868     {
2869       if (priv->during_reorder)
2870         {
2871           gint old_page_num, page_num;
2872           GList *element;
2873
2874           element = get_drop_position (notebook, page->pack);
2875           old_page_num = g_list_position (priv->children, priv->focus_tab);
2876           page_num = reorder_tab (notebook, element, priv->focus_tab);
2877           gtk_notebook_child_reordered (notebook, page);
2878           
2879           if (priv->has_scrolled || old_page_num != page_num)
2880             g_signal_emit (notebook,
2881                            notebook_signals[PAGE_REORDERED], 0,
2882                            page->child, page_num);
2883
2884           priv->has_scrolled = FALSE;
2885           priv->during_reorder = FALSE; 
2886         }
2887
2888       hide_drag_window (notebook, priv, page);
2889
2890       priv->operation = DRAG_OPERATION_NONE;
2891       gtk_notebook_pages_allocate (notebook);
2892
2893       if (priv->dnd_timer)
2894         {
2895           g_source_remove (priv->dnd_timer);
2896           priv->dnd_timer = 0;
2897         }
2898     }
2899 }
2900
2901 static gint
2902 gtk_notebook_button_release (GtkWidget      *widget,
2903                              GdkEventButton *event)
2904 {
2905   GtkNotebook *notebook;
2906   GtkNotebookPrivate *priv;
2907   GtkNotebookPage *page;
2908
2909   if (event->type != GDK_BUTTON_RELEASE)
2910     return FALSE;
2911
2912   notebook = GTK_NOTEBOOK (widget);
2913   priv = notebook->priv;
2914
2915   page = priv->cur_page;
2916
2917   if (!priv->during_detach &&
2918       page->reorderable &&
2919       event->button == priv->pressed_button)
2920     gtk_notebook_stop_reorder (notebook);
2921
2922   if (event->button == priv->button)
2923     {
2924       stop_scrolling (notebook);
2925       return TRUE;
2926     }
2927   else
2928     return FALSE;
2929 }
2930
2931 static gint
2932 gtk_notebook_leave_notify (GtkWidget        *widget,
2933                            GdkEventCrossing *event)
2934 {
2935   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2936   GtkNotebookPrivate *priv = notebook->priv;
2937   gint x, y;
2938
2939   if (!get_widget_coordinates (widget, (GdkEvent *)event, &x, &y))
2940     return FALSE;
2941
2942   if (priv->in_child)
2943     {
2944       priv->in_child = 0;
2945       gtk_notebook_redraw_arrows (notebook);
2946     }
2947
2948   return TRUE;
2949 }
2950
2951 static GtkNotebookPointerPosition
2952 get_pointer_position (GtkNotebook *notebook)
2953 {
2954   GtkNotebookPrivate *priv = notebook->priv;
2955   GtkWidget *widget = GTK_WIDGET (notebook);
2956   gint wx, wy, width, height;
2957   gboolean is_rtl;
2958
2959   if (!priv->scrollable)
2960     return POINTER_BETWEEN;
2961
2962   gdk_window_get_position (priv->event_window, &wx, &wy);
2963   width = gdk_window_get_width (priv->event_window);
2964   height = gdk_window_get_height (priv->event_window);
2965
2966   if (priv->tab_pos == GTK_POS_TOP ||
2967       priv->tab_pos == GTK_POS_BOTTOM)
2968     {
2969       gint x;
2970
2971       is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2972       x = priv->mouse_x - wx;
2973
2974       if (x > width - SCROLL_THRESHOLD)
2975         return (is_rtl) ? POINTER_BEFORE : POINTER_AFTER;
2976       else if (x < SCROLL_THRESHOLD)
2977         return (is_rtl) ? POINTER_AFTER : POINTER_BEFORE;
2978       else
2979         return POINTER_BETWEEN;
2980     }
2981   else
2982     {
2983       gint y;
2984
2985       y = priv->mouse_y - wy;
2986       if (y > height - SCROLL_THRESHOLD)
2987         return POINTER_AFTER;
2988       else if (y < SCROLL_THRESHOLD)
2989         return POINTER_BEFORE;
2990       else
2991         return POINTER_BETWEEN;
2992     }
2993 }
2994
2995 static gboolean
2996 scroll_notebook_timer (gpointer data)
2997 {
2998   GtkNotebook *notebook = GTK_NOTEBOOK (data);
2999   GtkNotebookPrivate *priv = notebook->priv;
3000   GtkNotebookPointerPosition pointer_position;
3001   GList *element, *first_tab;
3002
3003   pointer_position = get_pointer_position (notebook);
3004
3005   element = get_drop_position (notebook, priv->cur_page->pack);
3006   reorder_tab (notebook, element, priv->focus_tab);
3007   first_tab = gtk_notebook_search_page (notebook, priv->first_tab,
3008                                         (pointer_position == POINTER_BEFORE) ? STEP_PREV : STEP_NEXT,
3009                                         TRUE);
3010   if (first_tab)
3011     {
3012       priv->first_tab = first_tab;
3013       gtk_notebook_pages_allocate (notebook);
3014
3015       gdk_window_move_resize (priv->drag_window,
3016                               priv->drag_window_x,
3017                               priv->drag_window_y,
3018                               priv->cur_page->allocation.width,
3019                               priv->cur_page->allocation.height);
3020       gdk_window_raise (priv->drag_window);
3021     }
3022
3023   return TRUE;
3024 }
3025
3026 static gboolean
3027 check_threshold (GtkNotebook *notebook,
3028                  gint         current_x,
3029                  gint         current_y)
3030 {
3031   GtkNotebookPrivate *priv = notebook->priv;
3032   GtkWidget *widget;
3033   gint dnd_threshold;
3034   GdkRectangle rectangle = { 0, }; /* shut up gcc */
3035   GtkSettings *settings;
3036   
3037   widget = GTK_WIDGET (notebook);
3038   settings = gtk_widget_get_settings (GTK_WIDGET (notebook));
3039   g_object_get (G_OBJECT (settings), "gtk-dnd-drag-threshold", &dnd_threshold, NULL);
3040
3041   /* we want a large threshold */
3042   dnd_threshold *= DND_THRESHOLD_MULTIPLIER;
3043
3044   gdk_window_get_position (priv->event_window, &rectangle.x, &rectangle.y);
3045   rectangle.width = gdk_window_get_width (priv->event_window);
3046   rectangle.height = gdk_window_get_height (priv->event_window);
3047
3048   rectangle.x -= dnd_threshold;
3049   rectangle.width += 2 * dnd_threshold;
3050   rectangle.y -= dnd_threshold;
3051   rectangle.height += 2 * dnd_threshold;
3052
3053   return (current_x < rectangle.x ||
3054           current_x > rectangle.x + rectangle.width ||
3055           current_y < rectangle.y ||
3056           current_y > rectangle.y + rectangle.height);
3057 }
3058
3059 static gint
3060 gtk_notebook_motion_notify (GtkWidget      *widget,
3061                             GdkEventMotion *event)
3062 {
3063   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3064   GtkNotebookPrivate *priv = notebook->priv;
3065   GtkNotebookPage *page;
3066   GtkNotebookArrow arrow;
3067   GtkNotebookPointerPosition pointer_position;
3068   GtkSettings *settings;
3069   guint timeout;
3070   gint x_win, y_win;
3071
3072   page = priv->cur_page;
3073
3074   if (!page)
3075     return FALSE;
3076
3077   if (!(event->state & GDK_BUTTON1_MASK) &&
3078       priv->pressed_button != -1)
3079     {
3080       gtk_notebook_stop_reorder (notebook);
3081       stop_scrolling (notebook);
3082     }
3083
3084   if (event->time < priv->timestamp + MSECS_BETWEEN_UPDATES)
3085     return FALSE;
3086
3087   priv->timestamp = event->time;
3088
3089   /* While animating the move, event->x is relative to the flying tab
3090    * (priv->drag_window has a pointer grab), but we need coordinates relative to
3091    * the notebook widget.
3092    */
3093   gdk_window_get_origin (gtk_widget_get_window (widget), &x_win, &y_win);
3094   priv->mouse_x = event->x_root - x_win;
3095   priv->mouse_y = event->y_root - y_win;
3096
3097   arrow = gtk_notebook_get_arrow (notebook, priv->mouse_x, priv->mouse_y);
3098   if (arrow != priv->in_child)
3099     {
3100       priv->in_child = arrow;
3101       gtk_notebook_redraw_arrows (notebook);
3102     }
3103
3104   if (priv->pressed_button == -1)
3105     return FALSE;
3106
3107   if (page->detachable &&
3108       check_threshold (notebook, priv->mouse_x, priv->mouse_y))
3109     {
3110       priv->detached_tab = priv->cur_page;
3111       priv->during_detach = TRUE;
3112
3113       gtk_drag_begin (widget, priv->source_targets, GDK_ACTION_MOVE,
3114                       priv->pressed_button, (GdkEvent*) event);
3115       return TRUE;
3116     }
3117
3118   if (page->reorderable &&
3119       (priv->during_reorder ||
3120        gtk_drag_check_threshold (widget, priv->drag_begin_x, priv->drag_begin_y, priv->mouse_x, priv->mouse_y)))
3121     {
3122       priv->during_reorder = TRUE;
3123       pointer_position = get_pointer_position (notebook);
3124
3125       if (event->window == priv->drag_window &&
3126           pointer_position != POINTER_BETWEEN &&
3127           gtk_notebook_show_arrows (notebook))
3128         {
3129           /* scroll tabs */
3130           if (!priv->dnd_timer)
3131             {
3132               priv->has_scrolled = TRUE;
3133               settings = gtk_widget_get_settings (GTK_WIDGET (notebook));
3134               g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
3135
3136               priv->dnd_timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
3137                                                scroll_notebook_timer, 
3138                                                (gpointer) notebook);
3139             }
3140         }
3141       else
3142         {
3143           if (priv->dnd_timer)
3144             {
3145               g_source_remove (priv->dnd_timer);
3146               priv->dnd_timer = 0;
3147             }
3148         }
3149
3150       if (event->window == priv->drag_window ||
3151           priv->operation != DRAG_OPERATION_REORDER)
3152         {
3153           /* the drag operation is beginning, create the window */
3154           if (priv->operation != DRAG_OPERATION_REORDER)
3155             {
3156               priv->operation = DRAG_OPERATION_REORDER;
3157               show_drag_window (notebook, priv, page, event->device);
3158             }
3159
3160           gtk_notebook_pages_allocate (notebook);
3161           gdk_window_move_resize (priv->drag_window,
3162                                   priv->drag_window_x,
3163                                   priv->drag_window_y,
3164                                   page->allocation.width,
3165                                   page->allocation.height);
3166         }
3167     }
3168
3169   return TRUE;
3170 }
3171
3172 static void
3173 gtk_notebook_grab_notify (GtkWidget *widget,
3174                           gboolean   was_grabbed)
3175 {
3176   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3177
3178   if (!was_grabbed)
3179     {
3180       gtk_notebook_stop_reorder (notebook);
3181       stop_scrolling (notebook);
3182     }
3183 }
3184
3185 static void
3186 gtk_notebook_state_changed (GtkWidget    *widget,
3187                             GtkStateType  previous_state)
3188 {
3189   if (!gtk_widget_is_sensitive (widget))
3190     stop_scrolling (GTK_NOTEBOOK (widget));
3191 }
3192
3193 static gint
3194 gtk_notebook_focus_in (GtkWidget     *widget,
3195                        GdkEventFocus *event)
3196 {
3197   gtk_notebook_redraw_tabs (GTK_NOTEBOOK (widget));
3198   
3199   return FALSE;
3200 }
3201
3202 static gint
3203 gtk_notebook_focus_out (GtkWidget     *widget,
3204                         GdkEventFocus *event)
3205 {
3206   gtk_notebook_redraw_tabs (GTK_NOTEBOOK (widget));
3207
3208   return FALSE;
3209 }
3210
3211 static void
3212 gtk_notebook_style_set  (GtkWidget *widget,
3213                          GtkStyle  *previous)
3214 {
3215   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3216   GtkNotebookPrivate *priv = notebook->priv;
3217
3218   gboolean has_before_previous;
3219   gboolean has_before_next;
3220   gboolean has_after_previous;
3221   gboolean has_after_next;
3222
3223   gtk_widget_style_get (widget,
3224                         "has-backward-stepper", &has_before_previous,
3225                         "has-secondary-forward-stepper", &has_before_next,
3226                         "has-secondary-backward-stepper", &has_after_previous,
3227                         "has-forward-stepper", &has_after_next,
3228                         NULL);
3229
3230   priv->has_before_previous = has_before_previous;
3231   priv->has_before_next = has_before_next;
3232   priv->has_after_previous = has_after_previous;
3233   priv->has_after_next = has_after_next;
3234
3235   GTK_WIDGET_CLASS (gtk_notebook_parent_class)->style_set (widget, previous);
3236 }
3237
3238 static gboolean
3239 on_drag_icon_draw (GtkWidget *widget,
3240                    cairo_t   *cr,
3241                    gpointer   data)
3242 {
3243   GtkWidget *notebook, *child;
3244   GtkRequisition requisition;
3245   gint gap_pos;
3246
3247   notebook = GTK_WIDGET (data);
3248   child = gtk_bin_get_child (GTK_BIN (widget));
3249
3250   gtk_widget_get_preferred_size (widget,
3251                                  &requisition, NULL);
3252   gap_pos = get_tab_gap_pos (GTK_NOTEBOOK (notebook));
3253
3254   gtk_paint_extension (gtk_widget_get_style (notebook),
3255                        cr,
3256                        GTK_STATE_NORMAL, GTK_SHADOW_OUT,
3257                        widget, "tab",
3258                        0, 0,
3259                        requisition.width, requisition.height,
3260                        gap_pos);
3261   if (child)
3262     gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
3263
3264   return TRUE;
3265 }
3266
3267 static void
3268 gtk_notebook_drag_begin (GtkWidget        *widget,
3269                          GdkDragContext   *context)
3270 {
3271   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3272   GtkNotebookPrivate *priv = notebook->priv;
3273   GtkWidget *tab_label;
3274
3275   if (priv->dnd_timer)
3276     {
3277       g_source_remove (priv->dnd_timer);
3278       priv->dnd_timer = 0;
3279     }
3280
3281   priv->operation = DRAG_OPERATION_DETACH;
3282   gtk_notebook_pages_allocate (notebook);
3283
3284   tab_label = priv->detached_tab->tab_label;
3285
3286   hide_drag_window (notebook, priv, priv->cur_page);
3287   g_object_ref (tab_label);
3288   gtk_widget_unparent (tab_label);
3289
3290   priv->dnd_window = gtk_window_new (GTK_WINDOW_POPUP);
3291   gtk_window_set_screen (GTK_WINDOW (priv->dnd_window),
3292                          gtk_widget_get_screen (widget));
3293   gtk_container_add (GTK_CONTAINER (priv->dnd_window), tab_label);
3294   gtk_widget_set_size_request (priv->dnd_window,
3295                                priv->detached_tab->allocation.width,
3296                                priv->detached_tab->allocation.height);
3297   g_object_unref (tab_label);
3298
3299   g_signal_connect (G_OBJECT (priv->dnd_window), "draw",
3300                     G_CALLBACK (on_drag_icon_draw), notebook);
3301
3302   gtk_drag_set_icon_widget (context, priv->dnd_window, -2, -2);
3303 }
3304
3305 static void
3306 gtk_notebook_drag_end (GtkWidget      *widget,
3307                        GdkDragContext *context)
3308 {
3309   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3310   GtkNotebookPrivate *priv = notebook->priv;
3311
3312   gtk_notebook_stop_reorder (notebook);
3313
3314   if (priv->detached_tab)
3315     gtk_notebook_switch_page (notebook, priv->detached_tab);
3316
3317   _gtk_bin_set_child (GTK_BIN (priv->dnd_window), NULL);
3318   gtk_widget_destroy (priv->dnd_window);
3319   priv->dnd_window = NULL;
3320
3321   priv->operation = DRAG_OPERATION_NONE;
3322 }
3323
3324 static GtkNotebook *
3325 gtk_notebook_create_window (GtkNotebook *notebook,
3326                             GtkWidget   *page,
3327                             gint         x,
3328                             gint         y)
3329 {
3330   return NULL;
3331 }
3332
3333 static gboolean
3334 gtk_notebook_drag_failed (GtkWidget      *widget,
3335                           GdkDragContext *context,
3336                           GtkDragResult   result,
3337                           gpointer        data)
3338 {
3339   if (result == GTK_DRAG_RESULT_NO_TARGET)
3340     {
3341       GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3342       GtkNotebookPrivate *priv = notebook->priv;
3343       GtkNotebook *dest_notebook = NULL;
3344       GdkDisplay *display;
3345       gint x, y;
3346
3347       display = gtk_widget_get_display (widget);
3348       gdk_display_get_pointer (display, NULL, &x, &y, NULL);
3349
3350       g_signal_emit (notebook, notebook_signals[CREATE_WINDOW], 0,
3351                      priv->detached_tab->child, x, y, &dest_notebook);
3352
3353       if (dest_notebook)
3354         do_detach_tab (notebook, dest_notebook, priv->detached_tab->child, 0, 0);
3355
3356       return TRUE;
3357     }
3358
3359   return FALSE;
3360 }
3361
3362 static gboolean
3363 gtk_notebook_switch_tab_timeout (gpointer data)
3364 {
3365   GtkNotebook *notebook = GTK_NOTEBOOK (data);
3366   GtkNotebookPrivate *priv = notebook->priv;
3367   GList *tab;
3368   gint x, y;
3369
3370   priv->switch_tab_timer = 0;
3371   x = priv->mouse_x;
3372   y = priv->mouse_y;
3373
3374   if ((tab = get_tab_at_pos (notebook, x, y)) != NULL)
3375     {
3376       /* FIXME: hack, we don't want the
3377        * focus to move fom the source widget
3378        */
3379       priv->child_has_focus = FALSE;
3380       gtk_notebook_switch_focus_tab (notebook, tab);
3381     }
3382
3383   return FALSE;
3384 }
3385
3386 static gboolean
3387 gtk_notebook_drag_motion (GtkWidget      *widget,
3388                           GdkDragContext *context,
3389                           gint            x,
3390                           gint            y,
3391                           guint           time)
3392 {
3393   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3394   GtkNotebookPrivate *priv = notebook->priv;
3395   GtkAllocation allocation;
3396   GdkRectangle position;
3397   GtkSettings *settings;
3398   GtkNotebookArrow arrow;
3399   guint timeout;
3400   GdkAtom target, tab_target;
3401
3402   gtk_widget_get_allocation (widget, &allocation);
3403
3404   arrow = gtk_notebook_get_arrow (notebook,
3405                                   x + allocation.x,
3406                                   y + allocation.y);
3407   if (arrow)
3408     {
3409       priv->click_child = arrow;
3410       gtk_notebook_set_scroll_timer (notebook);
3411       gdk_drag_status (context, 0, time);
3412       return TRUE;
3413     }
3414
3415   stop_scrolling (notebook);
3416   target = gtk_drag_dest_find_target (widget, context, NULL);
3417   tab_target = gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB");
3418
3419   if (target == tab_target)
3420     {
3421       GQuark group, source_group;
3422       GtkNotebook *source;
3423       GtkWidget *source_child;
3424
3425       source = GTK_NOTEBOOK (gtk_drag_get_source_widget (context));
3426       source_child = source->priv->cur_page->child;
3427
3428       group = notebook->priv->group;
3429       source_group = source->priv->group;
3430
3431       if (group != 0 && group == source_group &&
3432           !(widget == source_child ||
3433             gtk_widget_is_ancestor (widget, source_child)))
3434         {
3435           gdk_drag_status (context, GDK_ACTION_MOVE, time);
3436           return TRUE;
3437         }
3438       else
3439         {
3440           /* it's a tab, but doesn't share
3441            * ID with this notebook */
3442           gdk_drag_status (context, 0, time);
3443         }
3444     }
3445
3446   x += allocation.x;
3447   y += allocation.y;
3448
3449   if (gtk_notebook_get_event_window_position (notebook, &position) &&
3450       x >= position.x && x <= position.x + position.width &&
3451       y >= position.y && y <= position.y + position.height)
3452     {
3453       priv->mouse_x = x;
3454       priv->mouse_y = y;
3455
3456       if (!priv->switch_tab_timer)
3457         {
3458           settings = gtk_widget_get_settings (widget);
3459
3460           g_object_get (settings, "gtk-timeout-expand", &timeout, NULL);
3461           priv->switch_tab_timer = gdk_threads_add_timeout (timeout,
3462                                                   gtk_notebook_switch_tab_timeout,
3463                                                   widget);
3464         }
3465     }
3466   else
3467     {
3468       if (priv->switch_tab_timer)
3469         {
3470           g_source_remove (priv->switch_tab_timer);
3471           priv->switch_tab_timer = 0;
3472         }
3473     }
3474
3475   return (target == tab_target) ? TRUE : FALSE;
3476 }
3477
3478 static void
3479 gtk_notebook_drag_leave (GtkWidget      *widget,
3480                          GdkDragContext *context,
3481                          guint           time)
3482 {
3483   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3484   GtkNotebookPrivate *priv = notebook->priv;
3485
3486   if (priv->switch_tab_timer)
3487     {
3488       g_source_remove (priv->switch_tab_timer);
3489       priv->switch_tab_timer = 0;
3490     }
3491
3492   stop_scrolling (GTK_NOTEBOOK (widget));
3493 }
3494
3495 static gboolean
3496 gtk_notebook_drag_drop (GtkWidget        *widget,
3497                         GdkDragContext   *context,
3498                         gint              x,
3499                         gint              y,
3500                         guint             time)
3501 {
3502   GdkAtom target, tab_target;
3503
3504   target = gtk_drag_dest_find_target (widget, context, NULL);
3505   tab_target = gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB");
3506
3507   if (target == tab_target)
3508     {
3509       gtk_drag_get_data (widget, context, target, time);
3510       return TRUE;
3511     }
3512
3513   return FALSE;
3514 }
3515
3516 static void
3517 do_detach_tab (GtkNotebook     *from,
3518                GtkNotebook     *to,
3519                GtkWidget       *child,
3520                gint             x,
3521                gint             y)
3522 {
3523   GtkNotebookPrivate *to_priv = to->priv;
3524   GtkAllocation to_allocation;
3525   GtkWidget *tab_label, *menu_label;
3526   gboolean tab_expand, tab_fill, reorderable, detachable;
3527   GList *element;
3528   guint tab_pack;
3529   gint page_num;
3530
3531   menu_label = gtk_notebook_get_menu_label (from, child);
3532
3533   if (menu_label)
3534     g_object_ref (menu_label);
3535
3536   tab_label = gtk_notebook_get_tab_label (from, child);
3537   
3538   if (tab_label)
3539     g_object_ref (tab_label);
3540
3541   g_object_ref (child);
3542
3543   gtk_container_child_get (GTK_CONTAINER (from),
3544                            child,
3545                            "tab-expand", &tab_expand,
3546                            "tab-fill", &tab_fill,
3547                            "tab-pack", &tab_pack,
3548                            "reorderable", &reorderable,
3549                            "detachable", &detachable,
3550                            NULL);
3551
3552   gtk_container_remove (GTK_CONTAINER (from), child);
3553
3554   gtk_widget_get_allocation (GTK_WIDGET (to), &to_allocation);
3555   to_priv->mouse_x = x + to_allocation.x;
3556   to_priv->mouse_y = y + to_allocation.y;
3557
3558   element = get_drop_position (to, tab_pack);
3559   page_num = g_list_position (to_priv->children, element);
3560   gtk_notebook_insert_page_menu (to, child, tab_label, menu_label, page_num);
3561
3562   gtk_container_child_set (GTK_CONTAINER (to), child,
3563                            "tab-pack", tab_pack,
3564                            "tab-expand", tab_expand,
3565                            "tab-fill", tab_fill,
3566                            "reorderable", reorderable,
3567                            "detachable", detachable,
3568                            NULL);
3569   if (child)
3570     g_object_unref (child);
3571
3572   if (tab_label)
3573     g_object_unref (tab_label);
3574
3575   if (menu_label)
3576     g_object_unref (menu_label);
3577
3578   gtk_notebook_set_current_page (to, page_num);
3579 }
3580
3581 static void
3582 gtk_notebook_drag_data_get (GtkWidget        *widget,
3583                             GdkDragContext   *context,
3584                             GtkSelectionData *data,
3585                             guint             info,
3586                             guint             time)
3587 {
3588   if (data->target == gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"))
3589     {
3590       GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3591       GtkNotebookPrivate *priv = notebook->priv;
3592
3593       gtk_selection_data_set (data,
3594                               data->target,
3595                               8,
3596                               (void*) &priv->detached_tab->child,
3597                               sizeof (gpointer));
3598     }
3599 }
3600
3601 static void
3602 gtk_notebook_drag_data_received (GtkWidget        *widget,
3603                                  GdkDragContext   *context,
3604                                  gint              x,
3605                                  gint              y,
3606                                  GtkSelectionData *data,
3607                                  guint             info,
3608                                  guint             time)
3609 {
3610   GtkNotebook *notebook;
3611   GtkWidget *source_widget;
3612   GtkWidget **child;
3613
3614   notebook = GTK_NOTEBOOK (widget);
3615   source_widget = gtk_drag_get_source_widget (context);
3616
3617   if (source_widget &&
3618       data->target == gdk_atom_intern_static_string ("GTK_NOTEBOOK_TAB"))
3619     {
3620       child = (void*) data->data;
3621
3622       do_detach_tab (GTK_NOTEBOOK (source_widget), notebook, *child, x, y);
3623       gtk_drag_finish (context, TRUE, FALSE, time);
3624     }
3625   else
3626     gtk_drag_finish (context, FALSE, FALSE, time);
3627 }
3628
3629 /* Private GtkContainer Methods :
3630  * 
3631  * gtk_notebook_set_child_arg
3632  * gtk_notebook_get_child_arg
3633  * gtk_notebook_add
3634  * gtk_notebook_remove
3635  * gtk_notebook_focus
3636  * gtk_notebook_set_focus_child
3637  * gtk_notebook_child_type
3638  * gtk_notebook_forall
3639  */
3640 static void
3641 gtk_notebook_set_child_property (GtkContainer    *container,
3642                                  GtkWidget       *child,
3643                                  guint            property_id,
3644                                  const GValue    *value,
3645                                  GParamSpec      *pspec)
3646 {
3647   gboolean expand;
3648   gboolean fill;
3649   GtkPackType pack_type;
3650
3651   /* not finding child's page is valid for menus or labels */
3652   if (!gtk_notebook_find_child (GTK_NOTEBOOK (container), child, NULL))
3653     return;
3654
3655   switch (property_id)
3656     {
3657     case CHILD_PROP_TAB_LABEL:
3658       /* a NULL pointer indicates a default_tab setting, otherwise
3659        * we need to set the associated label
3660        */
3661       gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (container), child,
3662                                        g_value_get_string (value));
3663       break;
3664     case CHILD_PROP_MENU_LABEL:
3665       gtk_notebook_set_menu_label_text (GTK_NOTEBOOK (container), child,
3666                                         g_value_get_string (value));
3667       break;
3668     case CHILD_PROP_POSITION:
3669       gtk_notebook_reorder_child (GTK_NOTEBOOK (container), child,
3670                                   g_value_get_int (value));
3671       break;
3672     case CHILD_PROP_TAB_EXPAND:
3673       gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3674                                             &expand, &fill, &pack_type);
3675       gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (container), child,
3676                                           g_value_get_boolean (value),
3677                                           fill, pack_type);
3678       break;
3679     case CHILD_PROP_TAB_FILL:
3680       gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3681                                             &expand, &fill, &pack_type);
3682       gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (container), child,
3683                                           expand,
3684                                           g_value_get_boolean (value),
3685                                           pack_type);
3686       break;
3687     case CHILD_PROP_TAB_PACK:
3688       gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3689                                             &expand, &fill, &pack_type);
3690       gtk_notebook_set_tab_label_packing (GTK_NOTEBOOK (container), child,
3691                                           expand, fill,
3692                                           g_value_get_enum (value));
3693       break;
3694     case CHILD_PROP_REORDERABLE:
3695       gtk_notebook_set_tab_reorderable (GTK_NOTEBOOK (container), child,
3696                                         g_value_get_boolean (value));
3697       break;
3698     case CHILD_PROP_DETACHABLE:
3699       gtk_notebook_set_tab_detachable (GTK_NOTEBOOK (container), child,
3700                                        g_value_get_boolean (value));
3701       break;
3702     default:
3703       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
3704       break;
3705     }
3706 }
3707
3708 static void
3709 gtk_notebook_get_child_property (GtkContainer    *container,
3710                                  GtkWidget       *child,
3711                                  guint            property_id,
3712                                  GValue          *value,
3713                                  GParamSpec      *pspec)
3714 {
3715   GtkNotebook *notebook = GTK_NOTEBOOK (container);
3716   GtkNotebookPrivate *priv = notebook->priv;
3717   GList *list;
3718   GtkWidget *label;
3719   gboolean expand;
3720   gboolean fill;
3721   GtkPackType pack_type;
3722
3723   /* not finding child's page is valid for menus or labels */
3724   list = gtk_notebook_find_child (notebook, child, NULL);
3725   if (!list)
3726     {
3727       /* nothing to set on labels or menus */
3728       g_param_value_set_default (pspec, value);
3729       return;
3730     }
3731
3732   switch (property_id)
3733     {
3734     case CHILD_PROP_TAB_LABEL:
3735       label = gtk_notebook_get_tab_label (notebook, child);
3736
3737       if (GTK_IS_LABEL (label))
3738         g_value_set_string (value, gtk_label_get_label (GTK_LABEL (label)));
3739       else
3740         g_value_set_string (value, NULL);
3741       break;
3742     case CHILD_PROP_MENU_LABEL:
3743       label = gtk_notebook_get_menu_label (notebook, child);
3744
3745       if (GTK_IS_LABEL (label))
3746         g_value_set_string (value, gtk_label_get_label (GTK_LABEL (label)));
3747       else
3748         g_value_set_string (value, NULL);
3749       break;
3750     case CHILD_PROP_POSITION:
3751       g_value_set_int (value, g_list_position (priv->children, list));
3752       break;
3753     case CHILD_PROP_TAB_EXPAND:
3754         gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3755                                               &expand, NULL, NULL);
3756         g_value_set_boolean (value, expand);
3757       break;
3758     case CHILD_PROP_TAB_FILL:
3759         gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3760                                               NULL, &fill, NULL);
3761         g_value_set_boolean (value, fill);
3762       break;
3763     case CHILD_PROP_TAB_PACK:
3764         gtk_notebook_query_tab_label_packing (GTK_NOTEBOOK (container), child,
3765                                               NULL, NULL, &pack_type);
3766         g_value_set_enum (value, pack_type);
3767       break;
3768     case CHILD_PROP_REORDERABLE:
3769       g_value_set_boolean (value,
3770                            gtk_notebook_get_tab_reorderable (GTK_NOTEBOOK (container), child));
3771       break;
3772     case CHILD_PROP_DETACHABLE:
3773       g_value_set_boolean (value,
3774                            gtk_notebook_get_tab_detachable (GTK_NOTEBOOK (container), child));
3775       break;
3776     default:
3777       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
3778       break;
3779     }
3780 }
3781
3782 static void
3783 gtk_notebook_add (GtkContainer *container,
3784                   GtkWidget    *widget)
3785 {
3786   gtk_notebook_insert_page_menu (GTK_NOTEBOOK (container), widget, 
3787                                  NULL, NULL, -1);
3788 }
3789
3790 static void
3791 gtk_notebook_remove (GtkContainer *container,
3792                      GtkWidget    *widget)
3793 {
3794   GtkNotebook *notebook = GTK_NOTEBOOK (container);
3795   GtkNotebookPrivate *priv = notebook->priv;
3796   GtkNotebookPage *page;
3797   GList *children;
3798   gint page_num = 0;
3799
3800   children = priv->children;
3801   while (children)
3802     {
3803       page = children->data;
3804
3805       if (page->child == widget)
3806         break;
3807
3808       page_num++;
3809       children = children->next;
3810     }
3811  
3812   if (children == NULL)
3813     return;
3814   
3815   g_object_ref (widget);
3816
3817   gtk_notebook_real_remove (notebook, children);
3818
3819   g_signal_emit (notebook,
3820                  notebook_signals[PAGE_REMOVED],
3821                  0,
3822                  widget,
3823                  page_num);
3824   
3825   g_object_unref (widget);
3826 }
3827
3828 static gboolean
3829 focus_tabs_in (GtkNotebook *notebook)
3830 {
3831   GtkNotebookPrivate *priv = notebook->priv;
3832
3833   if (priv->show_tabs && priv->cur_page)
3834     {
3835       gtk_widget_grab_focus (GTK_WIDGET (notebook));
3836
3837       gtk_notebook_switch_focus_tab (notebook,
3838                                      g_list_find (priv->children,
3839                                                   priv->cur_page));
3840
3841       return TRUE;
3842     }
3843   else
3844     return FALSE;
3845 }
3846
3847 static gboolean
3848 focus_tabs_move (GtkNotebook     *notebook,
3849                  GtkDirectionType direction,
3850                  gint             search_direction)
3851 {
3852   GtkNotebookPrivate *priv = notebook->priv;
3853   GList *new_page;
3854
3855   new_page = gtk_notebook_search_page (notebook, priv->focus_tab,
3856                                        search_direction, TRUE);
3857   if (!new_page)
3858     {
3859       gboolean wrap_around;
3860
3861       g_object_get (gtk_widget_get_settings (GTK_WIDGET (notebook)),
3862                     "gtk-keynav-wrap-around", &wrap_around,
3863                     NULL);
3864
3865       if (wrap_around)
3866         new_page = gtk_notebook_search_page (notebook, NULL,
3867                                              search_direction, TRUE);
3868     }
3869
3870   if (new_page)
3871     gtk_notebook_switch_focus_tab (notebook, new_page);
3872   else
3873     gtk_widget_error_bell (GTK_WIDGET (notebook));
3874
3875   return TRUE;
3876 }
3877
3878 static gboolean
3879 focus_child_in (GtkNotebook      *notebook,
3880                 GtkDirectionType  direction)
3881 {
3882   GtkNotebookPrivate *priv = notebook->priv;
3883
3884   if (priv->cur_page)
3885     return gtk_widget_child_focus (priv->cur_page->child, direction);
3886   else
3887     return FALSE;
3888 }
3889
3890 static gboolean
3891 focus_action_in (GtkNotebook      *notebook,
3892                  gint              action,
3893                  GtkDirectionType  direction)
3894 {
3895   GtkNotebookPrivate *priv = notebook->priv;
3896
3897   if (priv->action_widget[action] &&
3898       gtk_widget_get_visible (priv->action_widget[action]))
3899     return gtk_widget_child_focus (priv->action_widget[action], direction);
3900   else
3901     return FALSE;
3902 }
3903
3904 /* Focus in the notebook can either be on the pages, or on
3905  * the tabs or on the action_widgets.
3906  */
3907 static gint
3908 gtk_notebook_focus (GtkWidget        *widget,
3909                     GtkDirectionType  direction)
3910 {
3911   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
3912   GtkNotebookPrivate *priv = notebook->priv;
3913   GtkWidget *old_focus_child;
3914   GtkDirectionType effective_direction;
3915   gint first_action;
3916   gint last_action;
3917
3918   gboolean widget_is_focus;
3919   GtkContainer *container;
3920
3921   container = GTK_CONTAINER (widget);
3922
3923   if (priv->tab_pos == GTK_POS_TOP ||
3924       priv->tab_pos == GTK_POS_LEFT)
3925     {
3926       first_action = ACTION_WIDGET_START;
3927       last_action = ACTION_WIDGET_END;
3928     }
3929   else
3930     {
3931       first_action = ACTION_WIDGET_END;
3932       last_action = ACTION_WIDGET_START;
3933     }
3934
3935   if (priv->focus_out)
3936     {
3937       priv->focus_out = FALSE; /* Clear this to catch the wrap-around case */
3938       return FALSE;
3939     }
3940
3941   widget_is_focus = gtk_widget_is_focus (widget);
3942   old_focus_child = gtk_container_get_focus_child (container);
3943
3944   effective_direction = get_effective_direction (notebook, direction);
3945
3946   if (old_focus_child)          /* Focus on page child or action widget */
3947     {
3948       if (gtk_widget_child_focus (old_focus_child, direction))
3949         return TRUE;
3950
3951       if (old_focus_child == priv->action_widget[ACTION_WIDGET_START])
3952         {
3953           switch (effective_direction)
3954             {
3955             case GTK_DIR_DOWN:
3956               return focus_child_in (notebook, GTK_DIR_TAB_FORWARD);
3957             case GTK_DIR_RIGHT:
3958               return focus_tabs_in (notebook);
3959             case GTK_DIR_LEFT:
3960               return FALSE;
3961             case GTK_DIR_UP:
3962               return FALSE;
3963             default:
3964               switch (direction)
3965                 {
3966                 case GTK_DIR_TAB_FORWARD:
3967                   if ((priv->tab_pos == GTK_POS_RIGHT || priv->tab_pos == GTK_POS_BOTTOM) &&
3968                       focus_child_in (notebook, direction))
3969                     return TRUE;
3970                   return focus_tabs_in (notebook);
3971                 case GTK_DIR_TAB_BACKWARD:
3972                   return FALSE;
3973                 default:
3974                   g_assert_not_reached ();
3975                 }
3976             }
3977         }
3978       else if (old_focus_child == priv->action_widget[ACTION_WIDGET_END])
3979         {
3980           switch (effective_direction)
3981             {
3982             case GTK_DIR_DOWN:
3983               return focus_child_in (notebook, GTK_DIR_TAB_FORWARD);
3984             case GTK_DIR_RIGHT:
3985               return FALSE;
3986             case GTK_DIR_LEFT:
3987               return focus_tabs_in (notebook);
3988             case GTK_DIR_UP:
3989               return FALSE;
3990             default:
3991               switch (direction)
3992                 {
3993                 case GTK_DIR_TAB_FORWARD:
3994                   return FALSE;
3995                 case GTK_DIR_TAB_BACKWARD:
3996                   if ((priv->tab_pos == GTK_POS_TOP || priv->tab_pos == GTK_POS_LEFT) &&
3997                       focus_child_in (notebook, direction))
3998                     return TRUE;
3999                   return focus_tabs_in (notebook);
4000                 default:
4001                   g_assert_not_reached ();
4002                 }
4003             }
4004         }
4005       else
4006         {
4007           switch (effective_direction)
4008             {
4009             case GTK_DIR_TAB_BACKWARD:
4010             case GTK_DIR_UP:
4011               /* Focus onto the tabs */
4012               return focus_tabs_in (notebook);
4013             case GTK_DIR_DOWN:
4014             case GTK_DIR_LEFT:
4015             case GTK_DIR_RIGHT:
4016               return FALSE;
4017             case GTK_DIR_TAB_FORWARD:
4018               return focus_action_in (notebook, last_action, direction);
4019             }
4020         }
4021     }
4022   else if (widget_is_focus)     /* Focus was on tabs */
4023     {
4024       switch (effective_direction)
4025         {
4026         case GTK_DIR_TAB_BACKWARD:
4027               return focus_action_in (notebook, first_action, direction);
4028         case GTK_DIR_UP:
4029           return FALSE;
4030         case GTK_DIR_TAB_FORWARD:
4031           if (focus_child_in (notebook, GTK_DIR_TAB_FORWARD))
4032             return TRUE;
4033           return focus_action_in (notebook, last_action, direction);
4034         case GTK_DIR_DOWN:
4035           /* We use TAB_FORWARD rather than direction so that we focus a more
4036            * predictable widget for the user; users may be using arrow focusing
4037            * in this situation even if they don't usually use arrow focusing.
4038            */
4039           return focus_child_in (notebook, GTK_DIR_TAB_FORWARD);
4040         case GTK_DIR_LEFT:
4041           return focus_tabs_move (notebook, direction, STEP_PREV);
4042         case GTK_DIR_RIGHT:
4043           return focus_tabs_move (notebook, direction, STEP_NEXT);
4044         }
4045     }
4046   else /* Focus was not on widget */
4047     {
4048       switch (effective_direction)
4049         {
4050         case GTK_DIR_TAB_FORWARD:
4051         case GTK_DIR_DOWN:
4052           if (focus_action_in (notebook, first_action, direction))
4053             return TRUE;
4054           if (focus_tabs_in (notebook))
4055             return TRUE;
4056           if (focus_action_in (notebook, last_action, direction))
4057             return TRUE;
4058           if (focus_child_in (notebook, direction))
4059             return TRUE;
4060           return FALSE;
4061         case GTK_DIR_TAB_BACKWARD:
4062           if (focus_action_in (notebook, last_action, direction))
4063             return TRUE;
4064           if (focus_child_in (notebook, direction))
4065             return TRUE;
4066           if (focus_tabs_in (notebook))
4067             return TRUE;
4068           if (focus_action_in (notebook, first_action, direction))
4069             return TRUE;
4070         case GTK_DIR_UP:
4071         case GTK_DIR_LEFT:
4072         case GTK_DIR_RIGHT:
4073           return focus_child_in (notebook, direction);
4074         }
4075     }
4076
4077   g_assert_not_reached ();
4078   return FALSE;
4079 }
4080
4081 static void
4082 gtk_notebook_set_focus_child (GtkContainer *container,
4083                               GtkWidget    *child)
4084 {
4085   GtkNotebook *notebook = GTK_NOTEBOOK (container);
4086   GtkNotebookPrivate *priv = notebook->priv;
4087   GtkWidget *page_child;
4088   GtkWidget *toplevel;
4089
4090   /* If the old focus widget was within a page of the notebook,
4091    * (child may either be NULL or not in this case), record it
4092    * for future use if we switch to the page with a mnemonic.
4093    */
4094
4095   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (container));
4096   if (toplevel && gtk_widget_is_toplevel (toplevel))
4097     {
4098       page_child = gtk_window_get_focus (GTK_WINDOW (toplevel));
4099       while (page_child)
4100         {
4101           if (gtk_widget_get_parent (page_child) == GTK_WIDGET (container))
4102             {
4103               GList *list = gtk_notebook_find_child (notebook, page_child, NULL);
4104               if (list != NULL) 
4105                 {
4106                   GtkNotebookPage *page = list->data;
4107               
4108                   if (page->last_focus_child)
4109                     g_object_remove_weak_pointer (G_OBJECT (page->last_focus_child), (gpointer *)&page->last_focus_child);
4110
4111                   page->last_focus_child = gtk_window_get_focus (GTK_WINDOW (toplevel));
4112                   g_object_add_weak_pointer (G_OBJECT (page->last_focus_child), (gpointer *)&page->last_focus_child);
4113               
4114                   break;
4115                 }
4116             }
4117
4118           page_child = gtk_widget_get_parent (page_child);
4119         }
4120     }
4121   
4122   if (child)
4123     {
4124       g_return_if_fail (GTK_IS_WIDGET (child));
4125
4126       priv->child_has_focus = TRUE;
4127       if (!priv->focus_tab)
4128         {
4129           GList *children;
4130           GtkNotebookPage *page;
4131
4132           children = priv->children;
4133           while (children)
4134             {
4135               page = children->data;
4136               if (page->child == child || page->tab_label == child)
4137                 gtk_notebook_switch_focus_tab (notebook, children);
4138               children = children->next;
4139             }
4140         }
4141     }
4142   else
4143     priv->child_has_focus = FALSE;
4144
4145   GTK_CONTAINER_CLASS (gtk_notebook_parent_class)->set_focus_child (container, child);
4146 }
4147
4148 static void
4149 gtk_notebook_forall (GtkContainer *container,
4150                      gboolean      include_internals,
4151                      GtkCallback   callback,
4152                      gpointer      callback_data)
4153 {
4154   GtkNotebook *notebook = GTK_NOTEBOOK (container);
4155   GtkNotebookPrivate *priv = notebook->priv;
4156   GList *children;
4157   gint i;
4158
4159   children = priv->children;
4160   while (children)
4161     {
4162       GtkNotebookPage *page;
4163       
4164       page = children->data;
4165       children = children->next;
4166       (* callback) (page->child, callback_data);
4167
4168       if (include_internals)
4169         {
4170           if (page->tab_label)
4171             (* callback) (page->tab_label, callback_data);
4172         }
4173     }
4174
4175   if (include_internals) {
4176     for (i = 0; i < N_ACTION_WIDGETS; i++)
4177       {
4178         if (priv->action_widget[i])
4179           (* callback) (priv->action_widget[i], callback_data);
4180       }
4181   }
4182 }
4183
4184 static GType
4185 gtk_notebook_child_type (GtkContainer     *container)
4186 {
4187   return GTK_TYPE_WIDGET;
4188 }
4189
4190 /* Private GtkNotebook Methods:
4191  *
4192  * gtk_notebook_real_insert_page
4193  */
4194 static void
4195 page_visible_cb (GtkWidget  *page,
4196                  GParamSpec *arg,
4197                  gpointer    data)
4198 {
4199   GtkNotebook *notebook = GTK_NOTEBOOK (data);
4200   GtkNotebookPrivate *priv = notebook->priv;
4201   GList *list;
4202   GList *next = NULL;
4203
4204   if (priv->cur_page &&
4205       priv->cur_page->child == page &&
4206       !gtk_widget_get_visible (page))
4207     {
4208       list = g_list_find (priv->children, priv->cur_page);
4209       if (list)
4210         {
4211           next = gtk_notebook_search_page (notebook, list, STEP_NEXT, TRUE);
4212           if (!next)
4213             next = gtk_notebook_search_page (notebook, list, STEP_PREV, TRUE);
4214         }
4215
4216       if (next)
4217         gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (next));
4218     }
4219 }
4220
4221 static gint
4222 gtk_notebook_real_insert_page (GtkNotebook *notebook,
4223                                GtkWidget   *child,
4224                                GtkWidget   *tab_label,
4225                                GtkWidget   *menu_label,
4226                                gint         position)
4227 {
4228   GtkNotebookPrivate *priv = notebook->priv;
4229   GtkNotebookPage *page;
4230   gint nchildren;
4231
4232   gtk_widget_freeze_child_notify (child);
4233
4234   page = g_slice_new0 (GtkNotebookPage);
4235   page->child = child;
4236
4237   nchildren = g_list_length (priv->children);
4238   if ((position < 0) || (position > nchildren))
4239     position = nchildren;
4240
4241   priv->children = g_list_insert (priv->children, page, position);
4242
4243   if (!tab_label)
4244     {
4245       page->default_tab = TRUE;
4246       if (priv->show_tabs)
4247         tab_label = gtk_label_new (NULL);
4248     }
4249   page->tab_label = tab_label;
4250   page->menu_label = menu_label;
4251   page->expand = FALSE;
4252   page->fill = TRUE;
4253   page->pack = GTK_PACK_START; 
4254
4255   if (!menu_label)
4256     page->default_menu = TRUE;
4257   else  
4258     g_object_ref_sink (page->menu_label);
4259
4260   if (priv->menu)
4261     gtk_notebook_menu_item_create (notebook,
4262                                    g_list_find (priv->children, page));
4263
4264   gtk_widget_set_parent (child, GTK_WIDGET (notebook));
4265   if (tab_label)
4266     gtk_widget_set_parent (tab_label, GTK_WIDGET (notebook));
4267
4268   gtk_notebook_update_labels (notebook);
4269
4270   if (!priv->first_tab)
4271     priv->first_tab = priv->children;
4272
4273   /* child visible will be turned on by switch_page below */
4274   if (priv->cur_page != page)
4275     gtk_widget_set_child_visible (child, FALSE);
4276
4277   if (tab_label)
4278     {
4279       if (priv->show_tabs && gtk_widget_get_visible (child))
4280         gtk_widget_show (tab_label);
4281       else
4282         gtk_widget_hide (tab_label);
4283
4284     page->mnemonic_activate_signal =
4285       g_signal_connect (tab_label,
4286                         "mnemonic-activate",
4287                         G_CALLBACK (gtk_notebook_mnemonic_activate_switch_page),
4288                         notebook);
4289     }
4290
4291   page->notify_visible_handler = g_signal_connect (child, "notify::visible",
4292                                                    G_CALLBACK (page_visible_cb), notebook);
4293
4294   g_signal_emit (notebook,
4295                  notebook_signals[PAGE_ADDED],
4296                  0,
4297                  child,
4298                  position);
4299
4300   if (!priv->cur_page)
4301     {
4302       gtk_notebook_switch_page (notebook, page);
4303       /* focus_tab is set in the switch_page method */
4304       gtk_notebook_switch_focus_tab (notebook, priv->focus_tab);
4305     }
4306
4307   gtk_notebook_update_tab_states (notebook);
4308
4309   if (priv->scrollable)
4310     gtk_notebook_redraw_arrows (notebook);
4311
4312   gtk_widget_child_notify (child, "tab-expand");
4313   gtk_widget_child_notify (child, "tab-fill");
4314   gtk_widget_child_notify (child, "tab-pack");
4315   gtk_widget_child_notify (child, "tab-label");
4316   gtk_widget_child_notify (child, "menu-label");
4317   gtk_widget_child_notify (child, "position");
4318   gtk_widget_thaw_child_notify (child);
4319
4320   /* The page-added handler might have reordered the pages, re-get the position */
4321   return gtk_notebook_page_num (notebook, child);
4322 }
4323
4324 /* Private GtkNotebook Functions:
4325  *
4326  * gtk_notebook_redraw_tabs
4327  * gtk_notebook_real_remove
4328  * gtk_notebook_update_labels
4329  * gtk_notebook_timer
4330  * gtk_notebook_set_scroll_timer
4331  * gtk_notebook_page_compare
4332  * gtk_notebook_real_page_position
4333  * gtk_notebook_search_page
4334  */
4335 static void
4336 gtk_notebook_redraw_tabs (GtkNotebook *notebook)
4337 {
4338   GtkNotebookPrivate *priv = notebook->priv;
4339   GtkAllocation allocation;
4340   GtkWidget *widget;
4341   GtkNotebookPage *page;
4342   GtkStyle *style;
4343   GdkRectangle redraw_rect;
4344   gint border;
4345   gint tab_pos = get_effective_tab_pos (notebook);
4346
4347   widget = GTK_WIDGET (notebook);
4348   border = gtk_container_get_border_width (GTK_CONTAINER (notebook));
4349
4350   if (!gtk_widget_get_mapped (widget) || !priv->first_tab)
4351     return;
4352
4353   page = priv->first_tab->data;
4354
4355   redraw_rect.x = border;
4356   redraw_rect.y = border;
4357
4358   style = gtk_widget_get_style (widget);
4359   gtk_widget_get_allocation (widget, &allocation);
4360
4361   switch (tab_pos)
4362     {
4363     case GTK_POS_BOTTOM:
4364       redraw_rect.y = allocation.height - border -
4365         page->allocation.height - style->ythickness;
4366
4367       if (page != priv->cur_page)
4368         redraw_rect.y -= style->ythickness;
4369       /* fall through */
4370     case GTK_POS_TOP:
4371       redraw_rect.width = allocation.width - 2 * border;
4372       redraw_rect.height = page->allocation.height + style->ythickness;
4373
4374       if (page != priv->cur_page)
4375         redraw_rect.height += style->ythickness;
4376       break;
4377     case GTK_POS_RIGHT:
4378       redraw_rect.x = allocation.width - border -
4379         page->allocation.width - style->xthickness;
4380
4381       if (page != priv->cur_page)
4382         redraw_rect.x -= style->xthickness;
4383       /* fall through */
4384     case GTK_POS_LEFT:
4385       redraw_rect.width = page->allocation.width + style->xthickness;
4386       redraw_rect.height = allocation.height - 2 * border;
4387
4388       if (page != priv->cur_page)
4389         redraw_rect.width += style->xthickness;
4390       break;
4391     }
4392
4393   redraw_rect.x += allocation.x;
4394   redraw_rect.y += allocation.y;
4395
4396   gdk_window_invalidate_rect (gtk_widget_get_window (widget),
4397                               &redraw_rect, TRUE);
4398 }
4399
4400 static void
4401 gtk_notebook_redraw_arrows (GtkNotebook *notebook)
4402 {
4403   GtkNotebookPrivate *priv = notebook->priv;
4404
4405   if (gtk_widget_get_mapped (GTK_WIDGET (notebook)) &&
4406       gtk_notebook_show_arrows (notebook))
4407     {
4408       GdkRectangle rect;
4409       gint i;
4410       GtkNotebookArrow arrow[4];
4411
4412       arrow[0] = priv->has_before_previous ? ARROW_LEFT_BEFORE : ARROW_NONE;
4413       arrow[1] = priv->has_before_next ? ARROW_RIGHT_BEFORE : ARROW_NONE;
4414       arrow[2] = priv->has_after_previous ? ARROW_LEFT_AFTER : ARROW_NONE;
4415       arrow[3] = priv->has_after_next ? ARROW_RIGHT_AFTER : ARROW_NONE;
4416
4417       for (i = 0; i < 4; i++) 
4418         {
4419           if (arrow[i] == ARROW_NONE)
4420             continue;
4421
4422           gtk_notebook_get_arrow_rect (notebook, &rect, arrow[i]);
4423           gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (notebook)),
4424                                       &rect, FALSE);
4425         }
4426     }
4427 }
4428
4429 static gboolean
4430 gtk_notebook_timer (GtkNotebook *notebook)
4431 {
4432   GtkNotebookPrivate *priv = notebook->priv;
4433   gboolean retval = FALSE;
4434
4435   if (priv->timer)
4436     {
4437       gtk_notebook_do_arrow (notebook, priv->click_child);
4438
4439       if (priv->need_timer)
4440         {
4441           GtkSettings *settings;
4442           guint        timeout;
4443
4444           settings = gtk_widget_get_settings (GTK_WIDGET (notebook));
4445           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
4446
4447           priv->need_timer = FALSE;
4448           priv->timer = gdk_threads_add_timeout (timeout * SCROLL_DELAY_FACTOR,
4449                                            (GSourceFunc) gtk_notebook_timer,
4450                                            (gpointer) notebook);
4451         }
4452       else
4453         retval = TRUE;
4454     }
4455
4456   return retval;
4457 }
4458
4459 static void
4460 gtk_notebook_set_scroll_timer (GtkNotebook *notebook)
4461 {
4462   GtkNotebookPrivate *priv = notebook->priv;
4463   GtkWidget *widget = GTK_WIDGET (notebook);
4464
4465   if (!priv->timer)
4466     {
4467       GtkSettings *settings = gtk_widget_get_settings (widget);
4468       guint timeout;
4469
4470       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
4471
4472       priv->timer = gdk_threads_add_timeout (timeout,
4473                                        (GSourceFunc) gtk_notebook_timer,
4474                                        (gpointer) notebook);
4475       priv->need_timer = TRUE;
4476     }
4477 }
4478
4479 static gint
4480 gtk_notebook_page_compare (gconstpointer a,
4481                            gconstpointer b)
4482 {
4483   return (((GtkNotebookPage *) a)->child != b);
4484 }
4485
4486 static GList*
4487 gtk_notebook_find_child (GtkNotebook *notebook,
4488                          GtkWidget   *child,
4489                          const gchar *function)
4490 {
4491   GtkNotebookPrivate *priv = notebook->priv;
4492   GList *list = g_list_find_custom (priv->children, child,
4493                                     gtk_notebook_page_compare);
4494
4495 #ifndef G_DISABLE_CHECKS
4496   if (!list && function)
4497     g_warning ("%s: unable to find child %p in notebook %p",
4498                function, child, notebook);
4499 #endif
4500
4501   return list;
4502 }
4503
4504 static void
4505 gtk_notebook_remove_tab_label (GtkNotebook     *notebook,
4506                                GtkNotebookPage *page)
4507 {
4508   if (page->tab_label)
4509     {
4510       if (page->mnemonic_activate_signal)
4511         g_signal_handler_disconnect (page->tab_label,
4512                                      page->mnemonic_activate_signal);
4513       page->mnemonic_activate_signal = 0;
4514
4515       gtk_widget_set_state (page->tab_label, GTK_STATE_NORMAL);
4516       gtk_widget_unparent (page->tab_label);
4517       page->tab_label = NULL;
4518     }
4519 }
4520
4521 static void
4522 gtk_notebook_real_remove (GtkNotebook *notebook,
4523                           GList       *list)
4524 {
4525   GtkNotebookPrivate *priv = notebook->priv;
4526   GtkNotebookPage *page;
4527   GList * next_list;
4528   gint need_resize = FALSE;
4529   GtkWidget *tab_label;
4530   gboolean destroying;
4531
4532   destroying = gtk_widget_in_destruction (notebook);
4533
4534   next_list = gtk_notebook_search_page (notebook, list, STEP_NEXT, TRUE);
4535   if (!next_list)
4536     next_list = gtk_notebook_search_page (notebook, list, STEP_PREV, TRUE);
4537
4538   priv->children = g_list_remove_link (priv->children, list);
4539
4540   if (priv->cur_page == list->data)
4541     { 
4542       priv->cur_page = NULL;
4543       if (next_list && !destroying)
4544         gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (next_list));
4545     }
4546
4547   if (priv->detached_tab == list->data)
4548     priv->detached_tab = NULL;
4549
4550   if (list == priv->first_tab)
4551     priv->first_tab = next_list;
4552   if (list == priv->focus_tab && !destroying)
4553     gtk_notebook_switch_focus_tab (notebook, next_list);
4554
4555   page = list->data;
4556   
4557   g_signal_handler_disconnect (page->child, page->notify_visible_handler); 
4558
4559   if (gtk_widget_get_visible (page->child) &&
4560       gtk_widget_get_visible (GTK_WIDGET (notebook)))
4561     need_resize = TRUE;
4562
4563   gtk_widget_unparent (page->child);
4564
4565   tab_label = page->tab_label;
4566   if (tab_label)
4567     {
4568       g_object_ref (tab_label);
4569       gtk_notebook_remove_tab_label (notebook, page);
4570       if (destroying)
4571         gtk_widget_destroy (tab_label);
4572       g_object_unref (tab_label);
4573     }
4574
4575   if (priv->menu)
4576     {
4577       GtkWidget *parent = gtk_widget_get_parent (page->menu_label);
4578
4579       gtk_notebook_menu_label_unparent (parent, NULL);
4580       gtk_container_remove (GTK_CONTAINER (priv->menu), parent);
4581
4582       gtk_widget_queue_resize (priv->menu);
4583     }
4584   if (!page->default_menu)
4585     g_object_unref (page->menu_label);
4586
4587   g_list_free (list);
4588
4589   if (page->last_focus_child)
4590     {
4591       g_object_remove_weak_pointer (G_OBJECT (page->last_focus_child), (gpointer *)&page->last_focus_child);
4592       page->last_focus_child = NULL;
4593     }
4594   
4595   g_slice_free (GtkNotebookPage, page);
4596
4597   gtk_notebook_update_labels (notebook);
4598   if (need_resize)
4599     gtk_widget_queue_resize (GTK_WIDGET (notebook));
4600 }
4601
4602 static void
4603 gtk_notebook_update_labels (GtkNotebook *notebook)
4604 {
4605   GtkNotebookPrivate *priv = notebook->priv;
4606   GtkNotebookPage *page;
4607   GList *list;
4608   gchar string[32];
4609   gint page_num = 1;
4610
4611   if (!priv->show_tabs && !priv->menu)
4612     return;
4613
4614   for (list = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, FALSE);
4615        list;
4616        list = gtk_notebook_search_page (notebook, list, STEP_NEXT, FALSE))
4617     {
4618       page = list->data;
4619       g_snprintf (string, sizeof(string), _("Page %u"), page_num++);
4620       if (priv->show_tabs)
4621         {
4622           if (page->default_tab)
4623             {
4624               if (!page->tab_label)
4625                 {
4626                   page->tab_label = gtk_label_new (string);
4627                   gtk_widget_set_parent (page->tab_label,
4628                                          GTK_WIDGET (notebook));
4629                 }
4630               else
4631                 gtk_label_set_text (GTK_LABEL (page->tab_label), string);
4632             }
4633
4634           if (gtk_widget_get_visible (page->child) &&
4635               !gtk_widget_get_visible (page->tab_label))
4636             gtk_widget_show (page->tab_label);
4637           else if (!gtk_widget_get_visible (page->child) &&
4638                    gtk_widget_get_visible (page->tab_label))
4639             gtk_widget_hide (page->tab_label);
4640         }
4641       if (priv->menu && page->default_menu)
4642         {
4643           if (GTK_IS_LABEL (page->tab_label))
4644             gtk_label_set_text (GTK_LABEL (page->menu_label),
4645                                 gtk_label_get_label (GTK_LABEL (page->tab_label)));
4646           else
4647             gtk_label_set_text (GTK_LABEL (page->menu_label), string);
4648         }
4649     }  
4650 }
4651
4652 static gint
4653 gtk_notebook_real_page_position (GtkNotebook *notebook,
4654                                  GList       *list)
4655 {
4656   GtkNotebookPrivate *priv = notebook->priv;
4657   GList *work;
4658   gint count_start;
4659
4660   for (work = priv->children, count_start = 0;
4661        work && work != list; work = work->next)
4662     if (GTK_NOTEBOOK_PAGE (work)->pack == GTK_PACK_START)
4663       count_start++;
4664
4665   if (!work)
4666     return -1;
4667
4668   if (GTK_NOTEBOOK_PAGE (list)->pack == GTK_PACK_START)
4669     return count_start;
4670
4671   return (count_start + g_list_length (list) - 1);
4672 }
4673
4674 static GList *
4675 gtk_notebook_search_page (GtkNotebook *notebook,
4676                           GList       *list,
4677                           gint         direction,
4678                           gboolean     find_visible)
4679 {
4680   GtkNotebookPrivate *priv = notebook->priv;
4681   GtkNotebookPage *page = NULL;
4682   GList *old_list = NULL;
4683   gint flag = 0;
4684
4685   switch (direction)
4686     {
4687     case STEP_PREV:
4688       flag = GTK_PACK_END;
4689       break;
4690
4691     case STEP_NEXT:
4692       flag = GTK_PACK_START;
4693       break;
4694     }
4695
4696   if (list)
4697     page = list->data;
4698
4699   if (!page || page->pack == flag)
4700     {
4701       if (list)
4702         {
4703           old_list = list;
4704           list = list->next;
4705         }
4706       else
4707         list = priv->children;
4708
4709       while (list)
4710         {
4711           page = list->data;
4712           if (page->pack == flag &&
4713               (!find_visible ||
4714                (gtk_widget_get_visible (page->child) &&
4715                 (!page->tab_label || NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page)))))
4716             return list;
4717           old_list = list;
4718           list = list->next;
4719         }
4720       list = old_list;
4721     }
4722   else
4723     {
4724       old_list = list;
4725       list = list->prev;
4726     }
4727   while (list)
4728     {
4729       page = list->data;
4730       if (page->pack != flag &&
4731           (!find_visible ||
4732            (gtk_widget_get_visible (page->child) &&
4733             (!page->tab_label || NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page)))))
4734         return list;
4735       old_list = list;
4736       list = list->prev;
4737     }
4738   return NULL;
4739 }
4740
4741 /* Private GtkNotebook Drawing Functions:
4742  *
4743  * gtk_notebook_paint
4744  * gtk_notebook_draw_tab
4745  * gtk_notebook_draw_arrow
4746  */
4747 static void
4748 gtk_notebook_paint (GtkWidget    *widget,
4749                     cairo_t      *cr)
4750 {
4751   GtkNotebook *notebook;
4752   GtkNotebookPrivate *priv;
4753   GtkNotebookPage *page;
4754   GtkAllocation allocation;
4755   GList *children;
4756   gboolean showarrow;
4757   gint width, height;
4758   gint x, y;
4759   guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
4760   gint gap_x = 0, gap_width = 0, step = STEP_PREV;
4761   gboolean is_rtl;
4762   gint tab_pos;
4763    
4764   notebook = GTK_NOTEBOOK (widget);
4765   priv = notebook->priv;
4766   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
4767   tab_pos = get_effective_tab_pos (notebook);
4768
4769   if ((!priv->show_tabs && !priv->show_border) ||
4770       !priv->cur_page || !gtk_widget_get_visible (priv->cur_page->child))
4771     return;
4772
4773   gtk_widget_get_allocation (widget, &allocation);
4774
4775   x = allocation.x + border_width;
4776   y = allocation.y + border_width;
4777   width = allocation.width - border_width * 2;
4778   height = allocation.height - border_width * 2;
4779
4780   if (priv->show_border && (!priv->show_tabs || !priv->children))
4781     {
4782       gtk_paint_box (gtk_widget_get_style (widget), cr,
4783                      GTK_STATE_NORMAL, GTK_SHADOW_OUT,
4784                      widget, "notebook",
4785                      x, y, width, height);
4786       return;
4787     }
4788
4789   if (!priv->first_tab)
4790     priv->first_tab = priv->children;
4791
4792   if (!gtk_widget_get_mapped (priv->cur_page->tab_label))
4793     page = GTK_NOTEBOOK_PAGE (priv->first_tab);
4794   else
4795     page = priv->cur_page;
4796
4797   switch (tab_pos)
4798     {
4799     case GTK_POS_TOP:
4800       y += page->allocation.height;
4801       /* fall thru */
4802     case GTK_POS_BOTTOM:
4803       height -= page->allocation.height;
4804       break;
4805     case GTK_POS_LEFT:
4806       x += page->allocation.width;
4807       /* fall thru */
4808     case GTK_POS_RIGHT:
4809       width -= page->allocation.width;
4810       break;
4811     }
4812
4813   if (!NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, priv->cur_page) ||
4814       !gtk_widget_get_mapped (priv->cur_page->tab_label))
4815     {
4816       gap_x = 0;
4817       gap_width = 0;
4818     }
4819   else
4820     {
4821       switch (tab_pos)
4822         {
4823         case GTK_POS_TOP:
4824         case GTK_POS_BOTTOM:
4825           if (priv->operation == DRAG_OPERATION_REORDER)
4826             gap_x = priv->drag_window_x - allocation.x - border_width;
4827           else
4828             gap_x = priv->cur_page->allocation.x - allocation.x - border_width;
4829
4830           gap_width = priv->cur_page->allocation.width;
4831           step = is_rtl ? STEP_NEXT : STEP_PREV;
4832           break;
4833         case GTK_POS_LEFT:
4834         case GTK_POS_RIGHT:
4835           if (priv->operation == DRAG_OPERATION_REORDER)
4836             gap_x = priv->drag_window_y - border_width - allocation.y;
4837           else
4838             gap_x = priv->cur_page->allocation.y - allocation.y - border_width;
4839
4840           gap_width = priv->cur_page->allocation.height;
4841           step = STEP_PREV;
4842           break;
4843         }
4844     }
4845   gtk_paint_box_gap (gtk_widget_get_style (widget), cr,
4846                      GTK_STATE_NORMAL, GTK_SHADOW_OUT,
4847                      widget, "notebook",
4848                      x, y, width, height,
4849                      tab_pos, gap_x, gap_width);
4850
4851   showarrow = FALSE;
4852   children = gtk_notebook_search_page (notebook, NULL, step, TRUE);
4853   while (children)
4854     {
4855       page = children->data;
4856       children = gtk_notebook_search_page (notebook, children,
4857                                            step, TRUE);
4858       if (!gtk_widget_get_visible (page->child))
4859         continue;
4860       if (!gtk_widget_get_mapped (page->tab_label))
4861         showarrow = TRUE;
4862       else if (page != priv->cur_page)
4863         gtk_notebook_draw_tab (notebook, page, cr);
4864     }
4865
4866   if (showarrow && priv->scrollable)
4867     {
4868       if (priv->has_before_previous)
4869         gtk_notebook_draw_arrow (notebook, cr, ARROW_LEFT_BEFORE);
4870       if (priv->has_before_next)
4871         gtk_notebook_draw_arrow (notebook, cr, ARROW_RIGHT_BEFORE);
4872       if (priv->has_after_previous)
4873         gtk_notebook_draw_arrow (notebook, cr, ARROW_LEFT_AFTER);
4874       if (priv->has_after_next)
4875         gtk_notebook_draw_arrow (notebook, cr, ARROW_RIGHT_AFTER);
4876     }
4877
4878   if (priv->operation != DRAG_OPERATION_REORDER)
4879     gtk_notebook_draw_tab (notebook, priv->cur_page, cr);
4880 }
4881
4882 static void
4883 gtk_notebook_draw_tab (GtkNotebook     *notebook,
4884                        GtkNotebookPage *page,
4885                        cairo_t         *cr)
4886 {
4887   GtkNotebookPrivate *priv;
4888   GtkStateType state_type;
4889   GtkWidget *widget;
4890   
4891   if (!NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) ||
4892       !gtk_widget_get_mapped (page->tab_label) ||
4893       (page->allocation.width == 0) || (page->allocation.height == 0))
4894     return;
4895
4896   widget = GTK_WIDGET (notebook);
4897   priv = notebook->priv;
4898
4899   if (priv->cur_page == page)
4900     state_type = GTK_STATE_NORMAL;
4901   else 
4902     state_type = GTK_STATE_ACTIVE;
4903
4904   gtk_paint_extension (gtk_widget_get_style (widget), cr,
4905                        state_type, GTK_SHADOW_OUT,
4906                        widget, "tab",
4907                        page->allocation.x,
4908                        page->allocation.y,
4909                        page->allocation.width,
4910                        page->allocation.height,
4911                        get_tab_gap_pos (notebook));
4912
4913   if (gtk_widget_has_focus (widget) &&
4914       priv->cur_page == page)
4915     {
4916       gint focus_width;
4917       GtkAllocation allocation;
4918
4919       gtk_widget_get_allocation (page->tab_label, &allocation);
4920       gtk_widget_style_get (widget, "focus-line-width", &focus_width, NULL);
4921
4922       gtk_paint_focus (gtk_widget_get_style (widget), cr,
4923                        gtk_widget_get_state (widget), widget, "tab",
4924                        allocation.x - focus_width,
4925                        allocation.y - focus_width,
4926                        allocation.width + 2 * focus_width,
4927                        allocation.height + 2 * focus_width);
4928     }
4929 }
4930
4931 static void
4932 gtk_notebook_draw_arrow (GtkNotebook      *notebook,
4933                          cairo_t          *cr,
4934                          GtkNotebookArrow  nbarrow)
4935 {
4936   GtkNotebookPrivate *priv = notebook->priv;
4937   GtkStateType state_type;
4938   GtkShadowType shadow_type;
4939   GtkWidget *widget;
4940   GdkRectangle arrow_rect;
4941   GtkArrowType arrow;
4942   gboolean is_rtl, left;
4943   gint scroll_arrow_hlength;
4944   gint scroll_arrow_vlength;
4945   gint arrow_size;
4946
4947   widget = GTK_WIDGET (notebook);
4948
4949   gtk_notebook_get_arrow_rect (notebook, &arrow_rect, nbarrow);
4950
4951   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
4952   left = (ARROW_IS_LEFT (nbarrow) && !is_rtl) ||
4953          (!ARROW_IS_LEFT (nbarrow) && is_rtl); 
4954
4955   gtk_widget_style_get (widget,
4956                         "scroll-arrow-hlength", &scroll_arrow_hlength,
4957                         "scroll-arrow-vlength", &scroll_arrow_vlength,
4958                         NULL);
4959
4960   if (priv->in_child == nbarrow)
4961     {
4962       if (priv->click_child == nbarrow)
4963         state_type = GTK_STATE_ACTIVE;
4964       else
4965         state_type = GTK_STATE_PRELIGHT;
4966     }
4967   else
4968     state_type = gtk_widget_get_state (widget);
4969
4970   if (priv->click_child == nbarrow)
4971     shadow_type = GTK_SHADOW_IN;
4972   else
4973     shadow_type = GTK_SHADOW_OUT;
4974
4975   if (priv->focus_tab &&
4976       !gtk_notebook_search_page (notebook, priv->focus_tab,
4977                                  left ? STEP_PREV : STEP_NEXT, TRUE))
4978     {
4979       shadow_type = GTK_SHADOW_ETCHED_IN;
4980       state_type = GTK_STATE_INSENSITIVE;
4981     }
4982   
4983   if (priv->tab_pos == GTK_POS_LEFT ||
4984       priv->tab_pos == GTK_POS_RIGHT)
4985     {
4986       arrow = (ARROW_IS_LEFT (nbarrow) ? GTK_ARROW_UP : GTK_ARROW_DOWN);
4987       arrow_size = scroll_arrow_vlength;
4988     }
4989   else
4990     {
4991       arrow = (ARROW_IS_LEFT (nbarrow) ? GTK_ARROW_LEFT : GTK_ARROW_RIGHT);
4992       arrow_size = scroll_arrow_hlength;
4993     }
4994  
4995   gtk_paint_arrow (gtk_widget_get_style (widget),
4996                    cr, state_type, 
4997                    shadow_type, widget, "notebook",
4998                    arrow, TRUE, arrow_rect.x, arrow_rect.y, 
4999                    arrow_size, arrow_size);
5000 }
5001
5002 /* Private GtkNotebook Size Allocate Functions:
5003  *
5004  * gtk_notebook_tab_space
5005  * gtk_notebook_calculate_shown_tabs
5006  * gtk_notebook_calculate_tabs_allocation
5007  * gtk_notebook_pages_allocate
5008  * gtk_notebook_page_allocate
5009  * gtk_notebook_calc_tabs
5010  */
5011 static void
5012 gtk_notebook_tab_space (GtkNotebook *notebook,
5013                         gboolean    *show_arrows,
5014                         gint        *min,
5015                         gint        *max,
5016                         gint        *tab_space)
5017 {
5018   GtkNotebookPrivate *priv = notebook->priv;
5019   GtkAllocation allocation, action_allocation;
5020   GtkWidget *widget;
5021   GtkStyle *style;
5022   GList *children;
5023   gint tab_pos = get_effective_tab_pos (notebook);
5024   gint tab_overlap;
5025   gint arrow_spacing;
5026   gint scroll_arrow_hlength;
5027   gint scroll_arrow_vlength;
5028   gboolean is_rtl;
5029   gint i;
5030   guint border_width;
5031
5032   widget = GTK_WIDGET (notebook);
5033   children = priv->children;
5034   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
5035
5036   style = gtk_widget_get_style (widget);
5037
5038   gtk_widget_style_get (GTK_WIDGET (notebook),
5039                         "arrow-spacing", &arrow_spacing,
5040                         "scroll-arrow-hlength", &scroll_arrow_hlength,
5041                         "scroll-arrow-vlength", &scroll_arrow_vlength,
5042                         NULL);
5043
5044   border_width = gtk_container_get_border_width (GTK_CONTAINER (notebook));
5045
5046   gtk_widget_get_allocation (widget, &allocation);
5047
5048   switch (tab_pos)
5049     {
5050     case GTK_POS_TOP:
5051     case GTK_POS_BOTTOM:
5052       *min = allocation.x + border_width;
5053       *max = allocation.x + allocation.width - border_width;
5054
5055       for (i = 0; i < N_ACTION_WIDGETS; i++)
5056         {
5057           if (priv->action_widget[i])
5058             {
5059               gtk_widget_get_allocation (priv->action_widget[i], &action_allocation);
5060
5061               if ((i == ACTION_WIDGET_START && !is_rtl) ||
5062                   (i == ACTION_WIDGET_END && is_rtl))
5063                 *min += action_allocation.width + style->xthickness;
5064               else
5065                 *max -= action_allocation.width + style->xthickness;
5066             }
5067         }
5068
5069       while (children)
5070         {
5071           GtkNotebookPage *page;
5072
5073           page = children->data;
5074           children = children->next;
5075
5076           if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) &&
5077               gtk_widget_get_visible (page->child))
5078             *tab_space += page->requisition.width;
5079         }
5080       break;
5081     case GTK_POS_RIGHT:
5082     case GTK_POS_LEFT:
5083       *min = allocation.y + border_width;
5084       *max = allocation.y + allocation.height - border_width;
5085
5086       for (i = 0; i < N_ACTION_WIDGETS; i++)
5087         {
5088           if (priv->action_widget[i])
5089             {
5090               gtk_widget_get_allocation (priv->action_widget[i], &action_allocation);
5091
5092               if (i == ACTION_WIDGET_START)
5093                 *min += action_allocation.height + style->ythickness;
5094               else
5095                 *max -= action_allocation.height + style->ythickness;
5096             }
5097         }
5098
5099       while (children)
5100         {
5101           GtkNotebookPage *page;
5102
5103           page = children->data;
5104           children = children->next;
5105
5106           if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) &&
5107               gtk_widget_get_visible (page->child))
5108             *tab_space += page->requisition.height;
5109         }
5110       break;
5111     }
5112
5113   if (!priv->scrollable)
5114     *show_arrows = FALSE;
5115   else
5116     {
5117       gtk_widget_style_get (widget, "tab-overlap", &tab_overlap, NULL);
5118
5119       switch (tab_pos)
5120         {
5121         case GTK_POS_TOP:
5122         case GTK_POS_BOTTOM:
5123           if (*tab_space > *max - *min - tab_overlap)
5124             {
5125               *show_arrows = TRUE;
5126
5127               /* take arrows into account */
5128               *tab_space = *max - *min - tab_overlap;
5129
5130               if (priv->has_after_previous)
5131                 {
5132                   *tab_space -= arrow_spacing + scroll_arrow_hlength;
5133                   *max -= arrow_spacing + scroll_arrow_hlength;
5134                 }
5135
5136               if (priv->has_after_next)
5137                 {
5138                   *tab_space -= arrow_spacing + scroll_arrow_hlength;
5139                   *max -= arrow_spacing + scroll_arrow_hlength;
5140                 }
5141
5142               if (priv->has_before_previous)
5143                 {
5144                   *tab_space -= arrow_spacing + scroll_arrow_hlength;
5145                   *min += arrow_spacing + scroll_arrow_hlength;
5146                 }
5147
5148               if (priv->has_before_next)
5149                 {
5150                   *tab_space -= arrow_spacing + scroll_arrow_hlength;
5151                   *min += arrow_spacing + scroll_arrow_hlength;
5152                 }
5153             }
5154           break;
5155         case GTK_POS_LEFT:
5156         case GTK_POS_RIGHT:
5157           if (*tab_space > *max - *min - tab_overlap)
5158             {
5159               *show_arrows = TRUE;
5160
5161               /* take arrows into account */
5162               *tab_space = *max - *min - tab_overlap;
5163
5164               if (priv->has_after_previous || priv->has_after_next)
5165                 {
5166                   *tab_space -= arrow_spacing + scroll_arrow_vlength;
5167                   *max -= arrow_spacing + scroll_arrow_vlength;
5168                 }
5169
5170               if (priv->has_before_previous || priv->has_before_next)
5171                 {
5172                   *tab_space -= arrow_spacing + scroll_arrow_vlength;
5173                   *min += arrow_spacing + scroll_arrow_vlength;
5174                 }
5175             }
5176           break;
5177         }
5178     }
5179 }
5180
5181 static void
5182 gtk_notebook_calculate_shown_tabs (GtkNotebook *notebook,
5183                                    gboolean     show_arrows,
5184                                    gint         min,
5185                                    gint         max,
5186                                    gint         tab_space,
5187                                    GList      **last_child,
5188                                    gint        *n,
5189                                    gint        *remaining_space)
5190 {
5191   GtkNotebookPrivate *priv = notebook->priv;
5192   GtkWidget *widget;
5193   GtkContainer *container;
5194   GList *children;
5195   GtkNotebookPage *page;
5196   gint tab_pos, tab_overlap;
5197   
5198   widget = GTK_WIDGET (notebook);
5199   container = GTK_CONTAINER (notebook);
5200   gtk_widget_style_get (widget, "tab-overlap", &tab_overlap, NULL);
5201   tab_pos = get_effective_tab_pos (notebook);
5202
5203   if (show_arrows) /* first_tab <- focus_tab */
5204     {
5205       *remaining_space = tab_space;
5206
5207       if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, priv->cur_page) &&
5208           gtk_widget_get_visible (priv->cur_page->child))
5209         {
5210           gtk_notebook_calc_tabs (notebook,
5211                                   priv->focus_tab,
5212                                   &(priv->focus_tab),
5213                                   remaining_space, STEP_NEXT);
5214         }
5215
5216       if (tab_space <= 0 || *remaining_space <= 0)
5217         {
5218           /* show 1 tab */
5219           priv->first_tab = priv->focus_tab;
5220           *last_child = gtk_notebook_search_page (notebook, priv->focus_tab,
5221                                                   STEP_NEXT, TRUE);
5222           page = priv->first_tab->data;
5223           *remaining_space = tab_space - page->requisition.width;
5224           *n = 1;
5225         }
5226       else
5227         {
5228           children = NULL;
5229
5230           if (priv->first_tab && priv->first_tab != priv->focus_tab)
5231             {
5232               /* Is first_tab really predecessor of focus_tab? */
5233               page = priv->first_tab->data;
5234               if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) &&
5235                   gtk_widget_get_visible (page->child))
5236                 for (children = priv->focus_tab;
5237                      children && children != priv->first_tab;
5238                      children = gtk_notebook_search_page (notebook,
5239                                                           children,
5240                                                           STEP_PREV,
5241                                                           TRUE));
5242             }
5243
5244           if (!children)
5245             {
5246               if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, priv->cur_page))
5247                 priv->first_tab = priv->focus_tab;
5248               else
5249                 priv->first_tab = gtk_notebook_search_page (notebook, priv->focus_tab,
5250                                                                 STEP_NEXT, TRUE);
5251             }
5252           else
5253             /* calculate shown tabs counting backwards from the focus tab */
5254             gtk_notebook_calc_tabs (notebook,
5255                                     gtk_notebook_search_page (notebook,
5256                                                               priv->focus_tab,
5257                                                               STEP_PREV,
5258                                                               TRUE),
5259                                     &(priv->first_tab), remaining_space,
5260                                     STEP_PREV);
5261
5262           if (*remaining_space < 0)
5263             {
5264               priv->first_tab =
5265                 gtk_notebook_search_page (notebook, priv->first_tab,
5266                                           STEP_NEXT, TRUE);
5267               if (!priv->first_tab)
5268                 priv->first_tab = priv->focus_tab;
5269
5270               *last_child = gtk_notebook_search_page (notebook, priv->focus_tab,
5271                                                       STEP_NEXT, TRUE); 
5272             }
5273           else /* focus_tab -> end */   
5274             {
5275               if (!priv->first_tab)
5276                 priv->first_tab = gtk_notebook_search_page (notebook,
5277                                                                 NULL,
5278                                                                 STEP_NEXT,
5279                                                                 TRUE);
5280               children = NULL;
5281               gtk_notebook_calc_tabs (notebook,
5282                                       gtk_notebook_search_page (notebook,
5283                                                                 priv->focus_tab,
5284                                                                 STEP_NEXT,
5285                                                                 TRUE),
5286                                       &children, remaining_space, STEP_NEXT);
5287
5288               if (*remaining_space <= 0) 
5289                 *last_child = children;
5290               else /* start <- first_tab */
5291                 {
5292                   *last_child = NULL;
5293                   children = NULL;
5294
5295                   gtk_notebook_calc_tabs (notebook,
5296                                           gtk_notebook_search_page (notebook,
5297                                                                     priv->first_tab,
5298                                                                     STEP_PREV,
5299                                                                     TRUE),
5300                                           &children, remaining_space, STEP_PREV);
5301
5302                   if (*remaining_space == 0)
5303                     priv->first_tab = children;
5304                   else
5305                     priv->first_tab = gtk_notebook_search_page(notebook,
5306                                                                    children,
5307                                                                    STEP_NEXT,
5308                                                                    TRUE);
5309                 }
5310             }
5311
5312           if (*remaining_space < 0)
5313             {
5314               /* calculate number of tabs */
5315               *remaining_space = - (*remaining_space);
5316               *n = 0;
5317
5318               for (children = priv->first_tab;
5319                    children && children != *last_child;
5320                    children = gtk_notebook_search_page (notebook, children,
5321                                                         STEP_NEXT, TRUE))
5322                 (*n)++;
5323             }
5324           else
5325             *remaining_space = 0;
5326         }
5327
5328       /* unmap all non-visible tabs */
5329       for (children = gtk_notebook_search_page (notebook, NULL,
5330                                                 STEP_NEXT, TRUE);
5331            children && children != priv->first_tab;
5332            children = gtk_notebook_search_page (notebook, children,
5333                                                 STEP_NEXT, TRUE))
5334         {
5335           page = children->data;
5336
5337           if (page->tab_label &&
5338               NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page))
5339             gtk_widget_set_child_visible (page->tab_label, FALSE);
5340         }
5341
5342       for (children = *last_child; children;
5343            children = gtk_notebook_search_page (notebook, children,
5344                                                 STEP_NEXT, TRUE))
5345         {
5346           page = children->data;
5347
5348           if (page->tab_label &&
5349               NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page))
5350             gtk_widget_set_child_visible (page->tab_label, FALSE);
5351         }
5352     }
5353   else /* !show_arrows */
5354     {
5355       gint c = 0;
5356       *n = 0;
5357
5358       *remaining_space = max - min - tab_overlap - tab_space;
5359       children = priv->children;
5360       priv->first_tab = gtk_notebook_search_page (notebook, NULL,
5361                                                       STEP_NEXT, TRUE);
5362       while (children)
5363         {
5364           page = children->data;
5365           children = children->next;
5366
5367           if (!NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) ||
5368               !gtk_widget_get_visible (page->child))
5369             continue;
5370
5371           c++;
5372
5373           if (page->expand)
5374             (*n)++;
5375         }
5376
5377       /* if notebook is homogeneous, all tabs are expanded */
5378       if (priv->homogeneous && *n)
5379         *n = c;
5380     }
5381 }
5382
5383 static gboolean
5384 get_allocate_at_bottom (GtkWidget *widget,
5385                         gint       search_direction)
5386 {
5387   gboolean is_rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
5388   gboolean tab_pos = get_effective_tab_pos (GTK_NOTEBOOK (widget));
5389
5390   switch (tab_pos)
5391     {
5392     case GTK_POS_TOP:
5393     case GTK_POS_BOTTOM:
5394       if (!is_rtl)
5395         return (search_direction == STEP_PREV);
5396       else
5397         return (search_direction == STEP_NEXT);
5398
5399       break;
5400     case GTK_POS_RIGHT:
5401     case GTK_POS_LEFT:
5402       return (search_direction == STEP_PREV);
5403       break;
5404     }
5405
5406   return FALSE;
5407 }
5408
5409 static void
5410 gtk_notebook_calculate_tabs_allocation (GtkNotebook  *notebook,
5411                                         GList       **children,
5412                                         GList        *last_child,
5413                                         gboolean      showarrow,
5414                                         gint          direction,
5415                                         gint         *remaining_space,
5416                                         gint         *expanded_tabs,
5417                                         gint          min,
5418                                         gint          max)
5419 {
5420   GtkNotebookPrivate *priv = notebook->priv;
5421   GtkAllocation allocation;
5422   GtkWidget *widget;
5423   GtkContainer *container;
5424   GtkNotebookPage *page;
5425   GtkStyle *style;
5426   gboolean allocate_at_bottom;
5427   gint tab_overlap, tab_pos, tab_extra_space;
5428   gint left_x, right_x, top_y, bottom_y, anchor;
5429   gint xthickness, ythickness;
5430   guint border_width;
5431   gboolean gap_left, packing_changed;
5432   GtkAllocation child_allocation = { 0, };
5433
5434   widget = GTK_WIDGET (notebook);
5435   container = GTK_CONTAINER (notebook);
5436   gtk_widget_style_get (widget, "tab-overlap", &tab_overlap, NULL);
5437   tab_pos = get_effective_tab_pos (notebook);
5438   allocate_at_bottom = get_allocate_at_bottom (widget, direction);
5439   anchor = 0;
5440
5441   gtk_widget_get_allocation (widget, &allocation);
5442
5443   border_width = gtk_container_get_border_width (container);
5444   child_allocation.x = allocation.x + border_width;
5445   child_allocation.y = allocation.y + border_width;
5446
5447   style = gtk_widget_get_style (widget);
5448   xthickness = style->xthickness;
5449   ythickness = style->ythickness;
5450
5451   switch (tab_pos)
5452     {
5453     case GTK_POS_BOTTOM:
5454       child_allocation.y = allocation.y + allocation.height -
5455         priv->cur_page->requisition.height - border_width;
5456       /* fall through */
5457     case GTK_POS_TOP:
5458       child_allocation.x = (allocate_at_bottom) ? max : min;
5459       child_allocation.height = priv->cur_page->requisition.height;
5460       anchor = child_allocation.x;
5461       break;
5462       
5463     case GTK_POS_RIGHT:
5464       child_allocation.x = allocation.x + allocation.width -
5465         priv->cur_page->requisition.width - border_width;
5466       /* fall through */
5467     case GTK_POS_LEFT:
5468       child_allocation.y = (allocate_at_bottom) ? max : min;
5469       child_allocation.width = priv->cur_page->requisition.width;
5470       anchor = child_allocation.y;
5471       break;
5472     }
5473
5474   left_x   = CLAMP (priv->mouse_x - priv->drag_offset_x,
5475                     min, max - priv->cur_page->allocation.width);
5476   top_y    = CLAMP (priv->mouse_y - priv->drag_offset_y,
5477                     min, max - priv->cur_page->allocation.height);
5478   right_x  = left_x + priv->cur_page->allocation.width;
5479   bottom_y = top_y + priv->cur_page->allocation.height;
5480   gap_left = packing_changed = FALSE;
5481
5482   while (*children && *children != last_child)
5483     {
5484       page = (*children)->data;
5485
5486       if (direction == STEP_NEXT && page->pack != GTK_PACK_START)
5487         {
5488           if (!showarrow)
5489             break;
5490           else if (priv->operation == DRAG_OPERATION_REORDER)
5491             packing_changed = TRUE;
5492         }
5493
5494       if (direction == STEP_NEXT)
5495         *children = gtk_notebook_search_page (notebook, *children, direction, TRUE);
5496       else
5497         {
5498           *children = (*children)->next;
5499
5500           if (page->pack != GTK_PACK_END || !gtk_widget_get_visible (page->child))
5501             continue;
5502         }
5503
5504       if (!NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page))
5505         continue;
5506
5507       tab_extra_space = 0;
5508       if (*expanded_tabs && (showarrow || page->expand || priv->homogeneous))
5509         {
5510           tab_extra_space = *remaining_space / *expanded_tabs;
5511           *remaining_space -= tab_extra_space;
5512           (*expanded_tabs)--;
5513         }
5514
5515       switch (tab_pos)
5516         {
5517         case GTK_POS_TOP:
5518         case GTK_POS_BOTTOM:
5519           child_allocation.width = page->requisition.width + tab_overlap + tab_extra_space;
5520
5521           /* make sure that the reordered tab doesn't go past the last position */
5522           if (priv->operation == DRAG_OPERATION_REORDER &&
5523               !gap_left && packing_changed)
5524             {
5525               if (!allocate_at_bottom)
5526                 {
5527                   if ((priv->cur_page->pack == GTK_PACK_START && left_x >= anchor) ||
5528                       (priv->cur_page->pack == GTK_PACK_END && left_x < anchor))
5529                     {
5530                       left_x = priv->drag_window_x = anchor;
5531                       anchor += priv->cur_page->allocation.width - tab_overlap;
5532                     }
5533                 }
5534               else
5535                 {
5536                   if ((priv->cur_page->pack == GTK_PACK_START && right_x <= anchor) ||
5537                       (priv->cur_page->pack == GTK_PACK_END && right_x > anchor))
5538                     {
5539                       anchor -= priv->cur_page->allocation.width;
5540                       left_x = priv->drag_window_x = anchor;
5541                       anchor += tab_overlap;
5542                     }
5543                 }
5544
5545               gap_left = TRUE;
5546             }
5547
5548           if (priv->operation == DRAG_OPERATION_REORDER && page == priv->cur_page)
5549             {
5550               priv->drag_window_x = left_x;
5551               priv->drag_window_y = child_allocation.y;
5552             }
5553           else
5554             {
5555               if (allocate_at_bottom)
5556                 anchor -= child_allocation.width;
5557
5558               if (priv->operation == DRAG_OPERATION_REORDER && page->pack == priv->cur_page->pack)
5559                 {
5560                   if (!allocate_at_bottom &&
5561                       left_x >= anchor &&
5562                       left_x <= anchor + child_allocation.width / 2)
5563                     anchor += priv->cur_page->allocation.width - tab_overlap;
5564                   else if (allocate_at_bottom &&
5565                            right_x >= anchor + child_allocation.width / 2 &&
5566                            right_x <= anchor + child_allocation.width)
5567                     anchor -= priv->cur_page->allocation.width - tab_overlap;
5568                 }
5569
5570               child_allocation.x = anchor;
5571             }
5572
5573           break;
5574         case GTK_POS_LEFT:
5575         case GTK_POS_RIGHT:
5576           child_allocation.height = page->requisition.height + tab_overlap + tab_extra_space;
5577
5578           /* make sure that the reordered tab doesn't go past the last position */
5579           if (priv->operation == DRAG_OPERATION_REORDER &&
5580               !gap_left && packing_changed)
5581             {
5582               if (!allocate_at_bottom &&
5583                   ((priv->cur_page->pack == GTK_PACK_START && top_y >= anchor) ||
5584                    (priv->cur_page->pack == GTK_PACK_END && top_y < anchor)))
5585                 {
5586                   top_y = priv->drag_window_y = anchor;
5587                   anchor += priv->cur_page->allocation.height - tab_overlap;
5588                 }
5589  
5590               gap_left = TRUE;
5591             }
5592
5593           if (priv->operation == DRAG_OPERATION_REORDER && page == priv->cur_page)
5594             {
5595               priv->drag_window_x = child_allocation.x;
5596               priv->drag_window_y = top_y;
5597             }
5598           else
5599             {
5600               if (allocate_at_bottom)
5601                 anchor -= child_allocation.height;
5602
5603               if (priv->operation == DRAG_OPERATION_REORDER && page->pack == priv->cur_page->pack)
5604                 {
5605                   if (!allocate_at_bottom &&
5606                       top_y >= anchor &&
5607                       top_y <= anchor + child_allocation.height / 2)
5608                     anchor += priv->cur_page->allocation.height - tab_overlap;
5609                   else if (allocate_at_bottom &&
5610                            bottom_y >= anchor + child_allocation.height / 2 &&
5611                            bottom_y <= anchor + child_allocation.height)
5612                     anchor -= priv->cur_page->allocation.height - tab_overlap;
5613                 }
5614
5615               child_allocation.y = anchor;
5616             }
5617
5618           break;
5619         }
5620
5621       page->allocation = child_allocation;
5622
5623       if ((page == priv->detached_tab && priv->operation == DRAG_OPERATION_DETACH) ||
5624           (page == priv->cur_page && priv->operation == DRAG_OPERATION_REORDER))
5625         {
5626           /* needs to be allocated at 0,0
5627            * to be shown in the drag window */
5628           page->allocation.x = 0;
5629           page->allocation.y = 0;
5630         }
5631       
5632       if (page != priv->cur_page)
5633         {
5634           switch (tab_pos)
5635             {
5636             case GTK_POS_TOP:
5637               page->allocation.y += ythickness;
5638               /* fall through */
5639             case GTK_POS_BOTTOM:
5640               page->allocation.height = MAX (1, page->allocation.height - ythickness);
5641               break;
5642             case GTK_POS_LEFT:
5643               page->allocation.x += xthickness;
5644               /* fall through */
5645             case GTK_POS_RIGHT:
5646               page->allocation.width = MAX (1, page->allocation.width - xthickness);
5647               break;
5648             }
5649         }
5650
5651       /* calculate whether to leave a gap based on reorder operation or not */
5652       switch (tab_pos)
5653         {
5654         case GTK_POS_TOP:
5655         case GTK_POS_BOTTOM:
5656           if (priv->operation != DRAG_OPERATION_REORDER ||
5657               (priv->operation == DRAG_OPERATION_REORDER && page != priv->cur_page))
5658             {
5659               if (priv->operation == DRAG_OPERATION_REORDER)
5660                 {
5661                   if (page->pack == priv->cur_page->pack &&
5662                       !allocate_at_bottom &&
5663                       left_x >  anchor + child_allocation.width / 2 &&
5664                       left_x <= anchor + child_allocation.width)
5665                     anchor += priv->cur_page->allocation.width - tab_overlap;
5666                   else if (page->pack == priv->cur_page->pack &&
5667                            allocate_at_bottom &&
5668                            right_x >= anchor &&
5669                            right_x <= anchor + child_allocation.width / 2)
5670                     anchor -= priv->cur_page->allocation.width - tab_overlap;
5671                 }
5672  
5673               if (!allocate_at_bottom)
5674                 anchor += child_allocation.width - tab_overlap;
5675               else
5676                 anchor += tab_overlap;
5677             }
5678
5679           break;
5680         case GTK_POS_LEFT:
5681         case GTK_POS_RIGHT:
5682           if (priv->operation != DRAG_OPERATION_REORDER  ||
5683               (priv->operation == DRAG_OPERATION_REORDER && page != priv->cur_page))
5684             {
5685               if (priv->operation == DRAG_OPERATION_REORDER)
5686                 {
5687                   if (page->pack == priv->cur_page->pack &&
5688                       !allocate_at_bottom &&
5689                       top_y >= anchor + child_allocation.height / 2 &&
5690                       top_y <= anchor + child_allocation.height)
5691                     anchor += priv->cur_page->allocation.height - tab_overlap;
5692                   else if (page->pack == priv->cur_page->pack &&
5693                            allocate_at_bottom &&
5694                            bottom_y >= anchor &&
5695                            bottom_y <= anchor + child_allocation.height / 2)
5696                     anchor -= priv->cur_page->allocation.height - tab_overlap;
5697                 }
5698
5699               if (!allocate_at_bottom)
5700                 anchor += child_allocation.height - tab_overlap;
5701               else
5702                 anchor += tab_overlap;
5703             }
5704
5705           break;
5706         }
5707
5708       /* set child visible */
5709       if (page->tab_label)
5710         gtk_widget_set_child_visible (page->tab_label, TRUE);
5711     }
5712
5713   /* Don't move the current tab past the last position during tabs reordering */
5714   if (children &&
5715       priv->operation == DRAG_OPERATION_REORDER &&
5716       ((direction == STEP_NEXT && priv->cur_page->pack == GTK_PACK_START) ||
5717        ((direction == STEP_PREV || packing_changed) && priv->cur_page->pack == GTK_PACK_END)))
5718     {
5719       switch (tab_pos)
5720         {
5721         case GTK_POS_TOP:
5722         case GTK_POS_BOTTOM:
5723           if (allocate_at_bottom)
5724             anchor -= priv->cur_page->allocation.width;
5725
5726           if ((!allocate_at_bottom && priv->drag_window_x > anchor) ||
5727               (allocate_at_bottom && priv->drag_window_x < anchor))
5728             priv->drag_window_x = anchor;
5729           break;
5730         case GTK_POS_LEFT:
5731         case GTK_POS_RIGHT:
5732           if (allocate_at_bottom)
5733             anchor -= priv->cur_page->allocation.height;
5734
5735           if ((!allocate_at_bottom && priv->drag_window_y > anchor) ||
5736               (allocate_at_bottom && priv->drag_window_y < anchor))
5737             priv->drag_window_y = anchor;
5738           break;
5739         }
5740     }
5741 }
5742
5743 static void
5744 gtk_notebook_pages_allocate (GtkNotebook *notebook)
5745 {
5746   GtkNotebookPrivate *priv = notebook->priv;
5747   GList *children = NULL;
5748   GList *last_child = NULL;
5749   gboolean showarrow = FALSE;
5750   gint tab_space, min, max, remaining_space;
5751   gint expanded_tabs;
5752   gboolean tab_allocations_changed = FALSE;
5753
5754   if (!priv->show_tabs || !priv->children || !priv->cur_page)
5755     return;
5756
5757   min = max = tab_space = remaining_space = 0;
5758   expanded_tabs = 1;
5759
5760   gtk_notebook_tab_space (notebook, &showarrow,
5761                           &min, &max, &tab_space);
5762
5763   gtk_notebook_calculate_shown_tabs (notebook, showarrow,
5764                                      min, max, tab_space, &last_child,
5765                                      &expanded_tabs, &remaining_space);
5766
5767   children = priv->first_tab;
5768   gtk_notebook_calculate_tabs_allocation (notebook, &children, last_child,
5769                                           showarrow, STEP_NEXT,
5770                                           &remaining_space, &expanded_tabs, min, max);
5771   if (children && children != last_child)
5772     {
5773       children = priv->children;
5774       gtk_notebook_calculate_tabs_allocation (notebook, &children, last_child,
5775                                               showarrow, STEP_PREV,
5776                                               &remaining_space, &expanded_tabs, min, max);
5777     }
5778
5779   children = priv->children;
5780
5781   while (children)
5782     {
5783       if (gtk_notebook_page_allocate (notebook, GTK_NOTEBOOK_PAGE (children)))
5784         tab_allocations_changed = TRUE;
5785       children = children->next;
5786     }
5787
5788   if (!priv->first_tab)
5789     priv->first_tab = priv->children;
5790
5791   if (tab_allocations_changed)
5792     gtk_notebook_redraw_tabs (notebook);
5793 }
5794
5795 static gboolean
5796 gtk_notebook_page_allocate (GtkNotebook     *notebook,
5797                             GtkNotebookPage *page)
5798 {
5799   GtkWidget *widget = GTK_WIDGET (notebook);
5800   GtkNotebookPrivate *priv = notebook->priv;
5801   GtkAllocation child_allocation, label_allocation;
5802   GtkRequisition tab_requisition;
5803   GtkStyle *style;
5804   gint xthickness;
5805   gint ythickness;
5806   gint padding;
5807   gint focus_width;
5808   gint tab_curvature;
5809   gint tab_pos = get_effective_tab_pos (notebook);
5810   gboolean tab_allocation_changed;
5811   gboolean was_visible = page->tab_allocated_visible;
5812
5813   if (!page->tab_label ||
5814       !gtk_widget_get_visible (page->tab_label) ||
5815       !gtk_widget_get_child_visible (page->tab_label))
5816     {
5817       page->tab_allocated_visible = FALSE;
5818       return was_visible;
5819     }
5820
5821   style = gtk_widget_get_style (widget);
5822   xthickness = style->xthickness;
5823   ythickness = style->ythickness;
5824
5825   gtk_widget_get_preferred_size (page->tab_label, &tab_requisition, NULL);
5826   gtk_widget_style_get (widget,
5827                         "focus-line-width", &focus_width,
5828                         "tab-curvature", &tab_curvature,
5829                         NULL);
5830   switch (tab_pos)
5831     {
5832     case GTK_POS_TOP:
5833     case GTK_POS_BOTTOM:
5834       padding = tab_curvature + focus_width + priv->tab_hborder;
5835       if (page->fill)
5836         {
5837           child_allocation.x = xthickness + focus_width + priv->tab_hborder;
5838           child_allocation.width = MAX (1, page->allocation.width - 2 * child_allocation.x);
5839           child_allocation.x += page->allocation.x;
5840         }
5841       else
5842         {
5843           child_allocation.x = page->allocation.x +
5844             (page->allocation.width - tab_requisition.width) / 2;
5845
5846           child_allocation.width = tab_requisition.width;
5847         }
5848
5849       child_allocation.y = priv->tab_vborder + focus_width + page->allocation.y;
5850
5851       if (tab_pos == GTK_POS_TOP)
5852         child_allocation.y += ythickness;
5853
5854       child_allocation.height = MAX (1, (page->allocation.height - ythickness -
5855                                          2 * (priv->tab_vborder + focus_width)));
5856       break;
5857     case GTK_POS_LEFT:
5858     case GTK_POS_RIGHT:
5859       padding = tab_curvature + focus_width + priv->tab_vborder;
5860       if (page->fill)
5861         {
5862           child_allocation.y = ythickness + padding;
5863           child_allocation.height = MAX (1, (page->allocation.height -
5864                                              2 * child_allocation.y));
5865           child_allocation.y += page->allocation.y;
5866         }
5867       else
5868         {
5869           child_allocation.y = page->allocation.y +
5870             (page->allocation.height - tab_requisition.height) / 2;
5871
5872           child_allocation.height = tab_requisition.height;
5873         }
5874
5875       child_allocation.x = priv->tab_hborder + focus_width + page->allocation.x;
5876
5877       if (tab_pos == GTK_POS_LEFT)
5878         child_allocation.x += xthickness;
5879
5880       child_allocation.width = MAX (1, (page->allocation.width - xthickness -
5881                                         2 * (priv->tab_hborder + focus_width)));
5882       break;
5883     }
5884
5885   gtk_widget_get_allocation (page->tab_label, &label_allocation);
5886   tab_allocation_changed = (child_allocation.x != label_allocation.x ||
5887                             child_allocation.y != label_allocation.y ||
5888                             child_allocation.width != label_allocation.width ||
5889                             child_allocation.height != label_allocation.height);
5890
5891   gtk_widget_size_allocate (page->tab_label, &child_allocation);
5892
5893   if (!was_visible)
5894     {
5895       page->tab_allocated_visible = TRUE;
5896       tab_allocation_changed = TRUE;
5897     }
5898
5899   return tab_allocation_changed;
5900 }
5901
5902 static void 
5903 gtk_notebook_calc_tabs (GtkNotebook  *notebook,
5904                         GList        *start,
5905                         GList       **end,
5906                         gint         *tab_space,
5907                         guint         direction)
5908 {
5909   GtkNotebookPage *page = NULL;
5910   GList *children;
5911   GList *last_list = NULL;
5912   GList *last_calculated_child = NULL;
5913   gboolean pack;
5914   gint tab_pos = get_effective_tab_pos (notebook);
5915   guint real_direction;
5916
5917   if (!start)
5918     return;
5919
5920   children = start;
5921   pack = GTK_NOTEBOOK_PAGE (start)->pack;
5922   if (pack == GTK_PACK_END)
5923     real_direction = (direction == STEP_PREV) ? STEP_NEXT : STEP_PREV;
5924   else
5925     real_direction = direction;
5926
5927   while (1)
5928     {
5929       switch (tab_pos)
5930         {
5931         case GTK_POS_TOP:
5932         case GTK_POS_BOTTOM:
5933           while (children)
5934             {
5935               page = children->data;
5936               if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) &&
5937                   gtk_widget_get_visible (page->child))
5938                 {
5939                   if (page->pack == pack)
5940                     {
5941                       *tab_space -= page->requisition.width;
5942                       if (*tab_space < 0 || children == *end)
5943                         {
5944                           if (*tab_space < 0) 
5945                             {
5946                               *tab_space = - (*tab_space +
5947                                               page->requisition.width);
5948
5949                               if (*tab_space == 0 && direction == STEP_PREV)
5950                                 children = last_calculated_child;
5951
5952                               *end = children;
5953                             }
5954                           return;
5955                         }
5956
5957                       last_calculated_child = children;
5958                     }
5959                   last_list = children;
5960                 }
5961               if (real_direction == STEP_NEXT)
5962                 children = children->next;
5963               else
5964                 children = children->prev;
5965             }
5966           break;
5967         case GTK_POS_LEFT:
5968         case GTK_POS_RIGHT:
5969           while (children)
5970             {
5971               page = children->data;
5972               if (NOTEBOOK_IS_TAB_LABEL_PARENT (notebook, page) &&
5973                   gtk_widget_get_visible (page->child))
5974                 {
5975                   if (page->pack == pack)
5976                     {
5977                       *tab_space -= page->requisition.height;
5978                       if (*tab_space < 0 || children == *end)
5979                         {
5980                           if (*tab_space < 0)
5981                             {
5982                               *tab_space = - (*tab_space +
5983                                               page->requisition.height);
5984
5985                               if (*tab_space == 0 && direction == STEP_PREV)
5986                                 children = last_calculated_child;
5987
5988                               *end = children;
5989                             }
5990                           return;
5991                         }
5992
5993                       last_calculated_child = children;
5994                     }
5995                   last_list = children;
5996                 }
5997               if (real_direction == STEP_NEXT)
5998                 children = children->next;
5999               else
6000                 children = children->prev;
6001             }
6002           break;
6003         }
6004       if (real_direction == STEP_PREV)
6005         return;
6006       pack = (pack == GTK_PACK_END) ? GTK_PACK_START : GTK_PACK_END;
6007       real_direction = STEP_PREV;
6008       children = last_list;
6009     }
6010 }
6011
6012 static void
6013 gtk_notebook_update_tab_states (GtkNotebook *notebook)
6014 {
6015   GtkNotebookPrivate *priv = notebook->priv;
6016   GList *list;
6017
6018   for (list = priv->children; list != NULL; list = list->next)
6019     {
6020       GtkNotebookPage *page = list->data;
6021       
6022       if (page->tab_label)
6023         {
6024           if (page == priv->cur_page)
6025             gtk_widget_set_state (page->tab_label, GTK_STATE_NORMAL);
6026           else
6027             gtk_widget_set_state (page->tab_label, GTK_STATE_ACTIVE);
6028         }
6029     }
6030 }
6031
6032 /* Private GtkNotebook Page Switch Methods:
6033  *
6034  * gtk_notebook_real_switch_page
6035  */
6036 static void
6037 gtk_notebook_real_switch_page (GtkNotebook     *notebook,
6038                                GtkWidget*       child,
6039                                guint            page_num)
6040 {
6041   GtkNotebookPrivate *priv = notebook->priv;
6042   GList *list = gtk_notebook_find_child (notebook, GTK_WIDGET (child), NULL);
6043   GtkNotebookPage *page = GTK_NOTEBOOK_PAGE (list);
6044   gboolean child_has_focus;
6045
6046   if (priv->cur_page == page || !gtk_widget_get_visible (GTK_WIDGET (child)))
6047     return;
6048
6049   /* save the value here, changing visibility changes focus */
6050   child_has_focus = priv->child_has_focus;
6051
6052   if (priv->cur_page)
6053     gtk_widget_set_child_visible (priv->cur_page->child, FALSE);
6054
6055   priv->cur_page = page;
6056
6057   if (!priv->focus_tab ||
6058       priv->focus_tab->data != (gpointer) priv->cur_page)
6059     priv->focus_tab =
6060       g_list_find (priv->children, priv->cur_page);
6061
6062   gtk_widget_set_child_visible (priv->cur_page->child, TRUE);
6063
6064   /* If the focus was on the previous page, move it to the first
6065    * element on the new page, if possible, or if not, to the
6066    * notebook itself.
6067    */
6068   if (child_has_focus)
6069     {
6070       if (priv->cur_page->last_focus_child &&
6071           gtk_widget_is_ancestor (priv->cur_page->last_focus_child, priv->cur_page->child))
6072         gtk_widget_grab_focus (priv->cur_page->last_focus_child);
6073       else
6074         if (!gtk_widget_child_focus (priv->cur_page->child, GTK_DIR_TAB_FORWARD))
6075           gtk_widget_grab_focus (GTK_WIDGET (notebook));
6076     }
6077   
6078   gtk_notebook_update_tab_states (notebook);
6079   gtk_widget_queue_resize (GTK_WIDGET (notebook));
6080   g_object_notify (G_OBJECT (notebook), "page");
6081 }
6082
6083 /* Private GtkNotebook Page Switch Functions:
6084  *
6085  * gtk_notebook_switch_page
6086  * gtk_notebook_page_select
6087  * gtk_notebook_switch_focus_tab
6088  * gtk_notebook_menu_switch_page
6089  */
6090 static void
6091 gtk_notebook_switch_page (GtkNotebook     *notebook,
6092                           GtkNotebookPage *page)
6093 {
6094   GtkNotebookPrivate *priv = notebook->priv;
6095   guint page_num;
6096
6097   if (priv->cur_page == page)
6098     return;
6099
6100   page_num = g_list_index (priv->children, page);
6101
6102   g_signal_emit (notebook,
6103                  notebook_signals[SWITCH_PAGE],
6104                  0,
6105                  page->child,
6106                  page_num);
6107 }
6108
6109 static gint
6110 gtk_notebook_page_select (GtkNotebook *notebook,
6111                           gboolean     move_focus)
6112 {
6113   GtkNotebookPrivate *priv = notebook->priv;
6114   GtkNotebookPage *page;
6115   GtkDirectionType dir = GTK_DIR_DOWN; /* Quiet GCC */
6116   gint tab_pos = get_effective_tab_pos (notebook);
6117
6118   if (!priv->focus_tab)
6119     return FALSE;
6120
6121   page = priv->focus_tab->data;
6122   gtk_notebook_switch_page (notebook, page);
6123
6124   if (move_focus)
6125     {
6126       switch (tab_pos)
6127         {
6128         case GTK_POS_TOP:
6129           dir = GTK_DIR_DOWN;
6130           break;
6131         case GTK_POS_BOTTOM:
6132           dir = GTK_DIR_UP;
6133           break;
6134         case GTK_POS_LEFT:
6135           dir = GTK_DIR_RIGHT;
6136           break;
6137         case GTK_POS_RIGHT:
6138           dir = GTK_DIR_LEFT;
6139           break;
6140         }
6141
6142       if (gtk_widget_child_focus (page->child, dir))
6143         return TRUE;
6144     }
6145   return FALSE;
6146 }
6147
6148 static void
6149 gtk_notebook_switch_focus_tab (GtkNotebook *notebook, 
6150                                GList       *new_child)
6151 {
6152   GtkNotebookPrivate *priv = notebook->priv;
6153   GList *old_child;
6154   GtkNotebookPage *page;
6155
6156   if (priv->focus_tab == new_child)
6157     return;
6158
6159   old_child = priv->focus_tab;
6160   priv->focus_tab = new_child;
6161
6162   if (priv->scrollable)
6163     gtk_notebook_redraw_arrows (notebook);
6164
6165   if (!priv->show_tabs || !priv->focus_tab)
6166     return;
6167
6168   page = priv->focus_tab->data;
6169   if (gtk_widget_get_mapped (page->tab_label))
6170     gtk_notebook_redraw_tabs (notebook);
6171   else
6172     gtk_notebook_pages_allocate (notebook);
6173
6174   gtk_notebook_switch_page (notebook, page);
6175 }
6176
6177 static void
6178 gtk_notebook_menu_switch_page (GtkWidget       *widget,
6179                                GtkNotebookPage *page)
6180 {
6181   GtkNotebookPrivate *priv;
6182   GtkNotebook *notebook;
6183   GtkWidget *parent;
6184   GList *children;
6185   guint page_num;
6186
6187   parent = gtk_widget_get_parent (widget);
6188   notebook = GTK_NOTEBOOK (gtk_menu_get_attach_widget (GTK_MENU (parent)));
6189   priv = notebook->priv;
6190
6191   if (priv->cur_page == page)
6192     return;
6193
6194   page_num = 0;
6195   children = priv->children;
6196   while (children && children->data != page)
6197     {
6198       children = children->next;
6199       page_num++;
6200     }
6201
6202   g_signal_emit (notebook,
6203                  notebook_signals[SWITCH_PAGE],
6204                  0,
6205                  page->child,
6206                  page_num);
6207 }
6208
6209 /* Private GtkNotebook Menu Functions:
6210  *
6211  * gtk_notebook_menu_item_create
6212  * gtk_notebook_menu_label_unparent
6213  * gtk_notebook_menu_detacher
6214  */
6215 static void
6216 gtk_notebook_menu_item_create (GtkNotebook *notebook, 
6217                                GList       *list)
6218 {
6219   GtkNotebookPrivate *priv = notebook->priv;
6220   GtkNotebookPage *page;
6221   GtkWidget *menu_item;
6222
6223   page = list->data;
6224   if (page->default_menu)
6225     {
6226       if (GTK_IS_LABEL (page->tab_label))
6227         page->menu_label = gtk_label_new (gtk_label_get_label (GTK_LABEL (page->tab_label)));
6228       else
6229         page->menu_label = gtk_label_new ("");
6230       gtk_misc_set_alignment (GTK_MISC (page->menu_label), 0.0, 0.5);
6231     }
6232
6233   gtk_widget_show (page->menu_label);
6234   menu_item = gtk_menu_item_new ();
6235   gtk_container_add (GTK_CONTAINER (menu_item), page->menu_label);
6236   gtk_menu_shell_insert (GTK_MENU_SHELL (priv->menu), menu_item,
6237                          gtk_notebook_real_page_position (notebook, list));
6238   g_signal_connect (menu_item, "activate",
6239                     G_CALLBACK (gtk_notebook_menu_switch_page), page);
6240   if (gtk_widget_get_visible (page->child))
6241     gtk_widget_show (menu_item);
6242 }
6243
6244 static void
6245 gtk_notebook_menu_label_unparent (GtkWidget *widget, 
6246                                   gpointer  data)
6247 {
6248   gtk_widget_unparent (gtk_bin_get_child (GTK_BIN (widget)));
6249   _gtk_bin_set_child (GTK_BIN (widget), NULL);
6250 }
6251
6252 static void
6253 gtk_notebook_menu_detacher (GtkWidget *widget,
6254                             GtkMenu   *menu)
6255 {
6256   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
6257   GtkNotebookPrivate *priv = notebook->priv;
6258
6259   g_return_if_fail (priv->menu == (GtkWidget*) menu);
6260
6261   priv->menu = NULL;
6262 }
6263
6264 /* Public GtkNotebook Page Insert/Remove Methods :
6265  *
6266  * gtk_notebook_append_page
6267  * gtk_notebook_append_page_menu
6268  * gtk_notebook_prepend_page
6269  * gtk_notebook_prepend_page_menu
6270  * gtk_notebook_insert_page
6271  * gtk_notebook_insert_page_menu
6272  * gtk_notebook_remove_page
6273  */
6274 /**
6275  * gtk_notebook_append_page:
6276  * @notebook: a #GtkNotebook
6277  * @child: the #GtkWidget to use as the contents of the page.
6278  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6279  *             or %NULL to use the default label, 'page N'.
6280  *
6281  * Appends a page to @notebook.
6282  *
6283  * Return value: the index (starting from 0) of the appended
6284  * page in the notebook, or -1 if function fails
6285  **/
6286 gint
6287 gtk_notebook_append_page (GtkNotebook *notebook,
6288                           GtkWidget   *child,
6289                           GtkWidget   *tab_label)
6290 {
6291   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6292   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6293   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6294   
6295   return gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, -1);
6296 }
6297
6298 /**
6299  * gtk_notebook_append_page_menu:
6300  * @notebook: a #GtkNotebook
6301  * @child: the #GtkWidget to use as the contents of the page.
6302  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6303  *             or %NULL to use the default label, 'page N'.
6304  * @menu_label: (allow-none): the widget to use as a label for the page-switch
6305  *              menu, if that is enabled. If %NULL, and @tab_label
6306  *              is a #GtkLabel or %NULL, then the menu label will be
6307  *              a newly created label with the same text as @tab_label;
6308  *              If @tab_label is not a #GtkLabel, @menu_label must be
6309  *              specified if the page-switch menu is to be used.
6310  * 
6311  * Appends a page to @notebook, specifying the widget to use as the
6312  * label in the popup menu.
6313  *
6314  * Return value: the index (starting from 0) of the appended
6315  * page in the notebook, or -1 if function fails
6316  **/
6317 gint
6318 gtk_notebook_append_page_menu (GtkNotebook *notebook,
6319                                GtkWidget   *child,
6320                                GtkWidget   *tab_label,
6321                                GtkWidget   *menu_label)
6322 {
6323   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6324   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6325   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6326   g_return_val_if_fail (menu_label == NULL || GTK_IS_WIDGET (menu_label), -1);
6327   
6328   return gtk_notebook_insert_page_menu (notebook, child, tab_label, menu_label, -1);
6329 }
6330
6331 /**
6332  * gtk_notebook_prepend_page:
6333  * @notebook: a #GtkNotebook
6334  * @child: the #GtkWidget to use as the contents of the page.
6335  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6336  *             or %NULL to use the default label, 'page N'.
6337  *
6338  * Prepends a page to @notebook.
6339  *
6340  * Return value: the index (starting from 0) of the prepended
6341  * page in the notebook, or -1 if function fails
6342  **/
6343 gint
6344 gtk_notebook_prepend_page (GtkNotebook *notebook,
6345                            GtkWidget   *child,
6346                            GtkWidget   *tab_label)
6347 {
6348   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6349   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6350   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6351   
6352   return gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, 0);
6353 }
6354
6355 /**
6356  * gtk_notebook_prepend_page_menu:
6357  * @notebook: a #GtkNotebook
6358  * @child: the #GtkWidget to use as the contents of the page.
6359  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6360  *             or %NULL to use the default label, 'page N'.
6361  * @menu_label: (allow-none): the widget to use as a label for the page-switch
6362  *              menu, if that is enabled. If %NULL, and @tab_label
6363  *              is a #GtkLabel or %NULL, then the menu label will be
6364  *              a newly created label with the same text as @tab_label;
6365  *              If @tab_label is not a #GtkLabel, @menu_label must be
6366  *              specified if the page-switch menu is to be used.
6367  * 
6368  * Prepends a page to @notebook, specifying the widget to use as the
6369  * label in the popup menu.
6370  *
6371  * Return value: the index (starting from 0) of the prepended
6372  * page in the notebook, or -1 if function fails
6373  **/
6374 gint
6375 gtk_notebook_prepend_page_menu (GtkNotebook *notebook,
6376                                 GtkWidget   *child,
6377                                 GtkWidget   *tab_label,
6378                                 GtkWidget   *menu_label)
6379 {
6380   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6381   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6382   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6383   g_return_val_if_fail (menu_label == NULL || GTK_IS_WIDGET (menu_label), -1);
6384   
6385   return gtk_notebook_insert_page_menu (notebook, child, tab_label, menu_label, 0);
6386 }
6387
6388 /**
6389  * gtk_notebook_insert_page:
6390  * @notebook: a #GtkNotebook
6391  * @child: the #GtkWidget to use as the contents of the page.
6392  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6393  *             or %NULL to use the default label, 'page N'.
6394  * @position: the index (starting at 0) at which to insert the page,
6395  *            or -1 to append the page after all other pages.
6396  *
6397  * Insert a page into @notebook at the given position.
6398  *
6399  * Return value: the index (starting from 0) of the inserted
6400  * page in the notebook, or -1 if function fails
6401  **/
6402 gint
6403 gtk_notebook_insert_page (GtkNotebook *notebook,
6404                           GtkWidget   *child,
6405                           GtkWidget   *tab_label,
6406                           gint         position)
6407 {
6408   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6409   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6410   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6411   
6412   return gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, position);
6413 }
6414
6415
6416 static gint
6417 gtk_notebook_page_compare_tab (gconstpointer a,
6418                                gconstpointer b)
6419 {
6420   return (((GtkNotebookPage *) a)->tab_label != b);
6421 }
6422
6423 static gboolean
6424 gtk_notebook_mnemonic_activate_switch_page (GtkWidget *child,
6425                                             gboolean overload,
6426                                             gpointer data)
6427 {
6428   GtkNotebook *notebook = GTK_NOTEBOOK (data);
6429   GtkNotebookPrivate *priv = notebook->priv;
6430   GList *list;
6431
6432   list = g_list_find_custom (priv->children, child,
6433                              gtk_notebook_page_compare_tab);
6434   if (list)
6435     {
6436       GtkNotebookPage *page = list->data;
6437
6438       gtk_widget_grab_focus (GTK_WIDGET (notebook));    /* Do this first to avoid focusing new page */
6439       gtk_notebook_switch_page (notebook, page);
6440       focus_tabs_in (notebook);
6441     }
6442
6443   return TRUE;
6444 }
6445
6446 /**
6447  * gtk_notebook_insert_page_menu:
6448  * @notebook: a #GtkNotebook
6449  * @child: the #GtkWidget to use as the contents of the page.
6450  * @tab_label: (allow-none): the #GtkWidget to be used as the label for the page,
6451  *             or %NULL to use the default label, 'page N'.
6452  * @menu_label: (allow-none): the widget to use as a label for the page-switch
6453  *              menu, if that is enabled. If %NULL, and @tab_label
6454  *              is a #GtkLabel or %NULL, then the menu label will be
6455  *              a newly created label with the same text as @tab_label;
6456  *              If @tab_label is not a #GtkLabel, @menu_label must be
6457  *              specified if the page-switch menu is to be used.
6458  * @position: the index (starting at 0) at which to insert the page,
6459  *            or -1 to append the page after all other pages.
6460  * 
6461  * Insert a page into @notebook at the given position, specifying
6462  * the widget to use as the label in the popup menu.
6463  *
6464  * Return value: the index (starting from 0) of the inserted
6465  * page in the notebook
6466  **/
6467 gint
6468 gtk_notebook_insert_page_menu (GtkNotebook *notebook,
6469                                GtkWidget   *child,
6470                                GtkWidget   *tab_label,
6471                                GtkWidget   *menu_label,
6472                                gint         position)
6473 {
6474   GtkNotebookClass *class;
6475
6476   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6477   g_return_val_if_fail (GTK_IS_WIDGET (child), -1);
6478   g_return_val_if_fail (tab_label == NULL || GTK_IS_WIDGET (tab_label), -1);
6479   g_return_val_if_fail (menu_label == NULL || GTK_IS_WIDGET (menu_label), -1);
6480
6481   class = GTK_NOTEBOOK_GET_CLASS (notebook);
6482
6483   return (class->insert_page) (notebook, child, tab_label, menu_label, position);
6484 }
6485
6486 /**
6487  * gtk_notebook_remove_page:
6488  * @notebook: a #GtkNotebook.
6489  * @page_num: the index of a notebook page, starting
6490  *            from 0. If -1, the last page will
6491  *            be removed.
6492  * 
6493  * Removes a page from the notebook given its index
6494  * in the notebook.
6495  **/
6496 void
6497 gtk_notebook_remove_page (GtkNotebook *notebook,
6498                           gint         page_num)
6499 {
6500   GtkNotebookPrivate *priv;
6501   GList *list = NULL;
6502
6503   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6504
6505   priv = notebook->priv;
6506
6507   if (page_num >= 0)
6508     list = g_list_nth (priv->children, page_num);
6509   else
6510     list = g_list_last (priv->children);
6511
6512   if (list)
6513     gtk_container_remove (GTK_CONTAINER (notebook),
6514                           ((GtkNotebookPage *) list->data)->child);
6515 }
6516
6517 /* Public GtkNotebook Page Switch Methods :
6518  * gtk_notebook_get_current_page
6519  * gtk_notebook_page_num
6520  * gtk_notebook_set_current_page
6521  * gtk_notebook_next_page
6522  * gtk_notebook_prev_page
6523  */
6524 /**
6525  * gtk_notebook_get_current_page:
6526  * @notebook: a #GtkNotebook
6527  * 
6528  * Returns the page number of the current page.
6529  * 
6530  * Return value: the index (starting from 0) of the current
6531  * page in the notebook. If the notebook has no pages, then
6532  * -1 will be returned.
6533  **/
6534 gint
6535 gtk_notebook_get_current_page (GtkNotebook *notebook)
6536 {
6537   GtkNotebookPrivate *priv;
6538
6539   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6540
6541   priv = notebook->priv;
6542
6543   if (!priv->cur_page)
6544     return -1;
6545
6546   return g_list_index (priv->children, priv->cur_page);
6547 }
6548
6549 /**
6550  * gtk_notebook_get_nth_page:
6551  * @notebook: a #GtkNotebook
6552  * @page_num: the index of a page in the notebook, or -1
6553  *            to get the last page.
6554  * 
6555  * Returns the child widget contained in page number @page_num.
6556  *
6557  * Return value: (transfer none): the child widget, or %NULL if @page_num is
6558  * out of bounds.
6559  **/
6560 GtkWidget*
6561 gtk_notebook_get_nth_page (GtkNotebook *notebook,
6562                            gint         page_num)
6563 {
6564   GtkNotebookPrivate *priv;
6565   GtkNotebookPage *page;
6566   GList *list;
6567
6568   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
6569
6570   priv = notebook->priv;
6571
6572   if (page_num >= 0)
6573     list = g_list_nth (priv->children, page_num);
6574   else
6575     list = g_list_last (priv->children);
6576
6577   if (list)
6578     {
6579       page = list->data;
6580       return page->child;
6581     }
6582
6583   return NULL;
6584 }
6585
6586 /**
6587  * gtk_notebook_get_n_pages:
6588  * @notebook: a #GtkNotebook
6589  * 
6590  * Gets the number of pages in a notebook.
6591  * 
6592  * Return value: the number of pages in the notebook.
6593  *
6594  * Since: 2.2
6595  **/
6596 gint
6597 gtk_notebook_get_n_pages (GtkNotebook *notebook)
6598 {
6599   GtkNotebookPrivate *priv;
6600
6601   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), 0);
6602
6603   priv = notebook->priv;
6604
6605   return g_list_length (priv->children);
6606 }
6607
6608 /**
6609  * gtk_notebook_page_num:
6610  * @notebook: a #GtkNotebook
6611  * @child: a #GtkWidget
6612  * 
6613  * Finds the index of the page which contains the given child
6614  * widget.
6615  * 
6616  * Return value: the index of the page containing @child, or
6617  *   -1 if @child is not in the notebook.
6618  **/
6619 gint
6620 gtk_notebook_page_num (GtkNotebook      *notebook,
6621                        GtkWidget        *child)
6622 {
6623   GtkNotebookPrivate *priv;
6624   GList *children;
6625   gint num;
6626
6627   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
6628
6629   priv = notebook->priv;
6630
6631   num = 0;
6632   children = priv->children;
6633   while (children)
6634     {
6635       GtkNotebookPage *page =  children->data;
6636       
6637       if (page->child == child)
6638         return num;
6639
6640       children = children->next;
6641       num++;
6642     }
6643
6644   return -1;
6645 }
6646
6647 /**
6648  * gtk_notebook_set_current_page:
6649  * @notebook: a #GtkNotebook
6650  * @page_num: index of the page to switch to, starting from 0.
6651  *            If negative, the last page will be used. If greater
6652  *            than the number of pages in the notebook, nothing
6653  *            will be done.
6654  *                
6655  * Switches to the page number @page_num. 
6656  *
6657  * Note that due to historical reasons, GtkNotebook refuses
6658  * to switch to a page unless the child widget is visible. 
6659  * Therefore, it is recommended to show child widgets before
6660  * adding them to a notebook. 
6661  */
6662 void
6663 gtk_notebook_set_current_page (GtkNotebook *notebook,
6664                                gint         page_num)
6665 {
6666   GtkNotebookPrivate *priv;
6667   GList *list;
6668
6669   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6670
6671   priv = notebook->priv;
6672
6673   if (page_num < 0)
6674     page_num = g_list_length (priv->children) - 1;
6675
6676   list = g_list_nth (priv->children, page_num);
6677   if (list)
6678     gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (list));
6679 }
6680
6681 /**
6682  * gtk_notebook_next_page:
6683  * @notebook: a #GtkNotebook
6684  * 
6685  * Switches to the next page. Nothing happens if the current page is
6686  * the last page.
6687  **/
6688 void
6689 gtk_notebook_next_page (GtkNotebook *notebook)
6690 {
6691   GtkNotebookPrivate *priv;
6692   GList *list;
6693
6694   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6695
6696   priv = notebook->priv;
6697
6698   list = g_list_find (priv->children, priv->cur_page);
6699   if (!list)
6700     return;
6701
6702   list = gtk_notebook_search_page (notebook, list, STEP_NEXT, TRUE);
6703   if (!list)
6704     return;
6705
6706   gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (list));
6707 }
6708
6709 /**
6710  * gtk_notebook_prev_page:
6711  * @notebook: a #GtkNotebook
6712  * 
6713  * Switches to the previous page. Nothing happens if the current page
6714  * is the first page.
6715  **/
6716 void
6717 gtk_notebook_prev_page (GtkNotebook *notebook)
6718 {
6719   GtkNotebookPrivate *priv;
6720   GList *list;
6721
6722   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6723
6724   priv = notebook->priv;
6725
6726   list = g_list_find (priv->children, priv->cur_page);
6727   if (!list)
6728     return;
6729
6730   list = gtk_notebook_search_page (notebook, list, STEP_PREV, TRUE);
6731   if (!list)
6732     return;
6733
6734   gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (list));
6735 }
6736
6737 /* Public GtkNotebook/Tab Style Functions
6738  *
6739  * gtk_notebook_set_show_border
6740  * gtk_notebook_get_show_border
6741  * gtk_notebook_set_show_tabs
6742  * gtk_notebook_get_show_tabs
6743  * gtk_notebook_set_tab_pos
6744  * gtk_notebook_get_tab_pos
6745  * gtk_notebook_set_scrollable
6746  * gtk_notebook_get_scrollable
6747  * gtk_notebook_get_tab_hborder
6748  * gtk_notebook_get_tab_vborder
6749  */
6750 /**
6751  * gtk_notebook_set_show_border:
6752  * @notebook: a #GtkNotebook
6753  * @show_border: %TRUE if a bevel should be drawn around the notebook.
6754  * 
6755  * Sets whether a bevel will be drawn around the notebook pages.
6756  * This only has a visual effect when the tabs are not shown.
6757  * See gtk_notebook_set_show_tabs().
6758  **/
6759 void
6760 gtk_notebook_set_show_border (GtkNotebook *notebook,
6761                               gboolean     show_border)
6762 {
6763   GtkNotebookPrivate *priv;
6764
6765   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6766
6767   priv = notebook->priv;
6768
6769   if (priv->show_border != show_border)
6770     {
6771       priv->show_border = show_border;
6772
6773       if (gtk_widget_get_visible (GTK_WIDGET (notebook)))
6774         gtk_widget_queue_resize (GTK_WIDGET (notebook));
6775       
6776       g_object_notify (G_OBJECT (notebook), "show-border");
6777     }
6778 }
6779
6780 /**
6781  * gtk_notebook_get_show_border:
6782  * @notebook: a #GtkNotebook
6783  *
6784  * Returns whether a bevel will be drawn around the notebook pages. See
6785  * gtk_notebook_set_show_border().
6786  *
6787  * Return value: %TRUE if the bevel is drawn
6788  **/
6789 gboolean
6790 gtk_notebook_get_show_border (GtkNotebook *notebook)
6791 {
6792   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
6793
6794   return notebook->priv->show_border;
6795 }
6796
6797 /**
6798  * gtk_notebook_set_show_tabs:
6799  * @notebook: a #GtkNotebook
6800  * @show_tabs: %TRUE if the tabs should be shown.
6801  * 
6802  * Sets whether to show the tabs for the notebook or not.
6803  **/
6804 void
6805 gtk_notebook_set_show_tabs (GtkNotebook *notebook,
6806                             gboolean     show_tabs)
6807 {
6808   GtkNotebookPrivate *priv;
6809   GtkNotebookPage *page;
6810   GList *children;
6811   gint i;
6812
6813   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6814
6815   priv = notebook->priv;
6816
6817   show_tabs = show_tabs != FALSE;
6818
6819   if (priv->show_tabs == show_tabs)
6820     return;
6821
6822   priv->show_tabs = show_tabs;
6823   children = priv->children;
6824
6825   if (!show_tabs)
6826     {
6827       gtk_widget_set_can_focus (GTK_WIDGET (notebook), FALSE);
6828
6829       while (children)
6830         {
6831           page = children->data;
6832           children = children->next;
6833           if (page->default_tab)
6834             {
6835               gtk_widget_destroy (page->tab_label);
6836               page->tab_label = NULL;
6837             }
6838           else
6839             gtk_widget_hide (page->tab_label);
6840         }
6841     }
6842   else
6843     {
6844       gtk_widget_set_can_focus (GTK_WIDGET (notebook), TRUE);
6845       gtk_notebook_update_labels (notebook);
6846     }
6847
6848   for (i = 0; i < N_ACTION_WIDGETS; i++)
6849     {
6850       if (priv->action_widget[i])
6851         gtk_widget_set_child_visible (priv->action_widget[i], show_tabs);
6852     }
6853
6854   gtk_widget_queue_resize (GTK_WIDGET (notebook));
6855
6856   g_object_notify (G_OBJECT (notebook), "show-tabs");
6857 }
6858
6859 /**
6860  * gtk_notebook_get_show_tabs:
6861  * @notebook: a #GtkNotebook
6862  *
6863  * Returns whether the tabs of the notebook are shown. See
6864  * gtk_notebook_set_show_tabs().
6865  *
6866  * Return value: %TRUE if the tabs are shown
6867  **/
6868 gboolean
6869 gtk_notebook_get_show_tabs (GtkNotebook *notebook)
6870 {
6871   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
6872
6873   return notebook->priv->show_tabs;
6874 }
6875
6876 /**
6877  * gtk_notebook_set_tab_pos:
6878  * @notebook: a #GtkNotebook.
6879  * @pos: the edge to draw the tabs at.
6880  * 
6881  * Sets the edge at which the tabs for switching pages in the
6882  * notebook are drawn.
6883  **/
6884 void
6885 gtk_notebook_set_tab_pos (GtkNotebook     *notebook,
6886                           GtkPositionType  pos)
6887 {
6888   GtkNotebookPrivate *priv;
6889
6890   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6891
6892   priv = notebook->priv;
6893
6894   if (priv->tab_pos != pos)
6895     {
6896       priv->tab_pos = pos;
6897       if (gtk_widget_get_visible (GTK_WIDGET (notebook)))
6898         gtk_widget_queue_resize (GTK_WIDGET (notebook));
6899     }
6900
6901   g_object_notify (G_OBJECT (notebook), "tab-pos");
6902 }
6903
6904 /**
6905  * gtk_notebook_get_tab_pos:
6906  * @notebook: a #GtkNotebook
6907  *
6908  * Gets the edge at which the tabs for switching pages in the
6909  * notebook are drawn.
6910  *
6911  * Return value: the edge at which the tabs are drawn
6912  **/
6913 GtkPositionType
6914 gtk_notebook_get_tab_pos (GtkNotebook *notebook)
6915 {
6916   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), GTK_POS_TOP);
6917
6918   return notebook->priv->tab_pos;
6919 }
6920
6921 /**
6922  * gtk_notebook_set_scrollable:
6923  * @notebook: a #GtkNotebook
6924  * @scrollable: %TRUE if scroll arrows should be added
6925  * 
6926  * Sets whether the tab label area will have arrows for scrolling if
6927  * there are too many tabs to fit in the area.
6928  **/
6929 void
6930 gtk_notebook_set_scrollable (GtkNotebook *notebook,
6931                              gboolean     scrollable)
6932 {
6933   GtkNotebookPrivate *priv;
6934
6935   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
6936
6937   priv = notebook->priv;
6938
6939   scrollable = (scrollable != FALSE);
6940
6941   if (scrollable != priv->scrollable)
6942     {
6943       priv->scrollable = scrollable;
6944
6945       if (gtk_widget_get_visible (GTK_WIDGET (notebook)))
6946         gtk_widget_queue_resize (GTK_WIDGET (notebook));
6947
6948       g_object_notify (G_OBJECT (notebook), "scrollable");
6949     }
6950 }
6951
6952 /**
6953  * gtk_notebook_get_scrollable:
6954  * @notebook: a #GtkNotebook
6955  *
6956  * Returns whether the tab label area has arrows for scrolling. See
6957  * gtk_notebook_set_scrollable().
6958  *
6959  * Return value: %TRUE if arrows for scrolling are present
6960  **/
6961 gboolean
6962 gtk_notebook_get_scrollable (GtkNotebook *notebook)
6963 {
6964   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
6965
6966   return notebook->priv->scrollable;
6967 }
6968
6969 /**
6970  * gtk_notebook_get_tab_hborder:
6971  * @notebook: a #GtkNotebook
6972  *
6973  * Returns the horizontal width of a tab border.
6974  *
6975  * Return value: horizontal width of a tab border
6976  *
6977  * Since: 2.22
6978  */
6979 guint16
6980 gtk_notebook_get_tab_hborder (GtkNotebook *notebook)
6981 {
6982   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
6983
6984   return notebook->priv->tab_hborder;
6985 }
6986
6987 /**
6988  * gtk_notebook_get_tab_vborder:
6989  * @notebook: a #GtkNotebook
6990  *
6991  * Returns the vertical width of a tab border.
6992  *
6993  * Return value: vertical width of a tab border
6994  *
6995  * Since: 2.22
6996  */
6997 guint16
6998 gtk_notebook_get_tab_vborder (GtkNotebook *notebook)
6999 {
7000   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
7001
7002   return notebook->priv->tab_vborder;
7003 }
7004
7005
7006 /* Public GtkNotebook Popup Menu Methods:
7007  *
7008  * gtk_notebook_popup_enable
7009  * gtk_notebook_popup_disable
7010  */
7011
7012
7013 /**
7014  * gtk_notebook_popup_enable:
7015  * @notebook: a #GtkNotebook
7016  * 
7017  * Enables the popup menu: if the user clicks with the right mouse button on
7018  * the tab labels, a menu with all the pages will be popped up.
7019  **/
7020 void
7021 gtk_notebook_popup_enable (GtkNotebook *notebook)
7022 {
7023   GtkNotebookPrivate *priv;
7024   GList *list;
7025
7026   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7027
7028   priv = notebook->priv;
7029
7030   if (priv->menu)
7031     return;
7032
7033   priv->menu = gtk_menu_new ();
7034   for (list = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, FALSE);
7035        list;
7036        list = gtk_notebook_search_page (notebook, list, STEP_NEXT, FALSE))
7037     gtk_notebook_menu_item_create (notebook, list);
7038
7039   gtk_notebook_update_labels (notebook);
7040   gtk_menu_attach_to_widget (GTK_MENU (priv->menu),
7041                              GTK_WIDGET (notebook),
7042                              gtk_notebook_menu_detacher);
7043
7044   g_object_notify (G_OBJECT (notebook), "enable-popup");
7045 }
7046
7047 /**
7048  * gtk_notebook_popup_disable:
7049  * @notebook: a #GtkNotebook
7050  * 
7051  * Disables the popup menu.
7052  **/
7053 void       
7054 gtk_notebook_popup_disable  (GtkNotebook *notebook)
7055 {
7056   GtkNotebookPrivate *priv;
7057
7058   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7059
7060   priv = notebook->priv;
7061
7062   if (!priv->menu)
7063     return;
7064
7065   gtk_container_foreach (GTK_CONTAINER (priv->menu),
7066                          (GtkCallback) gtk_notebook_menu_label_unparent, NULL);
7067   gtk_widget_destroy (priv->menu);
7068
7069   g_object_notify (G_OBJECT (notebook), "enable-popup");
7070 }
7071
7072 /* Public GtkNotebook Page Properties Functions:
7073  *
7074  * gtk_notebook_get_tab_label
7075  * gtk_notebook_set_tab_label
7076  * gtk_notebook_set_tab_label_text
7077  * gtk_notebook_get_menu_label
7078  * gtk_notebook_set_menu_label
7079  * gtk_notebook_set_menu_label_text
7080  * gtk_notebook_get_tab_reorderable
7081  * gtk_notebook_set_tab_reorderable
7082  * gtk_notebook_get_tab_detachable
7083  * gtk_notebook_set_tab_detachable
7084  */
7085
7086 /**
7087  * gtk_notebook_get_tab_label:
7088  * @notebook: a #GtkNotebook
7089  * @child: the page
7090  * 
7091  * Returns the tab label widget for the page @child. %NULL is returned
7092  * if @child is not in @notebook or if no tab label has specifically
7093  * been set for @child.
7094  *
7095  * Return value: (transfer none): the tab label
7096  **/
7097 GtkWidget *
7098 gtk_notebook_get_tab_label (GtkNotebook *notebook,
7099                             GtkWidget   *child)
7100 {
7101   GList *list;
7102
7103   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7104   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
7105
7106   list = CHECK_FIND_CHILD (notebook, child);
7107   if (!list)  
7108     return NULL;
7109
7110   if (GTK_NOTEBOOK_PAGE (list)->default_tab)
7111     return NULL;
7112
7113   return GTK_NOTEBOOK_PAGE (list)->tab_label;
7114 }  
7115
7116 /**
7117  * gtk_notebook_set_tab_label:
7118  * @notebook: a #GtkNotebook
7119  * @child: the page
7120  * @tab_label: (allow-none): the tab label widget to use, or %NULL for default tab
7121  *             label.
7122  *
7123  * Changes the tab label for @child. If %NULL is specified
7124  * for @tab_label, then the page will have the label 'page N'.
7125  **/
7126 void
7127 gtk_notebook_set_tab_label (GtkNotebook *notebook,
7128                             GtkWidget   *child,
7129                             GtkWidget   *tab_label)
7130 {
7131   GtkNotebookPrivate *priv;
7132   GtkNotebookPage *page;
7133   GList *list;
7134
7135   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7136   g_return_if_fail (GTK_IS_WIDGET (child));
7137
7138   priv = notebook->priv;
7139
7140   list = CHECK_FIND_CHILD (notebook, child);
7141   if (!list)  
7142     return;
7143
7144   /* a NULL pointer indicates a default_tab setting, otherwise
7145    * we need to set the associated label
7146    */
7147   page = list->data;
7148   
7149   if (page->tab_label == tab_label)
7150     return;
7151   
7152
7153   gtk_notebook_remove_tab_label (notebook, page);
7154   
7155   if (tab_label)
7156     {
7157       page->default_tab = FALSE;
7158       page->tab_label = tab_label;
7159       gtk_widget_set_parent (page->tab_label, GTK_WIDGET (notebook));
7160     }
7161   else
7162     {
7163       page->default_tab = TRUE;
7164       page->tab_label = NULL;
7165
7166       if (priv->show_tabs)
7167         {
7168           gchar string[32];
7169
7170           g_snprintf (string, sizeof(string), _("Page %u"), 
7171                       gtk_notebook_real_page_position (notebook, list));
7172           page->tab_label = gtk_label_new (string);
7173           gtk_widget_set_parent (page->tab_label, GTK_WIDGET (notebook));
7174         }
7175     }
7176
7177   if (page->tab_label)
7178     page->mnemonic_activate_signal =
7179       g_signal_connect (page->tab_label,
7180                         "mnemonic-activate",
7181                         G_CALLBACK (gtk_notebook_mnemonic_activate_switch_page),
7182                         notebook);
7183
7184   if (priv->show_tabs && gtk_widget_get_visible (child))
7185     {
7186       gtk_widget_show (page->tab_label);
7187       gtk_widget_queue_resize (GTK_WIDGET (notebook));
7188     }
7189
7190   gtk_notebook_update_tab_states (notebook);
7191   gtk_widget_child_notify (child, "tab-label");
7192 }
7193
7194 /**
7195  * gtk_notebook_set_tab_label_text:
7196  * @notebook: a #GtkNotebook
7197  * @child: the page
7198  * @tab_text: the label text
7199  * 
7200  * Creates a new label and sets it as the tab label for the page
7201  * containing @child.
7202  **/
7203 void
7204 gtk_notebook_set_tab_label_text (GtkNotebook *notebook,
7205                                  GtkWidget   *child,
7206                                  const gchar *tab_text)
7207 {
7208   GtkWidget *tab_label = NULL;
7209
7210   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7211
7212   if (tab_text)
7213     tab_label = gtk_label_new (tab_text);
7214   gtk_notebook_set_tab_label (notebook, child, tab_label);
7215   gtk_widget_child_notify (child, "tab-label");
7216 }
7217
7218 /**
7219  * gtk_notebook_get_tab_label_text:
7220  * @notebook: a #GtkNotebook
7221  * @child: a widget contained in a page of @notebook
7222  *
7223  * Retrieves the text of the tab label for the page containing
7224  *    @child.
7225  *
7226  * Return value: the text of the tab label, or %NULL if the
7227  *               tab label widget is not a #GtkLabel. The
7228  *               string is owned by the widget and must not
7229  *               be freed.
7230  **/
7231 G_CONST_RETURN gchar *
7232 gtk_notebook_get_tab_label_text (GtkNotebook *notebook,
7233                                  GtkWidget   *child)
7234 {
7235   GtkWidget *tab_label;
7236
7237   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7238   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
7239
7240   tab_label = gtk_notebook_get_tab_label (notebook, child);
7241
7242   if (GTK_IS_LABEL (tab_label))
7243     return gtk_label_get_text (GTK_LABEL (tab_label));
7244   else
7245     return NULL;
7246 }
7247
7248 /**
7249  * gtk_notebook_get_menu_label:
7250  * @notebook: a #GtkNotebook
7251  * @child: a widget contained in a page of @notebook
7252  *
7253  * Retrieves the menu label widget of the page containing @child.
7254  *
7255  * Return value: (transfer none): the menu label, or %NULL if the
7256  *     notebook page does not have a menu label other than the
7257  *     default (the tab label).
7258  **/
7259 GtkWidget*
7260 gtk_notebook_get_menu_label (GtkNotebook *notebook,
7261                              GtkWidget   *child)
7262 {
7263   GList *list;
7264
7265   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7266   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
7267
7268   list = CHECK_FIND_CHILD (notebook, child);
7269   if (!list)
7270     return NULL;
7271
7272   if (GTK_NOTEBOOK_PAGE (list)->default_menu)
7273     return NULL;
7274
7275   return GTK_NOTEBOOK_PAGE (list)->menu_label;
7276 }
7277
7278 /**
7279  * gtk_notebook_set_menu_label:
7280  * @notebook: a #GtkNotebook
7281  * @child: the child widget
7282  * @menu_label: (allow-none): the menu label, or NULL for default
7283  *
7284  * Changes the menu label for the page containing @child.
7285  **/
7286 void
7287 gtk_notebook_set_menu_label (GtkNotebook *notebook,
7288                              GtkWidget   *child,
7289                              GtkWidget   *menu_label)
7290 {
7291   GtkNotebookPrivate *priv;
7292   GtkNotebookPage *page;
7293   GList *list;
7294
7295   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7296   g_return_if_fail (GTK_IS_WIDGET (child));
7297
7298   priv = notebook->priv;
7299
7300   list = CHECK_FIND_CHILD (notebook, child);
7301   if (!list)  
7302     return;
7303
7304   page = list->data;
7305   if (page->menu_label)
7306     {
7307       if (priv->menu)
7308         gtk_container_remove (GTK_CONTAINER (priv->menu),
7309                               gtk_widget_get_parent (page->menu_label));
7310
7311       if (!page->default_menu)
7312         g_object_unref (page->menu_label);
7313     }
7314
7315   if (menu_label)
7316     {
7317       page->menu_label = menu_label;
7318       g_object_ref_sink (page->menu_label);
7319       page->default_menu = FALSE;
7320     }
7321   else
7322     page->default_menu = TRUE;
7323
7324   if (priv->menu)
7325     gtk_notebook_menu_item_create (notebook, list);
7326   gtk_widget_child_notify (child, "menu-label");
7327 }
7328
7329 /**
7330  * gtk_notebook_set_menu_label_text:
7331  * @notebook: a #GtkNotebook
7332  * @child: the child widget
7333  * @menu_text: the label text
7334  * 
7335  * Creates a new label and sets it as the menu label of @child.
7336  **/
7337 void
7338 gtk_notebook_set_menu_label_text (GtkNotebook *notebook,
7339                                   GtkWidget   *child,
7340                                   const gchar *menu_text)
7341 {
7342   GtkWidget *menu_label = NULL;
7343
7344   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7345
7346   if (menu_text)
7347     {
7348       menu_label = gtk_label_new (menu_text);
7349       gtk_misc_set_alignment (GTK_MISC (menu_label), 0.0, 0.5);
7350     }
7351   gtk_notebook_set_menu_label (notebook, child, menu_label);
7352   gtk_widget_child_notify (child, "menu-label");
7353 }
7354
7355 /**
7356  * gtk_notebook_get_menu_label_text:
7357  * @notebook: a #GtkNotebook
7358  * @child: the child widget of a page of the notebook.
7359  *
7360  * Retrieves the text of the menu label for the page containing
7361  *    @child.
7362  *
7363  * Return value: the text of the tab label, or %NULL if the
7364  *               widget does not have a menu label other than
7365  *               the default menu label, or the menu label widget
7366  *               is not a #GtkLabel. The string is owned by
7367  *               the widget and must not be freed.
7368  **/
7369 G_CONST_RETURN gchar *
7370 gtk_notebook_get_menu_label_text (GtkNotebook *notebook,
7371                                   GtkWidget *child)
7372 {
7373   GtkWidget *menu_label;
7374
7375   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7376   g_return_val_if_fail (GTK_IS_WIDGET (child), NULL);
7377  
7378   menu_label = gtk_notebook_get_menu_label (notebook, child);
7379
7380   if (GTK_IS_LABEL (menu_label))
7381     return gtk_label_get_text (GTK_LABEL (menu_label));
7382   else
7383     return NULL;
7384 }
7385   
7386 /* Helper function called when pages are reordered
7387  */
7388 static void
7389 gtk_notebook_child_reordered (GtkNotebook     *notebook,
7390                               GtkNotebookPage *page)
7391 {
7392   GtkNotebookPrivate *priv = notebook->priv;
7393
7394   if (priv->menu)
7395     {
7396       GtkWidget *menu_item;
7397
7398       menu_item = gtk_widget_get_parent (page->menu_label);
7399       gtk_container_remove (GTK_CONTAINER (menu_item), page->menu_label);
7400       gtk_container_remove (GTK_CONTAINER (priv->menu), menu_item);
7401       gtk_notebook_menu_item_create (notebook, g_list_find (priv->children, page));
7402     }
7403
7404   gtk_notebook_update_tab_states (notebook);
7405   gtk_notebook_update_labels (notebook);
7406 }
7407
7408 static void
7409 gtk_notebook_set_tab_label_packing (GtkNotebook *notebook,
7410                                     GtkWidget   *child,
7411                                     gboolean     expand,
7412                                     gboolean     fill,
7413                                     GtkPackType  pack_type)
7414 {
7415   GtkNotebookPrivate *priv;
7416   GtkNotebookPage *page;
7417   GList *list;
7418
7419   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7420   g_return_if_fail (GTK_IS_WIDGET (child));
7421
7422   priv = notebook->priv;
7423
7424   list = CHECK_FIND_CHILD (notebook, child);
7425   if (!list)  
7426     return;
7427
7428   page = list->data;
7429   expand = expand != FALSE;
7430   fill = fill != FALSE;
7431   if (page->pack == pack_type && page->expand == expand && page->fill == fill)
7432     return;
7433
7434   gtk_widget_freeze_child_notify (child);
7435   page->expand = expand;
7436   gtk_widget_child_notify (child, "tab-expand");
7437   page->fill = fill;
7438   gtk_widget_child_notify (child, "tab-fill");
7439   if (page->pack != pack_type)
7440     {
7441       page->pack = pack_type;
7442       gtk_notebook_child_reordered (notebook, page);
7443     }
7444   gtk_widget_child_notify (child, "tab-pack");
7445   gtk_widget_child_notify (child, "position");
7446   if (priv->show_tabs)
7447     gtk_notebook_pages_allocate (notebook);
7448   gtk_widget_thaw_child_notify (child);
7449 }  
7450
7451 static void
7452 gtk_notebook_query_tab_label_packing (GtkNotebook *notebook,
7453                                       GtkWidget   *child,
7454                                       gboolean    *expand,
7455                                       gboolean    *fill,
7456                                       GtkPackType *pack_type)
7457 {
7458   GList *list;
7459
7460   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7461   g_return_if_fail (GTK_IS_WIDGET (child));
7462
7463   list = CHECK_FIND_CHILD (notebook, child);
7464   if (!list)
7465     return;
7466
7467   if (expand)
7468     *expand = GTK_NOTEBOOK_PAGE (list)->expand;
7469   if (fill)
7470     *fill = GTK_NOTEBOOK_PAGE (list)->fill;
7471   if (pack_type)
7472     *pack_type = GTK_NOTEBOOK_PAGE (list)->pack;
7473 }
7474
7475 /**
7476  * gtk_notebook_reorder_child:
7477  * @notebook: a #GtkNotebook
7478  * @child: the child to move
7479  * @position: the new position, or -1 to move to the end
7480  * 
7481  * Reorders the page containing @child, so that it appears in position
7482  * @position. If @position is greater than or equal to the number of
7483  * children in the list or negative, @child will be moved to the end
7484  * of the list.
7485  **/
7486 void
7487 gtk_notebook_reorder_child (GtkNotebook *notebook,
7488                             GtkWidget   *child,
7489                             gint         position)
7490 {
7491   GtkNotebookPrivate *priv;
7492   GList *list, *new_list;
7493   GtkNotebookPage *page;
7494   gint old_pos;
7495   gint max_pos;
7496
7497   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7498   g_return_if_fail (GTK_IS_WIDGET (child));
7499
7500   priv = notebook->priv;
7501
7502   list = CHECK_FIND_CHILD (notebook, child);
7503   if (!list)
7504     return;
7505
7506   max_pos = g_list_length (priv->children) - 1;
7507   if (position < 0 || position > max_pos)
7508     position = max_pos;
7509
7510   old_pos = g_list_position (priv->children, list);
7511
7512   if (old_pos == position)
7513     return;
7514
7515   page = list->data;
7516   priv->children = g_list_delete_link (priv->children, list);
7517
7518   priv->children = g_list_insert (priv->children, page, position);
7519   new_list = g_list_nth (priv->children, position);
7520
7521   /* Fix up GList references in GtkNotebook structure */
7522   if (priv->first_tab == list)
7523     priv->first_tab = new_list;
7524   if (priv->focus_tab == list)
7525     priv->focus_tab = new_list;
7526
7527   gtk_widget_freeze_child_notify (child);
7528
7529   /* Move around the menu items if necessary */
7530   gtk_notebook_child_reordered (notebook, page);
7531   gtk_widget_child_notify (child, "tab-pack");
7532   gtk_widget_child_notify (child, "position");
7533
7534   if (priv->show_tabs)
7535     gtk_notebook_pages_allocate (notebook);
7536
7537   gtk_widget_thaw_child_notify (child);
7538
7539   g_signal_emit (notebook,
7540                  notebook_signals[PAGE_REORDERED],
7541                  0,
7542                  child,
7543                  position);
7544 }
7545
7546 /**
7547  * gtk_notebook_set_group_name:
7548  * @notebook: a #GtkNotebook
7549  * @name: (allow-none): the name of the notebook group, or %NULL to unset it
7550  *
7551  * Sets a group name for @notebook.
7552  *
7553  * Notebooks with the same name will be able to exchange tabs
7554  * via drag and drop. A notebook with a %NULL group name will
7555  * not be able to exchange tabs with any other notebook.
7556  *
7557  * Since: 2.24
7558  */
7559 void
7560 gtk_notebook_set_group_name (GtkNotebook *notebook,
7561                              const gchar *group_name)
7562 {
7563   GtkNotebookPrivate *priv;
7564   GQuark group;
7565
7566   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7567
7568   priv = notebook->priv;
7569
7570   group = g_quark_from_string (group_name);
7571
7572   if (priv->group != group)
7573     {
7574       priv->group = group;
7575       g_object_notify (G_OBJECT (notebook), "group-name");
7576     }
7577 }
7578
7579 /**
7580  * gtk_notebook_get_group_name:
7581  * @notebook: a #GtkNotebook
7582  *
7583  * Gets the current group name for @notebook.
7584  *
7585  * Return Value: (transfer none): the group name,
7586  *     or %NULL if none is set.
7587  *
7588  * Since: 2.24
7589  **/
7590 const gchar *
7591 gtk_notebook_get_group_name (GtkNotebook *notebook)
7592 {
7593   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7594
7595   return g_quark_to_string (notebook->priv->group);
7596 }
7597
7598 /**
7599  * gtk_notebook_get_tab_reorderable:
7600  * @notebook: a #GtkNotebook
7601  * @child: a child #GtkWidget
7602  * 
7603  * Gets whether the tab can be reordered via drag and drop or not.
7604  * 
7605  * Return Value: %TRUE if the tab is reorderable.
7606  * 
7607  * Since: 2.10
7608  **/
7609 gboolean
7610 gtk_notebook_get_tab_reorderable (GtkNotebook *notebook,
7611                                   GtkWidget   *child)
7612 {
7613   GList *list;
7614
7615   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
7616   g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
7617
7618   list = CHECK_FIND_CHILD (notebook, child);
7619   if (!list)  
7620     return FALSE;
7621
7622   return GTK_NOTEBOOK_PAGE (list)->reorderable;
7623 }
7624
7625 /**
7626  * gtk_notebook_set_tab_reorderable:
7627  * @notebook: a #GtkNotebook
7628  * @child: a child #GtkWidget
7629  * @reorderable: whether the tab is reorderable or not.
7630  *
7631  * Sets whether the notebook tab can be reordered
7632  * via drag and drop or not.
7633  * 
7634  * Since: 2.10
7635  **/
7636 void
7637 gtk_notebook_set_tab_reorderable (GtkNotebook *notebook,
7638                                   GtkWidget   *child,
7639                                   gboolean     reorderable)
7640 {
7641   GList *list;
7642
7643   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7644   g_return_if_fail (GTK_IS_WIDGET (child));
7645
7646   list = CHECK_FIND_CHILD (notebook, child);
7647   if (!list)  
7648     return;
7649
7650   if (GTK_NOTEBOOK_PAGE (list)->reorderable != reorderable)
7651     {
7652       GTK_NOTEBOOK_PAGE (list)->reorderable = (reorderable == TRUE);
7653       gtk_widget_child_notify (child, "reorderable");
7654     }
7655 }
7656
7657 /**
7658  * gtk_notebook_get_tab_detachable:
7659  * @notebook: a #GtkNotebook
7660  * @child: a child #GtkWidget
7661  * 
7662  * Returns whether the tab contents can be detached from @notebook.
7663  * 
7664  * Return Value: TRUE if the tab is detachable.
7665  *
7666  * Since: 2.10
7667  **/
7668 gboolean
7669 gtk_notebook_get_tab_detachable (GtkNotebook *notebook,
7670                                  GtkWidget   *child)
7671 {
7672   GList *list;
7673
7674   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
7675   g_return_val_if_fail (GTK_IS_WIDGET (child), FALSE);
7676
7677   list = CHECK_FIND_CHILD (notebook, child);
7678   if (!list)  
7679     return FALSE;
7680
7681   return GTK_NOTEBOOK_PAGE (list)->detachable;
7682 }
7683
7684 /**
7685  * gtk_notebook_set_tab_detachable:
7686  * @notebook: a #GtkNotebook
7687  * @child: a child #GtkWidget
7688  * @detachable: whether the tab is detachable or not
7689  *
7690  * Sets whether the tab can be detached from @notebook to another
7691  * notebook or widget.
7692  *
7693  * Note that 2 notebooks must share a common group identificator
7694  * (see gtk_notebook_set_group()) to allow automatic tabs
7695  * interchange between them.
7696  *
7697  * If you want a widget to interact with a notebook through DnD
7698  * (i.e.: accept dragged tabs from it) it must be set as a drop
7699  * destination and accept the target "GTK_NOTEBOOK_TAB". The notebook
7700  * will fill the selection with a GtkWidget** pointing to the child
7701  * widget that corresponds to the dropped tab.
7702  * |[
7703  *  static void
7704  *  on_drop_zone_drag_data_received (GtkWidget        *widget,
7705  *                                   GdkDragContext   *context,
7706  *                                   gint              x,
7707  *                                   gint              y,
7708  *                                   GtkSelectionData *selection_data,
7709  *                                   guint             info,
7710  *                                   guint             time,
7711  *                                   gpointer          user_data)
7712  *  {
7713  *    GtkWidget *notebook;
7714  *    GtkWidget **child;
7715  *    
7716  *    notebook = gtk_drag_get_source_widget (context);
7717  *    child = (void*) selection_data->data;
7718  *    
7719  *    process_widget (*child);
7720  *    gtk_container_remove (GTK_CONTAINER (notebook), *child);
7721  *  }
7722  * ]|
7723  *
7724  * If you want a notebook to accept drags from other widgets,
7725  * you will have to set your own DnD code to do it.
7726  *
7727  * Since: 2.10
7728  **/
7729 void
7730 gtk_notebook_set_tab_detachable (GtkNotebook *notebook,
7731                                  GtkWidget  *child,
7732                                  gboolean    detachable)
7733 {
7734   GList *list;
7735
7736   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7737   g_return_if_fail (GTK_IS_WIDGET (child));
7738
7739   list = CHECK_FIND_CHILD (notebook, child);
7740   if (!list)  
7741     return;
7742
7743   if (GTK_NOTEBOOK_PAGE (list)->detachable != detachable)
7744     {
7745       GTK_NOTEBOOK_PAGE (list)->detachable = (detachable == TRUE);
7746       gtk_widget_child_notify (child, "detachable");
7747     }
7748 }
7749
7750 /**
7751  * gtk_notebook_get_action_widget:
7752  * @notebook: a #GtkNotebook
7753  * @pack_type: pack type of the action widget to receive
7754  *
7755  * Gets one of the action widgets. See gtk_notebook_set_action_widget().
7756  *
7757  * Returns: (transfer none): The action widget with the given @pack_type
7758  *     or %NULL when this action widget has not been set
7759  *
7760  * Since: 2.20
7761  */
7762 GtkWidget*
7763 gtk_notebook_get_action_widget (GtkNotebook *notebook,
7764                                 GtkPackType  pack_type)
7765 {
7766   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
7767
7768   return notebook->priv->action_widget[pack_type];
7769 }
7770
7771 /**
7772  * gtk_notebook_set_action_widget:
7773  * @notebook: a #GtkNotebook
7774  * @widget: a #GtkWidget
7775  * @pack_type: pack type of the action widget
7776  *
7777  * Sets @widget as one of the action widgets. Depending on the pack type
7778  * the widget will be placed before or after the tabs. You can use
7779  * a #GtkBox if you need to pack more than one widget on the same side.
7780  *
7781  * Note that action widgets are "internal" children of the notebook and thus
7782  * not included in the list returned from gtk_container_foreach().
7783  *
7784  * Since: 2.20
7785  */
7786 void
7787 gtk_notebook_set_action_widget (GtkNotebook *notebook,
7788                                 GtkWidget   *widget,
7789                                 GtkPackType  pack_type)
7790 {
7791   GtkNotebookPrivate *priv;
7792
7793   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
7794   g_return_if_fail (!widget || GTK_IS_WIDGET (widget));
7795   g_return_if_fail (!widget || gtk_widget_get_parent (widget) == NULL);
7796
7797   priv = notebook->priv;
7798
7799   if (priv->action_widget[pack_type])
7800     gtk_widget_unparent (priv->action_widget[pack_type]);
7801
7802   priv->action_widget[pack_type] = widget;
7803
7804   if (widget)
7805     {
7806       gtk_widget_set_child_visible (widget, priv->show_tabs);
7807       gtk_widget_set_parent (widget, GTK_WIDGET (notebook));
7808     }
7809
7810   gtk_widget_queue_resize (GTK_WIDGET (notebook));
7811 }