gtkadjustment.c 11.3 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

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

27
#include <config.h>
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkadjustment.h"
29
#include "gtkmarshalers.h"
30 31
#include "gtkprivate.h"
#include "gtkintl.h"
32
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
33

34 35 36 37 38 39 40 41 42 43
enum
{
  PROP_0,
  PROP_VALUE,
  PROP_LOWER,
  PROP_UPPER,
  PROP_STEP_INCREMENT,
  PROP_PAGE_INCREMENT,
  PROP_PAGE_SIZE
};
Elliot Lee's avatar
Elliot Lee committed
44 45 46 47 48 49 50 51 52 53 54

enum {
  CHANGED,
  VALUE_CHANGED,
  LAST_SIGNAL
};


static void gtk_adjustment_class_init (GtkAdjustmentClass *klass);
static void gtk_adjustment_init       (GtkAdjustment      *adjustment);

55 56 57 58 59 60 61 62
static void gtk_adjustment_get_property (GObject      *object,
                                         guint         prop_id,
                                         GValue       *value,
                                         GParamSpec   *pspec);
static void gtk_adjustment_set_property (GObject      *object,
                                         guint         prop_id,
                                         const GValue *value,
                                         GParamSpec   *pspec);
Elliot Lee's avatar
Elliot Lee committed
63

64
static guint adjustment_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
65 66


Manish Singh's avatar
Manish Singh committed
67
GType
68
gtk_adjustment_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
69
{
Manish Singh's avatar
Manish Singh committed
70
  static GType adjustment_type = 0;
Elliot Lee's avatar
Elliot Lee committed
71 72 73

  if (!adjustment_type)
    {
Manish Singh's avatar
Manish Singh committed
74
      static const GTypeInfo adjustment_info =
Elliot Lee's avatar
Elliot Lee committed
75 76
      {
	sizeof (GtkAdjustmentClass),
Manish Singh's avatar
Manish Singh committed
77 78 79 80 81 82 83 84
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_adjustment_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkAdjustment),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_adjustment_init,
Elliot Lee's avatar
Elliot Lee committed
85 86
      };

Manish Singh's avatar
Manish Singh committed
87
      adjustment_type =
88
	g_type_register_static (GTK_TYPE_OBJECT, I_("GtkAdjustment"),
Manish Singh's avatar
Manish Singh committed
89
				&adjustment_info, 0);
Elliot Lee's avatar
Elliot Lee committed
90 91 92 93 94 95 96 97
    }

  return adjustment_type;
}

static void
gtk_adjustment_class_init (GtkAdjustmentClass *class)
{
98 99 100 101 102
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  gobject_class->set_property = gtk_adjustment_set_property;
  gobject_class->get_property = gtk_adjustment_get_property;

103 104 105
  class->changed = NULL;
  class->value_changed = NULL;

106 107 108 109 110 111 112
  /**
   * GtkAdjustment:value:
   * 
   * The value of the adjustment.
   * 
   * Since: 2.4
   */
113 114 115 116 117 118 119 120
  g_object_class_install_property (gobject_class,
                                   PROP_VALUE,
                                   g_param_spec_double ("value",
							P_("Value"),
							P_("The value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
121
							GTK_PARAM_READWRITE));
122
  
123 124 125 126 127 128 129
  /**
   * GtkAdjustment:lower:
   * 
   * The minimum value of the adjustment.
   * 
   * Since: 2.4
   */
130 131 132 133 134 135 136 137
  g_object_class_install_property (gobject_class,
                                   PROP_LOWER,
                                   g_param_spec_double ("lower",
							P_("Minimum Value"),
							P_("The minimum value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0,
138
							GTK_PARAM_READWRITE));
139
  
140 141 142 143 144 145 146 147 148 149
  /**
   * GtkAdjustment:upper:
   * 
   * The maximum value of the adjustment. 
   * Note that values will be restricted by 
   * <literal>upper - page-size</literal> if the page-size 
   * property is nonzero.
   *
   * Since: 2.4
   */
150 151 152 153 154 155 156 157
  g_object_class_install_property (gobject_class,
                                   PROP_UPPER,
                                   g_param_spec_double ("upper",
							P_("Maximum Value"),
							P_("The maximum value of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
158
							GTK_PARAM_READWRITE));
159
  
160 161 162 163 164 165 166
  /**
   * GtkAdjustment:step-increment:
   * 
   * The step increment of the adjustment.
   * 
   * Since: 2.4
   */
167 168 169 170 171 172 173 174
  g_object_class_install_property (gobject_class,
                                   PROP_STEP_INCREMENT,
                                   g_param_spec_double ("step-increment",
							P_("Step Increment"),
							P_("The step increment of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
175
							GTK_PARAM_READWRITE));
176
  
177 178 179 180 181 182 183
  /**
   * GtkAdjustment:page-increment:
   * 
   * The page increment of the adjustment.
   * 
   * Since: 2.4
   */
184 185 186 187 188 189 190 191
  g_object_class_install_property (gobject_class,
                                   PROP_PAGE_INCREMENT,
                                   g_param_spec_double ("page-increment",
							P_("Page Increment"),
							P_("The page increment of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
192
							GTK_PARAM_READWRITE));
193
  
194 195 196 197 198 199 200 201 202 203
  /**
   * GtkAdjustment:page-size:
   * 
   * The page size of the adjustment. 
   * Note that the page-size is irrelevant and should be set to zero
   * if the adjustment is used for a simple scalar value, e.g. in a 
   * #GtkSpinButton.
   * 
   * Since: 2.4
   */
204 205 206 207 208 209 210 211
  g_object_class_install_property (gobject_class,
                                   PROP_PAGE_SIZE,
                                   g_param_spec_double ("page-size",
							P_("Page Size"),
							P_("The page size of the adjustment"),
							-G_MAXDOUBLE, 
							G_MAXDOUBLE, 
							0.0, 
212
							GTK_PARAM_READWRITE));
213 214
  

Elliot Lee's avatar
Elliot Lee committed
215
  adjustment_signals[CHANGED] =
216
    g_signal_new (I_("changed"),
Manish Singh's avatar
Manish Singh committed
217 218 219 220 221 222
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkAdjustmentClass, changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Elliot Lee's avatar
Elliot Lee committed
223
  adjustment_signals[VALUE_CHANGED] =
224
    g_signal_new (I_("value_changed"),
Manish Singh's avatar
Manish Singh committed
225 226 227 228 229 230
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE,
		  G_STRUCT_OFFSET (GtkAdjustmentClass, value_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Elliot Lee's avatar
Elliot Lee committed
231 232 233 234 235 236 237 238 239 240 241 242 243
}

static void
gtk_adjustment_init (GtkAdjustment *adjustment)
{
  adjustment->value = 0.0;
  adjustment->lower = 0.0;
  adjustment->upper = 0.0;
  adjustment->step_increment = 0.0;
  adjustment->page_increment = 0.0;
  adjustment->page_size = 0.0;
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static void
gtk_adjustment_get_property (GObject *object, guint prop_id, GValue *value, 
	                     GParamSpec *pspec)
{
  GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);

  switch (prop_id)
    {
    case PROP_VALUE:
      g_value_set_double (value, adjustment->value);
      break;
    case PROP_LOWER:
      g_value_set_double (value, adjustment->lower);
      break;
    case PROP_UPPER:
      g_value_set_double (value, adjustment->upper);
      break;
    case PROP_STEP_INCREMENT:
      g_value_set_double (value, adjustment->step_increment);
      break;
    case PROP_PAGE_INCREMENT:
      g_value_set_double (value, adjustment->page_increment);
      break;
    case PROP_PAGE_SIZE:
      g_value_set_double (value, adjustment->page_size);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_adjustment_set_property (GObject * object, guint prop_id, 
	                     const GValue * value, GParamSpec * pspec)
{
  GtkAdjustment *adjustment = GTK_ADJUSTMENT (object);
  gdouble double_value = g_value_get_double (value);

  switch (prop_id)
    {
    case PROP_VALUE:
      gtk_adjustment_set_value (GTK_ADJUSTMENT (object), double_value);
      break;
    case PROP_LOWER:
      if (adjustment->lower != double_value)
        {
          adjustment->lower = double_value;
          g_object_notify (G_OBJECT (adjustment), "lower");
        }
      break;
    case PROP_UPPER:
      if (adjustment->upper != double_value)
        {
          adjustment->upper = double_value;
          g_object_notify (G_OBJECT (adjustment), "upper");
        }
      break;
    case PROP_STEP_INCREMENT:
      if (adjustment->step_increment != double_value)
        {
          adjustment->step_increment = double_value;
          g_object_notify (G_OBJECT (adjustment), "step-increment");
        }
      break;
    case PROP_PAGE_INCREMENT:
      if (adjustment->page_increment != double_value)
        {
          adjustment->page_increment = double_value;
          g_object_notify (G_OBJECT (adjustment), "page-increment");
        }
      break;
    case PROP_PAGE_SIZE:
      if (adjustment->page_size != double_value)
        {
          adjustment->page_size = double_value;
          g_object_notify (G_OBJECT (adjustment), "page-size");
        }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
329
GtkObject*
330 331 332 333 334 335
gtk_adjustment_new (gdouble value,
		    gdouble lower,
		    gdouble upper,
		    gdouble step_increment,
		    gdouble page_increment,
		    gdouble page_size)
Elliot Lee's avatar
Elliot Lee committed
336
{
337 338 339 340 341
  return g_object_new (GTK_TYPE_ADJUSTMENT, 
		       "lower", lower,
		       "upper", upper,
		       "step-increment", step_increment,
		       "page-increment", page_increment,
342
		       "page-size", page_size,
343
		       "value", value,
344
		       NULL);
Elliot Lee's avatar
Elliot Lee committed
345
}
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/**
 * gtk_adjustment_get_value:
 * @adjustment: a #GtkAdjustment
 *
 * Gets the current value of the adjustment. See
 * gtk_adjustment_set_value ().
 *
 * Return value: The current value of the adjustment.
 **/
gdouble
gtk_adjustment_get_value (GtkAdjustment *adjustment)
{
  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), 0.);

  return adjustment->value;
}

364 365
void
gtk_adjustment_set_value (GtkAdjustment        *adjustment,
366
			  gdouble               value)
367 368 369
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

370 371 372 373 374 375
  value = CLAMP (value, adjustment->lower, adjustment->upper);

  if (value != adjustment->value)
    {
      adjustment->value = value;

376
      gtk_adjustment_value_changed (adjustment);
377 378 379 380 381 382 383 384
    }
}

void
gtk_adjustment_changed (GtkAdjustment        *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

Manish Singh's avatar
Manish Singh committed
385
  g_signal_emit (adjustment, adjustment_signals[CHANGED], 0);
386 387 388 389 390 391
}

void
gtk_adjustment_value_changed (GtkAdjustment        *adjustment)
{
  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
392

Manish Singh's avatar
Manish Singh committed
393
  g_signal_emit (adjustment, adjustment_signals[VALUE_CHANGED], 0);
394
  g_object_notify (G_OBJECT (adjustment), "value");
395
}
396 397 398

void
gtk_adjustment_clamp_page (GtkAdjustment *adjustment,
399 400
			   gdouble        lower,
			   gdouble        upper)
401
{
402
  gboolean need_emission;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  lower = CLAMP (lower, adjustment->lower, adjustment->upper);
  upper = CLAMP (upper, adjustment->lower, adjustment->upper);

  need_emission = FALSE;

  if (adjustment->value + adjustment->page_size < upper)
    {
      adjustment->value = upper - adjustment->page_size;
      need_emission = TRUE;
    }
  if (adjustment->value > lower)
    {
      adjustment->value = lower;
      need_emission = TRUE;
    }

  if (need_emission)
423
    gtk_adjustment_value_changed (adjustment);
424
}
425 426 427

#define __GTK_ADJUSTMENT_C__
#include "gtkaliasdef.c"