]> Pileus Git - ~andy/gtk/blob - gtk/gtknotebook.c
Changed LGPL address for FSF in all .h and .c files
[~andy/gtk] / gtk / gtknotebook.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "gtknotebook.h"
20 #include "gtksignal.h"
21 #include "gtkmain.h"
22 #include "gtkmenu.h"
23 #include "gtkmenuitem.h"
24 #include "gtklabel.h"
25 #include <gdk/gdkkeysyms.h>
26 #include <stdio.h>
27
28
29 #define TAB_OVERLAP    2
30 #define TAB_CURVATURE  1
31 #define ARROW_SIZE     11
32 #define ARROW_SPACING  3
33 #define NOTEBOOK_INIT_SCROLL_DELAY (200)
34 #define NOTEBOOK_SCROLL_DELAY      (100)
35
36
37 enum {
38   SWITCH_PAGE,
39   LAST_SIGNAL
40 };
41
42 enum {
43   STEP_PREV,
44   STEP_NEXT
45 };
46
47 typedef void (*GtkNotebookSignal) (GtkObject       *object,
48                                    GtkNotebookPage *arg1,
49                                    gint             arg2,
50                                    gpointer         data);
51
52 static void gtk_notebook_class_init          (GtkNotebookClass *klass);
53 static void gtk_notebook_init                (GtkNotebook      *notebook);
54 static void gtk_notebook_destroy             (GtkObject        *object);
55 static void gtk_notebook_map                 (GtkWidget        *widget);
56 static void gtk_notebook_unmap               (GtkWidget        *widget);
57 static void gtk_notebook_realize             (GtkWidget        *widget);
58 static void gtk_notebook_panel_realize       (GtkNotebook      *notebook);
59 static void gtk_notebook_size_request        (GtkWidget        *widget,
60                                               GtkRequisition   *requisition);
61 static void gtk_notebook_size_allocate       (GtkWidget        *widget,
62                                               GtkAllocation    *allocation);
63 static void gtk_notebook_paint               (GtkWidget        *widget,
64                                               GdkRectangle     *area);
65 static void gtk_notebook_draw                (GtkWidget        *widget,
66                                               GdkRectangle     *area);
67 static gint gtk_notebook_expose              (GtkWidget        *widget,
68                                               GdkEventExpose   *event);
69 static gint gtk_notebook_button_press        (GtkWidget        *widget,
70                                               GdkEventButton   *event);
71 static gint gtk_notebook_button_release      (GtkWidget        *widget,
72                                               GdkEventButton   *event);
73 static gint gtk_notebook_enter_notify        (GtkWidget        *widget,
74                                               GdkEventCrossing *event);
75 static gint gtk_notebook_leave_notify        (GtkWidget        *widget,
76                                               GdkEventCrossing *event);
77 static gint gtk_notebook_motion_notify       (GtkWidget        *widget,
78                                               GdkEventMotion   *event);
79 static gint gtk_notebook_key_press           (GtkWidget        *widget,
80                                               GdkEventKey      *event);
81 static void gtk_notebook_add                 (GtkContainer     *container,
82                                               GtkWidget        *widget);
83 static void gtk_notebook_remove              (GtkContainer     *container,
84                                               GtkWidget        *widget);
85 static void gtk_notebook_real_remove         (GtkNotebook      *notebook,
86                                               GList            *list,
87                                               gint              page_num);
88 static void gtk_notebook_foreach             (GtkContainer     *container,
89                                               GtkCallback       callback,
90                                               gpointer          callback_data);
91 static void gtk_notebook_switch_page         (GtkNotebook      *notebook,
92                                               GtkNotebookPage  *page,
93                                               gint              page_num);
94 static void gtk_notebook_draw_tab            (GtkNotebook      *notebook,
95                                               GtkNotebookPage  *page,
96                                               GdkRectangle     *area);
97 static gint gtk_notebook_focus_in            (GtkWidget        *widget,
98                                               GdkEventFocus    *event);
99 static gint gtk_notebook_focus_out           (GtkWidget        *widget,
100                                               GdkEventFocus    *event);
101 static void gtk_notebook_draw_focus          (GtkWidget        *widget);
102 static void gtk_notebook_focus_changed       (GtkNotebook      *notebook, 
103                                               GtkNotebookPage  *old_page);
104 static void gtk_notebook_pages_allocate      (GtkNotebook      *notebook,
105                                               GtkAllocation    *allocation);
106 static void gtk_notebook_page_allocate       (GtkNotebook      *notebook,
107                                               GtkNotebookPage  *page,
108                                               GtkAllocation    *allocation);
109 static void gtk_notebook_draw_arrow          (GtkNotebook      *notebook, 
110                                               guint             arrow);
111 static gint gtk_notebook_timer               (GtkNotebook      *notebook);
112 static gint gtk_notebook_focus               (GtkContainer     *container,
113                                               GtkDirectionType  direction);
114 static gint gtk_notebook_page_select         (GtkNotebook      *notebook);
115 static void gtk_notebook_calc_tabs           (GtkNotebook      *notebook, 
116                                               GList            *start, 
117                                               GList           **end,
118                                               gint             *tab_space,
119                                               guint             direction);
120 static void gtk_notebook_expose_tabs         (GtkNotebook      *notebook);
121 static void gtk_notebook_switch_focus_tab    (GtkNotebook      *notebook, 
122                                               GList            *new_child);
123 static void gtk_real_notebook_switch_page    (GtkNotebook      *notebook,
124                                               GtkNotebookPage  *page);
125 static void gtk_notebook_marshal_signal      (GtkObject        *object,
126                                               GtkSignalFunc     func,
127                                               gpointer          func_data,
128                                               GtkArg           *args);
129 static void gtk_notebook_menu_switch_page    (GtkWidget        *widget,
130                                               GtkNotebookPage  *page);
131 static void gtk_notebook_update_labels       (GtkNotebook      *notebook,
132                                               GList            *list,
133                                               gint              page_num);
134 static void gtk_notebook_menu_detacher       (GtkWidget        *widget,
135                                               GtkMenu          *menu);
136 static void gtk_notebook_menu_label_unparent (GtkWidget        *widget, 
137                                               gpointer          data);
138 static void gtk_notebook_menu_item_create    (GtkNotebook      *notebook, 
139                                               GtkNotebookPage  *page,
140                                               gint              position);
141
142
143 static GtkContainerClass *parent_class = NULL;
144 static guint notebook_signals[LAST_SIGNAL] = { 0 };
145
146 guint
147 gtk_notebook_get_type ()
148 {
149   static guint notebook_type = 0;
150
151   if (!notebook_type)
152     {
153       GtkTypeInfo notebook_info =
154       {
155         "GtkNotebook",
156         sizeof (GtkNotebook),
157         sizeof (GtkNotebookClass),
158         (GtkClassInitFunc) gtk_notebook_class_init,
159         (GtkObjectInitFunc) gtk_notebook_init,
160         (GtkArgSetFunc) NULL,
161         (GtkArgGetFunc) NULL,
162       };
163
164       notebook_type = gtk_type_unique (gtk_container_get_type (), &notebook_info);
165     }
166
167   return notebook_type;
168 }
169
170 static void
171 gtk_notebook_class_init (GtkNotebookClass *class)
172 {
173   GtkObjectClass *object_class;
174   GtkWidgetClass *widget_class;
175   GtkContainerClass *container_class;
176
177   object_class = (GtkObjectClass*) class;
178   widget_class = (GtkWidgetClass*) class;
179   container_class = (GtkContainerClass*) class;
180
181   parent_class = gtk_type_class (gtk_container_get_type ());
182
183   notebook_signals[SWITCH_PAGE] =
184     gtk_signal_new ("switch_page",
185                     GTK_RUN_LAST,
186                     object_class->type,
187                     GTK_SIGNAL_OFFSET (GtkNotebookClass, switch_page),
188                     gtk_notebook_marshal_signal,
189                     GTK_TYPE_NONE, 2,
190                     GTK_TYPE_POINTER,
191                     GTK_TYPE_INT);
192
193   gtk_object_class_add_signals (object_class, notebook_signals, LAST_SIGNAL);
194
195   object_class->destroy = gtk_notebook_destroy;
196
197   widget_class->map = gtk_notebook_map;
198   widget_class->unmap = gtk_notebook_unmap;
199   widget_class->realize = gtk_notebook_realize;
200   widget_class->size_request = gtk_notebook_size_request;
201   widget_class->size_allocate = gtk_notebook_size_allocate;
202   widget_class->draw = gtk_notebook_draw;
203   widget_class->expose_event = gtk_notebook_expose;
204   widget_class->button_press_event = gtk_notebook_button_press;
205   widget_class->button_release_event = gtk_notebook_button_release;
206   widget_class->enter_notify_event = gtk_notebook_enter_notify;
207   widget_class->leave_notify_event = gtk_notebook_leave_notify;
208   widget_class->motion_notify_event = gtk_notebook_motion_notify;
209   widget_class->key_press_event = gtk_notebook_key_press;
210   widget_class->focus_in_event = gtk_notebook_focus_in;
211   widget_class->focus_out_event = gtk_notebook_focus_out;
212   widget_class->draw_focus = gtk_notebook_draw_focus;
213
214   container_class->add = gtk_notebook_add;
215   container_class->remove = gtk_notebook_remove;
216   container_class->foreach = gtk_notebook_foreach;
217   container_class->focus = gtk_notebook_focus;
218
219   class->switch_page = gtk_real_notebook_switch_page;
220 }
221
222 static void
223 gtk_notebook_init (GtkNotebook *notebook)
224 {
225   GTK_WIDGET_SET_FLAGS (notebook, GTK_CAN_FOCUS);
226
227   notebook->cur_page = NULL;
228   notebook->children = NULL;
229   notebook->first_tab = NULL;
230   notebook->focus_tab = NULL;
231   notebook->panel = NULL;
232   notebook->menu = NULL;
233
234   notebook->tab_border = 3;
235   notebook->show_tabs = TRUE;
236   notebook->show_border = TRUE;
237   notebook->tab_pos = GTK_POS_TOP;
238   notebook->scrollable = FALSE;
239   notebook->in_child = 0;
240   notebook->click_child = 0;
241   notebook->button = 0;
242   notebook->need_timer = 0;
243 }
244
245 GtkWidget*
246 gtk_notebook_new ()
247 {
248   return GTK_WIDGET (gtk_type_new (gtk_notebook_get_type ()));
249 }
250
251 static void
252 gtk_notebook_destroy (GtkObject *object)
253 {
254   GtkNotebook *notebook;
255   
256   g_return_if_fail (object != NULL);
257   g_return_if_fail (GTK_IS_NOTEBOOK (object));
258
259   notebook = GTK_NOTEBOOK (object);
260
261   if (notebook->menu)
262     gtk_notebook_popup_disable (notebook);
263
264   GTK_OBJECT_CLASS (parent_class)->destroy (object);
265 }
266
267 void
268 gtk_notebook_append_page (GtkNotebook *notebook,
269                           GtkWidget   *child,
270                           GtkWidget   *tab_label)
271 {
272   g_return_if_fail (notebook != NULL);
273   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
274   g_return_if_fail (child != NULL);
275
276   gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, -1);
277 }
278
279 void
280 gtk_notebook_append_page_menu (GtkNotebook *notebook,
281                                GtkWidget   *child,
282                                GtkWidget   *tab_label,
283                                GtkWidget   *menu_label)
284 {
285   g_return_if_fail (notebook != NULL);
286   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
287   g_return_if_fail (child != NULL);
288
289   gtk_notebook_insert_page_menu (notebook, child, tab_label, menu_label, -1);
290 }
291
292 void
293 gtk_notebook_prepend_page (GtkNotebook *notebook,
294                            GtkWidget   *child,
295                            GtkWidget   *tab_label)
296 {
297   g_return_if_fail (notebook != NULL);
298   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
299   g_return_if_fail (child != NULL);
300
301   gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, 0);
302 }
303
304 void
305 gtk_notebook_prepend_page_menu (GtkNotebook *notebook,
306                                 GtkWidget   *child,
307                                 GtkWidget   *tab_label,
308                                 GtkWidget   *menu_label)
309 {
310   g_return_if_fail (notebook != NULL);
311   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
312   g_return_if_fail (child != NULL);
313
314   gtk_notebook_insert_page_menu (notebook, child, tab_label, menu_label, 0);
315 }
316
317 void
318 gtk_notebook_insert_page (GtkNotebook *notebook,
319                           GtkWidget   *child,
320                           GtkWidget   *tab_label,
321                           gint         position)
322 {
323   g_return_if_fail (notebook != NULL);
324   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
325   g_return_if_fail (child != NULL);
326
327   gtk_notebook_insert_page_menu (notebook, child, tab_label, NULL, position);
328 }
329
330 void
331 gtk_notebook_insert_page_menu (GtkNotebook *notebook,
332                                GtkWidget   *child,
333                                GtkWidget   *tab_label,
334                                GtkWidget   *menu_label,
335                                gint         position)
336 {
337   GtkNotebookPage *page;
338   gint nchildren;
339
340   g_return_if_fail (notebook != NULL);
341   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
342   g_return_if_fail (child != NULL);
343
344   page = g_new (GtkNotebookPage, 1);
345   page->child = child;
346   page->requisition.width = 0;
347   page->requisition.height = 0;
348   page->allocation.x = 0;
349   page->allocation.y = 0;
350   page->allocation.width = 0;
351   page->allocation.height = 0;
352   page->default_menu = FALSE;
353   page->default_tab = FALSE;
354
355   nchildren = g_list_length (notebook->children);
356   if ((position < 0) || (position > nchildren))
357     position = nchildren;
358
359   notebook->children = g_list_insert (notebook->children, page, position);
360
361   if (!tab_label)
362     {
363       page->default_tab = TRUE;
364       if (notebook->show_tabs)
365         tab_label = gtk_label_new ("");
366     }
367   page->tab_label = tab_label;
368   page->menu_label = menu_label;
369
370   if (!menu_label)
371     page->default_menu = TRUE;
372   else  
373     {
374       gtk_widget_ref (page->menu_label);
375       gtk_object_sink (GTK_OBJECT(page->menu_label));
376     }
377
378   if (notebook->menu)
379     gtk_notebook_menu_item_create (notebook, page, position);
380
381   gtk_notebook_update_labels 
382     (notebook, g_list_nth (notebook->children, position), position + 1);
383
384   if (!notebook->first_tab)
385     notebook->first_tab = notebook->children;
386
387   if (!notebook->focus_tab)
388     notebook->focus_tab = notebook->children;
389
390   gtk_widget_set_parent (child, GTK_WIDGET (notebook));
391   if (tab_label)
392     {
393       gtk_widget_set_parent (tab_label, GTK_WIDGET (notebook));
394       gtk_widget_show (tab_label);
395     }
396
397   if (!notebook->cur_page)
398     gtk_notebook_switch_page (notebook, page, 0);
399
400   if (GTK_WIDGET_VISIBLE (notebook))
401     {
402       if (GTK_WIDGET_REALIZED (notebook) &&
403           !GTK_WIDGET_REALIZED (child))
404         gtk_widget_realize (child);
405         
406       if (GTK_WIDGET_MAPPED (notebook) &&
407           !GTK_WIDGET_MAPPED (child) && notebook->cur_page == page)
408         gtk_widget_map (child);
409
410       if (tab_label)
411         {
412           if (GTK_WIDGET_REALIZED (notebook) &&
413               !GTK_WIDGET_REALIZED (tab_label))
414             gtk_widget_realize (tab_label);
415       
416           if (GTK_WIDGET_MAPPED (notebook) &&
417               !GTK_WIDGET_MAPPED (tab_label))
418             gtk_widget_map (tab_label);
419         }
420     }
421
422   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (notebook))
423     gtk_widget_queue_resize (child);
424 }
425
426 void
427 gtk_notebook_remove_page (GtkNotebook *notebook,
428                           gint         page_num)
429 {
430   GList *list;
431
432   g_return_if_fail (notebook != NULL);
433   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
434
435   if ((list = g_list_nth (notebook->children, page_num)))
436     gtk_notebook_real_remove (notebook, list, page_num);
437 }
438
439 static void
440 gtk_notebook_add (GtkContainer *container,
441                   GtkWidget    *widget)
442 {
443   g_return_if_fail (container != NULL);
444   g_return_if_fail (GTK_IS_NOTEBOOK (container));
445   g_return_if_fail (widget != NULL);
446
447   gtk_notebook_insert_page_menu (GTK_NOTEBOOK (container), widget, 
448                                  NULL, NULL, -1);
449 }
450
451 static void
452 gtk_notebook_remove (GtkContainer *container,
453                      GtkWidget    *widget)
454 {
455   GtkNotebook *notebook;
456   GtkNotebookPage *page;
457   GList *children;
458   gint page_num;
459
460   g_return_if_fail (container != NULL);
461   g_return_if_fail (GTK_IS_NOTEBOOK (container));
462   g_return_if_fail (widget != NULL);
463
464   notebook = GTK_NOTEBOOK (container);
465
466   children = notebook->children;
467   page_num = 0;
468   while (children)
469     {
470       page = children->data;
471       if (page->child == widget)
472         {
473           gtk_notebook_real_remove (notebook, children, page_num);
474           break;
475         }
476       page_num++;
477       children = children->next;
478     }
479 }
480
481 static void
482 gtk_notebook_real_remove (GtkNotebook *notebook,
483                           GList        *list,
484                           gint          page_num)
485 {
486   GtkNotebookPage *page;
487   GList * next_list;
488   gint need_resize = FALSE;
489       
490   if (list->prev)
491     {
492       next_list = list->prev;
493       page_num--;
494     }
495   else if (list->next)
496     {
497       next_list = list->next;
498       page_num++;
499     }
500   else 
501     next_list = NULL;
502   
503   if (notebook->cur_page == list->data)
504     { 
505       notebook->cur_page = NULL;
506       if (next_list)
507         {
508           page = next_list->data;
509           gtk_notebook_switch_page (notebook, page, page_num);
510         }
511     }
512   
513   if (list == notebook->first_tab)
514     notebook->first_tab = next_list;
515   if (list == notebook->focus_tab)
516     notebook->focus_tab = next_list;
517   
518   page = list->data;
519   
520   if ((GTK_WIDGET_VISIBLE (page->child) || 
521        (page->tab_label && GTK_WIDGET_VISIBLE (page->tab_label))) 
522        && GTK_WIDGET_VISIBLE (notebook))
523     need_resize = TRUE;
524
525   gtk_widget_unparent (page->child);
526
527   if (page->tab_label)
528     gtk_widget_unparent (page->tab_label);
529
530   if (notebook->menu)
531     {
532       gtk_container_remove (GTK_CONTAINER (notebook->menu), 
533                             page->menu_label->parent);
534       gtk_widget_queue_resize (notebook->menu);
535     }
536   if (!page->default_menu)
537     gtk_widget_unref (page->menu_label);
538   
539   gtk_notebook_update_labels (notebook, list->next, page_num + 1);
540   
541   notebook->children = g_list_remove_link (notebook->children, list);
542   g_list_free (list);
543   g_free (page);
544
545   if (need_resize)
546     gtk_widget_queue_resize (GTK_WIDGET (notebook));
547
548 }
549
550 gint
551 gtk_notebook_current_page (GtkNotebook *notebook)
552 {
553   GList *children;
554   gint cur_page;
555
556   g_return_val_if_fail (notebook != NULL, -1);
557   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), -1);
558
559   if (notebook->cur_page)
560     {
561       cur_page = 0;
562       children = notebook->children;
563
564       while (children)
565         {
566           if (children->data == notebook->cur_page)
567             break;
568           children = children->next;
569           cur_page += 1;
570         }
571
572       if (!children)
573         cur_page = -1;
574     }
575   else
576     {
577       cur_page = -1;
578     }
579
580   return cur_page;
581 }
582
583 void
584 gtk_notebook_set_page (GtkNotebook *notebook,
585                        gint         page_num)
586 {
587   GList *list;
588
589   g_return_if_fail (notebook != NULL);
590   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
591
592   if ((list = g_list_nth (notebook->children, page_num)))
593       gtk_notebook_switch_page (notebook, 
594                                 ((GtkNotebookPage *)(list->data)), page_num);
595 }
596
597 void
598 gtk_notebook_next_page (GtkNotebook *notebook)
599 {
600   GtkNotebookPage *page;
601   GList *children;
602   gint num;
603
604   g_return_if_fail (notebook != NULL);
605   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
606
607   children = notebook->children;
608   num = 0;
609   while (children)
610     {
611       if (notebook->cur_page == children->data)
612         break;
613       children = children->next;
614       num++;
615     }
616
617   if (!children)
618     return;
619
620   if (children->next)
621     {
622       children = children->next;
623       num++;
624     }
625   else
626     {
627       children = notebook->children;
628       num = 0;
629     }
630
631   page = children->data;
632   gtk_notebook_switch_page (notebook, page, num);
633 }
634
635 void
636 gtk_notebook_prev_page (GtkNotebook *notebook)
637 {
638   GtkNotebookPage *page;
639   GList *children;
640   gint num;
641
642   g_return_if_fail (notebook != NULL);
643   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
644
645   children = notebook->children;
646   num = 0;
647   while (children)
648     {
649       if (notebook->cur_page == children->data)
650         break;
651       children = children->next;
652       num++;
653     }
654
655   if (!children)
656     return;
657
658   if (children->prev)
659     {
660       children = children->prev;
661       num--;
662     }
663   else
664     {
665       while (children->next)
666         {
667           children = children->next;
668           num++;
669         }
670     }
671
672   page = children->data;
673   gtk_notebook_switch_page (notebook, page, num);
674 }
675
676 static void
677 gtk_notebook_foreach (GtkContainer *container,
678                       GtkCallback   callback,
679                       gpointer      callback_data)
680 {
681   GtkNotebook *notebook;
682   GtkNotebookPage *page;
683   GList *children;
684
685   g_return_if_fail (container != NULL);
686   g_return_if_fail (GTK_IS_NOTEBOOK (container));
687   g_return_if_fail (callback != NULL);
688
689   notebook = GTK_NOTEBOOK (container);
690
691   children = notebook->children;
692   while (children)
693     {
694       page = children->data;
695       children = children->next;
696       (* callback) (page->child, callback_data);
697     }
698 }
699
700 static void
701 gtk_notebook_expose_tabs (GtkNotebook *notebook)
702 {
703   GtkWidget *widget;
704   GtkNotebookPage *page;
705   GdkEventExpose event;
706   gint border;
707
708   widget = GTK_WIDGET (notebook);
709   border = GTK_CONTAINER (notebook)->border_width;
710
711   page = notebook->first_tab->data;
712
713   event.type = GDK_EXPOSE;
714   event.window = widget->window;
715   event.count = 0;
716   event.area.x = border;
717   event.area.y = border;
718
719   switch (notebook->tab_pos)
720     {
721     case GTK_POS_BOTTOM:
722       event.area.y = widget->allocation.height - border 
723         - page->allocation.height - widget->style->klass->ythickness;
724       if (notebook->first_tab->data != notebook->cur_page)
725         event.area.y -= widget->style->klass->ythickness;
726     case GTK_POS_TOP:
727       event.area.width = widget->allocation.width - 2 * border;
728       event.area.height = page->allocation.height 
729         + widget->style->klass->ythickness;
730       if (notebook->first_tab->data != notebook->cur_page)
731         event.area.height += widget->style->klass->ythickness;
732       break;
733     case GTK_POS_RIGHT:
734       event.area.x = widget->allocation.width - border 
735         - page->allocation.width - widget->style->klass->xthickness;
736       if (notebook->first_tab->data != notebook->cur_page)
737         event.area.x -= widget->style->klass->xthickness;
738     case GTK_POS_LEFT:
739       event.area.width = page->allocation.width 
740         + widget->style->klass->xthickness;
741       event.area.height = widget->allocation.height - 2 * border;
742       if (notebook->first_tab->data != notebook->cur_page)
743         event.area.width += widget->style->klass->xthickness;
744       break;
745     }        
746   gtk_widget_event (widget, (GdkEvent *) &event);
747 }
748
749 void
750 gtk_notebook_set_tab_pos (GtkNotebook     *notebook,
751                           GtkPositionType  pos)
752 {
753   g_return_if_fail (notebook != NULL);
754   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
755
756   if (notebook->tab_pos != pos)
757     {
758       notebook->tab_pos = pos;
759
760       if (GTK_WIDGET_VISIBLE (notebook))
761         {
762           gtk_widget_queue_resize (GTK_WIDGET (notebook));
763           if (notebook->panel)
764             gdk_window_clear (notebook->panel);
765         }
766     }
767 }
768
769 void
770 gtk_notebook_set_show_tabs (GtkNotebook *notebook,
771                             gint         show_tabs)
772 {
773   GtkNotebookPage *page;
774   GList *children;
775
776   g_return_if_fail (notebook != NULL);
777   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
778
779   if (notebook->show_tabs == show_tabs)
780     return;
781
782   notebook->show_tabs = show_tabs;
783   children = notebook->children;
784
785   if (!show_tabs)
786     {
787       GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS);
788       
789       while (children)
790         {
791           page = children->data;
792           children = children->next;
793           if (page->default_tab)
794             {
795               gtk_widget_destroy (page->tab_label);
796               page->tab_label = NULL;
797             }
798           else
799             gtk_widget_hide (page->tab_label);
800         }
801       
802       if (notebook->panel)
803         gdk_window_hide (notebook->panel);
804     }
805   else
806     {
807       gchar string[32];
808       gint i = 1;
809       
810       GTK_WIDGET_SET_FLAGS (notebook, GTK_CAN_FOCUS);
811       
812       while (children)
813         {
814           page = children->data;
815           children = children->next;
816           if (page->default_tab)
817             {
818               sprintf (string, "Page %d", i);
819               page->tab_label = gtk_label_new (string);
820               gtk_widget_set_parent (page->tab_label, GTK_WIDGET (notebook));
821             }
822           gtk_widget_show (page->tab_label);
823           i++;
824         }
825     }
826   gtk_widget_queue_resize (GTK_WIDGET (notebook));
827 }
828
829 void
830 gtk_notebook_set_show_border (GtkNotebook *notebook,
831                               gint         show_border)
832 {
833   g_return_if_fail (notebook != NULL);
834   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
835
836   if (notebook->show_border != show_border)
837     {
838       notebook->show_border = show_border;
839
840       if (GTK_WIDGET_VISIBLE (notebook))
841         gtk_widget_queue_resize (GTK_WIDGET (notebook));
842     }
843 }
844
845 void
846 gtk_notebook_set_scrollable (GtkNotebook     *notebook,
847                              gint             scrollable)
848 {
849   g_return_if_fail (notebook != NULL);
850   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
851
852   if (scrollable != notebook->scrollable)
853     {
854       if ( (notebook->scrollable = (scrollable != 0)) ) 
855         gtk_notebook_panel_realize (notebook);
856       else if (notebook->panel)
857         {
858           gdk_window_destroy (notebook->panel);
859           notebook->panel = NULL;
860         }
861       gtk_widget_queue_resize (GTK_WIDGET(notebook));
862     }     
863 }
864
865 static void
866 gtk_notebook_map (GtkWidget *widget)
867 {
868   GtkNotebook *notebook;
869   GtkNotebookPage *page;
870   GList *children;
871
872   g_return_if_fail (widget != NULL);
873   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
874
875   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
876   gdk_window_show (widget->window);
877
878   notebook = GTK_NOTEBOOK (widget);
879
880   if (notebook->cur_page && 
881       GTK_WIDGET_VISIBLE (notebook->cur_page->child) &&
882       !GTK_WIDGET_MAPPED (notebook->cur_page->child))
883     gtk_widget_map (notebook->cur_page->child);
884
885   if (notebook->scrollable)
886       gtk_notebook_pages_allocate (notebook, &(widget->allocation));
887   else
888     {
889       children = notebook->children;
890
891       while (children)
892         {
893           page = children->data;
894           children = children->next;
895
896           if (page->tab_label && 
897               GTK_WIDGET_VISIBLE (page->child) && 
898               !GTK_WIDGET_MAPPED (page->tab_label))
899             gtk_widget_map (page->tab_label);
900         }
901     }
902 }
903
904 static void
905 gtk_notebook_unmap (GtkWidget *widget)
906 {
907   g_return_if_fail (widget != NULL);
908   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
909
910   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
911   gdk_window_hide (widget->window);
912   if (GTK_NOTEBOOK (widget)->panel)
913     gdk_window_hide (GTK_NOTEBOOK (widget)->panel);
914 }
915
916 static void
917 gtk_notebook_realize (GtkWidget *widget)
918 {
919   GtkNotebook *notebook;
920   GdkWindowAttr attributes;
921   gint attributes_mask;
922
923   g_return_if_fail (widget != NULL);
924   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
925
926   notebook = GTK_NOTEBOOK (widget);
927   GTK_WIDGET_SET_FLAGS (notebook, GTK_REALIZED);
928
929   attributes.window_type = GDK_WINDOW_CHILD;
930   attributes.x = widget->allocation.x;
931   attributes.y = widget->allocation.y;
932   attributes.width = widget->allocation.width;
933   attributes.height = widget->allocation.height;
934   attributes.wclass = GDK_INPUT_OUTPUT;
935   attributes.visual = gtk_widget_get_visual (widget);
936   attributes.colormap = gtk_widget_get_colormap (widget);
937   attributes.event_mask = gtk_widget_get_events (widget);
938   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
939     | GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK;
940
941   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
942
943   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
944   gdk_window_set_user_data (widget->window, notebook);
945
946   widget->style = gtk_style_attach (widget->style, widget->window);
947   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
948
949   if (notebook->scrollable)
950     gtk_notebook_panel_realize (notebook);
951 }
952
953 static void
954 gtk_notebook_panel_realize (GtkNotebook *notebook)
955 {
956   GtkWidget *widget;
957   GdkWindowAttr attributes;
958   gint attributes_mask;
959   
960   g_return_if_fail (notebook != NULL);
961   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
962   
963   widget = GTK_WIDGET (notebook);
964   
965   attributes.window_type = GDK_WINDOW_CHILD;
966   attributes.wclass = GDK_INPUT_OUTPUT;
967   attributes.visual = gtk_widget_get_visual (widget);
968   attributes.colormap = gtk_widget_get_colormap (widget);
969   attributes.event_mask = gtk_widget_get_events (widget);
970   attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK 
971     | GDK_BUTTON_RELEASE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_ENTER_NOTIFY_MASK
972     | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
973   
974   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
975   
976   attributes.width = 2 * ARROW_SIZE + ARROW_SPACING;
977   attributes.height = ARROW_SIZE;
978
979   attributes.x = widget->allocation.width - attributes.width - 
980     GTK_CONTAINER (notebook)->border_width;
981   attributes.y = widget->allocation.height - ARROW_SIZE -
982     GTK_CONTAINER (notebook)->border_width;
983   if (notebook->tab_pos == GTK_POS_TOP)
984     attributes.y = GTK_CONTAINER (notebook)->border_width;
985   else if (notebook->tab_pos == GTK_POS_LEFT)
986     attributes.x = widget->allocation.x 
987       + GTK_CONTAINER (notebook)->border_width;
988
989   
990   notebook->panel = gdk_window_new (widget->window, &attributes, 
991                                     attributes_mask);
992   gtk_style_set_background (widget->style, notebook->panel, 
993                             GTK_STATE_NORMAL);
994   gdk_window_set_user_data (notebook->panel, widget);
995 }
996
997 static void
998 gtk_notebook_size_request (GtkWidget      *widget,
999                            GtkRequisition *requisition)
1000 {
1001   GtkNotebook *notebook;
1002   GtkNotebookPage *page;
1003   GList *children;
1004
1005   g_return_if_fail (widget != NULL);
1006   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
1007   g_return_if_fail (requisition != NULL);
1008
1009   notebook = GTK_NOTEBOOK (widget);
1010   widget->requisition.width = 0;
1011   widget->requisition.height = 0;
1012
1013   children = notebook->children;
1014   while (children)
1015     {
1016       page = children->data;
1017       children = children->next;
1018
1019       if (GTK_WIDGET_VISIBLE (page->child))
1020         {
1021           gtk_widget_size_request (page->child, &page->child->requisition);
1022           
1023           widget->requisition.width = MAX (widget->requisition.width,
1024                                            page->child->requisition.width);
1025           widget->requisition.height = MAX (widget->requisition.height,
1026                                             page->child->requisition.height);
1027         }
1028     }
1029
1030   if (notebook->show_border || notebook->show_tabs)
1031     {
1032       widget->requisition.width += widget->style->klass->xthickness * 2;
1033       widget->requisition.height += widget->style->klass->ythickness * 2;
1034
1035       if (notebook->show_tabs)
1036         {
1037           gint tab_width = 0;
1038           gint tab_height = 0;
1039           gint tab_max = 0;
1040           
1041           children = notebook->children;
1042           while (children)
1043             {
1044               page = children->data;
1045               children = children->next;
1046               
1047               if (GTK_WIDGET_VISIBLE (page->child))
1048                 {
1049                   gtk_widget_size_request (page->tab_label, 
1050                                            &page->tab_label->requisition);
1051                   
1052                   page->requisition.width = 
1053                     (page->tab_label->requisition.width +
1054                      (widget->style->klass->xthickness + notebook->tab_border)
1055                      * 2);
1056                   page->requisition.height = 
1057                     (page->tab_label->requisition.height +
1058                      (widget->style->klass->ythickness + notebook->tab_border)
1059                      * 2);
1060                   
1061                   switch (notebook->tab_pos)
1062                     {
1063                     case GTK_POS_TOP:
1064                     case GTK_POS_BOTTOM:
1065                       page->requisition.width -= TAB_OVERLAP;
1066
1067                       tab_width += page->requisition.width;
1068                       tab_height = MAX (tab_height, page->requisition.height);
1069                       tab_max = MAX (tab_max, page->requisition.width);
1070                       break;
1071                     case GTK_POS_LEFT:
1072                     case GTK_POS_RIGHT:
1073                       page->requisition.height -= TAB_OVERLAP;
1074
1075                       tab_width = MAX (tab_width, page->requisition.width);
1076                       tab_height += page->requisition.height;
1077                       tab_max = MAX (tab_max, page->requisition.height);
1078                       break;
1079                     }
1080                 }
1081             }
1082
1083           children = notebook->children;
1084
1085           if (children && children->next && notebook->scrollable) 
1086             {
1087               if ((notebook->tab_pos == GTK_POS_TOP) ||
1088                   (notebook->tab_pos == GTK_POS_BOTTOM))
1089                 {
1090                   if (widget->requisition.width < tab_width)
1091                     {
1092                       tab_width = tab_max + 2 * (ARROW_SIZE + ARROW_SPACING);
1093                       tab_height = MAX (tab_height, ARROW_SIZE);
1094                     }
1095                 }
1096               else
1097                 {
1098                   if (widget->requisition.height < tab_height)
1099                     {
1100                       tab_height = tab_max + ARROW_SIZE + ARROW_SPACING;
1101                       tab_width = MAX (tab_width, 
1102                                        ARROW_SPACING + 2 * ARROW_SIZE);
1103                     }
1104                 }
1105             }
1106
1107           while (children)
1108             {
1109               page = children->data;
1110               children = children->next;
1111               
1112               if (GTK_WIDGET_VISIBLE (page->child))
1113                 {
1114                   if ((notebook->tab_pos == GTK_POS_TOP) ||
1115                       (notebook->tab_pos == GTK_POS_BOTTOM))
1116                     page->requisition.height = tab_height;
1117                   else
1118                     page->requisition.width = tab_width;
1119                 }
1120             }
1121           
1122           switch (notebook->tab_pos)
1123             {
1124             case GTK_POS_TOP:
1125             case GTK_POS_BOTTOM:
1126               tab_width += widget->style->klass->xthickness;
1127               widget->requisition.width = MAX (widget->requisition.width, 
1128                                                tab_width);
1129               widget->requisition.height += tab_height;
1130               break;
1131             case GTK_POS_LEFT:
1132             case GTK_POS_RIGHT:
1133               tab_height += widget->style->klass->ythickness;
1134               widget->requisition.width += tab_width;
1135               widget->requisition.height = MAX (widget->requisition.height, 
1136                                                 tab_height);
1137               break;
1138             }
1139         }
1140     }
1141   widget->requisition.width += GTK_CONTAINER (widget)->border_width * 2;
1142   widget->requisition.height += GTK_CONTAINER (widget)->border_width * 2;
1143 }
1144
1145 static void
1146 gtk_notebook_size_allocate (GtkWidget     *widget,
1147                             GtkAllocation *allocation)
1148 {
1149   GtkNotebook *notebook;
1150   GtkNotebookPage *page;
1151   GtkAllocation child_allocation;
1152   GList *children;
1153
1154   g_return_if_fail (widget != NULL);
1155   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
1156   g_return_if_fail (allocation != NULL);
1157
1158   widget->allocation = *allocation;
1159   if (GTK_WIDGET_REALIZED (widget))
1160     gdk_window_move_resize (widget->window,
1161                             allocation->x, allocation->y,
1162                             allocation->width, allocation->height);
1163
1164   notebook = GTK_NOTEBOOK (widget);
1165   if (notebook->children)
1166     {
1167       child_allocation.x = GTK_CONTAINER (widget)->border_width;
1168       child_allocation.y = GTK_CONTAINER (widget)->border_width;
1169       child_allocation.width = MAX (1, allocation->width - child_allocation.x * 2);
1170       child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
1171
1172       if (notebook->show_tabs || notebook->show_border)
1173         {
1174           child_allocation.x += widget->style->klass->xthickness;
1175           child_allocation.y += widget->style->klass->ythickness;
1176           child_allocation.width = MAX (1, 
1177               child_allocation.width - widget->style->klass->xthickness * 2);
1178           child_allocation.height = MAX (1, 
1179               child_allocation.height - widget->style->klass->ythickness * 2);
1180
1181           if (notebook->show_tabs && notebook->children)
1182             {
1183               switch (notebook->tab_pos)
1184                 {
1185                 case GTK_POS_TOP:
1186                   child_allocation.y += notebook->cur_page->requisition.height;
1187                 case GTK_POS_BOTTOM:
1188                   child_allocation.height = MAX (1, 
1189                     child_allocation.height - notebook->cur_page->requisition.height);
1190                   break;
1191                 case GTK_POS_LEFT:
1192                   child_allocation.x += notebook->cur_page->requisition.width;
1193                 case GTK_POS_RIGHT:
1194                   child_allocation.width = MAX (1, 
1195                     child_allocation.width - notebook->cur_page->requisition.width);
1196                   break;
1197                 }
1198             }
1199         }
1200
1201       children = notebook->children;
1202       while (children)
1203         {
1204           page = children->data;
1205           children = children->next;
1206           
1207           if (GTK_WIDGET_VISIBLE (page->child))
1208             gtk_widget_size_allocate (page->child, &child_allocation);
1209         }
1210
1211       gtk_notebook_pages_allocate (notebook, allocation);
1212     }
1213 }
1214
1215 static void
1216 gtk_notebook_paint (GtkWidget    *widget,
1217                     GdkRectangle *area)
1218 {
1219   GtkNotebook *notebook;
1220   GtkNotebookPage *page;
1221   GList *children;
1222   GdkPoint points[6];
1223   gint width, height;
1224   gint x, y;
1225   gint showarrow;
1226
1227   g_return_if_fail (widget != NULL);
1228   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
1229   g_return_if_fail (area != NULL);
1230
1231   if (GTK_WIDGET_DRAWABLE (widget))
1232     {
1233       notebook = GTK_NOTEBOOK (widget);
1234
1235       gdk_window_clear_area (widget->window,
1236                              area->x, area->y,
1237                              area->width, area->height);
1238
1239       if (notebook->show_tabs || notebook->show_border)
1240         {
1241           x = GTK_CONTAINER (widget)->border_width;
1242           y = GTK_CONTAINER (widget)->border_width;
1243           width = widget->allocation.width - x * 2;
1244           height = widget->allocation.height - y * 2;
1245
1246           if (notebook->show_tabs && notebook->children)
1247             {
1248
1249               if (!GTK_WIDGET_MAPPED (notebook->cur_page->tab_label))
1250                 {
1251                   GtkNotebookPage *page;
1252
1253                   page = notebook->first_tab->data;
1254
1255                   switch (notebook->tab_pos)
1256                     {
1257                     case GTK_POS_TOP:
1258                       y += page->allocation.height +
1259                         widget->style->klass->ythickness;
1260                     case GTK_POS_BOTTOM:
1261                       height -= page->allocation.height +
1262                         widget->style->klass->ythickness;
1263                       break;
1264                     case GTK_POS_LEFT:
1265                       x += page->allocation.width +
1266                         widget->style->klass->xthickness;
1267                     case GTK_POS_RIGHT:
1268                       width -= page->allocation.width +
1269                         widget->style->klass->xthickness;
1270                     break;
1271                     }
1272                   gtk_draw_shadow (widget->style, widget->window,
1273                                    GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1274                                    x, y, width, height);
1275                 }
1276               else
1277                 {
1278                   switch (notebook->tab_pos)
1279                     {
1280                     case GTK_POS_TOP:
1281                       y += notebook->cur_page->allocation.height;
1282                     case GTK_POS_BOTTOM:
1283                       height -= notebook->cur_page->allocation.height;
1284                       break;
1285                     case GTK_POS_LEFT:
1286                       x += notebook->cur_page->allocation.width;
1287                     case GTK_POS_RIGHT:
1288                       width -= notebook->cur_page->allocation.width;
1289                       break;
1290                     }
1291
1292                   switch (notebook->tab_pos)
1293                     {
1294                     case GTK_POS_TOP:
1295                       points[0].x = notebook->cur_page->allocation.x;
1296                       points[0].y = y;
1297                       points[1].x = x;
1298                       points[1].y = y;
1299                       points[2].x = x;
1300                       points[2].y = y + height - 1;
1301                       points[3].x = x + width - 1;
1302                       points[3].y = y + height - 1;
1303                       points[4].x = x + width - 1;
1304                       points[4].y = y;
1305                       points[5].x = (notebook->cur_page->allocation.x +
1306                                      notebook->cur_page->allocation.width -
1307                                      widget->style->klass->xthickness);
1308                       points[5].y = y;
1309
1310                       if (points[5].x == (x + width))
1311                         points[5].x -= 1;
1312                       break;
1313                     case GTK_POS_BOTTOM:
1314                       points[0].x = (notebook->cur_page->allocation.x +
1315                                      notebook->cur_page->allocation.width -
1316                                      widget->style->klass->xthickness);
1317                       points[0].y = y + height - 1;
1318                       points[1].x = x + width - 1;
1319                       points[1].y = y + height - 1;
1320                       points[2].x = x + width - 1;
1321                       points[2].y = y;
1322                       points[3].x = x;
1323                       points[3].y = y;
1324                       points[4].x = x;
1325                       points[4].y = y + height - 1;
1326                       points[5].x = notebook->cur_page->allocation.x;
1327                       points[5].y = y + height - 1;
1328
1329                       if (points[0].x == (x + width))
1330                         points[0].x -= 1;
1331                       break;
1332                     case GTK_POS_LEFT:
1333                       points[0].x = x;
1334                       points[0].y = (notebook->cur_page->allocation.y +
1335                                      notebook->cur_page->allocation.height -
1336                                      widget->style->klass->ythickness);
1337                       points[1].x = x;
1338                       points[1].y = y + height - 1;
1339                       points[2].x = x + width - 1;
1340                       points[2].y = y + height - 1;
1341                       points[3].x = x + width - 1;
1342                       points[3].y = y;
1343                       points[4].x = x;
1344                       points[4].y = y;
1345                       points[5].x = x;
1346                       points[5].y = notebook->cur_page->allocation.y;
1347                       
1348                       if (points[0].y == (y + height))
1349                         points[0].y -= 1;
1350                       break;
1351                     case GTK_POS_RIGHT:
1352                       points[0].x = x + width - 1;
1353                       points[0].y = notebook->cur_page->allocation.y;
1354                       points[1].x = x + width - 1;
1355                       points[1].y = y;
1356                       points[2].x = x;
1357                       points[2].y = y;
1358                       points[3].x = x;
1359                       points[3].y = y + height - 1;
1360                       points[4].x = x + width - 1;
1361                       points[4].y = y + height - 1;
1362                       points[5].x = x + width - 1;
1363                       points[5].y = (notebook->cur_page->allocation.y +
1364                                      notebook->cur_page->allocation.height -
1365                                      widget->style->klass->ythickness);
1366
1367                       if (points[5].y == (y + height))
1368                         points[5].y -= 1;
1369                       break;
1370                     }
1371                   
1372                   gtk_draw_polygon (widget->style, widget->window,
1373                                     GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1374                                     points, 6, FALSE);
1375                 }
1376               children = g_list_last (notebook->children);
1377               showarrow = FALSE;
1378
1379               while (children)
1380                 {
1381                   page = children->data;
1382                   children = children->prev;
1383
1384                   if (!GTK_WIDGET_MAPPED (page->tab_label))
1385                     showarrow = TRUE;
1386                   else if (notebook->cur_page != page)
1387                     gtk_notebook_draw_tab (notebook, page, area);
1388                 }
1389
1390               if (showarrow && notebook->scrollable && notebook->show_tabs) 
1391                 {
1392                   gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
1393                   gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
1394                 }
1395               if (notebook->cur_page && 
1396                   GTK_WIDGET_MAPPED(((GtkNotebookPage *)
1397                                      (notebook->cur_page))->tab_label))
1398                 gtk_notebook_draw_tab (notebook, notebook->cur_page, area);
1399             }
1400           else if (notebook->show_border)
1401             {
1402               gtk_draw_shadow (widget->style, widget->window,
1403                                GTK_STATE_NORMAL, GTK_SHADOW_OUT,
1404                                x, y, width, height);
1405             }
1406         }
1407     }
1408 }
1409
1410 static void
1411 gtk_notebook_draw (GtkWidget    *widget,
1412                    GdkRectangle *area)
1413 {
1414   GtkNotebook *notebook;
1415   GdkRectangle child_area;
1416
1417   g_return_if_fail (widget != NULL);
1418   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
1419   g_return_if_fail (area != NULL);
1420
1421   if (GTK_WIDGET_DRAWABLE (widget))
1422     {
1423       notebook = GTK_NOTEBOOK (widget);
1424
1425       gtk_notebook_paint (widget, area);
1426       gtk_widget_draw_focus (widget);
1427
1428       if (notebook->cur_page &&
1429           gtk_widget_intersect (notebook->cur_page->child, area, &child_area))
1430         gtk_widget_draw (notebook->cur_page->child, &child_area);
1431     }
1432 }
1433
1434 static gint
1435 gtk_notebook_expose (GtkWidget      *widget,
1436                      GdkEventExpose *event)
1437 {
1438   GtkNotebook *notebook;
1439   GdkEventExpose child_event;
1440
1441   g_return_val_if_fail (widget != NULL, FALSE);
1442   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1443   g_return_val_if_fail (event != NULL, FALSE);
1444
1445   if (GTK_WIDGET_DRAWABLE (widget))
1446     {
1447       notebook = GTK_NOTEBOOK (widget);
1448
1449       gtk_notebook_paint (widget, &event->area);
1450       gtk_widget_draw_focus (widget);
1451
1452       child_event = *event;
1453       if (notebook->cur_page && 
1454           GTK_WIDGET_NO_WINDOW (notebook->cur_page->child) &&
1455           gtk_widget_intersect (notebook->cur_page->child, &event->area, 
1456                                 &child_event.area))
1457         gtk_widget_event (notebook->cur_page->child, (GdkEvent*) &child_event);
1458     }
1459
1460   return FALSE;
1461 }
1462
1463 static gint
1464 gtk_notebook_button_press (GtkWidget      *widget,
1465                            GdkEventButton *event)
1466 {
1467   GtkNotebook *notebook;
1468   GtkNotebookPage *page;
1469   GList *children;
1470   gint num;
1471
1472   g_return_val_if_fail (widget != NULL, FALSE);
1473   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1474   g_return_val_if_fail (event != NULL, FALSE);
1475
1476   notebook = GTK_NOTEBOOK (widget);
1477
1478   if (event->type != GDK_BUTTON_PRESS || !notebook->children 
1479       || notebook->button)
1480     return FALSE;
1481
1482   if (!GTK_WIDGET_HAS_FOCUS (widget) && !(GTK_CONTAINER (widget)->focus_child))
1483     gtk_widget_grab_focus (widget);
1484   
1485   if (event->window == notebook->panel)
1486     {
1487       if (!GTK_WIDGET_HAS_FOCUS (widget))
1488         {
1489           GTK_CONTAINER (widget)->focus_child = NULL;
1490           gtk_widget_grab_focus (widget);
1491         }
1492
1493       gtk_grab_add (widget);
1494       notebook->button = event->button;
1495       
1496       if (event->x <= ARROW_SIZE + ARROW_SPACING / 2)
1497         {
1498           notebook->click_child = GTK_ARROW_LEFT;
1499           if (event->button == 1)
1500             {
1501               gtk_container_focus (GTK_CONTAINER (notebook), GTK_DIR_LEFT);
1502               if (!notebook->timer)
1503                 {
1504                   notebook->timer = gtk_timeout_add 
1505                     (NOTEBOOK_INIT_SCROLL_DELAY, 
1506                      (GtkFunction) gtk_notebook_timer, (gpointer) notebook);
1507                   notebook->need_timer = TRUE;
1508                 }
1509             }
1510           else if (event->button == 2)
1511             gtk_notebook_page_select (GTK_NOTEBOOK (widget));
1512           else if (event->button == 3)
1513             gtk_notebook_switch_focus_tab (notebook, notebook->children);
1514           gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
1515         }
1516       else
1517         {
1518           notebook->click_child = GTK_ARROW_RIGHT;
1519           if (event->button == 1)
1520             {
1521               gtk_container_focus (GTK_CONTAINER (notebook), GTK_DIR_RIGHT);
1522               if (!notebook->timer)
1523                 {
1524                   notebook->timer = gtk_timeout_add 
1525                     (NOTEBOOK_INIT_SCROLL_DELAY, 
1526                      (GtkFunction) gtk_notebook_timer, (gpointer) notebook);
1527                   notebook->need_timer = TRUE;
1528                 }
1529             }      
1530           else if (event->button == 2)
1531             gtk_notebook_page_select (GTK_NOTEBOOK (widget));
1532           else if (event->button == 3)
1533             gtk_notebook_switch_focus_tab (notebook, 
1534                                            g_list_last (notebook->children));
1535           gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
1536         }
1537     }
1538   else if (event->window == widget->window)
1539     {
1540       if (event->button == 3 && notebook->menu)
1541         {
1542           gtk_menu_popup (GTK_MENU (notebook->menu), NULL, NULL, 
1543                           NULL, NULL, 3, event->time);
1544           return FALSE;
1545         }
1546
1547       num = 0;
1548       children = notebook->children;
1549       while (children)
1550         {
1551           page = children->data;
1552           
1553           if (GTK_WIDGET_VISIBLE (page->child) &&
1554               page->tab_label && GTK_WIDGET_MAPPED (page->tab_label) &&
1555               (event->x >= page->allocation.x) &&
1556               (event->y >= page->allocation.y) &&
1557               (event->x <= (page->allocation.x + page->allocation.width)) &&
1558               (event->y <= (page->allocation.y + page->allocation.height)))
1559             {
1560               GTK_CONTAINER (notebook)->focus_child = NULL;
1561
1562               if (page == notebook->cur_page &&
1563                   notebook->focus_tab != children && 
1564                   GTK_WIDGET_HAS_FOCUS (notebook))
1565                 {
1566                   GtkNotebookPage *old_page;
1567
1568                   old_page = (GtkNotebookPage *)
1569                               (notebook->focus_tab->data);
1570                   notebook->focus_tab = children;
1571                   gtk_notebook_focus_changed (notebook, old_page);
1572                 }
1573               else
1574                 {
1575                   notebook->focus_tab = children;
1576                   gtk_notebook_switch_page (notebook, page, num);
1577                   gtk_widget_grab_focus (widget);
1578                 }
1579               break;
1580             }
1581           children = children->next;
1582           num++;
1583         }
1584     }
1585   return FALSE;
1586 }
1587
1588 static gint
1589 gtk_notebook_button_release (GtkWidget      *widget,
1590                              GdkEventButton *event)
1591 {
1592   GtkNotebook *notebook;
1593
1594   g_return_val_if_fail (widget != NULL, FALSE);
1595   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1596   g_return_val_if_fail (event != NULL, FALSE);
1597
1598   if (event->type != GDK_BUTTON_RELEASE)
1599     return FALSE;
1600
1601   notebook = GTK_NOTEBOOK (widget);
1602
1603   if (event->button == notebook->button)
1604     {
1605       guint click_child;
1606
1607       if (notebook->timer)
1608         {
1609           gtk_timeout_remove (notebook->timer);
1610           notebook->timer = 0;
1611           notebook->need_timer = FALSE;
1612         }
1613       gtk_grab_remove (widget);
1614       click_child = notebook->click_child;
1615       notebook->click_child = 0;
1616       notebook->button = 0;
1617       gtk_notebook_draw_arrow (notebook, click_child);
1618       
1619     }
1620   return FALSE;
1621 }
1622
1623 static gint
1624 gtk_notebook_enter_notify (GtkWidget        *widget,
1625                            GdkEventCrossing *event)
1626 {
1627   GtkNotebook *notebook;
1628
1629   g_return_val_if_fail (widget != NULL, FALSE);
1630   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1631   g_return_val_if_fail (event != NULL, FALSE);
1632
1633   notebook = GTK_NOTEBOOK (widget);
1634
1635   if (event->window == notebook->panel)
1636     {
1637       gint x;
1638       gint y;
1639
1640       gdk_window_get_pointer (notebook->panel, &x, &y, NULL);
1641
1642       if (x <= ARROW_SIZE + ARROW_SPACING / 2)
1643         {
1644           notebook->in_child = GTK_ARROW_LEFT;
1645
1646           if (notebook->click_child == 0) 
1647             gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
1648         }
1649       else 
1650         {
1651           notebook->in_child = GTK_ARROW_RIGHT;
1652
1653           if (notebook->click_child == 0) 
1654             gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
1655         }
1656     }
1657
1658   return FALSE;
1659 }
1660
1661 static gint
1662 gtk_notebook_leave_notify (GtkWidget        *widget,
1663                            GdkEventCrossing *event)
1664 {
1665   GtkNotebook *notebook;
1666
1667   g_return_val_if_fail (widget != NULL, FALSE);
1668   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1669   g_return_val_if_fail (event != NULL, FALSE);
1670
1671   notebook = GTK_NOTEBOOK (widget);
1672
1673   if (event->window == notebook->panel && !notebook->click_child)
1674     {
1675       if (notebook->in_child == GTK_ARROW_LEFT)
1676         {
1677           notebook->in_child = 0;
1678           gtk_notebook_draw_arrow (notebook,GTK_ARROW_LEFT);
1679         }
1680       else
1681         {
1682           notebook->in_child = 0;
1683           gtk_notebook_draw_arrow (notebook,GTK_ARROW_RIGHT);
1684         }
1685     }
1686   return FALSE;
1687 }
1688
1689 static gint
1690 gtk_notebook_motion_notify (GtkWidget      *widget,
1691                             GdkEventMotion *event)
1692 {
1693   GtkNotebook *notebook;
1694
1695   g_return_val_if_fail (widget != NULL, FALSE);
1696   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
1697   g_return_val_if_fail (event != NULL, FALSE);
1698
1699   notebook = GTK_NOTEBOOK (widget);
1700   
1701   if (notebook->button)
1702     return FALSE;
1703
1704   if (event->window == notebook->panel)
1705     {
1706       gint x;
1707       
1708       x = event->x;
1709       if (event->is_hint)
1710         gdk_window_get_pointer (notebook->panel, &x, NULL, NULL);
1711
1712       if (x <= ARROW_SIZE + ARROW_SPACING / 2 && 
1713           notebook->in_child == GTK_ARROW_RIGHT)
1714         {
1715           notebook->in_child = GTK_ARROW_LEFT;
1716           gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
1717           gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
1718         }
1719       else if (x > ARROW_SIZE + ARROW_SPACING / 2 && 
1720                notebook->in_child == GTK_ARROW_LEFT)
1721         {
1722           notebook->in_child = GTK_ARROW_RIGHT;
1723           gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
1724           gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
1725         }
1726       return FALSE;
1727     }
1728   return FALSE;
1729 }
1730
1731 static gint
1732 gtk_notebook_timer (GtkNotebook *notebook)
1733 {
1734   g_return_val_if_fail (notebook != NULL, FALSE);
1735   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
1736
1737   if (notebook->timer)
1738     {
1739       if (notebook->click_child == GTK_ARROW_LEFT)
1740         gtk_container_focus (GTK_CONTAINER (notebook), GTK_DIR_LEFT);
1741       else if (notebook->click_child == GTK_ARROW_RIGHT)
1742         gtk_container_focus (GTK_CONTAINER (notebook), GTK_DIR_RIGHT);
1743       
1744       if (notebook->need_timer) 
1745         {
1746           notebook->need_timer = FALSE;
1747           notebook->timer = gtk_timeout_add 
1748             (NOTEBOOK_SCROLL_DELAY, (GtkFunction) gtk_notebook_timer, 
1749              (gpointer) notebook);
1750           return FALSE;
1751         }
1752       return TRUE;
1753     }
1754   return FALSE;
1755 }
1756
1757 static void
1758 gtk_notebook_draw_arrow (GtkNotebook *notebook, guint arrow)
1759 {
1760   GtkStateType state_type;
1761   GtkShadowType shadow_type;
1762   GtkWidget *widget;
1763
1764   g_return_if_fail (notebook != NULL);
1765   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
1766   
1767   widget = GTK_WIDGET(notebook);
1768
1769   if (GTK_WIDGET_DRAWABLE (notebook))
1770     {
1771       if (notebook->in_child == arrow)
1772         {
1773           if (notebook->click_child == arrow)
1774             state_type = GTK_STATE_ACTIVE;
1775           else
1776             state_type = GTK_STATE_PRELIGHT;
1777         }
1778       else
1779         state_type = GTK_STATE_NORMAL;
1780
1781       if (notebook->click_child == arrow)
1782         shadow_type = GTK_SHADOW_IN;
1783       else
1784         shadow_type = GTK_SHADOW_OUT;
1785
1786       if (arrow == GTK_ARROW_LEFT)
1787         {
1788           if (notebook->tab_pos == GTK_POS_LEFT ||
1789               notebook->tab_pos == GTK_POS_RIGHT)
1790             arrow = GTK_ARROW_UP;
1791           gtk_draw_arrow (widget->style, notebook->panel, state_type, 
1792                           shadow_type, arrow, TRUE, 
1793                           0, 0, ARROW_SIZE, ARROW_SIZE);
1794         }
1795       else
1796         {
1797           if (notebook->tab_pos == GTK_POS_LEFT ||
1798               notebook->tab_pos == GTK_POS_RIGHT)
1799             arrow = GTK_ARROW_DOWN;
1800           gtk_draw_arrow (widget->style, notebook->panel, state_type, 
1801                           shadow_type, arrow, TRUE, ARROW_SIZE + ARROW_SPACING,
1802                           0, ARROW_SIZE, ARROW_SIZE);
1803         }
1804     }
1805 }
1806
1807 static void
1808 gtk_real_notebook_switch_page (GtkNotebook     *notebook,
1809                                GtkNotebookPage *page)
1810 {
1811   g_return_if_fail (notebook != NULL);
1812   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
1813   g_return_if_fail (page != NULL);
1814
1815   if (notebook->cur_page == page)
1816     return;
1817
1818   if (notebook->cur_page && GTK_WIDGET_MAPPED (notebook->cur_page->child))
1819     gtk_widget_unmap (notebook->cur_page->child);
1820   
1821   notebook->cur_page = page;
1822
1823   if (notebook->focus_tab->data != (gpointer) notebook->cur_page)
1824     notebook->focus_tab = 
1825       g_list_find (notebook->children, notebook->cur_page);
1826
1827   gtk_notebook_pages_allocate (notebook, &GTK_WIDGET (notebook)->allocation);
1828
1829   if (GTK_WIDGET_MAPPED (notebook))
1830     {
1831       if (GTK_WIDGET_REALIZED (notebook->cur_page->child))
1832         gtk_widget_map (notebook->cur_page->child);
1833       else
1834         {
1835           gtk_widget_map (notebook->cur_page->child);
1836           gtk_widget_size_allocate (GTK_WIDGET (notebook), 
1837                                     &GTK_WIDGET (notebook)->allocation);
1838         }
1839     }
1840   
1841   if (GTK_WIDGET_DRAWABLE (notebook))
1842     gtk_widget_queue_draw (GTK_WIDGET (notebook));
1843 }
1844
1845 static void
1846 gtk_notebook_draw_tab (GtkNotebook     *notebook,
1847                        GtkNotebookPage *page,
1848                        GdkRectangle    *area)
1849 {
1850   GdkRectangle child_area;
1851   GdkRectangle page_area;
1852   GtkStateType state_type;
1853   GdkPoint points[6];
1854   gint n = 0;
1855  
1856   g_return_if_fail (notebook != NULL);
1857   g_return_if_fail (page != NULL);
1858   g_return_if_fail (area != NULL);
1859
1860   if (!GTK_WIDGET_MAPPED (page->tab_label))
1861     return;
1862
1863   page_area.x = page->allocation.x;
1864   page_area.y = page->allocation.y;
1865   page_area.width = page->allocation.width;
1866   page_area.height = page->allocation.height;
1867
1868   if (gdk_rectangle_intersect (&page_area, area, &child_area))
1869     {
1870       GtkWidget *widget;
1871
1872       switch (notebook->tab_pos)
1873         {
1874         case GTK_POS_TOP:
1875           if (child_area.x + child_area.width > 
1876               page->allocation.x + page->allocation.width - TAB_OVERLAP) 
1877             {
1878               points[0].x = page->allocation.x + page->allocation.width - 1;
1879               points[0].y = page->allocation.y + page->allocation.height - 1;
1880
1881               points[1].x = page->allocation.x + page->allocation.width - 1;
1882               points[1].y = page->allocation.y + TAB_CURVATURE;
1883
1884               points[2].x = page->allocation.x + page->allocation.width 
1885                 - TAB_CURVATURE - 1;
1886               points[2].y = page->allocation.y;
1887               n = 3;
1888             }
1889           else 
1890             {
1891               points[0].x = page->allocation.x + page->allocation.width 
1892                 - TAB_OVERLAP - 1;
1893               points[0].y = page->allocation.y;
1894               n = 1;
1895             }
1896           
1897           if ( (child_area.x < page->allocation.x + TAB_OVERLAP) &&
1898                (page == notebook->cur_page || 
1899                 page == (GtkNotebookPage *)(notebook->children->data) ||
1900                 (notebook->scrollable && 
1901                  page == (GtkNotebookPage *)(notebook->first_tab->data))) ) 
1902             {
1903               points[n].x = page->allocation.x + TAB_CURVATURE;
1904               points[n++].y = page->allocation.y;
1905             
1906               points[n].x = page->allocation.x;
1907               points[n++].y = page->allocation.y + TAB_CURVATURE;
1908
1909               points[n].x = page->allocation.x;
1910               points[n++].y = page->allocation.y + page->allocation.height - 1;
1911             }
1912           else 
1913             {
1914               points[n].x = page->allocation.x + TAB_OVERLAP;
1915               points[n++].y = page->allocation.y;
1916             }
1917           break;
1918         case GTK_POS_BOTTOM:
1919           if ( (child_area.x < page->allocation.x + TAB_OVERLAP) &&
1920                (page == notebook->cur_page || 
1921                 page == (GtkNotebookPage *)(notebook->children->data) ||
1922                 (notebook->scrollable && 
1923                  page == (GtkNotebookPage *)(notebook->first_tab->data))) ) 
1924             {
1925               points[0].x = page->allocation.x;
1926               points[0].y = page->allocation.y;
1927
1928               points[1].x = page->allocation.x;
1929               points[1].y = page->allocation.y + page->allocation.height 
1930                 - TAB_CURVATURE - 1;
1931
1932               points[2].x = page->allocation.x + TAB_CURVATURE;
1933               points[2].y = page->allocation.y + page->allocation.height - 1;
1934               n = 3;
1935             }
1936           else 
1937             {
1938               points[0].x = page->allocation.x + TAB_OVERLAP;
1939               points[0].y = page->allocation.y + page->allocation.height - 1;
1940               n = 1;
1941             }
1942
1943           if (child_area.x + child_area.width > 
1944               page->allocation.x + page->allocation.width - TAB_OVERLAP)
1945             {
1946               points[n].x = page->allocation.x + page->allocation.width 
1947                 - TAB_CURVATURE - 1;
1948               points[n++].y = page->allocation.y + page->allocation.height - 1;
1949
1950               points[n].x = page->allocation.x + page->allocation.width - 1;
1951               points[n++].y = page->allocation.y + page->allocation.height 
1952                 - TAB_CURVATURE - 1;
1953             
1954               points[n].x = page->allocation.x + page->allocation.width - 1;
1955               points[n++].y = page->allocation.y;
1956             }
1957           else 
1958             {
1959               points[n].x = page->allocation.x + page->allocation.width 
1960                 - TAB_OVERLAP - 1;
1961               points[n++].y = page->allocation.y + page->allocation.height - 1;
1962             }
1963           break;
1964         case GTK_POS_LEFT:
1965           if ( (child_area.y < page->allocation.y + TAB_OVERLAP) &&
1966                (page == notebook->cur_page || 
1967                 page == (GtkNotebookPage *)(notebook->children->data) ||
1968                 (notebook->scrollable && 
1969                  page == (GtkNotebookPage *)(notebook->first_tab->data))) )
1970             {
1971               points[0].x = page->allocation.x + page->allocation.width - 1;
1972               points[0].y = page->allocation.y;
1973               
1974               points[1].x = page->allocation.x + TAB_CURVATURE;
1975               points[1].y = page->allocation.y;
1976
1977               points[2].x = page->allocation.x;
1978               points[2].y = page->allocation.y + TAB_CURVATURE;
1979               n = 3;
1980             }
1981           else 
1982             {
1983               points[0].x = page->allocation.x;
1984               points[0].y = page->allocation.y + TAB_OVERLAP;
1985               n = 1;
1986             }
1987
1988           if (child_area.y + child_area.height > 
1989               page->allocation.y + page->allocation.height - TAB_OVERLAP) 
1990             {
1991               points[n].x = page->allocation.x;
1992               points[n++].y = page->allocation.y + page->allocation.height 
1993                 - TAB_CURVATURE - 1;
1994
1995               points[n].x = page->allocation.x + TAB_CURVATURE;
1996               points[n++].y = page->allocation.y + page->allocation.height - 1;
1997
1998               points[n].x = page->allocation.x + page->allocation.width - 1;
1999               points[n++].y = page->allocation.y + page->allocation.height - 1;
2000             }
2001           else 
2002             {
2003               points[n].x = page->allocation.x;
2004               points[n++].y = page->allocation.y + page->allocation.height 
2005                 - TAB_OVERLAP - 1;
2006             }
2007           break;
2008         case GTK_POS_RIGHT:
2009           if (child_area.y + child_area.height > 
2010               page->allocation.y + page->allocation.height - TAB_OVERLAP) 
2011             {
2012               points[0].x = page->allocation.x;
2013               points[0].y = page->allocation.y + page->allocation.height - 1;
2014
2015               points[1].x = page->allocation.x + page->allocation.width 
2016                 - TAB_CURVATURE - 1;
2017               points[1].y = page->allocation.y + page->allocation.height - 1;
2018               
2019               points[2].x = page->allocation.x + page->allocation.width - 1;
2020               points[2].y = page->allocation.y + page->allocation.height 
2021                 - TAB_CURVATURE - 1;
2022               n = 3;
2023             }
2024           else 
2025             {
2026               points[0].x = page->allocation.x + page->allocation.width - 1;
2027               points[0].y = page->allocation.y + page->allocation.height 
2028                 - TAB_OVERLAP - 1;
2029               n = 1;
2030             }
2031
2032           if ( (child_area.y < page->allocation.y + TAB_OVERLAP) && 
2033                (page == notebook->cur_page || 
2034                 page == (GtkNotebookPage *)(notebook->children->data) ||
2035                 (notebook->scrollable && 
2036                  page == (GtkNotebookPage *)(notebook->first_tab->data))) ) 
2037             {
2038               points[n].x = page->allocation.x + page->allocation.width - 1;
2039               points[n++].y = page->allocation.y + TAB_CURVATURE;
2040
2041               points[n].x = page->allocation.x + page->allocation.width 
2042                 - TAB_CURVATURE - 1;
2043               points[n++].y = page->allocation.y;
2044
2045               points[n].x = page->allocation.x;
2046               points[n++].y = page->allocation.y;
2047             }
2048           else 
2049             {
2050               points[n].x = page->allocation.x + page->allocation.width - 1;
2051               points[n++].y = page->allocation.y + TAB_OVERLAP;
2052             }
2053           break;
2054         }
2055
2056       widget = GTK_WIDGET(notebook);
2057
2058       if (notebook->cur_page == page)
2059         {
2060           state_type = GTK_STATE_NORMAL;
2061         }
2062       else 
2063         {
2064           state_type = GTK_STATE_ACTIVE;
2065           gdk_draw_rectangle (widget->window, widget->style->bg_gc[state_type],
2066                               TRUE, child_area.x, child_area.y,
2067                               child_area.width, child_area.height);
2068         }
2069       
2070       gtk_draw_polygon (widget->style, widget->window, state_type, 
2071                         GTK_SHADOW_OUT, points, n, FALSE);
2072
2073       if (gtk_widget_intersect (page->tab_label, area, &child_area))
2074         gtk_widget_draw (page->tab_label, &child_area);
2075     }
2076 }
2077
2078 static gint
2079 gtk_notebook_focus_in (GtkWidget     *widget,
2080                        GdkEventFocus *event)
2081 {
2082   g_return_val_if_fail (widget != NULL, FALSE);
2083   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
2084   g_return_val_if_fail (event != NULL, FALSE);
2085
2086   if (GTK_CONTAINER (widget)->focus_child)
2087     {
2088       if (gtk_notebook_page_select (GTK_NOTEBOOK (widget)))
2089         return FALSE;
2090       else
2091         GTK_CONTAINER (widget)->focus_child = NULL;
2092     }
2093
2094   GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2095   gtk_widget_draw_focus (widget);
2096
2097   return FALSE;
2098 }
2099
2100 static gint
2101 gtk_notebook_focus_out (GtkWidget     *widget,
2102                         GdkEventFocus *event)
2103 {
2104   g_return_val_if_fail (widget != NULL, FALSE);
2105   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
2106   g_return_val_if_fail (event != NULL, FALSE);
2107
2108   GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2109   gtk_widget_draw_focus (widget);
2110
2111   return FALSE;
2112 }
2113
2114 static void
2115 gtk_notebook_draw_focus (GtkWidget *widget)
2116 {
2117   GtkNotebook *notebook;
2118
2119   g_return_if_fail (widget != NULL);
2120   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
2121
2122   notebook = GTK_NOTEBOOK (widget);
2123
2124   if (GTK_WIDGET_DRAWABLE (widget) && notebook->show_tabs &&
2125       notebook->focus_tab)
2126     {
2127       GtkNotebookPage *page;
2128       GdkGC *gc;
2129
2130       page = notebook->focus_tab->data;
2131
2132       if (GTK_WIDGET_HAS_FOCUS (widget))
2133         gc = widget->style->black_gc;
2134       else if (page == notebook->cur_page)
2135         gc = widget->style->bg_gc[GTK_STATE_NORMAL];
2136       else
2137         gc = widget->style->bg_gc[GTK_STATE_ACTIVE];
2138
2139       gdk_draw_rectangle (widget->window, 
2140                           gc, FALSE, 
2141                           page->tab_label->allocation.x - 1, 
2142                           page->tab_label->allocation.y - 1,
2143                           page->tab_label->allocation.width + 1, 
2144                           page->tab_label->allocation.height + 1);
2145     }
2146 }
2147
2148 static void
2149 gtk_notebook_focus_changed (GtkNotebook *notebook, GtkNotebookPage *old_page)
2150 {
2151   GtkWidget *widget;
2152
2153   g_return_if_fail (notebook != NULL);
2154   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2155   g_return_if_fail (old_page != NULL);
2156
2157   widget = GTK_WIDGET (notebook);
2158
2159   if (GTK_WIDGET_DRAWABLE (widget) && notebook->show_tabs &&
2160       notebook->focus_tab)
2161     {
2162       GtkNotebookPage *page;
2163       GdkGC *gc;
2164
2165       page = notebook->focus_tab->data;
2166
2167       if (GTK_WIDGET_HAS_FOCUS (widget))
2168         gc = widget->style->black_gc;
2169       else if (page == notebook->cur_page)
2170         gc = widget->style->bg_gc[GTK_STATE_NORMAL];
2171       else
2172         gc = widget->style->bg_gc[GTK_STATE_ACTIVE];
2173
2174       gdk_draw_rectangle (widget->window, 
2175                           gc, FALSE, 
2176                           page->tab_label->allocation.x - 1, 
2177                           page->tab_label->allocation.y - 1,
2178                           page->tab_label->allocation.width + 1, 
2179                           page->tab_label->allocation.height + 1);
2180
2181       if (old_page == notebook->cur_page)
2182         gc = widget->style->bg_gc[GTK_STATE_NORMAL];
2183       else
2184         gc = widget->style->bg_gc[GTK_STATE_ACTIVE];
2185
2186       gdk_draw_rectangle (widget->window, 
2187                           gc, FALSE, 
2188                           old_page->tab_label->allocation.x - 1, 
2189                           old_page->tab_label->allocation.y - 1,
2190                           old_page->tab_label->allocation.width + 1, 
2191                           old_page->tab_label->allocation.height + 1);
2192     }
2193 }
2194
2195 static void 
2196 gtk_notebook_calc_tabs (GtkNotebook  *notebook, 
2197                         GList        *start, 
2198                         GList       **end,
2199                         gint         *tab_space,
2200                         guint         direction)
2201 {
2202   GtkNotebookPage *page = NULL;
2203   GList *children;
2204
2205   children = start;
2206   switch (notebook->tab_pos)
2207     {
2208     case GTK_POS_TOP:
2209     case GTK_POS_BOTTOM:
2210       while (children)
2211         {
2212           page = children->data;
2213           *tab_space -= page->requisition.width;
2214           if (*tab_space < 0 || children == *end)
2215             {
2216               if (*tab_space < 0) 
2217                 {
2218                   *tab_space = - (*tab_space + page->requisition.width);
2219                   *end = children;
2220                 }
2221               break;
2222             }
2223           if (direction == STEP_NEXT)
2224             children = children->next;
2225           else
2226             children = children->prev;
2227         }
2228       break;
2229     case GTK_POS_LEFT:
2230     case GTK_POS_RIGHT:
2231       while (children)
2232         {
2233           page = children->data;
2234           *tab_space -= page->requisition.height;
2235           if (*tab_space < 0 || children == *end)
2236             {
2237               if (*tab_space < 0)
2238                 {
2239                   *tab_space = - (*tab_space + page->requisition.height);
2240                   *end = children;
2241                 }
2242               break;
2243             }
2244           if (direction == STEP_NEXT)
2245             children = children->next;
2246           else
2247             children = children->prev;
2248         }
2249       break;
2250     }
2251 }
2252
2253 static void
2254 gtk_notebook_pages_allocate (GtkNotebook   *notebook,
2255                              GtkAllocation *allocation)
2256 {
2257   GtkWidget    *widget;
2258   GtkContainer *container;
2259   GtkNotebookPage *page = NULL;
2260   GtkAllocation child_allocation;
2261   GList *children;
2262   GList *last_child = NULL;
2263   gint showarrow = FALSE;
2264   gint tab_space = 0; 
2265   gint x = 0;
2266   gint y = 0;
2267   gint i;
2268   gint n = 1;
2269   gint old_fill = 0;
2270   gint new_fill = 0;
2271   
2272   if (!notebook->show_tabs || !notebook->children)
2273     return;
2274   
2275   widget = GTK_WIDGET (notebook);
2276   container = GTK_CONTAINER (notebook);
2277   
2278   child_allocation.x = container->border_width;
2279   child_allocation.y = container->border_width;
2280   
2281   switch (notebook->tab_pos)
2282     {
2283     case GTK_POS_BOTTOM:
2284       child_allocation.y = (allocation->height -
2285                             notebook->cur_page->requisition.height -
2286                             container->border_width);
2287     case GTK_POS_TOP:
2288       child_allocation.height = notebook->cur_page->requisition.height;
2289       break;
2290     case GTK_POS_RIGHT:
2291       child_allocation.x = (allocation->width -
2292                             notebook->cur_page->requisition.width -
2293                             container->border_width);
2294     case GTK_POS_LEFT:
2295       child_allocation.width = notebook->cur_page->requisition.width;
2296       break;
2297     }
2298   
2299   if (notebook->scrollable) 
2300     {
2301       children = notebook->children;
2302       switch (notebook->tab_pos)
2303         {
2304         case GTK_POS_TOP:
2305         case GTK_POS_BOTTOM:
2306           while (children)
2307             {
2308               page = children->data;
2309               children = children->next;
2310               tab_space += page->requisition.width;
2311             }
2312           if (tab_space > allocation->width - 2 * container->border_width - TAB_OVERLAP) 
2313             {
2314               showarrow = TRUE;
2315               page = notebook->focus_tab->data; 
2316               
2317               tab_space = (allocation->width - TAB_OVERLAP - page->requisition.width -
2318                            2 * (container->border_width + ARROW_SPACING + ARROW_SIZE));
2319               x = allocation->width - 2 * ARROW_SIZE - ARROW_SPACING - container->border_width;
2320               
2321               page = notebook->children->data;
2322               if (notebook->tab_pos == GTK_POS_TOP)
2323                 y = container->border_width + (page->requisition.height - ARROW_SIZE) / 2;
2324               else
2325                 y = (allocation->height - container->border_width - 
2326                      ARROW_SIZE - (page->requisition.height - ARROW_SIZE) / 2);
2327             }
2328           break;
2329         case GTK_POS_LEFT:
2330         case GTK_POS_RIGHT:
2331           while (children)
2332             {
2333               page = children->data;
2334               children = children->next;
2335               tab_space += page->requisition.height;
2336             }
2337           if (tab_space > (allocation->height - 2 * container->border_width - TAB_OVERLAP))
2338             {
2339               showarrow = TRUE;
2340               page = notebook->focus_tab->data; 
2341               tab_space = (allocation->height -
2342                            ARROW_SIZE - ARROW_SPACING - TAB_OVERLAP -
2343                            2 * container->border_width - page->requisition.height);
2344               y = allocation->height - container->border_width - ARROW_SIZE;  
2345               
2346               page = notebook->children->data;
2347               if (notebook->tab_pos == GTK_POS_LEFT)
2348                 x = (container->border_width +
2349                      (page->requisition.width - (2 * ARROW_SIZE - ARROW_SPACING)) / 2); 
2350               else
2351                 x = (allocation->width - container->border_width -
2352                      (2 * ARROW_SIZE -  ARROW_SPACING) -
2353                      (page->requisition.width - (2 * ARROW_SIZE - ARROW_SPACING)) / 2);
2354             }
2355           break;
2356         }
2357       if (showarrow) /* first_tab <- focus_tab */
2358         { 
2359           children = notebook->focus_tab->prev;
2360           while (children)
2361             {
2362               if (notebook->first_tab == children)
2363                 break;
2364               children = children->prev;
2365             }
2366           
2367           if (!children)
2368             notebook->first_tab = notebook->focus_tab;
2369           else
2370             gtk_notebook_calc_tabs (notebook, notebook->focus_tab->prev, 
2371                                     &(notebook->first_tab), &tab_space,
2372                                     STEP_PREV);
2373           if (tab_space <= 0)
2374             {
2375               notebook->first_tab = notebook->first_tab->next;
2376               if (!notebook->first_tab)
2377                 notebook->first_tab = notebook->focus_tab;
2378               last_child = notebook->focus_tab->next; 
2379             }
2380           else /* focus_tab -> end */   
2381             {
2382               if (!notebook->first_tab)
2383                 notebook->first_tab = notebook->children;
2384               
2385               children = NULL;
2386               gtk_notebook_calc_tabs (notebook, notebook->focus_tab->next, 
2387                                       &children, &tab_space, STEP_NEXT);
2388               
2389               if (tab_space <= 0) 
2390                 last_child = children;
2391               else /* start <- first_tab */
2392                 {
2393                   last_child = NULL;
2394                   children = NULL;
2395                   gtk_notebook_calc_tabs (notebook,notebook->first_tab->prev,
2396                                           &children, &tab_space, STEP_PREV);
2397                   if (children)
2398                     notebook->first_tab = children->next;
2399                   else
2400                     notebook->first_tab = notebook->children;
2401                 }
2402             }
2403           
2404           if (GTK_WIDGET_REALIZED (notebook))
2405             {
2406               gdk_window_move (notebook->panel, x, y);
2407               gdk_window_show (notebook->panel);
2408             }
2409           
2410           if (tab_space < 0) 
2411             {
2412               tab_space = -tab_space;
2413               n = 0;
2414               children = notebook->first_tab;
2415               while (children != last_child)
2416                 {
2417                   children = children->next;
2418                   n++;
2419                 }
2420             }
2421           else 
2422             tab_space = 0;
2423           
2424           children = notebook->children;
2425           while (children != notebook->first_tab)
2426             {
2427               page = children->data;
2428               children = children->next;
2429               
2430               if (page->tab_label && GTK_WIDGET_MAPPED (page->tab_label))
2431                 gtk_widget_unmap (page->tab_label);
2432               
2433             }
2434           children = last_child;
2435           while (children)
2436             {
2437               page = children->data;
2438               children = children->next;
2439               
2440               if (page->tab_label && GTK_WIDGET_MAPPED (page->tab_label))
2441                 gtk_widget_unmap (page->tab_label);
2442             }
2443         }
2444       else /* !showarrow */
2445         {
2446           notebook->first_tab = notebook->children;
2447           tab_space = 0;
2448           if (GTK_WIDGET_REALIZED (notebook))
2449             gdk_window_hide (notebook->panel);
2450         }
2451       children = notebook->first_tab;
2452     }
2453   else
2454     children = notebook->children;
2455   
2456   i = 1;
2457   while (children != last_child)
2458     {
2459       page = children->data;
2460       children = children->next;
2461       
2462       if (GTK_WIDGET_VISIBLE (page->child))
2463         {
2464           new_fill = (tab_space * i++) / n;
2465           switch (notebook->tab_pos)
2466             {
2467             case GTK_POS_TOP:
2468             case GTK_POS_BOTTOM:
2469               child_allocation.width = page->requisition.width + TAB_OVERLAP + new_fill - old_fill;
2470               break;
2471             case GTK_POS_LEFT:
2472             case GTK_POS_RIGHT:
2473               child_allocation.height = page->requisition.height + TAB_OVERLAP + new_fill - old_fill;
2474               break;
2475             }
2476           old_fill = new_fill;
2477           gtk_notebook_page_allocate (notebook, page, &child_allocation);
2478           
2479           switch (notebook->tab_pos)
2480             {
2481             case GTK_POS_TOP:
2482             case GTK_POS_BOTTOM:
2483               child_allocation.x += child_allocation.width - TAB_OVERLAP;
2484               break;
2485             case GTK_POS_LEFT:
2486             case GTK_POS_RIGHT:
2487               child_allocation.y += child_allocation.height - TAB_OVERLAP;
2488               break;
2489             }
2490           
2491           if (GTK_WIDGET_REALIZED (notebook) &&
2492               page->tab_label && !GTK_WIDGET_MAPPED (page->tab_label))
2493             gtk_widget_map (page->tab_label);
2494         }
2495     }
2496 }
2497
2498 static void
2499 gtk_notebook_page_allocate (GtkNotebook     *notebook,
2500                             GtkNotebookPage *page,
2501                             GtkAllocation   *allocation)
2502 {
2503   GtkAllocation child_allocation;
2504   gint xthickness, ythickness;
2505
2506   g_return_if_fail (notebook != NULL);
2507   g_return_if_fail (page != NULL);
2508   g_return_if_fail (allocation != NULL);
2509
2510   page->allocation = *allocation;
2511
2512   xthickness = GTK_WIDGET (notebook)->style->klass->xthickness;
2513   ythickness = GTK_WIDGET (notebook)->style->klass->ythickness;
2514
2515   if (notebook->cur_page != page)
2516     {
2517       switch (notebook->tab_pos)
2518         {
2519         case GTK_POS_TOP:
2520           page->allocation.y += ythickness;
2521         case GTK_POS_BOTTOM:
2522           page->allocation.height -= ythickness;
2523           break;
2524         case GTK_POS_LEFT:
2525           page->allocation.x += xthickness;
2526         case GTK_POS_RIGHT:
2527           page->allocation.width -= xthickness;
2528           break;
2529         }
2530     }
2531
2532   switch (notebook->tab_pos)
2533     {
2534     case GTK_POS_TOP:
2535       child_allocation.x = xthickness + notebook->tab_border;
2536       child_allocation.y = ythickness + notebook->tab_border + page->allocation.y;
2537       child_allocation.width = page->allocation.width - child_allocation.x * 2;
2538       child_allocation.height = page->allocation.height - ythickness - 2 * notebook->tab_border;
2539       child_allocation.x += page->allocation.x;
2540       break;
2541     case GTK_POS_BOTTOM:
2542       child_allocation.x = xthickness + notebook->tab_border;
2543       child_allocation.width = page->allocation.width - child_allocation.x * 2;
2544       child_allocation.height = page->allocation.height - ythickness - 2 * notebook->tab_border;
2545       child_allocation.x += page->allocation.x;
2546       child_allocation.y = page->allocation.y + notebook->tab_border;
2547       break;
2548     case GTK_POS_LEFT:
2549       child_allocation.x = xthickness + notebook->tab_border + page->allocation.x;
2550       child_allocation.y = ythickness + notebook->tab_border;
2551       child_allocation.width = page->allocation.width - xthickness - 2 * notebook->tab_border;
2552       child_allocation.height = page->allocation.height - child_allocation.y * 2;
2553       child_allocation.y += page->allocation.y;
2554       break;
2555     case GTK_POS_RIGHT:
2556       child_allocation.y = ythickness + notebook->tab_border;
2557       child_allocation.width = page->allocation.width - xthickness - 2 * notebook->tab_border;
2558       child_allocation.height = page->allocation.height - child_allocation.y * 2;
2559       child_allocation.x = page->allocation.x + notebook->tab_border;
2560       child_allocation.y += page->allocation.y;
2561       break;
2562     }
2563
2564   if (page->tab_label)
2565     gtk_widget_size_allocate (page->tab_label, &child_allocation);
2566 }
2567
2568 static void
2569 gtk_notebook_menu_switch_page (GtkWidget *widget,
2570                                GtkNotebookPage *page)
2571 {
2572   GtkNotebook *notebook;
2573   GList *children;
2574   gint page_num;
2575
2576   g_return_if_fail (widget != NULL);
2577   g_return_if_fail (page != NULL);
2578
2579   notebook = GTK_NOTEBOOK (gtk_menu_get_attach_widget 
2580                            (GTK_MENU (widget->parent)));
2581
2582   if (notebook->cur_page == page)
2583     return;
2584
2585   page_num = 0;
2586   children = notebook->children;
2587   while (children && children->data != page)
2588     {
2589       children = children->next;
2590       page_num++;
2591     }
2592
2593   gtk_signal_emit (GTK_OBJECT (notebook), 
2594                    notebook_signals[SWITCH_PAGE], 
2595                    page,
2596                    page_num);
2597 }
2598
2599 static void
2600 gtk_notebook_switch_page (GtkNotebook     *notebook,
2601                           GtkNotebookPage *page,
2602                           gint             page_num)
2603
2604   g_return_if_fail (notebook != NULL);
2605   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2606   g_return_if_fail (page != NULL);
2607  
2608   if (notebook->cur_page == page)
2609     return;
2610
2611   gtk_signal_emit (GTK_OBJECT (notebook), 
2612                    notebook_signals[SWITCH_PAGE], 
2613                    page,
2614                    page_num);
2615 }
2616
2617 static void
2618 gtk_notebook_marshal_signal (GtkObject      *object,
2619                              GtkSignalFunc   func,
2620                              gpointer        func_data,
2621                              GtkArg         *args)
2622 {
2623   GtkNotebookSignal rfunc;
2624
2625   rfunc = (GtkNotebookSignal) func;
2626
2627   (* rfunc) (object, GTK_VALUE_POINTER (args[0]), GTK_VALUE_INT (args[1]),
2628              func_data);
2629 }
2630
2631 static gint
2632 gtk_notebook_focus (GtkContainer     *container,
2633                     GtkDirectionType  direction)
2634 {
2635   GtkNotebook *notebook;
2636   GtkWidget *focus_child;
2637   GtkNotebookPage *page = NULL;
2638   GtkNotebookPage *old_page;
2639   gint return_val;
2640
2641   g_return_val_if_fail (container != NULL, FALSE);
2642   g_return_val_if_fail (GTK_IS_NOTEBOOK (container), FALSE);
2643
2644   notebook = GTK_NOTEBOOK (container);
2645
2646   if (!GTK_WIDGET_SENSITIVE (container) || !notebook->children)
2647     return FALSE;
2648
2649   focus_child = container->focus_child;
2650   container->focus_child = NULL;
2651
2652   if (!notebook->show_tabs)
2653     {
2654       if (GTK_WIDGET_VISIBLE (notebook->cur_page->child))
2655         {
2656           if (GTK_WIDGET_CAN_FOCUS (notebook->cur_page->child))
2657             {
2658               if (!focus_child)
2659                 {
2660                   gtk_widget_grab_focus (notebook->cur_page->child);
2661                   return TRUE;
2662                 }
2663             }
2664           else if (GTK_IS_CONTAINER (notebook->cur_page->child))
2665             {
2666               if (gtk_container_focus 
2667                   (GTK_CONTAINER (notebook->cur_page->child), direction))
2668                 return TRUE;
2669             }
2670         }
2671       return FALSE;
2672     }
2673
2674   old_page = notebook->focus_tab->data;
2675   return_val = FALSE;
2676
2677   if (!GTK_WIDGET_HAS_FOCUS (container) && !focus_child)
2678     {
2679       gtk_widget_grab_focus (GTK_WIDGET (container));
2680       return TRUE;
2681     }
2682
2683   if (focus_child && focus_child == old_page->child)
2684     {
2685       if (GTK_WIDGET_VISIBLE (old_page->child))
2686         {
2687           if (GTK_IS_CONTAINER (old_page->child) &&
2688               !GTK_WIDGET_HAS_FOCUS (old_page->child))
2689             {
2690               if (gtk_container_focus (GTK_CONTAINER (old_page->child),
2691                                        direction))
2692                 return TRUE;
2693             }
2694           gtk_widget_grab_focus (GTK_WIDGET(notebook));
2695           return TRUE;
2696         }
2697       return FALSE;
2698     }
2699   
2700   switch (direction)
2701     {
2702     case GTK_DIR_TAB_FORWARD:
2703     case GTK_DIR_RIGHT:
2704     case GTK_DIR_DOWN:
2705       if (!notebook->focus_tab->next)
2706         return FALSE;
2707
2708       notebook->focus_tab = notebook->focus_tab->next;
2709       page = notebook->focus_tab->data;
2710       return_val = TRUE;
2711       break;
2712     case GTK_DIR_TAB_BACKWARD:
2713     case GTK_DIR_LEFT:
2714     case GTK_DIR_UP:
2715       if (!notebook->focus_tab->prev)
2716           return FALSE;
2717       
2718       notebook->focus_tab = notebook->focus_tab->prev;
2719       page = notebook->focus_tab->data;
2720       return_val = TRUE;
2721       break;
2722     }
2723
2724   if (return_val)
2725     {
2726       if (GTK_WIDGET_MAPPED (page->tab_label))
2727         gtk_notebook_focus_changed (notebook, old_page);
2728       else
2729         {
2730           gtk_notebook_pages_allocate (notebook, 
2731                                        &(GTK_WIDGET (notebook)->allocation));
2732           gtk_notebook_expose_tabs (notebook);
2733         }
2734     }
2735
2736   return return_val;
2737 }
2738
2739 static gint
2740 gtk_notebook_page_select (GtkNotebook *notebook)
2741 {
2742   g_return_val_if_fail (notebook != NULL, FALSE);
2743   g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), FALSE);
2744
2745   if (notebook->focus_tab)
2746     {
2747       GtkNotebookPage *page;
2748       GList *children;
2749       gint num;
2750
2751       page = notebook->focus_tab->data;
2752
2753       children = notebook->children;
2754       num = 0;
2755       while (children != notebook->focus_tab)
2756         {
2757           children = children->next;
2758           num++;
2759         }
2760
2761       gtk_notebook_switch_page (notebook, page, num);
2762
2763      if (GTK_WIDGET_VISIBLE (page->child))
2764        {
2765          if (GTK_WIDGET_CAN_FOCUS (page->child))
2766            {
2767              gtk_widget_grab_focus (page->child);
2768              return TRUE;
2769            }
2770          else if (GTK_IS_CONTAINER (page->child))
2771            {
2772              if (gtk_container_focus (GTK_CONTAINER (page->child), 
2773                                       GTK_DIR_TAB_FORWARD))
2774                return TRUE;
2775            }
2776        }
2777     }
2778   return FALSE;
2779 }
2780
2781 static void
2782 gtk_notebook_switch_focus_tab (GtkNotebook *notebook, 
2783                                GList       *new_child)
2784 {
2785   GtkNotebookPage *old_page;
2786   GtkNotebookPage *page;
2787
2788   g_return_if_fail (notebook != NULL);
2789   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2790
2791   if (notebook->focus_tab != new_child)
2792     {
2793       old_page = notebook->focus_tab->data;
2794       notebook->focus_tab = new_child;
2795       page = notebook->focus_tab->data;
2796       if (GTK_WIDGET_MAPPED (page->tab_label))
2797         gtk_notebook_focus_changed (notebook, old_page);
2798       else
2799         {
2800           gtk_notebook_pages_allocate (notebook, 
2801                                        &(GTK_WIDGET (notebook)->allocation));
2802           gtk_notebook_expose_tabs (notebook);
2803         }
2804     }
2805 }
2806
2807 static gint
2808 gtk_notebook_key_press (GtkWidget   *widget,
2809                         GdkEventKey *event)
2810 {
2811   GtkNotebook *notebook;
2812   GtkDirectionType direction = 0;
2813   gint return_val;
2814   
2815   g_return_val_if_fail (widget != NULL, FALSE);
2816   g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
2817   g_return_val_if_fail (event != NULL, FALSE);
2818   
2819   notebook = GTK_NOTEBOOK (widget);
2820   return_val = TRUE;
2821
2822   if (!notebook->children || !notebook->show_tabs)
2823     return FALSE;
2824
2825   switch (event->keyval)
2826     {
2827     case GDK_Up:
2828       direction = GTK_DIR_UP;
2829       break;
2830     case GDK_Left:
2831       direction = GTK_DIR_LEFT;
2832       break;
2833     case GDK_Down:
2834       direction = GTK_DIR_DOWN;
2835       break;
2836     case GDK_Right:
2837       direction = GTK_DIR_RIGHT;
2838       break;
2839     case GDK_Tab:
2840       if (event->state & GDK_SHIFT_MASK)
2841         direction = GTK_DIR_TAB_BACKWARD;
2842       else
2843         direction = GTK_DIR_TAB_FORWARD;
2844       break;
2845     case GDK_Home:
2846       gtk_notebook_switch_focus_tab (notebook, notebook->children);
2847       return TRUE;
2848     case GDK_End:
2849       gtk_notebook_switch_focus_tab (notebook, 
2850                                      g_list_last (notebook->children));
2851       return TRUE;
2852     case GDK_Return:
2853     case GDK_space:
2854       gtk_notebook_page_select (GTK_NOTEBOOK (widget));
2855       return TRUE;
2856     default:
2857       return_val = FALSE;
2858     }
2859   if (return_val)
2860     return gtk_container_focus (GTK_CONTAINER (widget), direction);
2861   return return_val;
2862 }
2863
2864 void
2865 gtk_notebook_set_tab_border (GtkNotebook *notebook,
2866                              gint         tab_border)
2867 {
2868   g_return_if_fail (notebook != NULL);
2869   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2870
2871   if (notebook->tab_border != tab_border && tab_border > 0)
2872     {
2873       notebook->tab_border = tab_border;
2874
2875       if (GTK_WIDGET_VISIBLE (notebook) && notebook->show_tabs)
2876         gtk_widget_queue_resize (GTK_WIDGET (notebook));
2877     }
2878 }
2879
2880 static void
2881 gtk_notebook_update_labels (GtkNotebook *notebook,
2882                             GList       *list,
2883                             gint        page_num)
2884 {
2885   GtkNotebookPage *page;
2886   gchar string[32];
2887
2888   while (list)
2889     {
2890       page = list->data;
2891       list = list->next;
2892       sprintf (string, "Page %d", page_num);
2893       if (notebook->show_tabs && page->default_tab)
2894         gtk_label_set (GTK_LABEL (page->tab_label), string);
2895       if (notebook->menu && page->default_menu)
2896         {
2897           if (GTK_IS_LABEL (page->tab_label))
2898             gtk_label_set (GTK_LABEL (page->menu_label), GTK_LABEL (page->tab_label)->label);
2899           else
2900             gtk_label_set (GTK_LABEL (page->menu_label), string);
2901         }
2902       page_num++;
2903     }  
2904 }
2905
2906 static void
2907 gtk_notebook_menu_item_create (GtkNotebook     *notebook, 
2908                                GtkNotebookPage *page,
2909                                gint             position)
2910 {              
2911   GtkWidget *menu_item;
2912
2913   if (page->default_menu)
2914     {
2915       if (page->tab_label && GTK_IS_LABEL (page->tab_label))
2916         page->menu_label = gtk_label_new (GTK_LABEL (page->tab_label)->label);
2917       else
2918         page->menu_label = gtk_label_new ("");
2919     }
2920   gtk_widget_show (page->menu_label);
2921   menu_item = gtk_menu_item_new ();
2922   gtk_container_add (GTK_CONTAINER (menu_item), page->menu_label);
2923   gtk_menu_insert (GTK_MENU (notebook->menu), menu_item, position);
2924   gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
2925                       GTK_SIGNAL_FUNC (gtk_notebook_menu_switch_page), 
2926                       page);
2927   gtk_widget_show (menu_item);
2928 }
2929
2930 void
2931 gtk_notebook_popup_enable (GtkNotebook *notebook)
2932 {
2933   GtkNotebookPage *page;
2934   GList *children;
2935
2936   g_return_if_fail (notebook != NULL);
2937   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2938   
2939   if (notebook->menu)
2940     return;
2941
2942   notebook->menu = gtk_menu_new ();
2943   
2944   children = notebook->children;
2945   while (children)
2946     {
2947       page = children->data;
2948       children = children->next;
2949       gtk_notebook_menu_item_create (notebook, page, -1);
2950     }
2951   gtk_notebook_update_labels (notebook, notebook->children,1);
2952
2953   gtk_menu_attach_to_widget (GTK_MENU (notebook->menu), GTK_WIDGET (notebook),
2954                              gtk_notebook_menu_detacher);
2955 }
2956
2957 static void
2958 gtk_notebook_menu_label_unparent (GtkWidget *widget, 
2959                                   gpointer  data)
2960 {
2961   gtk_widget_unparent (GTK_BIN(widget)->child);
2962   GTK_BIN(widget)->child = NULL;
2963 }
2964
2965 void       
2966 gtk_notebook_popup_disable  (GtkNotebook *notebook)
2967 {
2968   g_return_if_fail (notebook != NULL);
2969   g_return_if_fail (GTK_IS_NOTEBOOK (notebook));
2970
2971   if (!notebook->menu)
2972     return;
2973
2974   gtk_container_foreach (GTK_CONTAINER (notebook->menu),
2975                          (GtkCallback) gtk_notebook_menu_label_unparent, NULL);
2976   gtk_widget_destroy (notebook->menu);
2977 }
2978
2979 static void
2980 gtk_notebook_menu_detacher (GtkWidget *widget,
2981                             GtkMenu   *menu)
2982 {
2983   GtkNotebook *notebook;
2984   
2985   g_return_if_fail (widget != NULL);
2986   g_return_if_fail (GTK_IS_NOTEBOOK (widget));
2987
2988   notebook = GTK_NOTEBOOK (widget);
2989   g_return_if_fail (notebook->menu == (GtkWidget*) menu);
2990
2991   notebook->menu = NULL;
2992 }