initial version
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_time.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file widget_time.cpp
3 **      \brief Template File
4 **
5 **      $Id: widget_time.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <gtkmm/entry.h>
32 #include <gtkmm/button.h>
33 #include "widget_time.h"
34 #include "app.h"
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 //using namespace etl;
42 using namespace sinfg;
43 using namespace studio;
44
45 /* === M A C R O S ========================================================= */
46
47 #if ! defined(_)
48 #define _(x)    (x)
49 #endif
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 sinfg::Time &data)
78 {
79         time_=data;
80         refresh_text();
81 }
82
83 sinfg::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         default:
138                 break;
139         }
140         
141         return Gtk::Entry::on_event(event);
142 }
143
144 bool
145 Widget_Time::on_focus_out_event(GdkEventFocus* event)
146 {
147         refresh_value();
148         refresh_text();         
149         return Gtk::Entry::on_focus_out_event(event);
150 }
151
152 bool
153 Widget_Time::on_focus_in_event(GdkEventFocus* event)
154 {
155         set_text(time_.get_string(fps_,App::get_time_format()|Time::FORMAT_FULL));
156         return Gtk::Entry::on_focus_in_event(event);
157 }