grab stable branch
[synfig.git] / synfig-studio / tags / stable / src / gtkmm / widget_vector.cpp
1 /* === S I N F 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 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <gtkmm/spinbutton.h>
32 #include "widget_vector.h"
33 #include "widget_distance.h"
34 #include "app.h"
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace sinfg;
42 using namespace studio;
43
44 /* === M A C R O S ========================================================= */
45
46 #define DIGITS          10
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 Widget_Vector::Widget_Vector():
55         x_adjustment(0,-100000000,100000000,0.05,0.05,0.05),
56         y_adjustment(0,-100000000,100000000,0.05,0.05,0.05)
57 {
58         Gtk::Label *label;
59         
60         label=manage(new class Gtk::Label("x:"));
61         label->show();
62         pack_start(*label, Gtk::PACK_SHRINK);
63
64         spinbutton_x=manage(new class Gtk::SpinButton(x_adjustment,0.05,DIGITS));
65         spinbutton_x->set_update_policy(Gtk::UPDATE_ALWAYS);
66         spinbutton_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
67         pack_start(*spinbutton_x, Gtk::PACK_EXPAND_WIDGET);
68
69         distance_x=manage(new Widget_Distance());
70         distance_x->set_digits(4);
71         distance_x->set_update_policy(Gtk::UPDATE_ALWAYS);
72         distance_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
73         pack_start(*distance_x, Gtk::PACK_EXPAND_WIDGET);
74
75         label=manage(new class Gtk::Label("y:"));
76         label->show();
77         pack_start(*label, Gtk::PACK_SHRINK);
78
79         spinbutton_y=manage(new class Gtk::SpinButton(y_adjustment,0.05,DIGITS));
80         spinbutton_y->set_update_policy(Gtk::UPDATE_ALWAYS);
81         spinbutton_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
82         spinbutton_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
83         pack_start(*spinbutton_y, Gtk::PACK_EXPAND_WIDGET);
84
85         distance_y=manage(new Widget_Distance());
86         distance_y->set_digits(4);
87         distance_y->set_update_policy(Gtk::UPDATE_ALWAYS);
88         distance_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
89         distance_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
90         pack_start(*distance_y, Gtk::PACK_EXPAND_WIDGET);
91
92         spinbutton_x->show();
93         spinbutton_y->show();
94         
95         spinbutton_x->signal_activate().connect(sigc::mem_fun(*spinbutton_y,&Gtk::SpinButton::grab_focus));
96         distance_x->signal_activate().connect(sigc::mem_fun(*distance_y,&Gtk::SpinButton::grab_focus));
97 }
98
99 Widget_Vector::~Widget_Vector()
100 {
101 }
102
103 void
104 Widget_Vector::on_grab_focus()
105 {
106         if(canvas_)
107                 distance_x->grab_focus();
108         else
109                 spinbutton_x->grab_focus();
110 }
111
112 void
113 Widget_Vector::set_has_frame(bool x)
114 {
115         if(spinbutton_x)
116         {
117                 spinbutton_x->set_has_frame(x);
118                 spinbutton_y->set_has_frame(x);
119                 spinbutton_x->set_size_request(48,-1);
120                 spinbutton_y->set_size_request(48,-1);
121         }
122
123         distance_x->set_has_frame(x);
124         distance_y->set_has_frame(x);
125         distance_x->set_size_request(48,-1);
126         distance_y->set_size_request(48,-1);
127 }
128
129 void
130 Widget_Vector::set_digits(int x)
131 {
132         if(spinbutton_x)
133         {
134                 spinbutton_x->set_digits(x);
135                 spinbutton_y->set_digits(x);
136                 spinbutton_x->set_size_request(48,-1);
137                 spinbutton_y->set_size_request(48,-1);
138         }
139         
140         distance_x->set_digits(x);
141         distance_y->set_digits(x);
142         distance_x->set_size_request(48,-1);
143         distance_y->set_size_request(48,-1);
144 }
145
146 void
147 Widget_Vector::set_value(const sinfg::Vector &data)
148 {
149         vector=data;
150
151         if(canvas_){try
152         {
153                 Distance distx(vector[0],Distance::SYSTEM_UNITS),disty(vector[1],Distance::SYSTEM_UNITS);
154                 distx.convert(App::distance_system,canvas_->rend_desc());
155                 disty.convert(App::distance_system,canvas_->rend_desc());
156                 distance_x->set_value(distx);
157                 distance_y->set_value(disty);
158                 spinbutton_x->hide();
159                 spinbutton_y->hide();
160         }catch(...) { sinfg::error("Widget_Vector::set_value(): Caught something that was thrown"); }}
161         else
162         {
163                 spinbutton_x->set_value(vector[0]);
164                 spinbutton_y->set_value(vector[1]);
165                 distance_x->hide();
166                 distance_y->hide();
167         }
168 }
169
170 const sinfg::Vector &
171 Widget_Vector::get_value()
172 {
173         if(!canvas_ && spinbutton_x)
174         {
175                 vector[0]=spinbutton_x->get_value();
176                 vector[1]=spinbutton_y->get_value();
177                 distance_x->hide();
178                 distance_y->hide();
179         }
180         else try
181         {
182                 vector[0]=distance_x->get_value().units(canvas_->rend_desc());
183                 vector[1]=distance_y->get_value().units(canvas_->rend_desc());
184                 spinbutton_x->hide();
185                 spinbutton_y->hide();
186         }catch(...) { sinfg::error("Widget_Vector::set_value(): Caught something that was thrown"); }
187         return vector;
188 }
189
190 void
191 Widget_Vector::on_value_changed()
192 {
193         signal_value_changed()();
194 }
195
196 void
197 Widget_Vector::set_canvas(Canvas::LooseHandle x)
198 {
199         canvas_=x;
200         if(x)
201         {
202                 if(spinbutton_x)
203                 {
204                         spinbutton_x->hide();
205                         spinbutton_y->hide();
206 //                      delete spinbutton_x;
207 //                      delete spinbutton_y;
208                 }
209                 distance_x->show();
210                 distance_y->show();
211         }
212         else
213         {
214                 if(spinbutton_x)
215                 {
216                         spinbutton_x->show();
217                         spinbutton_y->show();
218                 }
219                 distance_x->hide();
220                 distance_y->hide();
221         }
222 }
223
224 void
225 Widget_Vector::show_all_vfunc()
226 {
227         if(canvas_)
228         {
229                 distance_x->show();
230                 distance_y->show();
231         }
232         else
233         {
234                 spinbutton_x->show();
235                 spinbutton_y->show();
236         }
237         show();
238 }