Big fix for GTK 1.1.5
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipbrd.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "clipbrd.h"
12 #endif
13
14 #include "wx/clipbrd.h"
15
16 //-----------------------------------------------------------------------------
17 // data
18 //-----------------------------------------------------------------------------
19
20 wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
21
22 GdkAtom g_textAtom = 0;
23 GdkAtom g_clipboardAtom = 0;
24
25 //-----------------------------------------------------------------------------
26 // functions
27 //-----------------------------------------------------------------------------
28
29 void wxInitClipboard()
30 {
31 if (wxTheClipboard) delete wxTheClipboard;
32 wxTheClipboard = new wxClipboard();
33 }
34
35 void wxDoneClipboard()
36 {
37 if (wxTheClipboard) delete wxTheClipboard;
38 wxTheClipboard = (wxClipboard*) NULL;
39 }
40
41 //-----------------------------------------------------------------------------
42 // "selection_received"
43 //-----------------------------------------------------------------------------
44
45 static void
46 selection_received( GtkWidget *widget, GtkSelectionData *selection_data, gpointer data )
47 {
48 }
49
50 //-----------------------------------------------------------------------------
51 // "selection_clear"
52 //-----------------------------------------------------------------------------
53
54 static gint
55 selection_clear( GtkWidget *widget, GdkEventSelection *event )
56 {
57 /* The clipboard is no longer in our hands. We can delete the
58 * clipboard data. I hope I got that one right... */
59
60 if (!wxTheClipboard) return TRUE;
61
62 wxTheClipboard->SetData( (wxDataObject*) NULL );
63
64 return TRUE;
65 }
66
67 //-----------------------------------------------------------------------------
68 // selection handler for supplying data
69 //-----------------------------------------------------------------------------
70
71 static void
72 selection_handler( GtkWidget *WXUNUSED(widget), GtkSelectionData *selection_data, gpointer WXUNUSED(data) )
73 {
74 if (!wxTheClipboard) return;
75
76 wxDataObject *data_object = wxTheClipboard->m_data;
77
78 if (!data_object) return;
79
80 if (data_object->GetDataSize() == 0) return;
81
82 gint len = data_object->GetDataSize();
83 guchar *bin_data = (guchar*) malloc( len );
84 data_object->GetDataHere( (void*)bin_data );
85
86 if (selection_data->target == GDK_SELECTION_TYPE_STRING)
87 {
88 gtk_selection_data_set(
89 selection_data, GDK_SELECTION_TYPE_STRING, 8*sizeof(gchar), bin_data, len );
90 }
91 else if (selection_data->target == g_textAtom)
92 {
93 gtk_selection_data_set(
94 selection_data, g_textAtom, 8*sizeof(gchar), bin_data, len );
95 }
96 free( bin_data );
97 }
98
99 //-----------------------------------------------------------------------------
100 // wxClipboard
101 //-----------------------------------------------------------------------------
102
103 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
104
105 wxClipboard::wxClipboard()
106 {
107 m_data = (wxDataObject*) NULL;
108 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
109 gtk_widget_realize( m_clipboardWidget );
110
111 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget),
112 "selection_clear_event",
113 GTK_SIGNAL_FUNC( selection_clear ),
114 (gpointer) NULL );
115
116 if (!g_clipboardAtom) g_clipboardAtom = gdk_atom_intern( "CLIPBOARD", FALSE );
117 if (!g_textAtom) g_textAtom = gdk_atom_intern( "TEXT", FALSE );
118 }
119
120 wxClipboard::~wxClipboard()
121 {
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 */
124
125 if (m_data)
126 {
127 delete m_data;
128 gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
129 }
130 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
131 }
132
133 void wxClipboard::SetData( wxDataObject *data )
134 {
135 if (m_data) delete m_data;
136 m_data = data;
137 if (!m_data) return;
138
139 if (!gtk_selection_owner_set( m_clipboardWidget,
140 g_clipboardAtom,
141 GDK_CURRENT_TIME))
142 {
143 delete m_data;
144 m_data = (wxDataObject*) NULL;
145 return;
146 }
147
148 switch (m_data->GetPreferredFormat())
149 {
150 /*
151 case wxDF_STRING:
152 gtk_selection_add_handler( m_clipboardWidget,
153 g_clipboardAtom,
154 GDK_TARGET_STRING,
155 selection_handler,
156 NULL );
157 break;
158 case wxDF_TEXT:
159 gtk_selection_add_handler( m_clipboardWidget,
160 g_clipboardAtom,
161 g_textAtom,
162 selection_handler,
163 NULL );
164 break;
165 */
166 default:
167 break;
168 }
169 }
170
171 void *wxClipboard::GetData( wxDataFormat format, size_t *length )
172 {
173 if (!IsAvailable(format))
174 {
175 if (length) *length = 0;
176 return NULL;
177 }
178 return NULL;
179 }
180
181 bool wxClipboard::IsAvailable( wxDataFormat WXUNUSED(format) )
182 {
183 return FALSE;
184 }