/
Dcx.h
1616 lines (1396 loc) · 50.7 KB
/
Dcx.h
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
//#include "defines.h"
#include "mIRCLinker.h"
#include "DcxUXModule.h"
#include "DcxGDIModule.h"
#include "DcxDWMModule.h"
#include "DcxDPIModule.h"
#include "DcxWebView2Module.h"
#include "DcxDCompModule.h"
#include "Classes/xpopup/XPopupMenuManager.h"
#include "Classes/xpopup/xmenubar.h"
#include "Classes/DcxDialog.h"
#include "Classes\UltraDock\dcxDock.h"
#include <detours.h>
#include <concepts>
#include <utility>
#include <memory>
#include <iterator>
#include <type_traits>
#include <ranges>
//#if __has_include(<codecvt>)
//#include <codecvt>
//#endif
#include "DcxConcepts.h"
//#include "DcxTextRender.h"
// should DCX use the resource wrapper classes below? set to zero to disable
#define DCX_USE_WRAPPERS 1
#ifdef DCX_USE_WRAPPERS
#pragma message ("### DCX OPTION: Use Wrappers - Enabled")
#else
#pragma message ("### DCX OPTION: Use Wrappers - Disabled")
#endif
namespace Dcx
{
extern std::byte m_iGhostDrag;
extern IClassFactory* m_pClassFactory;
extern bool m_bDX9Installed;
extern DcxDialogCollection Dialogs;
extern DcxGDIModule GDIModule;
extern DcxUXModule UXModule;
extern DcxDWMModule DwmModule;
extern DcxDPIModule DpiModule;
extern DcxWebView2Module WebViewModule;
extern DcxDCompModule DCompModule;
/* additions */
extern XPopupMenuManager XPopups;
extern XMenuBar XMenubar;
extern bool m_bErrorTriggered;
// dragging
extern bool m_bShowingDragImage;
extern HIMAGELIST m_hDragImage;
extern DcxControl* m_pDragSourceCtrl;
// settings
extern bool setting_bStaticColours;
// global menu settings (only applies to currently displayed menu, used internally only)
extern std::byte m_CurrentMenuAlpha;
extern bool m_CurrentMenuRounded;
/// <summary>
/// Converts a Range to a std::vector.
/// take from: https://timur.audio/how-to-make-a-container-from-a-c20-range
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="r">- The range to convert</param>
/// <returns>a std::vector</returns>
template <class T>
[[nodiscard]] auto to_vector(const T& r)
{
std::vector<std::ranges::range_value_t<decltype(r)>> v;
if constexpr (std::ranges::sized_range<decltype(r)>)
v.reserve(std::ranges::size(r));
std::ranges::copy(r, std::back_inserter(v));
return v;
}
/// <summary>
/// 32bit Multiply and divide
/// </summary>
/// <param name="iNum"></param>
/// <param name="iMul"></param>
/// <param name="iDiv"></param>
/// <returns></returns>
[[nodiscard]] constexpr int32_t dcxMulDiv32(const int32_t iNum, const int32_t iMul, const int32_t iDiv) noexcept
{
return gsl::narrow_cast<int32_t>((gsl::narrow_cast<int64_t>(iNum) * gsl::narrow_cast<int64_t>(iMul)) / iDiv);
}
/// <summary>
/// Multiply and divide
/// Takes any bit width input & gives that bitwidth output. (max 64bit)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="iNum"></param>
/// <param name="iMul"></param>
/// <param name="iDiv"></param>
/// <returns></returns>
template <DcxConcepts::IsNumeric T>
[[nodiscard]] constexpr T dcxMulDiv(const T& iNum, const T& iMul, const T& iDiv) noexcept
{
return gsl::narrow_cast<T>((gsl::narrow_cast<int64_t>(iNum) * gsl::narrow_cast<int64_t>(iMul)) / iDiv);
}
/// <summary>
/// Rounds a number.
/// </summary>
/// <typeparam name="Result"></typeparam>
/// <typeparam name="Input"></typeparam>
/// <param name="in"></param>
/// <returns></returns>
template <DcxConcepts::IsNumeric Result, DcxConcepts::IsNumeric Input>
[[nodiscard]] constexpr Result dcxRound(const Input in) noexcept
{
const auto t = gsl::narrow_cast<Result>(in);
if ((in - gsl::narrow_cast<Input>(t)) > 0.5)
return t + Result{ 1 };
return t;
}
[[nodiscard("Memory Leak")]] HCURSOR dcxLoadCursorFromFile(const TString& filename);
[[nodiscard("Memory Leak")]] HCURSOR dcxLoadCursorFromResource(const TCHAR* CursorType);
//// determine whether _Ty is a Number type (excluding char / wchar)
//template<class _Ty>
//struct is_Numeric
// : std::integral_constant<bool,
// (std::is_arithmetic_v<_Ty> || std::is_same_v<_Ty, std::byte> || std::is_enum_v<_Ty>)
// && !std::is_same_v<_Ty, wchar_t>
// && !std::is_same_v<_Ty, char>
// && !std::is_pointer_v<_Ty>
// >
//{ // determine whether _Ty is a Number type (excluding char / wchar)
//};
//
//// determine whether T is a Number type (excluding char / wchar) & get the value
//template <typename T>
//constexpr bool is_Numeric_v = is_Numeric<T>::value;
// make_resource() function by Eric Scott Barr (http://ericscottbarr.com/blog/2014/04/c-plus-plus-14-and-sdl2-managing-resources/)
#if _MSC_VER > 1800
template<typename Creator, typename Destructor, typename... Arguments>
auto make_resource(Creator c, Destructor d, Arguments&&... args)
{
auto r = c(std::forward<Arguments>(args)...);
if (!r) { throw std::runtime_error{ "Unable to create resource" }; }
typedef typename std::decay<decltype(*r)>::type ResourceType;
return std::unique_ptr<ResourceType, void(*)(ResourceType*)>(r, d);
}
#else
template<typename Creator, typename Destructor, typename... Arguments>
auto make_resource(Creator c, Destructor d, Arguments&&... args)
-> std::unique_ptr<typename std::decay<decltype(*c(std::forward<Arguments>(args)...))>::type, void(*)(typename std::decay<decltype(*c(std::forward<Arguments>(args)...))>::type*)>
{
auto r = c(std::forward<Arguments>(args)...);
if (!r) { throw std::runtime_error{ "Unable to create resource" }; }
typedef typename std::decay<decltype(*r)>::type ResourceType;
return std::unique_ptr<ResourceType, void(*)(ResourceType*)>(r, d);
}
#endif
using dcxCursor_t = std::unique_ptr<HICON__, void(*)(HICON__*)>;
using dcxIcon_t = std::unique_ptr<HICON__, void(*)(HICON__*)>;
using dcxFile_t = std::unique_ptr<FILE, void(*)(FILE*)>;
using dcxHandle_t = std::unique_ptr<ULONG_PTR, void(*)(ULONG_PTR*)>;
using dcxHDC_t = std::unique_ptr<HDC__, void(*)(HDC)>;
using dcxBitmap_t = std::unique_ptr<HBITMAP__, void(*)(HBITMAP)>;
using dcxBSTR_t = std::unique_ptr<OLECHAR, void(*)(BSTR)>;
using dcxBRUSH_t = std::unique_ptr<HBRUSH__, void(*)(HBRUSH)>;
/// <summary>
/// Make a cursor resource
/// </summary>
/// <param name="tsFilename"></param>
/// <returns></returns>
inline dcxCursor_t make_cursor(const TString& tsFilename) {
return make_resource(dcxLoadCursorFromFile, [](HCURSOR hCursor) noexcept { if (hCursor) DestroyCursor(hCursor); }, tsFilename);
}
/// <summary>
/// Make an icon resource
/// </summary>
/// <param name="index"></param>
/// <param name="filename"></param>
/// <param name="large"></param>
/// <param name="flags"></param>
/// <returns></returns>
inline dcxIcon_t make_icon(const int index, TString& filename, const bool large, const TString& flags) {
return make_resource(dcxLoadIcon, [](HICON hIcon) noexcept { if (hIcon) DestroyIcon(hIcon); }, index, filename, large, flags);
}
/// <summary>
/// Make a file resource
/// </summary>
/// <param name="file"></param>
/// <param name="modes"></param>
/// <returns></returns>
inline dcxFile_t make_file(const WCHAR* file, const WCHAR* modes) { return make_resource(_wfopen, [](FILE* file) noexcept { if (file) fclose(file); }, file, modes); }
/// <summary>
/// Make an HDC resource
/// </summary>
/// <param name="hdc"></param>
/// <returns></returns>
inline dcxHDC_t make_hdc(HDC hdc) { return make_resource(CreateCompatibleDC, [](HDC obj) noexcept { if (obj) DeleteDC(obj); }, hdc); }
/// <summary>
/// Make a Bitmap resource
/// </summary>
/// <param name="hdc"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
inline dcxBitmap_t make_bitmap(HDC hdc, int x, int y) { return make_resource(CreateCompatibleBitmap, [](HBITMAP obj) noexcept { if (obj) DeleteBitmap(obj); }, hdc, x, y); }
//inline ULONG_PTR *dcxCreateFile(const TCHAR *file, DWORD dAccess, DWORD dShareMode, LPSECURITY_ATTRIBUTES lpSecurity, DWORD dCreation, DWORD dflags, HANDLE templateFile) {
// return (ULONG_PTR *)CreateFile(file, dAccess, dShareMode, lpSecurity, dCreation, dflags, templateFile);
//}
/// <summary>
/// Make a file handle resource
/// </summary>
/// <param name="file"></param>
/// <param name="dAccess"></param>
/// <param name="dShareMode"></param>
/// <param name="lpSecurity"></param>
/// <param name="dCreation"></param>
/// <param name="dflags"></param>
/// <param name="templateFile"></param>
/// <returns></returns>
inline dcxHandle_t make_filehandle(const TCHAR* file, DWORD dAccess, DWORD dShareMode, LPSECURITY_ATTRIBUTES lpSecurity, DWORD dCreation, DWORD dflags, HANDLE templateFile) {
//return make_resource(dcxCreateFile, [](ULONG_PTR *file){ CloseHandle((HANDLE)file); }, file, dAccess, dShareMode, lpSecurity, dCreation, dflags, templateFile);
return make_resource(
[](const TCHAR* file, DWORD dAccess, DWORD dShareMode, LPSECURITY_ATTRIBUTES lpSecurity, DWORD dCreation, DWORD dflags, HANDLE templateFile) noexcept { return static_cast<ULONG_PTR*>(CreateFile(file, dAccess, dShareMode, lpSecurity, dCreation, dflags, templateFile)); },
[](ULONG_PTR* file) noexcept { if ((file) && (file != INVALID_HANDLE_VALUE)) CloseHandle(file); }, file, dAccess, dShareMode, lpSecurity, dCreation, dflags, templateFile);
}
/// <summary>
/// Make a BSTR resource
/// </summary>
/// <param name="wstr"></param>
/// <returns></returns>
inline dcxBSTR_t make_bstr(const WCHAR* const wstr) { return make_resource(SysAllocString, [](BSTR obj) noexcept { if (obj) SysFreeString(obj); }, wstr); }
// NB: BaseType can be defined as some other pointer type
// so we static_cast it later on
template <typename Unique, typename BaseType = typename Unique::pointer>
struct dcxResource
{
dcxResource() = delete; // no default!
dcxResource(const dcxResource&) = delete; // no copy!
dcxResource& operator =(const dcxResource&) = delete; // No assignments!
dcxResource(dcxResource&&) = delete; // no move!
dcxResource& operator =(dcxResource&&) = delete; // No assignments!
//dcxResource(const Unique& m_uni) noexcept
// : m_uni(m_uni)
//{
//}
bool operator==(const dcxResource& other) const = default;
using pointer = typename Unique::pointer;
using element_type = typename Unique::element_type;
using return_type = BaseType;
constexpr dcxResource(Unique u) noexcept
: m_uni(std::move(u))
{
}
constexpr operator return_type() const noexcept { return get(); }
constexpr explicit operator bool() const noexcept { return (m_uni != nullptr); }
[[nodiscard("Memory Leak")]] constexpr BaseType release() const noexcept { return static_cast<BaseType>(m_uni.release()); }
constexpr return_type get() const noexcept { return (m_uni.get()); }
void reset(BaseType u) noexcept { m_uni.reset(u); }
private:
Unique m_uni;
};
struct dcxFileResource final
: dcxResource < dcxFile_t >
{
dcxFileResource() = delete; // no default constructor!
dcxFileResource(const dcxFileResource&) = delete; // no copy!
//dcxFileResource& operator =(const dcxFileResource&) = delete; // No assignments!
dcxFileResource(dcxFileResource&&) = delete; // no move constructor!
//dcxFileResource& operator =(dcxFileResource&&) = delete; // No move assignments!
// calls _wfopen()
dcxFileResource(const WCHAR* tsFilename, const WCHAR* tsMode)
: dcxResource(make_file(tsFilename, tsMode))
{
}
// calls _wfopen()
dcxFileResource(const TString& tsFilename, const WCHAR* tsMode)
: dcxResource(make_file(tsFilename.to_wchr(), tsMode))
{
}
using dcxResource::operator==;
//bool operator==(const dcxFileResource& other) const = default;
/// <summary>
/// Get the file size (32bit)
/// </summary>
/// <returns></returns>
const uint32_t Size() const noexcept
{
// Seek End of file
if (fseek(this->get(), 0, SEEK_END))
return 0UL;
// Read pointer location, because pointer is at the end, results into file size
const auto size = gsl::narrow_cast<uint32_t>(ftell(this->get()));
// Get back to file beginning
if (fseek(this->get(), 0, SEEK_SET))
return 0UL;
return size;
}
/// <summary>
/// Get the file size (64bit)
/// </summary>
/// <returns></returns>
const uint64_t SizeEx() const noexcept
{
// Seek End of file
if (_fseeki64(this->get(), 0, SEEK_END))
return 0ULL;
// Read pointer location, because pointer is at the end, results into file size
const auto size = gsl::narrow_cast<uint64_t>(_ftelli64(this->get()));
// Get back to file beginning
if (_fseeki64(this->get(), 0, SEEK_SET))
return 0ULL;
return size;
}
};
struct dcxFileHandleResource final
: dcxResource < dcxHandle_t >
{
dcxFileHandleResource() = delete; // no default!
dcxFileHandleResource(const dcxFileHandleResource&) = delete; // no copy!
//dcxFileHandleResource& operator =(const dcxFileHandleResource&) = delete; // No assignments!
dcxFileHandleResource(dcxFileHandleResource&&) = delete; // no copy!
//dcxFileHandleResource& operator =(dcxFileHandleResource&&) = delete; // No assignments!
// calls CreateFile()
dcxFileHandleResource(const TCHAR* file, DWORD dAccess, DWORD dShareMode, LPSECURITY_ATTRIBUTES lpSecurity, DWORD dCreation, DWORD dflags, HANDLE templateFile)
: dcxResource(make_filehandle(file, dAccess, dShareMode, lpSecurity, dCreation, dflags, templateFile))
{
}
/// <summary>
/// Get the file size (32bit)
/// </summary>
/// <returns></returns>
const auto Size() const noexcept
{
return GetFileSize(this->get(), nullptr);
}
/// <summary>
/// Get the file size (64bit)
/// </summary>
/// <returns></returns>
const LARGE_INTEGER SizeEx() const noexcept
{
LARGE_INTEGER sz{};
GetFileSizeEx(this->get(), &sz);
return sz;
}
};
struct dcxStringResource final
: dcxResource < std::unique_ptr<TCHAR[]> >
{
dcxStringResource() = delete; // no default!
dcxStringResource(const dcxStringResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxStringResource& operator =(const dcxStringResource&) = delete; // No assignments!
// calls std::make_unique<TCHAR[]>(uSize)
dcxStringResource(const size_t uSize)
: dcxResource(std::make_unique<TCHAR[]>(uSize))
{}
};
struct dcxBSTRResource final
: dcxResource < dcxBSTR_t >
{
dcxBSTRResource() = delete; // no default!
dcxBSTRResource(const dcxBSTRResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxBSTRResource& operator =(const dcxBSTRResource&) = delete; // No assignments!
dcxBSTRResource(dcxBSTRResource&&) = delete;
GSL_SUPPRESS(c.128) dcxBSTRResource& operator =(dcxBSTRResource&&) = delete;
dcxBSTRResource(const WCHAR* const wstr)
: dcxResource(make_bstr(wstr))
{}
};
struct dcxCursorResource final
: dcxResource < dcxCursor_t >
{
dcxCursorResource() = delete; // no default!
dcxCursorResource(const dcxCursorResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxCursorResource& operator =(const dcxCursorResource&) = delete; // No assignments!
dcxCursorResource(dcxCursorResource&&) = delete;
GSL_SUPPRESS(c.128) dcxCursorResource& operator =(dcxCursorResource&&) = delete;
// calls dcxLoadCursorFromFile()
explicit dcxCursorResource(const TString& tsFilename)
: dcxResource(make_cursor(tsFilename))
{
}
// calls dcxLoadCursorFromResource()
explicit dcxCursorResource(const PTCHAR CursorType)
: dcxResource(make_resource(dcxLoadCursorFromResource, [](HCURSOR hCursor) noexcept { if (hCursor) DestroyCursor(hCursor); }, CursorType))
{
}
};
struct dcxIconResource final
: dcxResource < dcxIcon_t >
{
dcxIconResource() = delete; // no default!
dcxIconResource(const dcxIconResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxIconResource& operator =(const dcxIconResource&) = delete; // No assignments!
dcxIconResource(dcxIconResource&&) = delete;
GSL_SUPPRESS(c.128) dcxIconResource& operator =(dcxIconResource&&) = delete;
// calls dcxLoadIcon()
dcxIconResource(const int index, TString& filename, const bool large, const TString& flags)
: dcxResource(make_icon(index, filename, large, flags))
{
}
// calls ExtractIconEx()
dcxIconResource(const TString& filename, const int fIndex, const bool bLarge)
: dcxResource(make_resource([](const TString& filename, const int fIndex, const bool bLarge) noexcept
{
HICON m_hIcon = nullptr;
if (bLarge)
ExtractIconEx(filename.to_chr(), fIndex, &m_hIcon, nullptr, 1);
else
ExtractIconEx(filename.to_chr(), fIndex, nullptr, &m_hIcon, 1);
return m_hIcon;
}, [](HICON hIcon) noexcept { if (hIcon) DestroyIcon(hIcon); }, filename, fIndex, bLarge))
{
}
};
struct dcxHDCResource
: dcxResource < dcxHDC_t >
{
dcxHDCResource() = delete; // no default!
dcxHDCResource(const dcxHDCResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxHDCResource& operator =(const dcxHDCResource&) = delete; // No assignments!
dcxHDCResource(dcxHDCResource&&) = delete;
GSL_SUPPRESS(c.128) dcxHDCResource& operator =(dcxHDCResource&&) = delete;
// calls CreateCompatibleDC()
explicit dcxHDCResource(HDC hdc)
: dcxResource(make_hdc(hdc))
{
}
// calls CreateDC()
dcxHDCResource(HDC hdc, LPCTSTR lpszDriver, LPCTSTR lpszDevice, LPCTSTR lpszOutput, const DEVMODE* lpInitData)
: dcxResource(make_resource(CreateDC, [](HDC obj) noexcept { if (obj) DeleteDC(obj); }, lpszDriver, lpszDevice, lpszOutput, lpInitData))
{
}
//// calls GetDC() - broken
//explicit dcxHDCResource(HWND hWnd)
// : dcxResource(make_resource(GetDC, [hWnd](HDC obj) { ReleaseDC(hWnd, obj); }, hWnd))
//{
//}
//// calls GetDCEx() - broken
//dcxHDCResource(HWND hWnd, HRGN hrgnClip, DWORD flags)
// : dcxResource(make_resource(GetDCEx, [hWnd](HDC obj) { ReleaseDC(hWnd, obj); }, hWnd, hrgnClip, flags))
//{
//}
//// calls GetWindowDC() - broken
//explicit dcxHDCResource(HWND hWnd)
// : dcxResource(make_resource(GetWindowDC, [](HDC obj) { ReleaseDC(nullptr, obj); }, hWnd))
//{
//}
};
// Wraps the HDC resource & auto handles cleanup etc..
// calls CreateCompatableDC() & then SelectBitmap()'s the supplied bitmap into it
// oldbitmap is saved & selected back into the hdc on destruction.
struct dcxHDCBitmapResource final
: dcxHDCResource
{
dcxHDCBitmapResource() = delete; // no default!
dcxHDCBitmapResource(const dcxHDCBitmapResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxHDCBitmapResource& operator =(const dcxHDCBitmapResource&) = delete; // No assignments!
dcxHDCBitmapResource(dcxHDCBitmapResource&&) = delete; // no move!
GSL_SUPPRESS(c.128) dcxHDCBitmapResource& operator =(dcxHDCBitmapResource&&) = delete; // No assignments!
//calls CreateCompatibleDC() then SelectBitmap()
dcxHDCBitmapResource(HDC hdc, HBITMAP hBitmap)
: dcxHDCResource(hdc)
{
if (hBitmap)
m_hOldBitmap = SelectBitmap(this->get(), hBitmap);
}
~dcxHDCBitmapResource()
{
if (m_hOldBitmap)
SelectBitmap(this->get(), m_hOldBitmap);
}
private:
HBITMAP m_hOldBitmap{ nullptr };
};
struct dcxBitmapResource final
: dcxResource < dcxBitmap_t >
{
dcxBitmapResource() = delete; // no default!
dcxBitmapResource(const dcxBitmapResource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxBitmapResource& operator =(const dcxBitmapResource&) = delete; // No assignments!
dcxBitmapResource(dcxBitmapResource&&) = delete;
GSL_SUPPRESS(c.128) dcxBitmapResource& operator =(dcxBitmapResource&&) = delete;
// calls CreateCompatibleBitmap()
dcxBitmapResource(HDC hdc, int w, int h)
: dcxResource(make_bitmap(hdc, w, h))
{
}
//calls CreateDIBitmap();
dcxBitmapResource(HDC hdc, const BITMAPINFOHEADER* pbmih, DWORD flInit, const void* pjBits, const BITMAPINFO* pbmi, UINT iUsage)
: dcxResource(make_resource(CreateDIBitmap, [](HBITMAP obj) noexcept { if (obj) DeleteBitmap(obj); }, hdc, pbmih, flInit, pjBits, pbmi, iUsage))
{
}
//calls CreateDIBSection();
dcxBitmapResource(HDC hdc, const BITMAPINFO* pbmi, UINT usage, void** ppvBits, HANDLE hSection, DWORD offset)
: dcxResource(make_resource(CreateDIBSection, [](HBITMAP obj) noexcept { if (obj) DeleteBitmap(obj); }, hdc, pbmi, usage, ppvBits, hSection, offset))
{
}
};
struct dcxHDCBitmap2Resource
: dcxHDCResource
{
dcxHDCBitmap2Resource() = delete; // no default!
dcxHDCBitmap2Resource(const dcxHDCBitmap2Resource&) = delete; // no copy!
GSL_SUPPRESS(c.128) dcxHDCBitmap2Resource& operator =(const dcxHDCBitmap2Resource&) = delete; // No assignments!
dcxHDCBitmap2Resource(dcxHDCBitmap2Resource&&) = delete;
GSL_SUPPRESS(c.128) dcxHDCBitmap2Resource& operator =(dcxHDCBitmap2Resource&&) = delete;
//calls CreateCompatibleDC() then CreateCompatibleBitmap(), then SelectBitmap()
dcxHDCBitmap2Resource(HDC hdc, const int& w, const int& h)
: dcxHDCResource(hdc)
, m_hBitmap(hdc, w, h)
, m_Width(w)
, m_Height(h)
{
m_hOldBitmap = SelectBitmap(this->get(), m_hBitmap.get());
}
~dcxHDCBitmap2Resource()
{
if (m_hOldBitmap)
SelectBitmap(this->get(), m_hOldBitmap);
}
/// <summary>
/// Get the bitmaps width.
/// </summary>
/// <returns></returns>
const int& getWidth() const noexcept { return m_Width; }
/// <summary>
/// Get the bitmaps height.
/// </summary>
/// <returns></returns>
const int& getHeight() const noexcept { return m_Height; }
/// <summary>
/// Get a pointer to the bitmap.
/// </summary>
/// <returns></returns>
const HBITMAP getBitMap() const noexcept { return m_hBitmap.get(); }
private:
HBITMAP m_hOldBitmap;
dcxBitmapResource m_hBitmap;
int m_Width, m_Height;
};
struct dcxHDCBuffer final
: dcxHDCBitmap2Resource
{
dcxHDCBuffer() = delete;
dcxHDCBuffer(const dcxHDCBuffer&) = delete;
GSL_SUPPRESS(c.128) dcxHDCBuffer& operator = (const dcxHDCBuffer&) = delete;
dcxHDCBuffer(dcxHDCBuffer&&) = delete;
GSL_SUPPRESS(c.128) dcxHDCBuffer& operator = (dcxHDCBuffer&&) = delete;
dcxHDCBuffer(const HDC hdc, const RECT& rc)
: dcxHDCBitmap2Resource(hdc, (rc.right - rc.left), (rc.bottom - rc.top))
{
// copy settings from hdc to buffer's hdc.
SetDCBrushColor(this->get(), GetDCBrushColor(hdc));
SetDCPenColor(this->get(), GetDCPenColor(hdc));
SetLayout(this->get(), GetLayout(hdc));
//m_hOldFont = SelectFont(this->get(), GetCurrentObject(hdc, OBJ_FONT));
m_hOldFont = static_cast<HFONT>(SelectObject(this->get(), GetCurrentObject(hdc, OBJ_FONT)));
SetTextColor(this->get(), GetTextColor(hdc));
SetBkColor(this->get(), GetBkColor(hdc));
SetBkMode(this->get(), GetBkMode(hdc));
SetROP2(this->get(), GetROP2(hdc));
SetMapMode(this->get(), GetMapMode(hdc));
SetPolyFillMode(this->get(), GetPolyFillMode(hdc));
SetStretchBltMode(this->get(), GetStretchBltMode(hdc));
SetGraphicsMode(this->get(), GetGraphicsMode(hdc));
SetTextAlign(this->get(), GetTextAlign(hdc));
//SetBrushOrgEx(this->get(), );
//GetBrushOrgEx
//SetWindowOrgEx
//GetWindowOrgEx
// copy contents of hdc within area to buffer.
GSL_SUPPRESS(type.4) BitBlt(this->get(), 0, 0, getWidth(), getHeight(), hdc, rc.left, rc.top, SRCCOPY);
// buffer is an exact duplicate of the hdc within the area specified.
}
~dcxHDCBuffer()
{
GdiFlush();
if (m_hOldFont)
//SelectFont(this->get(), m_hOldFont);
SelectObject(this->get(), m_hOldFont);
}
private:
HFONT m_hOldFont{ nullptr };
};
struct dcxVariant final
: VARIANT
{
dcxVariant() noexcept
{
VariantInit(this);
}
explicit dcxVariant(bool val) noexcept
: dcxVariant()
{
vt = VT_BOOL;
boolVal = gsl::narrow_cast<VARIANT_BOOL>(val);
}
explicit dcxVariant(int val) noexcept
: dcxVariant()
{
vt = VT_I4;
intVal = gsl::narrow_cast<INT>(val);
}
explicit dcxVariant(const BSTR bStr) noexcept
: dcxVariant()
{
vt = VT_BSTR;
bstrVal = bStr;
}
explicit dcxVariant(const WCHAR* cStr) noexcept
: dcxVariant(SysAllocString(cStr))
{
}
~dcxVariant() noexcept
{
VariantClear(this); // this does SysFreeString() for us.
}
dcxVariant(const dcxVariant&) noexcept = default;
GSL_SUPPRESS(c.128) dcxVariant& operator = (const dcxVariant&) noexcept = default;
dcxVariant(dcxVariant&&) noexcept = default;
GSL_SUPPRESS(c.128) dcxVariant& operator = (dcxVariant&&) noexcept = default;
};
struct dcxCursorPos final
: POINT
{
/// <summary>
/// Get cursor position on screen.
/// sets cursor position to -1,-1 on error.
/// </summary>
/// <returns></returns>
dcxCursorPos() noexcept
{
if (GetCursorPos(this) == FALSE)
x = y = -1;
}
/// <summary>
/// Get cursor position mapped to this window.
/// sets cursor position to -1,-1 on error. (check GetLastError() for more info)
/// </summary>
/// <param name="hwnd">- Window to map cusrsor position too.</param>
/// <returns></returns>
explicit dcxCursorPos(HWND hwnd) noexcept
: dcxCursorPos()
{
SetLastError(0);
if ((MapWindowPoints(nullptr, hwnd, this, 1) == 0) && (GetLastError() != 0))
x = y = -1;
}
~dcxCursorPos() noexcept = default;
explicit operator bool() const noexcept { return ((x != -1) && (y != -1)); };
bool operator==(const dcxCursorPos& other) const = default;
};
struct dcxRect
: RECT
{
/// <summary>
/// Get the rects width.
/// </summary>
/// <returns></returns>
long Width() const noexcept { return (right - left); }
/// <summary>
/// Get the rects height.
/// </summary>
/// <returns></returns>
long Height() const noexcept { return (bottom - top); }
/// <summary>
/// Get a copy of the rect.
/// </summary>
/// <returns></returns>
RECT CopyRect() const noexcept { return { left, top, right, bottom }; }
};
struct dcxWindowRect final
: dcxRect
{
dcxWindowRect() = delete;
// Gets the window rect for hwnd
explicit dcxWindowRect(HWND hwnd) noexcept
{
if (!hwnd) m_ok = false;
else m_ok = (GetWindowRect(hwnd, this) != FALSE);
}
// Gets the window rect for hwnd & maps it to hMap
dcxWindowRect(HWND hwnd, HWND hMap) noexcept
: dcxWindowRect(hwnd)
{
if (m_ok)
{
SetLastError(0U);
MapWindowRect(nullptr, hMap, this);
m_ok = (GetLastError() == 0U);
}
}
~dcxWindowRect() noexcept = default;
explicit operator bool() noexcept { return m_ok; }
bool m_ok{ false };
};
struct dcxClientRect final
: dcxRect
{
dcxClientRect() = delete;
// Gets the window rect for hwnd
explicit dcxClientRect(HWND hwnd) noexcept
{
if (!hwnd) m_ok = false;
else m_ok = (GetClientRect(hwnd, this) != FALSE);
}
// Gets the window client rect for hwnd & maps it to hMap
dcxClientRect(HWND hwnd, HWND hMap) noexcept
: dcxClientRect(hwnd)
{
if (m_ok)
{
SetLastError(0U);
MapWindowRect(hwnd, hMap, this);
m_ok = (GetLastError() == 0U);
}
}
~dcxClientRect() noexcept = default;
explicit operator bool() noexcept { return m_ok; }
bool m_ok{ false };
};
struct dcxClassName final
: stString<257>
{
dcxClassName() = delete;
// Gets the class name for hwnd
explicit dcxClassName(HWND hwnd) noexcept
{
if (hwnd)
GetClassName(hwnd, this->data(), gsl::narrow_cast<int32_t>(this->size()));
}
~dcxClassName() = default;
};
struct dcxPalette final
{
dcxPalette(bool bForce = false) noexcept
{
try {
getmIRCPalette(gsl::span<COLORREF>(m_Palette), bForce); // get mIRC palette
}
catch (...)
{
}
}
/// <summary>
/// Get a pointer to the palette.
/// </summary>
/// <returns></returns>
explicit operator COLORREF* () noexcept
{
return &m_Palette[0];
}
/// <summary>
/// Get the size of the palette.
/// </summary>
/// <returns></returns>
constexpr const size_t& size() const noexcept { return m_size; }
struct iter
{
COLORREF operator * () const noexcept { if (!itr_Palette) return CLR_INVALID; return itr_Palette[n]; }
iter& operator ++() noexcept { ++n; return *this; }
iter& operator --() noexcept { --n; return *this; }
friend
bool operator != (iter const& lhs, iter const& rhs) noexcept
{
return lhs.n != rhs.n;
}
size_t n;
const COLORREF* const itr_Palette;
};
iter begin() const noexcept { return{ 0, &m_Palette[0] }; }
iter end() const noexcept { return{ size(), nullptr }; }
constexpr static const size_t m_size = mIRC_PALETTE_SIZE;
COLORREF m_Palette[m_size]{ CLR_INVALID };
};
struct dcxTreeItem final
{
dcxTreeItem(HWND hWin, HTREEITEM hParent) noexcept
: m_Window{ hWin }
, m_Item{ TreeView_GetChild(hWin, hParent) }
{
}
GSL_SUPPRESS(es.47)
explicit dcxTreeItem(HWND hWin) noexcept
: m_Window{ hWin }
, m_Item{ TreeView_GetRoot(hWin) }
{
}
explicit operator HTREEITEM () noexcept
{
return m_Item;
}
struct iter
{
//HTREEITEM operator * () const noexcept { return itr_Item; }
dcxTreeItem operator * () const noexcept { return dcxTreeItem{ itr_Window,itr_Item }; }
iter& operator ++() noexcept
{
itr_Item = TreeView_GetNextSibling(itr_Window, itr_Item);
return *this;
}
iter& operator --() noexcept
{
itr_Item = TreeView_GetPrevSibling(itr_Window, itr_Item);
return *this;
}
iter operator ++(int) noexcept
{
iter res{ *this };
itr_Item = TreeView_GetNextSibling(itr_Window, itr_Item);
return res;
}
iter operator --(int) noexcept
{
iter res{ *this };
itr_Item = TreeView_GetPrevSibling(itr_Window, itr_Item);
return res;
}
friend
bool operator != (iter const& lhs, iter const& rhs) noexcept
{
return lhs.itr_Item != rhs.itr_Item;
}
HWND itr_Window{};
HTREEITEM itr_Item{};
};
GSL_SUPPRESS(lifetime.4) iter begin() const noexcept { return{ m_Window, TreeView_GetChild(m_Window, m_Item) }; }
GSL_SUPPRESS(lifetime.4) iter end() const noexcept { return{ m_Window, nullptr }; }
dcxTreeItem GetChild() const noexcept
{
if (!m_Window || !m_Item)
return { nullptr, nullptr };
return { m_Window, TreeView_GetChild(m_Window, m_Item) };
}
dcxTreeItem GetParent() const noexcept
{
if (!m_Window || !m_Item)
return { nullptr, nullptr };
return { m_Window, TreeView_GetParent(m_Window, m_Item) };
}
dcxTreeItem GetPrev() const noexcept
{
if (!m_Window || !m_Item)
return { nullptr, nullptr };
return { m_Window, TreeView_GetPrevSibling(m_Window, m_Item) };
}
dcxTreeItem GetNext() const noexcept
{
if (!m_Window || !m_Item)
return { nullptr, nullptr };
return { m_Window, TreeView_GetNextSibling(m_Window, m_Item) };
}
TVITEMEX GetItemEx(UINT uMask) const noexcept
{
TVITEMEX item{};
if (!m_Window || !m_Item)
return item;
item.hItem = m_Item;
// exclude text flag as no buffer set.
item.mask = (uMask & ~TVIF_TEXT) | TVIF_HANDLE;
TreeView_GetItem(m_Window, &item);
return item;
}
bool SetItemEx(const LPTVITEMEX pItem) noexcept
{
if (!pItem)
return false;
if (!m_Window || !m_Item)
return false;
TVITEMEX item{ *pItem };
item.hItem = m_Item;