summaryrefslogtreecommitdiff
path: root/plugins/Popup/src/opt_gen.cpp
blob: 109598ee87c8a835ee7016e9ad4ae0e030ecd1f7 (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
/*
Popup Plus plugin for Miranda IM

Copyright	© 2002 Luca Santarelli,
			© 2004-2007 Victor Pavlychko
			© 2010 MPK
			© 2010 Merlin_de

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.

===============================================================================

File name      : $HeadURL: http://svn.miranda.im/mainrepo/popup/trunk/src/opt_gen.cpp $
Revision       : $Revision: 1628 $
Last change on : $Date: 2010-06-30 03:42:27 +0300 (Ср, 30 июн 2010) $
Last change by : $Author: Merlin_de $

===============================================================================
*/

#include "headers.h"

INT_PTR CALLBACK PositionBoxDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);

//Helper for Status Tree
static int CountStatusModes(DWORD flags)
{
	int res = 0;
	if (flags & PF2_ONLINE) ++res;
	if (flags & PF2_INVISIBLE) ++res;
	if (flags & PF2_SHORTAWAY) ++res;
	if (flags & PF2_LONGAWAY) ++res;
	if (flags & PF2_LIGHTDND) ++res;
	if (flags & PF2_HEAVYDND) ++res;
	if (flags & PF2_FREECHAT) ++res;
	if (flags & PF2_OUTTOLUNCH) ++res;
	if (flags & PF2_ONTHEPHONE) ++res;
	if (res) ++res; // Offline
	return res;
}

int AddStatusMode (OPTTREE_OPTION *options, int pos, LPTSTR prefix, DWORD flag)
{
	if (!flag) return pos;
	options[pos].dwFlag = flag;
	options[pos].groupId = OPTTREE_CHECK;
	options[pos].pszOptionName = (LPTSTR)mir_alloc(sizeof(TCHAR)*lstrlen(prefix)+32);
	options[pos].pszSettingName = mir_tstrdup(prefix);
	options[pos].iconIndex = 0;

	lstrcpy(options[pos].pszOptionName, prefix);
	switch (flag)
	{
		case PF2_IDLE:		lstrcat(options[pos].pszOptionName, _T("/Offline")); break;
		case PF2_ONLINE:	lstrcat(options[pos].pszOptionName, _T("/Online")); break;
		case PF2_INVISIBLE:	lstrcat(options[pos].pszOptionName, _T("/Invisible")); break;
		case PF2_SHORTAWAY:	lstrcat(options[pos].pszOptionName, _T("/Away")); break;
		case PF2_LONGAWAY:	lstrcat(options[pos].pszOptionName, _T("/NA")); break;
		case PF2_LIGHTDND:	lstrcat(options[pos].pszOptionName, _T("/Occupied")); break;
		case PF2_HEAVYDND:	lstrcat(options[pos].pszOptionName, _T("/DND")); break;
		case PF2_FREECHAT:	lstrcat(options[pos].pszOptionName, _T("/Free for chat")); break;
		case PF2_OUTTOLUNCH:lstrcat(options[pos].pszOptionName, _T("/Out to lunch")); break;
		case PF2_ONTHEPHONE:lstrcat(options[pos].pszOptionName, _T("/On the phone")); break;
	}
	return pos+1;
}

int AddStatusModes(OPTTREE_OPTION *options, int pos, LPTSTR prefix, DWORD flags)
{
	pos = AddStatusMode(options, pos, prefix, PF2_IDLE);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_ONLINE);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_INVISIBLE);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_SHORTAWAY);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_LONGAWAY);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_LIGHTDND);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_HEAVYDND);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_FREECHAT);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_OUTTOLUNCH);
	pos = AddStatusMode(options, pos, prefix, flags&PF2_ONTHEPHONE);
	return pos;
}


//Main Dialog Proc
void LoadOption_General() {
	//Seconds
	PopUpOptions.InfiniteDelay		= DBGetContactSettingByte(NULL, MODULNAME, "InfiniteDelay", FALSE);
	PopUpOptions.Seconds			= DBGetContactSettingRangedWord(NULL, MODULNAME, "Seconds",
									  SETTING_LIFETIME_DEFAULT, SETTING_LIFETIME_MIN, SETTING_LIFETIME_MAX);
	PopUpOptions.LeaveHovered		= DBGetContactSettingByte(NULL, MODULNAME, "LeaveHovered", TRUE);
	//Dynamic Resize
	PopUpOptions.DynamicResize		= DBGetContactSettingByte(NULL, MODULNAME, "DynamicResize", FALSE);
	PopUpOptions.UseMinimumWidth	= DBGetContactSettingByte(NULL, MODULNAME, "UseMinimumWidth", TRUE);
	PopUpOptions.MinimumWidth		= DBGetContactSettingWord(NULL, MODULNAME, "MinimumWidth", 160);
	PopUpOptions.UseMaximumWidth	= DBGetContactSettingByte(NULL, MODULNAME, "UseMaximumWidth", TRUE);
	PopUpOptions.MaximumWidth		= DBGetContactSettingWord(NULL, MODULNAME, "MaximumWidth", 300);
	//Position
	PopUpOptions.Position			= DBGetContactSettingRangedByte(NULL, MODULNAME, "Position",
									  POS_LOWERRIGHT, POS_MINVALUE, POS_MAXVALUE);
	//Configure popup area
	PopUpOptions.gapTop				= DBGetContactSettingWord(NULL, MODULNAME, "gapTop", 5);
	PopUpOptions.gapBottom			= DBGetContactSettingWord(NULL, MODULNAME, "gapBottom", 5);
	PopUpOptions.gapLeft			= DBGetContactSettingWord(NULL, MODULNAME, "gapLeft", 5);
	PopUpOptions.gapRight			= DBGetContactSettingWord(NULL, MODULNAME, "gapRight", 5);
	PopUpOptions.spacing			= DBGetContactSettingWord(NULL, MODULNAME, "spacing", 5);
	//Spreading
	PopUpOptions.Spreading			= DBGetContactSettingRangedByte(NULL, MODULNAME, "Spreading",
									  SPREADING_VERTICAL, SPREADING_MINVALUE, SPREADING_MAXVALUE);
	//miscellaneous
	PopUpOptions.ReorderPopUps			= DBGetContactSettingByte(NULL, MODULNAME, "ReorderPopUps", TRUE);
	PopUpOptions.ReorderPopUpsWarning	= DBGetContactSettingByte(NULL, MODULNAME, "ReorderPopUpsWarning", TRUE);
	//disable When
	PopUpOptions.ModuleIsEnabled		= DBGetContactSettingByte(NULL, MODULNAME, "ModuleIsEnabled", TRUE);
	PopUpOptions.DisableWhenFullscreen	= DBGetContactSettingByte(NULL, MODULNAME, "DisableWhenFullscreen", TRUE);
	//new status options (done inside WM_INITDIALOG)
	//Debug (done inside LoadOptions())
}

INT_PTR CALLBACK DlgProcPopUpGeneral(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	static bool bDlgInit = false;	//some controls send WM_COMMAND before or during WM_INITDIALOG

	static OPTTREE_OPTION *statusOptions = NULL;
	static int statusOptionsCount = 0;
	if (statusOptions) {
		int index;
		if (OptTree_ProcessMessage(hwnd, msg, wParam, lParam, &index, IDC_STATUSES, statusOptions, statusOptionsCount))
			return TRUE;
	}

	switch (msg) {
		case WM_INITDIALOG: {
			HWND hCtrl = NULL;

			//Seconds of delay
			{
			CheckDlgButton(hwnd, IDC_INFINITEDELAY, PopUpOptions.InfiniteDelay);
			CheckDlgButton(hwnd, IDC_LEAVEHOVERED, PopUpOptions.LeaveHovered);
			EnableWindow(GetDlgItem(hwnd, IDC_SECONDS), !PopUpOptions.InfiniteDelay);
			EnableWindow(GetDlgItem(hwnd, IDC_SECONDS_STATIC1), !PopUpOptions.InfiniteDelay);
			EnableWindow(GetDlgItem(hwnd, IDC_SECONDS_STATIC2), !PopUpOptions.InfiniteDelay);
			EnableWindow(GetDlgItem(hwnd, IDC_LEAVEHOVERED), !PopUpOptions.InfiniteDelay);
			SetDlgItemInt(hwnd, IDC_SECONDS, PopUpOptions.Seconds, FALSE);
			SendDlgItemMessage(hwnd, IDC_SECONDS_SPIN, UDM_SETRANGE, 0, (LPARAM)MAKELONG(SETTING_LIFETIME_MAX, SETTING_LIFETIME_MIN));
			}

			//Dynamic Resize
			{
			CheckDlgButton(hwnd, IDC_DYNAMICRESIZE, PopUpOptions.DynamicResize);
			SetDlgItemText(hwnd, IDC_USEMAXIMUMWIDTH, PopUpOptions.DynamicResize ? _T("Maximum width"): _T("Width"));
			//Minimum Width
			CheckDlgButton(hwnd, IDC_USEMINIMUMWIDTH, PopUpOptions.UseMinimumWidth);
			SendDlgItemMessage(hwnd, IDC_MINIMUMWIDTH_SPIN, UDM_SETRANGE, 0, (LPARAM)MAKELONG(
				SETTING_MAXIMUMWIDTH_MAX,
				SETTING_MINIMUMWIDTH_MIN) );
			SetDlgItemInt(hwnd, IDC_MINIMUMWIDTH, PopUpOptions.MinimumWidth, FALSE);
			//Maximum Width
			PopUpOptions.UseMaximumWidth = PopUpOptions.DynamicResize ? PopUpOptions.UseMaximumWidth : TRUE;
			CheckDlgButton(hwnd, IDC_USEMAXIMUMWIDTH, PopUpOptions.UseMaximumWidth);
			SendDlgItemMessage(hwnd, IDC_MAXIMUMWIDTH_SPIN, UDM_SETRANGE, 0, (LPARAM)MAKELONG(
				SETTING_MAXIMUMWIDTH_MAX,
				SETTING_MINIMUMWIDTH_MIN) );
			SetDlgItemInt(hwnd, IDC_MAXIMUMWIDTH, PopUpOptions.MaximumWidth, FALSE);
			//And finally let's enable/disable them.
			EnableWindow(GetDlgItem(hwnd, IDC_USEMINIMUMWIDTH),		PopUpOptions.DynamicResize);
			EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH),		PopUpOptions.DynamicResize && PopUpOptions.UseMinimumWidth);
			EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH_SPIN),	PopUpOptions.DynamicResize && PopUpOptions.UseMinimumWidth);
			EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH),		PopUpOptions.UseMaximumWidth);
			EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH_SPIN),	PopUpOptions.UseMaximumWidth);
			}
			//Position combobox.
			{
			hCtrl = GetDlgItem(hwnd, IDC_WHERE);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("upper left corner"))	,POS_UPPERLEFT);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("lower left corner"))	,POS_LOWERLEFT);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("lower right corner"))	,POS_LOWERRIGHT);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("upper right corner"))	,POS_UPPERRIGHT);
			SendDlgItemMessage(hwnd, IDC_WHERE, CB_SETCURSEL, (PopUpOptions.Position) , 0);
			}
			//Configure popup area
			{
			hCtrl = GetDlgItem(hwnd, IDC_CUSTOMPOS);
			SendMessage(hCtrl, BUTTONSETASFLATBTN, 0, 0);
			SendMessage(hCtrl, BUTTONADDTOOLTIP, (WPARAM)_T("Popup Area"), BATF_TCHAR);
			SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)IcoLib_GetIcon(ICO_OPT_RESIZE,0));
			}
			//Spreading combobox
			{
			hCtrl = GetDlgItem(hwnd, IDC_LAYOUT);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("horizontal"))	,SPREADING_HORIZONTAL);
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("vertical"))	,SPREADING_VERTICAL);
			SendDlgItemMessage(hwnd, IDC_LAYOUT, CB_SETCURSEL, (PopUpOptions.Spreading), 0);
			}
			//miscellaneous
			{
			CheckDlgButton(hwnd, IDC_REORDERPOPUPS, PopUpOptions.ReorderPopUps);
			}
			//Popup enabled
			{
			CheckDlgButton(hwnd, IDC_POPUPENABLED,		PopUpOptions.ModuleIsEnabled?BST_UNCHECKED:BST_CHECKED);
			CheckDlgButton(hwnd, IDC_DISABLEINFS,			PopUpOptions.DisableWhenFullscreen);
			EnableWindow(GetDlgItem(hwnd, IDC_DISABLEINFS),	PopUpOptions.ModuleIsEnabled);
			EnableWindow(GetDlgItem(hwnd, IDC_STATUSES),	PopUpOptions.ModuleIsEnabled);
			}
			//new status options
			{
			int protocolCount = 0;
			int i;
			PROTOCOLDESCRIPTOR **protocols;
			CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&protocolCount, (LPARAM)&protocols);
			DWORD globalFlags = 0;
			for (i = 0; i < protocolCount; ++i) {
				if (protocols[i]->type != PROTOTYPE_PROTOCOL)
					continue;
				DWORD protoFlags = CallProtoService(protocols[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0);
				globalFlags |= protoFlags;
				statusOptionsCount += CountStatusModes(protoFlags);
			}
			statusOptionsCount += CountStatusModes(globalFlags);

			statusOptions = new OPTTREE_OPTION[statusOptionsCount];

			int pos = 0;
			pos = AddStatusModes(statusOptions, pos, _T("Global Status"), globalFlags);
			for (i = 0; i < protocolCount; ++i) {
				if (protocols[i]->type != PROTOTYPE_PROTOCOL)
					continue;
				DWORD protoFlags = CallProtoService(protocols[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0);
				if (!CountStatusModes(protoFlags))
					continue;
				TCHAR prefix[128];
				wsprintf(prefix, _T("Protocol Status/%hs"), protocols[i]->szName);
				pos = AddStatusModes(statusOptions, pos, prefix, protoFlags);
			}

			int index;
			OptTree_ProcessMessage(hwnd, msg, wParam, lParam, &index, IDC_STATUSES, statusOptions, statusOptionsCount);

			char prefix[128];
			LPTSTR pszSettingName = NULL;
			for (i = 0; i < protocolCount; ++i) {
				if (protocols[i]->type != PROTOTYPE_PROTOCOL)
					continue;
				DWORD protoFlags = CallProtoService(protocols[i]->szName, PS_GETCAPS, PFLAGNUM_2, 0);
				if (!CountStatusModes(protoFlags))
					continue;

				mir_snprintf(prefix, sizeof(prefix), "Protocol Status/%s", protocols[i]->szName);
				pszSettingName = mir_a2t(prefix);
				OptTree_SetOptions(hwnd, IDC_STATUSES, statusOptions, statusOptionsCount,
					DBGetContactSettingDword(NULL, MODULNAME, prefix, 0),
					pszSettingName);
				mir_free(pszSettingName); pszSettingName = NULL;
			}
			OptTree_SetOptions(hwnd, IDC_STATUSES, statusOptions, statusOptionsCount,
				DBGetContactSettingDword(NULL, MODULNAME, "Global Status", 0),
				_T("Global Status"));
			}
			//Debug
			{
			#if defined(_DEBUG)
				CheckDlgButton(hwnd, IDC_DEBUG, PopUpOptions.debug);
				ShowWindow(GetDlgItem(hwnd, IDC_DEBUG), SW_SHOW);
			#endif
			}

			TranslateDialogDefault(hwnd);	//do it on end of WM_INITDIALOG
			bDlgInit = true;
			}//end WM_INITDIALOG
			return TRUE;
		case WM_COMMAND: {
				UINT idCtrl = LOWORD(wParam);
				switch (HIWORD(wParam)) {
					case BN_CLICKED:		//Button controls
						switch(idCtrl) {
							case IDC_INFINITEDELAY:
								{
								PopUpOptions.InfiniteDelay = !PopUpOptions.InfiniteDelay;
								EnableWindow(GetDlgItem(hwnd, IDC_SECONDS),!PopUpOptions.InfiniteDelay);
								EnableWindow(GetDlgItem(hwnd, IDC_SECONDS_STATIC1),	!PopUpOptions.InfiniteDelay);
								EnableWindow(GetDlgItem(hwnd, IDC_SECONDS_STATIC2),	!PopUpOptions.InfiniteDelay);
								EnableWindow(GetDlgItem(hwnd, IDC_LEAVEHOVERED),	!PopUpOptions.InfiniteDelay);
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_LEAVEHOVERED:
								{
								PopUpOptions.LeaveHovered = !PopUpOptions.LeaveHovered;
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_DYNAMICRESIZE:
								{
								PopUpOptions.DynamicResize=!PopUpOptions.DynamicResize;
								EnableWindow(GetDlgItem(hwnd, IDC_USEMINIMUMWIDTH),		PopUpOptions.DynamicResize);
								EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH),		PopUpOptions.DynamicResize && PopUpOptions.UseMinimumWidth);
								EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH_SPIN),	PopUpOptions.DynamicResize && PopUpOptions.UseMinimumWidth);
								SetDlgItemText(hwnd, IDC_USEMAXIMUMWIDTH, PopUpOptions.DynamicResize ? TranslateT("Maximum width"): TranslateT("Width"));
								if(!PopUpOptions.DynamicResize) {
									PopUpOptions.UseMaximumWidth = TRUE;
									CheckDlgButton(hwnd, IDC_USEMAXIMUMWIDTH, BST_CHECKED);
									EnableWindow(GetDlgItem(hwnd, IDC_USEMAXIMUMWIDTH),		TRUE);
									EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH),		TRUE);
									EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH_SPIN),	TRUE);
								}
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_USEMINIMUMWIDTH:
								{
								PopUpOptions.UseMinimumWidth= !PopUpOptions.UseMinimumWidth;
								EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH),		PopUpOptions.UseMinimumWidth);
								EnableWindow(GetDlgItem(hwnd, IDC_MINIMUMWIDTH_SPIN),	PopUpOptions.UseMinimumWidth);
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_USEMAXIMUMWIDTH:
								{
								PopUpOptions.UseMaximumWidth= Button_GetCheck((HWND)lParam);
								if(!PopUpOptions.DynamicResize) { //ugly - set always on if DynamicResize = off
									CheckDlgButton(hwnd, idCtrl, BST_CHECKED);
									PopUpOptions.UseMaximumWidth = TRUE;
								}
								EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH), PopUpOptions.UseMaximumWidth);
								EnableWindow(GetDlgItem(hwnd, IDC_MAXIMUMWIDTH_SPIN),	PopUpOptions.UseMaximumWidth);
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_CUSTOMPOS:
								{
								RECT rcButton, rcBox;
								HWND hwndBox = CreateDialog(hInst, MAKEINTRESOURCE(IDD_POSITION), NULL, PositionBoxDlgProc);
								GetWindowRect((HWND)lParam, &rcButton);
								GetWindowRect(hwndBox, &rcBox);
								MoveWindow(hwndBox,
									rcButton.right-(rcBox.right-rcBox.left) + 15,
									rcButton.bottom + 3,
									rcBox.right-rcBox.left,
									rcBox.bottom-rcBox.top,
									FALSE);
#if defined(_UNICODE)
								SetWindowLongPtr(hwndBox, GWL_EXSTYLE, GetWindowLongPtr(hwndBox, GWL_EXSTYLE)|WS_EX_LAYERED);
								SetLayeredWindowAttributes(hwndBox, NULL, 0, LWA_ALPHA);
								ShowWindow(hwndBox, SW_SHOW);
								for (int i = 0; i <= 255; i += 15) {
									SetLayeredWindowAttributes(hwndBox, NULL, i, LWA_ALPHA);
									UpdateWindow(hwndBox);
									Sleep(1);
								}
								SetWindowLongPtr(hwndBox, GWL_EXSTYLE, GetWindowLongPtr(hwndBox, GWL_EXSTYLE)&~WS_EX_LAYERED);
#else
								if (MySetLayeredWindowAttributes) {
									SetWindowLongPtr(hwndBox, GWL_EXSTYLE, GetWindowLongPtr(hwndBox, GWL_EXSTYLE)|WS_EX_LAYERED);
									MySetLayeredWindowAttributes(hwndBox, NULL, 0, LWA_ALPHA);
									ShowWindow(hwndBox, SW_SHOW);
									for (int i = 0; i <= 255; i += 15) {
										MySetLayeredWindowAttributes(hwndBox, NULL, i, LWA_ALPHA);
										UpdateWindow(hwndBox);
										Sleep(1);
									}
									SetWindowLongPtr(hwndBox, GWL_EXSTYLE, GetWindowLongPtr(hwndBox, GWL_EXSTYLE)&~WS_EX_LAYERED);
								}
#endif
								ShowWindow(hwndBox, SW_SHOW);
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_REORDERPOPUPS:
								{
								PopUpOptions.ReorderPopUps = !PopUpOptions.ReorderPopUps;
								PopUpOptions.ReorderPopUpsWarning = PopUpOptions.ReorderPopUps ? 
									DBGetContactSettingByte(NULL, MODULNAME, "ReorderPopUpsWarning", TRUE) : TRUE;
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							case IDC_POPUPENABLED:
								{//PopUpOptions.ModuleIsEnabled is set and store by EnableDisable menu service !!!!!!
								int chk = IsDlgButtonChecked(hwnd, IDC_POPUPENABLED);
								if (PopUpOptions.ModuleIsEnabled&&chk || !PopUpOptions.ModuleIsEnabled&&!chk)
									svcEnableDisableMenuCommand(0,0);
								EnableWindow(GetDlgItem(hwnd, IDC_STATUSES),	PopUpOptions.ModuleIsEnabled);
								EnableWindow(GetDlgItem(hwnd, IDC_DISABLEINFS),	PopUpOptions.ModuleIsEnabled);
								}
								break;
							case IDC_DISABLEINFS:
								{
								PopUpOptions.DisableWhenFullscreen = !PopUpOptions.DisableWhenFullscreen;
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								}
								break;
							#if defined(_DEBUG)		//DEBUG
							case IDC_DEBUG:
								PopUpOptions.debug = (BYTE)Button_GetCheck((HWND)lParam);
								//The following line is dangerous, but useful for my debug purposes.
								//MySetLayeredWindowAttributes = (BOOL)!MySetLayeredWindowAttributes;
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								break;
							#endif					//DEBUG
							case IDC_PREVIEW:
								{
								PopUpPreview();
								}
								break;
							default:
								break;
						}
						break;
					case CBN_SELCHANGE:		//ComboBox controls
						switch(idCtrl) {
							//lParam = Handle to the control
							case IDC_WHERE:
								PopUpOptions.Position = ComboBox_GetItemData((HWND)lParam, ComboBox_GetCurSel((HWND)lParam));
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								break;
							case IDC_LAYOUT:
								PopUpOptions.Spreading = ComboBox_GetItemData((HWND)lParam, ComboBox_GetCurSel((HWND)lParam));
								SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
								break;
							default:
								break;
						}
						break;
					case EN_CHANGE:			//Edit controls change
						if(!bDlgInit) break;
						switch(idCtrl) {
							//lParam = Handle to the control
							case IDC_SECONDS:
								{
									int seconds = GetDlgItemInt(hwnd, idCtrl, NULL, FALSE);
									if (	seconds >= SETTING_LIFETIME_MIN && 
											seconds <= SETTING_LIFETIME_MAX &&
											seconds != PopUpOptions.Seconds) {
										PopUpOptions.Seconds = seconds;
										SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
									}
								}
								break;
							case IDC_MINIMUMWIDTH:
								{
									int temp = GetDlgItemInt(hwnd, IDC_MINIMUMWIDTH, NULL, FALSE);
									if (	temp >= SETTING_MINIMUMWIDTH_MIN && 
											temp <= SETTING_MAXIMUMWIDTH_MAX &&
											temp != PopUpOptions.MinimumWidth) {
										PopUpOptions.MinimumWidth = temp;
										SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
									}
								}
								break;
							case IDC_MAXIMUMWIDTH:
								{
									int temp = GetDlgItemInt(hwnd, IDC_MAXIMUMWIDTH, NULL, FALSE);
									if (	temp >= SETTING_MINIMUMWIDTH_MIN && 
											temp <= SETTING_MAXIMUMWIDTH_MAX &&
											temp != PopUpOptions.MaximumWidth) {
										PopUpOptions.MaximumWidth = temp;
										SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0);
									}
								}
								break;
							default:
								break;
						}//end switch(idCtrl)
						break;
					case EN_KILLFOCUS:		//Edit controls lost fokus
						switch(idCtrl) {
							//lParam = Handle to the control
							case IDC_SECONDS:
								{
									int seconds = GetDlgItemInt(hwnd, idCtrl, NULL, FALSE);
									if (seconds > SETTING_LIFETIME_MAX)
										PopUpOptions.Seconds = SETTING_LIFETIME_MAX;
									else if (seconds < SETTING_LIFETIME_MIN)
										PopUpOptions.Seconds = SETTING_LIFETIME_MIN;
									if(seconds != PopUpOptions.Seconds) {
										SetDlgItemInt(hwnd, idCtrl, PopUpOptions.Seconds, FALSE);
										ErrorMSG(SETTING_LIFETIME_MIN, SETTING_LIFETIME_MAX);
										SetFocus((HWND)lParam);
									}
								}
								break;
							case IDC_MINIMUMWIDTH:
								{
									int temp = GetDlgItemInt(hwnd, idCtrl, NULL, FALSE);
									if (temp < SETTING_MINIMUMWIDTH_MIN)
										PopUpOptions.MinimumWidth = SETTING_MINIMUMWIDTH_MIN;
									else if(temp > SETTING_MAXIMUMWIDTH_MAX)
										PopUpOptions.MinimumWidth = SETTING_MAXIMUMWIDTH_MAX;
									if(temp != PopUpOptions.MinimumWidth) {
										SetDlgItemInt(hwnd, idCtrl, PopUpOptions.MinimumWidth, FALSE);
										ErrorMSG(SETTING_MINIMUMWIDTH_MIN, SETTING_MAXIMUMWIDTH_MAX);
										SetFocus((HWND)lParam);
										break;
									}
									if (temp > PopUpOptions.MaximumWidth) {
										PopUpOptions.MaximumWidth = min(temp, SETTING_MAXIMUMWIDTH_MAX);
										SetDlgItemInt(hwnd, IDC_MAXIMUMWIDTH, PopUpOptions.MaximumWidth, FALSE);
									}
								}
								break;
							case IDC_MAXIMUMWIDTH:
								{
									int temp = GetDlgItemInt(hwnd, idCtrl, NULL, FALSE);
									if (temp >= SETTING_MAXIMUMWIDTH_MAX)
										PopUpOptions.MaximumWidth = SETTING_MAXIMUMWIDTH_MAX;
									else if(temp < SETTING_MINIMUMWIDTH_MIN)
										PopUpOptions.MaximumWidth = SETTING_MINIMUMWIDTH_MIN;
									if(temp != PopUpOptions.MaximumWidth) {
										SetDlgItemInt(hwnd, idCtrl, PopUpOptions.MaximumWidth, FALSE);
										ErrorMSG(SETTING_MINIMUMWIDTH_MIN, SETTING_MAXIMUMWIDTH_MAX);
										SetFocus((HWND)lParam);
										break;
									}
									if (temp < PopUpOptions.MinimumWidth) {
										PopUpOptions.MinimumWidth = max(temp, SETTING_MINIMUMWIDTH_MIN);
										SetDlgItemInt(hwnd, IDC_MINIMUMWIDTH, PopUpOptions.MinimumWidth, FALSE);
									}
								}
								break;
							default:
								break;
						}//end switch(idCtrl)
						break;
					default:
						break;
				}// end switch (HIWORD(wParam))
			}//end WM_COMMAND
			break;
		case WM_NOTIFY: {
			switch(((LPNMHDR)lParam)->idFrom) {
				case 0: {
					switch (((LPNMHDR)lParam)->code) {
						case PSN_RESET:
							LoadOption_General();
							return TRUE;
						case PSN_APPLY: {
							//Seconds
							DBWriteContactSettingByte(NULL, MODULNAME, "InfiniteDelay", PopUpOptions.InfiniteDelay);
							DBWriteContactSettingWord(NULL, MODULNAME, "Seconds", (WORD)PopUpOptions.Seconds);
							DBWriteContactSettingByte(NULL, MODULNAME, "LeaveHovered", PopUpOptions.LeaveHovered);
							//Dynamic Resize
							DBWriteContactSettingByte(NULL, MODULNAME, "DynamicResize", PopUpOptions.DynamicResize);
							DBWriteContactSettingByte(NULL, MODULNAME, "UseMinimumWidth", PopUpOptions.UseMinimumWidth);
							DBWriteContactSettingWord(NULL, MODULNAME, "MinimumWidth", PopUpOptions.MinimumWidth);
							DBWriteContactSettingByte(NULL, MODULNAME, "UseMaximumWidth", PopUpOptions.UseMaximumWidth);
							DBWriteContactSettingWord(NULL, MODULNAME, "MaximumWidth", PopUpOptions.MaximumWidth);
							//Position
							DBWriteContactSettingByte(NULL, MODULNAME, "Position", (BYTE)PopUpOptions.Position);
							//Configure popup area
							DBWriteContactSettingWord(NULL, MODULNAME, "gapTop",	(WORD)PopUpOptions.gapTop);
							DBWriteContactSettingWord(NULL, MODULNAME, "gapBottom",	(WORD)PopUpOptions.gapBottom);
							DBWriteContactSettingWord(NULL, MODULNAME, "gapLeft",	(WORD)PopUpOptions.gapLeft);
							DBWriteContactSettingWord(NULL, MODULNAME, "gapRight",	(WORD)PopUpOptions.gapRight);
							DBWriteContactSettingWord(NULL, MODULNAME, "spacing",	(WORD)PopUpOptions.spacing);
							//Spreading
							DBWriteContactSettingByte(NULL, MODULNAME, "Spreading", (BYTE)PopUpOptions.Spreading);
							//miscellaneous
							//DBWriteContactSettingByte(NULL, MODULNAME, "ReorderPopUps", PopUpOptions.ReorderPopUps);
							Check_ReorderPopUps(hwnd);	//this save also PopUpOptions.ReorderPopUps
							//disable When
							DBWriteContactSettingByte(NULL, MODULNAME, "DisableWhenFullscreen", PopUpOptions.DisableWhenFullscreen);
							//new status options
							{
								int protocolCount;
								PROTOCOLDESCRIPTOR **protocols;
								CallService(MS_PROTO_ENUMPROTOCOLS, (WPARAM)&protocolCount, (LPARAM)&protocols);

								char prefix[128];
								LPTSTR pszSettingName = NULL;
								for (int i = 0; i < protocolCount; ++i)
								{
									if (protocols[i]->type != PROTOTYPE_PROTOCOL)
										continue;

									mir_snprintf(prefix, sizeof(prefix), "Protocol Status/%s", protocols[i]->szName);
									pszSettingName = mir_a2t(prefix);
									DBWriteContactSettingDword(NULL, MODULNAME, prefix,
										OptTree_GetOptions(hwnd, IDC_STATUSES, statusOptions, statusOptionsCount, pszSettingName));
									mir_free(pszSettingName); pszSettingName = NULL;
								}
								DBWriteContactSettingDword(NULL, MODULNAME, "Global Status",
									OptTree_GetOptions(hwnd, IDC_STATUSES, statusOptions, statusOptionsCount, _T("Global Status")));
							}
							//Debug
							#if defined(_DEBUG)
								DBWriteContactSettingByte(NULL, MODULNAME, "debug", PopUpOptions.debug);
							#endif

							}//end PSN_APPLY:
							return TRUE;
						default:
							break;
					}
					}
					break;
				case IDC_MINIMUMWIDTH_SPIN:
					{
						LPNMUPDOWN lpnmud = (LPNMUPDOWN) lParam;
						int temp = lpnmud->iPos + lpnmud->iDelta;
						if (temp > PopUpOptions.MaximumWidth) {
							PopUpOptions.MaximumWidth = min(temp, SETTING_MAXIMUMWIDTH_MAX);
							SetDlgItemInt(hwnd, IDC_MAXIMUMWIDTH, PopUpOptions.MaximumWidth, FALSE);
						}
					}
					break;
				case IDC_MAXIMUMWIDTH_SPIN:
					{
						LPNMUPDOWN lpnmud = (LPNMUPDOWN) lParam;
						int temp = lpnmud->iPos + lpnmud->iDelta;
						if (temp < PopUpOptions.MinimumWidth) {
							PopUpOptions.MinimumWidth = max(temp, SETTING_MINIMUMWIDTH_MIN);
							SetDlgItemInt(hwnd, IDC_MINIMUMWIDTH, PopUpOptions.MinimumWidth, FALSE);
						}
					}
					break;
				default:
					break;
			}
			}//end WM_NOTIFY
			break;
		case WM_DESTROY: {
			if (statusOptions) {
				for (int i = 0; i < statusOptionsCount; ++i) {
					mir_free(statusOptions[i].pszOptionName);
					mir_free(statusOptions[i].pszSettingName);
				}
				delete [] statusOptions;
				statusOptions = NULL;
				statusOptionsCount = 0;
				bDlgInit = false;
			}
			}//end WM_DESTROY
			break;
		default:
			return FALSE;
		}//end switch (msg)
	return FALSE;
}

void ErrorMSG(int minValue, int maxValue) {
		TCHAR str[128];
		wsprintf(str, TranslateT("You cannot specify a value lower than %d and higher than %d."), minValue, maxValue);
		MSGERROR(str);
}

void Check_ReorderPopUps(HWND hwnd) {
	if (!PopUpOptions.ReorderPopUps && PopUpOptions.ReorderPopUpsWarning)
	{
		int res = MessageBox(hwnd,
			TranslateTS(
				_T("'Reorder Popups' option is currently diabled.\r\n")
				_T("This may cause misaligned popups when used with\r\n")
				_T("avatars and text replacement (mainly NewStatusNotify).\r\n")
				_T("\r\n")
				_T("Do you want to enable popup reordering now?\r\n")
				),
			TranslateT("Popup Plus Warning"), MB_ICONEXCLAMATION|MB_YESNOCANCEL);

		switch (res)
		{
			case IDYES:
				PopUpOptions.ReorderPopUps = TRUE;
				//Reset warning for next option change !!!
				PopUpOptions.ReorderPopUpsWarning = TRUE;
				break;
			case IDNO:
				PopUpOptions.ReorderPopUps = FALSE;
				PopUpOptions.ReorderPopUpsWarning = FALSE;
				break;
			default:
				return;
		}
	}
	DBWriteContactSettingByte(NULL, MODULNAME, "ReorderPopUps", PopUpOptions.ReorderPopUps);
	DBWriteContactSettingByte(NULL, MODULNAME, "ReorderPopUpsWarning", PopUpOptions.ReorderPopUpsWarning);
	if(hwnd) CheckDlgButton(hwnd, IDC_REORDERPOPUPS, PopUpOptions.ReorderPopUps);
}

void ThemeDialogBackground(HWND hwnd) {
	if (IsWinVerXPPlus()) {
		static HMODULE hThemeAPI = NULL;
		if (!hThemeAPI) hThemeAPI = GetModuleHandleA("uxtheme");
		if (hThemeAPI) {
			HRESULT (STDAPICALLTYPE *MyEnableThemeDialogTexture)(HWND,DWORD) = (HRESULT (STDAPICALLTYPE*)(HWND,DWORD))GetProcAddress(hThemeAPI,"EnableThemeDialogTexture");
			if (MyEnableThemeDialogTexture)
				MyEnableThemeDialogTexture(hwnd,0x00000002|0x00000004); //0x00000002|0x00000004=ETDT_ENABLETAB
		}
	}
}

INT_PTR CALLBACK PositionBoxDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static HFONT hFontTitle = 0;
	switch (msg)
	{
	case WM_INITDIALOG:
		ThemeDialogBackground(hwndDlg);

		LOGFONT lf;
		GetObject((HFONT)SendMessage(GetDlgItem(hwndDlg, IDC_TITLE), WM_GETFONT, 0, 0), sizeof(lf), &lf);
		lf.lfWeight = FW_BOLD;
		lf.lfHeight *= 1.2;
		hFontTitle = CreateFontIndirect(&lf);
		SendMessage(GetDlgItem(hwndDlg, IDC_TITLE), WM_SETFONT, (WPARAM)hFontTitle, TRUE);

		SendMessage(GetDlgItem(hwndDlg, IDOK), BUTTONSETASFLATBTN, 0, 0);
		SendMessage(GetDlgItem(hwndDlg, IDOK), BUTTONADDTOOLTIP, (WPARAM)_T("OK"), BATF_TCHAR);
		SendMessage(GetDlgItem(hwndDlg, IDOK), BM_SETIMAGE, IMAGE_ICON, (LPARAM)IcoLib_GetIcon(ICO_OPT_OK,0));

		SendMessage(GetDlgItem(hwndDlg, IDCANCEL), BUTTONSETASFLATBTN, 0, 0);
		SendMessage(GetDlgItem(hwndDlg, IDCANCEL), BUTTONADDTOOLTIP, (WPARAM)_T("Cancel"), BATF_TCHAR);
		SendMessage(GetDlgItem(hwndDlg, IDCANCEL), BM_SETIMAGE, IMAGE_ICON, (LPARAM)IcoLib_GetIcon(ICO_OPT_CANCEL,0));

		SetDlgItemInt(hwndDlg, IDC_TXT_TOP, PopUpOptions.gapTop, FALSE);
		SetDlgItemInt(hwndDlg, IDC_TXT_BOTTOM, PopUpOptions.gapBottom, FALSE);
		SetDlgItemInt(hwndDlg, IDC_TXT_LEFT, PopUpOptions.gapLeft, FALSE);
		SetDlgItemInt(hwndDlg, IDC_TXT_RIGHT, PopUpOptions.gapRight, FALSE);
		SetDlgItemInt(hwndDlg, IDC_TXT_SPACING, PopUpOptions.spacing, FALSE);

		TranslateDialogDefault(hwndDlg);
		break;
	case WM_COMMAND:
		if ((LOWORD(wParam)) == IDOK)
		{
			PopUpOptions.gapTop = GetDlgItemInt(hwndDlg, IDC_TXT_TOP, NULL, FALSE);
			PopUpOptions.gapBottom = GetDlgItemInt(hwndDlg, IDC_TXT_BOTTOM, NULL, FALSE);
			PopUpOptions.gapLeft = GetDlgItemInt(hwndDlg, IDC_TXT_LEFT, NULL, FALSE);
			PopUpOptions.gapRight = GetDlgItemInt(hwndDlg, IDC_TXT_RIGHT, NULL, FALSE);
			PopUpOptions.spacing = GetDlgItemInt(hwndDlg, IDC_TXT_SPACING, NULL, FALSE);
			PostMessage(hwndDlg, WM_CLOSE, 0, 0);
		} else
		if ((LOWORD(wParam)) == IDCANCEL)
		{
			PostMessage(hwndDlg, WM_CLOSE, 0, 0);
		}
		break;
	case WM_ACTIVATE:
		if (wParam == WA_INACTIVE)
		{
			PostMessage(hwndDlg, WM_CLOSE, 0, 0);
		}
		break;
	case WM_CLOSE:
		DestroyWindow(hwndDlg);
		break;
	case WM_DESTROY:
		DeleteObject(hFontTitle);
		break;
	}
	return FALSE;
}