clipboard: fix windows warnings and run vfmt (#6715)

pull/6719/head
Lukas Neubert 2020-11-02 23:00:29 +01:00 committed by GitHub
parent 788de9938a
commit 4ccb219079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 113 deletions

View File

@ -2,9 +2,7 @@ module clipboard
#include <libkern/OSAtomic.h>
#include <Cocoa/Cocoa.h>
#flag -framework Cocoa
pub struct Clipboard {
pb voidptr
last_cb_serial i64
@ -36,7 +34,9 @@ fn (mut cb Clipboard) free(){
}
fn (cb &Clipboard) has_ownership() bool {
if cb.last_cb_serial == 0 {return false}
if cb.last_cb_serial == 0 {
return false
}
#return [cb->pb changeCount] == cb->last_cb_serial;
return false
}
@ -47,12 +47,10 @@ fn (mut cb Clipboard) set_text(text string) bool {
cb.foo = 0
#NSString *ns_clip;
ret := false
#ns_clip = [[ NSString alloc ] initWithBytesNoCopy:text.str length:text.len encoding:NSUTF8StringEncoding freeWhenDone: false];
#[cb->pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
#ret = [cb->pb setString:ns_clip forType:NSStringPboardType];
#[ns_clip release];
mut serial := 0
#serial = [cb->pb changeCount];
C.OSAtomicCompareAndSwapLong(cb.last_cb_serial, serial, &cb.last_cb_serial)
@ -63,12 +61,10 @@ fn (mut cb Clipboard) get_text() string {
cb.foo = 0
#NSString *ns_clip;
utf8_clip := byteptr(0)
#ns_clip = [cb->pb stringForType:NSStringPboardType]; //NSPasteboardTypeString
#if (ns_clip == nil) {
#return tos3(""); //in case clipboard is empty
#}
#utf8_clip = [ns_clip UTF8String];
return unsafe {utf8_clip.vstring()}
}

View File

@ -2,11 +2,13 @@ import clipboard
fn run_test(is_primary bool) {
mut cb := if is_primary { clipboard.new_primary() } else { clipboard.new() }
if !cb.is_available() {return}
if !cb.is_available() {
return
}
assert cb.check_ownership() == false
assert cb.copy("I am a good boy!") == true
assert cb.copy('I am a good boy!') == true
assert cb.check_ownership() == true
assert cb.paste() == "I am a good boy!"
assert cb.paste() == 'I am a good boy!'
cb.clear_all()
assert cb.paste().len <= 0
cb.destroy()

View File

@ -3,7 +3,6 @@ module clipboard
import time
#flag -lUser32
struct WndClassEx {
cb_size u32
style u32
@ -23,7 +22,7 @@ fn C.RegisterClassEx(class WndClassEx) int
fn C.GetClipboardOwner() &C.HWND
fn C.CreateWindowEx(dwExStyle i64, lpClassName, lpWindowName &u16, dwStyle i64, x, y, nWidth, nHeight int, hWndParent i64, hMenu, h_instance, lpParam voidptr) &C.HWND
fn C.CreateWindowEx(dwExStyle i64, lpClassName &u16, lpWindowName &u16, dwStyle i64, x int, y int, nWidth int, nHeight int, hWndParent i64, hMenu voidptr, h_instance voidptr, lpParam voidptr) &C.HWND
// fn C.MultiByteToWideChar(CodePage u32, dw_flags u16, lpMultiByteStr byteptr, cbMultiByte int, lpWideCharStr u16, cchWideChar int) int
fn C.EmptyClipboard()
@ -94,8 +93,8 @@ fn new_clipboard() &Clipboard {
if C.RegisterClassEx(&wndclass) == 0 && C.GetLastError() != u32(C.ERROR_CLASS_ALREADY_EXISTS) {
println('Failed registering class.')
}
hwnd := C.CreateWindowEx(0, wndclass.lpsz_class_name, wndclass.lpsz_class_name, 0, 0, 0,
0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL)
hwnd := C.CreateWindowEx(0, wndclass.lpsz_class_name, wndclass.lpsz_class_name, 0,
0, 0, 0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL)
if hwnd == C.NULL {
println('Error creating window!')
}
@ -127,12 +126,13 @@ fn (mut cb Clipboard) free() {
// the string.to_wide doesn't work with SetClipboardData, don't know why
fn to_wide(text string) &C.HGLOBAL {
len_required := C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str, text.len +
1, C.NULL, 0)
len_required := C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str,
text.len + 1, C.NULL, 0)
buf := C.GlobalAlloc(C.GMEM_MOVEABLE, i64(sizeof(u16)) * len_required)
if buf != C.HGLOBAL(C.NULL) {
mut locked := &u16(C.GlobalLock(buf))
C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str, text.len + 1, locked, len_required)
C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str, text.len + 1,
locked, len_required)
unsafe {
locked[len_required - 1] = u16(0)
}