]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
Initial revision
[~andy/gtk] / gtk / gtkarrow.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtkarrow.h"
19
20
21 #define MIN_ARROW_SIZE  11
22
23
24 static void gtk_arrow_class_init (GtkArrowClass  *klass);
25 static void gtk_arrow_init       (GtkArrow       *arrow);
26 static gint gtk_arrow_expose     (GtkWidget      *widget,
27                                   GdkEventExpose *event);
28
29
30 guint
31 gtk_arrow_get_type ()
32 {
33   static guint arrow_type = 0;
34
35   if (!arrow_type)
36     {
37       GtkTypeInfo arrow_info =
38       {
39         "GtkArrow",
40         sizeof (GtkArrow),
41         sizeof (GtkArrowClass),
42         (GtkClassInitFunc) gtk_arrow_class_init,
43         (GtkObjectInitFunc) gtk_arrow_init,
44         (GtkArgFunc) NULL,
45       };
46
47       arrow_type = gtk_type_unique (gtk_misc_get_type (), &arrow_info);
48     }
49
50   return arrow_type;
51 }
52
53 static void
54 gtk_arrow_class_init (GtkArrowClass *class)
55 {
56   GtkWidgetClass *widget_class;
57
58   widget_class = (GtkWidgetClass*) class;
59
60   widget_class->expose_event = gtk_arrow_expose;
61 }
62
63 static void
64 gtk_arrow_init (GtkArrow *arrow)
65 {
66   GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);
67
68   arrow->arrow_type = GTK_ARROW_RIGHT;
69   arrow->shadow_type = GTK_SHADOW_OUT;
70 }
71
72 GtkWidget*
73 gtk_arrow_new (GtkArrowType  arrow_type,
74                GtkShadowType shadow_type)
75 {
76   GtkArrow *arrow;
77
78   arrow = gtk_type_new (gtk_arrow_get_type ());
79
80   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
81   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
82
83   arrow->arrow_type = arrow_type;
84   arrow->shadow_type = shadow_type;
85
86   return GTK_WIDGET (arrow);
87 }
88
89 void
90 gtk_arrow_set (GtkArrow      *arrow,
91                GtkArrowType   arrow_type,
92                GtkShadowType  shadow_type)
93 {
94   g_return_if_fail (arrow != NULL);
95   g_return_if_fail (GTK_IS_ARROW (arrow));
96
97   if (((GtkArrowType) arrow->arrow_type != arrow_type) ||
98       ((GtkShadowType) arrow->shadow_type != shadow_type))
99     {
100       arrow->arrow_type = arrow_type;
101       arrow->shadow_type = shadow_type;
102
103       if (GTK_WIDGET_DRAWABLE (arrow))
104         {
105           gdk_window_clear_area (GTK_WIDGET (arrow)->window,
106                                  GTK_WIDGET (arrow)->allocation.x + 1,
107                                  GTK_WIDGET (arrow)->allocation.y + 1,
108                                  GTK_WIDGET (arrow)->allocation.width - 2,
109                                  GTK_WIDGET (arrow)->allocation.height - 2);
110           gtk_widget_queue_draw (GTK_WIDGET (arrow));
111         }
112     }
113 }
114
115
116 static gint
117 gtk_arrow_expose (GtkWidget      *widget,
118                   GdkEventExpose *event)
119 {
120   GtkArrow *arrow;
121   GtkMisc *misc;
122   GtkShadowType shadow_type;
123   gint width, height;
124   gint x, y;
125   gint extent;
126
127   g_return_val_if_fail (widget != NULL, FALSE);
128   g_return_val_if_fail (GTK_IS_ARROW (widget), FALSE);
129   g_return_val_if_fail (event != NULL, FALSE);
130
131   if (GTK_WIDGET_DRAWABLE (widget))
132     {
133       arrow = GTK_ARROW (widget);
134       misc = GTK_MISC (widget);
135
136       width = widget->allocation.width - misc->xpad * 2;
137       height = widget->allocation.height - misc->ypad * 2;
138       extent = MIN (width, height);
139
140       x = ((widget->allocation.x + misc->xpad) * (1.0 - misc->xalign) +
141            (widget->allocation.x + widget->allocation.width - extent - misc->ypad) * misc->xalign);
142       y = ((widget->allocation.y + misc->ypad) * (1.0 - misc->yalign) +
143            (widget->allocation.y + widget->allocation.height - extent - misc->ypad) * misc->yalign);
144
145       shadow_type = arrow->shadow_type;
146
147       if (widget->state == GTK_STATE_ACTIVE)
148         {
149           if (shadow_type == GTK_SHADOW_IN)
150             shadow_type = GTK_SHADOW_OUT;
151           else if (shadow_type == GTK_SHADOW_OUT)
152             shadow_type = GTK_SHADOW_IN;
153           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
154             shadow_type = GTK_SHADOW_ETCHED_OUT;
155           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
156             shadow_type = GTK_SHADOW_ETCHED_IN;
157         }
158
159       gtk_draw_arrow (widget->style, widget->window,
160                       widget->state, shadow_type, arrow->arrow_type, TRUE,
161                       x, y, extent, extent);
162     }
163
164   return FALSE;
165 }