From 9fdf8b4fd9d12d1cc88b283fe00079a2324f2055 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Thu, 1 Nov 2012 17:30:39 -0700 Subject: [PATCH] Fl_Panzoomer: Get rid of callback in favor of virtual method and inheritance. --- FL/Fl_Panzoomer.H | 38 ++++++++------- src/Fl_Panzoomer.cxx | 114 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 112 insertions(+), 40 deletions(-) diff --git a/FL/Fl_Panzoomer.H b/FL/Fl_Panzoomer.H index 3be376e..f1820bd 100644 --- a/FL/Fl_Panzoomer.H +++ b/FL/Fl_Panzoomer.H @@ -26,11 +26,6 @@ class FL_EXPORT Fl_Panzoomer : public Fl_Valuator { protected: - typedef void (draw_thumbnail_view_callback_t)( int X,int Y,int W,int H,void *v ); - - draw_thumbnail_view_callback_t *_draw_thumbnail_view_callback; - void *_draw_thumbnail_view_userdata; - int _zoom; bool _zoom_changed; int _zoom_min; @@ -43,19 +38,32 @@ protected: * between 0-1. */ double _ysize, _xsize; +protected: + + virtual void draw_cursor ( int X, int Y, int W, int H ); + /* override this method to draw a thumbnail background... */ + virtual void draw_background ( int X, int Y, int W, int H ); + virtual void draw ( int X, int Y, int W, int H ); + virtual int handle ( int m, int X, int Y, int W, int H ); + void cursor_bounds ( int &cx, int &cy, int &cw, int &ch ) const; + public: + virtual void draw ( void ); + virtual int handle ( int m ); + Fl_Panzoomer ( int X, int Y, int W, int H, const char *L=0 ); virtual ~Fl_Panzoomer ( ) { } - virtual void draw ( void ); - virtual int handle ( int m ); /* these behave like Fl_Scrollbar::value() */ /* first and total are the bounds, size is the size of the window, * and pos is the leftmost edge. */ int x_value ( int pos, int size, int first, int total ) { + if ( pos + size > first + total ) + total = pos+size-first; + _xmin = first; _xmax = total; _xpos = pos; @@ -68,11 +76,15 @@ public: int y_value ( int pos, int size, int first, int total ) { + if ( pos + size > first + total ) + total = pos+size-first; + _ymin = first; _ymax = total; _ypos = pos; _ysize = size; + redraw(); return pos; @@ -83,7 +95,6 @@ public: void x_value ( double v ); void y_value ( double v ); - void cursor_bounds ( int &cx, int &cy, int &cw, int &ch ) const; bool zoom_changed ( void ) const { return _zoom_changed; } double zoom ( void ) const { return _zoom; } @@ -91,15 +102,8 @@ public: void zoom_range ( int zmin, int zmax ) { _zoom_min = zmin; _zoom_max = zmax; } - void zoom_in ( void ) { int z = _zoom; zoom( _zoom + 1 ); if ( z != _zoom ) do_callback(); } - void zoom_out ( void ) { int z = _zoom; zoom( _zoom - 1 ); if ( z != _zoom ) do_callback(); } - - /* set this callback if you want to display a thumbnail view of the canvas being scrolled */ - void draw_thumbnail_view_callback( draw_thumbnail_view_callback_t *cb, void *v ) - { - _draw_thumbnail_view_callback = cb; - _draw_thumbnail_view_userdata = v; - } + void zoom_in ( void ) { int z = _zoom; zoom( _zoom - 1 ); if ( z != _zoom ) do_callback(); } + void zoom_out ( void ) { int z = _zoom; zoom( _zoom + 1 ); if ( z != _zoom ) do_callback(); } }; #endif diff --git a/src/Fl_Panzoomer.cxx b/src/Fl_Panzoomer.cxx index 40a62c6..808826e 100644 --- a/src/Fl_Panzoomer.cxx +++ b/src/Fl_Panzoomer.cxx @@ -88,25 +88,41 @@ Fl_Panzoomer::zoom ( int v ) void Fl_Panzoomer::draw ( void ) { - type( FL_HORIZONTAL ); + draw(x(),y(),w(),h()); +} + +void +Fl_Panzoomer::draw ( int X, int Y, int W, int H ) +{ + fl_draw_box( box(), X,Y,W,H,color()); - fl_draw_box( box(), x(),y(),w(),h(), color()); -// fl_rectf( x(),y(),w(),h(), color() ); + X += Fl::box_dx( box() ); + Y += Fl::box_dy( box() ); + W -= Fl::box_dw( box() ); + H -= Fl::box_dh( box() ); - fl_push_clip( x(), y(), w(), h()); + fl_push_clip( X,Y,W,H ); - int X,Y,W,H; - - X = x() + Fl::box_dx( box() ); - Y = y() + Fl::box_dy( box() ); - W = w() - Fl::box_dw( box() ); - H = h() - Fl::box_dh( box() ); + draw_background( X,Y,W,H ); + draw_cursor( X,Y,W,H ); - if ( _draw_thumbnail_view_callback ) - _draw_thumbnail_view_callback( X,Y,W,H, _draw_thumbnail_view_userdata ); + fl_pop_clip(); + + draw_label(); +} + +void +Fl_Panzoomer::draw_background ( int X, int Y, int W, int H ) +{ +} +void +Fl_Panzoomer::draw_cursor ( int X, int Y, int W, int H ) +{ int cx,cy,cw,ch; + cx = X; cy = Y; cw = W; ch = H; + cursor_bounds( cx,cy,cw,ch ); fl_rectf( cx,cy,cw,ch, @@ -114,10 +130,6 @@ Fl_Panzoomer::draw ( void ) fl_rect( cx,cy,cw,ch, fl_color_add_alpha( FL_WHITE, 80 )); - - fl_pop_clip(); - - draw_label(); } void @@ -125,10 +137,10 @@ Fl_Panzoomer::cursor_bounds ( int &cx, int &cy, int &cw, int &ch ) const { int X,Y,W,H; - X = x() + Fl::box_dx( box() ); - Y = y() + Fl::box_dy( box() ); - W = w() - Fl::box_dw( box() ); - H = h() - Fl::box_dh( box() ); + X = cx; + Y = cy; + W = cw; + H = ch; double hval; if ( _xmin == _xmax ) @@ -160,11 +172,22 @@ Fl_Panzoomer::cursor_bounds ( int &cx, int &cy, int &cw, int &ch ) const int Fl_Panzoomer::handle ( int m ) +{ + return handle( m, x(),y(),w(),h()); +} + +int +Fl_Panzoomer::handle ( int m, int X, int Y, int W, int H ) { static int xoffset; static int yoffset; static bool drag; + X += Fl::box_dx( box() ); + Y += Fl::box_dy( box() ); + W -= Fl::box_dw( box() ); + H -= Fl::box_dh( box() ); + switch ( m ) { case FL_ENTER: @@ -174,6 +197,8 @@ Fl_Panzoomer::handle ( int m ) { int cx,cy,cw,ch; + cx = X; cy = Y; cw = W; ch = H; + cursor_bounds( cx,cy,cw,ch ); xoffset = Fl::event_x() - cx; @@ -188,13 +213,15 @@ Fl_Panzoomer::handle ( int m ) case FL_DRAG: { int cx,cy,cw,ch; + + cx = X; cy = Y; cw = W; ch = H; cursor_bounds( cx,cy,cw,ch ); if ( drag ) { - x_value((((double)Fl::event_x() - x() - xoffset) / w()) * _xmax); - y_value( (((double)Fl::event_y() - y() - yoffset) / h()) * _ymax ); + x_value((((double)Fl::event_x() - X - xoffset) / W) * _xmax); + y_value( (((double)Fl::event_y() - Y - yoffset) / H) * _ymax ); if ( when() & FL_WHEN_CHANGED ) do_callback(); @@ -215,7 +242,7 @@ Fl_Panzoomer::handle ( int m ) } else if (!Fl::event_alt() && !Fl::event_shift()) { - y_value( _ypos + ( (double)d*5 / h() ) * _ymax ); + y_value( _ypos + ( (double)d*5 / H ) * _ymax ); if ( when() & FL_WHEN_CHANGED ) do_callback(); @@ -237,6 +264,47 @@ Fl_Panzoomer::handle ( int m ) return 1; } + case FL_KEYBOARD: + { + if ( Fl::event_shift() || Fl::event_ctrl() || Fl::event_alt() ) + return 0; + + double xv = _xpos; + double yv = _ypos; + + /* FIXME: hack */ + int xls = _xsize / 50; + int yls = _ysize / 50; + + switch ( Fl::event_key() ) + { + case FL_Up: + yv -= yls; + break; + case FL_Down: + yv += yls; + break; + case FL_Left: + xv -= xls; + break; + case FL_Right: + xv += xls; + break; + default: + return 0; + } + + x_value( xv ); + y_value( yv ); + + do_callback(); + + redraw(); + + return 1; + + break; + } } return 0;