gtkarrow.c 10.7 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 Jardón's avatar
Javier Jardón 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-2001.  See the AUTHORS
20 21
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25 26 27 28
/**
 * SECTION:gtkarrow
 * @Short_description: Displays an arrow
 * @Title: GtkArrow
29
 * @See_also: gtk_render_arrow()
30 31 32 33 34
 *
 * GtkArrow should be used to draw simple arrows that need to point in
 * one of the four cardinal directions (up, down, left, or right).  The
 * style of the arrow can be one of shadow in, shadow out, etched in, or
 * etched out.  Note that these directions and style types may be
Christian Persch's avatar
Christian Persch committed
35
 * amended in versions of GTK+ to come.
36 37 38 39 40 41 42 43 44
 *
 * GtkArrow will fill any space alloted to it, but since it is inherited
 * from #GtkMisc, it can be padded and/or aligned, to fill exactly the
 * space the programmer desires.
 *
 * Arrows are created with a call to gtk_arrow_new().  The direction or
 * style of an arrow can be changed after creation by using gtk_arrow_set().
 */

45
#include "config.h"
46
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
47
#include "gtkarrow.h"
48
#include "gtksizerequest.h"
49
#include "gtktypebuiltins.h"
50
#include "gtkprivate.h"
51
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
52

53 54
#include "a11y/gtkarrowaccessible.h"

55
#define MIN_ARROW_SIZE  15
Elliot Lee's avatar
Elliot Lee committed
56

57
struct _GtkArrowPrivate
58 59 60 61 62
{
  gint16 arrow_type;
  gint16 shadow_type;
};

63
enum {
64 65
  PROP_0,
  PROP_ARROW_TYPE,
66
  PROP_SHADOW_TYPE
67 68
};

Elliot Lee's avatar
Elliot Lee committed
69

70 71 72 73 74 75 76 77
static void     gtk_arrow_set_property (GObject        *object,
                                        guint           prop_id,
                                        const GValue   *value,
                                        GParamSpec     *pspec);
static void     gtk_arrow_get_property (GObject        *object,
                                        guint           prop_id,
                                        GValue         *value,
                                        GParamSpec     *pspec);
78 79
static gboolean gtk_arrow_draw         (GtkWidget      *widget,
                                        cairo_t        *cr);
Elliot Lee's avatar
Elliot Lee committed
80

81 82 83 84 85 86
static void     gtk_arrow_get_preferred_width         (GtkWidget           *widget,
                                                       gint                *minimum_size,
                                                       gint                *natural_size);
static void     gtk_arrow_get_preferred_height        (GtkWidget           *widget,
                                                       gint                *minimum_size,
                                                       gint                *natural_size);
Elliot Lee's avatar
Elliot Lee committed
87

88
G_DEFINE_TYPE_WITH_PRIVATE (GtkArrow, gtk_arrow, GTK_TYPE_MISC)
Elliot Lee's avatar
Elliot Lee committed
89 90 91 92

static void
gtk_arrow_class_init (GtkArrowClass *class)
{
93
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
94 95
  GtkWidgetClass *widget_class;

96
  gobject_class = (GObjectClass*) class;
Elliot Lee's avatar
Elliot Lee committed
97 98
  widget_class = (GtkWidgetClass*) class;

99 100
  gobject_class->set_property = gtk_arrow_set_property;
  gobject_class->get_property = gtk_arrow_get_property;
101

102
  widget_class->draw = gtk_arrow_draw;
103 104
  widget_class->get_preferred_width  = gtk_arrow_get_preferred_width;
  widget_class->get_preferred_height = gtk_arrow_get_preferred_height;
105

106 107
  g_object_class_install_property (gobject_class,
                                   PROP_ARROW_TYPE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
108
                                   g_param_spec_enum ("arrow-type",
109 110
                                                      P_("Arrow direction"),
                                                      P_("The direction the arrow should point"),
111 112
						      GTK_TYPE_ARROW_TYPE,
						      GTK_ARROW_RIGHT,
113
                                                      GTK_PARAM_READWRITE));
114

115 116
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
117
                                   g_param_spec_enum ("shadow-type",
118 119
                                                      P_("Arrow shadow"),
                                                      P_("Appearance of the shadow surrounding the arrow"),
120 121
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_OUT,
122
                                                      GTK_PARAM_READWRITE));
123

124 125 126
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_float ("arrow-scaling",
                                                               P_("Arrow Scaling"),
127
                                                               P_("Amount of space used up by arrow"),
128 129
                                                               0.0, 1.0, 0.7,
                                                               GTK_PARAM_READABLE));
130

131
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ARROW_ACCESSIBLE);
Elliot Lee's avatar
Elliot Lee committed
132 133
}

Tim Janik's avatar
Tim Janik committed
134 135 136 137 138
static void
gtk_arrow_set_property (GObject         *object,
			guint            prop_id,
			const GValue    *value,
			GParamSpec      *pspec)
139
{
140
  GtkArrow *arrow = GTK_ARROW (object);
141
  GtkArrowPrivate *priv = arrow->priv;
142

143
  switch (prop_id)
144
    {
145
    case PROP_ARROW_TYPE:
146
      gtk_arrow_set (arrow,
147
		     g_value_get_enum (value),
148
		     priv->shadow_type);
149
      break;
150
    case PROP_SHADOW_TYPE:
151
      gtk_arrow_set (arrow,
152
		     priv->arrow_type,
153
		     g_value_get_enum (value));
154 155
      break;
    default:
156
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 158 159 160
      break;
    }
}

Tim Janik's avatar
Tim Janik committed
161 162 163 164 165
static void
gtk_arrow_get_property (GObject         *object,
			guint            prop_id,
			GValue          *value,
			GParamSpec      *pspec)
166
{
167
  GtkArrow *arrow = GTK_ARROW (object);
168
  GtkArrowPrivate *priv = arrow->priv;
169

170
  switch (prop_id)
171
    {
172
    case PROP_ARROW_TYPE:
173
      g_value_set_enum (value, priv->arrow_type);
174
      break;
175
    case PROP_SHADOW_TYPE:
176
      g_value_set_enum (value, priv->shadow_type);
177 178
      break;
    default:
179
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 181 182 183
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
184 185 186
static void
gtk_arrow_init (GtkArrow *arrow)
{
187
  arrow->priv = gtk_arrow_get_instance_private (arrow);
188

189
  gtk_widget_set_has_window (GTK_WIDGET (arrow), FALSE);
Elliot Lee's avatar
Elliot Lee committed
190

191 192
  arrow->priv->arrow_type = GTK_ARROW_RIGHT;
  arrow->priv->shadow_type = GTK_SHADOW_OUT;
Elliot Lee's avatar
Elliot Lee committed
193 194
}

195
static void
196 197 198
gtk_arrow_get_preferred_width (GtkWidget *widget,
                               gint      *minimum_size,
                               gint      *natural_size)
199
{
200
  GtkBorder border;
201

202
  _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
203 204

  if (minimum_size)
205
    *minimum_size = MIN_ARROW_SIZE + border.left + border.right;
206 207

  if (natural_size)
208
    *natural_size = MIN_ARROW_SIZE + border.left + border.right;
209 210 211
}

static void
212 213 214
gtk_arrow_get_preferred_height (GtkWidget *widget,
                                gint      *minimum_size,
                                gint      *natural_size)
215
{
216
  GtkBorder border;
217

218
  _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
219 220

  if (minimum_size)
221
    *minimum_size = MIN_ARROW_SIZE + border.top + border.bottom;
222 223

  if (natural_size)
224
    *natural_size = MIN_ARROW_SIZE + border.top + border.bottom;
225 226
}

227 228 229 230 231 232 233 234 235
/**
 * gtk_arrow_new:
 * @arrow_type: a valid #GtkArrowType.
 * @shadow_type: a valid #GtkShadowType.
 *
 * Creates a new #GtkArrow widget.
 *
 * Returns: the new #GtkArrow widget.
 */
Elliot Lee's avatar
Elliot Lee committed
236 237 238 239
GtkWidget*
gtk_arrow_new (GtkArrowType  arrow_type,
	       GtkShadowType shadow_type)
{
240
  GtkArrowPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
241 242
  GtkArrow *arrow;

Manish Singh's avatar
Manish Singh committed
243
  arrow = g_object_new (GTK_TYPE_ARROW, NULL);
Elliot Lee's avatar
Elliot Lee committed
244

245 246 247 248
  priv = arrow->priv;

  priv->arrow_type = arrow_type;
  priv->shadow_type = shadow_type;
Elliot Lee's avatar
Elliot Lee committed
249 250 251 252

  return GTK_WIDGET (arrow);
}

253 254 255 256 257 258 259 260
/**
 * gtk_arrow_set:
 * @arrow: a widget of type #GtkArrow.
 * @arrow_type: a valid #GtkArrowType.
 * @shadow_type: a valid #GtkShadowType.
 *
 * Sets the direction and style of the #GtkArrow, @arrow.
 */
Elliot Lee's avatar
Elliot Lee committed
261 262 263 264 265
void
gtk_arrow_set (GtkArrow      *arrow,
	       GtkArrowType   arrow_type,
	       GtkShadowType  shadow_type)
{
266
  GtkArrowPrivate *priv;
267 268
  GtkWidget *widget;

Elliot Lee's avatar
Elliot Lee committed
269 270
  g_return_if_fail (GTK_IS_ARROW (arrow));

271 272 273 274
  priv = arrow->priv;

  if (priv->arrow_type != arrow_type
      || priv->shadow_type != shadow_type)
Elliot Lee's avatar
Elliot Lee committed
275
    {
276
      g_object_freeze_notify (G_OBJECT (arrow));
277

278
      if ((GtkArrowType) priv->arrow_type != arrow_type)
279
        {
280
          priv->arrow_type = arrow_type;
281
          g_object_notify (G_OBJECT (arrow), "arrow-type");
282 283
        }

284
      if (priv->shadow_type != shadow_type)
285
        {
286
          priv->shadow_type = shadow_type;
287
          g_object_notify (G_OBJECT (arrow), "shadow-type");
288 289 290
        }

      g_object_thaw_notify (G_OBJECT (arrow));
Elliot Lee's avatar
Elliot Lee committed
291

292 293 294
      widget = GTK_WIDGET (arrow);
      if (gtk_widget_is_drawable (widget))
	gtk_widget_queue_draw (widget);
Elliot Lee's avatar
Elliot Lee committed
295 296 297
    }
}

298
static gboolean
299 300
gtk_arrow_draw (GtkWidget *widget,
                cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
301
{
302 303
  GtkArrow *arrow = GTK_ARROW (widget);
  GtkArrowPrivate *priv = arrow->priv;
304
  GtkStyleContext *context;
305 306
  gdouble x, y;
  gint width, height;
307
  gint extent;
308
  GtkBorder border;
309 310 311
  gfloat xalign, yalign;
  GtkArrowType effective_arrow_type;
  gfloat arrow_scaling;
312
  gdouble angle;
313

314 315 316 317
  if (priv->arrow_type == GTK_ARROW_NONE)
    return FALSE;

  context = gtk_widget_get_style_context (widget);
318 319
  gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);

320 321
  _gtk_misc_get_padding_and_border (GTK_MISC (widget), &border);
  gtk_misc_get_alignment (GTK_MISC (widget), &xalign, &yalign);
322

323 324
  width = gtk_widget_get_allocated_width (widget) - border.left - border.right;
  height = gtk_widget_get_allocated_height (widget) - border.top - border.bottom;
325

326
  extent = MIN (width, height) * arrow_scaling;
327 328 329
  effective_arrow_type = priv->arrow_type;

  if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
Elliot Lee's avatar
Elliot Lee committed
330
    {
331 332 333 334 335
      xalign = 1.0 - xalign;
      if (priv->arrow_type == GTK_ARROW_LEFT)
        effective_arrow_type = GTK_ARROW_RIGHT;
      else if (priv->arrow_type == GTK_ARROW_RIGHT)
        effective_arrow_type = GTK_ARROW_LEFT;
Elliot Lee's avatar
Elliot Lee committed
336 337
    }

338 339
  x = border.left + ((width - extent) * xalign);
  y = border.top + ((height - extent) * yalign);
340

341
  switch (effective_arrow_type)
342
    {
343 344 345 346 347 348 349 350 351 352 353 354 355
    case GTK_ARROW_UP:
      angle = 0;
      break;
    case GTK_ARROW_RIGHT:
      angle = G_PI / 2;
      break;
    case GTK_ARROW_DOWN:
      angle = G_PI;
      break;
    case GTK_ARROW_LEFT:
    default:
      angle = (3 * G_PI) / 2;
      break;
356 357
    }

358 359
  gtk_render_arrow (context, cr, angle, x, y, extent);

360
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
361
}