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 void ioncore_handle_selection_request(XSelectionRequestEvent *ev)
29 if(selection_data==NULL)
34 xwindow_set_text_property(ev->requestor, ev->property, p, 1);
36 sev.type=SelectionNotify;
37 sev.requestor=ev->requestor;
38 sev.selection=ev->selection;
39 sev.target=ev->target;
41 sev.property=ev->property;
42 XSendEvent(ioncore_g.dpy, ev->requestor, False, 0L, (XEvent*)&sev);
46 static void ins(Window win, const char *str, int n)
48 if(!continuation_set){
49 WWindow *wwin=XWINDOW_REGION_OF_T(win, WWindow);
51 window_insstr(wwin, str, n);
53 char *tmp=scopyn(str, n);
55 extl_call(continuation, "s", NULL, tmp);
62 static void insert_selection(Window win, Atom prop)
64 char **p=xwindow_get_text_property(win, prop, NULL);
66 ins(win, p[0], strlen(p[0]));
72 static void insert_cutbuffer(Window win)
77 p=XFetchBytes(ioncore_g.dpy, &n);
86 void ioncore_handle_selection(XSelectionEvent *ev)
88 Atom prop=ev->property;
89 Window win=ev->requestor;
93 insert_cutbuffer(win);
95 insert_selection(win, prop);
96 XDeleteProperty(ioncore_g.dpy, win, prop);
100 extl_unref_fn(continuation);
101 continuation_set=FALSE;
106 void ioncore_clear_selection()
108 if(selection_data!=NULL){
109 free(selection_data);
115 void ioncore_set_selection_n(const char *p, int n)
117 if(selection_data!=NULL)
118 free(selection_data);
120 selection_data=ALLOC_N(char, n+1);
122 if(selection_data==NULL)
125 memcpy(selection_data, p, n);
126 selection_data[n]='\0';
129 XStoreBytes(ioncore_g.dpy, p, n);
131 XSetSelectionOwner(ioncore_g.dpy, XA_PRIMARY,
132 DefaultRootWindow(ioncore_g.dpy),
138 * Set primary selection and cutbuffer0 to \var{p}.
141 void ioncore_set_selection(const char *p)
144 ioncore_clear_selection();
146 ioncore_set_selection_n(p, strlen(p));
150 void ioncore_request_selection_for(Window win)
154 if(continuation_set){
155 extl_unref_fn(continuation);
156 continuation_set=FALSE;
159 if(ioncore_g.use_mb){
160 #ifdef X_HAVE_UTF8_STRING
161 a=XInternAtom(ioncore_g.dpy, "UTF8_STRING", True);
163 a=XInternAtom(ioncore_g.dpy, "COMPOUND_TEXT", True);
167 XConvertSelection(ioncore_g.dpy, XA_PRIMARY, a,
168 ioncore_g.atom_selection, win, CurrentTime);
173 * Request (string) selection. The function \var{fn} will be called
174 * with the selection when and if it is received.
177 void ioncore_request_selection(ExtlFn fn)
179 assert(ioncore_g.rootwins!=NULL);
180 ioncore_request_selection_for(ioncore_g.rootwins->dummy_win);
181 continuation=extl_ref_fn(fn);
182 continuation_set=TRUE;