a standard experimental class any

This commit is contained in:
Jinhao 2016-01-25 23:56:05 +08:00
parent 7dbf0a5769
commit 5a960ed88c
16 changed files with 272 additions and 232 deletions

View File

@ -1,145 +1,159 @@
/**
* Any
* Nana C++ Library(http://www.nanapro.org)
* 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
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file nana/any.hpp
*
* @brief An implementation of experimental library any of C++ standard(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html#any)
*/
#ifndef NANA_ANY_HPP #ifndef NANA_ANY_HPP
#define NANA_ANY_HPP #define NANA_ANY_HPP
#include <typeinfo> #include <typeinfo>
#include <utility> //C++11 for std::move #include <type_traits>
#include "c++defines.hpp"
namespace nana namespace nana
{ {
class bad_any_cast
: public std::bad_cast
{
};
class any class any
{ {
struct super_type class content_interface
{ {
virtual ~super_type();
super_type& operator=(const super_type&);
virtual super_type& assign(const super_type&) = 0;
virtual bool same(const super_type&) const = 0;
virtual super_type* clone() const = 0;
}; //end struct super_type
template<typename T>
struct object_type
: public super_type
{
object_type(){}
object_type(T const & obj)
: object(obj)
{}
object_type(T && obj)
: object(std::move(obj))
{}
object_type(const object_type& rhs)
:object(rhs.object)
{}
virtual super_type& assign(const super_type& rhs)
{
if(this != &rhs)
{
auto other = dynamic_cast<const object_type*>(&rhs);
if(other)
object = other->object;
}
return *this;
}
virtual bool same(const super_type& rhs) const
{
return (dynamic_cast<const object_type*>(&rhs) != nullptr);
}
virtual super_type* clone() const
{
return new object_type(object);
}
T object;
}; //end struct object_type
public: public:
template<typename T> virtual ~content_interface() = default;
any(const T & obj)
: super_(new object_type<typename std::remove_reference<T>::type>(obj)) virtual const std::type_info& type() const noexcept = 0;
virtual content_interface* clone() const = 0;
};
template<typename Value>
class holder : public content_interface
{
holder& operator=(const holder&) = delete;
public:
holder(const Value& other)
: value(other)
{} {}
template<typename T> holder(Value&& other)
any(T && obj) : value(static_cast<Value&&>(other))
: super_(new object_type<typename std::remove_reference<T>::type>(std::move(obj)))
{} {}
public:
const std::type_info& type() const noexcept override
{
return typeid(Value);
}
content_interface* clone() const override
{
return new holder(value);
}
public:
Value value; //representation accessable for friend of any
};
public:
//constructors and destructor
any() noexcept;
any(const any& other);
any(any&& other) noexcept;
template<typename Value>
any(Value && value,
typename std::enable_if<!std::is_same<any&, Value>::value>::type * = nullptr,
typename std::enable_if<!std::is_const<Value>::value>::type* = nullptr)
: content_(new holder<typename std::decay<Value>::type>(static_cast<Value&&>(value)))
{
}
any();
any(const any&);
any(any&&);
~any(); ~any();
bool same(const any &) const; //assignments
any& operator=(const any&); any& operator=(const any& other);
any& operator=(any&&); any& operator=(any&& other) noexcept;
template<typename T> template<class Value>
any& operator=(T const &rhs) any& operator=(Value&& other)
{ {
T * obj = get<T>(); any(other).swap(*this);
if(nullptr == obj)
{
delete super_;
super_ = new object_type<T>(rhs);
}
else
*obj = rhs;
return *this; return *this;
} }
template<typename T> //modifiers
any & operator=(T && rhs) void clear() noexcept;
{ void swap(any& other) noexcept;
typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
type* obj = get<type>();
if(nullptr == obj)
{
delete super_;
super_ = new object_type<type>(std::move(rhs));
}
else
*obj = std::move(rhs);
return *this;
}
template<typename T> //observers
T * get() const bool empty() const noexcept;
{ const std::type_info& type() const noexcept;
if(super_)
{
typedef typename std::remove_const<T>::type type;
object_type<type>* obj = dynamic_cast<object_type<type>*>(super_);
if(obj) return &(obj->object);
}
return nullptr;
}
template<typename T>
operator T&() const
{
typedef typename std::remove_const<T>::type type;
type *obj = get<type>();
if(nullptr == obj)
throw std::bad_cast();
return *obj;
}
template<typename T>
operator T*() const
{
typedef typename std::remove_const<T>::type type;
return get<type>();
}
private: private:
super_type * super_; template<typename Value>
friend Value* any_cast(any*) noexcept;
private:
content_interface * content_;
}; };
// Non-member functions
inline void swap(any& x, any& y) noexcept
{
x.swap(y);
}
template<typename Value>
Value any_cast(const any& operand)
{
using value_type = typename std::remove_reference<Value>::type;
return any_cast<const value_type&>(const_cast<any&>(operand));
}
template<typename Value>
Value any_cast(any& operand)
{
using value_type = typename std::remove_reference<Value>::type;
auto value_ptr = any_cast<value_type>(&operand);
if (!value_ptr)
throw bad_any_cast();
using ref_type = typename std::conditional<std::is_reference<Value>::value, Value, typename std::add_lvalue_reference<Value>::type>::type;
return static_cast<ref_type>(*value_ptr);
}
template<typename Value>
Value any_cast(any && operand)
{
static_assert(std::is_rvalue_reference<Value&&>::value || std::is_const<typename ::std::remove_reference<Value>::type>::value, "nana::any_cast shall not be used for getting non-const reference to temporary objects");
return any_cast<Value>(operand);
}
template<typename Value>
const Value* any_cast(const any* operand) noexcept
{
return any_cast<Value>(const_cast<any*>(operand));
}
template<typename Value>
Value* any_cast(any* operand) noexcept
{
if (!operand)
return nullptr;
auto holder = dynamic_cast<any::holder<Value>*>(operand->content_);
return (holder ? &holder->value : nullptr);
}
}//end namespace nana }//end namespace nana
#endif #endif

View File

@ -43,6 +43,21 @@
#ifndef NANA_CXX_DEFINES_INCLUDED #ifndef NANA_CXX_DEFINES_INCLUDED
#define NANA_CXX_DEFINES_INCLUDED #define NANA_CXX_DEFINES_INCLUDED
//C++ language
#if defined(_MSC_VER)
# if (_MSC_VER < 1900)
# //Nana defines some macros for lack of support of keywords
# define _ALLOW_KEYWORD_MACROS
#
# define noexcept //no support of noexcept until Visual C++ 2015
# define constexpr //no support of constexpr until Visual C++ 2015
# endif
#elif defined(__GNUC__)
# if (__GNUC__ == 4 && __GNUC_MINOR__ < 6)
# define noexcept //no support of noexcept until GCC 4.6
# endif
#endif
// Select platform ...... // Select platform ......
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) //Microsoft Windows #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) //Microsoft Windows
#define NANA_WINDOWS #define NANA_WINDOWS

View File

@ -1,6 +1,6 @@
/* /*
* A Concepts Definition of Nana C++ Library * A Concepts Definition of Nana C++ Library
* Copyright(C) 2003-2013 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2003-2016 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
@ -22,7 +22,7 @@ namespace nana
class any_objective class any_objective
{ {
public: public:
virtual ~any_objective(){} virtual ~any_objective() = default;
template<typename Target> template<typename Target>
void anyobj(const Target& t) void anyobj(const Target& t)
@ -37,7 +37,7 @@ namespace nana
void anyobj(Target&& t) void anyobj(Target&& t)
{ {
nana::any * p = _m_anyobj(true); nana::any * p = _m_anyobj(true);
if(nullptr == 0) if(nullptr == p)
throw std::runtime_error("Nana.any_objective: Object does not exist"); throw std::runtime_error("Nana.any_objective: Object does not exist");
*p = std::move(t); *p = std::move(t);
@ -46,8 +46,7 @@ namespace nana
template<typename Target> template<typename Target>
Target * anyobj() const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match. Target * anyobj() const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match.
{ {
nana::any * p = _m_anyobj(false); return any_cast<Target>(_m_anyobj(false));
return (p ? p->get<Target>() : nullptr);
} }
private: private:
virtual nana::any* _m_anyobj(bool allocate_if_empty) const = 0; virtual nana::any* _m_anyobj(bool allocate_if_empty) const = 0;
@ -60,7 +59,7 @@ namespace nana
public: public:
typedef IndexType anyobj_index_t; ///< The type of index. It is available if Dimension is greater than 0. typedef IndexType anyobj_index_t; ///< The type of index. It is available if Dimension is greater than 0.
virtual ~any_objective(){} virtual ~any_objective() = default;
template<typename Target> template<typename Target>
void anyobj(anyobj_index_t i, const Target& t) void anyobj(anyobj_index_t i, const Target& t)
@ -83,8 +82,7 @@ namespace nana
template<typename Target> template<typename Target>
Target * anyobj(anyobj_index_t i) const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match. Target * anyobj(anyobj_index_t i) const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match.
{ {
nana::any * p = _m_anyobj(i, false); return any_cast<Target>(_m_anyobj(i, false));
return (p ? p->get<Target>() : nullptr);
} }
private: private:
virtual nana::any* _m_anyobj(anyobj_index_t i, bool allocate_if_empty) const = 0; virtual nana::any* _m_anyobj(anyobj_index_t i, bool allocate_if_empty) const = 0;
@ -121,8 +119,7 @@ namespace nana
template<typename Target> template<typename Target>
Target * anyobj(anyobj_index_t i0, anyobj_index_t i1) const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match. Target * anyobj(anyobj_index_t i0, anyobj_index_t i1) const ///< Retrieves the attached object. Returns a nullptr if empty or if the type not match.
{ {
nana::any * p = _m_anyobj(i0, i1, false); return any_cast<Target>(_m_anyobj(i0, i1, false));
return (p ? p->get<Target>() : nullptr);
} }
private: private:
virtual nana::any* _m_anyobj(anyobj_index_t i0, anyobj_index_t i1, bool allocate_if_empty) const = 0; virtual nana::any* _m_anyobj(anyobj_index_t i0, anyobj_index_t i1, bool allocate_if_empty) const = 0;

View File

@ -1,7 +1,7 @@
/* /*
* Platform Specification Implementation * Platform Specification Implementation
* Nana C++ Library(http://www.nanapro.org) * Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2014 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2003-2016 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
@ -34,7 +34,9 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include "msg_packet.hpp" #include "msg_packet.hpp"
#if defined(NANA_UNICODE)
#define NANA_USE_XFT
#if defined(NANA_USE_XFT)
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <iconv.h> #include <iconv.h>
#include <fstream> #include <fstream>
@ -45,7 +47,7 @@ namespace nana
namespace detail namespace detail
{ {
class msg_dispatcher; class msg_dispatcher;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
class conf class conf
{ {
public: public:
@ -76,7 +78,7 @@ namespace detail
bool italic; bool italic;
bool underline; bool underline;
bool strikeout; bool strikeout;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
XftFont * handle; XftFont * handle;
#else #else
XFontSet handle; XFontSet handle;
@ -99,7 +101,7 @@ namespace detail
unsigned tab_pixels; unsigned tab_pixels;
unsigned whitespace_pixels; unsigned whitespace_pixels;
}string; }string;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
XftDraw * xftdraw{nullptr}; XftDraw * xftdraw{nullptr};
XftColor xft_fgcolor; XftColor xft_fgcolor;
const std::string charset(const std::wstring& str, const std::string& strcode); const std::string charset(const std::wstring& str, const std::string& strcode);
@ -120,7 +122,7 @@ namespace detail
unsigned color_{ 0xFFFFFFFF }; unsigned color_{ 0xFFFFFFFF };
unsigned text_color_{ 0xFFFFFFFF }; unsigned text_color_{ 0xFFFFFFFF };
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
struct conv_tag struct conv_tag
{ {
iconv_t handle; iconv_t handle;

View File

@ -62,7 +62,7 @@ namespace nana
void selected(::nana::any & var) void selected(::nana::any & var)
{ {
auto vp = var.get<T>(); auto vp = any_cast<T>(&var);
T null_val; T null_val;
arg_categorize<T> arg(widget_, vp ? *vp : null_val); arg_categorize<T> arg(widget_, vp ? *vp : null_val);

View File

@ -1,7 +1,7 @@
/** /**
* A Combox Implementation * A Combox Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -85,8 +85,7 @@ namespace nana
template<typename T> template<typename T>
T * value_ptr() const T * value_ptr() const
{ {
auto p = _m_anyobj(false); return any_cast<T>(_m_anyobj(false));
return (p ? p->get<T>() : nullptr);
} }
template<typename T> template<typename T>
@ -96,7 +95,7 @@ namespace nana
if (nullptr == pany) if (nullptr == pany)
throw std::runtime_error("combox::item_proxy.value<T>() is empty"); throw std::runtime_error("combox::item_proxy.value<T>() is empty");
T * p = pany->get<T>(); T * p = any_cast<T>(pany);
if (nullptr == p) if (nullptr == p)
throw std::runtime_error("combox::item_proxy.value<T>() invalid type of value"); throw std::runtime_error("combox::item_proxy.value<T>() invalid type of value");
return *p; return *p;

View File

@ -1,7 +1,7 @@
/** /**
* A List Box Implementation * A List Box Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -277,8 +277,7 @@ namespace nana
template<typename T> template<typename T>
T* value_ptr() const T* value_ptr() const
{ {
auto * pany = _m_value(); return any_cast<T>(_m_value());
return (pany ? pany->get<T>() : nullptr);
} }
template<typename T> template<typename T>
@ -288,7 +287,7 @@ namespace nana
if(nullptr == pany) if(nullptr == pany)
throw std::runtime_error("listbox::item_proxy.value<T>() is empty"); throw std::runtime_error("listbox::item_proxy.value<T>() is empty");
T * p = pany->get<T>(); T * p = any_cast<T>(_m_value());
if(nullptr == p) if(nullptr == p)
throw std::runtime_error("listbox::item_proxy.value<T>() invalid type of value"); throw std::runtime_error("listbox::item_proxy.value<T>() invalid type of value");
return *p; return *p;

View File

@ -1,7 +1,7 @@
/** /**
* A Tabbar implementation * A Tabbar implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -222,7 +222,7 @@ namespace nana
value_type & operator[](std::size_t pos) const value_type & operator[](std::size_t pos) const
{ {
return static_cast<value_type&>(this->get_drawer_trigger().at_no_bound_check(pos)); return any_cast<value_type&>(this->get_drawer_trigger().at_no_bound_check(pos));
} }
void activated(std::size_t pos) /// Activates a tab specified by pos. void activated(std::size_t pos) /// Activates a tab specified by pos.

View File

@ -1,7 +1,7 @@
/** /**
* A Tree Box Implementation * A Tree Box Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -288,15 +288,15 @@ namespace nana
bool operator!=(const item_proxy&) const; bool operator!=(const item_proxy&) const;
template<typename T> template<typename T>
T * value_ptr() const const T * value_ptr() const
{ {
return _m_value().get<T>(); return any_cast<T>(&_m_value());
} }
template<typename T> template<typename T>
T& value() const const T& value() const
{ {
T* p = _m_value().get<T>(); auto p = any_cast<T>(&_m_value());
if(nullptr == p) if(nullptr == p)
throw std::runtime_error("treebox::value<T>() Invalid type of value."); throw std::runtime_error("treebox::value<T>() Invalid type of value.");
return *p; return *p;

View File

@ -1,70 +1,87 @@
#include <nana/any.hpp> /**
* Any
* Nana C++ Library(http://www.nanapro.org)
* 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
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file nana/any.cpp
*
* @brief An implementation of experimental library any of C++ standard(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4480.html#any)
*/
#include <nana/any.hpp>
#include <utility>
namespace nana namespace nana
{ {
//constructors and destructor
//class any any::any() noexcept
//struct super_type : content_(nullptr)
any::super_type::~super_type(){}
any::super_type& any::super_type::operator=(const super_type &rhs)
{ {
return assign(rhs);
} }
//end struct super_type
any::any() any::any(const any& other)
:super_(nullptr) : content_(other.content_ ? other.content_->clone() : nullptr)
{} {}
any::any(const any& rhs) any::any(any && other) noexcept
:super_(rhs.super_ ? rhs.super_->clone() : nullptr) : content_(other.content_)
{}
any::any(any&& r)
:super_(r.super_)
{ {
r.super_ = nullptr; other.content_ = nullptr;
} }
any::~any() any::~any()
{ {
delete super_; delete content_;
} }
any& any::operator=(const any& rhs) //assignments
any& any::operator=(const any& other)
{ {
if(this != &rhs) if (this != &other)
any(other).swap(*this);
return *this;
}
any& any::operator=(any&& other) noexcept
{ {
delete super_; if (this != &other)
super_ = (rhs.super_ ? rhs.super_->clone() : nullptr); {
other.swap(*this);
other.clear();
} }
return *this; return *this;
} }
any& any::operator=(any&& r) //modifiers
void any::clear() noexcept
{ {
if(this != &r) if (content_)
{ {
delete super_; auto cnt = content_;
super_ = r.super_; content_ = nullptr;
r.super_ = nullptr; delete cnt;
} }
return *this;
} }
bool any::same(const any &rhs) const void any::swap(any& other) noexcept
{ {
if(this != &rhs) std::swap(content_, other.content_);
}
//observers
bool any::empty() const noexcept
{ {
if(super_ && rhs.super_) return (nullptr == content_);
return super_->same(*rhs.super_);
else if(super_ || rhs.super_)
return false;
} }
return true;
const std::type_info& any::type() const noexcept
{
return (content_ ? content_->type() : typeid(void));
} }
//end class any
}//end namespace nana }//end namespace nana

View File

@ -1,7 +1,7 @@
/* /*
* Platform Specification Implementation * Platform Specification Implementation
* Nana C++ Library(http://www.nanapro.org) * 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 Nana Software License, Version 1.0. * Distributed under the Nana 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
@ -36,7 +36,7 @@ namespace nana
namespace detail namespace detail
{ {
typedef native_window_type native_window_type; typedef native_window_type native_window_type;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
//class conf //class conf
conf::conf(const char * file) conf::conf(const char * file)
{ {
@ -290,7 +290,7 @@ namespace detail
string.tab_length = 4; string.tab_length = 4;
string.tab_pixels = 0; string.tab_pixels = 0;
string.whitespace_pixels = 0; string.whitespace_pixels = 0;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
conv_.handle = ::iconv_open("UTF-8", "UTF-32"); conv_.handle = ::iconv_open("UTF-8", "UTF-32");
conv_.code = "UTF-32"; conv_.code = "UTF-32";
#endif #endif
@ -298,7 +298,7 @@ namespace detail
drawable_impl_type::~drawable_impl_type() drawable_impl_type::~drawable_impl_type()
{ {
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
::iconv_close(conv_.handle); ::iconv_close(conv_.handle);
#endif #endif
} }
@ -366,7 +366,7 @@ namespace detail
::XSetForeground(spec.open_display(), context, col); ::XSetForeground(spec.open_display(), context, col);
::XSetBackground(spec.open_display(), context, col); ::XSetBackground(spec.open_display(), context, col);
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
xft_fgcolor.color.red = ((0xFF0000 & col) >> 16) * 0x101; xft_fgcolor.color.red = ((0xFF0000 & col) >> 16) * 0x101;
xft_fgcolor.color.green = ((0xFF00 & col) >> 8) * 0x101; xft_fgcolor.color.green = ((0xFF00 & col) >> 8) * 0x101;
xft_fgcolor.color.blue = (0xFF & col) * 0x101; xft_fgcolor.color.blue = (0xFF & col) * 0x101;
@ -395,7 +395,7 @@ namespace detail
} }
::XSetForeground(spec.open_display(), context, rgb); ::XSetForeground(spec.open_display(), context, rgb);
::XSetBackground(spec.open_display(), context, rgb); ::XSetBackground(spec.open_display(), context, rgb);
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
xft_fgcolor.color.red = ((0xFF0000 & rgb) >> 16) * 0x101; xft_fgcolor.color.red = ((0xFF0000 & rgb) >> 16) * 0x101;
xft_fgcolor.color.green = ((0xFF00 & rgb) >> 8) * 0x101; xft_fgcolor.color.green = ((0xFF00 & rgb) >> 8) * 0x101;
xft_fgcolor.color.blue = (0xFF & rgb) * 0x101; xft_fgcolor.color.blue = (0xFF & rgb) * 0x101;
@ -412,7 +412,7 @@ namespace detail
if(fp && fp->handle) if(fp && fp->handle)
{ {
platform_scope_guard psg; platform_scope_guard psg;
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
::XftFontClose(nana::detail::platform_spec::instance().open_display(), fp->handle); ::XftFontClose(nana::detail::platform_spec::instance().open_display(), fp->handle);
#else #else
::XFreeFontSet(nana::detail::platform_spec::instance().open_display(), fp->handle); ::XFreeFontSet(nana::detail::platform_spec::instance().open_display(), fp->handle);

View File

@ -1,7 +1,7 @@
/* /*
* A Bedrock Implementation * A Bedrock Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -993,7 +993,7 @@ namespace detail
if(input_context) if(input_context)
{ {
nana::detail::platform_scope_guard psg; nana::detail::platform_scope_guard psg;
#if defined(NANA_UNICODE) #if 1 //Utf8
len = ::Xutf8LookupString(input_context, &xevent.xkey, keybuf, 32, &keysym, &status); len = ::Xutf8LookupString(input_context, &xevent.xkey, keybuf, 32, &keysym, &status);
if(status == XBufferOverflow) if(status == XBufferOverflow)
{ {
@ -1088,14 +1088,12 @@ namespace detail
if (msgwnd->flags.enabled) if (msgwnd->flags.enabled)
{ {
const wchar_t* charbuf; const wchar_t* charbuf;
#if defined(NANA_UNICODE)
nana::detail::charset_conv charset("UTF-32", "UTF-8"); nana::detail::charset_conv charset("UTF-32", "UTF-8");
const std::string& str = charset.charset(std::string(keybuf, keybuf + len)); const std::string& str = charset.charset(std::string(keybuf, keybuf + len));
charbuf = reinterpret_cast<const wchar_t*>(str.c_str()) + 1; charbuf = reinterpret_cast<const wchar_t*>(str.c_str()) + 1;
len = str.size() / sizeof(wchar_t) - 1; len = str.size() / sizeof(wchar_t) - 1;
#else
charbuf = keybuf;
#endif
for(int i = 0; i < len; ++i) for(int i = 0; i < len; ++i)
{ {
arg_keyboard arg; arg_keyboard arg;

View File

@ -202,8 +202,8 @@ namespace nana
ls_file_.events().mouse_down.connect_unignorable(fn_sel_file); ls_file_.events().mouse_down.connect_unignorable(fn_sel_file);
ls_file_.set_sort_compare(0, [](const std::string& a, nana::any* fs_a, const std::string& b, nana::any* fs_b, bool reverse) -> bool ls_file_.set_sort_compare(0, [](const std::string& a, nana::any* fs_a, const std::string& b, nana::any* fs_b, bool reverse) -> bool
{ {
int dira = fs_a->get<item_fs>()->directory ? 1 : 0; int dira = any_cast<item_fs>(fs_a)->directory ? 1 : 0;
int dirb = fs_b->get<item_fs>()->directory ? 1 : 0; int dirb = any_cast<item_fs>(fs_b)->directory ? 1 : 0;
if(dira != dirb) if(dira != dirb)
return (reverse ? dira < dirb : dira > dirb); return (reverse ? dira < dirb : dira > dirb);
@ -256,8 +256,8 @@ namespace nana
}); });
ls_file_.set_sort_compare(2, [](const std::string& a, nana::any* anyptr_a, const std::string& b, nana::any* anyptr_b, bool reverse) -> bool ls_file_.set_sort_compare(2, [](const std::string& a, nana::any* anyptr_a, const std::string& b, nana::any* anyptr_b, bool reverse) -> bool
{ {
int dir1 = anyptr_a->get<item_fs>()->directory ? 1 : 0; int dir1 = any_cast<item_fs>(anyptr_a)->directory ? 1 : 0;
int dir2 = anyptr_b->get<item_fs>()->directory ? 1 : 0; int dir2 = any_cast<item_fs>(anyptr_b)->directory ? 1 : 0;
if(dir1 != dir2) if(dir1 != dir2)
return (reverse ? dir1 < dir2 : dir1 > dir2); return (reverse ? dir1 < dir2 : dir1 > dir2);
@ -265,8 +265,8 @@ namespace nana
}); });
ls_file_.set_sort_compare(3, [this](const std::string&, nana::any* anyptr_a, const std::string&, nana::any* anyptr_b, bool reverse) -> bool ls_file_.set_sort_compare(3, [this](const std::string&, nana::any* anyptr_a, const std::string&, nana::any* anyptr_b, bool reverse) -> bool
{ {
item_fs * fsa = anyptr_a->get<item_fs>(); item_fs * fsa = any_cast<item_fs>(anyptr_a);
item_fs * fsb = anyptr_b->get<item_fs>(); item_fs * fsb = any_cast<item_fs>(anyptr_b);
return (reverse ? fsa->bytes > fsb->bytes : fsa->bytes < fsb->bytes); return (reverse ? fsa->bytes > fsb->bytes : fsa->bytes < fsb->bytes);
}); });

View File

@ -1,7 +1,7 @@
/* /*
* A List Box Implementation * A List Box Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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

View File

@ -1,7 +1,7 @@
/* /*
* Platform Implementation * Platform Implementation
* Nana C++ Library(http://www.nanapro.org) * 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. * 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
@ -137,7 +137,7 @@ namespace detail
if(::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size)) if(::GetTextExtentPoint32(dw->context, text, static_cast<int>(len), &size))
return nana::size(size.cx, size.cy); return nana::size(size.cx, size.cy);
#elif defined(NANA_X11) #elif defined(NANA_X11)
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
std::string utf8str = to_utf8(std::wstring(text, len)); std::string utf8str = to_utf8(std::wstring(text, len));
XGlyphInfo ext; XGlyphInfo ext;
XftFont * fs = reinterpret_cast<XftFont*>(dw->font->handle); XftFont * fs = reinterpret_cast<XftFont*>(dw->font->handle);
@ -179,7 +179,7 @@ namespace detail
::TextOut(dw->context, pos.x, pos.y, str, static_cast<int>(len)); ::TextOut(dw->context, pos.x, pos.y, str, static_cast<int>(len));
#elif defined(NANA_X11) #elif defined(NANA_X11)
auto disp = ::nana::detail::platform_spec::instance().open_display(); auto disp = ::nana::detail::platform_spec::instance().open_display();
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
auto fs = reinterpret_cast<XftFont*>(dw->font->handle); auto fs = reinterpret_cast<XftFont*>(dw->font->handle);
//Fixed missing array declaration by dareg //Fixed missing array declaration by dareg

View File

@ -1,7 +1,7 @@
/* /*
* Paint Graphics Implementation * Paint Graphics Implementation
* Nana C++ Library(http://www.nanapro.org) * Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2014 Jinhao(cnjinhao@hotmail.com) * Copyright(C) 2003-2016 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
@ -40,7 +40,7 @@ namespace paint
if(p) if(p)
{ {
Display* disp = reinterpret_cast<Display*>(nana::detail::platform_spec::instance().open_display()); Display* disp = reinterpret_cast<Display*>(nana::detail::platform_spec::instance().open_display());
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
::XftDrawDestroy(p->xftdraw); ::XftDrawDestroy(p->xftdraw);
#endif #endif
::XFreeGC(disp, p->context); ::XFreeGC(disp, p->context);
@ -306,7 +306,7 @@ namespace paint
Window root = ::XRootWindow(disp, screen); Window root = ::XRootWindow(disp, screen);
dw->pixmap = ::XCreatePixmap(disp, root, (sz.width ? sz.width : 1), (sz.height ? sz.height : 1), DefaultDepth(disp, screen)); dw->pixmap = ::XCreatePixmap(disp, root, (sz.width ? sz.width : 1), (sz.height ? sz.height : 1), DefaultDepth(disp, screen));
dw->context = ::XCreateGC(disp, dw->pixmap, 0, 0); dw->context = ::XCreateGC(disp, dw->pixmap, 0, 0);
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
dw->xftdraw = ::XftDrawCreate(disp, dw->pixmap, spec.screen_visual(), spec.colormap()); dw->xftdraw = ::XftDrawCreate(disp, dw->pixmap, spec.screen_visual(), spec.colormap());
#endif #endif
#endif #endif
@ -442,7 +442,8 @@ namespace paint
pxbuf[i] = (str[i] == '\t' ? tab_pixels : dx[i] - dx[i - 1]); pxbuf[i] = (str[i] == '\t' ? tab_pixels : dx[i] - dx[i - 1]);
} }
delete [] dx; delete [] dx;
#elif defined(NANA_X11) #elif defined(NANA_X11) && defined(NANA_USE_XFT)
Display * disp = nana::detail::platform_spec::instance().open_display(); Display * disp = nana::detail::platform_spec::instance().open_display();
XftFont * xft = handle_->font->handle; XftFont * xft = handle_->font->handle;
@ -465,7 +466,6 @@ namespace paint
nana::size graphics::bidi_extent_size(const std::wstring& str) const nana::size graphics::bidi_extent_size(const std::wstring& str) const
{ {
nana::size sz; nana::size sz;
#if defined NANA_UNICODE
if(handle_ && handle_->context && str.size()) if(handle_ && handle_->context && str.size())
{ {
std::vector<unicode_bidi::entity> reordered; std::vector<unicode_bidi::entity> reordered;
@ -479,7 +479,6 @@ namespace paint
sz.height = t.height; sz.height = t.height;
} }
} }
#endif
return sz; return sz;
} }
@ -502,7 +501,7 @@ namespace paint
#elif defined(NANA_X11) #elif defined(NANA_X11)
if(handle_->font) if(handle_->font)
{ {
#if defined(NANA_UNICODE) #if defined(NANA_USE_XFT)
XftFont * fs = reinterpret_cast<XftFont*>(handle_->font->handle); XftFont * fs = reinterpret_cast<XftFont*>(handle_->font->handle);
ascent = fs->ascent; ascent = fs->ascent;
descent = fs->descent; descent = fs->descent;