9458f1979bc1fdbedfe19fec287af176809df908
[synfig.git] / synfig-studio / trunk / 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 #include "general.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 //using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 Widget_Time::Widget_Time():
57         fps_(0),
58         time_(0)
59 {
60         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_value));
61         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_text));
62 }
63
64 Widget_Time::~Widget_Time()
65 {
66 }
67
68 void
69 Widget_Time::refresh_text()
70 {
71         set_text(time_.get_string(fps_,App::get_time_format()));
72 }
73
74
75 void
76 Widget_Time::set_value(const synfig::Time &data)
77 {
78         time_=data;
79         refresh_text();
80 }
81
82 synfig::Time
83 Widget_Time::get_value() const
84 {
85         return time_;
86 }
87
88 void
89 Widget_Time::set_fps(float x)
90 {
91         fps_=Time(x);
92         refresh_text();
93 }
94
95 void
96 Widget_Time::refresh_value()
97 {
98         try
99         {
100                 Time newtime(get_text(),fps_);
101                 if(abs(newtime-time_)>=0.001)
102                 {
103                         time_=newtime;
104                         refresh_text();
105                         signal_value_changed()();
106                 }
107         }
108         catch(...)
109         {
110                 throw string("Caught unknown exception");
111         }
112 }
113
114 bool
115 Widget_Time::on_event(GdkEvent* event)
116 {
117         const Time scroll_amount(0.25);
118
119         switch(event->type)
120         {
121         case GDK_SCROLL:
122                 if(event->scroll.direction==GDK_SCROLL_DOWN)
123                 {
124                         time_-=scroll_amount;
125                         refresh_text();
126                         signal_value_changed()();
127                 }
128                 else if(event->scroll.direction==GDK_SCROLL_UP)
129                 {
130                         time_+=scroll_amount;
131                         refresh_text();
132                         signal_value_changed()();
133                 }
134                 return true;
135                 break;
136         case GDK_BUTTON_PRESS:
137         case GDK_2BUTTON_PRESS:
138         case GDK_3BUTTON_PRESS:
139                 if (!has_focus())
140                         grab_focus();
141                 break;
142         default:
143                 break;
144         }
145
146         return Gtk::Entry::on_event(event);
147 }
148
149 bool
150 Widget_Time::on_focus_out_event(GdkEventFocus* event)
151 {
152         refresh_value();
153         refresh_text();
154         return Gtk::Entry::on_focus_out_event(event);
155 }
156
157 bool
158 Widget_Time::on_focus_in_event(GdkEventFocus* event)
159 {
160         // if defined, show the full time format "0h 0m 5s 0f" when the time widget gets focus
161         if (getenv("SYNFIG_SHOW_FULL_TIME_ON_FOCUS"))
162                 set_text(time_.get_string(fps_,App::get_time_format()|Time::FORMAT_FULL));
163
164         return Gtk::Entry::on_focus_in_event(event);
165 }