Skip to content

Commit

Permalink
platform
Browse files Browse the repository at this point in the history
  • Loading branch information
dustpg committed Jul 14, 2020
1 parent c09e8c8 commit bf60117
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 2,049 deletions.
2 changes: 0 additions & 2 deletions CodeUI/CodeUI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
<ClCompile Include="..\src\cfunciton\view_atoi.c" />
<ClCompile Include="..\src\cfunciton\view_unicode.c" />
<ClCompile Include="..\src\container\pod_hash.cpp" />
<ClCompile Include="..\src\container\pod_sharedarray.cpp" />
<ClCompile Include="..\src\container\pod_vector.cpp" />
<ClCompile Include="..\src\container\ui_string.cpp" />
<ClCompile Include="..\src\container\ui_string_view.cpp" />
Expand Down Expand Up @@ -301,7 +300,6 @@
<ClInclude Include="..\include\constexpr\const_log2.h" />
<ClInclude Include="..\include\container\const_string_list.h" />
<ClInclude Include="..\include\container\pod_hash.h" />
<ClInclude Include="..\include\container\pod_sharedarray.h" />
<ClInclude Include="..\include\container\pod_vector.h" />
<ClInclude Include="..\include\container\ui_string_vector.h" />
<ClInclude Include="..\include\control\ui_arrowscrollbox.h" />
Expand Down
6 changes: 0 additions & 6 deletions CodeUI/CodeUI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,6 @@
<ClCompile Include="..\src\container\pod_hash.cpp">
<Filter>source\container</Filter>
</ClCompile>
<ClCompile Include="..\src\container\pod_sharedarray.cpp">
<Filter>source\container</Filter>
</ClCompile>
<ClCompile Include="..\src\container\pod_vector.cpp">
<Filter>source\container</Filter>
</ClCompile>
Expand Down Expand Up @@ -552,9 +549,6 @@
<ClInclude Include="..\include\container\pod_hash.h">
<Filter>header\container</Filter>
</ClInclude>
<ClInclude Include="..\include\container\pod_sharedarray.h">
<Filter>header\container</Filter>
</ClInclude>
<ClInclude Include="..\include\container\pod_vector.h">
<Filter>header\container</Filter>
</ClInclude>
Expand Down
2 changes: 1 addition & 1 deletion demo/demo5_fullcontrol/xul/button.xul
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function setText(textID,val)
<groupbox flex="1">
<caption label="These are menubuttons." />
<hbox>
<button flex="1" type="menu" label="Menu">
<button flex="1" type="menu" label="Menu" id="btn-menu">
<menupopup>
<menuitem label="Option 1" oncommand="setText('menu-text','Option 1');" />
<menuitem label="Option 2" oncommand="setText('menu-text','Option 2');" />
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions include/core/ui_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ namespace LongUI {
void PrepareRender(Size2F) noexcept;
// mark one dirty rect
void MarkDirtyRect(const DirtyRect& rect) noexcept;
//// map to screen
//void MapToScreen(RectF& rect) const noexcept;
//// map to screen
//void MapToScreen(RectL& rect) const noexcept;
//// map from screen
//void MapFromScreen(Point2F& pos) const noexcept;
public:
// init
void Init(CUIWindow* parent, uint16_t flag) noexcept;
Expand All @@ -127,12 +133,6 @@ namespace LongUI {
void AfterAbsRect() noexcept;
// close window
void CloseWindow() noexcept;
// map to screen
void MapToScreen(RectF& rect) const noexcept;
// map to screen
void MapToScreen(RectL& rect) const noexcept;
// map from screen
void MapFromScreen(Point2F& pos) const noexcept;
// show window
void ShowWindow(int) noexcept;
// resize
Expand Down
10 changes: 2 additions & 8 deletions include/core/ui_platform_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace LongUI {
bool is_direct_composition() const noexcept { return m_bDcompSupport; }
// is skip render?
bool is_skip_render() const noexcept { return m_bSystemSkipRendering; }
// layered
void layered() noexcept;
// update adjust
void update_adjust(uint32_t, uint32_t, uint32_t dpi) noexcept;
// begin rendering
void begin_render() noexcept;
// end rendering
Expand Down Expand Up @@ -85,12 +85,6 @@ namespace LongUI {
void ReleaseDeviceData() noexcept;
// recreate
auto Recreate() noexcept->Result;
// map to screen
void MapToScreen(RectF& rect) const noexcept;
// map to screen
void MapToScreen(RectL& rect) const noexcept;
// map from screen
void MapFromScreen(Point2F& pos) const noexcept;
// after titlename set
void AfterTitleName() noexcept;
// after position set
Expand Down
2 changes: 2 additions & 0 deletions include/core/ui_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ namespace LongUI {
protected:
// get platform
auto&platform() noexcept { return reinterpret_cast<CUIPlatform&>(m_platform); }
// get screen lt
template<typename T>auto screen_lt() const noexcept -> Point<T>;
// init
void init() noexcept;
// recursive set result
Expand Down
9 changes: 7 additions & 2 deletions src/control/ui_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ PCN_NOINLINE
/// <returns></returns>
void LongUI::UIBoxLayout::relayout_h() noexcept {
#ifndef NDEBUG
if (!std::strcmp(m_id.id, "editable_combobox")) {
if (!std::strcmp(m_id.id, "btn-menu")) {
int bk = 9;
}
if (this->IsTopLevel()) {
Expand Down Expand Up @@ -298,6 +298,11 @@ void LongUI::UIBoxLayout::relayout_h() noexcept {
auto& child = *itr;
// 有效才处理
if (!child.IsVaildInLayout()) continue;
#ifndef NDEBUG
if (!std::strcmp(child.GetID().id, "btn-menu")) {
int bk = 9;
}
#endif
// 获取标准大小
const auto size = get_stdsize(child, remaining, style_align);
// 调整对齐
Expand Down Expand Up @@ -423,7 +428,7 @@ namespace LongUI { namespace impl {
/// <returns></returns>
void LongUI::UIBoxLayout::refresh_fitting() noexcept {
#ifndef NDEBUG
if (!std::strcmp(m_id.id, "b")) {
if (!std::strcmp(m_id.id, "btn-menu")) {
int bk = 9;
}
if (!std::strcmp(name_dbg, "scrollarea::vscrollbar")) {
Expand Down
4 changes: 2 additions & 2 deletions src/control/ui_viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ auto LongUI::UIViewport::FindSubViewport(U8View view) const noexcept -> UIViewpo
/// <param name="y">The y.</param>
/// <returns></returns>
void LongUI::UIViewport::JustResetZoom(float x, float y) noexcept {
m_mtWorld._11 = x ; m_mtWorld._12 = 0.f;
m_mtWorld._21 = 0.f; m_mtWorld._22 = y ;
m_mtWorld._11 = x; m_mtWorld._12 = 0;
m_mtWorld._21 = 0; m_mtWorld._22 = y;
}

/// <summary>
Expand Down
229 changes: 140 additions & 89 deletions src/core/ui_hidpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,106 +7,157 @@
#include <Windows.h>
#include <Winuser.h>

namespace LongUI {
// impl
namespace impl {
// dll name
static constexpr const char* dpi_support_dll = "Shcore.dll";
// longui::impl
namespace LongUI { namespace impl {
// dpi awareness
enum process_dpi_awareness {
dpi_unaware = 0,
dpi_system = 1,
dpi_per_monitor = 2,
};
// monitor dpi type
enum monitor_dpi_type {
mdt_effective_dpi,
mdt_angular_dpi,
mdt_raw_dpi,
mdt_default
};
// dll name
static constexpr const char* dpi_support_dll = "Shcore.dll";
// windows api
struct windows_api {
// AdjustWindowRectExForDpi
BOOL(WINAPI* adjust) (LPRECT, DWORD, BOOL, DWORD, UINT) noexcept;
// GetDpiForMonitor
static FARPROC ptr_GetDpiForMonitor = nullptr;
// dpi awareness
enum process_dpi_awareness {
dpi_unaware = 0,
dpi_system = 1,
dpi_per_monitor = 2,
};
// monitor dpi type
enum monitor_dpi_type {
mdt_effective_dpi,
mdt_angular_dpi,
mdt_raw_dpi,
mdt_default
};
// init HiDPI
void init_high_dpi_support() noexcept {
using pda = process_dpi_awareness;
// 万能union转换
union { HRESULT(WINAPI* set_pda)(pda) noexcept; FARPROC call; };
// XXX: 使用编号
const auto SETPROCESSDPIAWARENESS_NAME = "SetProcessDpiAwareness";
const auto GETDPIFORMONITOR_NAME = "GetDpiForMonitor";
// 载入
HRESULT(WINAPI* get_dpi)(HMONITOR, monitor_dpi_type, UINT*, UINT*) noexcept;
} g_api;
// init HiDPI
void init_high_dpi_support() noexcept {
// Win10API
{
const auto dll = ::GetModuleHandleA("User32.dll");
const auto api = ::GetProcAddress(dll, "AdjustWindowRectExForDpi");
reinterpret_cast<FARPROC&>(g_api.adjust) = api;
}
using pda = process_dpi_awareness;
// 万能union转换
union { HRESULT(WINAPI* set_pda)(pda) noexcept; FARPROC call; };
// XXX: 使用编号
const auto SETPROCESSDPIAWARENESS_NAME = "SetProcessDpiAwareness";
const auto GETDPIFORMONITOR_NAME = "GetDpiForMonitor";
// 载入
#ifdef LUI_WIN10_ONLY
const auto dll = ::LoadLibraryA(dpi_support_dll);
assert(dll && "support windows 10 only");
call = ::GetProcAddress(dll, SETPROCESSDPIAWARENESS_NAME);
assert(call && "support windows 10 only");
ptr_GetDpiForMonitor = ::GetProcAddress(dll, GETDPIFORMONITOR_NAME);
set_pda(dpi_per_monitor);
const auto dll = ::LoadLibraryA(dpi_support_dll);
assert(dll && "support windows 10 only");
call = ::GetProcAddress(dll, SETPROCESSDPIAWARENESS_NAME);
assert(call && "support windows 10 only");
ptr_GetDpiForMonitor = ::GetProcAddress(dll, GETDPIFORMONITOR_NAME);
set_pda(dpi_per_monitor);
#else
// win 8
if (const auto dll = ::LoadLibraryA(dpi_support_dll)) {
// win 8.1
if ((call = ::GetProcAddress(dll, SETPROCESSDPIAWARENESS_NAME))) {
ptr_GetDpiForMonitor = ::GetProcAddress(dll, GETDPIFORMONITOR_NAME);
// 设置DPI支持等级
if (set_pda(dpi_per_monitor) == E_INVALIDARG)
set_pda(dpi_system);
return;
}
// win 8
if (const auto dll = ::LoadLibraryA(dpi_support_dll)) {
// win 8.1
if ((call = ::GetProcAddress(dll, SETPROCESSDPIAWARENESS_NAME))) {
const auto api = ::GetProcAddress(dll, GETDPIFORMONITOR_NAME);
reinterpret_cast<FARPROC&>(g_api.get_dpi) = api;
// 设置DPI支持等级
if (set_pda(dpi_per_monitor) == E_INVALIDARG)
set_pda(dpi_system);
return;
}
// win7/win8
::SetProcessDPIAware();
#endif
}
/// <summary>
/// Uninits the high dpi support.
/// </summary>
/// <returns></returns>
void uninit_high_dpi_support() noexcept {
// win7/win8
::SetProcessDPIAware();
#endif
}
/// <summary>
/// Uninits the high dpi support.
/// </summary>
/// <returns></returns>
void uninit_high_dpi_support() noexcept {
#ifdef LUI_WIN10_ONLY
::FreeLibrary(::GetModuleHandleA(dpi_support_dll));
::FreeLibrary(::GetModuleHandleA(dpi_support_dll));
#else
if (const auto dll = ::GetModuleHandleA(dpi_support_dll)) {
::FreeLibrary(dll);
}
if (const auto dll = ::GetModuleHandleA(dpi_support_dll)) {
::FreeLibrary(dll);
}
#endif
}
/// <summary>
/// Gets the dpi from Rect.
/// </summary>
/// <param name="rect">The rect.</param>
/// <returns></returns>
auto get_dpi_from_rect(const RectL& rect) noexcept -> uint32_t {
//return { 3.f, 3.f };
#ifndef LUI_WIN10_ONLY
// WIN7/Win8 只支持相同的DPI
if (!g_api.get_dpi) {
static const auto dpi_x = []() noexcept ->uint32_t {
const auto hdc = ::GetDC(nullptr);
const auto dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX);
return static_cast<uint32_t>(dpi_x);
}();
return dpi_x;
}
/// <summary>
/// Gets the dpi scale from HWND.
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <returns></returns>
auto get_dpi_scale_from_hwnd(HWND hwnd) noexcept -> Size2F {
//return { 3.f, 3.f };
#endif
UINT dpi[2] = { 0 };
const auto ptr = reinterpret_cast<LPCRECT>(&rect);
const auto monitor = ::MonitorFromRect(ptr, MONITOR_DEFAULTTOPRIMARY);
const auto hr = g_api.get_dpi(monitor, mdt_effective_dpi, dpi, dpi + 1);
if (FAILED(hr)) return BASIC_DPI;
return dpi[0];
}
/// <summary>
/// Gets the dpi scale from HWND.
/// </summary>
/// <param name="hwnd">The HWND.</param>
/// <returns></returns>
auto get_dpi_scale_from_hwnd(HWND hwnd) noexcept -> Size2F {
//return { 3.f, 3.f };
#ifndef LUI_WIN10_ONLY
// WIN7/Win8 只支持相同的DPI
if (!ptr_GetDpiForMonitor) {
static const Size2F dpi_xy = []() noexcept ->Size2F {
const auto hdc = ::GetDC(nullptr);
const auto dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX);
const auto dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(nullptr, hdc);
const auto x = float(dpi_x) / float(BASIC_DPI);
const auto y = float(dpi_y) / float(BASIC_DPI);
return{ x, y };
}();
return dpi_xy;
}
// WIN7/Win8 只支持相同的DPI
if (!g_api.get_dpi) {
static const Size2F dpi_xy = []() noexcept ->Size2F {
const auto hdc = ::GetDC(nullptr);
const auto dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX);
const auto dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(nullptr, hdc);
const auto x = float(dpi_x) / float(BASIC_DPI);
const auto y = float(dpi_y) / float(BASIC_DPI);
return{ x, y };
}();
return dpi_xy;
}
#endif
const auto monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
union {
HRESULT(WINAPI* get_dpi)(HMONITOR, monitor_dpi_type, UINT*, UINT*) noexcept;
FARPROC call;
};
call = ptr_GetDpiForMonitor;
UINT dpi[2] = { 0 };
const auto hr = get_dpi(monitor, mdt_effective_dpi, dpi, dpi + 1);
if (FAILED(hr)) return { 1.f, 1.f };
const auto x = float(dpi[0]) / float(BASIC_DPI);
const auto y = float(dpi[1]) / float(BASIC_DPI);
return { x, y };
UINT dpi[2] = { 0 };
const auto monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
const auto hr = g_api.get_dpi(monitor, mdt_effective_dpi, dpi, dpi + 1);
if (FAILED(hr)) return { 1.f, 1.f };
const auto x = float(dpi[0]) / float(BASIC_DPI);
const auto y = float(dpi[1]) / float(BASIC_DPI);
return { x, y };
}
/// <summary>
/// adjust window rect
/// </summary>
/// <param name="rect"></param>
/// <param name="style"></param>
/// <param name="ex"></param>
/// <param name="dpi"></param>
/// <returns></returns>
void adjust_window(RectL& rect, uint32_t style, uint32_t ex, uint32_t dpi) noexcept {
static_assert(sizeof(RectL) == sizeof(RECT), "SAME!");
const auto ptr = reinterpret_cast<RECT*>(&rect);
#ifndef LUI_WIN10_ONLY
if (g_api.adjust) {
g_api.adjust(ptr, style, FALSE, ex, dpi);
return;
}
::AdjustWindowRectEx(ptr, style, FALSE, ex);
#else
g_api.adjust(ptr, style, FALSE, ex, dpi);
#endif
}
}
}}

Loading

0 comments on commit bf60117

Please sign in to comment.