gtkscrollbar.c 8.31 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 48 49 50 51
 * Its position and movement are controlled by the adjustment that is passed to
 * or created by gtk_scrollbar_new(). See #GtkAdjustment for more details. The
 * #GtkAdjustment:value field sets the position of the thumb and must be between
 * #GtkAdjustment:lower and #GtkAdjustment:upper - #GtkAdjustment:page-size. The
 * #GtkAdjustment:page-size represents the size of the visible scrollable area.
 * The fields #GtkAdjustment:step-increment and #GtkAdjustment:page-increment
52 53
 * fields are added to or subtracted from the #GtkAdjustment:value when the user
 * asks to move by a step (using e.g. the cursor arrow keys or, if present, the
54
 * stepper buttons) or by a page (using e.g. the Page Down/Up keys).
55 56 57 58
 *
 * # CSS nodes
 *
 * |[<!-- language="plain" -->
59 60 61 62 63 64 65 66
 * scrollbar[.fine-tune]
 * ╰── contents
 *     ├── [button.up]
 *     ├── [button.down]
 *     ├── trough
 *     │   ╰── slider
 *     ├── [button.up]
 *     ╰── [button.down]
67 68
 * ]|
 *
69 70
 * GtkScrollbar has a main CSS node with name scrollbar and a subnode for its
 * contents, with subnodes named trough and slider.
71 72 73 74 75 76 77
 *
 * 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.
78 79 80 81
 *
 * 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).
82 83 84
 */


85
static void gtk_scrollbar_style_updated (GtkWidget *widget);
86

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

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

94
  widget_class->style_updated = gtk_scrollbar_style_updated;
95

96 97 98 99 100 101 102 103
  /**
   * 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.
   */
104
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
105
					   g_param_spec_int ("min-slider-length",
106 107
							     P_("Minimum Slider Length"),
							     P_("Minimum length of scrollbar slider"),
108 109
							     0,
							     G_MAXINT,
110
							     21,
111
							     GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
112 113

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

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

127 128 129 130 131 132
  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));
133 134

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

141 142 143 144 145 146
  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));
147

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

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

  gtk_widget_style_get (widget,
161 162 163 164 165
                        "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,
166
                        NULL);
167

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

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

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

186 187
/**
 * gtk_scrollbar_new:
188
 * @orientation: the scrollbar’s orientation.
189
 * @adjustment: (allow-none): the #GtkAdjustment to use, or %NULL to create a new adjustment.
190 191 192
 *
 * Creates a new scrollbar with the given orientation.
 *
193
 * Returns:  the new #GtkScrollbar.
194
 *
195
 * Since: 3.0
196 197 198 199 200 201 202 203 204 205 206 207 208
 **/
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);
}