1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * Modified version of gimpchainbutton.c for gLabels:
6 * LIBGIMP - The GIMP Library
7 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
10 * Copyright (C) 1999-2000 Sven Neumann <sven@gimp.org>
12 * Modified or gLabels by Jim Evins <evins@snaught.com>
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this library; if not, write to the
26 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
30 #include "wdgt-chain-button.h"
32 #include <gtk/gtkdrawingarea.h>
33 #include <gtk/gtkimage.h>
34 #include <gtk/gtkbutton.h>
46 static void gl_wdgt_chain_button_clicked_callback (GtkWidget *widget,
47 glWdgtChainButton *button);
48 static gboolean gl_wdgt_chain_button_draw_lines (GtkWidget *widget,
49 GdkEventExpose *eevent,
50 glWdgtChainButton *button);
53 static const gchar *gl_wdgt_chain_stock_items[] =
56 GL_STOCK_HCHAIN_BROKEN,
58 GL_STOCK_VCHAIN_BROKEN
62 static guint gl_wdgt_chain_button_signals[LAST_SIGNAL] = { 0 };
66 G_DEFINE_TYPE (glWdgtChainButton, gl_wdgt_chain_button, GTK_TYPE_TABLE);
70 gl_wdgt_chain_button_class_init (glWdgtChainButtonClass *class)
72 gl_wdgt_chain_button_parent_class = g_type_class_peek_parent (class);
74 gl_wdgt_chain_button_signals[TOGGLED] =
75 g_signal_new ("toggled",
76 G_TYPE_FROM_CLASS (class),
78 G_STRUCT_OFFSET (glWdgtChainButtonClass, toggled),
80 g_cclosure_marshal_VOID__VOID,
83 class->toggled = NULL;
87 gl_wdgt_chain_button_init (glWdgtChainButton *button)
89 button->position = GL_WDGT_CHAIN_TOP;
90 button->active = FALSE;
92 button->line1 = gtk_drawing_area_new ();
93 button->line2 = gtk_drawing_area_new ();
94 button->image = gtk_image_new ();
96 button->button = gtk_button_new ();
98 gtk_button_set_relief (GTK_BUTTON (button->button), GTK_RELIEF_NONE);
99 gtk_container_add (GTK_CONTAINER (button->button), button->image);
100 gtk_widget_show (button->image);
102 g_signal_connect (button->button, "clicked",
103 G_CALLBACK (gl_wdgt_chain_button_clicked_callback),
105 g_signal_connect (button->line1, "expose_event",
106 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
108 g_signal_connect (button->line2, "expose_event",
109 G_CALLBACK (gl_wdgt_chain_button_draw_lines),
115 * gl_wdgt_chain_button_new:
116 * @position: The position you are going to use for the button
117 * with respect to the widgets you want to chain.
119 * Creates a new #glWdgtChainButton widget.
121 * This returns a button showing either a broken or a linked chain and
122 * small clamps attached to both sides that visually group the two widgets
123 * you want to connect. This widget looks best when attached
124 * to a table taking up two columns (or rows respectively) next
125 * to the widgets that it is supposed to connect. It may work
126 * for more than two widgets, but the look is optimized for two.
128 * Returns: Pointer to the new #glWdgtChainButton, which is inactive
129 * by default. Use gl_wdgt_chain_button_set_active() to
133 gl_wdgt_chain_button_new (glWdgtChainPosition position)
135 glWdgtChainButton *button;
137 button = g_object_new (GL_WDGT_TYPE_CHAIN_BUTTON, NULL);
139 button->position = position;
141 gtk_image_set_from_stock
142 (GTK_IMAGE (button->image),
143 gl_wdgt_chain_stock_items[((position & GL_WDGT_CHAIN_LEFT) << 1) + ! button->active],
144 GTK_ICON_SIZE_BUTTON);
146 if (position & GL_WDGT_CHAIN_LEFT) /* are we a vertical chainbutton? */
148 gtk_table_resize (GTK_TABLE (button), 3, 1);
149 gtk_table_attach (GTK_TABLE (button), button->button, 0, 1, 1, 2,
150 GTK_SHRINK, GTK_SHRINK, 0, 0);
151 gtk_table_attach_defaults (GTK_TABLE (button),
152 button->line1, 0, 1, 0, 1);
153 gtk_table_attach_defaults (GTK_TABLE (button),
154 button->line2, 0, 1, 2, 3);
158 gtk_table_resize (GTK_TABLE (button), 1, 3);
159 gtk_table_attach (GTK_TABLE (button), button->button, 1, 2, 0, 1,
160 GTK_SHRINK, GTK_SHRINK, 0, 0);
161 gtk_table_attach_defaults (GTK_TABLE (button),
162 button->line1, 0, 1, 0, 1);
163 gtk_table_attach_defaults (GTK_TABLE (button),
164 button->line2, 2, 3, 0, 1);
167 gtk_widget_show (button->button);
168 gtk_widget_show (button->line1);
169 gtk_widget_show (button->line2);
171 return GTK_WIDGET (button);
175 * gl_wdgt_chain_button_set_active:
176 * @button: Pointer to a #glWdgtChainButton.
177 * @active: The new state.
179 * Sets the state of the #glWdgtChainButton to be either locked (%TRUE) or
180 * unlocked (%FALSE) and changes the showed pixmap to reflect the new state.
183 gl_wdgt_chain_button_set_active (glWdgtChainButton *button,
186 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
188 if (button->active != active)
192 button->active = active ? TRUE : FALSE;
194 num = ((button->position & GL_WDGT_CHAIN_LEFT) << 1) + (active ? 0 : 1);
196 gtk_image_set_from_stock (GTK_IMAGE (button->image),
197 gl_wdgt_chain_stock_items[num],
198 GTK_ICON_SIZE_BUTTON);
203 * gl_wdgt_chain_button_get_active
204 * @button: Pointer to a #glWdgtChainButton.
206 * Checks the state of the #glWdgtChainButton.
208 * Returns: %TRUE if the #glWdgtChainButton is active (locked).
211 gl_wdgt_chain_button_get_active (glWdgtChainButton *button)
213 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
215 return button->active;
219 gl_wdgt_chain_button_clicked_callback (GtkWidget *widget,
220 glWdgtChainButton *button)
222 g_return_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button));
224 gl_wdgt_chain_button_set_active (button, ! button->active);
226 g_signal_emit (button, gl_wdgt_chain_button_signals[TOGGLED], 0);
230 gl_wdgt_chain_button_draw_lines (GtkWidget *widget,
231 GdkEventExpose *eevent,
232 glWdgtChainButton *button)
236 GtkShadowType shadow;
237 glWdgtChainPosition position;
241 /* don't set this too high, there's no check against drawing outside
242 the widgets bounds yet (and probably never will be) */
244 g_return_val_if_fail (GL_WDGT_IS_CHAIN_BUTTON (button), FALSE);
246 points[0].x = widget->allocation.width / 2;
247 points[0].y = widget->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) ? widget->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) ? widget->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) ? widget->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) ? widget->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 (widget->style,