2014-03-29 22:57:49 -05:00
|
|
|
// 29 march 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
2014-03-30 13:25:01 -05:00
|
|
|
"image"
|
2014-03-29 22:57:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
|
|
|
|
// #include <stdlib.h>
|
|
|
|
//// #include <HIToolbox/Events.h>
|
|
|
|
// #include "objc_darwin.h"
|
|
|
|
// extern void areaView_drawRect(id, struct xrect);
|
2014-03-30 13:25:01 -05:00
|
|
|
// extern BOOL areaView_isFlipped(id, SEL);
|
2014-03-29 22:57:49 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
const (
|
2014-03-30 10:19:13 -05:00
|
|
|
__goArea = "goArea"
|
2014-03-29 22:57:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-03-30 10:19:13 -05:00
|
|
|
_goArea C.id
|
|
|
|
|
2014-03-29 22:57:49 -05:00
|
|
|
_drawRect = sel_getUid("drawRect:")
|
2014-03-30 13:25:01 -05:00
|
|
|
_isFlipped = sel_getUid("isFlipped")
|
2014-03-29 22:57:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func mkAreaClass() error {
|
2014-03-30 10:19:13 -05:00
|
|
|
areaclass, err := makeAreaClass(__goArea)
|
2014-03-29 22:57:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating Area backend class: %v", err)
|
|
|
|
}
|
|
|
|
// addAreaViewDrawMethod() is in bleh_darwin.m
|
|
|
|
ok := C.addAreaViewDrawMethod(areaclass)
|
|
|
|
if ok != C.BOOL(C.YES) {
|
|
|
|
return fmt.Errorf("error overriding Area drawRect: method; reason unknown")
|
|
|
|
}
|
2014-03-30 13:25:01 -05:00
|
|
|
// TODO rename this function (it overrides anyway)
|
|
|
|
err = addDelegateMethod(areaclass, _isFlipped,
|
|
|
|
C.areaView_isFlipped, area_boolret)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error overriding Area isFlipped method: %v", err)
|
|
|
|
}
|
2014-03-30 10:19:13 -05:00
|
|
|
_goArea = objc_getClass(__goArea)
|
2014-03-29 22:57:49 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-30 13:25:01 -05:00
|
|
|
var (
|
|
|
|
_drawAtPoint = sel_getUid("drawAtPoint:")
|
|
|
|
)
|
|
|
|
|
2014-03-29 22:57:49 -05:00
|
|
|
//export areaView_drawRect
|
|
|
|
func areaView_drawRect(self C.id, rect C.struct_xrect) {
|
2014-03-30 13:25:01 -05:00
|
|
|
s := getSysData(self)
|
|
|
|
// TODO clear clip rect
|
2014-03-30 13:29:02 -05:00
|
|
|
// rectangles in Cocoa are origin/size, not point0/point1; if we don't watch for this, weird things will happen when scrolling
|
|
|
|
// TODO change names EVERYWHERE ELSE to match
|
|
|
|
cliprect := image.Rect(int(rect.x), int(rect.y), int(rect.x + rect.width), int(rect.y + rect.height))
|
2014-03-30 13:25:01 -05:00
|
|
|
max := C.objc_msgSend_stret_rect_noargs(self, _frame)
|
|
|
|
cliprect = image.Rect(0, 0, int(max.width), int(max.height)).Intersect(cliprect)
|
|
|
|
if cliprect.Empty() { // no intersection; nothing to paint
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i := s.handler.Paint(cliprect)
|
|
|
|
C.drawImage(
|
|
|
|
unsafe.Pointer(&i.Pix[0]), C.int64_t(i.Rect.Dx()), C.int64_t(i.Rect.Dy()), C.int64_t(i.Stride),
|
|
|
|
C.int64_t(cliprect.Min.X), C.int64_t(cliprect.Min.Y))
|
|
|
|
}
|
|
|
|
|
|
|
|
//export areaView_isFlipped
|
|
|
|
func areaView_isFlipped(self C.id, sel C.SEL) C.BOOL {
|
|
|
|
return C.BOOL(C.YES)
|
2014-03-30 10:19:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO combine these with the listbox functions?
|
|
|
|
|
|
|
|
func newAreaScrollView(area C.id) C.id {
|
|
|
|
scrollview := objc_alloc(_NSScrollView)
|
|
|
|
scrollview = objc_msgSend_rect(scrollview, _initWithFrame,
|
|
|
|
0, 0, 100, 100)
|
|
|
|
C.objc_msgSend_bool(scrollview, _setHasHorizontalScroller, C.BOOL(C.YES))
|
|
|
|
C.objc_msgSend_bool(scrollview, _setHasVerticalScroller, C.BOOL(C.YES))
|
|
|
|
C.objc_msgSend_bool(scrollview, _setAutohidesScrollers, C.BOOL(C.YES))
|
|
|
|
C.objc_msgSend_id(scrollview, _setDocumentView, area)
|
|
|
|
return scrollview
|
|
|
|
}
|
|
|
|
|
|
|
|
func areaInScrollView(scrollview C.id) C.id {
|
|
|
|
return C.objc_msgSend_noargs(scrollview, _documentView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeArea(parentWindow C.id, alternate bool) C.id {
|
|
|
|
area := objc_alloc(_goArea)
|
|
|
|
area = objc_msgSend_rect(area, _initWithFrame,
|
|
|
|
0, 0, 100, 100)
|
|
|
|
// TODO others?
|
|
|
|
area = newAreaScrollView(area)
|
|
|
|
addControl(parentWindow, area)
|
|
|
|
return area
|
2014-03-29 22:57:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO combine the below with the delegate stuff
|
|
|
|
|
|
|
|
var (
|
|
|
|
_NSView = objc_getClass("NSView")
|
2014-03-30 12:21:10 -05:00
|
|
|
_NSView_Class = C.Class(unsafe.Pointer(_NSView))
|
2014-03-29 22:57:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func makeAreaClass(name string) (C.Class, error) {
|
|
|
|
cname := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(cname))
|
|
|
|
|
|
|
|
c := C.objc_allocateClassPair(_NSView_Class, cname, 0)
|
|
|
|
if c == C.NilClass {
|
|
|
|
return C.NilClass, fmt.Errorf("unable to create Objective-C class %s for Area; reason unknown", name)
|
|
|
|
}
|
|
|
|
C.objc_registerClassPair(c)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2014-03-30 13:25:01 -05:00
|
|
|
area_boolret = []C.char{'c', '@', ':', 0} // BOOL (*)(id, SEL)
|
2014-03-29 22:57:49 -05:00
|
|
|
)
|