ClipboardViewer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // ClipboardViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "ClipboardViewer.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CClipboardViewer
  13. CClipboardViewer::CClipboardViewer(CCopyThread* pHandler)
  14. {
  15. m_hNextClipboardViewer = 0;
  16. m_bCalling_SetClipboardViewer = false;
  17. m_lReconectCount = 0;
  18. m_bIsConnected = false;
  19. m_bConnect = false;
  20. m_pHandler = pHandler;
  21. ASSERT(m_pHandler);
  22. m_bPinging = false;
  23. m_bPingSuccess = false;
  24. }
  25. CClipboardViewer::~CClipboardViewer()
  26. {
  27. }
  28. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  29. //{{AFX_MSG_MAP(CClipboardViewer)
  30. ON_WM_CREATE()
  31. ON_WM_CHANGECBCHAIN()
  32. ON_WM_DRAWCLIPBOARD()
  33. ON_WM_TIMER()
  34. ON_WM_DESTROY()
  35. //}}AFX_MSG_MAP
  36. ON_MESSAGE(WM_CV_GETCONNECT, OnCVGetConnect)
  37. ON_MESSAGE(WM_CV_SETCONNECT, OnCVSetConnect)
  38. ON_MESSAGE(WM_CV_IS_CONNECTED, OnCVIsConnected)
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CClipboardViewer message handlers
  42. void CClipboardViewer::Create()
  43. {
  44. CString strParentClass = AfxRegisterWndClass(0);
  45. CWnd::CreateEx(0, strParentClass, "Ditto Clipboard Viewer", 0, -1, -1, 0, 0, 0, 0);
  46. SetConnect( true );
  47. }
  48. // connects as a clipboard viewer
  49. void CClipboardViewer::Connect()
  50. {
  51. //Set up the clip board viewer
  52. m_bCalling_SetClipboardViewer = true;
  53. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  54. m_bCalling_SetClipboardViewer = false;
  55. m_bIsConnected = SendPing();
  56. // verify that we are in the chain every minute
  57. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE, 0);
  58. }
  59. // disconnects as a clipboard viewer
  60. void CClipboardViewer::Disconnect()
  61. {
  62. KillTimer(TIMER_ENSURE_VIEWER_IN_CHAIN);
  63. CWnd::ChangeClipboardChain( m_hNextClipboardViewer );
  64. m_hNextClipboardViewer = 0;
  65. m_bIsConnected = false;
  66. }
  67. bool CClipboardViewer::SendPing()
  68. {
  69. HWND hWnd;
  70. bool bResult = false;
  71. hWnd = ::GetClipboardViewer();
  72. // if there is a chain
  73. if(::IsWindow(hWnd))
  74. {
  75. m_bPingSuccess = false;
  76. m_bPinging = true;
  77. ::SendMessage(hWnd, WM_DRAWCLIPBOARD, 0, 0);
  78. m_bPinging = false;
  79. bResult = m_bPingSuccess;
  80. }
  81. m_bIsConnected = bResult;
  82. return bResult;
  83. }
  84. bool CClipboardViewer::EnsureConnected()
  85. {
  86. if(!SendPing())
  87. Connect();
  88. return m_bIsConnected;
  89. }
  90. // puts format "Clipboard Viewer Ignore" on the clipboard
  91. void CClipboardViewer::SetCVIgnore()
  92. {
  93. if(::OpenClipboard(m_hWnd))
  94. {
  95. ::SetClipboardData(theApp.m_cfIgnoreClipboard, NULL);
  96. ::CloseClipboard();
  97. }
  98. }
  99. void CClipboardViewer::SetConnect( bool bConnect )
  100. {
  101. m_bConnect = bConnect;
  102. if(m_bConnect)
  103. EnsureConnected();
  104. else
  105. Disconnect();
  106. }
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CClipboardViewer message handlers
  109. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  110. {
  111. if(CWnd::OnCreate(lpCreateStruct) == -1)
  112. return -1;
  113. //Set up the clip board viewer
  114. Connect();
  115. return 0;
  116. }
  117. void CClipboardViewer::OnDestroy()
  118. {
  119. Disconnect();
  120. CWnd::OnDestroy();
  121. }
  122. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  123. {
  124. // If the next window is closing, repair the chain.
  125. if(m_hNextClipboardViewer == hWndRemove)
  126. {
  127. m_hNextClipboardViewer = hWndAfter;
  128. }
  129. // Otherwise, pass the message to the next link.
  130. else if (m_hNextClipboardViewer != NULL)
  131. {
  132. if(m_hNextClipboardViewer != m_hWnd)
  133. {
  134. ::SendMessage(m_hNextClipboardViewer, WM_CHANGECBCHAIN, (WPARAM) hWndRemove, (LPARAM) hWndAfter);
  135. }
  136. else
  137. {
  138. m_hNextClipboardViewer = NULL;
  139. }
  140. }
  141. }
  142. //Message that the clipboard data has changed
  143. void CClipboardViewer::OnDrawClipboard()
  144. {
  145. if( m_bPinging )
  146. {
  147. m_bPingSuccess = true;
  148. return;
  149. }
  150. if((GetTickCount() - m_lLastCopy) > g_Opt.m_lSaveClipDelay)
  151. {
  152. // don't process the event when we first attach
  153. if( m_pHandler && !m_bCalling_SetClipboardViewer )
  154. {
  155. if( !::IsClipboardFormatAvailable( theApp.m_cfIgnoreClipboard ) )
  156. m_pHandler->OnClipboardChange();
  157. }
  158. }
  159. else
  160. {
  161. CString cs;
  162. cs.Format("Clip copy to fast difference from last copy = %d", (GetTickCount() - m_lLastCopy));
  163. Log(cs);
  164. }
  165. // pass the event to the next Clipboard viewer in the chain
  166. if( m_hNextClipboardViewer != NULL )
  167. {
  168. if(m_hNextClipboardViewer != m_hWnd)
  169. {
  170. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  171. }
  172. else
  173. {
  174. m_hNextClipboardViewer = NULL;
  175. }
  176. }
  177. }
  178. void CClipboardViewer::OnTimer(UINT nIDEvent)
  179. {
  180. switch(nIDEvent)
  181. {
  182. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  183. EnsureConnected();
  184. break;
  185. }
  186. CWnd::OnTimer(nIDEvent);
  187. }
  188. LRESULT CClipboardViewer::OnCVGetConnect(WPARAM wParam, LPARAM lParam)
  189. {
  190. return GetConnect();
  191. }
  192. LRESULT CClipboardViewer::OnCVSetConnect(WPARAM wParam, LPARAM lParam)
  193. {
  194. SetConnect(wParam != FALSE); // convert to bool
  195. return TRUE;
  196. }
  197. LRESULT CClipboardViewer::OnCVIsConnected(WPARAM wParam, LPARAM lParam)
  198. {
  199. return SendPing();
  200. }