add new platform_abstraction

add support of loading ttf file
This commit is contained in:
Jinhao
2017-04-26 08:04:25 +08:00
parent 4aba8e8c7f
commit 2ff8a1c729
42 changed files with 876 additions and 398 deletions

View File

@@ -0,0 +1,205 @@
/*
* Platform Specification Implementation
* 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/detail/platform_spec.hpp
*
* This file provides basis class and data structrue that required by nana
* This file should not be included by any header files.
*/
#if defined(NANA_WINDOWS)
#ifndef NANA_DETAIL_PLATFORM_SPEC_HPP
#define NANA_DETAIL_PLATFORM_SPEC_HPP
#include <nana/gui/basis.hpp>
#include <nana/paint/image.hpp>
#include <nana/gui/detail/event_code.hpp>
#include <windows.h>
#include <memory>
#include <functional>
#include "../platform_abstraction_types.hpp"
namespace nana
{
namespace detail
{
//struct messages
//@brief: This defines some messages that are used for remote thread invocation.
// Some Windows APIs are window-thread-dependent, the operation in other thread
// must be posted to its own thread.
struct messages
{
struct caret
{
int x;
int y;
unsigned width;
unsigned height;
bool visible;
};
struct move_window
{
enum { Pos = 1, Size = 2};
int x;
int y;
unsigned width;
unsigned height;
unsigned ignore; //determinate that pos or size would be ignored.
};
struct map_thread
{
rectangle update_area;
bool ignore_update_area;
bool forced;
};
struct arg_affinity_execute
{
const std::function<void()> * function_ptr;
};
enum
{
tray = 0x501,
async_activate,
async_set_focus,
remote_flush_surface,
remote_thread_destroy_window,
remote_thread_move_window,
operate_caret, //wParam: 1=Destroy, 2=SetPos
remote_thread_set_window_pos,
remote_thread_set_window_text,
//Execute a function in a thread with is associated with a specified native window
affinity_execute,
user,
};
};
struct drawable_impl_type
{
using font_type = ::std::shared_ptr<font_interface>;
HDC context;
HBITMAP pixmap;
pixel_argb_t* pixbuf_ptr{nullptr};
std::size_t bytes_per_line{0};
font_type font;
struct pen_spec
{
HPEN handle;
unsigned color;
int style;
int width;
void set(HDC context, int style, int width,unsigned color);
}pen;
struct brush_spec
{
enum t{Solid, HatchBDiagonal};
HBRUSH handle;
t style;
unsigned color;
void set(HDC context, t style, unsigned color);
}brush;
struct round_region_spec
{
HRGN handle;
nana::rectangle r;
unsigned radius_x;
unsigned radius_y;
void set(const nana::rectangle& r, unsigned radius_x, unsigned radius_y);
}round_region;
struct string_spec
{
unsigned tab_length;
unsigned tab_pixels;
unsigned whitespace_pixels;
}string;
drawable_impl_type(const drawable_impl_type&) = delete;
drawable_impl_type& operator=(const drawable_impl_type&) = delete;
drawable_impl_type();
~drawable_impl_type();
void fgcolor(const ::nana::color&); //deprecated
unsigned get_color() const;
unsigned get_text_color() const;
void set_color(const ::nana::color&);
void set_text_color(const ::nana::color&);
void update_pen();
void update_brush();
private:
unsigned color_{ 0xffffffff };
unsigned text_color_{0xffffffff};
};
class platform_spec
{
platform_spec();
platform_spec(const platform_spec&) = delete;
platform_spec& operator=(const platform_spec&) = delete;
platform_spec(platform_spec&&) = delete;
platform_spec& operator=(platform_spec&&) = delete;
public:
typedef ::nana::event_code event_code;
typedef ::nana::native_window_type native_window_type;
class co_initializer
{
public:
co_initializer();
~co_initializer();
private:
HMODULE ole32_;
};
struct window_icons
{
::nana::paint::image sml_icon;
::nana::paint::image big_icon;
};
~platform_spec();
static platform_spec& instance();
void keep_window_icon(native_window_type, const paint::image&sml_icon, const paint::image& big_icon);
void release_window_icon(native_window_type);
private:
struct implementation;
implementation * const impl_;
};
}//end namespace detail
}//end namespace nana
// .h ward
#endif
//#if defined(NANA_WINDOWS)
#endif

View File

@@ -0,0 +1,409 @@
#include "platform_abstraction.hpp"
#include <nana/deploy.hpp>
#include "../paint/truetype.hpp"
#ifdef NANA_WINDOWS
# include <windows.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************************
* *
* VersionHelpers.h -- This module defines helper functions to *
* promote version check with proper *
* comparisons. *
* *
* Copyright (c) Microsoft Corp. All rights reserved. *
* *
******************************************************************/
#include <specstrings.h> // for _In_, etc.
#if !defined(__midl) && !defined(SORTPP_PASS)
#if (NTDDI_VERSION >= NTDDI_WINXP)
#ifdef __cplusplus
#define VERSIONHELPERAPI inline bool
#else // __cplusplus
#define VERSIONHELPERAPI FORCEINLINE BOOL
#endif // __cplusplus
VERSIONHELPERAPI
IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
{
OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, { 0 }, 0, 0 };
DWORDLONG const dwlConditionMask = VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
0, VER_MAJORVERSION, VER_GREATER_EQUAL),
VER_MINORVERSION, VER_GREATER_EQUAL),
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
osvi.dwMajorVersion = wMajorVersion;
osvi.dwMinorVersion = wMinorVersion;
osvi.wServicePackMajor = wServicePackMajor;
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
}
VERSIONHELPERAPI
IsWindowsXPOrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 0);
}
VERSIONHELPERAPI
IsWindowsXPSP1OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 1);
}
VERSIONHELPERAPI
IsWindowsXPSP2OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 2);
}
VERSIONHELPERAPI
IsWindowsXPSP3OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 3);
}
VERSIONHELPERAPI
IsWindowsVistaOrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
}
VERSIONHELPERAPI
IsWindowsVistaSP1OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 1);
}
VERSIONHELPERAPI
IsWindowsVistaSP2OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 2);
}
VERSIONHELPERAPI
IsWindows7OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
}
VERSIONHELPERAPI
IsWindows7SP1OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 1);
}
#ifndef _WIN32_WINNT_WIN8 // (0x0602)
#define _WIN32_WINNT_WIN8 (0x0602)
#endif // _WIN32_WINNT_WIN8(0x0602)
VERSIONHELPERAPI
IsWindows8OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
}
#ifndef _WIN32_WINNT_WINBLUE // (0x0602)
#define _WIN32_WINNT_WINBLUE (0x0602)
#endif // _WIN32_WINNT_WINBLUE (0x0602)
VERSIONHELPERAPI
IsWindows8Point1OrGreater()
{
return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINBLUE), LOBYTE(_WIN32_WINNT_WINBLUE), 0);
}
VERSIONHELPERAPI
IsWindowsServer()
{
OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, { 0 }, 0, 0, 0, VER_NT_WORKSTATION };
DWORDLONG const dwlConditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL);
return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
}
#endif // NTDDI_VERSION
#endif // defined(__midl)
#else
# include "posix/platform_spec.hpp"
# include <fontconfig/fontconfig.h>
# if defined(NANA_USE_XFT)
# include <X11/Xft/Xft.h>
# include <iconv.h>
# include <fstream>
# endif
#endif
namespace nana
{
class internal_font
: public font_interface
{
public:
using path_type = std::experimental::filesystem::path;
internal_font(const path_type& ttf, const std::string& font_family, double font_size, const font_style& fs, native_font_type native_font):
ttf_(ttf),
family_(font_family),
size_(font_size),
style_(fs),
native_handle_(native_font)
{}
~internal_font()
{
#ifdef NANA_WINDOWS
::DeleteObject(reinterpret_cast<HFONT>(native_handle_));
#elif defined(NANA_X11)
auto disp = ::nana::detail::platform_spec::instance().open_display();
# ifdef NANA_USE_XFT
::XftFontClose(disp, reinterpret_cast<XftFont*>(native_handle_));
# else
::XFreeFontSet(disp, reinterpret_cast<XFontSet>(native_handle_));
# endif
#endif
if (!ttf_.empty())
platform_abstraction::font_resource(false, ttf_);
}
public:
const std::string& family() const override
{
return family_;
}
double size() const override
{
return size_;
}
const font_style & style() const override
{
return style_;
}
native_font_type native_handle() const override
{
return native_handle_;
}
private:
path_type const ttf_;
std::string const family_;
double const size_;
font_style const style_;
native_font_type const native_handle_;
};
struct platform_runtime
{
std::shared_ptr<font_interface> font;
#ifdef NANA_X11
std::map<std::string, std::size_t> fontconfig_counts;
#endif
};
namespace
{
namespace data
{
static platform_runtime* storage;
}
}
static platform_runtime& platform_storage()
{
if (nullptr == data::storage)
throw std::runtime_error("platform_abstraction is empty");
return *data::storage;
}
void platform_abstraction::initialize()
{
if (nullptr == data::storage)
data::storage = new platform_runtime;
}
void platform_abstraction::shutdown()
{
auto & r = platform_storage();
if (r.font.use_count() > 1)
throw std::runtime_error("platform_abstraction is disallowed to shutdown");
r.font.reset();
delete data::storage;
data::storage = nullptr;
}
::std::shared_ptr<platform_abstraction::font> platform_abstraction::default_font(const ::std::shared_ptr<font>& new_font)
{
auto & r = platform_storage();
if (new_font)
{
auto f = r.font;
if (new_font != r.font)
r.font = new_font;
return f;
}
if (!r.font)
r.font = make_font({}, 0, {});
return r.font;
}
static std::shared_ptr<platform_abstraction::font> font_factory(::std::string font_family, double size_pt, const platform_abstraction::font::font_style& fs, internal_font::path_type ttf)
{
using native_font_type = platform_abstraction::font::native_font_type;
#ifdef NANA_WINDOWS
std::wstring wfont_family = to_nstring(font_family);
//Make sure the length of font family less than LF_FACESIZE which is defined by Windows
if (wfont_family.length() + 1 > LF_FACESIZE)
wfont_family.clear();
//Translate pt to px
auto hDC = ::GetDC(nullptr);
auto font_height = -static_cast<LONG>(size_pt * ::GetDeviceCaps(hDC, LOGPIXELSY) / 72);
::ReleaseDC(nullptr, hDC);
if (wfont_family.empty() || (0 == font_height))
{
//Create default font object.
NONCLIENTMETRICS metrics = {};
metrics.cbSize = sizeof metrics;
#if(WINVER >= 0x0600)
#if defined(NANA_MINGW)
OSVERSIONINFO osvi = {};
osvi.dwOSVersionInfoSize = sizeof(osvi);
::GetVersionEx(&osvi);
if (osvi.dwMajorVersion < 6)
metrics.cbSize -= sizeof(metrics.iPaddedBorderWidth);
#else
if (!IsWindowsVistaOrGreater())
metrics.cbSize -= sizeof(metrics.iPaddedBorderWidth);
#endif
#endif
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof metrics, &metrics, 0);
if (wfont_family.empty())
{
wfont_family = metrics.lfMessageFont.lfFaceName;
font_family = to_utf8(wfont_family);
}
if (0 == font_height)
font_height = metrics.lfMessageFont.lfHeight;
}
::LOGFONT lf{};
std::wcscpy(lf.lfFaceName, wfont_family.c_str());
lf.lfHeight = font_height;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfWeight = fs.weight;
lf.lfQuality = PROOF_QUALITY;
lf.lfPitchAndFamily = FIXED_PITCH;
lf.lfItalic = fs.italic;
lf.lfUnderline = fs.underline;
lf.lfStrikeOut = fs.strike_out;
auto fd = ::CreateFontIndirect(&lf);
#elif defined(NANA_X11)
auto disp = ::nana::detail::platform_spec::instance().open_display();
# ifdef NANA_USE_XFT
if(font_family.empty())
font_family = '*';
std::string pat_str = font_family + '-' + std::to_string(size_pt ? size_pt : 10);
auto pat = ::XftNameParse(pat_str.c_str());
XftResult res;
auto match_pat = ::XftFontMatch(disp, ::XDefaultScreen(disp), pat, &res);
::XftFont* fd = nullptr;
if (match_pat)
fd = ::XftFontOpenPattern(disp, match_pat);
# else
std::string pat_str;
if (font_family.empty())
pat_str = "-misc-fixed-*";
else
pat_str = "-misc-fixed-" + font_family;
char ** missing_list;
int missing_count;
char * defstr;
XFontSet fd = ::XCreateFontSet(display_, const_cast<char*>(pat_str.c_str()), &missing_list, &missing_count, &defstr);
# endif
#endif
if (fd)
return std::make_shared<internal_font>(std::move(ttf), std::move(font_family), size_pt, fs, reinterpret_cast<native_font_type>(fd));
return{};
}
::std::shared_ptr<platform_abstraction::font> platform_abstraction::make_font(const std::string& font_family, double size_pt, const font::font_style& fs)
{
return font_factory(font_family, size_pt, fs, {});
}
::std::shared_ptr<platform_abstraction::font> platform_abstraction::make_font_from_ttf(const path_type& ttf, double size_pt, const font::font_style& fs)
{
::nana::spec::truetype truetype{ ttf };
if (truetype.font_family().empty())
return nullptr;
font_resource(true, ttf);
return font_factory(truetype.font_family(), size_pt, fs, ttf);
}
void platform_abstraction::font_resource(bool try_add, const path_type& ttf)
{
#ifdef NANA_WINDOWS
if (try_add)
::AddFontResourceEx(ttf.wstring().c_str(), FR_PRIVATE, nullptr);
else
::RemoveFontResourceEx(ttf.wstring().c_str(), FR_PRIVATE, nullptr);
#else
auto & fc = platform_storage().fontconfig_counts;
if(try_add)
{
if(1 == ++(fc[ttf.string()]))
{
auto state = ::FcConfigAppFontAddFile(nullptr, reinterpret_cast<const FcChar8*>(ttf.string().c_str()));
state = 0;
}
}
else
{
auto i = fc.find(ttf.string());
if(i != fc.end())
{
if(0 == --(i->second))
fc.erase(i);
if(0 == fc.size())
::FcConfigAppFontClear(nullptr);
}
}
#endif
}
}

View File

@@ -0,0 +1,43 @@
/*
* Platform Abstraction
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2017 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/detail/platform_spec.hpp
*
* The platform_abstraction provides some functions and types for the abstract
* system platform.
*/
#ifndef NANA_DETAIL_PLATFORM_ABSTRACTION_HEADER_INCLUDED
#define NANA_DETAIL_PLATFORM_ABSTRACTION_HEADER_INCLUDED
#include "platform_abstraction_types.hpp"
#include <memory>
#include <nana/filesystem/filesystem.hpp>
namespace nana
{
class platform_abstraction
{
public:
using font = font_interface;
using path_type = ::std::experimental::filesystem::path;
static void initialize();
/// Shutdown before destruction of platform_spec
static void shutdown();
static ::std::shared_ptr<font> default_font(const ::std::shared_ptr<font>&);
static ::std::shared_ptr<font> make_font(const ::std::string& font_family, double size_pt, const font::font_style& fs);
static ::std::shared_ptr<font> make_font_from_ttf(const path_type& ttf, double size_pt, const font::font_style& fs);
static void font_resource(bool try_add, const path_type& ttf);
};
}
#endif

View File

@@ -0,0 +1,29 @@
#ifndef NANA_DETAIL_PLATFORM_ABSTRACTION_TYPES_HEADER_INCLUDED
#define NANA_DETAIL_PLATFORM_ABSTRACTION_TYPES_HEADER_INCLUDED
#include <nana/config.hpp>
#include <nana/paint/detail/ptdefs.hpp>
#include <string>
#ifdef NANA_X11
# define NANA_USE_XFT
#endif
namespace nana
{
class font_interface
{
public:
using font_style = detail::font_style;
using native_font_type = paint::native_font_type;
virtual ~font_interface() = default;
virtual const std::string& family() const = 0;
virtual double size() const = 0;
virtual const font_style & style() const = 0;
virtual native_font_type native_handle() const = 0;
};
}
#endif

View File

@@ -14,7 +14,8 @@
* http://standards.freedesktop.org/clipboards-spec/clipboards-0.1.txt
*/
#include <nana/detail/platform_spec_selector.hpp>
#include "platform_spec_selector.hpp"
#include "platform_abstraction.hpp"
#if defined(NANA_POSIX) && defined(NANA_X11)
#include <nana/push_ignore_diagnostic>
@@ -33,7 +34,7 @@
#include <errno.h>
#include <sstream>
#include "x11/msg_dispatcher.hpp"
#include "posix/msg_dispatcher.hpp"
namespace nana
{
@@ -451,24 +452,6 @@ namespace detail
}
}
class font_deleter
{
public:
void operator()(const font_tag* fp) const
{
if(fp && fp->handle)
{
platform_scope_guard psg;
#if defined(NANA_USE_XFT)
::XftFontClose(nana::detail::platform_spec::instance().open_display(), fp->handle);
#else
::XFreeFontSet(nana::detail::platform_spec::instance().open_display(), fp->handle);
#endif
}
delete fp;
}
};//end class font_deleter
platform_scope_guard::platform_scope_guard()
{
platform_spec::instance().lock_xlib();
@@ -561,9 +544,9 @@ namespace detail
atombase_.xdnd_typelist = ::XInternAtom(display_, "XdndTypeList", False);
atombase_.xdnd_finished = ::XInternAtom(display_, "XdndFinished", False);
//Create default font object.
def_font_ptr_ = make_native_font(nullptr, font_size_to_height(10), 400, false, false, false);
msg_dispatcher_ = new msg_dispatcher(display_);
platform_abstraction::initialize();
}
platform_spec::~platform_spec()
@@ -572,75 +555,11 @@ namespace detail
//The font should be destroyed before closing display,
//otherwise it crashs
def_font_ptr_.reset();
platform_abstraction::shutdown();
close_display();
}
const platform_spec::font_ptr_t& platform_spec::default_native_font() const
{
return def_font_ptr_;
}
void platform_spec::default_native_font(const font_ptr_t& fp)
{
def_font_ptr_ = fp;
}
unsigned platform_spec::font_size_to_height(unsigned size) const
{
return size;
}
unsigned platform_spec::font_height_to_size(unsigned height) const
{
return height;
}
platform_spec::font_ptr_t platform_spec::make_native_font(const char* name, unsigned height, unsigned weight, bool italic, bool underline, bool strike_out)
{
font_ptr_t ref;
#if 1 //Xft
if(0 == name || *name == 0)
name = "*";
XftFont* handle = 0;
std::stringstream ss;
ss<<name<<"-"<<(height ? height : 10);
XftPattern * pat = ::XftNameParse(ss.str().c_str());
XftResult res;
XftPattern * match_pat = ::XftFontMatch(display_, ::XDefaultScreen(display_), pat, &res);
if(match_pat)
handle = ::XftFontOpenPattern(display_, match_pat);
#else
std::string basestr;
if(0 == name || *name == 0)
{
basestr = "-misc-fixed-*";
}
else
basestr = "-misc-fixed-*";
char ** missing_list;
int missing_count;
char * defstr;
XFontSet handle = ::XCreateFontSet(display_, const_cast<char*>(basestr.c_str()), &missing_list, &missing_count, &defstr);
#endif
if(handle)
{
font_tag * impl = new font_tag;
impl->name = name;
impl->height = height;
impl->weight = weight;
impl->italic = italic;
impl->underline = underline;
impl->strikeout = strike_out;
impl->handle = handle;
return font_ptr_t(impl, font_deleter());
}
return font_ptr_t();
}
Display* platform_spec::open_display()
{
return display_;

View File

@@ -0,0 +1,21 @@
/**
* Selector of Platform Specification
* 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/detail/platform_spec_selector.hpp
*
* @brief Selects the proper platform_spec header file for the current platform
*/
#include <nana/config.hpp>
#if defined(NANA_WINDOWS)
#include "mswin/platform_spec.hpp"
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
#include "posix/platform_spec.hpp"
#endif

View File

@@ -12,15 +12,18 @@
* @brief basis classes and data structures required by nana
*/
#include <nana/detail/platform_spec_selector.hpp>
#include "platform_spec_selector.hpp"
#include "platform_abstraction.hpp"
#if defined(NANA_WINDOWS)
#include <stdexcept>
#include <map>
#include <windows.h>
#include <shellapi.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************************
@@ -310,15 +313,6 @@ namespace detail
}
}
//struct font_tag::deleter
void font_tag::deleter::operator()(const font_tag* tag) const
{
if(tag && tag->handle)
::DeleteObject(tag->handle);
delete tag;
}
//end struct font_tag::deleter
//class platform_spec
platform_spec::co_initializer::co_initializer()
: ole32_(::LoadLibrary(L"OLE32.DLL"))
@@ -354,104 +348,21 @@ namespace detail
struct platform_spec::implementation
{
font_ptr_t def_font_ptr;
std::map<native_window_type, window_icons> iconbase;
};
platform_spec::platform_spec()
: impl_{ new implementation}
{
//Create default font object.
NONCLIENTMETRICS metrics = {};
metrics.cbSize = sizeof metrics;
#if(WINVER >= 0x0600)
#if defined(NANA_MINGW)
OSVERSIONINFO osvi = {};
osvi.dwOSVersionInfoSize = sizeof(osvi);
::GetVersionEx(&osvi);
if (osvi.dwMajorVersion < 6)
metrics.cbSize -= sizeof(metrics.iPaddedBorderWidth);
#else
if(!IsWindowsVistaOrGreater())
metrics.cbSize -= sizeof(metrics.iPaddedBorderWidth);
#endif
#endif
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof metrics, &metrics, 0);
impl_->def_font_ptr = make_native_font(to_utf8(metrics.lfMessageFont.lfFaceName).c_str(), font_size_to_height(9), 400, false, false, false);
platform_abstraction::initialize();
}
platform_spec::~platform_spec()
{
platform_abstraction::shutdown();
delete impl_;
}
const platform_spec::font_ptr_t& platform_spec::default_native_font() const
{
return impl_->def_font_ptr;
}
void platform_spec::default_native_font(const font_ptr_t& fp)
{
impl_->def_font_ptr = fp;
}
unsigned platform_spec::font_size_to_height(unsigned size) const
{
HDC hdc = ::GetDC(0);
size = ::MulDiv(int(size), ::GetDeviceCaps(hdc, LOGPIXELSY), 72);
::ReleaseDC(0, hdc);
return size;
}
unsigned platform_spec::font_height_to_size(unsigned height) const
{
HDC hdc = ::GetDC(0);
unsigned pixels = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
height = static_cast<unsigned>(static_cast<long long>(72) * height / pixels);
return height;
}
platform_spec::font_ptr_t platform_spec::make_native_font(const char* name, unsigned height, unsigned weight, bool italic, bool underline, bool strike_out)
{
::LOGFONT logfont;
memset(&logfont, 0, sizeof logfont);
if (name && *name)
std::wcscpy(logfont.lfFaceName, to_wstring(name).c_str());
else
std::wcscpy(logfont.lfFaceName, impl_->def_font_ptr->name.c_str());
logfont.lfCharSet = DEFAULT_CHARSET;
HDC hdc = ::GetDC(0);
logfont.lfHeight = -static_cast<int>(height);
::ReleaseDC(0, hdc);
logfont.lfWidth = 0;
logfont.lfWeight = weight;
logfont.lfQuality = PROOF_QUALITY;
logfont.lfPitchAndFamily = FIXED_PITCH;
logfont.lfItalic = italic;
logfont.lfUnderline = underline;
logfont.lfStrikeOut = strike_out;
HFONT result = ::CreateFontIndirect(&logfont);
if(result)
{
font_tag * impl = new font_tag;
impl->name = logfont.lfFaceName;
impl->height = height;
impl->weight = weight;
impl->italic = italic;
impl->underline = underline;
impl->strikeout = strike_out;
impl->handle = result;
return std::shared_ptr<font_tag>(impl, font_tag::deleter());
}
return nullptr;
}
platform_spec& platform_spec::instance()
{
static platform_spec object;

View File

@@ -17,7 +17,7 @@
#ifndef NANA_DETAIL_MSG_DISPATCHER_HPP
#define NANA_DETAIL_MSG_DISPATCHER_HPP
#include <nana/detail/linux_X11/msg_packet.hpp>
#include "msg_packet.hpp"
#include <nana/system/platform.hpp>
#include <list>
#include <set>

View File

@@ -0,0 +1,32 @@
#ifndef NANA_DETAIL_MSG_PACKET_HPP
#define NANA_DETAIL_MSG_PACKET_HPP
#include <X11/Xlib.h>
#include <vector>
#include <nana/deploy.hpp>
namespace nana
{
namespace detail
{
struct msg_packet_tag
{
enum kind_t{kind_xevent, kind_mouse_drop, kind_cleanup};
kind_t kind;
union
{
XEvent xevent;
Window packet_window; //Avaiable if the packet is not kind_xevent
struct mouse_drop_tag
{
Window window;
int x;
int y;
std::vector<std::string> * files;
}mouse_drop;
}u;
};
}//end namespace detail
}//end namespace nana
#endif

View File

@@ -0,0 +1,325 @@
/*
* Platform Specification Implementation
* 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/detail/platform_spec.hpp
*
* This file provides basis class and data structrue that required by nana
* This file should not be included by any header files.
*/
#if defined(NANA_LINUX) || defined(NANA_MACOS)
#ifndef NANA_DETAIL_PLATFORM_SPEC_HPP
#define NANA_DETAIL_PLATFORM_SPEC_HPP
#include <nana/push_ignore_diagnostic>
#include <thread>
#include <mutex>
#include <memory>
#include <condition_variable>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>
#include <nana/gui/basis.hpp>
#include <nana/paint/image.hpp>
#include <nana/paint/graphics.hpp>
#include <nana/gui/detail/event_code.hpp>
#include <vector>
#include <map>
#include "msg_packet.hpp"
#include "../platform_abstraction_types.hpp"
#if defined(NANA_USE_XFT)
#include <X11/Xft/Xft.h>
#include <iconv.h>
#include <fstream>
#endif
namespace nana
{
namespace detail
{
class msg_dispatcher;
#if defined(NANA_USE_XFT)
class conf
{
public:
conf(const char * file);
bool open(const char* file);
std::string value(const char* key);
private:
std::ifstream ifs_;
};
class charset_conv
{
charset_conv(const charset_conv&) = delete;
charset_conv& operator=(const charset_conv*) = delete;
public:
charset_conv(const char* tocode, const char* fromcode);
~charset_conv();
std::string charset(const std::string& str) const;
std::string charset(const char * buf, std::size_t len) const;
private:
iconv_t handle_;
};
#endif
struct drawable_impl_type
{
using font_type = ::std::shared_ptr<font_interface>;
Pixmap pixmap;
GC context;
font_type font;
nana::point line_begin_pos;
struct string_spec
{
unsigned tab_length;
unsigned tab_pixels;
unsigned whitespace_pixels;
}string;
#if defined(NANA_USE_XFT)
XftDraw * xftdraw{nullptr};
XftColor xft_fgcolor;
const std::string charset(const std::wstring& str, const std::string& strcode);
#endif
drawable_impl_type();
~drawable_impl_type();
void fgcolor(const ::nana::color&); //deprecated
unsigned get_color() const;
unsigned get_text_color() const;
void set_color(const ::nana::color&);
void set_text_color(const ::nana::color&);
void update_color();
void update_text_color();
private:
drawable_impl_type(const drawable_impl_type&) = delete;
drawable_impl_type& operator=(const drawable_impl_type&) = delete;
unsigned current_color_{ 0xFFFFFF };
unsigned color_{ 0xFFFFFFFF };
unsigned text_color_{ 0xFFFFFFFF };
#if defined(NANA_USE_XFT)
struct conv_tag
{
iconv_t handle;
std::string code;
}conv_;
#endif
};
struct atombase_tag
{
Atom wm_protocols;
//window manager support
Atom wm_change_state;
Atom wm_delete_window;
//ext
Atom net_wm_state;
Atom net_wm_state_skip_taskbar;
Atom net_wm_state_fullscreen;
Atom net_wm_state_maximized_horz;
Atom net_wm_state_maximized_vert;
Atom net_wm_state_modal;
Atom net_wm_name;
Atom net_wm_window_type;
Atom net_wm_window_type_normal;
Atom net_wm_window_type_utility;
Atom net_wm_window_type_dialog;
Atom motif_wm_hints;
Atom clipboard;
Atom text;
Atom text_uri_list;
Atom utf8_string;
Atom targets;
Atom xdnd_aware;
Atom xdnd_enter;
Atom xdnd_position;
Atom xdnd_status;
Atom xdnd_action_copy;
Atom xdnd_drop;
Atom xdnd_selection;
Atom xdnd_typelist;
Atom xdnd_finished;
};
//A forward declaration of caret data
struct caret_rep;
class timer_runner;
class platform_scope_guard
{
public:
platform_scope_guard();
~platform_scope_guard();
};
class platform_spec
{
typedef platform_spec self_type;
struct window_context_t
{
native_window_type owner;
std::vector<native_window_type> * owned;
};
public:
int error_code;
public:
typedef void (*timer_proc_type)(unsigned tid);
typedef void (*event_proc_type)(Display*, msg_packet_tag&);
typedef ::nana::event_code event_code;
typedef ::nana::native_window_type native_window_type;
platform_spec(const platform_spec&) = delete;
platform_spec& operator=(const platform_spec&) = delete;
platform_spec();
~platform_spec();
Display* open_display();
void close_display();
void lock_xlib();
void unlock_xlib();
Window root_window();
int screen_depth();
Visual* screen_visual();
Colormap& colormap();
static self_type& instance();
const atombase_tag & atombase() const;
void make_owner(native_window_type owner, native_window_type wd);
native_window_type get_owner(native_window_type) const;
void remove(native_window_type);
void write_keystate(const XKeyEvent&);
void read_keystate(XKeyEvent&);
XIC caret_input_context(native_window_type) const;
void caret_open(native_window_type, const ::nana::size&);
void caret_close(native_window_type);
void caret_pos(native_window_type, const ::nana::point&);
void caret_visible(native_window_type, bool);
bool caret_update(native_window_type, nana::paint::graphics& root_graph, bool is_erase_caret_from_root_graph);
void set_error_handler();
int rev_error_handler();
//grab
//register a grab window while capturing it if it is unviewable.
//when native_interface::show a window that is registered as a grab
//window, the native_interface grabs the window.
Window grab(Window);
void set_timer(std::size_t id, std::size_t interval, void (*timer_proc)(std::size_t id));
void kill_timer(std::size_t id);
void timer_proc(unsigned tid);
//Message dispatcher
void msg_insert(native_window_type);
void msg_set(timer_proc_type, event_proc_type);
void msg_dispatch(native_window_type modal);
//X Selections
void* request_selection(native_window_type requester, Atom type, size_t & bufsize);
void write_selection(native_window_type owner, Atom type, const void* buf, size_t bufsize);
//Icon storage
//@biref: The image object should be kept for a long time till the window is closed,
// the image object is release in remove() method.
const nana::paint::graphics& keep_window_icon(native_window_type, const nana::paint::image&);
private:
static int _m_msg_filter(XEvent&, msg_packet_tag&);
void _m_caret_routine();
private:
Display* display_;
Colormap colormap_;
atombase_tag atombase_;
XKeyEvent key_state_;
int (*def_X11_error_handler_)(Display*, XErrorEvent*);
Window grab_;
std::recursive_mutex xlib_locker_;
struct caret_holder_tag
{
volatile bool exit_thread;
std::unique_ptr<std::thread> thr;
std::map<native_window_type, caret_rep*> carets;
}caret_holder_;
std::map<native_window_type, window_context_t> wincontext_;
std::map<native_window_type, nana::paint::graphics> iconbase_;
struct timer_runner_tag
{
timer_runner * runner;
std::recursive_mutex mutex;
bool delete_declared;
timer_runner_tag();
}timer_;
struct selection_tag
{
struct item_t
{
Atom type;
Window requestor;
void* buffer;
size_t bufsize;
std::mutex cond_mutex;
std::condition_variable cond;
};
std::vector<item_t*> items;
struct content_tag
{
std::string * utf8_string;
}content;
}selection_;
struct xdnd_tag
{
Atom good_type;
int timestamp;
Window wd_src;
nana::point pos;
}xdnd_;
msg_dispatcher * msg_dispatcher_;
};//end class platform_X11
}//end namespace detail
}//end namespace nana
#include <nana/pop_ignore_diagnostic>
// .h ward
#endif
//#if defined(NANA_LINUX) || defined(NANA_MACOS)
#endif