1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "clipbrd.h"
14 #include "wx/clipbrd.h"
16 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
20 wxClipboard
*wxTheClipboard
= (wxClipboard
*) NULL
;
22 GdkAtom g_textAtom
= 0;
23 GdkAtom g_clipboardAtom
= 0;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 void wxInitClipboard()
31 if (wxTheClipboard
) delete wxTheClipboard
;
32 wxTheClipboard
= new wxClipboard();
35 void wxDoneClipboard()
37 if (wxTheClipboard
) delete wxTheClipboard
;
38 wxTheClipboard
= (wxClipboard
*) NULL
;
41 //-----------------------------------------------------------------------------
42 // "selection_received"
43 //-----------------------------------------------------------------------------
46 selection_received( GtkWidget
*widget
, GtkSelectionData
*selection_data
, gpointer data
)
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 selection_clear( GtkWidget
*widget
, GdkEventSelection
*event
)
57 /* The clipboard is no longer in our hands. We can delete the
58 * clipboard data. I hope I got that one right... */
60 if (!wxTheClipboard
) return TRUE
;
62 wxTheClipboard
->SetData( (wxDataObject
*) NULL
);
67 //-----------------------------------------------------------------------------
68 // selection handler for supplying data
69 //-----------------------------------------------------------------------------
72 selection_handler( GtkWidget
*WXUNUSED(widget
), GtkSelectionData
*selection_data
, gpointer
WXUNUSED(data
) )
74 if (!wxTheClipboard
) return;
76 wxDataObject
*data_object
= wxTheClipboard
->m_data
;
78 if (!data_object
) return;
80 if (data_object
->GetDataSize() == 0) return;
82 gint len
= data_object
->GetDataSize();
83 guchar
*bin_data
= (guchar
*) malloc( len
);
84 data_object
->GetDataHere( (void*)bin_data
);
86 if (selection_data
->target
== GDK_SELECTION_TYPE_STRING
)
88 gtk_selection_data_set(
89 selection_data
, GDK_SELECTION_TYPE_STRING
, 8*sizeof(gchar
), bin_data
, len
);
91 else if (selection_data
->target
== g_textAtom
)
93 gtk_selection_data_set(
94 selection_data
, g_textAtom
, 8*sizeof(gchar
), bin_data
, len
);
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
105 wxClipboard::wxClipboard()
107 m_data
= (wxDataObject
*) NULL
;
108 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
109 gtk_widget_realize( m_clipboardWidget
);
111 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
112 "selection_clear_event",
113 GTK_SIGNAL_FUNC( selection_clear
),
116 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
117 if (!g_textAtom
) g_textAtom
= gdk_atom_intern( "TEXT", FALSE
);
120 wxClipboard::~wxClipboard()
122 /* As we have data we also own the clipboard. Once we no longer own
123 it, clear_selection is called which will set m_data to zero */
128 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
, GDK_CURRENT_TIME
);
130 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
133 void wxClipboard::SetData( wxDataObject
*data
)
135 if (m_data
) delete m_data
;
139 if (!gtk_selection_owner_set( m_clipboardWidget
,
144 m_data
= (wxDataObject
*) NULL
;
148 switch (m_data
->GetPreferredFormat())
152 gtk_selection_add_handler( m_clipboardWidget,
159 gtk_selection_add_handler( m_clipboardWidget,
171 void *wxClipboard::GetData( wxDataFormat format
, size_t *length
)
173 if (!IsAvailable(format
))
175 if (length
) *length
= 0;
181 bool wxClipboard::IsAvailable( wxDataFormat
WXUNUSED(format
) )