summaryrefslogtreecommitdiff
path: root/include/m_gui.h
blob: 999db8899b853145e461ea56ebdfd947633a2b9c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
/*

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2002-04 Santithorn Bunchua
Copyright (c) 2005-12 George Hazan
Copyright (c) 2007-09 Maxim Mluhov
Copyright (c) 2007-09 Victor Pavlychko
Copyright (C) 2012-24 Miranda NG team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#pragma once

#ifndef __M_GUI_H
#define __M_GUI_H

#ifdef _MSC_VER
	#include <CommCtrl.h>
#endif // _WINDOWS

#include <m_system.h>
#include <m_protoint.h>
#include <m_clc.h>

#pragma warning(disable:4355 4251 4481)

/////////////////////////////////////////////////////////////////////////////////////////
// helpers for the option's visualization

template<int Size>
struct CMDBTraits
{
};

template<>
struct CMDBTraits<1>
{
	typedef uint8_t DBType;
	enum { DBTypeId = DBVT_BYTE };
	static __forceinline DBType Get(const char *szModule, const char *szSetting, DBType value)
	{
		return db_get_b(0, szModule, szSetting, value);
	}
	static __forceinline void Set(const char *szModule, const char *szSetting, DBType value)
	{
		db_set_b(0, szModule, szSetting, value);
	}
};

template<>
struct CMDBTraits<2>
{
	typedef uint16_t DBType;
	enum { DBTypeId = DBVT_WORD };
	static __forceinline DBType Get(const char *szModule, const char *szSetting, DBType value)
	{
		return db_get_w(0, szModule, szSetting, value);
	}
	static __forceinline void Set(const char *szModule, const char *szSetting, DBType value)
	{
		db_set_w(0, szModule, szSetting, value);
	}
};

template<>
struct CMDBTraits<4>
{
	typedef uint32_t DBType;
	enum { DBTypeId = DBVT_DWORD };
	static __forceinline DBType Get(const char *szModule, const char *szSetting, DBType value)
	{
		return db_get_dw(0, szModule, szSetting, value);
	}
	static __forceinline void Set(const char *szModule, const char *szSetting, DBType value)
	{
		db_set_dw(0, szModule, szSetting, value);
	}
};

template<>
struct CMDBTraits<8>
{
	typedef uint32_t DBType;
	enum { DBTypeId = DBVT_DWORD };
	static __forceinline DBType Get(const char *szModule, const char *szSetting, DBType value)
	{
		return db_get_dw(0, szModule, szSetting, value);
	}
	static __forceinline void Set(const char *szModule, const char *szSetting, DBType value)
	{
		db_set_dw(0, szModule, szSetting, value);
	}
};

class CMOptionBase : public MNonCopyable
{
public:
	__forceinline const char* GetDBModuleName() const { return m_szModuleName; }
	__forceinline const char* GetDBSettingName() const { return m_szSetting; }

	__forceinline void Delete() const 
	{	db_unset(0, m_szModuleName, m_szSetting);
	}

protected:
	__forceinline CMOptionBase(PROTO_INTERFACE *proto, const char *szSetting) :
		m_szModuleName(proto->m_szModuleName), m_szSetting(szSetting)
	{}

	__forceinline CMOptionBase(const char *module, const char *szSetting) :
		m_szModuleName(module), m_szSetting(szSetting)
	{}

	const char *m_szModuleName;
	const char *m_szSetting;
};

template<class T>
class CMOption : public CMOptionBase
{
public:
	typedef T Type;

	__forceinline CMOption(PROTO_INTERFACE *proto, const char *szSetting, Type defValue) :
		CMOptionBase(proto, szSetting), m_default(defValue)
	{}

	__forceinline CMOption(const char *szModule, const char *szSetting, Type defValue) :
		CMOptionBase(szModule, szSetting), m_default(defValue)
	{}

	__forceinline Type Default() const
	{
		return m_default;
	}

	__forceinline operator Type()
	{
		return (Type)CMDBTraits<sizeof(Type)>::Get(m_szModuleName, m_szSetting, m_default);
	}

	__forceinline Type operator= (Type value)
	{
      #ifdef _MSC_VER
		CMDBTraits<sizeof(Type)>::Set(m_szModuleName, m_szSetting, (CMDBTraits<sizeof(Type)>::DBType)value);
		#else
		CMDBTraits<sizeof(Type)>::Set(m_szModuleName, m_szSetting, value);
		#endif
		return value;
	}

private:
	Type m_default;
};

template<>
class CMOption<char*> : public CMOptionBase
{
public:

	typedef char Type;

	__forceinline CMOption(PROTO_INTERFACE *proto, const char *szSetting, const Type *defValue = nullptr) :
		CMOptionBase(proto, szSetting), m_default(defValue)
	{}

	__forceinline CMOption(const char *szModule, const char *szSetting, const Type *defValue = nullptr) :
		CMOptionBase(szModule, szSetting), m_default(defValue)
	{}

	__forceinline const Type* Default() const
	{
		return m_default;
	}

	__forceinline operator Type*()
	{
		m_value = db_get_sa(0, m_szModuleName, m_szSetting);
		if (!m_value) m_value = mir_strdup(m_default);
		return m_value;
	}

	__forceinline Type* operator= (Type *value)
	{
		db_set_s(0, m_szModuleName, m_szSetting, value);
		return value;
	}

private:
	const Type *m_default;
	mir_ptr<Type> m_value;
};

template<>
class CMOption<wchar_t*> : public CMOptionBase
{
public:

	typedef wchar_t Type;

	__forceinline CMOption(PROTO_INTERFACE *proto, const char *szSetting, const Type *defValue = nullptr) :
		CMOptionBase(proto, szSetting), m_default(defValue)
	{}

	__forceinline CMOption(const char *szModule, const char *szSetting, const Type *defValue = nullptr) :
		CMOptionBase(szModule, szSetting), m_default(defValue)
	{}

	__forceinline const Type* Default() const
	{
		return m_default;
	}

	__forceinline operator Type*()
	{
		m_value = db_get_wsa(0, m_szModuleName, m_szSetting);
		if (!m_value) m_value = mir_wstrdup(m_default);
		return m_value;
	}

	__forceinline const Type* operator= (const Type *value)
	{
		db_set_ws(0, m_szModuleName, m_szSetting, value);
		return value;
	}

private:
	const Type *m_default;
	mir_ptr<Type> m_value;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CDbLink

class MIR_CORE_EXPORT CDataLink
{
protected:
	uint8_t m_type;

public:
	__inline CDataLink(uint8_t type) : m_type(type) {}
	virtual ~CDataLink() {}

	__inline uint8_t GetDataType() const { return m_type; }

	virtual uint32_t LoadInt() = 0;
	virtual void  SaveInt(uint32_t value) = 0;

	virtual wchar_t* LoadText() = 0;
	virtual void   SaveText(wchar_t *value) = 0;
};

class MIR_CORE_EXPORT CDbLink : public CDataLink
{
	char *m_szModule;
	char *m_szSetting;

	uint32_t m_iDefault;
	wchar_t *m_szDefault;

	DBVARIANT dbv;

public:
	CDbLink(const char *szModule, const char *szSetting, uint8_t type, uint32_t iValue);
	CDbLink(const char *szModule, const char *szSetting, uint8_t type, wchar_t *szValue);
	~CDbLink();

	uint32_t LoadInt() override;
	void  SaveInt(uint32_t value) override;

	wchar_t* LoadText() override;
	void   SaveText(wchar_t *value) override;
};

template<class T>
class CMOptionLink : public CDataLink
{
private:
	CMOption<T> *m_option;

public:
	__forceinline CMOptionLink(CMOption<T> &option) :
		CDataLink(CMDBTraits<sizeof(T)>::DBTypeId), m_option(&option)
	{}

	__forceinline uint32_t LoadInt() override { return (uint32_t)(T)*m_option; }
	__forceinline void  SaveInt(uint32_t value) override { *m_option = (T)value; }

	__forceinline wchar_t* LoadText() override { return nullptr; }
	__forceinline void   SaveText(wchar_t*) override {}
};

template<>
class CMOptionLink<wchar_t*> : public CDataLink
{
private:
	typedef wchar_t *T;
	CMOption<T> *m_option;

public:
	__forceinline CMOptionLink(CMOption<T> &option) :
		CDataLink(DBVT_WCHAR), m_option(&option)
	{}

	__forceinline uint32_t LoadInt() override { return 0; }
	__forceinline void  SaveInt(uint32_t) override { }

	__forceinline wchar_t* LoadText() override { return *m_option; }
	__forceinline void   SaveText(wchar_t *value) override { *m_option = value; }
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlBase

struct CContextMenuPos
{
	const class CCtrlBase *pCtrl;
	POINT pt;
	union {
		int iCurr; // int for list boxes
		HTREEITEM hItem;
	};
};

class MIR_CORE_EXPORT CCtrlBase
{
	friend class CDlgBase;

	__forceinline CCtrlBase(const CCtrlBase&) = delete;
	__forceinline CCtrlBase& operator=(const CCtrlBase&) = delete;

public:
	CCtrlBase(CDlgBase *wnd, int idCtrl);
	virtual ~CCtrlBase();

	__forceinline MWindow GetHwnd() const { return m_hwnd; }
	__forceinline int GetCtrlId() const { return m_idCtrl; }
	__forceinline CDlgBase *GetParent() const { return m_parentWnd; }
	__forceinline bool IsChanged() const { return m_bChanged; }
	__forceinline void SetSilent(bool bSilent = true) { m_bSilent = bSilent; }
	__forceinline void UseSystemColors() { m_bUseSystemColors = true; }

	void Show(bool bShow = true);
	__forceinline void Hide() { Show(false); }

	void Enable(bool bIsEnable = true);
	__forceinline void Disable() { Enable(false); }
	bool Enabled(void) const;

	void NotifyChange();
	void SetDraw(bool bEnable);

	LRESULT  SendMsg(UINT Msg, WPARAM wParam, LPARAM lParam) const;

	void     SetText(const wchar_t *text);
	void     SetTextA(const char *text);
	void     SetInt(int value);

	wchar_t* GetText() const;
	char*    GetTextA() const;
	char*    GetTextU() const;

	wchar_t* GetText(wchar_t *buf, size_t size) const;
	char*    GetTextA(char *buf, size_t size) const;
	char*    GetTextU(char *buf, size_t size) const;

	int      GetInt() const;

	virtual  BOOL OnCommand(MWindow /*hwndCtrl*/, uint16_t /*idCtrl*/, uint16_t /*idCode*/) { return FALSE; }
	virtual  BOOL OnNotify(int /*idCtrl*/, NMHDR* /*pnmh*/) { return FALSE; }

	virtual  BOOL OnMeasureItem(MEASUREITEMSTRUCT*) { return FALSE; }
	virtual  BOOL OnDrawItem(DRAWITEMSTRUCT*) { return FALSE; }
	virtual  BOOL OnDeleteItem(DELETEITEMSTRUCT*) { return FALSE; }

	virtual  void OnInit();
	virtual  void OnDestroy();

	virtual  bool OnApply();
	virtual  void OnReset();

protected:
	MWindow m_hwnd = nullptr;  // must be the first data item
	int m_idCtrl;
	bool m_bChanged = false, m_bSilent = false, m_bUseSystemColors = false, m_bNotifiable = false;
	CDlgBase *m_parentWnd;

public:
	CCallback<CCtrlBase> OnChange;
	CCallback<CContextMenuPos> OnBuildMenu;

protected:
	virtual void GetCaretPos(CContextMenuPos&) const;
	virtual LRESULT CustomWndProc(UINT msg, WPARAM wParam, LPARAM lParam);

	void Subclass();

private:
	static LRESULT CALLBACK GlobalSubclassWndProc(MWindow hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
	WNDPROC oldProc;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlData - data access controls base class

class MIR_CORE_EXPORT CCtrlData : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlData(CDlgBase *dlg, int ctrlId);
	~CCtrlData();

	void CreateDbLink(const char* szModuleName, const char* szSetting, uint8_t type, uint32_t iValue);
	void CreateDbLink(const char* szModuleName, const char* szSetting, wchar_t* szValue);
	void CreateDbLink(CDataLink *link) { m_dbLink = link; }

	void OnInit() override;

protected:
	CDataLink *m_dbLink;

	__inline uint8_t GetDataType() { return m_dbLink ? m_dbLink->GetDataType() : DBVT_DELETED; }
	__inline uint32_t LoadInt() { return m_dbLink ? m_dbLink->LoadInt() : 0; }
	__inline void SaveInt(uint32_t value) { if (m_dbLink) m_dbLink->SaveInt(value); }
	__inline const wchar_t *LoadText() { return m_dbLink ? m_dbLink->LoadText() : L""; }
	__inline void SaveText(wchar_t *value) { if (m_dbLink) m_dbLink->SaveText(value); }
};

/////////////////////////////////////////////////////////////////////////////////////////
// CDlgBase - base dialog class

class MIR_CORE_EXPORT CDlgBase
{
	friend class CTimer;
	friend class CCtrlBase;
	friend class CCtrlData;

public:
	CDlgBase(class CMPluginBase &pPlug, int idDialog);
	virtual ~CDlgBase();

	// general utilities
	void Close();
	void Resize();
	void Create();
	void Show(int nCmdShow = SW_SHOW);
	int  DoModal();
	void EndModal(INT_PTR nResult);

	class CCtrlBase* FindControl(int idCtrl);
	class CCtrlBase* FindControl(MWindow hwnd);

	void SetCaption(const wchar_t *ptszCaption);
	void SetDraw(bool bEnable);
	void NotifyChange(void); // sends a notification to a parent window

	HINSTANCE GetInst() const;

	__forceinline HPLUGIN GetPlugin() const { return &m_pPlugin; }
	__forceinline MWindow GetHwnd() const { return m_hwnd; }
	__forceinline void Hide() { Show(SW_HIDE); }
	__forceinline bool IsInitialized() const { return m_bInitialized; }
	__forceinline void SetMinSize(int x, int y) { m_iMinWidth = x, m_iMinHeight = y; }
	__forceinline void SetParent(MWindow hwnd) { m_hwndParent = hwnd; }

	__forceinline CCtrlBase* operator[](int iControlId) { return FindControl(iControlId); }

	static CDlgBase* Find(MWindow hwnd);

protected:
	MWindow m_hwnd = nullptr;  // must be the first data item
	MWindow m_hwndParent = nullptr;
	int     m_idDialog;
	RECT    m_rcPrev;

	bool    m_isModal = false;
	bool    m_bInitialized = false;
	bool    m_forceResizable = false;
	bool    m_bFixedSize;
	bool    m_bSucceeded = false; // was IDOK pressed or not
	bool    m_bExiting = false; // window received WM_CLOSE and gonna die soon

	enum { CLOSE_ON_OK = 0x1, CLOSE_ON_CANCEL = 0x2 };
	uint8_t m_autoClose;    // automatically close dialog on IDOK/CANCEL commands. default: CLOSE_ON_OK|CLOSE_ON_CANCEL

	CMPluginBase &m_pPlugin;

	// override this handlers to provide custom functionality
	// general messages
	virtual bool OnInitDialog();
	virtual bool OnApply();
	virtual bool OnClose();
	virtual void OnDestroy();

	virtual void OnTimer(class CTimer*);

	// miranda-related stuff
	virtual int Resizer(UTILRESIZECONTROL *urc);
	virtual void OnResize();
	virtual void OnReset();
	virtual void OnChange();

	// main dialog procedure
	virtual INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam);

	CCallback<void> m_OnFinishWizard;

	// register controls
	void AddControl(CCtrlBase *ctrl);
	void RemoveControl(CCtrlBase *ctrl);

	// timers
	void AddTimer(CTimer *timer);
	void RemoveTimer(UINT_PTR idEvent);

	// options support
	void CreateLink(class CCtrlData& ctrl, const char *szSetting, uint8_t type, uint32_t iValue);
	void CreateLink(class CCtrlData& ctrl, const char *szSetting, wchar_t *szValue);

	template<class T>
	__inline void CreateLink(CCtrlData& ctrl, CMOption<T> &option)
	{
		ctrl.CreateDbLink(new CMOptionLink<T>(option));
	}

	// win32 stuff
	void ThemeDialogBackground(BOOL tabbed);

private:
	LIST<CTimer> m_timers;
	LIST<CCtrlBase> m_controls;

	void NotifyControls(void (CCtrlBase::*fn)());
	bool VerifyControls(bool (CCtrlBase::*fn)());

	CTimer* FindTimer(int idEvent);
	int m_iMinWidth = -1, m_iMinHeight = -1;

	static BOOL CALLBACK GlobalFieldEnum(MWindow hwnd, LPARAM lParam);
	static INT_PTR CALLBACK GlobalDlgProc(MWindow hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
	static int GlobalDlgResizer(MWindow hwnd, LPARAM lParam, UTILRESIZECONTROL *urc);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CTimer

class MIR_CORE_EXPORT CTimer
{
	friend class CDlgBase;

public:
	CTimer(CDlgBase* wnd, UINT_PTR idEvent);
	~CTimer();

	__forceinline UINT_PTR GetEventId() const { return m_idEvent; }
	__forceinline MWindow GetHwnd() const { return m_wnd->GetHwnd(); }

	virtual BOOL OnTimer();

	void Start(int elapse);
	bool Stop(); // returns true if timer was active

	void StartSafe(int elapse);
	void StopSafe();

	CCallback<CTimer> OnEvent;

protected:
	UINT_PTR  m_idEvent;
	CDlgBase* m_wnd;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlLabel

class MIR_CORE_EXPORT CCtrlLabel : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlLabel(CDlgBase *dlg, int ctrlId);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlButton

class MIR_CORE_EXPORT CCtrlButton : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlButton(CDlgBase *dlg, int ctrlId);

	BOOL OnCommand(MWindow hwndCtrl, uint16_t idCtrl, uint16_t idCode) override;

	CCallback<CCtrlButton> OnClick;

	void Click();
	bool IsPushed() const;
	void Push(bool bPushed);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlMButton

class MIR_CORE_EXPORT CCtrlMButton : public CCtrlButton
{
	typedef CCtrlButton CSuper;

public:
	CCtrlMButton(CDlgBase *dlg, int ctrlId, HICON hIcon, const char* tooltip);
	CCtrlMButton(CDlgBase *dlg, int ctrlId, int iCoreIcon, const char* tooltip);
	~CCtrlMButton();

	void MakeFlat();
	void MakePush();
	void SetTooltip(const char *pszTooltip);

	void OnInit() override;

protected:
	HICON m_hIcon;
	const char* m_toolTip;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CSplitter

class MIR_CORE_EXPORT CSplitter : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CSplitter(CDlgBase *dlg, int ctrlId);

	__forceinline int GetPos() const { return m_iPosition; }

protected:
	LRESULT CustomWndProc(UINT msg, WPARAM wParam, LPARAM lParam) override;
	void OnInit() override;

	int m_iPosition;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlHyperlink

class MIR_CORE_EXPORT CCtrlHyperlink : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlHyperlink(CDlgBase *dlg, int ctrlId, const char* url = nullptr);

	BOOL OnCommand(MWindow hwndCtrl, uint16_t idCtrl, uint16_t idCode) override;

	CCallback<CCtrlHyperlink> OnClick;

	void SetUrl(const char *url);
	const char *GetUrl();

protected:
	const char* m_url;

	void Default_OnClick(CCtrlHyperlink*);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CProgress

class MIR_CORE_EXPORT CCtrlProgress : public CCtrlBase
{
public:
	CCtrlProgress(CDlgBase *dlg, int ctrlId);

	void SetRange(uint16_t max, uint16_t min = 0);
	void SetPosition(uint16_t value);
	void SetStep(uint16_t value);
	uint16_t Move(uint16_t delta = 0);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlClc

#if !defined(MGROUP)
	typedef int MGROUP;
#endif

class MIR_CORE_EXPORT CCtrlClc : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlClc(CDlgBase *dlg, int ctrlId);

	void       AddContact(MCONTACT hContact);
	void       AddGroup(HANDLE hGroup);
	HANDLE     AddInfoItem(CLCINFOITEM *cii);
	void       AutoRebuild();
	void       DeleteItem(HANDLE hItem);
	void       EditLabel(HANDLE hItem);
	void       EndEditLabel(bool save);
	void       EnsureVisible(HANDLE hItem, bool partialOk);
	void       Expand(HANDLE hItem, uint32_t flags);
	HANDLE     FindContact(MCONTACT hContact);
	HANDLE     FindGroup(MGROUP hGroup);
	COLORREF   GetBkColor() const;
	bool       GetCheck(HANDLE hItem) const;
	int        GetCount() const;
	MWindow    GetEditControl() const;
	uint32_t   GetExStyle() const;
	uint32_t   GetExpand(HANDLE hItem) const;
	int        GetExtraColumns() const;
	uint8_t    GetExtraImage(HANDLE hItem, int iColumn) const;
	HIMAGELIST GetExtraImageList() const;
	HFONT      GetFont(int iFontId) const;
	bool       GetHideOfflineRoot() const;
	int        GetItemType(HANDLE hItem) const;
	HANDLE     GetNextItem(HANDLE hItem, uint32_t flags) const;
	HANDLE     GetSelection() const;
	HANDLE     HitTest(int x, int y, uint32_t *hitTest) const;
	void       SelectItem(HANDLE hItem);
	void       SetBkColor(COLORREF clBack);
	void       SetCheck(HANDLE hItem, bool check);
	void       SetExStyle(uint32_t exStyle);
	void       SetExtraColumns(int iColumns);
	void       SetExtraImage(HANDLE hItem, int iColumn, int iImage);
	void       SetExtraImageList(HIMAGELIST hImgList);
	void       SetFont(int iFontId, HANDLE hFont, bool bRedraw);
	void       SetItemText(HANDLE hItem, char *szText);
	void       SetHideEmptyGroups(bool state);
	void       SetHideOfflineRoot(bool state);
	void       SetOfflineModes(uint32_t modes);
	void       SetUseGroups(bool state);

	struct TEventInfo
	{
		CCtrlClc *ctrl;
		NMCLISTCONTROL *info;
	};

	CCallback<TEventInfo>	OnExpanded;
	CCallback<TEventInfo>	OnListRebuilt;
	CCallback<TEventInfo>	OnItemChecked;
	CCallback<TEventInfo>	OnDragging;
	CCallback<TEventInfo>	OnDropped;
	CCallback<TEventInfo>	OnListSizeChange;
	CCallback<TEventInfo>	OnOptionsChanged;
	CCallback<TEventInfo>	OnDragStop;
	CCallback<TEventInfo>	OnNewContact;
	CCallback<TEventInfo>	OnContactMoved;
	CCallback<TEventInfo>	OnCheckChanged;
	CCallback<TEventInfo>	OnClick;

protected:
	BOOL OnNotify(int idCtrl, NMHDR *pnmh) override;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlCheck

class MIR_CORE_EXPORT CCtrlCheck : public CCtrlData
{
	typedef CCtrlData CSuper;

public:
	CCtrlCheck(CDlgBase *dlg, int ctrlId);
	BOOL OnCommand(MWindow /*hwndCtrl*/, uint16_t /*idCtrl*/, uint16_t /*idCode*/) override;

	bool OnApply() override;
	void OnReset() override;

	int GetState() const;
	void SetState(int state);

	bool IsChecked();
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlColor - color picker

class MIR_CORE_EXPORT CCtrlColor : public CCtrlData
{
	typedef CCtrlData CSuper;

public:
	CCtrlColor(CDlgBase *dlg, int ctrlId);
	BOOL OnCommand(MWindow /*hwndCtrl*/, uint16_t /*idCtrl*/, uint16_t /*idCode*/) override;

	bool OnApply() override;
	void OnReset() override;

	uint32_t GetColor();
	void SetColor(uint32_t dwValue);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlDate - date & time picker

class MIR_CORE_EXPORT CCtrlDate : public CCtrlData
{
	typedef CCtrlData CSuper;

	BOOL OnNotify(int, NMHDR*) override;

public:
	CCtrlDate(CDlgBase *dlg, int ctrlId);

	void GetTime(SYSTEMTIME*);
	void SetTime(SYSTEMTIME*);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlEdit

class MIR_CORE_EXPORT CCtrlEdit : public CCtrlData
{
	typedef CCtrlData CSuper;

public:
	CCtrlEdit(CDlgBase *dlg, int ctrlId);
	BOOL OnCommand(MWindow /*hwndCtrl*/, uint16_t /*idCtrl*/, uint16_t idCode) override;

	bool OnApply() override;
	void OnReset() override;

	void SetMaxLength(unsigned int len);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlRichEdit

class MIR_CORE_EXPORT CCtrlRichEdit : public CCtrlEdit
{
	typedef CCtrlEdit CSuper;

public:
	CCtrlRichEdit(CDlgBase *dlg, int ctrlId);

	// returns text length in bytes if a parameter is omitted or in symbols, if not
	int GetRichTextLength(int iCodePage = CP_ACP) const;

	// returns a buffer that should be freed using mir_free() or ptrA/ptrW
	char* GetPlainRtf(bool bSelection = false);
	char* GetRichTextRtf(bool bText = false, bool bSelection = false) const; // returns text with formatting
	wchar_t* GetRichText() const;	// returns only text in ucs2

	// these methods return text length in Unicode chars
	int SetRichText(const wchar_t *text);
	int SetRichTextRtf(const char *text);

	// enables or disables content editing
	void SetReadOnly(bool bReadOnly);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlSlider

class MIR_CORE_EXPORT CCtrlSlider : public CCtrlData
{
	typedef CCtrlData CSuper;

	int m_wMin, m_wMax;

protected:
	BOOL OnCommand(MWindow hwndCtrl, uint16_t idCtrl, uint16_t idCode) override;

public:
	CCtrlSlider(CDlgBase *dlg, int ctrlId, int max = 100, int min = 0);

	bool OnApply() override;
	void OnReset() override;

	int  GetPosition() const;
	void SetPosition(int pos);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlSpin

class MIR_CORE_EXPORT CCtrlSpin : public CCtrlData
{
	typedef CCtrlData CSuper;

	int16_t m_wMin, m_wMax, m_wCurr;

	BOOL OnNotify(int, NMHDR*) override;

public:
	CCtrlSpin(CDlgBase *dlg, int ctrlId, int16_t max = 100, int16_t min = 0);

	bool OnApply() override;
	void OnReset() override;

	int16_t GetPosition();
	void SetPosition(int16_t pos);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlListBox

class MIR_CORE_EXPORT CCtrlListBox : public CCtrlBase
{
	typedef CCtrlBase CSuper;

public:
	CCtrlListBox(CDlgBase *dlg, int ctrlId);

	int      AddString(const wchar_t *text, LPARAM data=0);
	void     DeleteString(int index);
	int      FindString(const wchar_t *str, int index = -1, bool exact = false);
	int      GetCount() const;
	int      GetCurSel() const;
	LPARAM   GetItemData(int index) const;
	int      GetItemRect(int index, RECT *pResult) const;
	wchar_t* GetItemText(int index) const;
	wchar_t* GetItemText(int index, wchar_t *buf, int size) const;
	bool     GetSel(int index) const;
	int      GetSelCount() const;
	int*     GetSelItems(int *items, int count) const;
	int*     GetSelItems() const;
	int      InsertString(const wchar_t *text, int pos, LPARAM data=0);
	void     ResetContent();
	int      SelectString(const wchar_t *str);
	int      SetCurSel(int index);
	void     SetItemData(int index, LPARAM data);
	void     SetItemHeight(int index, int iHeight);
	void     SetSel(int index, bool sel = true);

	// Events
	CCallback<CCtrlListBox>	OnDblClick;
	CCallback<CCtrlListBox>	OnSelCancel;
	CCallback<CCtrlListBox>	OnSelChange;

protected:
	BOOL OnCommand(MWindow hwndCtrl, uint16_t idCtrl, uint16_t idCode) override;
	void GetCaretPos(CContextMenuPos&) const override;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlCombo

class MIR_CORE_EXPORT CCtrlCombo : public CCtrlData
{
	typedef CCtrlData CSuper;

public:
	CCtrlCombo(CDlgBase *dlg, int ctrlId);

	BOOL OnCommand(MWindow /*hwndCtrl*/, uint16_t /*idCtrl*/, uint16_t idCode) override;
	void OnInit() override;
	bool OnApply() override;
	void OnReset() override;

	// returns item data associated with the selected item or -1
	LPARAM   GetCurData() const;

	// selects line with userdata passed. returns index of this line or -1
	int      SelectData(LPARAM data);

	// Control interface
	int      AddString(const wchar_t *text, LPARAM data = 0);
	int      AddStringA(const char *text, LPARAM data = 0);
	void     DeleteString(int index);
	int      FindString(const wchar_t *str, int index = -1, bool exact = false);
	int      FindStringA(const char *str, int index = -1, bool exact = false);
	int      GetCount() const;
	int      GetCurSel() const;
	bool     GetDroppedState() const;
	LPARAM   GetItemData(int index) const;
	wchar_t* GetItemText(int index) const;
	wchar_t* GetItemText(int index, wchar_t *buf, int size) const;
	int      InsertString(const wchar_t *text, int pos, LPARAM data=0);
	void     ResetContent();
	int      SelectString(const wchar_t *str);
	int      SetCurSel(int index);
	void     SetItemData(int index, LPARAM data);
	void     ShowDropdown(bool show = true);

	// Events
	CCallback<CCtrlCombo>	OnCloseup;
	CCallback<CCtrlCombo>	OnDropdown;
	CCallback<CCtrlCombo>	OnKillFocus;
	CCallback<CCtrlCombo>	OnSelChanged;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlListView

class MIR_CORE_EXPORT CCtrlListView : public CCtrlBase
{
	typedef CCtrlBase CSuper;
	int m_iItem = 0;

public:
	CCtrlListView(CDlgBase *dlg, int ctrlId);

	// direction = -1 or 1. returns new item index
	int MoveItem(int idx, int direction);

	void SetCurSel(int idx);

	// Classic LV interface
	uint32_t   ApproximateViewRect(int cx, int cy, int iCount);
	void       Arrange(UINT code);
	void       CancelEditLabel();
	HIMAGELIST CreateDragImage(int iItem, POINT *lpptUpLeft);
	void       DeleteAllItems();
	void       DeleteColumn(int iCol);
	void       DeleteItem(int iItem);
	MWindow    EditLabel(int iItem);
	int        EnableGroupView(BOOL fEnable);
	BOOL       EnsureVisible(int i, BOOL fPartialOK);
	int        FindItem(int iStart, const LVFINDINFO *plvfi);
	COLORREF   GetBkColor() const;
	void       GetBkImage(LVBKIMAGE *plvbki) const;
	UINT       GetCallbackMask() const;
	BOOL       GetCheckState(UINT iIndex) const;
	void       GetColumn(int iCol, LVCOLUMN *pcol) const;
	void       GetColumnOrderArray(int iCount, int *lpiArray) const;
	int        GetColumnWidth(int iCol) const;
	int        GetCountPerPage() const;
	MWindow    GetEditControl() const;
	uint32_t   GetExtendedListViewStyle() const;
	int        GetFocusedGroup() const;
	int        GetGroupCount() const;
	void       GetGroupInfo(int iGroupId, LVGROUP *pgrp) const;
	void       GetGroupInfoByIndex(int iIndex, LVGROUP *pgrp) const;
	void       GetGroupMetrics(LVGROUPMETRICS *pGroupMetrics) const;
	UINT       GetGroupState(UINT dwGroupId, UINT dwMask) const;
	MWindow    GetHeader() const;
	HCURSOR    GetHotCursor() const;
	int        GetHotItem() const;
	uint32_t   GetHoverTime() const;
	HIMAGELIST GetImageList(int iImageList) const;
	BOOL       GetInsertMark(LVINSERTMARK *plvim) const;
	COLORREF   GetInsertMarkColor() const;
	int        GetInsertMarkRect(RECT *prc) const;
	BOOL       GetISearchString(LPSTR lpsz) const;
	bool       GetItem(LVITEM *pitem) const;
	int        GetItemCount() const;
	void       GetItemPosition(int i, POINT *ppt) const;
	void       GetItemRect(int i, RECT *prc, int code) const;
	uint32_t   GetItemSpacing(BOOL fSmall) const;
	UINT       GetItemState(int i, UINT mask) const;
	void       GetItemText(int iItem, int iSubItem, LPTSTR pszText, int cchTextMax) const;
	int        GetNextItem(int iStart, UINT flags) const;
	BOOL       GetNumberOfWorkAreas(UINT *lpuWorkAreas) const;
	BOOL       GetOrigin(POINT *lpptOrg) const;
	COLORREF   GetOutlineColor() const;
	UINT       GetSelectedColumn() const;
	UINT       GetSelectedCount() const;
	int        GetSelectionMark() const;
	int        GetStringWidth(LPCSTR psz) const;
	BOOL       GetSubItemRect(int iItem, int iSubItem, int code, RECT *lpRect) const;
	COLORREF   GetTextBkColor() const;
	COLORREF   GetTextColor() const;
	void       GetTileInfo(LVTILEINFO *plvtinfo) const;
	void       GetTileViewInfo(LVTILEVIEWINFO *plvtvinfo) const;
	MWindow    GetToolTips() const;
	int        GetTopIndex() const;
	BOOL       GetUnicodeFormat() const;
	uint32_t   GetView() const;
	BOOL       GetViewRect(RECT *prc) const;
	void       GetWorkAreas(int nWorkAreas, RECT *lprc) const;
	BOOL       HasGroup(int dwGroupId);
	int        HitTest(LVHITTESTINFO *pinfo) const;
	int        HitTestEx(LVHITTESTINFO *pinfo);
	int        InsertColumn(int iCol, const LVCOLUMN *pcol);
	int        InsertGroup(int index, LVGROUP *pgrp);
	void       InsertGroupSorted(LVINSERTGROUPSORTED *structInsert);
	int        InsertItem(const LVITEM *pitem);
	BOOL       InsertMarkHitTest(POINT *point, LVINSERTMARK *plvim);
	BOOL       IsGroupViewEnabled();
	UINT       IsItemVisible(UINT index);
	UINT       MapIDToIndex(UINT id);
	UINT       MapIndexToID(UINT index);
	BOOL       RedrawItems(int iFirst, int iLast);
	void       RemoveAllGroups();
	int        RemoveGroup(int iGroupId);
	BOOL       Scroll(int dx, int dy);
	BOOL       SetBkColor(COLORREF clrBk);
	BOOL       SetBkImage(LVBKIMAGE *plvbki);
	BOOL       SetCallbackMask(UINT mask);
	void       SetCheckState(UINT iIndex, BOOL fCheck);
	BOOL       SetColumn(int iCol, LVCOLUMN *pcol);
	BOOL       SetColumnOrderArray(int iCount, int *lpiArray);
	BOOL       SetColumnWidth(int iCol, int cx);
	void       SetExtendedListViewStyle(uint32_t dwExStyle);
	void       SetExtendedListViewStyleEx(uint32_t dwExMask, uint32_t dwExStyle);
	int        SetGroupInfo(int iGroupId, LVGROUP *pgrp);
	void       SetGroupMetrics(LVGROUPMETRICS *pGroupMetrics);
	void       SetGroupState(UINT dwGroupId, UINT dwMask, UINT dwState);
	HCURSOR    SetHotCursor(HCURSOR hCursor);
	int        SetHotItem(int iIndex);
	void       SetHoverTime(uint32_t dwHoverTime);
	uint32_t   SetIconSpacing(int cx, int cy);
	HIMAGELIST SetImageList(HIMAGELIST himl, int iImageList);
	BOOL       SetInfoTip(LVSETINFOTIP *plvSetInfoTip);
	BOOL       SetInsertMark(LVINSERTMARK *plvim);
	COLORREF   SetInsertMarkColor(COLORREF color);
	BOOL       SetItem(const LVITEM *pitem);
	void       SetItemCount(int cItems);
	void       SetItemCountEx(int cItems, uint32_t dwFlags);
	BOOL       SetItemPosition(int i, int x, int y);
	void       SetItemPosition32(int iItem, int x, int y);
	void       SetItemState(int i, UINT state, UINT mask);
	void       SetItemText(int i, int iSubItem, const wchar_t *pszText);
	COLORREF   SetOutlineColor(COLORREF color);
	void       SetSelectedColumn(int iCol);
	int        SetSelectionMark(int iIndex);
	BOOL       SetTextBkColor(COLORREF clrText);
	BOOL       SetTextColor(COLORREF clrText);
	BOOL       SetTileInfo(LVTILEINFO *plvtinfo);
	BOOL       SetTileViewInfo(LVTILEVIEWINFO *plvtvinfo);
	MWindow    SetToolTips(MWindow ToolTip);
	BOOL       SetUnicodeFormat(BOOL fUnicode);
	int        SetView(uint32_t iView);
	void       SetWorkAreas(int nWorkAreas, RECT *lprc);
	int        SubItemHitTest(LVHITTESTINFO *pInfo) const;
	int        SubItemHitTestEx(LVHITTESTINFO *plvhti);
	BOOL       Update(int iItem);

	#ifdef _MSC_VER
	int        SortGroups(PFNLVGROUPCOMPARE pfnGroupCompare, LPVOID plv);
	BOOL       SortItems(PFNLVCOMPARE pfnCompare, LPARAM lParamSort);
	BOOL       SortItemsEx(PFNLVCOMPARE pfnCompare, LPARAM lParamSort);
	#endif // _MSC_VER

	// Additional APIs
	HIMAGELIST CreateImageList(int iImageList);
	void       AddColumn(int iSubItem, const wchar_t *name, int cx);
	void       AddGroup(int iGroupId, const wchar_t *name);
	int        AddItem(const wchar_t *text, int iIcon, LPARAM lParam = 0, int iGroupId = -1);
	void       SetItem(int iItem, int iSubItem, const wchar_t *text, int iIcon = -1);
	LPARAM     GetItemData(int iItem) const;

	// Events
	struct TEventInfo {
		CCtrlListView *treeviewctrl;
		union {
			NMHDR          *nmhdr;
			NMLISTVIEW     *nmlv;
			NMLVDISPINFO   *nmlvdi;
			NMLVSCROLL     *nmlvscr;
			NMLVGETINFOTIP *nmlvit;
			NMLVFINDITEM   *nmlvfi;
			NMITEMACTIVATE *nmlvia;
			NMLVKEYDOWN    *nmlvkey;
			NMLVCUSTOMDRAW *nmcd;
		};
	};

	CCallback<TEventInfo> OnBeginDrag;
	CCallback<TEventInfo> OnBeginLabelEdit;
	CCallback<TEventInfo> OnBeginRDrag;
	CCallback<TEventInfo> OnBeginScroll;
	CCallback<TEventInfo> OnColumnClick;
	CCallback<TEventInfo> OnCustomDraw;
	CCallback<TEventInfo> OnDeleteAllItems;
	CCallback<TEventInfo> OnDeleteItem;
	CCallback<TEventInfo> OnClick;
	CCallback<TEventInfo> OnDoubleClick;
	CCallback<TEventInfo> OnEndLabelEdit;
	CCallback<TEventInfo> OnEndScroll;
	CCallback<TEventInfo> OnGetDispInfo;
	CCallback<TEventInfo> OnGetInfoTip;
	CCallback<TEventInfo> OnHotTrack;
	CCallback<TEventInfo> OnIncrementalSearch;
	CCallback<TEventInfo> OnInsertItem;
	CCallback<TEventInfo> OnItemActivate;
	CCallback<TEventInfo> OnItemChanged;
	CCallback<TEventInfo> OnItemChanging;
	CCallback<TEventInfo> OnKeyDown;
	CCallback<TEventInfo> OnMarqueeBegin;
	CCallback<TEventInfo> OnSetDispInfo;

protected:
	BOOL OnNotify(int idCtrl, NMHDR *pnmh) override;
	void GetCaretPos(CContextMenuPos&) const override;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlTreeView

#undef GetNextSibling
#undef GetPrevSibling

#define MTREE_CHECKBOX    0x0001
#define MTREE_DND         0x0002
#define MTREE_MULTISELECT 0x0004

class MIR_CORE_EXPORT CCtrlTreeView : public CCtrlBase
{
	typedef CCtrlBase CSuper;

	HTREEITEM MoveItemAbove(HTREEITEM hItem, HTREEITEM hInsertAfter, HTREEITEM hParent);

public:
	CCtrlTreeView(CDlgBase *dlg, int ctrlId);

	void       SetFlags(uint32_t dwFlags); // MTREE_* combination

	// Classic TV interface
	HIMAGELIST CreateDragImage(HTREEITEM hItem);
	void       DeleteAllItems();
	void       DeleteItem(HTREEITEM hItem);
	MWindow    EditLabel(HTREEITEM hItem);
	void       EndEditLabelNow(BOOL cancel);
	void       EnsureVisible(HTREEITEM hItem);
	void       Expand(HTREEITEM hItem, uint32_t flag);
	COLORREF   GetBkColor() const;
	uint32_t   GetCheckState(HTREEITEM hItem) const;
	HTREEITEM  GetChild(HTREEITEM hItem) const;
	int        GetCount() const;
	HTREEITEM  GetDropHilight() const;
	MWindow    GetEditControl() const;
	HTREEITEM  GetFirstVisible() const;
	HIMAGELIST GetImageList(int iImage) const;
	int        GetIndent() const;
	COLORREF   GetInsertMarkColor() const;
	bool       GetItem(TVITEMEX *tvi) const;
	int        GetItemHeight() const;
	void       GetItemRect(HTREEITEM hItem, RECT *rcItem, BOOL fItemRect) const;
	uint32_t   GetItemState(HTREEITEM hItem, uint32_t stateMask) const;
	HTREEITEM  GetLastVisible() const;
	COLORREF   GetLineColor() const;
	HTREEITEM  GetNextItem(HTREEITEM hItem, uint32_t flag) const;
	HTREEITEM  GetNextSibling(HTREEITEM hItem) const;
	HTREEITEM  GetNextVisible(HTREEITEM hItem) const;
	HTREEITEM  GetParent(HTREEITEM hItem) const;
	HTREEITEM  GetPrevSibling(HTREEITEM hItem) const;
	HTREEITEM  GetPrevVisible(HTREEITEM hItem) const;
	HTREEITEM  GetRoot() const;
	uint32_t   GetScrollTime() const;
	HTREEITEM  GetSelection() const;
	COLORREF   GetTextColor() const;
	MWindow    GetToolTips() const;
	BOOL       GetUnicodeFormat() const;
	unsigned   GetVisibleCount() const;
	HTREEITEM  HitTest(TVHITTESTINFO *hti) const;
	HTREEITEM  InsertItem(TVINSERTSTRUCT *tvis);
	void       Select(HTREEITEM hItem, uint32_t flag);
	void       SelectDropTarget(HTREEITEM hItem);
	void       SelectItem(HTREEITEM hItem);
	void       SelectSetFirstVisible(HTREEITEM hItem);
	COLORREF   SetBkColor(COLORREF clBack);
	void       SetCheckState(HTREEITEM hItem, uint32_t state);
	HIMAGELIST SetImageList(HIMAGELIST hIml, int iImage);
	void       SetIndent(int iIndent);
	void       SetInsertMark(HTREEITEM hItem, BOOL fAfter);
	COLORREF   SetInsertMarkColor(COLORREF clMark);
	void       SetItem(TVITEMEX *tvi);
	void       SetItemHeight(short cyItem);
	void       SetItemState(HTREEITEM hItem, uint32_t state, uint32_t stateMask);
	COLORREF   SetLineColor(COLORREF clLine);
	void       SetScrollTime(UINT uMaxScrollTime);
	COLORREF   SetTextColor(COLORREF clText);
	MWindow    SetToolTips(MWindow hwndToolTips);
	BOOL       SetUnicodeFormat(BOOL fUnicode);
	void       SortChildren(HTREEITEM hItem, BOOL fRecurse);
	void       SortChildrenCB(TVSORTCB *cb, BOOL fRecurse);

	// Additional stuff
	void       TranslateItem(HTREEITEM hItem);
	void       TranslateTree();
	HTREEITEM  FindNamedItem(HTREEITEM hItem, const wchar_t *name);
	void       GetItem(HTREEITEM hItem, TVITEMEX *tvi) const;
	void       GetItem(HTREEITEM hItem, TVITEMEX *tvi, wchar_t *szText, int iTextLength) const;
	void       InvertCheck(HTREEITEM hItem);

	bool       IsSelected(HTREEITEM hItem);
	int        GetNumSelected();
	void       GetSelected(LIST<_TREEITEM> &selected);

	void       Select(HTREEITEM hItem);
	void       Select(LIST<_TREEITEM> &selected);
	void       SelectAll();
	void       SelectRange(HTREEITEM hStart, HTREEITEM hEnd);

	void       Unselect(HTREEITEM hItem);
	void       UnselectAll();

	void       DropHilite(HTREEITEM hItem);
	void       DropUnhilite(HTREEITEM hItem);

	// Events
	struct TEventInfo {
		CCtrlTreeView *treeviewctrl;
		union {
			NMHDR *nmhdr;
			NMTREEVIEW *nmtv;
			NMTVKEYDOWN *nmtvkey;
			NMTVDISPINFO *nmtvdi;
			NMTVGETINFOTIP *nmtvit;
			NMTVCUSTOMDRAW *nmcd;
			HTREEITEM hItem; // for OnItemChanged
			TVHITTESTINFO *ntvhi; // for OnEndDrag
		};
	};

	CCallback<TEventInfo> OnBeginDrag;
	CCallback<TEventInfo> OnBeginLabelEdit;
	CCallback<TEventInfo> OnBeginRDrag;
	CCallback<TEventInfo> OnCustomDraw;
	CCallback<TEventInfo> OnDeleteItem;
	CCallback<TEventInfo> OnEndDrag;
	CCallback<TEventInfo> OnEndLabelEdit;
	CCallback<TEventInfo> OnGetDispInfo;
	CCallback<TEventInfo> OnGetInfoTip;
	CCallback<TEventInfo> OnItemChanged;
	CCallback<TEventInfo> OnItemExpanded;
	CCallback<TEventInfo> OnItemExpanding;
	CCallback<TEventInfo> OnKeyDown;
	CCallback<TEventInfo> OnRightClick;
	CCallback<TEventInfo> OnSelChanged;
	CCallback<TEventInfo> OnSelChanging;
	CCallback<TEventInfo> OnSetDispInfo;
	CCallback<TEventInfo> OnSingleExpand;

protected:
	void OnInit() override;
	BOOL OnNotify(int idCtrl, NMHDR *pnmh) override;

	void GetCaretPos(CContextMenuPos&) const override;
	LRESULT CustomWndProc(UINT msg, WPARAM wParam, LPARAM lParam) override;

	union {
		uint32_t m_dwFlags;
		struct {
			bool m_bDndEnabled : 1;
			bool m_bDragging : 1;
			bool m_bCheckBox : 1;
			bool m_bMultiSelect : 1;
		};
	};
	HTREEITEM m_hDragItem; // valid if m_bDragging == true
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlTreeOpts - array of options with sections

class MIR_CORE_EXPORT CCtrlTreeOpts : public CCtrlTreeView
{
	typedef CCtrlTreeView CSuper;

public:
	CCtrlTreeOpts(CDlgBase *dlg, int ctrlId);
	~CCtrlTreeOpts();

	void AddOption(const wchar_t *pwszSection, const wchar_t *pwszName, CMOption<bool> &option);
	void AddOption(const wchar_t *pwszSection, const wchar_t *pwszName, bool &option);
	void AddOption(const wchar_t *pwszSection, const wchar_t *pwszName, uint32_t &option, uint32_t mask);

	BOOL OnNotify(int idCtrl, NMHDR *pnmh) override;
	void OnDestroy() override;
	void OnInit() override;
	bool OnApply() override;

	CCallback<CCtrlTreeOpts> OnFillOptions;

protected:
	struct COptionsItem
	{
		const wchar_t *m_pwszSection, *m_pwszName;

		union
		{
			CMOption<bool> *m_option;
			bool *m_pBool;
			struct
			{
				uint32_t *m_pDword;
				uint32_t m_mask;
			};
		};

		HTREEITEM m_hItem = nullptr;
		enum OptionItemType { CMOPTION = 1, BOOL = 2, MASK = 3 };
		OptionItemType m_type;

		COptionsItem(const wchar_t *pwszSection, const wchar_t *pwszName, OptionItemType type) :
			m_pwszSection(pwszSection),
			m_pwszName(pwszName),
			m_type(type)
		{}
	};

	OBJLIST<COptionsItem> m_options;

	void ProcessItemClick(HTREEITEM hti);
};

/////////////////////////////////////////////////////////////////////////////////////////
// CCtrlPages

#define PSN_INFOCHANGED    1

// force-send a PSN_INFOCHANGED to all pages
#define PSM_FORCECHANGED  (WM_USER+100)

class MIR_CORE_EXPORT CCtrlPages : public CCtrlBase
{
	typedef CCtrlBase CSuper;

	HIMAGELIST m_hIml;
	CDlgBase *m_pActivePage;
	int m_numRows = 1;
	bool m_bOwnPages = false;

	struct TPageInfo;
	void CreatePage(CDlgBase *pDlg);
	void InsertPage(TPageInfo *pPage);
	void ShowPage(CDlgBase *pDlg);

	void CheckRowCount();
	TPageInfo* GetCurrPage();
	TPageInfo* GetItemPage(int iPage);
	LIST<TPageInfo> m_pages;

public:
	CCtrlPages(CDlgBase *dlg, int ctrlId);

	void AddPage(const wchar_t *ptszName, HICON hIcon, CDlgBase *pDlg);
	void ActivatePage(int iPage);
	int  GetCount(void);
	int  GetDlgIndex(CDlgBase*);
	void RemovePage(int iPage);
	void SwapPages(int idx1, int idx2);

	CDlgBase* GetNthPage(int iPage);

	__forceinline CDlgBase* GetActivePage() const
	{	return m_pActivePage;
	}

	__forceinline void SetPageOwner()
	{	m_bOwnPages = true;
	}

protected:
	BOOL OnNotify(int idCtrl, NMHDR *pnmh) override;

	void OnInit() override;
	void OnDestroy() override;

	bool OnApply() override;
	void OnReset() override;

	LRESULT CustomWndProc(UINT msg, WPARAM wParam, LPARAM lParam) override;
};

/////////////////////////////////////////////////////////////////////////////////////////
// CProtoDlgBase

#define WM_PROTO_REFRESH              (WM_USER + 100)
#define WM_PROTO_CHECK_ONLINE         (WM_USER + 101)
#define WM_PROTO_ACTIVATE             (WM_USER + 102)
#define WM_PROTO_LAST                 (WM_USER + 200)

struct PROTO_INTERFACE;

class MIR_APP_EXPORT CProtoIntDlgBase : public CDlgBase
{
	typedef CDlgBase CSuper;

public:
	CProtoIntDlgBase(PROTO_INTERFACE *proto, int idDialog);

	void CreateLink(CCtrlData &ctrl, const char *szSetting, uint8_t type, uint32_t iValue);
	void CreateLink(CCtrlData &ctrl, const char *szSetting, wchar_t *szValue);

	template<class T>
	__inline void CreateLink(CCtrlData &ctrl, CMOption<T> &option)
	{
		ctrl.CreateDbLink(new CMOptionLink<T>(option));
	}

	__inline PROTO_INTERFACE *GetProtoInterface() { return m_proto_interface; }

	void SetStatusText(const wchar_t *statusText);

protected:
	PROTO_INTERFACE *m_proto_interface;
	MWindow m_hwndStatus = nullptr;

	INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam) override;

	virtual void OnProtoRefresh(WPARAM, LPARAM);
	virtual void OnProtoActivate(WPARAM, LPARAM);
	virtual void OnProtoCheckOnline(WPARAM, LPARAM);

private:
	void UpdateStatusBar();
};

template<typename TProto>
class CProtoDlgBase : public CProtoIntDlgBase
{
	typedef CProtoIntDlgBase CSuper;

public:
	__inline CProtoDlgBase<TProto>(TProto *proto, int idDialog) :
		CProtoIntDlgBase(proto, idDialog),
		m_proto(proto)
	{
	}

	__inline TProto *GetProto() { return m_proto; }

protected:
	TProto* m_proto;
};

/////////////////////////////////////////////////////////////////////////////////////////
// Safe open/close dialogs
#define UI_SAFE_OPEN(dlgClass, dlgPtr)	\
	{	\
		if (dlgPtr)	\
		{	\
			SetForegroundWindow((dlgPtr)->GetHwnd());	\
		} else	\
		{	\
			(dlgPtr) = new dlgClass(this);	\
			(dlgPtr)->Show();	\
		}	\
	}

#define UI_SAFE_OPEN_EX(dlgClass, dlgPtr, dlgLocal)	\
	if (dlgPtr)	\
	{	\
		::SetForegroundWindow((dlgPtr)->GetHwnd());	\
	} else	\
	{	\
		(dlgPtr) = new dlgClass(this);	\
		(dlgPtr)->Show();	\
	}	\
	dlgClass *dlgLocal = (dlgClass *)(dlgPtr);

#define UI_SAFE_CLOSE(dlg)	\
	{	\
		if (dlg) {	\
			(dlg)->Close();	\
			(dlg) = NULL;	\
		}	\
	}

#define UI_SAFE_CLOSE_HWND(hwnd)	\
	{	\
		if (hwnd) {	\
			::SendMessage((hwnd), WM_CLOSE, 0, 0);	\
			(hwnd) = NULL;	\
		}	\
	}

/////////////////////////////////////////////////////////////////////////////////////////
// NULL-Safe dialog notifications
#define UI_SAFE_NOTIFY(dlg, msg)	\
	{	\
		if (dlg)	\
			::SendMessage((dlg)->GetHwnd(), msg, 0, 0);	\
	}

#define UI_SAFE_NOTIFY_HWND(hwnd, msg)	\
	{	\
		if (hwnd)	\
			::SendMessage((hwnd), msg, 0, 0);	\
	}

/////////////////////////////////////////////////////////////////////////////////////////
// Define message maps
#define UI_MESSAGE_MAP(dlgClass, baseDlgClass)	\
	typedef baseDlgClass CMessageMapSuperClass;	\
	virtual INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam)	\
	{	\
		switch (msg)	\
		{	\
		case 0:	\
			break	/* just to handle ";" symbol after macro */

#define UI_MESSAGE(msg, proc)	\
		case msg:	\
			proc(msg, wParam, lParam);	\
			break

#define UI_MESSAGE_EX(msg, func)	\
		case msg:	\
			return func(msg, wParam, lParam)

#define UI_POSTPROCESS_MESSAGE(msg, proc)	\
		case msg:	\
			CMessageMapSuperClass::DlgProc(msg, wParam, lParam);	\
			return FALSE

#define UI_POSTPROCESS_MESSAGE_EX(msg, func)	\
		case msg:	\
			CMessageMapSuperClass::DlgProc(msg, wParam, lParam);	\
			return func(msg, wParam, lParam)

#define UI_MESSAGE_MAP_END()	\
		}	\
		return CMessageMapSuperClass::DlgProc(msg, wParam, lParam);	\
	}

#endif // __M_GUI_H