QuickPaste.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. point = ptCaret;
  143. else if(nPosition == POS_AT_CURSOR)
  144. GetCursorPos(&point);
  145. else if(nPosition == POS_AT_PREVIOUS)
  146. CGetSetOptions::GetQuickPastePoint(point);
  147. CRect crRect = CRect(point, csSize);
  148. bool forceMoveWindow = m_forceResizeOnNextShow;
  149. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  150. {
  151. if(EnsureWindowVisible(&crRect))
  152. {
  153. forceMoveWindow = true;
  154. }
  155. }
  156. if((crRect.left >= (crRect.right - 20)) ||
  157. (crRect.top >= (crRect.bottom - 20)))
  158. {
  159. CRect orig = crRect;
  160. crRect = CRect(ptCaret, CSize(300, 300));
  161. forceMoveWindow = true;
  162. 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));
  163. }
  164. if( !IsWindow(m_pwndPaste->m_hWnd) )
  165. {
  166. CWnd *pLocalParent = pParent;
  167. if(CGetSetOptions::GetShowInTaskBar())
  168. {
  169. pLocalParent = NULL;
  170. }
  171. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  172. }
  173. //If minimized
  174. if (m_pwndPaste->IsIconic())
  175. {
  176. m_pwndPaste->ShowWindow(SW_RESTORE);
  177. if ((nPosition == POS_AT_CARET) ||
  178. (nPosition == POS_AT_CURSOR) ||
  179. bAtPrevPos ||
  180. forceMoveWindow)
  181. {
  182. m_pwndPaste->MoveWindow(crRect);
  183. }
  184. }
  185. else
  186. {
  187. if ((nPosition == POS_AT_CARET) ||
  188. (nPosition == POS_AT_CURSOR) ||
  189. bAtPrevPos ||
  190. forceMoveWindow)
  191. {
  192. m_pwndPaste->MoveWindow(crRect);
  193. }
  194. // Show the window
  195. m_pwndPaste->ShowWindow(SW_SHOW);
  196. }
  197. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  198. if(bReFillList)
  199. {
  200. m_pwndPaste->ShowQPasteWindow(bReFillList);
  201. }
  202. m_pwndPaste->SetForegroundWindow();
  203. 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));
  204. }
  205. void CQuickPaste::MoveSelection(bool down)
  206. {
  207. if(m_pwndPaste)
  208. {
  209. if (IsWindow(m_pwndPaste->m_hWnd))
  210. {
  211. m_pwndPaste->MoveSelection(down);
  212. }
  213. }
  214. }
  215. void CQuickPaste::OnKeyStateUp()
  216. {
  217. if(m_pwndPaste)
  218. {
  219. if (IsWindow(m_pwndPaste->m_hWnd))
  220. {
  221. m_pwndPaste->OnKeyStateUp();
  222. }
  223. }
  224. }
  225. void CQuickPaste::SetKeyModiferState(bool bActive)
  226. {
  227. if(m_pwndPaste)
  228. {
  229. if (IsWindow(m_pwndPaste->m_hWnd))
  230. {
  231. m_pwndPaste->SetKeyModiferState(bActive);
  232. }
  233. }
  234. }
  235. void CQuickPaste::HideQPasteWnd()
  236. {
  237. // Hide the window
  238. if(m_pwndPaste)
  239. {
  240. if (IsWindow(m_pwndPaste->m_hWnd))
  241. m_pwndPaste->HideQPasteWindow(true);
  242. }
  243. }
  244. BOOL CQuickPaste::IsWindowVisibleEx()
  245. {
  246. if(m_pwndPaste)
  247. return IsWindowVisible(m_pwndPaste->m_hWnd);
  248. return FALSE;
  249. }
  250. bool CQuickPaste::IsWindowTopLevel()
  251. {
  252. if(m_pwndPaste)
  253. {
  254. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  255. }
  256. return false;
  257. }
  258. void CQuickPaste::OnScreenResolutionChange()
  259. {
  260. if(m_pwndPaste != NULL &&
  261. ::IsWindow(m_pwndPaste->m_hWnd) &&
  262. m_pwndPaste->IsIconic() == FALSE &&
  263. IsWindowVisibleEx())
  264. {
  265. Log(StrF(_T("Window Position changed, moving window to position as of this screen resolution %dx%d"), GetScreenWidth(), GetScreenHeight()));
  266. CPoint point;
  267. CSize csSize;
  268. CGetSetOptions::GetQuickPastePoint(point);
  269. CGetSetOptions::GetQuickPasteSize(csSize);
  270. m_pwndPaste->MoveWindow(point.x, point.y, csSize.cx, csSize.cy);
  271. }
  272. else
  273. {
  274. m_forceResizeOnNextShow = true;
  275. }
  276. }