ClipboardViewer.cpp 5.7 KB

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