source: synfigstudio/trunk/fuentes/src/gui/widgets/widget_vector.cpp @ 481

Last change on this file since 481 was 481, checked in by jrpelegrina, 4 years ago

First release to xenial

File size: 8.5 KB
Line 
1/* === S Y N F I G ========================================================= */
2/*!     \file widget_vector.cpp
3**      \brief Template File
4**
5**      $Id$
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 "widgets/widget_vector.h"
34#include "widgets/widget_distance.h"
35#include "app.h"
36
37#include "general.h"
38
39#endif
40
41/* === U S I N G =========================================================== */
42
43using namespace std;
44using namespace synfig;
45using namespace studio;
46
47/* === M A C R O S ========================================================= */
48
49#define DIGITS          10
50
51/* === G L O B A L S ======================================================= */
52
53/* === P R O C E D U R E S ================================================= */
54
55/* === M E T H O D S ======================================================= */
56
57Widget_Vector::Widget_Vector():
58        Gtk::HBox(false, 5),
59        x_adjustment(Gtk::Adjustment::create(0,-100000000,100000000,0.05,0.05,0)),
60        y_adjustment(Gtk::Adjustment::create(0,-100000000,100000000,0.05,0.05,0))
61{
62        Gtk::Label *label;
63        int width_chars = 5;
64
65        label=manage(new class Gtk::Label("X:"));
66        label->set_alignment(0, 0.5);
67        //pack_start(*label, Gtk::PACK_SHRINK);
68       
69        entry_x=manage(new class Gtk::Entry());
70        entry_x->set_width_chars(width_chars);
71        entry_x->signal_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_entry_x_changed));
72        pack_start(*entry_x, Gtk::PACK_EXPAND_WIDGET);
73        entry_x->show();
74
75        spinbutton_x=manage(new class Gtk::SpinButton(x_adjustment,0.05,DIGITS));
76        spinbutton_x->set_alignment(1);
77        spinbutton_x->set_update_policy(Gtk::UPDATE_ALWAYS);
78        spinbutton_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
79        pack_start(*spinbutton_x, Gtk::PACK_EXPAND_WIDGET);
80
81        distance_x=manage(new Widget_Distance());
82        distance_x->set_digits(4);
83        distance_x->set_update_policy(Gtk::UPDATE_ALWAYS);
84        distance_x->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
85        pack_start(*distance_x, Gtk::PACK_EXPAND_WIDGET);
86
87        label=manage(new class Gtk::Label("Y:"));
88        label->set_alignment(0, 0.5);
89        label->show();
90        //pack_start(*label, Gtk::PACK_SHRINK);
91       
92        entry_y=manage(new class Gtk::Entry());
93        entry_y->set_width_chars(width_chars);
94        entry_y->signal_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_entry_y_changed));
95        pack_start(*entry_y, Gtk::PACK_EXPAND_WIDGET);
96        entry_y->show();
97
98        spinbutton_y=manage(new class Gtk::SpinButton(y_adjustment,0.05,DIGITS));
99        spinbutton_y->set_alignment(1);
100        spinbutton_y->set_update_policy(Gtk::UPDATE_ALWAYS);
101        spinbutton_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
102        spinbutton_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
103        pack_start(*spinbutton_y, Gtk::PACK_EXPAND_WIDGET);
104
105        distance_y=manage(new Widget_Distance());
106        distance_y->set_digits(4);
107        distance_y->set_update_policy(Gtk::UPDATE_ALWAYS);
108        distance_y->signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_Vector::on_value_changed));
109        //distance_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
110        pack_start(*distance_y, Gtk::PACK_EXPAND_WIDGET);
111
112        //spinbutton_x->show();
113        //spinbutton_y->show();
114
115        //spinbutton_x->signal_activate().connect(sigc::mem_fun(*spinbutton_y,&Gtk::SpinButton::grab_focus));
116        //distance_x->signal_activate().connect(sigc::mem_fun(*distance_y,&Gtk::SpinButton::grab_focus));
117        entry_x->signal_activate().connect(sigc::mem_fun(*entry_y,&Gtk::SpinButton::grab_focus));
118        entry_y->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Vector::activate));
119}
120
121Widget_Vector::~Widget_Vector()
122{
123}
124
125void
126Widget_Vector::on_grab_focus()
127{
128        //if(canvas_)
129        //      distance_x->grab_focus();
130        //else
131        //      spinbutton_x->grab_focus();
132        entry_x->grab_focus();
133}
134
135void
136Widget_Vector::set_has_frame(bool x)
137{
138        if(spinbutton_x)
139        {
140                spinbutton_x->set_has_frame(x);
141                spinbutton_y->set_has_frame(x);
142                spinbutton_x->set_size_request(48,-1);
143                spinbutton_y->set_size_request(48,-1);
144        }
145
146        distance_x->set_has_frame(x);
147        distance_y->set_has_frame(x);
148        distance_x->set_size_request(48,-1);
149        distance_y->set_size_request(48,-1);
150       
151        entry_x->set_has_frame(x);
152        entry_y->set_has_frame(x);
153}
154
155void
156Widget_Vector::set_digits(int x)
157{
158        if(spinbutton_x)
159        {
160                spinbutton_x->set_digits(x);
161                spinbutton_y->set_digits(x);
162                spinbutton_x->set_size_request(48,-1);
163                spinbutton_y->set_size_request(48,-1);
164        }
165
166        distance_x->set_digits(x);
167        distance_y->set_digits(x);
168        distance_x->set_size_request(48,-1);
169        distance_y->set_size_request(48,-1);
170}
171
172void
173Widget_Vector::set_value(const synfig::Vector &data)
174{
175        vector=data;
176
177        if(canvas_){try
178        {
179                Distance distx(vector[0],Distance::SYSTEM_UNITS),disty(vector[1],Distance::SYSTEM_UNITS);
180                distx.convert(App::distance_system,canvas_->rend_desc());
181                disty.convert(App::distance_system,canvas_->rend_desc());
182                distance_x->set_value(distx);
183                distance_y->set_value(disty);
184                entry_x->set_text(distance_x->get_value().get_string(4));
185                entry_y->set_text(distance_y->get_value().get_string(4));
186                //spinbutton_x->hide();
187                //spinbutton_y->hide();
188        }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }}
189        else
190        {
191                spinbutton_x->set_value(vector[0]);
192                spinbutton_y->set_value(vector[1]);
193               
194                String str;
195                std::ostringstream sstream_x;
196                sstream_x << spinbutton_x->get_value();
197                str=sstream_x.str();
198                while (*str.rbegin() == '0' && str.length() > 1)
199                        str=str.substr(0, str.size()-1);
200                entry_x->set_text(str);
201                std::ostringstream sstream_y;
202                sstream_y << spinbutton_y->get_value();
203                str=sstream_y.str();
204                while (*str.rbegin() == '0' && str.length() > 1)
205                        str=str.substr(0, str.size()-1);
206                entry_y->set_text(str);
207               
208                //distance_x->hide();
209                //distance_y->hide();
210        }
211}
212
213const synfig::Vector &
214Widget_Vector::get_value()
215{
216        if(!canvas_ && spinbutton_x)
217        {
218                vector[0]=spinbutton_x->get_value();
219                vector[1]=spinbutton_y->get_value();
220                //distance_x->hide();
221                //distance_y->hide();
222        }
223        else try
224        {
225                vector[0]=distance_x->get_value().units(canvas_->rend_desc());
226                vector[1]=distance_y->get_value().units(canvas_->rend_desc());
227                //spinbutton_x->hide();
228                //spinbutton_y->hide();
229        }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }
230        return vector;
231}
232
233void
234Widget_Vector::on_entry_x_changed()
235{
236        if(canvas_) {
237                Distance distx(0,Distance::SYSTEM_UNITS);
238                distx.convert(App::distance_system,canvas_->rend_desc());
239                distx = synfig::String(entry_x->get_text());
240                distance_x->set_value(distx);
241        }
242        else try 
243        {
244                spinbutton_x->set_value(atof(entry_x->get_text().c_str()));
245        }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }
246}
247
248void
249Widget_Vector::on_entry_y_changed()
250{
251        if(canvas_) {
252                Distance disty(0,Distance::SYSTEM_UNITS);
253                disty.convert(App::distance_system,canvas_->rend_desc());
254                disty = synfig::String(entry_y->get_text());
255                distance_y->set_value(disty);
256        }
257        else try 
258        {
259                spinbutton_y->set_value(atof(entry_y->get_text().c_str()));
260        }catch(...) { synfig::error("Widget_Vector::set_value(): Caught something that was thrown"); }
261}
262
263void
264Widget_Vector::on_value_changed()
265{       
266        signal_value_changed()();
267}
268
269void
270Widget_Vector::set_canvas(synfig::Canvas::LooseHandle x)
271{
272        canvas_=x;
273        /*
274        if(x)
275        {
276                if(spinbutton_x)
277                {
278                        spinbutton_x->hide();
279                        spinbutton_y->hide();
280                }
281                distance_x->show();
282                distance_y->show();
283        }
284        else
285        {
286                if(spinbutton_x)
287                {
288                        spinbutton_x->show();
289                        spinbutton_y->show();
290                }
291                distance_x->hide();
292                distance_y->hide();
293        }
294        */
295}
296
297void
298Widget_Vector::show_all_vfunc()
299{
300        /*
301        if(canvas_)
302        {
303                distance_x->show();
304                distance_y->show();
305        }
306        else
307        {
308                spinbutton_x->show();
309                spinbutton_y->show();
310        }
311        */
312        entry_x->show();
313        entry_y->show();
314        show();
315}
Note: See TracBrowser for help on using the repository browser.