Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_dragbox.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_dragbox.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "renderer_dragbox.h"
33 #include "workarea.h"
34 #include <ETL/misc>
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace studio;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 Renderer_Dragbox::~Renderer_Dragbox()
54 {
55 }
56
57 const synfig::Point&
58 Renderer_Dragbox::get_drag_point()const
59 {
60         return get_work_area()->get_drag_point();
61 }
62
63 const synfig::Point&
64 Renderer_Dragbox::get_curr_point()const
65 {
66         return get_work_area()->get_cursor_pos();
67 }
68
69 bool
70 Renderer_Dragbox::get_enabled_vfunc()const
71 {
72         return get_work_area()->get_dragging_mode()==WorkArea::DRAG_BOX;
73 }
74
75
76 void
77 Renderer_Dragbox::render_vfunc(
78         const Glib::RefPtr<Gdk::Drawable>& drawable,
79         const Gdk::Rectangle& /*expose_area*/
80 )
81 {
82         assert(get_work_area());
83         if(!get_work_area())
84                 return;
85
86         // const synfig::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
87
88         const synfig::Vector focus_point(get_work_area()->get_focus_point());
89
90 //      std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
91
92         int drawable_w,drawable_h;
93         drawable->get_size(drawable_w,drawable_h);
94
95         // Calculate the window coordinates of the top-left
96         // corner of the canvas.
97         // const synfig::Vector::value_type
98         //      x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
99         //      y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
100
101         /*const synfig::Vector::value_type window_startx(window_tl[0]);
102         const synfig::Vector::value_type window_endx(window_br[0]);
103         const synfig::Vector::value_type window_starty(window_tl[1]);
104         const synfig::Vector::value_type window_endy(window_br[1]);
105         */
106         // const int
107         //      tile_w(get_work_area()->get_tile_w()),
108         //      tile_h(get_work_area()->get_tile_h());
109
110         // const int
111         //      w(get_w()),
112         //      h(get_h());
113
114         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
115
116         //const synfig::Vector grid_size(get_grid_size());
117
118         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
119         // const synfig::Vector::value_type window_endx(get_work_area()->get_window_br()[0]);
120         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
121         // const synfig::Vector::value_type window_endy(get_work_area()->get_window_br()[1]);
122         const float pw(get_pw()),ph(get_ph());
123
124         const synfig::Point& curr_point(get_curr_point());
125         const synfig::Point& drag_point(get_drag_point());
126
127         {
128                 gc->set_function(Gdk::COPY);
129                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
130                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
131                 //gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
132
133                 Point tl(std::min(drag_point[0],curr_point[0]),std::min(drag_point[1],curr_point[1]));
134                 Point br(std::max(drag_point[0],curr_point[0]),std::max(drag_point[1],curr_point[1]));
135
136                 tl[0]=(tl[0]-window_startx)/pw;
137                 tl[1]=(tl[1]-window_starty)/ph;
138                 br[0]=(br[0]-window_startx)/pw;
139                 br[1]=(br[1]-window_starty)/ph;
140                 if(tl[0]>br[0])
141                         swap(tl[0],br[0]);
142                 if(tl[1]>br[1])
143                         swap(tl[1],br[1]);
144
145                 drawable->draw_rectangle(gc,false,
146                         round_to_int(tl[0]),
147                         round_to_int(tl[1]),
148                         round_to_int(br[0]-tl[0]),
149                         round_to_int(br[1]-tl[1])
150                 );
151         }
152 }