Added two new events to the text_field object. One for detecting when the

user hits enter and another for detecting when input focus is lost.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402279
pull/2/head
Davis King 17 years ago
parent 66f92cb156
commit 31b6ded6eb

@ -871,6 +871,8 @@ namespace dlib
highlight_start = 0;
highlight_end = -1;
if (focus_lost_handler.is_set())
focus_lost_handler();
parent.invalidate_rectangle(rect);
}
}
@ -1021,6 +1023,11 @@ namespace dlib
if (text_modified_handler.is_set())
text_modified_handler();
}
else if (key == '\n')
{
if (enter_key_handler.is_set())
enter_key_handler();
}
}
else if (key == base_window::KEY_BACKSPACE)
{

@ -519,6 +519,31 @@ namespace dlib
text_modified_handler.set(object,event_handler);
}
template <
typename T
>
void set_enter_key_handler (
T& object,
void (T::*event_handler)()
)
{
auto_mutex M(m);
enter_key_handler.set(object,event_handler);
}
template <
typename T
>
void set_focus_lost_handler (
T& object,
void (T::*event_handler)()
)
{
auto_mutex M(m);
focus_lost_handler.set(object,event_handler);
}
private:
void on_user_event (
@ -590,6 +615,8 @@ namespace dlib
long highlight_end;
long shift_pos;
member_function_pointer<>::kernel_1a_c text_modified_handler;
member_function_pointer<>::kernel_1a_c enter_key_handler;
member_function_pointer<>::kernel_1a_c focus_lost_handler;
timer<text_field>::kernel_2a t;

@ -701,6 +701,45 @@ namespace dlib
- std::bad_alloc
!*/
template <
typename T
>
void set_enter_key_handler (
T& object,
void (T::*event_handler)()
);
/*!
requires
- event_handler is a valid pointer to a member function in T
ensures
- the event_handler function is called on object when this text field
has input focus and the user hits the enter key on their keyboard.
- 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 <
typename T
>
void set_focus_lost_handler (
T& object,
void (T::*event_handler)()
);
/*!
requires
- event_handler is a valid pointer to a member function in T
ensures
- the event_handler function is called on object when this object
loses input focus due to the user clicking outside the text field
- 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:

@ -591,6 +591,8 @@ public:
tf.set_text("Davis685g@");
tf.set_width(500);
tf.set_text_color(rgb_pixel(255,0,0));
tf.set_enter_key_handler(*this,&win::on_enter_key);
tf.set_focus_lost_handler(*this,&win::on_tf_focus_lost);
button_count = 0;
@ -625,6 +627,18 @@ public:
private:
void on_enter_key()
{
cout << "enter key pressed" << endl;
}
void on_tf_focus_lost()
{
cout << "text field lost focus" << endl;
}
void on_open_file (const std::string& file)
{
message_box("file opened",file);

Loading…
Cancel
Save