gtktoolitemgroup.c 73.2 KB
Newer Older
Johannes Schmid's avatar
Johannes Schmid committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GtkToolPalette -- A tool palette with categories and DnD support
 * Copyright (C) 2008  Openismus GmbH
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Johannes Schmid's avatar
Johannes Schmid committed
16 17 18 19 20 21
 *
 * Authors:
 *      Mathias Hasselmann
 *      Jan Arne Petersen
 */

22 23
#include "config.h"

Johannes Schmid's avatar
Johannes Schmid committed
24 25
#include <math.h>
#include <string.h>
26 27 28

#include "gtktoolpaletteprivate.h"
#include "gtktypebuiltins.h"
29 30
#include "gtkprivate.h"
#include "gtkintl.h"
Johannes Schmid's avatar
Johannes Schmid committed
31 32 33 34 35 36 37

#define ANIMATION_TIMEOUT        50
#define ANIMATION_DURATION      (ANIMATION_TIMEOUT * 4)
#define DEFAULT_ANIMATION_STATE  TRUE
#define DEFAULT_EXPANDER_SIZE    16
#define DEFAULT_HEADER_SPACING   2

38
#define DEFAULT_LABEL            ""
Johannes Schmid's avatar
Johannes Schmid committed
39 40 41 42
#define DEFAULT_COLLAPSED        FALSE
#define DEFAULT_ELLIPSIZE        PANGO_ELLIPSIZE_NONE

/**
43 44 45
 * SECTION:gtktoolitemgroup
 * @Short_description: A sub container used in a tool palette
 * @Title: GtkToolItemGroup
Johannes Schmid's avatar
Johannes Schmid committed
46
 *
47
 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
48
 * #GtkToolItems to a palette like container with different
49
 * categories and drag and drop support.
Johannes Schmid's avatar
Johannes Schmid committed
50
 *
51 52 53 54
 * # CSS nodes
 *
 * GtkToolItemGroup has a single CSS node named toolitemgroup.
 *
55
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
56 57 58 59 60
 */

enum
{
  PROP_NONE,
61 62
  PROP_LABEL,
  PROP_LABEL_WIDGET,
Johannes Schmid's avatar
Johannes Schmid committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  PROP_COLLAPSED,
  PROP_ELLIPSIZE,
  PROP_RELIEF
};

enum
{
  CHILD_PROP_NONE,
  CHILD_PROP_HOMOGENEOUS,
  CHILD_PROP_EXPAND,
  CHILD_PROP_FILL,
  CHILD_PROP_NEW_ROW,
  CHILD_PROP_POSITION,
};

typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;

struct _GtkToolItemGroupPrivate
{
  GtkWidget         *header;
83
  GtkWidget         *label_widget;
Johannes Schmid's avatar
Johannes Schmid committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97

  GList             *children;

  gint64             animation_start;
  GSource           *animation_timeout;
  gint               expander_size;
  gint               header_spacing;

  gulong             focus_set_id;
  GtkWidget         *toplevel;

  GtkSettings       *settings;
  gulong             settings_connection;

98 99 100
  PangoEllipsizeMode ellipsize;

  guint              animation : 1;
Johannes Schmid's avatar
Johannes Schmid committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  guint              collapsed : 1;
};

struct _GtkToolItemGroupChild
{
  GtkToolItem *item;

  guint        homogeneous : 1;
  guint        expand : 1;
  guint        fill : 1;
  guint        new_row : 1;
};

static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);

G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
117 118 119
                         G_ADD_PRIVATE (GtkToolItemGroup)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
                                                gtk_tool_item_group_tool_shell_init));
Johannes Schmid's avatar
Johannes Schmid committed
120 121 122 123 124 125 126 127 128 129 130 131 132

static GtkWidget*
gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
{
  return gtk_bin_get_child (GTK_BIN (group->priv->header));
}

static GtkOrientation
gtk_tool_item_group_get_orientation (GtkToolShell *shell)
{
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));

  if (GTK_IS_TOOL_PALETTE (parent))
133
    return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
Johannes Schmid's avatar
Johannes Schmid committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

  return GTK_ORIENTATION_VERTICAL;
}

static GtkToolbarStyle
gtk_tool_item_group_get_style (GtkToolShell *shell)
{
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));

  if (GTK_IS_TOOL_PALETTE (parent))
    return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));

  return GTK_TOOLBAR_ICONS;
}

static GtkIconSize
gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
{
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));

  if (GTK_IS_TOOL_PALETTE (parent))
    return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));

  return GTK_ICON_SIZE_SMALL_TOOLBAR;
}

static PangoEllipsizeMode
gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
{
  return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
}

static gfloat
gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
{
  if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
      GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
    return 0.0;

  return 0.5;
}

static GtkOrientation
gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
{
  return GTK_ORIENTATION_HORIZONTAL;
}

static GtkSizeGroup *
gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
{
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));

  if (GTK_IS_TOOL_PALETTE (parent))
    return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));

  return NULL;
}

static void
animation_change_notify (GtkToolItemGroup *group)
{
  GtkSettings *settings = group->priv->settings;
  gboolean animation;

  if (settings)
    g_object_get (settings,
                  "gtk-enable-animations", &animation,
                  NULL);
  else
    animation = DEFAULT_ANIMATION_STATE;

  group->priv->animation = animation;
}

static void
gtk_tool_item_group_settings_change_notify (GtkSettings      *settings,
                                            const GParamSpec *pspec,
                                            GtkToolItemGroup *group)
{
214
  if (strcmp (pspec->name, "gtk-enable-animations") == 0)
Johannes Schmid's avatar
Johannes Schmid committed
215 216 217 218 219 220 221 222 223 224 225
    animation_change_notify (group);
}

static void
gtk_tool_item_group_screen_changed (GtkWidget *widget,
                                    GdkScreen *previous_screen)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
  GtkToolItemGroupPrivate* priv = group->priv;
  GtkSettings *old_settings = priv->settings;
  GtkSettings *settings;
226

Johannes Schmid's avatar
Johannes Schmid committed
227 228 229 230
  if (gtk_widget_has_screen (GTK_WIDGET (group)))
    settings = gtk_widget_get_settings (GTK_WIDGET (group));
  else
    settings = NULL;
231

Johannes Schmid's avatar
Johannes Schmid committed
232 233
  if (settings == old_settings)
    return;
234

Johannes Schmid's avatar
Johannes Schmid committed
235 236 237
  if (old_settings)
  {
    g_signal_handler_disconnect (old_settings, priv->settings_connection);
238
    priv->settings_connection = 0;
Johannes Schmid's avatar
Johannes Schmid committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    g_object_unref (old_settings);
  }

  if (settings)
  {
    priv->settings_connection =
      g_signal_connect (settings, "notify",
                        G_CALLBACK (gtk_tool_item_group_settings_change_notify),
                        group);
    priv->settings = g_object_ref (settings);
  }
  else
    priv->settings = NULL;

  animation_change_notify (group);
}

static void
gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
{
  iface->get_icon_size = gtk_tool_item_group_get_icon_size;
  iface->get_orientation = gtk_tool_item_group_get_orientation;
  iface->get_style = gtk_tool_item_group_get_style;
  iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
  iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
  iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
  iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
}

static gboolean
269 270 271
gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
                                    cairo_t   *cr,
                                    gpointer   data)
Johannes Schmid's avatar
Johannes Schmid committed
272 273
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
274
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
275
  GtkOrientation orientation;
276
  gint x, y, width, height;
Johannes Schmid's avatar
Johannes Schmid committed
277
  GtkTextDirection direction;
278 279
  GtkStyleContext *context;
  GtkStateFlags state = 0;
Johannes Schmid's avatar
Johannes Schmid committed
280 281 282

  orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
  direction = gtk_widget_get_direction (widget);
283 284
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
285
  context = gtk_widget_get_style_context (widget);
286
  state = gtk_widget_get_state_flags (widget);
287 288

  if (!priv->collapsed)
289
    state |= GTK_STATE_FLAG_CHECKED;
290 291 292 293

  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
Johannes Schmid's avatar
Johannes Schmid committed
294 295 296

  if (GTK_ORIENTATION_VERTICAL == orientation)
    {
297 298
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);

Johannes Schmid's avatar
Johannes Schmid committed
299
      if (GTK_TEXT_DIR_RTL == direction)
300
        x = width;
Johannes Schmid's avatar
Johannes Schmid committed
301
      else
302
        x = 0;
303

304
      y = height / 2 - priv->expander_size / 2;
Johannes Schmid's avatar
Johannes Schmid committed
305 306 307
    }
  else
    {
308 309 310
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
      x = width / 2 - priv->expander_size / 2;
      y = 0;
Johannes Schmid's avatar
Johannes Schmid committed
311 312
    }

313 314 315 316 317
  gtk_render_expander (context, cr, x, y,
                       priv->expander_size,
                       priv->expander_size);

  gtk_style_context_restore (context);
Johannes Schmid's avatar
Johannes Schmid committed
318 319 320 321 322

  return FALSE;
}

static void
Johannes Schmid's avatar
Johannes Schmid committed
323
gtk_tool_item_group_header_clicked_cb (GtkButton *button,
Johannes Schmid's avatar
Johannes Schmid committed
324 325 326
                                       gpointer   data)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
327
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
328 329
  GtkWidget *parent = gtk_widget_get_parent (data);

330
  if (priv->collapsed ||
Johannes Schmid's avatar
Johannes Schmid committed
331 332
      !GTK_IS_TOOL_PALETTE (parent) ||
      !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
333
    gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
Johannes Schmid's avatar
Johannes Schmid committed
334 335 336 337 338 339
}

static void
gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
{
  GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
340
  GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
Johannes Schmid's avatar
Johannes Schmid committed
341
  GtkWidget *widget = GTK_WIDGET (group);
342
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
343 344 345 346
  gint dx = 0, dy = 0;
  GtkTextDirection direction = gtk_widget_get_direction (widget);

  gtk_widget_style_get (widget,
347 348
                        "header-spacing", &(priv->header_spacing),
                        "expander-size", &(priv->expander_size),
Johannes Schmid's avatar
Johannes Schmid committed
349
                        NULL);
350 351
  
  gtk_widget_set_size_request (alignment, -1, priv->expander_size);
Johannes Schmid's avatar
Johannes Schmid committed
352 353 354 355

  switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
    {
      case GTK_ORIENTATION_HORIZONTAL:
356 357 358 359 360 361 362 363 364 365 366
        dy = priv->header_spacing + priv->expander_size;

        if (GTK_IS_LABEL (label_widget))
          {
            gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
            if (GTK_TEXT_DIR_RTL == direction)
              gtk_label_set_angle (GTK_LABEL (label_widget), -90);
            else
              gtk_label_set_angle (GTK_LABEL (label_widget), 90);
          }
       break;
Johannes Schmid's avatar
Johannes Schmid committed
367 368

      case GTK_ORIENTATION_VERTICAL:
369 370 371 372 373 374 375
        dx = priv->header_spacing + priv->expander_size;

        if (GTK_IS_LABEL (label_widget))
          {
            gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
            gtk_label_set_angle (GTK_LABEL (label_widget), 0);
          }
Johannes Schmid's avatar
Johannes Schmid committed
376 377 378
        break;
    }

Matthias Clasen's avatar
Matthias Clasen committed
379
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Johannes Schmid's avatar
Johannes Schmid committed
380
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
Matthias Clasen's avatar
Matthias Clasen committed
381
G_GNUC_END_IGNORE_DEPRECATIONS
Johannes Schmid's avatar
Johannes Schmid committed
382 383 384 385 386 387
}

static void
gtk_tool_item_group_init (GtkToolItemGroup *group)
{
  GtkWidget *alignment;
388
  GtkToolItemGroupPrivate* priv;
389

Johannes Schmid's avatar
Johannes Schmid committed
390 391
  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);

392
  group->priv = priv = gtk_tool_item_group_get_instance_private (group);
Johannes Schmid's avatar
Johannes Schmid committed
393

394 395 396
  priv->children = NULL;
  priv->header_spacing = DEFAULT_HEADER_SPACING;
  priv->expander_size = DEFAULT_EXPANDER_SIZE;
Johannes Schmid's avatar
Johannes Schmid committed
397

398
  priv->label_widget = gtk_label_new (NULL);
399 400
  gtk_widget_set_halign (priv->label_widget, GTK_ALIGN_START);
  gtk_widget_set_valign (priv->label_widget, GTK_ALIGN_CENTER);
Matthias Clasen's avatar
Matthias Clasen committed
401
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
Johannes Schmid's avatar
Johannes Schmid committed
402
  alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
Matthias Clasen's avatar
Matthias Clasen committed
403
G_GNUC_END_IGNORE_DEPRECATIONS
404
  gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
Johannes Schmid's avatar
Johannes Schmid committed
405 406
  gtk_widget_show_all (alignment);

407 408
  priv->header = gtk_button_new ();
  g_object_ref_sink (priv->header);
409
  gtk_widget_set_focus_on_click (priv->header, FALSE);
410 411
  gtk_container_add (GTK_CONTAINER (priv->header), alignment);
  gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
Johannes Schmid's avatar
Johannes Schmid committed
412 413 414

  gtk_tool_item_group_header_adjust_style (group);

415 416
  g_signal_connect_after (alignment, "draw",
                          G_CALLBACK (gtk_tool_item_group_header_draw_cb),
Johannes Schmid's avatar
Johannes Schmid committed
417 418
                          group);

419
  g_signal_connect (priv->header, "clicked",
Johannes Schmid's avatar
Johannes Schmid committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433
                    G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
                    group);
}

static void
gtk_tool_item_group_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);

  switch (prop_id)
    {
434 435
      case PROP_LABEL:
        gtk_tool_item_group_set_label (group, g_value_get_string (value));
Johannes Schmid's avatar
Johannes Schmid committed
436
        break;
437

438 439
      case PROP_LABEL_WIDGET:
        gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
440
	break;
441

Johannes Schmid's avatar
Johannes Schmid committed
442 443 444
      case PROP_COLLAPSED:
        gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
        break;
445

Johannes Schmid's avatar
Johannes Schmid committed
446 447 448
      case PROP_ELLIPSIZE:
        gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
        break;
449

Johannes Schmid's avatar
Johannes Schmid committed
450 451 452
      case PROP_RELIEF:
        gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
        break;
453

Johannes Schmid's avatar
Johannes Schmid committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_item_group_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);

  switch (prop_id)
    {
470 471 472 473 474 475 476
      case PROP_LABEL:
        g_value_set_string (value, gtk_tool_item_group_get_label (group));
        break;

      case PROP_LABEL_WIDGET:
        g_value_set_object (value,
			    gtk_tool_item_group_get_label_widget (group));
Johannes Schmid's avatar
Johannes Schmid committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        break;

      case PROP_COLLAPSED:
        g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
        break;

      case PROP_ELLIPSIZE:
        g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
        break;

      case PROP_RELIEF:
        g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_item_group_finalize (GObject *object)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);

502
  g_list_free (group->priv->children);
Johannes Schmid's avatar
Johannes Schmid committed
503 504 505 506 507 508 509 510

  G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
}

static void
gtk_tool_item_group_dispose (GObject *object)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
511
  GtkToolItemGroupPrivate* priv = group->priv;
512

513
  if (priv->toplevel)
Johannes Schmid's avatar
Johannes Schmid committed
514 515
    {
      /* disconnect focus tracking handler */
516 517
      g_signal_handler_disconnect (priv->toplevel,
                                   priv->focus_set_id);
Johannes Schmid's avatar
Johannes Schmid committed
518

519 520
      priv->focus_set_id = 0;
      priv->toplevel = NULL;
Johannes Schmid's avatar
Johannes Schmid committed
521 522
    }

523 524 525 526 527 528 529
  if (priv->settings_connection > 0)
    {
      g_signal_handler_disconnect (priv->settings, priv->settings_connection);
      priv->settings_connection = 0;
    }

  g_clear_object (&priv->settings);
530 531
  if (priv->header)
    gtk_widget_destroy (priv->header);
532
  g_clear_object (&priv->header);
533

Johannes Schmid's avatar
Johannes Schmid committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
  G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
}

static void
gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
                                   GtkRequisition   *item_size,
                                   gboolean          homogeneous_only,
                                   gint             *requested_rows)
{
  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));

  if (GTK_IS_TOOL_PALETTE (parent))
    _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
  else
    _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
}

static void
gtk_tool_item_group_size_request (GtkWidget      *widget,
                                  GtkRequisition *requisition)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
556
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
557 558 559
  GtkOrientation orientation;
  GtkRequisition item_size;
  gint requested_rows;
560
  guint border_width;
Johannes Schmid's avatar
Johannes Schmid committed
561

562
  if (priv->children && gtk_tool_item_group_get_label_widget (group))
Johannes Schmid's avatar
Johannes Schmid committed
563
    {
564 565
      gtk_widget_get_preferred_size (priv->header,
                                     requisition, NULL);
566
      gtk_widget_show (priv->header);
Johannes Schmid's avatar
Johannes Schmid committed
567 568 569 570
    }
  else
    {
      requisition->width = requisition->height = 0;
571
      gtk_widget_hide (priv->header);
Johannes Schmid's avatar
Johannes Schmid committed
572 573 574 575 576 577 578 579 580 581 582
    }

  gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);

  orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));

  if (GTK_ORIENTATION_VERTICAL == orientation)
    requisition->width = MAX (requisition->width, item_size.width);
  else
    requisition->height = MAX (requisition->height, item_size.height * requested_rows);

583
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
Johannes Schmid's avatar
Johannes Schmid committed
584 585 586 587
  requisition->width += border_width * 2;
  requisition->height += border_width * 2;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
static void
gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
					 gint      *minimum,
					 gint      *natural)
{
  GtkRequisition requisition;

  gtk_tool_item_group_size_request (widget, &requisition);

  *minimum = *natural = requisition.width;
}

static void
gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
					  gint      *minimum,
					  gint      *natural)
{
  GtkRequisition requisition;

  gtk_tool_item_group_size_request (widget, &requisition);

  *minimum = *natural = requisition.height;
}


Johannes Schmid's avatar
Johannes Schmid committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
static gboolean
gtk_tool_item_group_is_item_visible (GtkToolItemGroup      *group,
                                     GtkToolItemGroupChild *child)
{
  GtkToolbarStyle style;
  GtkOrientation orientation;

  orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
  style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));

  /* horizontal tool palettes with text style support only homogeneous items */
  if (!child->homogeneous &&
      GTK_ORIENTATION_HORIZONTAL == orientation &&
      GTK_TOOLBAR_TEXT == style)
    return FALSE;

  return
630
    (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
Johannes Schmid's avatar
Johannes Schmid committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    (GTK_ORIENTATION_VERTICAL == orientation ?
     gtk_tool_item_get_visible_vertical (child->item) :
     gtk_tool_item_get_visible_horizontal (child->item));
}

static inline unsigned
udiv (unsigned x,
      unsigned y)
{
  return (x + y - 1) / y;
}

static void
gtk_tool_item_group_real_size_query (GtkWidget      *widget,
                                     GtkAllocation  *allocation,
                                     GtkRequisition *inquery)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
649
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
650 651 652 653 654 655 656

  GtkRequisition item_size;
  GtkAllocation item_area;

  GtkOrientation orientation;

  gint min_rows;
657
  guint border_width;
Johannes Schmid's avatar
Johannes Schmid committed
658

659
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
Johannes Schmid's avatar
Johannes Schmid committed
660 661 662 663 664 665 666 667 668 669
  orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));

  /* figure out the size of homogeneous items */
  gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);

  if (GTK_ORIENTATION_VERTICAL == orientation)
    item_size.width = MIN (item_size.width, allocation->width);
  else
    item_size.height = MIN (item_size.height, allocation->height);

670 671 672
  item_size.width  = MAX (item_size.width, 1);
  item_size.height = MAX (item_size.height, 1);

Johannes Schmid's avatar
Johannes Schmid committed
673 674 675
  item_area.width = 0;
  item_area.height = 0;

676 677 678
  /* figure out the required columns (n_columns) and rows (n_rows)
   * to place all items
   */
679
  if (!priv->collapsed || !priv->animation || priv->animation_timeout)
Johannes Schmid's avatar
Johannes Schmid committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    {
      guint n_columns;
      gint n_rows;
      GList *it;

      if (GTK_ORIENTATION_VERTICAL == orientation)
        {
          gboolean new_row = FALSE;
          gint row = -1;
          guint col = 0;

          item_area.width = allocation->width - 2 * border_width;
          n_columns = MAX (item_area.width / item_size.width, 1);

          /* calculate required rows for n_columns columns */
695
          for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
            {
              GtkToolItemGroupChild *child = it->data;

              if (!gtk_tool_item_group_is_item_visible (group, child))
                continue;

              if (new_row || child->new_row)
                {
                  new_row = FALSE;
                  row++;
                  col = 0;
                }

              if (child->expand)
                new_row = TRUE;

              if (child->homogeneous)
                {
                  col++;
                  if (col >= n_columns)
                    new_row = TRUE;
                }
              else
                {
                  GtkRequisition req = {0, 0};
                  guint width;

723 724
                  gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
                                                 &req, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

                  width = udiv (req.width, item_size.width);
                  col += width;

                  if (col > n_columns)
                    row++;

                  col = width;

                  if (col >= n_columns)
                    new_row = TRUE;
                }
            }
          n_rows = row + 2;
        }
      else
        {
          guint *row_min_width;
          gint row = -1;
          gboolean new_row = TRUE;
          guint col = 0, min_col, max_col = 0, all_items = 0;
          gint i;

          item_area.height = allocation->height - 2 * border_width;
          n_rows = MAX (item_area.height / item_size.height, min_rows);

          row_min_width = g_new0 (guint, n_rows);

753 754 755
          /* calculate minimal and maximal required cols and minimal
           * required rows
           */
756
          for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
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
            {
              GtkToolItemGroupChild *child = it->data;

              if (!gtk_tool_item_group_is_item_visible (group, child))
                continue;

              if (new_row || child->new_row)
                {
                  new_row = FALSE;
                  row++;
                  col = 0;
                  row_min_width[row] = 1;
                }

              if (child->expand)
                new_row = TRUE;

              if (child->homogeneous)
                {
                  col++;
                  all_items++;
                }
              else
                {
                  GtkRequisition req = {0, 0};
                  guint width;

784 785
                  gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
                                                 &req, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

                  width = udiv (req.width, item_size.width);

                  col += width;
                  all_items += width;

                  row_min_width[row] = MAX (row_min_width[row], width);
                }

              max_col = MAX (max_col, col);
            }

          /* calculate minimal required cols */
          min_col = udiv (all_items, n_rows);

          for (i = 0; i <= row; i++)
            {
              min_col = MAX (min_col, row_min_width[i]);
            }

806 807 808
          /* simple linear search for minimal required columns
           * for the given maximal number of rows (n_rows)
           */
Johannes Schmid's avatar
Johannes Schmid committed
809 810 811 812 813
          for (n_columns = min_col; n_columns < max_col; n_columns ++)
            {
              new_row = TRUE;
              row = -1;
              /* calculate required rows for n_columns columns */
814
              for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
                {
                  GtkToolItemGroupChild *child = it->data;

                  if (!gtk_tool_item_group_is_item_visible (group, child))
                    continue;

                  if (new_row || child->new_row)
                    {
                      new_row = FALSE;
                      row++;
                      col = 0;
                    }

                  if (child->expand)
                    new_row = TRUE;

                  if (child->homogeneous)
                    {
                      col++;
                      if (col >= n_columns)
                        new_row = TRUE;
                    }
                  else
                    {
                      GtkRequisition req = {0, 0};
                      guint width;

842 843
                      gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
                                                     &req, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

                      width = udiv (req.width, item_size.width);
                      col += width;

                      if (col > n_columns)
                        row++;

                      col = width;

                      if (col >= n_columns)
                        new_row = TRUE;
                    }
                }

              if (row < n_rows)
                break;
            }
        }

      item_area.width = item_size.width * n_columns;
      item_area.height = item_size.height * n_rows;
    }

  inquery->width = 0;
  inquery->height = 0;

  /* figure out header widget size */
871
  if (gtk_widget_get_visible (priv->header))
Johannes Schmid's avatar
Johannes Schmid committed
872 873 874
    {
      GtkRequisition child_requisition;

875 876
      gtk_widget_get_preferred_size (priv->header,
                                     &child_requisition, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
877 878 879 880 881 882 883 884 885 886 887 888 889

      if (GTK_ORIENTATION_VERTICAL == orientation)
        inquery->height += child_requisition.height;
      else
        inquery->width += child_requisition.width;
    }

  /* report effective widget size */
  inquery->width += item_area.width + 2 * border_width;
  inquery->height += item_area.height + 2 * border_width;
}

static void
890 891
gtk_tool_item_group_real_size_allocate (GtkWidget     *widget,
                                        GtkAllocation *allocation)
Johannes Schmid's avatar
Johannes Schmid committed
892 893
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
894
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
895 896 897 898 899 900 901 902 903 904 905 906
  GtkRequisition child_requisition;
  GtkAllocation child_allocation;

  GtkRequisition item_size;
  GtkAllocation item_area;

  GtkOrientation orientation;

  GList *it;

  gint n_columns, n_rows = 1;
  gint min_rows;
907
  guint border_width;
908
  GtkTextDirection direction;
909 910

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
Johannes Schmid's avatar
Johannes Schmid committed
911

912
  direction = gtk_widget_get_direction (widget);
Johannes Schmid's avatar
Johannes Schmid committed
913 914 915 916 917 918 919 920 921 922

  orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));

  /* chain up */
  GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);

  child_allocation.x = border_width;
  child_allocation.y = border_width;

  /* place the header widget */
923
  if (gtk_widget_get_visible (priv->header))
Johannes Schmid's avatar
Johannes Schmid committed
924
    {
925 926
      gtk_widget_get_preferred_size (priv->header,
                                     &child_requisition, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

      if (GTK_ORIENTATION_VERTICAL == orientation)
        {
          child_allocation.width = allocation->width;
          child_allocation.height = child_requisition.height;
        }
      else
        {
          child_allocation.width = child_requisition.width;
          child_allocation.height = allocation->height;

          if (GTK_TEXT_DIR_RTL == direction)
            child_allocation.x = allocation->width - border_width - child_allocation.width;
        }

942
      gtk_widget_size_allocate (priv->header, &child_allocation);
Johannes Schmid's avatar
Johannes Schmid committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956

      if (GTK_ORIENTATION_VERTICAL == orientation)
        child_allocation.y += child_allocation.height;
      else if (GTK_TEXT_DIR_RTL != direction)
        child_allocation.x += child_allocation.width;
      else
        child_allocation.x = border_width;
    }
  else
    child_requisition.width = child_requisition.height = 0;

  /* figure out the size of homogeneous items */
  gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);

957 958 959
  item_size.width  = MAX (item_size.width, 1);
  item_size.height = MAX (item_size.height, 1);

Johannes Schmid's avatar
Johannes Schmid committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
  /* figure out the available columns and size of item_area */
  if (GTK_ORIENTATION_VERTICAL == orientation)
    {
      item_size.width = MIN (item_size.width, allocation->width);

      item_area.width = allocation->width - 2 * border_width;
      item_area.height = allocation->height - 2 * border_width - child_requisition.height;

      n_columns = MAX (item_area.width / item_size.width, 1);

      item_size.width = item_area.width / n_columns;
    }
  else
    {
      item_size.height = MIN (item_size.height, allocation->height);

      item_area.width = allocation->width - 2 * border_width - child_requisition.width;
      item_area.height = allocation->height - 2 * border_width;

      n_columns = MAX (item_area.width / item_size.width, 1);
      n_rows = MAX (item_area.height / item_size.height, min_rows);

      item_size.height = item_area.height / n_rows;
    }

  item_area.x = child_allocation.x;
  item_area.y = child_allocation.y;

  /* when expanded or in transition, place the tool items in a grid like layout */
989
  if (!priv->collapsed || !priv->animation || priv->animation_timeout)
Johannes Schmid's avatar
Johannes Schmid committed
990 991 992
    {
      gint col = 0, row = 0;

993
      for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
        {
          GtkToolItemGroupChild *child = it->data;
          gint col_child;

          if (!gtk_tool_item_group_is_item_visible (group, child))
            {
              gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);

              continue;
            }

          /* for non homogeneous widgets request the required size */
          child_requisition.width = 0;

          if (!child->homogeneous)
            {
1010 1011
              gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
                                             &child_requisition, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
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 1037 1038 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 1076 1077 1078 1079
              child_requisition.width = MIN (child_requisition.width, item_area.width);
            }

          /* select next row if at end of row */
          if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
            {
              row++;
              col = 0;
              child_allocation.y += child_allocation.height;
            }

          col_child = col;

          /* calculate the position and size of the item */
          if (!child->homogeneous)
            {
              gint col_width;
              gint width;

              if (!child->expand)
                col_width = udiv (child_requisition.width, item_size.width);
              else
                col_width = n_columns - col;

              width = col_width * item_size.width;

              if (GTK_TEXT_DIR_RTL == direction)
                col_child = (n_columns - col - col_width);

              if (child->fill)
                {
                  child_allocation.x = item_area.x + col_child * item_size.width;
                  child_allocation.width = width;
                }
              else
                {
                  child_allocation.x =
                    (item_area.x + col_child * item_size.width +
                    (width - child_requisition.width) / 2);
                  child_allocation.width = child_requisition.width;
                }

              col += col_width;
            }
          else
            {
              if (GTK_TEXT_DIR_RTL == direction)
                col_child = (n_columns - col - 1);

              child_allocation.x = item_area.x + col_child * item_size.width;
              child_allocation.width = item_size.width;

              col++;
            }

          child_allocation.height = item_size.height;

          gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
          gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
        }

      child_allocation.y += item_size.height;
    }

  /* or just hide all items, when collapsed */

  else
    {
1080
      for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
        {
          GtkToolItemGroupChild *child = it->data;

          gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
        }
    }
}

static void
gtk_tool_item_group_size_allocate (GtkWidget     *widget,
                                   GtkAllocation *allocation)
{
  gtk_tool_item_group_real_size_allocate (widget, allocation);

1095
  if (gtk_widget_get_mapped (widget))
1096
    gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
Johannes Schmid's avatar
Johannes Schmid committed
1097 1098 1099
}

static void
Johannes Schmid's avatar
Johannes Schmid committed
1100
gtk_tool_item_group_set_focus_cb (GtkWidget *window,
Johannes Schmid's avatar
Johannes Schmid committed
1101 1102 1103 1104
                                  GtkWidget *widget,
                                  gpointer   user_data)
{
  GtkAdjustment *adjustment;
1105
  GtkAllocation allocation, p_allocation;
Johannes Schmid's avatar
Johannes Schmid committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  GtkWidget *p;

  /* Find this group's parent widget in the focused widget's anchestry. */
  for (p = widget; p; p = gtk_widget_get_parent (p))
    if (p == user_data)
      {
        p = gtk_widget_get_parent (p);
        break;
      }

  if (GTK_IS_TOOL_PALETTE (p))
    {
      /* Check that the focused widgets is fully visible within
       * the group's parent widget and make it visible otherwise. */

1121
      adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
Johannes Schmid's avatar
Johannes Schmid committed
1122 1123 1124 1125 1126

      if (adjustment)
        {
          int y;

1127 1128 1129
          gtk_widget_get_allocation (widget, &allocation);
          gtk_widget_get_allocation (p, &p_allocation);

Johannes Schmid's avatar
Johannes Schmid committed
1130 1131 1132 1133
          /* Handle vertical adjustment. */
          if (gtk_widget_translate_coordinates
                (widget, p, 0, 0, NULL, &y) && y < 0)
            {
1134
              y += gtk_adjustment_get_value (adjustment);
1135
              gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
Johannes Schmid's avatar
Johannes Schmid committed
1136
            }
1137 1138
          else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
                   y > p_allocation.height)
Johannes Schmid's avatar
Johannes Schmid committed
1139
            {
1140
              y += gtk_adjustment_get_value (adjustment);
1141
              gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
Johannes Schmid's avatar
Johannes Schmid committed
1142 1143 1144
            }
        }

1145
      adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
Johannes Schmid's avatar
Johannes Schmid committed
1146 1147 1148 1149 1150

      if (adjustment)
        {
          int x;

1151 1152 1153
          gtk_widget_get_allocation (widget, &allocation);
          gtk_widget_get_allocation (p, &p_allocation);

Johannes Schmid's avatar
Johannes Schmid committed
1154 1155 1156 1157
          /* Handle horizontal adjustment. */
          if (gtk_widget_translate_coordinates
                (widget, p, 0, 0, &x, NULL) && x < 0)
            {
1158
              x += gtk_adjustment_get_value (adjustment);
1159
              gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
Johannes Schmid's avatar
Johannes Schmid committed
1160
            }
1161 1162
          else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
                   x > p_allocation.width)
Johannes Schmid's avatar
Johannes Schmid committed
1163
            {
1164
              x += gtk_adjustment_get_value (adjustment);
1165
              gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
Johannes Schmid's avatar
Johannes Schmid committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
            }

          return;
        }
    }
}

static void
gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
                                         GtkWidget        *toplevel)
{
1177
  GtkToolItemGroupPrivate* priv = group->priv;
1178 1179

  if (toplevel != priv->toplevel)
Johannes Schmid's avatar
Johannes Schmid committed
1180
    {
1181
      if (priv->toplevel)
Johannes Schmid's avatar
Johannes Schmid committed
1182 1183
        {
          /* Disconnect focus tracking handler. */
1184 1185
          g_signal_handler_disconnect (priv->toplevel,
                                       priv->focus_set_id);
Johannes Schmid's avatar
Johannes Schmid committed
1186

1187 1188
          priv->focus_set_id = 0;
          priv->toplevel = NULL;
Johannes Schmid's avatar
Johannes Schmid committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        }

      if (toplevel)
        {
          /* Install focus tracking handler. We connect to the window's
           * set-focus signal instead of connecting to the focus signal of
           * each child to:
           *
           * 1) Reduce the number of signal handlers used.
           * 2) Avoid special handling for group headers.
           * 3) Catch focus grabs not only for direct children,
           *    but also for nested widgets.
           */
1202
          priv->focus_set_id =
Johannes Schmid's avatar
Johannes Schmid committed
1203 1204 1205 1206
            g_signal_connect (toplevel, "set-focus",
                              G_CALLBACK (gtk_tool_item_group_set_focus_cb),
                              group);

1207
          priv->toplevel = toplevel;
Johannes Schmid's avatar
Johannes Schmid committed
1208 1209 1210 1211 1212 1213 1214
        }
    }
}

static void
gtk_tool_item_group_realize (GtkWidget *widget)
{
1215
  GtkAllocation allocation;
Johannes Schmid's avatar
Johannes Schmid committed
1216
  GtkWidget *toplevel_window;
1217
  GdkWindow *window;
Johannes Schmid's avatar
Johannes Schmid committed
1218
  GdkWindowAttr attributes;
1219
  gint attributes_mask;
1220 1221
  guint border_width;

1222 1223
  gtk_widget_set_realized (widget, TRUE);

1224
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1225 1226
  gtk_widget_get_allocation (widget, &allocation);

Johannes Schmid's avatar
Johannes Schmid committed
1227
  attributes.window_type = GDK_WINDOW_CHILD;
1228 1229 1230 1231
  attributes.x = allocation.x + border_width;
  attributes.y = allocation.y + border_width;
  attributes.width = allocation.width - border_width * 2;
  attributes.height = allocation.height - border_width * 2;
Johannes Schmid's avatar
Johannes Schmid committed
1232 1233
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
1234
  attributes.event_mask = gtk_widget_get_events (widget)
1235
                         | GDK_VISIBILITY_NOTIFY_MASK
1236 1237
                         | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
                         | GDK_BUTTON_MOTION_MASK;
1238
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Johannes Schmid's avatar
Johannes Schmid committed
1239

1240 1241 1242
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
Johannes Schmid's avatar
Johannes Schmid committed
1243

1244
  gtk_widget_register_window (widget, window);
1245

Johannes Schmid's avatar
Johannes Schmid committed
1246 1247
  gtk_container_forall (GTK_CONTAINER (widget),
                        (GtkCallback) gtk_widget_set_parent_window,
1248
                        window);
Johannes Schmid's avatar
Johannes Schmid committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263

  gtk_widget_queue_resize_no_redraw (widget);

  toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
  gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
                                           toplevel_window);
}

static void
gtk_tool_item_group_unrealize (GtkWidget *widget)
{
  gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
  GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
}

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
static gboolean
gtk_tool_item_group_draw (GtkWidget *widget,
                          cairo_t   *cr)
{
  gtk_render_background (gtk_widget_get_style_context (widget), cr,
                         0, 0,
                         gtk_widget_get_allocated_width (widget),
                         gtk_widget_get_allocated_height (widget));

  return GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->draw (widget, cr);
}

Johannes Schmid's avatar
Johannes Schmid committed
1276
static void
1277
gtk_tool_item_group_style_updated (GtkWidget *widget)
Johannes Schmid's avatar
Johannes Schmid committed
1278 1279
{
  gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1280
  GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
Johannes Schmid's avatar
Johannes Schmid committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
}

static void
gtk_tool_item_group_add (GtkContainer *container,
                         GtkWidget    *widget)
{
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
  g_return_if_fail (GTK_IS_TOOL_ITEM (widget));

  gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
                              GTK_TOOL_ITEM (widget), -1);
}

static void
gtk_tool_item_group_remove (GtkContainer *container,
                            GtkWidget    *child)
{
  GtkToolItemGroup *group;
1299
  GtkToolItemGroupPrivate* priv;
Johannes Schmid's avatar
Johannes Schmid committed
1300 1301 1302 1303
  GList *it;

  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
  group = GTK_TOOL_ITEM_GROUP (container);
1304
  priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
1305

1306
  for (it = priv->children; it != NULL; it = it->next)
Johannes Schmid's avatar
Johannes Schmid committed
1307 1308 1309 1310 1311 1312 1313 1314 1315
    {
      GtkToolItemGroupChild *child_info = it->data;

      if ((GtkWidget *)child_info->item == child)
        {
          g_object_unref (child);
          gtk_widget_unparent (child);

          g_free (child_info);
1316
          priv->children = g_list_delete_link (priv->children, it);
Johannes Schmid's avatar
Johannes Schmid committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

          gtk_widget_queue_resize (GTK_WIDGET (container));
          break;
        }
    }
}

static void
gtk_tool_item_group_forall (GtkContainer *container,
                            gboolean      internals,
                            GtkCallback   callback,
                            gpointer      callback_data)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1331
  GtkToolItemGroupPrivate* priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
1332 1333
  GList *children;

1334 1335
  if (internals && priv->header)
    callback (priv->header, callback_data);
Johannes Schmid's avatar
Johannes Schmid committed
1336

1337
  children = priv->children;
Johannes Schmid's avatar
Johannes Schmid committed
1338 1339 1340 1341 1342
  while (children)
    {
      GtkToolItemGroupChild *child = children->data;
      children = children->next; /* store pointer before call to callback
				    because the child pointer is invalid if the
1343
				    child->item is removed from the item group
Johannes Schmid's avatar
Johannes Schmid committed
1344 1345 1346 1347 1348 1349 1350
				    in callback */

      callback (GTK_WIDGET (child->item), callback_data);
    }
}

static GType
Johannes Schmid's avatar
Johannes Schmid committed
1351
gtk_tool_item_group_child_type (GtkContainer *container)
Johannes Schmid's avatar
Johannes Schmid committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
{
  return GTK_TYPE_TOOL_ITEM;
}

static GtkToolItemGroupChild *
gtk_tool_item_group_get_child (GtkToolItemGroup  *group,
                               GtkToolItem       *item,
                               gint              *position,
                               GList            **link)
{
  guint i;
  GList *it;

  g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
  g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);

  for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
    {
      GtkToolItemGroupChild *child = it->data;

      if (child->item == item)
        {
          if (position)
            *position = i;

          if (link)
            *link = it;

          return child;
        }
    }

  return NULL;
}

static void
gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
                                      GtkToolItem      *item,
                                      gboolean         *homogeneous,
                                      gboolean         *expand,
                                      gboolean         *fill,
                                      gboolean         *new_row)
{
  GtkToolItemGroupChild *child;

  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
  g_return_if_fail (GTK_IS_TOOL_ITEM (item));

  child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
  if (!child)
    return;

  if (expand)
    *expand = child->expand;

  if (homogeneous)
    *homogeneous = child->homogeneous;

  if (fill)
    *fill = child->fill;

  if (new_row)
    *new_row = child->new_row;
}

static void
gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
                                      GtkToolItem      *item,
                                      gboolean          homogeneous,
                                      gboolean          expand,
                                      gboolean          fill,
                                      gboolean          new_row)
{
  GtkToolItemGroupChild *child;
  gboolean changed = FALSE;

  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
  g_return_if_fail (GTK_IS_TOOL_ITEM (item));

  child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
  if (!child)
    return;

  gtk_widget_freeze_child_notify (GTK_WIDGET (item));

  if (child->homogeneous != homogeneous)
    {
      child->homogeneous = homogeneous;
      changed = TRUE;
      gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
    }
  if (child->expand != expand)
    {
      child->expand = expand;
      changed = TRUE;
      gtk_widget_child_notify (GTK_WIDGET (item), "expand");
    }
  if (child->fill != fill)
    {
      child->fill = fill;
      changed = TRUE;
      gtk_widget_child_notify (GTK_WIDGET (item), "fill");
    }
  if (child->new_row != new_row)
    {
      child->new_row = new_row;
      changed = TRUE;
      gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
    }

  gtk_widget_thaw_child_notify (GTK_WIDGET (item));

1464 1465 1466
  if (changed
      && gtk_widget_get_visible (GTK_WIDGET (group))
      && gtk_widget_get_visible (GTK_WIDGET (item)))
Johannes Schmid's avatar
Johannes Schmid committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
    gtk_widget_queue_resize (GTK_WIDGET (group));
}

static void
gtk_tool_item_group_set_child_property (GtkContainer *container,
                                        GtkWidget    *child,
                                        guint         prop_id,
                                        const GValue *value,
                                        GParamSpec   *pspec)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
  GtkToolItem *item = GTK_TOOL_ITEM (child);
  gboolean homogeneous, expand, fill, new_row;

  if (prop_id != CHILD_PROP_POSITION)
    gtk_tool_item_group_get_item_packing (group, item,
                                          &homogeneous,
                                          &expand,
                                          &fill,
                                          &new_row);

  switch (prop_id)
    {
      case CHILD_PROP_HOMOGENEOUS:
        gtk_tool_item_group_set_item_packing (group, item,
                                              g_value_get_boolean (value),
                                              expand,
                                              fill,
                                              new_row);
        break;

      case CHILD_PROP_EXPAND:
        gtk_tool_item_group_set_item_packing (group, item,
                                              homogeneous,
                                              g_value_get_boolean (value),
                                              fill,
                                              new_row);
        break;

      case CHILD_PROP_FILL:
        gtk_tool_item_group_set_item_packing (group, item,
                                              homogeneous,
                                              expand,
                                              g_value_get_boolean (value),
                                              new_row);
        break;

      case CHILD_PROP_NEW_ROW:
        gtk_tool_item_group_set_item_packing (group, item,
                                              homogeneous,
                                              expand,
                                              fill,
                                              g_value_get_boolean (value));
        break;

      case CHILD_PROP_POSITION:
        gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
        break;

      default:
        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_item_group_get_child_property (GtkContainer *container,
                                        GtkWidget    *child,
                                        guint         prop_id,
                                        GValue       *value,
                                        GParamSpec   *pspec)
{
  GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
  GtkToolItem *item = GTK_TOOL_ITEM (child);
  gboolean homogeneous, expand, fill, new_row;

  if (prop_id != CHILD_PROP_POSITION)
    gtk_tool_item_group_get_item_packing (group, item,
                                          &homogeneous,
                                          &expand,
                                          &fill,
                                          &new_row);

  switch (prop_id)
    {
      case CHILD_PROP_HOMOGENEOUS:
        g_value_set_boolean (value, homogeneous);
        break;

       case CHILD_PROP_EXPAND:
        g_value_set_boolean (value, expand);
        break;

       case CHILD_PROP_FILL:
        g_value_set_boolean (value, fill);
        break;

       case CHILD_PROP_NEW_ROW:
        g_value_set_boolean (value, new_row);
        break;

     case CHILD_PROP_POSITION:
        g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
        break;

      default:
        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
{
  GObjectClass       *oclass = G_OBJECT_CLASS (cls);
  GtkWidgetClass     *wclass = GTK_WIDGET_CLASS (cls);
  GtkContainerClass  *cclass = GTK_CONTAINER_CLASS (cls);

  oclass->set_property       = gtk_tool_item_group_set_property;
  oclass->get_property       = gtk_tool_item_group_get_property;
  oclass->finalize           = gtk_tool_item_group_finalize;
  oclass->dispose            = gtk_tool_item_group_dispose;

1590 1591 1592 1593 1594
  wclass->get_preferred_width  = gtk_tool_item_group_get_preferred_width;
  wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
  wclass->size_allocate        = gtk_tool_item_group_size_allocate;
  wclass->realize              = gtk_tool_item_group_realize;
  wclass->unrealize            = gtk_tool_item_group_unrealize;
1595
  wclass->style_updated        = gtk_tool_item_group_style_updated;
1596
  wclass->screen_changed       = gtk_tool_item_group_screen_changed;
1597
  wclass->draw                 = gtk_tool_item_group_draw;
Johannes Schmid's avatar
Johannes Schmid committed
1598 1599 1600 1601 1602 1603 1604 1605

  cclass->add                = gtk_tool_item_group_add;
  cclass->remove             = gtk_tool_item_group_remove;
  cclass->forall             = gtk_tool_item_group_forall;
  cclass->child_type         = gtk_tool_item_group_child_type;
  cclass->set_child_property = gtk_tool_item_group_set_child_property;
  cclass->get_child_property = gtk_tool_item_group_get_child_property;

1606 1607 1608 1609 1610
  g_object_class_install_property (oclass, PROP_LABEL,
                                   g_param_spec_string ("label",
                                                        P_("Label"),
                                                        P_("The human-readable title of this item group"),
                                                        DEFAULT_LABEL,
1611
                                                        GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1612

1613 1614 1615 1616 1617
  g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
                                   g_param_spec_object  ("label-widget",
                                                        P_("Label widget"),
                                                        P_("A widget to display in place of the usual label"),
                                                        GTK_TYPE_WIDGET,
1618
							GTK_PARAM_READWRITE));
1619

Johannes Schmid's avatar
Johannes Schmid committed
1620 1621 1622
  g_object_class_install_property (oclass, PROP_COLLAPSED,
                                   g_param_spec_boolean ("collapsed",
                                                         P_("Collapsed"),
1623
                                                         P_("Whether the group has been collapsed and items are hidden"),
Johannes Schmid's avatar
Johannes Schmid committed
1624
                                                         DEFAULT_COLLAPSED,
1625
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
Johannes Schmid's avatar
Johannes Schmid committed
1626 1627 1628 1629 1630 1631

  g_object_class_install_property (oclass, PROP_ELLIPSIZE,
                                   g_param_spec_enum ("ellipsize",
                                                      P_("ellipsize"),
                                                      P_("Ellipsize for item group headers"),
                                                      PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1632
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
Johannes Schmid's avatar
Johannes Schmid committed
1633

1634
  g_object_class_install_property (oclass, PROP_RELIEF,
Johannes Schmid's avatar
Johannes Schmid committed
1635
                                   g_param_spec_enum ("header-relief",
1636
                                                      P_("Header Relief"),
Johannes Schmid's avatar
Johannes Schmid committed
1637 1638
                                                      P_("Relief of the group header button"),
                                                      GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1639
                                                      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
Johannes Schmid's avatar
Johannes Schmid committed
1640 1641 1642 1643 1644

  gtk_widget_class_install_style_property (wclass,
                                           g_param_spec_int ("expander-size",
                                                             P_("Expander Size"),
                                                             P_("Size of the expander arrow"),
1645
                                                             0, G_MAXINT, DEFAULT_EXPANDER_SIZE,
1646
                                                             GTK_PARAM_READABLE));
Johannes Schmid's avatar
Johannes Schmid committed
1647 1648 1649 1650 1651

  gtk_widget_class_install_style_property (wclass,
                                           g_param_spec_int ("header-spacing",
                                                             P_("Header Spacing"),
                                                             P_("Spacing between expander arrow and caption"),
1652
                                                             0, G_MAXINT, DEFAULT_HEADER_SPACING,
1653
                                                             GTK_PARAM_READABLE));
Johannes Schmid's avatar
Johannes Schmid committed
1654 1655 1656 1657 1658 1659

  gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
                                              g_param_spec_boolean ("homogeneous",
                                                                    P_("Homogeneous"),
                                                                    P_("Whether the item should be the same size as other homogeneous items"),
                                                                    TRUE,
1660
                                                                    GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1661 1662 1663 1664

  gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
                                              g_param_spec_boolean ("expand",
                                                                    P_("Expand"),
1665
                                                                    P_("Whether the item should receive extra space when the group grows"),
Johannes Schmid's avatar
Johannes Schmid committed
1666
                                                                    FALSE,
1667
                                                                    GTK_PARAM_READWRITE)); 
Johannes Schmid's avatar
Johannes Schmid committed
1668 1669 1670 1671

  gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
                                              g_param_spec_boolean ("fill",
                                                                    P_("Fill"),
1672
                                                                    P_("Whether the item should fill the available space"),
Johannes Schmid's avatar
Johannes Schmid committed
1673
                                                                    TRUE,
1674
                                                                    GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1675 1676 1677 1678 1679 1680

  gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
                                              g_param_spec_boolean ("new-row",
                                                                    P_("New Row"),
                                                                    P_("Whether the item should start a new row"),
                                                                    FALSE,
1681
                                                                    GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1682 1683 1684 1685 1686 1687 1688 1689

  gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
                                              g_param_spec_int ("position",
                                                                P_("Position"),
                                                                P_("Position of the item within this group"),
                                                                0,
                                                                G_MAXINT,
                                                                0,
1690
                                                                GTK_PARAM_READWRITE));
1691 1692

  gtk_widget_class_set_css_name (wclass, "toolitemgroup");
Johannes Schmid's avatar
Johannes Schmid committed
1693 1694 1695 1696
}

/**
 * gtk_tool_item_group_new:
1697
 * @label: the label of the new group
Johannes Schmid's avatar
Johannes Schmid committed
1698
 *
1699
 * Creates a new tool item group with label @label.
Johannes Schmid's avatar
Johannes Schmid committed
1700 1701 1702
 *
 * Returns: a new #GtkToolItemGroup.
 *
1703
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1704 1705
 */
GtkWidget*
1706
gtk_tool_item_group_new (const gchar *label)
Johannes Schmid's avatar
Johannes Schmid committed
1707
{
1708
  return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
1709 1710 1711
}

/**
1712
 * gtk_tool_item_group_set_label:
1713 1714
 * @group: a #GtkToolItemGroup
 * @label: the new human-readable label of of the group
Johannes Schmid's avatar
Johannes Schmid committed
1715
 *
1716
 * Sets the label of the tool item group. The label is displayed in the header
Johannes Schmid's avatar
Johannes Schmid committed
1717 1718
 * of the group.
 *
1719
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1720 1721
 */
void
1722 1723
gtk_tool_item_group_set_label (GtkToolItemGroup *group,
                               const gchar      *label)
Johannes Schmid's avatar
Johannes Schmid committed
1724
{
1725 1726 1727
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));

  if (!label)
1728
    gtk_tool_item_group_set_label_widget (group, NULL);
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
  else
    {
      GtkWidget *child = gtk_label_new (label);
      gtk_widget_show (child);

      gtk_tool_item_group_set_label_widget (group, child);
    }

  g_object_notify (G_OBJECT (group), "label");
}

/**
 * gtk_tool_item_group_set_label_widget:
1742 1743
 * @group: a #GtkToolItemGroup
 * @label_widget: the widget to be displayed in place of the usual label
1744
 *
1745 1746 1747
 * Sets the label of the tool item group.
 * The label widget is displayed in the header of the group, in place
 * of the usual label.
1748
 *
1749
 * Since: 2.20
1750 1751 1752 1753 1754 1755 1756
 */
void
gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
                                      GtkWidget        *label_widget)
{
  GtkToolItemGroupPrivate* priv;
  GtkWidget *alignment;
Johannes Schmid's avatar
Johannes Schmid committed
1757 1758

  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1759
  g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1760
  g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1761 1762

  priv = group->priv;
Johannes Schmid's avatar
Johannes Schmid committed
1763

1764 1765 1766 1767
  if (priv->label_widget == label_widget)
    return;

  alignment = gtk_tool_item_group_get_alignment (group);
1768

1769
  if (priv->label_widget)
Johannes Schmid's avatar
Johannes Schmid committed
1770
    {
1771
      gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1772 1773
      gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
    }
Johannes Schmid's avatar
Johannes Schmid committed
1774 1775


1776 1777 1778
  if (label_widget)
      gtk_container_add (GTK_CONTAINER (alignment), label_widget);

1779
  priv->label_widget = label_widget;
1780

1781
  if (gtk_widget_get_visible (GTK_WIDGET (group)))
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
    gtk_widget_queue_resize (GTK_WIDGET (group));

  /* Only show the header widget if the group has children: */
  if (label_widget && priv->children)
    gtk_widget_show (priv->header);
  else
    gtk_widget_hide (priv->header);

  g_object_freeze_notify (G_OBJECT (group));
  g_object_notify (G_OBJECT (group), "label-widget");
  g_object_notify (G_OBJECT (group), "label");
  g_object_thaw_notify (G_OBJECT (group));
Johannes Schmid's avatar
Johannes Schmid committed
1794 1795 1796
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
1797
 * gtk_tool_item_group_set_header_relief:
1798 1799
 * @group: a #GtkToolItemGroup
 * @style: the #GtkReliefStyle
Johannes Schmid's avatar
Johannes Schmid committed
1800
 *
1801 1802
 * Set the button relief of the group header.
 * See gtk_button_set_relief() for details.
Johannes Schmid's avatar
Johannes Schmid committed
1803
 *