Improve the range of different scales used on the timeslider. Previously 3 fps anima...
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_timeslider.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_timeslider.cpp
3 **      \brief Time Slider Widget Implementation File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2004 Adrian Bentley
9 **      Copyright (c) 2007 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 "widget_timeslider.h"
34
35 #include <ETL/misc>
36
37 #include <cmath>
38
39 #include "general.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 using studio::Widget_Timeslider;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54 const double zoominfactor = 0.75;
55 const double zoomoutfactor = 1/zoominfactor;
56
57 /* === P R O C E D U R E S ================================================= */
58
59 Gdk::Color get_interp_color(synfig::Interpolation x)
60 {
61         switch(x)
62         {
63         case INTERPOLATION_TCB:
64                 return Gdk::Color("#00B000");
65
66                 break;
67
68         case INTERPOLATION_LINEAR:
69                 return Gdk::Color("#B0B000");
70                 break;
71
72         case INTERPOLATION_CONSTANT:
73                 return Gdk::Color("#C70000");
74                 break;
75
76         case INTERPOLATION_HALT:
77                 return Gdk::Color("#00b0b0");
78                 break;
79
80         case INTERPOLATION_MANUAL:
81                 return Gdk::Color("#B000B0");
82                 break;
83
84         case INTERPOLATION_UNDEFINED: default:
85                 return Gdk::Color("#808080");
86                 break;
87         }
88 }
89
90 static Gdk::Color
91 color_darken(Gdk::Color x, float amount)
92 {
93         double   red = x.get_red_p()   * amount;
94         double green = x.get_green_p() * amount;
95         double  blue = x.get_blue_p()  * amount;
96
97         x.set_rgb_p(  red > 1 ? 1 : red,
98                                 green > 1 ? 1 : green,
99                                  blue > 1 ? 1 : blue);
100
101         return x;
102 }
103
104 void
105 studio::render_time_point_to_window(
106         const Glib::RefPtr<Gdk::Drawable>& window,
107         const Gdk::Rectangle& area,
108         const synfig::TimePoint &tp,
109         bool selected
110 )
111 {
112         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
113         const Gdk::Color black("#000000");
114
115         if(selected)
116                 gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
117         else
118                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
119
120         Gdk::Color color;
121         std::vector<Gdk::Point> points;
122
123 /*-     BEFORE ------------------------------------- */
124
125         color=get_interp_color(tp.get_before());
126         color=color_darken(color,1.0f);
127         if(selected)color=color_darken(color,1.3f);
128         gc->set_rgb_fg_color(color);
129
130         switch(tp.get_before())
131         {
132         case INTERPOLATION_TCB:
133                 window->draw_arc(
134                         gc,
135                         true,
136                         area.get_x(),
137                         area.get_y(),
138                         area.get_width(),
139                         area.get_height(),
140                         64*90,
141                         64*180
142                 );
143                 gc->set_rgb_fg_color(black);
144                 window->draw_arc(
145                         gc,
146                         false,
147                         area.get_x(),
148                         area.get_y(),
149                         area.get_width(),
150                         area.get_height(),
151                         64*90,
152                         64*180
153                 );
154                 break;
155
156         case INTERPOLATION_HALT:
157                 window->draw_arc(
158                         gc,
159                         true,
160                         area.get_x(),
161                         area.get_y(),
162                         area.get_width(),
163                         area.get_height()*2,
164                         64*90,
165                         64*90
166                 );
167                 gc->set_rgb_fg_color(black);
168                 window->draw_arc(
169                         gc,
170                         false,
171                         area.get_x(),
172                         area.get_y(),
173                         area.get_width(),
174                         area.get_height()*2,
175                         64*90,
176                         64*90
177                 );
178                 break;
179
180         case INTERPOLATION_LINEAR:
181                 points.clear();
182                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
183                 points.push_back(Gdk::Point(area.get_x(),area.get_y()+area.get_height()));
184                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
185                 window->draw_polygon(gc,true,points);
186                 gc->set_rgb_fg_color(black);
187                 window->draw_lines(gc,points);
188                 break;
189
190         case INTERPOLATION_CONSTANT:
191                 points.clear();
192                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
193                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/4,area.get_y()));
194                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/4,area.get_y()+area.get_height()/2));
195                 points.push_back(Gdk::Point(area.get_x(),area.get_y()+area.get_height()/2));
196                 points.push_back(Gdk::Point(area.get_x(),area.get_y()+area.get_height()));
197                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
198                 window->draw_polygon(gc,true,points);
199                 gc->set_rgb_fg_color(black);
200                 window->draw_lines(gc,points);
201                 break;
202
203         case INTERPOLATION_UNDEFINED: default:
204                 points.clear();
205                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
206                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/3,area.get_y()));
207                 points.push_back(Gdk::Point(area.get_x(),area.get_y()+area.get_height()/3));
208                 points.push_back(Gdk::Point(area.get_x(),area.get_y()+area.get_height()-area.get_height()/3));
209                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/3,area.get_y()+area.get_height()));
210                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
211                 window->draw_polygon(gc,true,points);
212                 gc->set_rgb_fg_color(black);
213                 window->draw_lines(gc,points);
214                 break;
215         }
216
217 /*-     AFTER -------------------------------------- */
218
219         color=get_interp_color(tp.get_after());
220         color=color_darken(color,0.8f);
221         if(selected)color=color_darken(color,1.3f);
222         gc->set_rgb_fg_color(color);
223
224
225         switch(tp.get_after())
226         {
227         case INTERPOLATION_TCB:
228                 window->draw_arc(
229                         gc,
230                         true,
231                         area.get_x(),
232                         area.get_y(),
233                         area.get_width(),
234                         area.get_height(),
235                         64*270,
236                         64*180
237                 );
238                 gc->set_rgb_fg_color(black);
239                 window->draw_arc(
240                         gc,
241                         false,
242                         area.get_x(),
243                         area.get_y(),
244                         area.get_width(),
245                         area.get_height(),
246                         64*270,
247                         64*180
248                 );
249                 break;
250
251         case INTERPOLATION_HALT:
252                 window->draw_arc(
253                         gc,
254                         true,
255                         area.get_x(),
256                         area.get_y()-area.get_height(),
257                         area.get_width(),
258                         area.get_height()*2,
259                         64*270,
260                         64*90
261                 );
262                 gc->set_rgb_fg_color(black);
263                 window->draw_arc(
264                         gc,
265                         false,
266                         area.get_x(),
267                         area.get_y()-area.get_height(),
268                         area.get_width(),
269                         area.get_height()*2,
270                         64*270,
271                         64*90
272                 );
273                 break;
274
275         case INTERPOLATION_LINEAR:
276                 points.clear();
277                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
278                 points.push_back(Gdk::Point(area.get_x()+area.get_width(),area.get_y()));
279                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
280                 window->draw_polygon(gc,true,points);
281                 gc->set_rgb_fg_color(black);
282                 window->draw_lines(gc,points);
283                 break;
284
285         case INTERPOLATION_CONSTANT:
286                 points.clear();
287                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
288                 points.push_back(Gdk::Point(area.get_x()+area.get_width(),area.get_y()));
289                 points.push_back(Gdk::Point(area.get_x()+area.get_width(),area.get_y()+area.get_height()/2));
290                 points.push_back(Gdk::Point(area.get_x()+area.get_width()-area.get_width()/4,area.get_y()+area.get_height()/2));
291                 points.push_back(Gdk::Point(area.get_x()+area.get_width()-area.get_width()/4,area.get_y()+area.get_height()));
292                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
293                 window->draw_polygon(gc,true,points);
294                 gc->set_rgb_fg_color(black);
295                 window->draw_lines(gc,points);
296                 break;
297
298         case INTERPOLATION_UNDEFINED: default:
299                 points.clear();
300                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()));
301                 points.push_back(Gdk::Point(area.get_x()+area.get_width()-area.get_width()/3,area.get_y()));
302                 points.push_back(Gdk::Point(area.get_x()+area.get_width(),area.get_y()+area.get_height()/3));
303                 points.push_back(Gdk::Point(area.get_x()+area.get_width(),area.get_y()+area.get_height()-area.get_height()/3));
304                 points.push_back(Gdk::Point(area.get_x()+area.get_width()-area.get_width()/3,area.get_y()+area.get_height()));
305                 points.push_back(Gdk::Point(area.get_x()+area.get_width()/2,area.get_y()+area.get_height()));
306                 window->draw_polygon(gc,true,points);
307                 gc->set_rgb_fg_color(black);
308                 window->draw_lines(gc,points);
309                 break;
310         }
311
312 }
313
314 /* === M E T H O D S ======================================================= */
315
316 /* === E N T R Y P O I N T ================================================= */
317 double defaultfps = 24;
318 const int fullheight = 20;
319
320 Widget_Timeslider::Widget_Timeslider()
321 :layout(Pango::Layout::create(get_pango_context())),
322 adj_default(0,0,2,1/defaultfps,10/defaultfps),
323 adj_timescale(0),
324 //invalidated(false),
325 last_event_time(0),
326 fps(defaultfps),
327 dragscroll(false)
328 {
329         set_size_request(-1,fullheight);
330
331         //                click                    scroll                     zoom
332         add_events( Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK
333                                 | Gdk::BUTTON_MOTION_MASK | Gdk::SCROLL_MASK );
334
335         set_time_adjustment(&adj_default);
336         //update_times();
337 }
338
339 Widget_Timeslider::~Widget_Timeslider()
340 {
341 }
342
343 void Widget_Timeslider::set_time_adjustment(Gtk::Adjustment *x)
344 {
345         //disconnect old connections
346         time_value_change.disconnect();
347         time_other_change.disconnect();
348
349         //connect update function to new adjustment
350         adj_timescale = x;
351
352         if(x)
353         {
354                 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Timeslider::queue_draw));
355                 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Timeslider::queue_draw));
356                 //invalidated = true;
357                 //refresh();
358         }
359 }
360
361 void Widget_Timeslider::set_global_fps(float d)
362 {
363         if(fps != d)
364         {
365                 fps = d;
366
367                 //update everything since we need to redraw already
368                 //invalidated = true;
369                 //refresh();
370                 queue_draw();
371         }
372 }
373
374 /*void Widget_Timeslider::update_times()
375 {
376         if(adj_timescale)
377         {
378                 start = adj_timescale->get_lower();
379                 end = adj_timescale->get_upper();
380                 current = adj_timescale->get_value();
381         }
382 }*/
383
384 void Widget_Timeslider::refresh()
385 {
386 }
387 /*
388 {
389         if(invalidated)
390         {
391                 queue_draw();
392         }else if(adj_timescale)
393         {
394                 double  l = adj_timescale->get_lower(),
395                                 u = adj_timescale->get_upper(),
396                                 v = adj_timescale->get_value();
397
398                 bool invalid = (l != start) || (u != end) || (v != current);
399
400                 start = l;
401                 end = u;
402                 current = v;
403
404                 if(invalid) queue_draw();
405         }
406 }*/
407
408 bool Widget_Timeslider::redraw(bool /*doublebuffer*/)
409 {
410         Glib::RefPtr<Gdk::Window> window = get_window();
411
412         if(!window) return false;
413
414         Glib::RefPtr<Gdk::GC>   gc = Gdk::GC::create(window);
415         if(!gc) return false;
416
417         //synfig::info("Drawing Timeslider");
418         //clear and update to current values
419         //invalidated = false;
420         //update_times();
421
422         //draw grey rectangle
423         Gdk::Color      c("#7f7f7f");
424         gc->set_rgb_fg_color(c);
425         gc->set_background(c);
426
427         //Get the data for the window and the params to draw it...
428         int w = get_width(), h = get_height();
429
430         window->draw_rectangle(gc,true,0,0,w,h);
431
432         const double EPSILON = 1e-6;
433         if(!adj_timescale || w == 0) return true;
434
435         //Get the time information since we now know it's valid
436         double  start = adj_timescale->get_lower(),
437                         end = adj_timescale->get_upper(),
438                         current = adj_timescale->get_value();
439
440         if(end-start < EPSILON) return true;
441
442         //synfig::info("Drawing Lines");
443
444         //draw all the time stuff
445         double dtdp = (end - start)/get_width();
446         double dpdt = 1/dtdp;
447
448         //lines
449
450         //Draw the time line...
451         double tpx = (current-start)*dpdt;
452         gc->set_rgb_fg_color(Gdk::Color("#ffaf00"));
453         window->draw_line(gc,round_to_int(tpx),0,round_to_int(tpx),fullheight);
454
455         //normal line/text color
456         gc->set_rgb_fg_color(Gdk::Color("#333333"));
457
458         //draw these lines... (always 5 between) maybe 6?
459         const int subdiv = 4;
460
461         int ifps = round_to_int(fps);
462         if (ifps < 1) ifps = 1;
463
464         std::vector<double> ranges;
465
466         unsigned int pos = 0;
467
468         // build a list of all the factors of the frame rate
469         for (int i = 1; i*i <= ifps; i++)
470                 if ((ifps%i) == 0)
471                 {
472                         ranges.insert(ranges.begin()+pos, i/fps);
473                         if (i*i != ifps)
474                                 ranges.insert(ranges.begin()+pos+1, ifps/i/fps);
475                         pos++;
476                 }
477
478         // fill in any gaps where one factor is more than 2 times the previous
479         std::vector<double>::iterator iter, next;
480         pos = 0;
481         for (pos = 0; pos < ranges.size()-1; pos++)
482         {
483                 iter = ranges.begin()+pos;
484                 next = iter+1;
485                 if (*iter*2 < *next)
486                         ranges.insert(next, *iter*2);
487         }
488
489         double more_ranges[] = {
490                 2, 3, 5, 10, 20, 30, 60, 90, 120, 180,
491                 300, 600, 1200, 1800, 2700, 3600, 3600*2,
492                 3600*4, 3600*8, 3600*16, 3600*32, 3600*64,
493                 3600*128, 3600*256, 3600*512, 3600*1024 };
494
495         ranges.insert(ranges.end(), more_ranges, more_ranges + sizeof(more_ranges)/sizeof(double));
496
497         double lowerrange = dtdp*140, upperrange = dtdp*280;
498         double midrange = (lowerrange + upperrange)/2;
499
500         //find most ideal scale
501         double scale;
502         next = binary_find(ranges.begin(), ranges.end(), midrange);
503         iter = next++;
504
505         if (iter == ranges.end()) iter--;
506         if (next == ranges.end()) next--;
507
508         if (abs(*next - midrange) < abs(*iter - midrange))
509                 scale = *next;
510         else
511                 scale = *iter;
512
513         //synfig::info("Range found: (l %.2lf,u %.2lf - m %.2lf) -> %.2lf",lowerrange,upperrange,midrange,scale);
514
515         //search around this area to get the right one
516
517
518         //get first valid line and its position in pixel space
519         double time = 0;
520         double pixel = 0;
521
522         int sdindex = 0;
523
524         double subr = scale / subdiv;
525
526         //get its position inside...
527         time = ceil(start/subr)*subr - start;
528         pixel = time*dpdt;
529
530         //absolute time of the line to be drawn
531         time += start;
532
533         { //inside the big'n
534                 double t = (time/scale - floor(time/scale))*subdiv; // the difference from the big mark in 0:1
535                 //sdindex = (int)floor(t + 0.5); //get how far through the range it is...
536                 sdindex = round_to_int(t); //get how far through the range it is...
537
538                 //synfig::info("Extracted fr %.2lf -> %d", t, sdindex);
539         }
540
541         //synfig::info("Initial values: %.4lf t, %.1lf pixels, %d i", time,pixel,sdindex);
542
543         //loop to draw
544         const int heightbig = 12;
545         const int heightsmall = 4;
546
547         int width = get_width();
548         while( pixel < width )
549         {
550                 int xpx = round_to_int(pixel);
551
552                 //draw big
553                 if(sdindex == 0)
554                 {
555                         window->draw_line(gc,xpx,0,xpx,heightbig);
556                         //round the time to nearest frame and draw the text
557                         Time tm((double)time);
558                         if(get_global_fps()) tm.round(get_global_fps());
559                         Glib::ustring timecode(tm.get_string(get_global_fps(),App::get_time_format()));
560
561                         //gc->set_rgb_fg_color(Gdk::Color("#000000"));
562                         layout->set_text(timecode);
563                         window->draw_layout(gc,xpx+2,heightsmall,layout);
564                 }else
565                 {
566                         window->draw_line(gc,xpx,0,xpx,heightsmall);
567                 }
568
569                 //increment time and position
570                 pixel += subr / dtdp;
571                 time += subr;
572
573                 //increment index
574                 if(++sdindex >= subdiv) sdindex -= subdiv;
575         }
576
577         return true;
578 }
579
580 bool Widget_Timeslider::on_motion_notify_event(GdkEventMotion* event) //for dragging
581 {
582         if(!adj_timescale) return false;
583
584         Gdk::ModifierType mod = Gdk::ModifierType(event->state);
585
586         //scrolling...
587
588         //NOTE: we might want to address the possibility of dragging with both buttons held down
589
590         if(mod & Gdk::BUTTON2_MASK)
591         {
592
593                 //we need this for scrolling by dragging
594                 double  curx = event->x;
595
596                 double  start = adj_timescale->get_lower(),
597                                 end = adj_timescale->get_upper();
598
599
600                 if(dragscroll)
601                 {
602                         if(event->time-last_event_time<30)
603                                 return false;
604                         else
605                                 last_event_time=event->time;
606
607                         if(abs(lastx - curx) < 1 && end != start) return true;
608                         //translate the window and correct it
609
610                         //update our stuff so we are operating correctly
611                         //invalidated = true;
612                         //update_times();
613
614                         //Note: Use inverse of mouse movement because of conceptual space relationship
615                         double diff = lastx - curx; //curx - lastx;
616
617                         //NOTE: This might be incorrect...
618                         //fraction to move...
619                         double dpx = (end - start)/get_width();
620                         lastx = curx;
621
622                         diff *= dpx;
623
624                         //Adjust...
625                         start += diff;
626                         end += diff;
627
628                         //But clamp to bounds if they exist...
629                         //HACK - bounds should not be required for this slider
630                         if(adj_bounds)
631                         {
632                                 if(start < adj_bounds->get_lower())
633                                 {
634                                         diff = adj_bounds->get_lower() - start;
635                                         start += diff;
636                                         end += diff;
637                                 }
638
639                                 if(end > adj_bounds->get_upper())
640                                 {
641                                         diff = adj_bounds->get_upper() - end;
642                                         start += diff;
643                                         end += diff;
644                                 }
645                         }
646
647                         //synfig::info("Scrolling timerange to (%.4f,%.4f)",start,end);
648
649                         adj_timescale->set_lower(start);
650                         adj_timescale->set_upper(end);
651
652                         adj_timescale->changed();
653                 }else
654                 {
655                         dragscroll = true;
656                         lastx = curx;
657                         //lasty = cury;
658                 }
659
660                 return true;
661         }
662
663         if(mod & Gdk::BUTTON1_MASK)
664         {
665                 double curx = event->x;
666
667                 //get time from drag...
668                 double  start = adj_timescale->get_lower(),
669                                 end = adj_timescale->get_upper(),
670                                 current = adj_timescale->get_value();
671                 double t = start + curx*(end - start)/get_width();
672
673                 //snap it to fps - if they exist...
674                 if(fps)
675                 {
676                         t = floor(t*fps + 0.5)/fps;
677                 }
678
679                 //set time if needed
680                 if(current != t)
681                 {
682                         adj_timescale->set_value(t);
683
684                         //Fixed this to actually do what it's supposed to...
685                         if(event->time-last_event_time>50)
686                         {
687                                 adj_timescale->value_changed();
688                                 last_event_time = event->time;
689                         }
690                 }
691
692                 return true;
693         }
694
695         return false;
696 }
697
698 bool Widget_Timeslider::on_scroll_event(GdkEventScroll* event) //for zooming
699 {
700         if(!adj_timescale) return false;
701
702         //Update so we are calculating based on current values
703         //update_times();
704
705         //figure out if we should center ourselves on the current time
706         bool center = false;
707
708         //we want to zoom in on the time value if control is held down
709         if(Gdk::ModifierType(event->state) & Gdk::CONTROL_MASK)
710         {
711                 center = true;
712         }
713
714         switch(event->direction)
715         {
716                 case GDK_SCROLL_UP: //zoom in
717                 {
718                         zoom_in(center);
719
720                         return true;
721                 }
722                 case GDK_SCROLL_DOWN: //zoom out
723                 {
724                         zoom_out(center);
725
726                         return true;
727                 }
728                 
729                 case GDK_SCROLL_RIGHT:
730                 case GDK_SCROLL_LEFT:
731                 {       
732                         double t = adj_timescale->get_value();
733                         double start = adj_timescale->get_lower();
734                         double end = adj_timescale->get_upper();
735                         /*
736                         FIXME: be more intelligent about how far to scroll
737                         Perhaps it should be based on the tickmarks?
738                         for e.g. 1/4 of a tick mark per scroll event
739                         Obviously this  would need post-rounding to 1/fps
740                         */
741                         double adj = 1.0/fps;
742
743                         if( event->direction == GDK_SCROLL_RIGHT )
744                                 t += adj;
745                         else
746                                 t -= adj;
747
748                         if( t < start ){
749                                 adj_timescale->set_lower(t);
750                                 adj_timescale->set_upper(t+end-start);
751                         } else if( t > end ){ 
752                                 adj_timescale->set_upper(t);
753                                 adj_timescale->set_lower(t-end+start);
754                         }
755
756                         if(adj_timescale)
757                         {
758                                 adj_timescale->set_value(t);
759                                 adj_timescale->value_changed();
760                         }
761                         return true;
762                 }
763                 
764                 default:
765                 {
766                         return false;
767                 }
768         }
769 }
770
771 void Widget_Timeslider::zoom_in(bool centerontime)
772 {
773         if(!adj_timescale) return;
774
775         double  start = adj_timescale->get_lower(),
776                         end = adj_timescale->get_upper(),
777                         current = adj_timescale->get_value();
778
779         double focuspoint = centerontime ? current : (start + end)/2;
780
781         //calculate new beginning and end
782         end = focuspoint + (end-focuspoint)*zoominfactor;
783         start = focuspoint + (start-focuspoint)*zoominfactor;
784
785         //synfig::info("Zooming in timerange to (%.4f,%.4f)",start,end);
786         if(adj_bounds)
787         {
788                 if(start < adj_bounds->get_lower())
789                 {
790                         start = adj_bounds->get_lower();
791                 }
792
793                 if(end > adj_bounds->get_upper())
794                 {
795                         end = adj_bounds->get_upper();
796                 }
797         }
798
799         //reset values
800         adj_timescale->set_lower(start);
801         adj_timescale->set_upper(end);
802
803         //call changed function
804         adj_timescale->changed();
805 }
806
807 void Widget_Timeslider::zoom_out(bool centerontime)
808 {
809         if(!adj_timescale) return;
810
811         double  start = adj_timescale->get_lower(),
812                         end = adj_timescale->get_upper(),
813                         current = adj_timescale->get_value();
814
815         double focuspoint = centerontime ? current : (start + end)/2;
816
817         //calculate new beginning and end
818         end = focuspoint + (end-focuspoint)*zoomoutfactor;
819         start = focuspoint + (start-focuspoint)*zoomoutfactor;
820
821         //synfig::info("Zooming out timerange to (%.4f,%.4f)",start,end);
822         if(adj_bounds)
823         {
824                 if(start < adj_bounds->get_lower())
825                 {
826                         start = adj_bounds->get_lower();
827                 }
828
829                 if(end > adj_bounds->get_upper())
830                 {
831                         end = adj_bounds->get_upper();
832                 }
833         }
834
835         //reset values
836         adj_timescale->set_lower(start);
837         adj_timescale->set_upper(end);
838
839         //call changed function
840         adj_timescale->changed();
841 }
842
843 bool Widget_Timeslider::on_button_press_event(GdkEventButton *event) //for clicking
844 {
845         switch(event->button)
846         {
847                 //time click...
848                 case 1:
849                 {
850                         double  start = adj_timescale->get_lower(),
851                                         end = adj_timescale->get_upper(),
852                                         current = adj_timescale->get_value();
853
854                         double w = get_width();
855                         double t = start + (end - start) * event->x / w;
856
857                         t = floor(t*fps + 0.5)/fps;
858
859                         /*synfig::info("Clicking time from %.3lf to %.3lf [(%.2lf,%.2lf) %.2lf / %.2lf ... %.2lf",
860                                                 current, vt, start, end, event->x, w, fps);*/
861
862                         if(t != current)
863                         {
864                                 current = t;
865
866                                 if(adj_timescale)
867                                 {
868                                         adj_timescale->set_value(current);
869                                         adj_timescale->value_changed();
870                                 }
871                         }
872
873                         break;
874                 }
875
876                 //scroll click
877                 case 2:
878                 {
879                         //start dragging
880                         dragscroll = true;
881                         lastx = event->x;
882                         //lasty = event->y;
883
884                         return true;
885                 }
886
887                 default:
888                 {
889                         break;
890                 }
891         }
892
893         return false;
894 }
895
896 bool Widget_Timeslider::on_button_release_event(GdkEventButton *event) //end drag
897 {
898         switch(event->button)
899         {
900                 case 2:
901                 {
902                         //start dragging
903                         dragscroll = false;
904                         return true;
905                 }
906
907                 default:
908                 {
909                         break;
910                 }
911         }
912
913         return false;
914 }