Extract translatable strings from some more files
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_curves.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_curves.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 "widget_curves.h"
33 #include <cmath>
34 #include "app.h"
35 #include <gtkmm/drawingarea.h>
36 #include <map>
37 #include <vector>
38 #include <ETL/misc>
39 #include <sigc++/object.h>
40
41 #include "general.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 /*
59 void
60 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
61 {
62         const int height(ca.get_height());
63         const int width(ca.get_width());
64
65         const int square_size(height/2);
66
67         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
68
69         if(color.get_alpha()!=1.0)
70         {
71                 // In this case we need to render the alpha squares
72
73                 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
74                 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
75
76                 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
77                 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
78
79                 bool toggle(false);
80                 for(int i=0;i<width;i+=square_size)
81                 {
82                         const int square_width(min(square_size,width-i));
83
84                         if(toggle)
85                         {
86                                 gc->set_rgb_fg_color(gdk_c1);
87                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
88
89                                 gc->set_rgb_fg_color(gdk_c2);
90                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
91                                 toggle=false;
92                         }
93                         else
94                         {
95                                 gc->set_rgb_fg_color(gdk_c2);
96                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
97
98                                 gc->set_rgb_fg_color(gdk_c1);
99                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
100                                 toggle=true;
101                         }
102                 }
103         }
104         else
105         {
106                 // In this case we have a solid color to use
107                 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
108
109                 gc->set_rgb_fg_color(gdk_c1);
110                 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
111         }
112         gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
113         window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
114         gc->set_rgb_fg_color(Gdk::Color("#000000"));
115         window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
116 }
117 */
118
119 /* === C L A S S E S ======================================================= */
120
121
122
123 struct studio::Widget_Curves::Channel
124 {
125         synfig::String name;
126         Gdk::Color color;
127         std::map<synfig::Real,synfig::Real> values;
128 };
129
130 struct studio::Widget_Curves::CurveStruct : sigc::trackable
131 {
132         synfigapp::ValueDesc value_desc;
133         std::vector<Channel> channels;
134
135         CurveStruct(const synfigapp::ValueDesc& x):
136                 value_desc(x)
137         {
138                 switch(value_desc.get_value_type())
139                 {
140                         case ValueBase::TYPE_REAL:
141                                 channels.push_back(Channel());
142                                 channels.back().name="real";
143                                 channels.back().color=Gdk::Color("#007f7f");
144                                 break;
145                         case ValueBase::TYPE_TIME:
146                                 channels.push_back(Channel());
147                                 channels.back().name="time";
148                                 channels.back().color=Gdk::Color("#7f7f00");
149                                 break;
150                         case ValueBase::TYPE_INTEGER:
151                                 channels.push_back(Channel());
152                                 channels.back().name="int";
153                                 channels.back().color=Gdk::Color("#7f0000");
154                                 break;
155                         case ValueBase::TYPE_BOOL:
156                                 channels.push_back(Channel());
157                                 channels.back().name="bool";
158                                 channels.back().color=Gdk::Color("#ff7f00");
159                                 break;
160                         case ValueBase::TYPE_ANGLE:
161                                 channels.push_back(Channel());
162                                 channels.back().name="theta";
163                                 channels.back().color=Gdk::Color("#004f4f");
164                                 break;
165                         case ValueBase::TYPE_COLOR:
166                                 channels.push_back(Channel());
167                                 channels.back().name="red";
168                                 channels.back().color=Gdk::Color("#7f0000");
169                                 channels.push_back(Channel());
170                                 channels.back().name="green";
171                                 channels.back().color=Gdk::Color("#007f00");
172                                 channels.push_back(Channel());
173                                 channels.back().name="blue";
174                                 channels.back().color=Gdk::Color("#00007f");
175                                 channels.push_back(Channel());
176                                 channels.back().name="alpha";
177                                 channels.back().color=Gdk::Color("#000000");
178                                 break;
179                         case ValueBase::TYPE_VECTOR:
180                                 channels.push_back(Channel());
181                                 channels.back().name="x";
182                                 channels.back().color=Gdk::Color("#7f007f");
183                                 channels.push_back(Channel());
184                                 channels.back().name="y";
185                                 channels.back().color=Gdk::Color("#007f7f");
186                                 break;
187                         case ValueBase::TYPE_BLINEPOINT:
188                                 channels.push_back(Channel());
189                                 channels.back().name="v.x";
190                                 channels.back().color=Gdk::Color("#ff7f00");
191                                 channels.push_back(Channel());
192                                 channels.back().name="v.y";
193                                 channels.back().color=Gdk::Color("#7f3f00");
194
195                                 channels.push_back(Channel());
196                                 channels.back().name="width";
197                                 channels.back().color=Gdk::Color("#000000");
198
199                                 channels.push_back(Channel());
200                                 channels.back().name="origin";
201                                 channels.back().color=Gdk::Color("#ffffff");
202
203                                 channels.push_back(Channel());
204                                 channels.back().name="tsplit";
205                                 channels.back().color=Gdk::Color("#ff00ff");
206
207                                 channels.push_back(Channel());
208                                 channels.back().name="t1.x";
209                                 channels.back().color=Gdk::Color("#ff0000");
210                                 channels.push_back(Channel());
211                                 channels.back().name="t1.y";
212                                 channels.back().color=Gdk::Color("#7f0000");
213
214                                 channels.push_back(Channel());
215                                 channels.back().name="t2.x";
216                                 channels.back().color=Gdk::Color("#ffff00");
217                                 channels.push_back(Channel());
218                                 channels.back().name="t2.y";
219                                 channels.back().color=Gdk::Color("#7f7f00");
220                                 break;
221                         default:
222                                 throw synfig::Exception::BadType("Bad type for curves");
223                 }
224         }
225
226         void clear_all_values()
227         {
228                 std::vector<Channel>::iterator iter;
229                 for(iter=channels.begin();iter!=channels.end();++iter)
230                         iter->values.clear();
231         }
232
233         synfig::Real get_value(int chan, synfig::Real time, synfig::Real tolerance)
234         {
235                 std::map<synfig::Real,synfig::Real>::iterator iter;
236
237                 // First check to see if we have a value
238                 // that is "close enough" to the time
239                 // we are looking for
240                 iter=channels[chan].values.lower_bound(time);
241                 if(iter!=channels[chan].values.end() && iter->first-time<=tolerance)
242                         return -iter->second;
243
244                 // Since that didn't work, we now need
245                 // to go ahead and figure out what the
246                 // actual value is at that time.
247                 ValueBase value(value_desc.get_value(time));
248                 switch(value.get_type())
249                 {
250                         case ValueBase::TYPE_REAL:
251                                 channels[0].values[time]=value.get(Real());
252                                 break;
253                         case ValueBase::TYPE_TIME:
254                                 channels[0].values[time]=value.get(Time());
255                                 break;
256                         case ValueBase::TYPE_INTEGER:
257                                 channels[0].values[time]=value.get(int());
258                                 break;
259                         case ValueBase::TYPE_BOOL:
260                                 channels[0].values[time]=value.get(bool());
261                                 break;
262                         case ValueBase::TYPE_ANGLE:
263                                 channels[0].values[time]=Angle::rad(value.get(Angle())).get();
264                                 break;
265                         case ValueBase::TYPE_COLOR:
266                                 channels[0].values[time]=value.get(Color()).get_r();
267                                 channels[1].values[time]=value.get(Color()).get_g();
268                                 channels[2].values[time]=value.get(Color()).get_b();
269                                 channels[3].values[time]=value.get(Color()).get_a();
270                                 break;
271                         case ValueBase::TYPE_VECTOR:
272                                 channels[0].values[time]=value.get(Vector())[0];
273                                 channels[1].values[time]=value.get(Vector())[1];
274                                 break;
275                         case ValueBase::TYPE_BLINEPOINT:
276                                 channels[0].values[time]=value.get(BLinePoint()).get_vertex()[0];
277                                 channels[1].values[time]=value.get(BLinePoint()).get_vertex()[1];
278                                 channels[2].values[time]=value.get(BLinePoint()).get_width();
279                                 channels[3].values[time]=value.get(BLinePoint()).get_origin();
280                                 channels[4].values[time]=value.get(BLinePoint()).get_split_tangent_flag();
281                                 channels[5].values[time]=value.get(BLinePoint()).get_tangent1()[0];
282                                 channels[6].values[time]=value.get(BLinePoint()).get_tangent1()[1];
283                                 channels[7].values[time]=value.get(BLinePoint()).get_tangent2()[0];
284                                 channels[8].values[time]=value.get(BLinePoint()).get_tangent2()[1];
285                                 break;
286                         default:
287                                 return 0;
288                 }
289
290                 return -channels[chan].values[time];
291         }
292
293         static bool is_supported(const synfigapp::ValueDesc& x)
294         {
295                 return x.get_value_type() == ValueBase::TYPE_STRING
296                         && x.get_value_type() == ValueBase::TYPE_CANVAS;
297         }
298 };
299
300 /* === M E T H O D S ======================================================= */
301
302 Widget_Curves::Widget_Curves():
303         range_adjustment_(new Gtk::Adjustment(-1,-2,2,0.1,0.1,2))
304 {
305         set_size_request(64,64);
306
307         range_adjustment_->signal_changed().connect(
308                 sigc::mem_fun(
309                         *this,
310                         &Widget_Curves::queue_draw
311                 )
312         );
313         range_adjustment_->signal_value_changed().connect(
314                 sigc::mem_fun(
315                         *this,
316                         &Widget_Curves::queue_draw
317                 )
318         );
319         //set_vadjustment(*range_adjustment_);
320
321         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Curves::redraw));
322         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
323
324 }
325
326 Widget_Curves::~Widget_Curves()
327 {
328 }
329
330 void
331 Widget_Curves::set_time_adjustment(Gtk::Adjustment&x)
332 {
333         time_adjustment_=&x;
334         time_adjustment_->signal_changed().connect(
335                 sigc::mem_fun(
336                         *this,
337                         &Widget_Curves::queue_draw
338                 )
339         );
340         time_adjustment_->signal_value_changed().connect(
341                 sigc::mem_fun(
342                         *this,
343                         &Widget_Curves::queue_draw
344                 )
345         );
346         //set_hadjustment(*time_adjustment_);
347 }
348
349 void
350 Widget_Curves::clear()
351 {
352         curve_list_.clear();
353 }
354
355 void
356 Widget_Curves::refresh()
357 {
358         std::list<CurveStruct>::iterator curve_iter;
359         for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
360         {
361                 curve_iter->clear_all_values();
362         }
363         queue_draw();
364 }
365
366 void
367 Widget_Curves::set_value_descs(std::list<synfigapp::ValueDesc> value_descs)
368 {
369         curve_list_.clear();
370
371         std::list<synfigapp::ValueDesc>::iterator iter;
372         for(iter=value_descs.begin();iter!=value_descs.end();++iter)
373         {
374                 if (! CurveStruct::is_supported(*iter))
375                         continue;
376
377                 try {
378                         curve_list_.push_back(*iter);
379                         if(iter->is_value_node())
380                         {
381                                 iter->get_value_node()->signal_changed().connect(
382                                         sigc::mem_fun(
383                                                 *this,
384                                                 &studio::Widget_Curves::refresh
385                                         )
386                                 );
387                         }
388                         if(iter->parent_is_value_node())
389                         {
390                                 iter->get_parent_value_node()->signal_changed().connect(
391                                         sigc::mem_fun(
392                                                 *this,
393                                                 &studio::Widget_Curves::refresh
394                                         )
395                                 );
396                         }
397                         if(iter->parent_is_layer_param())
398                         {
399                                 iter->get_layer()->signal_changed().connect(
400                                         sigc::mem_fun(
401                                                 *this,
402                                                 &studio::Widget_Curves::refresh
403                                         )
404                                 );
405                         }
406                 }catch(synfig::Exception::BadType)
407                 {
408                         continue;
409                 }
410         }
411         queue_draw();
412 }
413
414 bool
415 Widget_Curves::on_event(GdkEvent *event)
416 {
417         switch(event->type)
418         {
419         case GDK_SCROLL:
420                 switch(event->scroll.direction)
421                 {
422                         case GDK_SCROLL_UP:
423                                 range_adjustment_->set_page_size(range_adjustment_->get_page_size()/1.25);
424                                 range_adjustment_->changed();
425                                 break;
426                         case GDK_SCROLL_DOWN:
427                                 range_adjustment_->set_page_size(range_adjustment_->get_page_size()*1.25);
428                                 range_adjustment_->changed();
429                                 break;
430                         default:
431                                 break;
432                 }
433                 break;
434         default:
435                 return Gtk::DrawingArea::on_event(event);
436                 break;
437         }
438
439         return true;
440
441 /*      switch(event->type)
442         {
443         case GDK_BUTTON_PRESS:
444                 if(event->button.button==1)
445                 {
446                         signal_activate_();
447                         return true;
448                 }
449                 if(event->button.button==3)
450                 {
451                         signal_secondary_();
452                         return true;
453                 }
454                 break;
455
456         default:
457                 break;
458         }
459         return false;
460 */
461 }
462
463 bool
464 Widget_Curves::redraw(GdkEventExpose */*bleh*/)
465 {
466         const int h(get_height());
467         const int w(get_width());
468         get_window()->clear();
469
470         if(!time_adjustment_ || !range_adjustment_ || !h || !w)
471                 return false;
472
473         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
474
475         const Real t_begin(time_adjustment_->get_lower());
476         const Real t_end(time_adjustment_->get_upper());
477         const Real dt((t_end-t_begin)/w);
478
479         const Real r_bottom(range_adjustment_->get_value());
480         const Real r_top(r_bottom+range_adjustment_->get_page_size());
481         const Real dr((r_top-r_bottom)/h);
482         Real r_max(-100000000);
483         Real r_min(100000000);
484
485         std::list<CurveStruct>::iterator curve_iter;
486
487         vector<Gdk::Point> points[10];
488
489         gc->set_function(Gdk::COPY);
490         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
491
492         // Draw zero mark
493         gc->set_rgb_fg_color(Gdk::Color("#4f4f4f"));
494         get_window()->draw_rectangle(gc, false, 0, round_to_int((0-r_bottom)/dr), w, 0);
495
496         // Draw current time
497         gc->set_rgb_fg_color(Gdk::Color("#00007f"));
498         get_window()->draw_rectangle(gc, false, round_to_int((time_adjustment_->get_value()-t_begin)/dt), 0, 0, h);
499
500         for(curve_iter=curve_list_.begin();curve_iter!=curve_list_.end();++curve_iter)
501         {
502                 Real t;
503                 int i;
504                 int channels(curve_iter->channels.size());
505                 for(i=0;i<channels;i++)
506                         points[i].clear();
507
508                 for(i=0,t=t_begin;i<w;i++,t+=dt)
509                 {
510                         for(int chan=0;chan<channels;chan++)
511                         {
512                                 Real x(curve_iter->get_value(chan,t,dt));
513                                 r_max=max(r_max,x);
514                                 r_min=min(r_min,x);
515                                 points[chan].push_back(
516                                         Gdk::Point(
517                                                 i,
518                                                 round_to_int(
519                                                         (
520                                                                 x-r_bottom
521                                                         )/dr
522                                                 )
523                                         )
524                                 );
525                         }
526                 }
527
528                 for(int chan=0;chan<channels;chan++)
529                 {
530                         gc->set_rgb_fg_color(curve_iter->channels[chan].color);
531
532                         // Draw the curve
533                         get_window()->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points[chan]));
534
535                         Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_pango_context()));
536
537                         layout->set_text(curve_iter->channels[chan].name);
538                         get_window()->draw_layout(gc, 1, points[chan][0].get_y()+1, layout);
539                 }
540         }
541
542         if(!curve_list_.empty())
543         {
544                 range_adjustment_->set_upper(r_max+range_adjustment_->get_page_size()/2);
545                 range_adjustment_->set_lower(r_min-range_adjustment_->get_page_size()/2);
546         }
547         get_window()->get_update_area();
548
549         return true;
550 }