2015-08-15 16:05:11 -05:00
|
|
|
// 15 august 2015
|
|
|
|
#import "uipriv_darwin.h"
|
|
|
|
|
|
|
|
struct uiTab {
|
|
|
|
uiDarwinControl c;
|
|
|
|
NSTabView *tabview;
|
2015-08-15 18:35:36 -05:00
|
|
|
// TODO either rename all uses of child to page or rename this to children
|
|
|
|
NSMutableArray *pages; // []NSValue<uiControl *>
|
|
|
|
// the views that contain the children's views
|
|
|
|
// these are the views that are assigned to each NSTabViewItem
|
|
|
|
NSMutableArray *views; // []NSView
|
|
|
|
NSMutableArray *margined; // []NSNumber
|
2015-08-15 16:05:11 -05:00
|
|
|
};
|
|
|
|
|
2015-08-19 16:15:45 -05:00
|
|
|
// NSTabView handles tab switching interestingly: it /removes the old tab from the view hierarchy outright/
|
|
|
|
// for some reason that I don't know (TODO), this casues problems for our auto layout, even if we do a depth first recomputation of all of our fitting sizes
|
|
|
|
// therefore, we have to only update the current tab page
|
|
|
|
// TODO this doesn't really work...
|
|
|
|
@interface tabDelegateClass : NSObject<NSTabViewDelegate> {
|
|
|
|
NSMapTable *tabs;
|
|
|
|
}
|
|
|
|
- (void)tabView:(NSTabView *)tv didSelectTabViewItem:(NSTabViewItem *)item;
|
|
|
|
- (void)registerTab:(uiTab *)b;
|
|
|
|
- (void)unregisterTab:(uiTab *)b;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation tabDelegateClass
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
self->tabs = newMap();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
if ([self->tabs count] != 0)
|
|
|
|
complain("attempt to destroy shared tab delegate but tabs are still registered to it");
|
|
|
|
[self->tabs release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tabView:(NSTabView *)tv didSelectTabViewItem:(NSTabViewItem *)item
|
|
|
|
{
|
|
|
|
uiTab *t;
|
|
|
|
|
|
|
|
t = (uiTab *) mapGet(self->tabs, tv);
|
|
|
|
uiDarwinControlTriggerRelayout(uiDarwinControl(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerTab:(uiTab *)t
|
|
|
|
{
|
|
|
|
mapSet(self->tabs, t->tabview, t);
|
|
|
|
[t->tabview setDelegate:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)unregisterTab:(uiTab *)t
|
|
|
|
{
|
|
|
|
[t->tabview setDelegate:nil];
|
|
|
|
[self->tabs removeObjectForKey:t->tabview];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static tabDelegateClass *tabDelegate = nil;
|
|
|
|
|
2015-08-15 16:05:11 -05:00
|
|
|
static void onDestroy(uiTab *);
|
|
|
|
|
|
|
|
uiDarwinDefineControlWithOnDestroy(
|
|
|
|
uiTab, // type name
|
|
|
|
uiTabType, // type function
|
|
|
|
tabview, // handle
|
|
|
|
onDestroy(this); // on destroy
|
|
|
|
)
|
|
|
|
|
|
|
|
static void onDestroy(uiTab *t)
|
|
|
|
{
|
2015-08-15 18:35:36 -05:00
|
|
|
// first remove all tab pages so we can destroy all the children
|
2015-08-15 16:05:11 -05:00
|
|
|
while ([t->tabview numberOfTabViewItems] != 0)
|
|
|
|
[t->tabview removeTabViewItem:[t->tabview tabViewItemAtIndex:0]];
|
2015-08-15 18:35:36 -05:00
|
|
|
// then destroy all the children
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->pages enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
NSValue *v = (NSValue *) obj;
|
2015-08-15 18:35:36 -05:00
|
|
|
uiControl *c;
|
2015-08-15 16:05:11 -05:00
|
|
|
|
2015-08-15 18:35:36 -05:00
|
|
|
c = (uiControl *) [v pointerValue];
|
2015-08-17 16:27:27 -05:00
|
|
|
uiControlSetParent(c, NULL);
|
2015-08-15 18:35:36 -05:00
|
|
|
uiControlDestroy(c);
|
2015-08-15 16:05:11 -05:00
|
|
|
}];
|
|
|
|
// and finally destroy ourselves
|
|
|
|
[t->pages release];
|
2015-08-15 18:35:36 -05:00
|
|
|
[t->views release];
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->margined release];
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO container update
|
|
|
|
|
2015-08-18 20:14:58 -05:00
|
|
|
static void tabRelayout(uiDarwinControl *c)
|
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
NSUInteger i;
|
2015-08-19 16:15:45 -05:00
|
|
|
NSValue *v;
|
|
|
|
uiControl *child;
|
|
|
|
uiDarwinControl *cc;
|
|
|
|
NSView *view, *childView;
|
|
|
|
NSNumber *margined;
|
2015-08-18 20:14:58 -05:00
|
|
|
|
2015-08-19 16:15:45 -05:00
|
|
|
if ([t->pages count] == 0)
|
|
|
|
return;
|
2015-08-18 20:14:58 -05:00
|
|
|
for (i = 0; i < [t->pages count]; i++) {
|
|
|
|
v = (NSValue *) [t->pages objectAtIndex:i];
|
|
|
|
child = (uiControl *) [v pointerValue];
|
|
|
|
view = (NSView *) [t->views objectAtIndex:i];
|
|
|
|
childView = (NSView *) uiControlHandle(child);
|
|
|
|
margined = (NSNumber *) [t->margined objectAtIndex:i];
|
|
|
|
// first lay out the child
|
|
|
|
cc = uiDarwinControl(child);
|
|
|
|
(*(cc->Relayout))(cc);
|
|
|
|
// then lay out the page
|
|
|
|
layoutSingleView(view, childView, [margined intValue]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 16:05:11 -05:00
|
|
|
void uiTabAppend(uiTab *t, const char *name, uiControl *child)
|
|
|
|
{
|
2015-08-15 18:35:36 -05:00
|
|
|
uiTabInsertAt(t, name, [t->pages count], child);
|
2015-08-15 16:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabInsertAt(uiTab *t, const char *name, uintmax_t n, uiControl *child)
|
|
|
|
{
|
2015-08-15 18:35:36 -05:00
|
|
|
NSView *childView;
|
|
|
|
NSView *view;
|
2015-08-15 16:05:11 -05:00
|
|
|
NSTabViewItem *i;
|
|
|
|
|
2015-08-15 18:35:36 -05:00
|
|
|
uiControlSetParent(child, uiControl(t));
|
|
|
|
|
|
|
|
childView = (NSView *) uiControlHandle(child);
|
|
|
|
view = [[NSView alloc] initWithFrame:NSZeroRect];
|
|
|
|
[view addSubview:childView];
|
|
|
|
|
2015-08-16 22:44:23 -05:00
|
|
|
[t->pages insertObject:[NSValue valueWithPointer:child] atIndex:n];
|
2015-08-15 18:35:36 -05:00
|
|
|
[t->views insertObject:view atIndex:n];
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->margined insertObject:[NSNumber numberWithInt:0] atIndex:n];
|
|
|
|
|
|
|
|
i = [[NSTabViewItem alloc] initWithIdentifier:nil];
|
|
|
|
[i setLabel:toNSString(name)];
|
2015-08-15 18:35:36 -05:00
|
|
|
[i setView:view];
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->tabview insertTabViewItem:i atIndex:n];
|
2015-08-18 20:14:58 -05:00
|
|
|
|
|
|
|
uiDarwinControlTriggerRelayout(uiDarwinControl(t));
|
2015-08-15 16:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabDelete(uiTab *t, uintmax_t n)
|
|
|
|
{
|
|
|
|
NSValue *v;
|
2015-08-15 18:35:36 -05:00
|
|
|
uiControl *child;
|
|
|
|
NSView *childView;
|
2015-08-15 16:05:11 -05:00
|
|
|
NSTabViewItem *i;
|
|
|
|
|
|
|
|
v = (NSValue *) [t->pages objectAtIndex:n];
|
2015-08-15 18:35:36 -05:00
|
|
|
child = (uiControl *) [v pointerValue];
|
|
|
|
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->pages removeObjectAtIndex:n];
|
2015-08-15 18:35:36 -05:00
|
|
|
[t->views removeObjectAtIndex:n];
|
2015-08-15 16:05:11 -05:00
|
|
|
[t->margined removeObjectAtIndex:n];
|
|
|
|
|
2015-08-15 18:35:36 -05:00
|
|
|
childView = (NSView *) uiControlHandle(child);
|
|
|
|
[childView removeFromSuperview];
|
2015-08-16 22:44:23 -05:00
|
|
|
uiControlSetParent(child, NULL);
|
2015-08-15 16:05:11 -05:00
|
|
|
|
|
|
|
i = [t->tabview tabViewItemAtIndex:n];
|
|
|
|
[t->tabview removeTabViewItem:i];
|
|
|
|
}
|
|
|
|
|
2015-08-16 22:44:23 -05:00
|
|
|
uintmax_t uiTabNumPages(uiTab *t)
|
2015-08-15 16:05:11 -05:00
|
|
|
{
|
|
|
|
return [t->pages count];
|
|
|
|
}
|
|
|
|
|
|
|
|
int uiTabMargined(uiTab *t, uintmax_t n)
|
|
|
|
{
|
|
|
|
NSNumber *v;
|
|
|
|
|
|
|
|
v = (NSNumber *) [t->margined objectAtIndex:n];
|
|
|
|
return [v intValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabSetMargined(uiTab *t, uintmax_t n, int margined)
|
|
|
|
{
|
|
|
|
NSNumber *v;
|
|
|
|
|
|
|
|
v = [NSNumber numberWithInt:margined];
|
|
|
|
[t->margined replaceObjectAtIndex:n withObject:v];
|
2015-08-18 20:14:58 -05:00
|
|
|
uiDarwinControlTriggerRelayout(uiDarwinControl(t));
|
2015-08-15 16:05:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiTab *uiNewTab(void)
|
|
|
|
{
|
|
|
|
uiTab *t;
|
|
|
|
|
|
|
|
t = (uiTab *) uiNewControl(uiTabType());
|
|
|
|
|
|
|
|
t->tabview = [[NSTabView alloc] initWithFrame:NSZeroRect];
|
|
|
|
// also good for NSTabView (same selector and everything)
|
|
|
|
uiDarwinSetControlFont((NSControl *) (t->tabview), NSRegularControlSize);
|
|
|
|
|
|
|
|
t->pages = [NSMutableArray new];
|
2015-08-15 18:35:36 -05:00
|
|
|
t->views = [NSMutableArray new];
|
2015-08-15 16:05:11 -05:00
|
|
|
t->margined = [NSMutableArray new];
|
|
|
|
|
2015-08-19 16:15:45 -05:00
|
|
|
if (tabDelegate == nil) {
|
|
|
|
tabDelegate = [tabDelegateClass new];
|
|
|
|
[delegates addObject:tabDelegate];
|
|
|
|
}
|
|
|
|
[tabDelegate registerTab:t];
|
|
|
|
|
2015-08-15 16:05:11 -05:00
|
|
|
uiDarwinFinishNewControl(t, uiTab);
|
2015-08-18 20:14:58 -05:00
|
|
|
uiDarwinControl(t)->Relayout = tabRelayout;
|
2015-08-15 16:05:11 -05:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|