menu-bar.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #if defined(Hiro_MenuBar)
  2. namespace hiro {
  3. auto pMenuBar::construct() -> void {
  4. _update();
  5. }
  6. auto pMenuBar::destruct() -> void {
  7. if(hmenu) { DestroyMenu(hmenu); hmenu = nullptr; }
  8. if(auto parent = _parent()) {
  9. SetMenu(parent->hwnd, nullptr);
  10. }
  11. }
  12. auto pMenuBar::append(sMenu) -> void {
  13. _update();
  14. }
  15. auto pMenuBar::remove(sMenu) -> void {
  16. _update();
  17. }
  18. auto pMenuBar::setEnabled(bool enabled) -> void {
  19. _update();
  20. }
  21. auto pMenuBar::setFont(const Font& font) -> void {
  22. //unsupported
  23. }
  24. auto pMenuBar::setVisible(bool visible) -> void {
  25. if(auto parent = _parent()) {
  26. SetMenu(parent->hwnd, visible ? hmenu : nullptr);
  27. parent->setGeometry(parent->state().geometry);
  28. }
  29. }
  30. auto pMenuBar::_parent() -> maybe<pWindow&> {
  31. if(auto parent = self().parentWindow(true)) {
  32. if(auto self = parent->self()) return *self;
  33. }
  34. return nothing;
  35. }
  36. auto pMenuBar::_update() -> void {
  37. if(hmenu) DestroyMenu(hmenu);
  38. hmenu = CreateMenu();
  39. MENUINFO mi{sizeof(MENUINFO)};
  40. mi.fMask = MIM_STYLE;
  41. mi.dwStyle = MNS_NOTIFYBYPOS; //| MNS_MODELESS;
  42. SetMenuInfo(hmenu, &mi);
  43. unsigned position = 0;
  44. #if defined(Hiro_Menu)
  45. for(auto& menu : state().menus) {
  46. unsigned enabled = menu->enabled() ? 0 : MF_GRAYED;
  47. MENUITEMINFO mii{sizeof(MENUITEMINFO)};
  48. mii.fMask = MIIM_DATA;
  49. mii.dwItemData = (ULONG_PTR)menu.data();
  50. if(menu->visible()) {
  51. if(auto self = menu->self()) {
  52. self->_update();
  53. AppendMenu(hmenu, MF_STRING | MF_POPUP | enabled, (UINT_PTR)self->hmenu, utf16_t(menu->text()));
  54. SetMenuItemInfo(hmenu, position++, true, &mii);
  55. }
  56. }
  57. }
  58. #endif
  59. if(auto parent = _parent()) {
  60. SetMenu(parent->hwnd, self().visible(true) ? hmenu : nullptr);
  61. parent->setGeometry(parent->state().geometry);
  62. }
  63. }
  64. }
  65. #endif