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