ProcessCopy.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // ProcessCopy.h: classes for saving the clipboard to db
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)
  5. #define AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include <afxole.h>
  10. #include <afxtempl.h>
  11. #include "MainTable.h"
  12. class CClip;
  13. class CCopyThread;
  14. typedef CArray<CLIPFORMAT, CLIPFORMAT> CClipTypes;
  15. /*----------------------------------------------------------------------------*\
  16. COleDataObjectEx
  17. \*----------------------------------------------------------------------------*/
  18. class COleDataObjectEx : public COleDataObject
  19. {
  20. public:
  21. // creates global from IStream if necessary
  22. HGLOBAL GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc = NULL);
  23. };
  24. /*----------------------------------------------------------------------------*\
  25. CClipFormat - holds the data of one clip format.
  26. \*----------------------------------------------------------------------------*/
  27. class CClipFormat
  28. {
  29. public:
  30. CLIPFORMAT m_cfType;
  31. HGLOBAL m_hgData;
  32. bool bDeleteData;
  33. CClipFormat(CLIPFORMAT cfType = 0, HGLOBAL hgData = 0);
  34. ~CClipFormat();
  35. void Clear();
  36. void Free();
  37. };
  38. /*----------------------------------------------------------------------------*\
  39. CClipFormats - holds an array of CClipFormat
  40. \*----------------------------------------------------------------------------*/
  41. class CClipFormats : public CArray<CClipFormat,CClipFormat&>
  42. {
  43. public:
  44. // returns a pointer to the CClipFormat in this array which matches the given type
  45. // or NULL if that type doesn't exist in this array.
  46. CClipFormat* FindFormat( UINT cfType );
  47. };
  48. /*----------------------------------------------------------------------------*\
  49. CClip - holds multiple CClipFormats and clip statistics
  50. - provides static functions for manipulating a Clip as a single unit.
  51. \*----------------------------------------------------------------------------*/
  52. class CClip
  53. {
  54. public:
  55. long m_ID; // 0 if it hasn't yet been saved or is unknown
  56. long m_DataID;
  57. CClipFormats m_Formats; // actual format data
  58. const CClip& operator=(const CClip &clip);
  59. // statistics assigned by LoadFromClipboard
  60. CTime m_Time; // time copied from clipboard
  61. CString m_Desc;
  62. ULONG m_lTotalCopySize;
  63. CClip();
  64. ~CClip();
  65. void Clear();
  66. void EmptyFormats();
  67. // Adds a new Format to this Clip by copying the given data.
  68. bool AddFormat( CLIPFORMAT cfType, void* pData, UINT nLen );
  69. // Fills this CClip with the contents of the clipboard.
  70. bool LoadFromClipboard( CClipTypes* pClipTypes );
  71. bool SetDescFromText( HGLOBAL hgData );
  72. bool SetDescFromType();
  73. // Immediately save this clip to the db (empties m_Formats due to AddToDataTable).
  74. bool AddToDB( bool bCheckForDuplicates = true );
  75. bool AddToMainTable(); // assigns m_ID
  76. bool AddToDataTable(); // Empties m_Formats as it saves them to the Data Table.
  77. // if a duplicate exists, set recset to the duplicate and return true
  78. bool FindDuplicate( CMainTable& recset, BOOL bCheckLastOnly = FALSE );
  79. int CompareFormatDataTo( long lDataID );
  80. // changes m_Time to be later than the latest clip entry in the db
  81. void MakeLatestTime();
  82. // STATICS
  83. // Allocates a Global containing the requested Clip's Format Data
  84. static HGLOBAL LoadFormat( long lID, UINT cfType );
  85. // Fills "formats" with the Data of all Formats in the db for the given Clip ID
  86. static bool LoadFormats( long lID, CClipFormats& formats );
  87. // Fills "types" with all Types in the db for the given Clip ID
  88. static void LoadTypes( long lID, CClipTypes& types );
  89. };
  90. /*----------------------------------------------------------------------------*\
  91. CClipList
  92. \*----------------------------------------------------------------------------*/
  93. class CClipList : public CList<CClip*,CClip*>
  94. {
  95. public:
  96. ~CClipList();
  97. // returns the number of clips actually saved
  98. // while this does empty the Format Data, it does not delete the Clips.
  99. int AddToDB( bool bLatestTime = false, bool bShowStatus = true );
  100. const CClipList& operator=(const CClipList &cliplist);
  101. };
  102. /*----------------------------------------------------------------------------*\
  103. CClipboardViewer
  104. \*----------------------------------------------------------------------------*/
  105. #define TIMER_ENSURE_VIEWER_IN_CHAIN 6
  106. class CClipboardViewer : public CWnd
  107. {
  108. protected:
  109. DECLARE_DYNAMIC(CClipboardViewer)
  110. public:
  111. // Overrides
  112. // ClassWizard generated virtual function overrides
  113. //{{AFX_VIRTUAL(CMainFrame)
  114. //}}AFX_VIRTUAL
  115. // Implementation
  116. CClipboardViewer( CCopyThread* pHandler );
  117. virtual ~CClipboardViewer();
  118. void Create();
  119. HWND m_hNextClipboardViewer;
  120. bool m_bCalling_SetClipboardViewer;
  121. long m_lReconectCount;
  122. bool m_bIsConnected; // a cache of the last known state
  123. bool m_bConnect; // the user's requested state for the viewer.
  124. // m_bConnect and m_bIsConnected can differ if, e.g., we want to stay
  125. // connected, but are dropped from the chain for some unknown reason.
  126. // m_pHandler->OnClipboardChange is called when the clipboard changes.
  127. CCopyThread* m_pHandler;
  128. void Connect(); // connects as a clipboard viewer
  129. void Disconnect(); // disconnects as a clipboard viewer
  130. bool m_bPinging;
  131. bool m_bPingSuccess;
  132. bool SendPing(); // returns true if we are in the chain
  133. bool EnsureConnected(); // pings and connects if ping fails
  134. void SetCVIgnore(); // puts format "Clipboard Viewer Ignore" on the clipboard
  135. bool GetConnect() { return m_bConnect; }
  136. void SetConnect( bool bConnect );
  137. // Generated message map functions
  138. protected:
  139. //{{AFX_MSG(CMainFrame)
  140. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  141. afx_msg void OnDestroy();
  142. afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter);
  143. afx_msg void OnDrawClipboard();
  144. afx_msg void OnTimer(UINT nIDEvent);
  145. //}}AFX_MSG
  146. afx_msg LRESULT OnCVGetConnect(WPARAM wParam, LPARAM lParam);
  147. afx_msg LRESULT OnCVSetConnect(WPARAM wParam, LPARAM lParam);
  148. afx_msg LRESULT OnCVIsConnected(WPARAM wParam, LPARAM lParam);
  149. DECLARE_MESSAGE_MAP()
  150. };
  151. /*----------------------------------------------------------------------------*\
  152. CCopyConfig - used with CCopyThread for efficient inter-thread communication
  153. \*----------------------------------------------------------------------------*/
  154. struct CCopyConfig
  155. {
  156. public:
  157. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  158. HWND m_hClipHandler;
  159. // true to use PostMessage (asynchronous)
  160. // false to use SendMessage (synchronous)
  161. bool m_bAsyncCopy;
  162. // true to create a copy of the clipboard contents when it changes
  163. // false to ignore changes in the clipboard
  164. bool m_bCopyOnChange;
  165. // the supported types which are copied from the clipboard when it changes.
  166. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  167. CCopyConfig( HWND hClipHandler = NULL,
  168. bool bAsyncCopy = false,
  169. bool bCopyOnChange = false,
  170. CClipTypes* pSupportedTypes = NULL )
  171. {
  172. m_hClipHandler = hClipHandler;
  173. m_bAsyncCopy = bAsyncCopy;
  174. m_bCopyOnChange = bCopyOnChange;
  175. m_pSupportedTypes = pSupportedTypes;
  176. }
  177. void DeleteTypes()
  178. {
  179. if( m_pSupportedTypes )
  180. {
  181. delete m_pSupportedTypes;
  182. m_pSupportedTypes = NULL;
  183. }
  184. }
  185. };
  186. /*----------------------------------------------------------------------------*\
  187. CCopyThread
  188. \*----------------------------------------------------------------------------*/
  189. class CCopyThread : public CWinThread
  190. {
  191. DECLARE_DYNCREATE(CCopyThread)
  192. public:
  193. //protected:
  194. CCopyThread(); // protected constructor used by dynamic creation
  195. virtual ~CCopyThread();
  196. public:
  197. bool m_bQuit;
  198. virtual BOOL InitInstance();
  199. virtual int ExitInstance();
  200. // critical section is held whenever shared data is changed
  201. CRITICAL_SECTION m_CS;
  202. void Hold() { ::EnterCriticalSection(&m_CS); }
  203. void Release() { ::LeaveCriticalSection(&m_CS); }
  204. // CopyThread Local (accessed from this CopyThread)
  205. // window owned by this thread which handles clipboard viewer messages
  206. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  207. CCopyConfig m_LocalConfig;
  208. // Called within Copy Thread:
  209. void OnClipboardChange(); // called by ClipboardViewer
  210. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  211. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  212. // Shared (use thread-safe access functions below)
  213. CCopyConfig m_SharedConfig;
  214. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  215. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  216. // Called within Main thread:
  217. bool IsClipboardViewerConnected();
  218. bool GetConnectCV();
  219. void SetConnectCV( bool bConnect );
  220. CClipList* GetClips(); // caller owns the returned CClipList
  221. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  222. HWND SetClipHandler( HWND hWnd ); // returns previous value
  223. HWND GetClipHandler();
  224. bool SetCopyOnChange( bool bVal ); // returns previous value
  225. bool GetCopyOnChange();
  226. bool SetAsyncCopy( bool bVal ); // returns previous value
  227. bool GetAsyncCopy();
  228. // Main thread
  229. void Init( CCopyConfig cfg );
  230. bool Quit();
  231. protected:
  232. DECLARE_MESSAGE_MAP()
  233. };
  234. /*----------------------------------------------------------------------------*\
  235. CProcessCopy
  236. \*----------------------------------------------------------------------------*/
  237. class CProcessCopy
  238. {
  239. public:
  240. CProcessCopy();
  241. virtual ~CProcessCopy();
  242. };
  243. #endif // !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)