1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2006 Yue Shi Lai
10 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include <sigc++/adaptors/hide.h>
37 #include "canvasview.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>
47 #include <sigc++/retype_return.h>
48 #include <sigc++/retype.h>
49 #include <sigc++/hide.h>
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"
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>
73 #include <synfig/mutex.h>
79 /* === U S I N G =========================================================== */
83 using namespace synfig;
84 using namespace studio;
86 /* === M A C R O S ========================================================= */
91 #define stratof(X) (atof((X).c_str()))
92 #define stratoi(X) (atoi((X).c_str()))
96 /* === G L O B A L S ======================================================= */
98 /* === C L A S S E S ======================================================= */
100 class studio::WorkAreaTarget : public synfig::Target_Tile
106 int real_tile_w,real_tile_h;
107 //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
109 int twindow_start, twindow_width, twindow_height, twindow_pad;
113 bool onion_first_tile;
116 std::list<synfig::Time> onion_skin_queue;
120 void set_onion_skin(bool x)
124 Time time(rend_desc().get_time_start());
126 onion_skin_queue.push_back(time);
127 //onion_skin_queue.push_back(time-1);
128 //onion_skin_queue.push_back(time+1);
132 onion_skin_queue.push_back(
133 get_canvas()->keyframe_list().find_prev(
143 onion_skin_queue.push_back(
144 get_canvas()->keyframe_list().find_next(
152 onion_layers=onion_skin_queue.size();
154 onion_first_tile=false;
158 WorkAreaTarget(WorkArea *workarea,int w, int h):
160 low_res(workarea->get_low_resolution_flag()),
163 real_tile_w(workarea->tile_w),
164 real_tile_h(workarea->tile_h),
165 refresh_id(workarea->refreshes),
169 //set_remove_alpha();
170 //set_avoid_time_sync();
174 int div = workarea->get_low_res_pixel_size();
175 set_tile_w(workarea->tile_w/div);
176 set_tile_h(workarea->tile_h/div);
180 set_tile_w(workarea->tile_w);
181 set_tile_h(workarea->tile_h);
183 set_canvas(workarea->get_canvas());
184 set_quality(workarea->get_quality());
189 workarea->queue_draw();
192 virtual bool set_rend_desc(synfig::RendDesc *newdesc)
195 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
197 int div = workarea->get_low_res_pixel_size();
198 newdesc->set_wh(w/div,h/div);
201 newdesc->set_wh(w,h);
205 || workarea->get_h()!=h
206 ) workarea->set_wh(w,h,4);
208 workarea->full_frame=false;
214 virtual int total_tiles()const
216 int tw(rend_desc().get_w()/get_tile_w());
217 int th(rend_desc().get_h()/get_tile_h());
218 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
219 if(rend_desc().get_h()%get_tile_h()!=0)th++;
223 virtual int next_frame(Time& time)
225 synfig::Mutex::Lock lock(mutex);
228 return synfig::Target_Tile::next_frame(time);
230 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
232 if(!onion_skin_queue.empty())
234 time=onion_skin_queue.front();
235 onion_skin_queue.pop_front();
240 return onion_skin_queue.size()+1;
243 virtual int next_tile(int& x, int& y)
245 synfig::Mutex::Lock lock(mutex);
246 //if(workarea->tile_queue.empty()) return 0;
248 //int curr_tile(workarea->tile_queue.front());
249 //workarea->tile_queue.pop_front();
250 int curr_tile(workarea->next_unrendered_tile(refresh_id-onion_skin_queue.size()));
254 // Width of the image(in tiles)
255 int tw(rend_desc().get_w()/get_tile_w());
256 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
258 y=(curr_tile/tw)*get_tile_w();
259 x=(curr_tile%tw)*get_tile_h();
261 // Mark this tile as "up-to-date"
263 workarea->tile_book[curr_tile].second=refresh_id-onion_skin_queue.size();
265 workarea->tile_book[curr_tile].second=refresh_id;
267 return total_tiles()-curr_tile+1;
271 virtual bool start_frame(synfig::ProgressCallback */*cb*/)
273 synfig::Mutex::Lock lock(mutex);
275 int tw(rend_desc().get_w()/get_tile_w());
276 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
277 int th(rend_desc().get_h()/get_tile_h());
278 if(rend_desc().get_h()%get_tile_h()!=0)th++;
285 workarea->tile_book.resize(total_tiles());
286 //tile_iter=workarea->tile_book.begin()+twindow_start;
290 static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
292 virtual bool add_tile(const synfig::Surface &surface, int x, int y)
294 synfig::Mutex::Lock lock(mutex);
297 PixelFormat pf(PF_RGB);
299 const int total_bytes(get_tile_w()*get_tile_h()*synfig::channels(pf));
301 unsigned char *buffer((unsigned char*)malloc(total_bytes));
303 if(!surface || !buffer)
306 unsigned char *dest(buffer);
307 const Color *src(surface[0]);
310 int x(surface.get_w()*surface.get_h());
312 // int div = workarea->get_low_res_pixel_size();
315 Color dark(0.6,0.6,0.6);
316 Color lite(0.8,0.8,0.8);
318 dest=Color2PixelFormat(
321 ((i/surface.get_w())*8/h+(i%surface.get_w())*8/w)&1?dark:lite,
330 int tw(rend_desc().get_w()/get_tile_w());
331 if(rend_desc().get_w()%get_tile_w()!=0)tw++;
332 unsigned int index=y*tw+x;
335 if(index>workarea->tile_book.size())
338 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
340 pixbuf=Gdk::Pixbuf::create_from_data(
341 buffer, // pointer to the data
342 Gdk::COLORSPACE_RGB, // the colorspace
343 ((pf&PF_A)==PF_A), // has alpha?
344 8, // bits per sample
345 surface.get_w(), // width
346 surface.get_h(), // height
347 surface.get_w()*synfig::channels(pf), // stride (pitch)
348 sigc::ptr_fun(&WorkAreaTarget::free_buff)
353 // We need to scale up
354 int div = workarea->get_low_res_pixel_size();
355 pixbuf=pixbuf->scale_simple(
362 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
364 workarea->tile_book[index].first=pixbuf;
369 workarea->tile_book[index].first, // Dest
372 pixbuf->get_width(), // dest width
373 pixbuf->get_height(), // dest_height,
374 0, // double offset_x
375 0, // double offset_y
378 Gdk::INTERP_NEAREST, // interp
379 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
384 workarea->queue_draw();
385 assert(workarea->tile_book[index].first);
389 virtual void end_frame()
391 //workarea->queue_draw();
396 class studio::WorkAreaTarget_Full : public synfig::Target_Scanline
402 int real_tile_w,real_tile_h;
403 //std::vector<Glib::RefPtr<Gdk::Pixbuf> >::iterator tile_iter;
405 int twindow_start, twindow_width, twindow_height, twindow_pad;
409 bool onion_first_tile;
414 std::list<synfig::Time> onion_skin_queue;
416 void set_onion_skin(bool x)
420 Time time(rend_desc().get_time_start());
422 onion_skin_queue.push_back(time);
423 //onion_skin_queue.push_back(time-1);
424 //onion_skin_queue.push_back(time+1);
430 onion_skin_queue.push_back(
431 get_canvas()->keyframe_list().find_prev(
441 onion_skin_queue.push_back(
442 get_canvas()->keyframe_list().find_next(
450 onion_layers=onion_skin_queue.size();
452 onion_first_tile=false;
456 WorkAreaTarget_Full(WorkArea *workarea,int w, int h):
458 low_res(workarea->get_low_resolution_flag()),
461 refresh_id(workarea->refreshes),
465 set_canvas(workarea->get_canvas());
466 set_quality(workarea->get_quality());
469 ~WorkAreaTarget_Full()
473 virtual bool set_rend_desc(synfig::RendDesc *newdesc)
476 newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
479 int div = workarea->get_low_res_pixel_size();
480 newdesc->set_wh(w/div,h/div);
483 newdesc->set_wh(w,h);
487 || workarea->get_h()!=h
488 ) workarea->set_wh(w,h,4);
490 surface.set_wh(newdesc->get_w(),newdesc->get_h());
493 workarea->full_frame=true;
494 workarea->tile_book.resize(1);
498 virtual int next_frame(Time& time)
500 // Mark this tile as "up-to-date"
502 workarea->tile_book[0].second=refresh_id-onion_skin_queue.size();
504 workarea->tile_book[0].second=refresh_id;
507 return synfig::Target_Scanline::next_frame(time);
509 onion_first_tile=(onion_layers==(signed)onion_skin_queue.size());
511 if(!onion_skin_queue.empty())
513 time=onion_skin_queue.front();
514 onion_skin_queue.pop_front();
518 return onion_skin_queue.size()+1;
522 virtual bool start_frame(synfig::ProgressCallback */*cb*/)
527 virtual Color * start_scanline(int scanline)
529 return surface[scanline];
532 virtual bool end_scanline()
537 static void free_buff(const guint8 *x) { free(const_cast<guint8*>(x)); }
539 virtual void end_frame()
543 PixelFormat pf(PF_RGB);
545 const int total_bytes(surface.get_w()*surface.get_h()*synfig::channels(pf));
547 unsigned char *buffer((unsigned char*)malloc(total_bytes));
549 if(!surface || !buffer)
552 unsigned char *dest(buffer);
553 const Color *src(surface[0]);
554 int w(surface.get_w());
555 //int h(surface.get_h());
556 int x(surface.get_w()*surface.get_h());
558 // int div = workarea->get_low_res_pixel_size();
561 Color dark(0.6,0.6,0.6);
562 Color lite(0.8,0.8,0.8);
563 int tw=workarea->tile_w;
564 int th=workarea->tile_h;
567 int div = workarea->get_low_res_pixel_size();
572 dest=Color2PixelFormat(
575 ((i/w)*8/th+(i%w)*8/tw)&1?dark:lite,
584 Glib::RefPtr<Gdk::Pixbuf> pixbuf;
586 pixbuf=Gdk::Pixbuf::create_from_data(
587 buffer, // pointer to the data
588 Gdk::COLORSPACE_RGB, // the colorspace
589 ((pf&PF_A)==PF_A), // has alpha?
590 8, // bits per sample
591 surface.get_w(), // width
592 surface.get_h(), // height
593 surface.get_w()*synfig::channels(pf), // stride (pitch)
594 sigc::ptr_fun(&WorkAreaTarget::free_buff)
599 // We need to scale up
600 int div = workarea->get_low_res_pixel_size();
601 pixbuf=pixbuf->scale_simple(
610 if(!onionskin || onion_first_tile || !workarea->tile_book[index].first)
612 workarea->tile_book[index].first=pixbuf;
617 workarea->tile_book[index].first, // Dest
620 pixbuf->get_width(), // dest width
621 pixbuf->get_height(), // dest_height,
622 0, // double offset_x
623 0, // double offset_y
626 Gdk::INTERP_NEAREST, // interp
627 255/(onion_layers-onion_skin_queue.size()+1) //int overall_alpha
631 workarea->queue_draw();
632 assert(workarea->tile_book[index].first);
639 /* === M E T H O D S ======================================================= */
642 WorkArea::WorkArea(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface):
643 Gtk::Table(4+RULER_FIX, 3, false),
644 canvas_interface(canvas_interface),
645 canvas(canvas_interface->get_canvas()),
646 scrollx_adjustment(0,-4,4,0.01,0.1),
647 scrolly_adjustment(0,-4,4,0.01,0.1),
662 allow_duck_clicks=true;
663 allow_layer_clicks=true;
664 render_idle_func_id=0;
667 low_res_pixel_size=2;
673 last_focus_point=Point(0,0);
676 dirty_trap_enabled=false;
681 meta_data_lock=false;
683 insert_renderer(new Renderer_Canvas, 000);
684 insert_renderer(new Renderer_Grid, 100);
685 insert_renderer(new Renderer_Guides, 200);
686 insert_renderer(new Renderer_Ducks, 300);
687 insert_renderer(new Renderer_BBox, 399);
688 insert_renderer(new Renderer_Dragbox, 400);
689 insert_renderer(new Renderer_Timecode, 500);
691 signal_duck_selection_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
692 signal_strokes_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
693 signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::queue_draw));
694 signal_grid_changed().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
695 signal_sketch_saved().connect(sigc::mem_fun(*this,&studio::WorkArea::save_meta_data));
697 // Not that it really makes a difference... (setting this to zero, that is)
700 drawing_area=manage(new class Gtk::DrawingArea());
701 drawing_area->show();
702 drawing_area->set_extension_events(Gdk::EXTENSION_EVENTS_ALL);
704 drawing_frame=manage(new Gtk::Frame);
705 drawing_frame->add(*drawing_area);
706 //drawing_frame->set_shadow_type(Gtk::SHADOW_NONE);
707 //drawing_frame->property_border_width()=5;
708 //drawing_frame->modify_fg(Gtk::STATE_NORMAL,Gdk::Color("#00ffff"));
709 //drawing_frame->modify_base(Gtk::STATE_NORMAL,Gdk::Color("#ff00ff"));
710 /*drawing_frame->modify_fg(Gtk::STATE_ACTIVE,Gdk::Color("#00ffff"));
711 drawing_frame->modify_base(Gtk::STATE_ACTIVE,Gdk::Color("#ff00ff"));
712 drawing_frame->modify_bg(Gtk::STATE_ACTIVE,Gdk::Color("#00ff00"));
713 drawing_frame->modify_fg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ffff"));
714 drawing_frame->modify_base(Gtk::STATE_INSENSITIVE,Gdk::Color("#ff00ff"));
715 drawing_frame->modify_bg(Gtk::STATE_INSENSITIVE,Gdk::Color("#00ff00"));
716 drawing_frame->modify_fg(Gtk::STATE_SELECTED,Gdk::Color("#00ffff"));
717 drawing_frame->modify_base(Gtk::STATE_SELECTED,Gdk::Color("#ff00ff"));
718 drawing_frame->modify_bg(Gtk::STATE_SELECTED,Gdk::Color("#00ff00"));
720 //drawing_frame->set_state(Gtk::STATE_NORMAL);
722 drawing_frame->show();
724 attach(*drawing_frame, 1, 3+RULER_FIX, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
726 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
729 // Create the vertical and horizontal rulers
730 vruler = manage(new class Gtk::VRuler());
731 hruler = manage(new class Gtk::HRuler());
732 vruler->set_metric(Gtk::PIXELS);
733 hruler->set_metric(Gtk::PIXELS);
736 attach(*vruler, 0, 1, 1, 2, Gtk::SHRINK|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
737 attach(*hruler, 1, 3+RULER_FIX, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
738 hruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_hruler_event));
739 vruler->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_vruler_event));
740 hruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
741 vruler->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
743 // Create the menu button
744 menubutton=manage(new class Gtk::Button());
745 Gtk::Arrow *arrow1 = manage(new class Gtk::Arrow(Gtk::ARROW_RIGHT, Gtk::SHADOW_OUT));
746 menubutton->add(*arrow1);
747 menubutton->show_all();
748 menubutton->signal_pressed().connect(sigc::mem_fun(*this, &WorkArea::popup_menu));
749 attach(*menubutton, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
753 Gtk::VScrollbar *vscrollbar1 = manage(new class Gtk::VScrollbar(*get_scrolly_adjustment()));
754 Gtk::HScrollbar *hscrollbar1 = manage(new class Gtk::HScrollbar(*get_scrollx_adjustment()));
757 attach(*vscrollbar1, 3+RULER_FIX, 4+RULER_FIX, 1, 2, Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
758 attach(*hscrollbar1, 2+RULER_FIX, 3+RULER_FIX, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::FILL, 0, 0);
760 ZoomDial *zoomdial=manage(new class ZoomDial(iconsize));
761 zoomdial->signal_zoom_in().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_in));
762 zoomdial->signal_zoom_out().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_out));
763 zoomdial->signal_zoom_fit().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_fit));
764 zoomdial->signal_zoom_norm().connect(sigc::mem_fun(*this, &studio::WorkArea::zoom_norm));
766 attach(*zoomdial, 0, 2+RULER_FIX, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
768 drawing_area->add_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
769 add_events(Gdk::KEY_PRESS_MASK);
770 drawing_area->add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
771 drawing_area->add_events(Gdk::BUTTON1_MOTION_MASK | Gdk::BUTTON2_MOTION_MASK |Gdk::POINTER_MOTION_MASK);
773 // ----------------- Attach signals
775 drawing_area->signal_expose_event().connect(sigc::mem_fun(*this, &WorkArea::refresh));
776 drawing_area->signal_event().connect(sigc::mem_fun(*this, &WorkArea::on_drawing_area_event));
777 drawing_area->signal_size_allocate().connect(sigc::hide(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info)));
781 canvas_interface->signal_rend_desc_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
782 // When either of the scrolling adjustments change, then redraw.
783 get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
784 get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::queue_scroll));
785 get_scrollx_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
786 get_scrolly_adjustment()->signal_value_changed().connect(sigc::mem_fun(*this, &WorkArea::refresh_dimension_info));
788 get_canvas()->signal_meta_data_changed("grid_size").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
789 get_canvas()->signal_meta_data_changed("grid_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
790 get_canvas()->signal_meta_data_changed("grid_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
791 get_canvas()->signal_meta_data_changed("guide_show").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
792 get_canvas()->signal_meta_data_changed("guide_x").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
793 get_canvas()->signal_meta_data_changed("guide_y").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
794 get_canvas()->signal_meta_data_changed("onion_skin").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
795 get_canvas()->signal_meta_data_changed("guide_snap").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
796 get_canvas()->signal_meta_data_changed("sketch").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
797 get_canvas()->signal_meta_data_changed("solid_lines").connect(sigc::mem_fun(*this,&WorkArea::load_meta_data));
800 meta_data_lock=false;
801 set_focus_point(Point(0,0));
807 String data(canvas->get_meta_data("sketch"));
810 if(!load_sketch(data))
811 load_sketch(dirname(canvas->get_file_name())+ETL_DIRECTORY_SEPARATOR+basename(data));
815 hruler->property_max_size()=double(10.0);
816 vruler->property_max_size()=double(10.0);
818 drawing_area->set_flags(drawing_area->get_flags()|Gtk::CAN_FOCUS);
821 WorkArea::~WorkArea()
825 // don't leave the render function queued if we are about to vanish;
826 // that causes crashes
827 if(render_idle_func_id)
828 render_idle_func_id=0;
831 #ifdef SINGLE_THREADED
833 WorkArea::get_updating()const
835 return App::single_threaded && async_renderer && async_renderer->updating;
839 #ifdef SINGLE_THREADED
841 WorkArea::stop_updating(bool cancel)
843 async_renderer->stop();
844 if (cancel) canceled_=true;
849 WorkArea::save_meta_data()
855 Vector s(get_grid_size());
856 canvas_interface->set_meta_data("grid_size",strprintf("%f %f",s[0],s[1]));
857 canvas_interface->set_meta_data("grid_snap",get_grid_snap()?"1":"0");
858 canvas_interface->set_meta_data("guide_snap",get_guide_snap()?"1":"0");
859 canvas_interface->set_meta_data("guide_show",get_show_guides()?"1":"0");
860 canvas_interface->set_meta_data("grid_show",show_grid?"1":"0");
861 canvas_interface->set_meta_data("onion_skin",onion_skin?"1":"0");
864 GuideList::const_iterator iter;
865 for(iter=get_guide_list_x().begin();iter!=get_guide_list_x().end();++iter)
869 data+=strprintf("%f",*iter);
872 canvas_interface->set_meta_data("guide_x",data);
875 for(iter=get_guide_list_y().begin();iter!=get_guide_list_y().end();++iter)
879 data+=strprintf("%f",*iter);
882 canvas_interface->set_meta_data("guide_y",data);
885 if(get_sketch_filename().size())
887 if(dirname(canvas->get_file_name())==dirname(get_sketch_filename()))
888 canvas_interface->set_meta_data("sketch",basename(get_sketch_filename()));
890 canvas_interface->set_meta_data("sketch",get_sketch_filename());
893 meta_data_lock=false;
897 WorkArea::load_meta_data()
905 data=canvas->get_meta_data("grid_size");
908 float gx(get_grid_size()[0]),gy(get_grid_size()[1]);
910 String::iterator iter(find(data.begin(),data.end(),' '));
911 String tmp(data.begin(),iter);
916 synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
921 tmp=String(iter+1,data.end());
926 synfig::error("WorkArea::load_meta_data(): Unable to parse data for \"grid_size\", which was \"%s\"",data.c_str());
928 set_grid_size(Vector(gx,gy));
931 data=canvas->get_meta_data("grid_show");
932 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
934 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
937 data=canvas->get_meta_data("solid_lines");
938 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
940 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
943 data=canvas->get_meta_data("guide_show");
944 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
946 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
949 data=canvas->get_meta_data("grid_snap");
950 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
952 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
953 set_grid_snap(false);
955 data=canvas->get_meta_data("guide_snap");
956 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
957 set_guide_snap(true);
958 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
959 set_guide_snap(false);
961 data=canvas->get_meta_data("onion_skin");
962 if(data.size() && (data=="1" || data[0]=='t' || data[0]=='T'))
963 set_onion_skin(true);
964 if(data.size() && (data=="0" || data[0]=='f' || data[0]=='F'))
965 set_onion_skin(false);
967 data=canvas->get_meta_data("guide_x");
968 get_guide_list_x().clear();
971 String::iterator iter(find(data.begin(),data.end(),' '));
972 String guide(data.begin(),iter);
975 get_guide_list_x().push_back(stratof(guide));
980 data=String(iter+1,data.end());
982 //sort(get_guide_list_x());
984 data=canvas->get_meta_data("guide_y");
985 get_guide_list_y().clear();
988 String::iterator iter(find(data.begin(),data.end(),' '));
989 String guide(data.begin(),iter);
992 get_guide_list_y().push_back(stratof(guide));
997 data=String(iter+1,data.end());
999 //sort(get_guide_list_y());
1001 meta_data_lock=false;
1006 WorkArea::set_onion_skin(bool x)
1012 queue_render_preview();
1013 signal_onion_skin_changed()();
1017 WorkArea::get_onion_skin()const
1023 WorkArea::enable_grid()
1031 WorkArea::disable_grid()
1039 WorkArea::set_show_guides(bool x)
1047 WorkArea::toggle_grid()
1049 show_grid=!show_grid;
1055 WorkArea::set_low_resolution_flag(bool x)
1057 if(x!=low_resolution)
1060 queue_render_preview();
1065 WorkArea::toggle_low_resolution_flag()
1067 set_low_resolution_flag(!get_low_resolution_flag());
1071 WorkArea::popup_menu()
1073 signal_popup_menu()();
1077 WorkArea::set_grid_size(const synfig::Vector &s)
1079 Duckmatic::set_grid_size(s);
1085 WorkArea::set_focus_point(const synfig::Point &point)
1087 // These next three lines try to ensure that we place the
1088 // focus on a pixel boundary
1089 /*Point adjusted(point[0]/abs(get_pw()),point[1]/abs(get_ph()));
1090 adjusted[0]=(abs(adjusted[0]-floor(adjusted[0]))<0.5)?floor(adjusted[0])*abs(get_pw()):ceil(adjusted[0])*abs(get_ph());
1091 adjusted[1]=(abs(adjusted[1]-floor(adjusted[1]))<0.5)?floor(adjusted[1])*abs(get_ph()):ceil(adjusted[1])*abs(get_ph());
1093 const synfig::Point& adjusted(point);
1095 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1096 Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1097 Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1099 get_scrollx_adjustment()->set_value(adjusted[0]*x_factor);
1100 get_scrolly_adjustment()->set_value(adjusted[1]*y_factor);
1104 WorkArea::get_focus_point()const
1106 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1107 Real x_factor=(rend_desc.get_br()[0]-rend_desc.get_tl()[0]>0)?-1:1;
1108 Real y_factor=(rend_desc.get_br()[1]-rend_desc.get_tl()[1]>0)?-1:1;
1110 return synfig::Point(get_scrollx_adjustment()->get_value()*x_factor, get_scrolly_adjustment()->get_value()*y_factor);
1114 WorkArea::set_wh(int W, int H,int CHAN)
1116 // If our size is already set, don't set it again
1117 if(W==w && H==h && CHAN==bpp)
1121 if(W<=0 || H<=0 || CHAN<=0)
1128 // Set all of the parameters
1133 refresh_dimension_info();
1141 WorkArea::on_key_press_event(GdkEventKey* event)
1143 if(get_selected_ducks().empty())
1146 Real multiplier(1.0);
1148 if(Gdk::ModifierType(event->state)&GDK_SHIFT_MASK)
1152 switch(event->keyval)
1155 nudge=Vector(-pw,0);
1161 nudge=Vector(0,-ph);
1171 synfigapp::Action::PassiveGrouper grouper(instance.get(),_("Nudge"));
1173 // Grid snap does not apply to nudging
1174 bool grid_snap_holder(get_grid_snap());
1175 bool guide_snap_holder(get_guide_snap());
1176 set_grid_snap(false);
1179 start_duck_drag(get_selected_duck()->get_trans_point());
1180 translate_selected_ducks(get_selected_duck()->get_trans_point()+nudge*multiplier);
1185 canvas_view->duck_refresh_flag=true;
1186 canvas_view->queue_rebuild_ducks();
1189 set_grid_snap(grid_snap_holder);
1190 set_guide_snap(guide_snap_holder);
1196 WorkArea::on_drawing_area_event(GdkEvent *event)
1198 synfig::Point mouse_pos;
1199 float bezier_click_pos;
1200 const float radius((abs(pw)+abs(ph))*4);
1201 int button_pressed(0);
1203 bool is_mouse(false);
1204 Gdk::ModifierType modifier(Gdk::ModifierType(0));
1206 // Handle input stuff
1208 event->any.type==GDK_MOTION_NOTIFY ||
1209 event->any.type==GDK_BUTTON_PRESS ||
1210 event->any.type==GDK_2BUTTON_PRESS ||
1211 event->any.type==GDK_3BUTTON_PRESS ||
1212 event->any.type==GDK_BUTTON_RELEASE
1216 if(event->any.type==GDK_MOTION_NOTIFY)
1218 device=event->motion.device;
1219 modifier=Gdk::ModifierType(event->motion.state);
1223 device=event->button.device;
1224 modifier=Gdk::ModifierType(event->button.state);
1225 drawing_area->grab_focus();
1228 // Make sure we recognize the device
1229 if(curr_input_device)
1231 if(curr_input_device!=device)
1234 curr_input_device=device;
1235 signal_input_device_changed()(curr_input_device);
1240 curr_input_device=device;
1241 signal_input_device_changed()(curr_input_device);
1244 assert(curr_input_device);
1246 // Calculate the position of the
1247 // input device in canvas coordinates
1249 if(!event->button.axes)
1251 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->button.x,event->button.y)));
1252 button_pressed=event->button.button;
1255 if(isnan(event->button.x) || isnan(event->button.y))
1260 double x(event->button.axes[0]);
1261 double y(event->button.axes[1]);
1262 if(isnan(x) || isnan(y))
1265 pressure=event->button.axes[2];
1266 //synfig::info("pressure=%f",pressure);
1268 pressure/=1.0f-0.04f;
1271 assert(!isnan(pressure));
1273 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)));
1275 button_pressed=event->button.button;
1277 if(button_pressed==1 && pressure<0 && (event->any.type!=GDK_BUTTON_RELEASE && event->any.type!=GDK_BUTTON_PRESS))
1282 //if(event->any.type==GDK_BUTTON_PRESS && button_pressed)
1283 // synfig::info("Button pressed on input device = %d",event->button.button);
1285 //if(event->button.axes[2]>0.1)
1286 // button_pressed=1;
1288 // button_pressed=0;
1291 // GDK mouse scrolling events
1292 else if(event->any.type==GDK_SCROLL)
1294 // GDK information needed to properly interpret mouse
1295 // scrolling events are: scroll.state, scroll.x/scroll.y, and
1296 // scroll.direction. The value of scroll.direction will be
1299 modifier=Gdk::ModifierType(event->scroll.state);
1300 mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->scroll.x,event->scroll.y)));
1303 // Handle the renderables
1305 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
1306 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
1308 if((*iter)->get_enabled())
1309 if((*iter)->event_vfunc(event))
1311 // Event handled. Return true.
1317 // Event hasn't been handled, pass it down
1320 case GDK_BUTTON_PRESS:
1322 switch(button_pressed)
1324 case 1: // Attempt to click on a duck
1326 etl::handle<Duck> duck;
1329 if(allow_duck_clicks)
1331 duck=find_duck(mouse_pos,radius);
1335 // make a note of whether the duck we click on was selected or not
1336 if(duck_is_selected(duck))
1341 // if CTRL isn't pressed, clicking an unselected duck will unselect all other ducks
1342 if(!(modifier&GDK_CONTROL_MASK))
1343 clear_selected_ducks();
1349 // clear_selected_ducks();
1353 selected_bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1356 if (!duck->get_editable())
1359 //get_selected_duck()->signal_user_click(0)();
1360 //if(clicked_duck)clicked_duck->signal_user_click(0)();
1362 // if the user is holding shift while clicking on a tangent duck, consider splitting the tangent
1363 if (event->motion.state&GDK_SHIFT_MASK && duck->get_type() == Duck::TYPE_TANGENT)
1365 synfigapp::ValueDesc value_desc = duck->get_value_desc();
1367 // we have the tangent, but need the vertex - that's the parent
1368 if (value_desc.parent_is_value_node()) {
1369 ValueNode_Composite::Handle parent_value_node = value_desc.get_parent_value_node();
1371 // if the tangent isn't split, then split it
1372 if (!((*(parent_value_node->get_link("split")))(get_time()).get(bool())))
1374 if (get_canvas_view()->canvas_interface()->
1375 change_value(synfigapp::ValueDesc(parent_value_node,
1376 parent_value_node->get_link_index_from_name("split")),
1379 // rebuild the ducks from scratch, so the tangents ducks aren't connected
1380 get_canvas_view()->rebuild_ducks();
1382 // reprocess the mouse click
1383 return on_drawing_area_event(event);
1389 // I don't know how to access the vertex from the tangent duck when originally drawing the bline in the bline tool
1391 // synfig::ValueNode::Handle vn = value_desc.get_value_node();
1392 synfig::info("parent isn't value node? shift-drag-tangent doesn't work in bline tool yet...");
1397 drag_point=mouse_pos;
1398 //drawing_area->queue_draw();
1399 start_duck_drag(mouse_pos);
1400 get_canvas_view()->reset_cancel_status();
1403 // I commented out this section because
1404 // it was causing issues when rotoscoping.
1405 // At the moment, we don't need it, so
1406 // this was the easiest way to fix the problem.
1412 selected_bezier->signal_user_click(0)(bezier_click_pos);
1417 //clear_selected_ducks();
1419 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_LEFT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1421 // Check for a guide click
1422 GuideList::iterator iter;
1424 iter=find_guide_x(mouse_pos,radius);
1425 if(iter==get_guide_list_x().end())
1427 curr_guide_is_x=false;
1428 iter=find_guide_y(mouse_pos,radius);
1431 curr_guide_is_x=true;
1432 if(iter!=get_guide_list_x().end() && iter!=get_guide_list_y().end())
1434 dragging=DRAG_GUIDE;
1440 // All else fails, try making a selection box
1442 curr_point=drag_point=mouse_pos;
1448 case 2: // Attempt to drag and move the window
1450 etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1451 etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1453 duck->signal_user_click(1)();
1456 bezier->signal_user_click(1)(bezier_click_pos);
1458 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_MIDDLE,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1461 dragging=DRAG_WINDOW;
1462 drag_point=mouse_pos;
1463 signal_user_click(1)(mouse_pos);
1467 case 3: // Attempt to either get info on a duck, or open the menu
1469 etl::handle<Duck> duck=find_duck(mouse_pos,radius);
1470 etl::handle<Bezier> bezier=find_bezier(mouse_pos,radius,&bezier_click_pos);
1472 Layer::Handle layer(get_canvas()->find_layer(mouse_pos));
1475 if(get_selected_ducks().size()<=1)
1476 duck->signal_user_click(2)();
1478 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1483 bezier->signal_user_click(2)(bezier_click_pos);
1488 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_RIGHT,mouse_pos))==Smach::RESULT_OK)
1493 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier));
1495 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,BUTTON_RIGHT,mouse_pos,pressure,modifier))==Smach::RESULT_OK)
1504 signal_user_click(3)(mouse_pos);
1507 signal_user_click(4)(mouse_pos);
1514 case GDK_MOTION_NOTIFY:
1515 curr_point=mouse_pos;
1517 if(event->motion.time-last_event_time<25)
1520 last_event_time=event->motion.time;
1522 signal_cursor_moved_();
1524 // Guide/Duck highlights on hover
1525 if(dragging==DRAG_NONE)
1527 GuideList::iterator iter;
1529 iter=find_guide_x(mouse_pos,radius);
1530 if(iter==get_guide_list_x().end())
1531 iter=find_guide_y(mouse_pos,radius);
1533 if(iter!=curr_guide)
1536 drawing_area->queue_draw();
1539 etl::handle<Duck> duck;
1540 duck=find_duck(mouse_pos,radius);
1541 if(duck!=hover_duck)
1544 drawing_area->queue_draw();
1549 if(dragging==DRAG_DUCK)
1551 if(canvas_view->get_cancel_status())
1554 canvas_view->queue_rebuild_ducks();
1558 Point point((mouse_pos-selected_duck->get_origin())/selected_duck->get_scalar());
1561 point[0]=floor(point[0]/grid_size[0]+0.5)*grid_size[0];
1562 point[1]=floor(point[1]/grid_size[1]+0.5)*grid_size[1];
1564 selected_duck->set_point(point);
1567 //Point p(mouse_pos);
1569 set_axis_lock(event->motion.state&GDK_SHIFT_MASK);
1571 translate_selected_ducks(mouse_pos);
1573 drawing_area->queue_draw();
1576 if(dragging==DRAG_BOX)
1578 curr_point=mouse_pos;
1579 drawing_area->queue_draw();
1582 if(dragging==DRAG_GUIDE)
1585 *curr_guide=mouse_pos[0];
1587 *curr_guide=mouse_pos[1];
1588 drawing_area->queue_draw();
1591 if(dragging!=DRAG_WINDOW)
1592 { // Update those triangle things on the rulers
1593 const synfig::Point point(mouse_pos);
1594 hruler->property_position()=Distance(point[0],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1595 vruler->property_position()=Distance(point[1],Distance::SYSTEM_UNITS).get(App::distance_system,get_canvas()->rend_desc());
1598 if(dragging == DRAG_WINDOW)
1599 set_focus_point(get_focus_point() + mouse_pos-drag_point);
1600 else if (event->motion.state & GDK_BUTTON1_MASK &&
1601 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_LEFT,
1602 mouse_pos,pressure,modifier)) == Smach::RESULT_ACCEPT)
1604 else if (event->motion.state & GDK_BUTTON2_MASK &&
1605 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_MIDDLE,
1606 mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1608 else if (event->motion.state & GDK_BUTTON3_MASK &&
1609 canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, BUTTON_RIGHT,
1610 mouse_pos, pressure, modifier)) == Smach::RESULT_ACCEPT)
1612 else if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_MOTION, BUTTON_NONE,
1613 mouse_pos, pressure,modifier)) == Smach::RESULT_ACCEPT)
1618 case GDK_BUTTON_RELEASE:
1622 if(dragging==DRAG_GUIDE)
1629 if(dragging==DRAG_DUCK)
1631 synfigapp::Action::PassiveGrouper grouper(instance.get(),_("Move"));
1633 //translate_selected_ducks(mouse_pos);
1634 set_axis_lock(false);
1637 get_canvas_view()->duck_refresh_flag=false;
1638 get_canvas_view()->duck_refresh_needed=false;
1639 const bool drag_did_anything(end_duck_drag());
1640 get_canvas_view()->duck_refresh_flag=true;
1641 if(!drag_did_anything)
1643 // if we originally clicked on a selected duck ...
1646 // ... and CTRL is pressed, then just toggle the clicked duck
1647 // otherwise make the clicked duck the only selected duck
1648 if(modifier&GDK_CONTROL_MASK)
1649 unselect_duck(clicked_duck);
1652 clear_selected_ducks();
1653 select_duck(clicked_duck);
1655 clicked_duck->signal_user_click(0)();
1660 if(canvas_view->duck_refresh_needed)
1661 canvas_view->queue_rebuild_ducks();
1666 canvas_view->duck_refresh_flag=true;
1667 canvas_view->queue_rebuild_ducks();
1676 if(dragging==DRAG_BOX)
1679 if((drag_point-mouse_pos).mag()>radius/2.0f)
1681 if(canvas_view->get_smach().process_event(EventBox(drag_point,mouse_pos,MouseButton(event->button.button),modifier))==Smach::RESULT_ACCEPT)
1684 // when dragging a box around some ducks:
1685 // SHIFT selects; CTRL toggles; SHIFT+CTRL unselects; <none> clears all then selects
1686 if(modifier&GDK_SHIFT_MASK)
1687 select_ducks_in_box(drag_point,mouse_pos);
1689 if(modifier&GDK_CONTROL_MASK)
1690 toggle_select_ducks_in_box(drag_point,mouse_pos);
1691 else if(!(modifier&GDK_SHIFT_MASK))
1693 clear_selected_ducks();
1694 select_ducks_in_box(drag_point,mouse_pos);
1700 if(allow_layer_clicks)
1702 Layer::Handle layer(get_canvas()->find_layer(drag_point));
1705 if(canvas_view->get_smach().process_event(EventLayerClick(layer,BUTTON_LEFT,mouse_pos,modifier))==Smach::RESULT_OK)
1706 signal_layer_selected_(layer);
1712 signal_user_click(0)(mouse_pos);
1719 if(canvas_view->get_smach().process_event(EventMouse(EVENT_WORKAREA_MOUSE_BUTTON_UP,MouseButton(event->button.button),mouse_pos,pressure,modifier))==Smach::RESULT_ACCEPT)
1727 // Handle a mouse scrolling event like Xara Xtreme and
1730 // Scroll up/down: scroll up/down
1731 // Shift + scroll up/down: scroll left/right
1732 // Control + scroll up/down: zoom in/out
1734 if(modifier&GDK_CONTROL_MASK)
1737 // The zoom is performed while preserving the pointer
1738 // position as a fixed point (similarly to Xara Xtreme and
1741 // The strategy used below is to scroll to the updated
1742 // position, then zoom. This is easy to implement within
1743 // the present architecture, but has the disadvantage of
1744 // triggering multiple visible refreshes. Note: 1.25 is
1745 // the hard wired ratio in zoom_in()/zoom_out(). The
1746 // variable "drift" compensates additional inaccuracies in
1747 // the zoom. There is also an additional minus sign for
1748 // the inverted y coordinates.
1750 // FIXME: One might want to figure out where in the code
1751 // this empirical drift is been introduced.
1753 const synfig::Point scroll_point(get_scrollx_adjustment()->get_value(),get_scrolly_adjustment()->get_value());
1754 const double drift = 0.052;
1756 switch(event->scroll.direction)
1759 case GDK_SCROLL_RIGHT:
1760 get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1.25-(1+drift)));
1761 get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1.25-(1+drift)));
1764 case GDK_SCROLL_DOWN:
1765 case GDK_SCROLL_LEFT:
1766 get_scrollx_adjustment()->set_value(scroll_point[0]+(mouse_pos[0]-scroll_point[0])*(1/1.25-(1+drift)));
1767 get_scrolly_adjustment()->set_value(scroll_point[1]-(mouse_pos[1]+scroll_point[1])*(1/1.25-(1+drift)));
1774 else if(modifier&GDK_SHIFT_MASK)
1776 // Scroll in either direction by 20 pixels. Ideally, the
1777 // amount of pixels per scrolling event should be
1778 // configurable. Xara Xtreme currently uses an (hard
1779 // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1781 const int scroll_pixel = 20;
1783 switch(event->scroll.direction)
1786 get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1788 case GDK_SCROLL_DOWN:
1789 get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1791 case GDK_SCROLL_LEFT:
1792 get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1794 case GDK_SCROLL_RIGHT:
1795 get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1803 // Scroll in either direction by 20 pixels. Ideally, the
1804 // amount of pixels per scrolling event should be
1805 // configurable. Xara Xtreme currently uses an (hard
1806 // wired) amount 20 pixel, Inkscape defaults to 40 pixels.
1808 const int scroll_pixel = 20;
1810 switch(event->scroll.direction)
1813 get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()+scroll_pixel*ph);
1815 case GDK_SCROLL_DOWN:
1816 get_scrolly_adjustment()->set_value(get_scrolly_adjustment()->get_value()-scroll_pixel*ph);
1818 case GDK_SCROLL_LEFT:
1819 get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()-scroll_pixel*pw);
1821 case GDK_SCROLL_RIGHT:
1822 get_scrollx_adjustment()->set_value(get_scrollx_adjustment()->get_value()+scroll_pixel*pw);
1837 WorkArea::on_hruler_event(GdkEvent */*event*/)
1842 case GDK_BUTTON_PRESS:
1843 if(dragging==DRAG_NONE)
1845 dragging=DRAG_GUIDE;
1846 curr_guide=get_guide_list_y().insert(get_guide_list_y().begin());
1847 curr_guide_is_x=false;
1852 case GDK_MOTION_NOTIFY:
1854 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1857 if(event->button.axes)
1859 x=(event->button.axes[0]);
1860 y=(event->button.axes[1]);
1868 if(isnan(y) || isnan(x))
1871 *curr_guide=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)))[1];
1878 case GDK_BUTTON_RELEASE:
1879 if(dragging==DRAG_GUIDE && curr_guide_is_x==false)
1882 get_guide_list_y().erase(curr_guide);
1894 WorkArea::on_vruler_event(GdkEvent */*event*/)
1899 case GDK_BUTTON_PRESS:
1900 if(dragging==DRAG_NONE)
1902 dragging=DRAG_GUIDE;
1903 curr_guide=get_guide_list_x().insert(get_guide_list_x().begin());
1904 curr_guide_is_x=true;
1908 case GDK_BUTTON_RELEASE:
1909 if(dragging==DRAG_GUIDE && curr_guide_is_x==true)
1912 get_guide_list_x().erase(curr_guide);
1925 WorkArea::refresh_dimension_info()
1927 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1929 canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1930 canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1935 scrollx_adjustment.set_page_increment(abs(get_grid_size()[0]));
1936 scrollx_adjustment.set_step_increment(abs(pw));
1937 scrollx_adjustment.set_lower(-abs(canvaswidth));
1938 scrollx_adjustment.set_upper(abs(canvaswidth));
1939 scrolly_adjustment.set_lower(-abs(canvasheight));
1940 scrolly_adjustment.set_upper(abs(canvasheight));
1941 scrolly_adjustment.set_step_increment(abs(ph));
1942 scrolly_adjustment.set_page_increment(abs(get_grid_size()[1]));
1946 if(drawing_area->get_width()<=0 || drawing_area->get_height()<=0 || w==0 || h==0)
1949 const synfig::Point focus_point(get_focus_point());
1950 const synfig::Real x(focus_point[0]/pw+drawing_area->get_width()/2-w/2);
1951 const synfig::Real y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
1953 window_tl[0]=rend_desc.get_tl()[0]-pw*x;
1954 window_br[0]=rend_desc.get_br()[0]+pw*(drawing_area->get_width()-x-w);
1956 window_tl[1]=rend_desc.get_tl()[1]-ph*y;
1957 window_br[1]=rend_desc.get_br()[1]+ph*(drawing_area->get_height()-y-h);
1959 hruler->property_lower()=Distance(window_tl[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1960 hruler->property_upper()=Distance(window_br[0],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1961 vruler->property_lower()=Distance(window_tl[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1962 vruler->property_upper()=Distance(window_br[1],Distance::SYSTEM_UNITS).get(App::distance_system,rend_desc);
1964 view_window_changed();
1969 WorkArea::screen_to_comp_coords(synfig::Point pos)const
1971 synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1972 //synfig::Vector::value_type canvaswidth=rend_desc.get_br()[0]-rend_desc.get_tl()[0];
1973 //synfig::Vector::value_type canvasheight=rend_desc.get_br()[1]-rend_desc.get_tl()[1];
1974 //synfig::Vector::value_type pw=canvaswidth/w;
1975 //synfig::Vector::value_type ph=canvasheight/h;
1976 Vector focus_point=get_focus_point();
1977 synfig::Vector::value_type x=focus_point[0]/pw+drawing_area->get_width()/2-w/2;
1978 synfig::Vector::value_type y=focus_point[1]/ph+drawing_area->get_height()/2-h/2;
1980 return rend_desc.get_tl()-synfig::Point(pw*x,ph*y)+synfig::Point(pw*pos[0],ph*pos[1]);
1984 WorkArea::comp_to_screen_coords(synfig::Point /*pos*/)const
1986 synfig::warning("WorkArea::comp_to_screen_coords: Not yet implemented");
1987 return synfig::Point();
1991 WorkArea::next_unrendered_tile(int refreshes)const
1993 //assert(!tile_book.empty());
1994 if(tile_book.empty())
1997 //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
1999 const synfig::Vector focus_point(get_focus_point());
2001 // Calculate the window coordinates of the top-left
2002 // corner of the canvas.
2003 const synfig::Vector::value_type
2004 x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2005 y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2007 int div = low_res_pixel_size;
2008 const int width_in_tiles(w/tile_w+((low_resolution?((w/div)%(tile_w/div)):(w%tile_w))?1:0));
2009 const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
2015 u2(int((-x+drawing_area->get_width())/tile_w+1)),
2016 v2(int((-y+drawing_area->get_height())/tile_h+1));
2018 if(u2>width_in_tiles)u2=width_in_tiles;
2019 if(v2>height_in_tiles)v2=height_in_tiles;
2023 int last_good_tile(-1);
2028 int index(v*width_in_tiles+u);
2029 if(tile_book[index].second<refreshes)
2031 last_good_tile=index;
2036 return last_good_tile;
2040 template <typename F, typename T=WorkAreaRenderer, typename R=typename F::result_type>
2041 class handle2ptr_t : public std::unary_function<typename etl::handle<T>,R>
2046 handle2ptr_t(const F &func):func(func) { };
2048 R operator()(typename etl::handle<T> x) { return func(*x); }
2051 template <typename F>
2055 return handle2ptr_t<F>(func);
2058 renderer_set_.begin(),
2059 renderer_set_.end(),
2064 &WorkAreaRenderer::render_vfunc
2066 Gdk::Rectangle(event->area)
2068 drawing_area->get_window()
2075 WorkArea::refresh(GdkEventExpose*event)
2077 assert(get_canvas());
2079 drawing_area->get_window()->clear();
2081 //const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2083 const synfig::Vector focus_point(get_focus_point());
2085 // Update the old focus point
2086 last_focus_point=focus_point;
2088 // Draw out the renderables
2090 std::set<etl::handle<WorkAreaRenderer> >::iterator iter;
2091 for(iter=renderer_set_.begin();iter!=renderer_set_.end();++iter)
2093 if((*iter)->get_enabled())
2094 (*iter)->render_vfunc(
2095 drawing_area->get_window(),
2096 Gdk::Rectangle(&event->area)
2101 // Calculate the window coordinates of the top-left
2102 // corner of the canvas.
2103 //const synfig::Vector::value_type
2104 // x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2105 // y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2107 //const synfig::Vector::value_type window_startx(window_tl[0]);
2108 //const synfig::Vector::value_type window_endx(window_br[0]);
2109 //const synfig::Vector::value_type window_starty(window_tl[1]);
2110 //const synfig::Vector::value_type window_endy(window_br[1]);
2112 Glib::RefPtr<Gdk::GC> gc=Gdk::GC::create(drawing_area->get_window());
2114 // If we are in animate mode, draw a red border around the screen
2115 if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2117 // #define USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2118 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2119 // This method of drawing the red border doesn't work on any
2120 // Gtk theme which uses the crux-engine, hcengine, industrial,
2121 // mist, or ubuntulooks engine, such as the default ubuntu
2123 drawing_frame->modify_bg(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2125 // So let's do it in a more primitive fashion.
2126 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
2127 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
2128 drawing_area->get_window()->draw_rectangle(
2132 drawing_area->get_width()-1,drawing_area->get_height()-1); // w,h
2135 #ifdef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2137 drawing_frame->unset_bg(Gtk::STATE_NORMAL);
2144 WorkArea::done_rendering()
2150 pix_buf=Gdk::Pixbuf::create_from_data(
2151 buffer, // pointer to the data
2152 Gdk::COLORSPACE_RGB, // the colorspace
2154 8, // bits per sample
2157 w*bpp); // stride (pitch)
2164 WorkArea::set_quality(int x)
2169 queue_render_preview();
2173 WorkArea::set_low_res_pixel_size(int x)
2175 if(x==low_res_pixel_size)
2177 low_res_pixel_size=x;
2178 queue_render_preview();
2183 class WorkAreaProgress : public synfig::ProgressCallback
2185 WorkArea *work_area;
2186 ProgressCallback *cb;
2190 WorkAreaProgress(WorkArea *work_area,ProgressCallback *cb):
2191 work_area(work_area),cb(cb)
2197 task(const std::string &str)
2199 if(work_area->dirty)
2201 return cb->task(str);
2205 error(const std::string &err)
2207 if(work_area->dirty)
2209 return cb->error(err);
2213 amount_complete(int current, int total)
2215 if(work_area->dirty)
2217 return cb->amount_complete(current,total);
2223 studio::WorkArea::async_update_preview()
2225 #ifdef SINGLE_THREADED
2229 queue_render_preview();
2238 get_canvas_view()->reset_cancel_status();
2240 // This object will mark us as busy until
2242 //studio::App::Busy busy;
2244 //WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2245 //synfig::ProgressCallback *cb=&callback;
2247 if(!is_visible())return false;
2250 // If we are queued to render the scene at the next idle
2251 // go ahead and de-queue it.
2252 if(render_idle_func_id)
2254 g_source_remove(render_idle_func_id);
2256 render_idle_func_id=0;
2261 get_canvas_view()->reset_cancel_status();
2264 RendDesc desc=get_canvas()->rend_desc();
2266 int w=(int)(desc.get_w()*zoom);
2267 int h=(int)(desc.get_h()*zoom);
2269 // ensure that the size we draw is at least one pixel in each dimension
2270 int min_size = low_resolution ? low_res_pixel_size : 1;
2271 if (w < min_size) w = min_size;
2272 if (h < min_size) h = min_size;
2274 // Setup the description parameters
2275 desc.set_antialias(1);
2276 desc.set_time(cur_time);
2278 set_rend_desc(desc);
2280 // Create the render target
2281 handle<Target> target;
2283 // if we have lots of pixels to render and the tile renderer isn't disabled, use it
2285 div = low_resolution ? low_res_pixel_size : 1;
2286 if (w*h > 240*div*135*div && !getenv("SYNFIG_DISABLE_TILE_RENDER"))
2289 handle<WorkAreaTarget> trgt(new class WorkAreaTarget(this,w,h));
2291 trgt->set_rend_desc(&desc);
2292 trgt->set_onion_skin(get_onion_skin());
2297 // do a scanline render
2298 handle<WorkAreaTarget_Full> trgt(new class WorkAreaTarget_Full(this,w,h));
2300 trgt->set_rend_desc(&desc);
2301 trgt->set_onion_skin(get_onion_skin());
2305 // We can rest assured that our time has already
2306 // been set, so there is no need to have to
2307 // recalculate that over again.
2308 // UPDATE: This is kind of needless with
2309 // the way that time is handled now in SYNFIG.
2310 //target->set_avoid_time_sync(true);
2311 async_renderer=new AsyncRenderer(target);
2312 async_renderer->signal_finished().connect(
2313 sigc::mem_fun(this,&WorkArea::async_update_finished)
2317 async_renderer->start();
2319 synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2322 cb->task(_("Rendering..."));
2329 studio::WorkArea::async_update_finished()
2331 synfig::ProgressCallback *cb=get_canvas_view()->get_ui_interface().get();
2338 // If we completed successfully, then
2339 // we aren't dirty anymore
2340 if(async_renderer->has_success())
2344 cb->task(_("Idle"));
2349 cb->task(_("Render Failed"));
2351 //get_canvas_view()->reset_cancel_status();
2356 studio::WorkArea::sync_update_preview()
2358 // const Time &time(cur_time);
2361 get_canvas_view()->reset_cancel_status();
2366 // This object will mark us as busy until
2368 studio::App::Busy busy;
2370 WorkAreaProgress callback(this,get_canvas_view()->get_ui_interface().get());
2371 synfig::ProgressCallback *cb=&callback;
2373 // We don't want to render if we are already rendering
2380 if(!is_visible())return false;
2381 get_canvas()->set_time(get_time());
2382 get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2383 signal_rendering()();
2385 // If we are queued to render the scene at the next idle
2386 // go ahead and de-queue it.
2387 if(render_idle_func_id)
2389 g_source_remove(render_idle_func_id);
2391 render_idle_func_id=0;
2397 get_canvas_view()->reset_cancel_status();
2399 RendDesc desc=get_canvas()->rend_desc();
2400 //newdesc->set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
2402 int w=(int)(desc.get_w()*zoom);
2403 int h=(int)(desc.get_h()*zoom);
2405 // Setup the description parameters
2406 desc.set_antialias(1);
2407 desc.set_time(cur_time);
2410 set_rend_desc(desc);
2412 // Create the render target
2413 handle<WorkAreaTarget> target(new class WorkAreaTarget(this,w,h));
2415 target->set_rend_desc(&desc);
2417 // We can rest assured that our time has already
2418 // been set, so there is no need to have to
2419 // recalculate that over again.
2420 target->set_avoid_time_sync(true);
2423 cb->task(strprintf(_("Rendering canvas %s..."),get_canvas()->get_name().c_str()));
2425 bool ret = target->render(cb);
2427 if(!ret && !get_canvas_view()->get_cancel_status() && dirty)
2433 if(get_canvas_view()->get_cancel_status())
2439 cb->task(_("Idle"));
2441 cb->task(_("Render Failed"));
2442 cb->amount_complete(0,1);
2445 // Refresh the work area to make sure that
2446 // it is being displayed correctly
2447 drawing_area->queue_draw();
2449 // If we completed successfully, then
2450 // we aren't dirty anymore
2458 //get_canvas_view()->reset_cancel_status();
2464 studio::WorkArea::async_render_preview(synfig::Time time)
2467 //tile_book.clear();
2470 if(!is_visible())return;
2472 get_canvas()->set_time(get_time());
2473 get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
2474 signal_rendering()();
2476 async_update_preview();
2479 WorkArea::async_render_preview()
2481 return async_render_preview(get_canvas_view()->get_time());
2485 studio::WorkArea::sync_render_preview(synfig::Time time)
2488 //tile_book.clear();
2490 if(!is_visible())return false;
2491 return sync_update_preview();
2495 WorkArea::sync_render_preview()
2497 return sync_render_preview(get_canvas_view()->get_time());
2501 WorkArea::sync_render_preview_hook()
2503 sync_render_preview(get_canvas_view()->get_time());
2507 WorkArea::queue_scroll()
2509 // const synfig::RendDesc &rend_desc(get_canvas()->rend_desc());
2511 const synfig::Point focus_point(get_focus_point());
2514 new_x(focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2515 new_y(focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2518 old_x(last_focus_point[0]/pw+drawing_area->get_width()/2-w/2),
2519 old_y(last_focus_point[1]/ph+drawing_area->get_height()/2-h/2);
2521 // If the coordinates didn't change, we shouldn't queue a draw
2522 if(old_x==new_x && old_y==new_y)
2526 dx(round_to_int(old_x)-round_to_int(new_x)),
2527 dy(round_to_int(old_y)-round_to_int(new_y));
2529 drawing_area->get_window()->scroll(-dx,-dy);
2531 if (timecode_width && timecode_height)
2533 drawing_area->queue_draw_area(4, 4, 4+timecode_width, 4+timecode_height);
2534 drawing_area->queue_draw_area(4-dx, 4-dy, 4-dx+timecode_width, 4-dy+timecode_height);
2537 #ifndef USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2538 if(canvas_interface->get_mode()&synfigapp::MODE_ANIMATE)
2540 int maxx = drawing_area->get_width()-1;
2541 int maxy = drawing_area->get_height()-1;
2545 drawing_area->queue_draw_area( 0, 0, 1, maxy);
2546 drawing_area->queue_draw_area(maxx-dx, 0, maxx-dx, maxy);
2550 drawing_area->queue_draw_area( maxx, 0, maxx, maxy);
2551 drawing_area->queue_draw_area( -dx, 0, -dx, maxy);
2555 drawing_area->queue_draw_area(0, 0, maxx, 1);
2556 drawing_area->queue_draw_area(0, maxy-dy, maxx, maxy-dy);
2560 drawing_area->queue_draw_area(0, maxy, maxx, maxy);
2561 drawing_area->queue_draw_area(0, -dy, maxx, -dy);
2564 #endif // USE_FRAME_BACKGROUND_TO_SHOW_EDIT_MODE
2566 last_focus_point=focus_point;
2570 studio::WorkArea::zoom_in()
2572 set_zoom(zoom*1.25);
2576 studio::WorkArea::zoom_out()
2578 set_zoom(zoom/1.25);
2582 studio::WorkArea::zoom_fit()
2584 float new_zoom(min(drawing_area->get_width() * zoom / w, drawing_area->get_height() * zoom / h));
2585 if (zoom / new_zoom > 0.995 && new_zoom / zoom > 0.995)
2587 set_zoom(prev_zoom);
2588 return set_focus_point(previous_focus);
2590 previous_focus = get_focus_point();
2593 set_focus_point(Point(0,0));
2597 studio::WorkArea::zoom_norm()
2599 if (zoom == 1.0) return set_zoom(prev_zoom);
2605 studio::WorkArea::__render_preview(gpointer data)
2607 WorkArea *work_area(static_cast<WorkArea*>(data));
2609 // there's no point anyone trying to cancel the timer now - it's gone off already
2610 work_area->render_idle_func_id = 0;
2612 work_area->queued=false;
2613 work_area->async_render_preview(work_area->get_canvas_view()->get_time());
2619 studio::WorkArea::queue_render_preview()
2621 //synfig::info("queue_render_preview(): called for %s", get_canvas_view()->get_time().get_string().c_str());
2626 //synfig::info("queue_render_preview(): already queued, unqueuing");
2627 /* if(render_idle_func_id)
2628 g_source_remove(render_idle_func_id);
2629 render_idle_func_id=0;
2635 if(dirty_trap_enabled)
2637 dirty_trap_queued++;
2649 //synfig::info("queue_render_preview(): (re)queuing...");
2650 //render_idle_func_id=g_idle_add_full(G_PRIORITY_DEFAULT,__render_preview,this,NULL);
2651 render_idle_func_id=g_timeout_add_full(
2652 G_PRIORITY_DEFAULT, // priority -
2653 queue_time, // interval - the time between calls to the function, in milliseconds (1/1000ths of a second)
2654 __render_preview, // function - function to call
2655 this, // data - data to pass to function
2656 NULL); // notify - function to call when the idle is removed, or NULL
2659 /* else if(rendering)
2668 DirtyTrap::DirtyTrap(WorkArea *work_area):work_area(work_area)
2670 work_area->dirty_trap_enabled=true;
2672 work_area->dirty_trap_queued=0;
2675 DirtyTrap::~DirtyTrap()
2677 work_area->dirty_trap_enabled=false;
2678 if(work_area->dirty_trap_queued)
2679 work_area->queue_render_preview();
2683 studio::WorkArea::queue_draw_preview()
2685 drawing_area->queue_draw();
2689 studio::WorkArea::set_cursor(const Gdk::Cursor& x)
2691 drawing_area->get_window()->set_cursor(x);
2694 studio::WorkArea::set_cursor(Gdk::CursorType x)
2696 drawing_area->get_window()->set_cursor(Gdk::Cursor(x));
2699 #include "iconcontroller.h"
2702 studio::WorkArea::refresh_cursor()
2704 // set_cursor(IconController::get_tool_cursor(canvas_view->get_smach().get_state_name(),drawing_area->get_window()));
2708 studio::WorkArea::reset_cursor()
2710 drawing_area->get_window()->set_cursor(Gdk::Cursor(Gdk::TOP_LEFT_ARROW));
2711 // set_cursor(Gdk::TOP_LEFT_ARROW);
2715 studio::WorkArea::set_zoom(float z)
2717 z=max(1.0f/128.0f,min(128.0f,z));
2721 refresh_dimension_info();
2722 /*if(async_renderer)
2724 async_renderer->stop();
2728 async_update_preview();
2729 //queue_render_preview();
2733 WorkArea::set_selected_value_node(etl::loose_handle<synfig::ValueNode> x)
2735 if(x!=selected_value_node_)
2737 selected_value_node_=x;
2743 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x)
2745 renderer_set_.insert(x);
2746 x->set_work_area(this);
2751 WorkArea::insert_renderer(const etl::handle<WorkAreaRenderer> &x, int priority)
2753 x->set_priority(priority);
2758 WorkArea::erase_renderer(const etl::handle<WorkAreaRenderer> &x)
2760 x->set_work_area(0);
2761 renderer_set_.erase(x);
2766 WorkArea::resort_render_set()
2768 std::set<etl::handle<WorkAreaRenderer> > tmp(
2769 renderer_set_.begin(),
2772 renderer_set_.swap(tmp);