menu.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if defined(Hiro_Menu)
  2. @implementation CocoaMenu : NSMenuItem
  3. -(id) initWith:(hiro::mMenu&)menuReference {
  4. if(self = [super initWithTitle:@"" action:nil keyEquivalent:@""]) {
  5. menu = &menuReference;
  6. cocoaMenu = [[NSMenu alloc] initWithTitle:@""];
  7. [self setSubmenu:cocoaMenu];
  8. }
  9. return self;
  10. }
  11. -(NSMenu*) cocoaMenu {
  12. return cocoaMenu;
  13. }
  14. @end
  15. namespace hiro {
  16. auto pMenu::construct() -> void {
  17. @autoreleasepool {
  18. cocoaAction = cocoaMenu = [[CocoaMenu alloc] initWith:self()];
  19. pAction::construct();
  20. setIcon(state().icon);
  21. setText(state().text);
  22. }
  23. }
  24. auto pMenu::destruct() -> void {
  25. @autoreleasepool {
  26. [[cocoaAction cocoaMenu] release];
  27. [cocoaAction release];
  28. }
  29. }
  30. auto pMenu::append(sAction action) -> void {
  31. @autoreleasepool {
  32. if(auto pAction = action->self()) {
  33. [[cocoaAction cocoaMenu] addItem:pAction->cocoaAction];
  34. }
  35. }
  36. }
  37. auto pMenu::remove(sAction action) -> void {
  38. @autoreleasepool {
  39. if(auto pAction = action->self()) {
  40. [[cocoaAction cocoaMenu] removeItem:pAction->cocoaAction];
  41. }
  42. }
  43. }
  44. auto pMenu::setIcon(const image& icon) -> void {
  45. @autoreleasepool {
  46. uint size = 15; //there is no API to retrieve the optimal size
  47. [cocoaAction setImage:NSMakeImage(icon, size, size)];
  48. }
  49. }
  50. auto pMenu::setText(const string& text) -> void {
  51. @autoreleasepool {
  52. [[cocoaAction cocoaMenu] setTitle:[NSString stringWithUTF8String:text]];
  53. [cocoaAction setTitle:[NSString stringWithUTF8String:text]];
  54. }
  55. }
  56. }
  57. #endif