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