]> Pileus Git - ~andy/gtk/blob - gtk/gtkarrow.c
call the base class init fucntions from all parent types upon class
[~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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "gtkarrow.h"
20
21
22 #define MIN_ARROW_SIZE  11
23
24
25 static void gtk_arrow_class_init (GtkArrowClass  *klass);
26 static void gtk_arrow_init       (GtkArrow       *arrow);
27 static gint gtk_arrow_expose     (GtkWidget      *widget,
28                                   GdkEventExpose *event);
29
30
31 guint
32 gtk_arrow_get_type (void)
33 {
34   static guint arrow_type = 0;
35
36   if (!arrow_type)
37     {
38       GtkTypeInfo arrow_info =
39       {
40         "GtkArrow",
41         sizeof (GtkArrow),
42         sizeof (GtkArrowClass),
43         (GtkClassInitFunc) gtk_arrow_class_init,
44         (GtkObjectInitFunc) gtk_arrow_init,
45         /* reversed_1 */ NULL,
46         /* reversed_2 */ NULL,
47         (GtkClassInitFunc) NULL,
48       };
49
50       arrow_type = gtk_type_unique (gtk_misc_get_type (), &arrow_info);
51     }
52
53   return arrow_type;
54 }
55
56 static void
57 gtk_arrow_class_init (GtkArrowClass *class)
58 {
59   GtkWidgetClass *widget_class;
60
61   widget_class = (GtkWidgetClass*) class;
62
63   widget_class->expose_event = gtk_arrow_expose;
64 }
65
66 static void
67 gtk_arrow_init (GtkArrow *arrow)
68 {
69   GTK_WIDGET_SET_FLAGS (arrow, GTK_NO_WINDOW);
70
71   arrow->arrow_type = GTK_ARROW_RIGHT;
72   arrow->shadow_type = GTK_SHADOW_OUT;
73 }
74
75 GtkWidget*
76 gtk_arrow_new (GtkArrowType  arrow_type,
77                GtkShadowType shadow_type)
78 {
79   GtkArrow *arrow;
80
81   arrow = gtk_type_new (gtk_arrow_get_type ());
82
83   GTK_WIDGET (arrow)->requisition.width = MIN_ARROW_SIZE + GTK_MISC (arrow)->xpad * 2;
84   GTK_WIDGET (arrow)->requisition.height = MIN_ARROW_SIZE + GTK_MISC (arrow)->ypad * 2;
85
86   arrow->arrow_type = arrow_type;
87   arrow->shadow_type = shadow_type;
88
89   return GTK_WIDGET (arrow);
90 }
91
92 void
93 gtk_arrow_set (GtkArrow      *arrow,
94                GtkArrowType   arrow_type,
95                GtkShadowType  shadow_type)
96 {
97   g_return_if_fail (arrow != NULL);
98   g_return_if_fail (GTK_IS_ARROW (arrow));
99
100   if (((GtkArrowType) arrow->arrow_type != arrow_type) ||
101       ((GtkShadowType) arrow->shadow_type != shadow_type))
102     {
103       arrow->arrow_type = arrow_type;
104       arrow->shadow_type = shadow_type;
105
106       if (GTK_WIDGET_DRAWABLE (arrow))
107         {
108           gdk_window_clear_area (GTK_WIDGET (arrow)->window,
109                                  GTK_WIDGET (arrow)->allocation.x + 1,
110                                  GTK_WIDGET (arrow)->allocation.y + 1,
111                                  GTK_WIDGET (arrow)->allocation.width - 2,
112                                  GTK_WIDGET (arrow)->allocation.height - 2);
113           gtk_widget_queue_draw (GTK_WIDGET (arrow));
114         }
115     }
116 }
117
118
119 static gint
120 gtk_arrow_expose (GtkWidget      *widget,
121                   GdkEventExpose *event)
122 {
123   GtkArrow *arrow;
124   GtkMisc *misc;
125   GtkShadowType shadow_type;
126   gint width, height;
127   gint x, y;
128   gint extent;
129
130   g_return_val_if_fail (widget != NULL, FALSE);
131   g_return_val_if_fail (GTK_IS_ARROW (widget), FALSE);
132   g_return_val_if_fail (event != NULL, FALSE);
133
134   if (GTK_WIDGET_DRAWABLE (widget))
135     {
136       arrow = GTK_ARROW (widget);
137       misc = GTK_MISC (widget);
138
139       width = widget->allocation.width - misc->xpad * 2;
140       height = widget->allocation.height - misc->ypad * 2;
141       extent = MIN (width, height);
142
143       x = ((widget->allocation.x + misc->xpad) * (1.0 - misc->xalign) +
144            (widget->allocation.x + widget->allocation.width - extent - misc->ypad) * misc->xalign);
145       y = ((widget->allocation.y + misc->ypad) * (1.0 - misc->yalign) +
146            (widget->allocation.y + widget->allocation.height - extent - misc->ypad) * misc->yalign);
147
148       shadow_type = arrow->shadow_type;
149
150       if (widget->state == GTK_STATE_ACTIVE)
151         {
152           if (shadow_type == GTK_SHADOW_IN)
153             shadow_type = GTK_SHADOW_OUT;
154           else if (shadow_type == GTK_SHADOW_OUT)
155             shadow_type = GTK_SHADOW_IN;
156           else if (shadow_type == GTK_SHADOW_ETCHED_IN)
157             shadow_type = GTK_SHADOW_ETCHED_OUT;
158           else if (shadow_type == GTK_SHADOW_ETCHED_OUT)
159             shadow_type = GTK_SHADOW_ETCHED_IN;
160         }
161
162       gtk_draw_arrow (widget->style, widget->window,
163                       widget->state, shadow_type, arrow->arrow_type, TRUE,
164                       x, y, extent, extent);
165     }
166
167   return FALSE;
168 }