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[] =
59 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
63 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE)
67 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
69 gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
71 gl_wdgt_chain_button_signals[TOGGLED] =
72 g_signal_new ("toggled",
73 G_TYPE_FROM_CLASS (class),
75 G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
77 g_cclosure_marshal_VOID__VOID,
80 class->toggled = NULL;
84 gl_wdgt_chain_button_init (glWdgtChainButton *button)
86 button->position = GL_WDGT_CHAIN_TOP;
87 button->active = FALSE;
89 button->line1 = gtk_drawing_area_new ();
90 button->line2 = gtk_drawing_area_new ();
91 button->image = gtk_image_new ();
93 button->button = gtk_toggle_button_new ();
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);
100 g_signal_connect (button->button, "clicked",
101 G_CALLBACK (gl_wdgt_chain_button_clicked_callback),
103 g_signal_connect (button->line1, "draw",
104 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
106 g_signal_connect (button->line2, "draw",
107 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
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.
118 * Creates a new #glWdgtChainButton widget.
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.
127 * Returns: Pointer to the new #glWdgtChainButton, which is inactive
128 * by default. Use gl_wdgt_chain_button_set_active() to
132 gl_wdgt_chain_button_new (glWdgtChainPosition position)
134 glWdgtChainButton *button;
136 button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
138 button->position = position;
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);
145 if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
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 );
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 );
166 gtk_widget_show (button->button);
167 gtk_widget_show (button->line1);
168 gtk_widget_show (button->line2);
170 return GTK_WIDGET (button);
174 * gl_wdgt_chain_button_set_active:
175 * @button: Pointer to a #glWdgtChainButton.
176 * @active: The new state.
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.
182 gl_wdgt_chain_button_set_active (glWdgtChainButton *button,
185 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
187 if (button->active != active)
191 button->active = active ? TRUE : FALSE;
193 num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
195 gtk_image_set_from_icon_name (GTK_IMAGE (button->image),
196 gl_wdgt_chain_stock_items[num],
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);
208 * gl_wdgt_chain_button_get_active
209 * @button: Pointer to a #glWdgtChainButton.
211 * Checks the state of the #glWdgtChainButton.
213 * Returns: %TRUE if the #glWdgtChainButton is active (locked).
216 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
218 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
220 return button->active;
224 gl_wdgt_chain_button_clicked_callback (GtkWidget *widget,
225 glWdgtChainButton *button)
227 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
229 gl_wdgt_chain_button_set_active (button, ! button->active);
231 g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
235 gl_wdgt_chain_button_draw_lines (GtkWidget *widget,
237 glWdgtChainButton *button)
239 GtkAllocation allocation;
241 glWdgtChainPosition position;
244 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
246 gtk_widget_get_allocation (widget, &allocation);
247 w = allocation.width;
248 h = allocation.height;
250 which_line = (widget == button->line1) ? 1 : -1;
252 position = button->position;
254 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
257 case GL_WDGT_CHAIN_LEFT:
258 position = GL_WDGT_CHAIN_RIGHT;
260 case GL_WDGT_CHAIN_RIGHT:
261 position = GL_WDGT_CHAIN_LEFT;
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);
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);
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);
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);
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);
305 * Local Variables: -- emacs
307 * c-basic-offset: 8 -- emacs
308 * tab-width: 8 -- emacs
309 * indent-tabs-mode: nil -- emacs