QuickPaste.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_pwndPaste = NULL;
  19. }
  20. CQuickPaste::~CQuickPaste()
  21. {
  22. if(m_pwndPaste)
  23. {
  24. delete m_pwndPaste;
  25. m_pwndPaste = NULL;
  26. }
  27. }
  28. void CQuickPaste::Create(CWnd *pParent)
  29. {
  30. CPoint point;
  31. CSize csSize;
  32. ASSERT(!m_pwndPaste);
  33. m_pwndPaste = new CQPasteWnd;
  34. ASSERT(m_pwndPaste);
  35. // load previous position and size
  36. CGetSetOptions::GetQuickPastePoint(point);
  37. CGetSetOptions::GetQuickPasteSize(csSize);
  38. CRect crRect = CRect(point, csSize);
  39. // Create the window
  40. ASSERT( m_pwndPaste->Create(crRect, pParent) );
  41. // place it at the previous position and size
  42. m_pwndPaste->MoveWindow(CRect(point, csSize));
  43. Log(_T("Creating QPasteWnd"));
  44. }
  45. BOOL CQuickPaste::CloseQPasteWnd()
  46. {
  47. if(m_pwndPaste)
  48. {
  49. if(m_pwndPaste)
  50. {
  51. m_pwndPaste->CloseWindow();
  52. m_pwndPaste->DestroyWindow();
  53. }
  54. Log(_T("CloseQPasteWnd called closing qpastewnd"));
  55. delete m_pwndPaste;
  56. m_pwndPaste = NULL;
  57. }
  58. return TRUE;
  59. }
  60. void CQuickPaste::ShowQPasteWnd(CWnd *pParent, bool bAtPrevPos, bool bFromKeyboard, BOOL bReFillList)
  61. {
  62. Log(StrF(_T("Start of ShowQPasteWnd, AtPrevPos: %d, FromKeyboard: %d, RefillList: %d"), bAtPrevPos, bFromKeyboard, bReFillList));
  63. if(bFromKeyboard == false && GetKeyState(VK_SHIFT) & 0x8000 && GetKeyState(VK_CONTROL) & 0x8000)
  64. {
  65. if(m_pwndPaste)
  66. {
  67. m_pwndPaste->CloseWindow();
  68. m_pwndPaste->DestroyWindow();
  69. }
  70. Log(_T("CloseQPasteWnd called closing qpastewnd from keyboard"));
  71. delete m_pwndPaste;
  72. m_pwndPaste = NULL;
  73. theApp.m_db.close();
  74. OpenDatabase(CGetSetOptions::GetDBPath());
  75. return;
  76. }
  77. if(g_Opt.m_bShowPersistent && m_pwndPaste != NULL)
  78. {
  79. m_pwndPaste->ShowWindow(SW_SHOW);
  80. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  81. m_pwndPaste->SetForegroundWindow();
  82. return;
  83. }
  84. int nPosition = CGetSetOptions::GetQuickPastePosition();
  85. CPoint point;
  86. CRect rcPrev;
  87. CSize csSize;
  88. if(!m_pwndPaste)
  89. m_pwndPaste = new CQPasteWnd;
  90. if(!m_pwndPaste)
  91. {
  92. ASSERT(FALSE);
  93. return;
  94. }
  95. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  96. //If it is a window get the rect otherwise get the saved point and size
  97. if (IsWindow(m_pwndPaste->m_hWnd) &&
  98. m_pwndPaste->IsIconic() == FALSE)
  99. {
  100. m_pwndPaste->GetWindowRect(rcPrev);
  101. csSize = rcPrev.Size();
  102. }
  103. else
  104. {
  105. CGetSetOptions::GetQuickPastePoint(point);
  106. CGetSetOptions::GetQuickPasteSize(csSize);
  107. }
  108. CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
  109. if(ptCaret.x <= 0 || ptCaret.y <= 0)
  110. {
  111. CRect cr;
  112. ::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
  113. if(cr.Width() > 0 && cr.Height() > 0)
  114. {
  115. ptCaret = cr.CenterPoint();
  116. ptCaret.x -= csSize.cx/2;
  117. ptCaret.y -= csSize.cy/2;
  118. }
  119. else
  120. {
  121. GetCursorPos(&point);
  122. CRect crPoint(point, CSize(1, 1));
  123. int nMonitor = GetMonitorFromRect(crPoint);
  124. if(nMonitor >= 0)
  125. {
  126. CRect crMonitor;
  127. GetMonitorRect(nMonitor, crMonitor);
  128. ptCaret = crMonitor.CenterPoint();
  129. ptCaret.x -= csSize.cx/2;
  130. ptCaret.y -= csSize.cy/2;
  131. }
  132. }
  133. }
  134. if(bAtPrevPos)
  135. {
  136. CGetSetOptions::GetQuickPastePoint(point);
  137. CGetSetOptions::GetQuickPasteSize(csSize);
  138. }
  139. else if(nPosition == POS_AT_CARET)
  140. point = ptCaret;
  141. else if(nPosition == POS_AT_CURSOR)
  142. GetCursorPos(&point);
  143. else if(nPosition == POS_AT_PREVIOUS)
  144. CGetSetOptions::GetQuickPastePoint(point);
  145. CRect crRect = CRect(point, csSize);
  146. bool forceMoveWindow = false;
  147. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  148. {
  149. if(EnsureWindowVisible(&crRect))
  150. {
  151. forceMoveWindow = true;
  152. }
  153. }
  154. if((crRect.left >= (crRect.right - 20)) ||
  155. (crRect.top >= (crRect.bottom - 20)))
  156. {
  157. CRect orig = crRect;
  158. crRect = CRect(ptCaret, CSize(300, 300));
  159. forceMoveWindow = true;
  160. 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));
  161. }
  162. if( !IsWindow(m_pwndPaste->m_hWnd) )
  163. {
  164. CWnd *pLocalParent = pParent;
  165. if(CGetSetOptions::GetShowInTaskBar())
  166. {
  167. pLocalParent = NULL;
  168. }
  169. VERIFY( m_pwndPaste->Create(crRect, pLocalParent) );
  170. }
  171. //If minimized
  172. if (m_pwndPaste->IsIconic())
  173. {
  174. m_pwndPaste->ShowWindow(SW_RESTORE);
  175. if ((nPosition == POS_AT_CARET) ||
  176. (nPosition == POS_AT_CURSOR) ||
  177. bAtPrevPos ||
  178. forceMoveWindow)
  179. {
  180. m_pwndPaste->MoveWindow(crRect);
  181. }
  182. }
  183. else
  184. {
  185. if ((nPosition == POS_AT_CARET) ||
  186. (nPosition == POS_AT_CURSOR) ||
  187. bAtPrevPos ||
  188. forceMoveWindow)
  189. {
  190. m_pwndPaste->MoveWindow(crRect);
  191. }
  192. // Show the window
  193. m_pwndPaste->ShowWindow(SW_SHOW);
  194. }
  195. m_pwndPaste->SetKeyModiferState(bFromKeyboard);
  196. if(bReFillList)
  197. {
  198. m_pwndPaste->ShowQPasteWindow(bReFillList);
  199. }
  200. m_pwndPaste->SetForegroundWindow();
  201. 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));
  202. }
  203. void CQuickPaste::MoveSelection(bool down)
  204. {
  205. if(m_pwndPaste)
  206. {
  207. if (IsWindow(m_pwndPaste->m_hWnd))
  208. {
  209. m_pwndPaste->MoveSelection(down);
  210. }
  211. }
  212. }
  213. void CQuickPaste::OnKeyStateUp()
  214. {
  215. if(m_pwndPaste)
  216. {
  217. if (IsWindow(m_pwndPaste->m_hWnd))
  218. {
  219. m_pwndPaste->OnKeyStateUp();
  220. }
  221. }
  222. }
  223. void CQuickPaste::SetKeyModiferState(bool bActive)
  224. {
  225. if(m_pwndPaste)
  226. {
  227. if (IsWindow(m_pwndPaste->m_hWnd))
  228. {
  229. m_pwndPaste->SetKeyModiferState(bActive);
  230. }
  231. }
  232. }
  233. void CQuickPaste::HideQPasteWnd()
  234. {
  235. // Hide the window
  236. if(m_pwndPaste)
  237. {
  238. if (IsWindow(m_pwndPaste->m_hWnd))
  239. m_pwndPaste->HideQPasteWindow(true);
  240. }
  241. }
  242. BOOL CQuickPaste::IsWindowVisibleEx()
  243. {
  244. if(m_pwndPaste)
  245. return IsWindowVisible(m_pwndPaste->m_hWnd);
  246. return FALSE;
  247. }
  248. bool CQuickPaste::IsWindowTopLevel()
  249. {
  250. if(m_pwndPaste)
  251. {
  252. return ::GetForegroundWindow() == m_pwndPaste->GetSafeHwnd();
  253. }
  254. return false;
  255. }