gtkscrollbar.c 8.26 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 * Copyright (C) 2001 Red Hat, Inc.
Elliot Lee's avatar
Elliot Lee committed
4 5
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
7 8 9 10 11 12
 * 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
13
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18 19

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

26
#include "config.h"
27

Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkscrollbar.h"
29
#include "gtkrangeprivate.h"
30 31

#include "gtkadjustment.h"
32
#include "gtkintl.h"
33
#include "gtkorientable.h"
34
#include "gtkprivate.h"
Elliot Lee's avatar
Elliot Lee committed
35

36 37 38

/**
 * SECTION:gtkscrollbar
39
 * @Short_description: A Scrollbar
40
 * @Title: GtkScrollbar
41
 * @See_also: #GtkAdjustment, #GtkScrolledWindow
42
 *
43
 * The #GtkScrollbar widget is a horizontal or vertical scrollbar,
44
 * depending on the value of the #GtkOrientable:orientation property.
45 46 47
 *
 * The position of the thumb in a scrollbar is controlled by the scroll
 * adjustments. See #GtkAdjustment for the fields in an adjustment - for
48 49 50 51 52 53
 * #GtkScrollbar, the #GtkAdjustment:value field represents the position
 * of the scrollbar, which must be between the #GtkAdjustment:lower field
 * and #GtkAdjustment:upper - #GtkAdjustment:page-size. The
 * #GtkAdjustment:page-size field represents the size of the visible
 * scrollable area. The #GtkAdjustment:step-increment and
 * #GtkAdjustment:page-increment fields are properties when the user asks to
54
 * step down (using the small stepper arrows) or page down (using for
55
 * example the `Page Down` key).
56 57 58 59
 *
 * # CSS nodes
 *
 * |[<!-- language="plain" -->
60 61 62 63 64 65 66 67
 * scrollbar[.fine-tune]
 * ╰── contents
 *     ├── [button.up]
 *     ├── [button.down]
 *     ├── trough
 *     │   ╰── slider
 *     ├── [button.up]
 *     ╰── [button.down]
68 69
 * ]|
 *
70 71
 * GtkScrollbar has a main CSS node with name scrollbar and a subnode for its
 * contents, with subnodes named trough and slider.
72 73 74 75 76 77 78
 *
 * The main node gets the style class .fine-tune added when the scrollbar is
 * in 'fine-tuning' mode.
 *
 * If steppers are enabled, they are represented by up to four additional
 * subnodes with name button. These get the style classes .up and .down to
 * indicate in which direction they are moving.
79 80 81 82
 *
 * Other style classes that may be added to scrollbars inside #GtkScrolledWindow
 * include the positional classes (.left, .right, .top, .bottom) and style
 * classes related to overlay scrolling (.overlay-indicator, .dragging, .hovering).
83 84 85
 */


86
static void gtk_scrollbar_style_updated (GtkWidget *widget);
87

88
G_DEFINE_TYPE (GtkScrollbar, gtk_scrollbar, GTK_TYPE_RANGE)
Elliot Lee's avatar
Elliot Lee committed
89 90 91 92

static void
gtk_scrollbar_class_init (GtkScrollbarClass *class)
{
93
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
94

95
  widget_class->style_updated = gtk_scrollbar_style_updated;
96

97 98 99 100 101 102 103 104
  /**
   * GtkScrollbar:min-slider-length:
   *
   * Minimum length of scrollbar slider.
   *
   * Deprecated: 3.20: Use min-height/min-width CSS properties on the slider
   *   element instead. The value of this style property is ignored.
   */
105
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
106
					   g_param_spec_int ("min-slider-length",
107 108
							     P_("Minimum Slider Length"),
							     P_("Minimum length of scrollbar slider"),
109 110
							     0,
							     G_MAXINT,
111
							     21,
112
							     GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
113 114

  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
115
					   g_param_spec_boolean ("fixed-slider-length",
116 117
                                                                 P_("Fixed slider size"),
                                                                 P_("Don't change slider size, just lock it to the minimum length"),
118
                                                                 FALSE,
119
                                                                 GTK_PARAM_READABLE));
120

121
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
122
					   g_param_spec_boolean ("has-backward-stepper",
123 124
                                                                 P_("Backward stepper"),
                                                                 P_("Display the standard backward arrow button"),
125
                                                                 TRUE,
126
                                                                 GTK_PARAM_READABLE));
127

128 129 130 131 132 133
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_boolean ("has-forward-stepper",
                                                                 P_("Forward stepper"),
                                                                 P_("Display the standard forward arrow button"),
                                                                 TRUE,
                                                                 GTK_PARAM_READABLE));
134 135

  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
136
					   g_param_spec_boolean ("has-secondary-backward-stepper",
137 138
                                                                 P_("Secondary backward stepper"),
                                                                 P_("Display a second backward arrow button on the opposite end of the scrollbar"),
139
                                                                 FALSE,
140
                                                                 GTK_PARAM_READABLE));
141

142 143 144 145 146 147
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_boolean ("has-secondary-forward-stepper",
                                                                 P_("Secondary forward stepper"),
                                                                 P_("Display a second forward arrow button on the opposite end of the scrollbar"),
                                                                 FALSE,
                                                                 GTK_PARAM_READABLE));
148

149
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_SCROLL_BAR);
150
  gtk_widget_class_set_css_name (widget_class, "scrollbar");
Elliot Lee's avatar
Elliot Lee committed
151 152 153
}

static void
154
gtk_scrollbar_update_style (GtkScrollbar *scrollbar)
Elliot Lee's avatar
Elliot Lee committed
155
{
156 157
  gboolean fixed_size;
  gboolean has_a, has_b, has_c, has_d;
158 159
  GtkRange *range = GTK_RANGE (scrollbar);
  GtkWidget *widget = GTK_WIDGET (scrollbar);
160 161

  gtk_widget_style_get (widget,
162 163 164 165 166
                        "fixed-slider-length", &fixed_size,
                        "has-backward-stepper", &has_a,
                        "has-secondary-forward-stepper", &has_b,
                        "has-secondary-backward-stepper", &has_c,
                        "has-forward-stepper", &has_d,
167
                        NULL);
168

169
  gtk_range_set_slider_size_fixed (range, fixed_size);
170
  _gtk_range_set_steppers (range, has_a, has_b, has_c, has_d);
171 172 173 174 175 176
}

static void
gtk_scrollbar_init (GtkScrollbar *scrollbar)
{
  gtk_scrollbar_update_style (scrollbar);
177
  gtk_range_set_slider_use_min_size (GTK_RANGE (scrollbar), TRUE);
178 179 180 181 182 183
}

static void
gtk_scrollbar_style_updated (GtkWidget *widget)
{
  gtk_scrollbar_update_style (GTK_SCROLLBAR (widget));
184
  GTK_WIDGET_CLASS (gtk_scrollbar_parent_class)->style_updated (widget);
Elliot Lee's avatar
Elliot Lee committed
185
}
186

187 188
/**
 * gtk_scrollbar_new:
189
 * @orientation: the scrollbar’s orientation.
190
 * @adjustment: (allow-none): the #GtkAdjustment to use, or %NULL to create a new adjustment.
191 192 193
 *
 * Creates a new scrollbar with the given orientation.
 *
194
 * Returns:  the new #GtkScrollbar.
195
 *
196
 * Since: 3.0
197 198 199 200 201 202 203 204 205 206 207 208 209
 **/
GtkWidget *
gtk_scrollbar_new (GtkOrientation  orientation,
                   GtkAdjustment  *adjustment)
{
  g_return_val_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment),
                        NULL);

  return g_object_new (GTK_TYPE_SCROLLBAR,
                       "orientation", orientation,
                       "adjustment",  adjustment,
                       NULL);
}