2019-11-17 02:40:03 +00:00
|
|
|
module clipboard
|
|
|
|
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#flag -framework Cocoa
|
2020-11-20 00:08:39 +00:00
|
|
|
#include "@VROOT/vlib/clipboard/clipboard_darwin.m"
|
2020-01-09 11:00:39 +00:00
|
|
|
pub struct Clipboard {
|
2020-11-02 22:00:29 +00:00
|
|
|
pb voidptr
|
|
|
|
last_cb_serial i64
|
2019-12-07 12:22:28 +00:00
|
|
|
mut:
|
2021-01-21 11:45:59 +00:00
|
|
|
foo int // TODO remove, for mut hack
|
2019-11-17 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 00:08:39 +00:00
|
|
|
fn C.darwin_new_pasteboard() voidptr
|
2021-01-09 09:37:40 +00:00
|
|
|
|
2020-11-20 02:28:28 +00:00
|
|
|
fn C.darwin_get_pasteboard_text(voidptr) byteptr
|
2021-01-09 09:37:40 +00:00
|
|
|
|
2021-01-31 12:57:06 +00:00
|
|
|
fn C.darwin_set_pasteboard_text(voidptr, string) bool
|
2020-11-20 00:08:39 +00:00
|
|
|
|
2020-11-02 22:00:29 +00:00
|
|
|
fn new_clipboard() &Clipboard {
|
2019-11-17 02:40:03 +00:00
|
|
|
cb := &Clipboard{
|
2021-01-09 09:37:40 +00:00
|
|
|
pb: C.darwin_new_pasteboard() // pb
|
2019-11-17 02:40:03 +00:00
|
|
|
}
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) check_availability() bool {
|
|
|
|
return cb.pb != C.NULL
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:00:29 +00:00
|
|
|
fn (mut cb Clipboard) clear() {
|
2019-12-07 12:22:28 +00:00
|
|
|
cb.foo = 0
|
2020-11-20 09:43:41 +00:00
|
|
|
cb.set_text('')
|
2020-11-20 10:58:53 +00:00
|
|
|
//#[cb->pb clearContents];
|
2019-11-17 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 22:00:29 +00:00
|
|
|
fn (mut cb Clipboard) free() {
|
2019-12-07 12:22:28 +00:00
|
|
|
cb.foo = 0
|
2020-11-02 22:00:29 +00:00
|
|
|
// nothing to free
|
2019-11-17 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) has_ownership() bool {
|
2020-11-02 22:00:29 +00:00
|
|
|
if cb.last_cb_serial == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-20 22:49:52 +00:00
|
|
|
//#return [cb->pb changeCount] == cb->last_cb_serial;
|
2019-11-17 02:40:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-24 03:27:02 +00:00
|
|
|
fn C.OSAtomicCompareAndSwapLong()
|
|
|
|
|
2020-05-17 11:51:18 +00:00
|
|
|
fn (mut cb Clipboard) set_text(text string) bool {
|
2020-11-20 02:28:28 +00:00
|
|
|
return C.darwin_set_pasteboard_text(cb.pb, text)
|
2019-11-17 02:40:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 11:51:18 +00:00
|
|
|
fn (mut cb Clipboard) get_text() string {
|
2019-12-07 12:22:28 +00:00
|
|
|
cb.foo = 0
|
2020-11-20 00:08:39 +00:00
|
|
|
if isnil(cb.pb) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
|
2021-01-09 09:37:40 +00:00
|
|
|
return unsafe { utf8_clip.vstring() }
|
2019-11-18 10:10:31 +00:00
|
|
|
}
|
2019-12-27 16:59:04 +00:00
|
|
|
|
2021-01-21 11:45:59 +00:00
|
|
|
// new_primary returns a new X11 `PRIMARY` type `Clipboard` instance allocated on the heap.
|
|
|
|
// Please note: new_primary only works on X11 based systems.
|
2019-12-27 16:59:04 +00:00
|
|
|
pub fn new_primary() &Clipboard {
|
|
|
|
panic('Primary clipboard is not supported on non-Linux systems.')
|
|
|
|
}
|