check-button.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #if defined(Hiro_CheckButton)
  2. @implementation CocoaCheckButton : NSButton
  3. -(id) initWith:(hiro::mCheckButton&)checkButtonReference {
  4. if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
  5. checkButton = &checkButtonReference;
  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. checkButton->state.checked = [self state] != NSOffState;
  15. checkButton->doToggle();
  16. }
  17. @end
  18. namespace hiro {
  19. auto pCheckButton::construct() -> void {
  20. @autoreleasepool {
  21. cocoaView = cocoaCheckButton = [[CocoaCheckButton alloc] initWith:self()];
  22. pWidget::construct();
  23. setBordered(state().bordered);
  24. setChecked(state().checked);
  25. setIcon(state().icon);
  26. setOrientation(state().orientation);
  27. setText(state().text);
  28. }
  29. }
  30. auto pCheckButton::destruct() -> void {
  31. @autoreleasepool {
  32. [cocoaView removeFromSuperview];
  33. [cocoaView release];
  34. }
  35. }
  36. auto pCheckButton::minimumSize() const -> Size {
  37. Size size = pFont::size(self().font(true), state().text);
  38. if(state().orientation == Orientation::Horizontal) {
  39. size.setWidth(size.width() + state().icon.width());
  40. size.setHeight(max(size.height(), state().icon.height()));
  41. }
  42. if(state().orientation == Orientation::Vertical) {
  43. size.setWidth(max(size.width(), state().icon.width()));
  44. size.setHeight(size.height() + state().icon.height());
  45. }
  46. return {size.width() + (state().text ? 20 : 8), size.height() + 8};
  47. }
  48. auto pCheckButton::setBordered(bool bordered) -> void {
  49. }
  50. auto pCheckButton::setChecked(bool checked) -> void {
  51. @autoreleasepool {
  52. [cocoaView setState:checked ? NSOnState : NSOffState];
  53. }
  54. }
  55. auto pCheckButton::setGeometry(Geometry geometry) -> void {
  56. pWidget::setGeometry({
  57. geometry.x() - 2, geometry.y() - 2,
  58. geometry.width() + 4, geometry.height() + 4
  59. });
  60. }
  61. auto pCheckButton::setIcon(const image& icon) -> void {
  62. @autoreleasepool {
  63. [cocoaView setImage:NSMakeImage(icon)];
  64. }
  65. }
  66. auto pCheckButton::setOrientation(Orientation orientation) -> void {
  67. @autoreleasepool {
  68. if(orientation == Orientation::Horizontal) [cocoaView setImagePosition:NSImageLeft];
  69. if(orientation == Orientation::Vertical ) [cocoaView setImagePosition:NSImageAbove];
  70. }
  71. }
  72. auto pCheckButton::setText(const string& text) -> void {
  73. @autoreleasepool {
  74. [cocoaView setTitle:[NSString stringWithUTF8String:text]];
  75. }
  76. }
  77. }
  78. #endif