-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCtrlHelper.cpp
More file actions
570 lines (463 loc) · 16.2 KB
/
CtrlHelper.cpp
File metadata and controls
570 lines (463 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//
//
//
#include "defines.h"
#include "CtrlHelper.h"
namespace Dcx
{
// ListView
TString dcxListView_GetItemText(_In_ HWND hwnd, _In_ int nItem, _In_ int nSubItem)
{
TString tsRes(gsl::narrow_cast<TString::size_type>(MIRC_BUFFER_SIZE_CCH));
dcxListView_GetItemText(hwnd, nItem, nSubItem, tsRes.to_chr(), tsRes.capacity_cch());
return tsRes;
}
size_t dcxListView_GetItemTextLength(_In_ HWND hwnd, _In_ int nItem, _In_ int nSubItem)
{
// Ook: why doesnt the control already have a message to do this when ListBox does?
TString tsRes;
LVITEM lvi{};
lvi.iSubItem = nSubItem;
size_t len{}, cap{}, check{};
do {
cap = tsRes.capacity_cch();
check = (cap - 1);
lvi.pszText = tsRes.to_chr();
lvi.cchTextMax = gsl::narrow_cast<int>(cap);
len = gsl::narrow_cast<size_t>(SendMessage(hwnd, LVM_GETITEMTEXT, nItem, reinterpret_cast<LPARAM>(&lvi)));
if (len >= check)
tsRes.reserve(cap * 2);
} while (len >= check);
return len;
}
int dcxListView_GetItemAtPos(_In_ HWND hwnd, _In_ int x, _In_ int y) noexcept
{
if (!hwnd)
return -1;
LVHITTESTINFO lvhti{};
lvhti.pt.x = x;
lvhti.pt.y = y;
// X & Y are relative to screen area.
MapWindowPoints(nullptr, hwnd, &lvhti.pt, 1);
Dcx::dcxListView_HitTest(hwnd, &lvhti);
// Out of the ListView?
if (lvhti.iItem == -1)
return -1;
// Not in an item?
if (!dcx_testflag(lvhti.flags, LVHT_ONITEMICON) && !dcx_testflag(lvhti.flags, LVHT_ONITEMLABEL) && !dcx_testflag(lvhti.flags, LVHT_ONITEMSTATEICON))
return -1;
return lvhti.iItem;
}
int dcxListView_GetItemAtCursor(_In_ HWND hwnd) noexcept
{
if (!hwnd)
return -1;
POINT pt{};
if (!GetCursorPos(&pt))
return -1;
return dcxListView_GetItemAtPos(hwnd, pt.x, pt.y);
}
LVHITTESTINFO dcxListView_CursorHitTest(_In_ HWND hwnd) noexcept
{
LVHITTESTINFO lvht{};
if (!hwnd)
return lvht;
if (!GetCursorPos(&lvht.pt))
return lvht;
// X & Y are relative to screen area.
MapWindowPoints(nullptr, hwnd, &lvht.pt, 1);
dcxListView_SubItemHitTest(hwnd, &lvht);
return lvht;
}
// ListBox
[[nodiscard]] TString dcxListBox_GetText(_In_ HWND hwnd, _In_ int i)
{
TString tsBuf;
const auto len = dcxListBox_GetTextLen(hwnd, i);
if (len < 1)
return tsBuf;
tsBuf.reserve(len + 1U);
dcxListBox_GetText(hwnd, i, tsBuf.to_chr());
return tsBuf;
}
int dcxListBox_GetPointItem(_In_ HWND hListbox, _In_ POINT pt) noexcept
{
if (!hListbox)
return -1;
//// do we have any items?
//if (const auto iCnt = dcxListBox_GetCount(hListbox); iCnt > 0)
//{
// RECT rc{};
//
// // check point is in client area.
// if (GetClientRect(hListbox, &rc))
// {
// if (!PtInRect(&rc, pt))
// return -1;
// }
//
// //ListBox_GetTopIndex(hwnd) used instead of zero to make the loop quicker.
//
// // loop through items & check for a match
// for (int i = dcxListBox_GetTopIndex(hListbox); i < iCnt; ++i)
// {
// // get items rect
// if (!dcxListBox_GetItemRect(hListbox, i, &rc))
// break;
//
// // check if point is within this item.
// if (PtInRect(&rc, pt))
// return i;
// }
//}
//return -1;
return LBItemFromPt(hListbox, pt, FALSE);
}
int dcxListBox_GetHoverItem(_In_ HWND hListbox) noexcept
{
if (!hListbox)
return -1;
//return dcxListBox_GetPointItem(hListbox, Dcx::dcxCursorPos(hListbox));
return dcxListBox_GetPointItem(hListbox, Dcx::dcxCursorPos());
}
bool dcxListBox_HitTest(_In_ HWND hListbox, _Inout_ LPLVHITTESTINFO phti) noexcept
{
if (!hListbox || !phti)
return false;
RECT rc{};
// check point is in client area.
if (!GetClientRect(hListbox, &rc))
return false;
if (PtInRect(&rc, phti->pt))
{
// in client rect
phti->flags = LVHT_NOWHERE;
// do we have any items?
if (const auto iCnt = dcxListBox_GetCount(hListbox); iCnt > 0)
{
//ListBox_GetTopIndex(hwnd) used instead of zero to make the loop quicker.
// loop through items & check for a match
for (int i = dcxListBox_GetTopIndex(hListbox); i < iCnt; ++i)
{
// get items rect
if (!Dcx::dcxListBox_GetItemRect(hListbox, i, &rc))
break;
// check if point is within this item.
if (PtInRect(&rc, phti->pt))
{
phti->flags = LVHT_ONITEMLABEL;
phti->iItem = i;
break;
}
}
}
}
else {
// not in client rect
if (phti->pt.y < rc.top)
phti->flags |= LVHT_ABOVE;
else if (phti->pt.y > rc.bottom)
phti->flags |= LVHT_BELOW;
if (phti->pt.x < rc.left)
phti->flags |= LVHT_TOLEFT;
else if (phti->pt.x > rc.right)
phti->flags |= LVHT_TORIGHT;
}
return true;
}
// Toolbar
TString dcxToolbar_GetButtonText(HWND hwnd, UINT uIndex)
{
const auto iLen = dcxToolbar_GetButtonText(hwnd, uIndex, nullptr);
TString tsText(gsl::narrow_cast<TString::size_type>(iLen + 1));
dcxToolbar_GetButtonText(hwnd, uIndex, tsText.to_chr());
return tsText;
}
/// <summary>
/// Sets the highlight state of a given button in a toolbar control.
/// </summary>
/// <param name="hwnd">- A handle to the control.</param>
/// <param name="uCmdId">- Command identifier for a toolbar button.</param>
/// <param name="fHighlight">- Indicates the new highlight state. If TRUE, the button is highlighted. If FALSE, the button is set to its default state.</param>
/// <returns>Returns nonzero if successful, or zero otherwise.</returns>
int dcxToolbar_MarkButton(HWND hwnd, UINT uCmdId, bool fHighlight) noexcept
{
return gsl::narrow_cast<int>(SendMessage(hwnd, TB_MARKBUTTON, gsl::narrow_cast<WPARAM>(uCmdId), Dcx::dcxMAKELPARAM(gsl::narrow_cast<uint8_t>(fHighlight), gsl::narrow_cast<uint8_t>(0))));
}
/// <summary>
/// Sets the state for the specified button in a toolbar.
/// </summary>
/// <param name="hwnd">- A handle to the control.</param>
/// <param name="uCmdId">- Command identifier for a toolbar button.</param>
/// <param name="uFlags">- A combination of values listed in Toolbar Button States.</param>
/// <returns>Returns TRUE if successful, or FALSE otherwise.</returns>
bool dcxToolbar_SetState(HWND hwnd, UINT uCmdId, UINT uFlags) noexcept
{
return !!SendMessage(hwnd, TB_SETSTATE, uCmdId, Dcx::dcxMAKELPARAM(gsl::narrow_cast<uint8_t>(uFlags), gsl::narrow_cast<uint8_t>(0)));
}
// TreeView
size_t dcxTreeView_GetCount(_In_ HWND hTree) noexcept
{
if (!hTree)
return 0U;
// NB: This macro returns values 0 - 32767 ok, but 32768 - 65536 are returned as negatives, & anything > 65536 returns as zero & the treeview fails to display.
// cant use TreeView_GetCount() macro as this types the result as unsigned.
const auto cnt = SendMessage(hTree, TVM_GETCOUNT, 0, 0);
if (cnt < 0)
return (gsl::narrow_cast<size_t>(std::abs(cnt)) + 32767U);
return gsl::narrow_cast<size_t>(cnt);
}
GSL_SUPPRESS(type.4)
[[nodiscard]] HTREEITEM dcxTreeView_GetLastSibling(_In_ HWND hTree, _In_ HTREEITEM child) noexcept
{
if (!child || !hTree)
return nullptr;
HTREEITEM current{};
for (HTREEITEM tmp = dcxTreeView_GetNextSibling(hTree, child); tmp; tmp = dcxTreeView_GetNextSibling(hTree, current))
current = tmp;
return current;
}
void dcxTreeView_SetItemState(_In_ HWND hTree, _In_ HTREEITEM hItem, _In_ UINT data, _In_ UINT mask) noexcept
{
if (!hTree)
return;
//TreeView_SetItemState(m_Hwnd, hItem, data, mask);
TVITEMEX _ms_TVi{};
_ms_TVi.mask = TVIF_STATE | TVIF_HANDLE;
_ms_TVi.hItem = hItem;
_ms_TVi.state = data;
_ms_TVi.stateMask = mask;
dcxTreeView_SetItem(hTree, std::addressof(_ms_TVi));
}
TString dcxTreeView_GetItemText(_In_ HWND hTree, _In_ HTREEITEM hItem)
{
TString tsRes;
TVITEMEX tvitem{};
TCHAR buf[MIRC_BUFFER_SIZE_CCH]{};
tvitem.hItem = hItem;
tvitem.mask = TVIF_TEXT | TVIF_HANDLE;
tvitem.pszText = &buf[0];
tvitem.cchTextMax = gsl::narrow_cast<int>(std::size(buf));
if (dcxTreeView_GetItem(hTree, &tvitem))
tsRes = tvitem.pszText;
return tsRes;
}
void dcxTreeView_SetItemText(_In_ HWND hTree, _In_ HTREEITEM hItem, _In_ const TString& txt) noexcept
{
if (!hTree)
return;
TVITEMEX tvi{};
tvi.mask = TVIF_TEXT | TVIF_HANDLE;
tvi.hItem = hItem;
GSL_SUPPRESS(type.3) tvi.pszText = const_cast<TCHAR*>(txt.to_chr());
dcxTreeView_SetItem(hTree, &tvi);
}
bool dcxTreeView_DeleteItem(_In_ HWND hTree, _In_ const HTREEITEM item) noexcept
{
if (!hTree)
return false;
// If the window style for a tree-view control contains TVS_SCROLL and all items are deleted,
// new items are not displayed until the window styles are reset.
// The following code shows one way to ensure that items are always displayed.
const auto styles = dcxGetWindowStyle(hTree);
Auto(dcxSetWindowStyle(hTree, styles));
return (TreeView_DeleteItem(hTree, item) != FALSE);
}
bool dcxTreeView_DeleteAllItems(_In_ HWND hTree) noexcept
{
if (!hTree)
return false;
// If the window style for a tree-view control contains TVS_SCROLL and all items are deleted,
// new items are not displayed until the window styles are reset.
// The following code shows one way to ensure that items are always displayed.
const auto styles = dcxGetWindowStyle(hTree);
if (!dcx_testflag(styles, TVS_NOSCROLL))
{
// If a TreeView control doesn't have the TVS_NOSCROLL style it can loose the scrollbars when all items are deleted.
// This fixes that.
dcxSetWindowStyle(hTree, styles | TVS_NOSCROLL);
}
Auto(dcxSetWindowStyle(hTree, styles));
return (TreeView_DeleteAllItems(hTree) != FALSE);
}
TVHITTESTINFO dcxTreeView_GetCursorItem(_In_ HWND hTree) noexcept
{
TVHITTESTINFO tvh{};
if (!hTree)
return tvh;
if (!GetCursorPos(&tvh.pt))
return tvh;
MapWindowPoints(nullptr, hTree, &tvh.pt, 1);
dcxTreeView_HitTest(hTree, &tvh);
return tvh;
}
namespace details
{
static HTREEITEM dcxTreeView_WalkChildItems(_In_ HWND mHwnd, _In_ HTREEITEM hParent, _Inout_ size_t& nCnt, _In_ const size_t& nIndex) noexcept
{
if ((!mHwnd) || (!hParent))
return nullptr;
for (auto hItem = Dcx::dcxTreeView_GetChild(mHwnd, hParent); hItem; hItem = Dcx::dcxTreeView_GetNextSibling(mHwnd, hItem))
{
if (++nCnt == nIndex)
return hItem;
if (auto hRes = dcxTreeView_WalkChildItems(mHwnd, hItem, nCnt, nIndex); hRes)
return hRes;
}
return nullptr;
}
}
[[nodiscard]] HTREEITEM dcxTreeView_MapIndexToItem(_In_ HWND hTree, _In_ size_t nIndex) noexcept
{
if ((!hTree) || (!IsWindow(hTree)))
return nullptr;
size_t nCnt{};
for (auto hItem = Dcx::dcxTreeView_GetRoot(hTree); hItem; hItem = Dcx::dcxTreeView_GetNextSibling(hTree, hItem))
{
if (++nCnt == nIndex)
return hItem;
if (auto hRes = details::dcxTreeView_WalkChildItems(hTree, hItem, nCnt, nIndex); hRes)
return hRes;
}
return nullptr;
}
TString dcxButton_GetNote(_In_ HWND hwnd)
{
TString tsBuf;
if (auto len = Dcx::dcxButton_GetNoteLength(hwnd); len > 0)
{
tsBuf.reserve(++len);
DWORD l{ gsl::narrow_cast<DWORD>(len) };
dcxButton_GetNote(hwnd, tsBuf.to_chr(), &l);
}
return tsBuf;
}
UINT dcxStatusBar_GetTextLength(_In_ HWND hwnd, _In_ int iPart) noexcept
{
return gsl::narrow_cast<UINT>(Dcx::dcxLOWORD(SendMessage(hwnd, SB_GETTEXTLENGTHW, gsl::narrow_cast<WPARAM>(iPart), 0)));
}
TString dcxStatusBar_GetText(HWND hwnd, int iPart)
{
TString tsText(gsl::narrow_cast<TString::size_type>(dcxStatusBar_GetTextLength(hwnd, iPart) + 1));
dcxStatusBar_GetText(hwnd, iPart, tsText.to_chr());
return tsText;
}
UINT dcxStatusBar_GetPartFlags(_In_ HWND hwnd, _In_ int iPart) noexcept
{
return gsl::narrow_cast<UINT>(Dcx::dcxHIWORD(SendMessage(hwnd, SB_GETTEXTLENGTHW, gsl::narrow_cast<WPARAM>(iPart), 0)));
}
TString dcxStatusBar_GetTipText(_In_ HWND hwnd, _In_ int iPart)
{
TString tsRes(gsl::narrow_cast<TString::size_type>(MIRC_BUFFER_SIZE_CCH));
dcxStatusBar_GetTipText(hwnd, iPart, gsl::narrow_cast<int>(tsRes.capacity_cch()), tsRes.to_chr());
return tsRes;
}
EC_ENDOFLINE dcxEdit_GetEndOfLine(_In_ HWND hwnd) noexcept
{
// EC_ENDOFLINE_CRLF one The end - of - line character used for new linebreaks is carriage return followed by linefeed(CRLF).
// EC_ENDOFLINE_CR two The end - of - line character used for new linebreaks is carriage return (CR).
// EC_ENDOFLINE_LF three The end - of - line character used for new linebreaks is linefeed(LF).
// When the end-of-line character used is set to EC_ENDOFLINE_DETECTFROMCONTENT using Edit_SetEndOfLine, this message will return the detected end-of-line character.
if (hwnd && Dcx::VersInfo.isWin10())
return Edit_GetEndOfLine(hwnd);
// use CRLF if not win10+
return EC_ENDOFLINE_CRLF;
}
bool dcxEdit_GetZoom(_In_ HWND hwnd, _In_ int* nNumerator, _In_ int* nDenominator) noexcept
{
//Supported in Windows 10 1809 and later. The edit control needs to have the ES_EX_ZOOMABLE extended style set, for this message to have an effect
//(the zoom ratio is always between 1/64 and 64) NOT inclusive, 1.0 = no zoom
if (!hwnd || !nNumerator || !nDenominator)
return false;
if (Dcx::VersInfo.isWin10())
return Edit_GetZoom(hwnd, nNumerator, nDenominator);
*nNumerator = 1;
*nDenominator = 0;
return true;
}
TString dcxEdit_GetEndOfLineCharacters(_In_ HWND hwnd)
{
switch (Dcx::dcxEdit_GetEndOfLine(hwnd))
{
case EC_ENDOFLINE_LF:
return TEXT("\n");
case EC_ENDOFLINE_CR:
return TEXT("\r");
case EC_ENDOFLINE_CRLF:
default:
break;
}
return TEXT("\r\n");
}
DWORD dcxEdit_GetCaretIndex(_In_ HWND hwnd) noexcept
{
if (!hwnd)
return 0;
// windows 10 only
if (Dcx::VersInfo.isWin10())
return Edit_GetCaretIndex(hwnd);
DWORD hiPos{}, loPos{};
dcxEdit_GetSel(hwnd, &loPos, &hiPos);
if (loPos != hiPos)
--hiPos;
return hiPos;
}
void dcxEdit_SetCaretIndex2(_In_ HWND hwnd, _In_ DWORD nPos) noexcept
{
if (!hwnd)
return;
// windows 10 only
if (Dcx::VersInfo.isWin10())
dcxEdit_SetCaretIndex(hwnd, nPos);
else
dcxEdit_SetSel(hwnd, nPos, nPos);
}
DWORD dcxEdit_CharFromPos(_In_ HWND hwnd, _In_ LONG ihPos, _In_ LONG ivPos) noexcept
{
if (!hwnd)
return 0;
return gsl::narrow_cast<DWORD>(SNDMSG(hwnd, EM_CHARFROMPOS, 0, Dcx::dcxMAKELPARAM(ihPos, ivPos)));
}
DWORD dcxEdit_LineFromChar(_In_ HWND hwnd, _In_ const LONG& ich) noexcept
{
if (!hwnd)
return 0;
return gsl::narrow_cast<DWORD>(Edit_LineFromChar(hwnd, ich));
}
int dcxEdit_GetSel(_In_ HWND hwnd, _In_opt_ DWORD* nStart, _In_opt_ DWORD* nEnd) noexcept
{
if (!hwnd)
return -1;
return gsl::narrow_cast<int>(SendMessage(hwnd, EM_GETSEL, reinterpret_cast<WPARAM>(nStart), reinterpret_cast<LPARAM>(nEnd)));
}
/// <summary>
/// Sets the background color for a rich edit control.
/// </summary>
/// <param name="hwnd">- A handle to the control.</param>
/// <param name="bUseSystem">- Specifies whether to use the system color. If this parameter is a nonzero value, the background is set to the window background system color. Otherwise, the background is set to the specified color.</param>
/// <param name="clr">- A COLORREF structure specifying the color if wParam is zero. To generate a COLORREF, use the RGB macro.</param>
/// <returns>The original background color.</returns>
COLORREF dcxRichEdit_SetBkgndColor(HWND hwnd, BOOL bUseSystem, COLORREF clr) noexcept
{
return gsl::narrow_cast<COLORREF>(SendMessage(hwnd, EM_SETBKGNDCOLOR, gsl::narrow_cast<WPARAM>(bUseSystem), gsl::narrow_cast<LPARAM>(clr)));
}
COLORREF dcxRichEdit_GetBkgndColor(HWND hwnd) noexcept
{
const auto clr = dcxRichEdit_SetBkgndColor(hwnd, TRUE, 0);
dcxRichEdit_SetBkgndColor(hwnd, FALSE, clr);
return clr;
}
int dcxTabCtrl_GetPointItem(HWND hwnd, POINT pt) noexcept
{
TCHITTESTINFO tti{};
tti.pt = pt;
const auto iTab = TabCtrl_HitTest(hwnd, &tti);
if (tti.flags & TCHT_ONITEM)
return iTab;
return -1;
}
int dcxTabCtrl_GetHoverItem(HWND hwnd) noexcept
{
return dcxTabCtrl_GetPointItem(hwnd, Dcx::dcxCursorPos(hwnd));
}
}