2019-11-17 03:40:03 +01:00
|
|
|
module clipboard
|
|
|
|
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#flag -framework Cocoa
|
2021-04-19 18:01:47 +02:00
|
|
|
#include "@VEXEROOT/vlib/clipboard/clipboard_darwin.m"
|
2021-03-04 09:49:40 +01:00
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// Clipboard represents a system clipboard.
|
|
|
|
//
|
|
|
|
// System "copy" and "paste" actions utilize the clipboard for temporary storage.
|
2020-01-09 12:00:39 +01:00
|
|
|
pub struct Clipboard {
|
2020-11-02 23:00:29 +01:00
|
|
|
pb voidptr
|
|
|
|
last_cb_serial i64
|
2019-12-07 13:22:28 +01:00
|
|
|
mut:
|
2021-01-21 12:45:59 +01:00
|
|
|
foo int // TODO remove, for mut hack
|
2019-11-17 03:40:03 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 01:08:39 +01:00
|
|
|
fn C.darwin_new_pasteboard() voidptr
|
2021-01-09 10:37:40 +01:00
|
|
|
|
2021-04-19 18:01:47 +02:00
|
|
|
fn C.darwin_get_pasteboard_text(voidptr) &byte
|
2021-01-09 10:37:40 +01:00
|
|
|
|
2021-01-31 13:57:06 +01:00
|
|
|
fn C.darwin_set_pasteboard_text(voidptr, string) bool
|
2020-11-20 01:08:39 +01:00
|
|
|
|
2020-11-02 23:00:29 +01:00
|
|
|
fn new_clipboard() &Clipboard {
|
2019-11-17 03:40:03 +01:00
|
|
|
cb := &Clipboard{
|
2021-01-09 10:37:40 +01:00
|
|
|
pb: C.darwin_new_pasteboard() // pb
|
2019-11-17 03:40:03 +01:00
|
|
|
}
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// check_availability returns true if the clipboard is ready to be used.
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (cb &Clipboard) check_availability() bool {
|
2019-11-17 03:40:03 +01:00
|
|
|
return cb.pb != C.NULL
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// clear empties the clipboard contents.
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (mut cb Clipboard) clear() {
|
2019-12-07 13:22:28 +01:00
|
|
|
cb.foo = 0
|
2020-11-20 10:43:41 +01:00
|
|
|
cb.set_text('')
|
2020-11-20 11:58:53 +01:00
|
|
|
//#[cb->pb clearContents];
|
2019-11-17 03:40:03 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// free releases all memory associated with the clipboard
|
|
|
|
// instance.
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (mut cb Clipboard) free() {
|
2019-12-07 13:22:28 +01:00
|
|
|
cb.foo = 0
|
2020-11-02 23:00:29 +01:00
|
|
|
// nothing to free
|
2019-11-17 03:40:03 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// has_ownership returns true if the contents of
|
|
|
|
// the clipboard were created by this clipboard instance.
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (cb &Clipboard) has_ownership() bool {
|
2020-11-02 23:00:29 +01:00
|
|
|
if cb.last_cb_serial == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-11-20 23:49:52 +01:00
|
|
|
//#return [cb->pb changeCount] == cb->last_cb_serial;
|
2019-11-17 03:40:03 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-24 04:27:02 +01:00
|
|
|
fn C.OSAtomicCompareAndSwapLong()
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// set_text transfers `text` to the system clipboard.
|
|
|
|
// This is often associated with a *copy* action (`Cmd` + `C`).
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (mut cb Clipboard) set_text(text string) bool {
|
2020-11-20 03:28:28 +01:00
|
|
|
return C.darwin_set_pasteboard_text(cb.pb, text)
|
2019-11-17 03:40:03 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 12:06:32 +02:00
|
|
|
// get_text retrieves the contents of the system clipboard
|
|
|
|
// as a `string`.
|
|
|
|
// This is often associated with a *paste* action (`Cmd` + `V`).
|
2021-03-04 09:49:40 +01:00
|
|
|
pub fn (mut cb Clipboard) get_text() string {
|
2019-12-07 13:22:28 +01:00
|
|
|
cb.foo = 0
|
2020-11-20 01:08:39 +01:00
|
|
|
if isnil(cb.pb) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
utf8_clip := C.darwin_get_pasteboard_text(cb.pb)
|
2022-04-15 13:58:56 +02:00
|
|
|
return unsafe { tos_clone(&u8(utf8_clip)) }
|
2019-11-18 11:10:31 +01:00
|
|
|
}
|
2019-12-27 17:59:04 +01:00
|
|
|
|
2021-01-21 12:45:59 +01: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 17:59:04 +01:00
|
|
|
pub fn new_primary() &Clipboard {
|
|
|
|
panic('Primary clipboard is not supported on non-Linux systems.')
|
|
|
|
}
|