]> git.sur5r.net Git - glabels/blob - src/wdgt-chain-button.c
Imported Upstream version 3.0.0
[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 <glib/gi18n.h>
33 #include <gtk/gtk.h>
34
35
36 enum
37 {
38   TOGGLED,
39   LAST_SIGNAL
40 };
41
42
43 static void      gl_wdgt_chain_button_clicked_callback (GtkWidget          *widget,
44                                                         glWdgtChainButton  *button);
45 static gboolean  gl_wdgt_chain_button_draw_lines       (GtkWidget          *widget,
46                                                         cairo_t            *cr,
47                                                         glWdgtChainButton  *button);
48
49
50 static const gchar *gl_wdgt_chain_stock_items[] =
51 {
52   "changes-prevent",
53   "changes-allow",
54   "changes-prevent",
55   "changes-allow"
56 };
57
58
59 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
60
61
62
63 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE)
64
65
66 static void
67 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
68 {
69         gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
70
71         gl_wdgt_chain_button_signals[TOGGLED] =
72                 g_signal_new ("toggled",
73                               G_TYPE_FROM_CLASS (class),
74                               G_SIGNAL_RUN_FIRST,
75                               G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
76                               NULL, NULL,
77                               g_cclosure_marshal_VOID__VOID,
78                               G_TYPE_NONE, 0);
79
80         class->toggled = NULL;
81 }
82
83 static void
84 gl_wdgt_chain_button_init (glWdgtChainButton *button)
85 {
86         button->position = GL_WDGT_CHAIN_TOP;
87         button->active   = FALSE;
88
89         button->line1    = gtk_drawing_area_new ();
90         button->line2    = gtk_drawing_area_new ();
91         button->image    = gtk_image_new ();
92
93         button->button   = gtk_toggle_button_new ();
94
95         gtk_button_set_relief (GTK_BUTTON (button->button), GTK_RELIEF_NONE);
96         gtk_button_set_focus_on_click (GTK_BUTTON (button->button), FALSE);
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, "draw",
104                           G_CALLBACK (gl_wdgt_chain_button_draw_lines),
105                           button);
106         g_signal_connect (button->line2, "draw",
107                           G_CALLBACK (gl_wdgt_chain_button_draw_lines),
108                           button);
109
110 }
111
112
113 /**
114  * gl_wdgt_chain_button_new:
115  * @position: The position you are going to use for the button
116  *            with respect to the widgets you want to chain.
117  *
118  * Creates a new #glWdgtChainButton widget.
119  *
120  * This returns a button showing either a broken or a linked chain and
121  * small clamps attached to both sides that visually group the two widgets
122  * you want to connect. This widget looks best when attached
123  * to a table taking up two columns (or rows respectively) next
124  * to the widgets that it is supposed to connect. It may work
125  * for more than two widgets, but the look is optimized for two.
126  *
127  * Returns: Pointer to the new #glWdgtChainButton, which is inactive
128  *          by default. Use gl_wdgt_chain_button_set_active() to
129  *          change its state.
130  */
131 GtkWidget *
132 gl_wdgt_chain_button_new (glWdgtChainPosition position)
133 {
134   glWdgtChainButton *button;
135
136   button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
137
138   button->position = position;
139
140   gtk_image_set_from_icon_name
141     (GTK_IMAGE (button->image),
142      gl_wdgt_chain_stock_items[((position & GL_WDGT_CHAIN_LEFT) << 1) + ! button->active],
143      GTK_ICON_SIZE_BUTTON);
144
145   if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
146     {
147       gtk_table_resize (GTK_TABLE (button), 3, 1);
148       gtk_table_attach (GTK_TABLE (button), button->button, 0, 1, 1, 2,
149                         GTK_SHRINK, GTK_SHRINK, 0, 0);
150       gtk_table_attach (GTK_TABLE (button), button->line1, 0, 1, 0, 1,
151                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0 );
152       gtk_table_attach (GTK_TABLE (button), button->line2, 0, 1, 2, 3,
153                         GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0 );
154     }
155   else
156     {
157       gtk_table_resize (GTK_TABLE (button), 1, 3);
158       gtk_table_attach (GTK_TABLE (button), button->button, 1, 2, 0, 1,
159                         GTK_SHRINK, GTK_SHRINK, 0, 0);
160       gtk_table_attach (GTK_TABLE (button), button->line1, 0, 1, 0, 1,
161                         GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
162       gtk_table_attach (GTK_TABLE (button), button->line2, 2, 3, 0, 1,
163                         GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
164     }
165
166   gtk_widget_show (button->button);
167   gtk_widget_show (button->line1);
168   gtk_widget_show (button->line2);
169
170   return GTK_WIDGET (button);
171 }
172
173 /**
174  * gl_wdgt_chain_button_set_active:
175  * @button: Pointer to a #glWdgtChainButton.
176  * @active: The new state.
177  *
178  * Sets the state of the #glWdgtChainButton to be either locked (%TRUE) or
179  * unlocked (%FALSE) and changes the showed pixmap to reflect the new state.
180  */
181 void
182 gl_wdgt_chain_button_set_active (glWdgtChainButton  *button,
183                                  gboolean          active)
184 {
185   g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
186
187   if (button->active != active)
188     {
189       guint num;
190
191       button->active = active ? TRUE : FALSE;
192
193       num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
194
195       gtk_image_set_from_icon_name (GTK_IMAGE (button->image),
196                                     gl_wdgt_chain_stock_items[num],
197                                     GTK_ICON_SIZE_MENU);
198
199       g_signal_handlers_block_by_func (G_OBJECT (button->button),
200                                        gl_wdgt_chain_button_clicked_callback, button);
201       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button->button), active);
202       g_signal_handlers_unblock_by_func (G_OBJECT (button->button),
203                                          gl_wdgt_chain_button_clicked_callback, button);
204     }
205 }
206
207 /**
208  * gl_wdgt_chain_button_get_active
209  * @button: Pointer to a #glWdgtChainButton.
210  *
211  * Checks the state of the #glWdgtChainButton.
212  *
213  * Returns: %TRUE if the #glWdgtChainButton is active (locked).
214  */
215 gboolean
216 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
217 {
218   g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
219
220   return button->active;
221 }
222
223 static void
224 gl_wdgt_chain_button_clicked_callback (GtkWidget       *widget,
225                                        glWdgtChainButton *button)
226 {
227   g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
228
229   gl_wdgt_chain_button_set_active (button, ! button->active);
230
231   g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
232 }
233
234 static gboolean
235 gl_wdgt_chain_button_draw_lines (GtkWidget         *widget,
236                                  cairo_t           *cr,
237                                  glWdgtChainButton *button)
238 {
239   GtkAllocation        allocation;
240   gdouble              w, h;
241   glWdgtChainPosition  position;
242   gint                 which_line;
243
244   g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
245
246   gtk_widget_get_allocation (widget, &allocation);
247   w = allocation.width;
248   h = allocation.height;
249
250   which_line = (widget == button->line1) ? 1 : -1;
251
252   position = button->position;
253
254   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
255     switch (position)
256       {
257       case GL_WDGT_CHAIN_LEFT:
258         position = GL_WDGT_CHAIN_RIGHT;
259         break;
260       case GL_WDGT_CHAIN_RIGHT:
261         position = GL_WDGT_CHAIN_LEFT;
262         break;
263       default:
264         break;
265       }
266
267   switch (position)
268     {
269     case GL_WDGT_CHAIN_LEFT:
270       cairo_move_to (cr, w-2, (which_line == 1) ? 0.6*h : 0.4*h);
271       cairo_line_to (cr, w/2, (which_line == 1) ? 0.6*h : 0.4*h);
272       cairo_line_to (cr, w/2, (which_line == 1) ? h-2 : 1);
273       break;
274     case GL_WDGT_CHAIN_RIGHT:
275       cairo_move_to (cr, 1,   (which_line == 1) ? 0.6*h : 0.4*h);
276       cairo_line_to (cr, w/2, (which_line == 1) ? 0.6*h : 0.4*h);
277       cairo_line_to (cr, w/2, (which_line == 1) ? h-2 : 1);
278       break;
279     case GL_WDGT_CHAIN_TOP:
280       cairo_move_to (cr, w/2, h-2);
281       cairo_line_to (cr, w/2, h/2);
282       cairo_line_to (cr, (which_line == 1) ? w-2 : 1, h/2);
283       break;
284     case GL_WDGT_CHAIN_BOTTOM:
285       cairo_move_to (cr, w/2, 1);
286       cairo_line_to (cr, w/2, h/2);
287       cairo_line_to (cr, (which_line == 1) ? w-2 : 1, h/2);
288       break;
289     default:
290       return FALSE;
291     }
292
293   cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
294   cairo_set_line_width (cr, 1.0);
295   cairo_set_source_rgb (cr, 0, 0, 0);
296
297   cairo_stroke (cr);
298
299   return TRUE;
300 }
301
302
303
304 /*
305  * Local Variables:       -- emacs
306  * mode: C                -- emacs
307  * c-basic-offset: 8      -- emacs
308  * tab-width: 8           -- emacs
309  * indent-tabs-mode: nil  -- emacs
310  * End:                   -- emacs
311  */