]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/clipbrd.cpp
Flicker war won.
[wxWidgets.git] / src / gtk1 / clipbrd.cpp
CommitLineData
dc86cb34
RR
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
20wxClipboard *wxTheClipboard = (wxClipboard*) NULL;
21
fd0eed64
RR
22GdkAtom g_textAtom = 0;
23GdkAtom g_clipboardAtom = 0;
24
dc86cb34
RR
25//-----------------------------------------------------------------------------
26// functions
27//-----------------------------------------------------------------------------
28
29void wxInitClipboard()
30{
31 if (wxTheClipboard) delete wxTheClipboard;
32 wxTheClipboard = new wxClipboard();
33}
34
35void wxDoneClipboard()
36{
37 if (wxTheClipboard) delete wxTheClipboard;
38 wxTheClipboard = (wxClipboard*) NULL;
39}
40
41//-----------------------------------------------------------------------------
42// "selection_received"
43//-----------------------------------------------------------------------------
44
fd0eed64
RR
45static void
46selection_received( GtkWidget *widget, GtkSelectionData *selection_data, gpointer data )
dc86cb34
RR
47{
48}
fd0eed64
RR
49
50//-----------------------------------------------------------------------------
51// "selection_clear"
52//-----------------------------------------------------------------------------
53
54static gint
55selection_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
71static void
72selection_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}
dc86cb34
RR
98
99//-----------------------------------------------------------------------------
100// wxClipboard
101//-----------------------------------------------------------------------------
102
103IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
104
105wxClipboard::wxClipboard()
106{
fd0eed64 107 m_data = (wxDataObject*) NULL;
dc86cb34
RR
108 m_clipboardWidget = gtk_window_new( GTK_WINDOW_POPUP );
109 gtk_widget_realize( m_clipboardWidget );
fd0eed64
RR
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 );
dc86cb34
RR
118}
119
120wxClipboard::~wxClipboard()
121{
fd0eed64
RR
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 }
dc86cb34
RR
130 if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
131}
132
133void wxClipboard::SetData( wxDataObject *data )
134{
135 if (m_data) delete m_data;
136 m_data = data;
fd0eed64
RR
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;
fd0eed64
RR
158 case wxDF_TEXT:
159 gtk_selection_add_handler( m_clipboardWidget,
160 g_clipboardAtom,
161 g_textAtom,
162 selection_handler,
163 NULL );
164 break;
38c7b3d3 165*/
fd0eed64
RR
166 default:
167 break;
168 }
dc86cb34
RR
169}
170
171void *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
181bool wxClipboard::IsAvailable( wxDataFormat WXUNUSED(format) )
182{
183 return FALSE;
184}