libui/newcontrol_darwin.m

125 lines
3.2 KiB
Mathematica
Raw Normal View History

2015-04-07 13:42:52 -05:00
// 7 april 2015
#include "uipriv_darwin.h"
typedef struct singleView singleView;
2015-04-07 13:42:52 -05:00
struct singleView {
NSView *view;
2015-04-07 13:42:52 -05:00
NSScrollView *scrollView;
NSView *immediate; // the control that is added to the parent container; either view or scrollView
uintptr_t parent;
2015-04-07 13:42:52 -05:00
};
// TODO this will need to change if we want to provide removal
static void singleDestroy(uiControl *c)
{
singleView *s = (singleView *) (c->internal);
[s->view removeFromSuperview];
}
2015-04-07 13:42:52 -05:00
static uintptr_t singleHandle(uiControl *c)
{
singleView *s = (singleView *) (c->internal);
return (uintptr_t) (s->view);
2015-04-07 13:42:52 -05:00
}
static void singleSetParent(uiControl *c, uintptr_t parent)
{
singleView *s = (singleView *) (c->internal);
NSView *parentView;
2015-04-07 13:42:52 -05:00
s->parent = parent;
parentView = (NSView *) (s->parent);
[parentView addSubview:s->immediate];
updateParent(s->parent);
2015-04-07 13:42:52 -05:00
}
static void singleRemoveParent(uiControl *c)
{
singleView *s = (singleView *) (c->internal);
uintptr_t oldparent;
oldparent = s->parent;
s->parent = 0;
[s->immediate removeFromSuperview];
updateParent(oldparent);
}
// also good for NSBox and NSProgressIndicator
static void singlePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
2015-04-07 13:42:52 -05:00
{
singleView *s = (singleView *) (c->internal);
NSControl *control;
NSRect r;
2015-04-07 13:42:52 -05:00
control = (NSControl *) (s->view);
[control sizeToFit];
// use alignmentRect here instead of frame because we'll be resizing based on that
r = [control alignmentRectForFrame:[control frame]];
*width = (intmax_t) r.size.width;
*height = (intmax_t) r.size.height;
2015-04-07 13:42:52 -05:00
}
static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
singleView *s = (singleView *) (c->internal);
2015-04-07 13:42:52 -05:00
NSRect frame;
frame.origin.x = x;
// mac os x coordinate system has (0,0) in the lower-left
frame.origin.y = ([[s->immediate superview] bounds].size.height - height) - y;
2015-04-07 13:42:52 -05:00
frame.size.width = width;
frame.size.height = height;
frame = [s->immediate frameForAlignmentRect:frame];
[s->immediate setFrame:frame];
2015-04-07 13:42:52 -05:00
}
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder)
2015-04-07 13:42:52 -05:00
{
uiControl *c;
singleView *s;
2015-04-07 13:42:52 -05:00
s = uiNew(singleView);
2015-04-07 13:42:52 -05:00
// thanks to autoxr and arwyn in irc.freenode.net/#macdev
s->view = (NSView *) [[class alloc] initWithFrame:NSZeroRect];
s->immediate = s->view;
2015-04-07 13:42:52 -05:00
if (inScrollView) {
s->scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
[s->scrollView setDocumentView:s->view];
[s->scrollView setHasHorizontalScroller:YES];
[s->scrollView setHasVerticalScroller:YES];
[s->scrollView setAutohidesScrollers:YES];
2015-04-07 13:42:52 -05:00
if (scrollViewHasBorder)
[s->scrollView setBorderType:NSBezelBorder];
2015-04-07 13:42:52 -05:00
else
[s->scrollView setBorderType:NSNoBorder];
s->immediate = (NSView *) (s->scrollView);
2015-04-07 13:42:52 -05:00
}
c = uiNew(uiControl);
c->internal = s;
c->destroy = singleDestroy;
c->handle = singleHandle;
c->setParent = singleSetParent;
c->removeParent = singleRemoveParent;
c->preferredSize = singlePreferredSize;
c->resize = singleResize;
2015-04-07 13:42:52 -05:00
return c;
2015-04-07 13:42:52 -05:00
}
BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview)
2015-04-07 13:42:52 -05:00
{
singleView *s = (singleView *) (c->internal);
if (newSuperview == nil) {
uiFree(s);
uiFree(c);
return YES;
}
return NO;
2015-04-07 13:42:52 -05:00
}