Allow PasteCanvas layers with unset canvas parameters to be exported.
[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                                                         if (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                                                         {
1379                                                                 // rebuild the ducks from scratch, so the tangents ducks aren't connected
1380                                                                 get_canvas_view()->rebuild_ducks();
1381
1382                                                                 // reprocess the mouse click
1383                                                                 return on_drawing_area_event(event);
1384                                                         }
1385                                                         else
1386                                                                 return true;
1387                                                 }
1388                                         } else {
1389                                                 // I don't know how to access the vertex from the tangent duck when originally drawing the bline in the bline tool
1390
1391                                                 // synfig::ValueNode::Handle vn = value_desc.get_value_node();
1392                                                 synfig::info("parent isn't value node?  shift-drag-tangent doesn't work in bline tool yet...");
1393                                         }
1394                                 }
1395
1396                                 dragging=DRAG_DUCK;
1397                                 drag_point=mouse_pos;
1398                                 //drawing_area->queue_draw();
1399                                 start_duck_drag(mouse_pos);
1400                                 get_canvas_view()->reset_cancel_status();
1401                                 return true;
1402                         }
1403 // I commented out this section because
1404 // it was causing issues when rotoscoping.
1405 // At the moment, we don't need it, so
1406 // this was the easiest way to fix the problem.
1407 /*
1408                         else
1409                         if(selected_bezier)
1410                         {
1411                                 selected_duck=0;
1412                                 selected_bezier->signal_user_click(0)(bezier_click_pos);
1413                         }
1414 */
1415                         else
1416                         {
1417                                 //clear_selected_ducks();
1418                                 selected_bezier=0;
1419                                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1420                                 {
1421                                         // Check for a guide click
1422                                         GuideList::iterator iter;
1423
1424                                         iter=find_guide_x(mouse_pos,radius);
1425                                         if(iter==get_guide_list_x().end())
1426                                         {
1427                                                 curr_guide_is_x=false;
1428                                                 iter=find_guide_y(mouse_pos,radius);
1429                                         }
1430                                         else
1431                                                 curr_guide_is_x=true;
1432                                         if(iter!=get_guide_list_x().end() && iter!=get_guide_list_y().end())
1433                                         {
1434                                                 dragging=DRAG_GUIDE;
1435                                                 curr_guide=iter;
1436                                                 return true;
1437                                         }
1438
1439
1440                                         // All else fails, try making a selection box
1441                                         dragging=DRAG_BOX;
1442                                         curr_point=drag_point=mouse_pos;
1443                                         return true;
1444                                 }
1445                         }
1446                         break;
1447                 }
1448                 case 2: // Attempt to drag and move the window
1449                 {
1450                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1451                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1452                         if(duck)
1453                                 duck->signal_user_click(1)();
1454                         else
1455                         if(bezier)
1456                                 bezier->signal_user_click(1)(bezier_click_pos);
1457
1458                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1459                         if(is_mouse)
1460                         {
1461                                 dragging=DRAG_WINDOW;
1462                                 drag_point=mouse_pos;
1463                                 signal_user_click(1)(mouse_pos);
1464                         }
1465                         break;
1466                 }
1467                 case 3: // Attempt to either get info on a duck, or open the menu
1468                 {
1469                         etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1470                         etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1471
1472                         Layer::Handle layer(get_canvas()->find_layer(mouse_pos));
1473                         if(duck)
1474                         {
1475                                 if(get_selected_ducks().size()<=1)
1476                                         duck->signal_user_click(2)();
1477                                 else
1478                                         canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1479                                 return true;
1480                         }
1481                         else if(bezier)
1482                         {
1483                                 bezier->signal_user_click(2)(bezier_click_pos);
1484                                 return true;
1485                         }
1486                         else if (layer)
1487                         {
1488                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_RIGHT,mouse_pos))==Smach::RESULT_OK)
1489                                         return false;
1490                                 return true;
1491                         }
1492                         else
1493                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1494                         /*
1495                         if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1496                         {
1497                                 //popup_menu();
1498                                 return true;
1499                         }
1500                         */
1501                         break;
1502                 }
1503                 case 4:
1504                         signal_user_click(3)(mouse_pos);
1505                         break;
1506                 case 5:
1507                         signal_user_click(4)(mouse_pos);
1508                         break;
1509                 default:
1510                         break;
1511                 }
1512                 }
1513                 break;
1514         case GDK_MOTION_NOTIFY:
1515                 curr_point=mouse_pos;
1516
1517                 if(event->motion.time-last_event_time<25)
1518                         return true;
1519                 else
1520                         last_event_time=event->motion.time;
1521
1522                 signal_cursor_moved_();
1523
1524                 // Guide/Duck highlights on hover
1525                 if(dragging==DRAG_NONE)
1526                 {
1527                         GuideList::iterator iter;
1528
1529                         iter=find_guide_x(mouse_pos,radius);
1530                         if(iter==get_guide_list_x().end())
1531                                 iter=find_guide_y(mouse_pos,radius);
1532
1533                         if(iter!=curr_guide)
1534                         {
1535                                 curr_guide=iter;
1536                                 drawing_area->queue_draw();
1537                         }
1538
1539                         etl::handle<Duck> duck;
1540                         duck=find_duck(mouse_pos,radius);
1541                         if(duck!=hover_duck)
1542                         {
1543                                 hover_duck=duck;
1544                                 drawing_area->queue_draw();
1545                         }
1546                 }
1547
1548
1549                 if(dragging==DRAG_DUCK)
1550                 {
1551                         if(canvas_view->get_cancel_status())
1552                         {
1553                                 dragging=DRAG_NONE;
1554                                 canvas_view->queue_rebuild_ducks();
1555                                 return true;
1556                         }
1557                         /*
1558                         Point point((mouse_pos-selected_duck->get_origin())/selected_duck->get_scalar());
1559                         if(get_grid_snap())
1560                         {
1561                                 point[0]=floor(point[0]/grid_size[0]+0.5)*grid_size[0];
1562                                 point[1]=floor(point[1]/grid_size[1]+0.5)*grid_size[1];
1563                         }
1564                         selected_duck->set_point(point);
1565                         */
1566
1567                         //Point p(mouse_pos);
1568
1569                         set_axis_lock(event->motion.state&GDK_SHIFT_MASK);
1570
1571                         translate_selected_ducks(mouse_pos);
1572
1573                         drawing_area->queue_draw();
1574                 }
1575
1576                 if(dragging==DRAG_BOX)
1577                 {
1578                         curr_point=mouse_pos;
1579                         drawing_area->queue_draw();
1580                 }
1581
1582                 if(dragging==DRAG_GUIDE)
1583                 {
1584                         if(curr_guide_is_x)
1585                                 *curr_guide=mouse_pos[0];
1586                         else
1587                                 *curr_guide=mouse_pos[1];
1588                         drawing_area->queue_draw();
1589                 }
1590
1591                 if(dragging!=DRAG_WINDOW)
1592                 {       // Update those triangle things on the rulers
1593                         const synfig::Point point(mouse_pos);
1594                         hruler->property_position()=Distance(point[0],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1595                         vruler->property_position()=Distance(point[1],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1596                 }
1597
1598                 if(dragging == DRAG_WINDOW)
1599                         set_focus_point(get_focus_point() + mouse_pos-drag_point);
1600                 else if (event->motion.state & GDK_BUTTON1_MASK &&
1601                                 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_LEFT,
1602                                                                                                                                   mouse_pos,pressure,modifier)) == Smach::RESULT_ACCEPT)
1603                         return true;
1604                 else if (event->motion.state & GDK_BUTTON2_MASK &&
1605                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_MIDDLE,
1606                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1607                         return true;
1608                 else if (event->motion.state & GDK_BUTTON3_MASK &&
1609                                  canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_RIGHT,
1610                                                                                                                                    mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1611                         return true;
1612                 else if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_MOTION, BUTTON_NONE,
1613                                                                                                                                   mouse_pos, pressure,modifier)) == Smach::RESULT_ACCEPT)
1614                         return true;
1615
1616                 break;
1617
1618         case GDK_BUTTON_RELEASE:
1619         {
1620                 bool ret(false);
1621
1622                 if(dragging==DRAG_GUIDE)
1623                 {
1624                         dragging=DRAG_NONE;
1625                         save_meta_data();
1626                         return true;
1627                 }
1628                 else
1629                 if(dragging==DRAG_DUCK)
1630                 {
1631                         synfigapp::Action::PassiveGrouper grouper(instance.get(),_("Move"));
1632                         dragging=DRAG_NONE;
1633                         //translate_selected_ducks(mouse_pos);
1634                         set_axis_lock(false);
1635
1636                         try{
1637                         get_canvas_view()->duck_refresh_flag=false;
1638                         get_canvas_view()->duck_refresh_needed=false;
1639                         const bool drag_did_anything(end_duck_drag());
1640                         get_canvas_view()->duck_refresh_flag=true;
1641                         if(!drag_did_anything)
1642                         {
1643                                 // if we originally clicked on a selected duck ...
1644                                 if(clicked_duck)
1645                                 {
1646                                         // ... and CTRL is pressed, then just toggle the clicked duck
1647                                         //     otherwise make the clicked duck the only selected duck
1648                                         if(modifier&GDK_CONTROL_MASK)
1649                                                 unselect_duck(clicked_duck);
1650                                         else
1651                                         {
1652                                                 clear_selected_ducks();
1653                                                 select_duck(clicked_duck);
1654                                         }
1655                                         clicked_duck->signal_user_click(0)();
1656                                 }
1657                         }
1658                         else
1659                         {
1660                                 if(canvas_view->duck_refresh_needed)
1661                                         canvas_view->queue_rebuild_ducks();
1662                                 return true;
1663                         }
1664                         }catch(String)
1665                         {
1666                                 canvas_view->duck_refresh_flag=true;
1667                                 canvas_view->queue_rebuild_ducks();
1668                                 return true;
1669                         }
1670                         //queue_draw();
1671                         clicked_duck=0;
1672
1673                         ret=true;
1674                 }
1675
1676                 if(dragging==DRAG_BOX)
1677                 {
1678                         dragging=DRAG_NONE;
1679                         if((drag_point-mouse_pos).mag()>radius/2.0f)
1680                         {
1681                                 if(canvas_view->get_smach().process_event(EventBox(drag_point,mouse_pos,MouseButton(event->button.button),modifier))==Smach::RESULT_ACCEPT)
1682                                         return true;
1683
1684                                 // when dragging a box around some ducks:
1685                                 // SHIFT selects; CTRL toggles; SHIFT+CTRL unselects; <none> clears all then selects
1686                                 if(modifier&GDK_SHIFT_MASK)
1687                                         select_ducks_in_box(drag_point,mouse_pos);
1688
1689                                 if(modifier&GDK_CONTROL_MASK)
1690                                         toggle_select_ducks_in_box(drag_point,mouse_pos);
1691                                 else if(!(modifier&GDK_SHIFT_MASK))
1692                                 {
1693                                         clear_selected_ducks();
1694                                         select_ducks_in_box(drag_point,mouse_pos);
1695                                 }
1696                                 ret=true;
1697                         }
1698                         else
1699                         {
1700                                 if(allow_layer_clicks)
1701                                 {
1702                                         Layer::Handle layer(get_canvas()->find_layer(drag_point));
1703                                         //if(layer)
1704                                         {
1705                                                 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_LEFT,mouse_pos,modifier))==Smach::RESULT_OK)
1706                                                         signal_layer_selected_(layer);
1707                                                 ret=true;
1708                                         }
1709                                 }
1710                                 else
1711                                 {
1712                                         signal_user_click(0)(mouse_pos);
1713                                 }
1714                         }
1715                 }
1716
1717                 dragging=DRAG_NONE;
1718
1719                 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_UP,MouseButton(event->button.button),mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1720                         ret=true;
1721
1722                 return ret;
1723         }
1724                 break;
1725         case GDK_SCROLL:
1726         {
1727                 // Handle a mouse scrolling event like Xara Xtreme and
1728                 // Inkscape:
1729
1730                 // Scroll up/down: scroll up/down
1731                 // Shift + scroll up/down: scroll left/right
1732                 // Control + scroll up/down: zoom in/out
1733
1734                 if(modifier&GDK_CONTROL_MASK)
1735                 {
1736
1737                         // The zoom is performed while preserving the pointer
1738                         // position as a fixed point (similarly to Xara Xtreme and
1739                         // Inkscape).
1740
1741                         // The strategy used below is to scroll to the updated
1742                         // position, then zoom. This is easy to implement within
1743                         // the present architecture, but has the disadvantage of
1744                         // triggering multiple visible refreshes. Note: 1.25 is
1745                         // the hard wired ratio in zoom_in()/zoom_out(). The
1746                         // variable "drift" compensates additional inaccuracies in
1747                         // the zoom. There is also an additional minus sign for
1748                         // the inverted y coordinates.
1749
1750                         // FIXME: One might want to figure out where in the code
1751                         // this empirical drift is been introduced.
1752
1753                         const synfig::Point scroll_point(get_scrollx_adjustment()->get_value(),get_scrolly_adjustment()->get_value());
1754                         const double drift = 0.052;
1755
1756                         switch(event->scroll.direction)
1757                         {
1758                                 case GDK_SCROLL_UP:
1759                                 case GDK_SCROLL_RIGHT:
1760                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1.25-(1+drift)));
1761                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1.25-(1+drift)));
1762                                         zoom_in();
1763                                         break;
1764                                 case GDK_SCROLL_DOWN:
1765                                 case GDK_SCROLL_LEFT:
1766                                         get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1/1.25-(1+drift)));
1767                                         get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1/1.25-(1+drift)));
1768                                         zoom_out();
1769                                         break;
1770                                 default:
1771                                         break;
1772                         }
1773                 }
1774                 else if(modifier&GDK_SHIFT_MASK)
1775                 {
1776                         // Scroll in either direction by 20 pixels. Ideally, the
1777                         // amount of pixels per scrolling event should be
1778                         // configurable. Xara Xtreme currently uses an (hard
1779                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1780
1781                         const int scroll_pixel = 20;
1782
1783                         switch(event->scroll.direction)
1784                         {
1785                                 case GDK_SCROLL_UP:
1786                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1787                                         break;
1788                                 case GDK_SCROLL_DOWN:
1789                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1790                                         break;
1791                                 case GDK_SCROLL_LEFT:
1792                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1793                                         break;
1794                                 case GDK_SCROLL_RIGHT:
1795                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1796                                         break;
1797                                 default:
1798                                         break;
1799                         }
1800                 }
1801                 else
1802                 {
1803                         // Scroll in either direction by 20 pixels. Ideally, the
1804                         // amount of pixels per scrolling event should be
1805                         // configurable. Xara Xtreme currently uses an (hard
1806                         // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1807
1808                         const int scroll_pixel = 20;
1809
1810                         switch(event->scroll.direction)
1811                         {
1812                                 case GDK_SCROLL_UP:
1813                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1814                                         break;
1815                                 case GDK_SCROLL_DOWN:
1816                                         get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1817                                         break;
1818                                 case GDK_SCROLL_LEFT:
1819                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1820                                         break;
1821                                 case GDK_SCROLL_RIGHT:
1822                                         get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1823                                         break;
1824                                 default:
1825                                         break;
1826                         }
1827                 }
1828         }
1829                 break;
1830         default:
1831                 break;
1832         }
1833                 return false;
1834 }
1835
1836 bool
1837 WorkArea::on_hruler_event(GdkEvent */*event*/)
1838 {
1839 /*
1840         switch(event->type)
1841     {
1842         case GDK_BUTTON_PRESS:
1843                 if(dragging==DRAG_NONE)
1844                 {
1845                         dragging=DRAG_GUIDE;
1846                         curr_guide=get_guide_list_y().insert(get_guide_list_y().begin());
1847                         curr_guide_is_x=false;
1848                 }
1849                 return true;
1850                 break;
1851
1852         case GDK_MOTION_NOTIFY:
1853                 // Guide movement
1854                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1855                 {
1856                         double y,x;
1857                         if(event->button.axes)
1858                         {
1859                                 x=(event->button.axes[0]);
1860                                 y=(event->button.axes[1]);
1861                         }
1862                         else
1863                         {
1864                                 x=event->button.x;
1865                                 y=event->button.y;
1866                         }
1867
1868                         if(isnan(y) || isnan(x))
1869                                 return false;
1870
1871                         *curr_guide=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)))[1];
1872
1873                         queue_draw();
1874                 }
1875                 return true;
1876                 break;
1877
1878         case GDK_BUTTON_RELEASE:
1879                 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1880                 {
1881                         dragging=DRAG_NONE;
1882                         get_guide_list_y().erase(curr_guide);
1883                 }
1884                 break;
1885                 return true;
1886         default:
1887                 break;
1888         }
1889 */
1890         return false;
1891 }
1892
1893 bool
1894 WorkArea::on_vruler_event(GdkEvent */*event*/)
1895 {
1896 /*
1897         switch(event->type)
1898     {
1899         case GDK_BUTTON_PRESS:
1900                 if(dragging==DRAG_NONE)
1901                 {
1902                         dragging=DRAG_GUIDE;
1903                         curr_guide=get_guide_list_x().insert(get_guide_list_x().begin());
1904                         curr_guide_is_x=true;
1905                 }
1906                 return true;
1907                 break;
1908         case GDK_BUTTON_RELEASE:
1909                 if(dragging==DRAG_GUIDE && curr_guide_is_x==true)
1910                 {
1911                         dragging=DRAG_NONE;
1912                         get_guide_list_x().erase(curr_guide);
1913                 }
1914                 break;
1915                 return true;
1916         default:
1917                 break;
1918         }
1919 */
1920         return false;
1921 }
1922
1923
1924 void
1925 WorkArea::refresh_dimension_info()
1926 {
1927         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1928
1929         canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1930         canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1931
1932         pw=canvaswidth/w;
1933         ph=canvasheight/h;
1934
1935         scrollx_adjustment.set_page_increment(abs(get_grid_size()[0]));
1936         scrollx_adjustment.set_step_increment(abs(pw));
1937         scrollx_adjustment.set_lower(-abs(canvaswidth));
1938         scrollx_adjustment.set_upper(abs(canvaswidth));
1939         scrolly_adjustment.set_lower(-abs(canvasheight));
1940         scrolly_adjustment.set_upper(abs(canvasheight));
1941         scrolly_adjustment.set_step_increment(abs(ph));
1942         scrolly_adjustment.set_page_increment(abs(get_grid_size()[1]));
1943
1944
1945
1946         if(drawing_area->get_width()<=0 || drawing_area->get_height()<=0 || w==0 || h==0)
1947                 return;
1948
1949         const synfig::Point focus_point(get_focus_point());
1950         const synfig::Real x(focus_point[0]/pw+drawing_area->get_width()/2-w/2);
1951         const synfig::Real y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1952
1953         window_tl[0]=rend_desc.get_tl()[0]-pw*x;
1954         window_br[0]=rend_desc.get_br()[0]+pw*(drawing_area->get_width()-x-w);
1955
1956         window_tl[1]=rend_desc.get_tl()[1]-ph*y;
1957         window_br[1]=rend_desc.get_br()[1]+ph*(drawing_area->get_height()-y-h);
1958
1959         hruler->property_lower()=Distance(window_tl[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1960         hruler->property_upper()=Distance(window_br[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1961         vruler->property_lower()=Distance(window_tl[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1962         vruler->property_upper()=Distance(window_br[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1963
1964         view_window_changed();
1965 }
1966
1967
1968 synfig::Point
1969 WorkArea::screen_to_comp_coords(synfig::Point pos)const
1970 {
1971         synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1972         //synfig::Vector::value_type canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1973         //synfig::Vector::value_type canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1974         //synfig::Vector::value_type pw=canvaswidth/w;
1975         //synfig::Vector::value_type ph=canvasheight/h;
1976         Vector focus_point=get_focus_point();
1977         synfig::Vector::value_type x=focus_point[0]/pw+drawing_area->get_width()/2-w/2;
1978         synfig::Vector::value_type y=focus_point[1]/ph+drawing_area->get_height()/2-h/2;
1979
1980         return rend_desc.get_tl()-synfig::Point(pw*x,ph*y)+synfig::Point(pw*pos[0],ph*pos[1]);
1981 }
1982
1983 synfig::Point
1984 WorkArea::comp_to_screen_coords(synfig::Point /*pos*/)const
1985 {
1986         synfig::warning("WorkArea::comp_to_screen_coords: Not yet implemented");
1987         return synfig::Point();
1988 }
1989
1990 int
1991 WorkArea::next_unrendered_tile(int refreshes)const
1992 {
1993         //assert(!tile_book.empty());
1994         if(tile_book.empty())
1995                 return -1;
1996
1997         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1998
1999         const synfig::Vector focus_point(get_focus_point());
2000
2001         // Calculate the window coordinates of the top-left
2002         // corner of the canvas.
2003         const synfig::Vector::value_type
2004                 x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2005                 y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2006
2007         int div = low_res_pixel_size;
2008         const int width_in_tiles(w/tile_w+((low_resolution?((w/div)%(tile_w/div)):(w%tile_w))?1:0));
2009         const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
2010
2011         int
2012                 u(0),v(0),
2013                 u1(int(-x/tile_w)),
2014                 v1(int(-y/tile_h)),
2015                 u2(int((-x+drawing_area->get_width())/tile_w+1)),
2016                 v2(int((-y+drawing_area->get_height())/tile_h+1));
2017
2018         if(u2>width_in_tiles)u2=width_in_tiles;
2019         if(v2>height_in_tiles)v2=height_in_tiles;
2020         if(u1<0)u1=0;
2021         if(v1<0)v1=0;
2022
2023         int last_good_tile(-1);
2024
2025         for(v=v1;v<v2;v++)
2026                 for(u=u1;u<u2;u++)
2027                 {
2028                         int index(v*width_in_tiles+u);
2029                         if(tile_book[index].second<refreshes)
2030                         {
2031                                 last_good_tile=index;
2032                                 if(rand()%8==0)
2033                                         return index;
2034                         }
2035                 }
2036         return last_good_tile;
2037 }
2038
2039 /*
2040 template <typename F, typename T=WorkAreaRenderer, typename R=typename F::result_type>
2041 class handle2ptr_t : public std::unary_function<typename etl::handle<T>,R>
2042 {
2043 private:
2044         F func;
2045 public:
2046         handle2ptr_t(const F &func):func(func) { };
2047
2048         R operator()(typename etl::handle<T> x) { return func(*x); }
2049 };
2050
2051 template <typename F>
2052 handle2ptr_t<F>
2053 handle2ptr(F func)
2054 {
2055         return handle2ptr_t<F>(func);
2056 }
2057         for_each(
2058                 renderer_set_.begin(),
2059                 renderer_set_.end(),
2060                 handle2ptr(
2061                         sigc::bind(
2062                                 sigc::bind(
2063                                         sigc::mem_fun(
2064                                                 &WorkAreaRenderer::render_vfunc
2065                                         ),
2066                                         Gdk::Rectangle(event->area)
2067                                 ),
2068                                 drawing_area->get_window()
2069                         )
2070                 )
2071         );
2072 */
2073
2074 bool
2075 WorkArea::refresh(GdkEventExpose*event)
2076 {
2077         assert(get_canvas());
2078
2079         drawing_area->get_window()->clear();
2080
2081         //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2082
2083         const synfig::Vector focus_point(get_focus_point());
2084
2085         // Update the old focus point
2086         last_focus_point=focus_point;
2087
2088         // Draw out the renderables
2089         {
2090                 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
2091                 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
2092                 {
2093                         if((*iter)->get_enabled())
2094                                 (*iter)->render_vfunc(
2095                                         drawing_area->get_window(),
2096                                         Gdk::Rectangle(&event->area)
2097                                 );
2098                 }
2099         }
2100
2101         // Calculate the window coordinates of the top-left
2102         // corner of the canvas.
2103         //const synfig::Vector::value_type
2104         //      x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2105         //      y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2106
2107         //const synfig::Vector::value_type window_startx(window_tl[0]);
2108         //const synfig::Vector::value_type window_endx(window_br[0]);
2109         //const synfig::Vector::value_type window_starty(window_tl[1]);
2110         //const synfig::Vector::value_type window_endy(window_br[1]);
2111
2112         Glib::RefPtr<Gdk::GC> gc=Gdk::GC::create(drawing_area->get_window());
2113
2114         // If we are in animate mode, draw a red border around the screen
2115         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2116         {
2117 // #define USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2118 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2119                 // This method of drawing the red border doesn't work on any
2120                 // Gtk theme which uses the crux-engine, hcengine, industrial,
2121                 // mist, or ubuntulooks engine, such as the default ubuntu
2122                 // 'Human' theme.
2123                 drawing_frame->modify_bg(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2124 #else
2125                 // So let's do it in a more primitive fashion.
2126                 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
2127                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
2128                 drawing_area->get_window()->draw_rectangle(
2129                         gc,
2130                         false,  // Fill?
2131                         0,0,    // x,y
2132                         drawing_area->get_width()-1,drawing_area->get_height()-1); // w,h
2133 #endif
2134         }
2135 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2136         else
2137                 drawing_frame->unset_bg(Gtk::STATE_NORMAL);
2138 #endif
2139
2140         return true;
2141 }
2142
2143 void
2144 WorkArea::done_rendering()
2145 {
2146 /*
2147         assert(buffer);
2148         assert(w>0);
2149         assert(h>0);
2150         pix_buf=Gdk::Pixbuf::create_from_data(
2151                 buffer, // pointer to the data
2152                 Gdk::COLORSPACE_RGB, // the colorspace
2153                 true, // has alpha?
2154                 8, // bits per sample
2155                 w,      // width
2156                 h,      // height
2157                 w*bpp); // stride (pitch)
2158         assert(pix_buf);
2159 */
2160 }
2161
2162
2163 void
2164 WorkArea::set_quality(int x)
2165 {
2166         if(x==quality)
2167                 return;
2168         quality=x;
2169         queue_render_preview();
2170 }
2171
2172 void
2173 WorkArea::set_low_res_pixel_size(int x)
2174 {
2175         if(x==low_res_pixel_size)
2176                 return;
2177         low_res_pixel_size=x;
2178         queue_render_preview();
2179 }
2180
2181 namespace studio
2182 {
2183 class WorkAreaProgress : public synfig::ProgressCallback
2184 {
2185         WorkArea *work_area;
2186         ProgressCallback *cb;
2187
2188 public:
2189
2190         WorkAreaProgress(WorkArea *work_area,ProgressCallback *cb):
2191                 work_area(work_area),cb(cb)
2192         {
2193                 assert(cb);
2194         }
2195
2196         virtual bool
2197         task(const std::string &str)
2198         {
2199                 if(work_area->dirty)
2200                         return false;
2201                 return cb->task(str);
2202         }
2203
2204         virtual bool
2205         error(const std::string &err)
2206         {
2207                 if(work_area->dirty)
2208                         return false;
2209                 return cb->error(err);
2210         }
2211
2212         virtual bool
2213         amount_complete(int current, int total)
2214         {
2215                 if(work_area->dirty)
2216                         return false;
2217                 return cb->amount_complete(current,total);
2218         }
2219 };
2220 }
2221
2222 bool
2223 studio::WorkArea::async_update_preview()
2224 {
2225 #ifdef SINGLE_THREADED
2226         if (get_updating())
2227         {
2228                 stop_updating();
2229                 queue_render_preview();
2230                 return false;
2231         }
2232 #endif
2233
2234         async_renderer=0;
2235
2236         queued=false;
2237         canceled_=false;
2238         get_canvas_view()->reset_cancel_status();
2239
2240         // This object will mark us as busy until
2241         // we are done.
2242         //studio::App::Busy busy;
2243
2244         //WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2245         //synfig::ProgressCallback *cb=&callback;
2246
2247         if(!is_visible())return false;
2248
2249         /*
2250         // If we are queued to render the scene at the next idle
2251         // go ahead and de-queue it.
2252         if(render_idle_func_id)
2253         {
2254                 g_source_remove(render_idle_func_id);
2255                 //queued=false;
2256                 render_idle_func_id=0;
2257         }
2258         */
2259
2260         dirty=false;
2261         get_canvas_view()->reset_cancel_status();
2262
2263         //bool ret=false;
2264         RendDesc desc=get_canvas()->rend_desc();
2265
2266         int w=(int)(desc.get_w()*zoom);
2267         int h=(int)(desc.get_h()*zoom);
2268
2269         // ensure that the size we draw is at least one pixel in each dimension
2270         int min_size = low_resolution ? low_res_pixel_size : 1;
2271         if (w < min_size) w = min_size;
2272         if (h < min_size) h = min_size;
2273
2274         // Setup the description parameters
2275         desc.set_antialias(1);
2276         desc.set_time(cur_time);
2277
2278         set_rend_desc(desc);
2279
2280         // Create the render target
2281         handle<Target> target;
2282
2283         // if we have lots of pixels to render and the tile renderer isn't disabled, use it
2284         int div;
2285         div = low_resolution ? low_res_pixel_size : 1;
2286         if ((w*h > 240*div*135*div && !getenv("SYNFIG_DISABLE_TILE_RENDER")) || getenv("SYNFIG_FORCE_TILE_RENDER"))
2287         {
2288                 // do a tile render
2289                 handle<WorkAreaTarget> trgt(new class WorkAreaTarget(this,w,h));
2290
2291                 trgt->set_rend_desc(&desc);
2292                 trgt->set_onion_skin(get_onion_skin());
2293                 target=trgt;
2294         }
2295         else
2296         {
2297                 // do a scanline render
2298                 handle<WorkAreaTarget_Full> trgt(new class WorkAreaTarget_Full(this,w,h));
2299
2300                 trgt->set_rend_desc(&desc);
2301                 trgt->set_onion_skin(get_onion_skin());
2302                 target=trgt;
2303         }
2304
2305         // We can rest assured that our time has already
2306         // been set, so there is no need to have to
2307         // recalculate that over again.
2308         // UPDATE: This is kind of needless with
2309         // the way that time is handled now in SYNFIG.
2310         //target->set_avoid_time_sync(true);
2311         async_renderer=new AsyncRenderer(target);
2312         async_renderer->signal_finished().connect(
2313                 sigc::mem_fun(this,&WorkArea::async_update_finished)
2314         );
2315
2316         rendering=true;
2317         async_renderer->start();
2318
2319         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2320
2321         rendering=true;
2322         cb->task(_("Rendering..."));
2323         rendering=true;
2324
2325         return true;
2326 }
2327
2328 void
2329 studio::WorkArea::async_update_finished()
2330 {
2331         synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2332
2333         rendering=false;
2334
2335         if(!async_renderer)
2336                 return;
2337
2338         // If we completed successfully, then
2339         // we aren't dirty anymore
2340         if(async_renderer->has_success())
2341         {
2342                 dirty=false;
2343                 //queued=false;
2344                 cb->task(_("Idle"));
2345         }
2346         else
2347         {
2348                 dirty=true;
2349                 cb->task(_("Render Failed"));
2350         }
2351         //get_canvas_view()->reset_cancel_status();
2352         done_rendering();
2353 }
2354
2355 bool
2356 studio::WorkArea::sync_update_preview()
2357 {
2358         //      const Time &time(cur_time);
2359
2360         canceled_=false;
2361         get_canvas_view()->reset_cancel_status();
2362
2363         async_renderer=0;
2364
2365 again:
2366         // This object will mark us as busy until
2367         // we are done.
2368         studio::App::Busy busy;
2369
2370         WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2371         synfig::ProgressCallback *cb=&callback;
2372
2373         // We don't want to render if we are already rendering
2374         if(rendering)
2375         {
2376                 dirty=true;
2377                 return false;
2378         }
2379
2380         if(!is_visible())return false;
2381         get_canvas()->set_time(get_time());
2382         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2383         signal_rendering()();
2384
2385         // If we are queued to render the scene at the next idle
2386         // go ahead and de-queue it.
2387         if(render_idle_func_id)
2388         {
2389                 g_source_remove(render_idle_func_id);
2390                 //queued=false;
2391                 render_idle_func_id=0;
2392         }
2393         // Start rendering
2394         rendering=true;
2395
2396         dirty=false;
2397         get_canvas_view()->reset_cancel_status();
2398
2399         RendDesc desc=get_canvas()->rend_desc();
2400         //newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2401
2402         int w=(int)(desc.get_w()*zoom);
2403         int h=(int)(desc.get_h()*zoom);
2404
2405         // Setup the description parameters
2406         desc.set_antialias(1);
2407         desc.set_time(cur_time);
2408         //desc.set_wh(w,h);
2409
2410         set_rend_desc(desc);
2411
2412         // Create the render target
2413         handle<WorkAreaTarget> target(new class WorkAreaTarget(this,w,h));
2414
2415         target->set_rend_desc(&desc);
2416
2417         // We can rest assured that our time has already
2418         // been set, so there is no need to have to
2419         // recalculate that over again.
2420         target->set_avoid_time_sync(true);
2421
2422         if(cb)
2423                 cb->task(strprintf(_("Rendering canvas %s..."),get_canvas()->get_name().c_str()));
2424
2425         bool ret = target->render(cb);
2426
2427         if(!ret && !get_canvas_view()->get_cancel_status() && dirty)
2428         {
2429                 rendering=false;
2430                 //canceled_=true;
2431                 goto again;
2432         }
2433         if(get_canvas_view()->get_cancel_status())
2434                 canceled_=true;
2435
2436         if(cb)
2437         {
2438                 if(ret)
2439                         cb->task(_("Idle"));
2440                 else
2441                         cb->task(_("Render Failed"));
2442                 cb->amount_complete(0,1);
2443         }
2444
2445         // Refresh the work area to make sure that
2446         // it is being displayed correctly
2447         drawing_area->queue_draw();
2448
2449         // If we completed successfully, then
2450         // we aren't dirty anymore
2451         if(ret)
2452         {
2453                 dirty=false;
2454                 //queued=false;
2455         }
2456         else dirty=true;
2457         rendering=false;
2458         //get_canvas_view()->reset_cancel_status();
2459         done_rendering();
2460         return ret;
2461 }
2462
2463 void
2464 studio::WorkArea::async_render_preview(synfig::Time time)
2465 {
2466         cur_time=time;
2467         //tile_book.clear();
2468
2469         refreshes+=5;
2470         if(!is_visible())return;
2471
2472         get_canvas()->set_time(get_time());
2473         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2474         signal_rendering()();
2475
2476         async_update_preview();
2477 }
2478 void
2479 WorkArea::async_render_preview()
2480 {
2481         return async_render_preview(get_canvas_view()->get_time());
2482 }
2483
2484 bool
2485 studio::WorkArea::sync_render_preview(synfig::Time time)
2486 {
2487         cur_time=time;
2488         //tile_book.clear();
2489         refreshes+=5;
2490         if(!is_visible())return false;
2491         return sync_update_preview();
2492 }
2493
2494 bool
2495 WorkArea::sync_render_preview()
2496 {
2497         return sync_render_preview(get_canvas_view()->get_time());
2498 }
2499
2500 void
2501 WorkArea::sync_render_preview_hook()
2502 {
2503         sync_render_preview(get_canvas_view()->get_time());
2504 }
2505
2506 void
2507 WorkArea::queue_scroll()
2508 {
2509 //      const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2510
2511         const synfig::Point focus_point(get_focus_point());
2512
2513         const synfig::Real
2514                 new_x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2515                 new_y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2516
2517         const synfig::Real
2518                 old_x(last_focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2519                 old_y(last_focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2520
2521         // If the coordinates didn't change, we shouldn't queue a draw
2522         if(old_x==new_x && old_y==new_y)
2523                 return;
2524
2525         const int
2526                 dx(round_to_int(old_x)-round_to_int(new_x)),
2527                 dy(round_to_int(old_y)-round_to_int(new_y));
2528
2529         drawing_area->get_window()->scroll(-dx,-dy);
2530
2531         if (timecode_width && timecode_height)
2532         {
2533                 drawing_area->queue_draw_area(4,       4,    4+timecode_width,    4+timecode_height);
2534                 drawing_area->queue_draw_area(4-dx, 4-dy, 4-dx+timecode_width, 4-dy+timecode_height);
2535         }
2536
2537 #ifndef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2538         if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2539         {
2540                 int maxx = drawing_area->get_width()-1;
2541                 int maxy = drawing_area->get_height()-1;
2542
2543                 if (dx > 0)
2544                 {
2545                         drawing_area->queue_draw_area(      0, 0,       1, maxy);
2546                         drawing_area->queue_draw_area(maxx-dx, 0, maxx-dx, maxy);
2547                 }
2548                 else if (dx < 0) 
2549                 {
2550                         drawing_area->queue_draw_area(   maxx, 0,    maxx, maxy);
2551                         drawing_area->queue_draw_area(    -dx, 0,     -dx, maxy);
2552                 }
2553                 if (dy > 0)
2554                 {
2555                         drawing_area->queue_draw_area(0,       0, maxx,       1);
2556                         drawing_area->queue_draw_area(0, maxy-dy, maxx, maxy-dy);
2557                 }
2558                 else if (dy < 0) 
2559                 {
2560                         drawing_area->queue_draw_area(0,    maxy, maxx,    maxy);
2561                         drawing_area->queue_draw_area(0,     -dy, maxx,     -dy);
2562                 }
2563         }
2564 #endif // USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2565
2566         last_focus_point=focus_point;
2567 }
2568
2569 void
2570 studio::WorkArea::zoom_in()
2571 {
2572         set_zoom(zoom*1.25);
2573 }
2574
2575 void
2576 studio::WorkArea::zoom_out()
2577 {
2578         set_zoom(zoom/1.25);
2579 }
2580
2581 void
2582 studio::WorkArea::zoom_fit()
2583 {
2584         float new_zoom(min(drawing_area->get_width() * zoom / w,
2585                                            drawing_area->get_height() * zoom / h) * 0.995);
2586         if (zoom / new_zoom > 0.995 && new_zoom / zoom > 0.995)
2587         {
2588                 set_zoom(prev_zoom);
2589                 return set_focus_point(previous_focus);
2590         }
2591         previous_focus = get_focus_point();
2592         prev_zoom = zoom;
2593         set_zoom(new_zoom);
2594         set_focus_point(Point(0,0));
2595 }
2596
2597 void
2598 studio::WorkArea::zoom_norm()
2599 {
2600         if (zoom == 1.0) return set_zoom(prev_zoom);
2601         prev_zoom = zoom;
2602         set_zoom(1.0f);
2603 }
2604
2605 gboolean
2606 studio::WorkArea::__render_preview(gpointer data)
2607 {
2608         WorkArea *work_area(static_cast<WorkArea*>(data));
2609
2610         // there's no point anyone trying to cancel the timer now - it's gone off already
2611         work_area->render_idle_func_id = 0;
2612
2613         work_area->queued=false;
2614         work_area->async_render_preview(work_area->get_canvas_view()->get_time());
2615
2616         return 0;
2617 }
2618
2619 void
2620 studio::WorkArea::queue_render_preview()
2621 {
2622         //synfig::info("queue_render_preview(): called for %s", get_canvas_view()->get_time().get_string().c_str());
2623
2624         if(queued==true)
2625         {
2626                 return;
2627                 //synfig::info("queue_render_preview(): already queued, unqueuing");
2628 /*              if(render_idle_func_id)
2629                         g_source_remove(render_idle_func_id);
2630                 render_idle_func_id=0;
2631                 queued=false;
2632 */
2633                 //async_renderer=0;
2634         }
2635
2636         if(dirty_trap_enabled)
2637         {
2638                 dirty_trap_queued++;
2639                 return;
2640         }
2641
2642         int queue_time=50;
2643
2644         if(rendering)
2645                 queue_time+=250;
2646
2647
2648         if(queued==false)
2649         {
2650                 //synfig::info("queue_render_preview(): (re)queuing...");
2651                 //render_idle_func_id=g_idle_add_full(G_PRIORITY_DEFAULT,__render_preview,this,NULL);
2652                 render_idle_func_id=g_timeout_add_full(
2653                         G_PRIORITY_DEFAULT,     // priority - 
2654                         queue_time,                     // interval - the time between calls to the function, in milliseconds (1/1000ths of a second)
2655                         __render_preview,       // function - function to call
2656                         this,                           // data     - data to pass to function
2657                         NULL);                          // notify   - function to call when the idle is removed, or NULL
2658                 queued=true;
2659         }
2660 /*      else if(rendering)
2661         {
2662                 refreshes+=5;
2663                 dirty=true;
2664                 queue_draw();
2665         }
2666 */
2667 }
2668
2669 DirtyTrap::DirtyTrap(WorkArea *work_area):work_area(work_area)
2670 {
2671         work_area->dirty_trap_enabled=true;
2672
2673         work_area->dirty_trap_queued=0;
2674 }
2675
2676 DirtyTrap::~DirtyTrap()
2677 {
2678         work_area->dirty_trap_enabled=false;
2679         if(work_area->dirty_trap_queued)
2680                 work_area->queue_render_preview();
2681 }
2682
2683 void
2684 studio::WorkArea::queue_draw_preview()
2685 {
2686         drawing_area->queue_draw();
2687 }
2688
2689 void
2690 studio::WorkArea::set_cursor(const Gdk::Cursor& x)
2691 {
2692         drawing_area->get_window()->set_cursor(x);
2693 }
2694 void
2695 studio::WorkArea::set_cursor(Gdk::CursorType x)
2696 {
2697         drawing_area->get_window()->set_cursor(Gdk::Cursor(x));
2698 }
2699
2700 #include "iconcontroller.h"
2701
2702 void
2703 studio::WorkArea::refresh_cursor()
2704 {
2705 //      set_cursor(IconController::get_tool_cursor(canvas_view->get_smach().get_state_name(),drawing_area->get_window()));
2706 }
2707
2708 void
2709 studio::WorkArea::reset_cursor()
2710 {
2711         drawing_area->get_window()->set_cursor(Gdk::Cursor(Gdk::TOP_LEFT_ARROW));
2712 //      set_cursor(Gdk::TOP_LEFT_ARROW);
2713 }
2714
2715 void
2716 studio::WorkArea::set_zoom(float z)
2717 {
2718         z=max(1.0f/128.0f,min(128.0f,z));
2719         if(z==zoom)
2720                 return;
2721         zoom = z;
2722         refresh_dimension_info();
2723         /*if(async_renderer)
2724         {
2725                 async_renderer->stop();
2726                 async_renderer=0;
2727         }*/
2728         refreshes+=5;
2729         async_update_preview();
2730         //queue_render_preview();
2731 }
2732
2733 void
2734 WorkArea::set_selected_value_node(etl::loose_handle<synfig::ValueNode> x)
2735 {
2736         if(x!=selected_value_node_)
2737         {
2738                 selected_value_node_=x;
2739                 queue_draw();
2740         }
2741 }
2742
2743 void
2744 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x)
2745 {
2746         renderer_set_.insert(x);
2747         x->set_work_area(this);
2748         queue_draw();
2749 }
2750
2751 void
2752 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x, int priority)
2753 {
2754         x->set_priority(priority);
2755         insert_renderer(x);
2756 }
2757
2758 void
2759 WorkArea::erase_renderer(const etl::handle<WorkAreaRenderer> &x)
2760 {
2761         x->set_work_area(0);
2762         renderer_set_.erase(x);
2763         queue_draw();
2764 }
2765
2766 void
2767 WorkArea::resort_render_set()
2768 {
2769         std::set<etl::handle<WorkAreaRenderer> > tmp(
2770                 renderer_set_.begin(),
2771                 renderer_set_.end()
2772         );
2773         renderer_set_.swap(tmp);
2774         queue_draw();
2775 }