From e144f2d3ce645742f33eb48b7d2be2eaee4cca25 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Tue, 28 Jan 2025 09:05:15 +0100 Subject: [PATCH] RichEdit: Ctrl+Click now opens http and https links --- uppsrc/RichEdit/Editor.cpp | 111 ++++++++++++++++++++----------------- uppsrc/RichEdit/Mouse.cpp | 20 +++++-- uppsrc/RichEdit/RichEdit.h | 2 + 3 files changed, 77 insertions(+), 56 deletions(-) diff --git a/uppsrc/RichEdit/Editor.cpp b/uppsrc/RichEdit/Editor.cpp index 591b6f669..29ed10b04 100644 --- a/uppsrc/RichEdit/Editor.cpp +++ b/uppsrc/RichEdit/Editor.cpp @@ -662,6 +662,58 @@ void RichEdit::Skin() SetLastCharFormat(last_format); } + +void RichEditWithToolBar::TheBar(Bar& bar) +{ + DefaultBar(bar, extended); +} + +void RichEditWithToolBar::RefreshBar() +{ + toolbar.Set(THISBACK(TheBar)); +} + +void RichEditWithToolBar::Skin() +{ + RichEdit::Skin(); + RefreshBar(); +} + +void RichEdit::EvaluateFields() +{ + WhenStartEvaluating(); + text.EvaluateFields(vars); + Finish(); +} + +RichEditWithToolBar::RichEditWithToolBar() +{ + InsertFrame(0, toolbar); + WhenRefreshBar = callback(this, &RichEditWithToolBar::RefreshBar); + extended = true; +} + +bool RichEdit::IsDarkContent() const +{ + return dark_content || allow_dark_content && IsDarkTheme(); +} + +RichEdit& RichEdit::DarkContent(bool b) +{ + dark_content = b; + Refresh(); + DoRefreshBar(); + return *this; +} + +RichEdit& RichEdit::AllowDarkContent(bool b) +{ + allow_dark_content = b; + Refresh(); + DoRefreshBar(); + return *this; +} + RichEdit::RichEdit() { floating_zoom = Null; @@ -803,59 +855,16 @@ RichEdit::RichEdit() ignore_physical_size = false; SetLastCharFormat(); + + WhenLink = [](const String& s) { + LaunchWebBrowser(s); + }; + + WhenIsLink = [](const String& s) { + return s.StartsWith("http://") || s.StartsWith("https://"); + }; } RichEdit::~RichEdit() {} -void RichEditWithToolBar::TheBar(Bar& bar) -{ - DefaultBar(bar, extended); -} - -void RichEditWithToolBar::RefreshBar() -{ - toolbar.Set(THISBACK(TheBar)); -} - -void RichEditWithToolBar::Skin() -{ - RichEdit::Skin(); - RefreshBar(); -} - -void RichEdit::EvaluateFields() -{ - WhenStartEvaluating(); - text.EvaluateFields(vars); - Finish(); -} - -RichEditWithToolBar::RichEditWithToolBar() -{ - InsertFrame(0, toolbar); - WhenRefreshBar = callback(this, &RichEditWithToolBar::RefreshBar); - extended = true; -} - -bool RichEdit::IsDarkContent() const -{ - return dark_content || allow_dark_content && IsDarkTheme(); -} - -RichEdit& RichEdit::DarkContent(bool b) -{ - dark_content = b; - Refresh(); - DoRefreshBar(); - return *this; -} - -RichEdit& RichEdit::AllowDarkContent(bool b) -{ - allow_dark_content = b; - Refresh(); - DoRefreshBar(); - return *this; -} - } diff --git a/uppsrc/RichEdit/Mouse.cpp b/uppsrc/RichEdit/Mouse.cpp index f1aacf1c5..0825f4273 100644 --- a/uppsrc/RichEdit/Mouse.cpp +++ b/uppsrc/RichEdit/Mouse.cpp @@ -79,14 +79,14 @@ void RichEdit::LeftDown(Point p, dword flags) NextUndo(); SetFocus(); selclick = false; - tabmove = GetHotPos(p); + tabmove = GetHotPos(p); // resizing table columns if(tabmove.table && tabmove.column >= -2) { SaveTableFormat(tabmove.table); SetCapture(); Move(text.GetCellPos(tabmove.table, 0, max(tabmove.column, 0)).pos); return; } - int c = GetHotSpot(p); + int c = GetHotSpot(p); // resizing active object if(c >= 0 && objectpos >= 0) { int pos = objectpos; RectTracker tracker(*this); @@ -123,11 +123,16 @@ void RichEdit::LeftDown(Point p, dword flags) } else { c = GetMousePos(p); + RichPos pi = text.GetRichPos(c); if(c >= 0) { if(InSelection(c)) { selclick = true; return; } + if((flags & K_CTRL) && WhenIsLink(pi.format.link)) { + WhenLink(pi.format.link); + return; + } Move(c, flags & K_SHIFT); mpos = c; SetCapture(); @@ -422,7 +427,9 @@ Image RichEdit::CursorImage(Point p, dword flags) case 2: return Image::SizeHorz(); default: - if(text.GetRichPos(GetMousePos(p)).object) { + int c = GetMousePos(p); + RichPos pi = text.GetRichPos(c); + if(pi.object) { return Image::Arrow(); } else @@ -433,8 +440,11 @@ Image RichEdit::CursorImage(Point p, dword flags) if(hp.table > 0) return Image::SizeHorz(); else { - int c = GetMousePos(p); - return InSelection(c) && !HasCapture() ? Image::Arrow() : Image::IBeam(); + if(InSelection(c) && !HasCapture()) + return Image::Arrow(); + if((flags & K_CTRL) && WhenIsLink(pi.format.link)) + return Image::Hand(); + return Image::IBeam(); } } } diff --git a/uppsrc/RichEdit/RichEdit.h b/uppsrc/RichEdit/RichEdit.h index 83c99362b..16746a5b4 100644 --- a/uppsrc/RichEdit/RichEdit.h +++ b/uppsrc/RichEdit/RichEdit.h @@ -695,6 +695,8 @@ public: Event WhenIndexEntry; Event WhenBar; Event<> WhenSel; + Gate WhenIsLink; + Event WhenLink; void StdBar(Bar& menu);