forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickPaste.cpp
377 lines (309 loc) · 8.31 KB
/
QuickPaste.cpp
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
// QuickPaste.cpp: implementation of the CQuickPaste class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CP_Main.h"
#include "QuickPaste.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define ID_QPASTE_WND 0x1001
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CQuickPaste::CQuickPaste()
{
m_forceResizeOnNextShow = false;
m_pwndPaste = NULL;
}
CQuickPaste::~CQuickPaste()
{
if(m_pwndPaste)
{
delete m_pwndPaste;
m_pwndPaste = NULL;
}
}
void CQuickPaste::Create(CWnd *pParent)
{
CPoint point;
CSize csSize;
ASSERT(!m_pwndPaste);
m_pwndPaste = new CQPasteWnd;
ASSERT(m_pwndPaste);
// load previous position and size
CGetSetOptions::GetQuickPastePoint(point);
CGetSetOptions::GetQuickPasteSize(csSize);
CRect crRect = CRect(point, csSize);
// Create the window
ASSERT( m_pwndPaste->Create(crRect, pParent) );
// place it at the previous position and size
m_pwndPaste->MoveWindow(CRect(point, csSize));
Log(_T("Creating QPasteWnd"));
}
BOOL CQuickPaste::CloseQPasteWnd()
{
if(m_pwndPaste)
{
if(m_pwndPaste)
{
m_pwndPaste->CloseWindow();
m_pwndPaste->DestroyWindow();
}
Log(_T("CloseQPasteWnd called closing qpastewnd"));
delete m_pwndPaste;
m_pwndPaste = NULL;
theApp.m_bShowingQuickPaste = false;
}
return TRUE;
}
void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboard, BOOL bReFillList)
{
Log(StrF(_T("Start of ShowQPasteWnd, AtPrevPos: %d, FromKeyboard: %d, RefillList: %d"), bAtPrevPos, bFromKeyboard, bReFillList));
if(bFromKeyboard == false && GetKeyState(VK_SHIFT) & 0x8000 && CONTROL_PRESSED)
{
if(m_pwndPaste)
{
m_pwndPaste->CloseWindow();
m_pwndPaste->DestroyWindow();
}
Log(_T("CloseQPasteWnd called closing qpastewnd from keyboard"));
delete m_pwndPaste;
m_pwndPaste = NULL;
theApp.m_db.close();
OpenDatabase(CGetSetOptions::GetDBPath());
return;
}
if(g_Opt.m_bShowPersistent && m_pwndPaste != NULL)
{
m_pwndPaste->ShowWindow(SW_SHOW);
m_pwndPaste->MinMaxWindow(FORCE_MAX);
m_pwndPaste->SetForegroundWindow();
return;
}
int nPosition = CGetSetOptions::GetQuickPastePosition();
CPoint point;
CRect rcPrev;
CSize csSize;
if(!m_pwndPaste)
m_pwndPaste = new CQPasteWnd;
if(!m_pwndPaste)
{
ASSERT(FALSE);
return;
}
m_pwndPaste->MinMaxWindow(FORCE_MAX);
//If it is a window get the rect otherwise get the saved point and size
if (IsWindow(m_pwndPaste->m_hWnd) &&
m_pwndPaste->IsIconic() == FALSE &&
m_forceResizeOnNextShow == false)
{
m_pwndPaste->GetWindowRect(rcPrev);
csSize = rcPrev.Size();
}
else
{
CGetSetOptions::GetQuickPastePoint(point);
CGetSetOptions::GetQuickPasteSize(csSize);
if (IsWindow(m_pwndPaste->m_hWnd))
{
csSize.cx = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cx);
csSize.cy = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cy);
}
}
CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
if(ptCaret.x == -1 || ptCaret.y == -1)
{
CRect cr;
::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
if(theApp.m_activeWnd.DesktopHasFocus() == false &&
cr.Width() > 0 &&
cr.Height() > 0)
{
ptCaret = cr.CenterPoint();
ptCaret.x -= csSize.cx/2;
ptCaret.y -= csSize.cy/2;
}
else
{
GetCursorPos(&point);
CRect crPoint(point, CSize(1, 1));
CRect crMonitor = MonitorRectFromRect(crPoint);
ptCaret = crMonitor.CenterPoint();
ptCaret.x -= csSize.cx/2;
ptCaret.y -= csSize.cy/2;
}
}
if(bAtPrevPos)
{
CGetSetOptions::GetQuickPastePoint(point);
CGetSetOptions::GetQuickPasteSize(csSize);
}
else if (nPosition == POS_AT_CARET)
{
point = ptCaret;
}
else if (nPosition == POS_AT_CURSOR)
{
GetCursorPos(&point);
//keep the mouse from showing the tooltip because if overlaps with the top corner
point.x += 2;
point.y += 2;
}
else if(nPosition == POS_AT_PREVIOUS)
CGetSetOptions::GetQuickPastePoint(point);
CRect crRect = CRect(point, csSize);
bool forceMoveWindow = m_forceResizeOnNextShow;
if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
{
if(EnsureWindowVisible(&crRect))
{
forceMoveWindow = true;
}
}
if((crRect.left >= (crRect.right - 20)) ||
(crRect.top >= (crRect.bottom - 20)))
{
CRect orig = crRect;
crRect = CRect(ptCaret, CSize(300, 300));
forceMoveWindow = true;
Log(StrF(_T("Invalid initial size %d %d %d %d, Centered Window %d %d %d %d"), orig.left, orig.top, orig.right, orig.bottom, crRect.left, crRect.top, crRect.right, crRect.bottom));
}
bool adjustRect = false;
if( !IsWindow(m_pwndPaste->m_hWnd) )
{
CWnd *pLocalParent = pParent;
if(CGetSetOptions::GetShowInTaskBar())
{
pLocalParent = NULL;
}
VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
adjustRect = true;
}
//If minimized
if (m_pwndPaste->IsIconic())
{
m_pwndPaste->ShowWindow(SW_RESTORE);
if ((nPosition == POS_AT_CARET) ||
(nPosition == POS_AT_CURSOR) ||
bAtPrevPos ||
forceMoveWindow)
{
if (adjustRect)
{
crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
if (g_Opt.m_bEnsureEntireWindowCanBeSeen)
{
EnsureWindowVisible(&crRect);
}
}
m_pwndPaste->MoveWindow(crRect);
}
}
else
{
if ((nPosition == POS_AT_CARET) ||
(nPosition == POS_AT_CURSOR) ||
bAtPrevPos ||
forceMoveWindow)
{
if (adjustRect)
{
crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
if (g_Opt.m_bEnsureEntireWindowCanBeSeen)
{
EnsureWindowVisible(&crRect);
}
}
m_pwndPaste->MoveWindow(crRect);
}
// Show the window
m_pwndPaste->ShowWindow(SW_SHOW);
}
m_pwndPaste->SetKeyModiferState(bFromKeyboard);
if(bReFillList)
{
m_pwndPaste->ShowQPasteWindow(bReFillList);
}
m_pwndPaste->SetForegroundWindow();
Log(StrF(_T("END of ShowQPasteWnd, AtPrevPos: %d, FromKeyboard: %d, RefillList: %d, Position, %d %d %d %d"), bAtPrevPos, bFromKeyboard, bReFillList, crRect.left, crRect.top, crRect.right, crRect.bottom));
m_forceResizeOnNextShow = false;
}
void CQuickPaste::MoveSelection(bool down)
{
if(m_pwndPaste && g_Opt.m_moveSelectionOnOpenHotkey)
{
if (IsWindow(m_pwndPaste->m_hWnd))
{
m_pwndPaste->MoveSelection(down, true);
}
}
}
void CQuickPaste::OnKeyStateUp()
{
if(m_pwndPaste && g_Opt.m_moveSelectionOnOpenHotkey)
{
if (IsWindow(m_pwndPaste->m_hWnd))
{
m_pwndPaste->OnKeyStateUp();
}
}
}
void CQuickPaste::SetKeyModiferState(bool bActive)
{
if(m_pwndPaste && g_Opt.m_moveSelectionOnOpenHotkey)
{
if (IsWindow(m_pwndPaste->m_hWnd))
{
m_pwndPaste->SetKeyModiferState(bActive);
}
}
}
void CQuickPaste::HideQPasteWnd()
{
// Hide the window
if(m_pwndPaste)
{
if (IsWindow(m_pwndPaste->m_hWnd))
m_pwndPaste->HideQPasteWindow(true);
}
}
BOOL CQuickPaste::IsWindowVisibleEx()
{
if(m_pwndPaste)
return IsWindowVisible(m_pwndPaste->m_hWnd);
return FALSE;
}
bool CQuickPaste::IsWindowTopLevel()
{
if(m_pwndPaste)
{
return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
}
return false;
}
void CQuickPaste::OnScreenResolutionChange()
{
if(m_pwndPaste != NULL &&
::IsWindow(m_pwndPaste->m_hWnd) &&
m_pwndPaste->IsIconic() == FALSE &&
IsWindowVisibleEx())
{
Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
CPoint point;
CSize csSize;
CGetSetOptions::GetQuickPastePoint(point);
CGetSetOptions::GetQuickPasteSize(csSize);
csSize.cx = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cx);
csSize.cy = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cy);
m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
}
else
{
m_forceResizeOnNextShow = true;
}
}