ui/win32/window.c

changeset 112
c3f2f16fa4b8
parent 108
77254bd6dccb
child 113
dde28a806552
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
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 case WM_SIZE: {
73 int width = LOWORD(lParam);
74 int height = HIWORD(lParam);
75 if (widget->layout) {
76 widget->layout(widget->layoutmanager, width, height);
77 }
78 break;
79 }
80 default: return DefWindowProc(hwnd, uMsg, wParam, lParam);
51 } 81 }
52 return 0; 82 return 0;
53 } 83 }
54 84
55 void ui_window_init(void) { 85 void ui_window_init(void) {
58 WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; 88 WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
59 wc.lpfnWndProc = WindowProc; 89 wc.lpfnWndProc = WindowProc;
60 wc.hInstance = hInstance; 90 wc.hInstance = hInstance;
61 wc.lpszClassName = mainWindowClass; 91 wc.lpszClassName = mainWindowClass;
62 wc.hCursor = LoadCursor(NULL, IDC_ARROW); 92 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
63 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 93 wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
64 94
65 if(!RegisterClassExA(&wc)) { 95 if(!RegisterClassExA(&wc)) {
66 MessageBox(NULL, "RegisterClassEx failed", "Error", MB_ICONERROR); 96 MessageBox(NULL, "RegisterClassEx failed", "Error", MB_ICONERROR);
67 exit(-1); 97 exit(-1);
68 } 98 }
69 } 99 }
70 100
71 static UiObject* create_window(const char *title, void *window_data, bool simple) { 101 static UiObject* create_window(const char *title, void *window_data, bool simple) {
72 UiObject *obj = uic_object_new_toplevel(); 102 UiObject *obj = uic_object_new_toplevel();
73 obj->window = window_data; 103 obj->window = window_data;
74 104
75 HWND hwnd = CreateWindowExA( 105 HWND hwnd = CreateWindowExA(
76 0, 106 0,
77 "UiMainWindow", 107 "UiMainWindow",
78 title, 108 title,
79 WS_OVERLAPPEDWINDOW | WS_VISIBLE, 109 WS_OVERLAPPEDWINDOW,
80 CW_USEDEFAULT, 110 CW_USEDEFAULT,
81 CW_USEDEFAULT, 111 CW_USEDEFAULT,
82 800, 112 800,
83 600, 113 600,
84 NULL, 114 NULL,
85 NULL, 115 NULL,
86 hInstance, 116 hInstance,
87 NULL); 117 NULL);
88 118
89 ShowWindow(hwnd, SW_SHOWNORMAL);
90 UpdateWindow(hwnd); 119 UpdateWindow(hwnd);
91 120
121 UiContainerX *container = ui_box_container_create(obj, hwnd, UI_BOX_VERTICAL, 0, INSETS_ZERO);
122 uic_object_push_container(obj, container);
123 UiBoxContainer *box = (UiBoxContainer*)container;
124
125 UiWindow *widget = w32_widget_create(&w32_toplevel_widget_class, hwnd, sizeof(UiWindow));
126 widget->obj = obj;
127 widget->widget.layout = (W32LayoutFunc)ui_grid_layout;
128 widget->widget.layoutmanager = box->layout;
129 obj->widget = (W32Widget*)widget;
130 obj->ref = 1;
131
92 return obj; 132 return obj;
93 } 133 }
94 134
95 UiObject *ui_window(const char *title, void *window_data) { 135 UiObject *ui_window(const char *title, void *window_data) {
96 return create_window(title, window_data, FALSE); 136 return create_window(title, window_data, FALSE);
97 } 137 }
98 138
139
140 void ui_window_widget_event(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
141 //UiWindow *window = (UiWindow*)widget;
142 }
143
144 void ui_window_widget_show(W32Widget *w, BOOLEAN show) {
145 ShowWindow(w->hwnd, show ? SW_SHOWNORMAL : SW_HIDE);
146 }

mercurial