gtkarrow.c 7.94 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

27
#include <config.h>
28
#include <math.h>
29
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
30
#include "gtkarrow.h"
31
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
32

33
#define MIN_ARROW_SIZE  15
Elliot Lee's avatar
Elliot Lee committed
34

35
enum {
36 37 38 39 40 41
  PROP_0,

  PROP_ARROW_TYPE,
  PROP_SHADOW_TYPE,
  
  PROP_LAST
42 43
};

Elliot Lee's avatar
Elliot Lee committed
44 45 46 47 48

static void gtk_arrow_class_init (GtkArrowClass  *klass);
static void gtk_arrow_init       (GtkArrow       *arrow);
static gint gtk_arrow_expose     (GtkWidget      *widget,
				  GdkEventExpose *event);
49 50 51
static void gtk_arrow_set_property (GObject         *object,
				    guint            prop_id,
				    const GValue    *value,
Tim Janik's avatar
Tim Janik committed
52
				    GParamSpec      *pspec);
53 54 55
static void gtk_arrow_get_property (GObject         *object,
				    guint            prop_id,
				    GValue          *value,
Tim Janik's avatar
Tim Janik committed
56
				    GParamSpec      *pspec);
Elliot Lee's avatar
Elliot Lee committed
57

Manish Singh's avatar
Manish Singh committed
58
GType
59
gtk_arrow_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
60
{
Manish Singh's avatar
Manish Singh committed
61
  static GType arrow_type = 0;
Elliot Lee's avatar
Elliot Lee committed
62 63 64

  if (!arrow_type)
    {
Manish Singh's avatar
Manish Singh committed
65
      static const GTypeInfo arrow_info =
Elliot Lee's avatar
Elliot Lee committed
66 67
      {
	sizeof (GtkArrowClass),
Manish Singh's avatar
Manish Singh committed
68 69 70 71 72 73 74 75
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_arrow_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkArrow),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_arrow_init,
Elliot Lee's avatar
Elliot Lee committed
76 77
      };

Manish Singh's avatar
Manish Singh committed
78 79
      arrow_type = g_type_register_static (GTK_TYPE_MISC, "GtkArrow",
					   &arrow_info, 0);
Elliot Lee's avatar
Elliot Lee committed
80 81 82 83 84 85 86 87
    }

  return arrow_type;
}

static void
gtk_arrow_class_init (GtkArrowClass *class)
{
88
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
89 90
  GtkWidgetClass *widget_class;

91
  gobject_class = (GObjectClass*) class;
Elliot Lee's avatar
Elliot Lee committed
92 93
  widget_class = (GtkWidgetClass*) class;

94 95 96 97 98 99
  gobject_class->set_property = gtk_arrow_set_property;
  gobject_class->get_property = gtk_arrow_get_property;
  
  g_object_class_install_property (gobject_class,
                                   PROP_ARROW_TYPE,
                                   g_param_spec_enum ("arrow_type",
100 101
                                                      P_("Arrow direction"),
                                                      P_("The direction the arrow should point"),
102 103 104 105 106 107
						      GTK_TYPE_ARROW_TYPE,
						      GTK_ARROW_RIGHT,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
                                   g_param_spec_enum ("shadow_type",
108 109
                                                      P_("Arrow shadow"),
                                                      P_("Appearance of the shadow surrounding the arrow"),
110 111 112 113
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_OUT,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  
Elliot Lee's avatar
Elliot Lee committed
114 115 116
  widget_class->expose_event = gtk_arrow_expose;
}

Tim Janik's avatar
Tim Janik committed
117 118 119 120 121
static void
gtk_arrow_set_property (GObject         *object,
			guint            prop_id,
			const GValue    *value,
			GParamSpec      *pspec)
122 123
{
  GtkArrow *arrow;
Tim Janik's avatar
Tim Janik committed
124
  
125 126
  arrow = GTK_ARROW (object);

127
  switch (prop_id)
128
    {
129
    case PROP_ARROW_TYPE:
130
      gtk_arrow_set (arrow,
131
		     g_value_get_enum (value),
132 133
		     arrow->shadow_type);
      break;
134
    case PROP_SHADOW_TYPE:
135 136
      gtk_arrow_set (arrow,
		     arrow->arrow_type,
137
		     g_value_get_enum (value));
138 139
      break;
    default:
140
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 142 143 144
      break;
    }
}

145

Tim Janik's avatar
Tim Janik committed
146 147 148 149 150
static void
gtk_arrow_get_property (GObject         *object,
			guint            prop_id,
			GValue          *value,
			GParamSpec      *pspec)
151 152
{
  GtkArrow *arrow;
Tim Janik's avatar
Tim Janik committed
153
  
154
  arrow = GTK_ARROW (object);
155
  switch (prop_id)
156
    {
157 158
    case PROP_ARROW_TYPE:
      g_value_set_enum (value, arrow->arrow_type);
159
      break;
160 161
    case PROP_SHADOW_TYPE:
      g_value_set_enum (value, arrow->shadow_type);
162 163
      break;
    default:
164
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 166 167 168
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
169 170 171 172 173
static void
gtk_arrow_init (GtkArrow *arrow)
{
  GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);

174 175 176
  GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
  GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;

Elliot Lee's avatar
Elliot Lee committed
177 178 179 180 181 182 183 184 185 186
  arrow->arrow_type = GTK_ARROW_RIGHT;
  arrow->shadow_type = GTK_SHADOW_OUT;
}

GtkWidget*
gtk_arrow_new (GtkArrowType  arrow_type,
	       GtkShadowType shadow_type)
{
  GtkArrow *arrow;

Manish Singh's avatar
Manish Singh committed
187
  arrow = g_object_new (GTK_TYPE_ARROW, NULL);
Elliot Lee's avatar
Elliot Lee committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201

  arrow->arrow_type = arrow_type;
  arrow->shadow_type = shadow_type;

  return GTK_WIDGET (arrow);
}

void
gtk_arrow_set (GtkArrow      *arrow,
	       GtkArrowType   arrow_type,
	       GtkShadowType  shadow_type)
{
  g_return_if_fail (GTK_IS_ARROW (arrow));

202 203
  if (   ((GtkArrowType) arrow->arrow_type != arrow_type)
      || ((GtkShadowType) arrow->shadow_type != shadow_type))
Elliot Lee's avatar
Elliot Lee committed
204
    {
205
      g_object_freeze_notify (G_OBJECT (arrow));
206

207 208 209 210 211 212 213 214 215 216 217 218 219
      if ((GtkArrowType) arrow->arrow_type != arrow_type)
        {
          arrow->arrow_type = arrow_type;
          g_object_notify (G_OBJECT (arrow), "arrow_type");
        }

      if ((GtkShadowType) arrow->shadow_type != shadow_type)
        {
          arrow->shadow_type = shadow_type;
          g_object_notify (G_OBJECT (arrow), "shadow_type");
        }

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

      if (GTK_WIDGET_DRAWABLE (arrow))
Manish Singh's avatar
Manish Singh committed
222
	gtk_widget_queue_draw (GTK_WIDGET (arrow));
Elliot Lee's avatar
Elliot Lee committed
223 224 225 226
    }
}


227
static gboolean 
Elliot Lee's avatar
Elliot Lee committed
228 229 230 231 232 233 234 235 236
gtk_arrow_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
{
  GtkArrow *arrow;
  GtkMisc *misc;
  GtkShadowType shadow_type;
  gint width, height;
  gint x, y;
  gint extent;
237
  gfloat xalign;
238
  GtkArrowType effective_arrow_type;
Elliot Lee's avatar
Elliot Lee committed
239 240 241 242 243 244 245 246

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      arrow = GTK_ARROW (widget);
      misc = GTK_MISC (widget);

      width = widget->allocation.width - misc->xpad * 2;
      height = widget->allocation.height - misc->ypad * 2;
247
      extent = MIN (width, height) * 0.7;
248
      effective_arrow_type = arrow->arrow_type;
Elliot Lee's avatar
Elliot Lee committed
249

250 251 252
      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	xalign = misc->xalign;
      else
253 254 255 256 257 258 259
	{
	  xalign = 1.0 - misc->xalign;
	  if (arrow->arrow_type == GTK_ARROW_LEFT)
	    effective_arrow_type = GTK_ARROW_RIGHT;
	  else if (arrow->arrow_type == GTK_ARROW_RIGHT)
	    effective_arrow_type = GTK_ARROW_LEFT;
	}
260

261 262 263 264 265 266 267
      x = floor (widget->allocation.x + misc->xpad
		 + ((widget->allocation.width - extent) * xalign)
		 + 0.5);
      y = floor (widget->allocation.y + misc->ypad 
		 + ((widget->allocation.height - extent) * misc->yalign)
		 + 0.5);
      
Elliot Lee's avatar
Elliot Lee committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281
      shadow_type = arrow->shadow_type;

      if (widget->state == GTK_STATE_ACTIVE)
	{
          if (shadow_type == GTK_SHADOW_IN)
            shadow_type = GTK_SHADOW_OUT;
          else if (shadow_type == GTK_SHADOW_OUT)
            shadow_type = GTK_SHADOW_IN;
          else if (shadow_type == GTK_SHADOW_ETCHED_IN)
            shadow_type = GTK_SHADOW_ETCHED_OUT;
          else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
            shadow_type = GTK_SHADOW_ETCHED_IN;
	}

282 283 284
      gtk_paint_arrow (widget->style, widget->window,
		       widget->state, shadow_type,
		       &event->area, widget, "arrow",
285
		       effective_arrow_type, TRUE,
286
		       x, y, extent, extent);
Elliot Lee's avatar
Elliot Lee committed
287 288
    }

289
  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
290
}