From 9005f2edfd988e7f4470a8460e6f96ea2287ba1f Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 31 Mar 2007 02:57:55 +0000
Subject: [PATCH] 1. Moved m_usePrimary to wxClipboardBase as it's now also
 used by wxMSW/wxMac 2. Added IsUsingPrimarySelection() 3. Fail all clipboard
 operations when IsUsingPrimarySelection() is true on    non-X11 platforms

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45183 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/clipbrd.tex       | 36 +++++++++++++++++++++++++++++----
 include/wx/clipbrd.h            | 23 ++++++++++++++++++---
 include/wx/gtk/clipbrd.h        |  7 +------
 include/wx/gtk1/clipbrd.h       |  6 ------
 include/wx/mac/carbon/clipbrd.h |  6 +-----
 include/wx/mgl/clipbrd.h        |  8 +-------
 include/wx/motif/clipbrd.h      |  7 +------
 include/wx/msw/clipbrd.h        |  7 ++-----
 include/wx/os2/clipbrd.h        |  6 +-----
 include/wx/palmos/clipbrd.h     |  3 ---
 include/wx/x11/clipbrd.h        | 18 +++++------------
 src/gtk/clipbrd.cpp             |  2 --
 src/gtk1/clipbrd.cpp            |  2 --
 src/mac/carbon/clipbrd.cpp      | 15 ++++++++++++++
 src/msw/clipbrd.cpp             | 14 ++++++++++++-
 src/x11/clipbrd.cpp             |  2 --
 16 files changed, 92 insertions(+), 70 deletions(-)

diff --git a/docs/latex/wx/clipbrd.tex b/docs/latex/wx/clipbrd.tex
index a216479e01..cba1926fb1 100644
--- a/docs/latex/wx/clipbrd.tex
+++ b/docs/latex/wx/clipbrd.tex
@@ -52,18 +52,21 @@ For example:
 
 \latexignore{\rtfignore{\wxheading{Members}}}
 
+
 \membersection{wxClipboard::wxClipboard}\label{wxclipboardctor}
 
 \func{}{wxClipboard}{\void}
 
 Constructor.
 
+
 \membersection{wxClipboard::\destruct{wxClipboard}}\label{wxclipboarddtor}
 
 \func{}{\destruct{wxClipboard}}{\void}
 
 Destructor.
 
+
 \membersection{wxClipboard::AddData}\label{wxclipboardadddata}
 
 \func{bool}{AddData}{\param{wxDataObject*}{ data}}
@@ -78,18 +81,21 @@ the data explicitly.
 
 \helpref{wxClipboard::SetData}{wxclipboardsetdata}
 
+
 \membersection{wxClipboard::Clear}\label{wxclipboardclear}
 
 \func{void}{Clear}{\void}
 
 Clears the global clipboard object and the system's clipboard if possible.
 
+
 \membersection{wxClipboard::Close}\label{wxclipboardclose}
 
 \func{void}{Close}{\void}
 
 Call this function to close the clipboard, having opened it with \helpref{wxClipboard::Open}{wxclipboardopen}.
 
+
 \membersection{wxClipboard::Flush}\label{wxclipboardflush}
 
 \func{bool}{Flush}{\void}
@@ -99,6 +105,7 @@ clipboard will stay available even after the application exits (possibly
 eating memory), otherwise the clipboard will be emptied on exit.
 Returns false if the operation is unsuccessful for any reason.
 
+
 \membersection{wxClipboard::GetData}\label{wxclipboardgetdata}
 
 \func{bool}{GetData}{\param{wxDataObject\&}{ data}}
@@ -106,18 +113,29 @@ Returns false if the operation is unsuccessful for any reason.
 Call this function to fill {\it data} with data on the clipboard, if available in the required
 format. Returns true on success.
 
+
 \membersection{wxClipboard::IsOpened}\label{wxclipboardisopened}
 
 \constfunc{bool}{IsOpened}{\void}
 
 Returns true if the clipboard has been opened.
 
+
 \membersection{wxClipboard::IsSupported}\label{wxclipboardissupported}
 
 \func{bool}{IsSupported}{\param{const wxDataFormat\&}{ format}}
 
 Returns true if there is data which matches the data format of the given data object currently {\bf available} (IsSupported sounds like a misnomer, FIXME: better deprecate this name?) on the clipboard.
 
+
+\membersection{wxClipboard::IsUsingPrimarySelection}\label{wxclipboardisusingprimaryselection}
+
+\constfunc{bool}{IsUsingPrimarySelection}{\void}
+
+Returns \true if we are using the primary selection, \false if clipboard one.
+See \helpref{UsePrimarySelection}{wxclipboarduseprimary} for more information.
+
+
 \membersection{wxClipboard::Open}\label{wxclipboardopen}
 
 \func{bool}{Open}{\void}
@@ -130,6 +148,7 @@ should keep the clipboard open for only a very short time.
 
 Returns true on success. This should be tested (as in the sample shown above).
 
+
 \membersection{wxClipboard::SetData}\label{wxclipboardsetdata}
 
 \func{bool}{SetData}{\param{wxDataObject*}{ data}}
@@ -145,12 +164,21 @@ the data explicitly.
 
 \helpref{wxClipboard::AddData}{wxclipboardadddata}
 
+
 \membersection{wxClipboard::UsePrimarySelection}\label{wxclipboarduseprimary}
 
 \func{void}{UsePrimarySelection}{\param{bool}{ primary = true}}
 
-On platforms supporting it (currently only GTK), selects the so called
-PRIMARY SELECTION as the clipboard as opposed to the normal clipboard,
-if {\it primary} is true.
-
+On platforms supporting it (all X11-based ports), wxClipboard uses the
+CLIPBOARD X11 selection by default. When this function is called with \true
+argument, all subsequent clipboard operations will use PRIMARY selection until
+this function is called again with \false.
+
+On the other platforms, there is no PRIMARY selection and so all clipboard
+operations will fail. This allows to implement the standard X11 handling of the
+clipboard which consists in copying data to the CLIPBOARD selection only when
+the user explicitly requests it (i.e. by selection \texttt{"Copy"} menu
+command) but putting the currently selected text into the PRIMARY selection
+automatically, without overwriting the normal clipboard contents with the
+currently selected text on the other platforms.
 
diff --git a/include/wx/clipbrd.h b/include/wx/clipbrd.h
index c749a4e675..122bbf7227 100644
--- a/include/wx/clipbrd.h
+++ b/include/wx/clipbrd.h
@@ -35,7 +35,7 @@ class WXDLLEXPORT wxClipboard;
 class WXDLLEXPORT wxClipboardBase : public wxObject
 {
 public:
-    wxClipboardBase() {}
+    wxClipboardBase() { m_usePrimary = false; }
 
     // open the clipboard before Add/SetData() and GetData()
     virtual bool Open() = 0;
@@ -70,11 +70,28 @@ public:
     // eating memory), otherwise the clipboard will be emptied on exit
     virtual bool Flush() { return false; }
 
-    // X11 has two clipboards which get selected by this call. Empty on MSW.
-    virtual void UsePrimarySelection( bool WXUNUSED(primary) = false ) { }
+    // this allows to choose whether we work with CLIPBOARD (default) or
+    // PRIMARY selection on X11-based systems
+    //
+    // on the other ones, working with primary selection does nothing: this
+    // allows to write code which sets the primary selection when something is
+    // selected without any ill effects (i.e. without overwriting the
+    // clipboard which would be wrong on the platforms without X11 PRIMARY)
+    virtual void UsePrimarySelection(bool usePrimary = false)
+    {
+        m_usePrimary = usePrimary;
+    }
+
+    // return true if we're using primary selection
+    bool IsUsingPrimarySelection() const { return m_usePrimary; }
 
     // Returns global instance (wxTheClipboard) of the object:
     static wxClipboard *Get();
+
+
+    // don't use this directly, it is public for compatibility with some ports
+    // (wxX11, wxMotif, ...) only
+    bool m_usePrimary;
 };
 
 // ----------------------------------------------------------------------------
diff --git a/include/wx/gtk/clipbrd.h b/include/wx/gtk/clipbrd.h
index d9f59e75b1..df95600542 100644
--- a/include/wx/gtk/clipbrd.h
+++ b/include/wx/gtk/clipbrd.h
@@ -52,11 +52,7 @@ public:
     // clears wxTheClipboard and the system's clipboard if possible
     virtual void Clear();
 
-    // If primary == TRUE, use primary selection in all further ops,
-    // primary == FALSE resets it.
-    virtual void UsePrimarySelection(bool primary = TRUE)
-        { m_usePrimary = primary; }
-    
+
 
     // implementation from now on
     // --------------------------
@@ -111,7 +107,6 @@ private:
     GtkWidget *m_targetsWidget;    // for getting list of supported formats
 
     bool m_open;
-    bool m_usePrimary;
     bool m_formatSupported;
 
 
diff --git a/include/wx/gtk1/clipbrd.h b/include/wx/gtk1/clipbrd.h
index c5cda19136..eb02404678 100644
--- a/include/wx/gtk1/clipbrd.h
+++ b/include/wx/gtk1/clipbrd.h
@@ -53,11 +53,6 @@ public:
     // clears wxTheClipboard and the system's clipboard if possible
     virtual void Clear();
 
-    // If primary == TRUE, use primary selection in all further ops,
-    // primary == FALSE resets it.
-    virtual void UsePrimarySelection(bool primary = TRUE)
-        { m_usePrimary = primary; }
-
     // implementation from now on
     bool              m_open;
     bool              m_ownsClipboard;
@@ -70,7 +65,6 @@ public:
 
     bool              m_formatSupported;
     GdkAtom           m_targetRequested;
-    bool              m_usePrimary;
     wxDataObject     *m_receivedData;
 
 private:
diff --git a/include/wx/mac/carbon/clipbrd.h b/include/wx/mac/carbon/clipbrd.h
index f484bbd7d3..1ad2c356ae 100644
--- a/include/wx/mac/carbon/clipbrd.h
+++ b/include/wx/mac/carbon/clipbrd.h
@@ -63,9 +63,6 @@ public:
     // eating memory), otherwise the clipboard will be emptied on exit
     virtual bool Flush();
 
-    // X11 has two clipboards which get selected by this call. Empty on MSW.
-    void UsePrimarySelection( bool WXUNUSED(primary) = FALSE ) { }
-
 private:
     wxDataObject     *m_data;
     bool              m_open;
@@ -73,5 +70,4 @@ private:
 
 #endif // wxUSE_CLIPBOARD
 
-#endif
-    // _WX_CLIPBRD_H_
+#endif // _WX_CLIPBRD_H_
diff --git a/include/wx/mgl/clipbrd.h b/include/wx/mgl/clipbrd.h
index 98a82d1149..307cc8de0b 100644
--- a/include/wx/mgl/clipbrd.h
+++ b/include/wx/mgl/clipbrd.h
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        clipboard.h
+// Name:        wx/mgl/clipboard.h
 // Purpose:
 // Author:      Vaclav Slavik
 // Id:          $Id$
@@ -52,11 +52,6 @@ public:
     // clears wxTheClipboard and the system's clipboard if possible
     virtual void Clear() {}
 
-    // If primary == TRUE, use primary selection in all further ops,
-    // primary == FALSE resets it.
-    virtual void UsePrimarySelection(bool primary = TRUE)
-        { m_usePrimary = primary; }
-    
     // implementation from now on
     bool              m_open;
     bool              m_ownsClipboard;
@@ -69,7 +64,6 @@ public:
 
     bool              m_formatSupported;
     GdkAtom           m_targetRequested;
-    bool              m_usePrimary;
     wxDataObject     *m_receivedData;
 
 private:
diff --git a/include/wx/motif/clipbrd.h b/include/wx/motif/clipbrd.h
index 7aecda908d..00210ca1ff 100644
--- a/include/wx/motif/clipbrd.h
+++ b/include/wx/motif/clipbrd.h
@@ -67,13 +67,9 @@ public:
     // clears wxTheClipboard and the system's clipboard if possible
     virtual void Clear();
 
-    virtual void UsePrimarySelection(bool primary = true)
-    { m_usePrimary = primary; }
-
     // implementation from now on
     bool              m_open;
     wxDataObjectList  m_data;
-    bool              m_usePrimary;
     wxDataIdToDataObjectList m_idToObject;
 
 private:
@@ -82,5 +78,4 @@ private:
 
 #endif // wxUSE_CLIPBOARD
 
-#endif
-// _WX_CLIPBRD_H_
+#endif // _WX_CLIPBRD_H_
diff --git a/include/wx/msw/clipbrd.h b/include/wx/msw/clipbrd.h
index 3d0277004e..2a760e2676 100644
--- a/include/wx/msw/clipbrd.h
+++ b/include/wx/msw/clipbrd.h
@@ -85,14 +85,11 @@ public:
     // eating memory), otherwise the clipboard will be emptied on exit
     virtual bool Flush();
 
-    // X11 has two clipboards which get selected by this call. Empty on MSW.
-    void UsePrimarySelection( bool WXUNUSED(primary) = false ) { }
-
 private:
     IDataObject *m_lastDataObject;
     bool m_isOpened;
 };
 
 #endif // wxUSE_CLIPBOARD
-#endif
-    // _WX_CLIPBRD_H_
+
+#endif // _WX_CLIPBRD_H_
diff --git a/include/wx/os2/clipbrd.h b/include/wx/os2/clipbrd.h
index 522d5641a0..729b956dfe 100644
--- a/include/wx/os2/clipbrd.h
+++ b/include/wx/os2/clipbrd.h
@@ -88,13 +88,9 @@ public:
     // eating memory), otherwise the clipboard will be emptied on exit
     virtual bool Flush();
 
-    // X11 has two clipboards which get selected by this call. Empty on MSW.
-    void UsePrimarySelection( bool WXUNUSED(primary) = FALSE ) { }
-
 private:
     bool m_clearOnExit;
 };
 
 #endif // wxUSE_CLIPBOARD
-#endif
-    // _WX_CLIPBRD_H_
+#endif // _WX_CLIPBRD_H_
diff --git a/include/wx/palmos/clipbrd.h b/include/wx/palmos/clipbrd.h
index 3f8023f4ec..8d6e19fab9 100644
--- a/include/wx/palmos/clipbrd.h
+++ b/include/wx/palmos/clipbrd.h
@@ -85,9 +85,6 @@ public:
     // eating memory), otherwise the clipboard will be emptied on exit
     virtual bool Flush();
 
-    // X11 has two clipboards which get selected by this call. Empty on MSW.
-    void UsePrimarySelection( bool WXUNUSED(primary) = FALSE ) { }
-
 private:
     bool m_clearOnExit;
     bool m_isOpened;
diff --git a/include/wx/x11/clipbrd.h b/include/wx/x11/clipbrd.h
index 326e70c48a..48a7c70350 100644
--- a/include/wx/x11/clipbrd.h
+++ b/include/wx/x11/clipbrd.h
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        clipbrd.h
+// Name:        wx/x11/clipbrd.h
 // Purpose:     Clipboard functionality.
 // Author:      Robert Roebling
 // Created:     17/09/98
@@ -8,8 +8,8 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifndef __X11CLIPBOARDH__
-#define __X11CLIPBOARDH__
+#ifndef _WX_X11_CLIPBRD_H_
+#define _WX_X11_CLIPBRD_H_
 
 #if wxUSE_CLIPBOARD
 
@@ -53,11 +53,6 @@ public:
     // clears wxTheClipboard and the system's clipboard if possible
     virtual void Clear();
 
-    // If primary == TRUE, use primary selection in all further ops,
-    // primary == FALSE resets it.
-    virtual void UsePrimarySelection(bool primary = TRUE)
-        { m_usePrimary = primary; }
-    
     // implementation from now on
     bool              m_open;
     bool              m_ownsClipboard;
@@ -70,15 +65,12 @@ public:
 
     bool              m_formatSupported;
     Atom              m_targetRequested;
-    bool              m_usePrimary;
     wxDataObject     *m_receivedData;
 
 private:
     DECLARE_DYNAMIC_CLASS(wxClipboard)
 };
 
-#endif
-   // wxUSE_CLIPBOARD
+#endif // wxUSE_CLIPBOARD
 
-#endif
-    // __X11CLIPBOARDH__
+#endif // _WX_X11_CLIPBRD_H_
diff --git a/src/gtk/clipbrd.cpp b/src/gtk/clipbrd.cpp
index e6b79180a1..628a663fbd 100644
--- a/src/gtk/clipbrd.cpp
+++ b/src/gtk/clipbrd.cpp
@@ -347,8 +347,6 @@ wxClipboard::wxClipboard()
     m_formatSupported = false;
     m_targetRequested = 0;
 
-    m_usePrimary = false;
-
     // we use m_targetsWidget to query what formats are available
     m_targetsWidget = gtk_window_new( GTK_WINDOW_POPUP );
     gtk_widget_realize( m_targetsWidget );
diff --git a/src/gtk1/clipbrd.cpp b/src/gtk1/clipbrd.cpp
index fc8309b47b..2d1ade92ec 100644
--- a/src/gtk1/clipbrd.cpp
+++ b/src/gtk1/clipbrd.cpp
@@ -347,8 +347,6 @@ wxClipboard::wxClipboard()
 
     m_formatSupported = false;
     m_targetRequested = 0;
-
-    m_usePrimary = false;
 }
 
 wxClipboard::~wxClipboard()
diff --git a/src/mac/carbon/clipbrd.cpp b/src/mac/carbon/clipbrd.cpp
index d0ea791e8b..413b80df02 100644
--- a/src/mac/carbon/clipbrd.cpp
+++ b/src/mac/carbon/clipbrd.cpp
@@ -173,6 +173,9 @@ wxClipboard::~wxClipboard()
 
 void wxClipboard::Clear()
 {
+    if ( IsUsingPrimarySelection() )
+        return;
+
     if (m_data != NULL)
     {
         delete m_data;
@@ -214,6 +217,9 @@ bool wxClipboard::IsOpened() const
 
 bool wxClipboard::SetData( wxDataObject *data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
     wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
     wxCHECK_MSG( data, false, wxT("data is invalid") );
 
@@ -226,6 +232,9 @@ bool wxClipboard::SetData( wxDataObject *data )
 
 bool wxClipboard::AddData( wxDataObject *data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
     wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
     wxCHECK_MSG( data, false, wxT("data is invalid") );
 
@@ -320,6 +329,9 @@ void wxClipboard::Close()
 
 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
     if ( m_data )
         return m_data->IsSupported( dataFormat );
 
@@ -370,6 +382,9 @@ bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
 
 bool wxClipboard::GetData( wxDataObject& data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
     wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
 
     size_t formatcount = data.GetFormatCount() + 1;
diff --git a/src/msw/clipbrd.cpp b/src/msw/clipbrd.cpp
index 4a1518b0ff..2369d769fb 100644
--- a/src/msw/clipbrd.cpp
+++ b/src/msw/clipbrd.cpp
@@ -547,6 +547,9 @@ wxClipboard::~wxClipboard()
 
 void wxClipboard::Clear()
 {
+    if ( IsUsingPrimarySelection() )
+        return;
+
 #if wxUSE_OLE_CLIPBOARD
     if (m_lastDataObject)
     {
@@ -613,6 +616,9 @@ bool wxClipboard::IsOpened() const
 
 bool wxClipboard::SetData( wxDataObject *data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
 #if !wxUSE_OLE_CLIPBOARD
     (void)wxEmptyClipboard();
 #endif // wxUSE_OLE_CLIPBOARD
@@ -625,6 +631,9 @@ bool wxClipboard::SetData( wxDataObject *data )
 
 bool wxClipboard::AddData( wxDataObject *data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
     wxCHECK_MSG( data, false, wxT("data is invalid") );
 
 #if wxUSE_OLE_CLIPBOARD
@@ -718,11 +727,14 @@ void wxClipboard::Close()
 
 bool wxClipboard::IsSupported( const wxDataFormat& format )
 {
-    return wxIsClipboardFormatAvailable(format);
+    return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format);
 }
 
 bool wxClipboard::GetData( wxDataObject& data )
 {
+    if ( IsUsingPrimarySelection() )
+        return false;
+
 #if wxUSE_OLE_CLIPBOARD
     IDataObject *pDataObject = NULL;
     HRESULT hr = OleGetClipboard(&pDataObject);
diff --git a/src/x11/clipbrd.cpp b/src/x11/clipbrd.cpp
index 69494fb10b..a523296de8 100644
--- a/src/x11/clipbrd.cpp
+++ b/src/x11/clipbrd.cpp
@@ -306,8 +306,6 @@ wxClipboard::wxClipboard()
 
     m_formatSupported = false;
     m_targetRequested = 0;
-
-    m_usePrimary = false;
 }
 
 wxClipboard::~wxClipboard()
-- 
2.45.2

