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"
32 #include <glib/gi18n.h>
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,
47 glWdgtChainButton *button);
50 static const gchar *gl_wdgt_chain_stock_items[] =
54 "glabels-hchain-broken",
56 "glabels-vchain-broken"
66 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
70 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE);
74 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
76 gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
78 gl_wdgt_chain_button_signals[TOGGLED] =
79 g_signal_new ("toggled",
80 G_TYPE_FROM_CLASS (class),
82 G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
84 g_cclosure_marshal_VOID__VOID,
87 class->toggled = NULL;
91 gl_wdgt_chain_button_init (glWdgtChainButton *button)
93 button->position = GL_WDGT_CHAIN_TOP;
94 button->active = FALSE;
96 button->line1 = gtk_drawing_area_new ();
97 button->line2 = gtk_drawing_area_new ();
98 button->image = gtk_image_new ();
100 button->button = gtk_toggle_button_new ();
102 gtk_button_set_relief (GTK_BUTTON (button->button), GTK_RELIEF_NONE);
103 gtk_button_set_focus_on_click (GTK_BUTTON (button->button), FALSE);
104 gtk_container_add (GTK_CONTAINER (button->button), button->image);
105 gtk_widget_show (button->image);
107 g_signal_connect (button->button, "clicked",
108 G_CALLBACK (gl_wdgt_chain_button_clicked_callback),
110 g_signal_connect (button->line1, "draw",
111 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
113 g_signal_connect (button->line2, "draw",
114 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
121 * gl_wdgt_chain_button_new:
122 * @position: The position you are going to use for the button
123 * with respect to the widgets you want to chain.
125 * Creates a new #glWdgtChainButton widget.
127 * This returns a button showing either a broken or a linked chain and
128 * small clamps attached to both sides that visually group the two widgets
129 * you want to connect. This widget looks best when attached
130 * to a table taking up two columns (or rows respectively) next
131 * to the widgets that it is supposed to connect. It may work
132 * for more than two widgets, but the look is optimized for two.
134 * Returns: Pointer to the new #glWdgtChainButton, which is inactive
135 * by default. Use gl_wdgt_chain_button_set_active() to
139 gl_wdgt_chain_button_new (glWdgtChainPosition position)
141 glWdgtChainButton *button;
143 button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
145 button->position = position;
147 gtk_image_set_from_icon_name
148 (GTK_IMAGE (button->image),
149 gl_wdgt_chain_stock_items[((position & GL_WDGT_CHAIN_LEFT) << 1) + ! button->active],
150 GTK_ICON_SIZE_BUTTON);
152 if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
154 gtk_table_resize (GTK_TABLE (button), 3, 1);
155 gtk_table_attach (GTK_TABLE (button), button->button, 0, 1, 1, 2,
156 GTK_SHRINK, GTK_SHRINK, 0, 0);
157 gtk_table_attach (GTK_TABLE (button), button->line1, 0, 1, 0, 1,
158 GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0 );
159 gtk_table_attach (GTK_TABLE (button), button->line2, 0, 1, 2, 3,
160 GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0 );
164 gtk_table_resize (GTK_TABLE (button), 1, 3);
165 gtk_table_attach (GTK_TABLE (button), button->button, 1, 2, 0, 1,
166 GTK_SHRINK, GTK_SHRINK, 0, 0);
167 gtk_table_attach (GTK_TABLE (button), button->line1, 0, 1, 0, 1,
168 GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
169 gtk_table_attach (GTK_TABLE (button), button->line2, 2, 3, 0, 1,
170 GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
173 gtk_widget_show (button->button);
174 gtk_widget_show (button->line1);
175 gtk_widget_show (button->line2);
177 return GTK_WIDGET (button);
181 * gl_wdgt_chain_button_set_active:
182 * @button: Pointer to a #glWdgtChainButton.
183 * @active: The new state.
185 * Sets the state of the #glWdgtChainButton to be either locked (%TRUE) or
186 * unlocked (%FALSE) and changes the showed pixmap to reflect the new state.
189 gl_wdgt_chain_button_set_active (glWdgtChainButton *button,
192 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
194 if (button->active != active)
198 button->active = active ? TRUE : FALSE;
200 num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
202 gtk_image_set_from_icon_name (GTK_IMAGE (button->image),
203 gl_wdgt_chain_stock_items[num],
206 g_signal_handlers_block_by_func (G_OBJECT (button->button),
207 gl_wdgt_chain_button_clicked_callback, button);
208 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button->button), active);
209 g_signal_handlers_unblock_by_func (G_OBJECT (button->button),
210 gl_wdgt_chain_button_clicked_callback, button);
215 * gl_wdgt_chain_button_get_active
216 * @button: Pointer to a #glWdgtChainButton.
218 * Checks the state of the #glWdgtChainButton.
220 * Returns: %TRUE if the #glWdgtChainButton is active (locked).
223 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
225 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
227 return button->active;
231 gl_wdgt_chain_button_clicked_callback (GtkWidget *widget,
232 glWdgtChainButton *button)
234 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
236 gl_wdgt_chain_button_set_active (button, ! button->active);
238 g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
242 gl_wdgt_chain_button_draw_lines (GtkWidget *widget,
244 glWdgtChainButton *button)
246 GtkAllocation allocation;
248 glWdgtChainPosition position;
251 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
253 gtk_widget_get_allocation (widget, &allocation);
254 w = allocation.width;
255 h = allocation.height;
257 which_line = (widget == button->line1) ? 1 : -1;
259 position = button->position;
261 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
264 case GL_WDGT_CHAIN_LEFT:
265 position = GL_WDGT_CHAIN_RIGHT;
267 case GL_WDGT_CHAIN_RIGHT:
268 position = GL_WDGT_CHAIN_LEFT;
276 case GL_WDGT_CHAIN_LEFT:
277 cairo_move_to (cr, w-2, (which_line == 1) ? 0.6*h : 0.4*h);
278 cairo_line_to (cr, w/2, (which_line == 1) ? 0.6*h : 0.4*h);
279 cairo_line_to (cr, w/2, (which_line == 1) ? h-2 : 1);
281 case GL_WDGT_CHAIN_RIGHT:
282 cairo_move_to (cr, 1, (which_line == 1) ? 0.6*h : 0.4*h);
283 cairo_line_to (cr, w/2, (which_line == 1) ? 0.6*h : 0.4*h);
284 cairo_line_to (cr, w/2, (which_line == 1) ? h-2 : 1);
286 case GL_WDGT_CHAIN_TOP:
287 cairo_move_to (cr, w/2, h-2);
288 cairo_line_to (cr, w/2, h/2);
289 cairo_line_to (cr, (which_line == 1) ? w-2 : 1, h/2);
291 case GL_WDGT_CHAIN_BOTTOM:
292 cairo_move_to (cr, w/2, 1);
293 cairo_line_to (cr, w/2, h/2);
294 cairo_line_to (cr, (which_line == 1) ? w-2 : 1, h/2);
300 cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
301 cairo_set_line_width (cr, 1.0);
302 cairo_set_source_rgb (cr, 0, 0, 0);
312 * Local Variables: -- emacs
314 * c-basic-offset: 8 -- emacs
315 * tab-width: 8 -- emacs
316 * indent-tabs-mode: nil -- emacs