gtknotebook.c 114 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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
 */
#include "gtknotebook.h"
20
#include "gtksignal.h"
21 22 23 24 25 26
#include "gtkmain.h"
#include "gtkmenu.h"
#include "gtkmenuitem.h"
#include "gtklabel.h"
#include <gdk/gdkkeysyms.h>
#include <stdio.h>
27
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
28 29 30 31


#define TAB_OVERLAP    2
#define TAB_CURVATURE  1
32 33
#define ARROW_SIZE     12
#define ARROW_SPACING  0
34
#define FOCUS_WIDTH    1
35 36
#define NOTEBOOK_INIT_SCROLL_DELAY (200)
#define NOTEBOOK_SCROLL_DELAY      (100)
Elliot Lee's avatar
Elliot Lee committed
37 38


39 40 41 42 43
enum {
  SWITCH_PAGE,
  LAST_SIGNAL
};

44 45 46 47 48
enum {
  STEP_PREV,
  STEP_NEXT
};

49 50 51 52 53 54 55
enum {
  ARG_0,
  ARG_TAB_POS,
  ARG_SHOW_TABS,
  ARG_SHOW_BORDER,
  ARG_SCROLLABLE,
  ARG_TAB_BORDER,
56 57
  ARG_TAB_HBORDER,
  ARG_TAB_VBORDER,
58 59 60 61 62 63 64 65
  ARG_PAGE,
  ARG_ENABLE_POPUP
};

enum {
  CHILD_ARG_0,
  CHILD_ARG_TAB_LABEL,
  CHILD_ARG_MENU_LABEL,
66 67 68 69
  CHILD_ARG_POSITION,
  CHILD_ARG_TAB_EXPAND,
  CHILD_ARG_TAB_FILL,
  CHILD_ARG_TAB_PACK
70 71
};

72
/** GtkNotebook Methods **/
73 74
static void gtk_notebook_class_init          (GtkNotebookClass *klass);
static void gtk_notebook_init                (GtkNotebook      *notebook);
75 76 77 78 79 80

/** GtkObject Methods **/
static void gtk_notebook_destroy             (GtkObject        *object);
static void gtk_notebook_set_arg             (GtkObject        *object,
				              GtkArg           *arg,
				              guint             arg_id);
81 82 83
static void gtk_notebook_get_arg	     (GtkObject        *object,
					      GtkArg           *arg,
					      guint             arg_id);
84 85

/** GtkWidget Methods **/
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static void gtk_notebook_map                 (GtkWidget        *widget);
static void gtk_notebook_unmap               (GtkWidget        *widget);
static void gtk_notebook_realize             (GtkWidget        *widget);
static void gtk_notebook_size_request        (GtkWidget        *widget,
					      GtkRequisition   *requisition);
static void gtk_notebook_size_allocate       (GtkWidget        *widget,
					      GtkAllocation    *allocation);
static void gtk_notebook_draw                (GtkWidget        *widget,
					      GdkRectangle     *area);
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);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static gint gtk_notebook_focus_in            (GtkWidget        *widget,
					      GdkEventFocus    *event);
static gint gtk_notebook_focus_out           (GtkWidget        *widget,
					      GdkEventFocus    *event);
static void gtk_notebook_draw_focus          (GtkWidget        *widget);
static void gtk_notebook_style_set           (GtkWidget        *widget,
					      GtkStyle         *previous_style);

/** GtkContainer Methods **/
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);
126 127 128 129
static void gtk_notebook_add                 (GtkContainer     *container,
					      GtkWidget        *widget);
static void gtk_notebook_remove              (GtkContainer     *container,
					      GtkWidget        *widget);
130 131 132 133 134
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);
135 136
static void gtk_notebook_forall              (GtkContainer     *container,
					      gboolean		include_internals,
137 138
					      GtkCallback       callback,
					      gpointer          callback_data);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

/** GtkNotebook Private Functions **/
static void gtk_notebook_panel_realize       (GtkNotebook      *notebook);
static void gtk_notebook_expose_tabs         (GtkNotebook      *notebook);
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);
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);

/** GtkNotebook Drawing Functions **/
static void gtk_notebook_paint               (GtkWidget        *widget,
					      GdkRectangle     *area);
161 162 163
static void gtk_notebook_draw_tab            (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GdkRectangle     *area);
164 165 166 167 168
static void gtk_notebook_draw_arrow          (GtkNotebook      *notebook,
					      guint             arrow);
static void gtk_notebook_set_shape           (GtkNotebook      *notebook);

/** GtkNotebook Size Allocate Functions **/
169 170 171 172 173
static void gtk_notebook_pages_allocate      (GtkNotebook      *notebook,
					      GtkAllocation    *allocation);
static void gtk_notebook_page_allocate       (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
					      GtkAllocation    *allocation);
174 175
static void gtk_notebook_calc_tabs           (GtkNotebook      *notebook,
			                      GList            *start,
176 177 178
					      GList           **end,
					      gint             *tab_space,
					      guint             direction);
179 180

/** GtkNotebook Page Switch Methods **/
181
static void gtk_notebook_real_switch_page    (GtkNotebook      *notebook,
Lars Hamann's avatar
Lars Hamann committed
182
					      GtkNotebookPage  *page,
183
					      guint             page_num);
184 185 186 187

/** GtkNotebook Page Switch Functions **/
static void gtk_notebook_switch_page         (GtkNotebook      *notebook,
					      GtkNotebookPage  *page,
188
					      gint              page_num);
189 190 191
static gint gtk_notebook_page_select         (GtkNotebook      *notebook);
static void gtk_notebook_switch_focus_tab    (GtkNotebook      *notebook,
                                              GList            *new_child);
192 193
static void gtk_notebook_menu_switch_page    (GtkWidget        *widget,
					      GtkNotebookPage  *page);
194 195 196 197 198 199

/** GtkNotebook Menu Functions **/
static void gtk_notebook_menu_item_create    (GtkNotebook      *notebook,
					      GList            *list);
static void gtk_notebook_menu_label_unparent (GtkWidget        *widget,
					      gpointer          data);
200 201
static void gtk_notebook_menu_detacher       (GtkWidget        *widget,
					      GtkMenu          *menu);
202

Elliot Lee's avatar
Elliot Lee committed
203

204
static GtkContainerClass *parent_class = NULL;
205
static guint notebook_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
206

207
GtkType
208
gtk_notebook_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
209
{
210
  static GtkType notebook_type = 0;
Elliot Lee's avatar
Elliot Lee committed
211 212 213

  if (!notebook_type)
    {
214
      static const GtkTypeInfo notebook_info =
Elliot Lee's avatar
Elliot Lee committed
215 216 217 218 219 220
      {
	"GtkNotebook",
	sizeof (GtkNotebook),
	sizeof (GtkNotebookClass),
	(GtkClassInitFunc) gtk_notebook_class_init,
	(GtkObjectInitFunc) gtk_notebook_init,
221 222
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
223
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      };

      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 ());

244 245
  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);
246
  gtk_object_add_arg_type ("GtkNotebook::tab_border", GTK_TYPE_UINT, GTK_ARG_WRITABLE, ARG_TAB_BORDER);
247 248
  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);
249 250 251 252 253 254
  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_container_add_child_arg_type ("GtkNotebook::tab_label", GTK_TYPE_STRING, GTK_ARG_READWRITE, CHILD_ARG_TAB_LABEL);
255 256
  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);
257 258
  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);
259

260 261
  notebook_signals[SWITCH_PAGE] =
    gtk_signal_new ("switch_page",
262 263 264 265 266
		    GTK_RUN_LAST,
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkNotebookClass, switch_page),
		    gtk_marshal_NONE__POINTER_UINT,
		    GTK_TYPE_NONE, 2,
267
		    GTK_TYPE_POINTER,
268
		    GTK_TYPE_UINT);
269 270 271

  gtk_object_class_add_signals (object_class, notebook_signals, LAST_SIGNAL);

272 273
  object_class->set_arg = gtk_notebook_set_arg;
  object_class->get_arg = gtk_notebook_get_arg;
274 275
  object_class->destroy = gtk_notebook_destroy;

Elliot Lee's avatar
Elliot Lee committed
276 277 278 279 280 281 282 283
  widget_class->map = gtk_notebook_map;
  widget_class->unmap = gtk_notebook_unmap;
  widget_class->realize = gtk_notebook_realize;
  widget_class->size_request = gtk_notebook_size_request;
  widget_class->size_allocate = gtk_notebook_size_allocate;
  widget_class->draw = gtk_notebook_draw;
  widget_class->expose_event = gtk_notebook_expose;
  widget_class->button_press_event = gtk_notebook_button_press;
284 285 286 287 288 289 290 291
  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;
  widget_class->focus_out_event = gtk_notebook_focus_out;
  widget_class->draw_focus = gtk_notebook_draw_focus;
292 293
  widget_class->style_set = gtk_notebook_style_set;
   
Elliot Lee's avatar
Elliot Lee committed
294 295
  container_class->add = gtk_notebook_add;
  container_class->remove = gtk_notebook_remove;
296
  container_class->forall = gtk_notebook_forall;
297
  container_class->focus = gtk_notebook_focus;
Lars Hamann's avatar
Lars Hamann committed
298
  container_class->set_focus_child = gtk_notebook_set_focus_child;
299 300
  container_class->get_child_arg = gtk_notebook_get_child_arg;
  container_class->set_child_arg = gtk_notebook_set_child_arg;
301
  container_class->child_type = gtk_notebook_child_type;
302

303
  class->switch_page = gtk_notebook_real_switch_page;
Elliot Lee's avatar
Elliot Lee committed
304 305
}

306
static void
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
gtk_notebook_init (GtkNotebook *notebook)
{
  GTK_WIDGET_SET_FLAGS (notebook, GTK_CAN_FOCUS);
  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;
331
  notebook->have_visible_child = FALSE;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

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)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
{
  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;
    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;
397 398 399 400 401 402
    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;
403 404 405 406 407 408
    default:
      break;
    }
}

static void
409 410 411
gtk_notebook_get_arg (GtkObject *object,
		      GtkArg    *arg,
		      guint      arg_id)
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_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;
    case ARG_PAGE:
432
      GTK_VALUE_INT (*arg) = gtk_notebook_get_current_page (notebook);
433 434 435 436
      break;
    case ARG_TAB_POS:
      GTK_VALUE_ENUM (*arg) = notebook->tab_pos;
      break;
437 438 439 440 441 442
    case ARG_TAB_HBORDER:
      GTK_VALUE_UINT (*arg) = notebook->tab_hborder;
      break;
    case ARG_TAB_VBORDER:
      GTK_VALUE_UINT (*arg) = notebook->tab_vborder;
      break;
443 444 445 446 447 448
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
/* Private GtkWidget Methods :
 * 
 * gtk_notebook_map
 * gtk_notebook_unmap
 * gtk_notebook_realize
 * gtk_notebook_size_request
 * gtk_notebook_size_allocate
 * gtk_notebook_draw
 * 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
 */
469
static void
470
gtk_notebook_map (GtkWidget *widget)
471 472
{
  GtkNotebook *notebook;
473 474
  GtkNotebookPage *page;
  GList *children;
475

476 477
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
478

479
  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
480

481
  notebook = GTK_NOTEBOOK (widget);
482

483 484 485 486
  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);
487

488 489 490 491 492
  if (notebook->scrollable)
      gtk_notebook_pages_allocate (notebook, &(widget->allocation));
  else
    {
      children = notebook->children;
493

494
      while (children)
495
	{
496 497
	  page = children->data;
	  children = children->next;
498

499 500 501 502
	  if (page->tab_label && 
	      GTK_WIDGET_VISIBLE (page->child) && 
	      !GTK_WIDGET_MAPPED (page->tab_label))
	    gtk_widget_map (page->tab_label);
503
	}
504
    }
505 506

  gdk_window_show (widget->window);
507
}
508

509 510 511 512 513
static void
gtk_notebook_unmap (GtkWidget *widget)
{
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
514

515 516 517 518
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
  gdk_window_hide (widget->window);
  if (GTK_NOTEBOOK (widget)->panel)
    gdk_window_hide (GTK_NOTEBOOK (widget)->panel);
519 520 521
}

static void
522
gtk_notebook_realize (GtkWidget *widget)
523 524
{
  GtkNotebook *notebook;
525 526
  GdkWindowAttr attributes;
  gint attributes_mask;
527

528 529
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
530

531 532
  notebook = GTK_NOTEBOOK (widget);
  GTK_WIDGET_SET_FLAGS (notebook, GTK_REALIZED);
533

534 535 536 537 538 539 540 541 542 543 544
  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);
545

546
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
547

548 549
  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, notebook);
550

551 552
  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
553

554 555 556
  gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
  if (notebook->scrollable)
    gtk_notebook_panel_realize (notebook);
Elliot Lee's avatar
Elliot Lee committed
557 558
}

559
static void
560 561
gtk_notebook_size_request (GtkWidget      *widget,
			   GtkRequisition *requisition)
562 563
{
  GtkNotebook *notebook;
564 565
  GtkNotebookPage *page;
  GList *children;
566
  GtkRequisition child_requisition;
567 568
  gboolean switch_page = FALSE;
  gint vis_pages;
569

570 571 572
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
  g_return_if_fail (requisition != NULL);
573

574 575 576
  notebook = GTK_NOTEBOOK (widget);
  widget->requisition.width = 0;
  widget->requisition.height = 0;
577

578 579 580 581
  for (children = notebook->children, vis_pages = 0; children;
       children = children->next)
    {
      page = children->data;
582

583 584 585
      if (GTK_WIDGET_VISIBLE (page->child))
	{
	  vis_pages++;
586
	  gtk_widget_size_request (page->child, &child_requisition);
587 588
	  
	  widget->requisition.width = MAX (widget->requisition.width,
589
					   child_requisition.width);
590
	  widget->requisition.height = MAX (widget->requisition.height,
591
					    child_requisition.height);
Elliot Lee's avatar
Elliot Lee committed
592

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	  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);
	}
    }
608

609 610 611 612
  if (notebook->show_border || notebook->show_tabs)
    {
      widget->requisition.width += widget->style->klass->xthickness * 2;
      widget->requisition.height += widget->style->klass->ythickness * 2;
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
      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
630

631 632
		  gtk_widget_size_request (page->tab_label,
					   &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
633

634
		  page->requisition.width = 
635
		    child_requisition.width +
636 637
		    2 * widget->style->klass->xthickness;
		  page->requisition.height = 
638
		    child_requisition.height +
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
		    2 * widget->style->klass->ythickness;
		  
		  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);
	    }
662

663
	  children = notebook->children;
664

665 666 667 668 669 670 671 672
	  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
673

674 675 676
		  if (notebook->scrollable && vis_pages > 1 && 
		      widget->requisition.width < tab_width)
		    tab_height = MAX (tab_height, ARROW_SIZE);
677

678 679 680 681 682 683 684 685 686 687
		  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;
688

689 690 691 692
		      if (notebook->homogeneous)
			page->requisition.width = tab_max;
		      else
			page->requisition.width += padding;
Elliot Lee's avatar
Elliot Lee committed
693

694 695 696
		      tab_width += page->requisition.width;
		      page->requisition.height = tab_height;
		    }
Elliot Lee's avatar
Elliot Lee committed
697

698 699 700
		  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
701

702 703 704 705 706 707 708
                  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
709

710 711 712 713 714 715
		  widget->requisition.height += tab_height;
		  break;
		case GTK_POS_LEFT:
		case GTK_POS_RIGHT:
		  if (tab_width == 0)
		    break;
716

717 718 719
		  if (notebook->scrollable && vis_pages > 1 && 
		      widget->requisition.height < tab_height)
		    tab_width = MAX (tab_width, ARROW_SPACING +2 * ARROW_SIZE);
720

721 722 723
		  padding = 2 * (TAB_CURVATURE + FOCUS_WIDTH +
				 notebook->tab_vborder) - TAB_OVERLAP;
		  tab_max += padding;
724

725 726 727 728
		  while (children)
		    {
		      page = children->data;
		      children = children->next;
729

730 731
		      if (!GTK_WIDGET_VISIBLE (page->child))
			continue;
732

733
		      page->requisition.width   = tab_width;
734

735 736 737 738
		      if (notebook->homogeneous)
			page->requisition.height = tab_max;
		      else
			page->requisition.height += padding;
Elliot Lee's avatar
Elliot Lee committed
739

740 741
		      tab_height += page->requisition.height;
		    }
Elliot Lee's avatar
Elliot Lee committed
742

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
		  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;
		}
	    }
766
	}
Elliot Lee's avatar
Elliot Lee committed
767 768
    }

769 770
  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
771

772
  if (switch_page)
773
    {
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
      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;
	}
792
    }
793
  if (vis_pages && !notebook->cur_page)
794
    {
795 796 797 798 799 800
      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);
	}
801
    }
802 803 804
}

static void
805 806
gtk_notebook_size_allocate (GtkWidget     *widget,
			    GtkAllocation *allocation)
807 808 809
{
  GtkNotebook *notebook;
  GtkNotebookPage *page;
810
  GtkAllocation child_allocation;
811 812 813
  GList *children;

  g_return_if_fail (widget != NULL);
814 815
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
  g_return_if_fail (allocation != NULL);
816

817 818 819 820 821
  widget->allocation = *allocation;
  if (GTK_WIDGET_REALIZED (widget))
    gdk_window_move_resize (widget->window,
			    allocation->x, allocation->y,
			    allocation->width, allocation->height);
822

823 824
  notebook = GTK_NOTEBOOK (widget);
  if (notebook->children)
Elliot Lee's avatar
Elliot Lee committed
825
    {
826 827
      child_allocation.x = GTK_CONTAINER (widget)->border_width;
      child_allocation.y = GTK_CONTAINER (widget)->border_width;
828 829
      child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
      child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
830 831

      if (notebook->show_tabs || notebook->show_border)
832
	{
833 834
	  child_allocation.x += widget->style->klass->xthickness;
	  child_allocation.y += widget->style->klass->ythickness;
835 836 837 838
	  child_allocation.width = MAX (1, (gint)child_allocation.width -
					(gint) widget->style->klass->xthickness * 2);
	  child_allocation.height = MAX (1, (gint)child_allocation.height -
					 (gint) widget->style->klass->ythickness * 2);
839 840 841 842 843 844 845 846 847

	  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 =
848 849
		    MAX (1, (gint)child_allocation.height -
			 (gint)notebook->cur_page->requisition.height);
850 851 852 853 854
		  break;
		case GTK_POS_LEFT:
		  child_allocation.x += notebook->cur_page->requisition.width;
		case GTK_POS_RIGHT:
		  child_allocation.width =
855 856
		    MAX (1, (gint)child_allocation.width -
			 (gint)notebook->cur_page->requisition.width);
857 858 859
		  break;
		}
	    }
860
	}
861 862 863 864 865 866 867 868 869 870 871 872

      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);
	}

      gtk_notebook_pages_allocate (notebook, allocation);
873
    }
874
   gtk_notebook_set_shape (notebook);
875
}
Elliot Lee's avatar
Elliot Lee committed
876

877 878 879
static void
gtk_notebook_draw (GtkWidget    *widget,
		   GdkRectangle *area)
880
{
881 882
  GtkNotebook *notebook;
  GdkRectangle child_area;
883
  GdkRectangle draw_area;
884

885 886 887
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_NOTEBOOK (widget));
  g_return_if_fail (area != NULL);
888

889 890
  notebook = GTK_NOTEBOOK (widget);

891 892
  draw_area = *area;

893
  if (GTK_WIDGET_DRAWABLE (widget))
894
    {
895 896 897 898 899 900 901
      gboolean have_visible_child;

      have_visible_child = notebook->cur_page && GTK_WIDGET_VISIBLE (notebook->cur_page->child);

      if (have_visible_child != notebook->have_visible_child)
	{
	  notebook->have_visible_child = have_visible_child;
902 903 904 905
	  draw_area.x = 0;
	  draw_area.y = 0;
	  draw_area.width = widget->allocation.width;
	  draw_area.height = widget->allocation.height;
906
	}
907 908

      gtk_notebook_paint (widget, &draw_area);
909

910
      gtk_widget_draw_focus (widget);
911

912
      if (notebook->cur_page && GTK_WIDGET_VISIBLE (notebook->cur_page->child) &&
913
	  gtk_widget_intersect (notebook->cur_page->child, &draw_area, &child_area))
914
	gtk_widget_draw (notebook->cur_page->child, &child_area);
915
    }
916 917
  else
    notebook->have_visible_child = FALSE;
918 919
}

920 921 922
static gint
gtk_notebook_expose (GtkWidget      *widget,
		     GdkEventExpose *event)
923
{
924 925 926 927 928 929 930
  GtkNotebook *notebook;
  GdkEventExpose child_event;
  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);
931

932 933 934
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      notebook = GTK_NOTEBOOK (widget);
935

936
      gtk_notebook_paint (widget, &event->area);
937 938 939 940 941 942 943
      if (notebook->show_tabs)
	{
	  if (notebook->cur_page && 
	      gtk_widget_intersect (notebook->cur_page->tab_label, 
				    &event->area, &child_area))
	    gtk_widget_draw_focus (widget);
	}
944

945 946 947 948 949 950
      child_event = *event;
      if (notebook->cur_page && 
	  GTK_WIDGET_NO_WINDOW (notebook->cur_page->child) &&
	  gtk_widget_intersect (notebook->cur_page->child, &event->area, 
				&child_event.area))
	gtk_widget_event (notebook->cur_page->child, (GdkEvent*) &child_event);
Elliot Lee's avatar
Elliot Lee committed
951
    }
952

953
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
954 955
}

956 957 958
static gint
gtk_notebook_button_press (GtkWidget      *widget,
			   GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
959
{
960 961
  GtkNotebook *notebook;
  GtkNotebookPage *page;
Elliot Lee's avatar
Elliot Lee committed
962
  GList *children;
963
  gint num;
Elliot Lee's avatar
Elliot Lee committed
964

965 966 967
  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
968

969
  notebook = GTK_NOTEBOOK (widget);
Elliot Lee's avatar
Elliot Lee committed
970

971 972 973
  if (event->type != GDK_BUTTON_PRESS || !notebook->children ||
      notebook->button)
    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
974

975
  if (event->window == notebook->panel)
Elliot Lee's avatar
Elliot Lee committed
976
    {
977 978
      if (!GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
Elliot Lee's avatar
Elliot Lee committed
979

980 981
      gtk_grab_add (widget);
      notebook->button = event->button;
Elliot Lee's avatar
Elliot Lee committed
982

983 984 985 986 987 988 989 990 991
      if (event->x <= ARROW_SIZE + ARROW_SPACING / 2)
	{
	  notebook->click_child = GTK_ARROW_LEFT;
	  if (event->button == 1)
	    {
	      if (!notebook->focus_tab ||
		  gtk_notebook_search_page (notebook, notebook->focus_tab,
					    STEP_PREV, TRUE))
		gtk_container_focus (GTK_CONTAINER (notebook), GTK_DIR_LEFT);
Elliot Lee's avatar
Elliot Lee committed
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
	      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 (GTK_NOTEBOOK (widget));
	  else if (event->button == 3)
	    gtk_notebook_switch_focus_tab (notebook,
					   gtk_notebook_search_page (notebook,
								     NULL,
								     STEP_NEXT,
								     TRUE));
	  gtk_notebook_draw_arrow (notebook, GTK_ARROW_LEFT);
	}
      else
	{
	  notebook->click_child = GTK_ARROW_RIGHT;
	  if (event->button == 1)
	    {
	      if (!notebook->focus_tab ||
		  gtk_notebook_search_page (notebook, notebook->focus_tab,
					    STEP_NEXT, TRUE))
		gtk_container_focus (GTK_CONTAINER (notebook), 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 (GTK_NOTEBOOK (widget));
	  else if (event->button == 3)
	    gtk_notebook_switch_focus_tab
	      (notebook, gtk_notebook_search_page (notebook, NULL,
						   STEP_PREV, TRUE));
	  gtk_notebook_draw_arrow (notebook, GTK_ARROW_RIGHT);
	}
1037
    }
1038
  else if (event->window == widget->window)
1039
    {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
      if (event->button == 3 && notebook->menu)
	{
	  gtk_menu_popup (GTK_MENU (notebook->menu), NULL, NULL, 
			  NULL, NULL, 3, event->time);
	  return FALSE;
	}

      num = 0;
      children = notebook->children;
      while (children)
	{
	  page = children->data;
	  
	  if (GTK_WIDGET_VISIBLE (page->child) &&
	      page->tab_label && GTK_WIDGET_MAPPED (page->tab_label) &&
	      (event->x >= page->allocation.x) &&
	      (event->y >= page->allocation.y) &&
	      (event->x <= (page->allocation.x + page->allocation.width)) &&
	      (event->y <= (page->allocation.y + page->allocation.height)))
	    {
	      if (page == notebook->cur_page && notebook->focus_tab &&
		  notebook->focus_tab != children &&
		  GTK_WIDGET_HAS_FOCUS (notebook))
		{
		  GtkNotebookPage *old_page;

		  notebook->child_has_focus = FALSE;
		  old_page = (GtkNotebookPage *)
			      (notebook->focus_tab->data);
		  gtk_notebook_switch_focus_tab (notebook, children);
		  gtk_notebook_focus_changed (notebook, old_page);
		}
	      else
		{
		  gtk_notebook_switch_focus_tab (notebook, children);
		  gtk_widget_grab_focus (widget);
1076
		  gtk_notebook_switch_page (notebook, page, num);
1077 1078 1079 1080 1081 1082 1083 1084
		}
	      break;
	    }
	  children = children->next;
	  num++;
	}
      if (!children && !GTK_WIDGET_HAS_FOCUS (widget))
	gtk_widget_grab_focus (widget);
1085
    }
1086 1087
   gtk_notebook_set_shape (notebook);
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1088 1089
}

1090 1091 1092
static gint
gtk_notebook_button_release (GtkWidget      *widget,
			     GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
1093
{
1094
  GtkNotebook *notebook;
Elliot Lee's avatar
Elliot Lee committed
1095

1096 1097 1098
  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