2 /*******************************************************************************/
3 /* Copyright (C) 2012 Jonathan Moore Liles */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
21 #include <FL/fl_draw.H>
22 #include <FL/Fl_Panzoomer.H>
24 Fl_Panzoomer::Fl_Panzoomer ( int X
, int Y
, int W
, int H
, const char *L
) :
25 Fl_Valuator( X
,Y
,W
,H
,L
)
30 _zoom_changed
= false;
32 _ymin
= _ymax
= _xmin
= _xmax
= _ysize
= _xsize
=
39 Fl_Panzoomer::x_value ( int pos
, int size
, int first
, int total
)
41 if ( pos
+ size
> first
+ total
)
42 total
= pos
+size
-first
;
44 if ( (int)_xmin
== first
&&
45 (int)_xmax
== total
&&
50 damage( FL_DAMAGE_USER1
);
61 Fl_Panzoomer::y_value ( int pos
, int size
, int first
, int total
)
63 if ( pos
+ size
> first
+ total
)
64 total
= pos
+size
-first
;
66 if ( (int)_ymin
== first
&&
67 (int)_ymax
== total
&&
72 damage( FL_DAMAGE_USER1
);
83 Fl_Panzoomer::x_value ( double v
)
91 else if ( _xpos
> _xmax
- _xsize
)
92 _xpos
= _xmax
- _xsize
;
94 damage( FL_DAMAGE_USER1
);
98 Fl_Panzoomer::y_value ( double v
)
107 else if ( _ypos
> _ymax
- _ysize
)
108 _ypos
= _ymax
- _ysize
;
110 damage( FL_DAMAGE_USER1
);
114 Fl_Panzoomer::zoom ( int v
)
120 if ( _zoom
> _zoom_max
)
123 if ( _zoom
< _zoom_min
)
128 _zoom_changed
= true;
130 _zoom_changed
= false;
135 Fl_Panzoomer::draw ( void )
137 draw(x(),y(),w(),h());
141 Fl_Panzoomer::draw ( int X
, int Y
, int W
, int H
)
143 fl_draw_box( box(), X
,Y
,W
,H
,color());
145 X
+= Fl::box_dx( box() );
146 Y
+= Fl::box_dy( box() );
147 W
-= Fl::box_dw( box() );
148 H
-= Fl::box_dh( box() );
150 fl_push_clip( X
,Y
,W
,H
);
152 draw_background( X
,Y
,W
,H
);
153 draw_cursor( X
,Y
,W
,H
);
161 Fl_Panzoomer::draw_background ( int X
, int Y
, int W
, int H
)
166 Fl_Panzoomer::draw_cursor ( int X
, int Y
, int W
, int H
)
170 cx
= X
; cy
= Y
; cw
= W
; ch
= H
;
172 cursor_bounds( cx
,cy
,cw
,ch
);
174 fl_rectf( cx
,cy
,cw
,ch
,
175 fl_color_add_alpha( FL_WHITE
, 40 ));
177 fl_rect( cx
,cy
,cw
,ch
,
178 fl_color_add_alpha( FL_WHITE
, 200 ));
182 Fl_Panzoomer::cursor_bounds ( int &cx
, int &cy
, int &cw
, int &ch
) const
195 if ( _xmin
== _xmax
)
199 hval
= (_xpos
-_xmin
)/(_xmax
-_xmin
);
201 if (hval
> 1.0) hval
= 1.0;
202 else if (hval
< 0.0) hval
= 0.0;
206 if ( _ymin
== _ymax
)
210 vval
= (_ypos
-_ymin
)/(_ymax
-_ymin
);
212 if (vval
> 1.0) vval
= 1.0;
213 else if (vval
< 0.0) vval
= 0.0;
216 cx
= X
+ (hval
) * W
+ .5;
217 cy
= _ymax
? Y
+ (vval
) * H
+ .5 : Y
;
218 cw
= W
* (_xsize
/_xmax
);
219 ch
= _ymax
? H
* (_ysize
/_ymax
) : H
;
221 cw
= cw
< minw
? minw
: cw
;
222 ch
= ch
< minh
? minh
: ch
;
226 Fl_Panzoomer::handle ( int m
)
228 return handle( m
, x(),y(),w(),h());
232 Fl_Panzoomer::handle ( int m
, int X
, int Y
, int W
, int H
)
238 X
+= Fl::box_dx( box() );
239 Y
+= Fl::box_dy( box() );
240 W
-= Fl::box_dw( box() );
241 H
-= Fl::box_dh( box() );
252 cx
= X
; cy
= Y
; cw
= W
; ch
= H
;
254 cursor_bounds( cx
,cy
,cw
,ch
);
256 if ( Fl::event_inside( cx
,cy
,cw
,ch
) )
258 xoffset
= Fl::event_x() - cx
;
259 yoffset
= Fl::event_y() - cy
;
267 if (// Fl::event_inside( cx,cy,cw,ch ) &&
268 Fl::event_button1() )
278 cx
= X
; cy
= Y
; cw
= W
; ch
= H
;
280 cursor_bounds( cx
,cy
,cw
,ch
);
284 x_value((((double)Fl::event_x() - X
- xoffset
) / W
) * _xmax
);
285 y_value( (((double)Fl::event_y() - Y
- yoffset
) / H
) * _ymax
);
287 if ( when() & FL_WHEN_CHANGED
)
291 damage( FL_DAMAGE_USER1
);
298 const int dy
= Fl::event_dy();
299 const int dx
= Fl::event_dx();
301 if ( dy
&& Fl::event_ctrl() )
305 damage( FL_DAMAGE_USER1
);
310 if ( Fl::event_alt() )
313 if ( Fl::event_shift())
316 y_value( _ypos
+ ( (double)dx
*5 / H
) * _ymax
);
319 x_value( _xpos
+ ( (double)dy
*5 / W
) * _xmax
);
321 if ( when() & FL_WHEN_CHANGED
)
324 damage( FL_DAMAGE_USER1
);
331 y_value( _ypos
+ ( (double)dy
*5 / H
) * _ymax
);
334 x_value( _xpos
+ ( (double)dx
*5 / W
) * _xmax
);
336 if ( when() & FL_WHEN_CHANGED
)
339 damage( FL_DAMAGE_USER1
);
353 if ( when() & FL_WHEN_RELEASE
)
361 if ( Fl::event_shift() || Fl::event_ctrl() || Fl::event_alt() )
368 int xls
= _xsize
/ 50;
369 int yls
= _ysize
/ 50;
371 switch ( Fl::event_key() )