gtktoolpalette.c 59.3 KB
Newer Older
Johannes Schmid's avatar
Johannes Schmid committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors:
 *      Mathias Hasselmann
 */

22 23 24 25 26
#include "config.h"

#include <string.h>
#include <gtk/gtk.h>

Johannes Schmid's avatar
Johannes Schmid committed
27 28
#include "gtktoolpaletteprivate.h"
#include "gtkmarshalers.h"
29
#include "gtktypebuiltins.h"
30
#include "gtkprivate.h"
Tadej Borovšak's avatar
Tadej Borovšak committed
31
#include "gtkscrollable.h"
32
#include "gtkintl.h"
Johannes Schmid's avatar
Johannes Schmid committed
33 34 35 36 37 38 39 40 41

#define DEFAULT_ICON_SIZE       GTK_ICON_SIZE_SMALL_TOOLBAR
#define DEFAULT_ORIENTATION     GTK_ORIENTATION_VERTICAL
#define DEFAULT_TOOLBAR_STYLE   GTK_TOOLBAR_ICONS

#define DEFAULT_CHILD_EXCLUSIVE FALSE
#define DEFAULT_CHILD_EXPAND    FALSE

/**
42 43 44
 * SECTION:gtktoolpalette
 * @Short_description: A tool palette with categories
 * @Title: GtkToolPalette
Johannes Schmid's avatar
Johannes Schmid committed
45
 *
46
 * A #GtkToolPalette allows you to add #GtkToolItem<!-- -->s to a palette-like
47
 * container with different categories and drag and drop support.
Johannes Schmid's avatar
Johannes Schmid committed
48
 *
49
 * A #GtkToolPalette is created with a call to gtk_tool_palette_new().
Johannes Schmid's avatar
Johannes Schmid committed
50
 *
51
 * #GtkToolItem<!-- -->s cannot be added directly to a #GtkToolPalette - 
52 53 54
 * instead they are added to a #GtkToolItemGroup which can than be added
 * to a #GtkToolPalette. To add a #GtkToolItemGroup to a #GtkToolPalette,
 * use gtk_container_add().
Johannes Schmid's avatar
Johannes Schmid committed
55 56 57 58 59 60 61 62 63 64 65 66 67
 *
 * |[
 * GtkWidget *palette, *group;
 * GtkToolItem *item;
 *
 * palette = gtk_tool_palette_new ();
 * group = gtk_tool_item_group_new (_("Test Category"));
 * gtk_container_add (GTK_CONTAINER (palette), group);
 *
 * item = gtk_tool_button_new_from_stock (GTK_STOCK_OK);
 * gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
 * ]|
 *
68 69 70 71 72
 * The easiest way to use drag and drop with #GtkToolPalette is to call
 * gtk_tool_palette_add_drag_dest() with the desired drag source @palette
 * and the desired drag target @widget. Then gtk_tool_palette_get_drag_item()
 * can be used to get the dragged item in the #GtkWidget::drag-data-received
 * signal handler of the drag target.
Johannes Schmid's avatar
Johannes Schmid committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 *
 * |[
 * static void
 * passive_canvas_drag_data_received (GtkWidget        *widget,
 *                                    GdkDragContext   *context,
 *                                    gint              x,
 *                                    gint              y,
 *                                    GtkSelectionData *selection,
 *                                    guint             info,
 *                                    guint             time,
 *                                    gpointer          data)
 * {
 *   GtkWidget *palette;
 *   GtkWidget *item;
 *
 *   /<!-- -->* Get the dragged item *<!-- -->/
89 90
 *   palette = gtk_widget_get_ancestor (gtk_drag_get_source_widget (context),
 *                                      GTK_TYPE_TOOL_PALETTE);
Johannes Schmid's avatar
Johannes Schmid committed
91
 *   if (palette != NULL)
92 93
 *     item = gtk_tool_palette_get_drag_item (GTK_TOOL_PALETTE (palette),
 *                                            selection);
Johannes Schmid's avatar
Johannes Schmid committed
94 95 96 97 98 99 100 101 102 103
 *
 *   /<!-- -->* Do something with item *<!-- -->/
 * }
 *
 * GtkWidget *target, palette;
 *
 * palette = gtk_tool_palette_new ();
 * target = gtk_drawing_area_new ();
 *
 * g_signal_connect (G_OBJECT (target), "drag-data-received",
104
 *                   G_CALLBACK (passive_canvas_drag_data_received), NULL);
Johannes Schmid's avatar
Johannes Schmid committed
105 106 107 108 109 110
 * gtk_tool_palette_add_drag_dest (GTK_TOOL_PALETTE (palette), target,
 *                                 GTK_DEST_DEFAULT_ALL,
 *                                 GTK_TOOL_PALETTE_DRAG_ITEMS,
 *                                 GDK_ACTION_COPY);
 * ]|
 *
111
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
112 113 114 115 116 117 118 119 120
 */

typedef struct _GtkToolItemGroupInfo   GtkToolItemGroupInfo;
typedef struct _GtkToolPaletteDragData GtkToolPaletteDragData;

enum
{
  PROP_NONE,
  PROP_ICON_SIZE,
121
  PROP_ICON_SIZE_SET,
Johannes Schmid's avatar
Johannes Schmid committed
122 123
  PROP_ORIENTATION,
  PROP_TOOLBAR_STYLE,
Tadej Borovšak's avatar
Tadej Borovšak committed
124
  PROP_HADJUSTMENT,
125 126 127
  PROP_VADJUSTMENT,
  PROP_HSCROLL_POLICY,
  PROP_VSCROLL_POLICY
Johannes Schmid's avatar
Johannes Schmid committed
128 129 130 131 132 133 134 135 136 137 138 139 140
};

enum
{
  CHILD_PROP_NONE,
  CHILD_PROP_EXCLUSIVE,
  CHILD_PROP_EXPAND,
};

struct _GtkToolItemGroupInfo
{
  GtkToolItemGroup *widget;

141
  gulong            notify_collapsed;
142
  guint             pos;
Johannes Schmid's avatar
Johannes Schmid committed
143 144 145 146 147 148
  guint             exclusive : 1;
  guint             expand : 1;
};

struct _GtkToolPalettePrivate
{
149
  GPtrArray* groups;
Johannes Schmid's avatar
Johannes Schmid committed
150 151 152 153 154

  GtkAdjustment        *hadjustment;
  GtkAdjustment        *vadjustment;

  GtkIconSize           icon_size;
155
  gboolean              icon_size_set;
Johannes Schmid's avatar
Johannes Schmid committed
156 157
  GtkOrientation        orientation;
  GtkToolbarStyle       style;
158
  gboolean              style_set;
Johannes Schmid's avatar
Johannes Schmid committed
159 160 161 162

  GtkWidget            *expanding_child;

  GtkSizeGroup         *text_size_group;
163

164 165
  GtkSettings          *settings;
  gulong                settings_connection;
Johannes Schmid's avatar
Johannes Schmid committed
166 167

  guint                 drag_source : 2;
168 169 170 171 172

  /* GtkScrollablePolicy needs to be checked when
   * driving the scrollable adjustment values */
  guint hscroll_policy : 1;
  guint vscroll_policy : 1;
Johannes Schmid's avatar
Johannes Schmid committed
173 174 175 176 177 178 179 180 181 182 183 184 185
};

struct _GtkToolPaletteDragData
{
  GtkToolPalette *palette;
  GtkWidget      *item;
};

static GdkAtom dnd_target_atom_item = GDK_NONE;
static GdkAtom dnd_target_atom_group = GDK_NONE;

static const GtkTargetEntry dnd_targets[] =
{
186 187
  { "application/x-gtk-tool-palette-item", GTK_TARGET_SAME_APP, 0 },
  { "application/x-gtk-tool-palette-group", GTK_TARGET_SAME_APP, 0 },
Johannes Schmid's avatar
Johannes Schmid committed
188 189
};

Tadej Borovšak's avatar
Tadej Borovšak committed
190 191 192 193 194 195
static void gtk_tool_palette_set_hadjustment (GtkToolPalette *palette,
                                              GtkAdjustment  *adjustment);
static void gtk_tool_palette_set_vadjustment (GtkToolPalette *palette,
                                              GtkAdjustment  *adjustment);


196
G_DEFINE_TYPE_WITH_CODE (GtkToolPalette,
Johannes Schmid's avatar
Johannes Schmid committed
197
               gtk_tool_palette,
198
               GTK_TYPE_CONTAINER,
Tadej Borovšak's avatar
Tadej Borovšak committed
199 200
               G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)
	       G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
Johannes Schmid's avatar
Johannes Schmid committed
201 202 203 204 205 206 207 208

static void
gtk_tool_palette_init (GtkToolPalette *palette)
{
  palette->priv = G_TYPE_INSTANCE_GET_PRIVATE (palette,
                                               GTK_TYPE_TOOL_PALETTE,
                                               GtkToolPalettePrivate);

209
  palette->priv->groups = g_ptr_array_sized_new (4);
210
  g_ptr_array_set_free_func (palette->priv->groups, g_free);
Johannes Schmid's avatar
Johannes Schmid committed
211 212

  palette->priv->icon_size = DEFAULT_ICON_SIZE;
213
  palette->priv->icon_size_set = FALSE;
Johannes Schmid's avatar
Johannes Schmid committed
214 215
  palette->priv->orientation = DEFAULT_ORIENTATION;
  palette->priv->style = DEFAULT_TOOLBAR_STYLE;
216
  palette->priv->style_set = FALSE;
Johannes Schmid's avatar
Johannes Schmid committed
217 218 219 220 221 222 223 224 225

  palette->priv->text_size_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
}

static void
gtk_tool_palette_reconfigured (GtkToolPalette *palette)
{
  guint i;

226
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
227
    {
228
      GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
229 230
      if (info->widget)
        _gtk_tool_item_group_palette_reconfigured (info->widget);
Johannes Schmid's avatar
Johannes Schmid committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    }

  gtk_widget_queue_resize_no_redraw (GTK_WIDGET (palette));
}

static void
gtk_tool_palette_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (object);

  switch (prop_id)
    {
      case PROP_ICON_SIZE:
        if ((guint) g_value_get_enum (value) != palette->priv->icon_size)
          {
            palette->priv->icon_size = g_value_get_enum (value);
            gtk_tool_palette_reconfigured (palette);
          }
        break;
253

254 255 256 257 258 259 260
      case PROP_ICON_SIZE_SET:
        if ((guint) g_value_get_enum (value) != palette->priv->icon_size)
          {
            palette->priv->icon_size_set = g_value_get_enum (value);
            gtk_tool_palette_reconfigured (palette);
          }
        break;
Johannes Schmid's avatar
Johannes Schmid committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

      case PROP_ORIENTATION:
        if ((guint) g_value_get_enum (value) != palette->priv->orientation)
          {
            palette->priv->orientation = g_value_get_enum (value);
            gtk_tool_palette_reconfigured (palette);
          }
        break;

      case PROP_TOOLBAR_STYLE:
        if ((guint) g_value_get_enum (value) != palette->priv->style)
          {
            palette->priv->style = g_value_get_enum (value);
            gtk_tool_palette_reconfigured (palette);
          }
        break;

Tadej Borovšak's avatar
Tadej Borovšak committed
278 279 280 281 282 283 284 285
      case PROP_HADJUSTMENT:
        gtk_tool_palette_set_hadjustment (palette, g_value_get_object (value));
        break;

      case PROP_VADJUSTMENT:
        gtk_tool_palette_set_vadjustment (palette, g_value_get_object (value));
        break;

286 287 288 289 290 291 292 293 294 295
      case PROP_HSCROLL_POLICY:
	palette->priv->hscroll_policy = g_value_get_enum (value);
	gtk_widget_queue_resize (GTK_WIDGET (palette));
	break;

      case PROP_VSCROLL_POLICY:
	palette->priv->vscroll_policy = g_value_get_enum (value);
	gtk_widget_queue_resize (GTK_WIDGET (palette));
	break;

Johannes Schmid's avatar
Johannes Schmid committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_palette_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (object);

  switch (prop_id)
    {
      case PROP_ICON_SIZE:
        g_value_set_enum (value, gtk_tool_palette_get_icon_size (palette));
        break;
315

316 317 318
      case PROP_ICON_SIZE_SET:
        g_value_set_boolean (value, palette->priv->icon_size_set);
        break;
Johannes Schmid's avatar
Johannes Schmid committed
319 320

      case PROP_ORIENTATION:
321
        g_value_set_enum (value, palette->priv->orientation);
Johannes Schmid's avatar
Johannes Schmid committed
322 323 324 325 326 327
        break;

      case PROP_TOOLBAR_STYLE:
        g_value_set_enum (value, gtk_tool_palette_get_style (palette));
        break;

Tadej Borovšak's avatar
Tadej Borovšak committed
328 329 330 331 332 333 334 335
      case PROP_HADJUSTMENT:
        g_value_set_object (value, palette->priv->hadjustment);
        break;

      case PROP_VADJUSTMENT:
        g_value_set_object (value, palette->priv->vadjustment);
        break;

336 337 338 339 340 341 342 343
      case PROP_HSCROLL_POLICY:
	g_value_set_enum (value, palette->priv->hscroll_policy);
	break;

      case PROP_VSCROLL_POLICY:
	g_value_set_enum (value, palette->priv->vscroll_policy);
	break;

Johannes Schmid's avatar
Johannes Schmid committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
gtk_tool_palette_dispose (GObject *object)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (object);
  guint i;

  if (palette->priv->hadjustment)
    {
      g_object_unref (palette->priv->hadjustment);
      palette->priv->hadjustment = NULL;
    }

  if (palette->priv->vadjustment)
    {
      g_object_unref (palette->priv->vadjustment);
      palette->priv->vadjustment = NULL;
    }

368
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
369
    {
370
      GtkToolItemGroupInfo *group = g_ptr_array_index (palette->priv->groups, i);
Johannes Schmid's avatar
Johannes Schmid committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

      if (group->notify_collapsed)
        {
          g_signal_handler_disconnect (group->widget, group->notify_collapsed);
          group->notify_collapsed = 0;
        }
    }

  if (palette->priv->text_size_group)
    {
      g_object_unref (palette->priv->text_size_group);
      palette->priv->text_size_group = NULL;
    }

  G_OBJECT_CLASS (gtk_tool_palette_parent_class)->dispose (object);
}

static void
gtk_tool_palette_finalize (GObject *object)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (object);

393
  g_ptr_array_free (palette->priv->groups, TRUE);
Johannes Schmid's avatar
Johannes Schmid committed
394 395 396 397 398 399 400 401 402 403

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

static void
gtk_tool_palette_size_request (GtkWidget      *widget,
                               GtkRequisition *requisition)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
  GtkRequisition child_requisition;
404
  guint border_width;
Johannes Schmid's avatar
Johannes Schmid committed
405 406
  guint i;

407 408
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

Johannes Schmid's avatar
Johannes Schmid committed
409 410 411
  requisition->width = 0;
  requisition->height = 0;

412
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
413
    {
414
      GtkToolItemGroupInfo *group = g_ptr_array_index (palette->priv->groups, i);
Johannes Schmid's avatar
Johannes Schmid committed
415 416 417 418

      if (!group->widget)
        continue;

419 420
      gtk_widget_get_preferred_size (GTK_WIDGET (group->widget),
                                     &child_requisition, NULL);
Johannes Schmid's avatar
Johannes Schmid committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

      if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
        {
          requisition->width = MAX (requisition->width, child_requisition.width);
          requisition->height += child_requisition.height;
        }
      else
        {
          requisition->width += child_requisition.width;
          requisition->height = MAX (requisition->height, child_requisition.height);
        }
    }

  requisition->width += border_width * 2;
  requisition->height += border_width * 2;
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
static void
gtk_tool_palette_get_preferred_width (GtkWidget *widget,
				      gint      *minimum,
				      gint      *natural)
{
  GtkRequisition requisition;

  gtk_tool_palette_size_request (widget, &requisition);

  *minimum = *natural = requisition.width;
}

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

  gtk_tool_palette_size_request (widget, &requisition);

  *minimum = *natural = requisition.height;
}


Johannes Schmid's avatar
Johannes Schmid committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
static void
gtk_tool_palette_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
  GtkAdjustment *adjustment = NULL;
  GtkAllocation child_allocation;

  gint n_expand_groups = 0;
  gint remaining_space = 0;
  gint expand_space = 0;

  gint page_start, page_size = 0;
  gint offset = 0;
  guint i;
478
  guint border_width;
Johannes Schmid's avatar
Johannes Schmid committed
479 480 481 482 483

  gint min_offset = -1, max_offset = -1;

  gint x;

484
  gint *group_sizes = g_newa (gint, palette->priv->groups->len);
485
  GtkTextDirection direction;
Johannes Schmid's avatar
Johannes Schmid committed
486

487
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
488
  direction = gtk_widget_get_direction (widget);
Johannes Schmid's avatar
Johannes Schmid committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

  GTK_WIDGET_CLASS (gtk_tool_palette_parent_class)->size_allocate (widget, allocation);

  if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
    {
      adjustment = palette->priv->vadjustment;
      page_size = allocation->height;
    }
  else
    {
      adjustment = palette->priv->hadjustment;
      page_size = allocation->width;
    }

  if (adjustment)
    offset = gtk_adjustment_get_value (adjustment);
  if (GTK_ORIENTATION_HORIZONTAL == palette->priv->orientation &&
      GTK_TEXT_DIR_RTL == direction)
    offset = -offset;

  if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
    child_allocation.width = allocation->width - border_width * 2;
  else
    child_allocation.height = allocation->height - border_width * 2;

  if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
    remaining_space = allocation->height;
  else
    remaining_space = allocation->width;

  /* figure out the required size of all groups to be able to distribute the
520 521
   * remaining space on allocation
   */
522
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
523
    {
524
      GtkToolItemGroupInfo *group = g_ptr_array_index (palette->priv->groups, i);
Johannes Schmid's avatar
Johannes Schmid committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
      gint size;

      if (!group->widget)
        continue;

      widget = GTK_WIDGET (group->widget);

      if (gtk_tool_item_group_get_n_items (group->widget))
        {
          if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
            size = _gtk_tool_item_group_get_height_for_width (group->widget, child_allocation.width);
          else
            size = _gtk_tool_item_group_get_width_for_height (group->widget, child_allocation.height);

          if (group->expand && !gtk_tool_item_group_get_collapsed (group->widget))
            n_expand_groups += 1;
        }
      else
        size = 0;

      remaining_space -= size;
      group_sizes[i] = size;

548 549 550
      /* if the widget is currently expanding an offset which allows to
       * display as much of the widget as possible is calculated
       */
Johannes Schmid's avatar
Johannes Schmid committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      if (widget == palette->priv->expanding_child)
        {
          gint limit =
            GTK_ORIENTATION_VERTICAL == palette->priv->orientation ?
            child_allocation.width : child_allocation.height;

          gint real_size;
          guint j;

          min_offset = 0;

          for (j = 0; j < i; ++j)
            min_offset += group_sizes[j];

          max_offset = min_offset + group_sizes[i];

          real_size = _gtk_tool_item_group_get_size_for_limit
            (GTK_TOOL_ITEM_GROUP (widget), limit,
             GTK_ORIENTATION_VERTICAL == palette->priv->orientation,
             FALSE);

          if (size == real_size)
            palette->priv->expanding_child = NULL;
        }
    }

  if (n_expand_groups > 0)
    {
      remaining_space = MAX (0, remaining_space);
      expand_space = remaining_space / n_expand_groups;
    }

  if (max_offset != -1)
    {
      gint limit =
        GTK_ORIENTATION_VERTICAL == palette->priv->orientation ?
        allocation->height : allocation->width;

      offset = MIN (MAX (offset, max_offset - limit), min_offset);
    }

  if (remaining_space > 0)
    offset = 0;

  x = border_width;
  child_allocation.y = border_width;

  if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
    child_allocation.y -= offset;
  else
    x -= offset;

  /* allocate all groups at the calculated positions */
604
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
605
    {
606
      GtkToolItemGroupInfo *group = g_ptr_array_index (palette->priv->groups, i);
Johannes Schmid's avatar
Johannes Schmid committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      GtkWidget *widget;

      if (!group->widget)
        continue;

      widget = GTK_WIDGET (group->widget);

      if (gtk_tool_item_group_get_n_items (group->widget))
        {
          gint size = group_sizes[i];

          if (group->expand && !gtk_tool_item_group_get_collapsed (group->widget))
            {
              size += MIN (expand_space, remaining_space);
              remaining_space -= expand_space;
            }

          if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
            child_allocation.height = size;
          else
            child_allocation.width = size;

          if (GTK_ORIENTATION_HORIZONTAL == palette->priv->orientation &&
              GTK_TEXT_DIR_RTL == direction)
            child_allocation.x = allocation->width - x - child_allocation.width;
          else
            child_allocation.x = x;

          gtk_widget_size_allocate (widget, &child_allocation);
          gtk_widget_show (widget);

          if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
            child_allocation.y += child_allocation.height;
          else
            x += child_allocation.width;
        }
      else
        gtk_widget_hide (widget);
    }

  if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
    {
      child_allocation.y += border_width;
      child_allocation.y += offset;

      page_start = child_allocation.y;
    }
  else
    {
      x += border_width;
      x += offset;

      page_start = x;
    }

  /* update the scrollbar to match the displayed adjustment */
  if (adjustment)
    {
      gdouble value;

      adjustment->page_increment = page_size * 0.9;
      adjustment->step_increment = page_size * 0.1;
      adjustment->page_size = page_size;

      if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation ||
          GTK_TEXT_DIR_LTR == direction)
        {
          adjustment->lower = 0;
          adjustment->upper = MAX (0, page_start);

          value = MIN (offset, adjustment->upper - adjustment->page_size);
          gtk_adjustment_clamp_page (adjustment, value, offset + page_size);
        }
      else
        {
          adjustment->lower = page_size - MAX (0, page_start);
          adjustment->upper = page_size;

          offset = -offset;

          value = MAX (offset, adjustment->lower);
          gtk_adjustment_clamp_page (adjustment, offset, value + page_size);
        }

      gtk_adjustment_changed (adjustment);
    }
}

static gboolean
696 697
gtk_tool_palette_draw (GtkWidget      *widget,
                       cairo_t        *cr)
Johannes Schmid's avatar
Johannes Schmid committed
698 699 700
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
  GdkDisplay *display;
701
  GdkWindow *window;
Johannes Schmid's avatar
Johannes Schmid committed
702 703
  guint i;

704 705
  window = gtk_widget_get_window (widget);

706
  display = gdk_window_get_display (window);
Johannes Schmid's avatar
Johannes Schmid committed
707 708 709 710 711 712

  if (!gdk_display_supports_composite (display))
    return FALSE;

  cairo_push_group (cr);

713 714
  for (i = 0; i < palette->priv->groups->len; ++i)
  {
715
    GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
716 717 718
    if (info->widget)
      _gtk_tool_item_group_paint (info->widget, cr);
  }
Johannes Schmid's avatar
Johannes Schmid committed
719 720 721 722 723 724 725 726 727 728 729

  cairo_pop_group_to_source (cr);

  cairo_paint (cr);

  return FALSE;
}

static void
gtk_tool_palette_realize (GtkWidget *widget)
{
730 731
  GtkAllocation allocation;
  GdkWindow *window;
Johannes Schmid's avatar
Johannes Schmid committed
732
  GdkWindowAttr attributes;
733
  gint attributes_mask;
734 735
  guint border_width;

736 737
  gtk_widget_set_realized (widget, TRUE);

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

740 741
  gtk_widget_get_allocation (widget, &allocation);

Johannes Schmid's avatar
Johannes Schmid committed
742
  attributes.window_type = GDK_WINDOW_CHILD;
743 744 745 746
  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
747 748
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
749 750 751 752
  attributes.event_mask = gtk_widget_get_events (widget)
                         | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
                         | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
                         | GDK_BUTTON_MOTION_MASK;
753
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Johannes Schmid's avatar
Johannes Schmid committed
754

755 756
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
757
  gtk_widget_set_window (widget, window);
758
  gdk_window_set_user_data (window, widget);
Johannes Schmid's avatar
Johannes Schmid committed
759

760 761
  gtk_style_context_set_background (gtk_widget_get_style_context (widget),
                                    window);
Johannes Schmid's avatar
Johannes Schmid committed
762 763 764

  gtk_container_forall (GTK_CONTAINER (widget),
                        (GtkCallback) gtk_widget_set_parent_window,
765
                        window);
Johannes Schmid's avatar
Johannes Schmid committed
766 767 768 769 770

  gtk_widget_queue_resize_no_redraw (widget);
}

static void
Johannes Schmid's avatar
Johannes Schmid committed
771
gtk_tool_palette_adjustment_value_changed (GtkAdjustment *adjustment,
Johannes Schmid's avatar
Johannes Schmid committed
772 773
                                           gpointer       data)
{
774
  GtkAllocation allocation;
Johannes Schmid's avatar
Johannes Schmid committed
775
  GtkWidget *widget = GTK_WIDGET (data);
776 777 778

  gtk_widget_get_allocation (widget, &allocation);
  gtk_tool_palette_size_allocate (widget, &allocation);
Johannes Schmid's avatar
Johannes Schmid committed
779 780 781 782 783 784 785
}

static void
gtk_tool_palette_add (GtkContainer *container,
                      GtkWidget    *child)
{
  GtkToolPalette *palette;
786
  GtkToolItemGroupInfo *info = g_new0(GtkToolItemGroupInfo, 1);
787

Johannes Schmid's avatar
Johannes Schmid committed
788 789 790 791 792
  g_return_if_fail (GTK_IS_TOOL_PALETTE (container));
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (child));

  palette = GTK_TOOL_PALETTE (container);

793 794 795
  g_ptr_array_add (palette->priv->groups, info);
  info->pos = palette->priv->groups->len - 1;
  info->widget = g_object_ref_sink (child);
Johannes Schmid's avatar
Johannes Schmid committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809

  gtk_widget_set_parent (child, GTK_WIDGET (palette));
}

static void
gtk_tool_palette_remove (GtkContainer *container,
                         GtkWidget    *child)
{
  GtkToolPalette *palette;
  guint i;

  g_return_if_fail (GTK_IS_TOOL_PALETTE (container));
  palette = GTK_TOOL_PALETTE (container);

810 811
  for (i = 0; i < palette->priv->groups->len; ++i)
    {
812
      GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
813 814 815 816
      if (GTK_WIDGET(info->widget) == child)
        {
          g_object_unref (child);
          gtk_widget_unparent (child);
Johannes Schmid's avatar
Johannes Schmid committed
817

818 819 820
          g_ptr_array_remove_index (palette->priv->groups, i);
        }
    }
Johannes Schmid's avatar
Johannes Schmid committed
821 822 823 824
}

static void
gtk_tool_palette_forall (GtkContainer *container,
Johannes Schmid's avatar
Johannes Schmid committed
825
                         gboolean      internals,
Johannes Schmid's avatar
Johannes Schmid committed
826 827 828 829 830 831
                         GtkCallback   callback,
                         gpointer      callback_data)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (container);
  guint i;

832 833

  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
834
    {
835
      GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
836 837 838
      if (info->widget)
        callback (GTK_WIDGET (info->widget),
                  callback_data);
Johannes Schmid's avatar
Johannes Schmid committed
839 840 841 842
    }
}

static GType
Johannes Schmid's avatar
Johannes Schmid committed
843
gtk_tool_palette_child_type (GtkContainer *container)
Johannes Schmid's avatar
Johannes Schmid committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
{
  return GTK_TYPE_TOOL_ITEM_GROUP;
}

static void
gtk_tool_palette_set_child_property (GtkContainer *container,
                                     GtkWidget    *child,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (container);

  switch (prop_id)
    {
      case CHILD_PROP_EXCLUSIVE:
860 861
        gtk_tool_palette_set_exclusive (palette, GTK_TOOL_ITEM_GROUP (child), 
          g_value_get_boolean (value));
Johannes Schmid's avatar
Johannes Schmid committed
862 863 864
        break;

      case CHILD_PROP_EXPAND:
865 866
        gtk_tool_palette_set_expand (palette, GTK_TOOL_ITEM_GROUP (child), 
          g_value_get_boolean (value));
Johannes Schmid's avatar
Johannes Schmid committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
        break;

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

static void
gtk_tool_palette_get_child_property (GtkContainer *container,
                                     GtkWidget    *child,
                                     guint         prop_id,
                                     GValue       *value,
                                     GParamSpec   *pspec)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (container);

  switch (prop_id)
    {
      case CHILD_PROP_EXCLUSIVE:
887 888
        g_value_set_boolean (value, 
          gtk_tool_palette_get_exclusive (palette, GTK_TOOL_ITEM_GROUP (child)));
Johannes Schmid's avatar
Johannes Schmid committed
889 890 891
        break;

      case CHILD_PROP_EXPAND:
892 893
        g_value_set_boolean (value, 
          gtk_tool_palette_get_expand (palette, GTK_TOOL_ITEM_GROUP (child)));
Johannes Schmid's avatar
Johannes Schmid committed
894 895 896 897 898 899 900 901
        break;

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

902 903 904 905
static void
style_change_notify (GtkToolPalette *palette)
{
  GtkToolPalettePrivate* priv = palette->priv;
906

907 908 909 910 911 912 913 914 915 916 917 918
  if (!priv->style_set)
    {
      /* pretend it was set, then unset, thus reverting to new default */
      priv->style_set = TRUE;
      gtk_tool_palette_unset_style (palette);
    }
}

static void
icon_size_change_notify (GtkToolPalette *palette)
{
  GtkToolPalettePrivate* priv = palette->priv;
919

920 921 922 923 924 925 926 927 928 929 930 931 932
  if (!priv->icon_size_set)
    {
      /* pretend it was set, then unset, thus reverting to new default */
      priv->icon_size_set = TRUE;
      gtk_tool_palette_unset_icon_size (palette);
    }
}

static void
gtk_tool_palette_settings_change_notify (GtkSettings      *settings,
                                         const GParamSpec *pspec,
                                         GtkToolPalette   *palette)
{
933
  if (strcmp (pspec->name, "gtk-toolbar-style") == 0)
934
    style_change_notify (palette);
935
  else if (strcmp (pspec->name, "gtk-toolbar-icon-size") == 0)
936 937 938 939 940 941 942 943 944 945 946
    icon_size_change_notify (palette);
}

static void
gtk_tool_palette_screen_changed (GtkWidget *widget,
                                 GdkScreen *previous_screen)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
  GtkToolPalettePrivate* priv = palette->priv;
  GtkSettings *old_settings = priv->settings;
  GtkSettings *settings;
947

948 949 950 951
  if (gtk_widget_has_screen (GTK_WIDGET (palette)))
    settings = gtk_widget_get_settings (GTK_WIDGET (palette));
  else
    settings = NULL;
952

953 954
  if (settings == old_settings)
    return;
955

956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  if (old_settings)
  {
    g_signal_handler_disconnect (old_settings, priv->settings_connection);
    g_object_unref (old_settings);
  }

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

  gtk_tool_palette_reconfigured (palette);
}


Johannes Schmid's avatar
Johannes Schmid committed
977 978 979 980 981 982 983 984 985 986 987 988
static void
gtk_tool_palette_class_init (GtkToolPaletteClass *cls)
{
  GObjectClass      *oclass   = G_OBJECT_CLASS (cls);
  GtkWidgetClass    *wclass   = GTK_WIDGET_CLASS (cls);
  GtkContainerClass *cclass   = GTK_CONTAINER_CLASS (cls);

  oclass->set_property        = gtk_tool_palette_set_property;
  oclass->get_property        = gtk_tool_palette_get_property;
  oclass->dispose             = gtk_tool_palette_dispose;
  oclass->finalize            = gtk_tool_palette_finalize;

989 990
  wclass->get_preferred_width = gtk_tool_palette_get_preferred_width;
  wclass->get_preferred_height= gtk_tool_palette_get_preferred_height;
Johannes Schmid's avatar
Johannes Schmid committed
991
  wclass->size_allocate       = gtk_tool_palette_size_allocate;
992
  wclass->draw                = gtk_tool_palette_draw;
Johannes Schmid's avatar
Johannes Schmid committed
993 994 995 996 997 998 999 1000 1001
  wclass->realize             = gtk_tool_palette_realize;

  cclass->add                 = gtk_tool_palette_add;
  cclass->remove              = gtk_tool_palette_remove;
  cclass->forall              = gtk_tool_palette_forall;
  cclass->child_type          = gtk_tool_palette_child_type;
  cclass->set_child_property  = gtk_tool_palette_set_child_property;
  cclass->get_child_property  = gtk_tool_palette_get_child_property;

1002 1003 1004
  /* Handle screen-changed so we can update our GtkSettings.
   */
  wclass->screen_changed      = gtk_tool_palette_screen_changed;
1005

1006
  g_object_class_override_property (oclass, PROP_ORIENTATION,    "orientation");
1007

1008 1009 1010 1011
  g_object_class_override_property (oclass, PROP_HADJUSTMENT,    "hadjustment");
  g_object_class_override_property (oclass, PROP_VADJUSTMENT,    "vadjustment");
  g_object_class_override_property (oclass, PROP_HSCROLL_POLICY, "hscroll-policy");
  g_object_class_override_property (oclass, PROP_VSCROLL_POLICY, "vscroll-policy");
Tadej Borovšak's avatar
Tadej Borovšak committed
1012

1013
  /**
1014 1015 1016
   * GtkToolPalette:icon-size:
   *
   * The size of the icons in a tool palette is normally determined by
1017 1018
   * the #GtkSettings:toolbar-icon-size setting. When this property is set,
   * it overrides the setting.
1019
   *
1020 1021
   * This should only be used for special-purpose tool palettes, normal
   * application tool palettes should respect the user preferences for the
1022 1023
   * size of icons.
   *
1024
   * Since: 2.20
1025 1026 1027
   */
  g_object_class_install_property (oclass,
                                   PROP_ICON_SIZE,
Johannes Schmid's avatar
Johannes Schmid committed
1028
                                   g_param_spec_enum ("icon-size",
1029 1030
                                                      P_("Icon size"),
                                                      P_("Size of icons in this tool palette"),
Johannes Schmid's avatar
Johannes Schmid committed
1031 1032
                                                      GTK_TYPE_ICON_SIZE,
                                                      DEFAULT_ICON_SIZE,
1033
                                                      GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1034

1035
  /**
Matthias Clasen's avatar
Matthias Clasen committed
1036
   * GtkToolPalette:icon-size-set:
1037
   *
1038
   * Is %TRUE if the #GtkToolPalette:icon-size property has been set.
1039
   *
1040
   * Since: 2.20
1041 1042 1043 1044 1045 1046 1047
   */
  g_object_class_install_property (oclass,
                                   PROP_ICON_SIZE_SET,
                                   g_param_spec_boolean ("icon-size-set",
                                                      P_("Icon size set"),
                                                      P_("Whether the icon-size property has been set"),
                                                      FALSE,
1048
                                                      GTK_PARAM_READWRITE));
1049

1050 1051 1052 1053 1054 1055 1056
  /**
   * GtkToolPalette:toolbar-style:
   *
   * The style of items in the tool palette.
   *
   * Since: 2.20
   */
Johannes Schmid's avatar
Johannes Schmid committed
1057 1058 1059 1060 1061 1062
  g_object_class_install_property (oclass, PROP_TOOLBAR_STYLE,
                                   g_param_spec_enum ("toolbar-style",
                                                      P_("Toolbar Style"),
                                                      P_("Style of items in the tool palette"),
                                                      GTK_TYPE_TOOLBAR_STYLE,
                                                      DEFAULT_TOOLBAR_STYLE,
1063
                                                      GTK_PARAM_READWRITE));
1064

Johannes Schmid's avatar
Johannes Schmid committed
1065

1066 1067 1068 1069 1070 1071 1072 1073
  /**
   * GtkToolPalette:exclusive:
   *
   * Whether the item group should be the only one that is expanded
   * at a given time.
   *
   * Since: 2.20
   */
Johannes Schmid's avatar
Johannes Schmid committed
1074 1075 1076 1077 1078
  gtk_container_class_install_child_property (cclass, CHILD_PROP_EXCLUSIVE,
                                              g_param_spec_boolean ("exclusive",
                                                                    P_("Exclusive"),
                                                                    P_("Whether the item group should be the only expanded at a given time"),
                                                                    DEFAULT_CHILD_EXCLUSIVE,
1079
                                                                    GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1080

1081 1082 1083 1084 1085 1086 1087 1088
  /**
   * GtkToolPalette:expand:
   *
   * Whether the item group should receive extra space when the palette grows.
   * at a given time.
   *
   * Since: 2.20
   */
Johannes Schmid's avatar
Johannes Schmid committed
1089 1090 1091 1092 1093
  gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
                                              g_param_spec_boolean ("expand",
                                                                    P_("Expand"),
                                                                    P_("Whether the item group should receive extra space when the palette grows"),
                                                                    DEFAULT_CHILD_EXPAND,
1094
                                                                    GTK_PARAM_READWRITE));
Johannes Schmid's avatar
Johannes Schmid committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

  g_type_class_add_private (cls, sizeof (GtkToolPalettePrivate));

  dnd_target_atom_item = gdk_atom_intern_static_string (dnd_targets[0].target);
  dnd_target_atom_group = gdk_atom_intern_static_string (dnd_targets[1].target);
}

/**
 * gtk_tool_palette_new:
 *
 * Creates a new tool palette.
 *
1107
 * Returns: a new #GtkToolPalette
Johannes Schmid's avatar
Johannes Schmid committed
1108
 *
1109
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1110 1111 1112 1113 1114 1115 1116 1117 1118
 */
GtkWidget*
gtk_tool_palette_new (void)
{
  return g_object_new (GTK_TYPE_TOOL_PALETTE, NULL);
}

/**
 * gtk_tool_palette_set_icon_size:
1119
 * @palette: a #GtkToolPalette
1120 1121
 * @icon_size: (type int): the #GtkIconSize that icons in the tool
 *     palette shall have
Johannes Schmid's avatar
Johannes Schmid committed
1122 1123 1124
 *
 * Sets the size of icons in the tool palette.
 *
1125
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1126 1127 1128 1129 1130
 */
void
gtk_tool_palette_set_icon_size (GtkToolPalette *palette,
                                GtkIconSize     icon_size)
{
1131
  GtkToolPalettePrivate *priv;
1132

Johannes Schmid's avatar
Johannes Schmid committed
1133
  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1134 1135 1136
  g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);

  priv = palette->priv;
1137

1138 1139
  if (!priv->icon_size_set)
    {
1140
      priv->icon_size_set = TRUE;
1141 1142
      g_object_notify (G_OBJECT (palette), "icon-size-set");
    }
Johannes Schmid's avatar
Johannes Schmid committed
1143

1144 1145
  if (priv->icon_size == icon_size)
    return;
1146

1147 1148
  priv->icon_size = icon_size;
  g_object_notify (G_OBJECT (palette), "icon-size");
1149

1150
  gtk_tool_palette_reconfigured (palette);
1151

1152 1153 1154 1155 1156 1157 1158 1159
  gtk_widget_queue_resize (GTK_WIDGET (palette));
}

static GtkSettings *
toolpalette_get_settings (GtkToolPalette *palette)
{
  GtkToolPalettePrivate *priv = palette->priv;
  return priv->settings;
Johannes Schmid's avatar
Johannes Schmid committed
1160 1161 1162
}

/**
1163
 * gtk_tool_palette_unset_icon_size:
1164
 * @palette: a #GtkToolPalette
1165
 *
1166 1167
 * Unsets the tool palette icon size set with gtk_tool_palette_set_icon_size(),
 * so that user preferences will be used to determine the icon size.
1168
 *
1169
 * Since: 2.20
1170 1171 1172 1173 1174 1175
 */
void
gtk_tool_palette_unset_icon_size (GtkToolPalette *palette)
{
  GtkToolPalettePrivate* priv = palette->priv;
  GtkIconSize size;
1176

1177
  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1178

1179 1180 1181
  if (palette->priv->icon_size_set)
    {
      GtkSettings *settings = toolpalette_get_settings (palette);
1182

1183 1184 1185 1186 1187 1188 1189 1190
      if (settings)
        {
          g_object_get (settings,
            "gtk-toolbar-icon-size", &size,
            NULL);
        }
      else
        size = DEFAULT_ICON_SIZE;
1191

1192 1193 1194
      if (size != palette->priv->icon_size)
      {
        gtk_tool_palette_set_icon_size (palette, size);
1195
        g_object_notify (G_OBJECT (palette), "icon-size");
1196
	    }
1197

1198
      priv->icon_size_set = FALSE;
1199
      g_object_notify (G_OBJECT (palette), "icon-size-set");
1200 1201 1202 1203
    }
}

/* Set the "toolbar-style" property and do appropriate things.
1204 1205
 * GtkToolbar does this by emitting a signal instead of just
 * calling a function...
1206 1207
 */
static void
1208 1209
gtk_tool_palette_change_style (GtkToolPalette  *palette,
                               GtkToolbarStyle  style)
1210 1211
{
  GtkToolPalettePrivate* priv = palette->priv;
1212

1213 1214 1215
  if (priv->style != style)
    {
      priv->style = style;
1216

1217
      gtk_tool_palette_reconfigured (palette);
1218

1219 1220 1221 1222 1223 1224 1225 1226
      gtk_widget_queue_resize (GTK_WIDGET (palette));
      g_object_notify (G_OBJECT (palette), "toolbar-style");
    }
}


/**
 * gtk_tool_palette_set_style:
1227 1228
 * @palette: a #GtkToolPalette
 * @style: the #GtkToolbarStyle that items in the tool palette shall have
Johannes Schmid's avatar
Johannes Schmid committed
1229 1230 1231
 *
 * Sets the style (text, icons or both) of items in the tool palette.
 *
1232
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1233 1234 1235 1236 1237 1238 1239
 */
void
gtk_tool_palette_set_style (GtkToolPalette  *palette,
                            GtkToolbarStyle  style)
{
  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));

1240
  palette->priv->style_set = TRUE;
1241 1242 1243 1244 1245 1246
  gtk_tool_palette_change_style (palette, style);
}


/**
 * gtk_tool_palette_unset_style:
1247 1248 1249 1250
 * @palette: a #GtkToolPalette
 *
 * Unsets a toolbar style set with gtk_tool_palette_set_style(),
 * so that user preferences will be used to determine the toolbar style.
1251
 *
1252 1253
 * Since: 2.20
 */
1254 1255 1256 1257 1258
void
gtk_tool_palette_unset_style (GtkToolPalette *palette)
{
  GtkToolPalettePrivate* priv = palette->priv;
  GtkToolbarStyle style;
1259

1260
  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1261

1262 1263 1264
  if (priv->style_set)
    {
      GtkSettings *settings = toolpalette_get_settings (palette);
1265

1266
      if (settings)
1267 1268 1269
        g_object_get (settings,
                      "gtk-toolbar-style", &style,
                      NULL);
1270 1271
      else
        style = DEFAULT_TOOLBAR_STYLE;
1272

1273 1274
      if (style != priv->style)
        gtk_tool_palette_change_style (palette, style);
1275

1276 1277
      priv->style_set = FALSE;
    }
Johannes Schmid's avatar
Johannes Schmid committed
1278 1279 1280 1281
}

/**
 * gtk_tool_palette_get_icon_size:
1282
 * @palette: a #GtkToolPalette
Johannes Schmid's avatar
Johannes Schmid committed
1283
 *
1284 1285
 * Gets the size of icons in the tool palette.
 * See gtk_tool_palette_set_icon_size().
1286
 *
1287
 * Returns: (type int): the #GtkIconSize of icons in the tool palette
Johannes Schmid's avatar
Johannes Schmid committed
1288
 *
1289
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1290 1291 1292 1293 1294
 */
GtkIconSize
gtk_tool_palette_get_icon_size (GtkToolPalette *palette)
{
  g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_ICON_SIZE);
1295

Johannes Schmid's avatar
Johannes Schmid committed
1296 1297 1298 1299 1300
  return palette->priv->icon_size;
}

/**
 * gtk_tool_palette_get_style:
1301
 * @palette: a #GtkToolPalette
Johannes Schmid's avatar
Johannes Schmid committed
1302 1303 1304 1305 1306
 *
 * Gets the style (icons, text or both) of items in the tool palette.
 *
 * Returns: the #GtkToolbarStyle of items in the tool palette.
 *
1307
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1308 1309 1310 1311 1312
 */
GtkToolbarStyle
gtk_tool_palette_get_style (GtkToolPalette *palette)
{
  g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_TOOLBAR_STYLE);
1313

Johannes Schmid's avatar
Johannes Schmid committed
1314 1315 1316
  return palette->priv->style;
}

1317
gint
1318
_gtk_tool_palette_compare_groups (gconstpointer a,
1319
                                  gconstpointer b)
1320 1321 1322 1323 1324 1325 1326
{
  const GtkToolItemGroupInfo *group_a = a;
  const GtkToolItemGroupInfo *group_b = b;

  return group_a->pos - group_b->pos;
}

Johannes Schmid's avatar
Johannes Schmid committed
1327 1328
/**
 * gtk_tool_palette_set_group_position:
1329 1330 1331
 * @palette: a #GtkToolPalette
 * @group: a #GtkToolItemGroup which is a child of palette
 * @position: a new index for group
Johannes Schmid's avatar
Johannes Schmid committed
1332 1333 1334 1335 1336
 *
 * Sets the position of the group as an index of the tool palette.
 * If position is 0 the group will become the first child, if position is
 * -1 it will become the last child.
 *
1337
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1338 1339
 */
void
1340 1341 1342
gtk_tool_palette_set_group_position (GtkToolPalette   *palette,
                                     GtkToolItemGroup *group,
                                     gint             position)
Johannes Schmid's avatar
Johannes Schmid committed
1343
{
1344
  GtkToolItemGroupInfo *group_new;
1345
  GtkToolItemGroupInfo *group_old;
Johannes Schmid's avatar
Johannes Schmid committed
1346 1347 1348 1349 1350 1351 1352
  gint old_position;

  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
  g_return_if_fail (position >= -1);

  if (-1 == position)
1353
    position = palette->priv->groups->len - 1;
Johannes Schmid's avatar
Johannes Schmid committed
1354

1355
  g_return_if_fail ((guint) position < palette->priv->groups->len);
Johannes Schmid's avatar
Johannes Schmid committed
1356

1357
  group_new = g_ptr_array_index (palette->priv->groups, position);
1358

1359
  if (GTK_TOOL_ITEM_GROUP (group) == group_new->widget)
Johannes Schmid's avatar
Johannes Schmid committed
1360 1361
    return;

1362
  old_position = gtk_tool_palette_get_group_position (palette, group);
Johannes Schmid's avatar
Johannes Schmid committed
1363 1364
  g_return_if_fail (old_position >= 0);

1365
  group_old = g_ptr_array_index (palette->priv->groups, old_position);
Johannes Schmid's avatar
Johannes Schmid committed
1366

1367 1368
  group_new->pos = position;
  group_old->pos = old_position;
Johannes Schmid's avatar
Johannes Schmid committed
1369

1370
  g_ptr_array_sort (palette->priv->groups, _gtk_tool_palette_compare_groups);
Johannes Schmid's avatar
Johannes Schmid committed
1371 1372 1373 1374 1375 1376

  gtk_widget_queue_resize (GTK_WIDGET (palette));
}

static void
gtk_tool_palette_group_notify_collapsed (GtkToolItemGroup *group,
Johannes Schmid's avatar
Johannes Schmid committed
1377
                                         GParamSpec       *pspec,
Johannes Schmid's avatar
Johannes Schmid committed
1378 1379 1380 1381 1382 1383 1384 1385
                                         gpointer          data)
{
  GtkToolPalette *palette = GTK_TOOL_PALETTE (data);
  guint i;

  if (gtk_tool_item_group_get_collapsed (group))
    return;

1386
  for (i = 0; i < palette->priv->groups->len; ++i)
Johannes Schmid's avatar
Johannes Schmid committed
1387
    {
1388 1389
      GtkToolItemGroupInfo *info = g_ptr_array_index (palette->priv->groups, i);
      GtkToolItemGroup *current_group = info->widget;
Johannes Schmid's avatar
Johannes Schmid committed
1390 1391

      if (current_group && current_group != group)
1392
        gtk_tool_item_group_set_collapsed (current_group, TRUE);
Johannes Schmid's avatar
Johannes Schmid committed
1393 1394 1395 1396 1397
    }
}

/**
 * gtk_tool_palette_set_exclusive:
1398 1399 1400
 * @palette: a #GtkToolPalette
 * @group: a #GtkToolItemGroup which is a child of palette
 * @exclusive: whether the group should be exclusive or not
Johannes Schmid's avatar
Johannes Schmid committed
1401
 *
1402 1403
 * Sets whether the group should be exclusive or not.
 * If an exclusive group is expanded all other groups are collapsed.
Johannes Schmid's avatar
Johannes Schmid committed
1404
 *
1405
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1406 1407
 */
void
1408 1409 1410
gtk_tool_palette_set_exclusive (GtkToolPalette   *palette,
                                GtkToolItemGroup *group,
                                gboolean          exclusive)
Johannes Schmid's avatar
Johannes Schmid committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
{
  GtkToolItemGroupInfo *group_info;
  gint position;

  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));

  position = gtk_tool_palette_get_group_position (palette, group);
  g_return_if_fail (position >= 0);

1421
  group_info = g_ptr_array_index (palette->priv->groups, position);
Johannes Schmid's avatar
Johannes Schmid committed
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444

  if (exclusive == group_info->exclusive)
    return;

  group_info->exclusive = exclusive;

  if (group_info->exclusive != (0 != group_info->notify_collapsed))
    {
      if (group_info->exclusive)
        {
          group_info->notify_collapsed =
            g_signal_connect (group, "notify::collapsed",
                              G_CALLBACK (gtk_tool_palette_group_notify_collapsed),
                              palette);
        }
      else
        {
          g_signal_handler_disconnect (group, group_info->notify_collapsed);
          group_info->notify_collapsed = 0;
        }
    }

  gtk_tool_palette_group_notify_collapsed (group_info->widget, NULL, palette);
1445
  gtk_widget_child_notify (GTK_WIDGET (group), "exclusive");
Johannes Schmid's avatar
Johannes Schmid committed
1446 1447 1448 1449
}

/**
 * gtk_tool_palette_set_expand:
1450 1451 1452
 * @palette: a #GtkToolPalette
 * @group: a #GtkToolItemGroup which is a child of palette
 * @expand: whether the group should be given extra space
Johannes Schmid's avatar
Johannes Schmid committed
1453 1454 1455
 *
 * Sets whether the group should be given extra space.
 *
1456
 * Since: 2.20
Johannes Schmid's avatar
Johannes Schmid committed
1457 1458
 */
void
1459 1460
gtk_tool_palette_set_expand (GtkToolPalette   *palette,
                             GtkToolItemGroup *group,
Johannes Schmid's avatar
Johannes Schmid committed
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
                             gboolean        expand)
{
  GtkToolItemGroupInfo *group_info;
  gint position;

  g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
  g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));

  position = gtk_tool_palette_get_group_position (palette, group);
  g_return_if_fail (position >= 0);

1472
  group_info = g_ptr_array_index (palette->priv->groups, position);
Johannes Schmid's avatar