Initial trial of keyframe list. It doesn't compile.
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_keyframe_list.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_keyframe_list.cpp
3 **      \brief A custom widget to manage keyframes in the timeline.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **      Copyright (c) 2009 Carlos López
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 "widget_keyframe_list.h"
35 #include "app.h"
36 #include <gtkmm/menu.h>
37 #include <synfig/exception.h>
38 #include <ETL/misc>
39
40 #include "general.h"
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace studio;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 Widget_Keyframe_List::Widget_Keyframe_List():
60         editable_(false)
61 {
62         set_size_request(-1,64);
63         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Keyframe_List::redraw));
64         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
65         add_events(Gdk::BUTTON1_MOTION_MASK);
66
67 }
68
69 Widget_Keyframe_List::~Widget_Keyframe_List()
70 {
71 }
72
73 bool
74 Widget_Gradient::redraw(GdkEventExpose */*bleh*/)
75 {
76         const int h(get_height());
77         const int w(get_width());
78
79         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
80         Gdk::Rectangle area(0,0,w,h);
81         if(!editable_)
82         {
83                 return true;
84         }
85
86         gc->set_rgb_fg_color(Gdk::Color("#7f7f7f"));
87         get_window()->draw_rectangle(gc, false, 0, 0, w, h);
88
89         KeyframeList::iterator iter,selected_iter;
90         bool show_selected(false);
91         for(iter=kf_list_.begin();iter!=kf_list_.end();iter++)
92         {
93                 if(*iter!=selected_kf)
94                 get_style()->paint_arrow(
95                         get_window(),
96                         (*iter==selected_kf)?Gtk::STATE_SELECTED:Gtk::STATE_ACTIVE,
97                         Gtk::SHADOW_OUT,
98                         area,
99                         *this,
100                         " ",
101                         Gtk::ARROW_DOWN,
102                         1,
103                         int(iter->get_time()*w)-h/2+1, /// to be fixed
104                         0,
105                         h,
106                         h
107                 );
108                 else
109                 {
110                         selected_iter=iter;
111                         show_selected=true;
112                 }
113         }
114
115         // we do this so that we can be sure that
116         // the selected marker is shown on top
117         if(show_selected)
118         {
119                 get_style()->paint_arrow(
120                         get_window(),
121                         Gtk::STATE_SELECTED,
122                         Gtk::SHADOW_OUT,
123                         area,
124                         *this,
125                         " ",
126                         Gtk::ARROW_DOWN,
127                         1,
128                         round_to_int(selected_iter->get_time()*w)-h/2+1,
129                         0,
130                         h,
131                         h
132                 );
133         }
134
135         return true;
136 }
137
138
139 void
140 Widget_Keyframe_List::popup_menu(Time /*t*/)
141 {
142 /*      Gtk::Menu* menu(manage(new Gtk::Menu()));
143         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
144
145         menu->items().clear();
146
147         menu->items().push_back(
148                 Gtk::Menu_Helpers::MenuElem(
149                         _("Insert CPoint"),
150                         sigc::bind(
151                                 sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
152                                 x
153                         )
154                 )
155         );
156
157         if(!gradient_.empty())
158         {
159                 menu->items().push_back(
160                         Gtk::Menu_Helpers::MenuElem(
161                                 _("Remove CPoint"),
162                                 sigc::bind(
163                                         sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
164                                         x
165                                 )
166                         )
167                 );
168         }
169
170         menu->popup(0,0);
171 */}
172
173 void
174 Widget_Keyframe_List::set_value(const synfig::KeyframeList& x)
175 {
176         kf_list_=x;
177         if(kf_list_.size())
178                 set_selected_keyframe(*kf_list_.find_next(synfig::Time::zero()));
179         queue_draw();
180 }
181
182 void
183 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
184 {
185         selected_kf=x;
186         signal_keyframe_selected_(selected_kf);
187         queue_draw();
188 }
189
190 void
191 Widget_Keyframe_List::update_keyframe(const synfig::Keyframe &x)
192 {
193         try
194         {
195                 KeyframeList::iterator iter(keyframe_list_.find(x));
196                 iter->pos=x.pos;///////7
197                 iter->color=x.color;/////////
198                 gradient_.sort();/////////
199                 queue_draw();
200         }
201         catch(synfig::Exception::NotFound)
202         {
203                 // Yotta...
204         }
205 }
206
207 bool
208 Widget_Keyframe_List::on_event(GdkEvent *event)
209 {
210         //if(editable_)
211         {
212                 const int x(static_cast<int>(event->button.x));
213                 const int y(static_cast<int>(event->button.y));
214
215                 float pos((float)x/(float)get_width());
216                 if(pos<0.0f)pos=0.0f;////////
217                 if(pos>1.0f)pos=1.0f;////////
218
219                 switch(event->type)
220                 {
221                 case GDK_MOTION_NOTIFY:
222                         if(editable_ && y>get_height()-h)
223                         {
224                                 if(!keyframe_list_.size()) return true;
225                                 synfig::KeyframeList::iterator iter(keyframe_list_.find(selected_kf.get_guid()));
226                                 iter->pos=pos;/////
227                                 gradient_.sort();
228
229 //                              signal_value_changed_();
230                                 changed_=true;
231                                 queue_draw();
232                                 return true;
233                         }
234                         break;
235                 case GDK_BUTTON_PRESS:
236                         changed_=false;
237                         if(event->button.button==1)
238                         {
239                                 if(editable_ && y>get_height()-CONTROL_HEIGHT)
240                                 {
241                                         set_selected_cpoint(*gradient_.proximity(pos));
242                                         queue_draw();
243                                         return true;
244                                 }
245                                 else
246                                 {
247                                         signal_clicked_();
248                                         return true;
249                                 }
250                         }
251                         else if(editable_ && event->button.button==3)
252                         {
253                                 popup_menu(pos);
254                                 return true;
255                         }
256                         break;
257                 case GDK_BUTTON_RELEASE:
258                         if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
259                         {
260                                 set_selected_cpoint(*gradient_.proximity(pos));
261                                 if(changed_)signal_value_changed_();
262                                 return true;
263                         }
264                 default:
265                         break;
266                 }
267         }
268
269         return false;
270 }