summaryrefslogtreecommitdiff
path: root/plugins/SendScreenshotPlus/src/UMainForm.cpp
blob: 0c6512f63c4e08a57df78b782f299cb315cdc621 (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-18 Miranda NG team (https://miranda-ng.org),
Copyright (c) 2000-09 Miranda ICQ/IM project,

This file is part of Send Screenshot Plus, a Miranda IM plugin.
Copyright (c) 2010 Ing.U.Horn

Parts of this file based on original sorce code
(c) 2004-2006 Sérgio Vieira Rolanski (portet from Borland C++)

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.
*/

#include "stdafx.h"

#include <list>
void TfrmMain::Unload()
{
	std::list<TfrmMain*> lst;
	for (CHandleMapping::iterator iter = _HandleMapping.begin(); iter != _HandleMapping.end(); ++iter) {
		lst.push_back(iter->second);//we can't delete inside loop.. not MT compatible
	}
	while (!lst.empty()) {
		DestroyWindow(lst.front()->m_hWnd);//deletes class
		lst.pop_front();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

INT_PTR CALLBACK TfrmMain::DlgProc_CaptureTabPage(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	// main message handling is done inside TfrmMain::DlgTfrmMain
	switch (uMsg) {
	case WM_INITDIALOG:
		switch (lParam) {
		case IDD_UMain_CaptureWindow:
			Static_SetIcon(GetDlgItem(hDlg, ID_imgTarget), GetIcon(ICO_TARGET));
			SetDlgItemText(hDlg, ID_edtCaption, TranslateT("Drag&Drop the target on the desired window."));
			break;
		case IDD_UMain_CaptureDesktop:
			Static_SetIcon(GetDlgItem(hDlg, ID_imgTarget), GetIcon(ICO_MONITOR));
			break;
		case IDD_UMain_CaptureFile:
			Static_SetIcon(GetDlgItem(hDlg, ID_imgTarget), GetIcon(ICO_MAIN));
			break;
		}
		SetFocus(GetDlgItem(hDlg, ID_imgTarget));
		return FALSE;

	case WM_CTLCOLORDLG:
	case WM_CTLCOLOREDIT:
	case WM_CTLCOLORSTATIC:
		SetTextColor((HDC)wParam, GetSysColor(COLOR_WINDOWTEXT));
		return (INT_PTR)GetStockObject(WHITE_BRUSH);

	case WM_COMMAND:
		if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == ID_btnExplore) { /// local file tab
			OPENFILENAME ofn = { sizeof(OPENFILENAME) };
			wchar_t filename[MAX_PATH];
			GetDlgItemText(hDlg, ID_edtSize, filename, _countof(filename));
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = hDlg;
			ofn.lpstrFilter = L"Images\0*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tif;*.tiff\0";
			ofn.nFilterIndex = 1;
			ofn.lpstrFile = filename;
			ofn.nMaxFile = MAX_PATH;
			//			ofn.lpstrInitialDir = m_FDestFolder;
			ofn.Flags = OFN_FILEMUSTEXIST | OFN_READONLY;
			if (GetOpenFileName(&ofn)) {
				SetDlgItemText(hDlg, ID_edtSize, filename);
			}
			break;
		}
		SendMessage(GetParent(hDlg), uMsg, wParam, lParam);
		break;
	case WM_NOTIFY:
		SendMessage(GetParent(hDlg), uMsg, wParam, lParam);
		break;
	case WM_DESTROY:
		break;
	}
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////////////////

TfrmMain::CHandleMapping TfrmMain::_HandleMapping;

INT_PTR CALLBACK TfrmMain::DlgTfrmMain(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if (msg == WM_CTLCOLOREDIT || msg == WM_CTLCOLORSTATIC) {
		switch (GetWindowLongPtr((HWND)lParam, GWL_ID)) {
		case IDC_HEADERBAR:
			SetTextColor((HDC)wParam, GetSysColor(COLOR_WINDOWTEXT));
			break;
		default:
			return 0;
		}
		SetBkColor((HDC)wParam, GetSysColor(COLOR_WINDOW));
		return (INT_PTR)GetStockObject(WHITE_BRUSH); 	//GetSysColorBrush(COLOR_WINDOW);
	}

	CHandleMapping::iterator wnd;
	if (msg == WM_INITDIALOG) {
		wnd = _HandleMapping.insert(CHandleMapping::value_type(hWnd, reinterpret_cast<TfrmMain*>(lParam))).first;
		wnd->second->m_hWnd = hWnd;
		wnd->second->wmInitdialog(wParam, lParam);
		return 0;
	}
	wnd = _HandleMapping.find(hWnd);
	if (wnd == _HandleMapping.end()) {	//something screwed up dialog!
		return 0;					//do not use ::DefWindowProc(hWnd, msg, wParam, lParam);
	}

	switch (msg) {
	case WM_DROPFILES:{ /// Drag&Drop of local files
			wchar_t filename[MAX_PATH];
			if (!DragQueryFile((HDROP)wParam, 0, filename, MAX_PATH)) *filename = '\0';
			DragFinish((HDROP)wParam);
			if (wnd->second->m_hwndTabPage)
				ShowWindow(wnd->second->m_hwndTabPage, SW_HIDE);
			TAB_INFO itab = { TCIF_PARAM };
			wnd->second->m_opt_tabCapture = 2; // activate file tab
			TabCtrl_SetCurSel(wnd->second->m_hwndTab, wnd->second->m_opt_tabCapture);
			TabCtrl_GetItem(wnd->second->m_hwndTab, wnd->second->m_opt_tabCapture, &itab);
			wnd->second->m_hwndTabPage = itab.hwndTabPage;
			ShowWindow(wnd->second->m_hwndTabPage, SW_SHOW);
			SetDlgItemText(wnd->second->m_hwndTabPage, ID_edtSize, filename);
			break; }
	case WM_COMMAND:
		wnd->second->wmCommand(wParam, lParam);
		break;
	case WM_CLOSE:
		wnd->second->wmClose(wParam, lParam);
		break;
	case WM_DESTROY:
		delete wnd->second;
		break;
	case WM_NOTIFY:
		wnd->second->wmNotify(wParam, lParam);
		break;
	case WM_TIMER:
		wnd->second->wmTimer(wParam, lParam);
		break;
	case UM_CLOSING:
		wnd->second->UMClosing(wParam, lParam);
		break;
	case UM_EVENT:
		wnd->second->UMevent(wParam, lParam);
		break;
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// WM_INITDIALOG:

int EnumCloudFileServices(const CFSERVICEINFO *serviceInfo, void *param)
{
	HWND hCtrl = (HWND)param;
	ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, serviceInfo->userName), new UPLOAD_INFO(SS_CLOUDFILE, (void*)serviceInfo->accountName));
	return 0;
}

void TfrmMain::wmInitdialog(WPARAM, LPARAM)
{
	HWND hCtrl;
	/// Taskbar and Window icon
	Window_SetIcon_IcoLib(m_hWnd, GetIconHandle(ICO_MAIN));

	wchar_t *pt = mir_wstrdup(pcli->pfnGetContactDisplayName(m_hContact, 0));
	if (pt && (m_hContact != 0)) {
		CMStringW string;
		string.AppendFormat(TranslateT("Send screenshot to %s"), pt);
		SetWindowText(m_hWnd, string);
	}
	mir_free(pt);

	/// Headerbar
	SendDlgItemMessage(m_hWnd, IDC_HEADERBAR, WM_SETICON, ICON_BIG, (LPARAM)GetIcon(ICO_MAIN));

	/// Timed controls
	CheckDlgButton(m_hWnd, ID_chkTimed, m_opt_chkTimed ? BST_CHECKED : BST_UNCHECKED);
	SetDlgItemInt(m_hWnd, ID_edtTimed, (UINT)m_opt_edtTimed, FALSE);
	SendDlgItemMessage(m_hWnd, ID_upTimed, UDM_SETRANGE, 0, (LPARAM)MAKELONG(250, 1));
	chkTimedClick();		//enable disable Timed controls

	/// create Image list for tab control
	if (!m_himlTab) {
		//m_himlTab = ImageList_Create(16, 16, PluginConfig.m_bIsXP ? ILC_COLOR32 | ILC_MASK : ILC_COLOR8 | ILC_MASK, 2, 0);
		m_himlTab = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 1);
		ImageList_AddIcon(m_himlTab, GetIcon(ICO_TARGET));
		ImageList_AddIcon(m_himlTab, GetIcon(ICO_MONITOR));
		ImageList_AddIcon(m_himlTab, GetIconBtn(ICO_BTN_FOLDER));
	}

	/// create the tab control.
	{
		m_hwndTab = GetDlgItem(m_hWnd, IDC_CAPTURETAB);
		TabCtrl_SetImageList(m_hwndTab, m_himlTab);
		TabCtrl_SetItemExtra(m_hwndTab, sizeof(TAB_INFO) - sizeof(TCITEMHEADER));
		RECT rcTab;
		TAB_INFO itab;
		itab.hwndMain = m_hWnd;
		itab.hwndTab = m_hwndTab;
		itab.tcih.mask = TCIF_PARAM | TCIF_TEXT | TCIF_IMAGE;

		/// Add a tab for each of the three child dialog boxes.
		itab.tcih.pszText = TranslateT("Window");
		itab.tcih.iImage = 0;
		itab.hwndTabPage = CreateDialogParam(g_hSendSS, MAKEINTRESOURCE(IDD_UMain_CaptureWindow), m_hWnd, DlgProc_CaptureTabPage, IDD_UMain_CaptureWindow);
		TabCtrl_InsertItem(m_hwndTab, 0, &itab);
		/// get tab boundaries (required after 1st tab)
		GetClientRect(m_hwndTab, &rcTab);
		MapWindowPoints(m_hwndTab, m_hWnd, (POINT*)&rcTab, 2);
		TabCtrl_AdjustRect(m_hwndTab, 0, &rcTab);
		rcTab.bottom -= rcTab.top; rcTab.right -= rcTab.left;
		///
		SetWindowPos(itab.hwndTabPage, HWND_TOP, rcTab.left, rcTab.top, rcTab.right, rcTab.bottom, 0);
		CheckDlgButton(itab.hwndTabPage, ID_chkIndirectCapture, m_opt_chkIndirectCapture ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(itab.hwndTabPage, ID_chkClientArea, m_opt_chkClientArea ? BST_CHECKED : BST_UNCHECKED);

		itab.tcih.pszText = TranslateT("Desktop");
		itab.tcih.iImage = 1;
		itab.hwndTabPage = CreateDialogParam(g_hSendSS, MAKEINTRESOURCE(IDD_UMain_CaptureDesktop), m_hWnd, DlgProc_CaptureTabPage, IDD_UMain_CaptureDesktop);
		TabCtrl_InsertItem(m_hwndTab, 1, &itab);
		SetWindowPos(itab.hwndTabPage, HWND_TOP, rcTab.left, rcTab.top, rcTab.right, rcTab.bottom, 0);

		hCtrl = GetDlgItem(itab.hwndTabPage, ID_edtCaption);
		ComboBox_ResetContent(hCtrl);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("<Entire Desktop>")), 0);
		ComboBox_SetCurSel(hCtrl, 0);
		if (m_MonitorCount > 1) {
			wchar_t tszTemp[120];
			for (size_t mon = 0; mon < m_MonitorCount; ++mon) { /// @todo : fix format for non MSVC compilers
				mir_snwprintf(tszTemp, L"%Iu. %s%s",
					mon + 1, TranslateT("Monitor"),
					(m_Monitors[mon].dwFlags & MONITORINFOF_PRIMARY) ? TranslateT(" (primary)") : L""
					);
				ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, tszTemp), mon + 1);
			}
			ComboBox_SelectItemData(hCtrl, m_opt_cboxDesktop);	//use Workaround for MS bug ComboBox_SelectItemData
		}
		PostMessage(m_hWnd, WM_COMMAND, MAKEWPARAM(ID_edtCaption, CBN_SELCHANGE), (LPARAM)hCtrl);

		itab.tcih.pszText = TranslateT("File");
		itab.tcih.iImage = 2;
		itab.hwndTabPage = CreateDialogParam(g_hSendSS, MAKEINTRESOURCE(IDD_UMain_CaptureFile), m_hWnd, DlgProc_CaptureTabPage, IDD_UMain_CaptureFile);
		TabCtrl_InsertItem(m_hwndTab, 2, &itab);
		SetWindowPos(itab.hwndTabPage, HWND_TOP, rcTab.left, rcTab.top, rcTab.right, rcTab.bottom, 0);

		/// select tab and set m_hwndTabPage
		TabCtrl_SetCurSel(m_hwndTab, m_opt_tabCapture);
		itab.tcih.mask = TCIF_PARAM;
		TabCtrl_GetItem(m_hwndTab, m_opt_tabCapture, &itab);
		m_hwndTabPage = itab.hwndTabPage;
		ShowWindow(m_hwndTabPage, SW_SHOW);

		/// enable Drag&Drop for local file pane
		typedef BOOL(WINAPI *ChangeWindowMessageFilterEx_t)(HWND hwnd, UINT message, DWORD action, PCHANGEFILTERSTRUCT pChangeFilterStruct);
		ChangeWindowMessageFilterEx_t pChangeWindowMessageFilterEx;
		pChangeWindowMessageFilterEx = (ChangeWindowMessageFilterEx_t)GetProcAddress(GetModuleHandleA("user32"), "ChangeWindowMessageFilterEx");
		if (pChangeWindowMessageFilterEx) { /// Win7+, UAC fix
			pChangeWindowMessageFilterEx(m_hWnd, WM_DROPFILES, MSGFLT_ALLOW, nullptr);
			pChangeWindowMessageFilterEx(m_hWnd, WM_COPYDATA, MSGFLT_ALLOW, nullptr);
			pChangeWindowMessageFilterEx(m_hWnd, 0x0049/*WM_COPYGLOBALDATA*/, MSGFLT_ALLOW, nullptr);
		}
		DragAcceptFiles(m_hWnd, 1);
	}
	/// init Format combo box
	{
		hCtrl = GetDlgItem(m_hWnd, ID_cboxFormat);
		ComboBox_ResetContent(hCtrl);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"PNG"), 0);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"JPG"), 1);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"BMP"), 2);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"TIF"), 3);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"GIF"), 4);
		ComboBox_SelectItemData(hCtrl, m_opt_cboxFormat);	//use Workaround for MS bug ComboBox_SelectItemData
	}
	/// init SendBy combo box
	{
		hCtrl = GetDlgItem(m_hWnd, ID_cboxSendBy);
		ComboBox_ResetContent(hCtrl);
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("<Only save>")), new UPLOAD_INFO(SS_JUSTSAVE));
		if (m_hContact) {
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("File Transfer")), new UPLOAD_INFO(SS_FILESEND));
			ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("E-mail")), new UPLOAD_INFO(SS_EMAIL));
			if (g_myGlobals.PluginHTTPExist) {
				ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"HTTP Server"), new UPLOAD_INFO(SS_HTTPSERVER));
			}
			else if (m_opt_cboxSendBy == SS_HTTPSERVER) {
				m_opt_cboxSendBy = SS_IMAGESHACK;
			}
			if (g_myGlobals.PluginFTPExist) {
				ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("FTP File")), new UPLOAD_INFO(SS_FTPFILE));
			}
			else if (m_opt_cboxSendBy == SS_FTPFILE) {
				m_opt_cboxSendBy = SS_IMAGESHACK;
			}
		}
		else if (m_opt_cboxSendBy == SS_FILESEND || m_opt_cboxSendBy == SS_EMAIL || m_opt_cboxSendBy == SS_HTTPSERVER || m_opt_cboxSendBy == SS_FTPFILE) {
			m_opt_cboxSendBy = SS_IMAGESHACK;
		}
		if (g_myGlobals.PluginCloudFileExist) {
			CallService(MS_CLOUDFILE_ENUMSERVICES, (WPARAM)EnumCloudFileServices, (LPARAM)hCtrl);
		}
		else if (m_opt_cboxSendBy == SS_CLOUDFILE) {
			m_opt_cboxSendBy = SS_IMAGESHACK;
		}
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"ImageShack"), new UPLOAD_INFO(SS_IMAGESHACK));
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("Upload Pie (30m)")), new UPLOAD_INFO(SS_UPLOADPIE, (void*)1));
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("Upload Pie (1d)")), new UPLOAD_INFO(SS_UPLOADPIE, (void*)4));
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, TranslateT("Upload Pie (1w)")), new UPLOAD_INFO(SS_UPLOADPIE, (void*)5));
		ComboBox_SetItemData(hCtrl, ComboBox_AddString(hCtrl, L"imgur"), new UPLOAD_INFO(SS_IMGUR));
		ComboBox_SelectItemData(hCtrl, m_opt_cboxSendBy);	//use Workaround for MS bug ComboBox_SelectItemData
	}
	/// init footer options
	CheckDlgButton(m_hWnd, ID_chkOpenAgain, m_opt_chkOpenAgain ? BST_CHECKED : BST_UNCHECKED);

	if (hCtrl = GetDlgItem(m_hWnd, ID_btnAbout)) {
		SendDlgItemMessage(m_hWnd, ID_btnAbout, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Information"), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(ICO_BTN_HELP);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, hIcon ? L"" : L"?");
	}

	if (hCtrl = GetDlgItem(m_hWnd, ID_btnExplore)) {
		SendDlgItemMessage(m_hWnd, ID_btnExplore, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Open Folder"), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(ICO_BTN_FOLDER);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, hIcon ? L"" : L"...");
	}

	if (hCtrl = GetDlgItem(m_hWnd, ID_chkDesc)) {
		SendDlgItemMessage(m_hWnd, ID_chkDesc, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Fill description textbox."), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(m_opt_btnDesc ? ICO_BTN_DESCON : ICO_BTN_DESC);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, hIcon ? L"" : L"D");
		SendMessage(hCtrl, BM_SETCHECK, m_opt_btnDesc ? BST_CHECKED : BST_UNCHECKED, NULL);
	}

	if (hCtrl = GetDlgItem(m_hWnd, ID_chkDeleteAfterSend)) {
		SendDlgItemMessage(m_hWnd, ID_chkDeleteAfterSend, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Delete after send"), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(m_opt_btnDeleteAfterSend ? ICO_BTN_DELON : ICO_BTN_DEL);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, hIcon ? L"" : L"X");
		SendMessage(hCtrl, BM_SETCHECK, m_opt_btnDeleteAfterSend ? BST_CHECKED : BST_UNCHECKED, NULL);
	}

	if (hCtrl = GetDlgItem(m_hWnd, ID_chkEditor)) {
		SendDlgItemMessage(m_hWnd, ID_chkEditor, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Open editor before sending"), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(m_opt_chkEditor ? ICO_BTN_EDITON : ICO_BTN_EDIT);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, hIcon ? L"" : L"E");
		SendMessage(hCtrl, BM_SETCHECK, m_opt_chkEditor ? BST_CHECKED : BST_UNCHECKED, NULL);
	}

	if (hCtrl = GetDlgItem(m_hWnd, ID_btnCapture)) {
		SendMessage(hCtrl, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Capture"), MBBF_TCHAR);
		HICON hIcon = GetIconBtn(ICO_BTN_OK);
		SendMessage(hCtrl, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		SetWindowText(hCtrl, TranslateT("&Capture"));
		SendMessage(hCtrl, BUTTONSETDEFAULT, 1, NULL);
	}
	cboxSendByChange(nullptr);		//enable disable controls

	TranslateDialogDefault(m_hWnd);
}

/////////////////////////////////////////////////////////////////////////////////////////
// WM_COMMAND:

void TfrmMain::wmCommand(WPARAM wParam, LPARAM lParam)
{
	int IDControl = LOWORD(wParam);
	switch (HIWORD(wParam)) {
	case BN_CLICKED:		//Button controls
		switch (IDControl) {
		case IDCANCEL: // ESC pressed
			this->Close();
			break;
		case ID_chkTimed:
			m_opt_chkTimed = (BYTE)Button_GetCheck((HWND)lParam);
			TfrmMain::chkTimedClick();
			break;
		case ID_chkIndirectCapture:
			m_opt_chkIndirectCapture = (BYTE)Button_GetCheck((HWND)lParam);
			break;
		case ID_chkClientArea:
			m_opt_chkClientArea = (BYTE)Button_GetCheck((HWND)lParam);
			if (m_hTargetWindow)
				edtSizeUpdate(m_hTargetWindow, m_opt_chkClientArea, GetParent((HWND)lParam), ID_edtSize);
			break;
		case ID_imgTarget:
			if (m_opt_tabCapture != 0) break;
			m_hLastWin = nullptr;
			SetTimer(m_hWnd, ID_imgTarget, BUTTON_POLLDELAY, nullptr);
			break;
		case ID_btnAbout:
			TfrmMain::btnAboutClick();
			break;
		case ID_btnExplore:
			TfrmMain::btnExploreClick();
			break;
		case ID_chkDesc:{
				m_opt_btnDesc = !m_opt_btnDesc;
				HICON hIcon = GetIconBtn(m_opt_btnDesc ? ICO_BTN_DESCON : ICO_BTN_DESC);
				SendMessage((HWND)lParam, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
				break; }
		case ID_chkDeleteAfterSend:{
				m_opt_btnDeleteAfterSend = !m_opt_btnDeleteAfterSend;
				HICON hIcon = GetIconBtn(m_opt_btnDeleteAfterSend ? ICO_BTN_DELON : ICO_BTN_DEL);
				SendMessage((HWND)lParam, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
				if (m_cSend) m_cSend->m_bDeleteAfterSend = m_opt_btnDeleteAfterSend;
				break; }
		case ID_chkEditor:{
				m_opt_chkEditor = !m_opt_chkEditor;
				HICON hIcon = GetIconBtn(m_opt_chkEditor ? ICO_BTN_EDITON : ICO_BTN_EDIT);
				SendMessage((HWND)lParam, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
				break; }
		case ID_chkOpenAgain:
			m_opt_chkOpenAgain = Button_GetCheck((HWND)lParam);
			break;
		case ID_btnCapture:
			TfrmMain::btnCaptureClick();
			break;
		default:
			break;
		}
		break;
	case CBN_SELCHANGE:		//ComboBox controls
		switch (IDControl) {
			//lParam = Handle to the control
		case ID_cboxFormat:			//not finish
			m_opt_cboxFormat = (BYTE)ComboBox_GetItemData((HWND)lParam, ComboBox_GetCurSel((HWND)lParam));
			break;
		case ID_cboxSendBy:
		{
			UPLOAD_INFO *upload = (UPLOAD_INFO*)ComboBox_GetItemData((HWND)lParam, ComboBox_GetCurSel((HWND)lParam));
			m_opt_cboxSendBy = upload->sendBy;
			cboxSendByChange(upload->param);
			break;
		}
		case ID_edtCaption:			//cboxDesktopChange
			m_opt_cboxDesktop = (BYTE)ComboBox_GetItemData((HWND)lParam, ComboBox_GetCurSel((HWND)lParam));
			m_hTargetWindow = nullptr;
			if (m_opt_cboxDesktop > 0) {
				edtSizeUpdate(m_Monitors[m_opt_cboxDesktop - 1].rcMonitor, GetParent((HWND)lParam), ID_edtSize);
			}
			else {
				edtSizeUpdate(m_VirtualScreen, GetParent((HWND)lParam), ID_edtSize);
			}
			break;
		default:
			break;
		}
		break;
	case EN_CHANGE:			//Edit controls
		switch (IDControl) {
			//lParam = Handle to the control
		case ID_edtQuality:
			m_opt_edtQuality = (BYTE)GetDlgItemInt(m_hWnd, ID_edtQuality, nullptr, FALSE);
			break;
		case ID_edtTimed:
			m_opt_edtTimed = (BYTE)GetDlgItemInt(m_hWnd, ID_edtTimed, nullptr, FALSE);
			break;
		default:
			break;
		}
		break;
	default:
		break;
	}
}

//WM_CLOSE:
void TfrmMain::wmClose(WPARAM, LPARAM)
{
	HWND hCtrl = GetDlgItem(m_hWnd, ID_cboxSendBy);
	size_t count = ComboBox_GetCount(hCtrl);
	for (size_t i = 0; i < count; i++) {
		UPLOAD_INFO *ui = (UPLOAD_INFO*)ComboBox_GetItemData(hCtrl, i);
		delete ui;
	}
	DestroyWindow(m_hWnd);
	return;
}

//WM_TIMER:
const int g_iTargetBorder = 7;
void TfrmMain::SetTargetWindow(HWND hwnd)
{
	if (!hwnd) {
		POINT point; GetCursorPos(&point);
		hwnd = WindowFromPoint(point);
		//		if(!((GetAsyncKeyState(VK_SHIFT)|GetAsyncKeyState(VK_MENU))&0x8000))
		for (HWND hTMP; (hTMP = GetParent(hwnd)); hwnd = hTMP);
	}
	m_hTargetWindow = hwnd;
	int len = GetWindowTextLength(m_hTargetWindow) + 1;
	wchar_t* lpTitle;
	if (len > 1) {
		lpTitle = (wchar_t*)mir_alloc(len*sizeof(wchar_t));
		GetWindowText(m_hTargetWindow, lpTitle, len);
	}
	else {//no WindowText present, use WindowClass
		lpTitle = (wchar_t*)mir_alloc(64 * sizeof(wchar_t));
		RealGetWindowClass(m_hTargetWindow, lpTitle, 64);
	}
	SetDlgItemText(m_hwndTabPage, ID_edtCaption, lpTitle);
	mir_free(lpTitle);
	edtSizeUpdate(m_hTargetWindow, m_opt_chkClientArea, m_hwndTabPage, ID_edtSize);
}

void TfrmMain::wmTimer(WPARAM wParam, LPARAM)
{
	if (wParam == ID_imgTarget) {// Timer for Target selector
		static int primarymouse;
		if (!m_hTargetHighlighter) {
			primarymouse = GetSystemMetrics(SM_SWAPBUTTON) ? VK_RBUTTON : VK_LBUTTON;
			m_hTargetHighlighter = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW, (wchar_t*)g_clsTargetHighlighter, nullptr, WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, g_hSendSS, nullptr);
			if (!m_hTargetHighlighter) return;
			SetLayeredWindowAttributes(m_hTargetHighlighter, 0, 123, LWA_ALPHA);
			SetSystemCursor(CopyCursor(GetIcon(ICO_TARGET)), OCR_IBEAM);//text cursor
			SetSystemCursor(CopyCursor(GetIcon(ICO_TARGET)), OCR_NORMAL);
			SetActiveWindow(m_hTargetHighlighter); // activate highlighter to fix focus problems with UAC (unelevated GetAsyncKeyState() fails if an elevated app got focus)
			Hide();
		}
		if (!(GetAsyncKeyState(primarymouse) & 0x8000)) {
			KillTimer(m_hWnd, ID_imgTarget);
			SystemParametersInfo(SPI_SETCURSORS, 0, nullptr, 0);
			DestroyWindow(m_hTargetHighlighter), m_hTargetHighlighter = nullptr;
			SetTargetWindow(m_hLastWin);
			Show();
			return;
		}
		POINT point; GetCursorPos(&point);
		HWND hwnd = WindowFromPoint(point);
		if (!((GetAsyncKeyState(VK_SHIFT) | GetAsyncKeyState(VK_MENU)) & 0x8000))
			for (HWND hTMP; (hTMP = GetAncestor(hwnd, GA_PARENT)) && IsChild(hTMP, hwnd); hwnd = hTMP);
		else {
			ScreenToClient(hwnd, &point);
			HWND hTMP; if ((hTMP = RealChildWindowFromPoint(hwnd, point)))
				hwnd = hTMP;
		}
		if (hwnd != m_hLastWin) {
			m_hLastWin = hwnd;
			RECT rect;
			if (m_opt_chkClientArea) {
				GetClientRect(hwnd, &rect);
				ClientToScreen(hwnd, (POINT*)&rect);
				rect.right = rect.left + rect.right;
				rect.bottom = rect.top + rect.bottom;
			}
			else
				GetWindowRect(hwnd, &rect);
			int width = rect.right - rect.left;
			int height = rect.bottom - rect.top;
			if (g_iTargetBorder) {
				SetWindowPos(m_hTargetHighlighter, nullptr, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOMOVE | SWP_NOSIZE);
				if (width > g_iTargetBorder * 2 && height > g_iTargetBorder * 2) {
					HRGN hRegnNew = CreateRectRgn(0, 0, width, height);
					HRGN hRgnHole = CreateRectRgn(g_iTargetBorder, g_iTargetBorder, width - g_iTargetBorder, height - g_iTargetBorder);
					CombineRgn(hRegnNew, hRegnNew, hRgnHole, RGN_XOR);
					DeleteObject(hRgnHole);
					SetWindowRgn(m_hTargetHighlighter, hRegnNew, FALSE);//cleans up hRegnNew
				}
				else SetWindowRgn(m_hTargetHighlighter, nullptr, FALSE);
			}
			SetWindowPos(m_hTargetHighlighter, HWND_TOPMOST, rect.left, rect.top, width, height, SWP_SHOWWINDOW | SWP_NOACTIVATE);
		}
		return;
	}
	if (wParam == ID_chkTimed) {// Timer for Screenshot
#ifdef _DEBUG
		OutputDebugStringA("SS Bitmap Timer Start\r\n");
#endif
		if (!m_bCapture) {		//only start once
			if (m_Screenshot) {
				FreeImage_Unload(m_Screenshot);
				m_Screenshot = nullptr;
			}
			m_bCapture = true;
			switch (m_opt_tabCapture) {
			case 0:
				m_Screenshot = CaptureWindow(m_hTargetWindow, m_opt_chkClientArea, m_opt_chkIndirectCapture);
				break;
			case 1:
				m_Screenshot = CaptureMonitor((m_opt_cboxDesktop > 0) ? m_Monitors[m_opt_cboxDesktop - 1].szDevice : nullptr);
				break;
			case 2: /// edge case, existing local file
				break;
#ifdef _DEBUG
			default:
				OutputDebugStringA("SS Bitmap Timer Stop (no tabCapture)\r\n");
#endif
			}
			m_bCapture = false;
			if (m_Screenshot || m_opt_tabCapture == 2) { /// @note : test without "if"
				KillTimer(m_hWnd, ID_chkTimed);
#ifdef _DEBUG
				OutputDebugStringA("SS Bitmap Timer Stop (CaptureDone)\r\n");
#endif
				SendMessage(m_hWnd, UM_EVENT, 0, (LPARAM)EVT_CaptureDone);
			}
		}
	}
}

//WM_NOTIFY:
void TfrmMain::wmNotify(WPARAM, LPARAM lParam)
{
	switch (((LPNMHDR)lParam)->idFrom) {
	case IDC_CAPTURETAB:				//TabControl IDC_CAPTURETAB
		switch (((LPNMHDR)lParam)->code) {
			//    HWND hwndFrom;	= member is handle to the tab control
			//    UINT_PTR idFrom;	= member is the child window identifier of the tab control.
			//    UINT code;		= member is TCN_SELCHANGE
		case TCN_SELCHANGING:{
				if (!m_hwndTabPage) break;
				ShowWindow(m_hwndTabPage, SW_HIDE);
				m_hwndTabPage = nullptr;
				break; }
		case TCN_SELCHANGE:{
				TAB_INFO itab = { TCIF_PARAM };
				m_opt_tabCapture = TabCtrl_GetCurSel(m_hwndTab);
				TabCtrl_GetItem(m_hwndTab, m_opt_tabCapture, &itab);
				m_hwndTabPage = itab.hwndTabPage;
				ShowWindow(m_hwndTabPage, SW_SHOW);
				break; }
		default:
			break;
		}
		break;
	default:
		break;
	}
}

//UM_CLOSING:
void TfrmMain::UMClosing(WPARAM wParam, LPARAM lParam)
{
	HWND hWnd = (HWND)wParam;
	switch (lParam) {
	case IDD_UAboutForm:
		btnAboutOnCloseWindow(hWnd);
		break;
	case IDD_UEditForm:
		;
		break;
	default:
		break;
	}
}

//UM_EVENT:
void TfrmMain::UMevent(WPARAM, LPARAM lParam)
{
	//HWND hWnd = (HWND)wParam;
	switch (lParam) {
	case EVT_CaptureDone:
		if (!m_Screenshot && m_opt_tabCapture != 2) {
			wchar_t *err = TranslateT("Couldn't take a screenshot");
			MessageBox(nullptr, err, ERROR_TITLE, MB_OK | MB_ICONWARNING);
			Show();
			return;
		}
		FormClose();
		break;
	case EVT_SendFileDone:
		break;
	case EVT_CheckOpenAgain:
		if (m_opt_chkOpenAgain) {
			if (m_Screenshot) {
				FreeImage_Unload(m_Screenshot);
				m_Screenshot = nullptr;
			}
			Show();
		}
		else {// Saving Options and close
			SaveOptions();
			Close();
		}
		break;
	default:
		break;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Standard konstruktor/destruktor

TfrmMain::TfrmMain()
{
	/* m_opt_XXX */
	m_bOnExitSave = TRUE;

	m_hWnd = nullptr;
	m_hContact = NULL;
	m_bFormAbout = false;
	m_hTargetWindow = m_hLastWin = nullptr;
	m_hTargetHighlighter = nullptr;
	m_FDestFolder = m_pszFile = nullptr;
	m_Screenshot = nullptr;
	/* m_AlphaColor */
	m_cSend = nullptr;

	m_Monitors = nullptr;
	m_MonitorCount = MonitorInfoEnum(m_Monitors, m_VirtualScreen);
	/* m_opt_XXX */ LoadOptions();
	m_bCapture = false;
	/* m_hwndTab,m_hwndTabPage */
	m_himlTab = nullptr;
}

TfrmMain::~TfrmMain()
{
	_HandleMapping.erase(m_hWnd);
	mir_free(m_pszFile);
	mir_free(m_FDestFolder);
	mir_free(m_Monitors);
	if (m_Screenshot) FreeImage_Unload(m_Screenshot);
	if (m_cSend) delete m_cSend;
	if (m_hTargetHighlighter) {
		DestroyWindow(m_hTargetHighlighter), m_hTargetHighlighter = nullptr;
		SystemParametersInfo(SPI_SETCURSORS, 0, nullptr, 0);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Load / Saving options from miranda's database

void TfrmMain::LoadOptions(void)
{
	DWORD rgb = db_get_dw(NULL, SZ_SENDSS, "AlphaColor", 16777215);
	m_AlphaColor.rgbRed = GetRValue(rgb);
	m_AlphaColor.rgbGreen = GetGValue(rgb);
	m_AlphaColor.rgbBlue = GetBValue(rgb);
	m_AlphaColor.rgbReserved = 0;

	//	m_opt_chkEmulateClick		= db_get_b(NULL, SZ_SENDSS, "AutoSend", 1);
	m_opt_edtQuality = db_get_b(NULL, SZ_SENDSS, "JpegQuality", 75);

	m_opt_tabCapture = db_get_b(NULL, SZ_SENDSS, "Capture", 0);
	m_opt_chkIndirectCapture = db_get_b(NULL, SZ_SENDSS, "IndirectCapture", 0);
	m_opt_chkClientArea = db_get_b(NULL, SZ_SENDSS, "ClientArea", 0);
	m_opt_cboxDesktop = db_get_b(NULL, SZ_SENDSS, "Desktop", 0);

	m_opt_chkTimed = db_get_b(NULL, SZ_SENDSS, "TimedCap", 0);
	m_opt_edtTimed = db_get_b(NULL, SZ_SENDSS, "CapTime", 3);
	m_opt_cboxFormat = db_get_b(NULL, SZ_SENDSS, "OutputFormat", 0);
	m_opt_cboxSendBy = db_get_b(NULL, SZ_SENDSS, "SendBy", 0);

	m_opt_btnDesc = db_get_b(NULL, SZ_SENDSS, "AutoDescription", 1);
	m_opt_btnDeleteAfterSend = db_get_b(NULL, SZ_SENDSS, "DelAfterSend", 1) != 0;
	m_opt_chkEditor = db_get_b(NULL, SZ_SENDSS, "Preview", 0);
	m_opt_chkOpenAgain = db_get_b(NULL, SZ_SENDSS, "OpenAgain", 0);
}

void TfrmMain::SaveOptions(void)
{
	if (m_bOnExitSave) {
		db_set_dw(NULL, SZ_SENDSS, "AlphaColor",
			(DWORD)RGB(m_AlphaColor.rgbRed, m_AlphaColor.rgbGreen, m_AlphaColor.rgbBlue));

		//		db_set_b(NULL, SZ_SENDSS, "AutoSend", m_opt_chkEmulateClick);
		db_set_b(NULL, SZ_SENDSS, "JpegQuality", m_opt_edtQuality);

		db_set_b(NULL, SZ_SENDSS, "Capture", m_opt_tabCapture);
		db_set_b(NULL, SZ_SENDSS, "IndirectCapture", m_opt_chkIndirectCapture);
		db_set_b(NULL, SZ_SENDSS, "ClientArea", m_opt_chkClientArea);
		db_set_b(NULL, SZ_SENDSS, "Desktop", m_opt_cboxDesktop);

		db_set_b(NULL, SZ_SENDSS, "TimedCap", m_opt_chkTimed);
		db_set_b(NULL, SZ_SENDSS, "CapTime", m_opt_edtTimed);
		db_set_b(NULL, SZ_SENDSS, "OutputFormat", m_opt_cboxFormat);
		db_set_b(NULL, SZ_SENDSS, "SendBy", m_opt_cboxSendBy);

		db_set_b(NULL, SZ_SENDSS, "AutoDescription", m_opt_btnDesc);
		db_set_b(NULL, SZ_SENDSS, "DelAfterSend", m_opt_btnDeleteAfterSend);
		db_set_b(NULL, SZ_SENDSS, "Preview", m_opt_chkEditor);
		db_set_b(NULL, SZ_SENDSS, "OpenAgain", m_opt_chkOpenAgain);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::Init(wchar_t* DestFolder, MCONTACT Contact)
{
	m_FDestFolder = mir_wstrdup(DestFolder);
	m_hContact = Contact;

	// create window
	m_hWnd = CreateDialogParam(g_hSendSS, MAKEINTRESOURCE(IDD_UMainForm), nullptr, DlgTfrmMain, (LPARAM)this);
	//register object
	_HandleMapping.insert(CHandleMapping::value_type(m_hWnd, this));

	//check Contact
	if (m_cSend)
		m_cSend->SetContact(Contact);
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::btnCaptureClick()
{
	if (m_opt_tabCapture == 1) m_hTargetWindow = GetDesktopWindow();
	else if (m_opt_tabCapture == 2) {
		wchar_t filename[MAX_PATH];
		GetDlgItemText(m_hwndTabPage, ID_edtSize, filename, _countof(filename));
		FILE* fp = _wfopen(filename, L"rb");
		if (!fp) {
			wchar_t *err = TranslateT("Select a file");
			MessageBox(m_hWnd, err, ERROR_TITLE, MB_OK | MB_ICONWARNING);
			return;
		}
		fclose(fp);
		mir_free(m_pszFile); m_pszFile = mir_wstrdup(filename);
	}
	else if (!m_hTargetWindow) {
		wchar_t *err = TranslateT("Select a target window.");
		MessageBox(m_hWnd, err, ERROR_TITLE, MB_OK | MB_ICONWARNING);
		return;
	}
	TfrmMain::Hide();

	if (m_opt_chkTimed) {
		SetTimer(m_hWnd, ID_chkTimed, m_opt_edtTimed ? m_opt_edtTimed * 1000 : 500, nullptr); /// calls EVT_CaptureDone
		return;
	}
	if (m_opt_tabCapture == 1) { /// desktop needs always time to update from TfrmMain::Hide()
		SetTimer(m_hWnd, ID_chkTimed, 500, nullptr); /// calls EVT_CaptureDone
		return;
	}
	if (m_opt_tabCapture != 2) {
		m_Screenshot = CaptureWindow(m_hTargetWindow, m_opt_chkClientArea, m_opt_chkIndirectCapture);
	}
	SendMessage(m_hWnd, UM_EVENT, 0, (LPARAM)EVT_CaptureDone);
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::chkTimedClick()
{
	Button_Enable(GetDlgItem(m_hWnd, ID_edtTimedLabel), (BOOL)m_opt_chkTimed);
	Button_Enable(GetDlgItem(m_hWnd, ID_edtTimed), (BOOL)m_opt_chkTimed);
	Button_Enable(GetDlgItem(m_hWnd, ID_upTimed), (BOOL)m_opt_chkTimed);
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::cboxSendByChange(void *param)
{
	BOOL bState;
	HICON hIcon;
	BYTE itemFlag = SS_DLG_DESCRIPTION;		//SS_DLG_AUTOSEND | SS_DLG_DELETEAFTERSSEND |
	if (m_cSend)
		delete m_cSend;
	switch (m_opt_cboxSendBy) {
	case SS_FILESEND:		//"File Transfer"
		m_cSend = new CSendFile(m_hWnd, m_hContact, true);
		break;
	case SS_EMAIL:			//"E-mail"
		m_cSend = new CSendEmail(m_hWnd, m_hContact, true);
		break;
	case SS_HTTPSERVER:		//"HTTP Server"
		m_cSend = new CSendHTTPServer(m_hWnd, m_hContact, true);
		break;
	case SS_FTPFILE:		//"FTP File"
		m_cSend = new CSendFTPFile(m_hWnd, m_hContact, true);
		break;
	case SS_CLOUDFILE:		//"CloudFile"
		m_cSend = new CSendCloudFile(m_hWnd, m_hContact, false, (char*)param);
		break;
	case SS_IMAGESHACK:		//"ImageShack"
		m_cSend = new CSendHost_ImageShack(m_hWnd, m_hContact, true);
		break;
	case SS_UPLOADPIE:		//"Upload Pie"
		m_cSend = new CSendHost_UploadPie(m_hWnd, m_hContact, true, (int)param);
		break;
	case SS_IMGUR:
		m_cSend = new CSendHost_Imgur(m_hWnd, m_hContact, true);
		break;
	default:				//SS_JUSTSAVE - "Just save it "
		m_cSend = nullptr;
		break;
	}
	if (m_cSend) {
		itemFlag = m_cSend->GetEnableItem();
		m_cSend->m_bDeleteAfterSend = m_opt_btnDeleteAfterSend;
	}
	bState = (itemFlag & SS_DLG_DELETEAFTERSSEND);
	hIcon = GetIconBtn(m_opt_btnDeleteAfterSend ? ICO_BTN_DELON : ICO_BTN_DEL);
	SendDlgItemMessage(m_hWnd, ID_chkDeleteAfterSend, BM_SETIMAGE, IMAGE_ICON, (LPARAM)(bState ? hIcon : nullptr));
	Button_Enable(GetDlgItem(m_hWnd, ID_chkDeleteAfterSend), bState);

	bState = (itemFlag & SS_DLG_DESCRIPTION);
	hIcon = GetIconBtn(m_opt_btnDesc ? ICO_BTN_DESCON : ICO_BTN_DESC);
	SendDlgItemMessage(m_hWnd, ID_chkDesc, BM_SETIMAGE, IMAGE_ICON, (LPARAM)(bState ? hIcon : nullptr));
	Button_Enable(GetDlgItem(m_hWnd, ID_chkDesc), bState);
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::btnAboutClick()
{
	if (m_bFormAbout) return;

	TfrmAbout *frmAbout = new TfrmAbout(m_hWnd);
	frmAbout->Show();
	m_bFormAbout = true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Edit window call this event before it closes

void TfrmMain::btnAboutOnCloseWindow(HWND)
{
	m_bFormAbout = false;
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::btnExploreClick()
{
	if (m_FDestFolder)
		ShellExecute(nullptr, L"explore", m_FDestFolder, nullptr, nullptr, SW_SHOW);
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::edtSizeUpdate(HWND hWnd, BOOL ClientArea, HWND hTarget, UINT Ctrl)
{
	// get window dimensions
	RECT rect = { 0 };
	RECT cliRect = { 0 };
	wchar_t B[33], H[16];
	GetWindowRect(hWnd, &rect);
	if (ClientArea) {
		POINT pt = { 0 };
		GetClientRect(hWnd, &cliRect);
		pt.x = cliRect.left;
		pt.y = cliRect.top;
		ClientToScreen(hWnd, &pt);
		pt.x = pt.x - rect.left;			//offset x for client area
		pt.y = pt.y - rect.top;				//offset y for client area
		rect = cliRect;
	}
	//	_itow_s(rect.right - rect.left, B, 33, 10);
	_itow(rect.right - rect.left, B, 10);
	//	_itow_s(rect.bottom - rect.top, H, 16, 10);
	_itow(rect.bottom - rect.top, H, 10);
	mir_wstrncat(B, L"x", _countof(B) - mir_wstrlen(B));
	mir_wstrncat(B, H, _countof(B) - mir_wstrlen(B));
	SetDlgItemText(hTarget, Ctrl, B);
}

void TfrmMain::edtSizeUpdate(RECT rect, HWND hTarget, UINT Ctrl)
{
	wchar_t B[33], H[16];
	//	_itow_s(ABS(rect.right - rect.left), B, 33, 10);
	_itow(ABS(rect.right - rect.left), B, 10);
	//	_itow_s(ABS(rect.bottom - rect.top), H, 16, 10);
	_itow(ABS(rect.bottom - rect.top), H, 10);
	mir_wstrncat(B, L"x", _countof(B) - mir_wstrlen(B));
	mir_wstrncat(B, H, _countof(B) - mir_wstrlen(B));
	SetDlgItemText(hTarget, Ctrl, B);
}

/////////////////////////////////////////////////////////////////////////////////////////

INT_PTR TfrmMain::SaveScreenshot(FIBITMAP *dib)
{
	if (!dib)
		return 1;		//error

	// generate file name
	unsigned FileNumber = db_get_dw(NULL, SZ_SENDSS, "FileNumber", 0) + 1;
	if (FileNumber > 99999)
		FileNumber = 1;
	
	CMStringW wszFileName(m_FDestFolder);
	if (wszFileName.Right(1) != L"\\")
		wszFileName.Append(L"\\");
	wszFileName.AppendFormat(L"shot%.5u", FileNumber);

	// generate a description according to the screenshot
	wchar_t winText[1024];
	GetDlgItemText(m_hwndTabPage, ID_edtCaption, winText, _countof(winText));

	CMStringW wszFileDesc;
	if (m_opt_tabCapture)
		wszFileDesc.Format(TranslateT("Screenshot of \"%s\""), winText);
	else {
		if (m_opt_chkClientArea)
			wszFileDesc.Format(TranslateT("Screenshot for client area of \"%s\" window"), winText);
		else 
			wszFileDesc.Format(TranslateT("Screenshot of \"%s\" window"), winText);
	}

	// convert to 32Bits (make sure it is 32bit)
	FIBITMAP *dib_new = FreeImage_ConvertTo32Bits(dib);
	FreeImage_SetTransparent(dib_new, TRUE);

	BOOL ret = FALSE;
	HWND hwndCombo = GetDlgItem(m_hWnd, ID_cboxFormat);
	switch (ComboBox_GetItemData(hwndCombo, ComboBox_GetCurSel(hwndCombo))) {
	case 0: // PNG
		wszFileName.Append(L".png");
		ret = FreeImage_SaveU(FIF_PNG, dib_new, wszFileName, 0);
		break;

	case 1: // JPG
		wszFileName.Append(L".jpg");
		{
			FIBITMAP *dib32 = FreeImage_Composite(dib_new, FALSE, &m_AlphaColor, nullptr);
			FIBITMAP *dib24 = FreeImage_ConvertTo24Bits(dib32);
			FreeImage_Unload(dib32);
			ret = FreeImage_SaveU(FIF_JPEG, dib24, wszFileName, 0);
			FreeImage_Unload(dib24);
		}
		break;

	case 2: // BMP
		wszFileName.Append(L".bmp");
		{
			FIBITMAP *dib32 = FreeImage_Composite(dib_new, FALSE, &m_AlphaColor, nullptr);
			FIBITMAP *dib24 = FreeImage_ConvertTo24Bits(dib32);
			FreeImage_Unload(dib32);
			ret = FreeImage_SaveU(FIF_BMP, dib24, wszFileName, 0);
			FreeImage_Unload(dib24);
		}
		break;

	case 3: //TIFF (miranda freeimage interface do not support save tiff, we udse GDI+)
		wszFileName.Append(L".tif");
		{
			FIBITMAP *dib32 = FreeImage_Composite(dib_new, FALSE, &m_AlphaColor, nullptr);
			FIBITMAP *dib24 = FreeImage_ConvertTo24Bits(dib32);
			FreeImage_Unload(dib32);
		
			HBITMAP hBmp = FreeImage_CreateHBITMAPFromDIB(dib24);
			FreeImage_Unload(dib24);
			SaveTIF(hBmp, wszFileName);
			DeleteObject(hBmp);
		}
		ret = TRUE;
		break;

	case 4: //GIF
		wszFileName.Append(L".gif");
		{
			HBITMAP hBmp = FreeImage_CreateHBITMAPFromDIB(dib_new);
			SaveGIF(hBmp, wszFileName);
			DeleteObject(hBmp);
		}
		ret = TRUE;
		break;
	}

	FreeImage_Unload(dib_new);

	if (!ret)
		return 1; // error
	
	db_set_dw(NULL, SZ_SENDSS, "FileNumber", FileNumber);
	replaceStrW(m_pszFile, wszFileName);

	if (!IsWindowEnabled(GetDlgItem(m_hWnd, ID_chkDesc)) || !m_opt_btnDesc)
		wszFileDesc.Empty();

	if (m_cSend) {
		m_cSend->SetFile(m_pszFile);
		m_cSend->SetDescription(wszFileDesc);
	}
	return 0; // OK
}

/////////////////////////////////////////////////////////////////////////////////////////

void TfrmMain::FormClose()
{
	bool bCanDelete = m_opt_btnDeleteAfterSend;
	if (m_opt_tabCapture == 2) { /// existing file
		wchar_t description[1024];
		GetDlgItemText(m_hwndTabPage, ID_edtCaption, description, _countof(description));
		if (!IsWindowEnabled(GetDlgItem(m_hWnd, ID_chkDesc)) || !m_opt_btnDesc)
			*description = '\0';
		if (m_cSend) {
			m_cSend->m_bDeleteAfterSend = false; /// well... guess it's better to not delete existing files for now...
			m_cSend->SetFile(m_pszFile);
			m_cSend->SetDescription(description);
		}
		bCanDelete = false;
	}
	else if (SaveScreenshot(m_Screenshot)) { /// Saving the screenshot
		Show();		// Error from SaveScreenshot
		return;
	}

	bool send = true;
	if (m_opt_chkEditor) {
		SHELLEXECUTEINFO shex = { sizeof(SHELLEXECUTEINFO) };
		shex.fMask = SEE_MASK_NOCLOSEPROCESS;
		shex.lpVerb = L"edit";
		shex.lpFile = m_pszFile;
		shex.nShow = SW_SHOWNORMAL;
		ShellExecuteEx(&shex);
		if (shex.hProcess) {
			DWORD res;
			MSG msg;
			do {
				// wait for editor exit or messages/input
				res = MsgWaitForMultipleObjects(1, &shex.hProcess, 0, INFINITE, QS_ALLINPUT);
				while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
					if (msg.message == WM_QUIT) {
						res = WAIT_OBJECT_0;
						PostMessage(nullptr, WM_QUIT, 0, 0); // forward for outer message loops
						break;
					}
					// process dialog messages (of unknown dialogs)
					HWND hwndDlgModeless = GetActiveWindow();
					//					HWND hwndDlgModeless = msg.hwnd;
					//					for(HWND hTMP; (hTMP=GetAncestor(hwndDlgModeless,GA_PARENT)) && IsChild(hTMP,hwndDlgModeless); hwndDlgModeless=hTMP);
					if (hwndDlgModeless != nullptr && IsDialogMessage(hwndDlgModeless, &msg)) /* Wine fix. */
						continue;
					// process messages
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
			} while (res == WAIT_OBJECT_0 + 1);
			CloseHandle(shex.hProcess);
		}
		if (MessageBox(m_hWnd, TranslateT("Send screenshot?"), L"SendSS", MB_YESNO | MB_ICONQUESTION | MB_SYSTEMMODAL) != IDYES)
			send = false;
	}

	if (send && m_cSend && m_pszFile) {
		if (!m_cSend->Send()) // not sent now, class deletes itself later
			m_cSend = nullptr; 
		cboxSendByChange(nullptr);
	}
	else if (!send && bCanDelete)
		DeleteFile(m_pszFile);

	SendMessage(m_hWnd, UM_EVENT, 0, (LPARAM)EVT_CheckOpenAgain);
}