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. #define LENGTH_OF_TEXT_SNIPET 249
  13. class CClip;
  14. class CCopyThread;
  15. typedef CArray<CLIPFORMAT, CLIPFORMAT> CClipTypes;
  16. /*----------------------------------------------------------------------------*\
  17. COleDataObjectEx
  18. \*----------------------------------------------------------------------------*/
  19. class COleDataObjectEx : public COleDataObject
  20. {
  21. public:
  22. // creates global from IStream if necessary
  23. HGLOBAL GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc = NULL);
  24. };
  25. /*----------------------------------------------------------------------------*\
  26. CClipFormat - holds the data of one clip format.
  27. \*----------------------------------------------------------------------------*/
  28. class CClipFormat
  29. {
  30. public:
  31. CLIPFORMAT m_cfType;
  32. HGLOBAL m_hgData;
  33. CClipFormat( CLIPFORMAT cfType = 0, HGLOBAL hgData = 0 )
  34. { m_cfType = cfType; m_hgData = hgData; }
  35. ~CClipFormat() { Free(); }
  36. void Clear() { m_cfType = 0; m_hgData = 0; }
  37. void Free() { if( m_hgData ) m_hgData = ::GlobalFree( m_hgData ); }
  38. };
  39. /*----------------------------------------------------------------------------*\
  40. CClipFormats - holds an array of CClipFormat
  41. \*----------------------------------------------------------------------------*/
  42. class CClipFormats : public CArray<CClipFormat,CClipFormat&>
  43. {
  44. public:
  45. // returns a pointer to the CClipFormat in this array which matches the given type
  46. // or NULL if that type doesn't exist in this array.
  47. CClipFormat* FindFormat( UINT cfType );
  48. };
  49. /*----------------------------------------------------------------------------*\
  50. CClip - holds multiple CClipFormats and clip statistics
  51. - provides static functions for manipulating a Clip as a single unit.
  52. \*----------------------------------------------------------------------------*/
  53. class CClip
  54. {
  55. public:
  56. long m_ID; // 0 if it hasn't yet been saved or is unknown
  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();
  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 lID );
  79. // changes m_Time to be later than the latest clip entry in the db
  80. void MakeLatestTime();
  81. // STATICS
  82. // deletes from both Main and Data Tables
  83. static BOOL Delete( int id );
  84. static BOOL Delete( ARRAY& IDs );
  85. static BOOL DeleteAll();
  86. // Allocates a Global containing the requested Clip's Format Data
  87. static HGLOBAL LoadFormat( long lID, UINT cfType );
  88. // Fills "formats" with the Data of all Formats in the db for the given Clip ID
  89. static bool LoadFormats( long lID, CClipFormats& formats );
  90. // Fills "types" with all Types in the db for the given Clip ID
  91. static void LoadTypes( long lID, CClipTypes& types );
  92. };
  93. /*----------------------------------------------------------------------------*\
  94. CClipList
  95. \*----------------------------------------------------------------------------*/
  96. class CClipList : public CList<CClip*,CClip*>
  97. {
  98. public:
  99. ~CClipList();
  100. // returns the number of clips actually saved
  101. // while this does empty the Format Data, it does not delete the Clips.
  102. int AddToDB( bool bLatestTime = false, bool bShowStatus = true );
  103. };
  104. /*----------------------------------------------------------------------------*\
  105. CClipboardViewer
  106. \*----------------------------------------------------------------------------*/
  107. #define TIMER_CHECK_TOP_LEVEL_VIEWER 6
  108. class CClipboardViewer : public CWnd
  109. {
  110. protected:
  111. DECLARE_DYNAMIC(CClipboardViewer)
  112. public:
  113. // Overrides
  114. // ClassWizard generated virtual function overrides
  115. //{{AFX_VIRTUAL(CMainFrame)
  116. //}}AFX_VIRTUAL
  117. // Implementation
  118. CClipboardViewer( CCopyThread* pHandler );
  119. virtual ~CClipboardViewer();
  120. void Create();
  121. HWND m_hNextClipboardViewer;
  122. bool m_bCalling_SetClipboardViewer;
  123. long m_lReconectCount;
  124. bool m_bIsConnected;
  125. // m_pHandler->OnClipboardChange is called when the clipboard changes.
  126. CCopyThread* m_pHandler;
  127. void Connect(); // connects as a clipboard viewer
  128. void Disconnect(); // disconnects as a clipboard viewer
  129. // Generated message map functions
  130. protected:
  131. //{{AFX_MSG(CMainFrame)
  132. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  133. afx_msg void OnDestroy();
  134. afx_msg void OnChangeCbChain(HWND hWndRemove, HWND hWndAfter);
  135. afx_msg void OnDrawClipboard();
  136. afx_msg void OnTimer(UINT nIDEvent);
  137. //}}AFX_MSG
  138. afx_msg LRESULT OnReconnectToCopyChain(WPARAM wParam, LPARAM lParam);
  139. afx_msg LRESULT OnGetIsTopViewer(WPARAM wParam, LPARAM lParam);
  140. DECLARE_MESSAGE_MAP()
  141. };
  142. /*----------------------------------------------------------------------------*\
  143. CCopyConfig - used with CCopyThread for efficient inter-thread communication
  144. \*----------------------------------------------------------------------------*/
  145. struct CCopyConfig
  146. {
  147. public:
  148. // WM_CLIPBOARD_COPIED is sent to this window when a copy is made.
  149. HWND m_hClipHandler;
  150. // true to use PostMessage (asynchronous)
  151. // false to use SendMessage (synchronous)
  152. bool m_bAsyncCopy;
  153. // true to create a copy of the clipboard contents when it changes
  154. // false to ignore changes in the clipboard
  155. bool m_bCopyOnChange;
  156. // the supported types which are copied from the clipboard when it changes.
  157. CClipTypes* m_pSupportedTypes; // ONLY accessed from CopyThread
  158. CCopyConfig( HWND hClipHandler = NULL,
  159. bool bAsyncCopy = false,
  160. bool bCopyOnChange = false,
  161. CClipTypes* pSupportedTypes = NULL )
  162. {
  163. m_hClipHandler = hClipHandler;
  164. m_bAsyncCopy = bAsyncCopy;
  165. m_bCopyOnChange = bCopyOnChange;
  166. m_pSupportedTypes = pSupportedTypes;
  167. }
  168. void DeleteTypes()
  169. {
  170. if( m_pSupportedTypes )
  171. {
  172. delete m_pSupportedTypes;
  173. m_pSupportedTypes = NULL;
  174. }
  175. }
  176. };
  177. /*----------------------------------------------------------------------------*\
  178. CCopyThread
  179. \*----------------------------------------------------------------------------*/
  180. class CCopyThread : public CWinThread
  181. {
  182. DECLARE_DYNCREATE(CCopyThread)
  183. public:
  184. //protected:
  185. CCopyThread(); // protected constructor used by dynamic creation
  186. virtual ~CCopyThread();
  187. public:
  188. bool m_bQuit;
  189. virtual BOOL InitInstance();
  190. virtual int ExitInstance();
  191. // critical section is held whenever shared data is changed
  192. CRITICAL_SECTION m_CS;
  193. void Hold() { ::EnterCriticalSection(&m_CS); }
  194. void Release() { ::LeaveCriticalSection(&m_CS); }
  195. // CopyThread Local (accessed from this CopyThread)
  196. // window owned by this thread which handles clipboard viewer messages
  197. CClipboardViewer* m_pClipboardViewer; // permanent during lifetime of thread
  198. CCopyConfig m_LocalConfig;
  199. // Called within Copy Thread:
  200. void OnClipboardChange(); // called by ClipboardViewer
  201. void SyncConfig(); // safely syncs m_LocalConfig with m_SharedConfig
  202. void AddToClips( CClip* pClip ); // after this, pClip is owned by m_pClips
  203. // Shared (use thread-safe access functions below)
  204. CCopyConfig m_SharedConfig;
  205. bool m_bConfigChanged; // true if m_SharedConfig was changed.
  206. CClipList* m_pClips; // snapshots of the clipboard when it changed.
  207. // Called within Main thread:
  208. bool IsClipboardViewerConnected() { return m_pClipboardViewer->m_bIsConnected; }
  209. CClipList* GetClips(); // caller owns the returned CClipList
  210. void SetSupportedTypes( CClipTypes* pTypes ); // CopyThread will own pTypes
  211. HWND SetClipHandler( HWND hWnd ); // returns previous value
  212. HWND GetClipHandler();
  213. bool SetCopyOnChange( bool bVal ); // returns previous value
  214. bool GetCopyOnChange();
  215. bool SetAsyncCopy( bool bVal ); // returns previous value
  216. bool GetAsyncCopy();
  217. // Main thread
  218. void Init( CCopyConfig cfg );
  219. bool Quit();
  220. protected:
  221. DECLARE_MESSAGE_MAP()
  222. };
  223. /*----------------------------------------------------------------------------*\
  224. CProcessCopy
  225. \*----------------------------------------------------------------------------*/
  226. class CProcessCopy
  227. {
  228. public:
  229. CProcessCopy();
  230. virtual ~CProcessCopy();
  231. };
  232. #endif // !defined(AFX_PROCESSCOPY_H__185CBB6F_4B63_4397_8FF9_E18D777DA506__INCLUDED_)