1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 235 236 237 238 239 240 241 242 243 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 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 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 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 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 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 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 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 1072 1073 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 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
* mx-adjustment.c: Adjustment object
*
* Copyright (C) 2008 OpenedHand
* Copyright (c) 2009, 2010 Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*
* Written by: Chris Lord <chris@openedhand.com>, inspired by GtkAdjustment
* Port to Mx by: Robert Staudinger <robsta@openedhand.com>
*
*/
/**
* SECTION:mx-adjustment
* @short_description: A GObject representing an adjustable bounded value
*
* The #MxAdjustment object represents a range of values bounded between a
* minimum and maximum, together with step and page increments and a page size.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <glib-object.h>
#include <clutter/clutter.h>
#include "mx-adjustment.h"
#include "mx-marshal.h"
#include "mx-private.h"
G_DEFINE_TYPE (MxAdjustment, mx_adjustment, G_TYPE_OBJECT)
#define ADJUSTMENT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MX_TYPE_ADJUSTMENT, MxAdjustmentPrivate))
struct _MxAdjustmentPrivate
{
/* Do not sanity-check values while constructing,
* not all properties may be set yet. */
guint is_constructing : 1;
guint clamp_value : 1;
guint elastic : 1;
gdouble lower;
gdouble upper;
gdouble value;
gdouble step_increment;
gdouble page_increment;
gdouble page_size;
/* For signal emission/notification */
guint lower_source;
guint upper_source;
guint value_source;
guint step_inc_source;
guint page_inc_source;
guint page_size_source;
guint changed_source;
/* For interpolation */
ClutterTimeline *interpolation;
gdouble old_position;
gdouble new_position;
ClutterAlpha *interpolate_alpha;
};
enum
{
PROP_0,
PROP_LOWER,
PROP_UPPER,
PROP_VALUE,
PROP_STEP_INC,
PROP_PAGE_INC,
PROP_PAGE_SIZE,
PROP_ELASTIC,
PROP_CLAMP_VALUE,
};
enum
{
CHANGED,
INTERPOLATION_COMPLETED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };
static gboolean _mx_adjustment_set_lower (MxAdjustment *adjustment,
gdouble lower);
static gboolean _mx_adjustment_set_upper (MxAdjustment *adjustment,
gdouble upper);
static gboolean _mx_adjustment_set_step_increment (MxAdjustment *adjustment,
gdouble step);
static gboolean _mx_adjustment_set_page_increment (MxAdjustment *adjustment,
gdouble page);
static gboolean _mx_adjustment_set_page_size (MxAdjustment *adjustment,
gdouble size);
static void mx_adjustment_clamp_page (MxAdjustment *adjustment,
gdouble lower,
gdouble upper);
static void mx_adjustment_emit_changed (MxAdjustment *adjustment);
static void
mx_adjustment_constructed (GObject *object)
{
GObjectClass *g_class;
MxAdjustment *self = MX_ADJUSTMENT (object);
g_class = G_OBJECT_CLASS (mx_adjustment_parent_class);
/* The docs say we're suppose to chain up, but would crash without
* some extra care. */
if (g_class && g_class->constructed &&
g_class->constructed != mx_adjustment_constructed)
{
g_class->constructed (object);
}
MX_ADJUSTMENT (self)->priv->is_constructing = FALSE;
mx_adjustment_clamp_page (self, self->priv->lower, self->priv->upper);
}
static void
mx_adjustment_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MxAdjustmentPrivate *priv = MX_ADJUSTMENT (gobject)->priv;
switch (prop_id)
{
case PROP_LOWER:
g_value_set_double (value, priv->lower);
break;
case PROP_UPPER:
g_value_set_double (value, priv->upper);
break;
case PROP_VALUE:
g_value_set_double (value, priv->value);
break;
case PROP_STEP_INC:
g_value_set_double (value, priv->step_increment);
break;
case PROP_PAGE_INC:
g_value_set_double (value, priv->page_increment);
break;
case PROP_PAGE_SIZE:
g_value_set_double (value, priv->page_size);
break;
case PROP_ELASTIC:
g_value_set_boolean (value, priv->elastic);
break;
case PROP_CLAMP_VALUE:
g_value_set_boolean (value, priv->clamp_value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
mx_adjustment_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MxAdjustment *adj = MX_ADJUSTMENT (gobject);
switch (prop_id)
{
case PROP_LOWER:
mx_adjustment_set_lower (adj, g_value_get_double (value));
break;
case PROP_UPPER:
mx_adjustment_set_upper (adj, g_value_get_double (value));
break;
case PROP_VALUE:
mx_adjustment_set_value (adj, g_value_get_double (value));
break;
case PROP_STEP_INC:
mx_adjustment_set_step_increment (adj, g_value_get_double (value));
break;
case PROP_PAGE_INC:
mx_adjustment_set_page_increment (adj, g_value_get_double (value));
break;
case PROP_PAGE_SIZE:
mx_adjustment_set_page_size (adj, g_value_get_double (value));
break;
case PROP_ELASTIC:
mx_adjustment_set_elastic (adj, g_value_get_boolean (value));
break;
case PROP_CLAMP_VALUE:
mx_adjustment_set_clamp_value (adj, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
stop_interpolation (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->interpolation)
{
clutter_timeline_stop (priv->interpolation);
g_object_unref (priv->interpolation);
priv->interpolation = NULL;
}
}
static void
mx_adjustment_remove_idle (guint *source)
{
if (*source)
{
g_source_remove (*source);
*source = 0;
}
}
static void
mx_adjustment_dispose (GObject *object)
{
MxAdjustmentPrivate *priv = MX_ADJUSTMENT (object)->priv;
stop_interpolation (MX_ADJUSTMENT (object));
/* Remove idle handlers */
mx_adjustment_remove_idle (&priv->value_source);
mx_adjustment_remove_idle (&priv->lower_source);
mx_adjustment_remove_idle (&priv->upper_source);
mx_adjustment_remove_idle (&priv->page_inc_source);
mx_adjustment_remove_idle (&priv->step_inc_source);
mx_adjustment_remove_idle (&priv->page_size_source);
mx_adjustment_remove_idle (&priv->changed_source);
if (priv->interpolate_alpha)
{
g_object_unref (priv->interpolate_alpha);
priv->interpolate_alpha = NULL;
}
G_OBJECT_CLASS (mx_adjustment_parent_class)->dispose (object);
}
static void
mx_adjustment_class_init (MxAdjustmentClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (MxAdjustmentPrivate));
object_class->constructed = mx_adjustment_constructed;
object_class->get_property = mx_adjustment_get_property;
object_class->set_property = mx_adjustment_set_property;
object_class->dispose = mx_adjustment_dispose;
g_object_class_install_property (object_class,
PROP_LOWER,
g_param_spec_double ("lower",
"Lower",
"Lower bound",
-G_MAXDOUBLE,
G_MAXDOUBLE,
0.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_UPPER,
g_param_spec_double ("upper",
"Upper",
"Upper bound",
-G_MAXDOUBLE,
G_MAXDOUBLE,
0.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_VALUE,
g_param_spec_double ("value",
"Value",
"Current value",
-G_MAXDOUBLE,
G_MAXDOUBLE,
0.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_STEP_INC,
g_param_spec_double ("step-increment",
"Step Increment",
"Step increment",
0.0,
G_MAXDOUBLE,
1.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_PAGE_INC,
g_param_spec_double ("page-increment",
"Page Increment",
"Page increment",
0.0,
G_MAXDOUBLE,
0.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_PAGE_SIZE,
g_param_spec_double ("page-size",
"Page Size",
"Page size",
0.0,
G_MAXDOUBLE,
0.0,
MX_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_ELASTIC,
g_param_spec_boolean ("elastic",
"Elastic",
"Make interpolation "
"behave in an "
"'elastic' way and "
"stop clamping value.",
FALSE,
MX_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_CLAMP_VALUE,
g_param_spec_boolean ("clamp-value",
"Clamp value",
"Clamp the adjustment "
"value between the "
"lower and upper "
"values, respecting "
"the page-size.",
TRUE,
MX_PARAM_READWRITE));
/**
* MxAdjustment::changed:
*
* Emitted when any of the adjustment values have changed
*/
signals[CHANGED] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MxAdjustmentClass, changed),
NULL, NULL,
_mx_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* MxAdjustment::interpolation-completed:
*
* Emitted when the animation started by mx_adjustment_interpolate completes
*/
signals[INTERPOLATION_COMPLETED] =
g_signal_new ("interpolation-completed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (MxAdjustmentClass, interpolation_completed),
NULL, NULL,
_mx_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
mx_adjustment_init (MxAdjustment *self)
{
self->priv = ADJUSTMENT_PRIVATE (self);
self->priv->is_constructing = TRUE;
self->priv->clamp_value = TRUE;
}
/**
* mx_adjustment_new:
*
* Create a new MxAdjustment
*
* Returns: a newly allocated MxAdjustment
*/
MxAdjustment *
mx_adjustment_new (void)
{
return g_object_new (MX_TYPE_ADJUSTMENT, NULL);
}
/**
* mx_adjustment_new_with_values:
* @value: A #gdouble
* @lower: A #gdouble
* @upper: A #gdouble
* @step_increment: A #gdouble
* @page_increment: A #gdouble
* @page_size: A #gdouble
*
* Create a new MxAdjustment with the properties set to the values specified.
*
* Returns: a newly allocated MxAdjustment
*/
MxAdjustment *
mx_adjustment_new_with_values (gdouble value,
gdouble lower,
gdouble upper,
gdouble step_increment,
gdouble page_increment,
gdouble page_size)
{
return g_object_new (MX_TYPE_ADJUSTMENT,
"value", value,
"lower", lower,
"upper", upper,
"step-increment", step_increment,
"page-increment", page_increment,
"page-size", page_size,
NULL);
}
/**
* mx_adjustment_get_value:
* @adjustment: An #MxAdjustment
*
* Get the current value of the #MxAdjustment:value property
*
* Returns: the current value of the "value" property
*/
gdouble
mx_adjustment_get_value (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv;
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0);
priv = adjustment->priv;
return priv->value;
}
static gboolean
mx_adjustment_value_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->value_source = 0;
g_object_notify (G_OBJECT (adjustment), "value");
return FALSE;
}
static gboolean
mx_adjustment_lower_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->lower_source = 0;
g_object_notify (G_OBJECT (adjustment), "lower");
return FALSE;
}
static gboolean
mx_adjustment_upper_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->upper_source = 0;
g_object_notify (G_OBJECT (adjustment), "upper");
return FALSE;
}
static gboolean
mx_adjustment_step_inc_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->step_inc_source = 0;
g_object_notify (G_OBJECT (adjustment), "step-increment");
return FALSE;
}
static gboolean
mx_adjustment_page_inc_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->page_inc_source = 0;
g_object_notify (G_OBJECT (adjustment), "page-increment");
return FALSE;
}
static gboolean
mx_adjustment_page_size_notify_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->page_size_source = 0;
g_object_notify (G_OBJECT (adjustment), "page-size");
return FALSE;
}
static gboolean
mx_adjustment_emit_changed_cb (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
priv->changed_source = 0;
g_signal_emit (adjustment, signals[CHANGED], 0);
return FALSE;
}
/**
* mx_adjustment_set_value:
* @adjustment: An #MxAdjustment
* @value: A #gdouble
*
* Set the value of the #MxAdjustment:value property.
*
*/
void
mx_adjustment_set_value (MxAdjustment *adjustment,
gdouble value)
{
MxAdjustmentPrivate *priv;
g_return_if_fail (MX_IS_ADJUSTMENT (adjustment));
priv = adjustment->priv;
/* Defer clamp until after construction. */
if (!priv->is_constructing)
{
if (!priv->elastic && priv->clamp_value)
value = CLAMP (value,
priv->lower,
MAX (priv->lower, priv->upper - priv->page_size));
}
if (priv->value != value)
{
stop_interpolation (adjustment);
priv->value = value;
g_object_notify (G_OBJECT (adjustment), "value");
mx_adjustment_emit_changed (adjustment);
}
}
static void
mx_adjustment_clamp_page (MxAdjustment *adjustment,
gdouble lower,
gdouble upper)
{
MxAdjustmentPrivate *priv;
gboolean changed;
g_return_if_fail (MX_IS_ADJUSTMENT (adjustment));
priv = adjustment->priv;
lower = CLAMP (lower, priv->lower, priv->upper - priv->page_size);
upper = CLAMP (upper, priv->lower + priv->page_size, priv->upper);
changed = FALSE;
if (priv->value + priv->page_size > upper)
{
priv->value = upper - priv->page_size;
changed = TRUE;
}
if (priv->value < lower)
{
priv->value = lower;
changed = TRUE;
}
if (changed && !priv->value_source)
priv->value_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_value_notify_cb,
adjustment,
NULL);
}
static void
mx_adjustment_emit_changed (MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (!priv->changed_source)
priv->changed_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_emit_changed_cb,
adjustment,
NULL);
}
static gboolean
_mx_adjustment_set_lower (MxAdjustment *adjustment,
gdouble lower)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->lower != lower)
{
priv->lower = lower;
mx_adjustment_emit_changed (adjustment);
if (!priv->lower_source)
priv->lower_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_lower_notify_cb,
adjustment,
NULL);
/* Defer clamp until after construction. */
if (!priv->is_constructing && priv->clamp_value)
mx_adjustment_clamp_page (adjustment, priv->lower, priv->upper);
return TRUE;
}
return FALSE;
}
/**
* mx_adjustment_set_lower:
* @adjustment: A #MxAdjustment
* @lower: A #gdouble
*
* Set the value of the #MxAdjustment:lower property.
*
*/
void
mx_adjustment_set_lower (MxAdjustment *adjustment,
gdouble lower)
{
_mx_adjustment_set_lower (adjustment, lower);
}
/**
* mx_adjustment_get_lower:
* @adjustment: A #MxAdjustment
*
* Get the value of the #MxAdjustment:lower property.
*
* Returns: the current value of the "lower" property.
*/
gdouble
mx_adjustment_get_lower (MxAdjustment *adjustment)
{
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0.0);
return adjustment->priv->lower;
}
static gboolean
_mx_adjustment_set_upper (MxAdjustment *adjustment,
gdouble upper)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->upper != upper)
{
priv->upper = upper;
mx_adjustment_emit_changed (adjustment);
if (!priv->upper_source)
priv->upper_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_upper_notify_cb,
adjustment,
NULL);
/* Defer clamp until after construction. */
if (!priv->is_constructing && priv->clamp_value)
mx_adjustment_clamp_page (adjustment, priv->lower, priv->upper);
return TRUE;
}
return FALSE;
}
/**
* mx_adjustment_set_upper:
* @adjustment: A #MxAdjustment
* @upper: A #gdouble
*
* Set the value of the #MxAdjustment:upper property.
*
*/
void
mx_adjustment_set_upper (MxAdjustment *adjustment,
gdouble upper)
{
_mx_adjustment_set_upper (adjustment, upper);
}
/**
* mx_adjustment_get_upper:
* @adjustment: A #MxAdjustment
*
* Get the value of the #MxAdjustment:upper property.
*
* Returns: the current value of the "upper" property.
*/
gdouble
mx_adjustment_get_upper (MxAdjustment *adjustment)
{
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0.0);
return adjustment->priv->upper;
}
static gboolean
_mx_adjustment_set_step_increment (MxAdjustment *adjustment,
gdouble step)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->step_increment != step)
{
priv->step_increment = step;
mx_adjustment_emit_changed (adjustment);
if (!priv->step_inc_source)
priv->step_inc_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_step_inc_notify_cb,
adjustment,
NULL);
return TRUE;
}
return FALSE;
}
/**
* mx_adjustment_set_step_increment:
* @adjustment: A #MxAdjustment
* @increment: A #gdouble
*
* Set the value of the #MxAdjustment:step-increment property.
*
*/
void
mx_adjustment_set_step_increment (MxAdjustment *adjustment,
gdouble increment)
{
_mx_adjustment_set_step_increment (adjustment, increment);
}
/**
* mx_adjustment_get_step_increment:
* @adjustment: A #MxAdjustment
*
* Get the value of the MxAdjustment:step-increment property.
*
* Returns: the current value of the "step-increment" property.
*/
gdouble
mx_adjustment_get_step_increment (MxAdjustment *adjustment)
{
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0.0);
return adjustment->priv->step_increment;
}
static gboolean
_mx_adjustment_set_page_increment (MxAdjustment *adjustment,
gdouble page)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->page_increment != page)
{
priv->page_increment = page;
mx_adjustment_emit_changed (adjustment);
if (!priv->page_inc_source)
priv->page_inc_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_page_inc_notify_cb,
adjustment,
NULL);
return TRUE;
}
return FALSE;
}
/**
* mx_adjustment_set_page_increment:
* @adjustment: A #MxAdjustment
* @increment: A #gdouble
*
* Set the value of the #MxAdjustment:page-increment property.
*
*/
void
mx_adjustment_set_page_increment (MxAdjustment *adjustment,
gdouble increment)
{
_mx_adjustment_set_page_increment (adjustment, increment);
}
/**
* mx_adjustment_get_page_increment:
* @adjustment: A #MxAdjustment
*
* Get the value of the MxAdjustment:page-increment property.
*
* Returns: the current value of the "page-increment" property.
*/
gdouble
mx_adjustment_get_page_increment (MxAdjustment *adjustment)
{
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0.0);
return adjustment->priv->page_increment;
}
static gboolean
_mx_adjustment_set_page_size (MxAdjustment *adjustment,
gdouble size)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->page_size != size)
{
priv->page_size = size;
mx_adjustment_emit_changed (adjustment);
if (!priv->page_size_source)
priv->page_size_source =
g_idle_add_full (CLUTTER_PRIORITY_REDRAW,
(GSourceFunc)mx_adjustment_page_size_notify_cb,
adjustment,
NULL);
/* Well explicitely clamp after construction. */
if (!priv->is_constructing && priv->clamp_value)
mx_adjustment_clamp_page (adjustment, priv->lower, priv->upper);
return TRUE;
}
return FALSE;
}
/**
* mx_adjustment_set_page_size:
* @adjustment: A #MxAdjustment
* @page_size: A #gdouble
*
* Set the #MxAdjustment:page-size property.
*
*/
void
mx_adjustment_set_page_size (MxAdjustment *adjustment,
gdouble page_size)
{
_mx_adjustment_set_page_size (adjustment, page_size);
}
/**
* mx_adjustment_get_page_size:
* @adjustment: A #MxAdjustment
*
* Get the value of the #MxAdjustment:page-size property.
*
* Returns: the current value of the "page-size" property.
*/
gdouble
mx_adjustment_get_page_size (MxAdjustment *adjustment)
{
g_return_val_if_fail (MX_IS_ADJUSTMENT (adjustment), 0.0);
return adjustment->priv->page_size;
}
/**
* mx_adjustment_set_values:
* @adjustment: A #MxAdjustment
* @value: A #gdouble
* @lower: A #gdouble
* @upper: A #gdouble
* @step_increment: A #gdouble
* @page_increment: A #gdouble
* @page_size: A #gdouble
*
* Set the various properties of MxAdjustment.
*
*/
void
mx_adjustment_set_values (MxAdjustment *adjustment,
gdouble value,
gdouble lower,
gdouble upper,
gdouble step_increment,
gdouble page_increment,
gdouble page_size)
{
MxAdjustmentPrivate *priv;
gboolean emit_changed = FALSE;
g_return_if_fail (MX_IS_ADJUSTMENT (adjustment));
g_return_if_fail (page_size >= 0 && page_size <= G_MAXDOUBLE);
g_return_if_fail (step_increment >= 0 && step_increment <= G_MAXDOUBLE);
g_return_if_fail (page_increment >= 0 && page_increment <= G_MAXDOUBLE);
priv = adjustment->priv;
emit_changed = FALSE;
g_object_freeze_notify (G_OBJECT (adjustment));
emit_changed |= _mx_adjustment_set_lower (adjustment, lower);
emit_changed |= _mx_adjustment_set_upper (adjustment, upper);
emit_changed |= _mx_adjustment_set_step_increment (adjustment,
step_increment);
emit_changed |= _mx_adjustment_set_page_increment (adjustment,
page_increment);
emit_changed |= _mx_adjustment_set_page_size (adjustment, page_size);
if (value != priv->value)
{
mx_adjustment_set_value (adjustment, value);
emit_changed = TRUE;
}
if (emit_changed)
mx_adjustment_emit_changed (adjustment);
g_object_thaw_notify (G_OBJECT (adjustment));
}
/**
* mx_adjustment_get_values:
* @adjustment: A #MxAdjustment
* @value: A #gdouble
* @lower: A #gdouble
* @upper: A #gdouble
* @step_increment: A #gdouble
* @page_increment: A #gdouble
* @page_size: A #gdouble
*
* Get the various properties of MxAdjustment.
*
*/
void
mx_adjustment_get_values (MxAdjustment *adjustment,
gdouble *value,
gdouble *lower,
gdouble *upper,
gdouble *step_increment,
gdouble *page_increment,
gdouble *page_size)
{
MxAdjustmentPrivate *priv;
g_return_if_fail (MX_IS_ADJUSTMENT (adjustment));
priv = adjustment->priv;
if (lower)
*lower = priv->lower;
if (upper)
*upper = priv->upper;
if (value)
*value = mx_adjustment_get_value (adjustment);
if (step_increment)
*step_increment = priv->step_increment;
if (page_increment)
*page_increment = priv->page_increment;
if (page_size)
*page_size = priv->page_size;
}
static void
interpolation_new_frame_cb (ClutterTimeline *timeline,
guint msecs,
MxAdjustment *adjustment)
{
gdouble new_value;
MxAdjustmentPrivate *priv = adjustment->priv;
priv->interpolation = NULL;
new_value = priv->old_position +
(priv->new_position - priv->old_position) *
clutter_alpha_get_alpha (priv->interpolate_alpha);
mx_adjustment_set_value (adjustment, new_value);
priv->interpolation = timeline;
/* Stop the interpolation if we've reached the end of the adjustment */
if (!priv->elastic && priv->clamp_value &&
((new_value < priv->lower) ||
(new_value > (priv->upper - priv->page_size))))
stop_interpolation (adjustment);
}
static void
interpolation_completed_cb (ClutterTimeline *timeline,
MxAdjustment *adjustment)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->elastic && priv->clamp_value)
{
if (clutter_timeline_get_direction (priv->interpolation) ==
CLUTTER_TIMELINE_FORWARD)
{
clutter_timeline_set_direction (priv->interpolation,
CLUTTER_TIMELINE_BACKWARD);
clutter_timeline_set_duration (priv->interpolation, 250);
clutter_timeline_rewind (priv->interpolation);
if (priv->new_position < priv->lower)
{
priv->old_position = priv->lower;
clutter_timeline_start (priv->interpolation);
}
else if (priv->new_position > (priv->upper - priv->page_size))
{
priv->old_position = priv->upper - priv->page_size;
clutter_timeline_start (priv->interpolation);
}
}
else
{
stop_interpolation (adjustment);
mx_adjustment_set_value (adjustment, priv->old_position);
}
}
else
{
stop_interpolation (adjustment);
mx_adjustment_set_value (adjustment, priv->new_position);
}
g_signal_emit (adjustment, signals[INTERPOLATION_COMPLETED], 0);
}
/**
* mx_adjustment_interpolate:
* @adjustment: A #MxAdjustment
* @value: A #gdouble
* @duration: duration in milliseconds
* @mode: A #ClutterAnimationMode
*
* Interpolate #MxAdjustment:value to the new value specified by @value, using
* the mode and duration given.
*/
void
mx_adjustment_interpolate (MxAdjustment *adjustment,
gdouble value,
guint duration,
gulong mode)
{
MxAdjustmentPrivate *priv = adjustment->priv;
g_return_if_fail (isfinite (value));
if (duration <= 1)
{
stop_interpolation (adjustment);
mx_adjustment_set_value (adjustment, value);
return;
}
priv->old_position = priv->value;
priv->new_position = value;
if (!priv->interpolation)
{
priv->interpolation = clutter_timeline_new (duration);
g_signal_connect (priv->interpolation,
"new-frame",
G_CALLBACK (interpolation_new_frame_cb),
adjustment);
g_signal_connect (priv->interpolation,
"completed",
G_CALLBACK (interpolation_completed_cb),
adjustment);
}
else
{
/* Extend the animation if it gets interrupted, otherwise frequent calls
* to this function will end up with no advancements until the calls
* finish (as the animation never gets a chance to start).
*/
clutter_timeline_set_direction (priv->interpolation,
CLUTTER_TIMELINE_FORWARD);
clutter_timeline_rewind (priv->interpolation);
clutter_timeline_set_duration (priv->interpolation, duration);
}
if (priv->interpolate_alpha)
g_object_unref (priv->interpolate_alpha);
priv->interpolate_alpha = clutter_alpha_new_full (priv->interpolation,
mode);
clutter_timeline_start (priv->interpolation);
}
/**
* mx_adjustment_interpolate_relative:
* @adjustment: A #MxAdjustment
* @offset: A #gdouble
* @duration: duration in milliseconds
* @mode: A #ClutterAnimationMode
*
* Interpolate the value of #MxAdjustment:value to a new value calculated from
* @offset.
*
*/
void
mx_adjustment_interpolate_relative (MxAdjustment *adjustment,
gdouble offset,
guint duration,
gulong mode)
{
MxAdjustmentPrivate *priv = adjustment->priv;
if (priv->interpolation)
offset += priv->new_position;
else
offset += priv->value;
mx_adjustment_interpolate (adjustment,
offset,
duration,
mode);
}
/**
* mx_adjustment_get_elastic:
* @adjustment: A #MxAdjustment
*
* Get the value of the #MxAdjustment:elastic property.
*
* Returns: the current value of the "elastic" property.
*/
gboolean
mx_adjustment_get_elastic (MxAdjustment *adjustment)
{
return adjustment->priv->elastic;
}
/**
* mx_adjustment_set_elastic:
* @adjustment: A #MxAdjustment
* @elastic: A #gboolean
*
* Set the value of the #MxAdjustment:elastic property.
*
*/
void
mx_adjustment_set_elastic (MxAdjustment *adjustment,
gboolean elastic)
{
adjustment->priv->elastic = elastic;
}
/**
* mx_adjustment_get_clamp_value:
* @adjustment: A #MxAdjustment
*
* Get the value of the #MxAdjustment:clamp-value property.
*
* Returns: the current value of the "clamp-value" property.
*
* Since: 1.2
*/
gboolean
mx_adjustment_get_clamp_value (MxAdjustment *adjustment)
{
return adjustment->priv->clamp_value;
}
/**
* mx_adjustment_set_clamp_value:
* @adjustment: A #MxAdjustment
* @clamp: a #gboolean
*
* Set the value of the #MxAdjustment:clamp-value property.
*
* Since: 1.2
*/
void
mx_adjustment_set_clamp_value (MxAdjustment *adjustment,
gboolean clamp)
{
adjustment->priv->clamp_value = clamp;
}
|