Add text_changed event for combox, spinbox and textbox

This commit is contained in:
cnjinhao
2015-01-31 04:17:34 +08:00
parent 7c93980530
commit 50f4bd79ce
9 changed files with 156 additions and 15 deletions

View File

@@ -16,6 +16,7 @@
#include <nana/system/dataexch.hpp>
#include <nana/gui/widgets/float_listbox.hpp>
#include <nana/gui/widgets/skeletons/text_editor.hpp>
#include <nana/gui/widgets/skeletons/textbase_export_interface.hpp>
namespace nana
{
@@ -23,6 +24,24 @@ namespace nana
{
namespace combox
{
class event_agent
: public widgets::skeletons::textbase_event_agent_interface
{
public:
event_agent(::nana::combox& wdg)
: widget_{wdg}
{}
void first_change() override{} //empty, because combox does not have this event.
void text_changed() override
{
widget_.events().text_changed.emit(::nana::arg_combox{ widget_ });
}
private:
::nana::combox & widget_;
};
struct item
: public float_listbox::item_interface
{
@@ -84,6 +103,9 @@ namespace nana
editor_->multi_lines(false);
editable(false);
graph_ = &graph;
evt_agent_.reset(new event_agent{ static_cast<nana::combox&>(wd) });
editor_->textbase().set_event_agent(evt_agent_.get());
}
void detached()
@@ -508,7 +530,7 @@ namespace nana
bool image_enabled_{ false };
unsigned image_pixels_{ 16 };
widgets::skeletons::text_editor * editor_{ nullptr };
std::unique_ptr<event_agent> evt_agent_;
struct state_type
{
bool focused;

View File

@@ -21,6 +21,24 @@ namespace nana
{
namespace spinbox
{
class event_agent
: public widgets::skeletons::textbase_event_agent_interface
{
public:
event_agent(::nana::spinbox& wdg)
: widget_{ wdg }
{}
void first_change() override{} //empty, because spinbox does not have this event.
void text_changed() override
{
widget_.events().text_changed.emit(::nana::arg_spinbox{ widget_ });
}
private:
::nana::spinbox & widget_;
};
enum class buttons
{
none, increase, decrease
@@ -32,6 +50,8 @@ namespace nana
virtual ~range_interface() = default;
virtual std::wstring value() const = 0;
virtual bool value(const std::wstring&) = 0;
virtual bool check_value(const std::wstring&) const = 0;
virtual void spin(bool increase) = 0;
};
@@ -51,6 +71,31 @@ namespace nana
return ss.str();
}
bool value(const std::wstring& value_str) override
{
std::wstringstream ss;
ss << value_str;
T v;
ss >> v;
if (begin_ <= v && v <= last_)
{
value_ = v;
return true;
}
return false;
}
bool check_value(const std::wstring& value_str) const override
{
std::wstringstream ss;
ss << value_str;
T v;
ss >> v;
return (begin_ <= v && v <= last_);
}
void spin(bool increase) override
{
if (increase)
@@ -97,6 +142,27 @@ namespace nana
return texts_[pos_];
}
bool value(const std::wstring& value_str) override
{
auto i = std::find(texts_.cbegin(), texts_.cend(), value_str);
if (i != texts_.cend())
{
pos_ = i - texts_.cbegin();
return true;
}
return false;
}
bool check_value(const std::wstring& str) const override
{
for (auto & s : texts_)
{
if (s.find(str) != s.npos)
return true;
}
return false;
}
void spin(bool increase) override
{
if (texts_.empty())
@@ -147,6 +213,15 @@ namespace nana
auto scheme = static_cast<::nana::widgets::skeletons::text_editor_scheme*>(API::dev::get_scheme(wd));
editor_ = new ::nana::widgets::skeletons::text_editor(wd, graph, scheme);
editor_->multi_lines(false);
editor_->set_accept([this](::nana::char_t ch)
{
auto str = editor_->text();
str += ch;
return range_->check_value(str);
});
evt_agent_.reset(new event_agent(static_cast<nana::spinbox&>(wdg)));
editor_->textbase().set_event_agent(evt_agent_.get());
if (!range_)
range_.reset(new range_numeric<int>(0, 100, 1));
@@ -171,6 +246,15 @@ namespace nana
return range_->value();
}
bool value(const ::nana::string& value_str)
{
if (!range_->value(value_str))
return false;
_m_text();
return true;
}
void set_range(std::unique_ptr<range_interface> ptr)
{
range_.swap(ptr);
@@ -344,6 +428,7 @@ namespace nana
private:
::nana::paint::graphics * graph_{nullptr};
::nana::widgets::skeletons::text_editor * editor_{nullptr};
std::unique_ptr<event_agent> evt_agent_;
buttons spin_stated_{ buttons::none };
std::unique_ptr<range_interface> range_;
::nana::timer timer_;
@@ -511,6 +596,16 @@ namespace nana
return{};
}
void spinbox::value(const ::nana::string& s)
{
internal_scope_guard lock;
if (handle())
{
if (get_drawer_trigger().impl()->value(s))
API::refresh_window(handle());
}
}
int spinbox::to_int() const
{
//std::stoi is not defined by MinGW

View File

@@ -27,7 +27,12 @@ namespace nana{ namespace drawerbase {
{
widget_.events().first_change.emit(::nana::arg_textbox{ widget_ });
}
//
void event_agent::text_changed()
{
widget_.events().text_changed.emit(::nana::arg_textbox{ widget_ });
}
//end class event_agent
//class draweer
drawer::drawer()

View File

@@ -145,14 +145,8 @@ namespace nana
{
static std::shared_ptr<data> data_ptr;
if (!data_ptr)
{
data_ptr = std::make_shared<data>();
//Initialize the predefined language text
auto & table = data_ptr->table;
table["Nana.OK"].assign(L"OK");
table["Nana.Cancel"].assign(L"Cancel");
}
return data_ptr;
}