2015-04-12 14:46:58 -05:00
|
|
|
// 12 april 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
2015-04-17 17:45:17 -05:00
|
|
|
struct tab {
|
|
|
|
uiTab t;
|
|
|
|
NSTabView *tabview;
|
2015-04-19 10:41:23 -05:00
|
|
|
NSMutableArray *pages;
|
2015-04-30 15:34:38 -05:00
|
|
|
NSMutableArray *margined;
|
2015-05-04 13:53:23 -05:00
|
|
|
void (*baseEnable)(uiControl *);
|
|
|
|
void (*baseDisable)(uiControl *);
|
|
|
|
void (*baseSysFunc)(uiControl *, uiControlSysFuncParams *);
|
2015-04-17 17:45:17 -05:00
|
|
|
};
|
2015-04-12 14:46:58 -05:00
|
|
|
|
2015-04-17 17:45:17 -05:00
|
|
|
static void destroy(void *data)
|
2015-04-12 14:46:58 -05:00
|
|
|
{
|
2015-04-17 17:45:17 -05:00
|
|
|
struct tab *t = (struct tab *) data;
|
2015-04-12 14:46:58 -05:00
|
|
|
|
2015-04-30 18:56:12 -05:00
|
|
|
// first destroy all tab pages so we can destroy all the bins
|
|
|
|
while ([t->tabview numberOfTabViewItems] != 0)
|
|
|
|
[t->tabview removeTabViewItem:[t->tabview tabViewItemAtIndex:0]];
|
|
|
|
// then destroy all the bins, destroying children in the process
|
2015-05-06 00:38:32 -05:00
|
|
|
// the above loop serves the purpose of binSetParent()
|
2015-04-19 10:41:23 -05:00
|
|
|
[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-19 10:41:23 -05:00
|
|
|
|
2015-04-29 09:06:39 -05:00
|
|
|
p = (uiContainer *) [v pointerValue];
|
|
|
|
uiControlDestroy(uiControl(p));
|
2015-04-19 10:41:23 -05:00
|
|
|
}];
|
2015-04-30 18:56:12 -05:00
|
|
|
// and finally destroy ourselves
|
2015-04-19 10:41:23 -05:00
|
|
|
[t->pages release];
|
2015-04-30 15:34:38 -05:00
|
|
|
[t->margined release];
|
2015-04-17 17:45:17 -05:00
|
|
|
uiFree(t);
|
|
|
|
}
|
2015-04-12 14:46:58 -05:00
|
|
|
|
|
|
|
// the default new control implementation uses -sizeToFit, which we don't have with NSTabView
|
|
|
|
// fortunately, we do have -minimumSize
|
2015-05-04 13:53:23 -05:00
|
|
|
static void tabPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-12 14:46:58 -05:00
|
|
|
{
|
2015-04-17 17:45:17 -05:00
|
|
|
struct tab *t = (struct tab *) c;
|
2015-04-12 14:46:58 -05:00
|
|
|
NSSize s;
|
|
|
|
|
2015-04-17 17:45:17 -05:00
|
|
|
s = [t->tabview minimumSize];
|
2015-04-12 14:46:58 -05:00
|
|
|
*width = (intmax_t) (s.width);
|
|
|
|
*height = (intmax_t) (s.height);
|
|
|
|
}
|
|
|
|
|
2015-05-04 13:53:23 -05:00
|
|
|
static void tabEnable(uiControl *c)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) c;
|
|
|
|
|
|
|
|
(*(t->baseEnable))(uiControl(t));
|
|
|
|
[t->pages enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
NSValue *v = (NSValue *) obj;
|
|
|
|
uiContainer *p;
|
|
|
|
|
|
|
|
p = (uiContainer *) [v pointerValue];
|
|
|
|
uiControlEnable(uiControl(p));
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tabDisable(uiControl *c)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) c;
|
|
|
|
|
|
|
|
(*(t->baseDisable))(uiControl(t));
|
|
|
|
[t->pages enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
NSValue *v = (NSValue *) obj;
|
|
|
|
uiContainer *p;
|
|
|
|
|
|
|
|
p = (uiContainer *) [v pointerValue];
|
|
|
|
uiControlDisable(uiControl(p));
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tabSysFunc(uiControl *c, uiControlSysFuncParams *p)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) c;
|
|
|
|
|
|
|
|
(*(t->baseSysFunc))(uiControl(t), p);
|
|
|
|
[t->pages enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
NSValue *v = (NSValue *) obj;
|
|
|
|
uiContainer *pp;
|
|
|
|
|
|
|
|
pp = (uiContainer *) [v pointerValue];
|
|
|
|
uiControlSysFunc(uiControl(pp), p);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-04-29 09:06:39 -05:00
|
|
|
static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
|
2015-04-12 14:46:58 -05:00
|
|
|
{
|
2015-04-17 17:45:17 -05:00
|
|
|
struct tab *t = (struct tab *) tt;
|
2015-04-29 09:06:39 -05:00
|
|
|
uiContainer *page;
|
2015-04-12 14:46:58 -05:00
|
|
|
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]];
|
2015-04-30 15:34:38 -05:00
|
|
|
[t->margined addObject:[NSNumber numberWithInt:0]];
|
2015-04-12 14:46:58 -05:00
|
|
|
|
|
|
|
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];
|
2015-04-12 14:46:58 -05:00
|
|
|
}
|
2015-04-16 13:50:05 -05:00
|
|
|
|
2015-05-06 13:46:50 -05:00
|
|
|
static void tabInsertPageBefore(uiTab *tt, const char *name, uintmax_t n, uiControl *child)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) tt;
|
|
|
|
uiContainer *page;
|
|
|
|
NSTabViewItem *i;
|
|
|
|
|
|
|
|
page = newBin();
|
|
|
|
binSetMainControl(page, child);
|
|
|
|
[t->pages insertObject:[NSValue valueWithPointer:page] atIndex:n];
|
|
|
|
[t->margined insertObject:[NSNumber numberWithInt:0] atIndex:n];
|
|
|
|
|
|
|
|
i = [[NSTabViewItem alloc] initWithIdentifier:nil];
|
|
|
|
[i setLabel:toNSString(name)];
|
|
|
|
[i setView:((NSView *) uiControlHandle(uiControl(page)))];
|
|
|
|
[t->tabview insertTabViewItem:i atIndex:n];
|
|
|
|
}
|
|
|
|
|
2015-04-19 10:41:23 -05:00
|
|
|
static void tabDeletePage(uiTab *tt, uintmax_t n)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) tt;
|
2015-04-19 10:53:31 -05:00
|
|
|
NSValue *v;
|
2015-04-29 09:06:39 -05:00
|
|
|
uiContainer *p;
|
2015-04-19 10:53:31 -05:00
|
|
|
NSTabViewItem *i;
|
2015-04-19 10:41:23 -05:00
|
|
|
|
2015-04-19 10:53:31 -05:00
|
|
|
v = (NSValue *) [t->pages objectAtIndex:n];
|
2015-04-29 09:06:39 -05:00
|
|
|
p = (uiContainer *) [v pointerValue];
|
2015-04-19 10:41:23 -05:00
|
|
|
[t->pages removeObjectAtIndex:n];
|
2015-04-30 15:34:38 -05:00
|
|
|
[t->margined removeObjectAtIndex:n];
|
2015-04-29 09:06:39 -05:00
|
|
|
|
2015-04-19 10:53:31 -05:00
|
|
|
// make sure the children of the tab aren't destroyed
|
2015-04-29 09:06:39 -05:00
|
|
|
binSetMainControl(p, NULL);
|
2015-04-19 10:53:31 -05:00
|
|
|
|
2015-04-30 18:56:12 -05:00
|
|
|
// remove the bin from the tab view
|
2015-05-06 00:38:32 -05:00
|
|
|
// this serves the purpose of binSetParent()
|
2015-04-19 10:53:31 -05:00
|
|
|
i = [t->tabview tabViewItemAtIndex:n];
|
|
|
|
[t->tabview removeTabViewItem:i];
|
2015-04-30 18:56:12 -05:00
|
|
|
|
|
|
|
// then destroy the bin
|
|
|
|
uiControlDestroy(uiControl(p));
|
2015-04-19 10:41:23 -05:00
|
|
|
}
|
|
|
|
|
2015-04-30 15:34:38 -05:00
|
|
|
static uintmax_t tabNumPages(uiTab *tt)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) tt;
|
|
|
|
|
|
|
|
return [t->pages count];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tabMargined(uiTab *tt, uintmax_t n)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) tt;
|
|
|
|
NSNumber *v;
|
|
|
|
|
|
|
|
v = (NSNumber *) [t->margined objectAtIndex:n];
|
|
|
|
return [v intValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are based on measurements from Interface Builder.
|
|
|
|
// These seem to be based on Auto Layout constants, but I don't see an API that exposes these...
|
|
|
|
#define tabLeftMargin 17
|
|
|
|
#define tabTopMargin 3
|
|
|
|
#define tabRightMargin 17
|
|
|
|
#define tabBottomMargin 17
|
|
|
|
|
|
|
|
// notes:
|
|
|
|
// top margin of a tab to its parent should be 12, not 20
|
|
|
|
// our system doesn't allow this...
|
|
|
|
|
|
|
|
static void tabSetMargined(uiTab *tt, uintmax_t n, int margined)
|
|
|
|
{
|
|
|
|
struct tab *t = (struct tab *) tt;
|
|
|
|
NSNumber *v;
|
|
|
|
NSValue *binv;
|
|
|
|
uiContainer *bin;
|
|
|
|
|
|
|
|
v = [NSNumber numberWithInt:margined];
|
|
|
|
[t->margined replaceObjectAtIndex:n withObject:v];
|
|
|
|
binv = (NSValue *) [t->pages objectAtIndex:n];
|
|
|
|
bin = (uiContainer *) [binv pointerValue];
|
|
|
|
if ([v intValue])
|
|
|
|
binSetMargins(bin, tabLeftMargin, tabTopMargin, tabRightMargin, tabBottomMargin);
|
|
|
|
else
|
|
|
|
binSetMargins(bin, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2015-04-16 13:50:05 -05:00
|
|
|
uiTab *uiNewTab(void)
|
|
|
|
{
|
2015-04-17 17:45:17 -05:00
|
|
|
struct tab *t;
|
2015-04-16 13:50:05 -05:00
|
|
|
|
2015-04-17 17:45:17 -05:00
|
|
|
t = uiNew(struct tab);
|
2015-04-16 14:08:49 -05:00
|
|
|
|
2015-05-02 19:51:00 -05:00
|
|
|
uiDarwinMakeControl(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));
|
2015-04-16 13:50:05 -05:00
|
|
|
|
|
|
|
// also good for NSTabView (same selector and everything)
|
2015-04-17 17:45:17 -05:00
|
|
|
setStandardControlFont((NSControl *) (t->tabview));
|
2015-04-16 13:50:05 -05:00
|
|
|
|
2015-04-19 10:41:23 -05:00
|
|
|
t->pages = [NSMutableArray new];
|
2015-04-30 15:34:38 -05:00
|
|
|
t->margined = [NSMutableArray new];
|
2015-04-19 10:41:23 -05:00
|
|
|
|
2015-05-04 13:53:23 -05:00
|
|
|
uiControl(t)->PreferredSize = tabPreferredSize;
|
|
|
|
t->baseEnable = uiControl(t)->Enable;
|
|
|
|
uiControl(t)->Enable = tabEnable;
|
|
|
|
t->baseDisable = uiControl(t)->Disable;
|
|
|
|
uiControl(t)->Disable = tabDisable;
|
|
|
|
t->baseSysFunc = uiControl(t)->SysFunc;
|
|
|
|
uiControl(t)->SysFunc = tabSysFunc;
|
2015-04-16 13:50:05 -05:00
|
|
|
|
2015-04-29 09:06:39 -05:00
|
|
|
uiTab(t)->AppendPage = tabAppendPage;
|
2015-05-06 13:46:50 -05:00
|
|
|
uiTab(t)->InsertPageBefore = tabInsertPageBefore;
|
2015-04-19 10:53:31 -05:00
|
|
|
uiTab(t)->DeletePage = tabDeletePage;
|
2015-04-30 15:34:38 -05:00
|
|
|
uiTab(t)->NumPages = tabNumPages;
|
|
|
|
uiTab(t)->Margined = tabMargined;
|
|
|
|
uiTab(t)->SetMargined = tabSetMargined;
|
2015-04-16 13:50:05 -05:00
|
|
|
|
2015-04-17 17:45:17 -05:00
|
|
|
return uiTab(t);
|
2015-04-16 13:50:05 -05:00
|
|
|
}
|