3 * Modified version of gimpchainbutton.h for gLabels:
5 * LIBGIMP - The GIMP Library
6 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
9 * Copyright (C) 1999-2000 Sven Neumann <sven@gimp.org>
11 * Modified or gLabels by Jim Evins <evins@snaught.com>
14 * This file is part of gLabels.
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.
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.
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/>.
30 #include "wdgt-chain-button.h"
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);
51 static const gchar *gl_wdgt_chain_stock_items[] =
54 GL_STOCK_HCHAIN_BROKEN,
56 GL_STOCK_VCHAIN_BROKEN
60 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
64 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE);
68 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
70 gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
72 gl_wdgt_chain_button_signals[TOGGLED] =
73 g_signal_new ("toggled",
74 G_TYPE_FROM_CLASS (class),
76 G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
78 g_cclosure_marshal_VOID__VOID,
81 class->toggled = NULL;
85 gl_wdgt_chain_button_init (glWdgtChainButton *button)
87 button->position = GL_WDGT_CHAIN_TOP;
88 button->active = FALSE;
90 button->line1 = gtk_drawing_area_new ();
91 button->line2 = gtk_drawing_area_new ();
92 button->image = gtk_image_new ();
94 button->button = gtk_button_new ();
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);
100 g_signal_connect (button->button, "clicked",
101 G_CALLBACK (gl_wdgt_chain_button_clicked_callback),
103 g_signal_connect (button->line1, "expose_event",
104 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
106 g_signal_connect (button->line2, "expose_event",
107 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
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.
117 * Creates a new #glWdgtChainButton widget.
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.
126 * Returns: Pointer to the new #glWdgtChainButton, which is inactive
127 * by default. Use gl_wdgt_chain_button_set_active() to
131 gl_wdgt_chain_button_new (glWdgtChainPosition position)
133 glWdgtChainButton *button;
135 button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
137 button->position = position;
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);
144 if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
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);
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);
165 gtk_widget_show (button->button);
166 gtk_widget_show (button->line1);
167 gtk_widget_show (button->line2);
169 return GTK_WIDGET (button);
173 * gl_wdgt_chain_button_set_active:
174 * @button: Pointer to a #glWdgtChainButton.
175 * @active: The new state.
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.
181 gl_wdgt_chain_button_set_active (glWdgtChainButton *button,
184 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
186 if (button->active != active)
190 button->active = active ? TRUE : FALSE;
192 num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
194 gtk_image_set_from_stock (GTK_IMAGE (button->image),
195 gl_wdgt_chain_stock_items[num],
196 GTK_ICON_SIZE_BUTTON);
201 * gl_wdgt_chain_button_get_active
202 * @button: Pointer to a #glWdgtChainButton.
204 * Checks the state of the #glWdgtChainButton.
206 * Returns: %TRUE if the #glWdgtChainButton is active (locked).
209 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
211 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
213 return button->active;
217 gl_wdgt_chain_button_clicked_callback (GtkWidget *widget,
218 glWdgtChainButton *button)
220 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
222 gl_wdgt_chain_button_set_active (button, ! button->active);
224 g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
228 gl_wdgt_chain_button_draw_lines (GtkWidget *widget,
229 GdkEventExpose *eevent,
230 glWdgtChainButton *button)
232 GtkAllocation allocation;
235 GtkShadowType shadow;
236 glWdgtChainPosition position;
240 /* don't set this too high, there's no check against drawing outside
241 the widgets bounds yet (and probably never will be) */
243 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
245 gtk_widget_get_allocation (widget, &allocation);
246 points[0].x = allocation.width / 2;
247 points[0].y = allocation.height / 2;
249 which_line = (widget == button->line1) ? 1 : -1;
251 position = button->position;
253 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
256 case GL_WDGT_CHAIN_LEFT:
257 position = GL_WDGT_CHAIN_RIGHT;
259 case GL_WDGT_CHAIN_RIGHT:
260 position = GL_WDGT_CHAIN_LEFT;
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;
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;
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;
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;
304 if ( ((shadow == GTK_SHADOW_ETCHED_OUT) && (which_line == -1)) ||
305 ((shadow == GTK_SHADOW_ETCHED_IN) && (which_line == 1)) )
308 points[0] = points[2];
312 gtk_paint_polygon (gtk_widget_get_style (widget),
313 gtk_widget_get_window (widget),
329 * Local Variables: -- emacs
331 * c-basic-offset: 8 -- emacs
332 * tab-width: 8 -- emacs
333 * indent-tabs-mode: nil -- emacs