OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
syslink.cpp
Go to the documentation of this file.
1//
2/// \file
3/// Implementation of class TSysLink
4//
5// Part of OWLNext - the next generation Object Windows Library
6// Copyright (c) 2014 Vidar Hasfjord
7//
8// For more information, including license details, see
9// http://owlnext.sourceforge.net
10//
11
12#include <owl/pch.h>
13#include <owl/syslink.h>
14#include <shellapi.h>
15
16namespace owl
17{
18
19//
20/// Constructs a SysLink from arguments.
21/// The provided text can include HTML anchor tags, and if so, these will be rendered as links.
22//
24 TWindow* parent,
25 int id,
26 const tstring& markupText,
27 const TRect& r,
28 TModule* module
29 )
30 : TControl(parent, id, markupText, r.Left(), r.Right(), r.Width(), r.Height(), module)
31{
33}
34
35//
36/// Constructs a SysLink from resource.
37//
43
44//
45/// Constructs an alias.
46//
52
53//
54/// Retrieves the preferred height of the control for the control's current width.
55/// Returns the preferred height of the control, in pixels.
56//
57auto TSysLink::GetIdealHeight() -> int const
58{
59 const auto r = SendMessage(LM_GETIDEALHEIGHT);
60 return static_cast<int>(r); // preferred height
61}
62
63//
64/// Retrieves the ideal size of the control for a given maximum width.
65/// Returns the preferred height of the control, in pixels.
66//
67auto TSysLink::GetIdealSize(int maxWidth, TSize& out) -> int const
68{
69 const auto p1 = static_cast<TParam1>(maxWidth);
70 const auto p2 = reinterpret_cast<TParam2>(&out);
71 const auto r = SendMessage(LM_GETIDEALSIZE, p1, p2);
72 return static_cast<int>(r); // preferred height
73}
74
75//
76/// Functional-style overload.
77//
79{
80 auto s = TSize{};
81 [[maybe_unused]] const auto h = GetIdealSize(maxWidth, s);
82 CHECK(h == s.Y());
83 return s;
84}
85
86//
87/// Retrieves the states and attributes of the specified anchor item.
88/// Note that you need to set the LIF_ITEMINDEX flag in LITEM::mask and provide the index of the
89/// requested item in LITEM::iLink before calling the function. You also need to set LITEM::mask
90/// and LITEM::stateMask to include the states and attributes you want to query. See MSDN:
91/// http://msdn.microsoft.com/en-us/library/windows/desktop/bb760710.aspx
92/// Returns `true` if successful.
93//
94auto TSysLink::GetItem(LITEM& inOut) -> bool const
95{
97 const auto p2 = reinterpret_cast<TParam2>(&inOut);
98 const auto r = SendMessage(LM_GETITEM, 0, p2);
99 return r != FALSE;
100}
101
102//
103/// Functional-style overload.
104/// Returns an LITEM with all available state and attribute information filled in.
105//
106auto TSysLink::GetItem(int index) -> LITEM const
107{
108 auto i = LITEM
109 {
110 0xFFFFFFFF, // mask
111 index,
112 0, // state
113 0xFFFFFFFF // stateMask
114 };
115 const auto r = GetItem(i);
116 if (!r) throw TXOwl(_T("TSysLink::GetItem failed"));
117 return i;
118}
119
120//
121/// Returns the ID attribute of the specified anchor item.
122//
123auto TSysLink::GetId(int index) -> tstring const
124{
126 return _W2A_A(GetItem(index).szID);
127}
128
129//
130/// Returns the HREF attribute of the specified anchor item.
131//
132auto TSysLink::GetUrl(int index) -> tstring const
133{
135 return _W2A_A(GetItem(index).szUrl);
136}
137
138//
139/// Sets the states and attributes of the specified anchor item.
140/// Note that you need to set the LIF_ITEMINDEX flag in LITEM::mask and provide the index of the
141/// requested item in LITEM::iLink before calling the function. You also need to set LITEM::mask
142/// and LITEM::stateMask to include the states and attributes you want to modify. See MSDN:
143/// http://msdn.microsoft.com/en-us/library/windows/desktop/bb760710.aspx
144/// Returns `true` if successful.
145//
146auto TSysLink::SetItem(const LITEM& i) -> bool
147{
149 const auto p2 = reinterpret_cast<TParam2>(&i);
150 const auto r = SendMessage(LM_SETITEM, 0, p2);
151 return r != FALSE;
152}
153
154//
155/// Performs a hit test on this control and returns information about the link hit, if any.
156/// Note that you need to set LHITTESTINFO::pt with the location of the hit-test before calling
157/// the function, and the location must be set in client coordinates (not screen coordinates).
158/// See MSDN:
159/// http://msdn.microsoft.com/en-us/library/windows/desktop/bb760708.aspx
160/// Returns `true` if a link was hit.
161//
163{
164 const auto p2 = reinterpret_cast<TParam2>(&inOut);
165 const auto r = SendMessage(LM_HITTEST, 0, p2);
166 return r != FALSE;
167}
168
169//
170/// Performs a hit-test on the given location.
171/// If an anchor item is hit, information about the item is returned in a unique_ptr. Otherwise a
172/// null-pointer is returned.
173//
174// TODO: When std::optional is available, use it instead of unique_ptr here.
175//
176auto TSysLink::HitTest(const TPoint& p) -> std::unique_ptr<LITEM> const
177{
178 auto i = LHITTESTINFO{p};
179 const auto r = HitTest(i);
180 return r ? std::make_unique<LITEM>(i.item) : nullptr;
181}
182
183//
184/// Returns the class name for a SysLink control.
185//
187{
188 PRECONDITION(WC_LINK == std::wstring{L"SysLink"});
189 return TWindowClassName{_T("SysLink")};
190}
191
192//-------------------------------------------------------------------------------------------------
193
195 EV_NM_CLICK(UINT_MAX, EvFollowLink),
196 EV_NM_RETURN(UINT_MAX, EvFollowLink),
198
199//
200/// Constructs a SysLink from arguments.
201/// The provided text can include HTML anchor tags, and if so, these will be rendered as links.
202//
204 TWindow* parent,
205 int id,
206 const tstring& markupText,
207 const TRect& r,
208 TModule* module
209 )
210 : TSysLink(parent, id, markupText, r, module)
211{}
212
213//
214/// Constructs a SysLink from resource.
215//
217 : TSysLink(parent, resourceId, module)
218{}
219
220//
221/// Constructs an alias.
222//
226
227void TAutoSysLink::EvFollowLink()
228{
229 auto a = reinterpret_cast<PNMLINK>(GetApplication()->GetCurrentEvent().Param2);
230 CHECK(a);
231 ShellExecuteW(nullptr, L"open", a->item.szUrl, nullptr, nullptr, SW_SHOW);
232}
233
234} // OWL namespace
235
#define CHECK(condition)
Definition checks.h:239
#define PRECONDITION(condition)
Definition checks.h:227
TCurrentEvent & GetCurrentEvent()
Return the current event from the message queue.
Definition applicat.h:652
TControl unifies its derived control classes, such as TScrollBar, TControlGadget, and TButton.
Definition control.h:38
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
The tagSIZE struct is defined as.
Definition geometry.h:234
Type-safe encapsulation of a Windows class name, a union between ATOM and LPCTSTR.
Definition module.h:47
TWindow, derived from TEventHandler and TStreamableBase, provides window-specific behavior and encaps...
Definition window.h:414
TApplication * GetApplication() const
Gets a pointer to the TApplication object associated with this.
Definition window.h:1855
int GetId() const
Returns Attr.Id, the ID used to find the window in a specified parent's child list.
Definition window.h:1881
HWND THandle
TWindow encapsulates an HWND.
Definition window.h:418
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define EV_NM_CLICK(id, method)
void method()
Definition commctrl.h:1105
#define EV_NM_RETURN(id, method)
void method()
Definition commctrl.h:1160
#define _T(x)
Definition cygwin.h:51
#define DEFINE_RESPONSE_TABLE1(cls, base)
Macro to define a response table for a class with one base.
Definition eventhan.h:492
#define _W2A_A(lpw)
Definition memory.h:223
#define _USES_CONVERSION_A
Definition memory.h:218
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InitializeCommonControls(uint controlFlags)
Wrapper for the Windows API function InitCommmonControlsEx.
Definition commctrl.cpp:19
END_RESPONSE_TABLE
Definition button.cpp:26
std::string tstring
Definition defs.h:79
TParam2 Param2
Second parameter (LPARAM)
Definition applicat.h:117