Debian Codegraph @

src/af/xap/gtk/xap_UnixClipboard.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (c) 2002 Dom Lachowicz
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <string.h>
21 #include "xap_UnixClipboard.h"
22 #include "xap_Frame.h"
23 #include "xav_View.h"
24
25 //////////////////////////////////////////////////////////////////
26 //////////////////////////////////////////////////////////////////
27
28 GtkClipboard * XAP_UnixClipboard::gtkClipboardForTarget(XAP_UnixClipboard::_T_AllowGet get)
29 {
30 if (XAP_UnixClipboard::TAG_ClipboardOnly == get)
31 return m_clip;
32 else if (XAP_UnixClipboard::TAG_PrimaryOnly == get)
33 return m_primary;
34 return 0;
35 }
36
37 static AV_View * viewFromApp(XAP_App * pApp)
38 {
39 XAP_Frame * pFrame = pApp->getLastFocussedFrame();
40 if ( !pFrame )
41 return 0 ;
42 return pFrame->getCurrentView () ;
43 }
44
45 //////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////
47
48 XAP_UnixClipboard::XAP_UnixClipboard(XAP_UnixApp * pUnixApp)
49 : m_pUnixApp(pUnixApp), m_Targets(0), m_nTargets(0)
50 {
51 m_clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
52 m_primary = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
53 }
54
55 XAP_UnixClipboard::~XAP_UnixClipboard()
56 {
57 clearData(true,true);
58 g_free(m_Targets);
59 }
60
61 //////////////////////////////////////////////////////////////////
62 //////////////////////////////////////////////////////////////////
63
64 void XAP_UnixClipboard::AddFmt(const char * szFormat)
65 {
66 UT_return_if_fail(szFormat && strlen(szFormat));
67 m_vecFormat_AP_Name.addItem(szFormat);
68 m_vecFormat_GdkAtom.addItem(gdk_atom_intern(szFormat,FALSE));
69 }
70
71 void XAP_UnixClipboard::deleteFmt(const char * szFormat)
72 {
73 UT_return_if_fail(szFormat && strlen(szFormat));
74 UT_sint32 item = m_vecFormat_AP_Name.findItem(szFormat);
75 m_vecFormat_AP_Name.deleteNthItem(item);
76 m_vecFormat_GdkAtom.findItem(gdk_atom_intern(szFormat,FALSE));
77 m_vecFormat_GdkAtom.deleteNthItem(item);
78 }
79
80 void XAP_UnixClipboard::initialize()
81 {
82 m_nTargets = m_vecFormat_AP_Name.getItemCount();
83 m_Targets = g_new0(GtkTargetEntry, m_nTargets);
84
85 for (int k = 0, kLimit = m_nTargets; (k < kLimit); k++)
86 {
87 GtkTargetEntry * target = &(m_Targets[k]);
88 target->target = (gchar*)m_vecFormat_AP_Name.getNthItem(k);
89 target->info = k;
90 }
91 }
92
93 //////////////////////////////////////////////////////////////////
94 //////////////////////////////////////////////////////////////////
95
96 void XAP_UnixClipboard::common_get_func(GtkClipboard * /*clipboard*/,
97 GtkSelectionData *selection_data,
98 guint /*info*/, T_AllowGet which)
99 {
100 XAP_FakeClipboard & which_clip = ( which == TAG_ClipboardOnly ? m_fakeClipboard : m_fakePrimaryClipboard );
101
102 // if this is for PRIMARY, we need to copy the current selection
103 // else this is for CLIPBOARD and the data is already copied; do nothing
104 if (which == TAG_PrimaryOnly)
105 {
106 // will only get the view from the last focussed frame, and not some offscreen
107 // (print, format painter) view. this is fine, since we're operating on PRIMARY
108 AV_View * pView = viewFromApp(m_pUnixApp);
109 if (!pView)
110 return; // race condition - have request for data but no view. fail harmlessly
111 pView->cmdCopy(false);
112 }
113
114 guint ntargets = m_vecFormat_GdkAtom.getItemCount();
115
116 GdkAtom needle = gtk_selection_data_get_target(selection_data);
117 for (guint i = 0 ; i < ntargets ; i++)
118 {
119 if (needle == m_vecFormat_GdkAtom.getNthItem(i))
120 {
121 const gchar * format_name = m_vecFormat_AP_Name.getNthItem(i);
122
123 if(which_clip.hasFormat(format_name))
124 {
125 guchar * data = 0;
126 UT_uint32 data_len = 0;
127
128 guchar **pdata = &data;
129 which_clip.getClipboardData(format_name,reinterpret_cast<void**>(pdata),&data_len);
130 gtk_selection_data_set(selection_data,needle,8, data, data_len);
131 }
132 break; // success or failure, we've found the needle in the haystack so exit the loop
133 }
134 }
135 }
136
137 void XAP_UnixClipboard::primary_clear_func (GtkClipboard * /*clipboard*/)
138 {
139 }
140
141 void XAP_UnixClipboard::clipboard_clear_func (GtkClipboard * /*clipboard*/)
142 {
143 }
144
145 void XAP_UnixClipboard::primary_get_func(GtkClipboard *clipboard,
146 GtkSelectionData *selection_data,
147 guint info)
148 {
149 common_get_func(clipboard, selection_data, info, TAG_PrimaryOnly);
150 }
151
152 void XAP_UnixClipboard::clipboard_get_func(GtkClipboard *clipboard,
153 GtkSelectionData *selection_data,
154 guint info)
155 {
156 common_get_func(clipboard, selection_data, info, TAG_ClipboardOnly);
157 }
158
159 //////////////////////////////////////////////////////////////////
160 //////////////////////////////////////////////////////////////////
161
162 bool XAP_UnixClipboard::assertSelection()
163 {
164 return (gtk_clipboard_set_with_data (gtkClipboardForTarget(TAG_PrimaryOnly),
165 m_Targets,
166 m_nTargets,
167 s_primary_get_func,
168 s_primary_clear_func,
169 this) == TRUE);
170 }
171
172 bool XAP_UnixClipboard::addData(T_AllowGet tFrom, const char* format, const void* pData, UT_sint32 iNumBytes)
173 {
174 if(tFrom == TAG_PrimaryOnly)
175 return m_fakePrimaryClipboard.addData(format,pData,iNumBytes);
176 else
177 {
178 if(!m_fakeClipboard.addData(format,pData,iNumBytes))
179 return false;
180
181 return true;
182 }
183 }
184
185 void XAP_UnixClipboard::finishedAddingData(void)
186 {
187 gtk_clipboard_set_with_data (gtkClipboardForTarget(TAG_ClipboardOnly),
188 m_Targets,
189 m_nTargets,
190 s_clipboard_get_func,
191 s_clipboard_clear_func,
192 this);
193
194 gtk_clipboard_set_can_store (gtkClipboardForTarget(TAG_ClipboardOnly), m_Targets, m_nTargets);
195 }
196
197 void XAP_UnixClipboard::clearData(bool bClipboard, bool bPrimary)
198 {
199 if (bClipboard)
200 {
201 gtk_clipboard_clear (gtkClipboardForTarget (TAG_ClipboardOnly));
202 m_fakeClipboard.clearClipboard();
203 }
204
205 if (bPrimary)
206 {
207 gtk_clipboard_clear(gtkClipboardForTarget (TAG_PrimaryOnly));
208 m_fakePrimaryClipboard.clearClipboard();
209 }
210 }
211
212 bool XAP_UnixClipboard::getData(T_AllowGet tFrom, const char** formatList,
213 void ** ppData, UT_uint32 * pLen,
214 const char **pszFormatFound)
215 {
216 // Fetch data from the clipboard (using the allowable source(s)) in one of
217 // the prioritized list of formats. Return pointer to clipboard's buffer.
218 *pszFormatFound = NULL;
219 *ppData = NULL;
220 *pLen = 0;
221 if (TAG_ClipboardOnly == tFrom)
222 return _getDataFromServer(tFrom,formatList,ppData,pLen,pszFormatFound);
223 else if (TAG_PrimaryOnly == tFrom)
224 return _getDataFromServer(tFrom,formatList,ppData,pLen,pszFormatFound);
225 else
226 return false;
227 }
228
229 bool XAP_UnixClipboard::getTextData(T_AllowGet tFrom, void ** ppData,
230 UT_uint32 * pLen)
231 {
232 // start out pessimistic
233 *ppData = NULL;
234 *pLen = 0;
235
236 GtkClipboard * clippy = gtkClipboardForTarget (tFrom);
237
238 char * txt = gtk_clipboard_wait_for_text (clippy);
239
240 if (!txt)
241 return false;
242
243 size_t len = strlen (txt);
244 if (!len)
245 return false;
246
247 XAP_FakeClipboard & which_clip = ( tFrom == TAG_ClipboardOnly ? m_fakeClipboard : m_fakePrimaryClipboard );
248
249 which_clip.addData("text/plain",txt,len);
250
251 g_free (txt);
252
253 // ignored
254 const char * pszFormatFound = NULL;
255
256 static const char * txtFormatList [] = {
257 "text/plain",
258 0
259 };
260
261 return _getDataFromFakeClipboard(tFrom, txtFormatList, ppData, pLen, &pszFormatFound);
262 }
263
264 //////////////////////////////////////////////////////////////////
265 //////////////////////////////////////////////////////////////////
266
267 bool XAP_UnixClipboard::_getDataFromFakeClipboard(T_AllowGet tFrom, const char** formatList,
268 void ** ppData, UT_uint32 * pLen,
269 const char **pszFormatFound)
270 {
271 XAP_FakeClipboard & which_clip = ( tFrom == TAG_ClipboardOnly ? m_fakeClipboard : m_fakePrimaryClipboard );
272
273 for (int k=0; (formatList[k]); k++)
274 if (which_clip.getClipboardData(formatList[k],ppData,pLen))
275 {
276 *pszFormatFound = formatList[k];
277 return true;
278 }
279
280 // should never happen since this is our internal buffer
281 return false;
282 }
283
284
285 #if DEBUG
286 static void allTargets(GtkClipboard * /*clipboard*/,
287 GdkAtom *atoms,
288 gint n_atoms,
289 gpointer /*data*/)
290 {
291 gint i= 0;
292 UT_DEBUGMSG(("Found %d target atoms \n", n_atoms));
293 for(i=0;i<n_atoms;i++)
294 {
295 GdkAtom Atom = atoms[i];
296 auto name = gdk_atom_name(Atom);
297 UT_DEBUGMSG((" Found Atom %s on clipboard \n", name));
298 g_free(name);
299 }
300 }
301 #endif
302
303 bool XAP_UnixClipboard::_getDataFromServer(T_AllowGet tFrom, const char** formatList,
304 void ** ppData, UT_uint32 * pLen,
305 const char **pszFormatFound)
306 {
307 bool rval = false;
308 if(formatList == NULL)
309 return false;
310
311 GtkClipboard * clipboard = gtkClipboardForTarget (tFrom);
312 #if DEBUG
313 gtk_clipboard_request_targets(clipboard,( GtkClipboardTargetsReceivedFunc) allTargets, this);
314 #endif
315 UT_GenericVector<GdkAtom> atoms ;
316 for(int atomCounter = 0; formatList[atomCounter]; atomCounter++)
317 atoms.addItem(gdk_atom_intern(formatList[atomCounter],FALSE));
318
319 int len = atoms.size () ;
320
321 // for(int i = 0; i < len && !rval; i++)
322 for(int i = 0; i < len; i++)
323 {
324 GdkAtom atom = atoms.getNthItem(i);
325 GtkSelectionData * selection = gtk_clipboard_wait_for_contents (clipboard, atom);
326 UT_DEBUGMSG(("Looking for %s on clipbaord \n",formatList[i]));
327 if(selection)
328 {
329 if (gtk_selection_data_get_data(selection) && (gtk_selection_data_get_length(selection) > 0))
330 {
331 if(!rval)
332 {
333 m_databuf.truncate(0);
334 *pLen = gtk_selection_data_get_length(selection);
335 m_databuf.append(static_cast<const UT_Byte *>(gtk_selection_data_get_data(selection)),
336 *pLen);
337 *ppData = (void *)(m_databuf.getPointer(0));
338 *pszFormatFound = formatList[i];
339 rval = true;
340 }
341 UT_DEBUGMSG(("Found format %s on clipbaord \n",formatList[i]));
342 }
343 gtk_selection_data_free(selection);
344 }
345 }
346
347 return rval;
348 }
349
350 //////////////////////////////////////////////////////////////////
351 //////////////////////////////////////////////////////////////////
352
353 bool XAP_UnixClipboard::canPaste(T_AllowGet tFrom)
354 {
355 #if 0
356 bool found = false;
357 GtkClipboard * clipboard = gtkClipboardForTarget(tFrom);
358 GtkSelectionData * selection = gtk_clipboard_wait_for_contents (clipboard, gdk_atom_intern("TARGETS", FALSE));
359
360 if (selection)
361 {
362 gint abi_targets = m_vecFormat_GdkAtom.getItemCount();
363
364 GdkAtom *targets;
365 gint clipboard_targets;
366
367 if (gtk_selection_data_get_targets (selection, &targets, &clipboard_targets))
368 {
369 for (gint i = 0; (i < abi_targets) && !found; i++)
370 {
371 GdkAtom needle = m_vecFormat_GdkAtom.getNthItem(i);
372 for (gint j = 0; (j < clipboard_targets) && !found; j++)
373 if (targets[j] == needle)
374 found = true;
375 }
376
377 g_free (targets);
378 }
379 gtk_selection_data_free(selection);
380 }
381
382 return found;
383 #else
384 UT_UNUSED(tFrom);
385 return true;
386 #endif
387 }