factor/library/ui/x11/clipboard.factor

106 lines
3.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2006 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
2006-07-23 21:38:58 -04:00
USING: alien arrays gadgets kernel math namespaces sequences ;
IN: x11
2006-07-23 21:45:35 -04:00
! This code was based on by McCLIM's Backends/CLX/port.lisp
! and http://common-lisp.net/~crhodes/clx/demo/clipboard.lisp.
TUPLE: x-clipboard atom contents ;
C: x-clipboard ( atom -- clipboard )
[ set-x-clipboard-atom ] keep
"" over set-x-clipboard-contents ;
: selection-property ( -- n )
2006-06-03 19:25:50 -04:00
"org.factorcode.Factor.SELECTION" x-atom ;
2006-06-03 19:25:50 -04:00
: convert-selection ( win selection -- n )
swap >r >r dpy get r> XA_STRING selection-property r>
2006-06-03 19:25:50 -04:00
CurrentTime XConvertSelection drop ;
2006-06-03 19:25:50 -04:00
: snarf-property ( prop-return -- string )
dup *void* [ *char* ] [ drop f ] if ;
: window-property ( win prop delete? -- string )
2006-06-03 19:25:50 -04:00
>r dpy get -rot 0 -1 r> AnyPropertyType
0 <Atom> 0 <int> 0 <ulong> 0 <ulong> f <void*>
2006-06-03 19:25:50 -04:00
[ XGetWindowProperty drop ] keep snarf-property ;
: selection-from-event ( event window -- string )
2006-07-23 21:38:58 -04:00
>r XSelectionEvent-property zero? [
r> drop f
] [
r> selection-property 1 window-property
] if ;
2006-06-03 19:25:50 -04:00
2006-07-23 18:51:25 -04:00
: own-selection ( prop win -- )
2006-07-24 02:02:06 -04:00
dpy get -rot CurrentTime XSetSelectionOwner drop
flush-dpy ;
2006-07-23 18:51:25 -04:00
2006-07-23 21:38:58 -04:00
: clipboard-for-atom ( atom -- clipboard )
{
{ [ dup XA_PRIMARY = ] [ drop selection get ] }
{ [ dup "CLIPBOARD" x-atom = ] [ drop clipboard get ] }
{ [ t ] [ drop <clipboard> ] }
} cond ;
: set-selection-prop ( evt -- )
dpy get swap
[ XSelectionRequestEvent-requestor ] keep
[ XSelectionRequestEvent-property ] keep
>r XA_STRING 8 PropModeReplace r>
XSelectionRequestEvent-selection
clipboard-for-atom x-clipboard-contents
dup length XChangeProperty drop ;
: set-targets-prop ( evt -- )
dpy get swap
[ XSelectionRequestEvent-requestor ] keep
XSelectionRequestEvent-property
"TARGETS" x-atom 32 PropModeReplace
{ "STRING" "TARGETS" "TIMESTAMP" } [ x-atom ] map >int-array
32 XChangeProperty drop ;
: set-timestamp-prop ( evt -- )
dpy get swap
[ XSelectionRequestEvent-requestor ] keep
[ XSelectionRequestEvent-property ] keep
>r "TIMESTAMP" x-atom 32 PropModeReplace r>
XSelectionRequestEvent-time 1array >int-array
32 XChangeProperty drop ;
: send-notify ( evt prop -- )
"XSelectionEvent" <c-object>
SelectionNotify over set-XSelectionEvent-type
2006-07-24 02:02:06 -04:00
[ set-XSelectionEvent-property ] keep
over XSelectionRequestEvent-display over set-XSelectionEvent-display
2006-07-23 21:38:58 -04:00
over XSelectionRequestEvent-requestor over set-XSelectionEvent-requestor
over XSelectionRequestEvent-selection over set-XSelectionEvent-selection
over XSelectionRequestEvent-target over set-XSelectionEvent-target
over XSelectionRequestEvent-time over set-XSelectionEvent-time
>r dpy get swap XSelectionRequestEvent-requestor 0 0 r>
2006-07-24 02:02:06 -04:00
XSendEvent drop
flush-dpy ;
2006-07-23 21:38:58 -04:00
: send-notify-success ( evt -- )
2006-07-24 02:02:06 -04:00
dup XSelectionRequestEvent-property send-notify ;
2006-07-23 21:38:58 -04:00
: send-notify-failure ( evt -- )
0 send-notify ;
2006-07-23 18:51:25 -04:00
: x-clipboard@ ( gadget clipboard -- prop win )
x-clipboard-atom swap find-world world-handle first ;
M: x-clipboard copy-clipboard
2006-07-23 18:51:25 -04:00
[ x-clipboard@ own-selection ] keep
set-x-clipboard-contents ;
2006-06-03 19:25:50 -04:00
M: x-clipboard paste-clipboard
2006-06-03 19:56:39 -04:00
>r find-world world-handle first r> x-clipboard-atom
2006-06-03 19:25:50 -04:00
convert-selection ;
: init-clipboard ( -- )
XA_PRIMARY <x-clipboard> selection set-global
"CLIPBOARD" x-atom <x-clipboard> clipboard set-global ;