CopyThread.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // CopyThread.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "CopyThread.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CCopyThread
  13. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  14. CCopyThread::CCopyThread():
  15. m_bQuit(false),
  16. m_bConfigChanged(false),
  17. m_pClips(NULL),
  18. m_pClipboardViewer(NULL)
  19. {
  20. m_bAutoDelete = false,
  21. ::InitializeCriticalSection(&m_CS);
  22. }
  23. CCopyThread::~CCopyThread()
  24. {
  25. m_LocalConfig.DeleteTypes();
  26. m_SharedConfig.DeleteTypes();
  27. DELETE_PTR(m_pClipboardViewer);
  28. DELETE_PTR(m_pClips);
  29. ::DeleteCriticalSection(&m_CS);
  30. }
  31. BOOL CCopyThread::InitInstance()
  32. {
  33. m_pClipboardViewer = new CClipboardViewer(this);
  34. // the window is created within this thread and therefore uses its message queue
  35. m_pClipboardViewer->Create();
  36. return TRUE;
  37. }
  38. int CCopyThread::ExitInstance()
  39. {
  40. m_pClipboardViewer->Disconnect(false);
  41. return CWinThread::ExitInstance();
  42. }
  43. BEGIN_MESSAGE_MAP(CCopyThread, CWinThread)
  44. //{{AFX_MSG_MAP(CCopyThread)
  45. // NOTE - the ClassWizard will add and remove mapping macros here.
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CCopyThread message handlers
  50. // Called within Copy Thread:
  51. void CCopyThread::OnClipboardChange()
  52. {
  53. Log(_T("OnClipboardChange - Start"));
  54. SyncConfig(); // synchronize with the main thread's copy configuration
  55. // if we are told not to copy on change, then we have nothing to do.
  56. if(!m_LocalConfig.m_bCopyOnChange)
  57. return;
  58. CClip* pClip = new CClip;
  59. CClipTypes* pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  60. bool bDeleteMemory = false;
  61. //If we are copying from a Ditto Buffer then save all to the database, so when we paste this it will paste
  62. //just like you were using Ctrl-V
  63. if(theApp.m_CopyBuffer.Active())
  64. {
  65. Log(_T("LoadFromClipboard - Copy buffer Active Start"));
  66. pSupportedTypes = new CClipTypes;
  67. if(pSupportedTypes)
  68. {
  69. bDeleteMemory = true;
  70. COleDataObject oleData;
  71. if(oleData.AttachClipboard())
  72. {
  73. oleData.BeginEnumFormats();
  74. FORMATETC format;
  75. while(oleData.GetNextFormat(&format))
  76. {
  77. pSupportedTypes->Add(format.cfFormat);
  78. }
  79. oleData.Release();
  80. }
  81. }
  82. else
  83. {
  84. pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  85. }
  86. Log(_T("LoadFromClipboard - Copy buffer Active End"));
  87. }
  88. Log(_T("LoadFromClipboard - Before"));
  89. bool bResult = pClip->LoadFromClipboard(pSupportedTypes);
  90. Log(_T("LoadFromClipboard - End"));
  91. if(bDeleteMemory)
  92. {
  93. delete pSupportedTypes;
  94. pSupportedTypes = NULL;
  95. }
  96. if(!bResult)
  97. {
  98. delete pClip;
  99. return; // error
  100. }
  101. AddToClips(pClip);
  102. if(m_LocalConfig.m_bAsyncCopy)
  103. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  104. else
  105. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  106. Log(_T("OnClipboardChange - End"));
  107. }
  108. void CCopyThread::SyncConfig()
  109. {
  110. // atomic read
  111. if(m_bConfigChanged)
  112. {
  113. CClipTypes* pTypes = NULL;
  114. Hold();
  115. pTypes = m_LocalConfig.m_pSupportedTypes;
  116. m_LocalConfig = m_SharedConfig;
  117. // NULL means that it shouldn't have been sync'ed
  118. if( m_SharedConfig.m_pSupportedTypes == NULL )
  119. { // let m_LocalConfig keep its types
  120. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  121. pTypes = NULL; // nothing to delete
  122. }
  123. else
  124. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  125. Release();
  126. // delete old types
  127. if( pTypes )
  128. delete pTypes;
  129. }
  130. }
  131. void CCopyThread::AddToClips(CClip* pClip)
  132. {
  133. Hold();
  134. if(!m_pClips)
  135. m_pClips = new CClipList;
  136. m_pClips->AddTail(pClip); // m_pClips now owns pClip
  137. Release();
  138. }
  139. bool CCopyThread::IsClipboardViewerConnected()
  140. {
  141. return m_pClipboardViewer->m_bIsConnected;
  142. }
  143. bool CCopyThread::GetConnectCV()
  144. {
  145. return m_pClipboardViewer->GetConnect();
  146. }
  147. void CCopyThread::SetConnectCV(bool bConnect)
  148. {
  149. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  150. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_SETCONNECT, bConnect, 0 );
  151. }
  152. CClipList* CCopyThread::GetClips()
  153. {
  154. Hold();
  155. CClipList* pRet = m_pClips;
  156. m_pClips = NULL;
  157. Release();
  158. return pRet;
  159. }
  160. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  161. {
  162. Hold();
  163. if(m_SharedConfig.m_pSupportedTypes)
  164. {
  165. DELETE_PTR(m_SharedConfig.m_pSupportedTypes);
  166. }
  167. m_SharedConfig.m_pSupportedTypes = pTypes;
  168. m_bConfigChanged = true;
  169. Release();
  170. }
  171. HWND CCopyThread::SetClipHandler(HWND hWnd)
  172. {
  173. Hold();
  174. HWND hRet = m_SharedConfig.m_hClipHandler;
  175. m_SharedConfig.m_hClipHandler = hWnd;
  176. m_bConfigChanged = (hRet != hWnd);
  177. Release();
  178. return hRet;
  179. }
  180. HWND CCopyThread::GetClipHandler()
  181. {
  182. Hold();
  183. HWND hRet = m_SharedConfig.m_hClipHandler;
  184. Release();
  185. return hRet;
  186. }
  187. bool CCopyThread::SetCopyOnChange(bool bVal)
  188. {
  189. Hold();
  190. bool bRet = m_SharedConfig.m_bCopyOnChange;
  191. m_SharedConfig.m_bCopyOnChange = bVal;
  192. m_bConfigChanged = (bRet != bVal);
  193. Release();
  194. return bRet;
  195. }
  196. bool CCopyThread::GetCopyOnChange()
  197. {
  198. Hold();
  199. bool bRet = m_SharedConfig.m_bCopyOnChange;
  200. Release();
  201. return bRet;
  202. }
  203. bool CCopyThread::SetAsyncCopy(bool bVal)
  204. {
  205. Hold();
  206. bool bRet = m_SharedConfig.m_bAsyncCopy;
  207. m_SharedConfig.m_bAsyncCopy = bVal;
  208. m_bConfigChanged = (bRet != bVal);
  209. Release();
  210. return bRet;
  211. }
  212. bool CCopyThread::GetAsyncCopy()
  213. {
  214. Hold();
  215. bool bRet = m_SharedConfig.m_bAsyncCopy;
  216. Release();
  217. return bRet;
  218. }
  219. void CCopyThread::Init(CCopyConfig cfg)
  220. {
  221. ASSERT(m_LocalConfig.m_pSupportedTypes == NULL);
  222. m_LocalConfig = m_SharedConfig = cfg;
  223. // let m_LocalConfig own the m_pSupportedTypes
  224. m_SharedConfig.m_pSupportedTypes = NULL;
  225. }
  226. bool CCopyThread::Quit()
  227. {
  228. m_bQuit = true;
  229. m_pClipboardViewer->PostMessage( WM_QUIT );
  230. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  231. }