summaryrefslogtreecommitdiff
path: root/new/parent_darwin.m
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2015-04-13 12:53:05 -0400
committerPietro Gagliardi <[email protected]>2015-04-13 12:53:05 -0400
commit963feb6c0668b3c64242607fe71f458420a91f32 (patch)
tree89876bc232359f6b166dc325a609bf27cac05ed7 /new/parent_darwin.m
parente4147f14afecc95db90430143d672512a9e92bdb (diff)
Converted and fixed the Mac OS X code for uiParent. I'm still not happy, but this is DEFINITELY a step in the right direction... Perhaps automatic deletion is a pipe dream :/ I should also really nail the parenting/unparenting logic, but for that I need to add one more method.
Diffstat (limited to 'new/parent_darwin.m')
-rw-r--r--new/parent_darwin.m118
1 files changed, 118 insertions, 0 deletions
diff --git a/new/parent_darwin.m b/new/parent_darwin.m
new file mode 100644
index 0000000..abf0e59
--- /dev/null
+++ b/new/parent_darwin.m
@@ -0,0 +1,118 @@
+// 4 august 2014
+#import "uipriv_darwin.h"
+
+// 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:])
+// - NSTabView resizing calls both -[setFrame:] and -[setFrameSIze:] on the current tab
+// - NSTabView 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 uipParent : NSView {
+// TODO
+@public
+ uiControl *child;
+ intmax_t marginLeft;
+ intmax_t marginTop;
+ intmax_t marginRight;
+ intmax_t marginBottom;
+}
+- (void)uiUpdateNow;
+@end
+
+@implementation uipParent
+
+uiLogObjCClassAllocations
+
+- (void)viewDidMoveToSuperview
+{
+ // we can't just use nil because NSTabView will set page views to nil when they're tabbed away
+ // this means that we have to explicitly move them to the destroyed controls view when we're done with them, and likewise in NSWindow
+ if ([self superview] == destroyedControlsView)
+ if (self->child != NULL) {
+ uiControlDestroy(self->child);
+ self->child = NULL;
+ [self release];
+ }
+ [super viewDidMoveToSuperview];
+}
+
+- (void)setFrameSize:(NSSize)s
+{
+ [super setFrameSize:s];
+ [self uiUpdateNow];
+}
+
+// These are based on measurements from Interface Builder.
+// These seem to be based on Auto Layout constants, but I don't see an API that exposes these...
+// This one is 8 for most pairs of controls that I've tried; the only difference is between two pushbuttons, where it's 12...
+#define macXPadding 8
+// Likewise, this one appears to be 12 for pairs of push buttons...
+#define macYPadding 8
+
+- (void)uiUpdateNow
+{
+ uiSizing d;
+ intmax_t x, y, width, height;
+
+ if (self->child == NULL)
+ return;
+ x = [self bounds].origin.x + self->marginLeft;
+ y = [self bounds].origin.y + self->marginTop;
+ width = [self bounds].size.width - (self->marginLeft + self->marginRight);
+ height = [self bounds].size.height - (self->marginTop + self->marginBottom);
+ d.xPadding = macXPadding;
+ d.yPadding = macYPadding;
+ uiControlResize(self->child, x, y, width, height, &d);
+}
+
+@end
+
+static uintptr_t parentHandle(uiParent *p)
+{
+ uipParent *pp = (uipParent *) (p->Internal);
+
+ return (uintptr_t) pp;
+}
+
+static void parentSetChild(uiParent *p, uiControl *child)
+{
+ uipParent *pp = (uipParent *) (p->Internal);
+
+ pp->child = child;
+ if (pp->child != NULL)
+ uiControlSetParent(child, p);
+}
+
+static void parentSetMargins(uiParent *p, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom)
+{
+ uipParent *pp = (uipParent *) (p->Internal);
+
+ pp->marginLeft = left;
+ pp->marginTop = top;
+ pp->marginRight = right;
+ pp->marginBottom = bottom;
+}
+
+static void parentUpdate(uiParent *p)
+{
+ uipParent *pp = (uipParent *) (p->Internal);
+
+ [pp uiUpdateNow];
+}
+
+uiParent *uiNewParent(uintptr_t osParent)
+{
+ uiParent *p;
+
+ p = uiNew(uiParent);
+ p->Internal = [[uipParent alloc] initWithFrame:NSZeroRect];
+ p->Handle = parentHandle;
+ p->SetChild = parentSetChild;
+ p->SetMargins = parentSetMargins;
+ p->Update = parentUpdate;
+ // don't use osParent; we'll need to call specific selectors to set the parent view
+ // and keep the view alive so we can release it properly later
+ [((uipParent *) (p->Internal)) retain];
+ return p;
+} \ No newline at end of file