Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07 / src / gtkmm / adjust_window.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file adjust_window.cpp
3 **      \brief Adjustment Window Implementation File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2004 Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "adjust_window.h"
33 #include "app.h"
34
35 #endif
36
37 /* === U S I N G =========================================================== */
38
39 using namespace std;
40 //using namespace etl;
41 //using namespace synfig;
42
43 using studio::Adjust_Window;
44
45 /* === M A C R O S ========================================================= */
46 const double EPSILON = 1.0e-6;
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 /* === E N T R Y P O I N T ================================================= */
55
56 Adjust_Window::Adjust_Window(double value, double lower, double upper,
57                                                         double stepinc, double pageinc, double pagesize,
58                                                         Gtk::Adjustment *adj)
59 : Adjustment(value,lower,upper,stepinc,pageinc,pagesize),
60         adj_child(0)
61 {
62         if(adj) set_child_adjustment(adj);
63 }
64
65 Adjust_Window::~Adjust_Window()
66 {
67         //connections should automatically be killed etc.
68 }
69
70 //child interface functions
71 Gtk::Adjustment *Adjust_Window::get_child_adjustment()
72 {
73         return adj_child;
74 }
75
76 const Gtk::Adjustment *Adjust_Window::get_child_adjustment() const
77 {
78         return adj_child;
79 }
80
81 void Adjust_Window::set_child_adjustment(Gtk::Adjustment *child)
82 {
83         childchanged.disconnect();
84
85         adj_child = child;
86
87         synfig::info("Adjust: connecting to child signals");
88         if(child)
89         {
90                 childchanged = child->signal_changed().connect(sigc::mem_fun(*this,&Adjust_Window::update_fromchild));
91
92                 update_child();
93         }
94 }
95
96 void Adjust_Window::on_changed()
97 {
98         update_child();
99 }
100
101 void Adjust_Window::on_value_changed()
102 {
103         update_child();
104 }
105
106 //SUB TIME FUNCTIONS
107 double Adjust_Window::get_sub_lower() const
108 {
109         return get_value();
110 }
111
112 double Adjust_Window::get_sub_upper() const
113 {
114         return get_value() + get_page_size();
115 }
116
117 //---- REFRESH FUNCTIONS -----
118 void Adjust_Window::update_child()
119 {
120         if(adj_child)
121         {
122                 bool childchanged = false;
123
124                 double v = get_value();
125                 double ve = v + get_page_size();
126
127                 //reset child's values if they need to be...
128                 if(abs(v - adj_child->get_lower()) > EPSILON)
129                 {
130                         adj_child->set_lower(v);
131                         childchanged = true;
132                 }
133
134                 if(abs(ve - adj_child->get_upper()) > EPSILON)
135                 {
136                         adj_child->set_upper(ve);
137                         childchanged = true;
138                 }
139
140                 if(childchanged)
141                 {
142                         adj_child->changed();
143                 }
144         }
145 }
146
147 void Adjust_Window::update_fromchild()
148 {
149         if(adj_child)
150         {
151                 double b = adj_child->get_lower();
152                 double dist = adj_child->get_upper() - b;
153
154                 //reset our values if they need to be...
155                 if(abs(get_value() - b) > EPSILON)
156                 {
157                         set_value(b);
158                         value_changed();
159                 }
160
161                 if(abs(get_page_size() - dist) > EPSILON)
162                 {
163                         set_page_size(dist);
164                         changed();
165                 }
166         }
167 }