libui/darwin/tab.m

98 lines
2.2 KiB
Mathematica
Raw Normal View History

// 12 april 2015
#import "uipriv_darwin.h"
// TODO
// - verify margins against extra space around the tab
2015-04-17 17:45:17 -05:00
struct tab {
uiTab t;
NSTabView *tabview;
NSMutableArray *pages;
2015-04-17 17:45:17 -05:00
};
2015-04-17 17:45:17 -05:00
static void destroy(void *data)
{
2015-04-17 17:45:17 -05:00
struct tab *t = (struct tab *) data;
[t->pages enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSValue *v = (NSValue *) obj;
2015-04-29 09:06:39 -05:00
uiContainer *p;
2015-04-29 09:06:39 -05:00
// TODO this is definitely wrong but
p = (uiContainer *) [v pointerValue];
uiControlDestroy(uiControl(p));
}];
[t->pages release];
2015-04-17 17:45:17 -05:00
uiFree(t);
}
// the default new control implementation uses -sizeToFit, which we don't have with NSTabView
// fortunately, we do have -minimumSize
static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
2015-04-17 17:45:17 -05:00
struct tab *t = (struct tab *) c;
NSSize s;
2015-04-17 17:45:17 -05:00
s = [t->tabview minimumSize];
*width = (intmax_t) (s.width);
*height = (intmax_t) (s.height);
}
2015-04-29 09:06:39 -05:00
static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
{
2015-04-17 17:45:17 -05:00
struct tab *t = (struct tab *) tt;
2015-04-29 09:06:39 -05:00
uiContainer *page;
NSTabViewItem *i;
2015-04-29 09:06:39 -05:00
page = newBin();
2015-04-29 09:17:49 -05:00
binSetMainControl(page, child);
2015-04-29 09:06:39 -05:00
[t->pages addObject:[NSValue valueWithPointer:page]];
i = [[NSTabViewItem alloc] initWithIdentifier:nil];
[i setLabel:toNSString(name)];
2015-04-29 09:17:49 -05:00
[i setView:((NSView *) uiControlHandle(uiControl(page)))];
2015-04-17 17:45:17 -05:00
[t->tabview addTabViewItem:i];
}
static void tabDeletePage(uiTab *tt, uintmax_t n)
{
struct tab *t = (struct tab *) tt;
NSValue *v;
2015-04-29 09:06:39 -05:00
uiContainer *p;
NSTabViewItem *i;
v = (NSValue *) [t->pages objectAtIndex:n];
2015-04-29 09:06:39 -05:00
p = (uiContainer *) [v pointerValue];
[t->pages removeObjectAtIndex:n];
2015-04-29 09:06:39 -05:00
// make sure the children of the tab aren't destroyed
2015-04-29 09:06:39 -05:00
binSetMainControl(p, NULL);
// TODO negotiate lifetimes better
i = [t->tabview tabViewItemAtIndex:n];
[t->tabview removeTabViewItem:i];
}
uiTab *uiNewTab(void)
{
2015-04-17 17:45:17 -05:00
struct tab *t;
2015-04-17 17:45:17 -05:00
t = uiNew(struct tab);
uiDarwinNewControl(uiControl(t), [NSTabView class], NO, NO, destroy, t);
2015-04-17 17:45:17 -05:00
2015-04-29 09:06:39 -05:00
t->tabview = (NSTabView *) uiControlHandle(uiControl(t));
// also good for NSTabView (same selector and everything)
2015-04-17 17:45:17 -05:00
setStandardControlFont((NSControl *) (t->tabview));
t->pages = [NSMutableArray new];
uiControl(t)->PreferredSize = preferredSize;
2015-04-29 09:06:39 -05:00
uiTab(t)->AppendPage = tabAppendPage;
uiTab(t)->DeletePage = tabDeletePage;
2015-04-17 17:45:17 -05:00
return uiTab(t);
}