summaryrefslogtreecommitdiff
path: root/updater/progress_dialog.cpp
blob: 64e1507668d5fe0b54483f2954f48b2d5314c68d (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
#include "common.h"
#include "progress_dialog.h"

#define ID_PROGTIMER	101

// message loop messages
#define WMU_NEWPROGRESSWINDOW			(WM_USER + 0x200)
#define WMU_KILLPROGRESSWINDOW			(WM_USER + 0x201)

HWND hwndProgress = 0;
//DWORD dwProgressThreadId = 0;
unsigned int dwProgressThreadId = 0;
HANDLE hProgSyncEvent = 0;

INT_PTR CALLBACK DlgProcProgress(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch ( msg ) {
	case WM_INITDIALOG:
		TranslateDialogDefault( hwndDlg );
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 0);
		// these change icons for all system dialogs!
		//SetClassLong(hwndDlg, GCL_HICON, (LONG)hIconCheck);
		//SetClassLong(hwndDlg, GCL_HICONSM, (LONG)hIconCheck);
		SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)LoadIconEx(I_CHKUPD));
		SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIconEx(I_CHKUPD, true));

		SAVEWINDOWPOS swp;
		swp.hwnd=hwndDlg; swp.hContact=0; swp.szModule=MODULE; swp.szNamePrefix="ProgressWindow";
		CallService(MS_UTILS_RESTOREWINDOWPOSITION, RWPF_NOSIZE | RWPF_NOACTIVATE, (LPARAM)&swp);

		PostMessage(hwndDlg, WMU_SETPROGRESS, 0, 0);
		return FALSE;
	case WM_TIMER:
		if(wParam == ID_PROGTIMER) 
		{
			TCHAR text[512];
			GetDlgItemText(hwndDlg, IDC_PROGMSG, text, 512);
			size_t len = _tcslen(text);
			INT_PTR pos = (INT_PTR)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			if(len >= 3 && len < 511) {
				pos = (pos + 1) % 4;
				if(pos == 0)
					text[len - 3] = 0;
				else
					_tcscat(text, _T("."));
				SetDlgItemText(hwndDlg, IDC_PROGMSG, text);
				SetWindowLongPtr(hwndDlg, GWLP_USERDATA, pos);
			}
			return TRUE;
		}
		break;
	case WMU_SETMESSAGE:
		KillTimer(hwndDlg, ID_PROGTIMER);
		SetDlgItemText(hwndDlg, IDC_PROGMSG, (TCHAR *)wParam);
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, 0);
		SetTimer(hwndDlg, ID_PROGTIMER, (WPARAM)500, 0);
		return TRUE;
	case WMU_SETPROGRESS:
		SendDlgItemMessage(hwndDlg, IDC_PROGRESS, PBM_SETPOS, wParam, 0);
		{
			TCHAR buff[512];
			
			mir_sntprintf(buff, SIZEOF(buff), TranslateT("Progress - %d%%"), wParam);
			SetWindowText(hwndDlg, buff);
		}
		return TRUE;

	case WM_COMMAND:
		return TRUE;	// disable esc, enter, etc

	case WM_CLOSE:
		PostThreadMessage(dwProgressThreadId, WMU_KILLPROGRESSWINDOW, 0, 0);
		break;

	case WM_DESTROY:
		KillTimer(hwndDlg, ID_PROGTIMER);
		Utils_SaveWindowPosition(hwndDlg,0,MODULE,"ProgressWindow");
		ReleaseIconEx((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_SMALL, (LPARAM)0));
		ReleaseIconEx((HICON)SendMessage(hwndDlg, WM_SETICON, ICON_BIG,   (LPARAM)0));
		break;
	}
	return FALSE;
}

//DWORD CALLBACK ProgressWindowThread(LPVOID param) {
unsigned int CALLBACK ProgressWindowThread(void *param) {
	if(param) SetEvent((HANDLE)param);

	MSG hwndMsg = {0};
	while(GetMessage(&hwndMsg, 0, 0, 0) > 0 && !Miranda_Terminated()) {
		switch(hwndMsg.message) {
			case WMU_NEWPROGRESSWINDOW:
				if(hwndProgress) DestroyWindow(hwndProgress);
				hwndProgress = CreateDialog(hInst, MAKEINTRESOURCE(IDD_PROGRESS), 0, DlgProcProgress);	
				SetWindowPos(hwndProgress, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
				//ShowWindow(hwndProgress, SW_SHOWNOACTIVATE);
				UpdateWindow(hwndProgress);
				if(param) SetEvent((HANDLE)param);
				break;
			case WMU_KILLPROGRESSWINDOW:
				if(hwndProgress) {
					DestroyWindow(hwndProgress);
					hwndProgress = 0;
				}
				break;
			default:
				if(!IsDialogMessage(hwndProgress, &hwndMsg)) {
					TranslateMessage(&hwndMsg);
					DispatchMessage(&hwndMsg);
				}
				break;
		}
	}

	if(hwndProgress) DestroyWindow(hwndProgress);
	hwndProgress = 0;

	return 0;
}

void MakeProgressWindowThread() {
	hProgSyncEvent = CreateEvent(0, 0, 0, 0);
	CloseHandle(mir_forkthreadex(ProgressWindowThread, hProgSyncEvent, 0, &dwProgressThreadId));
	WaitForSingleObject(hProgSyncEvent, INFINITE);
}


void KillProgressWindowThread() {
	PostThreadMessage(dwProgressThreadId, WM_QUIT, 0, 0);
	CloseHandle(hProgSyncEvent);
}

void CreateProgressWindow() {
	ResetEvent(hProgSyncEvent);
	PostThreadMessage(dwProgressThreadId, WMU_NEWPROGRESSWINDOW, 0, 0);
	WaitForSingleObject(hProgSyncEvent, INFINITE);
}

void ProgressWindowDone() {
	PostThreadMessage(dwProgressThreadId, WMU_KILLPROGRESSWINDOW, 0, 0);
}