QuickPaste.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // QuickPaste.cpp: implementation of the CQuickPaste class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "QuickPaste.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. #define ID_QPASTE_WND 0x1001
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CQuickPaste::CQuickPaste()
  17. {
  18. m_forceResizeOnNextShow = false;
  19. m_pwndPaste = NULL;
  20. }
  21. CQuickPaste::~CQuickPaste()
  22. {
  23. if(m_pwndPaste)
  24. {
  25. delete m_pwndPaste;
  26. m_pwndPaste = NULL;
  27. }
  28. }
  29. void CQuickPaste::Create(CWnd *pParent)
  30. {
  31. CPoint point;
  32. CSize csSize;
  33. ASSERT(!m_pwndPaste);
  34. m_pwndPaste = new CQPasteWnd;
  35. ASSERT(m_pwndPaste);
  36. // load previous position and size
  37. CGetSetOptions::GetQuickPastePoint(point);
  38. CGetSetOptions::GetQuickPasteSize(csSize);
  39. CRect crRect = CRect(point, csSize);
  40. // Create the window
  41. ASSERT( m_pwndPaste->Create(crRect, pParent) );
  42. // place it at the previous position and size
  43. m_pwndPaste->MoveWindow(CRect(point, csSize));
  44. Log(_T("Creating QPasteWnd"));
  45. }
  46. BOOL CQuickPaste::CloseQPasteWnd()
  47. {
  48. if(m_pwndPaste)
  49. {
  50. if(m_pwndPaste)
  51. {
  52. m_pwndPaste->CloseWindow();
  53. m_pwndPaste->DestroyWindow();
  54. }
  55. Log(_T("CloseQPasteWnd called closing qpastewnd"));
  56. delete m_pwndPaste;
  57. m_pwndPaste = NULL;
  58. theApp.m_bShowingQuickPaste = false;
  59. }
  60. return TRUE;
  61. }
  62. void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboard, BOOL bReFillList)
  63. {
  64. Log(StrF(_T("Start of ShowQPasteWnd, AtPrevPos: %d, FromKeyboard: %d, RefillList: %d"), bAtPrevPos, bFromKeyboard, bReFillList));
  65. if(bFromKeyboard == false && GetKeyState(VK_SHIFT) & 0x8000 && CONTROL_PRESSED)
  66. {
  67. if(m_pwndPaste)
  68. {
  69. m_pwndPaste->CloseWindow();
  70. m_pwndPaste->DestroyWindow();
  71. }
  72. Log(_T("CloseQPasteWnd called closing qpastewnd from keyboard"));
  73. delete m_pwndPaste;
  74. m_pwndPaste = NULL;
  75. theApp.m_db.close();
  76. OpenDatabase(CGetSetOptions::GetDBPath());
  77. return;
  78. }
  79. if(g_Opt.m_bShowPersistent && m_pwndPaste != NULL)
  80. {
  81. m_pwndPaste->ShowWindow(SW_SHOW);
  82. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  83. m_pwndPaste->SetForegroundWindow();
  84. return;
  85. }
  86. int nPosition = CGetSetOptions::GetQuickPastePosition();
  87. CPoint point;
  88. CRect rcPrev;
  89. CSize csSize;
  90. if(!m_pwndPaste)
  91. m_pwndPaste = new CQPasteWnd;
  92. if(!m_pwndPaste)
  93. {
  94. ASSERT(FALSE);
  95. return;
  96. }
  97. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  98. //If it is a window get the rect otherwise get the saved point and size
  99. if (IsWindow(m_pwndPaste->m_hWnd) &&
  100. m_pwndPaste->IsIconic() == FALSE &&
  101. m_forceResizeOnNextShow == false)
  102. {
  103. m_pwndPaste->GetWindowRect(rcPrev);
  104. csSize = rcPrev.Size();
  105. }
  106. else
  107. {
  108. CGetSetOptions::GetQuickPastePoint(point);
  109. CGetSetOptions::GetQuickPasteSize(csSize);
  110. if (IsWindow(m_pwndPaste->m_hWnd))
  111. {
  112. csSize.cx = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cx);
  113. csSize.cy = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cy);
  114. }
  115. }
  116. CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
  117. if(ptCaret.x == -1 || ptCaret.y == -1)
  118. {
  119. CRect cr;
  120. ::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
  121. if(theApp.m_activeWnd.DesktopHasFocus() == false &&
  122. cr.Width() > 0 &&
  123. cr.Height() > 0)
  124. {
  125. ptCaret = cr.CenterPoint();
  126. ptCaret.x -= csSize.cx/2;
  127. ptCaret.y -= csSize.cy/2;
  128. }
  129. else
  130. {
  131. GetCursorPos(&point);
  132. CRect crPoint(point, CSize(1, 1));
  133. CRect crMonitor = MonitorRectFromRect(crPoint);
  134. ptCaret = crMonitor.CenterPoint();
  135. ptCaret.x -= csSize.cx/2;
  136. ptCaret.y -= csSize.cy/2;
  137. }
  138. }
  139. if(bAtPrevPos)
  140. {
  141. CGetSetOptions::GetQuickPastePoint(point);
  142. CGetSetOptions::GetQuickPasteSize(csSize);
  143. }
  144. else if (nPosition == POS_AT_CARET)
  145. {
  146. point = ptCaret;
  147. }
  148. else if (nPosition == POS_AT_CURSOR)
  149. {
  150. GetCursorPos(&point);
  151. //keep the mouse from showing the tooltip because if overlaps with the top corner
  152. point.x += 2;
  153. point.y += 2;
  154. }
  155. else if(nPosition == POS_AT_PREVIOUS)
  156. CGetSetOptions::GetQuickPastePoint(point);
  157. CRect crRect = CRect(point, csSize);
  158. bool forceMoveWindow = m_forceResizeOnNextShow;
  159. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  160. {
  161. if(EnsureWindowVisible(&crRect))
  162. {
  163. forceMoveWindow = true;
  164. }
  165. }
  166. if((crRect.left >= (crRect.right - 20)) ||
  167. (crRect.top >= (crRect.bottom - 20)))
  168. {
  169. CRect orig = crRect;
  170. crRect = CRect(ptCaret, CSize(300, 300));
  171. forceMoveWindow = true;
  172. 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));
  173. }
  174. bool adjustRect = false;
  175. if( !IsWindow(m_pwndPaste->m_hWnd) )
  176. {
  177. CWnd *pLocalParent = pParent;
  178. if(CGetSetOptions::GetShowInTaskBar())
  179. {
  180. pLocalParent = NULL;
  181. }
  182. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  183. adjustRect = true;
  184. }
  185. //If minimized
  186. if (m_pwndPaste->IsIconic())
  187. {
  188. m_pwndPaste->ShowWindow(SW_RESTORE);
  189. if ((nPosition == POS_AT_CARET) ||
  190. (nPosition == POS_AT_CURSOR) ||
  191. bAtPrevPos ||
  192. forceMoveWindow)
  193. {
  194. if (adjustRect)
  195. {
  196. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
  197. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
  198. if (g_Opt.m_bEnsureEntireWindowCanBeSeen)
  199. {
  200. EnsureWindowVisible(&crRect);
  201. }
  202. }
  203. m_pwndPaste->MoveWindow(crRect);
  204. }
  205. }
  206. else
  207. {
  208. if ((nPosition == POS_AT_CARET) ||
  209. (nPosition == POS_AT_CURSOR) ||
  210. bAtPrevPos ||
  211. forceMoveWindow)
  212. {
  213. if (adjustRect)
  214. {
  215. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
  216. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
  217. if (g_Opt.m_bEnsureEntireWindowCanBeSeen)
  218. {
  219. EnsureWindowVisible(&crRect);
  220. }
  221. }
  222. m_pwndPaste->MoveWindow(crRect);
  223. }
  224. // Show the window
  225. m_pwndPaste->ShowWindow(SW_SHOW);
  226. }
  227. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  228. if(bReFillList)
  229. {
  230. m_pwndPaste->ShowQPasteWindow(bReFillList);
  231. }
  232. m_pwndPaste->SetForegroundWindow();
  233. 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));
  234. m_forceResizeOnNextShow = false;
  235. }
  236. void CQuickPaste::MoveSelection(bool down)
  237. {
  238. if(m_pwndPaste)
  239. {
  240. if (IsWindow(m_pwndPaste->m_hWnd))
  241. {
  242. m_pwndPaste->MoveSelection(down, true);
  243. }
  244. }
  245. }
  246. void CQuickPaste::OnKeyStateUp()
  247. {
  248. if(m_pwndPaste)
  249. {
  250. if (IsWindow(m_pwndPaste->m_hWnd))
  251. {
  252. m_pwndPaste->OnKeyStateUp();
  253. }
  254. }
  255. }
  256. void CQuickPaste::SetKeyModiferState(bool bActive)
  257. {
  258. if(m_pwndPaste)
  259. {
  260. if (IsWindow(m_pwndPaste->m_hWnd))
  261. {
  262. m_pwndPaste->SetKeyModiferState(bActive);
  263. }
  264. }
  265. }
  266. void CQuickPaste::HideQPasteWnd()
  267. {
  268. // Hide the window
  269. if(m_pwndPaste)
  270. {
  271. if (IsWindow(m_pwndPaste->m_hWnd))
  272. m_pwndPaste->HideQPasteWindow(true);
  273. }
  274. }
  275. BOOL CQuickPaste::IsWindowVisibleEx()
  276. {
  277. if(m_pwndPaste)
  278. return IsWindowVisible(m_pwndPaste->m_hWnd);
  279. return FALSE;
  280. }
  281. bool CQuickPaste::IsWindowTopLevel()
  282. {
  283. if(m_pwndPaste)
  284. {
  285. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  286. }
  287. return false;
  288. }
  289. void CQuickPaste::OnScreenResolutionChange()
  290. {
  291. if(m_pwndPaste != NULL &&
  292. ::IsWindow(m_pwndPaste->m_hWnd) &&
  293. m_pwndPaste->IsIconic() == FALSE &&
  294. IsWindowVisibleEx())
  295. {
  296. Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
  297. CPoint point;
  298. CSize csSize;
  299. CGetSetOptions::GetQuickPastePoint(point);
  300. CGetSetOptions::GetQuickPasteSize(csSize);
  301. csSize.cx = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cx);
  302. csSize.cy = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cy);
  303. m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
  304. }
  305. else
  306. {
  307. m_forceResizeOnNextShow = true;
  308. }
  309. }