Began the big sizer cleanup: renamed sizer to container, renamed sizer.go to container.go, did the Mac OS X migration, moved containerctrls.go out of the way by merging its declarations into basicctrls.go, and did a quick cleanup fix to container_darwin.m.

This commit is contained in:
Pietro Gagliardi 2014-08-04 17:46:08 -04:00
parent 74e2214f43
commit 88c01bf695
8 changed files with 78 additions and 98 deletions

View File

@ -63,6 +63,27 @@ func NewPasswordField() TextField {
return newPasswordField() return newPasswordField()
} }
// Tab is a Control that contains multiple pages of tabs, each containing a single Control.
// You can add and remove tabs from the Tab at any time.
//
// [TODO if each tab of your Tab is going to have the same content Controls, then use LikeTab instead, to conserve resources]
type Tab interface {
Control
// Append adds a new tab to Tab.
// The tab is added to the end of the current list of tabs.
Append(name string, control Control)
// Delete removes the given tab.
// It panics if index is out of range.
// Delete(index int)
//TODO
}
// NewTab creates a new Tab with no tabs.
func NewTab() Tab {
return newTab()
}
// Label is a Control that shows a static line of text. // Label is a Control that shows a static line of text.
// Label shows one line of text; any text that does not fit is truncated. // Label shows one line of text; any text that does not fit is truncated.

View File

@ -25,18 +25,18 @@ type controlSizing interface {
getAuxResizeInfo(*sizing) getAuxResizeInfo(*sizing)
} }
// A sizer hosts a Control and resizes that Control based on changes in size to the parent Window. // A container hosts a Control and resizes that Control based on changes in size to the parent Window.
// sizer is used by Window, Tab, and [TODO implement] Group to contain and control their respective controls. // container is used by Window, Tab, and [TODO implement] Group to contain and control their respective Controls.
// Window is the beginning of the resize chain; resizes happen on the system side. // Tab and Group use containers for their content; as such, their commitResize() functions should only change the size of the Tab and Group themselves, and have their containers do the real work.
// Tab and Group are Controls and thus implement controlSizing; they should call their internal sizers's resize() method in their own commitResize(). // All containers must embed containerbase.
type sizer struct { type containerbase struct {
child Control child Control
} }
// set to true to apply spacing to all windows // set to true to apply spacing to all windows
var spaced bool = false var spaced bool = false
func (c *sizer) resize(x, y, width, height int) { func (c *container) resize(x, y, width, height int) {
if c.child == nil { // no children; nothing to do if c.child == nil { // no children; nothing to do
return return
} }

View File

@ -10,15 +10,23 @@ import (
import "C" import "C"
type container struct { type container struct {
containerbase
// TODO rename to id // TODO rename to id
view C.id view C.id
*sizer }
type sizing struct {
sizingbase
// for size calculations
// nothing for mac
// for the actual resizing
neighborAlign C.struct_xalignment
} }
func newContainer(child Control) *container { func newContainer(child Control) *container {
c := &container{ c := new(container)
sizer: new(sizer),
}
c.view = C.newContainerView(unsafe.Pointer(c)) c.view = C.newContainerView(unsafe.Pointer(c))
c.child = child c.child = child
c.child.setParent(&controlParent{c.view}) c.child.setParent(&controlParent{c.view})
@ -31,3 +39,31 @@ func containerResized(data unsafe.Pointer, width C.intptr_t, height C.intptr_t)
// the origin of a view's content area is always (0, 0) // the origin of a view's content area is always (0, 0)
c.resize(0, 0, int(width), int(height)) c.resize(0, 0, int(width), int(height))
} }
// THIS IS A GUESS. TODO.
// The only indication that this is remotely correct is the Auto Layout Guide implying that 12 pixels is the "Aqua space".
const (
macXMargin = 12
macYMargin = 12
macXPadding = 12
macYPadding = 12
)
func (c *container) beginResize() (d *sizing) {
d = new(sizing)
if spaced {
d.xmargin = macXMargin
d.ymargin = macYMargin
d.xpadding = macXPadding
d.ypadding = macYPadding
}
return d
}
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
}
}

View File

@ -4,6 +4,8 @@
#include "_cgo_export.h" #include "_cgo_export.h"
#include <Cocoa/Cocoa.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 // 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: // fornunately:
// - NSWindow resizing calls -[setFrameSize:] (but not -[setFrame:]) // - NSWindow resizing calls -[setFrameSize:] (but not -[setFrame:])
@ -23,8 +25,7 @@
- (void)setFrameSize:(NSSize)s - (void)setFrameSize:(NSSize)s
{ {
[super setFrameSize:s]; [super setFrameSize:s];
if (self->gocontainer != NULL) containerResized(self->gocontainer, (intptr_t) s.width, (intptr_t) s.height);
containerResized(self->gocontainer, (intptr_t) s.width, (intptr_t) s.height);
} }
@end @end
@ -37,3 +38,8 @@ id newContainerView(void *gocontainer)
c->gocontainer = gocontainer; c->gocontainer = gocontainer;
return (id) c; 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)];
}

View File

@ -1,25 +0,0 @@
// 25 july 2014
package ui
// Tab is a Control that contains multiple pages of tabs, each containing a single Control.
// You can add and remove tabs from the Tab at any time.
//
// [TODO if each tab of your Tab is going to have the same content Controls, then use LikeTab instead, to conserve resources]
type Tab interface {
Control
// Append adds a new tab to Tab.
// The tab is added to the end of the current list of tabs.
Append(name string, control Control)
// Delete removes the given tab.
// It panics if index is out of range.
// Delete(index int)
//TODO
}
// NewTab creates a new Tab with no tabs.
func NewTab() Tab {
return newTab()
}

View File

@ -51,7 +51,8 @@ extern const char *textFieldText(id);
extern void textFieldSetText(id, char *); extern void textFieldSetText(id, char *);
extern id newLabel(void); extern id newLabel(void);
/* sizing_darwin.m */ /* container_darwin.m */
extern id newContainerView(void *);
extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t); extern void moveControl(id, intptr_t, intptr_t, intptr_t, intptr_t);
/* tab_darwin.m */ /* tab_darwin.m */
@ -88,7 +89,4 @@ extern struct xsize areaPrefSize(id);
extern struct xalignment alignmentInfo(id, struct xrect); extern struct xalignment alignmentInfo(id, struct xrect);
extern struct xrect frame(id); extern struct xrect frame(id);
/* container_darwin.m */
extern id newContainerView(void *);
#endif #endif

View File

@ -1,44 +0,0 @@
// 1 march 2014
package ui
// #include "objc_darwin.h"
import "C"
type sizing struct {
sizingbase
// for size calculations
// nothing for mac
// for the actual resizing
neighborAlign C.struct_xalignment
}
// THIS IS A GUESS. TODO.
// The only indication that this is remotely correct is the Auto Layout Guide implying that 12 pixels is the "Aqua space".
const (
macXMargin = 12
macYMargin = 12
macXPadding = 12
macYPadding = 12
)
func (s *sizer) beginResize() (d *sizing) {
d = new(sizing)
if spaced {
d.xmargin = macXMargin
d.ymargin = macYMargin
d.xpadding = macXPadding
d.ypadding = macYPadding
}
return d
}
func (s *sizer) 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
}
}

View File

@ -1,12 +0,0 @@
// 17 july 2014
#import "objc_darwin.h"
#import "_cgo_export.h"
#import <Cocoa/Cocoa.h>
#define toNSView(x) ((NSView *) (x))
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)];
}