| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
static bool Application_keyboardProc(HWND, UINT, WPARAM, LPARAM); |
| 4 |
static void Application_processDialogMessage(MSG&); |
| 5 |
static LRESULT CALLBACK Application_windowProc(HWND, UINT, WPARAM, LPARAM); |
| 6 |
|
| 7 |
void pApplication::run() { |
| 8 |
MSG msg; |
| 9 |
if(Application::main) { |
| 10 |
while(applicationState.quit == false) { |
| 11 |
Application::main(); |
| 12 |
processEvents(); |
| 13 |
} |
| 14 |
} else { |
| 15 |
MSG msg; |
| 16 |
while(GetMessage(&msg, 0, 0, 0)) { |
| 17 |
Application_processDialogMessage(msg); |
| 18 |
} |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
bool pApplication::pendingEvents() { |
| 23 |
MSG msg; |
| 24 |
return PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE); |
| 25 |
} |
| 26 |
|
| 27 |
void pApplication::processEvents() { |
| 28 |
while(pendingEvents()) { |
| 29 |
MSG msg; |
| 30 |
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { |
| 31 |
Application_processDialogMessage(msg); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
void Application_processDialogMessage(MSG& msg) { |
| 37 |
if(msg.message == WM_KEYDOWN || msg.message == WM_KEYUP |
| 38 |
|| msg.message == WM_SYSKEYDOWN || msg.message == WM_SYSKEYUP) { |
| 39 |
if(Application_keyboardProc(msg.hwnd, msg.message, msg.wParam, msg.lParam)) { |
| 40 |
DispatchMessage(&msg); |
| 41 |
return; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if(!IsDialogMessage(GetForegroundWindow(), &msg)) { |
| 46 |
TranslateMessage(&msg); |
| 47 |
DispatchMessage(&msg); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void pApplication::quit() { |
| 52 |
PostQuitMessage(0); |
| 53 |
} |
| 54 |
|
| 55 |
void pApplication::initialize() { |
| 56 |
CoInitialize(0); |
| 57 |
InitCommonControls(); |
| 58 |
|
| 59 |
WNDCLASS wc; |
| 60 |
wc.cbClsExtra = 0; |
| 61 |
wc.cbWndExtra = 0; |
| 62 |
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); |
| 63 |
wc.hCursor = LoadCursor(0, IDC_ARROW); |
| 64 |
wc.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(2)); |
| 65 |
wc.hInstance = GetModuleHandle(0); |
| 66 |
wc.lpfnWndProc = Application_windowProc; |
| 67 |
wc.lpszClassName = L"phoenix_window"; |
| 68 |
wc.lpszMenuName = 0; |
| 69 |
wc.style = CS_HREDRAW | CS_VREDRAW; |
| 70 |
RegisterClass(&wc); |
| 71 |
|
| 72 |
wc.cbClsExtra = 0; |
| 73 |
wc.cbWndExtra = 0; |
| 74 |
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); |
| 75 |
wc.hCursor = LoadCursor(0, IDC_ARROW); |
| 76 |
wc.hIcon = LoadIcon(0, IDI_APPLICATION); |
| 77 |
wc.hInstance = GetModuleHandle(0); |
| 78 |
wc.lpfnWndProc = Canvas_windowProc; |
| 79 |
wc.lpszClassName = L"phoenix_canvas"; |
| 80 |
wc.lpszMenuName = 0; |
| 81 |
wc.style = CS_HREDRAW | CS_VREDRAW; |
| 82 |
RegisterClass(&wc); |
| 83 |
|
| 84 |
wc.cbClsExtra = 0; |
| 85 |
wc.cbWndExtra = 0; |
| 86 |
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); |
| 87 |
wc.hCursor = LoadCursor(0, IDC_ARROW); |
| 88 |
wc.hIcon = LoadIcon(0, IDI_APPLICATION); |
| 89 |
wc.hInstance = GetModuleHandle(0); |
| 90 |
wc.lpfnWndProc = Label_windowProc; |
| 91 |
wc.lpszClassName = L"phoenix_label"; |
| 92 |
wc.lpszMenuName = 0; |
| 93 |
wc.style = CS_HREDRAW | CS_VREDRAW; |
| 94 |
RegisterClass(&wc); |
| 95 |
|
| 96 |
wc.cbClsExtra = 0; |
| 97 |
wc.cbWndExtra = 0; |
| 98 |
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0)); |
| 99 |
wc.hCursor = LoadCursor(0, IDC_ARROW); |
| 100 |
wc.hIcon = LoadIcon(0, IDI_APPLICATION); |
| 101 |
wc.hInstance = GetModuleHandle(0); |
| 102 |
wc.lpfnWndProc = Viewport_windowProc; |
| 103 |
wc.lpszClassName = L"phoenix_viewport"; |
| 104 |
wc.lpszMenuName = 0; |
| 105 |
wc.style = CS_HREDRAW | CS_VREDRAW; |
| 106 |
RegisterClass(&wc); |
| 107 |
|
| 108 |
settings = new Settings; |
| 109 |
pKeyboard::initialize(); |
| 110 |
} |
| 111 |
|
| 112 |
static bool Application_keyboardProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { |
| 113 |
if(msg != WM_KEYDOWN && msg != WM_SYSKEYDOWN && msg != WM_KEYUP && msg != WM_SYSKEYUP) return false; |
| 114 |
|
| 115 |
GUITHREADINFO info; |
| 116 |
memset(&info, 0, sizeof(GUITHREADINFO)); |
| 117 |
info.cbSize = sizeof(GUITHREADINFO); |
| 118 |
GetGUIThreadInfo(GetCurrentThreadId(), &info); |
| 119 |
Object* object = (Object*)GetWindowLongPtr(info.hwndFocus, GWLP_USERDATA); |
| 120 |
if(object == nullptr) return false; |
| 121 |
|
| 122 |
if(dynamic_cast<Window*>(object)) { |
| 123 |
Window& window = (Window&)*object; |
| 124 |
if(pWindow::modal.size() > 0 && !pWindow::modal.find(&window.p)) return false; |
| 125 |
Keyboard::Keycode keysym = Keysym(wparam, lparam); |
| 126 |
if(keysym != Keyboard::Keycode::None) { |
| 127 |
if((msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) && window.onKeyPress) window.onKeyPress(keysym); |
| 128 |
if((msg == WM_KEYUP || msg == WM_SYSKEYUP) && window.onKeyRelease) window.onKeyRelease(keysym); |
| 129 |
} |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
if(msg == WM_KEYDOWN) { |
| 134 |
if(dynamic_cast<ListView*>(object)) { |
| 135 |
ListView& listView = (ListView&)*object; |
| 136 |
if(wparam == VK_RETURN) { |
| 137 |
if(listView.selected()) return true; //returning true generates LVN_ITEMACTIVATE message |
| 138 |
} |
| 139 |
} else if(dynamic_cast<LineEdit*>(object)) { |
| 140 |
LineEdit& lineEdit = (LineEdit&)*object; |
| 141 |
if(wparam == VK_RETURN) { |
| 142 |
if(lineEdit.onActivate) lineEdit.onActivate(); |
| 143 |
} |
| 144 |
} else if(dynamic_cast<TextEdit*>(object)) { |
| 145 |
TextEdit& textEdit = (TextEdit&)*object; |
| 146 |
if(wparam == 'A' && GetKeyState(VK_CONTROL) < 0) { |
| 147 |
//Ctrl+A = select all text |
| 148 |
//note: this is not a standard accelerator on Windows |
| 149 |
Edit_SetSel(textEdit.p.hwnd, 0, ~0); |
| 150 |
return true; |
| 151 |
} else if(wparam == 'V' && GetKeyState(VK_CONTROL) < 0) { |
| 152 |
//Ctrl+V = paste text |
| 153 |
//note: this formats Unix (LF) and OS9 (CR) line-endings to Windows (CR+LF) line-endings |
| 154 |
//this is necessary as the EDIT control only supports Windows line-endings |
| 155 |
OpenClipboard(hwnd); |
| 156 |
HANDLE handle = GetClipboardData(CF_UNICODETEXT); |
| 157 |
if(handle) { |
| 158 |
wchar_t* text = (wchar_t*)GlobalLock(handle); |
| 159 |
if(text) { |
| 160 |
string data = (const char*)utf8_t(text); |
| 161 |
data.replace("\r\n", "\n"); |
| 162 |
data.replace("\r", "\n"); |
| 163 |
data.replace("\n", "\r\n"); |
| 164 |
GlobalUnlock(handle); |
| 165 |
utf16_t output(data); |
| 166 |
HGLOBAL resource = GlobalAlloc(GMEM_MOVEABLE, (wcslen(output) + 1) * sizeof(wchar_t)); |
| 167 |
if(resource) { |
| 168 |
wchar_t* write = (wchar_t*)GlobalLock(resource); |
| 169 |
if(write) { |
| 170 |
wcscpy(write, output); |
| 171 |
GlobalUnlock(write); |
| 172 |
if(SetClipboardData(CF_UNICODETEXT, resource) == FALSE) { |
| 173 |
GlobalFree(resource); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
CloseClipboard(); |
| 180 |
return false; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
/*case WM_GETMINMAXINFO: { |
| 189 |
MINMAXINFO* mmi = (MINMAXINFO*)lparam; |
| 190 |
mmi->ptMinTrackSize.x = 256 + window.p.frameMargin().width; |
| 191 |
mmi->ptMinTrackSize.y = 256 + window.p.frameMargin().height; |
| 192 |
return TRUE; |
| 193 |
break; |
| 194 |
}*/ |
| 195 |
|
| 196 |
static LRESULT CALLBACK Application_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { |
| 197 |
Object* object = (Object*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 198 |
if(object == nullptr) return DefWindowProc(hwnd, msg, wparam, lparam); |
| 199 |
Window& window = dynamic_cast<Window*>(object) ? (Window&)*object : *((Widget*)object)->Sizable::state.window; |
| 200 |
|
| 201 |
bool process = true; |
| 202 |
if(!pWindow::modal.empty() && !pWindow::modal.find(&window.p)) process = false; |
| 203 |
if(applicationState.quit) process = false; |
| 204 |
if(process == false) return DefWindowProc(hwnd, msg, wparam, lparam); |
| 205 |
|
| 206 |
switch(msg) { |
| 207 |
case WM_CLOSE: window.p.onClose(); return TRUE; |
| 208 |
case WM_MOVE: window.p.onMove(); break; |
| 209 |
case WM_SIZE: window.p.onSize(); break; |
| 210 |
case WM_DROPFILES: window.p.onDrop(wparam); return FALSE; |
| 211 |
case WM_ERASEBKGND: if(window.p.onEraseBackground()) return true; break; |
| 212 |
case WM_ENTERMENULOOP: case WM_ENTERSIZEMOVE: window.p.onModalBegin(); return FALSE; |
| 213 |
case WM_EXITMENULOOP: case WM_EXITSIZEMOVE: window.p.onModalEnd(); return FALSE; |
| 214 |
} |
| 215 |
|
| 216 |
return Shared_windowProc(DefWindowProc, hwnd, msg, wparam, lparam); |
| 217 |
} |
| 218 |
|
| 219 |
} |