]> git.sur5r.net Git - glabels/blob - src/wdgt-chain-button.c
Fixed a few -Wall warnings
[glabels] / src / wdgt-chain-button.c
1 /*
2  *  wdgt-chain-button.c
3  *  Modified version of gimpchainbutton.h for gLabels:
4  *
5  *  LIBGIMP - The GIMP Library
6  *  Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
7  *
8  *  gimpchainbutton.h
9  *  Copyright (C) 1999-2000 Sven Neumann <sven@gimp.org>
10  *
11  *  Modified or gLabels by Jim Evins <evins@snaught.com>
12  *
13  *
14  *  This file is part of gLabels.
15  *
16  *  gLabels is free software: you can redistribute it and/or modify
17  *  it under the terms of the GNU General Public License as published by
18  *  the Free Software Foundation, either version 3 of the License, or
19  *  (at your option) any later version.
20  *
21  *  gLabels is distributed in the hope that it will be useful,
22  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *  GNU General Public License for more details.
25  *
26  *  You should have received a copy of the GNU General Public License
27  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 #include "wdgt-chain-button.h"
31
32 #include <gtk/gtk.h>
33
34 #include "stock.h"
35
36
37 enum
38 {
39   TOGGLED,
40   LAST_SIGNAL
41 };
42
43
44 static void      gl_wdgt_chain_button_clicked_callback (GtkWidget          *widget,
45                                                         glWdgtChainButton  *button);
46 static gboolean  gl_wdgt_chain_button_draw_lines       (GtkWidget          *widget,
47                                                         GdkEventExpose     *eevent,
48                                                         glWdgtChainButton  *button);
49
50
51 static const gchar *gl_wdgt_chain_stock_items[] =
52 {
53   GL_STOCK_HCHAIN,
54   GL_STOCK_HCHAIN_BROKEN,
55   GL_STOCK_VCHAIN,
56   GL_STOCK_VCHAIN_BROKEN
57 };
58
59
60 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
61
62
63
64 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE);
65
66
67 static void
68 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
69 {
70         gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
71
72         gl_wdgt_chain_button_signals[TOGGLED] =
73                 g_signal_new ("toggled",
74                               G_TYPE_FROM_CLASS (class),
75                               G_SIGNAL_RUN_FIRST,
76                               G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
77                               NULL, NULL,
78                               g_cclosure_marshal_VOID__VOID,
79                               G_TYPE_NONE, 0);
80
81         class->toggled = NULL;
82 }
83
84 static void
85 gl_wdgt_chain_button_init (glWdgtChainButton *button)
86 {
87         button->position = GL_WDGT_CHAIN_TOP;
88         button->active   = FALSE;
89
90         button->line1    = gtk_drawing_area_new ();
91         button->line2    = gtk_drawing_area_new ();
92         button->image    = gtk_image_new ();
93
94         button->button   = gtk_button_new ();
95
96         gtk_button_set_relief (GTK_BUTTON (button->button), GTK_RELIEF_NONE);
97         gtk_container_add (GTK_CONTAINER (button->button), button->image);
98         gtk_widget_show (button->image);
99
100         g_signal_connect (button->button, "clicked",
101                           G_CALLBACK (gl_wdgt_chain_button_clicked_callback),
102                           button);
103         g_signal_connect (button->line1, "expose_event",
104                           G_CALLBACK (gl_wdgt_chain_button_draw_lines),
105                           button);
106         g_signal_connect (button->line2, "expose_event",
107                           G_CALLBACK (gl_wdgt_chain_button_draw_lines),
108                           button);
109 }
110
111
112 /**
113  * gl_wdgt_chain_button_new:
114  * @position: The position you are going to use for the button
115  *            with respect to the widgets you want to chain.
116  *
117  * Creates a new #glWdgtChainButton widget.
118  *
119  * This returns a button showing either a broken or a linked chain and
120  * small clamps attached to both sides that visually group the two widgets
121  * you want to connect. This widget looks best when attached
122  * to a table taking up two columns (or rows respectively) next
123  * to the widgets that it is supposed to connect. It may work
124  * for more than two widgets, but the look is optimized for two.
125  *
126  * Returns: Pointer to the new #glWdgtChainButton, which is inactive
127  *          by default. Use gl_wdgt_chain_button_set_active() to
128  *          change its state.
129  */
130 GtkWidget *
131 gl_wdgt_chain_button_new (glWdgtChainPosition position)
132 {
133   glWdgtChainButton *button;
134
135   button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
136
137   button->position = position;
138
139   gtk_image_set_from_stock
140     (GTK_IMAGE (button->image),
141      gl_wdgt_chain_stock_items[((position & GL_WDGT_CHAIN_LEFT) << 1) + ! button->active],
142      GTK_ICON_SIZE_BUTTON);
143
144   if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
145     {
146       gtk_table_resize (GTK_TABLE (button), 3, 1);
147       gtk_table_attach (GTK_TABLE (button), button->button, 0, 1, 1, 2,
148                         GTK_SHRINK, GTK_SHRINK, 0, 0);
149       gtk_table_attach_defaults (GTK_TABLE (button),
150                                  button->line1, 0, 1, 0, 1);
151       gtk_table_attach_defaults (GTK_TABLE (button),
152                                  button->line2, 0, 1, 2, 3);
153     }
154   else
155     {
156       gtk_table_resize (GTK_TABLE (button), 1, 3);
157       gtk_table_attach (GTK_TABLE (button), button->button, 1, 2, 0, 1,
158                         GTK_SHRINK, GTK_SHRINK, 0, 0);
159       gtk_table_attach_defaults (GTK_TABLE (button),
160                                  button->line1, 0, 1, 0, 1);
161       gtk_table_attach_defaults (GTK_TABLE (button),
162                                  button->line2, 2, 3, 0, 1);
163     }
164
165   gtk_widget_show (button->button);
166   gtk_widget_show (button->line1);
167   gtk_widget_show (button->line2);
168
169   return GTK_WIDGET (button);
170 }
171
172 /**
173  * gl_wdgt_chain_button_set_active:
174  * @button: Pointer to a #glWdgtChainButton.
175  * @active: The new state.
176  *
177  * Sets the state of the #glWdgtChainButton to be either locked (%TRUE) or
178  * unlocked (%FALSE) and changes the showed pixmap to reflect the new state.
179  */
180 void
181 gl_wdgt_chain_button_set_active (glWdgtChainButton  *button,
182                               gboolean          active)
183 {
184   g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
185
186   if (button->active != active)
187     {
188       guint num;
189
190       button->active = active ? TRUE : FALSE;
191
192       num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
193
194       gtk_image_set_from_stock (GTK_IMAGE (button->image),
195                                 gl_wdgt_chain_stock_items[num],
196                                 GTK_ICON_SIZE_BUTTON);
197     }
198 }
199
200 /**
201  * gl_wdgt_chain_button_get_active
202  * @button: Pointer to a #glWdgtChainButton.
203  *
204  * Checks the state of the #glWdgtChainButton.
205  *
206  * Returns: %TRUE if the #glWdgtChainButton is active (locked).
207  */
208 gboolean
209 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
210 {
211   g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
212
213   return button->active;
214 }
215
216 static void
217 gl_wdgt_chain_button_clicked_callback (GtkWidget       *widget,
218                                     glWdgtChainButton *button)
219 {
220   g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
221
222   gl_wdgt_chain_button_set_active (button, ! button->active);
223
224   g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
225 }
226
227 static gboolean
228 gl_wdgt_chain_button_draw_lines (GtkWidget         *widget,
229                                  GdkEventExpose    *eevent,
230                                  glWdgtChainButton *button)
231 {
232   GtkAllocation        allocation;
233   GdkPoint             points[3];
234   GdkPoint             buf;
235   GtkShadowType        shadow;
236   glWdgtChainPosition  position;
237   gint                 which_line;
238
239 #define SHORT_LINE 4
240   /* don't set this too high, there's no check against drawing outside
241      the widgets bounds yet (and probably never will be) */
242
243   g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
244
245   gtk_widget_get_allocation (widget, &allocation);
246   points[0].x = allocation.width / 2;
247   points[0].y = allocation.height / 2;
248
249   which_line = (widget == button->line1) ? 1 : -1;
250
251   position = button->position;
252
253   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
254     switch (position)
255       {
256       case GL_WDGT_CHAIN_LEFT:
257         position = GL_WDGT_CHAIN_RIGHT;
258         break;
259       case GL_WDGT_CHAIN_RIGHT:
260         position = GL_WDGT_CHAIN_LEFT;
261         break;
262       default:
263         break;
264       }
265
266   switch (position)
267     {
268     case GL_WDGT_CHAIN_LEFT:
269       points[0].x += SHORT_LINE;
270       points[1].x = points[0].x - SHORT_LINE;
271       points[1].y = points[0].y;
272       points[2].x = points[1].x;
273       points[2].y = (which_line == 1) ? allocation.height - 1 : 0;
274       shadow = GTK_SHADOW_ETCHED_IN;
275       break;
276     case GL_WDGT_CHAIN_RIGHT:
277       points[0].x -= SHORT_LINE;
278       points[1].x = points[0].x + SHORT_LINE;
279       points[1].y = points[0].y;
280       points[2].x = points[1].x;
281       points[2].y = (which_line == 1) ? allocation.height - 1 : 0;
282       shadow = GTK_SHADOW_ETCHED_OUT;
283       break;
284     case GL_WDGT_CHAIN_TOP:
285       points[0].y += SHORT_LINE;
286       points[1].x = points[0].x;
287       points[1].y = points[0].y - SHORT_LINE;
288       points[2].x = (which_line == 1) ? allocation.width - 1 : 0;
289       points[2].y = points[1].y;
290       shadow = GTK_SHADOW_ETCHED_OUT;
291       break;
292     case GL_WDGT_CHAIN_BOTTOM:
293       points[0].y -= SHORT_LINE;
294       points[1].x = points[0].x;
295       points[1].y = points[0].y + SHORT_LINE;
296       points[2].x = (which_line == 1) ? allocation.width - 1 : 0;
297       points[2].y = points[1].y;
298       shadow = GTK_SHADOW_ETCHED_IN;
299       break;
300     default:
301       return FALSE;
302     }
303
304   if ( ((shadow == GTK_SHADOW_ETCHED_OUT) && (which_line == -1)) ||
305        ((shadow == GTK_SHADOW_ETCHED_IN) && (which_line == 1)) )
306     {
307       buf = points[0];
308       points[0] = points[2];
309       points[2] = buf;
310     }
311
312   gtk_paint_polygon (gtk_widget_get_style (widget),
313                      gtk_widget_get_window (widget),
314                      GTK_STATE_NORMAL,
315                      shadow,
316                      &eevent->area,
317                      widget,
318                      "chainbutton",
319                      points,
320                      3,
321                      FALSE);
322
323   return TRUE;
324 }
325
326
327
328 /*
329  * Local Variables:       -- emacs
330  * mode: C                -- emacs
331  * c-basic-offset: 8      -- emacs
332  * tab-width: 8           -- emacs
333  * indent-tabs-mode: nil  -- emacs
334  * End:                   -- emacs
335  */