| 1 | module 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] |
| 12 | pub struct Clipboard { |
| 13 | pb voidptr |
| 14 | last_cb_serial i64 |
| 15 | mut: |
| 16 | foo int // TODO: remove, for mut hack |
| 17 | } |
| 18 | |
| 19 | fn C.darwin_new_pasteboard() voidptr |
| 20 | |
| 21 | fn C.darwin_get_pasteboard_text(voidptr) &u8 |
| 22 | |
| 23 | fn C.darwin_set_pasteboard_text(voidptr, string) bool |
| 24 | |
| 25 | fn 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. |
| 33 | pub fn (cb &Clipboard) check_availability() bool { |
| 34 | return cb.pb != C.NULL |
| 35 | } |
| 36 | |
| 37 | // clear empties the clipboard contents. |
| 38 | pub 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 instance. |
| 45 | pub fn (mut cb Clipboard) free() { |
| 46 | cb.foo = 0 |
| 47 | // nothing to free |
| 48 | } |
| 49 | |
| 50 | // has_ownership returns true if the contents of the clipboard were created by this clipboard instance. |
| 51 | pub fn (cb &Clipboard) has_ownership() bool { |
| 52 | if cb.last_cb_serial == 0 { |
| 53 | return false |
| 54 | } |
| 55 | //#return [cb->pb changeCount] == cb->last_cb_serial; |
| 56 | return false |
| 57 | } |
| 58 | |
| 59 | fn C.OSAtomicCompareAndSwapLong() |
| 60 | |
| 61 | // set_text transfers `text` to the system clipboard. |
| 62 | // This is often associated with a *copy* action (`Cmd` + `C`). |
| 63 | pub fn (mut cb Clipboard) set_text(text string) bool { |
| 64 | return C.darwin_set_pasteboard_text(cb.pb, text) |
| 65 | } |
| 66 | |
| 67 | // get_text retrieves the contents of the system clipboard. |
| 68 | // This is often associated with a *paste* action (`Cmd` + `V`). |
| 69 | pub fn (mut cb Clipboard) get_text() string { |
| 70 | cb.foo = 0 |
| 71 | if isnil(cb.pb) { |
| 72 | return '' |
| 73 | } |
| 74 | utf8_clip := C.darwin_get_pasteboard_text(cb.pb) |
| 75 | return unsafe { tos_clone(&u8(utf8_clip)) } |
| 76 | } |
| 77 | |
| 78 | // new_primary returns a new X11 `PRIMARY` type `Clipboard` instance allocated on the heap. |
| 79 | // Please note: new_primary only works on X11 based systems. |
| 80 | pub fn new_primary() &Clipboard { |
| 81 | panic('Primary clipboard is not supported on non-Linux systems.') |
| 82 | } |
| 83 | |