-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.h
218 lines (191 loc) · 6.3 KB
/
View.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef __WAKATIME_NPP_PLUGIN_VIEW
#define __WAKATIME_NPP_PLUGIN_VIEW
#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"
#include "INIReader.h"
#include "CommonUtility.h"
#include "logger\src\easylogging++.h"
#pragma comment(linker, \
"\"/manifestdependency:type='Win32' "\
"name='Microsoft.Windows.Common-Controls' "\
"version='6.0.0.0' "\
"processorArchitecture='*' "\
"publicKeyToken='6595b64144ccf1df' "\
"language='*'\"")
#pragma comment(lib, "ComCtl32.lib")
const std::wstring licenseAgreement(
L"Redistribution and use in source and binary forms, with or without"
L"modification, are permitted provided that the following conditions are met :"
L"*Redistributions of source code must retain the above copyright"
L"notice, this list of conditions and the following disclaimer.\r\n"
L"* Redistributions in binary form must reproduce the above copyright"
L"notice, this list of conditions and the following disclaimer"
L"in the documentation and / or other materials provided"
L"with the distribution.\r\n"
L"* Neither the names of Wakatime or WakaTime, nor the names of its"
L"contributors may be used to endorse or promote products derived"
L"from this software without specific prior written permission.\r\n"
L"THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND"
L"CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT"
L"NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR"
L"A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER"
L"OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,"
L"EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,"
L"PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR"
L"PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF"
L"LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING"
L"NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS"
L"SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.");
INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
{
HWND hEdit = GetDlgItem(hDlg, IDC_WAKATIME_KEY);
gConfigFileManager.ReadWakaTimeConfigFile();
SetDlgItemTextW(hDlg, IDC_WAKATIME_KEY,
gConfigFileManager.GetAPIKeyFromConfigFile().c_str());
SetFocus(hEdit);
return FALSE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDCANCEL:
// Handle cancel by sending the close windows message.
SendMessage(hDlg, WM_CLOSE, 0, 0);
return TRUE;
case IDOK:
{
// Write user provided Key to the config file with full trust.
HWND hEdit = GetDlgItem(hDlg, IDC_WAKATIME_KEY);
WCHAR key[MAX_PATH];
GetDlgItemTextW(hDlg, IDC_WAKATIME_KEY, key, MAX_PATH);
gConfigFileManager.UpdateWakaTimeAPIKey(key);
DestroyWindow(hDlg);
return TRUE;
}
case IDC_WAKATIME_KEY:
if (HIWORD(wParam) == EN_CHANGE)
{
HWND hOk = GetDlgItem(hDlg, IDOK);
EnableWindow(hOk, true);
return TRUE;
}
return FALSE;
}
break;
case WM_CLOSE:
{
// Close will close the dialog to enter WakaTime API key.
DestroyWindow(hDlg);
return TRUE;
}
case WM_DESTROY:
{
// Handle any processing that needs to be done before quitting.
return TRUE;
}
}
return FALSE;
}
int ShowWakaTimeAPIKeyDialog()
{
try
{
HWND hSelf = ::CreateDialogParam(currentInstance,
MAKEINTRESOURCE(IDD_WAKATIME_API_DLG), nppData._nppHandle,
(DLGPROC)DialogProc, (LPARAM)NULL);
if (hSelf == NULL)
{
::MessageBox(NULL, TEXT("WakaTime API Key dialog could not be created!"),
TEXT("WakaTime Notepad++ Plugin"), MB_ICONEXCLAMATION);
LOG(ERROR) << "WakaTime API Key dialog could not be created!";
return -1;
}
RECT rc;
::GetClientRect(nppData._nppHandle, &rc);
POINT center;
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
center.x = rc.left + w / 2;
center.y = rc.top + h / 2;
::ClientToScreen(nppData._nppHandle, ¢er);
RECT dlgRect;
::GetClientRect(hSelf, &dlgRect);
int x = center.x - (dlgRect.right - dlgRect.left) / 2;
int y = center.y - (dlgRect.bottom - dlgRect.top) / 2;
::SetWindowPos(hSelf, HWND_TOP, x, y, (dlgRect.right - dlgRect.left),
(dlgRect.bottom - dlgRect.top), SWP_SHOWWINDOW);
::ShowWindow(hSelf, SW_SHOW);
return 0;
}
catch (...)
{
// Don't let the NPP runtime issue a cryptic dialog bar that our plugin
// has failed. Rather fail gracefully.
::MessageBox(NULL, TEXT("WakaTime API Key dialog could not be created!"),
TEXT("WakaTime Notepad++ Plugin"), MB_ICONEXCLAMATION);
LOG(ERROR) << "Unknown error in ShowWakaTimeAPIKeyDialog function!";
return -1;
}
}
INT_PTR CALLBACK AbtDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_INITDIALOG)
{
SetDlgItemText(hDlg, IDC_EDIT1, licenseAgreement.c_str());
return TRUE;
}
if (uMsg == WM_COMMAND && LOWORD(wParam) == IDOK)
{
DestroyWindow(hDlg);
return TRUE;
}
return FALSE;
}
int ShowWakaTimeAbtDialog()
{
try
{
HWND hSelf = ::CreateDialogParam(currentInstance,
MAKEINTRESOURCE(IDD_ABOUT_DLG), nppData._nppHandle,
(DLGPROC)AbtDialogProc, (LPARAM)NULL);
if (hSelf == NULL)
{
::MessageBox(NULL, TEXT("WakaTime about dialog could not be created!"),
TEXT("WakaTime Notepad++ Plugin"), MB_ICONEXCLAMATION);
LOG(ERROR) << "WakaTime About Dialog could not be created!";
return -1;
}
RECT rc;
::GetClientRect(nppData._nppHandle, &rc);
POINT center;
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
center.x = rc.left + w / 2;
center.y = rc.top + h / 2;
::ClientToScreen(nppData._nppHandle, ¢er);
RECT dlgRect;
::GetClientRect(hSelf, &dlgRect);
int x = center.x - (dlgRect.right - dlgRect.left) / 2;
int y = center.y - (dlgRect.bottom - dlgRect.top) / 2;
::SetWindowPos(hSelf, HWND_TOP, x, y, (dlgRect.right - dlgRect.left),
(dlgRect.bottom - dlgRect.top), SWP_SHOWWINDOW);
::ShowWindow(hSelf, SW_SHOW);
return 0;
}
catch (...)
{
// Don't let the NPP runtime issue a cryptic dialog bar that our plugin
// has failed. Rather fail gracefully.
::MessageBox(NULL, TEXT("WakaTime about dialog could not be created!"),
TEXT("WakaTime Notepad++ Plugin"), MB_ICONEXCLAMATION);
LOG(ERROR) << "Unknown error in ShowWakaTimeAbtDialog function!";
return -1;
}
}
#endif