2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: DndText.c /main/5 1996/08/28 17:46:27 drk $ */
24 /*********************************************************************
28 * Description: Implementation of the Text Transfer routines
29 * for the DND Convenience API.
31 *********************************************************************
35 * RESTRICTED CONFIDENTIAL INFORMATION:
37 * The information in this document is subject to special
38 * restrictions in a confidential disclosure agreement bertween
39 * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
40 * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
41 * Sun's specific written approval. This documment and all copies
42 * and derivative works thereof must be returned or destroyed at
45 * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
47 * (c) Copyright 1993, 1994 Hewlett-Packard Company
48 * (c) Copyright 1993, 1994 International Business Machines Corp.
49 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
50 * (c) Copyright 1993, 1994 Novell, Inc.
57 #include <X11/Intrinsic.h>
58 #include <Xm/AtomMgr.h>
60 #include <Xm/DropSMgr.h>
61 #include <Xm/DropTrans.h>
64 #include "DtSvcLock.h"
67 * Text Transfer Function Prototypes
70 static void dndTextGetTargets(Atom**, Cardinal*);
71 static void dndTextGetAvailTargets(DtDragInfo*, Atom**, Cardinal*);
72 static void dndTextGetExportTargets(DtDragInfo*, Atom**, Cardinal*);
73 static void dndTextGetImportTargets(DtDropInfo*, Atom**, Cardinal*);
74 static void dndTextConvertInit(DtDragInfo*);
75 static Boolean dndTextConvert(Widget, DtDragInfo*, Atom*, Atom*,
76 Atom*, XtPointer*, unsigned long*, int*,
77 XSelectionRequestEvent*);
78 static void dndTextConvertFinish(DtDragInfo*);
79 static void dndTextTransferTargets(DtDropInfo*,
80 Atom*, Cardinal, Atom**, Cardinal*);
81 static void dndTextTransfer(Widget, DtDropInfo*, Atom*, Atom*,
82 Atom*, XtPointer, unsigned long*, int*);
83 static void dndTextTransferFinish(DtDropInfo*);
86 * Text Transfer Selection Target
89 static Atom XA_COMPOUND_TEXT;
90 static Boolean dndPreferString;
93 * Text Transfer ProtocolInfo
96 static DtDndMethods dndTextTransferProtocol = {
97 "DtDndTextTransfer", /* name */
98 (DtDndProtocol)DtDND_TEXT_TRANSFER, /* protocol */
99 DtDND_DRAG_SOURCE_TEXT, /* sourceType */
100 dndTextGetAvailTargets, /* getAvailTargets */
101 dndTextGetExportTargets, /* getExportTargets */
102 dndTextGetImportTargets, /* getImportTargets */
103 dndTextConvertInit, /* convertInit */
104 dndTextConvert, /* convert */
105 dndTextConvertFinish, /* convertFinish */
106 dndTextTransferTargets, /* transferTargets */
107 dndTextTransfer, /* transfer */
108 dndTextTransferFinish, /* transferFinish */
112 * Text transfer protocol initialization
115 _DtDndTextTransferProtocolInitialize(
119 if (XA_COMPOUND_TEXT == 0) {
120 XA_COMPOUND_TEXT = DtGetAtom(display, "COMPOUND_TEXT");
121 dndPreferString = (strcmp(setlocale(LC_CTYPE,NULL),"C") == 0);
123 printf("locale = %s, dndPreferString = %d\n",
124 setlocale(LC_CTYPE,NULL), dndPreferString);
127 _DtSvcProcessUnlock();
129 return &dndTextTransferProtocol;
133 * Returns generic export/import targets for text transfers
138 Cardinal * numTargets)
144 *targets = (Atom *)XtMalloc(*numTargets * sizeof(Atom));
146 (*targets)[ii++] = XA_COMPOUND_TEXT;
147 (*targets)[ii++] = XA_TEXT;
148 (*targets)[ii++] = XA_STRING;
152 * Returns available targets for text transfers
155 dndTextGetAvailTargets(
156 DtDragInfo * dtDragInfo,
157 Atom ** availTargets,
158 Cardinal * numAvailTargets)
160 dndTextGetTargets(availTargets, numAvailTargets);
164 * Returns export targets for text transfers
167 dndTextGetExportTargets(
168 DtDragInfo * dtDragInfo,
169 Atom ** exportTargets,
170 Cardinal * numExportTargets)
172 dndTextGetTargets(exportTargets, numExportTargets);
176 * Returns import targets for text transfers
179 dndTextGetImportTargets(
180 DtDropInfo * dtDropInfo,
181 Atom ** importTargets,
182 Cardinal * numImportTargets)
184 dndTextGetTargets(importTargets, numImportTargets);
188 * Initialize protocol specific part of drag data
192 DtDragInfo * dtDragInfo)
194 DtDndContext * dragData = dtDragInfo->dragData;
196 dragData->data.strings = (XmString *)
197 XtMalloc(dragData->numItems * sizeof(XmString));
201 * Convert the motif strings into selection data
206 DtDragInfo * dtDragInfo,
210 XtPointer * returnValue,
211 unsigned long * returnLength,
213 XSelectionRequestEvent * selectionRequestEvent)
215 DtDndContext * dragData = dtDragInfo->dragData;
216 Display * dpy = XtDisplayOfObject(dragContext);
217 XmString * stringList;
219 XTextProperty textProp;
220 XmICCEncodingStyle encStyle;
224 * Select the text encoding style; reject unknown targets
227 if (*target == XA_COMPOUND_TEXT) {
228 encStyle = XmSTYLE_COMPOUND_TEXT;
229 } else if (*target == XA_STRING) {
230 encStyle = XmSTYLE_STRING;
231 } else if (*target == XA_TEXT) {
232 encStyle = XmSTYLE_TEXT;
238 * Convert the XmString list into a string list
241 numStrings = dragData->numItems;
242 stringList = dragData->data.strings;
244 status = XmCvtXmStringTableToTextProperty(dpy, stringList, numStrings,
245 encStyle, &textProp);
246 if (status != Success)
250 * Return the text property
253 *returnType = textProp.encoding;
254 *returnValue = (XtPointer)textProp.value;
255 *returnLength = textProp.nitems;
256 *returnFormat = textProp.format;
262 * Clean up from the convert init proc
265 dndTextConvertFinish(
266 DtDragInfo * dtDragInfo)
268 DtDndContext * dragData = dtDragInfo->dragData;
270 if (dragData->data.strings) {
271 XtFree((char *)dragData->data.strings);
272 dragData->data.strings = NULL;
277 * Returns the transfer targets selected from the export targets
280 dndTextTransferTargets(
281 DtDropInfo * dtDropInfo,
282 Atom * exportTargets,
283 Cardinal numExportTargets,
284 Atom ** transferTargets,
285 Cardinal * numTransferTargets)
287 Boolean foundCT, foundText, foundString;
291 foundCT = foundText = foundString = False;
293 for (ii = 0; ii < numExportTargets; ii++) {
294 if (exportTargets[ii] == XA_COMPOUND_TEXT) {
296 } else if (exportTargets[ii] == XA_TEXT) {
298 } else if (exportTargets[ii] == XA_STRING) {
303 if (dndPreferString && foundString) {
305 } else if (foundCT) {
306 target = XA_COMPOUND_TEXT;
307 } else if (foundText) {
309 } else if (foundString) {
312 *numTransferTargets = 0;
313 *transferTargets = NULL;
317 *numTransferTargets = 1;
319 *transferTargets = (Atom *)XtMalloc(*numTransferTargets * sizeof(Atom));
321 (*transferTargets)[0] = target;
325 * Transfer the selection data into motif strings
330 DtDropInfo * dtDropInfo,
335 unsigned long * length,
338 Display * display = XtDisplayOfObject(dropTransfer);
339 DtDndContext * dropData = dtDropInfo->dropData;
340 XmString * stringList;
341 XTextProperty textProp;
343 int ii, status, textCount;
346 * Ignore transfers we don't understand or if we've already transferred
349 if (value == NULL || dropData->data.strings ||
350 (*target != XA_COMPOUND_TEXT &&
351 *target != XA_TEXT &&
352 *target != XA_STRING) ) {
359 * Convert the text property to a text list
362 textProp.value = (unsigned char *)value;
363 textProp.encoding = *type;
364 textProp.format = *format;
365 textProp.nitems = *length;
367 status = XmbTextPropertyToTextList(display, &textProp,
370 XtFree((char *)value);
372 if (status != Success) {
373 dtDropInfo->status = DtDND_FAILURE;
378 * Convert the text list into a XmString list
381 stringList = (XmString *)XtMalloc(textCount * sizeof(XmString));
383 for (ii = 0; ii < textCount; ii++) {
384 stringList[ii] = XmStringCreateLocalized(text[ii]);
387 XFreeStringList(text);
389 dropData->numItems = textCount;
390 dropData->data.strings = stringList;
394 * Clean up from the transfer proc
397 dndTextTransferFinish(
398 DtDropInfo * dtDropInfo)
400 DtDndContext * dropData = dtDropInfo->dropData;
403 for (ii = 0; ii < dropData->numItems; ii++) {
404 XmStringFree(dropData->data.strings[ii]);
406 XtFree((char *)dropData->data.strings);