QuickPaste.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. if( !IsWindow(m_pwndPaste->m_hWnd) )
  172. {
  173. CWnd *pLocalParent = pParent;
  174. if(CGetSetOptions::GetShowInTaskBar())
  175. {
  176. pLocalParent = NULL;
  177. }
  178. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  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. m_pwndPaste->MoveWindow(crRect);
  190. }
  191. }
  192. else
  193. {
  194. if ((nPosition == POS_AT_CARET) ||
  195. (nPosition == POS_AT_CURSOR) ||
  196. bAtPrevPos ||
  197. forceMoveWindow)
  198. {
  199. m_pwndPaste->MoveWindow(crRect);
  200. }
  201. // Show the window
  202. m_pwndPaste->ShowWindow(SW_SHOW);
  203. }
  204. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  205. if(bReFillList)
  206. {
  207. m_pwndPaste->ShowQPasteWindow(bReFillList);
  208. }
  209. m_pwndPaste->SetForegroundWindow();
  210. 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));
  211. }
  212. void CQuickPaste::MoveSelection(bool down)
  213. {
  214. if(m_pwndPaste)
  215. {
  216. if (IsWindow(m_pwndPaste->m_hWnd))
  217. {
  218. m_pwndPaste->MoveSelection(down);
  219. }
  220. }
  221. }
  222. void CQuickPaste::OnKeyStateUp()
  223. {
  224. if(m_pwndPaste)
  225. {
  226. if (IsWindow(m_pwndPaste->m_hWnd))
  227. {
  228. m_pwndPaste->OnKeyStateUp();
  229. }
  230. }
  231. }
  232. void CQuickPaste::SetKeyModiferState(bool bActive)
  233. {
  234. if(m_pwndPaste)
  235. {
  236. if (IsWindow(m_pwndPaste->m_hWnd))
  237. {
  238. m_pwndPaste->SetKeyModiferState(bActive);
  239. }
  240. }
  241. }
  242. void CQuickPaste::HideQPasteWnd()
  243. {
  244. // Hide the window
  245. if(m_pwndPaste)
  246. {
  247. if (IsWindow(m_pwndPaste->m_hWnd))
  248. m_pwndPaste->HideQPasteWindow(true);
  249. }
  250. }
  251. BOOL CQuickPaste::IsWindowVisibleEx()
  252. {
  253. if(m_pwndPaste)
  254. return IsWindowVisible(m_pwndPaste->m_hWnd);
  255. return FALSE;
  256. }
  257. bool CQuickPaste::IsWindowTopLevel()
  258. {
  259. if(m_pwndPaste)
  260. {
  261. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  262. }
  263. return false;
  264. }
  265. void CQuickPaste::OnScreenResolutionChange()
  266. {
  267. if(m_pwndPaste != NULL &&
  268. ::IsWindow(m_pwndPaste->m_hWnd) &&
  269. m_pwndPaste->IsIconic() == FALSE &&
  270. IsWindowVisibleEx())
  271. {
  272. Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
  273. CPoint point;
  274. CSize csSize;
  275. CGetSetOptions::GetQuickPastePoint(point);
  276. CGetSetOptions::GetQuickPasteSize(csSize);
  277. m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
  278. }
  279. else
  280. {
  281. m_forceResizeOnNextShow = true;
  282. }
  283. }