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-22 20:39:12 -05:00
|
|
|
|
2015-08-14 22:50:38 -05:00
|
|
|
struct uiRadioButtons {
|
|
|
|
uiDarwinControl c;
|
2015-08-22 14:12:48 -05:00
|
|
|
NSMatrix *matrix;
|
2015-08-14 22:50:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
uiDarwinDefineControl(
|
|
|
|
uiRadioButtons, // type name
|
|
|
|
uiRadioButtonsType, // type function
|
2015-08-22 14:12:48 -05:00
|
|
|
matrix // handle
|
2015-08-14 22:50:38 -05:00
|
|
|
)
|
|
|
|
|
2015-08-22 14:12:48 -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)
|
|
|
|
{
|
2015-08-22 16:21:27 -05:00
|
|
|
intmax_t prevSelection;
|
|
|
|
|
|
|
|
// renewRows:columns: will reset the selection
|
|
|
|
prevSelection = [r->matrix selectedRow];
|
2015-08-22 20:39:12 -05:00
|
|
|
|
2015-08-22 16:21:27 -05:00
|
|
|
[r->matrix renewRows:([r->matrix numberOfRows] + 1) columns:1];
|
2015-08-22 14:12:48 -05:00
|
|
|
[cellAt(r, [r->matrix numberOfRows] - 1) setTitle:toNSString(text)];
|
2015-08-22 20:39:12 -05:00
|
|
|
|
2015-08-22 14:12:48 -05:00
|
|
|
// 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
|
2015-08-22 20:39:12 -05:00
|
|
|
|
2015-08-22 16:21:27 -05:00
|
|
|
// 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];
|
2015-08-22 20:39:12 -05:00
|
|
|
|
2015-08-18 19:15:09 -05:00
|
|
|
uiDarwinControlTriggerRelayout(uiDarwinControl(r));
|
2015-08-14 22:50:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiRadioButtons *uiNewRadioButtons(void)
|
|
|
|
{
|
|
|
|
uiRadioButtons *r;
|
2015-08-22 15:09:21 -05:00
|
|
|
NSButtonCell *cell;
|
2015-08-14 22:50:38 -05:00
|
|
|
|
|
|
|
r = (uiRadioButtons *) uiNewControl(uiRadioButtonsType());
|
|
|
|
|
2015-08-22 15:09:21 -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];
|
2015-08-22 16:21:27 -05:00
|
|
|
// we manually twiddle this property to allow programmatic non-selection state
|
2015-08-22 14:12:48 -05:00
|
|
|
[r->matrix setAllowsEmptySelection:NO];
|
2015-08-22 15:15:11 -05:00
|
|
|
[r->matrix setSelectionByRect:YES];
|
2015-08-22 14:12:48 -05:00
|
|
|
[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
|
|
|
|
|
|
|
uiDarwinFinishNewControl(r, uiRadioButtons);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|