my log
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_guides.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file template.cpp
3 **      \brief Template File
4 **
5 **      $Id: renderer_guides.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "renderer_guides.h"
32 #include "workarea.h"
33 #include <ETL/misc>
34
35 #endif
36
37 /* === U S I N G =========================================================== */
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace studio;
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 /* === P R O C E D U R E S ================================================= */
49
50 /* === M E T H O D S ======================================================= */
51
52 Renderer_Guides::Renderer_Guides():
53         dragging(false)
54 {
55         
56 }
57
58 Renderer_Guides::~Renderer_Guides()
59 {
60 }
61
62 bool
63 Renderer_Guides::get_enabled_vfunc()const
64 {
65         return get_work_area()->get_show_guides();
66 }
67
68 std::list<float>&
69 Renderer_Guides::get_guide_list_x()
70 {
71         return get_work_area()->get_guide_list_x();
72 }
73
74 std::list<float>&
75 Renderer_Guides::get_guide_list_y()
76 {
77         return get_work_area()->get_guide_list_y();
78 }
79
80 bool
81 Renderer_Guides::event_vfunc(GdkEvent* event)
82 {
83         synfig::Point mouse_pos;
84     float bezier_click_pos;
85         const float radius((abs(get_pw())+abs(get_ph()))*4);
86         int button_pressed(0);
87         float pressure(0);
88         bool is_mouse(false);
89         Gdk::ModifierType modifier(Gdk::ModifierType(0));
90         
91         // Handle input stuff
92         if(
93                 event->any.type==GDK_MOTION_NOTIFY ||
94                 event->any.type==GDK_BUTTON_PRESS ||
95                 event->any.type==GDK_2BUTTON_PRESS ||
96                 event->any.type==GDK_3BUTTON_PRESS ||
97                 event->any.type==GDK_BUTTON_RELEASE
98         )
99         {
100                 GdkDevice *device;
101                 if(event->any.type==GDK_MOTION_NOTIFY)
102                 {
103                         device=event->motion.device;
104                         modifier=Gdk::ModifierType(event->motion.state);
105                 }
106                 else
107                 {
108                         device=event->button.device;
109                         modifier=Gdk::ModifierType(event->button.state);
110                 }
111                         
112                 // Make sure we recognise the device
113                 /*if(curr_input_device)
114                 {
115                         if(curr_input_device!=device)
116                         {
117                                 assert(device);
118                                 curr_input_device=device;
119                                 signal_input_device_changed()(curr_input_device);
120                         }
121                 }               
122                 else*/ if(device)
123                 {
124                         //curr_input_device=device;
125                         //signal_input_device_changed()(curr_input_device);
126                 }                       
127
128                 //assert(curr_input_device);
129                 
130                 // Calculate the position of the
131                 // input device in canvas coordinates
132                 // and the buttons
133                 if(!event->button.axes)
134                 {
135                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(event->button.x,event->button.y)));
136                         button_pressed=event->button.button;
137                         pressure=1.0f;
138                         is_mouse=true;
139                         if(isnan(event->button.x) || isnan(event->button.y))
140                                 return false;
141                 }
142                 else
143                 {
144                         double x(event->button.axes[0]);
145                         double y(event->button.axes[1]);
146                         if(isnan(x) || isnan(y))
147                                 return false;
148
149                         pressure=event->button.axes[2];
150                         //synfig::info("pressure=%f",pressure);
151                         pressure-=0.04f;
152                         pressure/=1.0f-0.04f;
153                         
154                         
155                         assert(!isnan(pressure));
156                         
157                         mouse_pos=synfig::Point(screen_to_comp_coords(synfig::Point(x,y)));
158                         
159                         button_pressed=event->button.button;
160                         
161                         if(button_pressed==1 && pressure<0 && (event->any.type!=GDK_BUTTON_RELEASE && event->any.type!=GDK_BUTTON_PRESS))
162                                 button_pressed=0;
163                         if(pressure<0)
164                                 pressure=0;
165
166                         //if(event->any.type==GDK_BUTTON_PRESS && button_pressed)
167                         //      synfig::info("Button pressed on input device = %d",event->button.button);
168                         
169                         //if(event->button.axes[2]>0.1)
170                         //      button_pressed=1;
171                         //else
172                         //      button_pressed=0;                               
173                 }
174         }
175         switch(event->type)
176     {
177         case GDK_BUTTON_PRESS:
178                 break;
179         case GDK_MOTION_NOTIFY:
180                 break;
181         case GDK_BUTTON_RELEASE:
182                 break;
183         default:
184                 break;
185         }
186         
187         return false;
188 }
189
190 void
191 Renderer_Guides::render_vfunc(
192         const Glib::RefPtr<Gdk::Drawable>& drawable,
193         const Gdk::Rectangle& expose_area
194 )
195 {
196         assert(get_work_area());
197         if(!get_work_area())
198                 return;
199         
200         const synfig::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
201         
202         const synfig::Vector focus_point(get_work_area()->get_focus_point());
203
204         //std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
205         
206         int drawable_w,drawable_h;
207         drawable->get_size(drawable_w,drawable_h);
208         
209         // Calculate the window coordinates of the top-left
210         // corner of the canvas.
211         const synfig::Vector::value_type
212                 x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
213                 y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
214
215         /*const synfig::Vector::value_type window_startx(window_tl[0]);
216         const synfig::Vector::value_type window_endx(window_br[0]);
217         const synfig::Vector::value_type window_starty(window_tl[1]);
218         const synfig::Vector::value_type window_endy(window_br[1]);
219         */
220         const int
221                 tile_w(get_work_area()->get_tile_w()),
222                 tile_h(get_work_area()->get_tile_h());
223
224         const int
225                 w(get_w()),
226                 h(get_h());
227         
228         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
229         
230         //const synfig::Vector grid_size(get_grid_size());
231
232         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
233         const synfig::Vector::value_type window_endx(get_work_area()->get_window_br()[0]);
234         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
235         const synfig::Vector::value_type window_endy(get_work_area()->get_window_br()[1]);
236         const float pw(get_pw()),ph(get_ph());
237
238         // Draw out the guides
239         {
240                 gc->set_function(Gdk::COPY);
241                 gc->set_rgb_fg_color(Gdk::Color("#9f9fff"));
242                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
243                 
244                 Duckmatic::GuideList::const_iterator iter;
245                 
246                 // vertical
247                 for(iter=get_guide_list_x().begin();iter!=get_guide_list_x().end();++iter)
248                 {
249                         const float x((*iter-window_startx)/pw);
250                         
251                         if(iter==get_work_area()->curr_guide)
252                                 gc->set_rgb_fg_color(Gdk::Color("#ff6f6f"));
253                         else
254                                 gc->set_rgb_fg_color(Gdk::Color("#6f6fff"));
255                                 
256                         drawable->draw_line(gc,
257                                 round_to_int(x),
258                                 0,
259                                 round_to_int(x),
260                                 drawable_h
261                         );
262                 }
263                 // horizontal
264                 for(iter=get_guide_list_y().begin();iter!=get_guide_list_y().end();++iter)
265                 {
266                         const float y((*iter-window_starty)/ph);
267
268                         if(iter==get_work_area()->curr_guide)
269                                 gc->set_rgb_fg_color(Gdk::Color("#ff6f6f"));
270                         else
271                                 gc->set_rgb_fg_color(Gdk::Color("#6f6fff"));
272
273                         drawable->draw_line(gc,
274                                 0,
275                                 round_to_int(y),
276                                 drawable_w,
277                                 round_to_int(y)
278                         );
279                 }
280         }
281 }