]> git.sur5r.net Git - glabels/blob - src/wdgt-chain-button.c
Organized master branch to be top-level directory for glabels, instead of
[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   GdkPoint             points[3];
233   GdkPoint             buf;
234   GtkShadowType        shadow;
235   glWdgtChainPosition  position;
236   gint                 which_line;
237
238 #define SHORT_LINE 4
239   /* don't set this too high, there's no check against drawing outside
240      the widgets bounds yet (and probably never will be) */
241
242   g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
243
244   points[0].x = widget->allocation.width / 2;
245   points[0].y = widget->allocation.height / 2;
246
247   which_line = (widget == button->line1) ? 1 : -1;
248
249   position = button->position;
250
251   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
252     switch (position)
253       {
254       case GL_WDGT_CHAIN_LEFT:
255         position = GL_WDGT_CHAIN_RIGHT;
256         break;
257       case GL_WDGT_CHAIN_RIGHT:
258         position = GL_WDGT_CHAIN_LEFT;
259         break;
260       default:
261         break;
262       }
263
264   switch (position)
265     {
266     case GL_WDGT_CHAIN_LEFT:
267       points[0].x += SHORT_LINE;
268       points[1].x = points[0].x - SHORT_LINE;
269       points[1].y = points[0].y;
270       points[2].x = points[1].x;
271       points[2].y = (which_line == 1) ? widget->allocation.height - 1 : 0;
272       shadow = GTK_SHADOW_ETCHED_IN;
273       break;
274     case GL_WDGT_CHAIN_RIGHT:
275       points[0].x -= SHORT_LINE;
276       points[1].x = points[0].x + SHORT_LINE;
277       points[1].y = points[0].y;
278       points[2].x = points[1].x;
279       points[2].y = (which_line == 1) ? widget->allocation.height - 1 : 0;
280       shadow = GTK_SHADOW_ETCHED_OUT;
281       break;
282     case GL_WDGT_CHAIN_TOP:
283       points[0].y += SHORT_LINE;
284       points[1].x = points[0].x;
285       points[1].y = points[0].y - SHORT_LINE;
286       points[2].x = (which_line == 1) ? widget->allocation.width - 1 : 0;
287       points[2].y = points[1].y;
288       shadow = GTK_SHADOW_ETCHED_OUT;
289       break;
290     case GL_WDGT_CHAIN_BOTTOM:
291       points[0].y -= SHORT_LINE;
292       points[1].x = points[0].x;
293       points[1].y = points[0].y + SHORT_LINE;
294       points[2].x = (which_line == 1) ? widget->allocation.width - 1 : 0;
295       points[2].y = points[1].y;
296       shadow = GTK_SHADOW_ETCHED_IN;
297       break;
298     default:
299       return FALSE;
300     }
301
302   if ( ((shadow == GTK_SHADOW_ETCHED_OUT) && (which_line == -1)) ||
303        ((shadow == GTK_SHADOW_ETCHED_IN) && (which_line == 1)) )
304     {
305       buf = points[0];
306       points[0] = points[2];
307       points[2] = buf;
308     }
309
310   gtk_paint_polygon (widget->style,
311                      widget->window,
312                      GTK_STATE_NORMAL,
313                      shadow,
314                      &eevent->area,
315                      widget,
316                      "chainbutton",
317                      points,
318                      3,
319                      FALSE);
320
321   return TRUE;
322 }
323
324
325
326 /*
327  * Local Variables:       -- emacs
328  * mode: C                -- emacs
329  * c-basic-offset: 8      -- emacs
330  * tab-width: 8           -- emacs
331  * indent-tabs-mode: nil  -- emacs
332  * End:                   -- emacs
333  */