gtknotebook.c 144 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27
#include "gtknotebook.h"
28 29 30 31 32 33
#include "gtkmain.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtklabel.h"
#include <gdk/gdkkeysyms.h>
#include <stdio.h>
34
#include "gtkintl.h"
35
#include "gtkmarshalers.h"
36
#include "gtkbindings.h"
Elliot Lee's avatar
Elliot Lee committed
37 38 39 40


#define TAB_OVERLAP    2
#define TAB_CURVATURE  1
41 42
#define ARROW_SIZE     12
#define ARROW_SPACING  0
43 44
#define NOTEBOOK_INIT_SCROLL_DELAY (200)
#define NOTEBOOK_SCROLL_DELAY      (100)
Elliot Lee's avatar
Elliot Lee committed
45 46


47 48
enum {
  SWITCH_PAGE,
49 50
  FOCUS_TAB,
  SELECT_PAGE,
51
  CHANGE_CURRENT_PAGE,
52
  MOVE_FOCUS_OUT,
53 54 55
  LAST_SIGNAL
};

56 57 58 59 60
enum {
  STEP_PREV,
  STEP_NEXT
};

61 62 63 64 65 66 67 68 69 70 71 72
typedef enum
{
  ARROW_NONE,
  ARROW_LEFT_BEFORE,
  ARROW_RIGHT_BEFORE,
  ARROW_LEFT_AFTER,
  ARROW_RIGHT_AFTER
} GtkNotebookArrow;

#define ARROW_IS_LEFT(arrow)  ((arrow) == ARROW_LEFT_BEFORE || (arrow) == ARROW_LEFT_AFTER)
#define ARROW_IS_BEFORE(arrow) ((arrow) == ARROW_LEFT_BEFORE || (arrow) == ARROW_RIGHT_BEFORE)

73
enum {
74 75 76 77 78 79 80 81 82 83 84
  PROP_0,
  PROP_TAB_POS,
  PROP_SHOW_TABS,
  PROP_SHOW_BORDER,
  PROP_SCROLLABLE,
  PROP_TAB_BORDER,
  PROP_TAB_HBORDER,
  PROP_TAB_VBORDER,
  PROP_PAGE,
  PROP_ENABLE_POPUP,
  PROP_HOMOGENEOUS
85 86 87
};

enum {
Tim Janik's avatar
Tim Janik committed
88 89 90 91 92 93 94
  CHILD_PROP_0,
  CHILD_PROP_TAB_LABEL,
  CHILD_PROP_MENU_LABEL,
  CHILD_PROP_POSITION,
  CHILD_PROP_TAB_EXPAND,
  CHILD_PROP_TAB_FILL,
  CHILD_PROP_TAB_PACK
95 96
};

97 98 99 100 101 102 103
#define GTK_NOTEBOOK_PAGE(_glist_)         ((GtkNotebookPage *)((GList *)(_glist_))->data)

struct _GtkNotebookPage
{
  GtkWidget *child;
  GtkWidget *tab_label;
  GtkWidget *menu_label;
104
  GtkWidget *last_focus_child;	/* Last descendant of the page that had focus */
105 106 107 108 109 110 111 112 113

  guint default_menu : 1;	/* If true, we create the menu label ourself */
  guint default_tab  : 1;	/* If true, we create the tab label ourself */
  guint expand       : 1;
  guint fill         : 1;
  guint pack         : 1;

  GtkRequisition requisition;
  GtkAllocation allocation;
114

115
  guint mnemonic_activate_signal;
116 117 118 119
};

#ifdef G_DISABLE_CHECKS
#define CHECK_FIND_CHILD(notebook, child)                           \
120
 gtk_notebook_find_child (notebook, child, G_STRLOC)
121 122
#else
#define CHECK_FIND_CHILD(notebook, child)                           \
123
 gtk_notebook_find_child (notebook, child, NULL)
124 125
#endif
 
126
/*** GtkNotebook Methods ***/
127 128
static void gtk_notebook_class_init          (GtkNotebookClass *klass);
static void gtk_notebook_init                (GtkNotebook      *notebook);
129

130 131 132 133 134 135 136 137
static gboolean gtk_notebook_select_page         (GtkNotebook      *notebook,
						  gboolean          move_focus);
static gboolean gtk_notebook_focus_tab           (GtkNotebook      *notebook,
						  GtkNotebookTab    type);
static void     gtk_notebook_change_current_page (GtkNotebook      *notebook,
						  gint              offset);
static void     gtk_notebook_move_focus_out      (GtkNotebook      *notebook,
						  GtkDirectionType  direction_type);
138

139
/*** GtkObject Methods ***/
140
static void gtk_notebook_destroy             (GtkObject        *object);
141 142 143 144 145 146 147 148
static void gtk_notebook_set_property	     (GObject         *object,
					      guint            prop_id,
					      const GValue    *value,
					      GParamSpec      *pspec);
static void gtk_notebook_get_property	     (GObject         *object,
					      guint            prop_id,
					      GValue          *value,
					      GParamSpec      *pspec);
149

150
/*** GtkWidget Methods ***/
151 152 153
static void gtk_notebook_map                 (GtkWidget        *widget);
static void gtk_notebook_unmap               (GtkWidget        *widget);
static void gtk_notebook_realize             (GtkWidget        *widget);
154
static void gtk_notebook_unrealize           (GtkWidget        *widget);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static void gtk_notebook_size_request        (GtkWidget        *widget,
					      GtkRequisition   *requisition);
static void gtk_notebook_size_allocate       (GtkWidget        *widget,
					      GtkAllocation    *allocation);
static gint gtk_notebook_expose              (GtkWidget        *widget,
					      GdkEventExpose   *event);
static gint gtk_notebook_button_press        (GtkWidget        *widget,
					      GdkEventButton   *event);
static gint gtk_notebook_button_release      (GtkWidget        *widget,
					      GdkEventButton   *event);
static gint gtk_notebook_enter_notify        (GtkWidget        *widget,
					      GdkEventCrossing *event);
static gint gtk_notebook_leave_notify        (GtkWidget        *widget,
					      GdkEventCrossing *event);
static gint gtk_notebook_motion_notify       (GtkWidget        *widget,
					      GdkEventMotion   *event);
171 172
static gint gtk_notebook_focus_in            (GtkWidget        *widget,
					      GdkEventFocus    *event);
Matthias Clasen's avatar
Matthias Clasen committed
173 174 175 176
static void gtk_notebook_grab_notify         (GtkWidget          *widget,
					      gboolean            was_grabbed);
static void gtk_notebook_state_changed       (GtkWidget          *widget,
					      GtkStateType        previous_state);
177
static void gtk_notebook_draw_focus          (GtkWidget        *widget);
178 179
static gint gtk_notebook_focus               (GtkWidget        *widget,
					      GtkDirectionType  direction);
180 181
static void gtk_notebook_style_set           (GtkWidget        *widget,
					      GtkStyle         *previous);
182

183
/*** GtkContainer Methods ***/
Tim Janik's avatar
Tim Janik committed
184
static void gtk_notebook_set_child_property  (GtkContainer     *container,
185
					      GtkWidget        *child,
Tim Janik's avatar
Tim Janik committed
186 187 188 189
					      guint             property_id,
					      const GValue     *value,
					      GParamSpec       *pspec);
static void gtk_notebook_get_child_property  (GtkContainer     *container,
190
					      GtkWidget        *child,
Tim Janik's avatar
Tim Janik committed
191 192 193
					      guint             property_id,
					      GValue           *value,
					      GParamSpec       *pspec);
194 195 196 197
static void gtk_notebook_add                 (GtkContainer     *container,
					      GtkWidget        *widget);
static void gtk_notebook_remove              (GtkContainer     *container,
					      GtkWidget        *widget);
198 199
static void gtk_notebook_set_focus_child     (GtkContainer     *container,
					      GtkWidget        *child);
Manish Singh's avatar
Manish Singh committed
200
static GType gtk_notebook_child_type       (GtkContainer     *container);
201 202
static void gtk_notebook_forall              (GtkContainer     *container,
					      gboolean		include_internals,
203 204
					      GtkCallback       callback,
					      gpointer          callback_data);
205

206
/*** GtkNotebook Private Functions ***/
207 208
static void gtk_notebook_redraw_tabs         (GtkNotebook      *notebook);
static void gtk_notebook_redraw_arrows       (GtkNotebook      *notebook);
209
static void gtk_notebook_real_remove         (GtkNotebook      *notebook,
210 211
					      GList            *list,
					      gboolean		destroying);
212 213 214 215
static void gtk_notebook_update_labels       (GtkNotebook      *notebook);
static gint gtk_notebook_timer               (GtkNotebook      *notebook);
static gint gtk_notebook_page_compare        (gconstpointer     a,
					      gconstpointer     b);
216
static GList* gtk_notebook_find_child        (GtkNotebook      *notebook,
217 218
					      GtkWidget        *child,
					      const gchar      *function);
219 220 221 222 223 224 225
static gint  gtk_notebook_real_page_position (GtkNotebook      *notebook,
					      GList            *list);
static GList * gtk_notebook_search_page      (GtkNotebook      *notebook,
					      GList            *list,
					      gint              direction,
					      gboolean          find_visible);

226
/*** GtkNotebook Drawing Functions ***/
227 228
static void gtk_notebook_paint               (GtkWidget        *widget,
					      GdkRectangle     *area);
229 230 231
static void gtk_notebook_draw_tab            (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GdkRectangle     *area);
232
static void gtk_notebook_draw_arrow          (GtkNotebook      *notebook,
233
					      GtkNotebookArrow  arrow);
234

235
/*** GtkNotebook Size Allocate Functions ***/
236
static void gtk_notebook_pages_allocate      (GtkNotebook      *notebook);
237 238 239
static void gtk_notebook_page_allocate       (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GtkAllocation    *allocation);
240 241
static void gtk_notebook_calc_tabs           (GtkNotebook      *notebook,
			                      GList            *start,
242 243 244
					      GList           **end,
					      gint             *tab_space,
					      guint             direction);
245

246
/*** GtkNotebook Page Switch Methods ***/
247
static void gtk_notebook_real_switch_page    (GtkNotebook      *notebook,
Lars Hamann's avatar
Lars Hamann committed
248
					      GtkNotebookPage  *page,
249
					      guint             page_num);
250

251
/*** GtkNotebook Page Switch Functions ***/
252 253
static void gtk_notebook_switch_page         (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
254
					      gint              page_num);
255 256
static gint gtk_notebook_page_select         (GtkNotebook      *notebook,
					      gboolean          move_focus);
257 258
static void gtk_notebook_switch_focus_tab    (GtkNotebook      *notebook,
                                              GList            *new_child);
259 260
static void gtk_notebook_menu_switch_page    (GtkWidget        *widget,
					      GtkNotebookPage  *page);
261

262
/*** GtkNotebook Menu Functions ***/
263 264 265 266
static void gtk_notebook_menu_item_create    (GtkNotebook      *notebook,
					      GList            *list);
static void gtk_notebook_menu_label_unparent (GtkWidget        *widget,
					      gpointer          data);
267 268
static void gtk_notebook_menu_detacher       (GtkWidget        *widget,
					      GtkMenu          *menu);
269

270 271 272 273 274 275 276 277 278 279
/*** GtkNotebook Private Setters ***/
static void gtk_notebook_set_homogeneous_tabs_internal (GtkNotebook *notebook,
							gboolean     homogeneous);
static void gtk_notebook_set_tab_border_internal       (GtkNotebook *notebook,
							guint        border_width);
static void gtk_notebook_set_tab_hborder_internal      (GtkNotebook *notebook,
							guint        tab_hborder);
static void gtk_notebook_set_tab_vborder_internal      (GtkNotebook *notebook,
							guint        tab_vborder);

280 281 282
static gboolean focus_tabs_in  (GtkNotebook      *notebook);
static gboolean focus_child_in (GtkNotebook      *notebook,
				GtkDirectionType  direction);
Elliot Lee's avatar
Elliot Lee committed
283

284
static GtkContainerClass *parent_class = NULL;
285
static guint notebook_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
286

Manish Singh's avatar
Manish Singh committed
287
GType
288
gtk_notebook_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
289
{
Manish Singh's avatar
Manish Singh committed
290
  static GType notebook_type = 0;
Elliot Lee's avatar
Elliot Lee committed
291 292 293

  if (!notebook_type)
    {
Manish Singh's avatar
Manish Singh committed
294
      static const GTypeInfo notebook_info =
Elliot Lee's avatar
Elliot Lee committed
295 296
      {
	sizeof (GtkNotebookClass),
Manish Singh's avatar
Manish Singh committed
297 298 299 300 301 302 303 304
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_notebook_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkNotebook),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_notebook_init,
Elliot Lee's avatar
Elliot Lee committed
305 306
      };

Manish Singh's avatar
Manish Singh committed
307 308
      notebook_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkNotebook",
					      &notebook_info, 0);
Elliot Lee's avatar
Elliot Lee committed
309 310 311 312 313
    }

  return notebook_type;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static void
add_tab_bindings (GtkBindingSet    *binding_set,
		  GdkModifierType   modifiers,
		  GtkDirectionType  direction)
{
  gtk_binding_entry_add_signal (binding_set, GDK_Tab, modifiers,
                                "move_focus_out", 1,
                                GTK_TYPE_DIRECTION_TYPE, direction);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, modifiers,
                                "move_focus_out", 1,
                                GTK_TYPE_DIRECTION_TYPE, direction);
}

static void
add_arrow_bindings (GtkBindingSet    *binding_set,
		    guint             keysym,
		    GtkDirectionType  direction)
{
  guint keypad_keysym = keysym - GDK_Left + GDK_KP_Left;
  
  gtk_binding_entry_add_signal (binding_set, keysym, GDK_CONTROL_MASK,
                                "move_focus_out", 1,
                                GTK_TYPE_DIRECTION_TYPE, direction);
  gtk_binding_entry_add_signal (binding_set, keypad_keysym, GDK_CONTROL_MASK,
                                "move_focus_out", 1,
                                GTK_TYPE_DIRECTION_TYPE, direction);
}

Elliot Lee's avatar
Elliot Lee committed
342 343 344
static void
gtk_notebook_class_init (GtkNotebookClass *class)
{
Tim Janik's avatar
Tim Janik committed
345 346 347 348
  GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
349 350
  GtkBindingSet *binding_set;
  
Tim Janik's avatar
Tim Janik committed
351
  parent_class = g_type_class_peek_parent (class);
352

353 354
  gobject_class->set_property = gtk_notebook_set_property;
  gobject_class->get_property = gtk_notebook_get_property;
355 356
  object_class->destroy = gtk_notebook_destroy;

Elliot Lee's avatar
Elliot Lee committed
357 358 359
  widget_class->map = gtk_notebook_map;
  widget_class->unmap = gtk_notebook_unmap;
  widget_class->realize = gtk_notebook_realize;
360
  widget_class->unrealize = gtk_notebook_unrealize;
Elliot Lee's avatar
Elliot Lee committed
361 362 363 364
  widget_class->size_request = gtk_notebook_size_request;
  widget_class->size_allocate = gtk_notebook_size_allocate;
  widget_class->expose_event = gtk_notebook_expose;
  widget_class->button_press_event = gtk_notebook_button_press;
365 366 367 368
  widget_class->button_release_event = gtk_notebook_button_release;
  widget_class->enter_notify_event = gtk_notebook_enter_notify;
  widget_class->leave_notify_event = gtk_notebook_leave_notify;
  widget_class->motion_notify_event = gtk_notebook_motion_notify;
Matthias Clasen's avatar
Matthias Clasen committed
369 370
  widget_class->grab_notify = gtk_notebook_grab_notify;
  widget_class->state_changed = gtk_notebook_state_changed;
371
  widget_class->focus_in_event = gtk_notebook_focus_in;
372
  widget_class->focus = gtk_notebook_focus;
373
  widget_class->style_set = gtk_notebook_style_set;
374
  
Elliot Lee's avatar
Elliot Lee committed
375 376
  container_class->add = gtk_notebook_add;
  container_class->remove = gtk_notebook_remove;
377
  container_class->forall = gtk_notebook_forall;
Lars Hamann's avatar
Lars Hamann committed
378
  container_class->set_focus_child = gtk_notebook_set_focus_child;
Tim Janik's avatar
Tim Janik committed
379 380
  container_class->get_child_property = gtk_notebook_get_child_property;
  container_class->set_child_property = gtk_notebook_set_child_property;
381
  container_class->child_type = gtk_notebook_child_type;
382

383
  class->switch_page = gtk_notebook_real_switch_page;
384

385 386
  class->focus_tab = gtk_notebook_focus_tab;
  class->select_page = gtk_notebook_select_page;
387
  class->change_current_page = gtk_notebook_change_current_page;
388
  class->move_focus_out = gtk_notebook_move_focus_out;
389
  
390 391 392 393 394 395 396 397 398
  g_object_class_install_property (gobject_class,
				   PROP_PAGE,
				   g_param_spec_int ("page",
 						     _("Page"),
 						     _("The index of the current page"),
 						     0,
 						     G_MAXINT,
 						     0,
 						     G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
399 400 401
  g_object_class_install_property (gobject_class,
				   PROP_TAB_POS,
				   g_param_spec_enum ("tab_pos",
402 403 404 405 406
 						      _("Tab Position"),
 						      _("Which side of the notebook holds the tabs"),
 						      GTK_TYPE_POSITION_TYPE,
 						      GTK_POS_TOP,
 						      G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
407 408 409
  g_object_class_install_property (gobject_class,
				   PROP_TAB_BORDER,
				   g_param_spec_uint ("tab_border",
410 411 412 413 414 415
 						      _("Tab Border"),
 						      _("Width of the border around the tab labels"),
 						      0,
 						      G_MAXUINT,
 						      2,
 						      G_PARAM_WRITABLE));
Tim Janik's avatar
Tim Janik committed
416 417 418
  g_object_class_install_property (gobject_class,
				   PROP_TAB_HBORDER,
				   g_param_spec_uint ("tab_hborder",
419 420 421 422 423 424
 						      _("Horizontal Tab Border"),
 						      _("Width of the horizontal border of tab labels"),
 						      0,
 						      G_MAXUINT,
 						      2,
 						      G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
425 426 427
  g_object_class_install_property (gobject_class,
				   PROP_TAB_VBORDER,
				   g_param_spec_uint ("tab_vborder",
428 429 430 431 432 433
 						      _("Vertical Tab Border"),
 						      _("Width of the vertical border of tab labels"),
 						      0,
 						      G_MAXUINT,
 						      2,
 						      G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
434 435 436
  g_object_class_install_property (gobject_class,
				   PROP_SHOW_TABS,
				   g_param_spec_boolean ("show_tabs",
437 438 439 440
 							 _("Show Tabs"),
 							 _("Whether tabs should be shown or not"),
 							 TRUE,
 							 G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
441 442 443
  g_object_class_install_property (gobject_class,
				   PROP_SHOW_BORDER,
				   g_param_spec_boolean ("show_border",
444 445 446 447
 							 _("Show Border"),
 							 _("Whether the border should be shown or not"),
 							 TRUE,
 							 G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
448 449 450
  g_object_class_install_property (gobject_class,
				   PROP_SCROLLABLE,
				   g_param_spec_boolean ("scrollable",
451
 							 _("Scrollable"),
Matthias Clasen's avatar
Matthias Clasen committed
452
 							 _("If TRUE, scroll arrows are added if there are too many tabs to fit"),
453 454
 							 FALSE,
 							 G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
455 456 457
  g_object_class_install_property (gobject_class,
				   PROP_ENABLE_POPUP,
				   g_param_spec_boolean ("enable_popup",
458 459 460 461
 							 _("Enable Popup"),
 							 _("If TRUE, pressing the right mouse button on the notebook pops up a menu that you can use to go to a page"),
 							 FALSE,
 							 G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
462 463 464
  g_object_class_install_property (gobject_class,
				   PROP_HOMOGENEOUS,
				   g_param_spec_boolean ("homogeneous",
465 466 467
 							 _("Homogeneous"),
 							 _("Whether tabs should have homogeneous sizes"),
 							 FALSE,
Tim Janik's avatar
Tim Janik committed
468 469 470 471
							 G_PARAM_READWRITE));

  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_TAB_LABEL,
472 473 474
					      g_param_spec_string ("tab_label", 
								   _("Tab label"),
								   _("The string displayed on the childs tab label"),
Tim Janik's avatar
Tim Janik committed
475 476 477 478
								   NULL,
								   G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_MENU_LABEL,
479 480 481
					      g_param_spec_string ("menu_label", 
								   _("Menu label"), 
								   _("The string displayed in the childs menu entry"),
Tim Janik's avatar
Tim Janik committed
482 483 484 485
								   NULL,
								   G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_POSITION,
486 487 488
					      g_param_spec_int ("position", 
								_("Position"), 
								_("The index of the child in the parent"),
Tim Janik's avatar
Tim Janik committed
489 490 491 492
								-1, G_MAXINT, 0,
								G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_TAB_EXPAND,
493 494 495
					      g_param_spec_boolean ("tab_expand", 
								    _("Tab expand"), 
								    _("Whether to expand the childs tab or not"),
Tim Janik's avatar
Tim Janik committed
496 497 498 499
								    TRUE,
								    G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_TAB_FILL,
500 501 502
					      g_param_spec_boolean ("tab_fill", 
								    _("Tab fill"), 
								    _("Wheather the childs tab should fill the allocated area or not"),
Tim Janik's avatar
Tim Janik committed
503 504 505 506
								    TRUE,
								    G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
					      CHILD_PROP_TAB_PACK,
507 508 509
					      g_param_spec_enum ("tab_pack", 
								 _("Tab pack type"),
								 _("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
510 511
								 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
								 G_PARAM_READWRITE));
Tim Janik's avatar
Tim Janik committed
512
  
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boolean ("has_secondary backward_stepper",
								 _("Secondary backward stepper"),
								 _("Display a second backward arrow button on the opposite end of the tab area"),
								 FALSE,
								 
								 G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boolean ("has_secondary_forward_stepper",
								 _("Secondary forward stepper"),
								 _("Display a second forward arrow button on the opposite end of the tab area"),
								 FALSE,
								 
								 G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boolean ("has_backward_stepper",
								 _("Backward stepper"),
								 _("Display the standard backward arrow button"),
								 TRUE,
								 
                                                                   G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boolean ("has_forward_stepper",
								 _("Forward stepper"),
								 _("Display the standard forward arrow button"),
								 TRUE,
								 
                                                                   G_PARAM_READABLE));

545
  notebook_signals[SWITCH_PAGE] =
Manish Singh's avatar
Manish Singh committed
546 547 548 549 550 551 552 553 554
    g_signal_new ("switch_page",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkNotebookClass, switch_page),
		  NULL, NULL,
		  _gtk_marshal_VOID__POINTER_UINT,
		  G_TYPE_NONE, 2,
		  G_TYPE_POINTER,
		  G_TYPE_UINT);
555
  notebook_signals[FOCUS_TAB] = 
556
    g_signal_new ("focus_tab",
Manish Singh's avatar
Manish Singh committed
557
                  G_TYPE_FROM_CLASS (gobject_class),
558 559 560
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkNotebookClass, focus_tab),
                  NULL, NULL,
561
                  _gtk_marshal_BOOLEAN__ENUM,
562
                  G_TYPE_BOOLEAN, 1,
563
                  GTK_TYPE_NOTEBOOK_TAB);
564
  notebook_signals[SELECT_PAGE] = 
565
    g_signal_new ("select_page",
Manish Singh's avatar
Manish Singh committed
566
                  G_TYPE_FROM_CLASS (gobject_class),
567 568 569
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkNotebookClass, select_page),
                  NULL, NULL,
570 571
                  _gtk_marshal_BOOLEAN__BOOLEAN,
                  G_TYPE_BOOLEAN, 1,
572
                  G_TYPE_BOOLEAN);
573 574
  notebook_signals[CHANGE_CURRENT_PAGE] = 
    g_signal_new ("change_current_page",
Manish Singh's avatar
Manish Singh committed
575
                  G_TYPE_FROM_CLASS (gobject_class),
576 577 578
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkNotebookClass, change_current_page),
                  NULL, NULL,
579 580
                  _gtk_marshal_VOID__INT,
                  G_TYPE_NONE, 1,
581
                  G_TYPE_INT);
582 583
  notebook_signals[MOVE_FOCUS_OUT] =
    g_signal_new ("move_focus_out",
Manish Singh's avatar
Manish Singh committed
584
                  G_TYPE_FROM_CLASS (gobject_class),
585 586 587 588 589 590 591
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkNotebookClass, move_focus_out),
                  NULL, NULL,
                  _gtk_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_DIRECTION_TYPE);
  
592
  
Manish Singh's avatar
Manish Singh committed
593
  binding_set = gtk_binding_set_by_class (class);
594 595 596 597
  gtk_binding_entry_add_signal (binding_set,
                                GDK_space, 0,
                                "select_page", 1, 
                                G_TYPE_BOOLEAN, FALSE);
598 599 600 601 602
  gtk_binding_entry_add_signal (binding_set,
                                GDK_KP_Space, 0,
                                "select_page", 1, 
                                G_TYPE_BOOLEAN, FALSE);
  
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  gtk_binding_entry_add_signal (binding_set,
                                GDK_Home, 0,
                                "focus_tab", 1, 
                                GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_FIRST);
  gtk_binding_entry_add_signal (binding_set,
                                GDK_KP_Home, 0,
                                "focus_tab", 1, 
                                GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_FIRST);
  gtk_binding_entry_add_signal (binding_set,
                                GDK_End, 0,
                                "focus_tab", 1, 
                                GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_LAST);
  gtk_binding_entry_add_signal (binding_set,
                                GDK_KP_End, 0,
                                "focus_tab", 1, 
                                GTK_TYPE_NOTEBOOK_TAB, GTK_NOTEBOOK_TAB_LAST);
619 620 621 622 623 624 625 626 627

  gtk_binding_entry_add_signal (binding_set,
                                GDK_Page_Up, GDK_CONTROL_MASK,
                                "change_current_page", 1,
                                G_TYPE_INT, -1);
  gtk_binding_entry_add_signal (binding_set,
                                GDK_Page_Down, GDK_CONTROL_MASK,
                                "change_current_page", 1,
                                G_TYPE_INT, 1);
628

629
  gtk_binding_entry_add_signal (binding_set,
630
                                GDK_Page_Up, GDK_CONTROL_MASK | GDK_MOD1_MASK,
631 632 633
                                "change_current_page", 1,
                                G_TYPE_INT, -1);
  gtk_binding_entry_add_signal (binding_set,
634
                                GDK_Page_Down, GDK_CONTROL_MASK | GDK_MOD1_MASK,
635 636 637
                                "change_current_page", 1,
                                G_TYPE_INT, 1);

638 639 640 641 642 643 644
  add_arrow_bindings (binding_set, GDK_Up, GTK_DIR_UP);
  add_arrow_bindings (binding_set, GDK_Down, GTK_DIR_DOWN);
  add_arrow_bindings (binding_set, GDK_Left, GTK_DIR_LEFT);
  add_arrow_bindings (binding_set, GDK_Right, GTK_DIR_RIGHT);

  add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
  add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
Elliot Lee's avatar
Elliot Lee committed
645 646
}

647
static void
648 649
gtk_notebook_init (GtkNotebook *notebook)
{
650
  GTK_WIDGET_SET_FLAGS (notebook, GTK_CAN_FOCUS);
651
  GTK_WIDGET_SET_FLAGS (notebook, GTK_NO_WINDOW);
652 653 654 655 656

  notebook->cur_page = NULL;
  notebook->children = NULL;
  notebook->first_tab = NULL;
  notebook->focus_tab = NULL;
657
  notebook->event_window = NULL;
658 659 660 661 662 663 664 665 666 667 668 669 670 671
  notebook->menu = NULL;

  notebook->tab_hborder = 2;
  notebook->tab_vborder = 2;

  notebook->show_tabs = TRUE;
  notebook->show_border = TRUE;
  notebook->tab_pos = GTK_POS_TOP;
  notebook->scrollable = FALSE;
  notebook->in_child = 0;
  notebook->click_child = 0;
  notebook->button = 0;
  notebook->need_timer = 0;
  notebook->child_has_focus = FALSE;
672
  notebook->have_visible_child = FALSE;
673
  notebook->focus_out = FALSE;
674 675 676 677 678

  notebook->has_before_previous = 1;
  notebook->has_before_next     = 0;
  notebook->has_after_previous  = 0;
  notebook->has_after_next      = 1;
679 680
}

681
static gboolean
682 683 684
gtk_notebook_select_page (GtkNotebook *notebook,
                          gboolean     move_focus)
{
685 686 687 688 689 690 691
  if (gtk_widget_is_focus (GTK_WIDGET (notebook)))
    {
      gtk_notebook_page_select (notebook, move_focus);
      return TRUE;
    }
  else
    return FALSE;
692 693
}

694
static gboolean
695 696 697 698
gtk_notebook_focus_tab (GtkNotebook       *notebook,
                        GtkNotebookTab     type)
{
  GList *list;
699 700

  if (gtk_widget_is_focus (GTK_WIDGET (notebook)))
701
    {
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      switch (type)
	{
	case GTK_NOTEBOOK_TAB_FIRST:
	  list = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, TRUE);
	  if (list)
	    gtk_notebook_switch_focus_tab (notebook, list);
	  break;
	case GTK_NOTEBOOK_TAB_LAST:
	  list = gtk_notebook_search_page (notebook, NULL, STEP_PREV, TRUE);
	  if (list)
	    gtk_notebook_switch_focus_tab (notebook, list);
	  break;
	}

      return TRUE;
717
    }
718 719
  else
    return FALSE;
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
static void
gtk_notebook_change_current_page (GtkNotebook *notebook,
				  gint         offset)
{
  GList *current = NULL;
  
  if (notebook->cur_page)
    current = g_list_find (notebook->children, notebook->cur_page);

  while (offset != 0)
    {
      current = gtk_notebook_search_page (notebook, current, offset < 0 ? STEP_PREV : STEP_NEXT, TRUE);
      offset += offset < 0 ? 1 : -1;
    }

  if (current)
    gtk_notebook_switch_page (notebook, current->data, -1);
739
  else
740
    gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (notebook)));
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
}

static GtkDirectionType
get_effective_direction (GtkNotebook      *notebook,
			 GtkDirectionType  direction)
{
  /* Remap the directions into the effective direction it would be for a
   * GTK_POS_TOP notebook
   */
#define D(rest) GTK_DIR_##rest

  static const GtkDirectionType translate_direction[4][6] = {
    /* LEFT */   { D(TAB_FORWARD),  D(TAB_BACKWARD), D(LEFT), D(RIGHT), D(UP),   D(DOWN) },
    /* RIGHT */  { D(TAB_BACKWARD), D(TAB_FORWARD),  D(LEFT), D(RIGHT), D(DOWN), D(UP)   },
    /* TOP */    { D(TAB_FORWARD),  D(TAB_BACKWARD), D(UP),   D(DOWN),  D(LEFT), D(RIGHT) },
    /* BOTTOM */ { D(TAB_BACKWARD), D(TAB_FORWARD),  D(DOWN), D(UP),    D(LEFT), D(RIGHT) },
  };

#undef D

  return translate_direction[notebook->tab_pos][direction];
}

static void
gtk_notebook_move_focus_out (GtkNotebook      *notebook,
			     GtkDirectionType  direction_type)
{
  GtkDirectionType effective_direction = get_effective_direction (notebook, direction_type);
  GtkWidget *toplevel;
  
  if (GTK_CONTAINER (notebook)->focus_child && effective_direction == GTK_DIR_UP)
    if (focus_tabs_in (notebook))
      return;
  
  if (gtk_widget_is_focus (GTK_WIDGET (notebook)) && effective_direction == GTK_DIR_DOWN)
    if (focus_child_in (notebook, GTK_DIR_TAB_FORWARD))
      return;

  /* At this point, we know we should be focusing out of the notebook entirely. We
   * do this by setting a flag, then propagating the focus motion to the notebook.
   */
  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (notebook));
  if (!GTK_WIDGET_TOPLEVEL (toplevel))
    return;

  g_object_ref (notebook);
  
  notebook->focus_out = TRUE;
Manish Singh's avatar
Manish Singh committed
789
  g_signal_emit_by_name (toplevel, "move_focus", direction_type);
790 791 792 793
  notebook->focus_out = FALSE;
  
  g_object_unref (notebook);

794 795
}

796 797 798 799 800 801 802
/**
 * gtk_notebook_new:
 * 
 * Creates a new #GtkNotebook widget with no pages.

 * Return value: the newly created #GtkNotebook
 **/
803 804 805
GtkWidget*
gtk_notebook_new (void)
{
Manish Singh's avatar
Manish Singh committed
806
  return g_object_new (GTK_TYPE_NOTEBOOK, NULL);
807 808 809 810 811 812 813 814 815 816 817
}

/* Private GtkObject Methods :
 * 
 * gtk_notebook_destroy
 * gtk_notebook_set_arg
 * gtk_notebook_get_arg
 */
static void
gtk_notebook_destroy (GtkObject *object)
{
818
  GList *children;
819
  GtkNotebook *notebook = GTK_NOTEBOOK (object);
820 821 822 823
  
  if (notebook->menu)
    gtk_notebook_popup_disable (notebook);

824 825 826 827 828 829 830 831 832
  children = notebook->children;
  while (children)
    {
      GList *child = children;
      children = child->next;
      
      gtk_notebook_real_remove (notebook, child, TRUE);
    }
  
833 834 835 836
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
837 838 839 840
gtk_notebook_set_property (GObject         *object,
			   guint            prop_id,
			   const GValue    *value,
			   GParamSpec      *pspec)
841 842 843 844 845
{
  GtkNotebook *notebook;

  notebook = GTK_NOTEBOOK (object);

846
  switch (prop_id)
847
    {
848 849
    case PROP_SHOW_TABS:
      gtk_notebook_set_show_tabs (notebook, g_value_get_boolean (value));
850
      break;
851 852
    case PROP_SHOW_BORDER:
      gtk_notebook_set_show_border (notebook, g_value_get_boolean (value));
853
      break;
854 855
    case PROP_SCROLLABLE:
      gtk_notebook_set_scrollable (notebook, g_value_get_boolean (value));
856
      break;
857 858
    case PROP_ENABLE_POPUP:
      if (g_value_get_boolean (value))
859 860 861 862
	gtk_notebook_popup_enable (notebook);
      else
	gtk_notebook_popup_disable (notebook);
      break;
863
    case PROP_HOMOGENEOUS:
864
      gtk_notebook_set_homogeneous_tabs_internal (notebook, g_value_get_boolean (value));
865
      break;  
866
    case PROP_PAGE:
867
      gtk_notebook_set_current_page (notebook, g_value_get_int (value));
868
      break;
869 870
    case PROP_TAB_POS:
      gtk_notebook_set_tab_pos (notebook, g_value_get_enum (value));
871
      break;
872
    case PROP_TAB_BORDER:
873
      gtk_notebook_set_tab_border_internal (notebook, g_value_get_uint (value));
874
      break;
875
    case PROP_TAB_HBORDER:
876
      gtk_notebook_set_tab_hborder_internal (notebook, g_value_get_uint (value));
877
      break;
878
    case PROP_TAB_VBORDER:
879
      gtk_notebook_set_tab_vborder_internal (notebook, g_value_get_uint (value));
880
      break;
881 882 883 884 885 886
    default:
      break;
    }
}

static void
887 888 889 890
gtk_notebook_get_property (GObject         *object,
			   guint            prop_id,
			   GValue          *value,
			   GParamSpec      *pspec)
891 892 893 894 895
{
  GtkNotebook *notebook;

  notebook = GTK_NOTEBOOK (object);

896
  switch (prop_id)
897
    {
898 899
    case PROP_SHOW_TABS:
      g_value_set_boolean (value, notebook->show_tabs);
900
      break;
901 902
    case PROP_SHOW_BORDER:
      g_value_set_boolean (value, notebook->show_border);
903
      break;
904 905
    case PROP_SCROLLABLE:
      g_value_set_boolean (value, notebook->scrollable);
906
      break;
907 908
    case PROP_ENABLE_POPUP:
      g_value_set_boolean (value, notebook->menu != NULL);
909
      break;
910 911
    case PROP_HOMOGENEOUS:
      g_value_set_boolean (value, notebook->homogeneous);
912
      break;
913 914
    case PROP_PAGE:
      g_value_set_int (value, gtk_notebook_get_current_page (notebook));
915
      break;
916 917
    case PROP_TAB_POS:
      g_value_set_enum (value, notebook->tab_pos);
918
      break;
919 920
    case PROP_TAB_HBORDER:
      g_value_set_uint (value, notebook->tab_hborder);
921
      break;
922 923
    case PROP_TAB_VBORDER:
      g_value_set_uint (value, notebook->tab_vborder);
924
      break;
925
    default:
926
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
927 928 929 930
      break;
    }
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
/* Private GtkWidget Methods :
 * 
 * gtk_notebook_map
 * gtk_notebook_unmap
 * gtk_notebook_realize
 * gtk_notebook_size_request
 * gtk_notebook_size_allocate
 * gtk_notebook_expose
 * gtk_notebook_button_press
 * gtk_notebook_button_release
 * gtk_notebook_enter_notify
 * gtk_notebook_leave_notify
 * gtk_notebook_motion_notify
 * gtk_notebook_focus_in
 * gtk_notebook_focus_out
 * gtk_notebook_draw_focus
 * gtk_notebook_style_set
 */
949 950 951 952 953
static gboolean
gtk_notebook_get_event_window_position (GtkNotebook  *notebook,
					GdkRectangle *rectangle)
{
  GtkWidget *widget = GTK_WIDGET (notebook);
954
  gint border_width = GTK_CONTAINER (notebook)->border_width;
955 956
  GtkNotebookPage *visible_page = NULL;
  GList *tmp_list;
957

958
  for (tmp_list = notebook->children; tmp_list; tmp_list = tmp_list->next)
959
    {
960 961
      GtkNotebookPage *page = tmp_list->data;
      if (GTK_WIDGET_VISIBLE (page->child))
962
	{
963 964 965 966
	  visible_page = page;
	  break;
	}
    }
967

968 969 970 971
  if (notebook->show_tabs && visible_page)
    {
      if (rectangle)
	{
972 973
	  rectangle->x = widget->allocation.x + border_width;
	  rectangle->y = widget->allocation.y + border_width;
974 975 976 977 978
	  
	  switch (notebook->tab_pos)
	    {
	    case GTK_POS_TOP:
	    case GTK_POS_BOTTOM:
979
	      rectangle->width = widget->allocation.width - 2 * border_width;
980
	      rectangle->height = visible_page->requisition.height;
981
	      if (notebook->tab_pos == GTK_POS_BOTTOM)
982
		rectangle->y += widget->allocation.height - 2 * border_width - rectangle->height;
983 984 985
	      break;
	    case GTK_POS_LEFT:
	    case GTK_POS_RIGHT:
986
	      rectangle->width = visible_page->requisition.width;
987
	      rectangle->height = widget->allocation.height - 2 * border_width;
988
	      if (notebook->tab_pos == GTK_POS_RIGHT)
989
		rectangle->x += widget->allocation.width - 2 * border_width - rectangle->width;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	      break;
	    }
	}

      return TRUE;
    }
  else
    {
      if (rectangle)
	{
	  rectangle->x = rectangle->y = 0;
	  rectangle->width = rectangle->height = 10;
	}
    }

  return FALSE;
}

1008
static void
1009
gtk_notebook_map (GtkWidget *widget)
1010 1011
{
  GtkNotebook *notebook;
1012 1013
  GtkNotebookPage *page;
  GList *children;
1014

1015
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
1016

1017
  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
1018

1019
  notebook = GTK_NOTEBOOK (widget);
1020