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