ClipboardViewer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. Log(StrF("OnChangeCbChain Removed = %d After = %d", hWndAfter, hWndAfter));
  125. // If the next window is closing, repair the chain.
  126. if(m_hNextClipboardViewer == hWndRemove)
  127. {
  128. m_hNextClipboardViewer = hWndAfter;
  129. }
  130. // Otherwise, pass the message to the next link.
  131. else if (m_hNextClipboardViewer != NULL)
  132. {
  133. if(m_hNextClipboardViewer != m_hWnd)
  134. {
  135. Log(StrF("OnChangeCbChain Sending WM_CHANGECBCHAIN to %d", m_hNextClipboardViewer));
  136. ::SendMessage(m_hNextClipboardViewer, WM_CHANGECBCHAIN, (WPARAM) hWndRemove, (LPARAM) hWndAfter);
  137. }
  138. else
  139. {
  140. m_hNextClipboardViewer = NULL;
  141. }
  142. }
  143. }
  144. //Message that the clipboard data has changed
  145. void CClipboardViewer::OnDrawClipboard()
  146. {
  147. if(m_bPinging)
  148. {
  149. m_bPingSuccess = true;
  150. return;
  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. {
  157. Log(StrF("OnDrawClipboard::SetTimer %d", GetTickCount()));
  158. KillTimer(TIMER_DRAW_CLIPBOARD);
  159. SetTimer(TIMER_DRAW_CLIPBOARD, g_Opt.m_lProcessDrawClipboardDelay, NULL);
  160. }
  161. }
  162. // pass the event to the next Clipboard viewer in the chain
  163. if(m_hNextClipboardViewer != NULL)
  164. {
  165. if(m_hNextClipboardViewer != m_hWnd)
  166. {
  167. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  168. }
  169. else
  170. {
  171. m_hNextClipboardViewer = NULL;
  172. }
  173. }
  174. }
  175. void CClipboardViewer::OnTimer(UINT nIDEvent)
  176. {
  177. switch(nIDEvent)
  178. {
  179. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  180. EnsureConnected();
  181. break;
  182. case TIMER_DRAW_CLIPBOARD:
  183. KillTimer(nIDEvent);
  184. if((GetTickCount() - m_lLastCopy) > g_Opt.m_lSaveClipDelay)
  185. {
  186. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  187. {
  188. Log(StrF("OnDrawClipboard::OnTimer %d", GetTickCount()));
  189. m_pHandler->OnClipboardChange();
  190. m_lLastCopy = GetTickCount();
  191. }
  192. }
  193. else
  194. {
  195. Log(StrF("Clip copy to fast difference from last copy = %d", (GetTickCount() - m_lLastCopy)));
  196. }
  197. break;
  198. }
  199. CWnd::OnTimer(nIDEvent);
  200. }
  201. LRESULT CClipboardViewer::OnCVGetConnect(WPARAM wParam, LPARAM lParam)
  202. {
  203. return GetConnect();
  204. }
  205. LRESULT CClipboardViewer::OnCVSetConnect(WPARAM wParam, LPARAM lParam)
  206. {
  207. SetConnect(wParam != FALSE); // convert to bool
  208. return TRUE;
  209. }
  210. LRESULT CClipboardViewer::OnCVIsConnected(WPARAM wParam, LPARAM lParam)
  211. {
  212. return SendPing();
  213. }