libui/darwin/newcontrol.m

193 lines
5.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
2015-04-29 08:38:20 -05:00
uiContainer *parent;
int hidden;
int userDisabled;
int containerDisabled;
void (*onDestroy)(void *);
void *onDestroyData;
2015-04-07 13:42:52 -05:00
};
static void singleDestroy(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
if (s->parent != NULL)
2015-04-29 08:38:20 -05:00
complain("attempt to destroy a uiControl at %p while it still has a parent", c);
2015-04-18 22:17:13 -05:00
(*(s->onDestroy))(s->onDestroyData);
// release the reference we took on creation to destroy the widget
[s->immediate release];
uiFree(s);
}
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);
return (uintptr_t) (s->view);
2015-04-07 13:42:52 -05:00
}
2015-04-29 09:17:49 -05:00
static void singleSetParent(uiControl *c, uiContainer *parent)
2015-04-07 13:42:52 -05:00
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
NSView *parentView;
2015-04-29 09:17:49 -05:00
uiContainer *oldparent;
oldparent = s->parent;
s->parent = parent;
2015-04-29 08:38:20 -05:00
if (oldparent != NULL)
[s->immediate removeFromSuperview];
if (s->parent != NULL) {
2015-04-29 09:17:49 -05:00
parentView = (NSView *) uiControlHandle(uiControl(s->parent));
[parentView addSubview:s->immediate];
}
2015-04-29 08:38:20 -05:00
if (oldparent != NULL)
uiContainerUpdate(oldparent);
if (s->parent != NULL)
2015-04-29 09:17:49 -05:00
uiContainerUpdate(s->parent);
}
// 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
{
2015-04-16 12:04:46 -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)
{
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;
// 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
}
static int singleVisible(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
2015-04-29 08:38:20 -05:00
return !s->hidden;
}
static void singleShow(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
2015-04-29 08:38:20 -05:00
[s->immediate setHidden:NO];
s->hidden = 0;
2015-04-29 08:38:20 -05:00
if (s->parent != NULL)
uiContainerUpdate(s->parent);
}
static void singleHide(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
[s->immediate setHidden:YES];
s->hidden = 1;
if (s->parent != NULL)
2015-04-29 08:38:20 -05:00
uiContainerUpdate(s->parent);
}
2015-04-29 08:38:20 -05:00
static void singleEnable(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
s->userDisabled = 0;
if (!s->containerDisabled)
if ([s->view respondsToSelector:@selector(setEnabled:)])
[((NSControl *) (s->view)) setEnabled:YES];
}
static void singleDisable(uiControl *c)
{
2015-04-16 12:04:46 -05:00
singleView *s = (singleView *) (c->Internal);
s->userDisabled = 1;
2015-04-29 08:38:20 -05:00
if ([s->view respondsToSelector:@selector(setEnabled:)])
[((NSControl *) (s->view)) setEnabled:NO];
}
static void singleSysFunc(uiControl *c, uiControlSysFuncParams *p)
{
singleView *s = (singleView *) (c->Internal);
switch (p->Func) {
case uiDarwinSysFuncContainerEnable:
s->containerDisabled = 0;
if (!s->userDisabled)
if ([s->view respondsToSelector:@selector(setEnabled:)])
[((NSControl *) (s->view)) setEnabled:YES];
return;
case uiDarwinSysFuncContainerDisable:
s->containerDisabled = 1;
if ([s->view respondsToSelector:@selector(setEnabled:)])
[((NSControl *) (s->view)) setEnabled:NO];
return;
}
complain("unknown p->Func %d in singleSysFunc()", p->Func);
}
void uiDarwinMakeControl(uiControl *c, Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void (*onDestroy)(void *), void *onDestroyData)
2015-04-07 13:42:52 -05:00
{
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
}
s->onDestroy = onDestroy;
s->onDestroyData = onDestroyData;
// and keep a reference to s->immediate for when we remove the control from its parent
[s->immediate retain];
uiControl(c)->Internal = s;
uiControl(c)->Destroy = singleDestroy;
uiControl(c)->Handle = singleHandle;
uiControl(c)->SetParent = singleSetParent;
uiControl(c)->PreferredSize = singlePreferredSize;
uiControl(c)->Resize = singleResize;
uiControl(c)->Visible = singleVisible;
uiControl(c)->Show = singleShow;
uiControl(c)->Hide = singleHide;
uiControl(c)->Enable = singleEnable;
uiControl(c)->Disable = singleDisable;
uiControl(c)->SysFunc = singleSysFunc;
2015-04-07 13:42:52 -05:00
}