QuickPaste.cpp 5.4 KB

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