Debian Codegraph @

texk/xdvik/gui/selection.c

raw
1 /*
2 * Copyright (c) 2004 Stefan Ulrich
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "xdvi-config.h"
24 #include "xdvi.h"
25
26 #include <string.h>
27
28 #include <X11/Xatom.h>
29 #include <X11/StringDefs.h>
30
31 #if HAVE_X11_XMU_XMU_H
32 #include <X11/Xmu/Atoms.h>
33 #include <X11/Xmu/Xmu.h>
34 #endif
35
36 #include "events.h"
37 #include "util.h"
38 #include "encodings.h"
39 #include "message-window.h"
40 #include "selection.h"
41 #include "statusline.h"
42
43 /*
44 * Set the current X selection (i.e. the primary selection AKA XA_PRIMARY).
45 * Most of this is copied from Asente/Converse/Swick: X Window System Toolkit Manual
46 */
47
48 /* This is file scope since deliver_selection_cb() needs access to it.
49 * I guess we could also store it in an Atom, but why bother ...
50 */
51 static char *m_selection_text = NULL;
52 static char *m_cvt_selection_text = NULL;
53 static size_t m_selection_size = 0;
54
55 /* helper routine */
56 static Atom
57 fetch_atom(Widget w, const char *name)
58 {
59 Atom a;
60 XrmValue source, dest;
61
62 source.size = strlen(name) + 1;
63 source.addr = (char *)name;
64 dest.size = sizeof a;
65 dest.addr = (caddr_t)&a;
66
67 (void)XtConvertAndStore(w, XtRString, &source, XtRAtom, &dest);
68 return a;
69 }
70
71 /* Lose the selection */
72 static void
73 lose_selection_cb(Widget w, Atom *selection)
74 {
75 UNUSED(w);
76 UNUSED(selection);
77
78 text_change_region(TEXT_SEL_CLEAR, NULL);
79 }
80
81 static char *
82 utf8_to_native_encoding(const char *utf8)
83 {
84 static const char *text_encoding = NULL;
85
86 /* convert to user encoding, similar to search-internal.c */
87 if (text_encoding == NULL) {
88 text_encoding = get_text_encoding();
89 }
90
91 if (memicmp(text_encoding, "iso-8859-1", strlen("iso-8859-1")) == 0
92 || memicmp(text_encoding, "iso8859-1", strlen("iso8859-1")) == 0) {
93 return str_utf8_to_iso_8859_1(utf8);
94 }
95 else if (memicmp(text_encoding, "utf-8", strlen("utf-8")) != 0
96 && memicmp(text_encoding, "utf8", strlen("utf8")) != 0) {
97 /* some other encoding */
98 return iconv_convert_string("utf-8", text_encoding, utf8);
99 }
100 /* fallback */
101 return xstrdup(utf8);
102 }
103
104 /* Deliver the selection. Only supports XA_STRING. */
105 static Boolean
106 deliver_selection_cb(Widget w,
107 Atom *selection,
108 Atom *target,
109 Atom *type,
110 XtPointer *value,
111 unsigned long *length,
112 int *format)
113 {
114 Atom targets = fetch_atom(w, "TARGETS");
115 Atom utf8_string = fetch_atom(w, "UTF8_STRING");
116
117 TRACE_GUI((stderr, "selection target = %lu (%s)",
118 *target, XGetAtomName(DISP, *target)));
119
120 if (m_selection_text == NULL) /* paranoia */
121 return False;
122
123 #if HAVE_X11_XMU_XMU_H
124 if (*target == targets) {
125 /* TARGETS handling copied from xclipboard.c */
126 Atom* targetP;
127 XPointer std_targets; /* was: Atom* */
128 unsigned long std_length;
129 XSelectionRequestEvent* req = XtGetSelectionRequest(w, *selection, (XtRequestId)NULL);
130
131 TRACE_GUI((stderr, "Selection type: targets"));
132 XmuConvertStandardSelection(w, req->time, selection,
133 target, type, &std_targets, &std_length, format);
134 *value = XtMalloc(sizeof(Atom)*(std_length + 2));
135 targetP = *(Atom**)value;
136 *length = std_length + 2;
137 *targetP++ = XA_COMPOUND_TEXT(DISP);
138 *targetP++ = XA_STRING;
139 memcpy((char*)targetP, (char*)std_targets, sizeof(Atom)*std_length);
140 XtFree((char*)std_targets);
141 *type = XA_ATOM;
142 *format = sizeof(Atom) * 8;
143 return True;
144 }
145 else
146 #endif
147 {
148 if (*target == utf8_string) {
149 TRACE_GUI((stderr, "Selection type: UTF8_STRING"));
150 *type = *target;
151 *value = (XtPointer)XtNewString(m_selection_text);
152 /* *value = (XtPointer)m_selection_text; */
153 *length = strlen(m_selection_text);
154 *format = 8;
155 return True;
156 }
157 else if (*target == XA_STRING) {
158 char *ptr = utf8_to_native_encoding(m_selection_text);
159 TRACE_GUI((stderr, "Selection type: XA_STRING"));
160 strncpy(m_cvt_selection_text, ptr, m_selection_size);
161 m_cvt_selection_text[m_selection_size - 1] = '\0'; /* ensure termination */
162 free(ptr);
163 *type = *target;
164 *value = (XtPointer)XtNewString(m_cvt_selection_text);
165 /* *value = (XtPointer)m_selection_text; */
166 *length = strlen(m_cvt_selection_text);
167 *format = 8;
168 return True;
169 }
170 #if HAVE_X11_XMU_XMU_H
171 else if (*target == XA_COMPOUND_TEXT(DISP) || *target == XA_TEXT(DISP)) {
172 const char *cl[1];
173 char *ptr;
174 int retval;
175
176 XTextProperty ct;
177 XICCEncodingStyle style = XStdICCTextStyle;
178
179 TRACE_GUI((stderr, "Selection type: XA_COMPOUND_TEXT"));
180 ptr = utf8_to_native_encoding(m_selection_text);
181 strncpy(m_cvt_selection_text, ptr, m_selection_size);
182 m_cvt_selection_text[m_selection_size - 1] = '\0'; /* ensure termination */
183 cl[0] = m_cvt_selection_text;
184
185 *type = *target;
186 retval = XmbTextListToTextProperty(DISP, (char **)cl, 1, style, &ct);
187
188 if (retval == XNoMemory || retval == XLocaleNotSupported || retval == XConverterNotFound) {
189 statusline_info(STATUS_MEDIUM, "XmbTextListToTextProperty failed: %d", retval);
190 return False;
191 }
192
193 *value = ct.value;
194 *length = ct.nitems;
195 *format = 8;
196 return True;
197 }
198 #endif
199 else {
200 #if HAVE_X11_XMU_XMU_H
201 TRACE_GUI((stderr, "Selection type: standard selection"));
202 if (XmuConvertStandardSelection(w, CurrentTime, selection,
203 target, type, (XPointer *)value, length, format))
204 return True;
205 else {
206 #endif
207 TRACE_GUI((stderr, "Selection type unsupported: %lu (%s)",
208 (unsigned long)*target, XGetAtomName(DISP, *target)));
209 statusline_error(STATUS_MEDIUM,
210 "X client asked for an unsupported selection target type: %lu (%s)",
211 (unsigned long)*target, XGetAtomName(DISP, *target));
212 return False ;
213 #if HAVE_X11_XMU_XMU_H
214 }
215 #endif
216 }
217 }
218 }
219
220 /* do it */
221 Boolean
222 set_selection(const char *text, Widget w)
223 {
224 /* caller should make sure that text is never longer than 4 * XMaxRequestSize(DISP) - 32 */
225 if (m_selection_text == NULL) {
226 m_selection_size = 4 * XMaxRequestSize(DISP);
227 m_selection_text = xmalloc(m_selection_size);
228 m_cvt_selection_text = xmalloc(m_selection_size);
229 }
230 strncpy(m_selection_text, text, m_selection_size);
231 m_selection_text[m_selection_size - 1] = '\0'; /* ensure termination */
232
233 return XtOwnSelection(w, XA_PRIMARY, XtLastTimestampProcessed(XtDisplay(w)),
234 deliver_selection_cb,
235 lose_selection_cb,
236 (XtSelectionDoneProc)NULL);
237 }
238
239 void
240 unset_selection(Widget w)
241 {
242 XtDisownSelection(w, XA_PRIMARY, XtLastTimestampProcessed(XtDisplay(w)));
243 }
244