Commit 719dadc3 authored by Davis King's avatar Davis King

Added overloads of all the GUI event handlers so that now you can

use general functions as callbacks (via any_function).  This way,
if you have a C++0x compiler, you can use lambda functions with the
event handlers.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404134
parent a4441c46
...@@ -818,6 +818,15 @@ namespace dlib ...@@ -818,6 +818,15 @@ namespace dlib
event_handler_self.clear(); event_handler_self.clear();
} }
void set_click_handler (
const any_function<void()>& event_handler_
)
{
auto_mutex M(m);
event_handler = event_handler_;
event_handler_self.clear();
}
template < template <
typename T typename T
> >
...@@ -831,6 +840,15 @@ namespace dlib ...@@ -831,6 +840,15 @@ namespace dlib
event_handler.clear(); event_handler.clear();
} }
void set_sourced_click_handler (
const any_function<void(button&)>& event_handler_
)
{
auto_mutex M(m);
event_handler_self = event_handler_;
event_handler.clear();
}
bool is_depressed ( bool is_depressed (
) const ) const
{ {
...@@ -850,6 +868,14 @@ namespace dlib ...@@ -850,6 +868,14 @@ namespace dlib
button_down_handler = make_mfp(object,event_handler); button_down_handler = make_mfp(object,event_handler);
} }
void set_button_down_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
button_down_handler = event_handler;
}
template < template <
typename T typename T
> >
...@@ -862,6 +888,14 @@ namespace dlib ...@@ -862,6 +888,14 @@ namespace dlib
button_up_handler = make_mfp(object,event_handler); button_up_handler = make_mfp(object,event_handler);
} }
void set_button_up_handler (
const any_function<void(bool)>& event_handler
)
{
auto_mutex M(m);
button_up_handler = event_handler;
}
template < template <
typename T typename T
> >
...@@ -874,6 +908,14 @@ namespace dlib ...@@ -874,6 +908,14 @@ namespace dlib
button_down_handler_self = make_mfp(object,event_handler); button_down_handler_self = make_mfp(object,event_handler);
} }
void set_sourced_button_down_handler (
const any_function<void(button&)>& event_handler
)
{
auto_mutex M(m);
button_down_handler_self = event_handler;
}
template < template <
typename T typename T
> >
...@@ -886,6 +928,14 @@ namespace dlib ...@@ -886,6 +928,14 @@ namespace dlib
button_up_handler_self = make_mfp(object,event_handler); button_up_handler_self = make_mfp(object,event_handler);
} }
void set_sourced_button_up_handler (
const any_function<void(bool,button&)>& event_handler
)
{
auto_mutex M(m);
button_up_handler_self = event_handler;
}
private: private:
// restricted functions // restricted functions
...@@ -994,6 +1044,10 @@ namespace dlib ...@@ -994,6 +1044,10 @@ namespace dlib
void (T::*eh)() void (T::*eh)()
) { auto_mutex M(m); scroll_handler = make_mfp(object,eh); } ) { auto_mutex M(m); scroll_handler = make_mfp(object,eh); }
void set_scroll_handler (
const any_function<void()>& eh
) { auto_mutex M(m); scroll_handler = eh; }
void set_pos ( void set_pos (
long x, long x,
long y long y
...@@ -1580,15 +1634,13 @@ namespace dlib ...@@ -1580,15 +1634,13 @@ namespace dlib
class menu_item_text : public menu_item class menu_item_text : public menu_item
{ {
template <typename T>
void initialize ( void initialize (
T &object, const any_function<void()>& event_handler_,
void (T::*event_handler_)(),
unichar hk unichar hk
) )
{ {
dlib::ustring &str = text; dlib::ustring &str = text;
action = make_mfp(object,event_handler_); action = event_handler_;
if (hk != 0) if (hk != 0)
{ {
...@@ -1619,7 +1671,19 @@ namespace dlib ...@@ -1619,7 +1671,19 @@ namespace dlib
f(default_font::get_font()), f(default_font::get_font()),
hotkey(hk) hotkey(hk)
{ {
initialize(object, event_handler_, hk); initialize(make_mfp(object, event_handler_), hk);
}
menu_item_text (
const std::string& str,
const any_function<void()>& event_handler_,
unichar hk = 0
) :
text(convert_wstring_to_utf32(convert_mbstring_to_wstring(str))),
f(default_font::get_font()),
hotkey(hk)
{
initialize(event_handler_, hk);
} }
template <typename T> template <typename T>
...@@ -1633,7 +1697,19 @@ namespace dlib ...@@ -1633,7 +1697,19 @@ namespace dlib
f(default_font::get_font()), f(default_font::get_font()),
hotkey(hk) hotkey(hk)
{ {
initialize(object, event_handler_, hk); initialize(make_mfp(object, event_handler_), hk);
}
menu_item_text (
const std::wstring& str,
const any_function<void()>& event_handler_,
unichar hk = 0
) :
text(convert_wstring_to_utf32(str)),
f(default_font::get_font()),
hotkey(hk)
{
initialize(event_handler_, hk);
} }
template <typename T> template <typename T>
...@@ -1647,7 +1723,19 @@ namespace dlib ...@@ -1647,7 +1723,19 @@ namespace dlib
f(default_font::get_font()), f(default_font::get_font()),
hotkey(hk) hotkey(hk)
{ {
initialize(object, event_handler_, hk); initialize(make_mfp(object, event_handler_), hk);
}
menu_item_text (
const dlib::ustring& str,
const any_function<void()>& event_handler_,
unichar hk = 0
) :
text(str),
f(default_font::get_font()),
hotkey(hk)
{
initialize(event_handler_, hk);
} }
virtual unichar get_hot_key ( virtual unichar get_hot_key (
......
...@@ -499,6 +499,20 @@ namespace dlib ...@@ -499,6 +499,20 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_click_handler (
const any_function<void()>& event_handler
);
/*!
ensures
- the event_handler function is called when the button is clicked by
the user.
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
template < template <
typename T typename T
> >
...@@ -520,6 +534,21 @@ namespace dlib ...@@ -520,6 +534,21 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_sourced_click_handler (
const any_function<void(button& self)>& event_handler
);
/*!
ensures
- &self == this
- the event_handler function is called when the button is clicked by
the user.
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
template < template <
typename T typename T
> >
...@@ -540,6 +569,20 @@ namespace dlib ...@@ -540,6 +569,20 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_button_down_handler (
const any_function<void()>& event_handler
);
/*!
ensures
- the event_handler function is called when the user causes the button
to go into its depressed state.
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
template < template <
typename T typename T
> >
...@@ -564,6 +607,24 @@ namespace dlib ...@@ -564,6 +607,24 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_button_up_handler (
const any_function<void(bool mouse_over)>& event_handler
);
/*!
ensures
- the event_handler function is called when the user causes the
button to go into its non-depressed state.
- if (the mouse is over this button when this event occurs) then
- mouse_over == true
- else
- mouse_over == false
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
template < template <
typename T typename T
> >
...@@ -585,6 +646,21 @@ namespace dlib ...@@ -585,6 +646,21 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_sourced_button_down_handler (
const any_function<void(button& self)>& event_handler
);
/*!
ensures
- &self == this
- the event_handler function is called when the user causes the button
to go into its depressed state.
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
template < template <
typename T typename T
> >
...@@ -610,6 +686,25 @@ namespace dlib ...@@ -610,6 +686,25 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_sourced_button_up_handler (
const any_function<void(bool mouse_over, button& self)>& event_handler
);
/*!
ensures
- &self == this
- the event_handler function is called when the user causes the
button to go into its non-depressed state.
- if (the mouse is over this button when this event occurs) then
- mouse_over == true
- else
- mouse_over == false
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
private: private:
// restricted functions // restricted functions
...@@ -786,6 +881,21 @@ namespace dlib ...@@ -786,6 +881,21 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
void set_scroll_handler (
const any_function<void()>& event_handler
);
/*!
ensures
- The event_handler function is called whenever the user causes the slider box
to move.
- This event is NOT triggered by calling set_slider_pos()
- any previous calls to this function are overridden by this new call.
(i.e. you can only have one event handler associated with this
event at a time)
throws
- std::bad_alloc
!*/
private: private:
// restricted functions // restricted functions
...@@ -1263,6 +1373,19 @@ namespace dlib ...@@ -1263,6 +1373,19 @@ namespace dlib
- #get_hot_key() == hotkey - #get_hot_key() == hotkey
!*/ !*/
menu_item_text (
const std::string& str,
const any_function<void()>& on_click_handler,
unichar hotkey = 0
);
/*!
ensures
- The text of this menu item will be str
- the on_click_handler function is called when this menu_item
clicked by the user.
- #get_hot_key() == hotkey
!*/
// overloads for wide character strings // overloads for wide character strings
template < template <
typename T typename T
...@@ -1274,6 +1397,12 @@ namespace dlib ...@@ -1274,6 +1397,12 @@ namespace dlib
unichar hotkey = 0 unichar hotkey = 0
); );
menu_item_text (
const std::wstring& str,
const any_function<void()>& on_click_handler,
unichar hotkey = 0
);
template < template <
typename T typename T
> >
...@@ -1283,6 +1412,15 @@ namespace dlib ...@@ -1283,6 +1412,15 @@ namespace dlib
void (T::*on_click_handler)(), void (T::*on_click_handler)(),
unichar hotkey = 0 unichar hotkey = 0
); );
template <
typename T
>
menu_item_text (
const dlib::ustring& str,
const any_function<void()>& on_click_handler,
unichar hotkey = 0
);
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -245,6 +245,15 @@ namespace dlib ...@@ -245,6 +245,15 @@ namespace dlib
event_handler_self.clear(); event_handler_self.clear();
} }
void set_click_handler (
const any_function<void()>& event_handler_
)
{
auto_mutex M(m);
event_handler = event_handler_;
event_handler_self.clear();
}
template < template <
typename T typename T
> >
...@@ -258,6 +267,15 @@ namespace dlib ...@@ -258,6 +267,15 @@ namespace dlib
event_handler.clear(); event_handler.clear();
} }
void set_sourced_click_handler (
const any_function<void(toggle_button&)>& event_handler_
)
{
auto_mutex M(m);
event_handler_self = event_handler_;
event_handler.clear();
}
private: private:
// restricted functions // restricted functions
...@@ -500,6 +518,21 @@ namespace dlib ...@@ -500,6 +518,21 @@ namespace dlib
enter_key_handler = make_mfp(object,event_handler); enter_key_handler = make_mfp(object,event_handler);
} }
void set_text_modified_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
text_modified_handler = event_handler;
}
void set_enter_key_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
enter_key_handler = event_handler;
}
template < template <
typename T typename T
...@@ -513,6 +546,14 @@ namespace dlib ...@@ -513,6 +546,14 @@ namespace dlib
focus_lost_handler = make_mfp(object,event_handler); focus_lost_handler = make_mfp(object,event_handler);
} }
void set_focus_lost_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
focus_lost_handler = event_handler;
}
private: private:
void on_cut ( void on_cut (
...@@ -853,6 +894,14 @@ namespace dlib ...@@ -853,6 +894,14 @@ namespace dlib
text_modified_handler = make_mfp(object,event_handler); text_modified_handler = make_mfp(object,event_handler);
} }
void set_text_modified_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
text_modified_handler = event_handler;
}
template < template <
typename T typename T
> >
...@@ -865,6 +914,13 @@ namespace dlib ...@@ -865,6 +914,13 @@ namespace dlib
enter_key_handler = make_mfp(object,event_handler); enter_key_handler = make_mfp(object,event_handler);
} }
void set_enter_key_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
enter_key_handler = event_handler;
}
template < template <
typename T typename T
...@@ -878,6 +934,14 @@ namespace dlib ...@@ -878,6 +934,14 @@ namespace dlib
focus_lost_handler = make_mfp(object,event_handler); focus_lost_handler = make_mfp(object,event_handler);
} }
void set_focus_lost_handler (
const any_function<void()>& event_handler
)
{
auto_mutex M(m);
focus_lost_handler = event_handler;
}
private: private:
void on_cut ( void on_cut (
...@@ -1167,6 +1231,14 @@ namespace dlib ...@@ -1167,6 +1231,14 @@ namespace dlib
event_handler = make_mfp(object,eh); event_handler = make_mfp(object,eh);
} }
void set_click_handler (
const any_function<void(unsigned long,unsigned long)>& eh
)
{
auto_mutex M(m);
event_handler = eh;
}
void set_tab_group ( void set_tab_group (
unsigned long idx, unsigned long idx,
widget_group& group widget_group& group
...@@ -1436,16 +1508,12 @@ namespace dlib ...@@ -1436,16 +1508,12 @@ namespace dlib
~box_win ( ~box_win (
); );
template <
typename T
>
void set_click_handler ( void set_click_handler (
T& object, const any_function<void()>& event_handler_
void (T::*event_handler_)()
) )
{ {
auto_mutex M(wm); auto_mutex M(wm);
event_handler = make_mfp(object,event_handler_); event_handler = event_handler_;
} }
private: private:
...@@ -1516,7 +1584,18 @@ namespace dlib ...@@ -1516,7 +1584,18 @@ namespace dlib
{ {
using namespace message_box_helper; using namespace message_box_helper;
box_win* win = new box_win(title,message); box_win* win = new box_win(title,message);
win->set_click_handler(object,event_handler); win->set_click_handler(make_mfp(object,event_handler));
}
inline void message_box (
const std::string& title,
const std::string& message,
const any_function<void()>& event_handler
)
{
using namespace message_box_helper;
box_win* win = new box_win(title,message);
win->set_click_handler(event_handler);
} }
inline void message_box ( inline void message_box (
...@@ -1670,6 +1749,10 @@ namespace dlib ...@@ -1670,6 +1749,10 @@ namespace dlib
void (T::*eh)(unsigned long index) void (T::*eh)(unsigned long index)
) { auto_mutex M(m); event_handler = make_mfp(object,eh); } ) { auto_mutex M(m); event_handler = make_mfp(object,eh); }
void set_double_click_handler (
const any_function<void(unsigned long)>& eh
) { auto_mutex M(m); event_handler = eh; }
template < template <
typename T typename T
> >
...@@ -1678,6 +1761,10 @@ namespace dlib ...@@ -1678,6 +1761,10 @@ namespace dlib
void (T::*eh)(unsigned long index) void (T::*eh)(unsigned long index)
) { auto_mutex M(m); single_click_event_handler = make_mfp(object,eh); } ) { auto_mutex M(m); single_click_event_handler = make_mfp(object,eh); }
void set_click_handler (
const any_function<void(unsigned long)>& eh
) { auto_mutex M(m); single_click_event_handler = eh; }
bool at_start ( bool at_start (
) const; ) const;
...@@ -1765,16 +1852,12 @@ namespace dlib ...@@ -1765,16 +1852,12 @@ namespace dlib
~box_win ( ~box_win (
); );
template <
typename T
>
void set_click_handler ( void set_click_handler (
T& object, const any_function<void(const std::string&)>& event_handler_
void (T::*event_handler_)(const std::string&)
) )
{ {
auto_mutex M(wm); auto_mutex M(wm);
event_handler = make_mfp(object,event_handler_); event_handler = event_handler_;
} }
private: private:
...@@ -1852,7 +1935,16 @@ namespace dlib ...@@ -1852,7 +1935,16 @@ namespace dlib
{ {
using namespace open_file_box_helper; using namespace open_file_box_helper;
box_win* win = new box_win("Open File",true); box_win* win = new box_win("Open File",true);
win->set_click_handler(object,event_handler); win->set_click_handler(make_mfp(object,event_handler));
}
inline void open_file_box (
const any_function<void(const std::string&)>& event_handler
)
{
using namespace open_file_box_helper;
box_win* win = new box_win("Open File",true);
win->set_click_handler(event_handler);
} }
template < template <
...@@ -1865,7 +1957,16 @@ namespace dlib ...@@ -1865,7 +1957,16 @@ namespace dlib
{ {
using namespace open_file_box_helper; using namespace open_file_box_helper;
box_win* win = new box_win("Open File"); box_win* win = new box_win("Open File");
win->set_click_handler(object,event_handler); win->set_click_handler(make_mfp(object,event_handler));
}
inline void open_existing_file_box (
const any_function<void(const std::string&)>& event_handler
)
{
using namespace open_file_box_helper;
box_win* win = new box_win("Open File");
win->set_click_handler(event_handler);
} }
template < template <
...@@ -1878,7 +1979,16 @@ namespace dlib ...@@ -1878,7 +1979,16 @@ namespace dlib
{ {
using namespace open_file_box_helper; using namespace open_file_box_helper;
box_win* win = new box_win("Save File",true); box_win* win = new box_win("Save File",true);
win->set_click_handler(object,event_handler); win->set_click_handler(make_mfp(object,event_handler));
}
inline void save_file_box (
const any_function<void(const std::string&)>& event_handler
)
{
using namespace open_file_box_helper;
box_win* win = new box_win("Save File",true);
win->set_click_handler(event_handler);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -2313,6 +2423,14 @@ namespace dlib ...@@ -2313,6 +2423,14 @@ namespace dlib
node_selected_handler = make_mfp(object,event_handler_); node_selected_handler = make_mfp(object,event_handler_);
} }
void set_node_selected_handler (
const any_function<void(unsigned long)>& event_handler_
)
{
auto_mutex M(m);
node_selected_handler = event_handler_;
}
template < template <
typename T typename T
> >
...@@ -2325,6 +2443,14 @@ namespace dlib ...@@ -2325,6 +2443,14 @@ namespace dlib
node_deselected_handler = make_mfp(object,event_handler_); node_deselected_handler = make_mfp(object,event_handler_);
} }
void set_node_deselected_handler (
const any_function<void(unsigned long)>& event_handler_
)
{
auto_mutex M(m);
node_deselected_handler = event_handler_;
}
template < template <
typename T typename T
> >
...@@ -2337,6 +2463,14 @@ namespace dlib ...@@ -2337,6 +2463,14 @@ namespace dlib
node_deleted_handler = make_mfp(object,event_handler_); node_deleted_handler = make_mfp(object,event_handler_);
} }
void set_node_deleted_handler (
const any_function<void()>& event_handler_
)
{
auto_mutex M(m);
node_deleted_handler = event_handler_;
}
template < template <
typename T typename T
> >
...@@ -2349,6 +2483,14 @@ namespace dlib ...@@ -2349,6 +2483,14 @@ namespace dlib
graph_modified_handler = make_mfp(object,event_handler_); graph_modified_handler = make_mfp(object,event_handler_);
} }
void set_graph_modified_handler (
const any_function<void()>& event_handler_
)
{
auto_mutex M(m);
graph_modified_handler = event_handler_;
}
protected: protected:
void on_keydown ( void on_keydown (
...@@ -2951,6 +3093,10 @@ namespace dlib ...@@ -2951,6 +3093,10 @@ namespace dlib
void (T::*eh)(unsigned long, unsigned long) void (T::*eh)(unsigned long, unsigned long)
) { text_modified_handler = make_mfp(object,eh); } ) { text_modified_handler = make_mfp(object,eh); }
void set_text_modified_handler (
const any_function<void(unsigned long, unsigned long)>& eh
) { text_modified_handler = eh; }
private: private:
void on_user_event ( void on_user_event (
......
This diff is collapsed.
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