gtkprogressbar.c 60.7 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
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
Matthias Clasen's avatar
Matthias Clasen committed
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25 26 27
#include "config.h"

#include <string.h>
28

Elliot Lee's avatar
Elliot Lee committed
29
#include "gtkprogressbar.h"
30
#include "gtkorientableprivate.h"
31
#include "gtkwidgetprivate.h"
32
#include "gtkprivate.h"
Alexander Larsson's avatar
Alexander Larsson committed
33
#include "gtkintl.h"
34 35 36 37
#include "gtkcssshadowsvalueprivate.h"
#include "gtkstylecontextprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcssstylepropertyprivate.h"
38 39
#include "gtkcsscustomgadgetprivate.h"
#include "gtkcssnumbervalueprivate.h"
40
#include "gtkprogresstrackerprivate.h"
Elliot Lee's avatar
Elliot Lee committed
41

42 43
#include "a11y/gtkprogressbaraccessible.h"

44 45
#include "fallback-c89.c"

46 47 48 49 50 51
/**
 * SECTION:gtkprogressbar
 * @Short_description: A widget which indicates progress visually
 * @Title: GtkProgressBar
 *
 * The #GtkProgressBar is typically used to display the progress of a long
52 53 54
 * running operation. It provides a visual clue that processing is underway.
 * The GtkProgressBar can be used in two different modes: percentage mode
 * and activity mode.
55 56 57
 *
 * When an application can determine how much work needs to take place
 * (e.g. read a fixed number of bytes from a file) and can monitor its
58 59 60
 * progress, it can use the GtkProgressBar in percentage mode and the
 * user sees a growing bar indicating the percentage of the work that
 * has been completed. In this mode, the application is required to call
61 62 63 64 65 66 67 68 69
 * gtk_progress_bar_set_fraction() periodically to update the progress bar.
 *
 * When an application has no accurate way of knowing the amount of work
 * to do, it can use the #GtkProgressBar in activity mode, which shows
 * activity by a block moving back and forth within the progress area. In
 * this mode, the application is required to call gtk_progress_bar_pulse()
 * periodically to update the progress bar.
 *
 * There is quite a bit of flexibility provided to control the appearance
70 71 72
 * of the #GtkProgressBar. Functions are provided to control the orientation
 * of the bar, optional text can be displayed along with the bar, and the
 * step size used in activity mode can be set.
73 74 75
 *
 * # CSS nodes
 *
76
 * |[<!-- language="plain" -->
77
 * progressbar[.osd]
78
 * ├── [text]
79
 * ╰── trough[.empty][.full]
80
 *     ╰── progress[.pulse]
81 82
 * ]|
 *
83
 * GtkProgressBar has a main CSS node with name progressbar and subnodes with
84 85 86 87 88 89
 * names text and trough, of which the latter has a subnode named progress. The
 * text subnode is only present if text is shown. The progress subnode has the
 * style class .pulse when in activity mode. It gets the style classes .left,
 * .right, .top or .bottom added when the progress 'touches' the corresponding
 * end of the GtkProgressBar. The .osd class on the progressbar node is for use
 * in overlays like the one Epiphany has for page loading progress.
90
 */
Elliot Lee's avatar
Elliot Lee committed
91

92
#define MIN_HORIZONTAL_BAR_WIDTH   150
93 94
#define MIN_HORIZONTAL_BAR_HEIGHT  6
#define MIN_VERTICAL_BAR_WIDTH     7
95
#define MIN_VERTICAL_BAR_HEIGHT    80
Elliot Lee's avatar
Elliot Lee committed
96

97
#define DEFAULT_PULSE_DURATION     250000000
98

99
struct _GtkProgressBarPrivate
100 101 102
{
  gchar         *text;

103 104 105 106
  GtkCssGadget  *gadget;
  GtkCssGadget  *text_gadget;
  GtkCssGadget  *trough_gadget;
  GtkCssGadget  *progress_gadget;
107

108 109 110
  gdouble        fraction;
  gdouble        pulse_fraction;

111
  double         activity_pos;
112 113
  guint          activity_blocks;

114 115
  GtkOrientation orientation;

116 117 118 119 120
  guint              tick_id;
  GtkProgressTracker tracker;
  gint64             pulse1;
  gint64             pulse2;
  gdouble            last_iteration;
121

122 123 124 125
  guint          activity_dir  : 1;
  guint          activity_mode : 1;
  guint          ellipsize     : 3;
  guint          show_text     : 1;
126
  guint          inverted      : 1;
127 128
};

129
enum {
Alexander Larsson's avatar
Alexander Larsson committed
130 131 132
  PROP_0,
  PROP_FRACTION,
  PROP_PULSE_STEP,
133
  PROP_INVERTED,
Alexander Larsson's avatar
Alexander Larsson committed
134
  PROP_TEXT,
Matthias Clasen's avatar
Matthias Clasen committed
135
  PROP_SHOW_TEXT,
136 137 138
  PROP_ELLIPSIZE,
  PROP_ORIENTATION,
  NUM_PROPERTIES = PROP_ORIENTATION
139
};
Elliot Lee's avatar
Elliot Lee committed
140

141 142
static GParamSpec *progress_props[NUM_PROPERTIES] = { NULL, };

143 144 145 146 147 148 149 150
static void gtk_progress_bar_set_property         (GObject        *object,
                                                   guint           prop_id,
                                                   const GValue   *value,
                                                   GParamSpec     *pspec);
static void gtk_progress_bar_get_property         (GObject        *object,
                                                   guint           prop_id,
                                                   GValue         *value,
                                                   GParamSpec     *pspec);
151 152
static void gtk_progress_bar_size_allocate        (GtkWidget      *widget,
                                                   GtkAllocation  *allocation);
153 154 155 156 157 158 159 160 161 162
static void gtk_progress_bar_get_preferred_width  (GtkWidget      *widget,
                                                   gint           *minimum,
                                                   gint           *natural);
static void gtk_progress_bar_get_preferred_height (GtkWidget      *widget,
                                                   gint           *minimum,
                                                   gint           *natural);

static gboolean gtk_progress_bar_draw             (GtkWidget      *widget,
                                                   cairo_t        *cr);
static void     gtk_progress_bar_act_mode_enter   (GtkProgressBar *progress);
163
static void     gtk_progress_bar_act_mode_leave   (GtkProgressBar *progress);
164 165 166
static void     gtk_progress_bar_finalize         (GObject        *object);
static void     gtk_progress_bar_set_orientation  (GtkProgressBar *progress,
                                                   GtkOrientation  orientation);
167 168
static void     gtk_progress_bar_direction_changed (GtkWidget        *widget,
                                                    GtkTextDirection  previous_dir);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static void     gtk_progress_bar_state_flags_changed (GtkWidget      *widget,
                                                      GtkStateFlags   previous_state);

static void     gtk_progress_bar_measure           (GtkCssGadget        *gadget,
                                                    GtkOrientation       orientation,
                                                    gint                 for_size,
                                                    gint                *minimum,
                                                    gint                *natural,
                                                    gint                *minimum_baseline,
                                                    gint                *natural_baseline,
                                                    gpointer             data);
static void     gtk_progress_bar_allocate          (GtkCssGadget        *gadget,
                                                    const GtkAllocation *allocation,
                                                    gint                 baseline,
                                                    GtkAllocation       *out_clip,
                                                    gpointer             data);
static gboolean gtk_progress_bar_render            (GtkCssGadget        *gadget,
                                                    cairo_t             *cr,
                                                    gint                 x,
                                                    gint                 y,
                                                    gint                 width,
                                                    gint                 height,
                                                    gpointer             data);
static void     gtk_progress_bar_allocate_trough   (GtkCssGadget        *gadget,
                                                    const GtkAllocation *allocation,
                                                    gint                 baseline,
                                                    GtkAllocation       *out_clip,
                                                    gpointer             data);
static void     gtk_progress_bar_measure_trough    (GtkCssGadget        *gadget,
                                                    GtkOrientation       orientation,
                                                    gint                 for_size,
                                                    gint                *minimum,
                                                    gint                *natural,
                                                    gint                *minimum_baseline,
                                                    gint                *natural_baseline,
                                                    gpointer             data);
static gboolean gtk_progress_bar_render_trough     (GtkCssGadget        *gadget,
                                                    cairo_t             *cr,
                                                    gint                 x,
                                                    gint                 y,
                                                    gint                 width,
                                                    gint                 height,
                                                    gpointer             data);
static void     gtk_progress_bar_measure_progress  (GtkCssGadget        *gadget,
                                                    GtkOrientation       orientation,
                                                    gint                 for_size,
                                                    gint                *minimum,
                                                    gint                *natural,
                                                    gint                *minimum_baseline,
                                                    gint                *natural_baseline,
                                                    gpointer             data);
static void     gtk_progress_bar_measure_text      (GtkCssGadget        *gadget,
                                                    GtkOrientation       orientation,
                                                    gint                 for_size,
                                                    gint                *minimum,
                                                    gint                *natural,
                                                    gint                *minimum_baseline,
                                                    gint                *natural_baseline,
                                                    gpointer             data);
static gboolean gtk_progress_bar_render_text       (GtkCssGadget        *gadget,
                                                    cairo_t             *cr,
                                                    gint                 x,
                                                    gint                 y,
                                                    gint                 width,
                                                    gint                 height,
                                                    gpointer             data);
Elliot Lee's avatar
Elliot Lee committed
235

236
G_DEFINE_TYPE_WITH_CODE (GtkProgressBar, gtk_progress_bar, GTK_TYPE_WIDGET,
237
                         G_ADD_PRIVATE (GtkProgressBar)
238
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
Elliot Lee's avatar
Elliot Lee committed
239 240 241 242

static void
gtk_progress_bar_class_init (GtkProgressBarClass *class)
{
Alexander Larsson's avatar
Alexander Larsson committed
243
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
244
  GtkWidgetClass *widget_class;
Matthias Clasen's avatar
Matthias Clasen committed
245

Alexander Larsson's avatar
Alexander Larsson committed
246
  gobject_class = G_OBJECT_CLASS (class);
247
  widget_class = (GtkWidgetClass *) class;
Elliot Lee's avatar
Elliot Lee committed
248

Alexander Larsson's avatar
Alexander Larsson committed
249 250
  gobject_class->set_property = gtk_progress_bar_set_property;
  gobject_class->get_property = gtk_progress_bar_get_property;
Matthias Clasen's avatar
Matthias Clasen committed
251 252
  gobject_class->finalize = gtk_progress_bar_finalize;

253
  widget_class->draw = gtk_progress_bar_draw;
254
  widget_class->size_allocate = gtk_progress_bar_size_allocate;
255 256
  widget_class->get_preferred_width = gtk_progress_bar_get_preferred_width;
  widget_class->get_preferred_height = gtk_progress_bar_get_preferred_height;
257
  widget_class->direction_changed = gtk_progress_bar_direction_changed;
258
  widget_class->state_flags_changed = gtk_progress_bar_state_flags_changed;
Elliot Lee's avatar
Elliot Lee committed
259

260
  g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
Matthias Clasen's avatar
Matthias Clasen committed
261

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  progress_props[PROP_INVERTED] =
      g_param_spec_boolean ("inverted",
                            P_("Inverted"),
                            P_("Invert the direction in which the progress bar grows"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  progress_props[PROP_FRACTION] =
      g_param_spec_double ("fraction",
                           P_("Fraction"),
                           P_("The fraction of total work that has been completed"),
                           0.0, 1.0,
                           0.0,
                           GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  progress_props[PROP_PULSE_STEP] =
      g_param_spec_double ("pulse-step",
                           P_("Pulse Step"),
                           P_("The fraction of total progress to move the bouncing block when pulsed"),
                           0.0, 1.0,
                           0.1,
                           GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  progress_props[PROP_TEXT] =
      g_param_spec_string ("text",
                           P_("Text"),
                           P_("Text to be displayed in the progress bar"),
                           NULL,
                           GTK_PARAM_READWRITE);
Matthias Clasen's avatar
Matthias Clasen committed
291

292
  /**
293
   * GtkProgressBar:show-text:
294
   *
295 296
   * Sets whether the progress bar will show a text in addition
   * to the bar itself. The shown text is either the value of
297 298
   * the #GtkProgressBar:text property or, if that is %NULL,
   * the #GtkProgressBar:fraction value, as a percentage.
299
   *
300 301 302 303
   * To make a progress bar that is styled and sized suitably for
   * showing text (even if the actual text is blank), set
   * #GtkProgressBar:show-text to %TRUE and #GtkProgressBar:text
   * to the empty string (not %NULL).
304
   *
305 306
   * Since: 3.0
   */
307 308 309 310 311 312
  progress_props[PROP_SHOW_TEXT] =
      g_param_spec_boolean ("show-text",
                            P_("Show text"),
                            P_("Whether the progress is shown as text."),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
Alexander Larsson's avatar
Alexander Larsson committed
313

314 315 316
  /**
   * GtkProgressBar:ellipsize:
   *
317
   * The preferred place to ellipsize the string, if the progress bar does
Matthias Clasen's avatar
Matthias Clasen committed
318
   * not have enough room to display the entire string, specified as a
319
   * #PangoEllipsizeMode.
320
   *
Matthias Clasen's avatar
Matthias Clasen committed
321
   * Note that setting this property to a value other than
322 323 324
   * %PANGO_ELLIPSIZE_NONE has the side-effect that the progress bar requests
   * only enough space to display the ellipsis ("..."). Another means to set a
   * progress bar's width is gtk_widget_set_size_request().
325 326 327
   *
   * Since: 2.6
   */
328 329 330 331 332 333 334 335 336 337
  progress_props[PROP_ELLIPSIZE] =
      g_param_spec_enum ("ellipsize",
                         P_("Ellipsize"),
                         P_("The preferred place to ellipsize the string, if the progress bar "
                            "does not have enough room to display the entire string, if at all."),
                         PANGO_TYPE_ELLIPSIZE_MODE,
                         PANGO_ELLIPSIZE_NONE,
                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (gobject_class, NUM_PROPERTIES, progress_props);
338

339 340 341 342 343 344 345 346
  /**
   * GtkProgressBar:xspacing:
   *
   * Extra spacing applied to the width of a progress bar.
   *
   * Deprecated: 3.20: Use the standard CSS padding and margins; the
   *     value of this style property is ignored.
   */
347 348
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("xspacing",
349
                                                             P_("X spacing"),
350
                                                             P_("Extra spacing applied to the width of a progress bar."),
351
                                                             0, G_MAXINT, 2,
352
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
353

354 355 356 357 358 359 360 361
  /**
   * GtkProgressBar:yspacing:
   *
   * Extra spacing applied to the height of a progress bar.
   *
   * Deprecated: 3.20: Use the standard CSS padding and margins; the
   *     value of this style property is ignored.
   */
362 363
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("yspacing",
364
                                                             P_("Y spacing"),
365
                                                             P_("Extra spacing applied to the height of a progress bar."),
366
                                                             0, G_MAXINT, 2,
367
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
368 369 370 371 372 373 374

  /**
   * GtkProgressBar:min-horizontal-bar-width:
   *
   * The minimum horizontal width of the progress bar.
   *
   * Since: 2.14
375 376
   *
   * Deprecated: 3.20: Use the standard CSS property min-width.
377 378 379
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("min-horizontal-bar-width",
380
                                                             P_("Minimum horizontal bar width"),
381 382
                                                             P_("The minimum horizontal width of the progress bar"),
                                                             1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
383
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
384 385 386 387 388 389
  /**
   * GtkProgressBar:min-horizontal-bar-height:
   *
   * Minimum horizontal height of the progress bar.
   *
   * Since: 2.14
390 391
   *
   * Deprecated: 3.20: Use the standard CSS property min-height.
392 393 394
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("min-horizontal-bar-height",
395
                                                             P_("Minimum horizontal bar height"),
396 397
                                                             P_("Minimum horizontal height of the progress bar"),
                                                             1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
398
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
399 400 401 402 403 404
  /**
   * GtkProgressBar:min-vertical-bar-width:
   *
   * The minimum vertical width of the progress bar.
   *
   * Since: 2.14
405
   *
406
   * Deprecated: 3.20: Use the standard CSS property min-width.
407 408 409
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("min-vertical-bar-width",
410
                                                             P_("Minimum vertical bar width"),
411 412
                                                             P_("The minimum vertical width of the progress bar"),
                                                             1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
413
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
414 415 416 417 418 419
  /**
   * GtkProgressBar:min-vertical-bar-height:
   *
   * The minimum vertical height of the progress bar.
   *
   * Since: 2.14
420 421
   *
   * Deprecated: 3.20: Use the standard CSS property min-height.
422 423 424
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("min-vertical-bar-height",
425
                                                             P_("Minimum vertical bar height"),
426 427
                                                             P_("The minimum vertical height of the progress bar"),
                                                             1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
428
                                                             G_PARAM_READWRITE|G_PARAM_DEPRECATED));
429

430
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_PROGRESS_BAR_ACCESSIBLE);
431 432 433
  gtk_widget_class_set_css_name (widget_class, "progressbar");
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447
static void
update_fraction_classes (GtkProgressBar *pbar)
{
  GtkProgressBarPrivate *priv = pbar->priv;
  gboolean empty = FALSE;
  gboolean full = FALSE;

  /* Here we set classes based on fill-level unless we're in activity-mode.
   */

  if (!priv->activity_mode)
    {
      if (priv->fraction <= 0.0)
        empty = TRUE;
448
      else if (priv->fraction >= 1.0)
449 450 451 452 453 454 455 456 457 458 459 460 461 462
        full = TRUE;
    }

  if (empty)
    gtk_css_gadget_add_class (priv->trough_gadget, "empty");
  else
    gtk_css_gadget_remove_class (priv->trough_gadget, "empty");

  if (full)
    gtk_css_gadget_add_class (priv->trough_gadget, "full");
  else
    gtk_css_gadget_remove_class (priv->trough_gadget, "full");
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
static void
update_node_classes (GtkProgressBar *pbar)
{
  GtkProgressBarPrivate *priv = pbar->priv;
  gboolean left = FALSE;
  gboolean right = FALSE;
  gboolean top = FALSE;
  gboolean bottom = FALSE;

  /* Here we set positional classes, depending on which end of the
   * progressbar the progress touches.
   */

  if (priv->activity_mode)
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          left = priv->activity_pos <= 0.0;
          right = priv->activity_pos >= 1.0;
        }
      else
        {
          top = priv->activity_pos <= 0.0;
          bottom = priv->activity_pos >= 1.0;
        }
    }
  else /* continuous */
    {
      gboolean inverted;

      inverted = priv->inverted;
      if (gtk_widget_get_direction (GTK_WIDGET (pbar)) == GTK_TEXT_DIR_RTL)
        {
          if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
            inverted = !inverted;
        }

      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          left = !inverted || priv->fraction >= 1.0;
          right = inverted || priv->fraction >= 1.0;
        }
      else
        {
          top = !inverted || priv->fraction >= 1.0;
          bottom = inverted || priv->fraction >= 1.0;
        }
    }

  if (left)
513
    gtk_css_gadget_add_class (priv->progress_gadget, GTK_STYLE_CLASS_LEFT);
514
  else
515
    gtk_css_gadget_remove_class (priv->progress_gadget, GTK_STYLE_CLASS_LEFT);
516 517

  if (right)
518
    gtk_css_gadget_add_class (priv->progress_gadget, GTK_STYLE_CLASS_RIGHT);
519
  else
520
    gtk_css_gadget_remove_class (priv->progress_gadget, GTK_STYLE_CLASS_RIGHT);
521 522

  if (top)
523
    gtk_css_gadget_add_class (priv->progress_gadget, GTK_STYLE_CLASS_TOP);
524
  else
525
    gtk_css_gadget_remove_class (priv->progress_gadget, GTK_STYLE_CLASS_TOP);
526 527

  if (bottom)
528
    gtk_css_gadget_add_class (priv->progress_gadget, GTK_STYLE_CLASS_BOTTOM);
529
  else
530
    gtk_css_gadget_remove_class (priv->progress_gadget, GTK_STYLE_CLASS_BOTTOM);
531 532

  update_fraction_classes (pbar);
533 534 535 536 537 538 539 540 541 542
}

static void
update_node_state (GtkProgressBar *pbar)
{
  GtkProgressBarPrivate *priv = pbar->priv;
  GtkStateFlags state;

  state = gtk_widget_get_state_flags (GTK_WIDGET (pbar));

543 544 545
  gtk_css_gadget_set_state (priv->gadget, state);
  gtk_css_gadget_set_state (priv->trough_gadget, state);
  gtk_css_gadget_set_state (priv->progress_gadget, state);
546
  if (priv->text_gadget)
547
    gtk_css_gadget_set_state (priv->text_gadget, state);
Elliot Lee's avatar
Elliot Lee committed
548 549 550 551 552
}

static void
gtk_progress_bar_init (GtkProgressBar *pbar)
{
553
  GtkProgressBarPrivate *priv;
554
  GtkCssNode *widget_node;
555

556
  pbar->priv = gtk_progress_bar_get_instance_private (pbar);
557 558
  priv = pbar->priv;

559 560
  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
  priv->inverted = FALSE;
561 562 563 564 565 566 567
  priv->pulse_fraction = 0.1;
  priv->activity_pos = 0;
  priv->activity_dir = 1;
  priv->activity_blocks = 5;
  priv->ellipsize = PANGO_ELLIPSIZE_NONE;
  priv->show_text = FALSE;

568 569
  priv->text = NULL;
  priv->fraction = 0.0;
Elliot Lee's avatar
Elliot Lee committed
570

571
  gtk_widget_set_has_window (GTK_WIDGET (pbar), FALSE);
572

573
  _gtk_orientable_set_style_classes (GTK_ORIENTABLE (pbar));
574 575

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (pbar));
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                     GTK_WIDGET (pbar),
                                                     gtk_progress_bar_measure,
                                                     gtk_progress_bar_allocate,
                                                     gtk_progress_bar_render,
                                                     NULL,
                                                     NULL);

  priv->trough_gadget = gtk_css_custom_gadget_new ("trough",
                                                   GTK_WIDGET (pbar),
                                                   priv->gadget,
                                                   NULL,
                                                   gtk_progress_bar_measure_trough,
                                                   gtk_progress_bar_allocate_trough,
                                                   gtk_progress_bar_render_trough,
                                                   NULL,
                                                   NULL);

  priv->progress_gadget = gtk_css_custom_gadget_new ("progress",
                                                     GTK_WIDGET (pbar),
                                                     priv->trough_gadget,
                                                     NULL,
                                                     gtk_progress_bar_measure_progress,
                                                     NULL,
                                                     NULL,
                                                     NULL,
                                                     NULL);

  update_node_state (pbar);
605
  update_node_classes (pbar);
Matthias Clasen's avatar
Matthias Clasen committed
606 607
}

608
static void
Alexander Larsson's avatar
Alexander Larsson committed
609
gtk_progress_bar_set_property (GObject      *object,
Matthias Clasen's avatar
Matthias Clasen committed
610 611 612
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
613 614 615 616
{
  GtkProgressBar *pbar;

  pbar = GTK_PROGRESS_BAR (object);
Elliot Lee's avatar
Elliot Lee committed
617

Alexander Larsson's avatar
Alexander Larsson committed
618
  switch (prop_id)
619
    {
Alexander Larsson's avatar
Alexander Larsson committed
620 621
    case PROP_ORIENTATION:
      gtk_progress_bar_set_orientation (pbar, g_value_get_enum (value));
622
      break;
623 624 625
    case PROP_INVERTED:
      gtk_progress_bar_set_inverted (pbar, g_value_get_boolean (value));
      break;
Alexander Larsson's avatar
Alexander Larsson committed
626 627
    case PROP_FRACTION:
      gtk_progress_bar_set_fraction (pbar, g_value_get_double (value));
628
      break;
Alexander Larsson's avatar
Alexander Larsson committed
629 630
    case PROP_PULSE_STEP:
      gtk_progress_bar_set_pulse_step (pbar, g_value_get_double (value));
631
      break;
Alexander Larsson's avatar
Alexander Larsson committed
632 633
    case PROP_TEXT:
      gtk_progress_bar_set_text (pbar, g_value_get_string (value));
634
      break;
Matthias Clasen's avatar
Matthias Clasen committed
635 636 637
    case PROP_SHOW_TEXT:
      gtk_progress_bar_set_show_text (pbar, g_value_get_boolean (value));
      break;
638 639 640
    case PROP_ELLIPSIZE:
      gtk_progress_bar_set_ellipsize (pbar, g_value_get_enum (value));
      break;
641
    default:
Alexander Larsson's avatar
Alexander Larsson committed
642
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 644 645 646 647
      break;
    }
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
648
gtk_progress_bar_get_property (GObject      *object,
Matthias Clasen's avatar
Matthias Clasen committed
649 650 651
                               guint         prop_id,
                               GValue       *value,
                               GParamSpec   *pspec)
Elliot Lee's avatar
Elliot Lee committed
652
{
653
  GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
654
  GtkProgressBarPrivate* priv = pbar->priv;
655

Alexander Larsson's avatar
Alexander Larsson committed
656
  switch (prop_id)
657
    {
Alexander Larsson's avatar
Alexander Larsson committed
658
    case PROP_ORIENTATION:
659
      g_value_set_enum (value, priv->orientation);
660
      break;
661 662 663
    case PROP_INVERTED:
      g_value_set_boolean (value, priv->inverted);
      break;
Alexander Larsson's avatar
Alexander Larsson committed
664
    case PROP_FRACTION:
665
      g_value_set_double (value, priv->fraction);
666
      break;
Alexander Larsson's avatar
Alexander Larsson committed
667
    case PROP_PULSE_STEP:
668
      g_value_set_double (value, priv->pulse_fraction);
669
      break;
Alexander Larsson's avatar
Alexander Larsson committed
670
    case PROP_TEXT:
671
      g_value_set_string (value, priv->text);
672
      break;
Matthias Clasen's avatar
Matthias Clasen committed
673
    case PROP_SHOW_TEXT:
674
      g_value_set_boolean (value, priv->show_text);
Matthias Clasen's avatar
Matthias Clasen committed
675
      break;
676
    case PROP_ELLIPSIZE:
677
      g_value_set_enum (value, priv->ellipsize);
678
      break;
679
    default:
Alexander Larsson's avatar
Alexander Larsson committed
680
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
681 682 683 684
      break;
    }
}

685 686 687 688 689 690 691
/**
 * gtk_progress_bar_new:
 *
 * Creates a new #GtkProgressBar.
 *
 * Returns: a #GtkProgressBar.
 */
692 693 694 695
GtkWidget*
gtk_progress_bar_new (void)
{
  GtkWidget *pbar;
696

697
  pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
698

699
  return pbar;
700 701
}

Matthias Clasen's avatar
Matthias Clasen committed
702 703 704 705
static void
gtk_progress_bar_finalize (GObject *object)
{
  GtkProgressBar *pbar = GTK_PROGRESS_BAR (object);
706
  GtkProgressBarPrivate *priv = pbar->priv;
Matthias Clasen's avatar
Matthias Clasen committed
707

708 709 710
  if (priv->activity_mode)
    gtk_progress_bar_act_mode_leave (pbar);

Matthias Clasen's avatar
Matthias Clasen committed
711 712
  g_free (priv->text);

713 714 715 716 717
  g_clear_object (&priv->text_gadget);
  g_clear_object (&priv->progress_gadget);
  g_clear_object (&priv->trough_gadget);
  g_clear_object (&priv->gadget);

Matthias Clasen's avatar
Matthias Clasen committed
718
  G_OBJECT_CLASS (gtk_progress_bar_parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
719 720
}

Matthias Clasen's avatar
Matthias Clasen committed
721 722 723
static gchar *
get_current_text (GtkProgressBar *pbar)
{
724
  GtkProgressBarPrivate *priv = pbar->priv;
Matthias Clasen's avatar
Matthias Clasen committed
725

Matthias Clasen's avatar
Matthias Clasen committed
726 727 728
  if (priv->text)
    return g_strdup (priv->text);
  else
729
    return g_strdup_printf (C_("progress bar label", "%.0f %%"), priv->fraction * 100.0);
Matthias Clasen's avatar
Matthias Clasen committed
730 731
}

732
static void
733 734 735 736 737 738 739 740
gtk_progress_bar_measure (GtkCssGadget   *gadget,
                          GtkOrientation  orientation,
                          int             for_size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline,
                          gpointer        data)
741
{
742 743 744 745 746
  GtkWidget *widget;
  GtkProgressBar *pbar;
  GtkProgressBarPrivate *priv;
  gint text_minimum, text_natural;
  gint trough_minimum, trough_natural;
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  widget = gtk_css_gadget_get_owner (gadget);
  pbar = GTK_PROGRESS_BAR (widget);
  priv = pbar->priv;

  if (priv->show_text)
    gtk_css_gadget_get_preferred_size (priv->text_gadget,
                                       orientation,
                                       -1,
                                       &text_minimum, &text_natural,
                                       NULL, NULL);
  else
    text_minimum = text_natural = 0;

  gtk_css_gadget_get_preferred_size (priv->trough_gadget,
                                     orientation,
                                     -1,
                                     &trough_minimum, &trough_natural,
                                     NULL, NULL);

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          *minimum = MAX (text_minimum, trough_minimum);
          *natural = MAX (text_natural, trough_natural);
        }
      else
        {
          *minimum = text_minimum + trough_minimum;
          *natural = text_natural + trough_natural;
        }
    }
  else
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
          *minimum = text_minimum + trough_minimum;
          *natural = text_natural + trough_natural;
        }
      else
        {
          *minimum = MAX (text_minimum, trough_minimum);
          *natural = MAX (text_natural, trough_natural);
        }
    }
793 794
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
static PangoLayout *
gtk_progress_bar_get_layout (GtkProgressBar *pbar)
{
  PangoLayout *layout;
  gchar *buf;
  GtkCssStyle *style;
  PangoAttrList *attrs;
  PangoFontDescription *desc;

  buf = get_current_text (pbar);
  layout = gtk_widget_create_pango_layout (GTK_WIDGET (pbar), buf);

  style = gtk_css_node_get_style (gtk_css_gadget_get_node (pbar->priv->text_gadget));

  attrs = gtk_css_style_get_pango_attributes (style);
  desc = gtk_css_style_get_pango_font (style);

  pango_layout_set_attributes (layout, attrs);
  pango_layout_set_font_description (layout, desc);

  if (attrs)
    pango_attr_list_unref (attrs);
  pango_font_description_free (desc);

  g_free (buf);

  return layout;
}

Elliot Lee's avatar
Elliot Lee committed
824
static void
825 826 827 828 829 830 831 832
gtk_progress_bar_measure_text (GtkCssGadget   *gadget,
                               GtkOrientation  orientation,
                               int             for_size,
                               int            *minimum,
                               int            *natural,
                               int            *minimum_baseline,
                               int            *natural_baseline,
                               gpointer        data)
Elliot Lee's avatar
Elliot Lee committed
833
{
834
  GtkWidget *widget;
Elliot Lee's avatar
Elliot Lee committed
835
  GtkProgressBar *pbar;
836
  GtkProgressBarPrivate *priv;
837
  PangoLayout *layout;
838
  PangoRectangle logical_rect;
839

840
  widget = gtk_css_gadget_get_owner (gadget);
Elliot Lee's avatar
Elliot Lee committed
841
  pbar = GTK_PROGRESS_BAR (widget);
842
  priv = pbar->priv;
Elliot Lee's avatar
Elliot Lee committed
843

844
  layout = gtk_progress_bar_get_layout (pbar);
845

846
  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
Matthias Clasen's avatar
Matthias Clasen committed
847

848 849
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
850
      if (priv->ellipsize)
Matthias Clasen's avatar
Matthias Clasen committed
851 852 853 854 855 856 857
        {
          PangoContext *context;
          PangoFontMetrics *metrics;
          gint char_width;

          /* The minimum size for ellipsized text is ~ 3 chars */
          context = pango_layout_get_context (layout);
Benjamin Otte's avatar
Benjamin Otte committed
858
          metrics = pango_context_get_metrics (context,
859
                                               pango_layout_get_font_description (layout),
Benjamin Otte's avatar
Benjamin Otte committed
860
                                               pango_context_get_language (context));
Matthias Clasen's avatar
Matthias Clasen committed
861 862 863 864

          char_width = pango_font_metrics_get_approximate_char_width (metrics);
          pango_font_metrics_unref (metrics);

865
          *minimum = PANGO_PIXELS (char_width) * 3;
Matthias Clasen's avatar
Matthias Clasen committed
866
        }
867
      else
868
        *minimum = logical_rect.width;
Matthias Clasen's avatar
Matthias Clasen committed
869

870
      *natural = MAX (*minimum, logical_rect.width);
871 872
    }
  else
873
    *minimum = *natural = logical_rect.height;
874

875 876 877 878 879 880 881 882 883 884 885 886 887
  g_object_unref (layout);
}

static gint
get_number (GtkCssStyle *style,
            guint        property)
{
  double d = _gtk_css_number_value_get (gtk_css_style_get_value (style, property), 100.0);

  if (d < 1)
    return ceil (d);
  else
    return floor (d);
888 889 890
}

static void
891 892 893 894 895 896 897 898
gtk_progress_bar_measure_trough (GtkCssGadget   *gadget,
                                 GtkOrientation  orientation,
                                 int             for_size,
                                 int            *minimum,
                                 int            *natural,
                                 int            *minimum_baseline,
                                 int            *natural_baseline,
                                 gpointer        data)
899
{
900
  GtkWidget *widget;
901
  GtkProgressBarPrivate *priv;
902
  GtkCssStyle *style;
903

904
  widget = gtk_css_gadget_get_owner (gadget);
Matthias Clasen's avatar
Matthias Clasen committed
905
  priv = GTK_PROGRESS_BAR (widget)->priv;
906

907 908 909
  style = gtk_css_gadget_get_style (gadget);
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
Matthias Clasen's avatar
Matthias Clasen committed
910
      gdouble min_width;
911

Matthias Clasen's avatar
Matthias Clasen committed
912
      min_width = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_MIN_WIDTH), 100.0);
913

Matthias Clasen's avatar
Matthias Clasen committed
914 915
      if (min_width > 0.0)
        *minimum = 0;
916 917 918 919 920 921 922
      else if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        gtk_widget_style_get (widget, "min-horizontal-bar-width", minimum, NULL);
      else
        gtk_widget_style_get (widget, "min-vertical-bar-width", minimum, NULL);
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
923
      gdouble min_height;
924

Matthias Clasen's avatar
Matthias Clasen committed
925
      min_height = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_MIN_HEIGHT), 100.0);
926

Matthias Clasen's avatar
Matthias Clasen committed
927 928
      if (min_height > 0.0)
        *minimum = 0;
929 930 931 932 933
      else if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        gtk_widget_style_get (widget, "min-horizontal-bar-height", minimum, NULL);
      else
        gtk_widget_style_get (widget, "min-vertical-bar-height", minimum, NULL);
    }
934

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
  *natural = *minimum;

  if (minimum_baseline)
    *minimum_baseline = -1;
  if (natural_baseline)
    *natural_baseline = -1;
}

static void
gtk_progress_bar_measure_progress (GtkCssGadget   *gadget,
                                   GtkOrientation  orientation,
                                   int             for_size,
                                   int            *minimum,
                                   int            *natural,
                                   int            *minimum_baseline,
                                   int            *natural_baseline,
                                   gpointer        data)
{
  GtkWidget *widget;
  GtkProgressBar *pbar;
  GtkProgressBarPrivate *priv;
  GtkCssStyle *style;

  widget = gtk_css_gadget_get_owner (gadget);
959 960 961
  pbar = GTK_PROGRESS_BAR (widget);
  priv = pbar->priv;

962 963 964 965 966 967
  style = gtk_css_gadget_get_style (gadget);
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      gint min_width;

      min_width = get_number (style, GTK_CSS_PROPERTY_MIN_WIDTH);
968

969 970 971 972 973 974 975 976
      if (min_width != 0)
        *minimum = min_width;
      else if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        *minimum = 0;
      else
        gtk_widget_style_get (widget, "min-vertical-bar-width", minimum, NULL);
    }
  else
977
    {
978
      gint min_height;
979

980
      min_height = get_number (style, GTK_CSS_PROPERTY_MIN_HEIGHT);
981

982 983 984 985 986 987 988
      if (min_height != 0)
        *minimum = min_height;
      else if (priv->orientation == GTK_ORIENTATION_VERTICAL)
        *minimum = 0;
      else
        gtk_widget_style_get (widget, "min-horizontal-bar-height", minimum, NULL);
    }
989

990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  *natural = *minimum;

  if (minimum_baseline)
    *minimum_baseline = -1;
  if (natural_baseline)
    *natural_baseline = -1;
}

static void
gtk_progress_bar_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GtkAllocation clip;

  gtk_widget_set_allocation (widget, allocation);

  gtk_css_gadget_allocate (GTK_PROGRESS_BAR (widget)->priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_progress_bar_allocate (GtkCssGadget        *gadget,
                           const GtkAllocation *allocation,
                           int                  baseline,
                           GtkAllocation       *out_clip,
                           gpointer             data)
{
  GtkWidget *widget;
  GtkProgressBarPrivate *priv;
  gint bar_width, bar_height;
  gint text_width, text_height, text_min, text_nat;
  GtkAllocation alloc;
  GtkAllocation text_clip;

  widget = gtk_css_gadget_get_owner (gadget);
  priv = GTK_PROGRESS_BAR (widget)->priv;

  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      gtk_css_gadget_get_preferred_size (priv->trough_gadget,
                                         GTK_ORIENTATION_VERTICAL,
                                         -1,
                                         &bar_height, NULL,
                                         NULL, NULL);
      bar_width = allocation->width;
1039
    }
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  else
    {
      gtk_css_gadget_get_preferred_size (priv->trough_gadget,
                                         GTK_ORIENTATION_HORIZONTAL,
                                         -1,
                                         &bar_width, NULL,
                                         NULL, NULL);
      bar_height = allocation->height;
    }

  alloc.x = allocation->x + allocation->width - bar_width;
  alloc.y = allocation->y + allocation->height - bar_height;
  alloc.width = bar_width;
  alloc.height = bar_height;

  gtk_css_gadget_allocate (priv->trough_gadget, &alloc, -1, out_clip);

  if (!priv->show_text)
    return;

  gtk_css_gadget_get_preferred_size (priv->text_gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     &text_min, &text_nat,
                                     NULL, NULL);
  gtk_css_gadget_get_preferred_size (priv->text_gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     &text_height, NULL,
                                     NULL, NULL);

  text_width = CLAMP (text_nat, text_min, allocation->width);
Matthias Clasen's avatar
Matthias Clasen committed
1072

1073
  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    {
      alloc.x = allocation->x + (allocation->width - text_width) / 2;
      alloc.y = allocation->y;
      alloc.width = text_width;
      alloc.height = text_height;
    }
  else
    {
      alloc.x = allocation->x + allocation->width - text_width;
      alloc.y = allocation->y + (allocation->height - text_height) / 2;
      alloc.width = text_width;
      alloc.height = text_height;
    }

  gtk_css_gadget_allocate (priv->text_gadget, &alloc, -1, &text_clip);

  gdk_rectangle_union (out_clip, &text_clip, out_clip);
}


static void
gtk_progress_bar_allocate_trough (GtkCssGadget        *gadget,
                                  const GtkAllocation *allocation,
                                  int                  baseline,
                                  GtkAllocation       *out_clip,
                                  gpointer             data)
{
  GtkWidget *widget;
  GtkProgressBarPrivate *priv;
  GtkAllocation alloc;
  gint width, height;
  gboolean inverted;

  widget = gtk_css_gadget_get_owner (gadget);
  priv = GTK_PROGRESS_BAR (widget)->priv;

  inverted = priv->inverted;
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        inverted = !inverted;
    }

  gtk_css_gadget_get_preferred_size (priv->progress_gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     &height, NULL,
                                     NULL, NULL);
  gtk_css_gadget_get_preferred_size (priv->progress_gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     &width, NULL,
                                     NULL, NULL);

  if (priv->activity_mode)
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
1132
          alloc.width = width + (allocation->width - width) / priv->activity_blocks;
1133 1134 1135 1136 1137 1138 1139
          alloc.x = allocation->x + priv->activity_pos * (allocation->width - alloc.width);
          alloc.y = allocation->y + (allocation->height - height) / 2;
          alloc.height = height;
        }
      else
        {

1140
          alloc.height = height + (allocation->height - height) / priv->activity_blocks;
1141 1142 1143 1144 1145
          alloc.y = allocation->y + priv->activity_pos * (allocation->height - alloc.height);
          alloc.x = allocation->x + (allocation->width - width) / 2;
          alloc.width = width;
        }
    }
1146
  else
1147 1148 1149
    {
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        {
1150
          alloc.width = width + (allocation->width - width) * priv->fraction;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
          alloc.height = height;
          alloc.y = allocation->y + (allocation->height - height) / 2;

          if (!inverted)
            alloc.x = allocation->x;
          else
            alloc.x = allocation->x + allocation->width - alloc.width;
        }
      else
        {
          alloc.width = width;
1162
          alloc.height = height + (allocation->height - height) * priv->fraction;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
          alloc.x = allocation->x + (allocation->width - width) / 2;

          if (!inverted)
            alloc.y = allocation->y;
          else
            alloc.y = allocation->y + allocation->height - alloc.height;
        }
    }

  gtk_css_gadget_allocate (priv->progress_gadget, &alloc, -1, out_clip);
}

static void
gtk_progress_bar_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum,
                                      gint      *natural)
{
  gtk_css_gadget_get_preferred_size (GTK_PROGRESS_BAR (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
}
1186

1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
static void
gtk_progress_bar_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum,
                                       gint      *natural)
{
  gtk_css_gadget_get_preferred_size (GTK_PROGRESS_BAR (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
1197
}
Elliot Lee's avatar
Elliot Lee committed
1198

1199 1200 1201 1202 1203 1204 1205
static gboolean
tick_cb (GtkWidget     *widget,
         GdkFrameClock *frame_clock,
         gpointer       user_data)
{
  GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
  GtkProgressBarPrivate *priv = pbar->priv;
1206 1207
  gint64 frame_time;
  gdouble iteration, pulse_iterations, current_iterations, fraction;
1208

1209 1210 1211 1212 1213
  if (priv->pulse2 == 0 && priv->pulse1 == 0)
    return G_SOURCE_CONTINUE;

  frame_time = gdk_frame_clock_get_frame_time (frame_clock);
  gtk_progress_tracker_advance_frame (&priv->tracker, frame_time);
1214 1215 1216

  g_assert (priv->pulse2 > priv->pulse1);

1217 1218
  pulse_iterations = (priv->pulse2 - priv->pulse1) / (gdouble) G_USEC_PER_SEC;
  current_iterations = (frame_time - priv->pulse1) / (gdouble) G_USEC_PER_SEC;
1219

1220
  iteration = gtk_progress_tracker_get_iteration (&priv->tracker);
1221 1222 1223 1224
  /* Determine the fraction to move the block from one frame
   * to the next when pulse_fraction is how far the block should
   * move between two calls to gtk_progress_bar_pulse().
   */
1225 1226
  fraction = priv->pulse_fraction * (iteration - priv->last_iteration) / MAX (pulse_iterations, current_iterations);
  priv->last_iteration = iteration;
1227

1228
  if (current_iterations > 3 * pulse_iterations)
1229 1230 1231 1232
    {
      priv->pulse1 = 0;
      return G_SOURCE_CONTINUE;
    }
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253

  /* advance the block */
  if (priv->activity_dir == 0)
    {
      priv->activity_pos += fraction;
      if (priv->activity_pos > 1.0)
        {
          priv->activity_pos = 1.0;
          priv->activity_dir = 1;
        }
    }
  else
    {
      priv->activity_pos -= fraction;
      if (priv->activity_pos <= 0)
        {
          priv->activity_pos = 0;
          priv->activity_dir = 0;
        }
    }

1254 1255
  update_node_classes (pbar);

1256
  gtk_widget_queue_allocate (widget);
1257 1258 1259 1260

  return G_SOURCE_CONTINUE;
}

1261
static void
Matthias Clasen's avatar
Matthias Clasen committed
1262
gtk_progress_bar_act_mode_enter (GtkProgressBar *pbar)
1263
{
1264
  GtkProgressBarPrivate *priv = pbar->priv;
1265
  GtkWidget *widget = GTK_WIDGET (pbar);
1266
  gboolean inverted;
Elliot Lee's avatar
Elliot Lee committed
1267

1268
  gtk_css_gadget_add_class (priv->progress_gadget, GTK_STYLE_CLASS_PULSE);
1269

1270
  inverted = priv->inverted;
Matthias Clasen's avatar
Matthias Clasen committed
1271
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1272
    {
1273 1274
      if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
        inverted = !inverted;
1275
    }
Matthias Clasen's avatar
Matthias Clasen committed
1276

1277
  /* calculate start pos */
1278
  if (!inverted)
1279
    {
1280 1281
      priv->activity_pos = 0.0;
      priv->activity_dir = 0;
1282 1283 1284
    }
  else
    {
1285 1286
      priv->activity_pos = 1.0;
      priv->activity_dir = 1;
1287
    }
1288

1289
  update_node_classes (pbar);
1290 1291 1292
  /* No fixed schedule for pulses, will adapt after calls to update_pulse. Just
   * start the tracker to repeat forever with iterations every second.*/
  gtk_progress_tracker_start (&priv->tracker, G_USEC_PER_SEC, 0, INFINITY);
1293 1294 1295
  priv->tick_id = gtk_widget_add_tick_callback (widget, tick_cb, NULL, NULL);
  priv->pulse2 = 0;
  priv->pulse1 = 0;
1296
  priv->last_iteration = 0;
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
}

static void
gtk_progress_bar_act_mode_leave (GtkProgressBar *pbar)
{
  GtkProgressBarPrivate *priv = pbar->priv;

  if (priv->tick_id)
    gtk_widget_remove_tick_callback (GTK_WIDGET (pbar), priv->tick_id);
  priv->tick_id = 0;
1307

1308
  gtk_css_gadget_remove_class (priv->progress_gadget, GTK_STYLE_CLASS_PULSE);
1309
  update_node_classes (pbar);
Elliot Lee's avatar
Elliot Lee committed
1310 1311
}

1312 1313 1314 1315 1316 1317 1318 1319
static gboolean
gtk_progress_bar_render_text (GtkCssGadget *gadget,
                              cairo_t      *cr,
                              int           x,
                              int           y,
                              int           width,
                              int           height,
                              gpointer      data)
1320
{
1321 1322 1323
  GtkWidget *widget;
  GtkProgressBar *pbar;
  GtkProgressBarPrivate *priv;
1324
  GtkStyleContext *context;
1325
  PangoLayout *layout;
1326 1327 1328 1329

  widget = gtk_css_gadget_get_owner (gadget);
  pbar = GTK_PROGRESS_BAR (widget);
  priv = pbar->priv;
1330

1331
  context = gtk_widget_get_style_context (widget);
1332
  gtk_style_context_save_to_node (context, gtk_css_gadget_get_node (gadget));
1333

1334
  layout = gtk_progress_bar_get_layout (pbar);
1335 1336
  pango_layout_set_ellipsize (layout, priv->ellipsize);
  if (priv->ellipsize)
1337
    pango_layout_set_width (layout, width * PANGO_SCALE);
1338

1339
  gtk_render_layout (context, cr, x, y, layout);
1340

1341
  g_object_unref (layout);
1342

1343
  gtk_style_context_restore (context);
1344

1345 1346
  return FALSE;
}
1347

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
static gboolean
gtk_progress_bar_render_trough (GtkCssGadget *gadget,
                                cairo_t      *cr,
                                int           x,
                                int           y,
                                int           width,
                                int           height,
                                gpointer      data)
{
  GtkWidget *widget;
  GtkProgressBarPrivate *priv;
1359

1360 1361
  widget = gtk_css_gadget_get_owner (gadget);
  priv = GTK_PROGRESS_BAR (widget)->priv;
1362

1363
  gtk_css_gadget_draw (priv->progress_gadget, cr);
1364

1365 1366
  return FALSE;
}
1367

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
static gboolean
gtk_progress_bar_render (GtkCssGadget *gadget,
                         cairo_t      *cr,
                         int           x,
                         int           y,
                         int           width,
                         int           height,
                         gpointer      data)
{
  GtkWidget *widget;
  GtkProgressBarPrivate *priv;
1379

1380 1381
  widget = gtk_css_gadget_get_owner (gadget);
  priv = GTK_PROGRESS_BAR (widget)->priv;
1382

1383 1384 1385
  gtk_css_gadget_draw (priv->trough_gadget, cr);
  if (priv->show_text)
    gtk_css_gadget_draw (priv->text_gadget, cr);
1386

1387
  return FALSE;
1388 1389
}

1390
static gboolean
1391 1392
gtk_progress_bar_draw (GtkWidget *widget,
                       cairo_t   *cr)
1393
{
1394
  GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
1395
  GtkProgressBarPrivate *priv = pbar->priv;
1396

1397
  gtk_css_gadget_draw (priv->gadget, cr);
1398 1399

  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
1400 1401
}

Matthias Clasen's avatar
Matthias Clasen committed
1402 1403 1404 1405
static void
gtk_progress_bar_set_activity_mode (GtkProgressBar *pbar,
                                    gboolean        activity_mode)
{
1406
  GtkProgressBarPrivate *priv = pbar->priv;
1407

Matthias Clasen's avatar
Matthias Clasen committed
1408 1409
  activity_mode = !!activity_mode;

1410
  if (priv->activity_mode != activity_mode)
Matthias Clasen's avatar
Matthias Clasen committed
1411
    {
1412
      priv->activity_mode = activity_mode;
1413

1414
      if (priv->activity_mode)
1415
        gtk_progress_bar_act_mode_enter (pbar);
1416 1417
      else
        gtk_progress_bar_act_mode_leave (pbar);
Matthias Clasen's avatar
Matthias Clasen committed
1418

1419
      gtk_widget_queue_resize (GTK_WIDGET (pbar));
Matthias Clasen's avatar
Matthias Clasen committed
1420 1421
    }
}
1422

1423 1424 1425
/**
 * gtk_progress_bar_set_fraction:
 * @pbar: a #GtkProgressBar
1426
 * @fraction: fraction of the task that’s been completed
Matthias Clasen's avatar
Matthias Clasen committed
1427
 *
1428
 * Causes the progress bar to “fill in” the given fraction
1429 1430
 * of the bar. The fraction should be between 0.0 and 1.0,
 * inclusive.
1431
 */
1432 1433
void
gtk_progress_bar_set_fraction (GtkProgressBar *pbar,
1434
                               gdouble         fraction)
1435
{
1436
  GtkProgressBarPrivate* priv;
1437

1438 1439
  g_return_if_fail (GTK_IS_PROGRESS_BAR (pbar));

1440 1441
  priv = pbar->priv;

1442
  priv->fraction = CLAMP (fraction, 0.0, 1.0);
Matthias Clasen's avatar
Matthias Clasen committed
1443
  gtk_progress_bar_set_activity_mode (pbar, FALSE);
1444
  gtk_widget_queue_allocate (GTK_WIDGET (pbar));
1445
  update_fraction_classes (pbar);
1446

1447
  g_object_notify_by_pspec (G_OBJECT (pbar), progress_props[PROP_FRACTION]);
1448 1449
}

1450 1451 1452 1453
static void
gtk_progress_bar_update_pulse (GtkProgressBar *pbar)
{
  GtkProgressBarPrivate *priv = pbar->priv;
1454 1455 1456 1457
  gint64 pulse_time = g_get_monotonic_time ();

  if (priv->pulse2 == pulse_time)