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 28 29 30 31 32 33

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

45 46
#define MENU_ID "gtk-separator-tool-item-menu-id"

47 48
struct _GtkSeparatorToolItemPrivate
{
49
  GdkWindow *event_window;
50 51 52
  guint draw : 1;
};

53 54 55 56 57 58 59
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,
60 61 62 63 64 65 66
                                                           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);
67 68 69 70 71 72
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);
73 74
static void     gtk_separator_tool_item_size_allocate     (GtkWidget                 *widget,
                                                           GtkAllocation             *allocation);
75 76
static gboolean gtk_separator_tool_item_draw              (GtkWidget                 *widget,
                                                           cairo_t                   *cr);
77
static void     gtk_separator_tool_item_add               (GtkContainer              *container,
78
                                                           GtkWidget                 *child);
79
static gint     get_space_size                            (GtkToolItem               *tool_item);
80 81 82 83 84 85
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);
86 87
static gboolean gtk_separator_tool_item_motion_event      (GtkWidget                 *widget,
                                                           GdkEventMotion            *event);
88 89


90
G_DEFINE_TYPE_WITH_PRIVATE (GtkSeparatorToolItem, gtk_separator_tool_item, GTK_TYPE_TOOL_ITEM)
91

92 93 94
static gint
get_space_size (GtkToolItem *tool_item)
{
95
  gint space_size = _gtk_toolbar_get_default_space_size();
96 97 98 99
  GtkWidget *parent;

  parent = gtk_widget_get_parent (GTK_WIDGET (tool_item));

100 101 102
  if (GTK_IS_TOOLBAR (parent))
    {
      gtk_widget_style_get (parent,
103 104
                            "space-size", &space_size,
                            NULL);
105
    }
106
  
107 108
  return space_size;
}
109 110 111 112

static void
gtk_separator_tool_item_class_init (GtkSeparatorToolItemClass *class)
{
113
  GObjectClass *object_class;
114 115
  GtkContainerClass *container_class;
  GtkToolItemClass *toolitem_class;
116
  GtkWidgetClass *widget_class;
117 118
  
  object_class = (GObjectClass *)class;
119 120
  container_class = (GtkContainerClass *)class;
  toolitem_class = (GtkToolItemClass *)class;
121
  widget_class = (GtkWidgetClass *)class;
122

123 124
  object_class->set_property = gtk_separator_tool_item_set_property;
  object_class->get_property = gtk_separator_tool_item_get_property;
125 126
  widget_class->get_preferred_width = gtk_separator_tool_item_get_preferred_width;
  widget_class->get_preferred_height = gtk_separator_tool_item_get_preferred_height;
127
  widget_class->size_allocate = gtk_separator_tool_item_size_allocate;
128
  widget_class->draw = gtk_separator_tool_item_draw;
129 130 131 132 133 134
  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;
135
  widget_class->motion_notify_event = gtk_separator_tool_item_motion_event;
136

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

static void
151
gtk_separator_tool_item_init (GtkSeparatorToolItem *separator_item)
152
{
153 154
  GtkStyleContext *context;

155
  separator_item->priv = gtk_separator_tool_item_get_instance_private (separator_item);
156
  separator_item->priv->draw = TRUE;
157 158

  gtk_widget_set_has_window (GTK_WIDGET (separator_item), FALSE);
159 160 161

  context = gtk_widget_get_style_context (GTK_WIDGET (separator_item));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SEPARATOR);
162 163 164 165
}

static void
gtk_separator_tool_item_add (GtkContainer *container,
166
                             GtkWidget    *child)
167
{
168
  g_warning ("attempt to add a child to an GtkSeparatorToolItem");
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182
static gboolean
gtk_separator_tool_item_create_menu_proxy (GtkToolItem *item)
{
  GtkWidget *menu_item = NULL;
  
  menu_item = gtk_separator_menu_item_new();
  
  gtk_tool_item_set_proxy_menu_item (item, MENU_ID, menu_item);
  
  return TRUE;
}

183 184
static void
gtk_separator_tool_item_set_property (GObject      *object,
185 186 187
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
  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,
204 205 206
                                      guint         prop_id,
                                      GValue       *value,
                                      GParamSpec   *pspec)
207 208 209 210 211 212 213 214 215 216 217 218 219 220
{
  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;
    }
}

221
static void
222 223 224 225
gtk_separator_tool_item_get_preferred_size (GtkWidget      *widget,
                                            GtkOrientation  orientation,
                                            gint           *minimum,
                                            gint           *natural)
226
{
227 228
  if (gtk_tool_item_get_orientation (GTK_TOOL_ITEM (widget)) == orientation)
    *minimum = *natural = get_space_size (GTK_TOOL_ITEM (widget));
229
  else
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    *minimum = *natural = 1;
}

static void
gtk_separator_tool_item_get_preferred_width (GtkWidget *widget,
                                             gint      *minimum,
                                             gint      *natural)
{
  gtk_separator_tool_item_get_preferred_size (widget,
                                              GTK_ORIENTATION_HORIZONTAL,
                                              minimum,
                                              natural);
}

static void
gtk_separator_tool_item_get_preferred_height (GtkWidget *widget,
                                              gint      *minimum,
                                              gint      *natural)
{
  gtk_separator_tool_item_get_preferred_size (widget,
                                              GTK_ORIENTATION_VERTICAL,
                                              minimum,
                                              natural);
253 254
}

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

262
  gtk_widget_set_allocation (widget, allocation);
263 264 265

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->event_window,
266 267 268 269
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
270 271 272 273 274 275

}

static void
gtk_separator_tool_item_realize (GtkWidget *widget)
{
276
  GtkAllocation allocation;
277 278
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;
279
  GdkWindow *window;
280 281 282 283 284
  GdkWindowAttr attributes;
  gint attributes_mask;

  gtk_widget_set_realized (widget, TRUE);

285 286
  gtk_widget_get_allocation (widget, &allocation);

287
  attributes.window_type = GDK_WINDOW_CHILD;
288 289 290 291
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
292 293 294 295
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = gtk_widget_get_events (widget) |
                          GDK_BUTTON_PRESS_MASK |
296 297
                          GDK_BUTTON_RELEASE_MASK |
                          GDK_POINTER_MOTION_MASK;
298 299
  attributes_mask = GDK_WA_X | GDK_WA_Y;

300 301 302
  window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, window);
  g_object_ref (window);
303 304 305

  priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                       &attributes, attributes_mask);
306
  gtk_widget_register_window (widget, priv->event_window);
307 308 309 310 311 312 313 314 315 316
}

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

  if (priv->event_window)
    {
317
      gtk_widget_unregister_window (widget, priv->event_window);
318 319 320 321 322 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
      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);
}

349 350 351 352 353 354 355 356 357 358 359 360 361
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;
}

362 363 364 365 366 367 368 369 370 371 372 373 374
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;
}

375
static gboolean
376 377
gtk_separator_tool_item_draw (GtkWidget *widget,
                              cairo_t   *cr)
378
{
379
  GtkAllocation allocation;
380
  GtkToolbar *toolbar = NULL;
381 382
  GtkSeparatorToolItem *separator = GTK_SEPARATOR_TOOL_ITEM (widget);
  GtkSeparatorToolItemPrivate *priv = separator->priv;
383
  GtkWidget *parent;
384

385 386
  if (priv->draw)
    {
387 388 389
      parent = gtk_widget_get_parent (widget);
      if (GTK_IS_TOOLBAR (parent))
        toolbar = GTK_TOOLBAR (parent);
390

391
      gtk_widget_get_allocation (widget, &allocation);
392
      _gtk_toolbar_paint_space_line (widget, toolbar, cr);
393
    }
394

395
  return FALSE;
396 397
}

398 399 400 401 402
/**
 * gtk_separator_tool_item_new:
 * 
 * Create a new #GtkSeparatorToolItem
 * 
403
 * Returns: the new #GtkSeparatorToolItem
404 405
 * 
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
406
 */
407 408 409 410
GtkToolItem *
gtk_separator_tool_item_new (void)
{
  GtkToolItem *self;
411
  
412
  self = g_object_new (GTK_TYPE_SEPARATOR_TOOL_ITEM,
413
                       NULL);
414 415 416
  
  return self;
}
417 418 419

/**
 * gtk_separator_tool_item_get_draw:
420
 * @item: a #GtkSeparatorToolItem 
421
 * 
Matthias Clasen's avatar
Matthias Clasen committed
422 423
 * Returns whether @item is drawn as a line, or just blank. 
 * See gtk_separator_tool_item_set_draw().
424
 * 
425
 * Returns: %TRUE if @item is drawn as a line, or just blank.
426 427
 * 
 * Since: 2.4
Matthias Clasen's avatar
Matthias Clasen committed
428
 */
429
gboolean
430
gtk_separator_tool_item_get_draw (GtkSeparatorToolItem *item)
431
{
432
  g_return_val_if_fail (GTK_IS_SEPARATOR_TOOL_ITEM (item), FALSE);
433
  
434
  return item->priv->draw;
435 436 437 438
}

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

  draw = draw != FALSE;

456
  if (draw != item->priv->draw)
457
    {
458
      item->priv->draw = draw;
459

460
      gtk_widget_queue_draw (GTK_WIDGET (item));
461

462
      g_object_notify (G_OBJECT (item), "draw");
463 464
    }
}