1 /* GTK - The GIMP Toolkit
2 * Copyright © 2012 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Cosimo Cecchi <cosimoc@gnome.org>
24 * @Short_description: A bar that can used as a level indicator
26 * The #GtkLevelBar is a bar widget that can be used
27 * as a level indicator. Typical use cases are displaying the strength
28 * of a password, or showing the charge level of a battery.
30 * Use gtk_level_bar_set_value() to set the current value, and
31 * gtk_level_bar_add_offset_value() to set the value offsets at which
32 * the bar will be considered in a different state. GTK will add two offsets
33 * by default on the level bar: #GTK_LEVEL_BAR_OFFSET_LOW and
34 * #GTK_LEVEL_BAR_OFFSET_HIGH, with values 0.25 and 0.75 respectively.
37 * <title>Adding a custom offset on the bar</title>
41 * create_level_bar (void)
43 * GtkWidget *level_bar;
45 * level_bar = gtk_level_bar_new ();
47 * /<!---->* This changes the value of the default low offset *<!---->/
48 * gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (level_bar),
49 * GTK_LEVEL_BAR_OFFSET_LOW, 0.10);
51 * /<!---->* This adds a new offset to the bar; the application will
52 * * be able to change its color by using the following selector,
53 * * either by adding it to its CSS file or using
54 * * gtk_css_provider_load_from_data() and gtk_style_context_add_provider()
56 * * .level-bar.fill-block.level-my-offset {
57 * * background-color: green;
58 * * border-style: solid;
59 * * border-color: black;
60 * * border-style: 1px;
63 * gtk_level_bar_add_offset_value (GTK_LEVEL_BAR (level_bar),
71 * The default interval of values is between zero and one, but it's possible to
72 * modify the interval using gtk_level_bar_set_min_value() and
73 * gtk_level_bar_set_max_value(). The value will be always drawn in proportion to
74 * the admissible interval, i.e. a value of 15 with a specified interval between
75 * 10 and 20 is equivalent to a value of 0.5 with an interval between 0 and 1.
76 * When #GTK_LEVEL_BAR_MODE_DISCRETE is used, the bar level is rendered
77 * as a finite and number of separated blocks instead of a single one. The number
78 * of blocks that will be rendered is equal to the number of units specified by
79 * the admissible interval.
80 * For instance, to build a bar rendered with five blocks, it's sufficient to
81 * set the minimum value to 0 and the maximum value to 5 after changing the indicator
88 #include "gtkbuildable.h"
90 #include "gtkorientableprivate.h"
91 #include "gtklevelbar.h"
92 #include "gtkmarshalers.h"
93 #include "gtkstylecontext.h"
94 #include "gtktypebuiltins.h"
95 #include "gtkwidget.h"
100 #include "fallback-c89.c"
102 #define DEFAULT_BLOCK_SIZE 3
104 /* these don't make sense outside of GtkLevelBar, so we don't add
106 #define STYLE_CLASS_INDICATOR_CONTINUOUS "indicator-continuous"
107 #define STYLE_CLASS_INDICATOR_DISCRETE "indicator-discrete"
108 #define STYLE_CLASS_FILL_BLOCK "fill-block"
109 #define STYLE_CLASS_EMPTY_FILL_BLOCK "empty-fill-block"
111 static void gtk_level_bar_buildable_init (GtkBuildableIface *iface);
113 G_DEFINE_TYPE_WITH_CODE (GtkLevelBar, gtk_level_bar, GTK_TYPE_WIDGET,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL)
115 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
116 gtk_level_bar_buildable_init))
125 PROP_ORIENTATION /* overridden */
129 SIGNAL_OFFSET_CHANGED,
133 static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
134 static guint signals[NUM_SIGNALS] = { 0, };
141 struct _GtkLevelBarPrivate {
142 GtkOrientation orientation;
150 GtkLevelBarMode bar_mode;
155 static void gtk_level_bar_set_value_internal (GtkLevelBar *self,
158 static GtkLevelBarOffset *
159 gtk_level_bar_offset_new (const gchar *name,
162 GtkLevelBarOffset *offset = g_slice_new0 (GtkLevelBarOffset);
164 offset->name = g_strdup (name);
165 offset->value = value;
171 gtk_level_bar_offset_free (GtkLevelBarOffset *offset)
173 g_free (offset->name);
174 g_slice_free (GtkLevelBarOffset, offset);
178 offset_find_func (gconstpointer data,
179 gconstpointer user_data)
181 const GtkLevelBarOffset *offset = data;
182 const gchar *name = user_data;
184 return g_strcmp0 (name, offset->name);
188 offset_sort_func (gconstpointer a,
191 const GtkLevelBarOffset *offset_a = a;
192 const GtkLevelBarOffset *offset_b = b;
194 return (offset_a->value > offset_b->value);
198 gtk_level_bar_ensure_offset (GtkLevelBar *self,
203 GtkLevelBarOffset *offset = NULL;
205 existing = g_list_find_custom (self->priv->offsets, name, offset_find_func);
207 offset = existing->data;
209 if (offset && (offset->value == value))
214 gtk_level_bar_offset_free (offset);
215 self->priv->offsets = g_list_delete_link (self->priv->offsets, existing);
218 offset = gtk_level_bar_offset_new (name, value);
219 self->priv->offsets = g_list_insert_sorted (self->priv->offsets, offset, offset_sort_func);
225 gtk_level_bar_value_in_interval (GtkLevelBar *self,
228 return ((value >= self->priv->min_value) &&
229 (value <= self->priv->max_value));
233 gtk_level_bar_get_min_block_size (GtkLevelBar *self,
237 GtkWidget *widget = GTK_WIDGET (self);
238 GtkStyleContext *context = gtk_widget_get_style_context (widget);
239 GtkStateFlags flags = gtk_widget_get_state_flags (widget);
240 GtkBorder border, tmp, tmp2;
241 gint min_width, min_height;
243 gtk_style_context_save (context);
244 gtk_style_context_add_class (context, STYLE_CLASS_FILL_BLOCK);
245 gtk_style_context_get_border (context, flags, &border);
246 gtk_style_context_get_padding (context, flags, &tmp);
247 gtk_style_context_get_margin (context, flags, &tmp2);
248 gtk_style_context_restore (context);
250 gtk_style_context_get_style (context,
251 "min-block-width", &min_width,
252 "min-block-height", &min_height,
255 border.top += tmp.top;
256 border.right += tmp.right;
257 border.bottom += tmp.bottom;
258 border.left += tmp.left;
260 border.top += tmp2.top;
261 border.right += tmp2.right;
262 border.bottom += tmp2.bottom;
263 border.left += tmp2.left;
266 *block_width = MAX (border.left + border.right, min_width);
268 *block_height = MAX (border.top + border.bottom, min_height);
272 gtk_level_bar_get_num_blocks (GtkLevelBar *self)
274 if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_CONTINUOUS)
276 else if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_DISCRETE)
277 return MAX (1, (gint) (round (self->priv->max_value) - round (self->priv->min_value)));
283 gtk_level_bar_get_borders (GtkLevelBar *self,
284 GtkBorder *borders_out)
286 GtkWidget *widget = GTK_WIDGET (self);
287 GtkStyleContext *context = gtk_widget_get_style_context (widget);
288 GtkStateFlags flags = gtk_widget_get_state_flags (widget);
289 GtkBorder border, tmp;
291 gtk_style_context_save (context);
292 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
293 gtk_style_context_get_border (context, flags, &border);
294 gtk_style_context_get_padding (context, flags, &tmp);
295 gtk_style_context_restore (context);
297 border.top += tmp.top;
298 border.right += tmp.right;
299 border.bottom += tmp.bottom;
300 border.left += tmp.left;
303 *borders_out = border;
307 gtk_level_bar_draw_fill_continuous (GtkLevelBar *self,
310 cairo_rectangle_int_t *fill_area)
312 GtkWidget *widget = GTK_WIDGET (self);
313 GtkStyleContext *context = gtk_widget_get_style_context (widget);
314 GtkStateFlags flags = gtk_widget_get_state_flags (widget);
315 cairo_rectangle_int_t base_area, block_area;
316 GtkBorder block_margin;
317 gdouble fill_percentage;
319 gtk_style_context_save (context);
320 gtk_style_context_add_class (context, STYLE_CLASS_FILL_BLOCK);
321 gtk_style_context_get_margin (context, flags, &block_margin);
323 /* render the empty (unfilled) part */
324 base_area = *fill_area;
325 base_area.x += block_margin.left;
326 base_area.y += block_margin.top;
327 base_area.width -= block_margin.left + block_margin.right;
328 base_area.height -= block_margin.top + block_margin.bottom;
330 gtk_style_context_add_class (context, STYLE_CLASS_EMPTY_FILL_BLOCK);
332 gtk_render_background (context, cr, base_area.x, base_area.y,
333 base_area.width, base_area.height);
334 gtk_render_frame (context, cr, base_area.x, base_area.y,
335 base_area.width, base_area.height);
337 gtk_style_context_remove_class (context, STYLE_CLASS_EMPTY_FILL_BLOCK);
339 /* now render the filled part on top of it */
340 block_area = base_area;
342 fill_percentage = (self->priv->cur_value - self->priv->min_value) /
343 (self->priv->max_value - self->priv->min_value);
345 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
347 block_area.width = (gint) floor (block_area.width * fill_percentage);
350 block_area.x += base_area.width - block_area.width;
354 block_area.height = (gint) floor (block_area.height * fill_percentage);
357 block_area.y += base_area.height - block_area.height;
360 gtk_render_background (context, cr, block_area.x, block_area.y,
361 block_area.width, block_area.height);
362 gtk_render_frame (context, cr, block_area.x, block_area.y,
363 block_area.width, block_area.height);
365 gtk_style_context_restore (context);
369 gtk_level_bar_draw_fill_discrete (GtkLevelBar *self,
372 cairo_rectangle_int_t *fill_area)
374 GtkWidget *widget = GTK_WIDGET (self);
375 GtkStyleContext *context = gtk_widget_get_style_context (widget);
376 GtkStateFlags flags = gtk_widget_get_state_flags (widget);
377 gint num_blocks, num_filled, idx;
378 gint block_width, block_height;
379 gint block_draw_width, block_draw_height;
380 GtkBorder block_margin;
381 cairo_rectangle_int_t block_area;
383 gtk_level_bar_get_min_block_size (self, &block_width, &block_height);
385 block_area = *fill_area;
386 num_blocks = gtk_level_bar_get_num_blocks (self);
387 num_filled = (gint) round (self->priv->cur_value) - (gint) round (self->priv->min_value);
389 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
390 block_width = MAX (block_width, (gint) floor (block_area.width / num_blocks));
392 block_height = MAX (block_height, (gint) floor (block_area.height / num_blocks));
394 gtk_style_context_save (context);
395 gtk_style_context_add_class (context, STYLE_CLASS_FILL_BLOCK);
396 gtk_style_context_get_margin (context, flags, &block_margin);
398 block_draw_width = block_width - block_margin.left - block_margin.right;
399 block_draw_height = block_height - block_margin.top - block_margin.bottom;
401 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
403 block_draw_height = MAX (block_draw_height, block_area.height - block_margin.top - block_margin.bottom);
404 block_area.y += block_margin.top;
407 block_area.x += block_area.width - block_draw_width;
411 block_draw_width = MAX (block_draw_width, block_area.width - block_margin.left - block_margin.right);
412 block_area.x += block_margin.left;
415 block_area.y += block_area.height - block_draw_height;
418 for (idx = 0; idx < num_blocks; idx++)
420 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
423 block_area.x -= block_margin.right;
425 block_area.x += block_margin.left;
430 block_area.y -= block_margin.bottom;
432 block_area.y += block_margin.top;
435 if (idx > num_filled - 1)
436 gtk_style_context_add_class (context, STYLE_CLASS_EMPTY_FILL_BLOCK);
438 gtk_render_background (context, cr,
439 block_area.x, block_area.y,
440 block_draw_width, block_draw_height);
441 gtk_render_frame (context, cr,
442 block_area.x, block_area.y,
443 block_draw_width, block_draw_height);
445 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
448 block_area.x -= block_draw_width + block_margin.left;
450 block_area.x += block_draw_width + block_margin.right;
455 block_area.y -= block_draw_height + block_margin.top;
457 block_area.y += block_draw_height + block_margin.bottom;
461 gtk_style_context_restore (context);
465 gtk_level_bar_draw_fill (GtkLevelBar *self,
468 GtkWidget *widget = GTK_WIDGET (self);
469 GtkBorder trough_borders;
471 cairo_rectangle_int_t fill_area;
473 gtk_level_bar_get_borders (self, &trough_borders);
475 fill_area.x = trough_borders.left;
476 fill_area.y = trough_borders.top;
477 fill_area.width = gtk_widget_get_allocated_width (widget) -
478 trough_borders.left - trough_borders.right;
479 fill_area.height = gtk_widget_get_allocated_height (widget) -
480 trough_borders.top - trough_borders.bottom;
482 inverted = self->priv->inverted;
483 if (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL)
485 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
486 inverted = !inverted;
489 if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_CONTINUOUS)
490 gtk_level_bar_draw_fill_continuous (self, cr, inverted, &fill_area);
491 else if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_DISCRETE)
492 gtk_level_bar_draw_fill_discrete (self, cr, inverted, &fill_area);
496 gtk_level_bar_draw (GtkWidget *widget,
499 GtkLevelBar *self = GTK_LEVEL_BAR (widget);
500 GtkStyleContext *context = gtk_widget_get_style_context (widget);
503 width = gtk_widget_get_allocated_width (widget);
504 height = gtk_widget_get_allocated_height (widget);
506 gtk_style_context_save (context);
507 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
509 gtk_render_background (context, cr, 0, 0, width, height);
510 gtk_render_frame (context, cr, 0, 0, width, height);
512 gtk_style_context_restore (context);
514 gtk_level_bar_draw_fill (self, cr);
520 gtk_level_bar_get_preferred_width (GtkWidget *widget,
524 GtkLevelBar *self = GTK_LEVEL_BAR (widget);
527 gint width, block_width;
529 num_blocks = gtk_level_bar_get_num_blocks (self);
530 gtk_level_bar_get_min_block_size (self, &block_width, NULL);
532 gtk_level_bar_get_borders (self, &borders);
533 width = borders.left + borders.right;
535 if (self->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
536 width += num_blocks * block_width;
538 width += block_width;
547 gtk_level_bar_get_preferred_height (GtkWidget *widget,
551 GtkLevelBar *self = GTK_LEVEL_BAR (widget);
554 gint height, block_height;
556 num_blocks = gtk_level_bar_get_num_blocks (self);
557 gtk_level_bar_get_min_block_size (self, NULL, &block_height);
559 gtk_level_bar_get_borders (self, &borders);
560 height = borders.top + borders.bottom;
562 if (self->priv->orientation == GTK_ORIENTATION_VERTICAL)
563 height += num_blocks * block_height;
565 height += block_height;
574 gtk_level_bar_update_mode_style_classes (GtkLevelBar *self)
576 GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
578 if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_CONTINUOUS)
580 gtk_style_context_remove_class (context, STYLE_CLASS_INDICATOR_DISCRETE);
581 gtk_style_context_add_class (context, STYLE_CLASS_INDICATOR_CONTINUOUS);
583 else if (self->priv->bar_mode == GTK_LEVEL_BAR_MODE_DISCRETE)
585 gtk_style_context_remove_class (context, STYLE_CLASS_INDICATOR_CONTINUOUS);
586 gtk_style_context_add_class (context, STYLE_CLASS_INDICATOR_DISCRETE);
591 gtk_level_bar_update_level_style_classes (GtkLevelBar *self)
593 GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
594 gdouble value = gtk_level_bar_get_value (self);
595 gchar *offset_style_class, *value_class = NULL;
596 GtkLevelBarOffset *offset, *prev_offset;
599 for (l = self->priv->offsets; l != NULL; l = l->next)
603 offset_style_class = g_strconcat ("level-", offset->name, NULL);
604 gtk_style_context_remove_class (context, offset_style_class);
606 /* find the right offset for our style class */
607 if (((l->prev == NULL) && (value <= offset->value)) ||
608 ((l->next == NULL) && (value >= offset->value)))
610 value_class = offset_style_class;
612 else if ((l->next != NULL) && (l->prev != NULL))
614 prev_offset = l->prev->data;
615 if ((prev_offset->value <= value) && (value < offset->value))
616 value_class = offset_style_class;
620 g_free (offset_style_class);
624 if (value_class != NULL)
626 gtk_style_context_add_class (context, value_class);
627 g_free (value_class);
632 gtk_level_bar_ensure_offsets_in_range (GtkLevelBar *self)
634 GtkLevelBarOffset *offset;
635 GList *l = self->priv->offsets;
642 if (offset->value < self->priv->min_value)
643 gtk_level_bar_ensure_offset (self, offset->name, self->priv->min_value);
644 else if (offset->value > self->priv->max_value)
645 gtk_level_bar_ensure_offset (self, offset->name, self->priv->max_value);
655 offset_start_element (GMarkupParseContext *context,
656 const gchar *element_name,
658 const gchar **values,
662 OffsetsParserData *parser_data = user_data;
663 const gchar *name = NULL;
664 const gchar *value_str = NULL;
665 GtkLevelBarOffset *offset;
666 gint line_number, char_number;
669 if (strcmp (element_name, "offsets") == 0)
671 else if (strcmp (element_name, "offset") == 0)
673 for (idx = 0; names[idx] != NULL; idx++)
675 if (strcmp (names[idx], "name") == 0)
679 else if (strcmp (names[idx], "value") == 0)
681 value_str = values[idx];
685 g_markup_parse_context_get_position (context,
690 GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
691 "%s:%d:%d '%s' is not a valid attribute of <%s>",
693 line_number, char_number, names[idx], "offset");
699 if (name && value_str)
701 offset = gtk_level_bar_offset_new (name, g_ascii_strtod (value_str, NULL));
702 parser_data->offsets = g_list_prepend (parser_data->offsets, offset);
707 g_markup_parse_context_get_position (context,
712 GTK_BUILDER_ERROR_UNHANDLED_TAG,
713 "%s:%d:%d unsupported tag for GtkLevelBar: \"%s\"",
715 line_number, char_number, element_name);
719 static const GMarkupParser offset_parser =
725 gtk_level_bar_buildable_custom_tag_start (GtkBuildable *buildable,
728 const gchar *tagname,
729 GMarkupParser *parser,
732 OffsetsParserData *parser_data;
737 if (strcmp (tagname, "offsets") != 0)
740 parser_data = g_slice_new0 (OffsetsParserData);
741 parser_data->self = GTK_LEVEL_BAR (buildable);
742 parser_data->offsets = NULL;
744 *parser = offset_parser;
751 gtk_level_bar_buildable_custom_finished (GtkBuildable *buildable,
754 const gchar *tagname,
757 OffsetsParserData *parser_data;
759 GtkLevelBarOffset *offset;
762 parser_data = user_data;
763 self = parser_data->self;
765 if (strcmp (tagname, "offsets") != 0)
768 for (l = parser_data->offsets; l != NULL; l = l->next)
771 gtk_level_bar_add_offset_value (self, offset->name, offset->value);
775 g_list_free_full (parser_data->offsets, (GDestroyNotify) gtk_level_bar_offset_free);
776 g_slice_free (OffsetsParserData, parser_data);
780 gtk_level_bar_buildable_init (GtkBuildableIface *iface)
782 iface->custom_tag_start = gtk_level_bar_buildable_custom_tag_start;
783 iface->custom_finished = gtk_level_bar_buildable_custom_finished;
787 gtk_level_bar_set_orientation (GtkLevelBar *self,
788 GtkOrientation orientation)
790 if (self->priv->orientation != orientation)
792 self->priv->orientation = orientation;
793 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (self));
795 gtk_widget_queue_resize (GTK_WIDGET (self));
800 gtk_level_bar_get_property (GObject *obj,
805 GtkLevelBar *self = GTK_LEVEL_BAR (obj);
810 g_value_set_double (value, gtk_level_bar_get_value (self));
813 g_value_set_double (value, gtk_level_bar_get_min_value (self));
816 g_value_set_double (value, gtk_level_bar_get_max_value (self));
819 g_value_set_enum (value, gtk_level_bar_get_mode (self));
822 g_value_set_boolean (value, gtk_level_bar_get_inverted (self));
824 case PROP_ORIENTATION:
825 g_value_set_enum (value, self->priv->orientation);
828 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
834 gtk_level_bar_set_property (GObject *obj,
839 GtkLevelBar *self = GTK_LEVEL_BAR (obj);
844 gtk_level_bar_set_value (self, g_value_get_double (value));
847 gtk_level_bar_set_min_value (self, g_value_get_double (value));
850 gtk_level_bar_set_max_value (self, g_value_get_double (value));
853 gtk_level_bar_set_mode (self, g_value_get_enum (value));
856 gtk_level_bar_set_inverted (self, g_value_get_boolean (value));
858 case PROP_ORIENTATION:
859 gtk_level_bar_set_orientation (self, g_value_get_enum (value));
862 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
868 gtk_level_bar_finalize (GObject *obj)
870 GtkLevelBar *self = GTK_LEVEL_BAR (obj);
872 g_list_free_full (self->priv->offsets, (GDestroyNotify) gtk_level_bar_offset_free);
874 G_OBJECT_CLASS (gtk_level_bar_parent_class)->finalize (obj);
878 gtk_level_bar_class_init (GtkLevelBarClass *klass)
880 GObjectClass *oclass = G_OBJECT_CLASS (klass);
881 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (klass);
883 oclass->get_property = gtk_level_bar_get_property;
884 oclass->set_property = gtk_level_bar_set_property;
885 oclass->finalize = gtk_level_bar_finalize;
887 wclass->draw = gtk_level_bar_draw;
888 wclass->get_preferred_width = gtk_level_bar_get_preferred_width;
889 wclass->get_preferred_height = gtk_level_bar_get_preferred_height;
891 g_object_class_override_property (oclass, PROP_ORIENTATION, "orientation");
894 * GtkLevelBar::offset-changed:
895 * @self: a #GtkLevelBar
896 * @name: the name of the offset that changed value
898 * Emitted when an offset specified on the bar changes value as an
899 * effect to gtk_level_bar_add_offset_value() being called.
901 * The signal supports detailed connections; you can connect to the
902 * detailed signal "changed::x" in order to only receive callbacks when
903 * the value of offset "x" changes.
907 signals[SIGNAL_OFFSET_CHANGED] =
908 g_signal_new ("offset-changed",
910 G_SIGNAL_RUN_FIRST | G_SIGNAL_DETAILED,
911 G_STRUCT_OFFSET (GtkLevelBarClass, offset_changed),
913 _gtk_marshal_VOID__STRING,
920 * The #GtkLevelBar:value property determines the currently
921 * filled value of the level bar.
925 properties[PROP_VALUE] =
926 g_param_spec_double ("value",
927 P_("Currently filled value level"),
928 P_("Currently filled value level of the level bar"),
929 0.0, G_MAXDOUBLE, 0.0,
930 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
932 * GtkLevelBar:min-value:
934 * The #GtkLevelBar:min-value property determines the minimum value of
935 * the interval that can be displayed by the bar.
939 properties[PROP_MIN_VALUE] =
940 g_param_spec_double ("min-value",
941 P_("Minimum value level for the bar"),
942 P_("Minimum value level that can be displayed by the bar"),
943 0.0, G_MAXDOUBLE, 0.0,
944 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
946 * GtkLevelBar:max-value:
948 * The #GtkLevelBar:max-value property determaxes the maximum value of
949 * the interval that can be displayed by the bar.
953 properties[PROP_MAX_VALUE] =
954 g_param_spec_double ("max-value",
955 P_("Maximum value level for the bar"),
956 P_("Maximum value level that can be displayed by the bar"),
957 0.0, G_MAXDOUBLE, 1.0,
958 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
962 * The #GtkLevelBar:bar-mode property determines the way #GtkLevelBar
963 * interprets the value properties to draw the level fill area.
964 * Specifically, when the value is #GTK_LEVEL_BAR_MODE_CONTINUOUS,
965 * #GtkLevelBar will draw a single block representing the current value in
966 * that area; when the value is #GTK_LEVEL_BAR_MODE_DISCRETE,
967 * the widget will draw a succession of separate blocks filling the
968 * draw area, with the number of blocks being equal to the units separating
969 * the integral roundings of #GtkLevelBar:min-value and #GtkLevelBar:max-value.
973 properties[PROP_MODE] =
974 g_param_spec_enum ("mode",
975 P_("The mode of the value indicator"),
976 P_("The mode of the value indicator displayed by the bar"),
977 GTK_TYPE_LEVEL_BAR_MODE,
978 GTK_LEVEL_BAR_MODE_CONTINUOUS,
982 * GtkLevelBar:inverted:
984 * Level bars normally grow from top to bottom or left to right.
985 * Inverted level bars grow in the opposite direction.
989 properties[PROP_INVERTED] =
990 g_param_spec_boolean ("inverted",
992 P_("Invert the direction in which the level bar grows"),
997 * GtkLevelBar:min-block-height:
999 * The min-block-height style property determines the minimum
1000 * height for blocks filling the #GtkLevelBar widget.
1004 gtk_widget_class_install_style_property
1005 (wclass, g_param_spec_int ("min-block-height",
1006 P_("Minimum height for filling blocks"),
1007 P_("Minimum height for blocks that fill the bar"),
1008 1, G_MAXINT, DEFAULT_BLOCK_SIZE,
1009 G_PARAM_READWRITE));
1011 * GtkLevelBar:min-block-width:
1013 * The min-block-width style property determines the minimum
1014 * width for blocks filling the #GtkLevelBar widget.
1018 gtk_widget_class_install_style_property
1019 (wclass, g_param_spec_int ("min-block-width",
1020 P_("Minimum width for filling blocks"),
1021 P_("Minimum width for blocks that fill the bar"),
1022 1, G_MAXINT, DEFAULT_BLOCK_SIZE,
1023 G_PARAM_READWRITE));
1025 g_type_class_add_private (klass, sizeof (GtkLevelBarPrivate));
1026 g_object_class_install_properties (oclass, LAST_PROPERTY, properties);
1030 gtk_level_bar_init (GtkLevelBar *self)
1032 GtkStyleContext *context;
1034 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_LEVEL_BAR, GtkLevelBarPrivate);
1036 context = gtk_widget_get_style_context (GTK_WIDGET (self));
1037 gtk_style_context_add_class (context, GTK_STYLE_CLASS_LEVEL_BAR);
1039 self->priv->cur_value = 0.0;
1040 self->priv->min_value = 0.0;
1041 self->priv->max_value = 1.0;
1043 gtk_level_bar_ensure_offset (self, GTK_LEVEL_BAR_OFFSET_LOW, 0.25);
1044 gtk_level_bar_ensure_offset (self, GTK_LEVEL_BAR_OFFSET_HIGH, 0.75);
1045 gtk_level_bar_update_level_style_classes (self);
1047 self->priv->bar_mode = GTK_LEVEL_BAR_MODE_CONTINUOUS;
1048 gtk_level_bar_update_mode_style_classes (self);
1050 /* set initial orientation and style classes */
1051 self->priv->orientation = GTK_ORIENTATION_HORIZONTAL;
1052 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (self));
1054 self->priv->inverted = FALSE;
1056 gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
1060 * gtk_level_bar_new:
1062 * Creates a new #GtkLevelBar.
1064 * Returns: a #GtkLevelBar.
1069 gtk_level_bar_new (void)
1071 return g_object_new (GTK_TYPE_LEVEL_BAR, NULL);
1075 * gtk_level_bar_new_for_interval:
1076 * @min_value: a positive value
1077 * @max_value: a positive value
1079 * Utility constructor that creates a new #GtkLevelBar for the specified
1082 * Returns: a #GtkLevelBar
1087 gtk_level_bar_new_for_interval (gdouble min_value,
1090 return g_object_new (GTK_TYPE_LEVEL_BAR,
1091 "min-value", min_value,
1092 "max-value", max_value,
1097 * gtk_level_bar_get_min_value:
1098 * @self: a #GtkLevelBar
1100 * Returns the value of the #GtkLevelBar:min-value property.
1102 * Returns: a positive value
1107 gtk_level_bar_get_min_value (GtkLevelBar *self)
1109 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), 0.0);
1111 return self->priv->min_value;
1115 * gtk_level_bar_get_max_value:
1116 * @self: a #GtkLevelBar
1118 * Returns the value of the #GtkLevelBar:max-value property.
1120 * Returns: a positive value
1125 gtk_level_bar_get_max_value (GtkLevelBar *self)
1127 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), 0.0);
1129 return self->priv->max_value;
1133 * gtk_level_bar_get_value:
1134 * @self: a #GtkLevelBar
1136 * Returns the value of the #GtkLevelBar:value property.
1138 * Returns: a value in the interval between
1139 * #GtkLevelBar:min-value and #GtkLevelBar:max-value
1144 gtk_level_bar_get_value (GtkLevelBar *self)
1146 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), 0.0);
1148 return self->priv->cur_value;
1152 gtk_level_bar_set_value_internal (GtkLevelBar *self,
1155 self->priv->cur_value = value;
1156 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VALUE]);
1157 gtk_widget_queue_draw (GTK_WIDGET (self));
1161 * gtk_level_bar_set_min_value:
1162 * @self: a #GtkLevelBar
1163 * @value: a positive value
1165 * Sets the value of the #GtkLevelBar:min-value property.
1170 gtk_level_bar_set_min_value (GtkLevelBar *self,
1173 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1174 g_return_if_fail (value >= 0.0);
1176 if (value != self->priv->min_value)
1178 self->priv->min_value = value;
1179 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MIN_VALUE]);
1181 if (self->priv->min_value > self->priv->cur_value)
1182 gtk_level_bar_set_value_internal (self, self->priv->min_value);
1184 gtk_level_bar_update_level_style_classes (self);
1189 * gtk_level_bar_set_max_value:
1190 * @self: a #GtkLevelBar
1191 * @value: a positive value
1193 * Sets the value of the #GtkLevelBar:max-value property.
1198 gtk_level_bar_set_max_value (GtkLevelBar *self,
1201 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1202 g_return_if_fail (value >= 0.0);
1204 if (value != self->priv->max_value)
1206 self->priv->max_value = value;
1207 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MAX_VALUE]);
1209 if (self->priv->max_value < self->priv->cur_value)
1210 gtk_level_bar_set_value_internal (self, self->priv->max_value);
1212 gtk_level_bar_ensure_offsets_in_range (self);
1213 gtk_level_bar_update_level_style_classes (self);
1218 * gtk_level_bar_set_value:
1219 * @self: a #GtkLevelBar
1220 * @value: a value in the interval between
1221 * #GtkLevelBar:min-value and #GtkLevelBar:max-value
1223 * Sets the value of the #GtkLevelBar:value property.
1228 gtk_level_bar_set_value (GtkLevelBar *self,
1231 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1233 if (value != self->priv->cur_value)
1235 gtk_level_bar_set_value_internal (self, value);
1236 gtk_level_bar_update_level_style_classes (self);
1241 * gtk_level_bar_get_mode:
1242 * @self: a #GtkLevelBar
1244 * Returns the value of the #GtkLevelBar:mode property.
1246 * Returns: a #GtkLevelBarMode
1251 gtk_level_bar_get_mode (GtkLevelBar *self)
1253 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), 0);
1255 return self->priv->bar_mode;
1259 * gtk_level_bar_set_mode:
1260 * @self: a #GtkLevelBar
1261 * @mode: a #GtkLevelBarMode
1263 * Sets the value of the #GtkLevelBar:mode property.
1268 gtk_level_bar_set_mode (GtkLevelBar *self,
1269 GtkLevelBarMode mode)
1271 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1273 if (self->priv->bar_mode != mode)
1275 self->priv->bar_mode = mode;
1276 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODE]);
1278 gtk_level_bar_update_mode_style_classes (self);
1279 gtk_widget_queue_resize (GTK_WIDGET (self));
1284 * gtk_level_bar_get_inverted:
1285 * @self: a #GtkLevelBar
1287 * Return the value of the #GtkLevelBar:inverted property.
1289 * Return value: %TRUE if the level bar is inverted
1294 gtk_level_bar_get_inverted (GtkLevelBar *self)
1296 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), FALSE);
1298 return self->priv->inverted;
1302 * gtk_level_bar_set_inverted:
1303 * @self: a #GtkLevelBar
1304 * @inverted: %TRUE to invert the level bar
1306 * Sets the value of the #GtkLevelBar:inverted property.
1311 gtk_level_bar_set_inverted (GtkLevelBar *self,
1314 GtkLevelBarPrivate *priv;
1316 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1320 if (priv->inverted != inverted)
1322 priv->inverted = inverted;
1324 gtk_widget_queue_resize (GTK_WIDGET (self));
1326 g_object_notify (G_OBJECT (self), "inverted");
1331 * gtk_level_bar_remove_offset_value:
1332 * @self: a #GtkLevelBar
1333 * @name: (allow-none): the name of an offset in the bar
1335 * Removes an offset marker previously added with
1336 * gtk_level_bar_add_offset_value().
1341 gtk_level_bar_remove_offset_value (GtkLevelBar *self,
1346 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1348 existing = g_list_find_custom (self->priv->offsets, name, offset_find_func);
1351 gtk_level_bar_offset_free (existing->data);
1352 self->priv->offsets = g_list_delete_link (self->priv->offsets, existing);
1354 gtk_level_bar_update_level_style_classes (self);
1359 * gtk_level_bar_add_offset_value:
1360 * @self: a #GtkLevelBar
1361 * @name: the name of the new offset
1362 * @value: the value for the new offset
1364 * Adds a new offset marker on @self at the position specified by @value.
1365 * When the bar value is in the interval topped by @value (or between @value
1366 * and #GtkLevelBar:max-value in case the offset is the last one on the bar)
1367 * a style class named <literal>level-</literal>@name will be applied
1368 * when rendering the level bar fill.
1369 * If another offset marker named @name exists, its value will be
1370 * replaced by @value.
1375 gtk_level_bar_add_offset_value (GtkLevelBar *self,
1381 g_return_if_fail (GTK_IS_LEVEL_BAR (self));
1382 g_return_if_fail (gtk_level_bar_value_in_interval (self, value));
1384 if (!gtk_level_bar_ensure_offset (self, name, value))
1387 gtk_level_bar_update_level_style_classes (self);
1388 name_quark = g_quark_from_string (name);
1389 g_signal_emit (self, signals[SIGNAL_OFFSET_CHANGED], name_quark, name);
1393 * gtk_level_bar_get_offset_value:
1394 * @self: a #GtkLevelBar
1395 * @name: (allow-none): the name of an offset in the bar
1396 * @value: (out): location where to store the value
1398 * Fetches the value specified for the offset marker @name in @self,
1399 * returning %TRUE in case an offset named @name was found.
1401 * Returns: %TRUE if the specified offset is found
1406 gtk_level_bar_get_offset_value (GtkLevelBar *self,
1411 GtkLevelBarOffset *offset = NULL;
1413 g_return_val_if_fail (GTK_IS_LEVEL_BAR (self), 0.0);
1415 existing = g_list_find_custom (self->priv->offsets, name, offset_find_func);
1417 offset = existing->data;
1423 *value = offset->value;