| 1 |
@implementation CocoaComboButton : NSPopUpButton |
| 2 |
|
| 3 |
-(id) initWith:(phoenix::ComboButton&)comboButtonReference { |
| 4 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0) pullsDown:NO]) { |
| 5 |
comboButton = &comboButtonReference; |
| 6 |
|
| 7 |
[self setTarget:self]; |
| 8 |
[self setAction:@selector(activate:)]; |
| 9 |
} |
| 10 |
return self; |
| 11 |
} |
| 12 |
|
| 13 |
-(IBAction) activate:(id)sender { |
| 14 |
comboButton->state.selection = [self indexOfSelectedItem]; |
| 15 |
if(comboButton->onChange) comboButton->onChange(); |
| 16 |
} |
| 17 |
|
| 18 |
@end |
| 19 |
|
| 20 |
namespace phoenix { |
| 21 |
|
| 22 |
void pComboButton::append(string text) { |
| 23 |
@autoreleasepool { |
| 24 |
[cocoaView addItemWithTitle:[NSString stringWithUTF8String:text]]; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
Size pComboButton::minimumSize() { |
| 29 |
unsigned maximumWidth = 0; |
| 30 |
for(auto& text : comboButton.state.text) maximumWidth = max(maximumWidth, Font::size(comboButton.font(), text).width); |
| 31 |
Size size = Font::size(comboButton.font(), " "); |
| 32 |
return {maximumWidth + 36, size.height + 6}; |
| 33 |
} |
| 34 |
|
| 35 |
void pComboButton::remove(unsigned selection) { |
| 36 |
@autoreleasepool { |
| 37 |
[cocoaView removeItemAtIndex:selection]; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void pComboButton::reset() { |
| 42 |
@autoreleasepool { |
| 43 |
[cocoaView removeAllItems]; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
void pComboButton::setGeometry(Geometry geometry) { |
| 48 |
pWidget::setGeometry({ |
| 49 |
geometry.x - 2, geometry.y, |
| 50 |
geometry.width + 4, geometry.height |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
void pComboButton::setSelection(unsigned selection) { |
| 55 |
@autoreleasepool { |
| 56 |
[cocoaView selectItemAtIndex:selection]; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void pComboButton::setText(unsigned selection, string text) { |
| 61 |
@autoreleasepool { |
| 62 |
[[cocoaView itemAtIndex:selection] setTitle:[NSString stringWithUTF8String:text]]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void pComboButton::constructor() { |
| 67 |
@autoreleasepool { |
| 68 |
cocoaView = cocoaComboButton = [[CocoaComboButton alloc] initWith:comboButton]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void pComboButton::destructor() { |
| 73 |
@autoreleasepool { |
| 74 |
[cocoaView release]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
} |