CopyThread.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. SyncConfig(); // synchronize with the main thread's copy configuration
  54. // if we are told not to copy on change, then we have nothing to do.
  55. if(!m_LocalConfig.m_bCopyOnChange)
  56. return;
  57. CClip* pClip = new CClip;
  58. CClipTypes* pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  59. bool bDeleteMemory = false;
  60. //If we are copying from a Ditto Buffer then save all to the database, so when we paste this it will paste
  61. //just like you were using Ctrl-V
  62. if(theApp.m_QuickPasteMode == CCP_MainApp::DITTO_BUFFER_QUICK_PASTE)
  63. {
  64. pSupportedTypes = new CClipTypes;
  65. if(pSupportedTypes)
  66. {
  67. bDeleteMemory = true;
  68. COleDataObject oleData;
  69. if(oleData.AttachClipboard())
  70. {
  71. oleData.BeginEnumFormats();
  72. FORMATETC format;
  73. while(oleData.GetNextFormat(&format))
  74. {
  75. pSupportedTypes->Add(format.cfFormat);
  76. }
  77. oleData.Release();
  78. }
  79. }
  80. else
  81. {
  82. pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  83. }
  84. }
  85. bool bResult = pClip->LoadFromClipboard(pSupportedTypes);
  86. if(bDeleteMemory)
  87. {
  88. delete pSupportedTypes;
  89. pSupportedTypes = NULL;
  90. }
  91. if(!bResult)
  92. {
  93. delete pClip;
  94. return; // error
  95. }
  96. AddToClips(pClip);
  97. if(m_LocalConfig.m_bAsyncCopy)
  98. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  99. else
  100. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  101. }
  102. void CCopyThread::SyncConfig()
  103. {
  104. // atomic read
  105. if(m_bConfigChanged)
  106. {
  107. CClipTypes* pTypes = NULL;
  108. Hold();
  109. pTypes = m_LocalConfig.m_pSupportedTypes;
  110. m_LocalConfig = m_SharedConfig;
  111. // NULL means that it shouldn't have been sync'ed
  112. if( m_SharedConfig.m_pSupportedTypes == NULL )
  113. { // let m_LocalConfig keep its types
  114. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  115. pTypes = NULL; // nothing to delete
  116. }
  117. else
  118. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  119. Release();
  120. // delete old types
  121. if( pTypes )
  122. delete pTypes;
  123. }
  124. }
  125. void CCopyThread::AddToClips(CClip* pClip)
  126. {
  127. Hold();
  128. if(!m_pClips)
  129. m_pClips = new CClipList;
  130. m_pClips->AddTail(pClip); // m_pClips now owns pClip
  131. Release();
  132. }
  133. bool CCopyThread::IsClipboardViewerConnected()
  134. {
  135. return m_pClipboardViewer->m_bIsConnected;
  136. }
  137. bool CCopyThread::GetConnectCV()
  138. {
  139. return m_pClipboardViewer->GetConnect();
  140. }
  141. void CCopyThread::SetConnectCV(bool bConnect)
  142. {
  143. ASSERT( m_pClipboardViewer && m_pClipboardViewer->m_hWnd );
  144. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_SETCONNECT, bConnect, 0 );
  145. }
  146. CClipList* CCopyThread::GetClips()
  147. {
  148. Hold();
  149. CClipList* pRet = m_pClips;
  150. m_pClips = NULL;
  151. Release();
  152. return pRet;
  153. }
  154. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  155. {
  156. Hold();
  157. if(m_SharedConfig.m_pSupportedTypes)
  158. {
  159. DELETE_PTR(m_SharedConfig.m_pSupportedTypes);
  160. }
  161. m_SharedConfig.m_pSupportedTypes = pTypes;
  162. m_bConfigChanged = true;
  163. Release();
  164. }
  165. HWND CCopyThread::SetClipHandler(HWND hWnd)
  166. {
  167. Hold();
  168. HWND hRet = m_SharedConfig.m_hClipHandler;
  169. m_SharedConfig.m_hClipHandler = hWnd;
  170. m_bConfigChanged = (hRet != hWnd);
  171. Release();
  172. return hRet;
  173. }
  174. HWND CCopyThread::GetClipHandler()
  175. {
  176. Hold();
  177. HWND hRet = m_SharedConfig.m_hClipHandler;
  178. Release();
  179. return hRet;
  180. }
  181. bool CCopyThread::SetCopyOnChange(bool bVal)
  182. {
  183. Hold();
  184. bool bRet = m_SharedConfig.m_bCopyOnChange;
  185. m_SharedConfig.m_bCopyOnChange = bVal;
  186. m_bConfigChanged = (bRet != bVal);
  187. Release();
  188. return bRet;
  189. }
  190. bool CCopyThread::GetCopyOnChange()
  191. {
  192. Hold();
  193. bool bRet = m_SharedConfig.m_bCopyOnChange;
  194. Release();
  195. return bRet;
  196. }
  197. bool CCopyThread::SetAsyncCopy(bool bVal)
  198. {
  199. Hold();
  200. bool bRet = m_SharedConfig.m_bAsyncCopy;
  201. m_SharedConfig.m_bAsyncCopy = bVal;
  202. m_bConfigChanged = (bRet != bVal);
  203. Release();
  204. return bRet;
  205. }
  206. bool CCopyThread::GetAsyncCopy()
  207. {
  208. Hold();
  209. bool bRet = m_SharedConfig.m_bAsyncCopy;
  210. Release();
  211. return bRet;
  212. }
  213. void CCopyThread::Init(CCopyConfig cfg)
  214. {
  215. ASSERT(m_LocalConfig.m_pSupportedTypes == NULL);
  216. m_LocalConfig = m_SharedConfig = cfg;
  217. // let m_LocalConfig own the m_pSupportedTypes
  218. m_SharedConfig.m_pSupportedTypes = NULL;
  219. }
  220. bool CCopyThread::Quit()
  221. {
  222. m_bQuit = true;
  223. m_pClipboardViewer->PostMessage( WM_QUIT );
  224. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  225. }