| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
static const unsigned Windows2000 = 0x0500; |
| 4 |
static const unsigned WindowsXP = 0x0501; |
| 5 |
static const unsigned WindowsVista = 0x0600; |
| 6 |
static const unsigned Windows7 = 0x0601; |
| 7 |
|
| 8 |
static unsigned OsVersion() { |
| 9 |
OSVERSIONINFO versionInfo = {0}; |
| 10 |
versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 11 |
GetVersionEx(&versionInfo); |
| 12 |
return (versionInfo.dwMajorVersion << 8) + (versionInfo.dwMajorVersion << 0); |
| 13 |
} |
| 14 |
|
| 15 |
static HBITMAP CreateBitmap(const image& image) { |
| 16 |
HDC hdc = GetDC(0); |
| 17 |
BITMAPINFO bitmapInfo; |
| 18 |
memset(&bitmapInfo, 0, sizeof(BITMAPINFO)); |
| 19 |
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 20 |
bitmapInfo.bmiHeader.biWidth = image.width; |
| 21 |
bitmapInfo.bmiHeader.biHeight = -image.height; //bitmaps are stored upside down unless we negate height |
| 22 |
bitmapInfo.bmiHeader.biPlanes = 1; |
| 23 |
bitmapInfo.bmiHeader.biBitCount = 32; |
| 24 |
bitmapInfo.bmiHeader.biCompression = BI_RGB; |
| 25 |
bitmapInfo.bmiHeader.biSizeImage = image.width * image.height * 4; |
| 26 |
void* bits = nullptr; |
| 27 |
HBITMAP hbitmap = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0); |
| 28 |
if(bits) memcpy(bits, image.data, image.width * image.height * 4); |
| 29 |
ReleaseDC(0, hdc); |
| 30 |
return hbitmap; |
| 31 |
} |
| 32 |
|
| 33 |
static lstring DropPaths(WPARAM wparam) { |
| 34 |
auto dropList = HDROP(wparam); |
| 35 |
auto fileCount = DragQueryFile(dropList, ~0u, nullptr, 0); |
| 36 |
|
| 37 |
lstring paths; |
| 38 |
for(unsigned n = 0; n < fileCount; n++) { |
| 39 |
auto length = DragQueryFile(dropList, n, nullptr, 0); |
| 40 |
auto buffer = new wchar_t[length + 1]; |
| 41 |
|
| 42 |
if(DragQueryFile(dropList, n, buffer, length + 1)) { |
| 43 |
string path = (const char*)utf8_t(buffer); |
| 44 |
path.transform("\\", "/"); |
| 45 |
if(directory::exists(path) && !path.endsWith("/")) path.append("/"); |
| 46 |
paths.append(path); |
| 47 |
} |
| 48 |
|
| 49 |
delete[] buffer; |
| 50 |
} |
| 51 |
|
| 52 |
return paths; |
| 53 |
} |
| 54 |
|
| 55 |
static Layout* GetParentWidgetLayout(Sizable* sizable) { |
| 56 |
while(sizable) { |
| 57 |
if(sizable->state.parent && dynamic_cast<TabFrame*>(sizable->state.parent)) return (Layout*)sizable; |
| 58 |
sizable = sizable->state.parent; |
| 59 |
} |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
|
| 63 |
static Widget* GetParentWidget(Sizable* sizable) { |
| 64 |
while(sizable) { |
| 65 |
if(sizable->state.parent && dynamic_cast<TabFrame*>(sizable->state.parent)) return (Widget*)sizable->state.parent; |
| 66 |
sizable = sizable->state.parent; |
| 67 |
} |
| 68 |
return nullptr; |
| 69 |
} |
| 70 |
|
| 71 |
static unsigned GetWindowZOrder(HWND hwnd) { |
| 72 |
unsigned z = 0; |
| 73 |
for(HWND next = hwnd; next != NULL; next = GetWindow(next, GW_HWNDPREV)) z++; |
| 74 |
return z; |
| 75 |
} |
| 76 |
|
| 77 |
static void ImageList_Append(HIMAGELIST imageList, const nall::image& source, unsigned scale) { |
| 78 |
auto image = source; |
| 79 |
if(image.empty()) { |
| 80 |
image.allocate(scale, scale); |
| 81 |
image.fill(GetSysColor(COLOR_WINDOW)); |
| 82 |
} |
| 83 |
image.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0); |
| 84 |
image.scale(scale, scale); |
| 85 |
HBITMAP bitmap = CreateBitmap(image); |
| 86 |
ImageList_Add(imageList, bitmap, NULL); |
| 87 |
DeleteObject(bitmap); |
| 88 |
} |
| 89 |
|
| 90 |
static Keyboard::Keycode Keysym(unsigned keysym, unsigned keyflags) { |
| 91 |
#define pressed(keysym) (GetAsyncKeyState(keysym) & 0x8000) |
| 92 |
#define enabled(keysym) (GetKeyState(keysym)) |
| 93 |
#define shifted() (pressed(VK_LSHIFT) || pressed(VK_RSHIFT)) |
| 94 |
#define extended() (keyflags & (1 << 24)) |
| 95 |
|
| 96 |
switch(keysym) { |
| 97 |
case VK_ESCAPE: return Keyboard::Keycode::Escape; |
| 98 |
case VK_F1: return Keyboard::Keycode::F1; |
| 99 |
case VK_F2: return Keyboard::Keycode::F2; |
| 100 |
case VK_F3: return Keyboard::Keycode::F3; |
| 101 |
case VK_F4: return Keyboard::Keycode::F4; |
| 102 |
case VK_F5: return Keyboard::Keycode::F5; |
| 103 |
case VK_F6: return Keyboard::Keycode::F6; |
| 104 |
case VK_F7: return Keyboard::Keycode::F7; |
| 105 |
case VK_F8: return Keyboard::Keycode::F8; |
| 106 |
case VK_F9: return Keyboard::Keycode::F9; |
| 107 |
//Keyboard::Keycode::F10 (should be captured under VK_MENU from WM_SYSKEY(UP,DOWN); but this is not working...) |
| 108 |
case VK_F11: return Keyboard::Keycode::F11; |
| 109 |
case VK_F12: return Keyboard::Keycode::F12; |
| 110 |
|
| 111 |
//Keyboard::Keycode::PrintScreen |
| 112 |
//Keyboard::Keycode::SysRq |
| 113 |
case VK_SCROLL: return Keyboard::Keycode::ScrollLock; |
| 114 |
case VK_PAUSE: return Keyboard::Keycode::Pause; |
| 115 |
//Keyboard::Keycode::Break |
| 116 |
|
| 117 |
case VK_INSERT: return extended() ? Keyboard::Keycode::Insert : Keyboard::Keycode::KeypadInsert; |
| 118 |
case VK_DELETE: return extended() ? Keyboard::Keycode::Delete : Keyboard::Keycode::KeypadDelete; |
| 119 |
case VK_HOME: return extended() ? Keyboard::Keycode::Home : Keyboard::Keycode::KeypadHome; |
| 120 |
case VK_END: return extended() ? Keyboard::Keycode::End : Keyboard::Keycode::KeypadEnd; |
| 121 |
case VK_PRIOR: return extended() ? Keyboard::Keycode::PageUp : Keyboard::Keycode::KeypadPageUp; |
| 122 |
case VK_NEXT: return extended() ? Keyboard::Keycode::PageDown : Keyboard::Keycode::KeypadPageDown; |
| 123 |
|
| 124 |
case VK_UP: return extended() ? Keyboard::Keycode::Up : Keyboard::Keycode::KeypadUp; |
| 125 |
case VK_DOWN: return extended() ? Keyboard::Keycode::Down : Keyboard::Keycode::KeypadDown; |
| 126 |
case VK_LEFT: return extended() ? Keyboard::Keycode::Left : Keyboard::Keycode::KeypadLeft; |
| 127 |
case VK_RIGHT: return extended() ? Keyboard::Keycode::Right : Keyboard::Keycode::KeypadRight; |
| 128 |
|
| 129 |
case VK_OEM_3: return !shifted() ? Keyboard::Keycode::Grave : Keyboard::Keycode::Tilde; |
| 130 |
case '1': return !shifted() ? Keyboard::Keycode::Number1 : Keyboard::Keycode::Exclamation; |
| 131 |
case '2': return !shifted() ? Keyboard::Keycode::Number2 : Keyboard::Keycode::At; |
| 132 |
case '3': return !shifted() ? Keyboard::Keycode::Number3 : Keyboard::Keycode::Pound; |
| 133 |
case '4': return !shifted() ? Keyboard::Keycode::Number4 : Keyboard::Keycode::Dollar; |
| 134 |
case '5': return !shifted() ? Keyboard::Keycode::Number5 : Keyboard::Keycode::Percent; |
| 135 |
case '6': return !shifted() ? Keyboard::Keycode::Number6 : Keyboard::Keycode::Power; |
| 136 |
case '7': return !shifted() ? Keyboard::Keycode::Number7 : Keyboard::Keycode::Ampersand; |
| 137 |
case '8': return !shifted() ? Keyboard::Keycode::Number8 : Keyboard::Keycode::Asterisk; |
| 138 |
case '9': return !shifted() ? Keyboard::Keycode::Number9 : Keyboard::Keycode::ParenthesisLeft; |
| 139 |
case '0': return !shifted() ? Keyboard::Keycode::Number0 : Keyboard::Keycode::ParenthesisRight; |
| 140 |
case VK_OEM_MINUS: return !shifted() ? Keyboard::Keycode::Minus : Keyboard::Keycode::Underscore; |
| 141 |
case VK_OEM_PLUS: return !shifted() ? Keyboard::Keycode::Equal : Keyboard::Keycode::Plus; |
| 142 |
case VK_BACK: return Keyboard::Keycode::Backspace; |
| 143 |
|
| 144 |
case VK_OEM_4: return !shifted() ? Keyboard::Keycode::BracketLeft : Keyboard::Keycode::BraceLeft; |
| 145 |
case VK_OEM_6: return !shifted() ? Keyboard::Keycode::BracketRight : Keyboard::Keycode::BraceRight; |
| 146 |
case VK_OEM_5: return !shifted() ? Keyboard::Keycode::Backslash : Keyboard::Keycode::Pipe; |
| 147 |
case VK_OEM_1: return !shifted() ? Keyboard::Keycode::Semicolon : Keyboard::Keycode::Colon; |
| 148 |
case VK_OEM_7: return !shifted() ? Keyboard::Keycode::Apostrophe : Keyboard::Keycode::Quote; |
| 149 |
case VK_OEM_COMMA: return !shifted() ? Keyboard::Keycode::Comma : Keyboard::Keycode::CaretLeft; |
| 150 |
case VK_OEM_PERIOD: return !shifted() ? Keyboard::Keycode::Period : Keyboard::Keycode::CaretRight; |
| 151 |
case VK_OEM_2: return !shifted() ? Keyboard::Keycode::Slash : Keyboard::Keycode::Question; |
| 152 |
|
| 153 |
case VK_TAB: return Keyboard::Keycode::Tab; |
| 154 |
case VK_CAPITAL: return Keyboard::Keycode::CapsLock; |
| 155 |
case VK_RETURN: return !extended() ? Keyboard::Keycode::Return : Keyboard::Keycode::Enter; |
| 156 |
case VK_SHIFT: return !pressed(VK_RSHIFT) ? Keyboard::Keycode::ShiftLeft : Keyboard::Keycode::ShiftRight; |
| 157 |
case VK_CONTROL: return !pressed(VK_RCONTROL) ? Keyboard::Keycode::ControlLeft : Keyboard::Keycode::ControlRight; |
| 158 |
case VK_LWIN: return Keyboard::Keycode::SuperLeft; |
| 159 |
case VK_RWIN: return Keyboard::Keycode::SuperRight; |
| 160 |
case VK_MENU: |
| 161 |
if(keyflags & (1 << 24)) return Keyboard::Keycode::AltRight; |
| 162 |
return Keyboard::Keycode::AltLeft; |
| 163 |
case VK_SPACE: return Keyboard::Keycode::Space; |
| 164 |
case VK_APPS: return Keyboard::Keycode::Menu; |
| 165 |
|
| 166 |
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': |
| 167 |
case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': |
| 168 |
if(enabled(VK_CAPITAL)) { |
| 169 |
if(shifted()) { |
| 170 |
return (Keyboard::Keycode)((unsigned)Keyboard::Keycode::a + keysym - 'A'); |
| 171 |
} else { |
| 172 |
return (Keyboard::Keycode)((unsigned)Keyboard::Keycode::A + keysym - 'A'); |
| 173 |
} |
| 174 |
} else { |
| 175 |
if(shifted()) { |
| 176 |
return (Keyboard::Keycode)((unsigned)Keyboard::Keycode::A + keysym - 'A'); |
| 177 |
} else { |
| 178 |
return (Keyboard::Keycode)((unsigned)Keyboard::Keycode::a + keysym - 'A'); |
| 179 |
} |
| 180 |
} |
| 181 |
break; |
| 182 |
|
| 183 |
case VK_NUMLOCK: return Keyboard::Keycode::NumLock; |
| 184 |
case VK_DIVIDE: return Keyboard::Keycode::Divide; |
| 185 |
case VK_MULTIPLY: return Keyboard::Keycode::Multiply; |
| 186 |
case VK_SUBTRACT: return Keyboard::Keycode::Subtract; |
| 187 |
case VK_ADD: return Keyboard::Keycode::Add; |
| 188 |
case VK_DECIMAL: return Keyboard::Keycode::Point; |
| 189 |
case VK_NUMPAD1: return Keyboard::Keycode::Keypad1; |
| 190 |
case VK_NUMPAD2: return Keyboard::Keycode::Keypad2; |
| 191 |
case VK_NUMPAD3: return Keyboard::Keycode::Keypad3; |
| 192 |
case VK_NUMPAD4: return Keyboard::Keycode::Keypad4; |
| 193 |
case VK_NUMPAD5: return Keyboard::Keycode::Keypad5; |
| 194 |
case VK_NUMPAD6: return Keyboard::Keycode::Keypad6; |
| 195 |
case VK_NUMPAD7: return Keyboard::Keycode::Keypad7; |
| 196 |
case VK_NUMPAD8: return Keyboard::Keycode::Keypad8; |
| 197 |
case VK_NUMPAD9: return Keyboard::Keycode::Keypad9; |
| 198 |
case VK_NUMPAD0: return Keyboard::Keycode::Keypad0; |
| 199 |
|
| 200 |
case VK_CLEAR: return Keyboard::Keycode::KeypadCenter; |
| 201 |
} |
| 202 |
|
| 203 |
return Keyboard::Keycode::None; |
| 204 |
|
| 205 |
#undef pressed |
| 206 |
#undef enabled |
| 207 |
#undef shifted |
| 208 |
#undef extended |
| 209 |
} |
| 210 |
|
| 211 |
static unsigned ScrollEvent(HWND hwnd, WPARAM wparam) { |
| 212 |
SCROLLINFO info; |
| 213 |
memset(&info, 0, sizeof(SCROLLINFO)); |
| 214 |
info.cbSize = sizeof(SCROLLINFO); |
| 215 |
info.fMask = SIF_ALL; |
| 216 |
GetScrollInfo(hwnd, SB_CTL, &info); |
| 217 |
|
| 218 |
switch(LOWORD(wparam)) { |
| 219 |
case SB_LEFT: info.nPos = info.nMin; break; |
| 220 |
case SB_RIGHT: info.nPos = info.nMax; break; |
| 221 |
case SB_LINELEFT: info.nPos--; break; |
| 222 |
case SB_LINERIGHT: info.nPos++; break; |
| 223 |
case SB_PAGELEFT: info.nPos -= info.nMax >> 3; break; |
| 224 |
case SB_PAGERIGHT: info.nPos += info.nMax >> 3; break; |
| 225 |
case SB_THUMBTRACK: info.nPos = info.nTrackPos; break; |
| 226 |
} |
| 227 |
|
| 228 |
info.fMask = SIF_POS; |
| 229 |
SetScrollInfo(hwnd, SB_CTL, &info, TRUE); |
| 230 |
|
| 231 |
//Windows may clamp position to scroller range |
| 232 |
GetScrollInfo(hwnd, SB_CTL, &info); |
| 233 |
return info.nPos; |
| 234 |
} |
| 235 |
|
| 236 |
static LRESULT CALLBACK Shared_windowProc(WindowProc windowProc, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { |
| 237 |
Object* object = (Object*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 238 |
if(object == nullptr) return DefWindowProc(hwnd, msg, wparam, lparam); |
| 239 |
Window& window = dynamic_cast<Window*>(object) ? *(Window*)object : *((Widget*)object)->Sizable::state.window; |
| 240 |
|
| 241 |
bool process = true; |
| 242 |
if(!pWindow::modal.empty() && !pWindow::modal.find(&window.p)) process = false; |
| 243 |
if(applicationState.quit) process = false; |
| 244 |
if(process == false) return DefWindowProc(hwnd, msg, wparam, lparam); |
| 245 |
|
| 246 |
switch(msg) { |
| 247 |
case WM_CTLCOLORBTN: |
| 248 |
case WM_CTLCOLORSTATIC: { |
| 249 |
Object* object = (Object*)GetWindowLongPtr((HWND)lparam, GWLP_USERDATA); |
| 250 |
if(object == nullptr) break; |
| 251 |
if(dynamic_cast<HexEdit*>(object) || dynamic_cast<LineEdit*>(object) || dynamic_cast<TextEdit*>(object)) { |
| 252 |
//text edit controls, when disabled, use CTLCOLORSTATIC instead of CTLCOLOREDIT |
| 253 |
//override this behavior: we do not want read-only edit controls to use the parent window background color |
| 254 |
return windowProc(hwnd, WM_CTLCOLOREDIT, wparam, lparam); |
| 255 |
} else if(!GetParentWidget((Sizable*)object) && window.p.brush) { |
| 256 |
SetBkColor((HDC)wparam, window.p.brushColor); |
| 257 |
return (INT_PTR)window.p.brush; |
| 258 |
} |
| 259 |
break; |
| 260 |
} |
| 261 |
|
| 262 |
case WM_DRAWITEM: { |
| 263 |
unsigned id = LOWORD(wparam); |
| 264 |
HWND control = GetDlgItem(hwnd, id); |
| 265 |
Object* object = (Object*)GetWindowLongPtr(control, GWLP_USERDATA); |
| 266 |
if(object == nullptr) break; |
| 267 |
if(dynamic_cast<TabFrame*>(object)) { ((TabFrame*)object)->p.onDrawItem(lparam); return TRUE; } |
| 268 |
break; |
| 269 |
} |
| 270 |
|
| 271 |
case WM_COMMAND: { |
| 272 |
unsigned id = LOWORD(wparam); |
| 273 |
HWND control = GetDlgItem(hwnd, id); |
| 274 |
Object* object = control ? (Object*)GetWindowLongPtr(control, GWLP_USERDATA) : pObject::find(id); |
| 275 |
if(object == nullptr) break; |
| 276 |
if(dynamic_cast<Item*>(object)) { ((Item*)object)->p.onActivate(); return FALSE; } |
| 277 |
if(dynamic_cast<CheckItem*>(object)) { ((CheckItem*)object)->p.onToggle(); return FALSE; } |
| 278 |
if(dynamic_cast<RadioItem*>(object)) { ((RadioItem*)object)->p.onActivate(); return FALSE; } |
| 279 |
if(dynamic_cast<Button*>(object)) { ((Button*)object)->p.onActivate(); return FALSE; } |
| 280 |
if(dynamic_cast<CheckButton*>(object)) { ((CheckButton*)object)->p.onToggle(); return FALSE; } |
| 281 |
if(dynamic_cast<CheckLabel*>(object)) { ((CheckLabel*)object)->p.onToggle(); return FALSE; } |
| 282 |
if(dynamic_cast<ComboButton*>(object) && HIWORD(wparam) == CBN_SELCHANGE) { ((ComboButton*)object)->p.onChange(); return FALSE; } |
| 283 |
if(dynamic_cast<LineEdit*>(object) && HIWORD(wparam) == EN_CHANGE) { ((LineEdit*)object)->p.onChange(); return FALSE; } |
| 284 |
if(dynamic_cast<RadioButton*>(object)) { ((RadioButton*)object)->p.onActivate(); return FALSE; } |
| 285 |
if(dynamic_cast<RadioLabel*>(object)) { ((RadioLabel*)object)->p.onActivate(); return FALSE; } |
| 286 |
if(dynamic_cast<TextEdit*>(object) && HIWORD(wparam) == EN_CHANGE) { ((TextEdit*)object)->p.onChange(); return FALSE; } |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
case WM_NOTIFY: { |
| 291 |
unsigned id = LOWORD(wparam); |
| 292 |
HWND control = GetDlgItem(hwnd, id); |
| 293 |
Object* object = (Object*)GetWindowLongPtr(control, GWLP_USERDATA); |
| 294 |
if(object == nullptr) break; |
| 295 |
if(dynamic_cast<ListView*>(object) && ((LPNMHDR)lparam)->code == LVN_ITEMACTIVATE) { ((ListView*)object)->p.onActivate(lparam); break; } |
| 296 |
if(dynamic_cast<ListView*>(object) && ((LPNMHDR)lparam)->code == LVN_ITEMCHANGED) { ((ListView*)object)->p.onChange(lparam); break; } |
| 297 |
if(dynamic_cast<ListView*>(object) && ((LPNMHDR)lparam)->code == NM_CUSTOMDRAW) { return ((ListView*)object)->p.onCustomDraw(lparam); } |
| 298 |
if(dynamic_cast<TabFrame*>(object) && ((LPNMHDR)lparam)->code == TCN_SELCHANGE) { ((TabFrame*)object)->p.onChange(); break; } |
| 299 |
break; |
| 300 |
} |
| 301 |
|
| 302 |
case WM_APP + AppMessage::ListView_onActivate: { |
| 303 |
ListView* listView = (ListView*)lparam; |
| 304 |
if(listView && listView->onActivate) listView->onActivate(); |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
case WM_HSCROLL: |
| 309 |
case WM_VSCROLL: { |
| 310 |
Object* object = nullptr; |
| 311 |
if(lparam) { |
| 312 |
object = (Object*)GetWindowLongPtr((HWND)lparam, GWLP_USERDATA); |
| 313 |
} else { |
| 314 |
unsigned id = LOWORD(wparam); |
| 315 |
HWND control = GetDlgItem(hwnd, id); |
| 316 |
object = (Object*)GetWindowLongPtr(control, GWLP_USERDATA); |
| 317 |
} |
| 318 |
if(object == nullptr) break; |
| 319 |
if(dynamic_cast<HorizontalScroller*>(object)) { ((HorizontalScroller*)object)->p.onChange(wparam); return TRUE; } |
| 320 |
if(dynamic_cast<VerticalScroller*>(object)) { ((VerticalScroller*)object)->p.onChange(wparam); return TRUE; } |
| 321 |
if(dynamic_cast<HorizontalSlider*>(object)) { ((HorizontalSlider*)object)->p.onChange(); return TRUE; } |
| 322 |
if(dynamic_cast<VerticalSlider*>(object)) { ((VerticalSlider*)object)->p.onChange(); return TRUE; } |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return windowProc(hwnd, msg, wparam, lparam); |
| 328 |
} |
| 329 |
|
| 330 |
} |