Debian Codegraph @

src/paneltogglebutton.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2005 by Benjamin Otte <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 $Id$
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23 #include "paneltogglebutton.h"
24
25 static GtkToggleButtonClass *parent_class = NULL;
26
27
28 static void
29 panel_toggle_button_size_request (GtkWidget *widget,
30 GtkRequisition *requisition)
31 {
32 GtkWidget *child;
33
34 child = gtk_bin_get_child (GTK_BIN (widget));
35
36 if (child) {
37 gtk_widget_get_preferred_size (child, requisition, NULL);
38 } else {
39 requisition->width = requisition->height = 0;
40 }
41 }
42
43 static void
44 panel_toggle_button_get_preferred_width (GtkWidget *widget,
45 gint *minimal_width,
46 gint *natural_width)
47 {
48 GtkRequisition requisition;
49
50 panel_toggle_button_size_request (widget, &requisition);
51
52 *minimal_width = *natural_width = requisition.width;
53 }
54
55 static void
56 panel_toggle_button_get_preferred_height (GtkWidget *widget,
57 gint *minimal_height,
58 gint *natural_height)
59 {
60 GtkRequisition requisition;
61
62 panel_toggle_button_size_request (widget, &requisition);
63
64 *minimal_height = *natural_height = requisition.height;
65 }
66
67 static void
68 panel_toggle_button_size_allocate (GtkWidget *widget,
69 GtkAllocation *allocation)
70 {
71 GtkWidget *child;
72
73 child = gtk_bin_get_child (GTK_BIN (widget));
74
75 gtk_widget_set_allocation (widget, allocation);
76
77 if (gtk_widget_get_realized (widget))
78 gdk_window_move_resize (gtk_button_get_event_window (GTK_BUTTON(widget)),
79 allocation->x, allocation->y,
80 allocation->width, allocation->height);
81
82 if (child)
83 gtk_widget_size_allocate (child, allocation);
84 }
85
86 static gboolean
87 panel_toggle_button_draw (GtkWidget *widget, cairo_t *cr)
88 {
89 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
90 GtkStateType state_type;
91 GtkShadowType shadow_type;
92 GtkAllocation allocation;
93 GtkStyleContext *context = gtk_widget_get_style_context (widget);
94 GtkStateFlags flags = 0;
95
96 state_type = gtk_widget_get_state (widget);
97
98 /* FIXME: someone make this layout work nicely for all themes
99 * Currently I'm trying to imitate the volume applet's widget */
100 if (gtk_toggle_button_get_inconsistent (GTK_TOGGLE_BUTTON (widget))) {
101 if (state_type == GTK_STATE_ACTIVE)
102 state_type = GTK_STATE_NORMAL;
103 shadow_type = GTK_SHADOW_ETCHED_IN;
104 } else {
105 shadow_type = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)) ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
106 }
107 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
108 state_type = GTK_STATE_SELECTED;
109 /* FIXME: better detail? */
110 gtk_widget_get_allocation (widget, &allocation);
111 gtk_style_context_add_class (context, "togglebutton");
112 switch (state_type)
113 {
114 case GTK_STATE_PRELIGHT:
115 flags |= GTK_STATE_FLAG_PRELIGHT;
116 break;
117 case GTK_STATE_SELECTED:
118 flags |= GTK_STATE_FLAG_SELECTED;
119 break;
120 case GTK_STATE_INSENSITIVE:
121 flags |= GTK_STATE_FLAG_INSENSITIVE;
122 break;
123 case GTK_STATE_ACTIVE:
124 flags |= GTK_STATE_FLAG_ACTIVE;
125 break;
126 case GTK_STATE_FOCUSED:
127 flags |= GTK_STATE_FLAG_FOCUSED;
128 break;
129 case GTK_STATE_NORMAL:
130 case GTK_STATE_INCONSISTENT:
131 default:
132 break;
133 }
134
135 gtk_style_context_set_state (context, flags);
136 gtk_render_background (context, cr, (gdouble) allocation.x, (gdouble) allocation.y,
137 (gdouble) allocation.width, (gdouble) allocation.height);
138 (void) shadow_type;
139
140 if (child)
141 gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
142
143 return FALSE;
144 }
145
146 static gboolean
147 panel_toggle_button_button_press (GtkWidget *widget, GdkEventButton *event)
148 {
149 if (event->button == 3 || event->button == 2)
150 return FALSE;
151
152 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
153 }
154
155 static gboolean
156 panel_toggle_button_button_release (GtkWidget *widget, GdkEventButton *event)
157 {
158 if (event->button == 3 || event->button == 2)
159 return FALSE;
160
161 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
162 }
163
164 static void
165 panel_toggle_button_class_init (PanelToggleButtonClass *class)
166 {
167 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
168
169 parent_class = g_type_class_peek_parent (class);
170
171 widget_class->get_preferred_width = panel_toggle_button_get_preferred_width;
172 widget_class->get_preferred_height = panel_toggle_button_get_preferred_height;
173 widget_class->size_allocate = panel_toggle_button_size_allocate;
174 widget_class->draw = panel_toggle_button_draw;
175 widget_class->button_press_event = panel_toggle_button_button_press;
176 widget_class->button_release_event = panel_toggle_button_button_release;
177 }
178
179 static void
180 panel_toggle_button_init (PanelToggleButton *toggle_button)
181 {
182 }
183
184 GType
185 panel_toggle_button_get_type (void)
186 {
187 static GType toggle_button_type = 0;
188
189 if (!toggle_button_type)
190 {
191 static const GTypeInfo toggle_button_info =
192 {
193 sizeof (PanelToggleButtonClass),
194 NULL, /* base_init */
195 NULL, /* base_finalize */
196 (GClassInitFunc) panel_toggle_button_class_init,
197 NULL, /* class_finalize */
198 NULL, /* class_data */
199 sizeof (PanelToggleButton),
200 0, /* n_preallocs */
201 (GInstanceInitFunc) panel_toggle_button_init,
202 NULL, /* value_table */
203 };
204
205 toggle_button_type = g_type_register_static (GTK_TYPE_TOGGLE_BUTTON, "PanelToggleButton",
206 &toggle_button_info, 0);
207 }
208
209 return toggle_button_type;
210 }
211
212 GtkWidget *
213 panel_toggle_button_new (void)
214 {
215 return GTK_WIDGET (g_object_new (PANEL_TYPE_TOGGLE_BUTTON, NULL));
216 }
217