2015-04-07 13:42:52 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv_darwin.h"
|
|
|
|
|
2015-04-09 17:39:21 -05:00
|
|
|
typedef struct singleView singleView;
|
2015-04-07 13:42:52 -05:00
|
|
|
|
2015-04-09 17:39:21 -05:00
|
|
|
struct singleView {
|
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-13 11:53:05 -05:00
|
|
|
uiParent *parent;
|
2015-04-11 17:17:46 -05:00
|
|
|
BOOL userHid;
|
|
|
|
BOOL containerHid;
|
|
|
|
BOOL userDisabled;
|
|
|
|
BOOL containerDisabled;
|
2015-04-17 17:11:03 -05:00
|
|
|
void (*onDestroy)(void *);
|
|
|
|
void *onDestroyData;
|
2015-04-07 13:42:52 -05:00
|
|
|
};
|
|
|
|
|
2015-04-08 02:38:08 -05:00
|
|
|
static void singleDestroy(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-09 17:39:21 -05:00
|
|
|
|
2015-04-17 17:11:03 -05:00
|
|
|
[s->immediate retain]; // to keep alive when removing
|
|
|
|
if (s->parent != NULL) {
|
|
|
|
[s->immediate removeFromSuperview];
|
|
|
|
s->parent = NULL;
|
|
|
|
}
|
2015-04-10 22:24:21 -05:00
|
|
|
[destroyedControlsView addSubview:s->immediate];
|
2015-04-17 17:11:03 -05:00
|
|
|
(*(s->onDestroy))(s->onDestroyData);
|
|
|
|
[s->immediate release];
|
2015-04-08 02:38:08 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 13:42:52 -05:00
|
|
|
static uintptr_t singleHandle(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-09 17:39:21 -05:00
|
|
|
|
|
|
|
return (uintptr_t) (s->view);
|
2015-04-07 13:42:52 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:11:03 -05:00
|
|
|
// TODO figure out retain/release for this
|
2015-04-13 11:53:05 -05:00
|
|
|
static void singleSetParent(uiControl *c, uiParent *parent)
|
2015-04-07 13:42:52 -05:00
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-08 18:14:22 -05:00
|
|
|
NSView *parentView;
|
2015-04-12 19:08:32 -05:00
|
|
|
uiParent *oldparent;
|
2015-04-09 15:21:09 -05:00
|
|
|
|
|
|
|
oldparent = s->parent;
|
2015-04-14 11:51:20 -05:00
|
|
|
s->parent = parent;
|
|
|
|
if (oldparent != NULL) {
|
|
|
|
[s->immediate removeFromSuperview];
|
|
|
|
uiParentUpdate(oldparent);
|
|
|
|
}
|
|
|
|
if (s->parent != NULL) {
|
2015-04-14 18:38:41 -05:00
|
|
|
// TODO uiControlView(), uiParentView()
|
2015-04-14 11:51:20 -05:00
|
|
|
parentView = (NSView *) uiParentHandle(s->parent);
|
|
|
|
[parentView addSubview:s->immediate];
|
|
|
|
uiParentUpdate(s->parent);
|
|
|
|
}
|
2015-04-09 15:21:09 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 19:28:28 -05:00
|
|
|
// also good for NSBox and NSProgressIndicator
|
2015-04-09 17:39:21 -05:00
|
|
|
static void singlePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-07 13:42:52 -05:00
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-07 19:28:28 -05:00
|
|
|
NSControl *control;
|
|
|
|
NSRect r;
|
2015-04-07 13:42:52 -05:00
|
|
|
|
2015-04-09 17:39:21 -05:00
|
|
|
control = (NSControl *) (s->view);
|
2015-04-07 19:28:28 -05:00
|
|
|
[control sizeToFit];
|
|
|
|
// use alignmentRect here instead of frame because we'll be resizing based on that
|
|
|
|
r = [control alignmentRectForFrame:[control frame]];
|
2015-04-09 17:39:21 -05:00
|
|
|
*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)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-07 13:42:52 -05:00
|
|
|
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-09 17:39:21 -05:00
|
|
|
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;
|
2015-04-09 17:39:21 -05:00
|
|
|
frame = [s->immediate frameForAlignmentRect:frame];
|
|
|
|
[s->immediate setFrame:frame];
|
2015-04-07 13:42:52 -05:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:17:46 -05:00
|
|
|
static int singleVisible(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
if (s->userHid)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleShow(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->userHid = NO;
|
|
|
|
if (!s->containerHid) {
|
|
|
|
[s->immediate setHidden:NO];
|
2015-04-13 11:53:05 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 17:17:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleHide(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->userHid = YES;
|
|
|
|
[s->immediate setHidden:YES];
|
2015-04-13 11:53:05 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 17:17:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void singleContainerShow(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->containerHid = NO;
|
|
|
|
if (!s->userHid) {
|
|
|
|
[s->immediate setHidden:NO];
|
2015-04-13 11:53:05 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 17:17:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleContainerHide(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->containerHid = YES;
|
|
|
|
[s->immediate setHidden:YES];
|
2015-04-13 11:53:05 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 17:17:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void enable(singleView *s)
|
|
|
|
{
|
|
|
|
if ([s->view respondsToSelector:@selector(setEnabled:)])
|
|
|
|
[((NSControl *) (s->view)) setEnabled:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disable(singleView *s)
|
|
|
|
{
|
|
|
|
if ([s->view respondsToSelector:@selector(setEnabled:)])
|
|
|
|
[((NSControl *) (s->view)) setEnabled:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleEnable(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->userDisabled = NO;
|
|
|
|
if (!s->containerDisabled)
|
|
|
|
enable(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleDisable(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->userDisabled = YES;
|
|
|
|
disable(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleContainerEnable(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->containerDisabled = NO;
|
|
|
|
if (!s->userDisabled)
|
|
|
|
enable(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void singleContainerDisable(uiControl *c)
|
|
|
|
{
|
2015-04-16 12:04:46 -05:00
|
|
|
singleView *s = (singleView *) (c->Internal);
|
2015-04-11 17:17:46 -05:00
|
|
|
|
|
|
|
s->containerDisabled = YES;
|
|
|
|
disable(s);
|
|
|
|
}
|
|
|
|
|
2015-04-17 17:11:03 -05:00
|
|
|
void uiDarwinNewControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void (*onDestroy)(void *), void *onDestroyData)
|
2015-04-07 13:42:52 -05:00
|
|
|
{
|
2015-04-09 17:39:21 -05:00
|
|
|
singleView *s;
|
2015-04-07 13:42:52 -05:00
|
|
|
|
2015-04-09 17:39:21 -05:00
|
|
|
s = uiNew(singleView);
|
2015-04-07 13:42:52 -05:00
|
|
|
// thanks to autoxr and arwyn in irc.freenode.net/#macdev
|
2015-04-09 17:39:21 -05:00
|
|
|
s->view = (NSView *) [[class alloc] initWithFrame:NSZeroRect];
|
|
|
|
s->immediate = s->view;
|
2015-04-07 13:42:52 -05:00
|
|
|
|
|
|
|
if (inScrollView) {
|
2015-04-09 17:39:21 -05:00
|
|
|
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)
|
2015-04-09 17:39:21 -05:00
|
|
|
[s->scrollView setBorderType:NSBezelBorder];
|
2015-04-07 13:42:52 -05:00
|
|
|
else
|
2015-04-09 17:39:21 -05:00
|
|
|
[s->scrollView setBorderType:NSNoBorder];
|
|
|
|
s->immediate = (NSView *) (s->scrollView);
|
2015-04-07 13:42:52 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 17:11:03 -05:00
|
|
|
s->onDestroy = onDestroy;
|
|
|
|
s->onDestroyData = onDestroyData;
|
|
|
|
|
2015-04-10 22:22:46 -05:00
|
|
|
// and keep a reference to s->immediate for when we remove the control from its parent
|
|
|
|
[s->immediate retain];
|
|
|
|
|
2015-04-16 12:04:46 -05:00
|
|
|
c->Internal = s;
|
|
|
|
c->Destroy = singleDestroy;
|
|
|
|
c->Handle = singleHandle;
|
|
|
|
c->SetParent = singleSetParent;
|
|
|
|
c->PreferredSize = singlePreferredSize;
|
|
|
|
c->Resize = singleResize;
|
|
|
|
c->Visible = singleVisible;
|
|
|
|
c->Show = singleShow;
|
|
|
|
c->Hide = singleHide;
|
|
|
|
c->ContainerShow = singleContainerShow;
|
|
|
|
c->ContainerHide = singleContainerHide;
|
|
|
|
c->Enable = singleEnable;
|
|
|
|
c->Disable = singleDisable;
|
|
|
|
c->ContainerEnable = singleContainerEnable;
|
|
|
|
c->ContainerDisable = singleContainerDisable;
|
2015-04-07 13:42:52 -05:00
|
|
|
}
|