Setup UI menu code

This commit is contained in:
Dr. Chat
2015-05-09 16:47:52 -05:00
parent 4411a4499d
commit 459024059a
9 changed files with 108 additions and 21 deletions

View File

@@ -9,12 +9,29 @@
#include "xenia/ui/win32/win32_menu_item.h"
#include "xenia/base/assert.h"
namespace xe {
namespace ui {
namespace win32 {
Win32MenuItem::Win32MenuItem(Type type, int id, const std::wstring& text)
: id_(id), MenuItem(type, text) {
switch (type) {
case MenuItem::Type::kNormal:
handle_ = CreateMenu();
break;
case MenuItem::Type::kPopup:
handle_ = CreatePopupMenu();
break;
}
}
Win32MenuItem::Win32MenuItem(Type type)
: MenuItem(type), handle_(CreateMenu()) {}
: Win32MenuItem(type, 0, L"") {}
Win32MenuItem::Win32MenuItem(Type type, const std::wstring& text)
: Win32MenuItem(type, 0, text) {}
Win32MenuItem::~Win32MenuItem() {
if (handle_) {
@@ -24,7 +41,20 @@ Win32MenuItem::~Win32MenuItem() {
void Win32MenuItem::OnChildAdded(MenuItem* generic_child_item) {
auto child_item = static_cast<Win32MenuItem*>(generic_child_item);
//
switch (child_item->type()) {
case MenuItem::Type::kPopup:
AppendMenuW(handle_, MF_POPUP, (UINT)child_item->handle(),
child_item->text().c_str());
break;
case MenuItem::Type::kSeparator:
AppendMenuW(handle_, MF_SEPARATOR, child_item->id(), 0);
break;
case MenuItem::Type::kString:
AppendMenuW(handle_, MF_STRING, child_item->id(),
child_item->text().c_str());
break;
}
}
void Win32MenuItem::OnChildRemoved(MenuItem* generic_child_item) {

View File

@@ -21,16 +21,23 @@ namespace win32 {
class Win32MenuItem : public MenuItem {
public:
Win32MenuItem(Type type);
Win32MenuItem(Type type, const std::wstring &text);
Win32MenuItem(Type type, int id, const std::wstring &text);
~Win32MenuItem() override;
HMENU handle() { return handle_; }
int id() { return id_; }
protected:
void OnChildAdded(MenuItem* child_item) override;
void OnChildRemoved(MenuItem* child_item) override;
private:
Win32MenuItem(Type type);
HMENU handle_;
uint32_t position_; // Position within parent, if any
int id_;
};
} // namespace win32

View File

@@ -20,7 +20,9 @@ namespace ui {
namespace win32 {
Win32Window::Win32Window(const std::wstring& title)
: Window(title), main_menu_(nullptr), closing_(false) {}
: Window(title), closing_(false) {
menu_ = nullptr;
}
Win32Window::~Win32Window() {}
@@ -57,7 +59,7 @@ bool Win32Window::Create() {
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
// Create window.
hwnd_ = CreateWindowEx(window_ex_style, L"XeniaWindowClass", L"Xenia",
hwnd_ = CreateWindowEx(window_ex_style, L"XeniaWindowClass", title_.c_str(),
window_style, rc.left, rc.top, rc.right - rc.left,
rc.bottom - rc.top, nullptr, nullptr, hInstance, this);
if (!hwnd_) {
@@ -65,10 +67,6 @@ bool Win32Window::Create() {
return false;
}
main_menu_ = CreateMenu();
AppendMenu(main_menu_, MF_STRING, 0, L"TODO");
SetMenu(hwnd_, main_menu_);
// Disable flicks.
ATOM atom = GlobalAddAtom(L"MicrosoftTabletPenServiceProperty");
const DWORD_PTR dwHwndTabletProperty =
@@ -137,7 +135,7 @@ bool Win32Window::set_title(const std::wstring& title) {
void Win32Window::Resize(int32_t width, int32_t height) {
RECT rc = {0, 0, width, height};
bool has_menu = main_menu_ ? true : false;
bool has_menu = menu_ ? true : false;
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, has_menu);
if (true) {
rc.right += 100 - rc.left;
@@ -151,7 +149,7 @@ void Win32Window::Resize(int32_t width, int32_t height) {
void Win32Window::Resize(int32_t left, int32_t top, int32_t right,
int32_t bottom) {
RECT rc = {left, top, right, bottom};
bool has_menu = main_menu_ ? true : false;
bool has_menu = menu_ ? true : false;
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, has_menu);
Window::Resize(rc.left, rc.top, rc.right, rc.bottom);
}
@@ -168,6 +166,13 @@ void Win32Window::OnClose() {
}
}
void Win32Window::OnSetMenu(MenuItem* menu) {
Win32MenuItem* win_menu = reinterpret_cast<Win32MenuItem*>(menu);
if (win_menu) {
::SetMenu(hwnd_, win_menu->handle());
}
}
LRESULT Win32Window::WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
switch (message) {
@@ -197,9 +202,9 @@ LRESULT Win32Window::WndProc(HWND hWnd, UINT message, WPARAM wParam,
TABLET_DISABLE_TOUCHSWITCH | TABLET_DISABLE_SMOOTHSCROLLING |
TABLET_DISABLE_TOUCHUIFORCEON | TABLET_ENABLE_MULTITOUCHDATA;
case WM_COMMAND:
switch (LOWORD(wParam)) {
// TODO(benvanik): dispatch to menu.
case WM_COMMAND: {
OnCommand(LOWORD(wParam));
break;
}
break;
}

View File

@@ -13,6 +13,7 @@
#include <string>
#include "xenia/ui/win32/win32_control.h"
#include "xenia/ui/win32/win32_menu_item.h"
#include "xenia/ui/window.h"
namespace xe {
@@ -38,13 +39,14 @@ class Win32Window : public Window<Win32Control> {
bool Create() override;
void OnClose() override;
void OnSetMenu(MenuItem*) override;
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) override;
private:
void EnableMMCSS();
HMENU main_menu_;
bool closing_;
};