ProcessCopy.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. CClipFormat( CLIPFORMAT cfType = 0, HGLOBAL hgData = 0 )
  33. { m_cfType = cfType; m_hgData = hgData; }
  34. ~CClipFormat() { Free(); }
  35. void Clear() { m_cfType = 0; m_hgData = 0; }
  36. void Free() { if( m_hgData ) m_hgData = ::GlobalFree( m_hgData ); }
  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. // statistics assigned by LoadFromClipboard
  59. CTime m_Time; // time copied from clipboard
  60. CString m_Desc;
  61. ULONG m_lTotalCopySize;
  62. CClip();
  63. ~CClip();
  64. void Clear();
  65. void EmptyFormats();
  66. // Adds a new Format to this Clip by copying the given data.
  67. bool AddFormat( CLIPFORMAT cfType, void* pData, UINT nLen );
  68. // Fills this CClip with the contents of the clipboard.
  69. bool LoadFromClipboard( CClipTypes* pClipTypes );
  70. bool SetDescFromText( HGLOBAL hgData );
  71. bool SetDescFromType();
  72. // Immediately save this clip to the db (empties m_Formats due to AddToDataTable).
  73. bool AddToDB( bool bCheckForDuplicates = true );
  74. bool AddToMainTable(); // assigns m_ID
  75. bool AddToDataTable(); // Empties m_Formats as it saves them to the Data Table.
  76. // if a duplicate exists, set recset to the duplicate and return true
  77. bool FindDuplicate( CMainTable& recset, BOOL bCheckLastOnly = FALSE );
  78. int CompareFormatDataTo( long lDataID );
  79. // changes m_Time to be later than the latest clip entry in the db
  80. void MakeLatestTime();
  81. // STATICS
  82. // Allocates a Global containing the requested Clip's Format Data
  83. static HGLOBAL LoadFormat( long lID, UINT cfType );
  84. // Fills "formats" with the Data of all Formats in the db for the given Clip ID
  85. static bool LoadFormats( long lID, CClipFormats& formats );
  86. // Fills "types" with all Types in the db for the given Clip ID
  87. static void LoadTypes( long lID, CClipTypes& types );
  88. };
  89. /*----------------------------------------------------------------------------*\
  90. CClipList
  91. \*----------------------------------------------------------------------------*/
  92. class CClipList : public CList<CClip*,CClip*>
  93. {
  94. public:
  95. ~CClipList();
  96. // returns the number of clips actually saved
  97. // while this does empty the Format Data, it does not delete the Clips.
  98. int AddToDB( bool bLatestTime = false, bool bShowStatus = true );
  99. };
  100. /*----------------------------------------------------------------------------*\
  101. CClipboardViewer
  102. \*----------------------------------------------------------------------------*/
  103. #define TIMER_ENSURE_VIEWER_IN_CHAIN 6
  104. class CClipboardViewer : public CWnd
  105. {
  106. protected:
  107. DECLARE_DYNAMIC(CClipboardViewer)
  108. public:
  109. // Overrides
  110. // ClassWizard generated virtual function overrides
  111. //{{AFX_VIRTUAL(CMainFrame)
  112. //}}AFX_VIRTUAL
  113. // Implementation
  114. CClipboardViewer( CCopyThread* pHandler );
  115. virtual ~CClipboardViewer();
  116. void Create();
  117. HWND m_hNextClipboardViewer;
  118. bool m_bCalling_SetClipboardViewer;
  119. long m_lReconectCount;
  120. bool m_bIsConnected;
  121. // m_pHandler->OnClipboardChange is called when the clipboard changes.
  122. CCopyThread* m_pHandler;
  123. void Connect(); // connects as a clipboard viewer
  124. void Disconnect(); // disconnects as a clipboard viewer
  125. bool m_bPinging;
  126. bool m_bPingSuccess;
  127. bool SendPing(); // returns true if we are in the chain
  128. bool EnsureConnected();
  129. void SetCVIgnore(); // puts format "Clipboard Viewer Ignore" on the clipboard
  130. // Generated message map functions
  131. protected:
  132. //{{AFX_MSG(CMainFrame)
  133. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  134. afx_msg void OnDestroy();
  135. afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter);
  136. afx_msg void OnDrawClipboard();
  137. afx_msg void OnTimer(UINT nIDEvent);
  138. //}}AFX_MSG
  139. afx_msg LRESULT OnCVReconnect(WPARAM wParam, LPARAM lParam);
  140. afx_msg LRESULT OnCVIsConnected(WPARAM wParam, LPARAM lParam);
  141. DECLARE_MESSAGE_MAP()
  142. };
  143. /*----------------------------------------------------------------------------*\
  144. CCopyConfig - used with CCopyThread for efficient inter-thread communication
  145. \*----------------------------------------------------------------------------*/
  146. struct CCopyConfig
  147. {
  148. public:
  149. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  150. HWND m_hClipHandler;
  151. // true to use PostMessage (asynchronous)
  152. // false to use SendMessage (synchronous)
  153. bool m_bAsyncCopy;
  154. // true to create a copy of the clipboard contents when it changes
  155. // false to ignore changes in the clipboard
  156. bool m_bCopyOnChange;
  157. // the supported types which are copied from the clipboard when it changes.
  158. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  159. CCopyConfig( HWND hClipHandler = NULL,
  160. bool bAsyncCopy = false,
  161. bool bCopyOnChange = false,
  162. CClipTypes* pSupportedTypes = NULL )
  163. {
  164. m_hClipHandler = hClipHandler;
  165. m_bAsyncCopy = bAsyncCopy;
  166. m_bCopyOnChange = bCopyOnChange;
  167. m_pSupportedTypes = pSupportedTypes;
  168. }
  169. void DeleteTypes()
  170. {
  171. if( m_pSupportedTypes )
  172. {
  173. delete m_pSupportedTypes;
  174. m_pSupportedTypes = NULL;
  175. }
  176. }
  177. };
  178. /*----------------------------------------------------------------------------*\
  179. CCopyThread
  180. \*----------------------------------------------------------------------------*/
  181. class CCopyThread : public CWinThread
  182. {
  183. DECLARE_DYNCREATE(CCopyThread)
  184. public:
  185. //protected:
  186. CCopyThread(); // protected constructor used by dynamic creation
  187. virtual ~CCopyThread();
  188. public:
  189. bool m_bQuit;
  190. virtual BOOL InitInstance();
  191. virtual int ExitInstance();
  192. // critical section is held whenever shared data is changed
  193. CRITICAL_SECTION m_CS;
  194. void Hold() { ::EnterCriticalSection(&m_CS); }
  195. void Release() { ::LeaveCriticalSection(&m_CS); }
  196. // CopyThread Local (accessed from this CopyThread)
  197. // window owned by this thread which handles clipboard viewer messages
  198. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  199. CCopyConfig m_LocalConfig;
  200. // Called within Copy Thread:
  201. void OnClipboardChange(); // called by ClipboardViewer
  202. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  203. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  204. // Shared (use thread-safe access functions below)
  205. CCopyConfig m_SharedConfig;
  206. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  207. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  208. // Called within Main thread:
  209. bool IsClipboardViewerConnected();
  210. CClipList* GetClips(); // caller owns the returned CClipList
  211. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  212. HWND SetClipHandler( HWND hWnd ); // returns previous value
  213. HWND GetClipHandler();
  214. bool SetCopyOnChange( bool bVal ); // returns previous value
  215. bool GetCopyOnChange();
  216. bool SetAsyncCopy( bool bVal ); // returns previous value
  217. bool GetAsyncCopy();
  218. // Main thread
  219. void Init( CCopyConfig cfg );
  220. bool Quit();
  221. protected:
  222. DECLARE_MESSAGE_MAP()
  223. };
  224. /*----------------------------------------------------------------------------*\
  225. CProcessCopy
  226. \*----------------------------------------------------------------------------*/
  227. class CProcessCopy
  228. {
  229. public:
  230. CProcessCopy();
  231. virtual ~CProcessCopy();
  232. };
  233. #endif // !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)