gtkmisc.c 13.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardon's avatar
Javier Jardon committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
26 27
#include "gtkcontainer.h"
#include "gtkmisc.h"
28
#include "gtklabel.h"
Alexander Larsson's avatar
Alexander Larsson committed
29
#include "gtkintl.h"
30
#include "gtkprivate.h"
Elliot Lee's avatar
Elliot Lee committed
31 32


Matthias Clasen's avatar
Matthias Clasen committed
33 34
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * SECTION:gtkmisc
 * @Short_description: Base class for widgets with alignments and padding
 * @Title: GtkMisc
 *
 * The #GtkMisc widget is an abstract widget which is not useful itself, but
 * is used to derive subclasses which have alignment and padding attributes.
 *
 * The horizontal and vertical padding attributes allows extra space to be
 * added around the widget.
 *
 * The horizontal and vertical alignment attributes enable the widget to be
 * positioned within its allocated area. Note that if the widget is added to
 * a container in such a way that it expands automatically to fill its
49
 * allocated area, the alignment settings will not alter the widget's position.
50 51 52
 *
 * Note that the desired effect can in most cases be achieved by using the
 * #GtkWidget:halign, #GtkWidget:valign and #GtkWidget:margin properties
Matthias Clasen's avatar
Matthias Clasen committed
53 54
 * on the child widget, so GtkMisc should not be used in new code. To reflect
 * this fact, all #GtkMisc API has been deprecated.
55 56
 */

57
struct _GtkMiscPrivate
58 59 60 61 62 63 64 65
{
  gfloat        xalign;
  gfloat        yalign;

  guint16       xpad;
  guint16       ypad;
};

66
enum {
Alexander Larsson's avatar
Alexander Larsson committed
67 68 69 70 71
  PROP_0,
  PROP_XALIGN,
  PROP_YALIGN,
  PROP_XPAD,
  PROP_YPAD
72 73
};

Manish Singh's avatar
Manish Singh committed
74 75 76 77 78 79 80 81 82 83 84
static void gtk_misc_realize      (GtkWidget    *widget);
static void gtk_misc_set_property (GObject         *object,
				   guint            prop_id,
				   const GValue    *value,
				   GParamSpec      *pspec);
static void gtk_misc_get_property (GObject         *object,
				   guint            prop_id,
				   GValue          *value,
				   GParamSpec      *pspec);


85
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GtkMisc, gtk_misc, GTK_TYPE_WIDGET)
Elliot Lee's avatar
Elliot Lee committed
86 87 88 89

static void
gtk_misc_class_init (GtkMiscClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
90
  GObjectClass   *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
91 92
  GtkWidgetClass *widget_class;

Alexander Larsson's avatar
Alexander Larsson committed
93
  gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
94 95
  widget_class = (GtkWidgetClass*) class;

Alexander Larsson's avatar
Alexander Larsson committed
96 97
  gobject_class->set_property = gtk_misc_set_property;
  gobject_class->get_property = gtk_misc_get_property;
98
  
Elliot Lee's avatar
Elliot Lee committed
99
  widget_class->realize = gtk_misc_realize;
Alexander Larsson's avatar
Alexander Larsson committed
100

101 102 103 104 105 106 107 108 109 110
  /**
   * GtkMisc:xalign:
   *
   * The horizontal alignment. A value of 0.0 means left alignment (or right
   * on RTL locales); a value of 1.0 means right alignment (or left on RTL
   * locales).
   *
   * Deprecated: 3.14: Use gtk_widget_set_halign() instead. If you are using
   *   #GtkLabel, use #GtkLabel:xalign instead.
   */
Alexander Larsson's avatar
Alexander Larsson committed
111 112 113
  g_object_class_install_property (gobject_class,
                                   PROP_XALIGN,
                                   g_param_spec_float ("xalign",
114
						       P_("X align"),
115
						       P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
Alexander Larsson's avatar
Alexander Larsson committed
116 117 118
						       0.0,
						       1.0,
						       0.5,
119
						       GTK_PARAM_READWRITE|G_PARAM_DEPRECATED));
Alexander Larsson's avatar
Alexander Larsson committed
120

121 122 123 124 125 126 127 128 129
  /**
   * GtkMisc:yalign:
   *
   * The vertical alignment. A value of 0.0 means top alignment;
   * a value of 1.0 means bottom alignment.
   *
   * Deprecated: 3.14: Use gtk_widget_set_valign() instead. If you are using
   *   #GtkLabel, use #GtkLabel:yalign instead.
   */
Alexander Larsson's avatar
Alexander Larsson committed
130 131 132
  g_object_class_install_property (gobject_class,
                                   PROP_YALIGN,
                                   g_param_spec_float ("yalign",
133 134
						       P_("Y align"),
						       P_("The vertical alignment, from 0 (top) to 1 (bottom)"),
Alexander Larsson's avatar
Alexander Larsson committed
135 136 137
						       0.0,
						       1.0,
						       0.5,
138
						       GTK_PARAM_READWRITE|G_PARAM_DEPRECATED));
Alexander Larsson's avatar
Alexander Larsson committed
139

140 141 142 143 144 145 146 147 148
  /**
   * GtkMisc:xpad:
   *
   * The amount of space to add on the left and right of the widget, in
   * pixels.
   *
   * Deprecated: 3.14: Use gtk_widget_set_margin_start() and
   *   gtk_widget_set_margin_end() instead
   */
Alexander Larsson's avatar
Alexander Larsson committed
149 150 151
  g_object_class_install_property (gobject_class,
                                   PROP_XPAD,
                                   g_param_spec_int ("xpad",
152 153
						     P_("X pad"),
						     P_("The amount of space to add on the left and right of the widget, in pixels"),
Alexander Larsson's avatar
Alexander Larsson committed
154 155 156
						     0,
						     G_MAXINT,
						     0,
Matthias Clasen's avatar
Matthias Clasen committed
157
						     GTK_PARAM_READWRITE|G_PARAM_DEPRECATED));
Alexander Larsson's avatar
Alexander Larsson committed
158

159 160 161 162 163 164 165 166 167
  /**
   * GtkMisc:ypad:
   *
   * The amount of space to add on the top and bottom of the widget, in
   * pixels.
   *
   * Deprecated: 3.14: Use gtk_widget_set_margin_top() and
   *   gtk_widget_set_margin_bottom() instead
   */
Alexander Larsson's avatar
Alexander Larsson committed
168 169 170
  g_object_class_install_property (gobject_class,
                                   PROP_YPAD,
                                   g_param_spec_int ("ypad",
171 172
						     P_("Y pad"),
						     P_("The amount of space to add on the top and bottom of the widget, in pixels"),
Alexander Larsson's avatar
Alexander Larsson committed
173 174 175
						     0,
						     G_MAXINT,
						     0,
Matthias Clasen's avatar
Matthias Clasen committed
176
						     GTK_PARAM_READWRITE|G_PARAM_DEPRECATED));
Elliot Lee's avatar
Elliot Lee committed
177 178 179 180 181
}

static void
gtk_misc_init (GtkMisc *misc)
{
182
  GtkMiscPrivate *priv;
183

184
  misc->priv = gtk_misc_get_instance_private (misc); 
185 186 187 188 189 190
  priv = misc->priv;

  priv->xalign = 0.5;
  priv->yalign = 0.5;
  priv->xpad = 0;
  priv->ypad = 0;
Elliot Lee's avatar
Elliot Lee committed
191 192
}

193
static void
Alexander Larsson's avatar
Alexander Larsson committed
194 195 196 197
gtk_misc_set_property (GObject      *object,
		       guint         prop_id,
		       const GValue *value,
		       GParamSpec   *pspec)
198
{
199
  GtkMisc *misc = GTK_MISC (object);
200
  GtkMiscPrivate *priv = misc->priv;
201

Alexander Larsson's avatar
Alexander Larsson committed
202
  switch (prop_id)
203
    {
Alexander Larsson's avatar
Alexander Larsson committed
204
    case PROP_XALIGN:
205
      gtk_misc_set_alignment (misc, g_value_get_float (value), priv->yalign);
206
      break;
Alexander Larsson's avatar
Alexander Larsson committed
207
    case PROP_YALIGN:
208
      gtk_misc_set_alignment (misc, priv->xalign, g_value_get_float (value));
209
      break;
Alexander Larsson's avatar
Alexander Larsson committed
210
    case PROP_XPAD:
211
      gtk_misc_set_padding (misc, g_value_get_int (value), priv->ypad);
212
      break;
Alexander Larsson's avatar
Alexander Larsson committed
213
    case PROP_YPAD:
214
      gtk_misc_set_padding (misc, priv->xpad, g_value_get_int (value));
215 216
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 219 220 221 222
      break;
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
223 224 225 226
gtk_misc_get_property (GObject      *object,
		       guint         prop_id,
		       GValue       *value,
		       GParamSpec   *pspec)
227
{
228
  GtkMisc *misc = GTK_MISC (object);
229
  GtkMiscPrivate *priv = misc->priv;
230

Alexander Larsson's avatar
Alexander Larsson committed
231
  switch (prop_id)
232
    {
Alexander Larsson's avatar
Alexander Larsson committed
233
    case PROP_XALIGN:
234
      g_value_set_float (value, priv->xalign);
235
      break;
Alexander Larsson's avatar
Alexander Larsson committed
236
    case PROP_YALIGN:
237
      g_value_set_float (value, priv->yalign);
238
      break;
Alexander Larsson's avatar
Alexander Larsson committed
239
    case PROP_XPAD:
240
      g_value_set_int (value, priv->xpad);
241
      break;
Alexander Larsson's avatar
Alexander Larsson committed
242
    case PROP_YPAD:
243
      g_value_set_int (value, priv->ypad);
244 245
      break;
    default:
Alexander Larsson's avatar
Alexander Larsson committed
246
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 248 249 250
      break;
    }
}

251 252 253 254 255 256 257
/**
 * gtk_misc_set_alignment:
 * @misc: a #GtkMisc.
 * @xalign: the horizontal alignment, from 0 (left) to 1 (right).
 * @yalign: the vertical alignment, from 0 (top) to 1 (bottom).
 *
 * Sets the alignment of the widget.
Matthias Clasen's avatar
Matthias Clasen committed
258
 *
259
 * Deprecated: 3.14: Use #GtkWidget's alignment (#GtkWidget:halign and #GtkWidget:valign) and margin properties or #GtkLabel's #GtkLabel:xalign and #GtkLabel:yalign properties.
260
 */
Elliot Lee's avatar
Elliot Lee committed
261 262 263 264 265
void
gtk_misc_set_alignment (GtkMisc *misc,
			gfloat   xalign,
			gfloat   yalign)
{
266
  GtkMiscPrivate *priv;
267 268
  GtkWidget *widget;

Elliot Lee's avatar
Elliot Lee committed
269 270
  g_return_if_fail (GTK_IS_MISC (misc));

271 272
  priv = misc->priv;

Elliot Lee's avatar
Elliot Lee committed
273 274 275 276 277 278 279 280 281 282
  if (xalign < 0.0)
    xalign = 0.0;
  else if (xalign > 1.0)
    xalign = 1.0;

  if (yalign < 0.0)
    yalign = 0.0;
  else if (yalign > 1.0)
    yalign = 1.0;

283
  if ((xalign != priv->xalign) || (yalign != priv->yalign))
Elliot Lee's avatar
Elliot Lee committed
284
    {
Matthias Clasen's avatar
Matthias Clasen committed
285
      g_object_freeze_notify (G_OBJECT (misc));
286
      if (xalign != priv->xalign)
Matthias Clasen's avatar
Matthias Clasen committed
287 288
	g_object_notify (G_OBJECT (misc), "xalign");

289
      if (yalign != priv->yalign)
Matthias Clasen's avatar
Matthias Clasen committed
290 291
	g_object_notify (G_OBJECT (misc), "yalign");

292 293
      priv->xalign = xalign;
      priv->yalign = yalign;
294
      
295 296 297 298 299 300
      if (GTK_IS_LABEL (misc))
        {
          gtk_label_set_xalign (GTK_LABEL (misc), xalign);
          gtk_label_set_yalign (GTK_LABEL (misc), yalign);
        }

Elliot Lee's avatar
Elliot Lee committed
301
      /* clear the area that was allocated before the change
302
       */
303 304 305
      widget = GTK_WIDGET (misc);
      if (gtk_widget_is_drawable (widget))
        gtk_widget_queue_draw (widget);
Alexander Larsson's avatar
Alexander Larsson committed
306

307
      g_object_thaw_notify (G_OBJECT (misc));
Elliot Lee's avatar
Elliot Lee committed
308 309 310
    }
}

311 312 313
/**
 * gtk_misc_get_alignment:
 * @misc: a #GtkMisc
314 315
 * @xalign: (out) (allow-none): location to store X alignment of @misc, or %NULL
 * @yalign: (out) (allow-none): location to store Y alignment of @misc, or %NULL
316
 *
317 318
 * Gets the X and Y alignment of the widget within its allocation. 
 * See gtk_misc_set_alignment().
Matthias Clasen's avatar
Matthias Clasen committed
319 320
 *
 * Deprecated: 3.14: Use #GtkWidget alignment and margin properties.
321 322 323 324 325 326
 **/
void
gtk_misc_get_alignment (GtkMisc *misc,
		        gfloat  *xalign,
			gfloat  *yalign)
{
327
  GtkMiscPrivate *priv;
328

329 330
  g_return_if_fail (GTK_IS_MISC (misc));

331 332
  priv = misc->priv;

333
  if (xalign)
334
    *xalign = priv->xalign;
335
  if (yalign)
336
    *yalign = priv->yalign;
337 338
}

339 340 341 342 343 344 345 346 347
/**
 * gtk_misc_set_padding:
 * @misc: a #GtkMisc.
 * @xpad: the amount of space to add on the left and right of the widget,
 *   in pixels.
 * @ypad: the amount of space to add on the top and bottom of the widget,
 *   in pixels.
 *
 * Sets the amount of space to add around the widget.
Matthias Clasen's avatar
Matthias Clasen committed
348 349
 *
 * Deprecated: 3.14: Use #GtkWidget alignment and margin properties.
350
 */
Elliot Lee's avatar
Elliot Lee committed
351 352 353 354 355
void
gtk_misc_set_padding (GtkMisc *misc,
		      gint     xpad,
		      gint     ypad)
{
356
  GtkMiscPrivate *priv;
357

Elliot Lee's avatar
Elliot Lee committed
358
  g_return_if_fail (GTK_IS_MISC (misc));
359 360 361

  priv = misc->priv;

Elliot Lee's avatar
Elliot Lee committed
362 363 364 365
  if (xpad < 0)
    xpad = 0;
  if (ypad < 0)
    ypad = 0;
366 367

  if ((xpad != priv->xpad) || (ypad != priv->ypad))
Elliot Lee's avatar
Elliot Lee committed
368
    {
Matthias Clasen's avatar
Matthias Clasen committed
369
      g_object_freeze_notify (G_OBJECT (misc));
370
      if (xpad != priv->xpad)
Matthias Clasen's avatar
Matthias Clasen committed
371 372
	g_object_notify (G_OBJECT (misc), "xpad");

373
      if (ypad != priv->ypad)
Matthias Clasen's avatar
Matthias Clasen committed
374 375
	g_object_notify (G_OBJECT (misc), "ypad");

376 377 378
      priv->xpad = xpad;
      priv->ypad = ypad;

379
      if (gtk_widget_is_drawable (GTK_WIDGET (misc)))
Elliot Lee's avatar
Elliot Lee committed
380
	gtk_widget_queue_resize (GTK_WIDGET (misc));
Alexander Larsson's avatar
Alexander Larsson committed
381

382
      g_object_thaw_notify (G_OBJECT (misc));
Elliot Lee's avatar
Elliot Lee committed
383 384 385
    }
}

386 387 388
/**
 * gtk_misc_get_padding:
 * @misc: a #GtkMisc
389 390 391 392
 * @xpad: (out) (allow-none): location to store padding in the X
 *        direction, or %NULL
 * @ypad: (out) (allow-none): location to store padding in the Y
 *        direction, or %NULL
393
 *
394 395
 * Gets the padding in the X and Y directions of the widget. 
 * See gtk_misc_set_padding().
Matthias Clasen's avatar
Matthias Clasen committed
396 397
 *
 * Deprecated: 3.14: Use #GtkWidget alignment and margin properties.
398 399 400 401 402 403
 **/
void
gtk_misc_get_padding (GtkMisc *misc,
		      gint    *xpad,
		      gint    *ypad)
{
404
  GtkMiscPrivate *priv;
405

406 407
  g_return_if_fail (GTK_IS_MISC (misc));

408 409
  priv = misc->priv;

410
  if (xpad)
411
    *xpad = priv->xpad;
412
  if (ypad)
413
    *ypad = priv->ypad;
414 415
}

Elliot Lee's avatar
Elliot Lee committed
416 417 418
static void
gtk_misc_realize (GtkWidget *widget)
{
419 420
  GtkAllocation allocation;
  GdkWindow *window;
Elliot Lee's avatar
Elliot Lee committed
421 422 423
  GdkWindowAttr attributes;
  gint attributes_mask;

424
  gtk_widget_set_realized (widget, TRUE);
Elliot Lee's avatar
Elliot Lee committed
425

426
  if (!gtk_widget_get_has_window (widget))
Elliot Lee's avatar
Elliot Lee committed
427
    {
428 429 430
      window = gtk_widget_get_parent_window (widget);
      gtk_widget_set_window (widget, window);
      g_object_ref (window);
Elliot Lee's avatar
Elliot Lee committed
431 432 433
    }
  else
    {
434 435
      gtk_widget_get_allocation (widget, &allocation);

Elliot Lee's avatar
Elliot Lee committed
436
      attributes.window_type = GDK_WINDOW_CHILD;
437 438 439 440
      attributes.x = allocation.x;
      attributes.y = allocation.y;
      attributes.width = allocation.width;
      attributes.height = allocation.height;
Elliot Lee's avatar
Elliot Lee committed
441 442
      attributes.wclass = GDK_INPUT_OUTPUT;
      attributes.visual = gtk_widget_get_visual (widget);
443
      attributes.event_mask = gtk_widget_get_events (widget);
444
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Elliot Lee's avatar
Elliot Lee committed
445

446 447
      window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
      gtk_widget_set_window (widget, window);
448
      gtk_widget_register_window (widget, window);
449
      gdk_window_set_background_pattern (window, NULL);
Elliot Lee's avatar
Elliot Lee committed
450 451
    }
}
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

/* Semi-private function used by gtk widgets inheriting from
 * GtkMisc that takes into account both css padding and border
 * and the padding specified with the GtkMisc properties.
 */
void
_gtk_misc_get_padding_and_border (GtkMisc   *misc,
                                  GtkBorder *border)
{
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder tmp;
  gint xpad, ypad;

  g_return_if_fail (GTK_IS_MISC (misc));

  context = gtk_widget_get_style_context (GTK_WIDGET (misc));
  state = gtk_widget_get_state_flags (GTK_WIDGET (misc));

  gtk_style_context_get_padding (context, state, border);

  gtk_misc_get_padding (misc, &xpad, &ypad);
474 475 476 477
  border->top += ypad;
  border->left += xpad;
  border->bottom += ypad;
  border->right += xpad;
478 479 480 481 482 483 484 485

  gtk_style_context_get_border (context, state, &tmp);
  border->top += tmp.top;
  border->right += tmp.right;
  border->bottom += tmp.bottom;
  border->left += tmp.left;
}