Commit 07b0dae0 authored by Davis King's avatar Davis King

Made the scrollable_region and zoomable_region widgets have user settable styles.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402592
parent 972ffa75
......@@ -1940,6 +1940,7 @@ namespace dlib
{
scale = 1;
mouse_drag_screen = false;
style.reset(new scrollable_region_style_default());
hsb.set_scroll_handler(*this,&zoomable_region::on_h_scroll);
vsb.set_scroll_handler(*this,&zoomable_region::on_v_scroll);
......@@ -1962,10 +1963,14 @@ namespace dlib
{
auto_mutex M(m);
drawable::set_pos(x,y);
vsb.set_pos(rect.right()-2-vsb.width(),rect.top()+2);
hsb.set_pos(rect.left()+2,rect.bottom()-2-hsb.height());
const long border_size = style->get_border_size();
vsb.set_pos(rect.right()-border_size+1-vsb.width(),rect.top()+border_size);
hsb.set_pos(rect.left()+border_size,rect.bottom()-border_size+1-hsb.height());
display_rect_ = rectangle(rect.left()+2,rect.top()+2,rect.right()-2-vsb.width(),rect.bottom()-2-hsb.height());
display_rect_ = rectangle(rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size-vsb.width(),
rect.bottom()-border_size-hsb.height());
}
......@@ -2054,11 +2059,15 @@ namespace dlib
{
auto_mutex M(m);
rectangle old(rect);
const long border_size = style->get_border_size();
rect = resize_rect(rect,width,height);
vsb.set_pos(rect.right()-1-vsb.width(), rect.top()+2);
hsb.set_pos(rect.left()+2, rect.bottom()-1-hsb.height());
vsb.set_pos(rect.right()-border_size+1-vsb.width(), rect.top()+border_size);
hsb.set_pos(rect.left()+border_size, rect.bottom()-border_size+1-hsb.height());
display_rect_ = rectangle(rect.left()+2,rect.top()+2,rect.right()-2-vsb.width(),rect.bottom()-2-hsb.height());
display_rect_ = rectangle(rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size-vsb.width(),
rect.bottom()-border_size-hsb.height());
vsb.set_length(display_rect_.height());
hsb.set_length(display_rect_.width());
parent.invalidate_rectangle(rect+old);
......@@ -2318,7 +2327,7 @@ namespace dlib
const canvas& c
) const
{
draw_sunken_rectangle(c,rect);
style->draw_scrollable_region_border(c, rect, enabled);
}
// ----------------------------------------------------------------------------------------
......@@ -2411,7 +2420,6 @@ namespace dlib
drawable(w, MOUSE_WHEEL|events|MOUSE_CLICK|MOUSE_MOVE),
hsb(w,scroll_bar::HORIZONTAL),
vsb(w,scroll_bar::VERTICAL),
border_size(2),
hscroll_bar_inc(1),
vscroll_bar_inc(1),
h_wheel_scroll_bar_inc(1),
......@@ -2419,6 +2427,8 @@ namespace dlib
mouse_drag_enabled_(false),
user_is_dragging_mouse(false)
{
style.reset(new scrollable_region_style_default());
hsb.set_scroll_handler(*this,&scrollable_region::on_h_scroll);
vsb.set_scroll_handler(*this,&scrollable_region::on_v_scroll);
}
......@@ -2505,8 +2515,8 @@ namespace dlib
auto_mutex M(m);
rectangle old(rect);
rect = resize_rect(rect,width,height);
vsb.set_pos(rect.right()-border_size-vsb.width()+1, rect.top()+border_size);
hsb.set_pos(rect.left()+border_size, rect.bottom()-border_size-hsb.height()+1);
vsb.set_pos(rect.right()-style->get_border_size()-vsb.width()+1, rect.top()+style->get_border_size());
hsb.set_pos(rect.left()+style->get_border_size(), rect.bottom()-style->get_border_size()-hsb.height()+1);
// adjust the display_rect_
if (need_h_scroll() && need_v_scroll())
......@@ -2517,10 +2527,10 @@ namespace dlib
vsb.show();
hsb.show();
}
display_rect_ = rectangle( rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size-vsb.width(),
rect.bottom()-border_size-hsb.height());
display_rect_ = rectangle( rect.left()+style->get_border_size(),
rect.top()+style->get_border_size(),
rect.right()-style->get_border_size()-vsb.width(),
rect.bottom()-style->get_border_size()-hsb.height());
// figure out how many scroll bar positions there should be
unsigned long hdelta = total_rect_.width()-display_rect_.width();
......@@ -2542,10 +2552,10 @@ namespace dlib
hsb.show();
vsb.hide();
}
display_rect_ = rectangle( rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size,
rect.bottom()-border_size-hsb.height());
display_rect_ = rectangle( rect.left()+style->get_border_size(),
rect.top()+style->get_border_size(),
rect.right()-style->get_border_size(),
rect.bottom()-style->get_border_size()-hsb.height());
// figure out how many scroll bar positions there should be
unsigned long hdelta = total_rect_.width()-display_rect_.width();
......@@ -2564,10 +2574,10 @@ namespace dlib
hsb.hide();
vsb.show();
}
display_rect_ = rectangle( rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size-vsb.width(),
rect.bottom()-border_size);
display_rect_ = rectangle( rect.left()+style->get_border_size(),
rect.top()+style->get_border_size(),
rect.right()-style->get_border_size()-vsb.width(),
rect.bottom()-style->get_border_size());
unsigned long vdelta = total_rect_.height()-display_rect_.height();
vdelta = (vdelta+vscroll_bar_inc-1)/vscroll_bar_inc;
......@@ -2585,10 +2595,10 @@ namespace dlib
hsb.hide();
vsb.hide();
}
display_rect_ = rectangle( rect.left()+border_size,
rect.top()+border_size,
rect.right()-border_size,
rect.bottom()-border_size);
display_rect_ = rectangle( rect.left()+style->get_border_size(),
rect.top()+style->get_border_size(),
rect.right()-style->get_border_size(),
rect.bottom()-style->get_border_size());
hsb.set_max_slider_pos(0);
vsb.set_max_slider_pos(0);
......@@ -2748,13 +2758,13 @@ namespace dlib
{
auto_mutex M(m);
drawable::set_pos(x,y);
vsb.set_pos(rect.right()-border_size-vsb.width()+1, rect.top()+border_size);
hsb.set_pos(rect.left()+border_size, rect.bottom()-border_size-hsb.height()+1);
vsb.set_pos(rect.right()-style->get_border_size()-vsb.width()+1, rect.top()+style->get_border_size());
hsb.set_pos(rect.left()+style->get_border_size(), rect.bottom()-style->get_border_size()-hsb.height()+1);
const long delta_x = total_rect_.left() - display_rect_.left();
const long delta_y = total_rect_.top() - display_rect_.top();
display_rect_ = move_rect(display_rect_, rect.left()+border_size, rect.top()+border_size);
display_rect_ = move_rect(display_rect_, rect.left()+style->get_border_size(), rect.top()+style->get_border_size());
total_rect_ = move_rect(total_rect_, display_rect_.left()+delta_x, display_rect_.top()+delta_y);
}
......@@ -2989,11 +2999,7 @@ namespace dlib
const canvas& c
) const
{
rectangle area = c.intersect(rect);
if (area.is_empty() == true)
return;
draw_sunken_rectangle(c,rect);
style->draw_scrollable_region_border(c, rect, enabled);
}
// ----------------------------------------------------------------------------------------
......@@ -3002,7 +3008,7 @@ namespace dlib
need_h_scroll (
) const
{
if (total_rect_.width() > rect.width()-border_size*2)
if (total_rect_.width() > rect.width()-style->get_border_size()*2)
{
return true;
}
......@@ -3010,8 +3016,8 @@ namespace dlib
{
// check if we would need a vertical scroll bar and if adding one would make us need
// a horizontal one
if (total_rect_.height() > rect.height()-border_size*2 &&
total_rect_.width() > rect.width()-border_size*2-vsb.width())
if (total_rect_.height() > rect.height()-style->get_border_size()*2 &&
total_rect_.width() > rect.width()-style->get_border_size()*2-vsb.width())
return true;
else
return false;
......@@ -3024,7 +3030,7 @@ namespace dlib
need_v_scroll (
) const
{
if (total_rect_.height() > rect.height()-border_size*2)
if (total_rect_.height() > rect.height()-style->get_border_size()*2)
{
return true;
}
......@@ -3032,8 +3038,8 @@ namespace dlib
{
// check if we would need a horizontal scroll bar and if adding one would make us need
// a vertical_scroll_pos one
if (total_rect_.width() > rect.width()-border_size*2 &&
total_rect_.height() > rect.height()-border_size*2-hsb.height())
if (total_rect_.width() > rect.width()-style->get_border_size()*2 &&
total_rect_.height() > rect.height()-style->get_border_size()*2-hsb.height())
return true;
else
return false;
......
......@@ -2096,6 +2096,22 @@ namespace dlib
long y
);
template <
typename style_type
>
void set_style (
const style_type& style_
)
{
auto_mutex M(m);
style.reset(new style_type(style_));
hsb.set_style(style_.get_horizontal_scroll_bar_style());
vsb.set_style(style_.get_vertical_scroll_bar_style());
// do this just so that everything gets redrawn right
set_size(rect.width(), rect.height());
}
void set_zoom_increment (
double zi
);
......@@ -2240,6 +2256,8 @@ namespace dlib
scroll_bar vsb;
scroll_bar hsb;
scoped_ptr<scrollable_region_style> style;
// restricted functions
zoomable_region(zoomable_region&); // copy constructor
zoomable_region& operator=(zoomable_region&); // assignment operator
......@@ -2252,7 +2270,6 @@ namespace dlib
{
/*!
INITIAL VALUE
- border_size == 2
- hscroll_bar_inc == 1
- vscroll_bar_inc == 1
- h_wheel_scroll_bar_inc == 1
......@@ -2262,7 +2279,6 @@ namespace dlib
CONVENTION
- mouse_drag_enabled() == mouse_drag_enabled_
- border_size == 2
- horizontal_scroll_increment() == hscroll_bar_inc
- vertical_scroll_increment() == vscroll_bar_inc
- horizontal_mouse_wheel_scroll_increment() == h_wheel_scroll_bar_inc
......@@ -2290,6 +2306,22 @@ namespace dlib
virtual ~scrollable_region (
) = 0;
template <
typename style_type
>
void set_style (
const style_type& style_
)
{
auto_mutex M(m);
style.reset(new style_type(style_));
hsb.set_style(style_.get_horizontal_scroll_bar_style());
vsb.set_style(style_.get_vertical_scroll_bar_style());
// do this just so that everything gets redrawn right
set_size(rect.width(), rect.height());
}
void show (
);
......@@ -2435,7 +2467,6 @@ namespace dlib
rectangle display_rect_;
scroll_bar hsb;
scroll_bar vsb;
const unsigned long border_size;
unsigned long hscroll_bar_inc;
unsigned long vscroll_bar_inc;
unsigned long h_wheel_scroll_bar_inc;
......@@ -2443,6 +2474,7 @@ namespace dlib
bool mouse_drag_enabled_;
bool user_is_dragging_mouse;
point drag_origin;
scoped_ptr<scrollable_region_style> style;
};
......
......@@ -1614,6 +1614,20 @@ namespace dlib
- all resources associated with *this have been released
!*/
template <
typename style_type
>
void set_style (
const style_type& style_
);
/*!
requires
- style_type == a type that inherits from scrollable_region_style
ensures
- this zoomable_region object will draw itself using the given
style
!*/
void set_zoom_increment (
double zi
);
......@@ -1841,6 +1855,20 @@ namespace dlib
- all resources associated with *this have been released
!*/
template <
typename style_type
>
void set_style (
const style_type& style_
);
/*!
requires
- style_type == a type that inherits from scrollable_region_style
ensures
- this scrollable_region object will draw itself using the given
style
!*/
void set_size (
unsigned long width,
unsigned long height
......
......@@ -533,6 +533,50 @@ namespace dlib
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// scrollable_region styles
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class scrollable_region_style
{
public:
virtual ~scrollable_region_style() {}
virtual long get_border_size (
) const = 0;
virtual void draw_scrollable_region_border (
const canvas& c,
const rectangle& rect,
const bool enabled
) const = 0;
};
// ----------------------------------------------------------------------------------------
class scrollable_region_style_default : public scrollable_region_style
{
public:
scroll_bar_style_default get_horizontal_scroll_bar_style (
) const { return scroll_bar_style_default(); }
scroll_bar_style_default get_vertical_scroll_bar_style (
) const { return scroll_bar_style_default(); }
virtual long get_border_size (
) const { return 2; }
virtual void draw_scrollable_region_border (
const canvas& c,
const rectangle& rect,
const bool enabled
) const { draw_sunken_rectangle(c,rect); }
};
// ----------------------------------------------------------------------------------------
}
......
......@@ -418,6 +418,78 @@ namespace dlib
!*/
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// scrollable_region (and zoomable_region) styles
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class scrollable_region_style
{
/*!
WHAT THIS OBJECT REPRESENTS
This is an abstract class that defines the interface a
scrollable_region and zoomable_region style object must implement.
Note that derived classes must be copyable via
their copy constructors.
!*/
public:
virtual ~scrollable_region_style() {}
virtual long get_border_size (
) const = 0;
/*!
requires
- the mutex drawable::m is locked
ensures
- returns the size of the border region in pixels
!*/
virtual void draw_scrollable_region_border (
const canvas& c,
const rectangle& rect,
const bool enabled
) const = 0;
/*!
requires
- the mutex drawable::m is locked
- c == the canvas to draw on
- rect and enabled are the variables defined in the protected section
of the drawable class.
ensures
- draws the border part of a scrollable_region on the canvas c at the
location given by rect.
!*/
scroll_bar_style_type get_horizontal_scroll_bar_style (
) const;
/*!
ensures
- returns the style of scroll_bar to use for the
horizontal scroll_bar in this widget.
!*/
scroll_bar_style_type get_vertical_scroll_bar_style (
) const;
/*!
ensures
- returns the style of scroll_bar to use for the
vertical scroll_bar in this widget.
!*/
};
// ----------------------------------------------------------------------------------------
class scrollable_region_style_default : public scrollable_region_style
{
public:
/*!
This is the default style for scrollable_region and zoomable_region objects.
!*/
};
// ----------------------------------------------------------------------------------------
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment