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