Partial implementation of missing nana::typeface()

This commit is contained in:
James Bremner 2018-12-18 17:05:45 -05:00
parent e7bc1405ec
commit 1d2c7f85f4
2 changed files with 254 additions and 235 deletions

View File

@ -3,8 +3,8 @@
* Nana C++ Library(http://www.nanaro.org) * Nana C++ Library(http://www.nanaro.org)
* Copyright(C) 2015-2018 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2015-2018 Jinhao(cnjinhao@hotmail.com)
* *
* Distributed under the Boost Software License, Version 1.0. * Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at * (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt) * http://www.boost.org/LICENSE_1_0.txt)
* *
* @file: nana/gui/widgets/group.hpp * @file: nana/gui/widgets/group.hpp
@ -77,6 +77,9 @@ namespace nana{
/// Determines whether a specified option is checked, it throws an out_of_range if !(pos < number of options) /// Determines whether a specified option is checked, it throws an out_of_range if !(pos < number of options)
bool option_checked(std::size_t pos) const; bool option_checked(std::size_t pos) const;
/// Change typeface of caption label ( does not effect child widgets )
void typeface( const nana::paint::font& font );
group& enable_format_caption(bool format); group& enable_format_caption(bool format);
group& collocate() noexcept; group& collocate() noexcept;
@ -86,7 +89,7 @@ namespace nana{
void field_display(const char* field_name, bool display); ///<Displays/Discards an existing field. void field_display(const char* field_name, bool display); ///<Displays/Discards an existing field.
bool field_display(const char* field_name) const; ///<Determines whether the specified field is displayed. bool field_display(const char* field_name) const; ///<Determines whether the specified field is displayed.
void erase(window handle); ///< Erases a window from field. void erase(window handle); ///< Erases a window from field.
template<typename Widget, typename ...Args> template<typename Widget, typename ...Args>
Widget* create_child(const char* field, Args && ... args) Widget* create_child(const char* field, Args && ... args)
{ {

View File

@ -3,8 +3,8 @@
* Nana C++ Library(http://www.nanaro.org) * Nana C++ Library(http://www.nanaro.org)
* Copyright(C) 2015-2018 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2015-2018 Jinhao(cnjinhao@hotmail.com)
* *
* Distributed under the Boost Software License, Version 1.0. * Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at * (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt) * http://www.boost.org/LICENSE_1_0.txt)
* *
* @file: nana/gui/widgets/group.cpp * @file: nana/gui/widgets/group.cpp
@ -14,7 +14,7 @@
* @brief group is a widget used to visually group and layout other widgets. * @brief group is a widget used to visually group and layout other widgets.
* *
* @contributor: * @contributor:
* dankan1890(https://github.com/dankan1890) * dankan1890(https://github.com/dankan1890)
*/ */
@ -27,300 +27,316 @@
if(empty()) \ if(empty()) \
throw std::logic_error("the group is invalid"); throw std::logic_error("the group is invalid");
namespace nana{ namespace nana
{
static const char* field_title = "__nana_group_title__"; static const char* field_title = "__nana_group_title__";
static const char* field_options = "__nana_group_options__"; static const char* field_options = "__nana_group_options__";
struct group::implement struct group::implement
{ {
label caption; label caption;
align caption_align{ align::left }; align caption_align{ align::left };
place place_content; place place_content;
unsigned gap{2}; unsigned gap{2};
std::string usr_div_str; std::string usr_div_str;
nana::size caption_dimension; nana::size caption_dimension;
std::vector<std::unique_ptr<checkbox>> options; std::vector<std::unique_ptr<checkbox>> options;
radio_group * radio_logic{nullptr}; radio_group * radio_logic{nullptr};
implement() = default; implement() = default;
implement(window grp_panel, ::std::string titel, bool vsb, unsigned gap=2) implement(window grp_panel, ::std::string titel, bool vsb, unsigned gap=2)
: caption (grp_panel, std::move(titel), vsb), : caption (grp_panel, std::move(titel), vsb),
place_content{grp_panel}, place_content{grp_panel},
gap{gap} gap{gap}
{ {
} }
void create(window pnl) void create(window pnl)
{ {
caption.create(pnl); caption.create(pnl);
caption.caption(""); caption.caption("");
place_content.bind(pnl); place_content.bind(pnl);
if (!radio_logic) if (!radio_logic)
radio_logic = new radio_group; radio_logic = new radio_group;
} }
void update_div() void update_div()
{ {
const std::size_t padding = 10; const std::size_t padding = 10;
caption_dimension = caption.measure(1000); caption_dimension = caption.measure(1000);
caption_dimension.width += 1; caption_dimension.width += 1;
std::string div = "vert margin=[0," + std::to_string(gap) + "," + std::to_string(gap + 5) + "," + std::to_string(gap) + "]"; std::string div = "vert margin=[0," + std::to_string(gap) + "," + std::to_string(gap + 5) + "," + std::to_string(gap) + "]";
div += "<weight=" + std::to_string(caption_dimension.height) + " "; div += "<weight=" + std::to_string(caption_dimension.height) + " ";
if (align::left == caption_align) if (align::left == caption_align)
div += "<weight=" + std::to_string(padding) + ">"; div += "<weight=" + std::to_string(padding) + ">";
else else
div += "<>"; //right or center div += "<>"; //right or center
div += "<" + std::string{ field_title } + " weight=" + std::to_string(caption_dimension.width) + ">"; div += "<" + std::string{ field_title } + " weight=" + std::to_string(caption_dimension.width) + ">";
if (align::right == caption_align) if (align::right == caption_align)
div += "<weight=" + std::to_string(padding) + ">"; div += "<weight=" + std::to_string(padding) + ">";
else if (align::center == caption_align) else if (align::center == caption_align)
div += "<>"; div += "<>";
div += "><<vert margin=5 " + std::string(field_options) + ">"; div += "><<vert margin=5 " + std::string(field_options) + ">";
if (!usr_div_str.empty()) if (!usr_div_str.empty())
div += "<" + usr_div_str + ">>"; div += "<" + usr_div_str + ">>";
else else
div += ">"; div += ">";
place_content.div(div.c_str()); place_content.div(div.c_str());
if (options.empty()) if (options.empty())
place_content.field_display(field_options, false); place_content.field_display(field_options, false);
if (caption.caption().empty()) if (caption.caption().empty())
place_content.field_display(field_title, false); place_content.field_display(field_title, false);
} }
}; };
group::group() group::group()
: impl_(new implement) : impl_(new implement)
{ {
} }
group::group(window parent, const rectangle& r, bool vsb) group::group(window parent, const rectangle& r, bool vsb)
: group() : group()
{ {
create(parent, r, vsb); create(parent, r, vsb);
} }
using groupbase_type = widget_object<category::widget_tag, drawerbase::panel::drawer, general_events, drawerbase::group::scheme>; using groupbase_type = widget_object<category::widget_tag, drawerbase::panel::drawer, general_events, drawerbase::group::scheme>;
group::group(window parent, ::std::string titel, bool formatted, unsigned gap, const rectangle& r, bool vsb) group::group(window parent, ::std::string titel, bool formatted, unsigned gap, const rectangle& r, bool vsb)
: group(parent, r, vsb) : group(parent, r, vsb)
{ {
this->bgcolor(API::bgcolor(parent)); this->bgcolor(API::bgcolor(parent));
impl_.reset(new implement(*this, std::move(titel), vsb, gap)); impl_.reset(new implement(*this, std::move(titel), vsb, gap));
impl_->caption.format(formatted); impl_->caption.format(formatted);
_m_init(); _m_init();
} }
group::~group() group::~group()
{ {
delete impl_->radio_logic; delete impl_->radio_logic;
} }
checkbox& group::add_option(std::string text) checkbox& group::add_option(std::string text)
{ {
_THROW_IF_EMPTY() _THROW_IF_EMPTY()
#ifdef _nana_std_has_emplace_return_type #ifdef _nana_std_has_emplace_return_type
auto & opt = impl_->options.emplace_back(new checkbox{ handle() }); auto & opt = impl_->options.emplace_back(new checkbox { handle() });
#else #else
impl_->options.emplace_back(new checkbox(handle())); impl_->options.emplace_back(new checkbox(handle()));
auto & opt = impl_->options.back(); auto & opt = impl_->options.back();
#endif #endif
opt->transparent(true); opt->transparent(true);
opt->caption(std::move(text)); opt->caption(std::move(text));
impl_->place_content[field_options] << *opt; impl_->place_content[field_options] << *opt;
impl_->place_content.field_display(field_options, true); impl_->place_content.field_display(field_options, true);
impl_->place_content.collocate(); impl_->place_content.collocate();
if (impl_->radio_logic) if (impl_->radio_logic)
impl_->radio_logic->add(*opt); impl_->radio_logic->add(*opt);
return *impl_->options.back(); return *impl_->options.back();
} }
void group::caption_align(align position) void group::caption_align(align position)
{ {
if (position != impl_->caption_align) if (position != impl_->caption_align)
{ {
impl_->caption_align = position; impl_->caption_align = position;
impl_->update_div(); impl_->update_div();
impl_->place_content.collocate(); impl_->place_content.collocate();
API::refresh_window(*this); API::refresh_window(*this);
} }
} }
group& group::radio_mode(bool enable) group& group::radio_mode(bool enable)
{ {
_THROW_IF_EMPTY() _THROW_IF_EMPTY()
if (enable) if (enable)
{ {
//Create radio_group if it is null //Create radio_group if it is null
if (!impl_->radio_logic) if (!impl_->radio_logic)
impl_->radio_logic = new ::nana::radio_group; impl_->radio_logic = new ::nana::radio_group;
//add all options into the radio_group //add all options into the radio_group
for (auto & opt : impl_->options) for (auto & opt : impl_->options)
impl_->radio_logic->add(*opt); impl_->radio_logic->add(*opt);
} }
else else
{ {
delete impl_->radio_logic; delete impl_->radio_logic;
impl_->radio_logic = nullptr; impl_->radio_logic = nullptr;
} }
return *this; return *this;
} }
std::size_t group::option() const std::size_t group::option() const
{ {
_THROW_IF_EMPTY(); _THROW_IF_EMPTY();
if (impl_->radio_logic) if (impl_->radio_logic)
return impl_->radio_logic->checked(); return impl_->radio_logic->checked();
throw std::logic_error("the radio_mode of the group is disabled"); throw std::logic_error("the radio_mode of the group is disabled");
} }
bool group::option_checked(std::size_t pos) const bool group::option_checked(std::size_t pos) const
{ {
_THROW_IF_EMPTY(); _THROW_IF_EMPTY();
return impl_->options.at(pos)->checked(); return impl_->options.at(pos)->checked();
} }
group& group::enable_format_caption(bool format) void group::typeface( const nana::paint::font& font )
{ {
impl_->caption.format(format); // change typeface of caption label
return *this; impl_->caption.typeface( font );
}
group& group::collocate() noexcept /* change size of caption label
{
impl_->place_content.collocate();
return *this;
}
group& group::div(const char* div_str) noexcept The caption may be changed AFTER this call
{ so the neccessary label size is unknown
if (div_str) set it to 80% of the current widget width and 50 pixels
impl_->usr_div_str = div_str; */
else impl_->caption.move( rectangle(0,0,size().width * 0.8,50));
impl_->usr_div_str.clear(); }
impl_->update_div(); group& group::enable_format_caption(bool format)
return *this; {
} impl_->caption.format(format);
return *this;
}
group::field_reference group::operator[](const char* field) group& group::collocate() noexcept
{ {
return impl_->place_content.field(field); impl_->place_content.collocate();
} return *this;
}
void group::field_display(const char* field_name, bool display) group& group::div(const char* div_str) noexcept
{ {
impl_->place_content.field_display(field_name, display); if (div_str)
} impl_->usr_div_str = div_str;
else
impl_->usr_div_str.clear();
bool group::field_display(const char* field_name) const impl_->update_div();
{ return *this;
return impl_->place_content.field_display(field_name); }
}
void group::erase(window handle) group::field_reference group::operator[](const char* field)
{ {
impl_->place_content.erase(handle); return impl_->place_content.field(field);
} }
void group::_m_add_child(const char* field, widget* wdg) void group::field_display(const char* field_name, bool display)
{ {
impl_->place_content[field] << wdg->handle(); impl_->place_content.field_display(field_name, display);
} }
void group::_m_init() bool group::field_display(const char* field_name) const
{ {
this->div(nullptr); return impl_->place_content.field_display(field_name);
}
auto & outter = impl_->place_content; void group::erase(window handle)
{
impl_->place_content.erase(handle);
}
outter[field_title] << impl_->caption; void group::_m_add_child(const char* field, widget* wdg)
outter.collocate(); {
impl_->place_content[field] << wdg->handle();
}
impl_->caption.transparent(true); void group::_m_init()
color pbg = API::bgcolor(this->parent()); {
impl_->caption.bgcolor(pbg.blend(colors::black, 0.025)); this->div(nullptr);
this->bgcolor(pbg.blend(colors::black, 0.05)); auto & outter = impl_->place_content;
drawing dw(*this); outter[field_title] << impl_->caption;
outter.collocate();
//When the group is resized, the drawing is called before moving the caption, but impl_->caption.transparent(true);
//the drawing of group requires the lastest position of caption for gradual rectangle. color pbg = API::bgcolor(this->parent());
//For the requirement, a move event handler is required for listning the change of caption's position. impl_->caption.bgcolor(pbg.blend(colors::black, 0.025));
impl_->caption.events().move([this](const arg_move&){
if (align::left != impl_->caption_align)
API::refresh_window(*this);
});
// This drawing function is owner by the onwer of dw (the outer panel of the group widget), not by dw !! this->bgcolor(pbg.blend(colors::black, 0.05));
dw.draw([this](paint::graphics& graph)
{
auto gap_px = impl_->gap - 1;
auto const top_round_line = static_cast<int>(impl_->caption_dimension.height) / 2; drawing dw(*this);
graph.rectangle(true, API::bgcolor(this->parent())); //When the group is resized, the drawing is called before moving the caption, but
graph.round_rectangle(rectangle(point(gap_px, top_round_line), //the drawing of group requires the lastest position of caption for gradual rectangle.
nana::size(graph.width() - 2 * gap_px, graph.height() - top_round_line - gap_px) //For the requirement, a move event handler is required for listning the change of caption's position.
), impl_->caption.events().move([this](const arg_move&)
3, 3, this->scheme().border, true, this->bgcolor()); {
if (align::left != impl_->caption_align)
API::refresh_window(*this);
});
auto opt_r = API::window_rectangle(impl_->caption); // This drawing function is owner by the onwer of dw (the outer panel of the group widget), not by dw !!
if (opt_r) dw.draw([this](paint::graphics& graph)
{ {
rectangle grad_r{ opt_r->position(), nana::size{ opt_r->width + 4, static_cast<unsigned>(top_round_line - opt_r->y) } }; auto gap_px = impl_->gap - 1;
grad_r.y += top_round_line*2 / 3; auto const top_round_line = static_cast<int>(impl_->caption_dimension.height) / 2;
grad_r.x -= 2;
graph.gradual_rectangle(grad_r, graph.rectangle(true, API::bgcolor(this->parent()));
API::bgcolor(this->parent()), this->bgcolor(), true graph.round_rectangle(rectangle(point(gap_px, top_round_line),
); nana::size(graph.width() - 2 * gap_px, graph.height() - top_round_line - gap_px)
} ),
}); 3, 3, this->scheme().border, true, this->bgcolor());
}
void group::_m_complete_creation() auto opt_r = API::window_rectangle(impl_->caption);
{ if (opt_r)
widget::_m_complete_creation(); {
impl_->create(handle()); rectangle grad_r{ opt_r->position(), nana::size{ opt_r->width + 4, static_cast<unsigned>(top_round_line - opt_r->y) } };
_m_init();
}
auto group::_m_caption() const noexcept -> native_string_type grad_r.y += top_round_line*2 / 3;
{ grad_r.x -= 2;
return impl_->caption.caption_native();
}
void group::_m_caption(native_string_type&& str) graph.gradual_rectangle(grad_r,
{ API::bgcolor(this->parent()), this->bgcolor(), true
impl_->caption.caption(std::move(str)); );
impl_->update_div(); }
impl_->place_content.collocate(); });
} }
void group::_m_complete_creation()
{
widget::_m_complete_creation();
impl_->create(handle());
_m_init();
}
auto group::_m_caption() const noexcept -> native_string_type
{
return impl_->caption.caption_native();
}
void group::_m_caption(native_string_type&& str)
{
impl_->caption.caption(std::move(str));
impl_->update_div();
impl_->place_content.collocate();
}
}//end namespace nana }//end namespace nana