Debian Codegraph @

pTk/mTk/win/tkWinClipboard.c

raw
1 /*
2 * tkWinClipboard.c --
3 *
4 * This file contains functions for managing the clipboard.
5 *
6 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
7 * Copyright (c) 1998-2000 by Scriptics Corporation.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkWinClipboard.c,v 1.7 2000/04/12 18:51:11 hobbs Exp $
13 */
14
15 #include "tkWinInt.h"
16 #include "tkSelect.h"
17
18 static void UpdateClipboard _ANSI_ARGS_((HWND hwnd));
19
20 /*
21 *----------------------------------------------------------------------
22 *
23 * TkSelGetSelection --
24 *
25 * Retrieve the specified selection from another process. For
26 * now, only fetching XA_STRING from CLIPBOARD is supported.
27 * Eventually other types should be allowed.
28 *
29 * Results:
30 * The return value is a standard Tcl return value.
31 * If an error occurs (such as no selection exists)
32 * then an error message is left in the interp's result.
33 *
34 * Side effects:
35 * None.
36 *
37 *----------------------------------------------------------------------
38 */
39
40 int
41 TkSelGetSelection(interp, tkwin, selection, target, proc, clientData)
42 Tcl_Interp *interp; /* Interpreter to use for reporting
43 * errors. */
44 Tk_Window tkwin; /* Window on whose behalf to retrieve
45 * the selection (determines display
46 * from which to retrieve). */
47 Atom selection; /* Selection to retrieve. */
48 Atom target; /* Desired form in which selection
49 * is to be returned. */
50 Tk_GetXSelProc *proc; /* Procedure to call to process the
51 * selection, once it has been retrieved. */
52 ClientData clientData; /* Arbitrary value to pass to proc. */
53 {
54 char *data, *destPtr;
55 Tcl_DString ds;
56 HGLOBAL handle;
57 Tcl_Encoding encoding;
58 int result, locale;
59
60 if ((selection != Tk_InternAtom(tkwin, "CLIPBOARD"))
61 || (target != XA_STRING)
62 || !OpenClipboard(NULL)) {
63 goto error;
64 }
65
66 /*
67 * Attempt to get the data in Unicode form if available as this is
68 * less work that CF_TEXT.
69 */
70
71 result = TCL_ERROR;
72 if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
73 handle = GetClipboardData(CF_UNICODETEXT);
74 if (!handle) {
75 CloseClipboard();
76 goto error;
77 }
78 data = GlobalLock(handle);
79 Tcl_DStringInit(&ds);
80 Tcl_UniCharToUtfDString((Tcl_UniChar *)data,
81 Tcl_UniCharLen((Tcl_UniChar *)data), &ds);
82 GlobalUnlock(handle);
83 } else if (IsClipboardFormatAvailable(CF_TEXT)) {
84 /*
85 * Determine the encoding to use to convert this text.
86 */
87
88 if (IsClipboardFormatAvailable(CF_LOCALE)) {
89 handle = GetClipboardData(CF_LOCALE);
90 if (!handle) {
91 CloseClipboard();
92 goto error;
93 }
94
95 /*
96 * Get the locale identifier, determine the proper code page
97 * to use, and find the corresponding encoding.
98 */
99
100 Tcl_DStringInit(&ds);
101 Tcl_DStringAppend(&ds, "cp######", -1);
102 data = GlobalLock(handle);
103
104
105 /*
106 * Even though the documentation claims that GetLocaleInfo
107 * expects an LCID, on Windows 9x it really seems to expect
108 * a LanguageID.
109 */
110
111 locale = LANGIDFROMLCID(*((int*)data));
112 GetLocaleInfo(locale, LOCALE_IDEFAULTANSICODEPAGE,
113 Tcl_DStringValue(&ds)+2, Tcl_DStringLength(&ds)-2);
114 GlobalUnlock(handle);
115
116 encoding = Tcl_GetEncoding(NULL, Tcl_DStringValue(&ds));
117 Tcl_DStringFree(&ds);
118 } else {
119 encoding = NULL;
120 }
121
122 /*
123 * Fetch the text and convert it to UTF.
124 */
125
126 handle = GetClipboardData(CF_TEXT);
127 if (!handle) {
128 if (encoding) {
129 Tcl_FreeEncoding(encoding);
130 }
131 CloseClipboard();
132 goto error;
133 }
134 data = GlobalLock(handle);
135 Tcl_ExternalToUtfDString(encoding, data, -1, &ds);
136 GlobalUnlock(handle);
137 if (encoding) {
138 Tcl_FreeEncoding(encoding);
139 }
140
141 } else {
142 CloseClipboard();
143 goto error;
144 }
145
146 /*
147 * Translate CR/LF to LF.
148 */
149
150 data = destPtr = Tcl_DStringValue(&ds);
151 while (*data) {
152 if (data[0] == '\r' && data[1] == '\n') {
153 data++;
154 } else {
155 *destPtr++ = *data++;
156 }
157 }
158 *destPtr = '\0';
159
160 /*
161 * Pass the data off to the selection procedure.
162 */
163
164 result = (*proc)(clientData, interp, (long *) Tcl_DStringValue(&ds),
165 Tcl_DStringLength(&ds),8,target,tkwin);
166 Tcl_DStringFree(&ds);
167 CloseClipboard();
168 return result;
169
170 error:
171 Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection),
172 " selection doesn't exist or form \"",
173 Tk_GetAtomName(tkwin, target),
174 "\" not defined", (char *) NULL);
175 return TCL_ERROR;
176 }
177
178 /*
179 *----------------------------------------------------------------------
180 *
181 * TkSetSelectionOwner --
182 *
183 * This function claims ownership of the specified selection.
184 * If the selection is CLIPBOARD, then we empty the system
185 * clipboard.
186 *
187 * Results:
188 * None.
189 *
190 * Side effects:
191 * Empties the system clipboard, and claims ownership.
192 *
193 *----------------------------------------------------------------------
194 */
195
196 Window
197 XGetSelectionOwner(Display *display, Atom selection)
198 {
199 return None;
200 }
201
202 void
203 XSetSelectionOwner(display, selection, owner, time)
204 Display* display;
205 Atom selection;
206 Window owner;
207 Time time;
208 {
209 HWND hwnd = owner ? TkWinGetHWND(owner) : NULL;
210 Tk_Window tkwin;
211
212 /*
213 * This is a gross hack because the Tk_InternAtom interface is broken.
214 * It expects a Tk_Window, even though it only needs a Tk_Display.
215 */
216
217 tkwin = (Tk_Window) TkGetMainInfoList()->winPtr;
218
219 if (selection == Tk_InternAtom(tkwin, "CLIPBOARD")) {
220
221 /*
222 * Only claim and empty the clipboard if we aren't already the
223 * owner of the clipboard.
224 */
225
226 if (GetClipboardOwner() != hwnd) {
227 UpdateClipboard(hwnd);
228 }
229 }
230 }
231
232 /*
233 *----------------------------------------------------------------------
234 *
235 * TkWinClipboardRender --
236 *
237 * This function supplies the contents of the clipboard in
238 * response to a WM_RENDERFORMAT message.
239 *
240 * Results:
241 * None.
242 *
243 * Side effects:
244 * Sets the contents of the clipboard.
245 *
246 *----------------------------------------------------------------------
247 */
248
249 void
250 TkWinClipboardRender(dispPtr, format)
251 TkDisplay *dispPtr;
252 UINT format;
253 {
254 TkClipboardTarget *targetPtr;
255 TkClipboardBuffer *cbPtr;
256 HGLOBAL handle;
257 char *buffer, *p, *rawText, *endPtr;
258 int length;
259 Tcl_DString ds;
260
261 for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
262 targetPtr = targetPtr->nextPtr) {
263 if (targetPtr->type == XA_STRING)
264 break;
265 }
266
267 /*
268 * Count the number of newlines so we can add space for them in
269 * the resulting string.
270 */
271
272 length = 0;
273 if (targetPtr != NULL) {
274 for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
275 cbPtr = cbPtr->nextPtr) {
276 length += cbPtr->length;
277 for (p = cbPtr->buffer, endPtr = p + cbPtr->length;
278 p < endPtr; p++) {
279 if (*p == '\n') {
280 length++;
281 }
282 }
283 }
284 }
285
286 /*
287 * Copy the data and change EOL characters.
288 */
289
290 buffer = rawText = ckalloc(length + 1);
291 if (targetPtr != NULL) {
292 for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
293 cbPtr = cbPtr->nextPtr) {
294 for (p = cbPtr->buffer, endPtr = p + cbPtr->length;
295 p < endPtr; p++) {
296 if (*p == '\n') {
297 *buffer++ = '\r';
298 }
299 *buffer++ = *p;
300 }
301 }
302 }
303 *buffer = '\0';
304
305 /*
306 * Depending on the platform, turn the data into Unicode or the
307 * system encoding before placing it on the clipboard.
308 */
309
310 if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
311 Tcl_DStringInit(&ds);
312 Tcl_UtfToUniCharDString(rawText, -1, &ds);
313 ckfree(rawText);
314 handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
315 Tcl_DStringLength(&ds)+2);
316 if (!handle) {
317 Tcl_DStringFree(&ds);
318 return;
319 }
320 buffer = GlobalLock(handle);
321 memcpy(buffer, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds) + 2);
322 GlobalUnlock(handle);
323 Tcl_DStringFree(&ds);
324 SetClipboardData(CF_UNICODETEXT, handle);
325 } else {
326 Tcl_UtfToExternalDString(NULL, rawText, -1, &ds);
327 ckfree(rawText);
328 handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
329 Tcl_DStringLength(&ds)+1);
330 if (!handle) {
331 Tcl_DStringFree(&ds);
332 return;
333 }
334 buffer = GlobalLock(handle);
335 memcpy(buffer, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds) + 1);
336 GlobalUnlock(handle);
337 Tcl_DStringFree(&ds);
338 SetClipboardData(CF_TEXT, handle);
339 }
340
341 return;
342 }
343
344 /*
345 *----------------------------------------------------------------------
346 *
347 * TkSelUpdateClipboard --
348 *
349 * This function is called to force the clipboard to be updated
350 * after new data is added.
351 *
352 * Results:
353 * None.
354 *
355 * Side effects:
356 * Clears the current contents of the clipboard.
357 *
358 *----------------------------------------------------------------------
359 */
360
361 void
362 TkSelUpdateClipboard(winPtr, targetPtr)
363 TkWindow *winPtr;
364 TkClipboardTarget *targetPtr;
365 {
366 HWND hwnd = TkWinGetHWND(winPtr->window);
367 UpdateClipboard(hwnd);
368 }
369
370 /*
371 *----------------------------------------------------------------------
372 *
373 * UpdateClipboard --
374 *
375 * Take ownership of the clipboard, clear it, and indicate to the
376 * system the supported formats.
377 *
378 * Results:
379 * None.
380 *
381 * Side effects:
382 * None.
383 *
384 *----------------------------------------------------------------------
385 */
386
387 static void
388 UpdateClipboard(hwnd)
389 HWND hwnd;
390 {
391 TkWinUpdatingClipboard(TRUE);
392 OpenClipboard(hwnd);
393 EmptyClipboard();
394
395 /*
396 * CF_UNICODETEXT is only supported on NT, but it it is prefered
397 * when possible.
398 */
399
400 if (TkWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
401 SetClipboardData(CF_UNICODETEXT, NULL);
402 } else {
403 SetClipboardData(CF_TEXT, NULL);
404 }
405 CloseClipboard();
406 TkWinUpdatingClipboard(FALSE);
407 }
408
409 /*
410 *--------------------------------------------------------------
411 *
412 * TkSelEventProc --
413 *
414 * This procedure is invoked whenever a selection-related
415 * event occurs.
416 *
417 * Results:
418 * None.
419 *
420 * Side effects:
421 * Lots: depends on the type of event.
422 *
423 *--------------------------------------------------------------
424 */
425
426 void
427 TkSelEventProc(tkwin, eventPtr)
428 Tk_Window tkwin; /* Window for which event was
429 * targeted. */
430 register XEvent *eventPtr; /* X event: either SelectionClear,
431 * SelectionRequest, or
432 * SelectionNotify. */
433 {
434 if (eventPtr->type == SelectionClear) {
435 TkSelClearSelection(tkwin, eventPtr);
436 }
437 }
438
439 /*
440 *----------------------------------------------------------------------
441 *
442 * TkSelPropProc --
443 *
444 * This procedure is invoked when property-change events
445 * occur on windows not known to the toolkit. This is a stub
446 * function under Windows.
447 *
448 * Results:
449 * None.
450 *
451 * Side effects:
452 * None.
453 *
454 *----------------------------------------------------------------------
455 */
456
457 void
458 TkSelPropProc(eventPtr)
459 register XEvent *eventPtr; /* X PropertyChange event. */
460 {
461 }