CopyThread.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #if !defined(AFX_COPYTHREAD_H__C6766F04_0111_4314_986A_A0E02FF3B322__INCLUDED_)
  2. #define AFX_COPYTHREAD_H__C6766F04_0111_4314_986A_A0E02FF3B322__INCLUDED_
  3. #if _MSC_VER > 1000
  4. #pragma once
  5. #endif // _MSC_VER > 1000
  6. #include "ClipboardViewer.h"
  7. struct CCopyConfig
  8. {
  9. public:
  10. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  11. HWND m_hClipHandler;
  12. // true to use PostMessage (asynchronous)
  13. // false to use SendMessage (synchronous)
  14. bool m_bAsyncCopy;
  15. // true to create a copy of the clipboard contents when it changes
  16. // false to ignore changes in the clipboard
  17. bool m_bCopyOnChange;
  18. // the supported types which are copied from the clipboard when it changes.
  19. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  20. CCopyConfig( HWND hClipHandler = NULL,
  21. bool bAsyncCopy = false,
  22. bool bCopyOnChange = false,
  23. CClipTypes* pSupportedTypes = NULL )
  24. {
  25. m_hClipHandler = hClipHandler;
  26. m_bAsyncCopy = bAsyncCopy;
  27. m_bCopyOnChange = bCopyOnChange;
  28. m_pSupportedTypes = pSupportedTypes;
  29. }
  30. void DeleteTypes()
  31. {
  32. if( m_pSupportedTypes )
  33. {
  34. delete m_pSupportedTypes;
  35. m_pSupportedTypes = NULL;
  36. }
  37. }
  38. };
  39. class CCopyThread : public CWinThread
  40. {
  41. DECLARE_DYNCREATE(CCopyThread)
  42. public:
  43. CCopyThread();
  44. virtual ~CCopyThread();
  45. // Attributes
  46. public:
  47. // Operations
  48. public:
  49. bool m_bQuit;
  50. // critical section is held whenever shared data is changed
  51. CRITICAL_SECTION m_CS;
  52. void Hold() { ::EnterCriticalSection(&m_CS); }
  53. void Release() { ::LeaveCriticalSection(&m_CS); }
  54. // CopyThread Local (accessed from this CopyThread)
  55. // window owned by this thread which handles clipboard viewer messages
  56. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  57. CCopyConfig m_LocalConfig;
  58. // Called within Copy Thread:
  59. void OnClipboardChange(); // called by ClipboardViewer
  60. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  61. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  62. // Shared (use thread-safe access functions below)
  63. CCopyConfig m_SharedConfig;
  64. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  65. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  66. // Called within Main thread:
  67. bool IsClipboardViewerConnected();
  68. bool GetConnectCV();
  69. void SetConnectCV( bool bConnect );
  70. CClipList* GetClips(); // caller owns the returned CClipList
  71. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  72. HWND SetClipHandler( HWND hWnd ); // returns previous value
  73. HWND GetClipHandler();
  74. bool SetCopyOnChange( bool bVal ); // returns previous value
  75. bool GetCopyOnChange();
  76. bool SetAsyncCopy( bool bVal ); // returns previous value
  77. bool GetAsyncCopy();
  78. // Main thread
  79. void Init( CCopyConfig cfg );
  80. bool Quit();
  81. // Overrides
  82. // ClassWizard generated virtual function overrides
  83. //{{AFX_VIRTUAL(CCopyThread)
  84. public:
  85. virtual BOOL InitInstance();
  86. virtual int ExitInstance();
  87. //}}AFX_VIRTUAL
  88. // Generated message map functions
  89. //{{AFX_MSG(CCopyThread)
  90. // NOTE - the ClassWizard will add and remove member functions here.
  91. //}}AFX_MSG
  92. DECLARE_MESSAGE_MAP()
  93. };
  94. /////////////////////////////////////////////////////////////////////////////
  95. //{{AFX_INSERT_LOCATION}}
  96. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  97. #endif // !defined(AFX_COPYTHREAD_H__C6766F04_0111_4314_986A_A0E02FF3B322__INCLUDED_)