QuickPaste.cpp 6.0 KB

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