File: menu-bar.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (95 lines) | stat: -rw-r--r-- 2,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#if defined(Hiro_MenuBar)

namespace hiro {

auto pMenuBar::construct() -> void {
  _update();
}

auto pMenuBar::destruct() -> void {
  pApplication::state().menuBarsToRebuild.removeByValue(this);
  if(hmenu) { DestroyMenu(hmenu); hmenu = nullptr; }
  if(auto parent = _parent()) {
    SetMenu(parent->hwnd, nullptr);
  }
}

auto pMenuBar::append(sMenu) -> void {
  _update();
}

auto pMenuBar::remove(sMenu) -> void {
  _update();
}

auto pMenuBar::setEnabled(bool enabled) -> void {
  _update();
}

auto pMenuBar::setFont(const Font& font) -> void {
  //unsupported
}

auto pMenuBar::setVisible(bool visible) -> void {
  if(auto parent = _parent()) {
    SetMenu(parent->hwnd, visible ? hmenu : nullptr);
    parent->setGeometry(parent->state().geometry);
  }
}

auto pMenuBar::_parent() -> maybe<pWindow&> {
  if(auto parent = self().parentWindow(true)) {
    if(auto self = parent->self()) return *self;
  }
  return nothing;
}

auto pMenuBar::_rebuild() -> void {
  if(hmenu) DestroyMenu(hmenu);
  hmenu = CreateMenu();

  MENUINFO mi{sizeof(MENUINFO)};
  mi.fMask = MIM_STYLE;
  mi.dwStyle = MNS_NOTIFYBYPOS;  //| MNS_MODELESS;
  SetMenuInfo(hmenu, &mi);

  u32 position = 0;

  #if defined(Hiro_Menu)
  for(auto& menu : state().menus) {
    u32 enabled = menu->enabled() ? 0 : MF_GRAYED;

    MENUITEMINFO mii{sizeof(MENUITEMINFO)};
    mii.fMask = MIIM_DATA;
    mii.dwItemData = (ULONG_PTR)menu.data();

    if(menu->visible()) {
      if(auto self = menu->self()) {
        self->_update();
        AppendMenu(hmenu, MF_STRING | MF_POPUP | enabled, (UINT_PTR)self->hmenu, utf16_t(menu->text()));
        SetMenuItemInfo(hmenu, position++, true, &mii);
      }
    }
  }
  #endif

  if(auto parent = _parent()) {
    SetMenu(parent->hwnd, self().visible(true) ? hmenu : nullptr);
    parent->setGeometry(parent->state().geometry);
  }
}

auto pMenuBar::_update() -> void {
  //if there is no menu bar, build it immediately to ensure correct window geometry calculations
  if(auto parent = _parent()) {
    if(!GetMenu(parent->hwnd)) return _rebuild();
  }

  //otherwise, defer the menu bar update for later to reduce flickering
  pApplication::state().menuBarsToRebuild.removeByValue(this);
  pApplication::state().menuBarsToRebuild.append(this);
}

}

#endif