ProcessCopy.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. 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. };
  101. /*----------------------------------------------------------------------------*\
  102. CClipboardViewer
  103. \*----------------------------------------------------------------------------*/
  104. #define TIMER_ENSURE_VIEWER_IN_CHAIN 6
  105. class CClipboardViewer : public CWnd
  106. {
  107. protected:
  108. DECLARE_DYNAMIC(CClipboardViewer)
  109. public:
  110. // Overrides
  111. // ClassWizard generated virtual function overrides
  112. //{{AFX_VIRTUAL(CMainFrame)
  113. //}}AFX_VIRTUAL
  114. // Implementation
  115. CClipboardViewer( CCopyThread* pHandler );
  116. virtual ~CClipboardViewer();
  117. void Create();
  118. HWND m_hNextClipboardViewer;
  119. bool m_bCalling_SetClipboardViewer;
  120. long m_lReconectCount;
  121. bool m_bIsConnected;
  122. // m_pHandler->OnClipboardChange is called when the clipboard changes.
  123. CCopyThread* m_pHandler;
  124. void Connect(); // connects as a clipboard viewer
  125. void Disconnect(); // disconnects as a clipboard viewer
  126. bool m_bPinging;
  127. bool m_bPingSuccess;
  128. bool SendPing(); // returns true if we are in the chain
  129. bool EnsureConnected();
  130. void SetCVIgnore(); // puts format "Clipboard Viewer Ignore" on the clipboard
  131. // Generated message map functions
  132. protected:
  133. //{{AFX_MSG(CMainFrame)
  134. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  135. afx_msg void OnDestroy();
  136. afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter);
  137. afx_msg void OnDrawClipboard();
  138. afx_msg void OnTimer(UINT nIDEvent);
  139. //}}AFX_MSG
  140. afx_msg LRESULT OnCVReconnect(WPARAM wParam, LPARAM lParam);
  141. afx_msg LRESULT OnCVIsConnected(WPARAM wParam, LPARAM lParam);
  142. DECLARE_MESSAGE_MAP()
  143. };
  144. /*----------------------------------------------------------------------------*\
  145. CCopyConfig - used with CCopyThread for efficient inter-thread communication
  146. \*----------------------------------------------------------------------------*/
  147. struct CCopyConfig
  148. {
  149. public:
  150. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  151. HWND m_hClipHandler;
  152. // true to use PostMessage (asynchronous)
  153. // false to use SendMessage (synchronous)
  154. bool m_bAsyncCopy;
  155. // true to create a copy of the clipboard contents when it changes
  156. // false to ignore changes in the clipboard
  157. bool m_bCopyOnChange;
  158. // the supported types which are copied from the clipboard when it changes.
  159. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  160. CCopyConfig( HWND hClipHandler = NULL,
  161. bool bAsyncCopy = false,
  162. bool bCopyOnChange = false,
  163. CClipTypes* pSupportedTypes = NULL )
  164. {
  165. m_hClipHandler = hClipHandler;
  166. m_bAsyncCopy = bAsyncCopy;
  167. m_bCopyOnChange = bCopyOnChange;
  168. m_pSupportedTypes = pSupportedTypes;
  169. }
  170. void DeleteTypes()
  171. {
  172. if( m_pSupportedTypes )
  173. {
  174. delete m_pSupportedTypes;
  175. m_pSupportedTypes = NULL;
  176. }
  177. }
  178. };
  179. /*----------------------------------------------------------------------------*\
  180. CCopyThread
  181. \*----------------------------------------------------------------------------*/
  182. class CCopyThread : public CWinThread
  183. {
  184. DECLARE_DYNCREATE(CCopyThread)
  185. public:
  186. //protected:
  187. CCopyThread(); // protected constructor used by dynamic creation
  188. virtual ~CCopyThread();
  189. public:
  190. bool m_bQuit;
  191. virtual BOOL InitInstance();
  192. virtual int ExitInstance();
  193. // critical section is held whenever shared data is changed
  194. CRITICAL_SECTION m_CS;
  195. void Hold() { ::EnterCriticalSection(&m_CS); }
  196. void Release() { ::LeaveCriticalSection(&m_CS); }
  197. // CopyThread Local (accessed from this CopyThread)
  198. // window owned by this thread which handles clipboard viewer messages
  199. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  200. CCopyConfig m_LocalConfig;
  201. // Called within Copy Thread:
  202. void OnClipboardChange(); // called by ClipboardViewer
  203. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  204. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  205. // Shared (use thread-safe access functions below)
  206. CCopyConfig m_SharedConfig;
  207. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  208. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  209. // Called within Main thread:
  210. bool IsClipboardViewerConnected();
  211. CClipList* GetClips(); // caller owns the returned CClipList
  212. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  213. HWND SetClipHandler( HWND hWnd ); // returns previous value
  214. HWND GetClipHandler();
  215. bool SetCopyOnChange( bool bVal ); // returns previous value
  216. bool GetCopyOnChange();
  217. bool SetAsyncCopy( bool bVal ); // returns previous value
  218. bool GetAsyncCopy();
  219. // Main thread
  220. void Init( CCopyConfig cfg );
  221. bool Quit();
  222. protected:
  223. DECLARE_MESSAGE_MAP()
  224. };
  225. /*----------------------------------------------------------------------------*\
  226. CProcessCopy
  227. \*----------------------------------------------------------------------------*/
  228. class CProcessCopy
  229. {
  230. public:
  231. CProcessCopy();
  232. virtual ~CProcessCopy();
  233. };
  234. #endif // !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)