| 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
| 27 */ |
27 */ |
| 28 |
28 |
| 29 #include "window.h" |
29 #include "window.h" |
| 30 #include "Windows.h" |
30 #include <Windows.h> |
| |
31 |
| |
32 #include "container.h" |
| 31 |
33 |
| 32 #include "../common/object.h" |
34 #include "../common/object.h" |
| 33 |
35 |
| 34 |
36 |
| 35 #include <stdbool.h> |
37 #include <stdbool.h> |
| 36 #include <stdio.h> |
38 #include <stdio.h> |
| 37 #include <stdlib.h> |
39 #include <stdlib.h> |
| 38 |
40 |
| |
41 #include "win32.h" |
| |
42 |
| |
43 static W32WidgetClass w32_toplevel_widget_class = { |
| |
44 .eventproc = ui_window_widget_event, |
| |
45 .show = ui_window_widget_show, |
| |
46 .enable = NULL, |
| |
47 .get_preferred_size = NULL, |
| |
48 .destroy = w32_widget_default_destroy |
| |
49 }; |
| 39 |
50 |
| 40 static HINSTANCE hInstance; |
51 static HINSTANCE hInstance; |
| 41 |
52 |
| 42 static const char *mainWindowClass = "UiMainWindow"; |
53 static const char *mainWindowClass = "UiMainWindow"; |
| 43 |
54 |
| 44 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
55 LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| |
56 W32Widget *widget = (W32Widget*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| |
57 if (widget && widget->wclass->eventproc) { |
| |
58 widget->wclass->eventproc(widget, hwnd, uMsg, wParam, lParam); |
| |
59 } |
| 45 switch(uMsg) { |
60 switch(uMsg) { |
| 46 case WM_DESTROY: |
61 case WM_DESTROY: { |
| 47 PostQuitMessage(0); |
62 PostQuitMessage(0); |
| 48 break; |
63 break; |
| 49 default: |
64 } |
| 50 return DefWindowProc(hwnd, uMsg, wParam, lParam); |
65 case WM_COMMAND: { |
| |
66 HWND hwndCtrl = (HWND)lParam; |
| |
67 W32Widget *cmdWidget = (W32Widget*)GetWindowLongPtr(hwndCtrl, GWLP_USERDATA); |
| |
68 if (cmdWidget && cmdWidget->wclass->eventproc) { |
| |
69 cmdWidget->wclass->eventproc(cmdWidget, hwnd, uMsg, wParam, lParam); |
| |
70 } |
| |
71 } |
| |
72 default: return DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 51 } |
73 } |
| 52 return 0; |
74 return 0; |
| 53 } |
75 } |
| 54 |
76 |
| 55 void ui_window_init(void) { |
77 void ui_window_init(void) { |
| 58 WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; |
80 WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; |
| 59 wc.lpfnWndProc = WindowProc; |
81 wc.lpfnWndProc = WindowProc; |
| 60 wc.hInstance = hInstance; |
82 wc.hInstance = hInstance; |
| 61 wc.lpszClassName = mainWindowClass; |
83 wc.lpszClassName = mainWindowClass; |
| 62 wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
84 wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 63 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); |
85 wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); |
| 64 |
86 |
| 65 if(!RegisterClassExA(&wc)) { |
87 if(!RegisterClassExA(&wc)) { |
| 66 MessageBox(NULL, "RegisterClassEx failed", "Error", MB_ICONERROR); |
88 MessageBox(NULL, "RegisterClassEx failed", "Error", MB_ICONERROR); |
| 67 exit(-1); |
89 exit(-1); |
| 68 } |
90 } |
| 69 } |
91 } |
| 70 |
92 |
| 71 static UiObject* create_window(const char *title, void *window_data, bool simple) { |
93 static UiObject* create_window(const char *title, void *window_data, bool simple) { |
| 72 UiObject *obj = uic_object_new_toplevel(); |
94 UiObject *obj = uic_object_new_toplevel(); |
| 73 obj->window = window_data; |
95 obj->window = window_data; |
| 74 |
96 |
| 75 HWND hwnd = CreateWindowExA( |
97 HWND hwnd = CreateWindowExA( |
| 76 0, |
98 0, |
| 77 "UiMainWindow", |
99 "UiMainWindow", |
| 78 title, |
100 title, |
| 79 WS_OVERLAPPEDWINDOW | WS_VISIBLE, |
101 WS_OVERLAPPEDWINDOW, |
| 80 CW_USEDEFAULT, |
102 CW_USEDEFAULT, |
| 81 CW_USEDEFAULT, |
103 CW_USEDEFAULT, |
| 82 800, |
104 800, |
| 83 600, |
105 600, |
| 84 NULL, |
106 NULL, |
| 85 NULL, |
107 NULL, |
| 86 hInstance, |
108 hInstance, |
| 87 NULL); |
109 NULL); |
| 88 |
110 |
| 89 ShowWindow(hwnd, SW_SHOWNORMAL); |
|
| 90 UpdateWindow(hwnd); |
111 UpdateWindow(hwnd); |
| 91 |
112 |
| |
113 UiContainerX *container = ui_box_container_create(obj, hwnd, UI_BOX_VERTICAL, 0, INSETS_ZERO); |
| |
114 uic_object_push_container(obj, container); |
| |
115 |
| |
116 UiWindow *widget = w32_widget_create(&w32_toplevel_widget_class, hwnd, sizeof(UiWindow)); |
| |
117 widget->obj = obj; |
| |
118 widget->container = (UiBoxContainer *)container; |
| |
119 obj->widget = (W32Widget*)widget; |
| |
120 obj->ref = 1; |
| |
121 |
| 92 return obj; |
122 return obj; |
| 93 } |
123 } |
| 94 |
124 |
| 95 UiObject *ui_window(const char *title, void *window_data) { |
125 UiObject *ui_window(const char *title, void *window_data) { |
| 96 return create_window(title, window_data, FALSE); |
126 return create_window(title, window_data, FALSE); |
| 97 } |
127 } |
| 98 |
128 |
| |
129 |
| |
130 void ui_window_widget_event(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| |
131 UiWindow *window = (UiWindow*)widget; |
| |
132 switch (uMsg) { |
| |
133 case WM_SIZE: { |
| |
134 int width = LOWORD(lParam); |
| |
135 int height = HIWORD(lParam); |
| |
136 ui_grid_layout(window->container->layout, width, height); |
| |
137 break; |
| |
138 } |
| |
139 } |
| |
140 } |
| |
141 |
| |
142 void ui_window_widget_show(W32Widget *w, BOOLEAN show) { |
| |
143 ShowWindow(w->hwnd, show ? SW_SHOWNORMAL : SW_HIDE); |
| |
144 } |