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