code refine

This commit is contained in:
Jinhao 2016-08-11 02:26:47 +08:00
parent 6e86b6ae6c
commit 9b28c96e9b
11 changed files with 225 additions and 125 deletions

View File

@ -1,7 +1,7 @@
/*
* Implementations of Inner Forward Declaration
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2015 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2016 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@ -26,87 +26,32 @@ namespace nana{
{
class shortkey_container
{
struct item_type
{
window handle;
std::vector<unsigned long> keys;
};
struct item_type;
//Noncopyable
shortkey_container(const shortkey_container&) = delete;
shortkey_container& operator=(const shortkey_container&) = delete;
shortkey_container& operator=(shortkey_container&&) = delete;
public:
void clear()
{
keybase_.clear();
}
shortkey_container();
bool make(window wd, unsigned long key)
{
if (wd == nullptr) return false;
if (key < 0x61) key += (0x61 - 0x41);
shortkey_container(shortkey_container&&);
for (auto & m : keybase_)
{
if (m.handle == wd)
{
m.keys.push_back(key);
return true;
}
}
~shortkey_container();
item_type m;
m.handle = wd;
m.keys.push_back(key);
keybase_.push_back(m);
void clear();
return true;
}
bool make(window wd, unsigned long key);
void umake(window wd)
{
if (wd == nullptr) return;
void umake(window wd);
for (auto i = keybase_.begin(), end = keybase_.end(); i != end; ++i)
{
if (i->handle == wd)
{
keybase_.erase(i);
break;
}
}
}
std::vector<unsigned long> keys(window wd) const;
std::vector<unsigned long> keys(window wd) const
{
std::vector<unsigned long> v;
if (wd)
{
for (auto & m : keybase_)
{
if (m.handle == wd)
{
v = m.keys;
break;
}
}
}
return v;
}
window find(unsigned long key) const
{
if (key < 0x61) key += (0x61 - 0x41);
for (auto & m : keybase_)
{
for (auto n : m.keys)
{
if (key == n)
return m.handle;
}
}
return nullptr;
}
window find(unsigned long key) const;
private:
std::vector<item_type> keybase_;
struct implementation;
implementation * impl_;
};
@ -118,57 +63,39 @@ namespace nana{
struct condition_rep
{
bool ignore_tab{ false }; //ignore tab when the focus is changed by TAB key.
basic_window* pressed{ nullptr }; //The handle to a window which has been pressed by mouse left button.
basic_window* pressed_by_space{ nullptr }; //The handle to a window which has been pressed by SPACEBAR key.
basic_window* hovered{ nullptr }; //the latest window that mouse moved
bool ignore_tab; //ignore tab when the focus is changed by TAB key.
basic_window* pressed; //The handle to a window which has been pressed by mouse left button.
basic_window* pressed_by_space; //The handle to a window which has been pressed by SPACEBAR key.
basic_window* hovered; //the latest window that mouse moved
}condition;
root_misc(basic_window * wd, unsigned width, unsigned height)
: window(wd),
root_graph({ width, height })
{}
root_misc(root_misc&&);
root_misc(basic_window * wd, unsigned width, unsigned height);
};//end struct root_misc
class root_register
{
//Noncopyable
root_register(const root_register&) = delete;
root_register& operator=(const root_register&) = delete;
//Nonmovable
root_register(root_register&&) = delete;
root_register& operator=(root_register&&) = delete;
public:
root_misc* insert(native_window_type wd, root_misc&& misc)
{
recent_ = wd;
auto ret = table_.emplace(wd, std::move(misc));
misc_ptr_ = &(ret.first->second);
return misc_ptr_;
}
root_register();
~root_register();
root_misc * find(native_window_type wd)
{
if (wd == recent_)
return misc_ptr_;
root_misc* insert(native_window_type, root_misc&&);
recent_ = wd;
auto i = table_.find(wd);
if (i != table_.end())
misc_ptr_ = &(i->second);
else
misc_ptr_ = nullptr;
root_misc * find(native_window_type);
return misc_ptr_;
}
void erase(native_window_type wd)
{
table_.erase(wd);
recent_ = wd;
misc_ptr_ = nullptr;
}
void erase(native_window_type);
private:
//Cached
mutable native_window_type recent_{nullptr};
mutable root_misc * misc_ptr_{nullptr};
std::map<native_window_type, root_misc> table_;
struct implementation;
implementation * const impl_;
};
}
}//end namespace nana

View File

@ -29,6 +29,7 @@
#include <nana/gui/detail/basic_window.hpp>
#include <nana/gui/detail/window_manager.hpp>
#include <nana/system/platform.hpp>
#include <nana/paint/pixel_buffer.hpp>
#include <errno.h>
#include <sstream>

View File

@ -17,7 +17,7 @@
#include <sstream>
#include <nana/system/timepiece.hpp>
#include <nana/gui/wvl.hpp>
#include <nana/gui/detail/inner_fwd_implement.hpp>
#include <nana/gui/detail/basic_window.hpp>
#include <nana/gui/detail/native_window_interface.hpp>
#include <nana/gui/layout_utility.hpp>
#include <nana/gui/detail/element_store.hpp>
@ -25,6 +25,7 @@
namespace nana
{
//class internal_scope_guard
internal_scope_guard::internal_scope_guard()
{

View File

@ -13,9 +13,9 @@
#include <nana/config.hpp>
#include <nana/gui/detail/bedrock.hpp>
#include <nana/gui/detail/drawer.hpp>
#include <nana/gui/detail/dynamic_drawing_object.hpp>
#include <nana/gui/detail/effects_renderer.hpp>
#include <nana/gui/detail/basic_window.hpp>
#include "dynamic_drawing_object.hpp"
#if defined(NANA_X11)
#include <nana/detail/linux_X11/platform_spec.hpp>

View File

@ -32,6 +32,178 @@
namespace nana
{
namespace detail
{
//class shortkey_container
struct shortkey_rep
{
window handle;
std::vector<unsigned long> keys;
};
struct shortkey_container::implementation
{
std::vector<shortkey_rep> base;
};
shortkey_container::shortkey_container()
:impl_(new implementation)
{}
shortkey_container::shortkey_container(shortkey_container&& other)
: impl_(other.impl_)
{
other.impl_ = nullptr;
}
shortkey_container::~shortkey_container()
{
delete impl_;
}
void shortkey_container::clear()
{
impl_->base.clear();
}
bool shortkey_container::make(window wd, unsigned long key)
{
if (wd == nullptr) return false;
if (key < 0x61) key += (0x61 - 0x41);
for (auto & m : impl_->base)
{
if (m.handle == wd)
{
m.keys.emplace_back(key);
return true;
}
}
impl_->base.emplace_back();
auto & rep = impl_->base.back();
rep.handle = wd;
rep.keys.emplace_back(key);
return true;
}
void shortkey_container::umake(window wd)
{
if (wd == nullptr) return;
for (auto i = impl_->base.begin(); i != impl_->base.end(); ++i)
{
if (i->handle == wd)
{
impl_->base.erase(i);
break;
}
}
}
std::vector<unsigned long> shortkey_container::keys(window wd) const
{
if (wd)
{
for (auto & m : impl_->base)
{
if (m.handle == wd)
return m.keys;
}
}
return{};
}
window shortkey_container::find(unsigned long key) const
{
if (key < 0x61) key += (0x61 - 0x41);
for (auto & m : impl_->base)
{
for (auto n : m.keys)
{
if (key == n)
return m.handle;
}
}
return nullptr;
}
//end class shortkey_container
//struct root_misc
root_misc::root_misc(root_misc&& other):
window(other.window),
root_graph(std::move(other.root_graph)),
shortkeys(std::move(other.shortkeys)),
condition(std::move(other.condition))
{
}
root_misc::root_misc(basic_window * wd, unsigned width, unsigned height)
: window(wd),
root_graph({ width, height })
{
condition.ignore_tab = false;
condition.pressed = nullptr;
condition.pressed_by_space = nullptr;
condition.hovered = nullptr;
}
//end struct root_misc
//class root_register
struct root_register::implementation
{
//Cached
native_window_type recent_access{ nullptr };
root_misc * misc_ptr{ nullptr };
std::map<native_window_type, root_misc> table;
};
root_register::root_register()
: impl_(new implementation)
{}
root_register::~root_register()
{
delete impl_;
}
root_misc* root_register::insert(native_window_type wd, root_misc&& misc)
{
impl_->recent_access = wd;
auto ret = impl_->table.emplace(wd, std::move(misc));
impl_->misc_ptr = &(ret.first->second);
return impl_->misc_ptr;
}
root_misc * root_register::find(native_window_type wd)
{
if (wd == impl_->recent_access)
return impl_->misc_ptr;
impl_->recent_access = wd;
auto i = impl_->table.find(wd);
if (i != impl_->table.end())
impl_->misc_ptr = &(i->second);
else
impl_->misc_ptr = nullptr;
return impl_->misc_ptr;
}
void root_register::erase(native_window_type wd)
{
impl_->table.erase(wd);
impl_->recent_access = wd;
impl_->misc_ptr = nullptr;
}
//end class root_register
}
namespace detail
{
template<typename Key, typename Value>

View File

@ -1176,7 +1176,7 @@ namespace nana
}
bground::bground(const bground& rhs)
: impl_{ new implementation }
: impl_{ new implementation(*rhs.impl_) }
{
if (impl_->method)
impl_->method = impl_->method->clone();

View File

@ -272,7 +272,7 @@ namespace nana
if(item.sub_menu == nullptr)
{
item.sub_menu = &sub;
sub.owner.push_back(&root_);
sub.owner.emplace_back(&root_);
return true;
}
}

View File

@ -55,7 +55,7 @@ namespace nana
void append(const native_string_type& text, unsigned long shortkey)
{
if(shortkey && shortkey < 0x61) shortkey += (0x61 - 0x41);
cont_.push_back(new item_type(text, shortkey));
cont_.emplace_back(new item_type(text, shortkey));
}
std::size_t find(unsigned long shortkey) const

View File

@ -748,14 +748,13 @@ namespace nana
bool _m_add_tab(std::size_t pos)
{
item_t m;
if((pos == npos) || (pos >= list_.size()))
{
this->list_.push_back(m);
this->list_.emplace_back();
pos = static_cast<unsigned>(list_.size() - 1);
}
else
list_.insert(iterator_at(pos), m);
list_.emplace(iterator_at(pos));
basis_.active = pos;
if(evt_agent_)
@ -1505,7 +1504,7 @@ namespace nana
for (auto & m : items)
{
auto ts = graph.bidi_extent_size(m.text);
pxs.push_back(ts.width + 12);
pxs.emplace_back(ts.width + 12);
pixels += ts.width + 12;
}

View File

@ -973,7 +973,7 @@ namespace paint
#elif defined(NANA_X11)
Display* disp = nana::detail::platform_spec::instance().open_display();
impl_->handle->update_color();
::XDrawPoint(disp, impl_->handle->pixmap, handle_->context, x, y);
::XDrawPoint(disp, impl_->handle->pixmap, impl_->handle->context, x, y);
#endif
if (impl_->changed == false) impl_->changed = true;
}
@ -997,7 +997,7 @@ namespace paint
auto const end = str + len;
auto i = std::find(str, end, '\t');
#if defined(NANA_LINUX) || defined(NANA_MACOS)
handle_->update_text_color();
impl_->handle->update_text_color();
#endif
if (i != end)
{
@ -1177,7 +1177,7 @@ namespace paint
impl_->pxbuf.paste(impl_->handle, point{});
}
#elif defined(NANA_X11)
if (nullptr == handle_) return;
if (nullptr == impl_->handle) return;
double deltapx = double(vertical ? rct.height : rct.width);
double r, g, b;