gtkseparatortoolitem.c 16.1 KB
Newer Older
1 2
/* gtkseparatortoolitem.c
 *
3
 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4 5 6 7 8 9 10 11 12 13 14 15 16
 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
 *
 * 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 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
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20
#include "config.h"
21 22 23
#include "gtkseparatormenuitem.h"
#include "gtkseparatortoolitem.h"
#include "gtkintl.h"
24
#include "gtktoolbarprivate.h"
25
#include "gtkprivate.h"
26 27
#include "gtkwidgetprivate.h"
#include "gtkcsscustomgadgetprivate.h"
28 29 30 31 32 33 34 35

/**
 * SECTION:gtkseparatortoolitem
 * @Short_description: A toolbar item that separates groups of other
 *   toolbar items
 * @Title: GtkSeparatorToolItem
 * @See_also: #GtkToolbar, #GtkRadioToolButton
 *
36
 * A #GtkSeparatorToolItem is a #GtkToolItem that separates groups of other
37 38 39
 * #GtkToolItems. Depending on the theme, a #GtkSeparatorToolItem will
 * often look like a vertical line on horizontally docked toolbars.
 *
40
 * If the #GtkToolbar child property “expand” is %TRUE and the property
41
 * #GtkSeparatorToolItem:draw is %FALSE, a #GtkSeparatorToolItem will act as
42
 * a “spring” that forces other items to the ends of the toolbar.
43 44
 *
 * Use gtk_separator_tool_item_new() to create a new #GtkSeparatorToolItem.
45 46 47 48
 *
 * # CSS nodes
 *
 * GtkSeparatorToolItem has a single CSS node with name separator.
49 50
 */

51 52
#define MENU_ID "gtk-separator-tool-item-menu-id"

53 54
struct _GtkSeparatorToolItemPrivate
{
55
  GtkCssGadget *gadget;
56
  GdkWindow *event_window;
57 58 59
  guint draw : 1;
};

60 61 62 63 64 65 66
enum {
  PROP_0,
  PROP_DRAW
};

static gboolean gtk_separator_tool_item_create_menu_proxy (GtkToolItem               *item);
static void     gtk_separator_tool_item_set_property      (GObject                   *object,
67 68 69 70 71 72 73
                                                           guint                      prop_id,
                                                           const GValue              *value,
                                                           GParamSpec                *pspec);
static void     gtk_separator_tool_item_get_property      (GObject                   *object,
                                                           guint                      prop_id,
                                                           GValue                    *value,
                                                           GParamSpec                *pspec);
74 75 76 77 78 79
static void     gtk_separator_tool_item_get_preferred_width (GtkWidget               *widget,
                                                           gint                      *minimum,
                                                           gint                      *natural);
static void     gtk_separator_tool_item_get_preferred_height (GtkWidget              *widget,
                                                           gint                      *minimum,
                                                           gint                      *natural);
80 81
static void     gtk_separator_tool_item_size_allocate     (GtkWidget                 *widget,
                                                           GtkAllocation             *allocation);
82 83
static gboolean gtk_separator_tool_item_draw              (GtkWidget                 *widget,
                                                           cairo_t                   *cr);
84
static void     gtk_separator_tool_item_add               (GtkContainer              *container,
85 86 87 88 89 90 91
                                                           GtkWidget                 *child);
static void     gtk_separator_tool_item_realize           (GtkWidget                 *widget);
static void     gtk_separator_tool_item_unrealize         (GtkWidget                 *widget);
static void     gtk_separator_tool_item_map               (GtkWidget                 *widget);
static void     gtk_separator_tool_item_unmap             (GtkWidget                 *widget);
static gboolean gtk_separator_tool_item_button_event      (GtkWidget                 *widget,
                                                           GdkEventButton            *event);
92 93
static gboolean gtk_separator_tool_item_motion_event      (GtkWidget                 *widget,
                                                           GdkEventMotion            *event);
94

95
G_DEFINE_TYPE_WITH_PRIVATE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
96

97 98 99 100 101 102 103 104 105 106
static void
gtk_separator_tool_item_finalize (GObject *object)
{
  GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);

  g_clear_object (&item->priv->gadget);

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

107 108 109
static void
gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
{
110
  GObjectClass *object_class;
111 112
  GtkContainerClass *container_class;
  GtkToolItemClass *toolitem_class;
113
  GtkWidgetClass *widget_class;
114 115
  
  object_class = (GObjectClass *)class;
116 117
  container_class = (GtkContainerClass *)class;
  toolitem_class = (GtkToolItemClass *)class;
118
  widget_class = (GtkWidgetClass *)class;
119

120 121
  object_class->set_property = gtk_separator_tool_item_set_property;
  object_class->get_property = gtk_separator_tool_item_get_property;
122 123
  object_class->finalize = gtk_separator_tool_item_finalize;

124 125
  widget_class->get_preferred_width = gtk_separator_tool_item_get_preferred_width;
  widget_class->get_preferred_height = gtk_separator_tool_item_get_preferred_height;
126
  widget_class->size_allocate = gtk_separator_tool_item_size_allocate;
127
  widget_class->draw = gtk_separator_tool_item_draw;
128 129 130 131 132 133
  widget_class->realize = gtk_separator_tool_item_realize;
  widget_class->unrealize = gtk_separator_tool_item_unrealize;
  widget_class->map = gtk_separator_tool_item_map;
  widget_class->unmap = gtk_separator_tool_item_unmap;
  widget_class->button_press_event = gtk_separator_tool_item_button_event;
  widget_class->button_release_event = gtk_separator_tool_item_button_event;
134
  widget_class->motion_notify_event = gtk_separator_tool_item_motion_event;
135

136
  toolitem_class->create_menu_proxy = gtk_separator_tool_item_create_menu_proxy;
137
  
138
  container_class->add = gtk_separator_tool_item_add;
139 140
  
  g_object_class_install_property (object_class,
141 142 143 144 145
                                   PROP_DRAW,
                                   g_param_spec_boolean ("draw",
                                                         P_("Draw"),
                                                         P_("Whether the separator is drawn, or just blank"),
                                                         TRUE,
146
                                                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
147 148

  gtk_widget_class_set_css_name (widget_class, "separator");
149 150 151
}

static void
152
gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
153
{
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  GtkSeparatorToolItemPrivate *priv;
  GtkWidget *widget;
  GtkCssNode *widget_node;

  widget = GTK_WIDGET (separator_item);
  priv = separator_item->priv = gtk_separator_tool_item_get_instance_private (separator_item);
  priv->draw = TRUE;

  gtk_widget_set_has_window (widget, FALSE);

  widget_node = gtk_widget_get_css_node (widget);
  separator_item->priv->gadget =
    gtk_css_custom_gadget_new_for_node (widget_node,
                                        widget,
168 169
                                        NULL, NULL, NULL,
                                        NULL, NULL);
170 171 172 173
}

static void
gtk_separator_tool_item_add (GtkContainer *container,
174
                             GtkWidget    *child)
175
{
176
  g_warning ("attempt to add a child to an GtkSeparatorToolItem");
177 178
}

179 180 181 182 183
static gboolean
gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
{
  GtkWidget *menu_item = NULL;
  
184
  menu_item = gtk_separator_menu_item_new ();
185 186 187 188 189 190
  
  gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
  
  return TRUE;
}

191 192
static void
gtk_separator_tool_item_set_property (GObject      *object,
193 194 195
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
  GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_DRAW:
      gtk_separator_tool_item_set_draw (item, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_separator_tool_item_get_property (GObject      *object,
212 213 214
                                      guint         prop_id,
                                      GValue       *value,
                                      GParamSpec   *pspec)
215 216 217 218 219 220 221 222 223 224 225 226 227 228
{
  GtkSeparatorToolItem *item = GTK_SEPARATOR_TOOL_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_DRAW:
      g_value_set_boolean (value, gtk_separator_tool_item_get_draw (item));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

229 230 231 232 233
static void
gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
                                             gint      *minimum,
                                             gint      *natural)
{
234 235 236 237 238
  gtk_css_gadget_get_preferred_size (GTK_SEPARATOR_TOOL_ITEM (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
239 240 241 242 243 244 245
}

static void
gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
                                              gint      *minimum,
                                              gint      *natural)
{
246 247 248 249 250
  gtk_css_gadget_get_preferred_size (GTK_SEPARATOR_TOOL_ITEM (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
251 252
}

253 254 255 256 257 258
static void
gtk_separator_tool_item_size_allocate (GtkWidget     *widget,
                                       GtkAllocation *allocation)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;
259
  GtkAllocation clip;
260

261
  gtk_widget_set_allocation (widget, allocation);
262 263 264

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->event_window,
265 266 267 268
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
269

270 271 272 273 274 275
  gtk_css_gadget_allocate (priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
276 277 278 279 280
}

static void
gtk_separator_tool_item_realize (GtkWidget *widget)
{
281
  GtkAllocation allocation;
282 283
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;
284
  GdkWindow *window;
285 286 287 288 289
  GdkWindowAttr attributes;
  gint attributes_mask;

  gtk_widget_set_realized (widget, TRUE);

290 291
  gtk_widget_get_allocation (widget, &allocation);

292
  attributes.window_type = GDK_WINDOW_CHILD;
293 294 295 296
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
297 298 299 300
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = gtk_widget_get_events (widget) |
                          GDK_BUTTON_PRESS_MASK |
301 302
                          GDK_BUTTON_RELEASE_MASK |
                          GDK_POINTER_MOTION_MASK;
303 304
  attributes_mask = GDK_WA_X | GDK_WA_Y;

305 306 307
  window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, window);
  g_object_ref (window);
308 309 310

  priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                       &attributes, attributes_mask);
311
  gtk_widget_register_window (widget, priv->event_window);
312 313 314 315 316 317 318 319 320 321
}

static void
gtk_separator_tool_item_unrealize (GtkWidget *widget)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;

  if (priv->event_window)
    {
322
      gtk_widget_unregister_window (widget, priv->event_window);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
      gdk_window_destroy (priv->event_window);
      priv->event_window = NULL;
    }

  GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unrealize (widget);
}

static void
gtk_separator_tool_item_map (GtkWidget *widget)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;

  GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->map (widget);

  if (priv->event_window)
    gdk_window_show (priv->event_window);
}

static void
gtk_separator_tool_item_unmap (GtkWidget *widget)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;

  if (priv->event_window)
    gdk_window_hide (priv->event_window);

  GTK_WIDGET_CLASS (gtk_separator_tool_item_parent_class)->unmap (widget);
}

354 355 356 357 358 359 360 361 362 363 364 365 366
static gboolean
gtk_separator_tool_item_motion_event (GtkWidget      *widget,
                                      GdkEventMotion *event)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;

  /* We want window dragging to work on empty toolbar areas,
   * so we only eat button events on visible separators
   */
  return priv->draw;
}

367 368 369 370 371 372 373 374 375 376 377 378 379
static gboolean
gtk_separator_tool_item_button_event (GtkWidget      *widget,
                                      GdkEventButton *event)
{
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;

  /* We want window dragging to work on empty toolbar areas,
   * so we only eat button events on visible separators
   */
  return priv->draw;
}

380
static gboolean
381 382
gtk_separator_tool_item_draw (GtkWidget *widget,
                              cairo_t   *cr)
383
{
384 385
  if (GTK_SEPARATOR_TOOL_ITEM (widget)->priv->draw)
    gtk_css_gadget_draw (GTK_SEPARATOR_TOOL_ITEM (widget)->priv->gadget, cr);
386

387
  return FALSE;
388 389
}

390 391 392 393 394
/**
 * gtk_separator_tool_item_new:
 * 
 * Create a new #GtkSeparatorToolItem
 * 
395
 * Returns: the new #GtkSeparatorToolItem
396 397
 * 
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
398
 */
399 400 401 402
GtkToolItem *
gtk_separator_tool_item_new (void)
{
  GtkToolItem *self;
403
  
404
  self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
405
                       NULL);
406 407 408
  
  return self;
}
409 410 411

/**
 * gtk_separator_tool_item_get_draw:
412
 * @item: a #GtkSeparatorToolItem 
413
 * 
Matthias Clasen's avatar
Matthias Clasen committed
414 415
 * Returns whether @item is drawn as a line, or just blank. 
 * See gtk_separator_tool_item_set_draw().
416
 * 
417
 * Returns: %TRUE if @item is drawn as a line, or just blank.
418 419
 * 
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
420
 */
421
gboolean
422
gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
423
{
424
  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
425
  
426
  return item->priv->draw;
427 428 429 430
}

/**
 * gtk_separator_tool_item_set_draw:
431
 * @item: a #GtkSeparatorToolItem
Matthias Clasen's avatar
Matthias Clasen committed
432
 * @draw: whether @item is drawn as a vertical line
433
 * 
Matthias Clasen's avatar
Matthias Clasen committed
434 435
 * Whether @item is drawn as a vertical line, or just blank.
 * Setting this to %FALSE along with gtk_tool_item_set_expand() is useful
436 437 438
 * to create an item that forces following items to the end of the toolbar.
 * 
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
439
 */
440
void
441
gtk_separator_tool_item_set_draw (GtkSeparatorToolItem *item,
442
                                  gboolean              draw)
443
{
444
  g_return_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item));
445 446 447

  draw = draw != FALSE;

448
  if (draw != item->priv->draw)
449
    {
450
      item->priv->draw = draw;
451 452 453 454
      if (draw)
        gtk_css_gadget_remove_class (item->priv->gadget, "invisible");
      else
        gtk_css_gadget_add_class (item->priv->gadget, "invisible");
455

456
      gtk_widget_queue_draw (GTK_WIDGET (item));
457

458
      g_object_notify (G_OBJECT (item), "draw");
459 460
    }
}