]> git.decadent.org.uk Git - ion3.git/blob - ioncore/selection.c
Merge commit '20070708' into HEAD
[ion3.git] / ioncore / selection.c
1 /*
2  * ion/ioncore/selection.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <X11/Xmd.h>
10 #include <string.h>
11
12 #include "common.h"
13 #include "global.h"
14 #include "property.h"
15 #include "xwindow.h"
16 #include <libextl/extl.h>
17
18
19 static char *selection_data=NULL;
20 static int selection_length;
21 static bool continuation_set=FALSE;
22 static ExtlFn continuation;
23
24 #define CLIPATOM(X) XA_PRIMARY
25
26 static Atom XA_COMPOUND_TEXT(Display *unused)
27 {
28     static Atom a=None;
29     
30     if(a==None)
31         a=XInternAtom(ioncore_g.dpy, "COMPOUND_TEXT", False);
32         
33     return a;
34 }
35
36
37 void ioncore_handle_selection_request(XSelectionRequestEvent *ev)
38 {
39     XSelectionEvent sev;
40     XTextProperty prop;
41     const char *p[1];
42     bool ok=FALSE;
43     
44     sev.property=None;
45     sev.target=None;
46     
47     if(selection_data==NULL || ev->property==None)
48         goto refuse;
49     
50     p[0]=selection_data;
51     
52     if(!ioncore_g.use_mb && ev->target==XA_STRING){
53         Status st=XStringListToTextProperty((char **)p, 1, &prop);
54         ok=(st!=0);
55     }else if(ioncore_g.use_mb){
56         XICCEncodingStyle style;
57         
58         if(ev->target==XA_STRING){
59             style=XStringStyle;
60             ok=TRUE;
61         }else if(ev->target==XA_COMPOUND_TEXT(ioncore_g.dpy)){
62             style=XCompoundTextStyle;
63             ok=TRUE;
64         }
65         
66         if(ok){
67             int st=XmbTextListToTextProperty(ioncore_g.dpy, (char **)p, 1,
68                                              style, &prop);
69             ok=(st>=0);
70         }
71     }
72     
73     if(ok){
74         XSetTextProperty(ioncore_g.dpy, ev->requestor, &prop, ev->property);
75         sev.target=ev->target;
76         sev.property=ev->property;
77         XFree(prop.value);
78     }
79
80 refuse:    
81     sev.type=SelectionNotify;
82     sev.requestor=ev->requestor;
83     sev.selection=ev->selection;
84     sev.time=ev->time;
85     XSendEvent(ioncore_g.dpy, ev->requestor, False, 0L, (XEvent*)&sev);
86 }
87
88
89 static void ins(Window win, const char *str, int n)
90 {
91     if(!continuation_set){
92         WWindow *wwin=XWINDOW_REGION_OF_T(win, WWindow);
93         if(wwin)
94             window_insstr(wwin, str, n);
95     }else{
96         char *tmp=scopyn(str, n);
97         if(tmp!=NULL){
98             extl_call(continuation, "s", NULL, tmp);
99             free(tmp);
100         }
101     }
102 }
103
104     
105 static void insert_selection(Window win, Atom prop)
106 {
107     char **p=xwindow_get_text_property(win, prop, NULL);
108     if(p!=NULL){
109         ins(win, p[0], strlen(p[0]));
110         XFreeStringList(p);
111     }
112 }
113
114
115 void ioncore_handle_selection(XSelectionEvent *ev)
116 {
117     Atom prop=ev->property;
118     Window win=ev->requestor;
119     WWindow *wwin;
120     
121     if(prop!=None){
122         insert_selection(win, prop);
123         XDeleteProperty(ioncore_g.dpy, win, prop);
124     }
125     
126     if(continuation_set){
127         extl_unref_fn(continuation);
128         continuation_set=FALSE;
129     }
130 }
131
132
133 void ioncore_clear_selection()
134 {
135     if(selection_data!=NULL){
136         free(selection_data);
137         selection_data=NULL;
138     }
139 }
140
141
142 void ioncore_set_selection_n(const char *p, int n)
143 {
144     if(selection_data!=NULL)
145         free(selection_data);
146     
147     selection_data=ALLOC_N(char, n+1);
148     
149     if(selection_data==NULL)
150         return;
151     
152     memcpy(selection_data, p, n);
153     selection_data[n]='\0';
154     selection_length=n;
155     
156     XSetSelectionOwner(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy),
157                        DefaultRootWindow(ioncore_g.dpy),
158                        CurrentTime);
159 }
160
161
162 /*EXTL_DOC
163  * Set primary selection and cutbuffer0 to \var{p}.
164  */
165 EXTL_EXPORT
166 void ioncore_set_selection(const char *p)
167 {
168     if(p==NULL)
169         ioncore_clear_selection();
170     else
171         ioncore_set_selection_n(p, strlen(p));
172 }
173
174
175 void ioncore_request_selection_for(Window win)
176 {
177     Atom a=XA_STRING;
178     
179     if(continuation_set){
180         extl_unref_fn(continuation);
181         continuation_set=FALSE;
182     }
183     
184     if(ioncore_g.use_mb)
185         a=XA_COMPOUND_TEXT(ioncore_g.dpy);
186     
187     XConvertSelection(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy), a,
188                       ioncore_g.atom_selection, win, CurrentTime);
189 }
190
191
192 /*EXTL_DOC
193  * Request (string) selection. The function \var{fn} will be called 
194  * with the selection when and if it is received.
195  */
196 EXTL_EXPORT
197 void ioncore_request_selection(ExtlFn fn)
198 {
199     assert(ioncore_g.rootwins!=NULL);
200     ioncore_request_selection_for(ioncore_g.rootwins->dummy_win);
201     continuation=extl_ref_fn(fn);
202     continuation_set=TRUE;
203 }
204