clipbrd.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/clipbrd.h
  3. // Purpose: wxClipboad class and clipboard functions
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 19.10.99
  7. // Copyright: (c) wxWidgets Team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CLIPBRD_H_BASE_
  11. #define _WX_CLIPBRD_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_CLIPBOARD
  14. #include "wx/event.h"
  15. #include "wx/chartype.h"
  16. #include "wx/dataobj.h" // for wxDataFormat
  17. #include "wx/vector.h"
  18. class WXDLLIMPEXP_FWD_CORE wxClipboard;
  19. // ----------------------------------------------------------------------------
  20. // wxClipboard represents the system clipboard. Normally, you should use
  21. // wxTheClipboard which is a global pointer to the (unique) clipboard.
  22. //
  23. // Clipboard can be used to copy data to/paste data from. It works together
  24. // with wxDataObject.
  25. // ----------------------------------------------------------------------------
  26. class WXDLLIMPEXP_CORE wxClipboardBase : public wxObject
  27. {
  28. public:
  29. wxClipboardBase() { m_usePrimary = false; }
  30. // open the clipboard before Add/SetData() and GetData()
  31. virtual bool Open() = 0;
  32. // close the clipboard after Add/SetData() and GetData()
  33. virtual void Close() = 0;
  34. // query whether the clipboard is opened
  35. virtual bool IsOpened() const = 0;
  36. // add to the clipboard data
  37. //
  38. // NB: the clipboard owns the pointer and will delete it, so data must be
  39. // allocated on the heap
  40. virtual bool AddData( wxDataObject *data ) = 0;
  41. // set the clipboard data, this is the same as Clear() followed by
  42. // AddData()
  43. virtual bool SetData( wxDataObject *data ) = 0;
  44. // ask if data in correct format is available
  45. virtual bool IsSupported( const wxDataFormat& format ) = 0;
  46. // ask if data in correct format is available
  47. virtual bool IsSupportedAsync( wxEvtHandler *sink );
  48. // fill data with data on the clipboard (if available)
  49. virtual bool GetData( wxDataObject& data ) = 0;
  50. // clears wxTheClipboard and the system's clipboard if possible
  51. virtual void Clear() = 0;
  52. // flushes the clipboard: this means that the data which is currently on
  53. // clipboard will stay available even after the application exits (possibly
  54. // eating memory), otherwise the clipboard will be emptied on exit
  55. virtual bool Flush() { return false; }
  56. // this allows to choose whether we work with CLIPBOARD (default) or
  57. // PRIMARY selection on X11-based systems
  58. //
  59. // on the other ones, working with primary selection does nothing: this
  60. // allows to write code which sets the primary selection when something is
  61. // selected without any ill effects (i.e. without overwriting the
  62. // clipboard which would be wrong on the platforms without X11 PRIMARY)
  63. virtual void UsePrimarySelection(bool usePrimary = false)
  64. {
  65. m_usePrimary = usePrimary;
  66. }
  67. // return true if we're using primary selection
  68. bool IsUsingPrimarySelection() const { return m_usePrimary; }
  69. // Returns global instance (wxTheClipboard) of the object:
  70. static wxClipboard *Get();
  71. // don't use this directly, it is public for compatibility with some ports
  72. // (wxX11, wxMotif, ...) only
  73. bool m_usePrimary;
  74. };
  75. // ----------------------------------------------------------------------------
  76. // asynchronous clipboard event
  77. // ----------------------------------------------------------------------------
  78. class WXDLLIMPEXP_CORE wxClipboardEvent : public wxEvent
  79. {
  80. public:
  81. wxClipboardEvent(wxEventType evtType = wxEVT_NULL)
  82. : wxEvent(0, evtType)
  83. {
  84. }
  85. wxClipboardEvent(const wxClipboardEvent& event)
  86. : wxEvent(event),
  87. m_formats(event.m_formats)
  88. {
  89. }
  90. bool SupportsFormat(const wxDataFormat& format) const;
  91. void AddFormat(const wxDataFormat& format);
  92. virtual wxEvent *Clone() const
  93. {
  94. return new wxClipboardEvent(*this);
  95. }
  96. protected:
  97. wxVector<wxDataFormat> m_formats;
  98. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxClipboardEvent)
  99. };
  100. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_CLIPBOARD_CHANGED, wxClipboardEvent );
  101. typedef void (wxEvtHandler::*wxClipboardEventFunction)(wxClipboardEvent&);
  102. #define wxClipboardEventHandler(func) \
  103. wxEVENT_HANDLER_CAST(wxClipboardEventFunction, func)
  104. #define EVT_CLIPBOARD_CHANGED(func) wx__DECLARE_EVT0(wxEVT_CLIPBOARD_CHANGED, wxClipboardEventHandler(func))
  105. // ----------------------------------------------------------------------------
  106. // globals
  107. // ----------------------------------------------------------------------------
  108. // The global clipboard object - backward compatible access macro:
  109. #define wxTheClipboard (wxClipboard::Get())
  110. // ----------------------------------------------------------------------------
  111. // include platform-specific class declaration
  112. // ----------------------------------------------------------------------------
  113. #if defined(__WXMSW__)
  114. #include "wx/msw/clipbrd.h"
  115. #elif defined(__WXMOTIF__)
  116. #include "wx/motif/clipbrd.h"
  117. #elif defined(__WXGTK20__)
  118. #include "wx/gtk/clipbrd.h"
  119. #elif defined(__WXGTK__)
  120. #include "wx/gtk1/clipbrd.h"
  121. #elif defined(__WXX11__)
  122. #include "wx/x11/clipbrd.h"
  123. #elif defined(__WXMAC__)
  124. #include "wx/osx/clipbrd.h"
  125. #elif defined(__WXCOCOA__)
  126. #include "wx/cocoa/clipbrd.h"
  127. #elif defined(__WXPM__)
  128. #include "wx/os2/clipbrd.h"
  129. #endif
  130. // ----------------------------------------------------------------------------
  131. // helpful class for opening the clipboard and automatically closing it
  132. // ----------------------------------------------------------------------------
  133. class WXDLLIMPEXP_CORE wxClipboardLocker
  134. {
  135. public:
  136. wxClipboardLocker(wxClipboard *clipboard = NULL)
  137. {
  138. m_clipboard = clipboard ? clipboard : wxTheClipboard;
  139. if ( m_clipboard )
  140. {
  141. m_clipboard->Open();
  142. }
  143. }
  144. bool operator!() const { return !m_clipboard->IsOpened(); }
  145. ~wxClipboardLocker()
  146. {
  147. if ( m_clipboard )
  148. {
  149. m_clipboard->Close();
  150. }
  151. }
  152. private:
  153. wxClipboard *m_clipboard;
  154. wxDECLARE_NO_COPY_CLASS(wxClipboardLocker);
  155. };
  156. #endif // wxUSE_CLIPBOARD
  157. #endif // _WX_CLIPBRD_H_BASE_