v / vlib / clipboard
Raw file | 85 loc (71 sloc) | 2.14 KB | Latest commit hash 02e026e29
1module clipboard
2
3#include <libkern/OSAtomic.h>
4#include <Cocoa/Cocoa.h>
5#flag -framework Cocoa
6#include "@VEXEROOT/vlib/clipboard/clipboard_darwin.m"
7
8// Clipboard represents a system clipboard.
9//
10// System "copy" and "paste" actions utilize the clipboard for temporary storage.
11[heap]
12pub struct Clipboard {
13 pb voidptr
14 last_cb_serial i64
15mut:
16 foo int // TODO remove, for mut hack
17}
18
19fn C.darwin_new_pasteboard() voidptr
20
21fn C.darwin_get_pasteboard_text(voidptr) &u8
22
23fn C.darwin_set_pasteboard_text(voidptr, string) bool
24
25fn new_clipboard() &Clipboard {
26 cb := &Clipboard{
27 pb: C.darwin_new_pasteboard() // pb
28 }
29 return cb
30}
31
32// check_availability returns true if the clipboard is ready to be used.
33pub fn (cb &Clipboard) check_availability() bool {
34 return cb.pb != C.NULL
35}
36
37// clear empties the clipboard contents.
38pub fn (mut cb Clipboard) clear() {
39 cb.foo = 0
40 cb.set_text('')
41 //#[cb->pb clearContents];
42}
43
44// free releases all memory associated with the clipboard
45// instance.
46pub fn (mut cb Clipboard) free() {
47 cb.foo = 0
48 // nothing to free
49}
50
51// has_ownership returns true if the contents of
52// the clipboard were created by this clipboard instance.
53pub fn (cb &Clipboard) has_ownership() bool {
54 if cb.last_cb_serial == 0 {
55 return false
56 }
57 //#return [cb->pb changeCount] == cb->last_cb_serial;
58 return false
59}
60
61fn C.OSAtomicCompareAndSwapLong()
62
63// set_text transfers `text` to the system clipboard.
64// This is often associated with a *copy* action (`Cmd` + `C`).
65pub fn (mut cb Clipboard) set_text(text string) bool {
66 return C.darwin_set_pasteboard_text(cb.pb, text)
67}
68
69// get_text retrieves the contents of the system clipboard
70// as a `string`.
71// This is often associated with a *paste* action (`Cmd` + `V`).
72pub fn (mut cb Clipboard) get_text() string {
73 cb.foo = 0
74 if isnil(cb.pb) {
75 return ''
76 }
77 utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
78 return unsafe { tos_clone(&u8(utf8_clip)) }
79}
80
81// new_primary returns a new X11 `PRIMARY` type `Clipboard` instance allocated on the heap.
82// Please note: new_primary only works on X11 based systems.
83pub fn new_primary() &Clipboard {
84 panic('Primary clipboard is not supported on non-Linux systems.')
85}