Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Windows/Window.h |
michael@0 | 2 | |
michael@0 | 3 | #ifndef __WINDOWS_WINDOW_H |
michael@0 | 4 | #define __WINDOWS_WINDOW_H |
michael@0 | 5 | |
michael@0 | 6 | #include "Windows/Defs.h" |
michael@0 | 7 | #include "Common/String.h" |
michael@0 | 8 | |
michael@0 | 9 | namespace NWindows { |
michael@0 | 10 | |
michael@0 | 11 | inline ATOM MyRegisterClass(CONST WNDCLASS *wndClass) |
michael@0 | 12 | { return ::RegisterClass(wndClass); } |
michael@0 | 13 | |
michael@0 | 14 | #ifndef _UNICODE |
michael@0 | 15 | ATOM MyRegisterClass(CONST WNDCLASSW *wndClass); |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | #ifdef _UNICODE |
michael@0 | 19 | inline bool MySetWindowText(HWND wnd, LPCWSTR s) { return BOOLToBool(::SetWindowText(wnd, s)); } |
michael@0 | 20 | #else |
michael@0 | 21 | bool MySetWindowText(HWND wnd, LPCWSTR s); |
michael@0 | 22 | #endif |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | class CWindow |
michael@0 | 27 | { |
michael@0 | 28 | private: |
michael@0 | 29 | // bool ModifyStyleBase(int styleOffset, DWORD remove, DWORD add, UINT flags); |
michael@0 | 30 | protected: |
michael@0 | 31 | HWND _window; |
michael@0 | 32 | public: |
michael@0 | 33 | CWindow(HWND newWindow = NULL): _window(newWindow){}; |
michael@0 | 34 | CWindow& operator=(HWND newWindow) |
michael@0 | 35 | { |
michael@0 | 36 | _window = newWindow; |
michael@0 | 37 | return *this; |
michael@0 | 38 | } |
michael@0 | 39 | operator HWND() const { return _window; } |
michael@0 | 40 | void Attach(HWND newWindow) { _window = newWindow; } |
michael@0 | 41 | HWND Detach() |
michael@0 | 42 | { |
michael@0 | 43 | HWND window = _window; |
michael@0 | 44 | _window = NULL; |
michael@0 | 45 | return window; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | HWND GetParent() const { return ::GetParent(_window); } |
michael@0 | 49 | bool GetWindowRect(LPRECT rect) const { return BOOLToBool(::GetWindowRect(_window,rect )); } |
michael@0 | 50 | bool IsZoomed() const { return BOOLToBool(::IsZoomed(_window)); } |
michael@0 | 51 | bool ClientToScreen(LPPOINT point) const { return BOOLToBool(::ClientToScreen(_window, point)); } |
michael@0 | 52 | bool ScreenToClient(LPPOINT point) const { return BOOLToBool(::ScreenToClient(_window, point)); } |
michael@0 | 53 | |
michael@0 | 54 | bool CreateEx(DWORD exStyle, LPCTSTR className, |
michael@0 | 55 | LPCTSTR windowName, DWORD style, |
michael@0 | 56 | int x, int y, int width, int height, |
michael@0 | 57 | HWND parentWindow, HMENU idOrHMenu, |
michael@0 | 58 | HINSTANCE instance, LPVOID createParam) |
michael@0 | 59 | { |
michael@0 | 60 | _window = ::CreateWindowEx(exStyle, className, windowName, |
michael@0 | 61 | style, x, y, width, height, parentWindow, |
michael@0 | 62 | idOrHMenu, instance, createParam); |
michael@0 | 63 | return (_window != NULL); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | bool Create(LPCTSTR className, |
michael@0 | 67 | LPCTSTR windowName, DWORD style, |
michael@0 | 68 | int x, int y, int width, int height, |
michael@0 | 69 | HWND parentWindow, HMENU idOrHMenu, |
michael@0 | 70 | HINSTANCE instance, LPVOID createParam) |
michael@0 | 71 | { |
michael@0 | 72 | _window = ::CreateWindow(className, windowName, |
michael@0 | 73 | style, x, y, width, height, parentWindow, |
michael@0 | 74 | idOrHMenu, instance, createParam); |
michael@0 | 75 | return (_window != NULL); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | #ifndef _UNICODE |
michael@0 | 79 | bool Create(LPCWSTR className, |
michael@0 | 80 | LPCWSTR windowName, DWORD style, |
michael@0 | 81 | int x, int y, int width, int height, |
michael@0 | 82 | HWND parentWindow, HMENU idOrHMenu, |
michael@0 | 83 | HINSTANCE instance, LPVOID createParam); |
michael@0 | 84 | bool CreateEx(DWORD exStyle, LPCWSTR className, |
michael@0 | 85 | LPCWSTR windowName, DWORD style, |
michael@0 | 86 | int x, int y, int width, int height, |
michael@0 | 87 | HWND parentWindow, HMENU idOrHMenu, |
michael@0 | 88 | HINSTANCE instance, LPVOID createParam); |
michael@0 | 89 | #endif |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | bool Destroy() |
michael@0 | 93 | { |
michael@0 | 94 | if (_window == NULL) |
michael@0 | 95 | return true; |
michael@0 | 96 | bool result = BOOLToBool(::DestroyWindow(_window)); |
michael@0 | 97 | if(result) |
michael@0 | 98 | _window = NULL; |
michael@0 | 99 | return result; |
michael@0 | 100 | } |
michael@0 | 101 | bool IsWindow() { return BOOLToBool(::IsWindow(_window)); } |
michael@0 | 102 | bool Move(int x, int y, int width, int height, bool repaint = true) |
michael@0 | 103 | { return BOOLToBool(::MoveWindow(_window, x, y, width, height, BoolToBOOL(repaint))); } |
michael@0 | 104 | bool GetClientRect(LPRECT rect) { return BOOLToBool(::GetClientRect(_window, rect)); } |
michael@0 | 105 | bool Show(int cmdShow) { return BOOLToBool(::ShowWindow(_window, cmdShow)); } |
michael@0 | 106 | bool SetPlacement(CONST WINDOWPLACEMENT *placement) { return BOOLToBool(::SetWindowPlacement(_window, placement)); } |
michael@0 | 107 | bool GetPlacement(WINDOWPLACEMENT *placement) { return BOOLToBool(::GetWindowPlacement(_window, placement)); } |
michael@0 | 108 | bool Update() { return BOOLToBool(::UpdateWindow(_window)); } |
michael@0 | 109 | bool InvalidateRect(LPCRECT rect, bool backgroundErase = true) |
michael@0 | 110 | { return BOOLToBool(::InvalidateRect(_window, rect, BoolToBOOL(backgroundErase))); } |
michael@0 | 111 | void SetRedraw(bool redraw = true) { SendMessage(WM_SETREDRAW, BoolToBOOL(redraw), 0); } |
michael@0 | 112 | |
michael@0 | 113 | #ifndef _WIN32_WCE |
michael@0 | 114 | LONG SetStyle(LONG_PTR style) |
michael@0 | 115 | { return SetLongPtr(GWL_STYLE, style); } |
michael@0 | 116 | DWORD GetStyle( ) const |
michael@0 | 117 | { return GetLongPtr(GWL_STYLE); } |
michael@0 | 118 | #else |
michael@0 | 119 | LONG SetStyle(LONG_PTR style) |
michael@0 | 120 | { return SetLong(GWL_STYLE, style); } |
michael@0 | 121 | DWORD GetStyle( ) const |
michael@0 | 122 | { return GetLong(GWL_STYLE); } |
michael@0 | 123 | #endif |
michael@0 | 124 | |
michael@0 | 125 | LONG_PTR SetLong(int index, LONG_PTR newLongPtr ) |
michael@0 | 126 | { return ::SetWindowLong(_window, index, newLongPtr); } |
michael@0 | 127 | LONG_PTR GetLong(int index) const |
michael@0 | 128 | { return ::GetWindowLong(_window, index ); } |
michael@0 | 129 | LONG_PTR SetUserDataLong(LONG_PTR newLongPtr ) |
michael@0 | 130 | { return SetLong(GWLP_USERDATA, newLongPtr); } |
michael@0 | 131 | LONG_PTR GetUserDataLong() const |
michael@0 | 132 | { return GetLong(GWLP_USERDATA); } |
michael@0 | 133 | |
michael@0 | 134 | #ifndef _WIN32_WCE |
michael@0 | 135 | LONG_PTR SetLongPtr(int index, LONG_PTR newLongPtr ) |
michael@0 | 136 | { return ::SetWindowLongPtr(_window, index, newLongPtr); } |
michael@0 | 137 | #ifndef _UNICODE |
michael@0 | 138 | LONG_PTR SetLongPtrW(int index, LONG_PTR newLongPtr ) |
michael@0 | 139 | { return ::SetWindowLongPtrW(_window, index, newLongPtr); } |
michael@0 | 140 | #endif |
michael@0 | 141 | |
michael@0 | 142 | LONG_PTR GetLongPtr(int index) const |
michael@0 | 143 | { return ::GetWindowLongPtr(_window, index ); } |
michael@0 | 144 | LONG_PTR SetUserDataLongPtr(LONG_PTR newLongPtr ) |
michael@0 | 145 | { return SetLongPtr(GWLP_USERDATA, newLongPtr); } |
michael@0 | 146 | LONG_PTR GetUserDataLongPtr() const |
michael@0 | 147 | { return GetLongPtr(GWLP_USERDATA); } |
michael@0 | 148 | #endif |
michael@0 | 149 | |
michael@0 | 150 | /* |
michael@0 | 151 | bool ModifyStyle(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0) |
michael@0 | 152 | { return ModifyStyleBase(GWL_STYLE, remove, add, flags); } |
michael@0 | 153 | bool ModifyStyleEx(HWND hWnd, DWORD remove, DWORD add, UINT flags = 0) |
michael@0 | 154 | { return ModifyStyleBase(GWL_EXSTYLE, remove, add, flags); } |
michael@0 | 155 | */ |
michael@0 | 156 | |
michael@0 | 157 | HWND SetFocus() { return ::SetFocus(_window); } |
michael@0 | 158 | |
michael@0 | 159 | LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0) |
michael@0 | 160 | { return ::SendMessage(_window, message, wParam, lParam) ;} |
michael@0 | 161 | #ifndef _UNICODE |
michael@0 | 162 | LRESULT SendMessageW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0) |
michael@0 | 163 | { return ::SendMessageW(_window, message, wParam, lParam) ;} |
michael@0 | 164 | #endif |
michael@0 | 165 | |
michael@0 | 166 | bool PostMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0) |
michael@0 | 167 | { return BOOLToBool(::PostMessage(_window, message, wParam, lParam)) ;} |
michael@0 | 168 | #ifndef _UNICODE |
michael@0 | 169 | LRESULT PostMessageW(UINT message, WPARAM wParam = 0, LPARAM lParam = 0) |
michael@0 | 170 | { return ::PostMessageW(_window, message, wParam, lParam) ;} |
michael@0 | 171 | #endif |
michael@0 | 172 | |
michael@0 | 173 | bool SetText(LPCTSTR s) { return BOOLToBool(::SetWindowText(_window, s)); } |
michael@0 | 174 | #ifndef _UNICODE |
michael@0 | 175 | bool CWindow::SetText(LPCWSTR s) { return MySetWindowText(_window, s); } |
michael@0 | 176 | #endif |
michael@0 | 177 | |
michael@0 | 178 | int GetTextLength() const |
michael@0 | 179 | { return GetWindowTextLength(_window); } |
michael@0 | 180 | UINT GetText(LPTSTR string, int maxCount) const |
michael@0 | 181 | { return GetWindowText(_window, string, maxCount); } |
michael@0 | 182 | bool GetText(CSysString &s); |
michael@0 | 183 | #ifndef _UNICODE |
michael@0 | 184 | /* |
michael@0 | 185 | UINT GetText(LPWSTR string, int maxCount) const |
michael@0 | 186 | { return GetWindowTextW(_window, string, maxCount); } |
michael@0 | 187 | */ |
michael@0 | 188 | bool GetText(UString &s); |
michael@0 | 189 | #endif |
michael@0 | 190 | |
michael@0 | 191 | bool Enable(bool enable) |
michael@0 | 192 | { return BOOLToBool(::EnableWindow(_window, BoolToBOOL(enable))); } |
michael@0 | 193 | |
michael@0 | 194 | bool IsEnabled() |
michael@0 | 195 | { return BOOLToBool(::IsWindowEnabled(_window)); } |
michael@0 | 196 | |
michael@0 | 197 | #ifndef _WIN32_WCE |
michael@0 | 198 | HMENU GetSystemMenu(bool revert) |
michael@0 | 199 | { return ::GetSystemMenu(_window, BoolToBOOL(revert)); } |
michael@0 | 200 | #endif |
michael@0 | 201 | |
michael@0 | 202 | UINT_PTR SetTimer(UINT_PTR idEvent, UINT elapse, TIMERPROC timerFunc = 0) |
michael@0 | 203 | { return ::SetTimer(_window, idEvent, elapse, timerFunc); } |
michael@0 | 204 | bool KillTimer(UINT_PTR idEvent) |
michael@0 | 205 | {return BOOLToBool(::KillTimer(_window, idEvent)); } |
michael@0 | 206 | }; |
michael@0 | 207 | |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | #endif |
michael@0 | 211 |