gtkcheckbutton.c 22.9 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
26

Elliot Lee's avatar
Elliot Lee committed
27
#include "gtkcheckbutton.h"
28

29
#include "gtkbuttonprivate.h"
Elliot Lee's avatar
Elliot Lee committed
30
#include "gtklabel.h"
31

32
#include "gtkintl.h"
33
#include "gtkprivate.h"
34
#include "gtkrender.h"
35
#include "gtkwidgetprivate.h"
36 37 38
#include "gtkcssnodeprivate.h"
#include "gtkcssstylepropertyprivate.h"
#include "gtkradiobutton.h"
Elliot Lee's avatar
Elliot Lee committed
39 40


41 42 43 44 45 46 47 48 49 50 51 52
/**
 * SECTION:gtkcheckbutton
 * @Short_description: Create widgets with a discrete toggle button
 * @Title: GtkCheckButton
 * @See_also: #GtkCheckMenuItem, #GtkButton, #GtkToggleButton, #GtkRadioButton
 *
 * A #GtkCheckButton places a discrete #GtkToggleButton next to a widget,
 * (usually a #GtkLabel). See the section on #GtkToggleButton widgets for
 * more information about toggle/check buttons.
 *
 * The important signal ( #GtkToggleButton::toggled ) is also inherited from
 * #GtkToggleButton.
53 54 55
 *
 * # CSS nodes
 *
56 57 58 59 60
 * |[<!-- language="plain" -->
 * checkbutton
 * ╰── check
 * ]|
 *
61 62 63
 * A GtkCheckButton with indicator (see gtk_toggle_button_set_mode()) has a
 * main CSS node with name checkbutton and a subnode with name check.
 *
64 65 66 67 68
 * |[<!-- language="plain" -->
 * button.check
 * ╰── [check]
 * ]|
 *
69 70 71
 * A GtkCheckButton without indicator changes the name of its main node
 * to button and adds a .check style class to it. The subnode is invisible
 * in this case.
72 73 74
 */


75
#define INDICATOR_SIZE     16
Elliot Lee's avatar
Elliot Lee committed
76 77 78
#define INDICATOR_SPACING  2


79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void gtk_check_button_get_preferred_width                         (GtkWidget          *widget,
                                                                          gint               *minimum,
                                                                          gint               *natural);
static void gtk_check_button_get_preferred_width_for_height              (GtkWidget          *widget,
                                                                          gint                height,
                                                                          gint               *minimum,
                                                                          gint               *natural);
static void gtk_check_button_get_preferred_height                        (GtkWidget          *widget,
                                                                          gint               *minimum,
                                                                          gint               *natural);
static void gtk_check_button_get_preferred_height_for_width              (GtkWidget          *widget,
                                                                          gint                width,
                                                                          gint               *minimum,
                                                                          gint               *natural);
93 94 95 96 97 98
static void gtk_check_button_get_preferred_height_and_baseline_for_width (GtkWidget          *widget,
									  gint                width,
									  gint               *minimum,
									  gint               *natural,
									  gint               *minimum_baseline,
									  gint               *natural_baseline);
Elliot Lee's avatar
Elliot Lee committed
99 100
static void gtk_check_button_size_allocate       (GtkWidget           *widget,
						  GtkAllocation       *allocation);
101 102
static gboolean gtk_check_button_draw            (GtkWidget           *widget,
						  cairo_t             *cr);
103
static void gtk_check_button_paint               (GtkWidget           *widget,
104
						  cairo_t             *cr);
Elliot Lee's avatar
Elliot Lee committed
105
static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
106
						  cairo_t             *cr);
Elliot Lee's avatar
Elliot Lee committed
107
static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
108
						  cairo_t             *cr);
Elliot Lee's avatar
Elliot Lee committed
109

110 111 112 113 114
typedef struct {
  GtkCssNode *indicator_node;
} GtkCheckButtonPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
Elliot Lee's avatar
Elliot Lee committed
115

116 117 118 119
static void
gtk_check_button_state_flags_changed (GtkWidget     *widget,
				      GtkStateFlags  previous_state_flags)
{
120 121 122
  GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (GTK_CHECK_BUTTON (widget));

  gtk_css_node_set_state (priv->indicator_node, gtk_widget_get_state_flags (widget));
123 124 125 126

  GTK_WIDGET_CLASS (gtk_check_button_parent_class)->state_flags_changed (widget, previous_state_flags);
}

Elliot Lee's avatar
Elliot Lee committed
127 128 129 130
static void
gtk_check_button_class_init (GtkCheckButtonClass *class)
{
  GtkWidgetClass *widget_class;
131

Elliot Lee's avatar
Elliot Lee committed
132
  widget_class = (GtkWidgetClass*) class;
133 134

  widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
135
  widget_class->get_preferred_width_for_height = gtk_check_button_get_preferred_width_for_height;
136
  widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
137
  widget_class->get_preferred_height_for_width = gtk_check_button_get_preferred_height_for_width;
138
  widget_class->get_preferred_height_and_baseline_for_width = gtk_check_button_get_preferred_height_and_baseline_for_width;
Elliot Lee's avatar
Elliot Lee committed
139
  widget_class->size_allocate = gtk_check_button_size_allocate;
140
  widget_class->draw = gtk_check_button_draw;
141
  widget_class->state_flags_changed = gtk_check_button_state_flags_changed;
142

Elliot Lee's avatar
Elliot Lee committed
143
  class->draw_indicator = gtk_real_check_button_draw_indicator;
144 145

  gtk_widget_class_install_style_property (widget_class,
146
					   g_param_spec_int ("indicator-size",
147 148
							     P_("Indicator Size"),
							     P_("Size of check or radio indicator"),
149 150 151
							     0,
							     G_MAXINT,
							     INDICATOR_SIZE,
152
							     GTK_PARAM_READABLE));
153
  gtk_widget_class_install_style_property (widget_class,
154
					   g_param_spec_int ("indicator-spacing",
155 156
							     P_("Indicator Spacing"),
							     P_("Spacing around check or radio indicator"),
157 158 159
							     0,
							     G_MAXINT,
							     INDICATOR_SPACING,
160
							     GTK_PARAM_READABLE));
161 162 163

  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_CHECK_BOX);
  gtk_widget_class_set_css_name (widget_class, "checkbutton");
Elliot Lee's avatar
Elliot Lee committed
164 165
}

166 167 168 169 170 171
static void
draw_indicator_changed (GObject    *object,
                        GParamSpec *pspec,
                        gpointer    user_data)
{
  GtkButton *button = GTK_BUTTON (object);
172 173 174 175
  GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (GTK_CHECK_BUTTON (button));
  GtkCssNode *widget_node;

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
176

177
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
178
  if (gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (button)))
179 180 181
    {
      gtk_button_set_alignment (button, 0.0, 0.5);
      gtk_css_node_set_visible (priv->indicator_node, TRUE);
182 183 184 185 186 187 188 189 190 191
      if (GTK_IS_RADIO_BUTTON (button))
        {
          gtk_css_node_remove_class (widget_node, g_quark_from_static_string ("radio"));
          gtk_css_node_set_name (widget_node, I_("radiobutton"));
        }
      else if (GTK_IS_CHECK_BUTTON (button))
        {
          gtk_css_node_remove_class (widget_node, g_quark_from_static_string ("check"));
          gtk_css_node_set_name (widget_node, I_("checkbutton"));
        }
192
    }
193
  else
194 195 196
    {
      gtk_button_set_alignment (button, 0.5, 0.5);
      gtk_css_node_set_visible (priv->indicator_node, FALSE);
197 198 199 200 201 202 203 204 205 206
      if (GTK_IS_RADIO_BUTTON (button))
        {
          gtk_css_node_add_class (widget_node, g_quark_from_static_string ("radio"));
          gtk_css_node_set_name (widget_node, I_("button"));
        }
      else if (GTK_IS_CHECK_BUTTON (button))
        {
          gtk_css_node_add_class (widget_node, g_quark_from_static_string ("check"));
          gtk_css_node_set_name (widget_node, I_("button"));
        }
207
    }
208
G_GNUC_END_IGNORE_DEPRECATIONS
209 210
}

211
static void
212 213 214
node_style_changed_cb (GtkCssNode        *node,
                       GtkCssStyleChange *change,
                       GtkWidget         *widget)
215
{
216
  if (gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_SIZE | GTK_CSS_AFFECTS_CLIP))
217 218 219 220 221
    gtk_widget_queue_resize (widget);
  else
    gtk_widget_queue_draw (widget);
}

Elliot Lee's avatar
Elliot Lee committed
222 223 224
static void
gtk_check_button_init (GtkCheckButton *check_button)
{
225 226 227
  GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button);
  GtkCssNode *widget_node;

228
  gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
229
  g_signal_connect (check_button, "notify::draw-indicator", G_CALLBACK (draw_indicator_changed), NULL);
230
  gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
231 232 233 234 235 236 237 238 239 240

  gtk_style_context_remove_class (gtk_widget_get_style_context (GTK_WIDGET (check_button)), "toggle");

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (check_button));
  priv->indicator_node = gtk_css_node_new ();
  gtk_css_node_set_name (priv->indicator_node, I_("check"));
  gtk_css_node_set_parent (priv->indicator_node, widget_node);
  gtk_css_node_set_state (priv->indicator_node, gtk_css_node_get_state (widget_node));
  g_signal_connect_object (priv->indicator_node, "style-changed", G_CALLBACK (node_style_changed_cb), check_button, 0);
  g_object_unref (priv->indicator_node);
Elliot Lee's avatar
Elliot Lee committed
241 242
}

243 244 245 246 247 248 249
/**
 * gtk_check_button_new:
 *
 * Creates a new #GtkCheckButton.
 *
 * Returns: a #GtkWidget.
 */
Elliot Lee's avatar
Elliot Lee committed
250
GtkWidget*
251
gtk_check_button_new (void)
Elliot Lee's avatar
Elliot Lee committed
252
{
Manish Singh's avatar
Manish Singh committed
253
  return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
Elliot Lee's avatar
Elliot Lee committed
254 255 256
}


257 258 259 260 261 262 263 264
/**
 * gtk_check_button_new_with_label:
 * @label: the text for the check button.
 *
 * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
 *
 * Returns: a #GtkWidget.
 */
Elliot Lee's avatar
Elliot Lee committed
265 266 267
GtkWidget*
gtk_check_button_new_with_label (const gchar *label)
{
268
  return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
Elliot Lee's avatar
Elliot Lee committed
269 270
}

271 272 273
/**
 * gtk_check_button_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
274
 *   mnemonic character
275
 *
276 277 278
 * Creates a new #GtkCheckButton containing a label. The label
 * will be created using gtk_label_new_with_mnemonic(), so underscores
 * in @label indicate the mnemonic for the check button.
Matthias Clasen's avatar
Matthias Clasen committed
279 280 281
 *
 * Returns: a new #GtkCheckButton
 */
282 283 284
GtkWidget*
gtk_check_button_new_with_mnemonic (const gchar *label)
{
285 286 287 288
  return g_object_new (GTK_TYPE_CHECK_BUTTON, 
                       "label", label, 
                       "use-underline", TRUE, 
                       NULL);
289 290 291
}


292 293 294 295 296
/* This should only be called when toggle_button->draw_indicator
 * is true.
 */
static void
gtk_check_button_paint (GtkWidget    *widget,
297
			cairo_t      *cr)
298
{
299
  GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
300

301
  gtk_check_button_draw_indicator (check_button, cr);
302

303
  if (gtk_widget_has_visible_focus (widget))
304
    {
305
      GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
306
      GtkStyleContext *context;
307
      GtkAllocation allocation;
308 309 310
      gint border_width;

      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
311
      gtk_widget_get_allocation (widget, &allocation);
312
      context = gtk_widget_get_style_context (widget);
313

314
      if (child && gtk_widget_get_visible (child))
315 316 317 318
        {
          GtkAllocation child_allocation;

          gtk_widget_get_allocation (child, &child_allocation);
319
          gtk_render_focus (context, cr,
320 321 322 323
                            child_allocation.x - allocation.x,
                            child_allocation.y - allocation.y,
                            child_allocation.width,
                            child_allocation.height);
324 325
        }
      else
326 327 328 329
        gtk_render_focus (context, cr,
                          border_width, border_width,
                          allocation.width - 2 * border_width,
                          allocation.height - 2 * border_width);
330 331 332
    }
}

333 334 335 336 337 338 339 340
void
_gtk_check_button_get_props (GtkCheckButton *check_button,
			     gint           *indicator_size,
			     gint           *indicator_spacing)
{
  GtkWidget *widget =  GTK_WIDGET (check_button);

  if (indicator_size)
341
    gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
342 343

  if (indicator_spacing)
344
    gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
345 346
}

347 348 349 350 351 352 353 354 355
static void
get_padding_and_border (GtkWidget *widget,
                        GtkBorder *border)
{
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder tmp;

  context = gtk_widget_get_style_context (widget);
356
  state = gtk_style_context_get_state (context);
357 358 359 360 361 362 363 364 365

  gtk_style_context_get_padding (context, state, border);
  gtk_style_context_get_border (context, state, &tmp);
  border->top += tmp.top;
  border->right += tmp.right;
  border->bottom += tmp.bottom;
  border->left += tmp.left;
}

366 367 368 369 370
static void
gtk_check_button_get_full_border (GtkCheckButton *check_button,
                                  GtkBorder      *border,
                                  gint           *indicator)
{
371
  int indicator_size, indicator_spacing, indicator_extra, border_width;
372 373
  GtkWidget *child;

374
  get_padding_and_border (GTK_WIDGET (check_button), border);
375 376 377 378 379 380 381
  border_width = gtk_container_get_border_width (GTK_CONTAINER (check_button));
  gtk_widget_style_get (GTK_WIDGET (check_button),
                        "indicator-size", &indicator_size,
                        "indicator-spacing", &indicator_spacing,
                        NULL);
  child = gtk_bin_get_child (GTK_BIN (check_button));

382 383 384 385
  border->left += border_width;
  border->right += border_width;
  border->top += border_width;
  border->bottom += border_width;
386 387 388 389 390 391 392 393 394 395 396 397 398

  indicator_extra = indicator_size + 2 * indicator_spacing;
  if (child && gtk_widget_get_visible (child))
    indicator_extra += indicator_spacing;
  if (gtk_widget_get_direction (GTK_WIDGET (check_button)) == GTK_TEXT_DIR_RTL)
    border->right += indicator_extra;
  else
    border->left += indicator_extra;

  if (indicator)
    *indicator = indicator_size + 2 * indicator_spacing;
}

Elliot Lee's avatar
Elliot Lee committed
399
static void
400 401 402 403
gtk_check_button_get_preferred_width_for_height (GtkWidget *widget,
                                                 gint       height,
                                                 gint      *minimum,
                                                 gint      *natural)
Elliot Lee's avatar
Elliot Lee committed
404
{
405
  GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
406
  
407 408 409
  if (gtk_toggle_button_get_mode (toggle_button))
    {
      GtkWidget *child;
410
      GtkBorder border;
411

412
      gtk_check_button_get_full_border (GTK_CHECK_BUTTON (widget), &border, NULL);
413 414 415 416 417


      child = gtk_bin_get_child (GTK_BIN (widget));
      if (child && gtk_widget_get_visible (child))
        {
418 419 420 421 422 423 424 425
          if (height > -1)
            height -= border.top + border.bottom;

          _gtk_widget_get_preferred_size_for_size (child,
                                                   GTK_ORIENTATION_HORIZONTAL,
                                                   height,
                                                   minimum, natural,
                                                   NULL, NULL);
426 427 428 429 430
        }
      else
        {
          *minimum = 0;
          *natural = 0;
431 432
        }

433 434
      *minimum += border.left + border.right;
      *natural += border.left + border.right;
435 436 437 438 439
    }
  else
    GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
}

440 441 442 443 444 445 446 447
static void
gtk_check_button_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum,
                                      gint      *natural)
{
  gtk_check_button_get_preferred_width_for_height (widget, -1, minimum, natural);
}

448
static void
449 450 451 452 453 454
gtk_check_button_get_preferred_height_and_baseline_for_width (GtkWidget          *widget,
							      gint                width,
							      gint               *minimum,
							      gint               *natural,
							      gint               *minimum_baseline,
							      gint               *natural_baseline)
455 456 457
{
  GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);

458
  if (gtk_toggle_button_get_mode (toggle_button))
Elliot Lee's avatar
Elliot Lee committed
459
    {
460
      GtkWidget *child;
461 462
      GtkBorder border;
      gint indicator;
463

464
      gtk_check_button_get_full_border (GTK_CHECK_BUTTON (widget), &border, &indicator);
465

Javier Jardón's avatar
Javier Jardón committed
466
      child = gtk_bin_get_child (GTK_BIN (widget));
467
      if (child && gtk_widget_get_visible (child))
468 469
        {
          gint child_min, child_nat;
470
	  gint child_min_baseline = -1, child_nat_baseline = -1;
471

472 473 474 475
          if (width > -1)
            width -= border.left + border.right;

          gtk_widget_get_preferred_height_and_baseline_for_width (child, width,
476 477 478
								  &child_min, &child_nat,
								  &child_min_baseline, &child_nat_baseline);

479 480 481
          *minimum = MAX (indicator, child_min);
          *natural = MAX (indicator, child_nat);

482
	  if (minimum_baseline && child_min_baseline >= 0)
483
	    *minimum_baseline = child_min_baseline + border.top + (*minimum - child_min) / 2;
484
	  if (natural_baseline && child_nat_baseline >= 0)
485 486 487 488 489 490
	    *natural_baseline = child_nat_baseline + border.top + (*natural - child_nat) / 2;
        }
      else
        {
          *minimum = indicator;
          *natural = indicator;
491
        }
Havoc Pennington's avatar
Havoc Pennington committed
492

493 494
      *minimum += border.top + border.bottom;
      *natural += border.top + border.bottom;
Elliot Lee's avatar
Elliot Lee committed
495
    }
496
  else
497 498 499 500 501
    GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height_and_baseline_for_width (widget, width,
												   minimum, natural,
												   minimum_baseline, natural_baseline);
}

502 503 504 505 506 507 508 509 510 511 512
static void
gtk_check_button_get_preferred_height_for_width (GtkWidget *widget,
                                                 gint       width,
                                                 gint      *minimum,
                                                 gint      *natural)
{
  gtk_check_button_get_preferred_height_and_baseline_for_width (widget, width,
								minimum, natural,
								NULL, NULL);
}

513 514 515 516 517 518 519 520
static void
gtk_check_button_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum,
                                       gint      *natural)
{
  gtk_check_button_get_preferred_height_and_baseline_for_width (widget, -1,
								minimum, natural,
								NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
521 522 523 524 525 526
}

static void
gtk_check_button_size_allocate (GtkWidget     *widget,
				GtkAllocation *allocation)
{
527 528
  PangoContext *pango_context;
  PangoFontMetrics *metrics;
Elliot Lee's avatar
Elliot Lee committed
529
  GtkCheckButton *check_button;
530
  GtkToggleButton *toggle_button;
Elliot Lee's avatar
Elliot Lee committed
531 532
  GtkButton *button;
  GtkAllocation child_allocation;
533
  gint baseline;
534

535
  button = GTK_BUTTON (widget);
Elliot Lee's avatar
Elliot Lee committed
536
  check_button = GTK_CHECK_BUTTON (widget);
537
  toggle_button = GTK_TOGGLE_BUTTON (widget);
538

539
  if (gtk_toggle_button_get_mode (toggle_button))
Elliot Lee's avatar
Elliot Lee committed
540
    {
Javier Jardón's avatar
Javier Jardón committed
541
      GtkWidget *child;
542 543 544

      gtk_widget_set_allocation (widget, allocation);

545
      if (gtk_widget_get_realized (widget))
546
	gdk_window_move_resize (gtk_button_get_event_window (button),
Elliot Lee's avatar
Elliot Lee committed
547 548
				allocation->x, allocation->y,
				allocation->width, allocation->height);
Javier Jardón's avatar
Javier Jardón committed
549 550 551

      child = gtk_bin_get_child (GTK_BIN (button));
      if (child && gtk_widget_get_visible (child))
Elliot Lee's avatar
Elliot Lee committed
552
	{
553 554 555 556 557 558 559 560 561
          GtkBorder border;
          
          gtk_check_button_get_full_border (check_button, &border, NULL);

          child_allocation.x = allocation->x + border.left;
          child_allocation.y = allocation->y + border.top;
	  child_allocation.width = allocation->width - border.left - border.right;
	  child_allocation.height = allocation->height - border.top - border.bottom;

562 563
	  baseline = gtk_widget_get_allocated_baseline (widget);
	  if (baseline != -1)
564
	    baseline -= border.top;
565
	  gtk_widget_size_allocate_with_baseline (child, &child_allocation, baseline);
Elliot Lee's avatar
Elliot Lee committed
566
	}
567 568 569 570 571 572 573 574 575 576

      pango_context = gtk_widget_get_pango_context (widget);
      metrics = pango_context_get_metrics (pango_context,
					   pango_context_get_font_description (pango_context),
					   pango_context_get_language (pango_context));
      button->priv->baseline_align =
	(double)pango_font_metrics_get_ascent (metrics) /
	(pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics));
      pango_font_metrics_unref (metrics);

Elliot Lee's avatar
Elliot Lee committed
577 578
    }
  else
579
    GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
Elliot Lee's avatar
Elliot Lee committed
580 581 582
}

static gint
583 584
gtk_check_button_draw (GtkWidget *widget,
                       cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
585
{
586 587
  GtkToggleButton *toggle_button;
  GtkBin *bin;
Javier Jardón's avatar
Javier Jardón committed
588
  GtkWidget *child;
589 590 591
  
  toggle_button = GTK_TOGGLE_BUTTON (widget);
  bin = GTK_BIN (widget);
592 593

  if (gtk_toggle_button_get_mode (toggle_button))
Elliot Lee's avatar
Elliot Lee committed
594
    {
595
      gtk_check_button_paint (widget, cr);
Javier Jardón's avatar
Javier Jardón committed
596

597 598 599 600 601
      child = gtk_bin_get_child (bin);
      if (child)
        gtk_container_propagate_draw (GTK_CONTAINER (widget),
                                      child,
                                      cr);
Elliot Lee's avatar
Elliot Lee committed
602
    }
603 604 605
  else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
    GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);

Elliot Lee's avatar
Elliot Lee committed
606 607 608 609 610 611
  return FALSE;
}


static void
gtk_check_button_draw_indicator (GtkCheckButton *check_button,
612
				 cairo_t        *cr)
Elliot Lee's avatar
Elliot Lee committed
613
{
614
  GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
615

Elliot Lee's avatar
Elliot Lee committed
616
  if (class->draw_indicator)
617
    class->draw_indicator (check_button, cr);
Elliot Lee's avatar
Elliot Lee committed
618 619 620 621
}

static void
gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
622
				      cairo_t        *cr)
Elliot Lee's avatar
Elliot Lee committed
623
{
624
  GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button);
Elliot Lee's avatar
Elliot Lee committed
625
  GtkWidget *widget;
626
  GtkButton *button;
Elliot Lee's avatar
Elliot Lee committed
627
  gint x, y;
628 629
  gint indicator_size;
  gint indicator_spacing;
630
  gint baseline;
631
  guint border_width;
632
  GtkAllocation allocation;
633
  GtkStyleContext *context;
634 635

  widget = GTK_WIDGET (check_button);
636
  button = GTK_BUTTON (check_button);
637

638
  gtk_widget_get_allocation (widget, &allocation);
639
  baseline = gtk_widget_get_allocated_baseline (widget);
640
  context = gtk_widget_get_style_context (widget);
641

642
  _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
643

644
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
645

646
  x = indicator_spacing + border_width;
647 648 649 650 651
  if (baseline == -1)
    y = (allocation.height - indicator_size) / 2;
  else
    y = CLAMP (baseline - indicator_size * button->priv->baseline_align,
	       0, allocation.height - indicator_size);
652

653
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
654
    x = allocation.width - (indicator_size + x);
655

656
  gtk_style_context_save_to_node (context, priv->indicator_node);
657

658 659 660 661
  gtk_render_background (context, cr,
                         border_width, border_width,
                         allocation.width - (2 * border_width),
                         allocation.height - (2 * border_width));
662 663 664

  gtk_render_check (context, cr,
		    x, y, indicator_size, indicator_size);
665

666
  gtk_style_context_restore (context);
Elliot Lee's avatar
Elliot Lee committed
667
}
668 669 670 671 672 673 674 675

GtkCssNode *
gtk_check_button_get_indicator_node (GtkCheckButton *check_button)
{
  GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button);

  return priv->indicator_node;
}