button.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if defined(Hiro_Button)
  2. @implementation CocoaButton : NSButton
  3. -(id) initWith:(hiro::mButton&)buttonReference {
  4. if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
  5. button = &buttonReference;
  6. [self setTarget:self];
  7. [self setAction:@selector(activate:)];
  8. //NSRoundedBezelStyle has a fixed height; which breaks both icons and larger/smaller text
  9. [self setBezelStyle:NSRegularSquareBezelStyle];
  10. }
  11. return self;
  12. }
  13. -(IBAction) activate:(id)sender {
  14. button->doActivate();
  15. }
  16. @end
  17. namespace hiro {
  18. auto pButton::construct() -> void {
  19. @autoreleasepool {
  20. cocoaView = cocoaButton = [[CocoaButton alloc] initWith:self()];
  21. pWidget::construct();
  22. setBordered(state().bordered);
  23. setIcon(state().icon);
  24. setOrientation(state().orientation);
  25. setText(state().text);
  26. }
  27. }
  28. auto pButton::destruct() -> void {
  29. @autoreleasepool {
  30. [cocoaView removeFromSuperview];
  31. [cocoaView release];
  32. }
  33. }
  34. auto pButton::minimumSize() const -> Size {
  35. Size size = pFont::size(self().font(true), state().text);
  36. if(state().orientation == Orientation::Horizontal) {
  37. size.setWidth(size.width() + state().icon.width());
  38. size.setHeight(max(size.height(), state().icon.height()));
  39. }
  40. if(state().orientation == Orientation::Vertical) {
  41. size.setWidth(max(size.width(), state().icon.width()));
  42. size.setHeight(size.height() + state().icon.height());
  43. }
  44. return {size.width() + (state().text ? 20 : 8), size.height() + 8};
  45. }
  46. auto pButton::setBordered(bool bordered) -> void {
  47. }
  48. auto pButton::setGeometry(Geometry geometry) -> void {
  49. pWidget::setGeometry({
  50. geometry.x() - 2, geometry.y() - 2,
  51. geometry.width() + 4, geometry.height() + 4
  52. });
  53. }
  54. auto pButton::setIcon(const image& icon) -> void {
  55. @autoreleasepool {
  56. [cocoaView setImage:NSMakeImage(icon)];
  57. }
  58. }
  59. auto pButton::setOrientation(Orientation orientation) -> void {
  60. @autoreleasepool {
  61. if(orientation == Orientation::Horizontal) [cocoaView setImagePosition:NSImageLeft];
  62. if(orientation == Orientation::Vertical ) [cocoaView setImagePosition:NSImageAbove];
  63. }
  64. }
  65. auto pButton::setText(const string& text) -> void {
  66. @autoreleasepool {
  67. [cocoaView setTitle:[NSString stringWithUTF8String:text]];
  68. }
  69. }
  70. }
  71. #endif