2 * ion/ioncore/selection.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
16 #include <libextl/extl.h>
19 static char *selection_data=NULL;
20 static int selection_length;
21 static bool continuation_set=FALSE;
22 static ExtlFn continuation;
24 #define CLIPATOM(X) XA_PRIMARY
26 static Atom XA_COMPOUND_TEXT(Display *unused)
31 a=XInternAtom(ioncore_g.dpy, "COMPOUND_TEXT", False);
37 void ioncore_handle_selection_request(XSelectionRequestEvent *ev)
47 if(selection_data==NULL || ev->property==None)
52 if(!ioncore_g.use_mb && ev->target==XA_STRING){
53 Status st=XStringListToTextProperty((char **)p, 1, &prop);
55 }else if(ioncore_g.use_mb){
56 XICCEncodingStyle style;
58 if(ev->target==XA_STRING){
61 }else if(ev->target==XA_COMPOUND_TEXT(ioncore_g.dpy)){
62 style=XCompoundTextStyle;
67 int st=XmbTextListToTextProperty(ioncore_g.dpy, (char **)p, 1,
74 XSetTextProperty(ioncore_g.dpy, ev->requestor, &prop, ev->property);
75 sev.target=ev->target;
76 sev.property=ev->property;
81 sev.type=SelectionNotify;
82 sev.requestor=ev->requestor;
83 sev.selection=ev->selection;
85 XSendEvent(ioncore_g.dpy, ev->requestor, False, 0L, (XEvent*)&sev);
89 static void ins(Window win, const char *str, int n)
91 if(!continuation_set){
92 WWindow *wwin=XWINDOW_REGION_OF_T(win, WWindow);
94 window_insstr(wwin, str, n);
96 char *tmp=scopyn(str, n);
98 extl_call(continuation, "s", NULL, tmp);
105 static void insert_selection(Window win, Atom prop)
107 char **p=xwindow_get_text_property(win, prop, NULL);
109 ins(win, p[0], strlen(p[0]));
115 void ioncore_handle_selection(XSelectionEvent *ev)
117 Atom prop=ev->property;
118 Window win=ev->requestor;
122 insert_selection(win, prop);
123 XDeleteProperty(ioncore_g.dpy, win, prop);
126 if(continuation_set){
127 extl_unref_fn(continuation);
128 continuation_set=FALSE;
133 void ioncore_clear_selection()
135 if(selection_data!=NULL){
136 free(selection_data);
142 void ioncore_set_selection_n(const char *p, int n)
144 if(selection_data!=NULL)
145 free(selection_data);
147 selection_data=ALLOC_N(char, n+1);
149 if(selection_data==NULL)
152 memcpy(selection_data, p, n);
153 selection_data[n]='\0';
156 XSetSelectionOwner(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy),
157 DefaultRootWindow(ioncore_g.dpy),
163 * Set primary selection and cutbuffer0 to \var{p}.
166 void ioncore_set_selection(const char *p)
169 ioncore_clear_selection();
171 ioncore_set_selection_n(p, strlen(p));
175 void ioncore_request_selection_for(Window win)
179 if(continuation_set){
180 extl_unref_fn(continuation);
181 continuation_set=FALSE;
185 a=XA_COMPOUND_TEXT(ioncore_g.dpy);
187 XConvertSelection(ioncore_g.dpy, CLIPATOM(ioncore_g.dpy), a,
188 ioncore_g.atom_selection, win, CurrentTime);
193 * Request (string) selection. The function \var{fn} will be called
194 * with the selection when and if it is received.
197 void ioncore_request_selection(ExtlFn fn)
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;