summaryrefslogtreecommitdiff
path: root/Editor/GUI/ContainnerWindow.cpp
blob: 5e6c35648e4337e6c34b9b47d7d94bd228971504 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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()));
}