Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_04 / synfig-studio / src / gtkmm / widget_vector.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_vector.cpp
3 **      \brief Template File
4 **
5 **      $Id: widget_vector.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., 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 <gtkmm/spinbutton.h>
33 #include "widget_vector.h"
34 #include "widget_distance.h"
35 #include "app.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace synfig;
43 using namespace studio;
44
45 /* === M A C R O S ========================================================= */
46
47 #define DIGITS          10
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 Widget_Vector::Widget_Vector():
56         x_adjustment(0,-100000000,100000000,0.05,0.05,0.05),
57         y_adjustment(0,-100000000,100000000,0.05,0.05,0.05)
58 {
59         Gtk::Label *label;
60         
61         label=manage(new class Gtk::Label("x:"));
62         label->show();
63         pack_start(*label, Gtk::PACK_SHRINK);
64
65         spinbutton_x=manage(new class Gtk::SpinButton(x_adjustment,0.05,DIGITS));
66         spinbutton_x->set_update_policy(Gtk::UPDATE_ALWAYS);
67         spinbutton_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
68         pack_start(*spinbutton_x, Gtk::PACK_EXPAND_WIDGET);
69
70         distance_x=manage(new Widget_Distance());
71         distance_x->set_digits(4);
72         distance_x->set_update_policy(Gtk::UPDATE_ALWAYS);
73         distance_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
74         pack_start(*distance_x, Gtk::PACK_EXPAND_WIDGET);
75
76         label=manage(new class Gtk::Label("y:"));
77         label->show();
78         pack_start(*label, Gtk::PACK_SHRINK);
79
80         spinbutton_y=manage(new class Gtk::SpinButton(y_adjustment,0.05,DIGITS));
81         spinbutton_y->set_update_policy(Gtk::UPDATE_ALWAYS);
82         spinbutton_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
83         spinbutton_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
84         pack_start(*spinbutton_y, Gtk::PACK_EXPAND_WIDGET);
85
86         distance_y=manage(new Widget_Distance());
87         distance_y->set_digits(4);
88         distance_y->set_update_policy(Gtk::UPDATE_ALWAYS);
89         distance_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
90         distance_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
91         pack_start(*distance_y, Gtk::PACK_EXPAND_WIDGET);
92
93         spinbutton_x->show();
94         spinbutton_y->show();
95         
96         spinbutton_x->signal_activate().connect(sigc::mem_fun(*spinbutton_y,&Gtk::SpinButton::grab_focus));
97         distance_x->signal_activate().connect(sigc::mem_fun(*distance_y,&Gtk::SpinButton::grab_focus));
98 }
99
100 Widget_Vector::~Widget_Vector()
101 {
102 }
103
104 void
105 Widget_Vector::on_grab_focus()
106 {
107         if(canvas_)
108                 distance_x->grab_focus();
109         else
110                 spinbutton_x->grab_focus();
111 }
112
113 void
114 Widget_Vector::set_has_frame(bool x)
115 {
116         if(spinbutton_x)
117         {
118                 spinbutton_x->set_has_frame(x);
119                 spinbutton_y->set_has_frame(x);
120                 spinbutton_x->set_size_request(48,-1);
121                 spinbutton_y->set_size_request(48,-1);
122         }
123
124         distance_x->set_has_frame(x);
125         distance_y->set_has_frame(x);
126         distance_x->set_size_request(48,-1);
127         distance_y->set_size_request(48,-1);
128 }
129
130 void
131 Widget_Vector::set_digits(int x)
132 {
133         if(spinbutton_x)
134         {
135                 spinbutton_x->set_digits(x);
136                 spinbutton_y->set_digits(x);
137                 spinbutton_x->set_size_request(48,-1);
138                 spinbutton_y->set_size_request(48,-1);
139         }
140         
141         distance_x->set_digits(x);
142         distance_y->set_digits(x);
143         distance_x->set_size_request(48,-1);
144         distance_y->set_size_request(48,-1);
145 }
146
147 void
148 Widget_Vector::set_value(const synfig::Vector &data)
149 {
150         vector=data;
151
152         if(canvas_){try
153         {
154                 Distance distx(vector[0],Distance::SYSTEM_UNITS),disty(vector[1],Distance::SYSTEM_UNITS);
155                 distx.convert(App::distance_system,canvas_->rend_desc());
156                 disty.convert(App::distance_system,canvas_->rend_desc());
157                 distance_x->set_value(distx);
158                 distance_y->set_value(disty);
159                 spinbutton_x->hide();
160                 spinbutton_y->hide();
161         }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }}
162         else
163         {
164                 spinbutton_x->set_value(vector[0]);
165                 spinbutton_y->set_value(vector[1]);
166                 distance_x->hide();
167                 distance_y->hide();
168         }
169 }
170
171 const synfig::Vector &
172 Widget_Vector::get_value()
173 {
174         if(!canvas_ && spinbutton_x)
175         {
176                 vector[0]=spinbutton_x->get_value();
177                 vector[1]=spinbutton_y->get_value();
178                 distance_x->hide();
179                 distance_y->hide();
180         }
181         else try
182         {
183                 vector[0]=distance_x->get_value().units(canvas_->rend_desc());
184                 vector[1]=distance_y->get_value().units(canvas_->rend_desc());
185                 spinbutton_x->hide();
186                 spinbutton_y->hide();
187         }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }
188         return vector;
189 }
190
191 void
192 Widget_Vector::on_value_changed()
193 {
194         signal_value_changed()();
195 }
196
197 void
198 Widget_Vector::set_canvas(Canvas::LooseHandle x)
199 {
200         canvas_=x;
201         if(x)
202         {
203                 if(spinbutton_x)
204                 {
205                         spinbutton_x->hide();
206                         spinbutton_y->hide();
207 //                      delete spinbutton_x;
208 //                      delete spinbutton_y;
209                 }
210                 distance_x->show();
211                 distance_y->show();
212         }
213         else
214         {
215                 if(spinbutton_x)
216                 {
217                         spinbutton_x->show();
218                         spinbutton_y->show();
219                 }
220                 distance_x->hide();
221                 distance_y->hide();
222         }
223 }
224
225 void
226 Widget_Vector::show_all_vfunc()
227 {
228         if(canvas_)
229         {
230                 distance_x->show();
231                 distance_y->show();
232         }
233         else
234         {
235                 spinbutton_x->show();
236                 spinbutton_y->show();
237         }
238         show();
239 }