gtknotebook.c 124 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
#include "gtksignal.h"
29 30 31 32 33 34
#include "gtkmain.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtklabel.h"
#include <gdk/gdkkeysyms.h>
#include <stdio.h>
35
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
36 37 38 39


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


47 48 49 50 51
enum {
  SWITCH_PAGE,
  LAST_SIGNAL
};

52 53 54 55 56
enum {
  STEP_PREV,
  STEP_NEXT
};

57 58 59 60 61 62 63
enum {
  ARG_0,
  ARG_TAB_POS,
  ARG_SHOW_TABS,
  ARG_SHOW_BORDER,
  ARG_SCROLLABLE,
  ARG_TAB_BORDER,
64 65
  ARG_TAB_HBORDER,
  ARG_TAB_VBORDER,
66
  ARG_PAGE,
67 68
  ARG_ENABLE_POPUP,
  ARG_HOMOGENEOUS
69 70 71 72 73 74
};

enum {
  CHILD_ARG_0,
  CHILD_ARG_TAB_LABEL,
  CHILD_ARG_MENU_LABEL,
75 76 77 78
  CHILD_ARG_POSITION,
  CHILD_ARG_TAB_EXPAND,
  CHILD_ARG_TAB_FILL,
  CHILD_ARG_TAB_PACK
79 80
};

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#define GTK_NOTEBOOK_PAGE(_glist_)         ((GtkNotebookPage *)((GList *)(_glist_))->data)

struct _GtkNotebookPage
{
  GtkWidget *child;
  GtkWidget *tab_label;
  GtkWidget *menu_label;

  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;
97 98

  guint activate_mnemonic_signal;
99 100 101 102 103 104 105 106 107 108 109
};

#ifdef G_DISABLE_CHECKS
#define CHECK_FIND_CHILD(notebook, child)                           \
 gtk_notebook_find_child(notebook, child,                           \
			 G_GNUC_PRETTY_FUNCTION)
#else
#define CHECK_FIND_CHILD(notebook, child)                           \
 gtk_notebook_find_child(notebook, child, NULL)
#endif
 
110
/*** GtkNotebook Methods ***/
111 112
static void gtk_notebook_class_init          (GtkNotebookClass *klass);
static void gtk_notebook_init                (GtkNotebook      *notebook);
113

114
/*** GtkObject Methods ***/
115 116 117 118
static void gtk_notebook_destroy             (GtkObject        *object);
static void gtk_notebook_set_arg             (GtkObject        *object,
				              GtkArg           *arg,
				              guint             arg_id);
119 120 121
static void gtk_notebook_get_arg	     (GtkObject        *object,
					      GtkArg           *arg,
					      guint             arg_id);
122

123
/*** GtkWidget Methods ***/
124 125 126
static void gtk_notebook_map                 (GtkWidget        *widget);
static void gtk_notebook_unmap               (GtkWidget        *widget);
static void gtk_notebook_realize             (GtkWidget        *widget);
127
static void gtk_notebook_unrealize           (GtkWidget        *widget);
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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);
static gint gtk_notebook_key_press           (GtkWidget        *widget,
					      GdkEventKey      *event);
146 147 148 149 150 151
static gint gtk_notebook_focus_in            (GtkWidget        *widget,
					      GdkEventFocus    *event);
static void gtk_notebook_draw_focus          (GtkWidget        *widget);
static void gtk_notebook_style_set           (GtkWidget        *widget,
					      GtkStyle         *previous_style);

152
/*** GtkContainer Methods ***/
153 154 155 156 157 158 159 160
static void gtk_notebook_set_child_arg	     (GtkContainer     *container,
					      GtkWidget        *child,
					      GtkArg           *arg,
					      guint             arg_id);
static void gtk_notebook_get_child_arg	     (GtkContainer     *container,
					      GtkWidget        *child,
					      GtkArg           *arg,
					      guint             arg_id);
161 162 163 164
static void gtk_notebook_add                 (GtkContainer     *container,
					      GtkWidget        *widget);
static void gtk_notebook_remove              (GtkContainer     *container,
					      GtkWidget        *widget);
165 166 167 168 169
static gint gtk_notebook_focus               (GtkContainer     *container,
					      GtkDirectionType  direction);
static void gtk_notebook_set_focus_child     (GtkContainer     *container,
					      GtkWidget        *child);
static GtkType gtk_notebook_child_type       (GtkContainer     *container);
170 171
static void gtk_notebook_forall              (GtkContainer     *container,
					      gboolean		include_internals,
172 173
					      GtkCallback       callback,
					      gpointer          callback_data);
174

175
/*** GtkNotebook Private Functions ***/
176
static void gtk_notebook_panel_realize       (GtkNotebook      *notebook);
177 178
static void gtk_notebook_redraw_tabs         (GtkNotebook      *notebook);
static void gtk_notebook_redraw_arrows       (GtkNotebook      *notebook);
179 180 181 182 183 184 185 186
static void gtk_notebook_focus_changed       (GtkNotebook      *notebook,
					      GtkNotebookPage  *old_page);
static void gtk_notebook_real_remove         (GtkNotebook      *notebook,
					      GList            *list);
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);
187 188 189
static GList *gtk_notebook_find_child        (GtkNotebook      *notebook,
					      GtkWidget        *child,
					      const gchar      *function);
190 191 192 193 194 195 196
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);

197
/*** GtkNotebook Drawing Functions ***/
198 199
static void gtk_notebook_paint               (GtkWidget        *widget,
					      GdkRectangle     *area);
200 201 202
static void gtk_notebook_draw_tab            (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GdkRectangle     *area);
203 204 205 206
static void gtk_notebook_draw_arrow          (GtkNotebook      *notebook,
					      guint             arrow);
static void gtk_notebook_set_shape           (GtkNotebook      *notebook);

207
/*** GtkNotebook Size Allocate Functions ***/
208
static void gtk_notebook_pages_allocate      (GtkNotebook      *notebook);
209 210 211
static void gtk_notebook_page_allocate       (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GtkAllocation    *allocation);
212 213
static void gtk_notebook_calc_tabs           (GtkNotebook      *notebook,
			                      GList            *start,
214 215 216
					      GList           **end,
					      gint             *tab_space,
					      guint             direction);
217

218
/*** GtkNotebook Page Switch Methods ***/
219
static void gtk_notebook_real_switch_page    (GtkNotebook      *notebook,
Lars Hamann's avatar
Lars Hamann committed
220
					      GtkNotebookPage  *page,
221
					      guint             page_num);
222

223
/*** GtkNotebook Page Switch Functions ***/
224 225
static void gtk_notebook_switch_page         (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
226
					      gint              page_num);
227 228
static gint gtk_notebook_page_select         (GtkNotebook      *notebook,
					      gboolean          move_focus);
229 230
static void gtk_notebook_switch_focus_tab    (GtkNotebook      *notebook,
                                              GList            *new_child);
231 232
static void gtk_notebook_menu_switch_page    (GtkWidget        *widget,
					      GtkNotebookPage  *page);
233

234
/*** GtkNotebook Menu Functions ***/
235 236 237 238
static void gtk_notebook_menu_item_create    (GtkNotebook      *notebook,
					      GList            *list);
static void gtk_notebook_menu_label_unparent (GtkWidget        *widget,
					      gpointer          data);
239 240
static void gtk_notebook_menu_detacher       (GtkWidget        *widget,
					      GtkMenu          *menu);
241

Elliot Lee's avatar
Elliot Lee committed
242

243
static GtkContainerClass *parent_class = NULL;
244
static guint notebook_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
245

246
GtkType
247
gtk_notebook_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
248
{
249
  static GtkType notebook_type = 0;
Elliot Lee's avatar
Elliot Lee committed
250 251 252

  if (!notebook_type)
    {
253
      static const GtkTypeInfo notebook_info =
Elliot Lee's avatar
Elliot Lee committed
254 255 256 257 258 259
      {
	"GtkNotebook",
	sizeof (GtkNotebook),
	sizeof (GtkNotebookClass),
	(GtkClassInitFunc) gtk_notebook_class_init,
	(GtkObjectInitFunc) gtk_notebook_init,
260 261
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
262
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      };

      notebook_type = gtk_type_unique (gtk_container_get_type (), &notebook_info);
    }

  return notebook_type;
}

static void
gtk_notebook_class_init (GtkNotebookClass *class)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
  container_class = (GtkContainerClass*) class;
  parent_class = gtk_type_class (gtk_container_get_type ());

283

284 285
  object_class->set_arg = gtk_notebook_set_arg;
  object_class->get_arg = gtk_notebook_get_arg;
286 287
  object_class->destroy = gtk_notebook_destroy;

Elliot Lee's avatar
Elliot Lee committed
288 289 290
  widget_class->map = gtk_notebook_map;
  widget_class->unmap = gtk_notebook_unmap;
  widget_class->realize = gtk_notebook_realize;
291
  widget_class->unrealize = gtk_notebook_unrealize;
Elliot Lee's avatar
Elliot Lee committed
292 293 294 295
  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;
296 297 298 299 300 301
  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;
  widget_class->key_press_event = gtk_notebook_key_press;
  widget_class->focus_in_event = gtk_notebook_focus_in;
302 303
  widget_class->style_set = gtk_notebook_style_set;
   
Elliot Lee's avatar
Elliot Lee committed
304 305
  container_class->add = gtk_notebook_add;
  container_class->remove = gtk_notebook_remove;
306
  container_class->forall = gtk_notebook_forall;
307
  container_class->focus = gtk_notebook_focus;
Lars Hamann's avatar
Lars Hamann committed
308
  container_class->set_focus_child = gtk_notebook_set_focus_child;
309 310
  container_class->get_child_arg = gtk_notebook_get_child_arg;
  container_class->set_child_arg = gtk_notebook_set_child_arg;
311
  container_class->child_type = gtk_notebook_child_type;
312

313
  class->switch_page = gtk_notebook_real_switch_page;
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

  gtk_object_add_arg_type ("GtkNotebook::page", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_PAGE);
  gtk_object_add_arg_type ("GtkNotebook::tab_pos", GTK_TYPE_POSITION_TYPE, GTK_ARG_READWRITE, ARG_TAB_POS);
  gtk_object_add_arg_type ("GtkNotebook::tab_border", GTK_TYPE_UINT, GTK_ARG_WRITABLE, ARG_TAB_BORDER);
  gtk_object_add_arg_type ("GtkNotebook::tab_hborder", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_TAB_HBORDER);
  gtk_object_add_arg_type ("GtkNotebook::tab_vborder", GTK_TYPE_UINT, GTK_ARG_READWRITE, ARG_TAB_VBORDER);
  gtk_object_add_arg_type ("GtkNotebook::show_tabs", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_SHOW_TABS);
  gtk_object_add_arg_type ("GtkNotebook::show_border", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_SHOW_BORDER);
  gtk_object_add_arg_type ("GtkNotebook::scrollable", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_SCROLLABLE);
  gtk_object_add_arg_type ("GtkNotebook::enable_popup", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_ENABLE_POPUP);
  gtk_object_add_arg_type ("GtkNotebook::homogeneous", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_HOMOGENEOUS);

  gtk_container_add_child_arg_type ("GtkNotebook::tab_label", GTK_TYPE_STRING, GTK_ARG_READWRITE, CHILD_ARG_TAB_LABEL);
  gtk_container_add_child_arg_type ("GtkNotebook::menu_label", GTK_TYPE_STRING, GTK_ARG_READWRITE, CHILD_ARG_MENU_LABEL);
  gtk_container_add_child_arg_type ("GtkNotebook::position", GTK_TYPE_INT, GTK_ARG_READWRITE, CHILD_ARG_POSITION);
  gtk_container_add_child_arg_type ("GtkNotebook::tab_fill", GTK_TYPE_BOOL, GTK_ARG_READWRITE, CHILD_ARG_TAB_FILL);
  gtk_container_add_child_arg_type ("GtkNotebook::tab_pack", GTK_TYPE_BOOL, GTK_ARG_READWRITE, CHILD_ARG_TAB_PACK);

  notebook_signals[SWITCH_PAGE] =
    gtk_signal_new ("switch_page",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkNotebookClass, switch_page),
		    gtk_marshal_VOID__POINTER_UINT,
		    GTK_TYPE_NONE, 2,
		    GTK_TYPE_POINTER,
		    GTK_TYPE_UINT);
Elliot Lee's avatar
Elliot Lee committed
341 342
}

343
static void
344 345
gtk_notebook_init (GtkNotebook *notebook)
{
346
  GTK_WIDGET_SET_FLAGS (notebook, GTK_CAN_FOCUS | GTK_RECEIVES_DEFAULT);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  GTK_WIDGET_UNSET_FLAGS (notebook, GTK_NO_WINDOW);

  notebook->cur_page = NULL;
  notebook->children = NULL;
  notebook->first_tab = NULL;
  notebook->focus_tab = NULL;
  notebook->panel = NULL;
  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;
368
  notebook->have_visible_child = FALSE;
369 370
}

371 372 373 374 375 376 377
/**
 * gtk_notebook_new:
 * 
 * Creates a new #GtkNotebook widget with no pages.

 * Return value: the newly created #GtkNotebook
 **/
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
GtkWidget*
gtk_notebook_new (void)
{
  return GTK_WIDGET (gtk_type_new (gtk_notebook_get_type ()));
}

/* Private GtkObject Methods :
 * 
 * gtk_notebook_destroy
 * gtk_notebook_set_arg
 * gtk_notebook_get_arg
 */
static void
gtk_notebook_destroy (GtkObject *object)
{
  GtkNotebook *notebook;
  
  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (object));

  notebook = GTK_NOTEBOOK (object);

  if (notebook->menu)
    gtk_notebook_popup_disable (notebook);

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
gtk_notebook_set_arg (GtkObject *object,
		      GtkArg    *arg,
		      guint      arg_id)
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
{
  GtkNotebook *notebook;

  notebook = GTK_NOTEBOOK (object);

  switch (arg_id)
    {
    case ARG_SHOW_TABS:
      gtk_notebook_set_show_tabs (notebook, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_SHOW_BORDER:
      gtk_notebook_set_show_border (notebook, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_SCROLLABLE:
      gtk_notebook_set_scrollable (notebook, GTK_VALUE_BOOL (*arg));
      break;
    case ARG_ENABLE_POPUP:
      if (GTK_VALUE_BOOL (*arg))
	gtk_notebook_popup_enable (notebook);
      else
	gtk_notebook_popup_disable (notebook);
      break;
432 433 434
    case ARG_HOMOGENEOUS:
      gtk_notebook_set_homogeneous_tabs (notebook, GTK_VALUE_BOOL (*arg));
      break;  
435 436 437 438 439 440 441 442 443
    case ARG_PAGE:
      gtk_notebook_set_page (notebook, GTK_VALUE_INT (*arg));
      break;
    case ARG_TAB_POS:
      gtk_notebook_set_tab_pos (notebook, GTK_VALUE_ENUM (*arg));
      break;
    case ARG_TAB_BORDER:
      gtk_notebook_set_tab_border (notebook, GTK_VALUE_UINT (*arg));
      break;
444 445 446 447 448 449
    case ARG_TAB_HBORDER:
      gtk_notebook_set_tab_hborder (notebook, GTK_VALUE_UINT (*arg));
      break;
    case ARG_TAB_VBORDER:
      gtk_notebook_set_tab_vborder (notebook, GTK_VALUE_UINT (*arg));
      break;
450 451 452 453 454 455
    default:
      break;
    }
}

static void
456 457 458
gtk_notebook_get_arg (GtkObject *object,
		      GtkArg    *arg,
		      guint      arg_id)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
{
  GtkNotebook *notebook;

  notebook = GTK_NOTEBOOK (object);

  switch (arg_id)
    {
    case ARG_SHOW_TABS:
      GTK_VALUE_BOOL (*arg) = notebook->show_tabs;
      break;
    case ARG_SHOW_BORDER:
      GTK_VALUE_BOOL (*arg) = notebook->show_border;
      break;
    case ARG_SCROLLABLE:
      GTK_VALUE_BOOL (*arg) = notebook->scrollable;
      break;
    case ARG_ENABLE_POPUP:
      GTK_VALUE_BOOL (*arg) = notebook->menu != NULL;
      break;
478 479 480
    case ARG_HOMOGENEOUS:
      GTK_VALUE_BOOL (*arg) = notebook->homogeneous;
      break;
481
    case ARG_PAGE:
482
      GTK_VALUE_INT (*arg) = gtk_notebook_get_current_page (notebook);
483 484 485 486
      break;
    case ARG_TAB_POS:
      GTK_VALUE_ENUM (*arg) = notebook->tab_pos;
      break;
487 488 489 490 491 492
    case ARG_TAB_HBORDER:
      GTK_VALUE_UINT (*arg) = notebook->tab_hborder;
      break;
    case ARG_TAB_VBORDER:
      GTK_VALUE_UINT (*arg) = notebook->tab_vborder;
      break;
493 494 495 496 497 498
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/* 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_key_press
 * gtk_notebook_focus_in
 * gtk_notebook_focus_out
 * gtk_notebook_draw_focus
 * gtk_notebook_style_set
 */
518
static void
519
gtk_notebook_map (GtkWidget *widget)
520 521
{
  GtkNotebook *notebook;
522 523
  GtkNotebookPage *page;
  GList *children;
524

525 526
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
527

528
  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
529

530
  notebook = GTK_NOTEBOOK (widget);
531

532 533 534 535
  if (notebook->cur_page && 
      GTK_WIDGET_VISIBLE (notebook->cur_page->child) &&
      !GTK_WIDGET_MAPPED (notebook->cur_page->child))
    gtk_widget_map (notebook->cur_page->child);
536

537
  if (notebook->scrollable)
538
    gtk_notebook_pages_allocate (notebook);
539 540 541
  else
    {
      children = notebook->children;
542

543
      while (children)
544
	{
545 546
	  page = children->data;
	  children = children->next;
547

Owen Taylor's avatar
Owen Taylor committed
548 549
	  if (page->tab_label &&
	      GTK_WIDGET_VISIBLE (page->tab_label) &&
550 551
	      !GTK_WIDGET_MAPPED (page->tab_label))
	    gtk_widget_map (page->tab_label);
552
	}
553
    }
554 555

  gdk_window_show (widget->window);
556
}
557

558 559 560 561 562
static void
gtk_notebook_unmap (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
563

564 565 566 567
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
  gdk_window_hide (widget->window);
  if (GTK_NOTEBOOK (widget)->panel)
    gdk_window_hide (GTK_NOTEBOOK (widget)->panel);
568 569 570
}

static void
571
gtk_notebook_realize (GtkWidget *widget)
572 573
{
  GtkNotebook *notebook;
574 575
  GdkWindowAttr attributes;
  gint attributes_mask;
576

577 578
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
579

580 581
  notebook = GTK_NOTEBOOK (widget);
  GTK_WIDGET_SET_FLAGS (notebook, GTK_REALIZED);
582

583 584 585 586 587 588 589 590 591 592 593
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
			    GDK_BUTTON_RELEASE_MASK | GDK_KEY_PRESS_MASK);
594

595
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
596

597 598
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, notebook);
599

600 601
  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
Elliot Lee's avatar
Elliot Lee committed
602

603 604 605
  gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
  if (notebook->scrollable)
    gtk_notebook_panel_realize (notebook);
Elliot Lee's avatar
Elliot Lee committed
606 607
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
static void
gtk_notebook_unrealize (GtkWidget *widget)
{
  GtkNotebook *notebook;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));

  notebook = GTK_NOTEBOOK (widget);

  if (notebook->panel)
    {
      gdk_window_set_user_data (notebook->panel, NULL);
      gdk_window_destroy (notebook->panel);
      notebook->panel = NULL;
    }

  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

629
static void
630 631
gtk_notebook_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
632 633
{
  GtkNotebook *notebook;
634 635
  GtkNotebookPage *page;
  GList *children;
636
  GtkRequisition child_requisition;
637 638
  gboolean switch_page = FALSE;
  gint vis_pages;
639

640 641 642
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
  g_return_if_fail (requisition != NULL);
643

644 645 646
  notebook = GTK_NOTEBOOK (widget);
  widget->requisition.width = 0;
  widget->requisition.height = 0;
647

648 649 650 651
  for (children = notebook->children, vis_pages = 0; children;
       children = children->next)
    {
      page = children->data;
652

653 654 655
      if (GTK_WIDGET_VISIBLE (page->child))
	{
	  vis_pages++;
656
	  gtk_widget_size_request (page->child, &child_requisition);
657 658
	  
	  widget->requisition.width = MAX (widget->requisition.width,
659
					   child_requisition.width);
660
	  widget->requisition.height = MAX (widget->requisition.height,
661
					    child_requisition.height);
Elliot Lee's avatar
Elliot Lee committed
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	  if (GTK_WIDGET_MAPPED (page->child) && page != notebook->cur_page)
	    gtk_widget_unmap (page->child);
	  if (notebook->menu && page->menu_label->parent &&
	      !GTK_WIDGET_VISIBLE (page->menu_label->parent))
	    gtk_widget_show (page->menu_label->parent);
	}
      else
	{
	  if (page == notebook->cur_page)
	    switch_page = TRUE;
	  if (notebook->menu && page->menu_label->parent &&
	      GTK_WIDGET_VISIBLE (page->menu_label->parent))
	    gtk_widget_hide (page->menu_label->parent);
	}
    }
678

679 680
  if (notebook->show_border || notebook->show_tabs)
    {
681 682
      widget->requisition.width += widget->style->xthickness * 2;
      widget->requisition.height += widget->style->ythickness * 2;
683

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
      if (notebook->show_tabs)
	{
	  gint tab_width = 0;
	  gint tab_height = 0;
	  gint tab_max = 0;
	  gint padding;
	  
	  for (children = notebook->children; children;
	       children = children->next)
	    {
	      page = children->data;
	      
	      if (GTK_WIDGET_VISIBLE (page->child))
		{
		  if (!GTK_WIDGET_VISIBLE (page->tab_label))
		    gtk_widget_show (page->tab_label);
Elliot Lee's avatar
Elliot Lee committed
700

701 702
		  gtk_widget_size_request (page->tab_label,
					   &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
703

704
		  page->requisition.width = 
705
		    child_requisition.width +
706
		    2 * widget->style->xthickness;
707
		  page->requisition.height = 
708
		    child_requisition.height +
709
		    2 * widget->style->ythickness;
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
		  
		  switch (notebook->tab_pos)
		    {
		    case GTK_POS_TOP:
		    case GTK_POS_BOTTOM:
		      page->requisition.height += 2 * (notebook->tab_vborder +
						       FOCUS_WIDTH);
		      tab_height = MAX (tab_height, page->requisition.height);
		      tab_max = MAX (tab_max, page->requisition.width);
		      break;
		    case GTK_POS_LEFT:
		    case GTK_POS_RIGHT:
		      page->requisition.width += 2 * (notebook->tab_hborder +
						      FOCUS_WIDTH);
		      tab_width = MAX (tab_width, page->requisition.width);
		      tab_max = MAX (tab_max, page->requisition.height);
		      break;
		    }
		}
	      else if (GTK_WIDGET_VISIBLE (page->tab_label))
		gtk_widget_hide (page->tab_label);
	    }
732

733
	  children = notebook->children;
734

735 736 737 738 739 740 741 742
	  if (vis_pages)
	    {
	      switch (notebook->tab_pos)
		{
		case GTK_POS_TOP:
		case GTK_POS_BOTTOM:
		  if (tab_height == 0)
		    break;
Elliot Lee's avatar
Elliot Lee committed
743

744 745 746
		  if (notebook->scrollable && vis_pages > 1 && 
		      widget->requisition.width < tab_width)
		    tab_height = MAX (tab_height, ARROW_SIZE);
747

748 749 750 751 752 753 754 755 756 757
		  padding = 2 * (TAB_CURVATURE + FOCUS_WIDTH +
				 notebook->tab_hborder) - TAB_OVERLAP;
		  tab_max += padding;
		  while (children)
		    {
		      page = children->data;
		      children = children->next;
		  
		      if (!GTK_WIDGET_VISIBLE (page->child))
			continue;
758

759 760 761 762
		      if (notebook->homogeneous)
			page->requisition.width = tab_max;
		      else
			page->requisition.width += padding;
Elliot Lee's avatar
Elliot Lee committed
763

764 765 766
		      tab_width += page->requisition.width;
		      page->requisition.height = tab_height;
		    }
Elliot Lee's avatar
Elliot Lee committed
767

768 769 770
		  if (notebook->scrollable && vis_pages > 1 &&
		      widget->requisition.width < tab_width)
		    tab_width = tab_max + 2 * (ARROW_SIZE + ARROW_SPACING);
Elliot Lee's avatar
Elliot Lee committed
771

772 773 774 775 776 777 778
                  if (notebook->homogeneous && !notebook->scrollable)
                    widget->requisition.width = MAX (widget->requisition.width,
                                                     vis_pages * tab_max +
                                                     TAB_OVERLAP);
                  else
                    widget->requisition.width = MAX (widget->requisition.width,
                                                     tab_width + TAB_OVERLAP);
Elliot Lee's avatar
Elliot Lee committed
779

780 781 782 783 784 785
		  widget->requisition.height += tab_height;
		  break;
		case GTK_POS_LEFT:
		case GTK_POS_RIGHT:
		  if (tab_width == 0)
		    break;
786

787 788 789
		  if (notebook->scrollable && vis_pages > 1 && 
		      widget->requisition.height < tab_height)
		    tab_width = MAX (tab_width, ARROW_SPACING +2 * ARROW_SIZE);
790

791 792 793
		  padding = 2 * (TAB_CURVATURE + FOCUS_WIDTH +
				 notebook->tab_vborder) - TAB_OVERLAP;
		  tab_max += padding;
794

795 796 797 798
		  while (children)
		    {
		      page = children->data;
		      children = children->next;
799

800 801
		      if (!GTK_WIDGET_VISIBLE (page->child))
			continue;
802

803
		      page->requisition.width   = tab_width;
804

805 806 807 808
		      if (notebook->homogeneous)
			page->requisition.height = tab_max;
		      else
			page->requisition.height += padding;
Elliot Lee's avatar
Elliot Lee committed
809

810 811
		      tab_height += page->requisition.height;
		    }
Elliot Lee's avatar
Elliot Lee committed
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		  if (notebook->scrollable && vis_pages > 1 && 
		      widget->requisition.height < tab_height)
		    tab_height = tab_max + ARROW_SIZE + ARROW_SPACING;

		  widget->requisition.width += tab_width;

                  if (notebook->homogeneous && !notebook->scrollable)
                    widget->requisition.height =
		      MAX (widget->requisition.height,
			   vis_pages * tab_max + TAB_OVERLAP);
                  else
                    widget->requisition.height =
		      MAX (widget->requisition.height,
			   tab_height + TAB_OVERLAP);

		  if (!notebook->homogeneous || notebook->scrollable)
		    vis_pages = 1;
		  widget->requisition.height = MAX (widget->requisition.height,
						    vis_pages * tab_max +
						    TAB_OVERLAP);
		  break;
		}
	    }
836
	}
Owen Taylor's avatar
Owen Taylor committed
837 838 839 840 841 842 843 844 845 846 847
      else
	{
	  for (children = notebook->children; children;
	       children = children->next)
	    {
	      page = children->data;
	      
	      if (page->tab_label && GTK_WIDGET_VISIBLE (page->tab_label))
		gtk_widget_hide (page->tab_label);
	    }
	}
Elliot Lee's avatar
Elliot Lee committed
848 849
    }

850 851
  widget->requisition.width += GTK_CONTAINER (widget)->border_width * 2;
  widget->requisition.height += GTK_CONTAINER (widget)->border_width * 2;
Elliot Lee's avatar
Elliot Lee committed
852

853
  if (switch_page)
854
    {
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
      if (vis_pages)
	{
	  for (children = notebook->children; children;
	       children = children->next)
	    {
	      page = children->data;
	      if (GTK_WIDGET_VISIBLE (page->child))
		{
		  gtk_notebook_switch_page (notebook, page, -1);
		  break;
		}
	    }
	}
      else if (GTK_WIDGET_VISIBLE (widget))
	{
	  widget->requisition.width = GTK_CONTAINER (widget)->border_width * 2;
	  widget->requisition.height= GTK_CONTAINER (widget)->border_width * 2;
	}
873
    }
874
  if (vis_pages && !notebook->cur_page)
875
    {
876 877 878 879 880 881
      children = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, TRUE);
      if (children)
	{
	  notebook->first_tab = children;
	  gtk_notebook_switch_page (notebook, GTK_NOTEBOOK_PAGE (children),-1);
	}
882
    }
883 884 885
}

static void
886 887
gtk_notebook_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
888 889 890
{
  GtkNotebook *notebook;
  GtkNotebookPage *page;
891
  GtkAllocation child_allocation;
892 893 894
  GList *children;

  g_return_if_fail (widget != NULL);
895 896
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
  g_return_if_fail (allocation != NULL);
897

898 899 900 901 902
  widget->allocation = *allocation;
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (widget->window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);
903

904 905
  notebook = GTK_NOTEBOOK (widget);
  if (notebook->children)
Elliot Lee's avatar
Elliot Lee committed
906
    {
907 908
      child_allocation.x = GTK_CONTAINER (widget)->border_width;
      child_allocation.y = GTK_CONTAINER (widget)->border_width;
909 910
      child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
      child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
911 912

      if (notebook->show_tabs || notebook->show_border)
913
	{
914 915
	  child_allocation.x += widget->style->xthickness;
	  child_allocation.y += widget->style->ythickness;
916
	  child_allocation.width = MAX (1, (gint)child_allocation.width -
917
					(gint) widget->style->xthickness * 2);
918
	  child_allocation.height = MAX (1, (gint)child_allocation.height -
919
					 (gint) widget->style->ythickness * 2);
920 921 922 923 924 925 926 927 928

	  if (notebook->show_tabs && notebook->children && notebook->cur_page)
	    {
	      switch (notebook->tab_pos)
		{
		case GTK_POS_TOP:
		  child_allocation.y += notebook->cur_page->requisition.height;
		case GTK_POS_BOTTOM:
		  child_allocation.height =
929 930
		    MAX (1, (gint)child_allocation.height -
			 (gint)notebook->cur_page->requisition.height);
931 932 933 934 935
		  break;
		case GTK_POS_LEFT:
		  child_allocation.x += notebook->cur_page->requisition.width;
		case GTK_POS_RIGHT:
		  child_allocation.width =
936 937
		    MAX (1, (gint)child_allocation.width -
			 (gint)notebook->cur_page->requisition.width);
938 939 940
		  break;
		}
	    }
941
	}
942 943 944 945 946 947 948 949 950 951 952

      children = notebook->children;
      while (children)
	{
	  page = children->data;
	  children = children->next;
	  
	  if (GTK_WIDGET_VISIBLE (page->child))
	    gtk_widget_size_allocate (page->child, &child_allocation);
	}

953
      gtk_notebook_pages_allocate (notebook);
954
    }
955
   gtk_notebook_set_shape (notebook);
956
}
Elliot Lee's avatar
Elliot Lee committed
957

958 959 960
static gint
gtk_notebook_expose (GtkWidget      *widget,
		     GdkEventExpose *event)
961
{
962 963 964 965 966 967
  GtkNotebook *notebook;
  GdkRectangle child_area;
   
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
968

969 970 971
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      notebook = GTK_NOTEBOOK (widget);
972

973
      gtk_notebook_paint (widget, &event->area);
974 975 976 977 978
      if (notebook->show_tabs)
	{
	  if (notebook->cur_page && 
	      gtk_widget_intersect (notebook->cur_page->tab_label, 
				    &event->area, &child_area))
979
	    gtk_notebook_draw_focus (widget);
980
	}
981

982 983 984 985 986
      
      if (notebook->cur_page)
	gtk_container_propagate_expose (GTK_CONTAINER (notebook),
					notebook->cur_page->child,
					event);
Elliot Lee's avatar
Elliot Lee committed
987
    }
988

989
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
990 991
}

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
static gboolean
gtk_notebook_arrow_button_press (GtkNotebook    *notebook,
				 GdkEventButton *event)
{
  GtkWidget *widget = GTK_WIDGET (notebook);
  
  GtkArrowType arrow = event->x <= ARROW_SIZE + ARROW_SPACING / 2 ? GTK_ARROW_LEFT : GTK_ARROW_RIGHT;

  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);
  
  notebook->button = event->button;
  notebook->click_child = arrow;
  
  if (event->button == 1)
    {
      if (!notebook->focus_tab ||
	  gtk_notebook_search_page (notebook, notebook->focus_tab,
				    arrow == GTK_ARROW_LEFT ? STEP_PREV : STEP_NEXT,
				    TRUE))
	gtk_container_focus (GTK_CONTAINER (notebook),
			     arrow == GTK_ARROW_LEFT ? GTK_DIR_LEFT : GTK_DIR_RIGHT);
      
      if (!notebook->timer)
	{
	  notebook->timer = gtk_timeout_add 
	    (NOTEBOOK_INIT_SCROLL_DELAY, 
	     (GtkFunction) gtk_notebook_timer, (gpointer) notebook);
	  notebook->need_timer = TRUE;
	}
    }
  else if (event->button == 2)
    gtk_notebook_page_select (notebook, TRUE);
  else if (event->button == 3)
    gtk_notebook_switch_focus_tab (notebook,
				   gtk_notebook_search_page (notebook,
							     NULL,
							     arrow == GTK_ARROW_LEFT ? STEP_NEXT : STEP_PREV,
							     TRUE));
  gtk_notebook_redraw_arrows (notebook);

  return TRUE;
}

static gboolean
1037 1038
gtk_notebook_button_press (GtkWidget      *widget,
			   GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
1039
{
1040 1041
  GtkNotebook *notebook;
  GtkNotebookPage *page;
Elliot Lee's avatar
Elliot Lee committed
1042
  GList *children;
1043
  gint num;
Elliot Lee's avatar
Elliot Lee committed
1044

1045 1046 1047
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_NOTEBOOK (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1048

1049
  notebook = GTK_NOTEBOOK (widget);
Elliot Lee's avatar
Elliot Lee committed
1050

1051 1052 1053
  if (event->type != GDK_BUTTON_PRESS || !notebook->children ||
      notebook->button)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1054

1055
  if (event->window == notebook->panel)
Elliot Lee's avatar
Elliot Lee committed
1056
    {
1057
      return gtk_notebook_arrow_button_press (notebook, event);
1058
    }
1059
  else if (event->window == widget->window)
1060
    {
1061 1062 1063 1064
      if (event->button == 3 && notebook->menu)
	{
	  gtk_menu_popup (GTK_MENU (notebook->menu), NULL, NULL, 
			  NULL, NULL, 3, event->time);
1065
	  return TRUE;