radio-button.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if defined(Hiro_RadioButton)
  2. @implementation CocoaRadioButton : NSButton
  3. -(id) initWith:(hiro::mRadioButton&)radioButtonReference {
  4. if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
  5. radioButton = &radioButtonReference;
  6. [self setTarget:self];
  7. [self setAction:@selector(activate:)];
  8. [self setBezelStyle:NSRegularSquareBezelStyle];
  9. [self setButtonType:NSOnOffButton];
  10. }
  11. return self;
  12. }
  13. -(IBAction) activate:(id)sender {
  14. bool wasChecked = radioButton->state.checked;
  15. radioButton->setChecked();
  16. if(!wasChecked) radioButton->doActivate();
  17. }
  18. @end
  19. namespace hiro {
  20. auto pRadioButton::construct() -> void {
  21. @autoreleasepool {
  22. cocoaView = cocoaRadioButton = [[CocoaRadioButton alloc] initWith:self()];
  23. pWidget::construct();
  24. setBordered(state().bordered);
  25. if(state().checked) setChecked();
  26. setIcon(state().icon);
  27. setOrientation(state().orientation);
  28. setText(state().text);
  29. }
  30. }
  31. auto pRadioButton::destruct() -> void {
  32. @autoreleasepool {
  33. [cocoaView removeFromSuperview];
  34. [cocoaView release];
  35. }
  36. }
  37. auto pRadioButton::minimumSize() const -> Size {
  38. Size size = pFont::size(self().font(true), state().text);
  39. if(state().orientation == Orientation::Horizontal) {
  40. size.setWidth(size.width() + state().icon.width());
  41. size.setHeight(max(size.height(), state().icon.height()));
  42. }
  43. if(state().orientation == Orientation::Vertical) {
  44. size.setWidth(max(size.width(), state().icon.width()));
  45. size.setHeight(size.height() + state().icon.height());
  46. }
  47. return {size.width() + (state().text ? 20 : 8), size.height() + 8};
  48. }
  49. auto pRadioButton::setBordered(bool bordered) -> void {
  50. }
  51. auto pRadioButton::setChecked() -> void {
  52. @autoreleasepool {
  53. if(auto group = state().group) {
  54. for(auto& weak : group->state.objects) {
  55. if(auto object = weak.acquire()) {
  56. if(auto self = object->self()) {
  57. if(auto p = dynamic_cast<pRadioButton*>(self)) {
  58. auto state = this == p ? NSOnState : NSOffState;
  59. [p->cocoaView setState:state];
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. auto pRadioButton::setGeometry(Geometry geometry) -> void {
  68. pWidget::setGeometry({
  69. geometry.x() - 2, geometry.y() - 2,
  70. geometry.width() + 4, geometry.height() + 4
  71. });
  72. }
  73. auto pRadioButton::setGroup(sGroup group) -> void {
  74. }
  75. auto pRadioButton::setIcon(const image& icon) -> void {
  76. @autoreleasepool {
  77. [cocoaView setImage:NSMakeImage(icon)];
  78. }
  79. }
  80. auto pRadioButton::setOrientation(Orientation orientation) -> void {
  81. @autoreleasepool {
  82. if(orientation == Orientation::Horizontal) [cocoaView setImagePosition:NSImageLeft];
  83. if(orientation == Orientation::Vertical ) [cocoaView setImagePosition:NSImageAbove];
  84. }
  85. }
  86. auto pRadioButton::setText(const string& text) -> void {
  87. @autoreleasepool {
  88. [cocoaView setTitle:[NSString stringWithUTF8String:text]];
  89. }
  90. }
  91. }
  92. #endif