Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / src / gtkmm / widget_time.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_time.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/entry.h>
33 #include <gtkmm/button.h>
34 #include "widget_time.h"
35 #include "app.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 //using namespace etl;
43 using namespace synfig;
44 using namespace studio;
45
46 /* === M A C R O S ========================================================= */
47
48 #if ! defined(_)
49 #define _(x)    (x)
50 #endif
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 Widget_Time::Widget_Time():
59         fps_(0),
60         time_(0)
61 {
62         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_value));
63         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_text));
64 }
65
66 Widget_Time::~Widget_Time()
67 {
68 }
69
70 void
71 Widget_Time::refresh_text()
72 {
73         set_text(time_.get_string(fps_,App::get_time_format()));
74 }
75
76
77 void
78 Widget_Time::set_value(const synfig::Time &data)
79 {
80         time_=data;
81         refresh_text();
82 }
83
84 synfig::Time
85 Widget_Time::get_value() const
86 {
87         return time_;
88 }
89
90 void
91 Widget_Time::set_fps(float x)
92 {
93         fps_=Time(x);
94         refresh_text();
95 }
96
97 void
98 Widget_Time::refresh_value()
99 {
100         try
101         {
102                 Time newtime(get_text(),fps_);
103                 if(abs(newtime-time_)>=0.001)
104                 {
105                         time_=newtime;
106                         refresh_text();
107                         signal_value_changed()();
108                 }
109         }
110         catch(...)
111         {
112                 throw string("Caught unknown exception");
113         }
114 }
115
116 bool
117 Widget_Time::on_event(GdkEvent* event)
118 {
119         const Time scroll_amount(0.25);
120
121         switch(event->type)
122         {
123         case GDK_SCROLL:
124                 if(event->scroll.direction==GDK_SCROLL_DOWN)
125                 {
126                         time_-=scroll_amount;
127                         refresh_text();
128                         signal_value_changed()();
129                 }
130                 else if(event->scroll.direction==GDK_SCROLL_UP)
131                 {
132                         time_+=scroll_amount;
133                         refresh_text();
134                         signal_value_changed()();
135                 }
136                 return true;
137                 break;
138         default:
139                 break;
140         }
141
142         return Gtk::Entry::on_event(event);
143 }
144
145 bool
146 Widget_Time::on_focus_out_event(GdkEventFocus* event)
147 {
148         refresh_value();
149         refresh_text();
150         return Gtk::Entry::on_focus_out_event(event);
151 }
152
153 bool
154 Widget_Time::on_focus_in_event(GdkEventFocus* event)
155 {
156         set_text(time_.get_string(fps_,App::get_time_format()|Time::FORMAT_FULL));
157         return Gtk::Entry::on_focus_in_event(event);
158 }