libui/darwin/radiobuttons.m

71 lines
2.2 KiB
Mathematica
Raw Normal View History

2015-08-14 22:50:38 -05:00
// 14 august 2015
#import "uipriv_darwin.h"
2015-08-25 16:34:18 -05:00
// TODO the selection should NOT be lost when starting a new drag
2015-08-14 22:50:38 -05:00
struct uiRadioButtons {
uiDarwinControl c;
NSMatrix *matrix;
2015-08-14 22:50:38 -05:00
};
uiDarwinControlAllDefaults(uiRadioButtons, matrix)
2015-08-14 22:50:38 -05:00
static NSButtonCell *cellAt(uiRadioButtons *r, uintmax_t n)
{
return (NSButtonCell *) [r->matrix cellAtRow:n column:0];
}
2015-08-14 22:50:38 -05:00
void uiRadioButtonsAppend(uiRadioButtons *r, const char *text)
{
intmax_t prevSelection;
// renewRows:columns: will reset the selection
prevSelection = [r->matrix selectedRow];
[r->matrix renewRows:([r->matrix numberOfRows] + 1) columns:1];
[cellAt(r, [r->matrix numberOfRows] - 1) setTitle:toNSString(text)];
// this will definitely cause a resize in at least the vertical direction, even if not in the horizontal
2015-08-23 16:53:29 -05:00
// DO NOT CALL sizeToCells! this will glitch out; see http://stackoverflow.com/questions/32162562/dynamically-adding-cells-to-a-nsmatrix-laid-out-with-auto-layout-has-weird-effec
// and renew the previous selection
// we need to turn on allowing empty selection for this to work properly on the initial state
// TODO this doesn't actually work
[r->matrix setAllowsEmptySelection:YES];
[r->matrix selectCellAtRow:prevSelection column:0];
[r->matrix setAllowsEmptySelection:NO];
uiDarwinControlTriggerRelayout(uiDarwinControl(r));
2015-08-14 22:50:38 -05:00
}
uiRadioButtons *uiNewRadioButtons(void)
{
uiRadioButtons *r;
NSButtonCell *cell;
2015-08-14 22:50:38 -05:00
uiDarwinNewControl(uiRadioButtons, r);
2015-08-14 22:50:38 -05:00
// we have to set up the NSMatrix this way (prototype first)
// otherwise we won't be able to change its properties (such as the button type)
cell = [NSButtonCell new];
[cell setButtonType:NSRadioButton];
// works on NSCells too (same selector)
uiDarwinSetControlFont((NSControl *) cell, NSRegularControlSize);
r->matrix = [[NSMatrix alloc] initWithFrame:NSZeroRect
mode:NSRadioModeMatrix
prototype:cell
numberOfRows:0
numberOfColumns:0];
// we manually twiddle this property to allow programmatic non-selection state
[r->matrix setAllowsEmptySelection:NO];
2015-08-22 15:15:11 -05:00
[r->matrix setSelectionByRect:YES];
[r->matrix setIntercellSpacing:NSMakeSize(4, 2)];
[r->matrix setAutorecalculatesCellSize:YES];
[r->matrix setDrawsBackground:NO];
[r->matrix setDrawsCellBackground:NO];
[r->matrix setAutosizesCells:YES];
2015-08-14 22:50:38 -05:00
return r;
}