Add 2 new valuenode types: "Angle String" and "Int String".
[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 (c) 2006 Yue Shi Lai
10 **      Copyright (c) 2007, 2008 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 #ifdef SINGLE_THREADED
832 bool
833 WorkArea::get_updating()const
834 {
835         return App::single_threaded && async_renderer && async_renderer->updating;
836 }
837 #endif
838
839 #ifdef SINGLE_THREADED
840 void
841 WorkArea::stop_updating(bool cancel)
842 {
843         async_renderer->stop();
844         if (cancel) canceled_=true;
845 }
846 #endif
847
848 void
849 WorkArea::save_meta_data()
850 {
851         if(meta_data_lock)
852                 return;
853         meta_data_lock=true;
854
855         Vector s(get_grid_size());
856         canvas_interface->set_meta_data("grid_size",strprintf("%f %f",s[0],s[1]));
857         canvas_interface->set_meta_data("grid_snap",get_grid_snap()?"1":"0");
858         canvas_interface->set_meta_data("guide_snap",get_guide_snap()?"1":"0");
859         canvas_interface->set_meta_data("guide_show",get_show_guides()?"1":"0");
860         canvas_interface->set_meta_data("grid_show",show_grid?"1":"0");
861         canvas_interface->set_meta_data("onion_skin",onion_skin?"1":"0");
862         {
863                 String data;
864                 GuideList::const_iterator iter;
865                 for(iter=get_guide_list_x().begin();iter!=get_guide_list_x().end();++iter)
866                 {
867                         if(!data.empty())
868                                 data+=' ';
869                         data+=strprintf("%f",*iter);
870                 }
871                 if(!data.empty())
872                         canvas_interface->set_meta_data("guide_x",data);
873
874                 data.clear();
875                 for(iter=get_guide_list_y().begin();iter!=get_guide_list_y().end();++iter)
876                 {
877                         if(!data.empty())
878                                 data+=' ';
879                         data+=strprintf("%f",*iter);
880                 }
881                 if(!data.empty())
882                         canvas_interface->set_meta_data("guide_y",data);
883         }
884
885         if(get_sketch_filename().size())
886         {
887                 if(dirname(canvas->get_file_name())==dirname(get_sketch_filename()))
888                         canvas_interface->set_meta_data("sketch",basename(get_sketch_filename()));
889                 else
890                         canvas_interface->set_meta_data("sketch",get_sketch_filename());
891         }
892
893         meta_data_lock=false;
894 }
895
896 void
897 WorkArea::load_meta_data()
898 {
899         if(meta_data_lock)
900                 return;
901         meta_data_lock=true;
902
903         String data;
904
905         data=canvas->get_meta_data("grid_size");
906         if(!data.empty())
907         {
908                 float gx(get_grid_size()[0]),gy(get_grid_size()[1]);
909
910                 String::iterator iter(find(data.begin(),data.end(),' '));
911                 String tmp(data.begin(),iter);
912
913                 if(!tmp.empty())
914                         gx=stratof(tmp);
915                 else
916                         synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
917
918                 if(iter==data.end())
919                         tmp.clear();
920                 else
921                         tmp=String(iter+1,data.end());
922
923                 if(!tmp.empty())
924                         gy=stratof(tmp);
925                 else
926                         synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
927
928                 set_grid_size(Vector(gx,gy));
929         }
930
931         data=canvas->get_meta_data("grid_show");
932         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
933                 show_grid=true;
934         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
935                 show_grid=false;
936
937         data=canvas->get_meta_data("solid_lines");
938         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
939                 solid_lines=true;
940         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
941                 solid_lines=false;
942
943         data=canvas->get_meta_data("guide_show");
944         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
945                 show_guides=true;
946         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
947                 show_guides=false;
948
949         data=canvas->get_meta_data("grid_snap");
950         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
951                 set_grid_snap(true);
952         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
953                 set_grid_snap(false);
954
955         data=canvas->get_meta_data("guide_snap");
956         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
957                 set_guide_snap(true);
958         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
959                 set_guide_snap(false);
960
961         data=canvas->get_meta_data("onion_skin");
962         if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
963                 set_onion_skin(true);
964         if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
965                 set_onion_skin(false);
966
967         data=canvas->get_meta_data("guide_x");
968         get_guide_list_x().clear();
969         while(!data.empty())
970         {
971                 String::iterator iter(find(data.begin(),data.end(),' '));
972                 String guide(data.begin(),iter);
973
974                 if(!guide.empty())
975                         get_guide_list_x().push_back(stratof(guide));
976
977                 if(iter==data.end())
978                         data.clear();
979                 else
980                         data=String(iter+1,data.end());
981         }
982         //sort(get_guide_list_x());
983
984         data=canvas->get_meta_data("guide_y");
985         get_guide_list_y().clear();
986         while(!data.empty())
987         {
988                 String::iterator iter(find(data.begin(),data.end(),' '));
989                 String guide(data.begin(),iter);
990
991                 if(!guide.empty())
992                         get_guide_list_y().push_back(stratof(guide));
993
994                 if(iter==data.end())
995                         data.clear();
996                 else
997                         data=String(iter+1,data.end());
998         }
999         //sort(get_guide_list_y());
1000
1001         meta_data_lock=false;
1002         queue_draw();
1003 }
1004
1005 void
1006 WorkArea::set_onion_skin(bool x)
1007 {
1008         if(onion_skin==x)
1009                 return;
1010         onion_skin=x;
1011         save_meta_data();
1012         queue_render_preview();
1013         signal_onion_skin_changed()();
1014 }
1015
1016 bool
1017 WorkArea::get_onion_skin()const
1018 {
1019         return onion_skin;
1020 }
1021
1022 void
1023 WorkArea::enable_grid()
1024 {
1025         show_grid=true;
1026         save_meta_data();
1027         queue_draw();
1028 }
1029
1030 void
1031 WorkArea::disable_grid()
1032 {
1033         show_grid=false;
1034         save_meta_data();
1035         queue_draw();
1036 }
1037
1038 void
1039 WorkArea::set_show_guides(bool x)
1040 {
1041         show_guides=x;
1042         save_meta_data();
1043         queue_draw();
1044 }
1045
1046 void
1047 WorkArea::toggle_grid()
1048 {
1049         show_grid=!show_grid;
1050         save_meta_data();
1051         queue_draw();
1052 }
1053
1054 void
1055 WorkArea::set_low_resolution_flag(bool x)
1056 {
1057         if(x!=low_resolution)
1058         {
1059                 low_resolution=x;
1060                 queue_render_preview();
1061         }
1062 }
1063
1064 void
1065 WorkArea::toggle_low_resolution_flag()
1066 {
1067         set_low_resolution_flag(!get_low_resolution_flag());
1068 }
1069
1070 void
1071 WorkArea::popup_menu()
1072 {
1073         signal_popup_menu()();
1074 }
1075
1076 void
1077 WorkArea::set_grid_size(const synfig::Vector &s)
1078 {
1079         Duckmatic::set_grid_size(s);
1080         save_meta_data();
1081         queue_draw();
1082 }
1083
1084 void
1085 WorkArea::set_focus_point(const synfig::Point &point)
1086 {
1087         // These next three lines try to ensure that we place the
1088         // focus on a pixel boundary
1089         /*Point adjusted(point[0]/abs(get_pw()),point[1]/abs(get_ph()));
1090         adjusted[0]=(abs(adjusted[0]-floor(adjusted[0]))<0.5)?floor(adjusted[0])*abs(get_pw()):ceil(adjusted[0])*abs(get_ph());
1091         adjusted[1]=(abs(adjusted[1]-floor(adjusted[1]))<0.5)?floor(adjusted[1])*abs(get_ph()):ceil(adjusted[1])*abs(get_ph());
1092         */
1093         const synfig::Point& adjusted(point);
1094
1095         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1096         Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1097         Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1098
1099         get_scrollx_adjustment()->set_value(adjusted[0]*x_factor);
1100         get_scrolly_adjustment()->set_value(adjusted[1]*y_factor);
1101 }
1102
1103 synfig::Point
1104 WorkArea::get_focus_point()const
1105 {
1106         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1107         Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1108         Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1109
1110         return synfig::Point(get_scrollx_adjustment()->get_value()*x_factor, get_scrolly_adjustment()->get_value()*y_factor);
1111 }
1112
1113 bool
1114 WorkArea::set_wh(int W, int H,int CHAN)
1115 {
1116         // If our size is already set, don't set it again
1117         if(W==w && H==h && CHAN==bpp)
1118         {
1119                 return true;
1120         }
1121         if(W<=0 || H<=0 || CHAN<=0)
1122                 return false;
1123
1124         assert(W>0);
1125         assert(H>0);
1126         assert(CHAN>0);
1127
1128         // Set all of the parameters
1129         w=W;
1130         h=H;
1131         bpp=CHAN;
1132
1133         refresh_dimension_info();
1134
1135         tile_book.clear();
1136
1137         return true;
1138 }
1139
1140 bool
1141 WorkArea::on_key_press_event(GdkEventKey* event)
1142 {
1143         if(get_selected_ducks().empty())
1144                 return false;
1145
1146         Real multiplier(1.0);
1147
1148         if(Gdk::ModifierType(event->state)&GDK_SHIFT_MASK)
1149                 multiplier=10.0;
1150
1151         Vector nudge;
1152         switch(event->keyval)
1153         {
1154                 case GDK_Left:
1155                         nudge=Vector(-pw,0);
1156                         break;
1157                 case GDK_Right:
1158                         nudge=Vector(pw,0);
1159                         break;
1160                 case GDK_Up:
1161                         nudge=Vector(0,-ph);
1162                         break;
1163                 case GDK_Down:
1164                         nudge=Vector(0,ph);
1165                         break;
1166                 default:
1167                         return false;
1168                         break;
1169         }
1170
1171         synfigapp::Action::PassiveGrouper grouper(instance.get(),_("Nudge"));
1172
1173         // Grid snap does not apply to nudging
1174         bool grid_snap_holder(get_grid_snap());
1175         bool guide_snap_holder(get_guide_snap());
1176         set_grid_snap(false);
1177
1178         try {
1179                 start_duck_drag(get_selected_duck()->get_trans_point());
1180                 translate_selected_ducks(get_selected_duck()->get_trans_point()+nudge*multiplier);
1181                 end_duck_drag();
1182         }
1183         catch(String)
1184         {
1185                 canvas_view->duck_refresh_flag=true;
1186                 canvas_view->queue_rebuild_ducks();
1187         }
1188
1189         set_grid_snap(grid_snap_holder);
1190         set_guide_snap(guide_snap_holder);
1191
1192         return true;
1193 }
1194
1195 bool
1196 WorkArea::on_drawing_area_event(GdkEvent *event)
1197 {
1198         synfig::Point mouse_pos;
1199     float bezier_click_pos;
1200         const float radius((abs(pw)+abs(ph))*4);
1201         int button_pressed(0);
1202         float pressure(0);
1203         bool is_mouse(false);
1204         Gdk::ModifierType modifier(Gdk::ModifierType(0));
1205
1206         // Handle input stuff
1207         if(
1208                 event->any.type==GDK_MOTION_NOTIFY ||
1209                 event->any.type==GDK_BUTTON_PRESS ||
1210                 event->any.type==GDK_2BUTTON_PRESS ||
1211                 event->any.type==GDK_3BUTTON_PRESS ||
1212                 event->any.type==GDK_BUTTON_RELEASE
1213         )
1214         {
1215                 GdkDevice *device;
1216                 if(event->any.type==GDK_MOTION_NOTIFY)
1217                 {
1218                         device=event->motion.device;
1219                         modifier=Gdk::ModifierType(event->motion.state);
1220                 }
1221                 else
1222                 {
1223                         device=event->button.device;
1224                         modifier=Gdk::ModifierType(event->button.state);
1225                         drawing_area->grab_focus();
1226                 }
1227
1228                 // Make sure we recognize the device
1229                 if(curr_input_device)
1230                 {
1231                         if(curr_input_device!=device)
1232                         {
1233                                 assert(device);
1234                                 curr_input_device=device;
1235                                 signal_input_device_changed()(curr_input_device);
1236                         }
1237                 }
1238                 else if(device)
1239                 {
1240                         curr_input_device=device;
1241                         signal_input_device_changed()(curr_input_device);
1242                 }
1243
1244                 assert(curr_input_device);
1245
1246                 // Calculate the position of the
1247                 // input device in canvas coordinates
1248                 // and the buttons
1249                 if(!event->button.axes)
1250                 {
1251                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->button.x,event->button.y)));
1252                         button_pressed=event->button.button;
1253                         pressure=1.0f;
1254                         is_mouse=true;
1255                         if(isnan(event->button.x) || isnan(event->button.y))
1256                                 return false;
1257                 }
1258                 else
1259                 {
1260                         double x(event->button.axes[0]);
1261                         double y(event->button.axes[1]);
1262                         if(isnan(x) || isnan(y))
1263                                 return false;
1264
1265                         pressure=event->button.axes[2];
1266                         //synfig::info("pressure=%f",pressure);
1267                         pressure-=0.04f;
1268                         pressure/=1.0f-0.04f;
1269
1270
1271                         assert(!isnan(pressure));
1272
1273                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)));
1274
1275                         button_pressed=event->button.button;
1276
1277                         if(button_pressed==1 && pressure<0 && (event->any.type!=GDK_BUTTON_RELEASE && event->any.type!=GDK_BUTTON_PRESS))
1278                                 button_pressed=0;
1279                         if(pressure<0)
1280                                 pressure=0;
1281
1282                         //if(event->any.type==GDK_BUTTON_PRESS && button_pressed)
1283                         //      synfig::info("Button pressed on input device = %d",event->button.button);
1284
1285                         //if(event->button.axes[2]>0.1)
1286                         //      button_pressed=1;
1287                         //else
1288                         //      button_pressed=0;
1289                 }
1290         }
1291         // GDK mouse scrolling events
1292         else if(event->any.type==GDK_SCROLL)
1293         {
1294                 // GDK information needed to properly interpret mouse
1295                 // scrolling events are: scroll.state, scroll.x/scroll.y, and
1296                 // scroll.direction. The value of scroll.direction will be
1297                 // obtained later.
1298
1299                 modifier=Gdk::ModifierType(event->scroll.state);
1300                 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->scroll.x,event->scroll.y)));
1301         }
1302
1303         // Handle the renderables
1304         {
1305                 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
1306                 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
1307                 {
1308                         if((*iter)->get_enabled())
1309                                 if((*iter)->event_vfunc(event))
1310                                 {
1311                                         // Event handled. Return true.
1312                                         return true;
1313                                 }
1314                 }
1315         }
1316
1317         // Event hasn't been handled, pass it down
1318         switch(event->type)
1319     {
1320         case GDK_BUTTON_PRESS:
1321                 {
1322                 switch(button_pressed)
1323                 {
1324                 case 1: // Attempt to click on a duck
1325                 {
1326                         etl::handle<Duck> duck;
1327                         dragging=DRAG_NONE;
1328
1329                         if(allow_duck_clicks)
1330                         {
1331                                 duck=find_duck(mouse_pos,radius);
1332
1333                                 if(duck)
1334                                 {
1335                                         // make a note of whether the duck we click on was selected or not
1336                                         if(duck_is_selected(duck))
1337                                                 clicked_duck=duck;
1338                                         else
1339                                         {
1340                                                 clicked_duck=0;
1341                                                 // if CTRL isn't pressed, clicking an unselected duck will unselect all other ducks
1342                                                 if(!(modifier&GDK_CONTROL_MASK))
1343                                                         clear_selected_ducks();
1344                                                 select_duck(duck);
1345                                         }
1346                                 }
1347                         }
1348                         //else
1349                         //      clear_selected_ducks();
1350
1351
1352
1353                         selected_bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1354                         if(duck)
1355                         {
1356                                 if (!duck->get_editable())
1357                                         return true;
1358
1359                                 //get_selected_duck()->signal_user_click(0)();
1360                                 //if(clicked_duck)clicked_duck->signal_user_click(0)();
1361
1362                                 // if the user is holding shift while clicking on a tangent duck, consider splitting the tangent
1363                                 if (event->motion.state&GDK_SHIFT_MASK && duck->get_type() == Duck::TYPE_TANGENT)
1364                                 {
1365                                         synfigapp::ValueDesc value_desc = duck->get_value_desc();
1366
1367                                         // we have the tangent, but need the vertex - that's the parent
1368                                         if (value_desc.parent_is_value_node()) {
1369                                                 ValueNode_Composite::Handle parent_value_node = value_desc.get_parent_value_node();
1370
1371                                                 // if the tangent isn't split, then split it
1372                                                 if (!((*(parent_value_node->get_link("split")))(get_time()).get(bool())))
1373                                                 {
1374                                                         get_canvas_view()->canvas_interface()->
1375                                                                 change_value(synfigapp::ValueDesc(parent_value_node,
1376                                                                                                                                   parent_value_node->get_link_index_from_name("split")),
1377                                                                                          true);
1378                                                         // rebuild the ducks from scratch, so the tangents ducks aren't connected
1379                                                         get_canvas_view()->rebuild_ducks();
1380
1381                                                         // reprocess the mouse click
1382                                                         return on_drawing_area_event(event);
1383                                                 }
1384                                         } else {
1385                                                 // I don't know how to access the vertex from the tangent duck when originally drawing the bline in the bline tool
1386
1387                                                 // synfig::ValueNode::Handle vn = value_desc.get_value_node();
1388                                                 synfig::info("parent isn't value node?  shift-drag-tangent doesn't work in bline tool yet...");
1389                                         }
1390                                 }
1391
1392                                 dragging=DRAG_DUCK;
1393                                 drag_point=mouse_pos;
1394                                 //drawing_area->queue_draw();
1395                                 start_duck_drag(mouse_pos);
1396                                 get_canvas_view()->reset_cancel_status();
1397                                 return true;
1398                         }
1399 // I commented out this section because
1400 // it was causing issues when rotoscoping.
1401 // At the moment, we don't need it, so
1402 // this was the easiest way to fix the problem.
1403 /*
1404                         else
1405                         if(selected_bezier)
1406                         {
1407                                 selected_duck=0;
1408                                 selected_bezier->signal_user_click(0)(bezier_click_pos);
1409                         }
1410 */
1411                         else
1412                         {
1413                                 //clear_selected_ducks();
1414                                 selected_bezier=0;
1415                                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1416                                 {
1417                                         // Check for a guide click
1418                                         GuideList::iterator iter;
1419
1420                                         iter=find_guide_x(mouse_pos,radius);
1421                                         if(iter==get_guide_list_x().end())
1422                                         {
1423                                                 curr_guide_is_x=false;
1424                                                 iter=find_guide_y(mouse_pos,radius);
1425                                         }
1426                                         else
1427                                                 curr_guide_is_x=true;
1428                                         if(iter!=get_guide_list_x().end() && iter!=get_guide_list_y().end())
1429                                         {
1430                                                 dragging=DRAG_GUIDE;
1431                                                 curr_guide=iter;
1432                                                 return true;
1433                                         }
1434
1435
1436                                         // All else fails, try making a selection box
1437                                         dragging=DRAG_BOX;
1438                                         curr_point=drag_point=mouse_pos;
1439                                         return true;
1440                                 }
1441                         }
1442                         break;
1443                 }
1444                 case 2: // Attempt to drag and move the window
1445                 {
1446                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1447                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1448                         if(duck)
1449                                 duck->signal_user_click(1)();
1450                         else
1451                         if(bezier)
1452                                 bezier->signal_user_click(1)(bezier_click_pos);
1453
1454                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1455                         if(is_mouse)
1456                         {
1457                                 dragging=DRAG_WINDOW;
1458                                 drag_point=mouse_pos;
1459                                 signal_user_click(1)(mouse_pos);
1460                         }
1461                         break;
1462                 }
1463                 case 3: // Attempt to either get info on a duck, or open the menu
1464                 {
1465                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1466                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1467
1468                         Layer::Handle layer(get_canvas()->find_layer(mouse_pos));
1469                         if(duck)
1470                         {
1471                                 if(get_selected_ducks().size()<=1)
1472                                         duck->signal_user_click(2)();
1473                                 else
1474                                         canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1475                                 return true;
1476                         }
1477                         else if(bezier)
1478                         {
1479                                 bezier->signal_user_click(2)(bezier_click_pos);
1480                                 return true;
1481                         }
1482                         else if (layer)
1483                         {
1484                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_RIGHT,mouse_pos))==Smach::RESULT_OK)
1485                                         return false;
1486                                 return true;
1487                         }
1488                         else
1489                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1490                         /*
1491                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1492                         {
1493                                 //popup_menu();
1494                                 return true;
1495                         }
1496                         */
1497                         break;
1498                 }
1499                 case 4:
1500                         signal_user_click(3)(mouse_pos);
1501                         break;
1502                 case 5:
1503                         signal_user_click(4)(mouse_pos);
1504                         break;
1505                 default:
1506                         break;
1507                 }
1508                 }
1509                 break;
1510         case GDK_MOTION_NOTIFY:
1511                 curr_point=mouse_pos;
1512
1513                 if(event->motion.time-last_event_time<25)
1514                         return true;
1515                 else
1516                         last_event_time=event->motion.time;
1517
1518                 signal_cursor_moved_();
1519
1520                 // Guide/Duck highlights on hover
1521                 if(dragging==DRAG_NONE)
1522                 {
1523                         GuideList::iterator iter;
1524
1525                         iter=find_guide_x(mouse_pos,radius);
1526                         if(iter==get_guide_list_x().end())
1527                                 iter=find_guide_y(mouse_pos,radius);
1528
1529                         if(iter!=curr_guide)
1530                         {
1531                                 curr_guide=iter;
1532                                 drawing_area->queue_draw();
1533                         }
1534
1535                         etl::handle<Duck> duck;
1536                         duck=find_duck(mouse_pos,radius);
1537                         if(duck!=hover_duck)
1538                         {
1539                                 hover_duck=duck;
1540                                 drawing_area->queue_draw();
1541                         }
1542                 }
1543
1544
1545                 if(dragging==DRAG_DUCK)
1546                 {
1547                         if(canvas_view->get_cancel_status())
1548                         {
1549                                 dragging=DRAG_NONE;
1550                                 canvas_view->queue_rebuild_ducks();
1551                                 return true;
1552                         }
1553                         /*
1554                         Point point((mouse_pos-selected_duck->get_origin())/selected_duck->get_scalar());
1555                         if(get_grid_snap())
1556                         {
1557                                 point[0]=floor(point[0]/grid_size[0]+0.5)*grid_size[0];
1558                                 point[1]=floor(point[1]/grid_size[1]+0.5)*grid_size[1];
1559                         }
1560                         selected_duck->set_point(point);
1561                         */
1562
1563                         //Point p(mouse_pos);
1564
1565                         set_axis_lock(event->motion.state&GDK_SHIFT_MASK);
1566
1567                         translate_selected_ducks(mouse_pos);
1568
1569                         drawing_area->queue_draw();
1570                 }
1571
1572                 if(dragging==DRAG_BOX)
1573                 {
1574                         curr_point=mouse_pos;
1575                         drawing_area->queue_draw();
1576                 }
1577
1578                 if(dragging==DRAG_GUIDE)
1579                 {
1580                         if(curr_guide_is_x)
1581                                 *curr_guide=mouse_pos[0];
1582                         else
1583                                 *curr_guide=mouse_pos[1];
1584                         drawing_area->queue_draw();
1585                 }
1586
1587                 if(dragging!=DRAG_WINDOW)
1588                 {       // Update those triangle things on the rulers
1589                         const synfig::Point point(mouse_pos);
1590                         hruler->property_position()=Distance(point[0],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1591                         vruler->property_position()=Distance(point[1],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1592                 }
1593
1594                 if(dragging == DRAG_WINDOW)
1595                         set_focus_point(get_focus_point() + mouse_pos-drag_point);
1596                 else if (event->motion.state & GDK_BUTTON1_MASK &&
1597                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_LEFT,
1598                                                                                                                                   mouse_pos,pressure,modifier)) == Smach::RESULT_ACCEPT)
1599                         return true;
1600                 else if (event->motion.state & GDK_BUTTON2_MASK &&
1601                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_MIDDLE,
1602                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1603                         return true;
1604                 else if (event->motion.state & GDK_BUTTON3_MASK &&
1605                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_RIGHT,
1606                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1607                         return true;
1608                 else if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_MOTION, BUTTON_NONE,
1609                                                                                                                                   mouse_pos, pressure,modifier)) == Smach::RESULT_ACCEPT)
1610                         return true;
1611
1612                 break;
1613
1614         case GDK_BUTTON_RELEASE:
1615         {
1616                 bool ret(false);
1617
1618                 if(dragging==DRAG_GUIDE)
1619                 {
1620                         dragging=DRAG_NONE;
1621                         save_meta_data();
1622                         return true;
1623                 }
1624                 else
1625                 if(dragging==DRAG_DUCK)
1626                 {
1627                         synfigapp::Action::PassiveGrouper grouper(instance.get(),_("Move"));
1628                         dragging=DRAG_NONE;
1629                         //translate_selected_ducks(mouse_pos);
1630                         set_axis_lock(false);
1631
1632                         try{
1633                         get_canvas_view()->duck_refresh_flag=false;
1634                         get_canvas_view()->duck_refresh_needed=false;
1635                         const bool drag_did_anything(end_duck_drag());
1636                         get_canvas_view()->duck_refresh_flag=true;
1637                         if(!drag_did_anything)
1638                         {
1639                                 // if we originally clicked on a selected duck ...
1640                                 if(clicked_duck)
1641                                 {
1642                                         // ... and CTRL is pressed, then just toggle the clicked duck
1643                                         //     otherwise make the clicked duck the only selected duck
1644                                         if(modifier&GDK_CONTROL_MASK)
1645                                                 unselect_duck(clicked_duck);
1646                                         else
1647                                         {
1648                                                 clear_selected_ducks();
1649                                                 select_duck(clicked_duck);
1650                                         }
1651                                         clicked_duck->signal_user_click(0)();
1652                                 }
1653                         }
1654                         else
1655                         {
1656                                 if(canvas_view->duck_refresh_needed)
1657                                         canvas_view->queue_rebuild_ducks();
1658                                 return true;
1659                         }
1660                         }catch(String)
1661                         {
1662                                 canvas_view->duck_refresh_flag=true;
1663                                 canvas_view->queue_rebuild_ducks();
1664                                 return true;
1665                         }
1666                         //queue_draw();
1667                         clicked_duck=0;
1668
1669                         ret=true;
1670                 }
1671
1672                 if(dragging==DRAG_BOX)
1673                 {
1674                         dragging=DRAG_NONE;
1675                         if((drag_point-mouse_pos).mag()>radius/2.0f)
1676                         {
1677                                 if(canvas_view->get_smach().process_event(EventBox(drag_point,mouse_pos,MouseButton(event->button.button),modifier))==Smach::RESULT_ACCEPT)
1678                                         return true;
1679
1680                                 // when dragging a box around some ducks:
1681                                 // SHIFT selects; CTRL toggles; SHIFT+CTRL unselects; <none> clears all then selects
1682                                 if(modifier&GDK_SHIFT_MASK)
1683                                         select_ducks_in_box(drag_point,mouse_pos);
1684
1685                                 if(modifier&GDK_CONTROL_MASK)
1686                                         toggle_select_ducks_in_box(drag_point,mouse_pos);
1687                                 else if(!(modifier&GDK_SHIFT_MASK))
1688                                 {
1689                                         clear_selected_ducks();
1690                                         select_ducks_in_box(drag_point,mouse_pos);
1691                                 }
1692                                 ret=true;
1693                         }
1694                         else
1695                         {
1696                                 if(allow_layer_clicks)
1697                                 {
1698                                         Layer::Handle layer(get_canvas()->find_layer(drag_point));
1699                                         //if(layer)
1700                                         {
1701                                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_LEFT,mouse_pos,modifier))==Smach::RESULT_OK)
1702                                                         signal_layer_selected_(layer);
1703                                                 ret=true;
1704                                         }
1705                                 }
1706                                 else
1707                                 {
1708                                         signal_user_click(0)(mouse_pos);
1709                                 }
1710                         }
1711                 }
1712
1713                 dragging=DRAG_NONE;
1714
1715                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_UP,MouseButton(event->button.button),mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1716                         ret=true;
1717
1718                 return ret;
1719         }
1720                 break;
1721         case GDK_SCROLL:
1722         {
1723                 // Handle a mouse scrolling event like Xara Xtreme and
1724                 // Inkscape:
1725
1726                 // Scroll up/down: scroll up/down
1727                 // Shift + scroll up/down: scroll left/right
1728                 // Control + scroll up/down: zoom in/out
1729
1730                 if(modifier&GDK_CONTROL_MASK)
1731                 {
1732
1733                         // The zoom is performed while preserving the pointer
1734                         // position as a fixed point (similarly to Xara Xtreme and
1735                         // Inkscape).
1736
1737                         // The strategy used below is to scroll to the updated
1738                         // position, then zoom. This is easy to implement within
1739                         // the present architecture, but has the disadvantage of
1740                         // triggering multiple visible refreshes. Note: 1.25 is
1741                         // the hard wired ratio in zoom_in()/zoom_out(). The
1742                         // variable "drift" compensates additional inaccuracies in
1743                         // the zoom. There is also an additional minus sign for
1744                         // the inverted y coordinates.
1745
1746                         // FIXME: One might want to figure out where in the code
1747                         // this empirical drift is been introduced.
1748
1749                         const synfig::Point scroll_point(get_scrollx_adjustment()->get_value(),get_scrolly_adjustment()->get_value());
1750                         const double drift = 0.052;
1751
1752                         switch(event->scroll.direction)
1753                         {
1754                                 case GDK_SCROLL_UP:
1755                                 case GDK_SCROLL_RIGHT:
1756                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1.25-(1+drift)));
1757                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1.25-(1+drift)));
1758                                         zoom_in();
1759                                         break;
1760                                 case GDK_SCROLL_DOWN:
1761                                 case GDK_SCROLL_LEFT:
1762                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1/1.25-(1+drift)));
1763                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1/1.25-(1+drift)));
1764                                         zoom_out();
1765                                         break;
1766                                 default:
1767                                         break;
1768                         }
1769                 }
1770                 else if(modifier&GDK_SHIFT_MASK)
1771                 {
1772                         // Scroll in either direction by 20 pixels. Ideally, the
1773                         // amount of pixels per scrolling event should be
1774                         // configurable. Xara Xtreme currently uses an (hard
1775                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1776
1777                         const int scroll_pixel = 20;
1778
1779                         switch(event->scroll.direction)
1780                         {
1781                                 case GDK_SCROLL_UP:
1782                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1783                                         break;
1784                                 case GDK_SCROLL_DOWN:
1785                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1786                                         break;
1787                                 case GDK_SCROLL_LEFT:
1788                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1789                                         break;
1790                                 case GDK_SCROLL_RIGHT:
1791                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1792                                         break;
1793                                 default:
1794                                         break;
1795                         }
1796                 }
1797                 else
1798                 {
1799                         // Scroll in either direction by 20 pixels. Ideally, the
1800                         // amount of pixels per scrolling event should be
1801                         // configurable. Xara Xtreme currently uses an (hard
1802                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1803
1804                         const int scroll_pixel = 20;
1805
1806                         switch(event->scroll.direction)
1807                         {
1808                                 case GDK_SCROLL_UP:
1809                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1810                                         break;
1811                                 case GDK_SCROLL_DOWN:
1812                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1813                                         break;
1814                                 case GDK_SCROLL_LEFT:
1815                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1816                                         break;
1817                                 case GDK_SCROLL_RIGHT:
1818                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1819                                         break;
1820                                 default:
1821                                         break;
1822                         }
1823                 }
1824         }
1825                 break;
1826         default:
1827                 break;
1828         }
1829                 return false;
1830 }
1831
1832 bool
1833 WorkArea::on_hruler_event(GdkEvent */*event*/)
1834 {
1835 /*
1836         switch(event->type)
1837     {
1838         case GDK_BUTTON_PRESS:
1839                 if(dragging==DRAG_NONE)
1840                 {
1841                         dragging=DRAG_GUIDE;
1842                         curr_guide=get_guide_list_y().insert(get_guide_list_y().begin());
1843                         curr_guide_is_x=false;
1844                 }
1845                 return true;
1846                 break;
1847
1848         case GDK_MOTION_NOTIFY:
1849                 // Guide movement
1850                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1851                 {
1852                         double y,x;
1853                         if(event->button.axes)
1854                         {
1855                                 x=(event->button.axes[0]);
1856                                 y=(event->button.axes[1]);
1857                         }
1858                         else
1859                         {
1860                                 x=event->button.x;
1861                                 y=event->button.y;
1862                         }
1863
1864                         if(isnan(y) || isnan(x))
1865                                 return false;
1866
1867                         *curr_guide=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)))[1];
1868
1869                         queue_draw();
1870                 }
1871                 return true;
1872                 break;
1873
1874         case GDK_BUTTON_RELEASE:
1875                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1876                 {
1877                         dragging=DRAG_NONE;
1878                         get_guide_list_y().erase(curr_guide);
1879                 }
1880                 break;
1881                 return true;
1882         default:
1883                 break;
1884         }
1885 */
1886         return false;
1887 }
1888
1889 bool
1890 WorkArea::on_vruler_event(GdkEvent */*event*/)
1891 {
1892 /*
1893         switch(event->type)
1894     {
1895         case GDK_BUTTON_PRESS:
1896                 if(dragging==DRAG_NONE)
1897                 {
1898                         dragging=DRAG_GUIDE;
1899                         curr_guide=get_guide_list_x().insert(get_guide_list_x().begin());
1900                         curr_guide_is_x=true;
1901                 }
1902                 return true;
1903                 break;
1904         case GDK_BUTTON_RELEASE:
1905                 if(dragging==DRAG_GUIDE && curr_guide_is_x==true)
1906                 {
1907                         dragging=DRAG_NONE;
1908                         get_guide_list_x().erase(curr_guide);
1909                 }
1910                 break;
1911                 return true;
1912         default:
1913                 break;
1914         }
1915 */
1916         return false;
1917 }
1918
1919
1920 void
1921 WorkArea::refresh_dimension_info()
1922 {
1923         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1924
1925         canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1926         canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1927
1928         pw=canvaswidth/w;
1929         ph=canvasheight/h;
1930
1931         scrollx_adjustment.set_page_increment(abs(get_grid_size()[0]));
1932         scrollx_adjustment.set_step_increment(abs(pw));
1933         scrollx_adjustment.set_lower(-abs(canvaswidth));
1934         scrollx_adjustment.set_upper(abs(canvaswidth));
1935         scrolly_adjustment.set_lower(-abs(canvasheight));
1936         scrolly_adjustment.set_upper(abs(canvasheight));
1937         scrolly_adjustment.set_step_increment(abs(ph));
1938         scrolly_adjustment.set_page_increment(abs(get_grid_size()[1]));
1939
1940
1941
1942         if(drawing_area->get_width()<=0 || drawing_area->get_height()<=0 || w==0 || h==0)
1943                 return;
1944
1945         const synfig::Point focus_point(get_focus_point());
1946         const synfig::Real x(focus_point[0]/pw+drawing_area->get_width()/2-w/2);
1947         const synfig::Real y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1948
1949         window_tl[0]=rend_desc.get_tl()[0]-pw*x;
1950         window_br[0]=rend_desc.get_br()[0]+pw*(drawing_area->get_width()-x-w);
1951
1952         window_tl[1]=rend_desc.get_tl()[1]-ph*y;
1953         window_br[1]=rend_desc.get_br()[1]+ph*(drawing_area->get_height()-y-h);
1954
1955         hruler->property_lower()=Distance(window_tl[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1956         hruler->property_upper()=Distance(window_br[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1957         vruler->property_lower()=Distance(window_tl[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1958         vruler->property_upper()=Distance(window_br[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1959
1960         view_window_changed();
1961 }
1962
1963
1964 synfig::Point
1965 WorkArea::screen_to_comp_coords(synfig::Point pos)const
1966 {
1967         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1968         //synfig::Vector::value_type canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1969         //synfig::Vector::value_type canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1970         //synfig::Vector::value_type pw=canvaswidth/w;
1971         //synfig::Vector::value_type ph=canvasheight/h;
1972         Vector focus_point=get_focus_point();
1973         synfig::Vector::value_type x=focus_point[0]/pw+drawing_area->get_width()/2-w/2;
1974         synfig::Vector::value_type y=focus_point[1]/ph+drawing_area->get_height()/2-h/2;
1975
1976         return rend_desc.get_tl()-synfig::Point(pw*x,ph*y)+synfig::Point(pw*pos[0],ph*pos[1]);
1977 }
1978
1979 synfig::Point
1980 WorkArea::comp_to_screen_coords(synfig::Point /*pos*/)const
1981 {
1982         synfig::warning("WorkArea::comp_to_screen_coords: Not yet implemented");
1983         return synfig::Point();
1984 }
1985
1986 int
1987 WorkArea::next_unrendered_tile(int refreshes)const
1988 {
1989         //assert(!tile_book.empty());
1990         if(tile_book.empty())
1991                 return -1;
1992
1993         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1994
1995         const synfig::Vector focus_point(get_focus_point());
1996
1997         // Calculate the window coordinates of the top-left
1998         // corner of the canvas.
1999         const synfig::Vector::value_type
2000                 x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2001                 y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2002
2003         int div = low_res_pixel_size;
2004         const int width_in_tiles(w/tile_w+((low_resolution?((w/div)%(tile_w/div)):(w%tile_w))?1:0));
2005         const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
2006
2007         int
2008                 u(0),v(0),
2009                 u1(int(-x/tile_w)),
2010                 v1(int(-y/tile_h)),
2011                 u2(int((-x+drawing_area->get_width())/tile_w+1)),
2012                 v2(int((-y+drawing_area->get_height())/tile_h+1));
2013
2014         if(u2>width_in_tiles)u2=width_in_tiles;
2015         if(v2>height_in_tiles)v2=height_in_tiles;
2016         if(u1<0)u1=0;
2017         if(v1<0)v1=0;
2018
2019         int last_good_tile(-1);
2020
2021         for(v=v1;v<v2;v++)
2022                 for(u=u1;u<u2;u++)
2023                 {
2024                         int index(v*width_in_tiles+u);
2025                         if(tile_book[index].second<refreshes)
2026                         {
2027                                 last_good_tile=index;
2028                                 if(rand()%8==0)
2029                                         return index;
2030                         }
2031                 }
2032         return last_good_tile;
2033 }
2034
2035 /*
2036 template <typename F, typename T=WorkAreaRenderer, typename R=typename F::result_type>
2037 class handle2ptr_t : public std::unary_function<typename etl::handle<T>,R>
2038 {
2039 private:
2040         F func;
2041 public:
2042         handle2ptr_t(const F &func):func(func) { };
2043
2044         R operator()(typename etl::handle<T> x) { return func(*x); }
2045 };
2046
2047 template <typename F>
2048 handle2ptr_t<F>
2049 handle2ptr(F func)
2050 {
2051         return handle2ptr_t<F>(func);
2052 }
2053         for_each(
2054                 renderer_set_.begin(),
2055                 renderer_set_.end(),
2056                 handle2ptr(
2057                         sigc::bind(
2058                                 sigc::bind(
2059                                         sigc::mem_fun(
2060                                                 &WorkAreaRenderer::render_vfunc
2061                                         ),
2062                                         Gdk::Rectangle(event->area)
2063                                 ),
2064                                 drawing_area->get_window()
2065                         )
2066                 )
2067         );
2068 */
2069
2070 bool
2071 WorkArea::refresh(GdkEventExpose*event)
2072 {
2073         assert(get_canvas());
2074
2075         drawing_area->get_window()->clear();
2076
2077         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2078
2079         const synfig::Vector focus_point(get_focus_point());
2080
2081         // Update the old focus point
2082         last_focus_point=focus_point;
2083
2084         // Draw out the renderables
2085         {
2086                 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
2087                 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
2088                 {
2089                         if((*iter)->get_enabled())
2090                                 (*iter)->render_vfunc(
2091                                         drawing_area->get_window(),
2092                                         Gdk::Rectangle(&event->area)
2093                                 );
2094                 }
2095         }
2096
2097         // Calculate the window coordinates of the top-left
2098         // corner of the canvas.
2099         //const synfig::Vector::value_type
2100         //      x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2101         //      y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2102
2103         //const synfig::Vector::value_type window_startx(window_tl[0]);
2104         //const synfig::Vector::value_type window_endx(window_br[0]);
2105         //const synfig::Vector::value_type window_starty(window_tl[1]);
2106         //const synfig::Vector::value_type window_endy(window_br[1]);
2107
2108         Glib::RefPtr<Gdk::GC> gc=Gdk::GC::create(drawing_area->get_window());
2109
2110         // If we are in animate mode, draw a red border around the screen
2111         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2112         {
2113 // #define USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2114 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2115                 // This method of drawing the red border doesn't work on any
2116                 // Gtk theme which uses the crux-engine, hcengine, industrial,
2117                 // mist, or ubuntulooks engine, such as the default ubuntu
2118                 // 'Human' theme.
2119                 drawing_frame->modify_bg(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2120 #else
2121                 // So let's do it in a more primitive fashion.
2122                 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
2123                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
2124                 drawing_area->get_window()->draw_rectangle(
2125                         gc,
2126                         false,  // Fill?
2127                         0,0,    // x,y
2128                         drawing_area->get_width()-1,drawing_area->get_height()-1); // w,h
2129 #endif
2130         }
2131 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2132         else
2133                 drawing_frame->unset_bg(Gtk::STATE_NORMAL);
2134 #endif
2135
2136         return true;
2137 }
2138
2139 void
2140 WorkArea::done_rendering()
2141 {
2142 /*
2143         assert(buffer);
2144         assert(w>0);
2145         assert(h>0);
2146         pix_buf=Gdk::Pixbuf::create_from_data(
2147                 buffer, // pointer to the data
2148                 Gdk::COLORSPACE_RGB, // the colorspace
2149                 true, // has alpha?
2150                 8, // bits per sample
2151                 w,      // width
2152                 h,      // height
2153                 w*bpp); // stride (pitch)
2154         assert(pix_buf);
2155 */
2156 }
2157
2158
2159 void
2160 WorkArea::set_quality(int x)
2161 {
2162         if(x==quality)
2163                 return;
2164         quality=x;
2165         queue_render_preview();
2166 }
2167
2168 void
2169 WorkArea::set_low_res_pixel_size(int x)
2170 {
2171         if(x==low_res_pixel_size)
2172                 return;
2173         low_res_pixel_size=x;
2174         queue_render_preview();
2175 }
2176
2177 namespace studio
2178 {
2179 class WorkAreaProgress : public synfig::ProgressCallback
2180 {
2181         WorkArea *work_area;
2182         ProgressCallback *cb;
2183
2184 public:
2185
2186         WorkAreaProgress(WorkArea *work_area,ProgressCallback *cb):
2187                 work_area(work_area),cb(cb)
2188         {
2189                 assert(cb);
2190         }
2191
2192         virtual bool
2193         task(const std::string &str)
2194         {
2195                 if(work_area->dirty)
2196                         return false;
2197                 return cb->task(str);
2198         }
2199
2200         virtual bool
2201         error(const std::string &err)
2202         {
2203                 if(work_area->dirty)
2204                         return false;
2205                 return cb->error(err);
2206         }
2207
2208         virtual bool
2209         amount_complete(int current, int total)
2210         {
2211                 if(work_area->dirty)
2212                         return false;
2213                 return cb->amount_complete(current,total);
2214         }
2215 };
2216 }
2217
2218 bool
2219 studio::WorkArea::async_update_preview()
2220 {
2221 #ifdef SINGLE_THREADED
2222         if (get_updating())
2223         {
2224                 stop_updating();
2225                 queue_render_preview();
2226                 return false;
2227         }
2228 #endif
2229
2230         async_renderer=0;
2231
2232         queued=false;
2233         canceled_=false;
2234         get_canvas_view()->reset_cancel_status();
2235
2236         // This object will mark us as busy until
2237         // we are done.
2238         //studio::App::Busy busy;
2239
2240         //WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2241         //synfig::ProgressCallback *cb=&callback;
2242
2243         if(!is_visible())return false;
2244
2245         /*
2246         // If we are queued to render the scene at the next idle
2247         // go ahead and de-queue it.
2248         if(render_idle_func_id)
2249         {
2250                 g_source_remove(render_idle_func_id);
2251                 //queued=false;
2252                 render_idle_func_id=0;
2253         }
2254         */
2255
2256         dirty=false;
2257         get_canvas_view()->reset_cancel_status();
2258
2259         //bool ret=false;
2260         RendDesc desc=get_canvas()->rend_desc();
2261
2262         int w=(int)(desc.get_w()*zoom);
2263         int h=(int)(desc.get_h()*zoom);
2264
2265         // ensure that the size we draw is at least one pixel in each dimension
2266         int min_size = low_resolution ? low_res_pixel_size : 1;
2267         if (w < min_size) w = min_size;
2268         if (h < min_size) h = min_size;
2269
2270         // Setup the description parameters
2271         desc.set_antialias(1);
2272         desc.set_time(cur_time);
2273
2274         set_rend_desc(desc);
2275
2276         // Create the render target
2277         handle<Target> target;
2278
2279         // if we have lots of pixels to render and the tile renderer isn't disabled, use it
2280         int div;
2281         div = low_resolution ? low_res_pixel_size : 1;
2282         if (w*h > 240*div*135*div && !getenv("SYNFIG_DISABLE_TILE_RENDER"))
2283         {
2284                 // do a tile render
2285                 handle<WorkAreaTarget> trgt(new class WorkAreaTarget(this,w,h));
2286
2287                 trgt->set_rend_desc(&desc);
2288                 trgt->set_onion_skin(get_onion_skin());
2289                 target=trgt;
2290         }
2291         else
2292         {
2293                 // do a scanline render
2294                 handle<WorkAreaTarget_Full> trgt(new class WorkAreaTarget_Full(this,w,h));
2295
2296                 trgt->set_rend_desc(&desc);
2297                 trgt->set_onion_skin(get_onion_skin());
2298                 target=trgt;
2299         }
2300
2301         // We can rest assured that our time has already
2302         // been set, so there is no need to have to
2303         // recalculate that over again.
2304         // UPDATE: This is kind of needless with
2305         // the way that time is handled now in SYNFIG.
2306         //target->set_avoid_time_sync(true);
2307         async_renderer=new AsyncRenderer(target);
2308         async_renderer->signal_finished().connect(
2309                 sigc::mem_fun(this,&WorkArea::async_update_finished)
2310         );
2311
2312         rendering=true;
2313         async_renderer->start();
2314
2315         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2316
2317         rendering=true;
2318         cb->task(_("Rendering..."));
2319         rendering=true;
2320
2321         return true;
2322 }
2323
2324 void
2325 studio::WorkArea::async_update_finished()
2326 {
2327         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2328
2329         rendering=false;
2330
2331         if(!async_renderer)
2332                 return;
2333
2334         // If we completed successfully, then
2335         // we aren't dirty anymore
2336         if(async_renderer->has_success())
2337         {
2338                 dirty=false;
2339                 //queued=false;
2340                 cb->task(_("Idle"));
2341         }
2342         else
2343         {
2344                 dirty=true;
2345                 cb->task(_("Render Failed"));
2346         }
2347         //get_canvas_view()->reset_cancel_status();
2348         done_rendering();
2349 }
2350
2351 bool
2352 studio::WorkArea::sync_update_preview()
2353 {
2354         //      const Time &time(cur_time);
2355
2356         canceled_=false;
2357         get_canvas_view()->reset_cancel_status();
2358
2359         async_renderer=0;
2360
2361 again:
2362         // This object will mark us as busy until
2363         // we are done.
2364         studio::App::Busy busy;
2365
2366         WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2367         synfig::ProgressCallback *cb=&callback;
2368
2369         // We don't want to render if we are already rendering
2370         if(rendering)
2371         {
2372                 dirty=true;
2373                 return false;
2374         }
2375
2376         if(!is_visible())return false;
2377         get_canvas()->set_time(get_time());
2378         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2379         signal_rendering()();
2380
2381         // If we are queued to render the scene at the next idle
2382         // go ahead and de-queue it.
2383         if(render_idle_func_id)
2384         {
2385                 g_source_remove(render_idle_func_id);
2386                 //queued=false;
2387                 render_idle_func_id=0;
2388         }
2389         // Start rendering
2390         rendering=true;
2391
2392         dirty=false;
2393         get_canvas_view()->reset_cancel_status();
2394
2395         RendDesc desc=get_canvas()->rend_desc();
2396         //newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2397
2398         int w=(int)(desc.get_w()*zoom);
2399         int h=(int)(desc.get_h()*zoom);
2400
2401         // Setup the description parameters
2402         desc.set_antialias(1);
2403         desc.set_time(cur_time);
2404         //desc.set_wh(w,h);
2405
2406         set_rend_desc(desc);
2407
2408         // Create the render target
2409         handle<WorkAreaTarget> target(new class WorkAreaTarget(this,w,h));
2410
2411         target->set_rend_desc(&desc);
2412
2413         // We can rest assured that our time has already
2414         // been set, so there is no need to have to
2415         // recalculate that over again.
2416         target->set_avoid_time_sync(true);
2417
2418         if(cb)
2419                 cb->task(strprintf(_("Rendering canvas %s..."),get_canvas()->get_name().c_str()));
2420
2421         bool ret = target->render(cb);
2422
2423         if(!ret && !get_canvas_view()->get_cancel_status() && dirty)
2424         {
2425                 rendering=false;
2426                 //canceled_=true;
2427                 goto again;
2428         }
2429         if(get_canvas_view()->get_cancel_status())
2430                 canceled_=true;
2431
2432         if(cb)
2433         {
2434                 if(ret)
2435                         cb->task(_("Idle"));
2436                 else
2437                         cb->task(_("Render Failed"));
2438                 cb->amount_complete(0,1);
2439         }
2440
2441         // Refresh the work area to make sure that
2442         // it is being displayed correctly
2443         drawing_area->queue_draw();
2444
2445         // If we completed successfully, then
2446         // we aren't dirty anymore
2447         if(ret)
2448         {
2449                 dirty=false;
2450                 //queued=false;
2451         }
2452         else dirty=true;
2453         rendering=false;
2454         //get_canvas_view()->reset_cancel_status();
2455         done_rendering();
2456         return ret;
2457 }
2458
2459 void
2460 studio::WorkArea::async_render_preview(synfig::Time time)
2461 {
2462         cur_time=time;
2463         //tile_book.clear();
2464
2465         refreshes+=5;
2466         if(!is_visible())return;
2467
2468         get_canvas()->set_time(get_time());
2469         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2470         signal_rendering()();
2471
2472         async_update_preview();
2473 }
2474 void
2475 WorkArea::async_render_preview()
2476 {
2477         return async_render_preview(get_canvas_view()->get_time());
2478 }
2479
2480 bool
2481 studio::WorkArea::sync_render_preview(synfig::Time time)
2482 {
2483         cur_time=time;
2484         //tile_book.clear();
2485         refreshes+=5;
2486         if(!is_visible())return false;
2487         return sync_update_preview();
2488 }
2489
2490 bool
2491 WorkArea::sync_render_preview()
2492 {
2493         return sync_render_preview(get_canvas_view()->get_time());
2494 }
2495
2496 void
2497 WorkArea::sync_render_preview_hook()
2498 {
2499         sync_render_preview(get_canvas_view()->get_time());
2500 }
2501
2502 void
2503 WorkArea::queue_scroll()
2504 {
2505 //      const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2506
2507         const synfig::Point focus_point(get_focus_point());
2508
2509         const synfig::Real
2510                 new_x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2511                 new_y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2512
2513         const synfig::Real
2514                 old_x(last_focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2515                 old_y(last_focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2516
2517         // If the coordinates didn't change, we shouldn't queue a draw
2518         if(old_x==new_x && old_y==new_y)
2519                 return;
2520
2521         const int
2522                 dx(round_to_int(old_x)-round_to_int(new_x)),
2523                 dy(round_to_int(old_y)-round_to_int(new_y));
2524
2525         drawing_area->get_window()->scroll(-dx,-dy);
2526
2527         if (timecode_width && timecode_height)
2528         {
2529                 drawing_area->queue_draw_area(4,       4,    4+timecode_width,    4+timecode_height);
2530                 drawing_area->queue_draw_area(4-dx, 4-dy, 4-dx+timecode_width, 4-dy+timecode_height);
2531         }
2532
2533 #ifndef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2534         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2535         {
2536                 int maxx = drawing_area->get_width()-1;
2537                 int maxy = drawing_area->get_height()-1;
2538
2539                 if (dx > 0)
2540                 {
2541                         drawing_area->queue_draw_area(      0, 0,       1, maxy);
2542                         drawing_area->queue_draw_area(maxx-dx, 0, maxx-dx, maxy);
2543                 }
2544                 else if (dx < 0) 
2545                 {
2546                         drawing_area->queue_draw_area(   maxx, 0,    maxx, maxy);
2547                         drawing_area->queue_draw_area(    -dx, 0,     -dx, maxy);
2548                 }
2549                 if (dy > 0)
2550                 {
2551                         drawing_area->queue_draw_area(0,       0, maxx,       1);
2552                         drawing_area->queue_draw_area(0, maxy-dy, maxx, maxy-dy);
2553                 }
2554                 else if (dy < 0) 
2555                 {
2556                         drawing_area->queue_draw_area(0,    maxy, maxx,    maxy);
2557                         drawing_area->queue_draw_area(0,     -dy, maxx,     -dy);
2558                 }
2559         }
2560 #endif // USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2561
2562         last_focus_point=focus_point;
2563 }
2564
2565 void
2566 studio::WorkArea::zoom_in()
2567 {
2568         set_zoom(zoom*1.25);
2569 }
2570
2571 void
2572 studio::WorkArea::zoom_out()
2573 {
2574         set_zoom(zoom/1.25);
2575 }
2576
2577 void
2578 studio::WorkArea::zoom_fit()
2579 {
2580         float new_zoom(min(drawing_area->get_width() * zoom / w, drawing_area->get_height() * zoom / h));
2581         if (zoom / new_zoom > 0.995 && new_zoom / zoom > 0.995)
2582         {
2583                 set_zoom(prev_zoom);
2584                 return set_focus_point(previous_focus);
2585         }
2586         previous_focus = get_focus_point();
2587         prev_zoom = zoom;
2588         set_zoom(new_zoom);
2589         set_focus_point(Point(0,0));
2590 }
2591
2592 void
2593 studio::WorkArea::zoom_norm()
2594 {
2595         if (zoom == 1.0) return set_zoom(prev_zoom);
2596         prev_zoom = zoom;
2597         set_zoom(1.0f);
2598 }
2599
2600 gboolean
2601 studio::WorkArea::__render_preview(gpointer data)
2602 {
2603         WorkArea *work_area(static_cast<WorkArea*>(data));
2604
2605         // there's no point anyone trying to cancel the timer now - it's gone off already
2606         work_area->render_idle_func_id = 0;
2607
2608         work_area->queued=false;
2609         work_area->async_render_preview(work_area->get_canvas_view()->get_time());
2610
2611         return 0;
2612 }
2613
2614 void
2615 studio::WorkArea::queue_render_preview()
2616 {
2617         //synfig::info("queue_render_preview(): called for %s", get_canvas_view()->get_time().get_string().c_str());
2618
2619         if(queued==true)
2620         {
2621                 return;
2622                 //synfig::info("queue_render_preview(): already queued, unqueuing");
2623 /*              if(render_idle_func_id)
2624                         g_source_remove(render_idle_func_id);
2625                 render_idle_func_id=0;
2626                 queued=false;
2627 */
2628                 //async_renderer=0;
2629         }
2630
2631         if(dirty_trap_enabled)
2632         {
2633                 dirty_trap_queued++;
2634                 return;
2635         }
2636
2637         int queue_time=50;
2638
2639         if(rendering)
2640                 queue_time+=250;
2641
2642
2643         if(queued==false)
2644         {
2645                 //synfig::info("queue_render_preview(): (re)queuing...");
2646                 //render_idle_func_id=g_idle_add_full(G_PRIORITY_DEFAULT,__render_preview,this,NULL);
2647                 render_idle_func_id=g_timeout_add_full(
2648                         G_PRIORITY_DEFAULT,     // priority - 
2649                         queue_time,                     // interval - the time between calls to the function, in milliseconds (1/1000ths of a second)
2650                         __render_preview,       // function - function to call
2651                         this,                           // data     - data to pass to function
2652                         NULL);                          // notify   - function to call when the idle is removed, or NULL
2653                 queued=true;
2654         }
2655 /*      else if(rendering)
2656         {
2657                 refreshes+=5;
2658                 dirty=true;
2659                 queue_draw();
2660         }
2661 */
2662 }
2663
2664 DirtyTrap::DirtyTrap(WorkArea *work_area):work_area(work_area)
2665 {
2666         work_area->dirty_trap_enabled=true;
2667
2668         work_area->dirty_trap_queued=0;
2669 }
2670
2671 DirtyTrap::~DirtyTrap()
2672 {
2673         work_area->dirty_trap_enabled=false;
2674         if(work_area->dirty_trap_queued)
2675                 work_area->queue_render_preview();
2676 }
2677
2678 void
2679 studio::WorkArea::queue_draw_preview()
2680 {
2681         drawing_area->queue_draw();
2682 }
2683
2684 void
2685 studio::WorkArea::set_cursor(const Gdk::Cursor& x)
2686 {
2687         drawing_area->get_window()->set_cursor(x);
2688 }
2689 void
2690 studio::WorkArea::set_cursor(Gdk::CursorType x)
2691 {
2692         drawing_area->get_window()->set_cursor(Gdk::Cursor(x));
2693 }
2694
2695 #include "iconcontroller.h"
2696
2697 void
2698 studio::WorkArea::refresh_cursor()
2699 {
2700 //      set_cursor(IconController::get_tool_cursor(canvas_view->get_smach().get_state_name(),drawing_area->get_window()));
2701 }
2702
2703 void
2704 studio::WorkArea::reset_cursor()
2705 {
2706         drawing_area->get_window()->set_cursor(Gdk::Cursor(Gdk::TOP_LEFT_ARROW));
2707 //      set_cursor(Gdk::TOP_LEFT_ARROW);
2708 }
2709
2710 void
2711 studio::WorkArea::set_zoom(float z)
2712 {
2713         z=max(1.0f/128.0f,min(128.0f,z));
2714         if(z==zoom)
2715                 return;
2716         zoom = z;
2717         refresh_dimension_info();
2718         /*if(async_renderer)
2719         {
2720                 async_renderer->stop();
2721                 async_renderer=0;
2722         }*/
2723         refreshes+=5;
2724         async_update_preview();
2725         //queue_render_preview();
2726 }
2727
2728 void
2729 WorkArea::set_selected_value_node(etl::loose_handle<synfig::ValueNode> x)
2730 {
2731         if(x!=selected_value_node_)
2732         {
2733                 selected_value_node_=x;
2734                 queue_draw();
2735         }
2736 }
2737
2738 void
2739 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x)
2740 {
2741         renderer_set_.insert(x);
2742         x->set_work_area(this);
2743         queue_draw();
2744 }
2745
2746 void
2747 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x, int priority)
2748 {
2749         x->set_priority(priority);
2750         insert_renderer(x);
2751 }
2752
2753 void
2754 WorkArea::erase_renderer(const etl::handle<WorkAreaRenderer> &x)
2755 {
2756         x->set_work_area(0);
2757         renderer_set_.erase(x);
2758         queue_draw();
2759 }
2760
2761 void
2762 WorkArea::resort_render_set()
2763 {
2764         std::set<etl::handle<WorkAreaRenderer> > tmp(
2765                 renderer_set_.begin(),
2766                 renderer_set_.end()
2767         );
2768         renderer_set_.swap(tmp);
2769         queue_draw();
2770 }