Added copyright lines for files I've edited this year.
[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 **      Copyright (c) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/entry.h>
34 #include <gtkmm/button.h>
35 #include "widget_time.h"
36 #include "app.h"
37
38 #include "general.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 //using namespace etl;
46 using namespace synfig;
47 using namespace studio;
48
49 /* === M A C R O S ========================================================= */
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
57 Widget_Time::Widget_Time():
58         fps_(0),
59         time_(0)
60 {
61         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_value));
62         signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_Time::refresh_text));
63 }
64
65 Widget_Time::~Widget_Time()
66 {
67 }
68
69 void
70 Widget_Time::refresh_text()
71 {
72         set_text(time_.get_string(fps_,App::get_time_format()));
73 }
74
75
76 void
77 Widget_Time::set_value(const synfig::Time &data)
78 {
79         time_=data;
80         refresh_text();
81 }
82
83 synfig::Time
84 Widget_Time::get_value() const
85 {
86         return time_;
87 }
88
89 void
90 Widget_Time::set_fps(float x)
91 {
92         fps_=Time(x);
93         refresh_text();
94 }
95
96 void
97 Widget_Time::refresh_value()
98 {
99         try
100         {
101                 Time newtime(get_text(),fps_);
102                 if(abs(newtime-time_)>=0.001)
103                 {
104                         time_=newtime;
105                         refresh_text();
106                         signal_value_changed()();
107                 }
108         }
109         catch(...)
110         {
111                 throw string("Caught unknown exception");
112         }
113 }
114
115 bool
116 Widget_Time::on_event(GdkEvent* event)
117 {
118         const Time scroll_amount(0.25);
119
120         switch(event->type)
121         {
122         case GDK_SCROLL:
123                 if(event->scroll.direction==GDK_SCROLL_DOWN)
124                 {
125                         time_-=scroll_amount;
126                         refresh_text();
127                         signal_value_changed()();
128                 }
129                 else if(event->scroll.direction==GDK_SCROLL_UP)
130                 {
131                         time_+=scroll_amount;
132                         refresh_text();
133                         signal_value_changed()();
134                 }
135                 return true;
136                 break;
137         case GDK_BUTTON_PRESS:
138         case GDK_2BUTTON_PRESS:
139         case GDK_3BUTTON_PRESS:
140                 if (!has_focus())
141                         grab_focus();
142                 break;
143         default:
144                 break;
145         }
146
147         return Gtk::Entry::on_event(event);
148 }
149
150 bool
151 Widget_Time::on_focus_out_event(GdkEventFocus* event)
152 {
153         refresh_value();
154         refresh_text();
155         return Gtk::Entry::on_focus_out_event(event);
156 }
157
158 bool
159 Widget_Time::on_focus_in_event(GdkEventFocus* event)
160 {
161         // if defined, show the full time format "0h 0m 5s 0f" when the time widget gets focus
162         if (getenv("SYNFIG_SHOW_FULL_TIME_ON_FOCUS"))
163                 set_text(time_.get_string(fps_,App::get_time_format()|Time::FORMAT_FULL));
164
165         return Gtk::Entry::on_focus_in_event(event);
166 }