aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CruUI/application.cpp66
-rw-r--r--CruUI/application.h14
-rw-r--r--CruUI/timer.cpp2
-rw-r--r--CruUI/ui/controls/text_block.cpp87
-rw-r--r--CruUI/ui/controls/text_block.h8
-rw-r--r--CruUI/ui/window.cpp139
-rw-r--r--CruUI/ui/window.h12
7 files changed, 253 insertions, 75 deletions
diff --git a/CruUI/application.cpp b/CruUI/application.cpp
index 3ee7817d..c60bb043 100644
--- a/CruUI/application.cpp
+++ b/CruUI/application.cpp
@@ -1,10 +1,13 @@
#include "application.h"
+#include <fmt/format.h>
+
#include "timer.h"
#include "ui/window.h"
#include "graph/graph.h"
namespace cru {
+ constexpr auto god_window_class_name = L"GodWindowClass";
constexpr int invoke_later_message_id = WM_USER + 2000;
Application* Application::instance_ = nullptr;
@@ -21,6 +24,20 @@ namespace cru {
instance_ = this;
+ god_window_class_ = std::make_unique<ui::WindowClass>(god_window_class_name, GodWndProc, h_instance);
+
+ const auto hwnd = CreateWindowEx(0,
+ god_window_class_name,
+ L"", 0,
+ CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+ nullptr, nullptr, h_instance, nullptr
+ );
+
+ if (hwnd == nullptr)
+ throw std::runtime_error("Failed to create window.");
+
+ god_hwnd_ = hwnd;
+
window_manager_ = std::make_unique<ui::WindowManager>();
graph_manager_ = std::make_unique<graph::GraphManager>();
timer_manager_ = std::make_unique<TimerManager>();
@@ -37,12 +54,8 @@ namespace cru {
while (GetMessage(&msg, nullptr, 0, 0))
{
- if (!HandleThreadMessage(msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
-
- }
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
@@ -52,45 +65,54 @@ namespace cru {
::PostQuitMessage(quit_code);
}
- bool Application::HandleThreadMessage(const MSG& message)
+ LRESULT Application::GodWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
- if (message.hwnd != nullptr)
- return false;
+ const auto app = Application::GetInstance();
- switch (message.message)
+ if (app)
+ {
+ const auto result = app->HandleGodWindowMessage(hWnd, Msg, wParam, lParam);
+ if (result.has_value())
+ return result.value();
+ else
+ return DefWindowProc(hWnd, Msg, wParam, lParam);
+ }
+ else
+ return DefWindowProc(hWnd, Msg, wParam, lParam);
+ }
+
+ std::optional<LRESULT> Application::HandleGodWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param)
+ {
+ switch (msg)
{
case invoke_later_message_id:
{
- const auto p_action = reinterpret_cast<InvokeLaterAction*>(message.wParam);
+ const auto p_action = reinterpret_cast<InvokeLaterAction*>(w_param);
(*p_action)();
delete p_action;
- return true;
+ return 0;
}
case WM_TIMER:
{
- const auto action = timer_manager_->GetAction(static_cast<UINT_PTR>(message.wParam));
+ const auto action = GetTimerManager()->GetAction(static_cast<UINT_PTR>(w_param));
if (action.has_value())
{
action.value()();
- return true;
+ return 0;
}
break;
}
default:
- return false;
+ return std::nullopt;
}
- return false;
+ return std::nullopt;
}
void InvokeLater(InvokeLaterAction&& action) {
//copy the action to a safe place
auto p_action_copy = new InvokeLaterAction(std::move(action));
- PostMessage(
- nullptr,
- invoke_later_message_id,
- reinterpret_cast<WPARAM>(p_action_copy),
- 0
- );
+ if (PostMessageW(Application::GetInstance()->GetGodWindowHandle(), invoke_later_message_id, reinterpret_cast<WPARAM>(p_action_copy), 0) == 0)
+ throw std::runtime_error(fmt::format("Last error: {}.", ::GetLastError()));
}
}
diff --git a/CruUI/application.h b/CruUI/application.h
index be5d21ba..4285cc21 100644
--- a/CruUI/application.h
+++ b/CruUI/application.h
@@ -2,6 +2,7 @@
#include "system_headers.h"
#include <memory>
+#include <optional>
#include "base.h"
@@ -9,6 +10,7 @@ namespace cru
{
namespace ui
{
+ class WindowClass;
class WindowManager;
}
@@ -58,11 +60,21 @@ namespace cru
return h_instance_;
}
+ HWND GetGodWindowHandle() const
+ {
+ return god_hwnd_;
+ }
+
private:
- bool HandleThreadMessage(const MSG& message);
+ static LRESULT __stdcall GodWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
+ std::optional<LRESULT> HandleGodWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param);
private:
HINSTANCE h_instance_;
+
+ std::unique_ptr<ui::WindowClass> god_window_class_;
+ HWND god_hwnd_;
+
std::unique_ptr<ui::WindowManager> window_manager_;
std::unique_ptr<graph::GraphManager> graph_manager_;
std::unique_ptr<TimerManager> timer_manager_;
diff --git a/CruUI/timer.cpp b/CruUI/timer.cpp
index 5eb803c7..82a2a0f4 100644
--- a/CruUI/timer.cpp
+++ b/CruUI/timer.cpp
@@ -21,7 +21,7 @@ namespace cru
UINT_PTR TimerManager::CreateTimer(const UINT microseconds, const bool loop, const TimerAction & action)
{
- auto id = ::SetTimer(nullptr, 0, microseconds, nullptr);
+ auto id = ::SetTimer(Application::GetInstance()->GetGodWindowHandle(), 0, microseconds, nullptr);
if (loop)
map_[id] = action;
else
diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp
index 739cd793..c528d3ca 100644
--- a/CruUI/ui/controls/text_block.cpp
+++ b/CruUI/ui/controls/text_block.cpp
@@ -1,7 +1,5 @@
#include "text_block.h"
-#include <chrono>
-
#include "ui/window.h"
#include "graph/graph.h"
#include "exception.h"
@@ -18,12 +16,22 @@ namespace cru
return graph::GraphManager::GetInstance()->GetDWriteFactory();
}
+ Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidBrush(const D2D1_COLOR_F& color)
+ {
+ const auto device_context = graph::GraphManager::GetInstance()->GetD2D1DeviceContext();
+ Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush;
+ device_context->CreateSolidColorBrush(color, &solid_color_brush);
+ return solid_color_brush;
+ }
+
TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format,
const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false)
{
text_format_ = init_text_format;
if (init_brush == nullptr)
CreateDefaultBrush();
+
+ selection_brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Blue)); //TODO!
}
TextBlock::~TextBlock() = default;
@@ -62,25 +70,83 @@ namespace cru
void TextBlock::OnDraw(ID2D1DeviceContext* device_context)
{
if (text_layout_ != nullptr)
+ {
+ if (selected_range_.has_value())
+ {
+ DWRITE_TEXT_METRICS text_metrics{};
+ ThrowIfFailed(text_layout_->GetMetrics(&text_metrics));
+ const auto metrics_count = text_metrics.lineCount * text_metrics.maxBidiReorderingDepth;
+
+ Vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count);
+ UINT32 actual_count;
+ text_layout_->HitTestTextRange(
+ selected_range_.value().position, selected_range_.value().count,
+ 0, 0,
+ hit_test_metrics.data(), metrics_count, &actual_count
+ );
+
+ hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, hit_test_metrics.cend());
+
+ for (const auto& metrics : hit_test_metrics)
+ {
+ device_context->FillRoundedRectangle(D2D1::RoundedRect(D2D1::RectF(metrics.left, metrics.top, metrics.left + metrics.width, metrics.top + metrics.height), 3, 3), selection_brush_.Get());
+ }
+ }
device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get());
+ }
+ }
+
+ std::optional<unsigned> TextLayoutHitTest(IDWriteTextLayout* text_layout, const Point& point, bool test_inside = true)
+ {
+ BOOL is_trailing, is_inside;
+ DWRITE_HIT_TEST_METRICS metrics{};
+ text_layout->HitTestPoint(point.x, point.y, &is_trailing, &is_inside, &metrics);
+ if (!test_inside || is_inside)
+ return is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1;
+ else
+ return std::nullopt;
}
void TextBlock::OnMouseDownCore(events::MouseButtonEventArgs& args)
{
if (args.GetMouseButton() == MouseButton::Left)
{
- BOOL is_trailing, is_inside;
- DWRITE_HIT_TEST_METRICS metrics;
- text_layout_->HitTestPoint(args.GetPoint(this).x, args.GetPoint(this).y, &is_trailing, &is_inside, &metrics);
- if (is_inside)
+ const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), true);
+ if (hit_test_result.has_value())
{
- mouse_down_position_ = is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1;
- is_mouse_down_ = true;
+ mouse_down_position_ = hit_test_result.value();
+ is_selecting_ = true;
+ GetWindow()->CaptureMouseFor(this);
}
}
Control::OnMouseDownCore(args);
}
+ void TextBlock::OnMouseMoveCore(events::MouseEventArgs& args)
+ {
+ if (is_selecting_)
+ {
+ const auto hit_test_result = TextLayoutHitTest(text_layout_.Get(), args.GetPoint(this), false).value();
+ if (hit_test_result > mouse_down_position_)
+ selected_range_ = TextRange(mouse_down_position_, hit_test_result - mouse_down_position_);
+ else
+ selected_range_ = TextRange(hit_test_result, mouse_down_position_ - hit_test_result);
+ Repaint();
+ }
+ }
+
+ void TextBlock::OnMouseUpCore(events::MouseButtonEventArgs& args)
+ {
+ if (args.GetMouseButton() == MouseButton::Left)
+ {
+ if (is_selecting_)
+ {
+ is_selecting_ = false;
+ GetWindow()->ReleaseCurrentMouseCapture();
+ }
+ }
+ }
+
Size TextBlock::OnMeasure(const Size& available_size)
{
if (text_.empty())
@@ -147,10 +213,7 @@ namespace cru
void TextBlock::CreateDefaultBrush()
{
- const auto device_context = graph::GraphManager::GetInstance()->GetD2D1DeviceContext();
- Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush;
- device_context->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &solid_color_brush);
- brush_ = solid_color_brush;
+ brush_ = CreateSolidBrush(D2D1::ColorF(D2D1::ColorF::Black));
}
void TextBlock::CreateDefaultTextFormat()
diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h
index d6d173ba..1d1166a5 100644
--- a/CruUI/ui/controls/text_block.h
+++ b/CruUI/ui/controls/text_block.h
@@ -102,6 +102,7 @@ namespace cru
void OnMouseDownCore(events::MouseButtonEventArgs& args) override;
void OnMouseMoveCore(events::MouseEventArgs& args) override;
+ void OnMouseUpCore(events::MouseButtonEventArgs& args) override;
Size OnMeasure(const Size& available_size) override;
@@ -116,15 +117,16 @@ namespace cru
String text_;
Microsoft::WRL::ComPtr<ID2D1Brush> brush_;
+ Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_;
Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;
Vector<std::shared_ptr<TextLayoutHandler>> text_layout_handlers_;
- unsigned mouse_down_position_;
- std::optional<TextRange> selected_range_;
+ unsigned mouse_down_position_ = 0;
+ std::optional<TextRange> selected_range_ = std::nullopt;
- bool is_mouse_down_ = false; //TODO!!!
+ bool is_selecting_ = false;
};
}
}
diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp
index 3a9393ff..8430d09b 100644
--- a/CruUI/ui/window.cpp
+++ b/CruUI/ui/window.cpp
@@ -1,5 +1,7 @@
#include "window.h"
+#include <fmt/format.h>
+
#include "application.h"
#include "graph/graph.h"
#include "exception.h"
@@ -83,17 +85,19 @@ namespace cru
// find descendant then erase it; find ancestor then just return.
for (auto i = cache_invalid_controls_.cbegin(); i != cache_invalid_controls_.cend(); ++i)
{
- if (IsAncestorOrDescendant(*i, control) == control)
+ const auto result = IsAncestorOrDescendant(*i, control);
+ if (result == control)
cache_invalid_controls_.erase(i);
- else
+ else if (result != nullptr)
return; // find a ancestor of "control", just return
}
-
+
cache_invalid_controls_.insert(control);
if (cache_invalid_controls_.size() == 1) // when insert just now and not repeat to "InvokeLater".
{
InvokeLater([this] {
+
RefreshInvalidControlPositionCache(); // first refresh position cache.
for (const auto i : cache_invalid_controls_) // traverse all descendants of position-invalid controls and notify position change event
i->TraverseDescendants([](Control* control)
@@ -101,6 +105,7 @@ namespace cru
control->CheckAndNotifyPositionChanged();
});
cache_invalid_controls_.clear(); // after update and notify, clear the set.
+
});
}
}
@@ -128,7 +133,7 @@ namespace cru
const auto position = control->GetPositionRelative();
Point lefttop(
parent_lefttop_absolute.x + position.x,
- parent_lefttop_absolute.y + position.x
+ parent_lefttop_absolute.y + position.y
);
control->position_cache_.lefttop_position_absolute = lefttop;
control->ForeachChild([lefttop](Control* c) {
@@ -136,6 +141,14 @@ namespace cru
});
}
+ inline Point PiToDip(const POINT& pi_point)
+ {
+ return Point(
+ graph::PixelToDipX(pi_point.x),
+ graph::PixelToDipY(pi_point.y)
+ );
+ }
+
Window::Window() : Control(true), layout_manager_(new WindowLayoutManager()), control_list_({ this }) {
auto app = Application::GetInstance();
hwnd_ = CreateWindowEx(0,
@@ -257,6 +270,15 @@ namespace cru
OnKillFocusInternal();
result = 0;
return true;
+ case WM_MOUSEMOVE:
+ {
+ POINT point;
+ point.x = GET_X_LPARAM(l_param);
+ point.y = GET_Y_LPARAM(l_param);
+ OnMouseMoveInternal(point);
+ result = 0;
+ return true;
+ }
case WM_LBUTTONDOWN:
{
POINT point;
@@ -271,7 +293,7 @@ namespace cru
POINT point;
point.x = GET_X_LPARAM(l_param);
point.y = GET_Y_LPARAM(l_param);
- OnMouseDownInternal(MouseButton::Left, point);
+ OnMouseUpInternal(MouseButton::Left, point);
result = 0;
return true;
}
@@ -289,7 +311,7 @@ namespace cru
POINT point;
point.x = GET_X_LPARAM(l_param);
point.y = GET_Y_LPARAM(l_param);
- OnMouseDownInternal(MouseButton::Right, point);
+ OnMouseUpInternal(MouseButton::Right, point);
result = 0;
return true;
}
@@ -307,7 +329,7 @@ namespace cru
POINT point;
point.x = GET_X_LPARAM(l_param);
point.y = GET_Y_LPARAM(l_param);
- OnMouseDownInternal(MouseButton::Middle, point);
+ OnMouseUpInternal(MouseButton::Middle, point);
result = 0;
return true;
}
@@ -324,6 +346,14 @@ namespace cru
}
}
+ Point Window::GetMousePosition()
+ {
+ POINT point;
+ ::GetCursorPos(&point);
+ ::ScreenToClient(hwnd_, &point);
+ return PiToDip(point);
+ }
+
Point Window::GetPositionRelative()
{
return Point();
@@ -394,6 +424,37 @@ namespace cru
return focus_control_;
}
+ Control* Window::CaptureMouseFor(Control* control)
+ {
+ if (control != nullptr)
+ {
+ ::SetCapture(hwnd_);
+ std::swap(mouse_capture_control_, control);
+ DispatchMouseHoverControlChangeEvent(control ? control : mouse_hover_control_, mouse_capture_control_, GetMousePosition());
+ return control;
+ }
+ else
+ {
+ return ReleaseCurrentMouseCapture();
+ }
+ }
+
+ Control* Window::ReleaseCurrentMouseCapture()
+ {
+ if (mouse_capture_control_)
+ {
+ const auto previous = mouse_capture_control_;
+ mouse_capture_control_ = nullptr;
+ ::ReleaseCapture();
+ DispatchMouseHoverControlChangeEvent(previous, mouse_hover_control_, GetMousePosition());
+ return previous;
+ }
+ else
+ {
+ return nullptr;
+ }
+ }
+
RECT Window::GetClientRectPixel() {
RECT rect{ };
GetClientRect(hwnd_, &rect);
@@ -451,12 +512,9 @@ namespace cru
DispatchEvent(focus_control_, &Control::OnLoseFocusCore, nullptr);
}
- void Window::OnMouseMoveInternal(POINT point)
+ void Window::OnMouseMoveInternal(const POINT point)
{
- Point dip_point(
- graph::PixelToDipX(point.x),
- graph::PixelToDipY(point.y)
- );
+ const auto dip_point = PiToDip(point);
//when mouse was previous outside the window
if (mouse_hover_control_ == nullptr) {
@@ -471,21 +529,18 @@ namespace cru
//Find the first control that hit test succeed.
const auto new_control_mouse_hover = HitTest(dip_point);
+ const auto old_control_mouse_hover = mouse_hover_control_;
+ mouse_hover_control_ = new_control_mouse_hover;
- if (new_control_mouse_hover != mouse_hover_control_) //if the mouse-hover-on control changed
+ if (mouse_capture_control_) // if mouse is captured
{
- const auto lowest_common_ancestor = FindLowestCommonAncestor(mouse_hover_control_, new_control_mouse_hover);
- if (mouse_hover_control_ != nullptr) // if last mouse-hover-on control exists
- {
- // dispatch mouse leave event.
- DispatchEvent(mouse_hover_control_, &Control::OnMouseLeaveCore, lowest_common_ancestor);
- }
- mouse_hover_control_ = new_control_mouse_hover;
- // dispatch mouse enter event.
- DispatchEvent(new_control_mouse_hover, &Control::OnMouseEnterCore, lowest_common_ancestor, dip_point);
+ DispatchEvent(mouse_capture_control_, &Control::OnMouseMoveCore, nullptr, dip_point);
+ }
+ else
+ {
+ DispatchMouseHoverControlChangeEvent(old_control_mouse_hover, new_control_mouse_hover, dip_point);
+ DispatchEvent(new_control_mouse_hover, &Control::OnMouseMoveCore, nullptr, dip_point);
}
-
- DispatchEvent(new_control_mouse_hover, &Control::OnMouseMoveCore, nullptr, dip_point);
}
void Window::OnMouseLeaveInternal()
@@ -496,26 +551,42 @@ namespace cru
void Window::OnMouseDownInternal(MouseButton button, POINT point)
{
- Point dip_point(
- graph::PixelToDipX(point.x),
- graph::PixelToDipY(point.y)
- );
+ const auto dip_point = PiToDip(point);
- const auto control = HitTest(dip_point);
+ Control* control;
+
+ if (mouse_capture_control_)
+ control = mouse_capture_control_;
+ else
+ control = HitTest(dip_point);
DispatchEvent(control, &Control::OnMouseDownCore, nullptr, dip_point, button);
}
void Window::OnMouseUpInternal(MouseButton button, POINT point)
{
- Point dip_point(
- graph::PixelToDipX(point.x),
- graph::PixelToDipY(point.y)
- );
+ const auto dip_point = PiToDip(point);
- const auto control = HitTest(dip_point);
+ Control* control;
+
+ if (mouse_capture_control_)
+ control = mouse_capture_control_;
+ else
+ control = HitTest(dip_point);
DispatchEvent(control, &Control::OnMouseUpCore, nullptr, dip_point, button);
}
+
+ void Window::DispatchMouseHoverControlChangeEvent(Control* old_control, Control* new_control, const Point& point)
+ {
+ if (new_control != old_control) //if the mouse-hover-on control changed
+ {
+ const auto lowest_common_ancestor = FindLowestCommonAncestor(old_control, new_control);
+ if (old_control != nullptr) // if last mouse-hover-on control exists
+ DispatchEvent(old_control, &Control::OnMouseLeaveCore, lowest_common_ancestor); // dispatch mouse leave event.
+ if (new_control != nullptr)
+ DispatchEvent(new_control, &Control::OnMouseEnterCore, lowest_common_ancestor, point); // dispatch mouse enter event.
+ }
+ }
}
}
diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h
index 9ffa4dc6..551e2048 100644
--- a/CruUI/ui/window.h
+++ b/CruUI/ui/window.h
@@ -172,6 +172,7 @@ namespace cru {
//Return false if the message is not handled.
bool HandleWindowMessage(HWND hwnd, int msg, WPARAM w_param, LPARAM l_param, LRESULT& result);
+ Point GetMousePosition();
//*************** region: position and size ***************
@@ -207,6 +208,11 @@ namespace cru {
Control* GetFocusControl();
+ //*************** region: mouse capture ***************
+
+ Control* CaptureMouseFor(Control* control);
+ Control* ReleaseCurrentMouseCapture();
+
private:
//*************** region: native operations ***************
@@ -230,8 +236,6 @@ namespace cru {
void OnMouseDownInternal(MouseButton button, POINT point);
void OnMouseUpInternal(MouseButton button, POINT point);
-
-
//*************** region: event dispatcher helper ***************
template<typename EventArgs>
@@ -258,6 +262,8 @@ namespace cru {
}
}
+ void DispatchMouseHoverControlChangeEvent(Control* old_control, Control * new_control, const Point& point);
+
private:
std::unique_ptr<WindowLayoutManager> layout_manager_;
@@ -270,6 +276,8 @@ namespace cru {
bool window_focus_ = false;
Control* focus_control_ = this; // "focus_control_" can't be nullptr
+
+ Control* mouse_capture_control_ = nullptr;
};
}
}