[14481] | 1 | /* GTK - The GIMP Toolkit |
---|
| 2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
---|
| 3 | * |
---|
| 4 | * This library is free software; you can redistribute it and/or |
---|
| 5 | * modify it under the terms of the GNU Library General Public |
---|
| 6 | * License as published by the Free Software Foundation; either |
---|
| 7 | * version 2 of the License, or (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This library is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 12 | * Library General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU Library General Public |
---|
| 15 | * License along with this library; if not, write to the |
---|
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
| 17 | * Boston, MA 02111-1307, USA. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /* |
---|
| 21 | * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS |
---|
| 22 | * file for a list of people on the GTK+ Team. See the ChangeLog |
---|
| 23 | * files for a list of changes. These files are distributed with |
---|
| 24 | * GTK+ at ftp://ftp.gtk.org/pub/gtk/. |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include "gtklabel.h" |
---|
| 28 | #include "gtkmain.h" |
---|
| 29 | #include "gtksignal.h" |
---|
| 30 | #include "gtktogglebutton.h" |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | #define DEFAULT_LEFT_POS 4 |
---|
| 34 | #define DEFAULT_TOP_POS 4 |
---|
| 35 | #define DEFAULT_SPACING 7 |
---|
| 36 | |
---|
| 37 | enum { |
---|
| 38 | TOGGLED, |
---|
| 39 | LAST_SIGNAL |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | enum { |
---|
| 43 | ARG_0, |
---|
| 44 | ARG_ACTIVE, |
---|
| 45 | ARG_DRAW_INDICATOR |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | static void gtk_toggle_button_class_init (GtkToggleButtonClass *klass); |
---|
| 50 | static void gtk_toggle_button_init (GtkToggleButton *toggle_button); |
---|
| 51 | static void gtk_toggle_button_paint (GtkWidget *widget, |
---|
| 52 | GdkRectangle *area); |
---|
| 53 | static void gtk_toggle_button_size_allocate (GtkWidget *widget, |
---|
| 54 | GtkAllocation *allocation); |
---|
| 55 | static void gtk_toggle_button_draw (GtkWidget *widget, |
---|
| 56 | GdkRectangle *area); |
---|
| 57 | static gint gtk_toggle_button_expose (GtkWidget *widget, |
---|
| 58 | GdkEventExpose *event); |
---|
| 59 | static void gtk_toggle_button_pressed (GtkButton *button); |
---|
| 60 | static void gtk_toggle_button_released (GtkButton *button); |
---|
| 61 | static void gtk_toggle_button_clicked (GtkButton *button); |
---|
| 62 | static void gtk_toggle_button_enter (GtkButton *button); |
---|
| 63 | static void gtk_toggle_button_leave (GtkButton *button); |
---|
| 64 | static void gtk_toggle_button_set_arg (GtkObject *object, |
---|
| 65 | GtkArg *arg, |
---|
| 66 | guint arg_id); |
---|
| 67 | static void gtk_toggle_button_get_arg (GtkObject *object, |
---|
| 68 | GtkArg *arg, |
---|
| 69 | guint arg_id); |
---|
| 70 | static void gtk_toggle_button_leave (GtkButton *button); |
---|
| 71 | static void gtk_toggle_button_realize (GtkWidget *widget); |
---|
| 72 | static void gtk_toggle_button_unrealize (GtkWidget *widget); |
---|
| 73 | static void gtk_toggle_button_map (GtkWidget *widget); |
---|
| 74 | static void gtk_toggle_button_unmap (GtkWidget *widget); |
---|
| 75 | |
---|
| 76 | static guint toggle_button_signals[LAST_SIGNAL] = { 0 }; |
---|
| 77 | static GtkContainerClass *parent_class = NULL; |
---|
| 78 | |
---|
| 79 | GtkType |
---|
| 80 | gtk_toggle_button_get_type (void) |
---|
| 81 | { |
---|
| 82 | static GtkType toggle_button_type = 0; |
---|
| 83 | |
---|
| 84 | if (!toggle_button_type) |
---|
| 85 | { |
---|
| 86 | static const GtkTypeInfo toggle_button_info = |
---|
| 87 | { |
---|
| 88 | "GtkToggleButton", |
---|
| 89 | sizeof (GtkToggleButton), |
---|
| 90 | sizeof (GtkToggleButtonClass), |
---|
| 91 | (GtkClassInitFunc) gtk_toggle_button_class_init, |
---|
| 92 | (GtkObjectInitFunc) gtk_toggle_button_init, |
---|
| 93 | /* reserved_1 */ NULL, |
---|
| 94 | /* reserved_2 */ NULL, |
---|
| 95 | (GtkClassInitFunc) NULL, |
---|
| 96 | }; |
---|
| 97 | |
---|
| 98 | toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return toggle_button_type; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | static void |
---|
| 105 | gtk_toggle_button_class_init (GtkToggleButtonClass *class) |
---|
| 106 | { |
---|
| 107 | GtkObjectClass *object_class; |
---|
| 108 | GtkWidgetClass *widget_class; |
---|
| 109 | GtkContainerClass *container_class; |
---|
| 110 | GtkButtonClass *button_class; |
---|
| 111 | |
---|
| 112 | object_class = (GtkObjectClass*) class; |
---|
| 113 | widget_class = (GtkWidgetClass*) class; |
---|
| 114 | container_class = (GtkContainerClass*) class; |
---|
| 115 | button_class = (GtkButtonClass*) class; |
---|
| 116 | |
---|
| 117 | parent_class = gtk_type_class (GTK_TYPE_BUTTON); |
---|
| 118 | |
---|
| 119 | gtk_object_add_arg_type ("GtkToggleButton::active", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_ACTIVE); |
---|
| 120 | gtk_object_add_arg_type ("GtkToggleButton::draw_indicator", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_DRAW_INDICATOR); |
---|
| 121 | |
---|
| 122 | toggle_button_signals[TOGGLED] = |
---|
| 123 | gtk_signal_new ("toggled", |
---|
| 124 | GTK_RUN_FIRST, |
---|
| 125 | object_class->type, |
---|
| 126 | GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled), |
---|
| 127 | gtk_marshal_NONE__NONE, |
---|
| 128 | GTK_TYPE_NONE, 0); |
---|
| 129 | |
---|
| 130 | gtk_object_class_add_signals (object_class, toggle_button_signals, LAST_SIGNAL); |
---|
| 131 | |
---|
| 132 | object_class->set_arg = gtk_toggle_button_set_arg; |
---|
| 133 | object_class->get_arg = gtk_toggle_button_get_arg; |
---|
| 134 | |
---|
| 135 | widget_class->size_allocate = gtk_toggle_button_size_allocate; |
---|
| 136 | widget_class->draw = gtk_toggle_button_draw; |
---|
| 137 | widget_class->expose_event = gtk_toggle_button_expose; |
---|
| 138 | widget_class->realize = gtk_toggle_button_realize; |
---|
| 139 | widget_class->unrealize = gtk_toggle_button_unrealize; |
---|
| 140 | widget_class->map = gtk_toggle_button_map; |
---|
| 141 | widget_class->unmap = gtk_toggle_button_unmap; |
---|
| 142 | |
---|
| 143 | button_class->pressed = gtk_toggle_button_pressed; |
---|
| 144 | button_class->released = gtk_toggle_button_released; |
---|
| 145 | button_class->clicked = gtk_toggle_button_clicked; |
---|
| 146 | button_class->enter = gtk_toggle_button_enter; |
---|
| 147 | button_class->leave = gtk_toggle_button_leave; |
---|
| 148 | |
---|
| 149 | class->toggled = NULL; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static void |
---|
| 153 | gtk_toggle_button_init (GtkToggleButton *toggle_button) |
---|
| 154 | { |
---|
| 155 | toggle_button->active = FALSE; |
---|
| 156 | toggle_button->draw_indicator = FALSE; |
---|
| 157 | GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | GtkWidget* |
---|
| 162 | gtk_toggle_button_new (void) |
---|
| 163 | { |
---|
| 164 | return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ())); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | GtkWidget* |
---|
| 168 | gtk_toggle_button_new_with_label (const gchar *label) |
---|
| 169 | { |
---|
| 170 | GtkWidget *toggle_button; |
---|
| 171 | GtkWidget *label_widget; |
---|
| 172 | |
---|
| 173 | toggle_button = gtk_toggle_button_new (); |
---|
| 174 | label_widget = gtk_label_new (label); |
---|
| 175 | gtk_misc_set_alignment (GTK_MISC (label_widget), 0.5, 0.5); |
---|
| 176 | |
---|
| 177 | gtk_container_add (GTK_CONTAINER (toggle_button), label_widget); |
---|
| 178 | gtk_widget_show (label_widget); |
---|
| 179 | |
---|
| 180 | return toggle_button; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | static void |
---|
| 184 | gtk_toggle_button_set_arg (GtkObject *object, |
---|
| 185 | GtkArg *arg, |
---|
| 186 | guint arg_id) |
---|
| 187 | { |
---|
| 188 | GtkToggleButton *tb; |
---|
| 189 | |
---|
| 190 | tb = GTK_TOGGLE_BUTTON (object); |
---|
| 191 | |
---|
| 192 | switch (arg_id) |
---|
| 193 | { |
---|
| 194 | case ARG_ACTIVE: |
---|
| 195 | gtk_toggle_button_set_active (tb, GTK_VALUE_BOOL (*arg)); |
---|
| 196 | break; |
---|
| 197 | case ARG_DRAW_INDICATOR: |
---|
| 198 | gtk_toggle_button_set_mode (tb, GTK_VALUE_BOOL (*arg)); |
---|
| 199 | break; |
---|
| 200 | default: |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | static void |
---|
| 206 | gtk_toggle_button_get_arg (GtkObject *object, |
---|
| 207 | GtkArg *arg, |
---|
| 208 | guint arg_id) |
---|
| 209 | { |
---|
| 210 | GtkToggleButton *tb; |
---|
| 211 | |
---|
| 212 | tb = GTK_TOGGLE_BUTTON (object); |
---|
| 213 | |
---|
| 214 | switch (arg_id) |
---|
| 215 | { |
---|
| 216 | case ARG_ACTIVE: |
---|
| 217 | GTK_VALUE_BOOL (*arg) = tb->active; |
---|
| 218 | break; |
---|
| 219 | case ARG_DRAW_INDICATOR: |
---|
| 220 | GTK_VALUE_BOOL (*arg) = tb->draw_indicator; |
---|
| 221 | break; |
---|
| 222 | default: |
---|
| 223 | arg->type = GTK_TYPE_INVALID; |
---|
| 224 | break; |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void |
---|
| 229 | gtk_toggle_button_set_mode (GtkToggleButton *toggle_button, |
---|
| 230 | gboolean draw_indicator) |
---|
| 231 | { |
---|
| 232 | GtkWidget *widget; |
---|
| 233 | |
---|
| 234 | g_return_if_fail (toggle_button != NULL); |
---|
| 235 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button)); |
---|
| 236 | |
---|
| 237 | widget = GTK_WIDGET (toggle_button); |
---|
| 238 | |
---|
| 239 | draw_indicator = draw_indicator ? TRUE : FALSE; |
---|
| 240 | |
---|
| 241 | if (toggle_button->draw_indicator != draw_indicator) |
---|
| 242 | { |
---|
| 243 | if (GTK_WIDGET_REALIZED (toggle_button)) |
---|
| 244 | { |
---|
| 245 | gboolean visible = GTK_WIDGET_VISIBLE (toggle_button); |
---|
| 246 | |
---|
| 247 | if (visible) |
---|
| 248 | gtk_widget_hide (widget); |
---|
| 249 | |
---|
| 250 | gtk_widget_unrealize (widget); |
---|
| 251 | toggle_button->draw_indicator = draw_indicator; |
---|
| 252 | |
---|
| 253 | if (toggle_button->draw_indicator) |
---|
| 254 | GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW); |
---|
| 255 | else |
---|
| 256 | GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW); |
---|
| 257 | |
---|
| 258 | gtk_widget_realize (widget); |
---|
| 259 | |
---|
| 260 | if (visible) |
---|
| 261 | gtk_widget_show (widget); |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | toggle_button->draw_indicator = draw_indicator; |
---|
| 266 | |
---|
| 267 | if (toggle_button->draw_indicator) |
---|
| 268 | GTK_WIDGET_SET_FLAGS (toggle_button, GTK_NO_WINDOW); |
---|
| 269 | else |
---|
| 270 | GTK_WIDGET_UNSET_FLAGS (toggle_button, GTK_NO_WINDOW); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | if (GTK_WIDGET_VISIBLE (toggle_button)) |
---|
| 274 | gtk_widget_queue_resize (GTK_WIDGET (toggle_button)); |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | |
---|
| 279 | void |
---|
| 280 | gtk_toggle_button_set_active (GtkToggleButton *toggle_button, |
---|
| 281 | gboolean is_active) |
---|
| 282 | { |
---|
| 283 | g_return_if_fail (toggle_button != NULL); |
---|
| 284 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button)); |
---|
| 285 | |
---|
| 286 | is_active = is_active != 0; |
---|
| 287 | |
---|
| 288 | if (toggle_button->active != is_active) |
---|
| 289 | gtk_button_clicked (GTK_BUTTON (toggle_button)); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | |
---|
| 293 | gboolean |
---|
| 294 | gtk_toggle_button_get_active (GtkToggleButton *toggle_button) |
---|
| 295 | { |
---|
| 296 | g_return_val_if_fail (toggle_button != NULL, FALSE); |
---|
| 297 | g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE); |
---|
| 298 | |
---|
| 299 | return (toggle_button->active) ? TRUE : FALSE; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | void |
---|
| 304 | gtk_toggle_button_toggled (GtkToggleButton *toggle_button) |
---|
| 305 | { |
---|
| 306 | g_return_if_fail (toggle_button != NULL); |
---|
| 307 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button)); |
---|
| 308 | |
---|
| 309 | gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | static void |
---|
| 314 | gtk_toggle_button_paint (GtkWidget *widget, |
---|
| 315 | GdkRectangle *area) |
---|
| 316 | { |
---|
| 317 | GtkButton *button; |
---|
| 318 | GtkToggleButton *toggle_button; |
---|
| 319 | GtkShadowType shadow_type; |
---|
| 320 | gint width, height; |
---|
| 321 | gint x, y; |
---|
| 322 | |
---|
| 323 | button = GTK_BUTTON (widget); |
---|
| 324 | toggle_button = GTK_TOGGLE_BUTTON (widget); |
---|
| 325 | |
---|
| 326 | if (GTK_WIDGET_DRAWABLE (widget)) |
---|
| 327 | { |
---|
| 328 | x = 0; |
---|
| 329 | y = 0; |
---|
| 330 | width = widget->allocation.width - GTK_CONTAINER (widget)->border_width * 2; |
---|
| 331 | height = widget->allocation.height - GTK_CONTAINER (widget)->border_width * 2; |
---|
| 332 | |
---|
| 333 | gdk_window_set_back_pixmap (widget->window, NULL, TRUE); |
---|
| 334 | gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height); |
---|
| 335 | |
---|
| 336 | if (GTK_WIDGET_HAS_DEFAULT (widget) && |
---|
| 337 | GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL) |
---|
| 338 | { |
---|
| 339 | gtk_paint_box (widget->style, widget->window, |
---|
| 340 | GTK_STATE_NORMAL, GTK_SHADOW_IN, |
---|
| 341 | area, widget, "togglebuttondefault", |
---|
| 342 | x, y, width, height); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if (GTK_WIDGET_CAN_DEFAULT (widget)) |
---|
| 346 | { |
---|
| 347 | x += widget->style->klass->xthickness; |
---|
| 348 | y += widget->style->klass->ythickness; |
---|
| 349 | width -= 2 * x + DEFAULT_SPACING; |
---|
| 350 | height -= 2 * y + DEFAULT_SPACING; |
---|
| 351 | x += DEFAULT_LEFT_POS; |
---|
| 352 | y += DEFAULT_TOP_POS; |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | if (GTK_WIDGET_HAS_FOCUS (widget)) |
---|
| 356 | { |
---|
| 357 | x += 1; |
---|
| 358 | y += 1; |
---|
| 359 | width -= 2; |
---|
| 360 | height -= 2; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | if ((GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE) || |
---|
| 364 | toggle_button->active) |
---|
| 365 | shadow_type = GTK_SHADOW_IN; |
---|
| 366 | else |
---|
| 367 | shadow_type = GTK_SHADOW_OUT; |
---|
| 368 | |
---|
| 369 | if (button->relief != GTK_RELIEF_NONE || |
---|
| 370 | (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL && |
---|
| 371 | GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE)) |
---|
| 372 | gtk_paint_box (widget->style, widget->window, |
---|
| 373 | GTK_WIDGET_STATE (widget), |
---|
| 374 | shadow_type, area, widget, "togglebutton", |
---|
| 375 | x, y, width, height); |
---|
| 376 | |
---|
| 377 | if (GTK_WIDGET_HAS_FOCUS (widget)) |
---|
| 378 | { |
---|
| 379 | x -= 1; |
---|
| 380 | y -= 1; |
---|
| 381 | width += 2; |
---|
| 382 | height += 2; |
---|
| 383 | |
---|
| 384 | gtk_paint_focus (widget->style, widget->window, |
---|
| 385 | area, widget, "togglebutton", |
---|
| 386 | x, y, width - 1, height - 1); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | static void |
---|
| 392 | gtk_toggle_button_size_allocate (GtkWidget *widget, |
---|
| 393 | GtkAllocation *allocation) |
---|
| 394 | { |
---|
| 395 | if (!GTK_WIDGET_NO_WINDOW (widget) && |
---|
| 396 | GTK_WIDGET_CLASS (parent_class)->size_allocate) |
---|
| 397 | GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | static gint |
---|
| 401 | gtk_toggle_button_expose (GtkWidget *widget, |
---|
| 402 | GdkEventExpose *event) |
---|
| 403 | { |
---|
[15780] | 404 | GtkBin *bin; |
---|
| 405 | GdkEventExpose child_event; |
---|
| 406 | |
---|
| 407 | if (GTK_WIDGET_DRAWABLE (widget)) |
---|
| 408 | { |
---|
| 409 | bin = GTK_BIN (widget); |
---|
| 410 | |
---|
| 411 | gtk_toggle_button_paint (widget, &event->area); |
---|
| 412 | |
---|
| 413 | child_event = *event; |
---|
| 414 | if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) && |
---|
| 415 | gtk_widget_intersect (bin->child, &event->area, &child_event.area)) |
---|
| 416 | gtk_widget_event (bin->child, (GdkEvent*) &child_event); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | return TRUE; |
---|
[14481] | 420 | } |
---|
| 421 | |
---|
| 422 | static void |
---|
| 423 | gtk_toggle_button_draw (GtkWidget *widget, |
---|
| 424 | GdkRectangle *area) |
---|
| 425 | { |
---|
| 426 | GdkRectangle child_area; |
---|
| 427 | GdkRectangle tmp_area; |
---|
| 428 | GtkBin *bin; |
---|
| 429 | |
---|
| 430 | g_return_if_fail (widget != NULL); |
---|
| 431 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
| 432 | g_return_if_fail (area != NULL); |
---|
| 433 | |
---|
| 434 | bin = GTK_BIN (widget); |
---|
| 435 | |
---|
| 436 | if (GTK_WIDGET_DRAWABLE (widget) && !GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 437 | { |
---|
| 438 | tmp_area = *area; |
---|
| 439 | tmp_area.x -= GTK_CONTAINER (widget)->border_width; |
---|
| 440 | tmp_area.y -= GTK_CONTAINER (widget)->border_width; |
---|
| 441 | |
---|
| 442 | gtk_toggle_button_paint (widget, &tmp_area); |
---|
| 443 | |
---|
| 444 | if (bin->child && gtk_widget_intersect (bin->child, &tmp_area, &child_area)) |
---|
| 445 | gtk_widget_draw (bin->child, &child_area); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | static void |
---|
| 450 | gtk_toggle_button_pressed (GtkButton *button) |
---|
| 451 | { |
---|
| 452 | GtkToggleButton *toggle_button; |
---|
| 453 | GtkStateType new_state; |
---|
| 454 | |
---|
| 455 | g_return_if_fail (button != NULL); |
---|
| 456 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button)); |
---|
| 457 | |
---|
| 458 | toggle_button = GTK_TOGGLE_BUTTON (button); |
---|
| 459 | |
---|
| 460 | button->button_down = TRUE; |
---|
| 461 | |
---|
| 462 | if (toggle_button->active) |
---|
| 463 | new_state = (button->in_button ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE); |
---|
| 464 | else |
---|
| 465 | new_state = (button->in_button ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL); |
---|
| 466 | |
---|
| 467 | if (GTK_WIDGET_STATE (button) != new_state) |
---|
| 468 | gtk_widget_set_state (GTK_WIDGET (button), new_state); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | static void |
---|
| 472 | gtk_toggle_button_released (GtkButton *button) |
---|
| 473 | { |
---|
| 474 | GtkToggleButton *toggle_button; |
---|
| 475 | GtkStateType new_state; |
---|
| 476 | |
---|
| 477 | g_return_if_fail (button != NULL); |
---|
| 478 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button)); |
---|
| 479 | |
---|
| 480 | if (button->button_down) |
---|
| 481 | { |
---|
| 482 | toggle_button = GTK_TOGGLE_BUTTON (button); |
---|
| 483 | |
---|
| 484 | button->button_down = FALSE; |
---|
| 485 | |
---|
| 486 | if (button->in_button) |
---|
| 487 | { |
---|
| 488 | gtk_button_clicked (button); |
---|
| 489 | } |
---|
| 490 | else |
---|
| 491 | { |
---|
| 492 | if (toggle_button->active) |
---|
| 493 | new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE); |
---|
| 494 | else |
---|
| 495 | new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL); |
---|
| 496 | |
---|
| 497 | if (GTK_WIDGET_STATE (button) != new_state) |
---|
| 498 | gtk_widget_set_state (GTK_WIDGET (button), new_state); |
---|
| 499 | } |
---|
| 500 | } |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | static void |
---|
| 504 | gtk_toggle_button_clicked (GtkButton *button) |
---|
| 505 | { |
---|
| 506 | GtkToggleButton *toggle_button; |
---|
| 507 | GtkStateType new_state; |
---|
| 508 | |
---|
| 509 | g_return_if_fail (button != NULL); |
---|
| 510 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button)); |
---|
| 511 | |
---|
| 512 | toggle_button = GTK_TOGGLE_BUTTON (button); |
---|
| 513 | toggle_button->active = !toggle_button->active; |
---|
| 514 | |
---|
| 515 | gtk_toggle_button_toggled (toggle_button); |
---|
| 516 | |
---|
| 517 | if (toggle_button->active) |
---|
| 518 | new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_ACTIVE); |
---|
| 519 | else |
---|
| 520 | new_state = (button->in_button ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL); |
---|
| 521 | |
---|
| 522 | if (GTK_WIDGET_STATE (button) != new_state) |
---|
| 523 | gtk_widget_set_state (GTK_WIDGET (button), new_state); |
---|
| 524 | else |
---|
| 525 | gtk_widget_queue_draw (GTK_WIDGET (button)); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | static void |
---|
| 529 | gtk_toggle_button_enter (GtkButton *button) |
---|
| 530 | { |
---|
| 531 | GtkToggleButton *toggle_button; |
---|
| 532 | GtkStateType new_state; |
---|
| 533 | |
---|
| 534 | g_return_if_fail (button != NULL); |
---|
| 535 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button)); |
---|
| 536 | |
---|
| 537 | toggle_button = GTK_TOGGLE_BUTTON (button); |
---|
| 538 | |
---|
| 539 | if (toggle_button->active) |
---|
| 540 | new_state = (button->button_down ? GTK_STATE_NORMAL : GTK_STATE_PRELIGHT); |
---|
| 541 | else |
---|
| 542 | new_state = (button->button_down ? GTK_STATE_ACTIVE : GTK_STATE_PRELIGHT); |
---|
| 543 | |
---|
| 544 | if (GTK_WIDGET_STATE (button) != new_state) |
---|
| 545 | gtk_widget_set_state (GTK_WIDGET (button), new_state); |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | static void |
---|
| 549 | gtk_toggle_button_leave (GtkButton *button) |
---|
| 550 | { |
---|
| 551 | GtkToggleButton *toggle_button; |
---|
| 552 | GtkStateType new_state; |
---|
| 553 | |
---|
| 554 | g_return_if_fail (button != NULL); |
---|
| 555 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (button)); |
---|
| 556 | |
---|
| 557 | toggle_button = GTK_TOGGLE_BUTTON (button); |
---|
| 558 | |
---|
| 559 | new_state = (toggle_button->active ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL); |
---|
| 560 | |
---|
| 561 | if (GTK_WIDGET_STATE (button) != new_state) |
---|
| 562 | gtk_widget_set_state (GTK_WIDGET (button), new_state); |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | static void |
---|
| 566 | gtk_toggle_button_realize (GtkWidget *widget) |
---|
| 567 | { |
---|
| 568 | GtkToggleButton *toggle_button; |
---|
| 569 | GdkWindowAttr attributes; |
---|
| 570 | gint attributes_mask; |
---|
| 571 | gint border_width; |
---|
| 572 | |
---|
| 573 | g_return_if_fail (widget != NULL); |
---|
| 574 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
| 575 | |
---|
| 576 | toggle_button = GTK_TOGGLE_BUTTON (widget); |
---|
| 577 | GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); |
---|
| 578 | |
---|
| 579 | border_width = GTK_CONTAINER (widget)->border_width; |
---|
| 580 | |
---|
| 581 | attributes.window_type = GDK_WINDOW_CHILD; |
---|
| 582 | attributes.x = widget->allocation.x + border_width; |
---|
| 583 | attributes.y = widget->allocation.y + border_width; |
---|
| 584 | attributes.width = widget->allocation.width - border_width * 2; |
---|
| 585 | attributes.height = widget->allocation.height - border_width * 2; |
---|
| 586 | attributes.event_mask = gtk_widget_get_events (widget); |
---|
| 587 | attributes.event_mask |= (GDK_EXPOSURE_MASK | |
---|
| 588 | GDK_BUTTON_PRESS_MASK | |
---|
| 589 | GDK_BUTTON_RELEASE_MASK | |
---|
| 590 | GDK_ENTER_NOTIFY_MASK | |
---|
| 591 | GDK_LEAVE_NOTIFY_MASK); |
---|
| 592 | |
---|
| 593 | if (GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 594 | { |
---|
| 595 | attributes.wclass = GDK_INPUT_ONLY; |
---|
| 596 | attributes_mask = GDK_WA_X | GDK_WA_Y; |
---|
| 597 | |
---|
| 598 | widget->window = gtk_widget_get_parent_window (widget); |
---|
| 599 | gdk_window_ref (widget->window); |
---|
| 600 | |
---|
| 601 | toggle_button->event_window = gdk_window_new (gtk_widget_get_parent_window (widget), |
---|
| 602 | &attributes, attributes_mask); |
---|
| 603 | gdk_window_set_user_data (toggle_button->event_window, toggle_button); |
---|
| 604 | } |
---|
| 605 | else |
---|
| 606 | { |
---|
| 607 | attributes.wclass = GDK_INPUT_OUTPUT; |
---|
| 608 | attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; |
---|
| 609 | attributes.visual = gtk_widget_get_visual (widget); |
---|
| 610 | attributes.colormap = gtk_widget_get_colormap (widget); |
---|
| 611 | widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), |
---|
| 612 | &attributes, attributes_mask); |
---|
| 613 | gdk_window_set_user_data (widget->window, toggle_button); |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | widget->style = gtk_style_attach (widget->style, widget->window); |
---|
| 617 | |
---|
| 618 | if (!GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 619 | gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL); |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | static void |
---|
| 623 | gtk_toggle_button_unrealize (GtkWidget *widget) |
---|
| 624 | { |
---|
| 625 | GtkToggleButton *toggle_button; |
---|
| 626 | |
---|
| 627 | g_return_if_fail (widget != NULL); |
---|
| 628 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
| 629 | |
---|
| 630 | toggle_button = GTK_TOGGLE_BUTTON (widget); |
---|
| 631 | |
---|
| 632 | if (GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 633 | { |
---|
| 634 | gdk_window_set_user_data (toggle_button->event_window, NULL); |
---|
| 635 | gdk_window_destroy (toggle_button->event_window); |
---|
| 636 | toggle_button->event_window = NULL; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | if (GTK_WIDGET_CLASS (parent_class)->unrealize) |
---|
| 640 | (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | static void |
---|
| 644 | gtk_toggle_button_map (GtkWidget *widget) |
---|
| 645 | { |
---|
| 646 | g_return_if_fail (widget != NULL); |
---|
| 647 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
| 648 | |
---|
| 649 | if (GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 650 | gdk_window_show (GTK_TOGGLE_BUTTON (widget)->event_window); |
---|
| 651 | |
---|
| 652 | GTK_WIDGET_CLASS (parent_class)->map (widget); |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | static void |
---|
| 656 | gtk_toggle_button_unmap (GtkWidget *widget) |
---|
| 657 | { |
---|
| 658 | g_return_if_fail (widget != NULL); |
---|
| 659 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
| 660 | |
---|
| 661 | if (GTK_WIDGET_NO_WINDOW (widget)) |
---|
| 662 | gdk_window_hide (GTK_TOGGLE_BUTTON (widget)->event_window); |
---|
| 663 | |
---|
| 664 | GTK_WIDGET_CLASS (parent_class)->unmap (widget); |
---|
| 665 | } |
---|