radio-label.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #if defined(Hiro_RadioLabel)
  2. @implementation CocoaRadioLabel : NSButton
  3. -(id) initWith:(hiro::mRadioLabel&)radioLabelReference {
  4. if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
  5. radioLabel = &radioLabelReference;
  6. [self setTarget:self];
  7. [self setAction:@selector(activate:)];
  8. [self setButtonType:NSRadioButton];
  9. }
  10. return self;
  11. }
  12. -(IBAction) activate:(id)sender {
  13. radioLabel->setChecked();
  14. radioLabel->doActivate();
  15. }
  16. @end
  17. namespace hiro {
  18. auto pRadioLabel::construct() -> void {
  19. @autoreleasepool {
  20. cocoaView = cocoaRadioLabel = [[CocoaRadioLabel alloc] initWith:self()];
  21. pWidget::construct();
  22. setGroup(state().group);
  23. setText(state().text);
  24. }
  25. }
  26. auto pRadioLabel::destruct() -> void {
  27. @autoreleasepool {
  28. [cocoaView removeFromSuperview];
  29. [cocoaView release];
  30. }
  31. }
  32. auto pRadioLabel::minimumSize() const -> Size {
  33. Size size = pFont::size(self().font(true), state().text);
  34. return {size.width() + 22, size.height() + 2};
  35. }
  36. auto pRadioLabel::setChecked() -> void {
  37. setGroup(state().group);
  38. }
  39. auto pRadioLabel::setGeometry(Geometry geometry) -> void {
  40. pWidget::setGeometry({
  41. geometry.x() - 1, geometry.y(),
  42. geometry.width() + 2, geometry.height()
  43. });
  44. //buttonType:NSRadioButton does not set initial icon via programmatically calling setState:NSOnState.
  45. //I can only get the icon to show as checked initially by setting the state on geometry resizes.
  46. //adjusting the initWithFrame:NSMakeRect did not help.
  47. if(state().checked) setChecked();
  48. }
  49. auto pRadioLabel::setGroup(sGroup group) -> void {
  50. @autoreleasepool {
  51. if(!group) return;
  52. for(auto& weak : group->state.objects) {
  53. if(auto object = weak.acquire()) {
  54. if(auto self = object->self()) {
  55. if(auto p = dynamic_cast<pRadioLabel*>(self)) {
  56. auto state = p->state().checked ? NSOnState : NSOffState;
  57. [p->cocoaView setState:state];
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. auto pRadioLabel::setText(const string& text) -> void {
  65. @autoreleasepool {
  66. [cocoaView setTitle:[NSString stringWithUTF8String:text]];
  67. }
  68. }
  69. }
  70. #endif