ClipboardViewer.cpp 5.7 KB

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