2 * ion/ioncore/selection.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
19 #include <libextl/extl.h>
22 static char *selection_data=NULL;
23 static int selection_length;
24 static bool continuation_set=FALSE;
25 static ExtlFn continuation;
27 void ioncore_handle_selection_request(XSelectionRequestEvent *ev)
32 if(selection_data==NULL)
37 xwindow_set_text_property(ev->requestor, ev->property, p, 1);
39 sev.type=SelectionNotify;
40 sev.requestor=ev->requestor;
41 sev.selection=ev->selection;
42 sev.target=ev->target;
44 sev.property=ev->property;
45 XSendEvent(ioncore_g.dpy, ev->requestor, False, 0L, (XEvent*)&sev);
49 static void ins(Window win, const char *str, int n)
51 if(!continuation_set){
52 WWindow *wwin=XWINDOW_REGION_OF_T(win, WWindow);
54 window_insstr(wwin, str, n);
56 char *tmp=scopyn(str, n);
58 extl_call(continuation, "s", NULL, tmp);
65 static void insert_selection(Window win, Atom prop)
67 char **p=xwindow_get_text_property(win, prop, NULL);
69 ins(win, p[0], strlen(p[0]));
75 static void insert_cutbuffer(Window win)
80 p=XFetchBytes(ioncore_g.dpy, &n);
89 void ioncore_handle_selection(XSelectionEvent *ev)
91 Atom prop=ev->property;
92 Window win=ev->requestor;
96 insert_cutbuffer(win);
98 insert_selection(win, prop);
99 XDeleteProperty(ioncore_g.dpy, win, prop);
102 if(continuation_set){
103 extl_unref_fn(continuation);
104 continuation_set=FALSE;
109 void ioncore_clear_selection()
111 if(selection_data!=NULL){
112 free(selection_data);
118 void ioncore_set_selection_n(const char *p, int n)
120 if(selection_data!=NULL)
121 free(selection_data);
123 selection_data=ALLOC_N(char, n+1);
125 if(selection_data==NULL)
128 memcpy(selection_data, p, n);
129 selection_data[n]='\0';
132 XStoreBytes(ioncore_g.dpy, p, n);
134 XSetSelectionOwner(ioncore_g.dpy, XA_PRIMARY,
135 DefaultRootWindow(ioncore_g.dpy),
141 * Set primary selection and cutbuffer0 to \var{p}.
144 void ioncore_set_selection(const char *p)
147 ioncore_clear_selection();
149 ioncore_set_selection_n(p, strlen(p));
153 void ioncore_request_selection_for(Window win)
157 if(continuation_set){
158 extl_unref_fn(continuation);
159 continuation_set=FALSE;
162 if(ioncore_g.use_mb){
163 #ifdef X_HAVE_UTF8_STRING
164 a=XInternAtom(ioncore_g.dpy, "UTF8_STRING", True);
166 a=XInternAtom(ioncore_g.dpy, "COMPOUND_TEXT", True);
170 XConvertSelection(ioncore_g.dpy, XA_PRIMARY, a,
171 ioncore_g.atom_selection, win, CurrentTime);
176 * Request (string) selection. The function \var{fn} will be called
177 * with the selection when and if it is received.
180 void ioncore_request_selection(ExtlFn fn)
182 assert(ioncore_g.rootwins!=NULL);
183 ioncore_request_selection_for(ioncore_g.rootwins->dummy_win);
184 continuation=extl_ref_fn(fn);
185 continuation_set=TRUE;