diff options
| author | Pietro Gagliardi <[email protected]> | 2014-10-18 14:17:35 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2014-10-18 14:17:35 -0400 |
| commit | fd9e614faaae390a42f7dc39e63d0197ea9d7efa (patch) | |
| tree | 67daa05343c858cd697584b87061b63e7805e302 | |
| parent | b1ac28cc938d2de25357328d9da910a84a2cc8cc (diff) | |
Started the Mac OS X conversion.
| -rw-r--r-- | newctrl/container_darwin.go | 76 | ||||
| -rw-r--r-- | newctrl/container_darwin.m | 51 | ||||
| -rw-r--r-- | newctrl/control_darwin.go | 56 | ||||
| -rw-r--r-- | newctrl/objc_darwin.h | 151 |
4 files changed, 334 insertions, 0 deletions
diff --git a/newctrl/container_darwin.go b/newctrl/container_darwin.go new file mode 100644 index 0000000..3e8c437 --- /dev/null +++ b/newctrl/container_darwin.go @@ -0,0 +1,76 @@ +// 4 august 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type container struct { + *controlSingleObject +} + +type sizing struct { + sizingbase + + // for size calculations + // nothing for mac + + // for the actual resizing + neighborAlign C.struct_xalignment +} + +func newContainer() *container { + c := new(container) + c.controlSingleObject = newControlSingleObject(C.newContainerView(unsafe.Pointer(c))) + return c +} + +func (c *container) parent() *controlParent { + return &controlParent{c.id} +} + +func (c *container) allocation(margined bool) C.struct_xrect { + b := C.containerBounds(c.id) + if margined { + b.x += C.intptr_t(macXMargin) + b.y += C.intptr_t(macYMargin) + b.width -= C.intptr_t(macXMargin) * 2 + b.height -= C.intptr_t(macYMargin) * 2 + } + return b +} + +// we can just return these values as is +func (c *container) bounds(d *sizing) (int, int, int, int) { + b := C.containerBounds(c.id) + return int(b.x), int(b.y), int(b.width), int(b.height) +} + +// These are based on measurements from Interface Builder. +const ( + macXMargin = 20 + macYMargin = 20 + macXPadding = 8 + macYPadding = 8 +) + +func (w *window) beginResize() (d *sizing) { + d = new(sizing) + d.xpadding = macXPadding + d.ypadding = macYPadding + return d +} + +/*TODO +func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { + for _, a := range allocations { + // winheight - y because (0,0) is the bottom-left corner of the window and not the top-left corner + // (winheight - y) - height because (x, y) is the bottom-left corner of the control and not the top-left + a.y = (winheight - a.y) - a.height + } +} +*/ diff --git a/newctrl/container_darwin.m b/newctrl/container_darwin.m new file mode 100644 index 0000000..c5a50e1 --- /dev/null +++ b/newctrl/container_darwin.m @@ -0,0 +1,51 @@ +// 4 august 2014 + +#include "objc_darwin.h" +#include "_cgo_export.h" +#include <Cocoa/Cocoa.h> + +#define toNSView(x) ((NSView *) (x)) + +// calling -[className] on the content views of NSWindow, NSTabItem, and NSBox all return NSView, so I'm assuming I just need to override these +// fornunately: +// - NSWindow resizing calls -[setFrameSize:] (but not -[setFrame:]) +// - NSTab resizing calls both -[setFrame:] and -[setFrameSIze:] on the current tab +// - NSTab switching tabs calls both -[setFrame:] and -[setFrameSize:] on the new tab +// so we just override setFrameSize: +// thanks to mikeash and JtRip in irc.freenode.net/#macdev +@interface goContainerView : NSView { +@public + void *gocontainer; +} +@end + +@implementation goContainerView + +@end + +id newContainerView(void *gocontainer) +{ + goContainerView *c; + + c = [[goContainerView alloc] initWithFrame:NSZeroRect]; + c->gocontainer = gocontainer; + return (id) c; +} + +void moveControl(id c, intptr_t x, intptr_t y, intptr_t width, intptr_t height) +{ + [toNSView(c) setFrame:NSMakeRect((CGFloat) x, (CGFloat) y, (CGFloat) width, (CGFloat) height)]; +} + +struct xrect containerBounds(id view) +{ + NSRect b; + struct xrect r; + + b = [toNSView(view) bounds]; + r.x = (intptr_t) b.origin.x; + r.y = (intptr_t) b.origin.y; + r.width = (intptr_t) b.size.width; + r.height = (intptr_t) b.size.height; + return r; +} diff --git a/newctrl/control_darwin.go b/newctrl/control_darwin.go new file mode 100644 index 0000000..330d50c --- /dev/null +++ b/newctrl/control_darwin.go @@ -0,0 +1,56 @@ +// 30 july 2014 + +package ui + +// #include "objc_darwin.h" +import "C" + +type controlParent struct { + id C.id +} + +type controlSingleObject struct { + *controlbase + id C.id +} + +func newControlSingleObject(id C.id) *controlSingleObject { + c := new(controlSingleObject) + c.controlbase = &controlbase{ + fsetParent: c.xsetParent, + fpreferredSize: c.xpreferredSize, + fresize: c.xresize, + } + c.id = id + return c +} + +func (c *controlSingleObject) xsetParent(p *controlParent) { + // redrawing the new window handled by C.parent() + C.parent(c.id, p.id) +} + +func (c *controlSingleObject) xpreferredSize(d *sizing) (int, int) { + s := C.controlPreferredSize(c.id) + return int(s.width), int(s.height) +} + +func (c *controlSingleObject) xresize(x int, y int, width int, height int, d *sizing) { + C.moveControl(c.id, C.intptr_t(x), C.intptr_t(y), C.intptr_t(width), C.intptr_t(height)) +} + +type scroller struct { + *controlSingleObject + scroller *controlSingleObject +} + +func newScroller(child C.id, bordered bool) *scroller { + sid := C.newScrollView(child, toBOOL(bordered)) + s := &scroller{ + controlSingleObject: newControlSingleObject(child), + scroller: newControlSingleObject(sid), + } + s.fsetParent = s.scroller.fsetParent + s.fresize = s .scroller.fresize + return s +} diff --git a/newctrl/objc_darwin.h b/newctrl/objc_darwin.h new file mode 100644 index 0000000..9f51b8e --- /dev/null +++ b/newctrl/objc_darwin.h @@ -0,0 +1,151 @@ +/* 8 july 2014 */ + +/* cgo will include this file multiple times */ +#ifndef __GO_UI_OBJC_DARWIN_H__ +#define __GO_UI_OBJC_DARWIN_H__ + +#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_7 +#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7 + +#include <stdlib.h> +#include <stdint.h> +#include <objc/message.h> +#include <objc/objc.h> +#include <objc/runtime.h> + +/* Objective-C -> Go types for max safety */ +struct xsize { + intptr_t width; + intptr_t height; +}; + +struct xrect { + intptr_t x; + intptr_t y; + intptr_t width; + intptr_t height; +}; + +struct xalignment { + struct xrect rect; + intptr_t baseline; +}; + +struct xpoint { + intptr_t x; + intptr_t y; +}; + +/* uitask_darwin.m */ +extern id getAppDelegate(void); /* used by the other .m files */ +extern void uiinit(char **); +extern void uimsgloop(void); +extern void uistop(void); +extern void beginModal(void); +extern void endModal(void); +extern void issue(void *); + +/* window_darwin.m */ +extern id newWindow(intptr_t, intptr_t); +extern void windowSetDelegate(id, void *); +extern void windowSetContentView(id, id); +extern const char *windowTitle(id); +extern void windowSetTitle(id, const char *); +extern void windowShow(id); +extern void windowHide(id); +extern void windowClose(id); +extern id windowContentView(id); +extern void windowRedraw(id); + +/* basicctrls_darwin.m */ +#define textfieldWidth (96) /* according to Interface Builder */ +extern id newButton(void); +extern void buttonSetDelegate(id, void *); +extern const char *buttonText(id); +extern void buttonSetText(id, char *); +extern id newCheckbox(void); +extern void checkboxSetDelegate(id, void *); +extern BOOL checkboxChecked(id); +extern void checkboxSetChecked(id, BOOL); +extern id finishNewTextField(id, BOOL); +extern id newTextField(void); +extern id newPasswordField(void); +extern void textfieldSetDelegate(id, void *); +extern const char *textfieldText(id); +extern void textfieldSetText(id, char *); +extern id textfieldOpenInvalidPopover(id, char *); +extern void textfieldCloseInvalidPopover(id); +extern id newLabel(void); +extern id newGroup(id); +extern const char *groupText(id); +extern void groupSetText(id, char *); + +/* container_darwin.m */ +extern id newContainerView(void *); +extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t); +extern struct xrect containerBounds(id); + +/* tab_darwin.m */ +extern id newTab(void); +extern void tabAppend(id, char *, id); +extern struct xsize tabPreferredSize(id); + +/* table_darwin.m */ +enum { + colTypeText, + colTypeImage, + colTypeCheckbox, +}; +extern id newTable(void); +extern void tableAppendColumn(id, intptr_t, char *, int, BOOL); +extern void tableUpdate(id); +extern void tableMakeDataSource(id, void *); +extern struct xsize tablePreferredSize(id); +extern intptr_t tableSelected(id); +extern void tableSelect(id, intptr_t); + +/* control_darwin.m */ +extern void parent(id, id); +extern void controlSetHidden(id, BOOL); +extern void setStandardControlFont(id); +extern void setSmallControlFont(id); +extern struct xsize controlPreferredSize(id); +extern id newScrollView(id, BOOL); +extern struct xalignment alignmentInfo(id, struct xrect); +extern struct xalignment alignmentInfoFrame(id); + +/* area_darwin.h */ +extern Class getAreaClass(void); +extern id newArea(void *); +extern BOOL drawImage(void *, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t); +extern const uintptr_t cNSShiftKeyMask; +extern const uintptr_t cNSControlKeyMask; +extern const uintptr_t cNSAlternateKeyMask; +extern const uintptr_t cNSCommandKeyMask; +extern uintptr_t modifierFlags(id); +extern struct xpoint getTranslatedEventPoint(id, id); +extern intptr_t buttonNumber(id); +extern intptr_t clickCount(id); +extern uintptr_t pressedMouseButtons(void); +extern uintptr_t keyCode(id); +extern void areaRepaint(id, struct xrect); +extern void areaRepaintAll(id); +extern void areaTextFieldOpen(id, id, intptr_t, intptr_t); +extern void areaSetTextField(id, id); +extern void areaEndTextFieldEditing(id, id); + + +/* common_darwin.m */ +extern void disableAutocorrect(id); + +/* imagerep_darwin.m */ +extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t); + +/* dialog_darwin.m */ +extern void openFile(id, void *); + +/* warningpopover_darwin.m */ +extern id newWarningPopover(char *); +extern void warningPopoverShow(id, id); + +#endif |
