ProcessCopy.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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, bool bOnlyLoad_CF_TEXT = false);
  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. long m_lLastCopy;
  138. // Generated message map functions
  139. protected:
  140. //{{AFX_MSG(CMainFrame)
  141. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  142. afx_msg void OnDestroy();
  143. afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter);
  144. afx_msg void OnDrawClipboard();
  145. afx_msg void OnTimer(UINT nIDEvent);
  146. //}}AFX_MSG
  147. afx_msg LRESULT OnCVGetConnect(WPARAM wParam, LPARAM lParam);
  148. afx_msg LRESULT OnCVSetConnect(WPARAM wParam, LPARAM lParam);
  149. afx_msg LRESULT OnCVIsConnected(WPARAM wParam, LPARAM lParam);
  150. DECLARE_MESSAGE_MAP()
  151. };
  152. /*----------------------------------------------------------------------------*\
  153. CCopyConfig - used with CCopyThread for efficient inter-thread communication
  154. \*----------------------------------------------------------------------------*/
  155. struct CCopyConfig
  156. {
  157. public:
  158. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  159. HWND m_hClipHandler;
  160. // true to use PostMessage (asynchronous)
  161. // false to use SendMessage (synchronous)
  162. bool m_bAsyncCopy;
  163. // true to create a copy of the clipboard contents when it changes
  164. // false to ignore changes in the clipboard
  165. bool m_bCopyOnChange;
  166. // the supported types which are copied from the clipboard when it changes.
  167. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  168. CCopyConfig( HWND hClipHandler = NULL,
  169. bool bAsyncCopy = false,
  170. bool bCopyOnChange = false,
  171. CClipTypes* pSupportedTypes = NULL )
  172. {
  173. m_hClipHandler = hClipHandler;
  174. m_bAsyncCopy = bAsyncCopy;
  175. m_bCopyOnChange = bCopyOnChange;
  176. m_pSupportedTypes = pSupportedTypes;
  177. }
  178. void DeleteTypes()
  179. {
  180. if( m_pSupportedTypes )
  181. {
  182. delete m_pSupportedTypes;
  183. m_pSupportedTypes = NULL;
  184. }
  185. }
  186. };
  187. /*----------------------------------------------------------------------------*\
  188. CCopyThread
  189. \*----------------------------------------------------------------------------*/
  190. class CCopyThread : public CWinThread
  191. {
  192. DECLARE_DYNCREATE(CCopyThread)
  193. public:
  194. //protected:
  195. CCopyThread(); // protected constructor used by dynamic creation
  196. virtual ~CCopyThread();
  197. public:
  198. bool m_bQuit;
  199. virtual BOOL InitInstance();
  200. virtual int ExitInstance();
  201. // critical section is held whenever shared data is changed
  202. CRITICAL_SECTION m_CS;
  203. void Hold() { ::EnterCriticalSection(&m_CS); }
  204. void Release() { ::LeaveCriticalSection(&m_CS); }
  205. // CopyThread Local (accessed from this CopyThread)
  206. // window owned by this thread which handles clipboard viewer messages
  207. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  208. CCopyConfig m_LocalConfig;
  209. // Called within Copy Thread:
  210. void OnClipboardChange(); // called by ClipboardViewer
  211. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  212. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  213. // Shared (use thread-safe access functions below)
  214. CCopyConfig m_SharedConfig;
  215. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  216. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  217. // Called within Main thread:
  218. bool IsClipboardViewerConnected();
  219. bool GetConnectCV();
  220. void SetConnectCV( bool bConnect );
  221. CClipList* GetClips(); // caller owns the returned CClipList
  222. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  223. HWND SetClipHandler( HWND hWnd ); // returns previous value
  224. HWND GetClipHandler();
  225. bool SetCopyOnChange( bool bVal ); // returns previous value
  226. bool GetCopyOnChange();
  227. bool SetAsyncCopy( bool bVal ); // returns previous value
  228. bool GetAsyncCopy();
  229. // Main thread
  230. void Init( CCopyConfig cfg );
  231. bool Quit();
  232. protected:
  233. DECLARE_MESSAGE_MAP()
  234. };
  235. /*----------------------------------------------------------------------------*\
  236. CProcessCopy
  237. \*----------------------------------------------------------------------------*/
  238. class CProcessCopy
  239. {
  240. public:
  241. CProcessCopy();
  242. virtual ~CProcessCopy();
  243. };
  244. #endif // !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)