libui/darwin/group.m

122 lines
3.0 KiB
Mathematica
Raw Normal View History

2015-08-14 21:50:20 -05:00
// 14 august 2015
#import "uipriv_darwin.h"
struct uiGroup {
uiDarwinControl c;
NSBox *box;
uiControl *child;
int margined;
};
static void onDestroy(uiGroup *);
2015-08-14 21:50:20 -05:00
uiDarwinDefineControlWithOnDestroy(
uiGroup, // type name
uiGroupType, // type function
box, // handle
onDestroy(this); // on destroy
2015-08-14 21:50:20 -05:00
)
static void onDestroy(uiGroup *g)
{
if (g->child != NULL) {
uiControlSetParent(g->child, NULL);
uiControlDestroy(g->child);
}
}
static void groupContainerUpdateState(uiControl *c)
{
uiGroup *g = uiGroup(c);
if (g->child != NULL)
controlUpdateState(g->child);
}
2015-08-14 21:50:20 -05:00
static void groupRelayout(uiDarwinControl *c)
{
uiGroup *g = uiGroup(c);
uiDarwinControl *cc;
NSView *childView;
if (g->child == NULL)
return;
[g->box removeConstraints:[g->box constraints]];
cc = uiDarwinControl(g->child);
childView = (NSView *) uiControlHandle(g->child);
// first relayout the child
(*(cc->Relayout))(cc);
// now relayout ourselves
// see below on using the content view
layoutSingleView(g->box, childView, g->margined);
// we need to explicitly tell the NSBox to recompute its own size based on the new content layout
[g->box sizeToFit];
}
2015-08-14 21:50:20 -05:00
char *uiGroupTitle(uiGroup *g)
{
2015-08-22 10:17:13 -05:00
return uiDarwinNSStringToText([g->box title]);
2015-08-14 21:50:20 -05:00
}
2015-08-17 17:30:04 -05:00
void uiGroupSetTitle(uiGroup *g, const char *title)
2015-08-14 21:50:20 -05:00
{
2015-08-17 17:30:04 -05:00
[g->box setTitle:toNSString(title)];
2015-08-14 21:50:20 -05:00
// changing the text might necessitate a change in the groupbox's size
uiDarwinControlTriggerRelayout(uiDarwinControl(g));
2015-08-14 21:50:20 -05:00
}
void uiGroupSetChild(uiGroup *g, uiControl *child)
{
NSView *childView;
if (g->child != NULL) {
childView = (NSView *) uiControlHandle(g->child);
[childView removeFromSuperview];
2015-08-14 21:50:20 -05:00
uiControlSetParent(g->child, NULL);
}
2015-08-14 21:50:20 -05:00
g->child = child;
if (g->child != NULL) {
childView = (NSView *) uiControlHandle(g->child);
2015-08-14 21:50:20 -05:00
uiControlSetParent(g->child, uiControl(g));
// we have to add controls to the box itself NOT the content view
// otherwise, things get really glitchy
// we also need to call -sizeToFit, but we'll do that in the relayout that's triggered below (see above)
[g->box addSubview:childView];
uiDarwinControlTriggerRelayout(uiDarwinControl(g));
2015-08-14 21:50:20 -05:00
}
}
int uiGroupMargined(uiGroup *g)
{
return g->margined;
}
void uiGroupSetMargined(uiGroup *g, int margined)
{
g->margined = margined;
if (g->child != NULL)
uiDarwinControlTriggerRelayout(uiDarwinControl(g));
2015-08-14 21:50:20 -05:00
}
uiGroup *uiNewGroup(const char *title)
{
uiGroup *g;
g = (uiGroup *) uiNewControl(uiGroupType());
g->box = [[NSBox alloc] initWithFrame:NSZeroRect];
2015-08-17 17:30:04 -05:00
[g->box setTitle:toNSString(title)];
2015-08-14 21:50:20 -05:00
[g->box setBoxType:NSBoxPrimary];
2015-08-17 17:30:04 -05:00
[g->box setBorderType:NSLineBorder];
2015-08-14 21:50:20 -05:00
[g->box setTransparent:NO];
[g->box setTitlePosition:NSAtTop];
2015-08-17 17:30:04 -05:00
// we can't use uiDarwinSetControlFont() because the selector is different
[g->box setTitleFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
2015-08-14 21:50:20 -05:00
uiDarwinFinishNewControl(g, uiGroup);
uiControl(g)->ContainerUpdateState = groupContainerUpdateState;
uiDarwinControl(g)->Relayout = groupRelayout;
2015-08-14 21:50:20 -05:00
return g;
}