Get rid of all references to the bootstrap script since it was removed
[synfig.git] / synfig-studio / trunk / src / gtkmm / workarea.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file workarea.cpp
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright 2006 Yue Shi Lai
10 **      Copyright (c) 2007 Chris Moore
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 <sigc++/adaptors/hide.h>
35
36 #include "workarea.h"
37 #include "canvasview.h"
38 #include "app.h"
39 #include <gtkmm/window.h>
40 #include <gtkmm/image.h>
41 #include <gtkmm/drawingarea.h>
42 #include <gtkmm/ruler.h>
43 #include <gtkmm/arrow.h>
44 #include <gtkmm/image.h>
45 #include <gtkmm/scrollbar.h>
46 #include <cmath>
47 #include <sigc++/retype_return.h>
48 #include <sigc++/retype.h>
49 #include <sigc++/hide.h>
50 #include <ETL/misc>
51
52 #include <synfig/target_scanline.h>
53 #include <synfig/target_tile.h>
54 #include <synfig/surface.h>
55 #include <synfig/valuenode_composite.h>
56 #include <synfigapp/canvasinterface.h>
57 #include "event_mouse.h"
58 #include "event_layerclick.h"
59 #include "widget_color.h"
60 #include <synfig/distance.h>
61 #include "workarearenderer.h"
62
63 #include "renderer_canvas.h"
64 #include "renderer_grid.h"
65 #include "renderer_guides.h"
66 #include "renderer_timecode.h"
67 #include "renderer_ducks.h"
68 #include "renderer_dragbox.h"
69 #include "renderer_bbox.h"
70 #include "asyncrenderer.h"
71 #include <gtkmm/frame.h>
72
73 #include <synfig/mutex.h>
74
75 #include "general.h"
76
77 #endif
78
79 /* === U S I N G =========================================================== */
80
81 using namespace std;
82 using namespace etl;
83 using namespace synfig;
84 using namespace studio;
85
86 /* === M A C R O S ========================================================= */
87
88 #define RULER_FIX               15
89
90 #ifndef stratof
91 #define stratof(X) (atof((X).c_str()))
92 #define stratoi(X) (atoi((X).c_str()))
93 #endif
94
95
96 /* === G L O B A L S ======================================================= */
97
98 /* === C L A S S E S ======================================================= */
99
100 class studio::WorkAreaTarget : public synfig::Target_Tile
101 {
102 public:
103         WorkArea *workarea;
104         bool low_res;
105         int w,h;
106         int real_tile_w,real_tile_h;
107         //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
108
109         int twindow_start, twindow_width, twindow_height, twindow_pad;
110         int refresh_id;
111
112         bool onionskin;
113         bool onion_first_tile;
114         int onion_layers;
115
116         std::list<synfig::Time> onion_skin_queue;
117
118         synfig::Mutex mutex;
119
120         void set_onion_skin(bool x)
121         {
122                 onionskin=x;
123
124                 Time time(rend_desc().get_time_start());
125
126                 onion_skin_queue.push_back(time);
127                 //onion_skin_queue.push_back(time-1);
128                 //onion_skin_queue.push_back(time+1);
129
130                 try
131                 {
132                         onion_skin_queue.push_back(
133                                 get_canvas()->keyframe_list().find_prev(
134                                         time
135                                 )->get_time()
136                         );
137                 }
138                 catch(...)
139                 {  }
140
141                 try
142                 {
143                         onion_skin_queue.push_back(
144                                 get_canvas()->keyframe_list().find_next(
145                                         time
146                                 )->get_time()
147                         );
148                 }
149                 catch(...)
150                 {  }
151
152                 onion_layers=onion_skin_queue.size();
153
154                 onion_first_tile=false;
155         }
156 public:
157
158         WorkAreaTarget(WorkArea *workarea,int w, int h):
159                 workarea(workarea),
160                 low_res(workarea->get_low_resolution_flag()),
161                 w(w),
162                 h(h),
163                 real_tile_w(workarea->tile_w),
164                 real_tile_h(workarea->tile_h),
165                 refresh_id(workarea->refreshes),
166                 onionskin(false),
167                 onion_layers(0)
168         {
169                 //set_remove_alpha();
170                 //set_avoid_time_sync();
171                 set_clipping(true);
172                 if(low_res)
173                 {
174                         int div = workarea->get_low_res_pixel_size();
175                         set_tile_w(workarea->tile_w/div);
176                         set_tile_h(workarea->tile_h/div);
177                 }
178                 else
179                 {
180                         set_tile_w(workarea->tile_w);
181                         set_tile_h(workarea->tile_h);
182                 }
183                 set_canvas(workarea->get_canvas());
184                 set_quality(workarea->get_quality());
185         }
186
187         ~WorkAreaTarget()
188         {
189                 workarea->queue_draw();
190         }
191
192         virtual bool set_rend_desc(synfig::RendDesc *newdesc)
193         {
194                 assert(workarea);
195                 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
196                 if(low_res) {
197                         int div = workarea->get_low_res_pixel_size();
198                         newdesc->set_wh(w/div,h/div);
199                 }
200                 else
201                         newdesc->set_wh(w,h);
202
203                 if(
204                                 workarea->get_w()!=w
205                         ||      workarea->get_h()!=h
206                 ) workarea->set_wh(w,h,4);
207
208                 workarea->full_frame=false;
209
210                 desc=*newdesc;
211                 return true;
212         }
213
214         virtual int total_tiles()const
215         {
216                 int tw(rend_desc().get_w()/get_tile_w());
217                 int th(rend_desc().get_h()/get_tile_h());
218                 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
219                 if(rend_desc().get_h()%get_tile_h()!=0)th++;
220                 return tw*th;
221         }
222
223         virtual int next_frame(Time& time)
224         {
225                 synfig::Mutex::Lock lock(mutex);
226
227                 if(!onionskin)
228                         return synfig::Target_Tile::next_frame(time);
229
230                 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
231
232                 if(!onion_skin_queue.empty())
233                 {
234                         time=onion_skin_queue.front();
235                         onion_skin_queue.pop_front();
236                 }
237                 else
238                         return 0;
239
240                 return onion_skin_queue.size()+1;
241         }
242
243         virtual int next_tile(int& x, int& y)
244         {
245                 synfig::Mutex::Lock lock(mutex);
246                 //if(workarea->tile_queue.empty()) return 0;
247
248                 //int curr_tile(workarea->tile_queue.front());
249                 //workarea->tile_queue.pop_front();
250                 int curr_tile(workarea->next_unrendered_tile(refresh_id-onion_skin_queue.size()));
251                 if(curr_tile<0)
252                         return 0;
253
254                 // Width of the image(in tiles)
255                 int tw(rend_desc().get_w()/get_tile_w());
256                 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
257
258                 y=(curr_tile/tw)*get_tile_w();
259                 x=(curr_tile%tw)*get_tile_h();
260
261                 // Mark this tile as "up-to-date"
262                 if(onionskin)
263                         workarea->tile_book[curr_tile].second=refresh_id-onion_skin_queue.size();
264                 else
265                         workarea->tile_book[curr_tile].second=refresh_id;
266
267                 return total_tiles()-curr_tile+1;
268         }
269
270
271         virtual bool start_frame(synfig::ProgressCallback */*cb*/)
272         {
273                 synfig::Mutex::Lock lock(mutex);
274
275                 int tw(rend_desc().get_w()/get_tile_w());
276                 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
277                 int th(rend_desc().get_h()/get_tile_h());
278                 if(rend_desc().get_h()%get_tile_h()!=0)th++;
279
280                 twindow_start=0;
281                 twindow_width=tw;
282                 twindow_height=th;
283                 twindow_pad=0;
284
285                 workarea->tile_book.resize(total_tiles());
286                 //tile_iter=workarea->tile_book.begin()+twindow_start;
287                 return true;
288         }
289
290         static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
291
292         virtual bool add_tile(const synfig::Surface &surface, int x, int y)
293         {
294                 synfig::Mutex::Lock lock(mutex);
295                 assert(surface);
296
297                 PixelFormat pf(PF_RGB);
298
299                 const int total_bytes(get_tile_w()*get_tile_h()*synfig::channels(pf));
300
301                 unsigned char *buffer((unsigned char*)malloc(total_bytes));
302
303                 if(!surface || !buffer)
304                         return false;
305                 {
306                         unsigned char *dest(buffer);
307                         const Color *src(surface[0]);
308                         int w(get_tile_w());
309                         int h(get_tile_h());
310                         int x(surface.get_w()*surface.get_h());
311                         //if(low_res) {
312                         //      int div = workarea->get_low_res_pixel_size();
313                         //      w/=div,h/=div;
314                         //}
315                         Color dark(0.6,0.6,0.6);
316                         Color lite(0.8,0.8,0.8);
317                         for(int i=0;i<x;i++)
318                                 dest=Color2PixelFormat(
319                                         Color::blend(
320                                                 (*(src++)),
321                                                 ((i/surface.get_w())*8/h+(i%surface.get_w())*8/w)&1?dark:lite,
322                                                 1.0f
323                                         ).clamped(),
324                                         pf,dest,App::gamma
325                                 );
326                 }
327
328                 x/=get_tile_w();
329                 y/=get_tile_h();
330                 int tw(rend_desc().get_w()/get_tile_w());
331                 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
332                 unsigned int index=y*tw+x;
333
334                 // Sanity check
335                 if(index>workarea->tile_book.size())
336                         return false;
337
338                 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
339
340                 pixbuf=Gdk::Pixbuf::create_from_data(
341                         buffer, // pointer to the data
342                         Gdk::COLORSPACE_RGB, // the colorspace
343                         ((pf&PF_A)==PF_A), // has alpha?
344                         8, // bits per sample
345                         surface.get_w(),        // width
346                         surface.get_h(),        // height
347                         surface.get_w()*synfig::channels(pf), // stride (pitch)
348                         sigc::ptr_fun(&WorkAreaTarget::free_buff)
349                 );
350
351                 if(low_res)
352                 {
353                         // We need to scale up
354                         int div = workarea->get_low_res_pixel_size();
355                         pixbuf=pixbuf->scale_simple(
356                                 surface.get_w()*div,
357                                 surface.get_h()*div,
358                                 Gdk::INTERP_NEAREST
359                         );
360                 }
361
362                 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
363                 {
364                         workarea->tile_book[index].first=pixbuf;
365                 }
366                 else
367                 {
368                         pixbuf->composite(
369                                 workarea->tile_book[index].first, // Dest
370                                 0,//int dest_x
371                                 0,//int dest_y
372                                 pixbuf->get_width(), // dest width
373                                 pixbuf->get_height(), // dest_height,
374                                 0, // double offset_x
375                                 0, // double offset_y
376                                 1, // double scale_x
377                                 1, // double scale_y
378                                 Gdk::INTERP_NEAREST, // interp
379                                 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
380                         );
381                 }
382
383                 //if(index%2)
384                         workarea->queue_draw();
385                 assert(workarea->tile_book[index].first);
386                 return true;
387         }
388
389         virtual void end_frame()
390         {
391                 //workarea->queue_draw();
392         }
393 };
394
395
396 class studio::WorkAreaTarget_Full : public synfig::Target_Scanline
397 {
398 public:
399         WorkArea *workarea;
400         bool low_res;
401         int w,h;
402         int real_tile_w,real_tile_h;
403         //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
404
405         int twindow_start, twindow_width, twindow_height, twindow_pad;
406         int refresh_id;
407
408         bool onionskin;
409         bool onion_first_tile;
410         int onion_layers;
411
412         Surface surface;
413
414         std::list<synfig::Time> onion_skin_queue;
415
416         void set_onion_skin(bool x)
417         {
418                 onionskin=x;
419
420                 Time time(rend_desc().get_time_start());
421
422                 onion_skin_queue.push_back(time);
423                 //onion_skin_queue.push_back(time-1);
424                 //onion_skin_queue.push_back(time+1);
425                 if(!onionskin)
426                         return;
427
428                 try
429                 {
430                         onion_skin_queue.push_back(
431                                 get_canvas()->keyframe_list().find_prev(
432                                         time
433                                 )->get_time()
434                         );
435                 }
436                 catch(...)
437                 {  }
438
439                 try
440                 {
441                         onion_skin_queue.push_back(
442                                 get_canvas()->keyframe_list().find_next(
443                                         time
444                                 )->get_time()
445                         );
446                 }
447                 catch(...)
448                 {  }
449
450                 onion_layers=onion_skin_queue.size();
451
452                 onion_first_tile=false;
453         }
454 public:
455
456         WorkAreaTarget_Full(WorkArea *workarea,int w, int h):
457                 workarea(workarea),
458                 low_res(workarea->get_low_resolution_flag()),
459                 w(w),
460                 h(h),
461                 refresh_id(workarea->refreshes),
462                 onionskin(false),
463                 onion_layers(0)
464         {
465                 set_canvas(workarea->get_canvas());
466                 set_quality(workarea->get_quality());
467         }
468
469         ~WorkAreaTarget_Full()
470         {
471         }
472
473         virtual bool set_rend_desc(synfig::RendDesc *newdesc)
474         {
475                 assert(workarea);
476                 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
477                 if(low_res)
478                 {
479                         int div = workarea->get_low_res_pixel_size();
480                         newdesc->set_wh(w/div,h/div);
481                 }
482                 else
483                         newdesc->set_wh(w,h);
484
485                 if(
486                                 workarea->get_w()!=w
487                         ||      workarea->get_h()!=h
488                 ) workarea->set_wh(w,h,4);
489
490                 surface.set_wh(newdesc->get_w(),newdesc->get_h());
491
492                 desc=*newdesc;
493                 workarea->full_frame=true;
494                 workarea->tile_book.resize(1);
495                 return true;
496         }
497
498         virtual int next_frame(Time& time)
499         {
500                 // Mark this tile as "up-to-date"
501                 if(onionskin)
502                         workarea->tile_book[0].second=refresh_id-onion_skin_queue.size();
503                 else
504                         workarea->tile_book[0].second=refresh_id;
505
506                 if(!onionskin)
507                         return synfig::Target_Scanline::next_frame(time);
508
509                 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
510
511                 if(!onion_skin_queue.empty())
512                 {
513                         time=onion_skin_queue.front();
514                         onion_skin_queue.pop_front();
515                 }
516                 else
517                         return 0;
518                 return onion_skin_queue.size()+1;
519         }
520
521
522         virtual bool start_frame(synfig::ProgressCallback */*cb*/)
523         {
524                 return true;
525         }
526
527         virtual Color * start_scanline(int scanline)
528         {
529                 return surface[scanline];
530         }
531
532         virtual bool end_scanline()
533         {
534                 return true;
535         }
536
537         static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
538
539         virtual void end_frame()
540         {
541                 assert(surface);
542
543                 PixelFormat pf(PF_RGB);
544
545                 const int total_bytes(surface.get_w()*surface.get_h()*synfig::channels(pf));
546
547                 unsigned char *buffer((unsigned char*)malloc(total_bytes));
548
549                 if(!surface || !buffer)
550                         return;
551                 {
552                         unsigned char *dest(buffer);
553                         const Color *src(surface[0]);
554                         int w(surface.get_w());
555                         //int h(surface.get_h());
556                         int x(surface.get_w()*surface.get_h());
557                         //if(low_res) {
558                         //      int div = workarea->get_low_res_pixel_size();
559                         //      w/=div,h/=div;
560                         //}
561                         Color dark(0.6,0.6,0.6);
562                         Color lite(0.8,0.8,0.8);
563                         int tw=workarea->tile_w;
564                         int th=workarea->tile_h;
565                         if(low_res)
566                         {
567                                 int div = workarea->get_low_res_pixel_size();
568                                 tw/=div;
569                                 th/=div;
570                         }
571                         for(int i=0;i<x;i++)
572                                 dest=Color2PixelFormat(
573                                         Color::blend(
574                                                 (*(src++)),
575                                                 ((i/w)*8/th+(i%w)*8/tw)&1?dark:lite,
576                                                 1.0f
577                                         ).clamped(),
578                                         pf,
579                                         dest,
580                                         App::gamma
581                                 );
582                 }
583
584                 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
585
586                 pixbuf=Gdk::Pixbuf::create_from_data(
587                         buffer, // pointer to the data
588                         Gdk::COLORSPACE_RGB, // the colorspace
589                         ((pf&PF_A)==PF_A), // has alpha?
590                         8, // bits per sample
591                         surface.get_w(),        // width
592                         surface.get_h(),        // height
593                         surface.get_w()*synfig::channels(pf), // stride (pitch)
594                         sigc::ptr_fun(&WorkAreaTarget::free_buff)
595                 );
596
597                 if(low_res)
598                 {
599                         // We need to scale up
600                         int div = workarea->get_low_res_pixel_size();
601                         pixbuf=pixbuf->scale_simple(
602                                 surface.get_w()*div,
603                                 surface.get_h()*div,
604                                 Gdk::INTERP_NEAREST
605                         );
606                 }
607
608                 int index=0;
609
610                 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
611                 {
612                         workarea->tile_book[index].first=pixbuf;
613                 }
614                 else
615                 {
616                         pixbuf->composite(
617                                 workarea->tile_book[index].first, // Dest
618                                 0,//int dest_x
619                                 0,//int dest_y
620                                 pixbuf->get_width(), // dest width
621                                 pixbuf->get_height(), // dest_height,
622                                 0, // double offset_x
623                                 0, // double offset_y
624                                 1, // double scale_x
625                                 1, // double scale_y
626                                 Gdk::INTERP_NEAREST, // interp
627                                 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
628                         );
629                 }
630
631                 workarea->queue_draw();
632                 assert(workarea->tile_book[index].first);
633         }
634 };
635
636
637
638
639 /* === M E T H O D S ======================================================= */
640
641
642 WorkArea::WorkArea(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface):
643         Gtk::Table(4+RULER_FIX, 3, false),
644         canvas_interface(canvas_interface),
645         canvas(canvas_interface->get_canvas()),
646         scrollx_adjustment(0,-4,4,0.01,0.1),
647         scrolly_adjustment(0,-4,4,0.01,0.1),
648         w(TILE_SIZE),
649         h(TILE_SIZE),
650         last_event_time(0),
651         progresscallback(0),
652         dragging(DRAG_NONE),
653         show_grid(false),
654         tile_w(TILE_SIZE),
655         tile_h(TILE_SIZE),
656         timecode_width(0),
657         timecode_height(0)
658 {
659         show_guides=true;
660         curr_input_device=0;
661         full_frame=false;
662         allow_duck_clicks=true;
663         allow_layer_clicks=true;
664         render_idle_func_id=0;
665         zoom=prev_zoom=1.0;
666         quality=10;
667         low_res_pixel_size=2;
668         rendering=false;
669         canceled_=false;
670         low_resolution=true;
671         pw=0.001;
672         ph=0.001;
673         last_focus_point=Point(0,0);
674         onion_skin=false;
675         queued=false;
676         dirty_trap_enabled=false;
677         solid_lines=true;
678
679         dirty_trap_queued=0;
680
681         meta_data_lock=false;
682
683         insert_renderer(new Renderer_Canvas,    000);
684         insert_renderer(new Renderer_Grid,              100);
685         insert_renderer(new Renderer_Guides,    200);
686         insert_renderer(new Renderer_Ducks,             300);
687         insert_renderer(new Renderer_BBox,              399);
688         insert_renderer(new Renderer_Dragbox,   400);
689         insert_renderer(new Renderer_Timecode,  500);
690
691         signal_duck_selection_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
692         signal_strokes_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
693         signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
694         signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
695         signal_sketch_saved().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
696
697         // Not that it really makes a difference... (setting this to zero, that is)
698         refreshes=0;
699
700         drawing_area=manage(new class Gtk::DrawingArea());
701         drawing_area->show();
702         drawing_area->set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
703
704         drawing_frame=manage(new Gtk::Frame);
705         drawing_frame->add(*drawing_area);
706         //drawing_frame->set_shadow_type(Gtk::SHADOW_NONE);
707         //drawing_frame->property_border_width()=5;
708         //drawing_frame->modify_fg(Gtk::STATE_NORMAL,Gdk::Color("#00ffff"));
709         //drawing_frame->modify_base(Gtk::STATE_NORMAL,Gdk::Color("#ff00ff"));
710         /*drawing_frame->modify_fg(Gtk::STATE_ACTIVE,Gdk::Color("#00ffff"));
711         drawing_frame->modify_base(Gtk::STATE_ACTIVE,Gdk::Color("#ff00ff"));
712         drawing_frame->modify_bg(Gtk::STATE_ACTIVE,Gdk::Color("#00ff00"));
713         drawing_frame->modify_fg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ffff"));
714         drawing_frame->modify_base(Gtk::STATE_INSENSITIVE,Gdk::Color("#ff00ff"));
715         drawing_frame->modify_bg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ff00"));
716         drawing_frame->modify_fg(Gtk::STATE_SELECTED,Gdk::Color("#00ffff"));
717         drawing_frame->modify_base(Gtk::STATE_SELECTED,Gdk::Color("#ff00ff"));
718         drawing_frame->modify_bg(Gtk::STATE_SELECTED,Gdk::Color("#00ff00"));
719         */
720         //drawing_frame->set_state(Gtk::STATE_NORMAL);
721
722         drawing_frame->show();
723
724         attach(*drawing_frame, 1, 3+RULER_FIX, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
725
726         Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
727
728
729         // Create the vertical and horizontal rulers
730         vruler = manage(new class Gtk::VRuler());
731         hruler = manage(new class Gtk::HRuler());
732         vruler->set_metric(Gtk::PIXELS);
733         hruler->set_metric(Gtk::PIXELS);
734         vruler->show();
735         hruler->show();
736         attach(*vruler, 0, 1, 1, 2, Gtk::SHRINK|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
737         attach(*hruler, 1, 3+RULER_FIX, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
738         hruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_hruler_event));
739         vruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_vruler_event));
740         hruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
741         vruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
742
743         // Create the menu button
744         menubutton=manage(new class Gtk::Button());
745         Gtk::Arrow *arrow1 = manage(new class Gtk::Arrow(Gtk::ARROW_RIGHT, Gtk::SHADOW_OUT));
746         menubutton->add(*arrow1);
747         menubutton->show_all();
748         menubutton->signal_pressed().connect(sigc::mem_fun(*this, &WorkArea::popup_menu));
749         attach(*menubutton, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
750
751
752
753         Gtk::VScrollbar *vscrollbar1 = manage(new class Gtk::VScrollbar(*get_scrolly_adjustment()));
754         Gtk::HScrollbar *hscrollbar1 = manage(new class Gtk::HScrollbar(*get_scrollx_adjustment()));
755         vscrollbar1->show();
756         hscrollbar1->show();
757         attach(*vscrollbar1, 3+RULER_FIX, 4+RULER_FIX, 1, 2, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
758         attach(*hscrollbar1, 2+RULER_FIX, 3+RULER_FIX, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
759
760         ZoomDial *zoomdial=manage(new class ZoomDial(iconsize));
761         zoomdial->signal_zoom_in().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_in));
762         zoomdial->signal_zoom_out().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_out));
763         zoomdial->signal_zoom_fit().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_fit));
764         zoomdial->signal_zoom_norm().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_norm));
765         zoomdial->show();
766         attach(*zoomdial, 0, 2+RULER_FIX, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
767
768         drawing_area->add_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
769         add_events(Gdk::KEY_PRESS_MASK);
770         drawing_area->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
771         drawing_area->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::POINTER_MOTION_MASK);
772
773         // ----------------- Attach signals
774
775         drawing_area->signal_expose_event().connect(sigc::mem_fun(*this, &WorkArea::refresh));
776         drawing_area->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_drawing_area_event));
777         drawing_area->signal_size_allocate().connect(sigc::hide(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info)));
778
779
780
781         canvas_interface->signal_rend_desc_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
782         // When either of the scrolling adjustments change, then redraw.
783         get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
784         get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
785         get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
786         get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
787
788         get_canvas()->signal_meta_data_changed("grid_size").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
789         get_canvas()->signal_meta_data_changed("grid_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
790         get_canvas()->signal_meta_data_changed("grid_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
791         get_canvas()->signal_meta_data_changed("guide_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
792         get_canvas()->signal_meta_data_changed("guide_x").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
793         get_canvas()->signal_meta_data_changed("guide_y").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
794         get_canvas()->signal_meta_data_changed("onion_skin").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
795         get_canvas()->signal_meta_data_changed("guide_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
796         get_canvas()->signal_meta_data_changed("sketch").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
797         get_canvas()->signal_meta_data_changed("solid_lines").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
798
799         queued=false;
800         meta_data_lock=false;
801         set_focus_point(Point(0,0));
802
803
804         load_meta_data();
805         // Load sketch
806         {
807                 String data(canvas->get_meta_data("sketch"));
808                 if(!data.empty())
809                 {
810                         if(!load_sketch(data))
811                                 load_sketch(dirname(canvas->get_file_name())+ETL_DIRECTORY_SEPARATOR+basename(data));
812                 }
813         }
814
815         hruler->property_max_size()=double(10.0);
816         vruler->property_max_size()=double(10.0);
817
818         drawing_area->set_flags(drawing_area->get_flags()|Gtk::CAN_FOCUS);
819 }
820
821 WorkArea::~WorkArea()
822 {
823 //      delete [] buffer;
824
825         // don't leave the render function queued if we are about to vanish;
826         // that causes crashes
827         if(render_idle_func_id)
828                 render_idle_func_id=0;
829 }
830
831 bool
832 WorkArea::get_updating()const
833 {
834         return App::single_threaded && async_renderer && async_renderer->updating;
835 }
836
837 void
838 WorkArea::stop_updating(bool cancel)
839 {
840         async_renderer->stop();
841         if (cancel) canceled_=true;
842 }
843
844 void
845 WorkArea::save_meta_data()
846 {
847         if(meta_data_lock)
848                 return;
849         meta_data_lock=true;
850
851         Vector s(get_grid_size());
852         canvas_interface->set_meta_data("grid_size",strprintf("%f %f",s[0],s[1]));
853         canvas_interface->set_meta_data("grid_snap",get_grid_snap()?"1":"0");
854         canvas_interface->set_meta_data("guide_snap",get_guide_snap()?"1":"0");
855         canvas_interface->set_meta_data("guide_show",get_show_guides()?"1":"0");
856         canvas_interface->set_meta_data("grid_show",show_grid?"1":"0");
857         canvas_interface->set_meta_data("onion_skin",onion_skin?"1":"0");
858         {
859                 String data;
860                 GuideList::const_iterator iter;
861                 for(iter=get_guide_list_x().begin();iter!=get_guide_list_x().end();++iter)
862                 {
863                         if(!data.empty())
864                                 data+=' ';
865                         data+=strprintf("%f",*iter);
866                 }
867                 if(!data.empty())
868                         canvas_interface->set_meta_data("guide_x",data);
869
870                 data.clear();
871                 for(iter=get_guide_list_y().begin();iter!=get_guide_list_y().end();++iter)
872                 {
873                         if(!data.empty())
874                                 data+=' ';
875                         data+=strprintf("%f",*iter);
876                 }
877                 if(!data.empty())
878                         canvas_interface->set_meta_data("guide_y",data);
879         }
880
881         if(get_sketch_filename().size())
882         {
883                 if(dirname(canvas->get_file_name())==dirname(get_sketch_filename()))
884                         canvas_interface->set_meta_data("sketch",basename(get_sketch_filename()));
885                 else
886                         canvas_interface->set_meta_data("sketch",get_sketch_filename());
887         }
888
889         meta_data_lock=false;
890 }
891
892 void
893 WorkArea::load_meta_data()
894 {
895         if(meta_data_lock)
896                 return;
897         meta_data_lock=true;
898
899         String data;
900
901         data=canvas->get_meta_data("grid_size");
902         if(!data.empty())
903         {
904                 float gx(get_grid_size()[0]),gy(get_grid_size()[1]);
905
906                 String::iterator iter(find(data.begin(),data.end(),' '));
907                 String tmp(data.begin(),iter);
908
909                 if(!tmp.empty())
910                         gx=stratof(tmp);
911                 else
912                         synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
913
914                 if(iter==data.end())
915                         tmp.clear();
916                 else
917                         tmp=String(iter+1,data.end());
918
919                 if(!tmp.empty())
920                         gy=stratof(tmp);
921                 else
922                         synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
923
924                 set_grid_size(Vector(gx,gy));
925         }
926
927         data=canvas->get_meta_data("grid_show");
928         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
929                 show_grid=true;
930         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
931                 show_grid=false;
932
933         data=canvas->get_meta_data("solid_lines");
934         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
935                 solid_lines=true;
936         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
937                 solid_lines=false;
938
939         data=canvas->get_meta_data("guide_show");
940         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
941                 show_guides=true;
942         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
943                 show_guides=false;
944
945         data=canvas->get_meta_data("grid_snap");
946         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
947                 set_grid_snap(true);
948         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
949                 set_grid_snap(false);
950
951         data=canvas->get_meta_data("guide_snap");
952         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
953                 set_guide_snap(true);
954         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
955                 set_guide_snap(false);
956
957         data=canvas->get_meta_data("onion_skin");
958         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
959                 set_onion_skin(true);
960         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
961                 set_onion_skin(false);
962
963         data=canvas->get_meta_data("guide_x");
964         get_guide_list_x().clear();
965         while(!data.empty())
966         {
967                 String::iterator iter(find(data.begin(),data.end(),' '));
968                 String guide(data.begin(),iter);
969
970                 if(!guide.empty())
971                         get_guide_list_x().push_back(stratof(guide));
972
973                 if(iter==data.end())
974                         data.clear();
975                 else
976                         data=String(iter+1,data.end());
977         }
978         //sort(get_guide_list_x());
979
980         data=canvas->get_meta_data("guide_y");
981         get_guide_list_y().clear();
982         while(!data.empty())
983         {
984                 String::iterator iter(find(data.begin(),data.end(),' '));
985                 String guide(data.begin(),iter);
986
987                 if(!guide.empty())
988                         get_guide_list_y().push_back(stratof(guide));
989
990                 if(iter==data.end())
991                         data.clear();
992                 else
993                         data=String(iter+1,data.end());
994         }
995         //sort(get_guide_list_y());
996
997         meta_data_lock=false;
998         queue_draw();
999 }
1000
1001 void
1002 WorkArea::set_onion_skin(bool x)
1003 {
1004         if(onion_skin==x)
1005                 return;
1006         onion_skin=x;
1007         save_meta_data();
1008         queue_render_preview();
1009         signal_onion_skin_changed()();
1010 }
1011
1012 bool
1013 WorkArea::get_onion_skin()const
1014 {
1015         return onion_skin;
1016 }
1017
1018 void
1019 WorkArea::enable_grid()
1020 {
1021         show_grid=true;
1022         save_meta_data();
1023         queue_draw();
1024 }
1025
1026 void
1027 WorkArea::disable_grid()
1028 {
1029         show_grid=false;
1030         save_meta_data();
1031         queue_draw();
1032 }
1033
1034 void
1035 WorkArea::set_show_guides(bool x)
1036 {
1037         show_guides=x;
1038         save_meta_data();
1039         queue_draw();
1040 }
1041
1042 void
1043 WorkArea::toggle_grid()
1044 {
1045         show_grid=!show_grid;
1046         save_meta_data();
1047         queue_draw();
1048 }
1049
1050 void
1051 WorkArea::set_low_resolution_flag(bool x)
1052 {
1053         if(x!=low_resolution)
1054         {
1055                 low_resolution=x;
1056                 queue_render_preview();
1057         }
1058 }
1059
1060 void
1061 WorkArea::toggle_low_resolution_flag()
1062 {
1063         set_low_resolution_flag(!get_low_resolution_flag());
1064 }
1065
1066 void
1067 WorkArea::popup_menu()
1068 {
1069         signal_popup_menu()();
1070 }
1071
1072 void
1073 WorkArea::set_grid_size(const synfig::Vector &s)
1074 {
1075         Duckmatic::set_grid_size(s);
1076         save_meta_data();
1077         queue_draw();
1078 }
1079
1080 void
1081 WorkArea::set_focus_point(const synfig::Point &point)
1082 {
1083         // These next three lines try to ensure that we place the
1084         // focus on a pixel boundary
1085         /*Point adjusted(point[0]/abs(get_pw()),point[1]/abs(get_ph()));
1086         adjusted[0]=(abs(adjusted[0]-floor(adjusted[0]))<0.5)?floor(adjusted[0])*abs(get_pw()):ceil(adjusted[0])*abs(get_ph());
1087         adjusted[1]=(abs(adjusted[1]-floor(adjusted[1]))<0.5)?floor(adjusted[1])*abs(get_ph()):ceil(adjusted[1])*abs(get_ph());
1088         */
1089         const synfig::Point& adjusted(point);
1090
1091         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1092         Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1093         Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1094
1095         get_scrollx_adjustment()->set_value(adjusted[0]*x_factor);
1096         get_scrolly_adjustment()->set_value(adjusted[1]*y_factor);
1097 }
1098
1099 synfig::Point
1100 WorkArea::get_focus_point()const
1101 {
1102         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1103         Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1104         Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1105
1106         return synfig::Point(get_scrollx_adjustment()->get_value()*x_factor, get_scrolly_adjustment()->get_value()*y_factor);
1107 }
1108
1109 bool
1110 WorkArea::set_wh(int W, int H,int CHAN)
1111 {
1112         // If our size is already set, don't set it again
1113         if(W==w && H==h && CHAN==bpp)
1114         {
1115                 return true;
1116         }
1117         if(W<=0 || H<=0 || CHAN<=0)
1118                 return false;
1119
1120         assert(W>0);
1121         assert(H>0);
1122         assert(CHAN>0);
1123
1124         // Set all of the parameters
1125         w=W;
1126         h=H;
1127         bpp=CHAN;
1128
1129         refresh_dimension_info();
1130
1131         tile_book.clear();
1132
1133         return true;
1134 }
1135
1136 bool
1137 WorkArea::on_key_press_event(GdkEventKey* event)
1138 {
1139         if(get_selected_ducks().empty())
1140                 return false;
1141
1142         Real multiplier(1.0);
1143
1144         if(Gdk::ModifierType(event->state)&GDK_SHIFT_MASK)
1145                 multiplier=10.0;
1146
1147         Vector nudge;
1148         switch(event->keyval)
1149         {
1150                 case GDK_Left:
1151                         nudge=Vector(-pw,0);
1152                         break;
1153                 case GDK_Right:
1154                         nudge=Vector(pw,0);
1155                         break;
1156                 case GDK_Up:
1157                         nudge=Vector(0,-ph);
1158                         break;
1159                 case GDK_Down:
1160                         nudge=Vector(0,ph);
1161                         break;
1162                 default:
1163                         return false;
1164                         break;
1165         }
1166
1167         synfigapp::Action::PassiveGrouper grouper(instance.get(),"Nudge");
1168
1169         // Grid snap does not apply to nudging
1170         bool grid_snap_holder(get_grid_snap());
1171         bool guide_snap_holder(get_guide_snap());
1172         set_grid_snap(false);
1173
1174         try {
1175                 start_duck_drag(get_selected_duck()->get_trans_point());
1176                 translate_selected_ducks(get_selected_duck()->get_trans_point()+nudge*multiplier);
1177                 end_duck_drag();
1178         }
1179         catch(String)
1180         {
1181                 canvas_view->duck_refresh_flag=true;
1182                 canvas_view->queue_rebuild_ducks();
1183         }
1184
1185         set_grid_snap(grid_snap_holder);
1186         set_guide_snap(guide_snap_holder);
1187
1188         return true;
1189 }
1190
1191 bool
1192 WorkArea::on_drawing_area_event(GdkEvent *event)
1193 {
1194         synfig::Point mouse_pos;
1195     float bezier_click_pos;
1196         const float radius((abs(pw)+abs(ph))*4);
1197         int button_pressed(0);
1198         float pressure(0);
1199         bool is_mouse(false);
1200         Gdk::ModifierType modifier(Gdk::ModifierType(0));
1201
1202         drawing_area->grab_focus();
1203
1204         // Handle input stuff
1205         if(
1206                 event->any.type==GDK_MOTION_NOTIFY ||
1207                 event->any.type==GDK_BUTTON_PRESS ||
1208                 event->any.type==GDK_2BUTTON_PRESS ||
1209                 event->any.type==GDK_3BUTTON_PRESS ||
1210                 event->any.type==GDK_BUTTON_RELEASE
1211         )
1212         {
1213                 GdkDevice *device;
1214                 if(event->any.type==GDK_MOTION_NOTIFY)
1215                 {
1216                         device=event->motion.device;
1217                         modifier=Gdk::ModifierType(event->motion.state);
1218                 }
1219                 else
1220                 {
1221                         device=event->button.device;
1222                         modifier=Gdk::ModifierType(event->button.state);
1223                 }
1224
1225                 // Make sure we recognize the device
1226                 if(curr_input_device)
1227                 {
1228                         if(curr_input_device!=device)
1229                         {
1230                                 assert(device);
1231                                 curr_input_device=device;
1232                                 signal_input_device_changed()(curr_input_device);
1233                         }
1234                 }
1235                 else if(device)
1236                 {
1237                         curr_input_device=device;
1238                         signal_input_device_changed()(curr_input_device);
1239                 }
1240
1241                 assert(curr_input_device);
1242
1243                 // Calculate the position of the
1244                 // input device in canvas coordinates
1245                 // and the buttons
1246                 if(!event->button.axes)
1247                 {
1248                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->button.x,event->button.y)));
1249                         button_pressed=event->button.button;
1250                         pressure=1.0f;
1251                         is_mouse=true;
1252                         if(isnan(event->button.x) || isnan(event->button.y))
1253                                 return false;
1254                 }
1255                 else
1256                 {
1257                         double x(event->button.axes[0]);
1258                         double y(event->button.axes[1]);
1259                         if(isnan(x) || isnan(y))
1260                                 return false;
1261
1262                         pressure=event->button.axes[2];
1263                         //synfig::info("pressure=%f",pressure);
1264                         pressure-=0.04f;
1265                         pressure/=1.0f-0.04f;
1266
1267
1268                         assert(!isnan(pressure));
1269
1270                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)));
1271
1272                         button_pressed=event->button.button;
1273
1274                         if(button_pressed==1 && pressure<0 && (event->any.type!=GDK_BUTTON_RELEASE && event->any.type!=GDK_BUTTON_PRESS))
1275                                 button_pressed=0;
1276                         if(pressure<0)
1277                                 pressure=0;
1278
1279                         //if(event->any.type==GDK_BUTTON_PRESS && button_pressed)
1280                         //      synfig::info("Button pressed on input device = %d",event->button.button);
1281
1282                         //if(event->button.axes[2]>0.1)
1283                         //      button_pressed=1;
1284                         //else
1285                         //      button_pressed=0;
1286                 }
1287         }
1288         // GDK mouse scrolling events
1289         else if(event->any.type==GDK_SCROLL)
1290         {
1291                 // GDK information needed to properly interpret mouse
1292                 // scrolling events are: scroll.state, scroll.x/scroll.y, and
1293                 // scroll.direction. The value of scroll.direction will be
1294                 // obtained later.
1295
1296                 modifier=Gdk::ModifierType(event->scroll.state);
1297                 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->scroll.x,event->scroll.y)));
1298         }
1299
1300         // Handle the renderables
1301         {
1302                 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
1303                 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
1304                 {
1305                         if((*iter)->get_enabled())
1306                                 if((*iter)->event_vfunc(event))
1307                                 {
1308                                         // Event handled. Return true.
1309                                         return true;
1310                                 }
1311                 }
1312         }
1313
1314         // Event hasn't been handled, pass it down
1315         switch(event->type)
1316     {
1317         case GDK_BUTTON_PRESS:
1318                 {
1319                 switch(button_pressed)
1320                 {
1321                 case 1: // Attempt to click on a duck
1322                 {
1323                         etl::handle<Duck> duck;
1324                         dragging=DRAG_NONE;
1325
1326                         if(allow_duck_clicks)
1327                         {
1328                                 duck=find_duck(mouse_pos,radius);
1329
1330                                 if(duck)
1331                                 {
1332                                         // make a note of whether the duck we click on was selected or not
1333                                         if(duck_is_selected(duck))
1334                                                 clicked_duck=duck;
1335                                         else
1336                                         {
1337                                                 clicked_duck=0;
1338                                                 // if CTRL isn't pressed, clicking an unselected duck will unselect all other ducks
1339                                                 if(!(modifier&GDK_CONTROL_MASK))
1340                                                         clear_selected_ducks();
1341                                                 select_duck(duck);
1342                                         }
1343                                 }
1344                         }
1345                         //else
1346                         //      clear_selected_ducks();
1347
1348
1349
1350                         selected_bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1351                         if(duck && duck->get_editable())
1352                         {
1353                                 //get_selected_duck()->signal_user_click(0)();
1354                                 //if(clicked_duck)clicked_duck->signal_user_click(0)();
1355
1356                                 // if the user is holding shift while clicking on a tangent duck, consider splitting the tangent
1357                                 if (event->motion.state&GDK_SHIFT_MASK && duck->get_type() == Duck::TYPE_TANGENT)
1358                                 {
1359                                         synfigapp::ValueDesc value_desc = duck->get_value_desc();
1360
1361                                         // we have the tangent, but need the vertex - that's the parent
1362                                         if (value_desc.parent_is_value_node()) {
1363                                                 ValueNode_Composite::Handle parent_value_node = value_desc.get_parent_value_node();
1364
1365                                                 // if the tangent isn't split, then split it
1366                                                 if (!((*(parent_value_node->get_link("split")))(get_time()).get(bool())))
1367                                                 {
1368                                                         get_canvas_view()->canvas_interface()->
1369                                                                 change_value(synfigapp::ValueDesc(parent_value_node,
1370                                                                                                                                   parent_value_node->get_link_index_from_name("split")),
1371                                                                                          true);
1372                                                         // rebuild the ducks from scratch, so the tangents ducks aren't connected
1373                                                         get_canvas_view()->rebuild_ducks();
1374
1375                                                         // reprocess the mouse click
1376                                                         return on_drawing_area_event(event);
1377                                                 }
1378                                         } else {
1379                                                 // I don't know how to access the vertex from the tangent duck when originally drawing the bline in the bline tool
1380
1381                                                 // synfig::ValueNode::Handle vn = value_desc.get_value_node();
1382                                                 synfig::info("parent isn't value node?  shift-drag-tangent doesn't work in bline tool yet...");
1383                                         }
1384                                 }
1385
1386                                 dragging=DRAG_DUCK;
1387                                 drag_point=mouse_pos;
1388                                 //drawing_area->queue_draw();
1389                                 start_duck_drag(mouse_pos);
1390                                 get_canvas_view()->reset_cancel_status();
1391                                 return true;
1392                         }
1393 // I commented out this section because
1394 // it was causing issues when rotoscoping.
1395 // At the moment, we don't need it, so
1396 // this was the easiest way to fix the problem.
1397 /*
1398                         else
1399                         if(selected_bezier)
1400                         {
1401                                 selected_duck=0;
1402                                 selected_bezier->signal_user_click(0)(bezier_click_pos);
1403                         }
1404 */
1405                         else
1406                         {
1407                                 //clear_selected_ducks();
1408                                 selected_bezier=0;
1409                                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1410                                 {
1411                                         // Check for a guide click
1412                                         GuideList::iterator iter;
1413
1414                                         iter=find_guide_x(mouse_pos,radius);
1415                                         if(iter==get_guide_list_x().end())
1416                                         {
1417                                                 curr_guide_is_x=false;
1418                                                 iter=find_guide_y(mouse_pos,radius);
1419                                         }
1420                                         else
1421                                                 curr_guide_is_x=true;
1422                                         if(iter!=get_guide_list_x().end() && iter!=get_guide_list_y().end())
1423                                         {
1424                                                 dragging=DRAG_GUIDE;
1425                                                 curr_guide=iter;
1426                                                 return true;
1427                                         }
1428
1429
1430                                         // All else fails, try making a selection box
1431                                         dragging=DRAG_BOX;
1432                                         curr_point=drag_point=mouse_pos;
1433                                         return true;
1434                                 }
1435                         }
1436                         break;
1437                 }
1438                 case 2: // Attempt to drag and move the window
1439                 {
1440                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1441                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1442                         if(duck)
1443                                 duck->signal_user_click(1)();
1444                         else
1445                         if(bezier)
1446                                 bezier->signal_user_click(1)(bezier_click_pos);
1447
1448                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1449                         if(is_mouse)
1450                         {
1451                                 dragging=DRAG_WINDOW;
1452                                 drag_point=mouse_pos;
1453                                 signal_user_click(1)(mouse_pos);
1454                         }
1455                         break;
1456                 }
1457                 case 3: // Attempt to either get info on a duck, or open the menu
1458                 {
1459                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1460                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1461
1462                         Layer::Handle layer(get_canvas()->find_layer(mouse_pos));
1463                         if(duck)
1464                         {
1465                                 if(get_selected_ducks().size()<=1)
1466                                         duck->signal_user_click(2)();
1467                                 else
1468                                         canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1469                                 return true;
1470                         }
1471                         else if(bezier)
1472                         {
1473                                 bezier->signal_user_click(2)(bezier_click_pos);
1474                                 return true;
1475                         }
1476                         else if (layer)
1477                         {
1478                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_RIGHT,mouse_pos))==Smach::RESULT_OK)
1479                                         return false;
1480                                 return true;
1481                         }
1482                         else
1483                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1484                         /*
1485                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1486                         {
1487                                 //popup_menu();
1488                                 return true;
1489                         }
1490                         */
1491                         break;
1492                 }
1493                 case 4:
1494                         signal_user_click(3)(mouse_pos);
1495                         break;
1496                 case 5:
1497                         signal_user_click(4)(mouse_pos);
1498                         break;
1499                 default:
1500                         break;
1501                 }
1502                 }
1503                 break;
1504         case GDK_MOTION_NOTIFY:
1505                 curr_point=mouse_pos;
1506
1507                 if(event->motion.time-last_event_time<25)
1508                         return true;
1509                 else
1510                         last_event_time=event->motion.time;
1511
1512                 signal_cursor_moved_();
1513
1514                 // Guide/Duck highlights on hover
1515                 if(dragging==DRAG_NONE)
1516                 {
1517                         GuideList::iterator iter;
1518
1519                         iter=find_guide_x(mouse_pos,radius);
1520                         if(iter==get_guide_list_x().end())
1521                                 iter=find_guide_y(mouse_pos,radius);
1522
1523                         if(iter!=curr_guide)
1524                         {
1525                                 curr_guide=iter;
1526                                 drawing_area->queue_draw();
1527                         }
1528
1529                         etl::handle<Duck> duck;
1530                         duck=find_duck(mouse_pos,radius);
1531                         if(duck!=hover_duck)
1532                         {
1533                                 hover_duck=duck;
1534                                 drawing_area->queue_draw();
1535                         }
1536                 }
1537
1538
1539                 if(dragging==DRAG_DUCK)
1540                 {
1541                         if(canvas_view->get_cancel_status())
1542                         {
1543                                 dragging=DRAG_NONE;
1544                                 canvas_view->queue_rebuild_ducks();
1545                                 return true;
1546                         }
1547                         /*
1548                         Point point((mouse_pos-selected_duck->get_origin())/selected_duck->get_scalar());
1549                         if(get_grid_snap())
1550                         {
1551                                 point[0]=floor(point[0]/grid_size[0]+0.5)*grid_size[0];
1552                                 point[1]=floor(point[1]/grid_size[1]+0.5)*grid_size[1];
1553                         }
1554                         selected_duck->set_point(point);
1555                         */
1556
1557                         //Point p(mouse_pos);
1558
1559                         set_axis_lock(event->motion.state&GDK_SHIFT_MASK);
1560
1561                         translate_selected_ducks(mouse_pos);
1562
1563                         drawing_area->queue_draw();
1564                 }
1565
1566                 if(dragging==DRAG_BOX)
1567                 {
1568                         curr_point=mouse_pos;
1569                         drawing_area->queue_draw();
1570                 }
1571
1572                 if(dragging==DRAG_GUIDE)
1573                 {
1574                         if(curr_guide_is_x)
1575                                 *curr_guide=mouse_pos[0];
1576                         else
1577                                 *curr_guide=mouse_pos[1];
1578                         drawing_area->queue_draw();
1579                 }
1580
1581                 if(dragging!=DRAG_WINDOW)
1582                 {       // Update those triangle things on the rulers
1583                         const synfig::Point point(mouse_pos);
1584                         hruler->property_position()=Distance(point[0],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1585                         vruler->property_position()=Distance(point[1],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1586                 }
1587
1588                 if(dragging == DRAG_WINDOW)
1589                         set_focus_point(get_focus_point() + mouse_pos-drag_point);
1590                 else if (event->motion.state & GDK_BUTTON1_MASK &&
1591                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_LEFT,
1592                                                                                                                                   mouse_pos,pressure,modifier)) == Smach::RESULT_ACCEPT)
1593                         return true;
1594                 else if (event->motion.state & GDK_BUTTON2_MASK &&
1595                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_MIDDLE,
1596                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1597                         return true;
1598                 else if (event->motion.state & GDK_BUTTON3_MASK &&
1599                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_RIGHT,
1600                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1601                         return true;
1602                 else if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_MOTION, BUTTON_NONE,
1603                                                                                                                                   mouse_pos, pressure,modifier)) == Smach::RESULT_ACCEPT)
1604                         return true;
1605
1606                 break;
1607
1608         case GDK_BUTTON_RELEASE:
1609         {
1610                 bool ret(false);
1611
1612                 if(dragging==DRAG_GUIDE)
1613                 {
1614                         dragging=DRAG_NONE;
1615                         save_meta_data();
1616                         return true;
1617                 }
1618                 else
1619                 if(dragging==DRAG_DUCK)
1620                 {
1621                         synfigapp::Action::PassiveGrouper grouper(instance.get(),"Move");
1622                         dragging=DRAG_NONE;
1623                         //translate_selected_ducks(mouse_pos);
1624                         set_axis_lock(false);
1625
1626                         try{
1627                         get_canvas_view()->duck_refresh_flag=false;
1628                         get_canvas_view()->duck_refresh_needed=false;
1629                         const bool drag_did_anything(end_duck_drag());
1630                         get_canvas_view()->duck_refresh_flag=true;
1631                         if(!drag_did_anything)
1632                         {
1633                                 // if we originally clicked on a selected duck ...
1634                                 if(clicked_duck)
1635                                 {
1636                                         // ... and CTRL is pressed, then just toggle the clicked duck
1637                                         //     otherwise make the clicked duck the only selected duck
1638                                         if(modifier&GDK_CONTROL_MASK)
1639                                                 unselect_duck(clicked_duck);
1640                                         else
1641                                         {
1642                                                 clear_selected_ducks();
1643                                                 select_duck(clicked_duck);
1644                                         }
1645                                         clicked_duck->signal_user_click(0)();
1646                                 }
1647                         }
1648                         else
1649                         {
1650                                 if(canvas_view->duck_refresh_needed)
1651                                         canvas_view->queue_rebuild_ducks();
1652                                 return true;
1653                         }
1654                         }catch(String)
1655                         {
1656                                 canvas_view->duck_refresh_flag=true;
1657                                 canvas_view->queue_rebuild_ducks();
1658                                 return true;
1659                         }
1660                         //queue_draw();
1661                         clicked_duck=0;
1662
1663                         ret=true;
1664                 }
1665
1666                 if(dragging==DRAG_BOX)
1667                 {
1668                         dragging=DRAG_NONE;
1669                         if((drag_point-mouse_pos).mag()>radius/2.0f)
1670                         {
1671                                 if(canvas_view->get_smach().process_event(EventBox(drag_point,mouse_pos,MouseButton(event->button.button),modifier))==Smach::RESULT_ACCEPT)
1672                                         return true;
1673
1674                                 // when dragging a box around some ducks:
1675                                 // SHIFT selects; CTRL toggles; SHIFT+CTRL unselects; <none> clears all then selects
1676                                 if(modifier&GDK_SHIFT_MASK)
1677                                         select_ducks_in_box(drag_point,mouse_pos);
1678
1679                                 if(modifier&GDK_CONTROL_MASK)
1680                                         toggle_select_ducks_in_box(drag_point,mouse_pos);
1681                                 else if(!(modifier&GDK_SHIFT_MASK))
1682                                 {
1683                                         clear_selected_ducks();
1684                                         select_ducks_in_box(drag_point,mouse_pos);
1685                                 }
1686                                 ret=true;
1687                         }
1688                         else
1689                         {
1690                                 if(allow_layer_clicks)
1691                                 {
1692                                         Layer::Handle layer(get_canvas()->find_layer(drag_point));
1693                                         //if(layer)
1694                                         {
1695                                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_LEFT,mouse_pos,modifier))==Smach::RESULT_OK)
1696                                                         signal_layer_selected_(layer);
1697                                                 ret=true;
1698                                         }
1699                                 }
1700                                 else
1701                                 {
1702                                         signal_user_click(0)(mouse_pos);
1703                                 }
1704                         }
1705                 }
1706
1707                 dragging=DRAG_NONE;
1708
1709                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_UP,MouseButton(event->button.button),mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1710                         ret=true;
1711
1712                 return ret;
1713         }
1714                 break;
1715         case GDK_SCROLL:
1716         {
1717                 // Handle a mouse scrolling event like Xara Xtreme and
1718                 // Inkscape:
1719
1720                 // Scroll up/down: scroll up/down
1721                 // Shift + scroll up/down: scroll left/right
1722                 // Control + scroll up/down: zoom in/out
1723
1724                 if(modifier&GDK_CONTROL_MASK)
1725                 {
1726
1727                         // The zoom is performed while preserving the pointer
1728                         // position as a fixed point (similarly to Xara Xtreme and
1729                         // Inkscape).
1730
1731                         // The strategy used below is to scroll to the updated
1732                         // position, then zoom. This is easy to implement within
1733                         // the present architecture, but has the disadvantage of
1734                         // triggering multiple visible refreshes. Note: 1.25 is
1735                         // the hard wired ratio in zoom_in()/zoom_out(). The
1736                         // variable "drift" compensates additional inaccuracies in
1737                         // the zoom. There is also an additional minus sign for
1738                         // the inverted y coordinates.
1739
1740                         // FIXME: One might want to figure out where in the code
1741                         // this empirical drift is been introduced.
1742
1743                         const synfig::Point scroll_point(get_scrollx_adjustment()->get_value(),get_scrolly_adjustment()->get_value());
1744                         const double drift = 0.052;
1745
1746                         switch(event->scroll.direction)
1747                         {
1748                                 case GDK_SCROLL_UP:
1749                                 case GDK_SCROLL_RIGHT:
1750                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1.25-(1+drift)));
1751                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1.25-(1+drift)));
1752                                         zoom_in();
1753                                         break;
1754                                 case GDK_SCROLL_DOWN:
1755                                 case GDK_SCROLL_LEFT:
1756                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1/1.25-(1+drift)));
1757                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1/1.25-(1+drift)));
1758                                         zoom_out();
1759                                         break;
1760                                 default:
1761                                         break;
1762                         }
1763                 }
1764                 else if(modifier&GDK_SHIFT_MASK)
1765                 {
1766                         // Scroll in either direction by 20 pixels. Ideally, the
1767                         // amount of pixels per scrolling event should be
1768                         // configurable. Xara Xtreme currently uses an (hard
1769                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1770
1771                         const int scroll_pixel = 20;
1772
1773                         switch(event->scroll.direction)
1774                         {
1775                                 case GDK_SCROLL_UP:
1776                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1777                                         break;
1778                                 case GDK_SCROLL_DOWN:
1779                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1780                                         break;
1781                                 case GDK_SCROLL_LEFT:
1782                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1783                                         break;
1784                                 case GDK_SCROLL_RIGHT:
1785                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1786                                         break;
1787                                 default:
1788                                         break;
1789                         }
1790                 }
1791                 else
1792                 {
1793                         // Scroll in either direction by 20 pixels. Ideally, the
1794                         // amount of pixels per scrolling event should be
1795                         // configurable. Xara Xtreme currently uses an (hard
1796                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1797
1798                         const int scroll_pixel = 20;
1799
1800                         switch(event->scroll.direction)
1801                         {
1802                                 case GDK_SCROLL_UP:
1803                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1804                                         break;
1805                                 case GDK_SCROLL_DOWN:
1806                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1807                                         break;
1808                                 case GDK_SCROLL_LEFT:
1809                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1810                                         break;
1811                                 case GDK_SCROLL_RIGHT:
1812                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1813                                         break;
1814                                 default:
1815                                         break;
1816                         }
1817                 }
1818         }
1819                 break;
1820         default:
1821                 break;
1822         }
1823                 return false;
1824 }
1825
1826 bool
1827 WorkArea::on_hruler_event(GdkEvent */*event*/)
1828 {
1829 /*
1830         switch(event->type)
1831     {
1832         case GDK_BUTTON_PRESS:
1833                 if(dragging==DRAG_NONE)
1834                 {
1835                         dragging=DRAG_GUIDE;
1836                         curr_guide=get_guide_list_y().insert(get_guide_list_y().begin());
1837                         curr_guide_is_x=false;
1838                 }
1839                 return true;
1840                 break;
1841
1842         case GDK_MOTION_NOTIFY:
1843                 // Guide movement
1844                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1845                 {
1846                         double y,x;
1847                         if(event->button.axes)
1848                         {
1849                                 x=(event->button.axes[0]);
1850                                 y=(event->button.axes[1]);
1851                         }
1852                         else
1853                         {
1854                                 x=event->button.x;
1855                                 y=event->button.y;
1856                         }
1857
1858                         if(isnan(y) || isnan(x))
1859                                 return false;
1860
1861                         *curr_guide=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)))[1];
1862
1863                         queue_draw();
1864                 }
1865                 return true;
1866                 break;
1867
1868         case GDK_BUTTON_RELEASE:
1869                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1870                 {
1871                         dragging=DRAG_NONE;
1872                         get_guide_list_y().erase(curr_guide);
1873                 }
1874                 break;
1875                 return true;
1876         default:
1877                 break;
1878         }
1879 */
1880         return false;
1881 }
1882
1883 bool
1884 WorkArea::on_vruler_event(GdkEvent */*event*/)
1885 {
1886 /*
1887         switch(event->type)
1888     {
1889         case GDK_BUTTON_PRESS:
1890                 if(dragging==DRAG_NONE)
1891                 {
1892                         dragging=DRAG_GUIDE;
1893                         curr_guide=get_guide_list_x().insert(get_guide_list_x().begin());
1894                         curr_guide_is_x=true;
1895                 }
1896                 return true;
1897                 break;
1898         case GDK_BUTTON_RELEASE:
1899                 if(dragging==DRAG_GUIDE && curr_guide_is_x==true)
1900                 {
1901                         dragging=DRAG_NONE;
1902                         get_guide_list_x().erase(curr_guide);
1903                 }
1904                 break;
1905                 return true;
1906         default:
1907                 break;
1908         }
1909 */
1910         return false;
1911 }
1912
1913
1914 void
1915 WorkArea::refresh_dimension_info()
1916 {
1917         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1918
1919         canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1920         canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1921
1922         pw=canvaswidth/w;
1923         ph=canvasheight/h;
1924
1925         scrollx_adjustment.set_page_increment(abs(get_grid_size()[0]));
1926         scrollx_adjustment.set_step_increment(abs(pw));
1927         scrollx_adjustment.set_lower(-abs(canvaswidth));
1928         scrollx_adjustment.set_upper(abs(canvaswidth));
1929         scrolly_adjustment.set_lower(-abs(canvasheight));
1930         scrolly_adjustment.set_upper(abs(canvasheight));
1931         scrolly_adjustment.set_step_increment(abs(ph));
1932         scrolly_adjustment.set_page_increment(abs(get_grid_size()[1]));
1933
1934
1935
1936         if(drawing_area->get_width()<=0 || drawing_area->get_height()<=0 || w==0 || h==0)
1937                 return;
1938
1939         const synfig::Point focus_point(get_focus_point());
1940         const synfig::Real x(focus_point[0]/pw+drawing_area->get_width()/2-w/2);
1941         const synfig::Real y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1942
1943         window_tl[0]=rend_desc.get_tl()[0]-pw*x;
1944         window_br[0]=rend_desc.get_br()[0]+pw*(drawing_area->get_width()-x-w);
1945
1946         window_tl[1]=rend_desc.get_tl()[1]-ph*y;
1947         window_br[1]=rend_desc.get_br()[1]+ph*(drawing_area->get_height()-y-h);
1948
1949         hruler->property_lower()=Distance(window_tl[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1950         hruler->property_upper()=Distance(window_br[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1951         vruler->property_lower()=Distance(window_tl[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1952         vruler->property_upper()=Distance(window_br[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1953
1954         view_window_changed();
1955 }
1956
1957
1958 synfig::Point
1959 WorkArea::screen_to_comp_coords(synfig::Point pos)const
1960 {
1961         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1962         //synfig::Vector::value_type canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1963         //synfig::Vector::value_type canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1964         //synfig::Vector::value_type pw=canvaswidth/w;
1965         //synfig::Vector::value_type ph=canvasheight/h;
1966         Vector focus_point=get_focus_point();
1967         synfig::Vector::value_type x=focus_point[0]/pw+drawing_area->get_width()/2-w/2;
1968         synfig::Vector::value_type y=focus_point[1]/ph+drawing_area->get_height()/2-h/2;
1969
1970         return rend_desc.get_tl()-synfig::Point(pw*x,ph*y)+synfig::Point(pw*pos[0],ph*pos[1]);
1971 }
1972
1973 synfig::Point
1974 WorkArea::comp_to_screen_coords(synfig::Point /*pos*/)const
1975 {
1976         synfig::warning("WorkArea::comp_to_screen_coords: Not yet implemented");
1977         return synfig::Point();
1978 }
1979
1980 int
1981 WorkArea::next_unrendered_tile(int refreshes)const
1982 {
1983         //assert(!tile_book.empty());
1984         if(tile_book.empty())
1985                 return -1;
1986
1987         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1988
1989         const synfig::Vector focus_point(get_focus_point());
1990
1991         // Calculate the window coordinates of the top-left
1992         // corner of the canvas.
1993         const synfig::Vector::value_type
1994                 x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
1995                 y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1996
1997         int div = low_res_pixel_size;
1998         const int width_in_tiles(w/tile_w+((low_resolution?((w/div)%(tile_w/div)):(w%tile_w))?1:0));
1999         const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
2000
2001         int
2002                 u(0),v(0),
2003                 u1(int(-x/tile_w)),
2004                 v1(int(-y/tile_h)),
2005                 u2(int((-x+drawing_area->get_width())/tile_w+1)),
2006                 v2(int((-y+drawing_area->get_height())/tile_h+1));
2007
2008         if(u2>width_in_tiles)u2=width_in_tiles;
2009         if(v2>height_in_tiles)v2=height_in_tiles;
2010         if(u1<0)u1=0;
2011         if(v1<0)v1=0;
2012
2013         int last_good_tile(-1);
2014
2015         for(v=v1;v<v2;v++)
2016                 for(u=u1;u<u2;u++)
2017                 {
2018                         int index(v*width_in_tiles+u);
2019                         if(tile_book[index].second<refreshes)
2020                         {
2021                                 last_good_tile=index;
2022                                 if(rand()%8==0)
2023                                         return index;
2024                         }
2025                 }
2026         return last_good_tile;
2027 }
2028
2029 /*
2030 template <typename F, typename T=WorkAreaRenderer, typename R=typename F::result_type>
2031 class handle2ptr_t : public std::unary_function<typename etl::handle<T>,R>
2032 {
2033 private:
2034         F func;
2035 public:
2036         handle2ptr_t(const F &func):func(func) { };
2037
2038         R operator()(typename etl::handle<T> x) { return func(*x); }
2039 };
2040
2041 template <typename F>
2042 handle2ptr_t<F>
2043 handle2ptr(F func)
2044 {
2045         return handle2ptr_t<F>(func);
2046 }
2047         for_each(
2048                 renderer_set_.begin(),
2049                 renderer_set_.end(),
2050                 handle2ptr(
2051                         sigc::bind(
2052                                 sigc::bind(
2053                                         sigc::mem_fun(
2054                                                 &WorkAreaRenderer::render_vfunc
2055                                         ),
2056                                         Gdk::Rectangle(event->area)
2057                                 ),
2058                                 drawing_area->get_window()
2059                         )
2060                 )
2061         );
2062 */
2063
2064 bool
2065 WorkArea::refresh(GdkEventExpose*event)
2066 {
2067         assert(get_canvas());
2068
2069         drawing_area->get_window()->clear();
2070
2071         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2072
2073         const synfig::Vector focus_point(get_focus_point());
2074
2075         // Update the old focus point
2076         last_focus_point=focus_point;
2077
2078         // Draw out the renderables
2079         {
2080                 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
2081                 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
2082                 {
2083                         if((*iter)->get_enabled())
2084                                 (*iter)->render_vfunc(
2085                                         drawing_area->get_window(),
2086                                         Gdk::Rectangle(&event->area)
2087                                 );
2088                 }
2089         }
2090
2091         // Calculate the window coordinates of the top-left
2092         // corner of the canvas.
2093         //const synfig::Vector::value_type
2094         //      x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2095         //      y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2096
2097         //const synfig::Vector::value_type window_startx(window_tl[0]);
2098         //const synfig::Vector::value_type window_endx(window_br[0]);
2099         //const synfig::Vector::value_type window_starty(window_tl[1]);
2100         //const synfig::Vector::value_type window_endy(window_br[1]);
2101
2102         Glib::RefPtr<Gdk::GC> gc=Gdk::GC::create(drawing_area->get_window());
2103
2104         // If we are in animate mode, draw a red border around the screen
2105         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2106         {
2107 // #define USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2108 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2109                 // This method of drawing the red border doesn't work on any
2110                 // Gtk theme which uses the crux-engine, hcengine, industrial,
2111                 // mist, or ubuntulooks engine, such as the default ubuntu
2112                 // 'Human' theme.
2113                 drawing_frame->modify_bg(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2114 #else
2115                 // So let's do it in a more primitive fashion.
2116                 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
2117                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
2118                 drawing_area->get_window()->draw_rectangle(
2119                         gc,
2120                         false,  // Fill?
2121                         0,0,    // x,y
2122                         drawing_area->get_width()-1,drawing_area->get_height()-1); // w,h
2123 #endif
2124         }
2125 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2126         else
2127                 drawing_frame->unset_bg(Gtk::STATE_NORMAL);
2128 #endif
2129
2130         previous_focus=get_focus_point();
2131
2132         return true;
2133 }
2134
2135 void
2136 WorkArea::done_rendering()
2137 {
2138 /*
2139         assert(buffer);
2140         assert(w>0);
2141         assert(h>0);
2142         pix_buf=Gdk::Pixbuf::create_from_data(
2143                 buffer, // pointer to the data
2144                 Gdk::COLORSPACE_RGB, // the colorspace
2145                 true, // has alpha?
2146                 8, // bits per sample
2147                 w,      // width
2148                 h,      // height
2149                 w*bpp); // stride (pitch)
2150         assert(pix_buf);
2151 */
2152 }
2153
2154
2155 void
2156 WorkArea::set_quality(int x)
2157 {
2158         if(x==quality)
2159                 return;
2160         quality=x;
2161         queue_render_preview();
2162 }
2163
2164 void
2165 WorkArea::set_low_res_pixel_size(int x)
2166 {
2167         if(x==low_res_pixel_size)
2168                 return;
2169         low_res_pixel_size=x;
2170         queue_render_preview();
2171 }
2172
2173 namespace studio
2174 {
2175 class WorkAreaProgress : public synfig::ProgressCallback
2176 {
2177         WorkArea *work_area;
2178         ProgressCallback *cb;
2179
2180 public:
2181
2182         WorkAreaProgress(WorkArea *work_area,ProgressCallback *cb):
2183                 work_area(work_area),cb(cb)
2184         {
2185                 assert(cb);
2186         }
2187
2188         virtual bool
2189         task(const std::string &str)
2190         {
2191                 if(work_area->dirty)
2192                         return false;
2193                 return cb->task(str);
2194         }
2195
2196         virtual bool
2197         error(const std::string &err)
2198         {
2199                 if(work_area->dirty)
2200                         return false;
2201                 return cb->error(err);
2202         }
2203
2204         virtual bool
2205         amount_complete(int current, int total)
2206         {
2207                 if(work_area->dirty)
2208                         return false;
2209                 return cb->amount_complete(current,total);
2210         }
2211 };
2212 }
2213
2214 bool
2215 studio::WorkArea::async_update_preview()
2216 {
2217         if (get_updating())
2218         {
2219                 stop_updating();
2220                 queue_render_preview();
2221                 return false;
2222         }
2223
2224         async_renderer=0;
2225
2226         queued=false;
2227         canceled_=false;
2228         get_canvas_view()->reset_cancel_status();
2229
2230         // This object will mark us as busy until
2231         // we are done.
2232         //studio::App::Busy busy;
2233
2234         //WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2235         //synfig::ProgressCallback *cb=&callback;
2236
2237         if(!is_visible())return false;
2238
2239         /*
2240         // If we are queued to render the scene at the next idle
2241         // go ahead and de-queue it.
2242         if(render_idle_func_id)
2243         {
2244                 g_source_remove(render_idle_func_id);
2245                 //queued=false;
2246                 render_idle_func_id=0;
2247         }
2248         */
2249
2250         dirty=false;
2251         get_canvas_view()->reset_cancel_status();
2252
2253         //bool ret=false;
2254         RendDesc desc=get_canvas()->rend_desc();
2255
2256         int w=(int)(desc.get_w()*zoom);
2257         int h=(int)(desc.get_h()*zoom);
2258
2259         // ensure that the size we draw is at least one pixel in each dimension
2260         int min_size = low_resolution ? low_res_pixel_size : 1;
2261         if (w < min_size) w = min_size;
2262         if (h < min_size) h = min_size;
2263
2264         // Setup the description parameters
2265         desc.set_antialias(1);
2266         desc.set_time(cur_time);
2267
2268         set_rend_desc(desc);
2269
2270         // Create the render target
2271         handle<Target> target;
2272
2273         // if we have lots of pixels to render and the tile renderer isn't disabled, use it
2274         int div;
2275         div = low_resolution ? low_res_pixel_size : 1;
2276         if (w*h > 240*div*135*div && !getenv("SYNFIG_DISABLE_TILE_RENDER"))
2277         {
2278                 // do a tile render
2279                 handle<WorkAreaTarget> trgt(new class WorkAreaTarget(this,w,h));
2280
2281                 trgt->set_rend_desc(&desc);
2282                 trgt->set_onion_skin(get_onion_skin());
2283                 target=trgt;
2284         }
2285         else
2286         {
2287                 // do a scanline render
2288                 handle<WorkAreaTarget_Full> trgt(new class WorkAreaTarget_Full(this,w,h));
2289
2290                 trgt->set_rend_desc(&desc);
2291                 trgt->set_onion_skin(get_onion_skin());
2292                 target=trgt;
2293         }
2294
2295         // We can rest assured that our time has already
2296         // been set, so there is no need to have to
2297         // recalculate that over again.
2298         // UPDATE: This is kind of needless with
2299         // the way that time is handled now in SYNFIG.
2300         //target->set_avoid_time_sync(true);
2301         async_renderer=new AsyncRenderer(target);
2302         async_renderer->signal_finished().connect(
2303                 sigc::mem_fun(this,&WorkArea::async_update_finished)
2304         );
2305
2306         rendering=true;
2307         async_renderer->start();
2308
2309         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2310
2311         rendering=true;
2312         cb->task(_("Rendering..."));
2313         rendering=true;
2314
2315         return true;
2316 }
2317
2318 void
2319 studio::WorkArea::async_update_finished()
2320 {
2321         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2322
2323         rendering=false;
2324
2325         if(!async_renderer)
2326                 return;
2327
2328         // If we completed successfully, then
2329         // we aren't dirty anymore
2330         if(async_renderer->has_success())
2331         {
2332                 dirty=false;
2333                 //queued=false;
2334                 cb->task(_("Idle"));
2335         }
2336         else
2337         {
2338                 dirty=true;
2339                 cb->task(_("Render Failed"));
2340         }
2341         //get_canvas_view()->reset_cancel_status();
2342         done_rendering();
2343 }
2344
2345 bool
2346 studio::WorkArea::sync_update_preview()
2347 {
2348         //      const Time &time(cur_time);
2349
2350         canceled_=false;
2351         get_canvas_view()->reset_cancel_status();
2352
2353         async_renderer=0;
2354
2355 again:
2356         // This object will mark us as busy until
2357         // we are done.
2358         studio::App::Busy busy;
2359
2360         WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2361         synfig::ProgressCallback *cb=&callback;
2362
2363         // We don't want to render if we are already rendering
2364         if(rendering)
2365         {
2366                 dirty=true;
2367                 return false;
2368         }
2369
2370         if(!is_visible())return false;
2371         get_canvas()->set_time(get_time());
2372         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2373         signal_rendering()();
2374
2375         // If we are queued to render the scene at the next idle
2376         // go ahead and de-queue it.
2377         if(render_idle_func_id)
2378         {
2379                 g_source_remove(render_idle_func_id);
2380                 //queued=false;
2381                 render_idle_func_id=0;
2382         }
2383         // Start rendering
2384         rendering=true;
2385
2386         dirty=false;
2387         get_canvas_view()->reset_cancel_status();
2388
2389         RendDesc desc=get_canvas()->rend_desc();
2390         //newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2391
2392         int w=(int)(desc.get_w()*zoom);
2393         int h=(int)(desc.get_h()*zoom);
2394
2395         // Setup the description parameters
2396         desc.set_antialias(1);
2397         desc.set_time(cur_time);
2398         //desc.set_wh(w,h);
2399
2400         set_rend_desc(desc);
2401
2402         // Create the render target
2403         handle<WorkAreaTarget> target(new class WorkAreaTarget(this,w,h));
2404
2405         target->set_rend_desc(&desc);
2406
2407         // We can rest assured that our time has already
2408         // been set, so there is no need to have to
2409         // recalculate that over again.
2410         target->set_avoid_time_sync(true);
2411
2412         if(cb)
2413                 cb->task(strprintf(_("Rendering canvas %s..."),get_canvas()->get_name().c_str()));
2414
2415         bool ret = target->render(cb);
2416
2417         if(!ret && !get_canvas_view()->get_cancel_status() && dirty)
2418         {
2419                 rendering=false;
2420                 //canceled_=true;
2421                 goto again;
2422         }
2423         if(get_canvas_view()->get_cancel_status())
2424                 canceled_=true;
2425
2426         if(cb)
2427         {
2428                 if(ret)
2429                         cb->task(_("Idle"));
2430                 else
2431                         cb->task(_("Render Failed"));
2432                 cb->amount_complete(0,1);
2433         }
2434
2435         // Refresh the work area to make sure that
2436         // it is being displayed correctly
2437         drawing_area->queue_draw();
2438
2439         // If we completed successfully, then
2440         // we aren't dirty anymore
2441         if(ret)
2442         {
2443                 dirty=false;
2444                 //queued=false;
2445         }
2446         else dirty=true;
2447         rendering=false;
2448         //get_canvas_view()->reset_cancel_status();
2449         done_rendering();
2450         return ret;
2451 }
2452
2453 void
2454 studio::WorkArea::async_render_preview(synfig::Time time)
2455 {
2456         cur_time=time;
2457         //tile_book.clear();
2458
2459         refreshes+=5;
2460         if(!is_visible())return;
2461
2462         get_canvas()->set_time(get_time());
2463         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2464         signal_rendering()();
2465
2466         async_update_preview();
2467 }
2468 void
2469 WorkArea::async_render_preview()
2470 {
2471         return async_render_preview(get_canvas_view()->get_time());
2472 }
2473
2474 bool
2475 studio::WorkArea::sync_render_preview(synfig::Time time)
2476 {
2477         cur_time=time;
2478         //tile_book.clear();
2479         refreshes+=5;
2480         if(!is_visible())return false;
2481         return sync_update_preview();
2482 }
2483
2484 bool
2485 WorkArea::sync_render_preview()
2486 {
2487         return sync_render_preview(get_canvas_view()->get_time());
2488 }
2489
2490 void
2491 WorkArea::sync_render_preview_hook()
2492 {
2493         sync_render_preview(get_canvas_view()->get_time());
2494 }
2495
2496 void
2497 WorkArea::queue_scroll()
2498 {
2499 //      const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2500
2501         const synfig::Point focus_point(get_focus_point());
2502
2503         const synfig::Real
2504                 new_x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2505                 new_y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2506
2507         const synfig::Real
2508                 old_x(last_focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2509                 old_y(last_focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2510
2511         // If the coordinates didn't change, we shouldn't queue a draw
2512         if(old_x==new_x && old_y==new_y)
2513                 return;
2514
2515         const int
2516                 dx(round_to_int(old_x)-round_to_int(new_x)),
2517                 dy(round_to_int(old_y)-round_to_int(new_y));
2518
2519         drawing_area->get_window()->scroll(-dx,-dy);
2520
2521         if (timecode_width && timecode_height)
2522         {
2523                 drawing_area->queue_draw_area(4,       4,    4+timecode_width,    4+timecode_height);
2524                 drawing_area->queue_draw_area(4-dx, 4-dy, 4-dx+timecode_width, 4-dy+timecode_height);
2525         }
2526
2527 #ifndef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2528         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2529         {
2530                 int maxx = drawing_area->get_width()-1;
2531                 int maxy = drawing_area->get_height()-1;
2532
2533                 if (dx > 0)
2534                 {
2535                         drawing_area->queue_draw_area(      0, 0,       1, maxy);
2536                         drawing_area->queue_draw_area(maxx-dx, 0, maxx-dx, maxy);
2537                 }
2538                 else if (dx < 0) 
2539                 {
2540                         drawing_area->queue_draw_area(   maxx, 0,    maxx, maxy);
2541                         drawing_area->queue_draw_area(    -dx, 0,     -dx, maxy);
2542                 }
2543                 if (dy > 0)
2544                 {
2545                         drawing_area->queue_draw_area(0,       0, maxx,       1);
2546                         drawing_area->queue_draw_area(0, maxy-dy, maxx, maxy-dy);
2547                 }
2548                 else if (dy < 0) 
2549                 {
2550                         drawing_area->queue_draw_area(0,    maxy, maxx,    maxy);
2551                         drawing_area->queue_draw_area(0,     -dy, maxx,     -dy);
2552                 }
2553         }
2554 #endif // USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2555
2556         last_focus_point=focus_point;
2557 }
2558
2559 void
2560 studio::WorkArea::zoom_in()
2561 {
2562         set_zoom(zoom*1.25);
2563 }
2564
2565 void
2566 studio::WorkArea::zoom_out()
2567 {
2568         set_zoom(zoom/1.25);
2569 }
2570
2571 void
2572 studio::WorkArea::zoom_fit()
2573 {
2574         // This really doesn't zoom to fit. Bug.
2575         zoom_norm();
2576 }
2577
2578 void
2579 studio::WorkArea::zoom_norm()
2580 {
2581         if(zoom==1.0)
2582                 set_zoom(prev_zoom);
2583         else
2584         {
2585                 prev_zoom=zoom;
2586                 set_zoom(1.0f);
2587         }
2588 }
2589
2590 gboolean
2591 studio::WorkArea::__render_preview(gpointer data)
2592 {
2593         WorkArea *work_area(static_cast<WorkArea*>(data));
2594
2595         // there's no point anyone trying to cancel the timer now - it's gone off already
2596         work_area->render_idle_func_id = 0;
2597
2598         work_area->queued=false;
2599         work_area->async_render_preview(work_area->get_canvas_view()->get_time());
2600
2601         return 0;
2602 }
2603
2604 void
2605 studio::WorkArea::queue_render_preview()
2606 {
2607         //synfig::info("queue_render_preview(): called for %s", get_canvas_view()->get_time().get_string().c_str());
2608
2609         if(queued==true)
2610         {
2611                 return;
2612                 //synfig::info("queue_render_preview(): already queued, unqueuing");
2613 /*              if(render_idle_func_id)
2614                         g_source_remove(render_idle_func_id);
2615                 render_idle_func_id=0;
2616                 queued=false;
2617 */
2618                 //async_renderer=0;
2619         }
2620
2621         if(dirty_trap_enabled)
2622         {
2623                 dirty_trap_queued++;
2624                 return;
2625         }
2626
2627         int queue_time=50;
2628
2629         if(rendering)
2630                 queue_time+=250;
2631
2632
2633         if(queued==false)
2634         {
2635                 //synfig::info("queue_render_preview(): (re)queuing...");
2636                 //render_idle_func_id=g_idle_add_full(G_PRIORITY_DEFAULT,__render_preview,this,NULL);
2637                 render_idle_func_id=g_timeout_add_full(
2638                         G_PRIORITY_DEFAULT,     // priority - 
2639                         queue_time,                     // interval - the time between calls to the function, in milliseconds (1/1000ths of a second)
2640                         __render_preview,       // function - function to call
2641                         this,                           // data     - data to pass to function
2642                         NULL);                          // notify   - function to call when the idle is removed, or NULL
2643                 queued=true;
2644         }
2645 /*      else if(rendering)
2646         {
2647                 refreshes+=5;
2648                 dirty=true;
2649                 queue_draw();
2650         }
2651 */
2652 }
2653
2654 DirtyTrap::DirtyTrap(WorkArea *work_area):work_area(work_area)
2655 {
2656         work_area->dirty_trap_enabled=true;
2657
2658         work_area->dirty_trap_queued=0;
2659 }
2660
2661 DirtyTrap::~DirtyTrap()
2662 {
2663         work_area->dirty_trap_enabled=false;
2664         if(work_area->dirty_trap_queued)
2665                 work_area->queue_render_preview();
2666 }
2667
2668 void
2669 studio::WorkArea::queue_draw_preview()
2670 {
2671         drawing_area->queue_draw();
2672 }
2673
2674 void
2675 studio::WorkArea::set_cursor(const Gdk::Cursor& x)
2676 {
2677         drawing_area->get_window()->set_cursor(x);
2678 }
2679 void
2680 studio::WorkArea::set_cursor(Gdk::CursorType x)
2681 {
2682         drawing_area->get_window()->set_cursor(Gdk::Cursor(x));
2683 }
2684
2685 #include "iconcontroller.h"
2686
2687 void
2688 studio::WorkArea::refresh_cursor()
2689 {
2690 //      set_cursor(IconController::get_tool_cursor(canvas_view->get_smach().get_state_name(),drawing_area->get_window()));
2691 }
2692
2693 void
2694 studio::WorkArea::reset_cursor()
2695 {
2696         drawing_area->get_window()->set_cursor(Gdk::Cursor(Gdk::TOP_LEFT_ARROW));
2697 //      set_cursor(Gdk::TOP_LEFT_ARROW);
2698 }
2699
2700 void
2701 studio::WorkArea::set_zoom(float z)
2702 {
2703         z=max(1.0f/128.0f,min(128.0f,z));
2704         if(z==zoom)
2705                 return;
2706         zoom = z;
2707         refresh_dimension_info();
2708         /*if(async_renderer)
2709         {
2710                 async_renderer->stop();
2711                 async_renderer=0;
2712         }*/
2713         refreshes+=5;
2714         async_update_preview();
2715         //queue_render_preview();
2716 }
2717
2718 void
2719 WorkArea::set_selected_value_node(etl::loose_handle<synfig::ValueNode> x)
2720 {
2721         if(x!=selected_value_node_)
2722         {
2723                 selected_value_node_=x;
2724                 queue_draw();
2725         }
2726 }
2727
2728 void
2729 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x)
2730 {
2731         renderer_set_.insert(x);
2732         x->set_work_area(this);
2733         queue_draw();
2734 }
2735
2736 void
2737 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x, int priority)
2738 {
2739         x->set_priority(priority);
2740         insert_renderer(x);
2741 }
2742
2743 void
2744 WorkArea::erase_renderer(const etl::handle<WorkAreaRenderer> &x)
2745 {
2746         x->set_work_area(0);
2747         renderer_set_.erase(x);
2748         queue_draw();
2749 }
2750
2751 void
2752 WorkArea::resort_render_set()
2753 {
2754         std::set<etl::handle<WorkAreaRenderer> > tmp(
2755                 renderer_set_.begin(),
2756                 renderer_set_.end()
2757         );
2758         renderer_set_.swap(tmp);
2759         queue_draw();
2760 }