ClipboardViewer.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. m_hNextClipboardViewer(0),
  15. m_bCalling_SetClipboardViewer(false),
  16. m_pHandler(pHandler),
  17. m_bPinging(false),
  18. m_bHandlingClipChange(false),
  19. m_bIsConnected(false),
  20. m_bConnect(false),
  21. m_dwLastCopy(0)
  22. {
  23. }
  24. CClipboardViewer::~CClipboardViewer()
  25. {
  26. }
  27. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  28. //{{AFX_MSG_MAP(CClipboardViewer)
  29. ON_WM_CREATE()
  30. ON_WM_CHANGECBCHAIN()
  31. ON_WM_DRAWCLIPBOARD()
  32. ON_WM_TIMER()
  33. ON_WM_DESTROY()
  34. //}}AFX_MSG_MAP
  35. ON_MESSAGE(WM_SETCONNECT, OnSetConnect)
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CClipboardViewer message handlers
  39. void CClipboardViewer::Create()
  40. {
  41. CString strParentClass = AfxRegisterWndClass(0);
  42. CWnd::CreateEx(0, strParentClass, _T("Ditto Clipboard Viewer"), 0, -1, -1, 0, 0, 0, 0);
  43. SetConnect(true);
  44. }
  45. // connects as a clipboard viewer
  46. void CClipboardViewer::Connect()
  47. {
  48. Log(_T("Connect to Clipboard"));
  49. //Set up the clip board viewer
  50. m_bCalling_SetClipboardViewer = true;
  51. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  52. m_bCalling_SetClipboardViewer = false;
  53. m_bIsConnected = true;
  54. m_bConnect = true;
  55. SetEnsureConnectedTimer();
  56. }
  57. void CClipboardViewer::SetEnsureConnectedTimer()
  58. {
  59. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE*5, NULL);
  60. }
  61. // disconnects as a clipboard viewer
  62. void CClipboardViewer::Disconnect(bool bSendPing)
  63. {
  64. Log(_T("Disconnect From Clipboard"));
  65. KillTimer(TIMER_ENSURE_VIEWER_IN_CHAIN);
  66. BOOL bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  67. if(!bRet)
  68. {
  69. Log(_T("Error disconnecting from clipboard"));
  70. bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  71. if(!bRet)
  72. {
  73. Log(_T("Error disconnecting from clipboard2"));
  74. }
  75. }
  76. m_hNextClipboardViewer = 0;
  77. m_bConnect = false;
  78. m_bIsConnected = false;
  79. if(bSendPing)
  80. SendPing();
  81. }
  82. void CClipboardViewer::SendPing()
  83. {
  84. if(g_Opt.m_bEnsureConnectToClipboard)
  85. {
  86. if(OpenClipboard())
  87. {
  88. m_bPinging = true;
  89. SetClipboardData(theApp.m_PingFormat, NewGlobalP("Ditto Ping", sizeof("Ditto Ping")));
  90. SetClipboardData(theApp.m_cfIgnoreClipboard , NewGlobalP("Ignore", sizeof("Ignore")));
  91. SetTimer(TIMER_PING, 2000, NULL);
  92. CloseClipboard();
  93. }
  94. }
  95. }
  96. void CClipboardViewer::SetConnect(bool bConnect)
  97. {
  98. m_bConnect = bConnect;
  99. if(bConnect)
  100. {
  101. if(m_bIsConnected == false)
  102. {
  103. Connect();
  104. }
  105. else
  106. {
  107. SendPing();
  108. }
  109. }
  110. else
  111. {
  112. Disconnect();
  113. }
  114. }
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CClipboardViewer message handlers
  117. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  118. {
  119. if(CWnd::OnCreate(lpCreateStruct) == -1)
  120. return -1;
  121. //Set up the clip board viewer
  122. Connect();
  123. return 0;
  124. }
  125. void CClipboardViewer::OnDestroy()
  126. {
  127. Disconnect();
  128. CWnd::OnDestroy();
  129. }
  130. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  131. {
  132. Log(_T("OnChangeCbChain"));
  133. // If the next window is closing, repair the chain.
  134. if(m_hNextClipboardViewer == hWndRemove)
  135. {
  136. m_hNextClipboardViewer = hWndAfter;
  137. }
  138. // Otherwise, pass the message to the next link.
  139. else if (m_hNextClipboardViewer != NULL)
  140. {
  141. if(m_hNextClipboardViewer != m_hWnd)
  142. {
  143. ::SendMessage(m_hNextClipboardViewer, WM_CHANGECBCHAIN, (WPARAM) hWndRemove, (LPARAM) hWndAfter);
  144. }
  145. else
  146. {
  147. m_hNextClipboardViewer = NULL;
  148. }
  149. }
  150. }
  151. //Message that the clipboard data has changed
  152. void CClipboardViewer::OnDrawClipboard()
  153. {
  154. if(::IsClipboardFormatAvailable(theApp.m_PingFormat))
  155. {
  156. m_bPinging = false;
  157. return;
  158. }
  159. // don't process the event when we first attach
  160. if(m_pHandler && !m_bCalling_SetClipboardViewer)
  161. {
  162. if(m_bIsConnected)
  163. {
  164. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  165. {
  166. Log(StrF(_T("OnDrawClipboard:: *** SetTimer *** %d"), GetTickCount()));
  167. KillTimer(TIMER_DRAW_CLIPBOARD);
  168. SetTimer(TIMER_DRAW_CLIPBOARD, g_Opt.m_lProcessDrawClipboardDelay, NULL);
  169. }
  170. }
  171. else
  172. {
  173. Log(_T("Not connected, ignore clipboard change"));
  174. }
  175. }
  176. // pass the event to the next Clipboard viewer in the chain
  177. if(m_hNextClipboardViewer != NULL)
  178. {
  179. if(m_hNextClipboardViewer != m_hWnd)
  180. {
  181. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  182. }
  183. else
  184. {
  185. m_hNextClipboardViewer = NULL;
  186. }
  187. }
  188. }
  189. void CClipboardViewer::OnTimer(UINT nIDEvent)
  190. {
  191. switch(nIDEvent)
  192. {
  193. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  194. SendPing();
  195. break;
  196. case TIMER_DRAW_CLIPBOARD:
  197. KillTimer(nIDEvent);
  198. if(m_bHandlingClipChange == false)
  199. {
  200. m_bHandlingClipChange = true;
  201. DWORD dwNow = GetTickCount();
  202. if(dwNow - m_dwLastCopy > g_Opt.m_dwSaveClipDelay || m_dwLastCopy > dwNow)
  203. {
  204. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  205. {
  206. Log(StrF(_T("OnDrawClipboard::OnTimer %d"), dwNow));
  207. m_pHandler->OnClipboardChange();
  208. m_dwLastCopy = dwNow;
  209. }
  210. }
  211. else
  212. {
  213. Log(StrF(_T("Clip copy to fast difference from last copy = %d"), (dwNow - m_dwLastCopy)));
  214. }
  215. m_bHandlingClipChange = false;
  216. }
  217. else
  218. {
  219. Log(_T("HandlingClipChange is Set, ERROR"));
  220. }
  221. case TIMER_PING:
  222. KillTimer(TIMER_PING);
  223. //If we haven't received the change clipboard message then we are disconnected
  224. //if so reconnect
  225. if(m_bPinging)
  226. {
  227. if(m_bConnect)
  228. {
  229. Log(_T("Ping Failed Reconnecting to clipboard"));
  230. Connect();
  231. }
  232. else
  233. {
  234. Log(_T("Ping Failed but Connected set to FALSE so this is ok"));
  235. }
  236. }
  237. else
  238. {
  239. if(m_bConnect)
  240. {
  241. m_bIsConnected = true;
  242. }
  243. }
  244. break;
  245. }
  246. CWnd::OnTimer(nIDEvent);
  247. }
  248. LRESULT CClipboardViewer::OnSetConnect(WPARAM wParam, LPARAM lParam)
  249. {
  250. bool bConnect = wParam == TRUE;
  251. SetConnect(bConnect);
  252. return TRUE;
  253. }