summaryrefslogtreecommitdiff
path: root/Editor/GUI/ContainnerWindow.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-17 11:14:00 +0800
committerchai <chaifix@163.com>2021-10-17 11:14:00 +0800
commitd35db57d457132dd9d83fa2bd51491b148530655 (patch)
treeb5a29675f091f268577b5991988c723f273b0bd1 /Editor/GUI/ContainnerWindow.cpp
parent7b0a6d1fe0117cf42a5776aaabda2db78599e5b8 (diff)
*GUI
Diffstat (limited to 'Editor/GUI/ContainnerWindow.cpp')
-rw-r--r--Editor/GUI/ContainnerWindow.cpp291
1 files changed, 291 insertions, 0 deletions
diff --git a/Editor/GUI/ContainnerWindow.cpp b/Editor/GUI/ContainnerWindow.cpp
new file mode 100644
index 0000000..5e6c356
--- /dev/null
+++ b/Editor/GUI/ContainnerWindow.cpp
@@ -0,0 +1,291 @@
+#include <string>
+
+#include "EditorWindows.h"
+#include "WinUtils.h"
+#include "Editor/Utils/HelperFuncs.h"
+#include "MenuManager.h"
+#include "Runtime/Utilities/Assert.h"
+
+using namespace std;
+
+static PAINTSTRUCT ps;
+LRESULT CALLBACK ContainnerWindow::ContainerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+ log_info("WndProc", "ContainnerWindow::ContainerWndProc()");
+
+ ContainnerWindow *self = (ContainnerWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
+ if (!self)
+ {
+ log_warning("Nullptr ContainnerWindow");
+ return DefWindowProcW(hWnd, message, wParam, lParam);
+ }
+
+ switch (message)
+ {
+ case WM_MENUSELECT:
+ {
+ if (self->m_ShowMode != ShowMode::kShowMainWindow)
+ return 0;
+ uint menuId = LOWORD(wParam);
+ uint flags = HIWORD(wParam);
+ HMENU menu = (HMENU)lParam;
+ MenuManager::Instance()->HandleMenuItemEvent(menu, menuId, flags);
+ return 0;
+ }
+ case WM_SETICON:
+ return WM_SETICON;
+ case WM_PAINT:
+ {
+ log_info("WM_PAINT");
+ self->SetAsRenderContext();
+ glEnable(GL_BLEND);
+ float c = 26 / 255.f;
+ glClearColor(c, c, c, 1);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glFlush();
+ BeginPaint(self->m_Window, &ps);
+ EndPaint(self->m_Window, &ps);
+ UpdateWindow(self->m_Window);
+ return 0;
+ }
+ }
+
+ long flag = DefWindowProcW(hWnd, message, wParam, lParam);
+ return flag;
+}
+
+ContainnerWindow::ContainnerWindow()
+{
+}
+
+ContainnerWindow::~ContainnerWindow()
+{
+
+}
+
+void ContainnerWindow::DoPaint()
+{
+ SetWindowLongPtr(m_Window, GWLP_USERDATA, (LONG_PTR)this);
+ SendMessage(m_Window, WM_PAINT, 0, 0);
+}
+
+void ContainnerWindow::SetAsRenderContext()
+{
+ Assert(m_DC != NULL);
+ Assert(m_RC != NULL);
+ Assert(wglMakeCurrent(m_DC, m_RC));
+ RECT rect;
+ GetWindowRect(m_Window, &rect);
+ glViewport(0, 0, rect.right - rect.left, rect.bottom - rect.top);
+}
+
+bool ContainnerWindow::SetRenderContext()
+{
+ m_DC = ::GetDC(m_Window);
+ if (!m_DC)
+ log_error("Can't create DC");
+
+ int bits = 32;
+
+ //static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
+ //{
+ // sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
+ // 1, // Version Number
+ // PFD_DRAW_TO_WINDOW | // Format Must Support Window
+ // PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
+ // PFD_DOUBLEBUFFER, // Must Support Double Buffering
+ // PFD_TYPE_RGBA, // Request An RGBA Format
+ // bits, // Select Our Color Depth
+ // 0, 0, 0, 0, 0, 0, // Color Bits Ignored
+ // 0, // No Alpha Buffer
+ // 0, // Shift Bit Ignored
+ // 0, // No Accumulation Buffer
+ // 0, 0, 0, 0, // Accumulation Bits Ignored
+ // 16, // 16Bit Z-Buffer (Depth Buffer)
+ // 0, // No Stencil Buffer
+ // 0, // No Auxiliary Buffer
+ // PFD_MAIN_PLANE, // Main Drawing Layer
+ // 0, // Reserved
+ // 0, 0, 0 // Layer Masks Ignored
+ //};
+ static PIXELFORMATDESCRIPTOR pfd;
+ pfd.nSize = sizeof(pfd);
+ pfd.nVersion = 1;
+ pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
+ pfd.iPixelType = PFD_TYPE_RGBA;
+ pfd.cColorBits = 32;
+
+ GLuint PixelFormat;
+
+ if (!(PixelFormat = ChoosePixelFormat(m_DC, &pfd))) // Did Windows Find A Matching Pixel Format?
+ {
+ MessageBox(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
+ return FALSE; // Return FALSE
+ }
+
+ if (!SetPixelFormat(m_DC, PixelFormat, &pfd)) // Are We Able To Set The Pixel Format?
+ {
+ MessageBox(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
+ return FALSE; // Return FALSE
+ }
+
+ int pf = 0;
+ DescribePixelFormat(m_DC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
+
+ if (!(m_RC = wglCreateContext(m_DC))) // Are We Able To Get A Rendering Context?
+ {
+ MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
+ return FALSE; // Return FALSE
+ }
+
+}
+
+HWND ContainnerWindow::GetWindowHandle()
+{
+ return m_Window;
+}
+
+// 初始化,创建窗口
+void ContainnerWindow::Init(Rectf pixelRect, int showMode, const Vector2f& minSize, const Vector2f& maxSize)
+{
+ // Aux windows are mac only. on windows they look just like normal utility windows.
+ if (showMode == kShowAuxWindow)
+ showMode = kShowUtility;
+
+ m_ShowMode = static_cast<ShowMode>(showMode);
+ m_IsClosing = false;
+ m_InMenuLoop = false;
+ m_CloseFromScriptDontShutdown = false;
+
+ bool shouldMaximize = false;
+ if (showMode == kShowMainWindow)
+ {
+ //shouldMaximize = s_IsMainWindowMaximized = EditorPrefs::GetBool(kIsMainWindowMaximized, false);
+ //GetRestoredMainWindowDimensions();
+ }
+
+ RECT rect;
+ rect.left = pixelRect.x;
+ rect.right = pixelRect.x + pixelRect.width;
+ rect.top = pixelRect.y;
+ rect.bottom = pixelRect.y + pixelRect.height;
+ m_InternalRect.x = m_InternalRect.y = m_InternalRect.width = m_InternalRect.height = 0.0f;
+
+ DWORD windowStyle = 0;
+ DWORD extendedStyle = 0;
+ LPCWSTR windowClass = WindowUtil::kContainerWindowClassName;
+
+ switch (showMode) {
+ // 容纳GUIPanel的非主窗口
+ case kShowNormalWindow:
+ windowStyle = WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME;
+ extendedStyle = WS_EX_TOOLWINDOW;
+ break;
+ // 主窗口,含菜单
+ case kShowMainWindow:
+ windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
+ extendedStyle = 0;
+ break;
+ case kShowPopupMenu:
+ windowStyle = WS_POPUP | WS_CLIPCHILDREN;
+ extendedStyle = WS_EX_TOOLWINDOW;
+ windowClass = WindowUtil::kPopupWindowClassName;
+ break;
+ case kShowNoShadow:
+ windowStyle = WS_POPUP | WS_CLIPCHILDREN;
+ extendedStyle = WS_EX_TOOLWINDOW;
+ break;
+ case kShowUtility:
+ windowStyle = WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_SYSMENU;
+ extendedStyle = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW;
+ break;
+ default:
+ // ErrorString("Unknown container show mode");
+ break;
+ }
+
+ HWND parentWindow = NULL;
+ if (showMode == kShowMainWindow)
+ {
+ parentWindow = NULL;
+ }
+ else
+ {
+ // parentWindow = GetMainEditorWindow();
+ }
+
+ bool notSizeable = (minSize == maxSize) && (minSize != Vector2f::zero);
+ if (notSizeable)
+ windowStyle &= ~(WS_THICKFRAME);
+
+ AdjustWindowRectEx(&rect, windowStyle, showMode == kShowMainWindow, extendedStyle);
+ int extraX = rect.right - rect.left - 200;
+ int extraY = rect.bottom - rect.top - 200;
+
+ m_MinSize.x = minSize.x + extraX;
+ m_MinSize.y = minSize.y + extraY;
+ m_MaxSize.x = maxSize.x + extraX;
+ m_MaxSize.y = maxSize.y + extraY;
+
+ //if (showMode == kShowUtility)
+ // s_ZUtilityWindows.push_back(m_ContainerListNode);
+ // Create window
+ m_Window = CreateWindowExW(
+ extendedStyle,
+ windowClass,
+ L"",
+ windowStyle,
+ rect.left,
+ rect.top,
+ rect.right - rect.left,
+ rect.bottom - rect.top,
+ parentWindow,
+ NULL,
+ winutils::GetInstanceHandle(),
+ NULL
+ );
+ SetWindowLongPtr(m_Window, GWLP_USERDATA, (LONG_PTR)this);
+ //UpdateMaxMaximizedRect();
+
+ if (pixelRect.width > 10 || pixelRect.height > 10)
+ {
+ //SetRect(pixelRect);
+ if (shouldMaximize)
+ SetWindowLong(m_Window, GWL_STYLE, windowStyle | WS_MAXIMIZE);
+ }
+
+ if (showMode == kShowMainWindow)
+ {
+ //SetMainEditorWindow(m_Window);
+ //HMENU menu = GetMainMenuHandle();
+ //if (menu)
+ // SetMenu(m_Window, menu);
+ //GetApplication().UpdateMainWindowTitle();
+ //GetApplication().UpdateDocumentEdited();
+ }
+ ShowWindow(m_Window, SW_SHOW);
+
+ //s_ContainerWindows.insert(this);
+
+ //ShowInTaskbarIfNoMainWindow(m_Window);
+
+ SetRenderContext();
+}
+
+void ContainnerWindow::SetTitle(const char* title)
+{
+ SetWindowTextA(m_Window, title);
+}
+
+void ContainnerWindow::SetIcon(LPCSTR iconName)
+{
+ Assert(m_ShowMode == ShowMode::kShowMainWindow);
+
+ HICON hWindowIcon = (HICON )LoadImage(winutils::GetInstanceHandle(), iconName, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
+ if (hWindowIcon == NULL)
+ log_error("icon error: " + to_string(GetLastError()));
+ if (SendMessage(m_Window, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon) != WM_SETICON)
+ log_error("icon error: " + to_string(GetLastError()));
+ if (SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon) != WM_SETICON)
+ log_error("icon error: " + to_string(GetLastError()));
+}