summaryrefslogtreecommitdiff
path: root/Editor/GUI/GUIWindow.cpp
blob: e13770e3174ef2dc74f2c809e8deb993a1d59eca (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
#include "EditorWindows.h"
#include "WinUtils.h"
#include "Runtime/Graphics/OpenGL.h"

static PAINTSTRUCT ps;
LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	log_info("WndProc", "GUIWindow::GUIViewWndProc(), hWnd=" /*+ (long)hWnd*/);

	GUIWindow* wnd = (GUIWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
	if (!wnd)
	{
		log_warning("GUIWindow::GUIViewWndProc(), lParamΪ¿Õ");
		return  DefWindowProcW(hWnd, message, wParam, lParam);
	}

	switch (message) 
	{
		case WM_PAINT:
		{
			log_info("WM_PAINT");
			wnd->SetAsRenderContext();
			glEnable(GL_BLEND);
			glClearColor(1,0,0,1);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glFlush();
			BeginPaint(wnd->m_Handle, &ps); 
			EndPaint(wnd->m_Handle, &ps);
			UpdateWindow(wnd->m_Handle);
			return 0;
		}
	}

	long flag = DefWindowProcW(hWnd, message, wParam, lParam);
	return flag;
}

void GUIWindow::RepaintAll()
{
}

//////////////////////////////////////////////////////////////////////////////////////////

void GUIWindow::Init() 
{
	log_info("Init GUIWindow " /*+ (long)this*/);
	
	DWORD windowStyle = WS_CHILD;
	//DWORD windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
	DWORD extendedStyle = WS_EX_TOOLWINDOW;

	Rectf pixelRect;
	pixelRect.x = 0;
	pixelRect.y = 0;
	pixelRect.width = 32;
	pixelRect.height = 32;

	RECT rc;
	rc.left = pixelRect.x;
	rc.top = pixelRect.y;
	rc.right = pixelRect.x + pixelRect.width;
	rc.bottom = pixelRect.y + pixelRect.height;
	AdjustWindowRectEx(&rc, windowStyle, FALSE, extendedStyle);

	m_Handle = CreateWindowExW(
		extendedStyle, 
		WindowUtil::kGUIWindowClassName, 
		L"",
		windowStyle,
		rc.left, 
		rc.top, 
		rc.right - rc.left, 
		rc.bottom - rc.top,
		HWND_MESSAGE,  // message only
		NULL, 
		winutils::GetInstanceHandle(), 
		NULL
	);

	SetWindowLongPtr(m_Handle, GWLP_USERDATA, (LONG_PTR)this);

	ShowWindow(m_Handle, SW_SHOW);

	if (!SetRenderContext())
		log_error("Failed to setup rendering context");

	log_info("Created GUIWindow " /*+ (long)this*/);
}

bool GUIWindow::SetRenderContext()
{
	m_DC = ::GetDC(m_Handle);
	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
	}

	return true;
}

void GUIWindow::DoPaint()
{
	SetWindowLongPtr(m_Handle, GWLP_USERDATA,(LONG_PTR ) this);
	SendMessage(m_Handle, WM_PAINT, 0, 0);
}

void GUIWindow::OnFocus()
{
}

void GUIWindow::OnLostFocus()
{
}

void GUIWindow::SetPosition(Rectf position)
{
	log_info("GUIWindow::SetPosition()");
	RECT rc;
	rc.left = position.x;
	rc.top = position.y;
	rc.right = position.x + position.width;
	rc.bottom = position.y + position.height;
	::SetWindowPos(m_Handle, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_FRAMECHANGED | SWP_NOOWNERZORDER | SWP_NOACTIVATE | SWP_NOZORDER);
}

void GUIWindow::SetAsRenderContext()
{
	Assert(m_DC != NULL);
	Assert(m_RC != NULL);
	Assert(wglMakeCurrent(m_DC, m_RC));
	RECT rect;
	GetWindowRect(m_Handle, &rect);
	glViewport(0, 0, rect.right - rect.left, rect.bottom - rect.top);
}

void GUIWindow::Focus()
{
	log_info("Focus GUIWindow " + (long)this);
	if (m_Handle)
	{
		if (::GetForegroundWindow() != m_ContainnerWindow->GetWindowHandle())
			::SetForegroundWindow(m_ContainnerWindow->GetWindowHandle());
		SetFocus(m_Handle);
	}
}

void GUIWindow::SetContainnerWindow(ContainnerWindow* wnd)
{
	Assert(wnd != NULL);

	m_ContainnerWindow = wnd;

	if (wnd->GetWindowHandle() != m_Handle)
	{
		HWND parentWindowHandle = wnd->GetWindowHandle();
		if (::GetParent(m_Handle) != parentWindowHandle)
			::SetParent(m_Handle, parentWindowHandle);
		::ShowWindow(m_Handle, SW_SHOWNORMAL);
	}
}