QuickPaste.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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->IsWindowVisible())
  49. {
  50. Log(_T("CloseQPasteWnd called but the window is visible"));
  51. return FALSE;
  52. }
  53. if(m_pwndPaste)
  54. m_pwndPaste->DestroyWindow();
  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 && GetKeyState(VK_CONTROL) & 0x8000)
  65. {
  66. if(m_pwndPaste)
  67. m_pwndPaste->DestroyWindow();
  68. Log(_T("CloseQPasteWnd called closing qpastewnd from keyboard"));
  69. delete m_pwndPaste;
  70. m_pwndPaste = NULL;
  71. theApp.m_db.close();
  72. OpenDatabase(CGetSetOptions::GetDBPath());
  73. return;
  74. }
  75. if(g_Opt.m_bShowPersistent && m_pwndPaste != NULL)
  76. {
  77. m_pwndPaste->ShowWindow(SW_SHOW);
  78. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  79. m_pwndPaste->SetForegroundWindow();
  80. return;
  81. }
  82. int nPosition = CGetSetOptions::GetQuickPastePosition();
  83. CPoint point;
  84. CRect rcPrev;
  85. CSize csSize;
  86. if(!m_pwndPaste)
  87. m_pwndPaste = new CQPasteWnd;
  88. if(!m_pwndPaste)
  89. {
  90. ASSERT(FALSE);
  91. return;
  92. }
  93. m_pwndPaste->MinMaxWindow(FORCE_MAX);
  94. //If it is a window get the rect otherwise get the saved point and size
  95. if (IsWindow(m_pwndPaste->m_hWnd))
  96. {
  97. m_pwndPaste->GetWindowRect(rcPrev);
  98. csSize = rcPrev.Size();
  99. }
  100. else
  101. {
  102. CGetSetOptions::GetQuickPastePoint(point);
  103. CGetSetOptions::GetQuickPasteSize(csSize);
  104. }
  105. CPoint ptCaret = theApp.m_activeWnd.FocusCaret();
  106. if(ptCaret.x <= 0 || ptCaret.y <= 0)
  107. {
  108. CRect cr;
  109. ::GetWindowRect(theApp.m_activeWnd.ActiveWnd(), cr);
  110. if(cr.Width() > 0 && cr.Height() > 0)
  111. {
  112. ptCaret = cr.CenterPoint();
  113. ptCaret.x -= csSize.cx/2;
  114. ptCaret.y -= csSize.cy/2;
  115. }
  116. else
  117. {
  118. GetCursorPos(&point);
  119. CRect crPoint(point, CSize(1, 1));
  120. int nMonitor = GetMonitorFromRect(crPoint);
  121. if(nMonitor >= 0)
  122. {
  123. CRect crMonitor;
  124. GetMonitorRect(nMonitor, crMonitor);
  125. ptCaret = crMonitor.CenterPoint();
  126. ptCaret.x -= csSize.cx/2;
  127. ptCaret.y -= csSize.cy/2;
  128. }
  129. }
  130. }
  131. if(bAtPrevPos)
  132. {
  133. CGetSetOptions::GetQuickPastePoint(point);
  134. CGetSetOptions::GetQuickPasteSize(csSize);
  135. }
  136. else if(nPosition == POS_AT_CARET)
  137. point = ptCaret;
  138. else if(nPosition == POS_AT_CURSOR)
  139. GetCursorPos(&point);
  140. else if(nPosition == POS_AT_PREVIOUS)
  141. CGetSetOptions::GetQuickPastePoint(point);
  142. if( !IsWindow(m_pwndPaste->m_hWnd) )
  143. {
  144. // Create the window
  145. VERIFY( m_pwndPaste->Create(point, pParent) );
  146. }
  147. CRect crRect = CRect(point, csSize);
  148. if(g_Opt.m_bEnsureEntireWindowCanBeSeen)
  149. EnsureWindowVisible(&crRect);
  150. if((nPosition == POS_AT_CARET) ||
  151. (nPosition == POS_AT_CURSOR) ||
  152. (bAtPrevPos))
  153. {
  154. m_pwndPaste->MoveWindow(crRect);
  155. }
  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();
  202. }
  203. }
  204. BOOL CQuickPaste::IsWindowVisibleEx()
  205. {
  206. if(m_pwndPaste)
  207. return IsWindowVisible(m_pwndPaste->m_hWnd);
  208. return FALSE;
  209. }