Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / stable / 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 **      Copyright (c) 2008 Chris Moore
10 **  Copyright (c) 2008 Paul Wise
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <gtkmm/entry.h>
35 #include <gtkmm/button.h>
36 #include "widget_time.h"
37 #include "app.h"
38
39 #include "general.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 //using namespace etl;
47 using namespace synfig;
48 using namespace studio;
49
50 /* === M A C R O S ========================================================= */
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 || event->scroll.direction==GDK_SCROLL_LEFT)
125                 {
126                         time_-=scroll_amount;
127                         refresh_text();
128                         signal_value_changed()();
129                 }
130                 else if(event->scroll.direction==GDK_SCROLL_UP || event->scroll.direction==GDK_SCROLL_RIGHT)
131                 {
132                         time_+=scroll_amount;
133                         refresh_text();
134                         signal_value_changed()();
135                 }
136                 return true;
137                 break;
138         case GDK_BUTTON_PRESS:
139         case GDK_2BUTTON_PRESS:
140         case GDK_3BUTTON_PRESS:
141                 if (!has_focus())
142                         grab_focus();
143                 break;
144         default:
145                 break;
146         }
147
148         return Gtk::Entry::on_event(event);
149 }
150
151 bool
152 Widget_Time::on_focus_out_event(GdkEventFocus* event)
153 {
154         refresh_value();
155         refresh_text();
156         return Gtk::Entry::on_focus_out_event(event);
157 }
158
159 bool
160 Widget_Time::on_focus_in_event(GdkEventFocus* event)
161 {
162         // if defined, show the full time format "0h 0m 5s 0f" when the time widget gets focus
163         if (getenv("SYNFIG_SHOW_FULL_TIME_ON_FOCUS"))
164                 set_text(time_.get_string(fps_,App::get_time_format()|Time::FORMAT_FULL));
165
166         return Gtk::Entry::on_focus_in_event(event);
167 }