71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
// +build windows
|
|
|
|
package drop
|
|
|
|
import (
|
|
"errors"
|
|
"unsafe"
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
"opslag.de/schobers/geom"
|
|
)
|
|
|
|
/*
|
|
#include <Windows.h>
|
|
#include <stdint.h>
|
|
|
|
void SetDragAndDropHook(void* window);
|
|
*/
|
|
import "C"
|
|
|
|
var handler Dropper = nil
|
|
var drops map[uint32]droppedFiles = map[uint32]droppedFiles{}
|
|
|
|
type droppedFiles struct {
|
|
X, Y int
|
|
Files []string
|
|
}
|
|
|
|
//export droppedFilesPosition
|
|
func droppedFilesPosition(id C.uint32_t, x, y C.INT) {
|
|
files := drops[uint32(id)]
|
|
files.X = int(x)
|
|
files.Y = int(y)
|
|
drops[uint32(id)] = files
|
|
}
|
|
|
|
//export droppedFilesFile
|
|
func droppedFilesFile(id C.uint32_t, filePath *C.wchar_t, filePathLength C.UINT) {
|
|
files := drops[uint32(id)]
|
|
pathBytes := C.GoBytes(unsafe.Pointer(filePath), C.int(filePathLength)*C.sizeof_wchar_t)
|
|
decoder := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()
|
|
path, err := decoder.Bytes(pathBytes)
|
|
if err != nil {
|
|
return
|
|
}
|
|
files.Files = append(files.Files, string(path))
|
|
drops[uint32(id)] = files
|
|
}
|
|
|
|
//export droppedFilesDrop
|
|
func droppedFilesDrop(id C.uint32_t) {
|
|
h := handler
|
|
if h == nil {
|
|
return
|
|
}
|
|
drop, ok := drops[uint32(id)]
|
|
if !ok {
|
|
return
|
|
}
|
|
h.FilesDropped(geom.Pt(drop.X, drop.Y).ToF32(), drop.Files)
|
|
}
|
|
|
|
func Register(dropper Dropper) error {
|
|
if handler != nil {
|
|
return errors.New(`can only register single dropper`)
|
|
}
|
|
handler = dropper
|
|
C.SetDragAndDropHook(unsafe.Pointer(dropper.WindowHandle()))
|
|
return nil
|
|
}
|