QuickPaste.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. }
  111. CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
  112. if(ptCaret.x == -1 || ptCaret.y == -1)
  113. {
  114. CRect cr;
  115. ::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
  116. if(theApp.m_activeWnd.DesktopHasFocus() == false &&
  117. cr.Width() > 0 &&
  118. cr.Height() > 0)
  119. {
  120. ptCaret = cr.CenterPoint();
  121. ptCaret.x -= csSize.cx/2;
  122. ptCaret.y -= csSize.cy/2;
  123. }
  124. else
  125. {
  126. GetCursorPos(&point);
  127. CRect crPoint(point, CSize(1, 1));
  128. CRect crMonitor = MonitorRectFromRect(crPoint);
  129. ptCaret = crMonitor.CenterPoint();
  130. ptCaret.x -= csSize.cx/2;
  131. ptCaret.y -= csSize.cy/2;
  132. }
  133. }
  134. if(bAtPrevPos)
  135. {
  136. CGetSetOptions::GetQuickPastePoint(point);
  137. CGetSetOptions::GetQuickPasteSize(csSize);
  138. }
  139. else if (nPosition == POS_AT_CARET)
  140. {
  141. point = ptCaret;
  142. }
  143. else if (nPosition == POS_AT_CURSOR)
  144. {
  145. GetCursorPos(&point);
  146. //keep the mouse from showing the tooltip because if overlaps with the top corner
  147. point.x += 2;
  148. point.y += 2;
  149. }
  150. else if(nPosition == POS_AT_PREVIOUS)
  151. CGetSetOptions::GetQuickPastePoint(point);
  152. CRect crRect = CRect(point, csSize);
  153. bool forceMoveWindow = m_forceResizeOnNextShow;
  154. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  155. {
  156. if(EnsureWindowVisible(&crRect))
  157. {
  158. forceMoveWindow = true;
  159. }
  160. }
  161. if((crRect.left >= (crRect.right - 20)) ||
  162. (crRect.top >= (crRect.bottom - 20)))
  163. {
  164. CRect orig = crRect;
  165. crRect = CRect(ptCaret, CSize(300, 300));
  166. forceMoveWindow = true;
  167. 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));
  168. }
  169. bool adjustRect = false;
  170. if( !IsWindow(m_pwndPaste->m_hWnd) )
  171. {
  172. CWnd *pLocalParent = pParent;
  173. if(CGetSetOptions::GetShowInTaskBar())
  174. {
  175. pLocalParent = NULL;
  176. }
  177. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  178. adjustRect = true;
  179. }
  180. //If minimized
  181. if (m_pwndPaste->IsIconic())
  182. {
  183. m_pwndPaste->ShowWindow(SW_RESTORE);
  184. if ((nPosition == POS_AT_CARET) ||
  185. (nPosition == POS_AT_CURSOR) ||
  186. bAtPrevPos ||
  187. forceMoveWindow)
  188. {
  189. if (adjustRect)
  190. {
  191. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
  192. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
  193. }
  194. m_pwndPaste->MoveWindow(crRect);
  195. }
  196. }
  197. else
  198. {
  199. if ((nPosition == POS_AT_CARET) ||
  200. (nPosition == POS_AT_CURSOR) ||
  201. bAtPrevPos ||
  202. forceMoveWindow)
  203. {
  204. if (adjustRect)
  205. {
  206. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Width());
  207. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.Scale(crRect.Height());
  208. }
  209. m_pwndPaste->MoveWindow(crRect);
  210. }
  211. // Show the window
  212. m_pwndPaste->ShowWindow(SW_SHOW);
  213. }
  214. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  215. if(bReFillList)
  216. {
  217. m_pwndPaste->ShowQPasteWindow(bReFillList);
  218. }
  219. m_pwndPaste->SetForegroundWindow();
  220. 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));
  221. m_forceResizeOnNextShow = false;
  222. }
  223. void CQuickPaste::MoveSelection(bool down)
  224. {
  225. if(m_pwndPaste)
  226. {
  227. if (IsWindow(m_pwndPaste->m_hWnd))
  228. {
  229. m_pwndPaste->MoveSelection(down, true);
  230. }
  231. }
  232. }
  233. void CQuickPaste::OnKeyStateUp()
  234. {
  235. if(m_pwndPaste)
  236. {
  237. if (IsWindow(m_pwndPaste->m_hWnd))
  238. {
  239. m_pwndPaste->OnKeyStateUp();
  240. }
  241. }
  242. }
  243. void CQuickPaste::SetKeyModiferState(bool bActive)
  244. {
  245. if(m_pwndPaste)
  246. {
  247. if (IsWindow(m_pwndPaste->m_hWnd))
  248. {
  249. m_pwndPaste->SetKeyModiferState(bActive);
  250. }
  251. }
  252. }
  253. void CQuickPaste::HideQPasteWnd()
  254. {
  255. // Hide the window
  256. if(m_pwndPaste)
  257. {
  258. if (IsWindow(m_pwndPaste->m_hWnd))
  259. m_pwndPaste->HideQPasteWindow(true);
  260. }
  261. }
  262. BOOL CQuickPaste::IsWindowVisibleEx()
  263. {
  264. if(m_pwndPaste)
  265. return IsWindowVisible(m_pwndPaste->m_hWnd);
  266. return FALSE;
  267. }
  268. bool CQuickPaste::IsWindowTopLevel()
  269. {
  270. if(m_pwndPaste)
  271. {
  272. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  273. }
  274. return false;
  275. }
  276. void CQuickPaste::OnScreenResolutionChange()
  277. {
  278. if(m_pwndPaste != NULL &&
  279. ::IsWindow(m_pwndPaste->m_hWnd) &&
  280. m_pwndPaste->IsIconic() == FALSE &&
  281. IsWindowVisibleEx())
  282. {
  283. Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
  284. CPoint point;
  285. CSize csSize;
  286. CGetSetOptions::GetQuickPastePoint(point);
  287. CGetSetOptions::GetQuickPasteSize(csSize);
  288. csSize.cx = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cx);
  289. csSize.cy = m_pwndPaste->m_DittoWindow.m_dpi.Scale(csSize.cy);
  290. m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
  291. }
  292. else
  293. {
  294. m_forceResizeOnNextShow = true;
  295. }
  296. }