Debian Codegraph @

src/GUI/FGWindowsMenuBar.cxx

raw symbols SCIP
1 #include "FGWindowsMenuBar.hxx"
2
3 #include <windows.h>
4 #include <cstring>
5
6 #include <osgViewer/Viewer>
7 #include <osgViewer/GraphicsWindow>
8 #include <osgViewer/api/Win32/GraphicsWindowWin32>
9
10 #include <simgear/props/props.hxx>
11 #include <simgear/props/props_io.hxx>
12 #include <simgear/debug/logstream.hxx>
13 #include <simgear/structure/SGBinding.hxx>
14 #include <simgear/misc/strutils.hxx>
15
16 #include <Main/fg_props.hxx>
17 #include <Main/globals.hxx>
18 #include <Viewer/renderer.hxx>
19
20 #include <iostream>
21
22 using namespace simgear;
23
24 namespace {
25
26 HWND getMainViewerHWND()
27 {
28 osgViewer::Viewer::Windows windows;
29 if (!globals->get_renderer() || !globals->get_renderer()->getViewerBase()) {
30 return 0;
31 }
32
33 globals->get_renderer()->getViewerBase()->getWindows(windows);
34 osgViewer::Viewer::Windows::const_iterator it = windows.begin();
35 for(; it != windows.end(); ++it) {
36 if (strcmp((*it)->className(), "GraphicsWindowWin32")) {
37 continue;
38 }
39
40 osgViewer::GraphicsWindowWin32* platformWin =
41 static_cast<osgViewer::GraphicsWindowWin32*>(*it);
42 return platformWin->getHWND();
43 }
44
45 return 0;
46 }
47
48 bool labelIsSeparator(const std::string& s)
49 {
50 std::string t = "---";
51 if (s.compare(0, t.length(), t) == 0)
52 return true;
53 else
54 return false;
55 }
56
57 LRESULT CALLBACK menubarWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
58 {
59 SG_LOG(SG_GENERAL, SG_INFO, "called window proc");
60
61
62 return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
63 }
64
65 } // of anonymous namespace
66
67 //
68
69 class FGWindowsMenuBar::WindowsMenuBarPrivate
70 {
71 public:
72 WindowsMenuBarPrivate();
73 ~WindowsMenuBarPrivate();
74
75 void fireBindingsForItem(UINT commandId)
76 {
77 fireBindingList(itemBindings[commandId]);
78 }
79
80 HWND mainWindow;
81 HMENU menuBar;
82 bool visible;
83 WNDPROC baseMenuProc;
84
85 typedef std::vector<SGBindingList> MenuItemBindings;
86 MenuItemBindings itemBindings;
87
88 };
89
90 FGWindowsMenuBar::FGWindowsMenuBar() :
91 p(new WindowsMenuBarPrivate)
92 {
93
94 }
95
96 FGWindowsMenuBar::~FGWindowsMenuBar()
97 {
98
99 }
100
101 FGWindowsMenuBar::WindowsMenuBarPrivate::WindowsMenuBarPrivate() :
102 visible(true)
103 {
104 mainWindow = getMainViewerHWND();
105 menuBar = 0;
106 }
107
108 FGWindowsMenuBar::WindowsMenuBarPrivate::~WindowsMenuBarPrivate()
109 {
110 if (menuBar) {
111 SetMenu(mainWindow, NULL);
112 DestroyMenu(menuBar);
113 }
114 }
115
116 void FGWindowsMenuBar::init()
117 {
118 int menuIndex = 0;
119 SGPropertyNode_ptr props = fgGetNode("/sim/menubar/default",true);
120
121 p->menuBar = CreateMenu();
122 // p->baseMenuProc = (WNDPROC) ::SetWindowLongPtr((HWND) p->mainWindow, GWL_WNDPROC, (LONG_PTR) menubarWindowProc);
123
124 for (auto n : props->getChildren("menu")) {
125 // synchronise menu with properties
126 std::string l = getLocalizedLabel(n);
127 std::string label = strutils::simplify(l).c_str();
128 HMENU menuItems = CreatePopupMenu();
129
130 if (!n->hasValue("enabled")) {
131 n->setBoolValue("enabled", true);
132 }
133
134 bool enabled = n->getBoolValue("enabled");
135
136 UINT flags = MF_POPUP;
137 AppendMenuA(p->menuBar, flags, (UINT)menuItems, label.c_str());
138
139 // submenu
140 int subMenuIndex = 0;
141 SGPropertyNode* menuNode = n;
142 for (auto n2 : menuNode->getChildren("item")) {
143
144 if (!n2->hasValue("enabled")) {
145 n2->setBoolValue("enabled", true);
146 }
147
148 std::string l2 = getLocalizedLabel(n2);
149 std::string label2 = strutils::simplify(l2).c_str();
150 std::string shortcut = n2->getStringValue("key");
151
152 SGBindingList bl = readBindingList(n->getChildren("binding"), globals->get_props());
153 UINT commandId = p->itemBindings.size();
154 p->itemBindings.push_back(bl);
155
156 if (labelIsSeparator(label2)) {
157 AppendMenuA(menuItems, MF_SEPARATOR, NULL, NULL);
158 } else {
159 if (!shortcut.empty()) {
160 label2 += "\t"+shortcut;
161 }
162 BOOL enabled = n2->getBoolValue("enabled");
163
164 UINT flags = MF_STRING;
165 AppendMenuA(menuItems, flags, commandId, label2.c_str());
166 }
167 subMenuIndex++;
168 }
169 menuIndex++;
170 }
171
172 show();
173 }
174
175 bool FGWindowsMenuBar::isVisible() const
176 {
177 return p->visible;
178 }
179
180 void FGWindowsMenuBar::show()
181 {
182 SetMenu(p->mainWindow, p->menuBar);
183 p->visible = true;
184 }
185
186 void FGWindowsMenuBar::hide()
187 {
188 SetMenu(p->mainWindow, NULL);
189 p->visible = false;
190 }
191
192 #if 0
193 LRESULT CALLBACK WndProcedure(HWND hwnd, UINT Msg,
194 WPARAM wParam, LPARAM lParam)
195 {
196 switch(Msg)
197 {
198 case WM_COMMAND:
199 switch(LOWORD(wParam))
200 {
201 case MY_MENU:
202 MessageBox(hwnd, "Menu Item Selected = Large", "Message", MB_OK);
203 break;
204 }
205 return 0;
206
207 case WM_DESTROY:
208 PostQuitMessage(WM_QUIT);
209 break;
210
211 default:
212 return DefWindowProc(hwnd, Msg, wParam, lParam);
213 }
214
215 return 0;
216 }
217 #endif