QuickPaste.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. }
  59. return TRUE;
  60. }
  61. void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboard, BOOL bReFillList)
  62. {
  63. Log(StrF(_T("Start of ShowQPasteWnd, AtPrevPos: %d, FromKeyboard: %d, RefillList: %d"), bAtPrevPos, bFromKeyboard, bReFillList));
  64. if(bFromKeyboard == false && GetKeyState(VK_SHIFT) & 0x8000 && CONTROL_PRESSED)
  65. {
  66. if(m_pwndPaste)
  67. {
  68. m_pwndPaste->CloseWindow();
  69. m_pwndPaste->DestroyWindow();
  70. }
  71. Log(_T("CloseQPasteWnd called closing qpastewnd from keyboard"));
  72. delete m_pwndPaste;
  73. m_pwndPaste = NULL;
  74. theApp.m_db.close();
  75. OpenDatabase(CGetSetOptions::GetDBPath());
  76. return;
  77. }
  78. if(g_Opt.m_bShowPersistent && m_pwndPaste != NULL)
  79. {
  80. m_pwndPaste->ShowWindow(SW_SHOW);
  81. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  82. m_pwndPaste->SetForegroundWindow();
  83. return;
  84. }
  85. int nPosition = CGetSetOptions::GetQuickPastePosition();
  86. CPoint point;
  87. CRect rcPrev;
  88. CSize csSize;
  89. if(!m_pwndPaste)
  90. m_pwndPaste = new CQPasteWnd;
  91. if(!m_pwndPaste)
  92. {
  93. ASSERT(FALSE);
  94. return;
  95. }
  96. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  97. //If it is a window get the rect otherwise get the saved point and size
  98. if (IsWindow(m_pwndPaste->m_hWnd) &&
  99. m_pwndPaste->IsIconic() == FALSE &&
  100. m_forceResizeOnNextShow == false)
  101. {
  102. m_pwndPaste->GetWindowRect(rcPrev);
  103. csSize = rcPrev.Size();
  104. }
  105. else
  106. {
  107. CGetSetOptions::GetQuickPastePoint(point);
  108. CGetSetOptions::GetQuickPasteSize(csSize);
  109. }
  110. CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
  111. if(ptCaret.x <= 0 || ptCaret.y <= 0)
  112. {
  113. CRect cr;
  114. ::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
  115. if(cr.Width() > 0 && cr.Height() > 0)
  116. {
  117. ptCaret = cr.CenterPoint();
  118. ptCaret.x -= csSize.cx/2;
  119. ptCaret.y -= csSize.cy/2;
  120. }
  121. else
  122. {
  123. GetCursorPos(&point);
  124. CRect crPoint(point, CSize(1, 1));
  125. int nMonitor = GetMonitorFromRect(crPoint);
  126. if(nMonitor >= 0)
  127. {
  128. CRect crMonitor;
  129. GetMonitorRect(nMonitor, crMonitor);
  130. ptCaret = crMonitor.CenterPoint();
  131. ptCaret.x -= csSize.cx/2;
  132. ptCaret.y -= csSize.cy/2;
  133. }
  134. }
  135. }
  136. if(bAtPrevPos)
  137. {
  138. CGetSetOptions::GetQuickPastePoint(point);
  139. CGetSetOptions::GetQuickPasteSize(csSize);
  140. }
  141. else if (nPosition == POS_AT_CARET)
  142. {
  143. point = ptCaret;
  144. }
  145. else if (nPosition == POS_AT_CURSOR)
  146. {
  147. GetCursorPos(&point);
  148. //keep the mouse from showing the tooltip because if overlaps with the top corner
  149. point.x += 2;
  150. point.y += 2;
  151. }
  152. else if(nPosition == POS_AT_PREVIOUS)
  153. CGetSetOptions::GetQuickPastePoint(point);
  154. CRect crRect = CRect(point, csSize);
  155. bool forceMoveWindow = m_forceResizeOnNextShow;
  156. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  157. {
  158. if(EnsureWindowVisible(&crRect))
  159. {
  160. forceMoveWindow = true;
  161. }
  162. }
  163. if((crRect.left >= (crRect.right - 20)) ||
  164. (crRect.top >= (crRect.bottom - 20)))
  165. {
  166. CRect orig = crRect;
  167. crRect = CRect(ptCaret, CSize(300, 300));
  168. forceMoveWindow = true;
  169. 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));
  170. }
  171. bool adjustRect = false;
  172. if( !IsWindow(m_pwndPaste->m_hWnd) )
  173. {
  174. CWnd *pLocalParent = pParent;
  175. if(CGetSetOptions::GetShowInTaskBar())
  176. {
  177. pLocalParent = NULL;
  178. }
  179. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  180. adjustRect = true;
  181. }
  182. //If minimized
  183. if (m_pwndPaste->IsIconic())
  184. {
  185. m_pwndPaste->ShowWindow(SW_RESTORE);
  186. if ((nPosition == POS_AT_CARET) ||
  187. (nPosition == POS_AT_CURSOR) ||
  188. bAtPrevPos ||
  189. forceMoveWindow)
  190. {
  191. if (adjustRect)
  192. {
  193. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.ScaleX(crRect.Width());
  194. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.ScaleY(crRect.Height());
  195. }
  196. m_pwndPaste->MoveWindow(crRect);
  197. }
  198. }
  199. else
  200. {
  201. if ((nPosition == POS_AT_CARET) ||
  202. (nPosition == POS_AT_CURSOR) ||
  203. bAtPrevPos ||
  204. forceMoveWindow)
  205. {
  206. if (adjustRect)
  207. {
  208. crRect.right = crRect.left + m_pwndPaste->m_DittoWindow.m_dpi.ScaleX(crRect.Width());
  209. crRect.bottom = crRect.top + m_pwndPaste->m_DittoWindow.m_dpi.ScaleY(crRect.Height());
  210. }
  211. m_pwndPaste->MoveWindow(crRect);
  212. }
  213. // Show the window
  214. m_pwndPaste->ShowWindow(SW_SHOW);
  215. }
  216. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  217. if(bReFillList)
  218. {
  219. m_pwndPaste->ShowQPasteWindow(bReFillList);
  220. }
  221. m_pwndPaste->SetForegroundWindow();
  222. 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));
  223. }
  224. void CQuickPaste::MoveSelection(bool down)
  225. {
  226. if(m_pwndPaste)
  227. {
  228. if (IsWindow(m_pwndPaste->m_hWnd))
  229. {
  230. m_pwndPaste->MoveSelection(down, true);
  231. }
  232. }
  233. }
  234. void CQuickPaste::OnKeyStateUp()
  235. {
  236. if(m_pwndPaste)
  237. {
  238. if (IsWindow(m_pwndPaste->m_hWnd))
  239. {
  240. m_pwndPaste->OnKeyStateUp();
  241. }
  242. }
  243. }
  244. void CQuickPaste::SetKeyModiferState(bool bActive)
  245. {
  246. if(m_pwndPaste)
  247. {
  248. if (IsWindow(m_pwndPaste->m_hWnd))
  249. {
  250. m_pwndPaste->SetKeyModiferState(bActive);
  251. }
  252. }
  253. }
  254. void CQuickPaste::HideQPasteWnd()
  255. {
  256. // Hide the window
  257. if(m_pwndPaste)
  258. {
  259. if (IsWindow(m_pwndPaste->m_hWnd))
  260. m_pwndPaste->HideQPasteWindow(true);
  261. }
  262. }
  263. BOOL CQuickPaste::IsWindowVisibleEx()
  264. {
  265. if(m_pwndPaste)
  266. return IsWindowVisible(m_pwndPaste->m_hWnd);
  267. return FALSE;
  268. }
  269. bool CQuickPaste::IsWindowTopLevel()
  270. {
  271. if(m_pwndPaste)
  272. {
  273. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  274. }
  275. return false;
  276. }
  277. void CQuickPaste::OnScreenResolutionChange()
  278. {
  279. if(m_pwndPaste != NULL &&
  280. ::IsWindow(m_pwndPaste->m_hWnd) &&
  281. m_pwndPaste->IsIconic() == FALSE &&
  282. IsWindowVisibleEx())
  283. {
  284. Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
  285. CPoint point;
  286. CSize csSize;
  287. CGetSetOptions::GetQuickPastePoint(point);
  288. CGetSetOptions::GetQuickPasteSize(csSize);
  289. m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
  290. }
  291. else
  292. {
  293. m_forceResizeOnNextShow = true;
  294. }
  295. }