-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
161 lines (133 loc) · 5.22 KB
/
Window.cpp
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
// Include windows.h without WIN32_LEAN_AND_MEAN
// to support drag & drop.
#include <windows.h>
#include <windowsx.h>
#include <iostream>
#include "Window.h"
using namespace std;
namespace ST
{
Window* MainWindow;
//-------------- Window constructor --------------//
Window::Window(LPCTSTR title, DWORD style, DWORD exStyle,
int width, int height, int x, int y, HWND parent, HMENU menu)
{
keyEventProcessor = 0;
WNDCLASSEX wc = {0};
HINSTANCE handle = ::GetModuleHandle(0);
wc.cbSize = sizeof(wc);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hInstance = handle;
wc.lpfnWndProc = Window::windowProc;
wc.lpszClassName = TEXT("AppWindowClass");
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
::RegisterClassEx(&wc);
RECT rect;
rect.top = y;
rect.left = x;
rect.right = x + width;
rect.bottom = y + height;
::AdjustWindowRectEx(&rect, style, (bool)menu, exStyle);
hwnd = ::CreateWindowEx(exStyle, TEXT("AppWindowClass"),
title, style, x, y, rect.right - rect.left,
rect.bottom - rect.top, parent, menu, handle, this);
}
//-------------- Window Procedure --------------//
LRESULT CALLBACK Window::windowProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static Window* windowInstance = 0; // What window do we process?
// Before window is created, store the pointer in window's memory.
if (msg == WM_NCCREATE)
{
CREATESTRUCT* createStruct = (CREATESTRUCT*)lParam;
windowInstance = (Window*)createStruct->lpCreateParams;
// For now CreateWindowEx() hasn't returned HWND, so
// windowInstance doesn't contain it's HWND. As we may need
// this handle later, assign it's value beforehand.
windowInstance->hwnd = hwnd;
}
if (windowInstance)
return windowInstance->handleMessage(msg, wParam, lParam);
else
// This will happen exactly twice (before WM_NCCREATE message).
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//-------------- Message Handler --------------//
LRESULT Window::handleMessage(UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
{
if (keyEventProcessor)
keyEventProcessor->KeyDown(wParam);
} return 0;
case WM_DROPFILES:
{
if (keyEventProcessor)
{
HDROP hDrop = (HDROP)wParam;
const size_t size = 1000;
TCHAR file[size];
int fileNum = DragQueryFile(hDrop, -1, 0, 0);
if (fileNum > 2)
{
MessageBox(0, TEXT("To many files dropped."),
TEXT("ERROR"), MB_OK);
}
else
{
// Go through all drag and dropped files.
for (int i = 0; i < fileNum; i++)
{
// Get the name of the file.
DragQueryFile(hDrop, i, file, size);
// That's why the whole
// project cannot work with UNICODE.
string fileName(file);
size_t dotPos = fileName.rfind(".");
if (dotPos == string::npos)
{
MessageBox(0, TEXT("File has bad extension."),
TEXT("ERROR"), MB_OK);
break;
}
string name = fileName.substr(0, dotPos);
string ext = fileName.substr(dotPos);
keyEventProcessor->FileDropped(name, ext);
}
}
DragFinish(hDrop); // Release resources.
}
} return 0;
case WM_LBUTTONDOWN:
{
size_t x = GET_X_LPARAM(lParam);
size_t y = GET_Y_LPARAM(lParam);
keyEventProcessor->LButtonDown(x, y);
} return 0;
case WM_LBUTTONUP:
{
size_t x = GET_X_LPARAM(lParam);
size_t y = GET_Y_LPARAM(lParam);
keyEventProcessor->LButtonUp(x, y);
} return 0;
case WM_MOUSEMOVE:
{
size_t x = GET_X_LPARAM(lParam);
size_t y = GET_Y_LPARAM(lParam);
keyEventProcessor->MouseMove(x, y);
} return 0;
case WM_DESTROY:
{
::PostQuitMessage(0);
} return 0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// Something is wrong.
return true;
}
}