1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "window.h"
30 #include <Windows.h>
31
32 #include "container.h"
33
34 #include "../common/object.h"
35
36
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 #include "win32.h"
42 #include "menu.h"
43
44 static W32WidgetClass w32_toplevel_widget_class = {
45 .eventproc = ui_window_widget_event,
46 .show = ui_window_widget_show,
47 .enable =
NULL,
48 .get_preferred_size =
NULL,
49 .destroy = w32_widget_default_destroy
50 };
51
52 static HINSTANCE hInstance;
53
54 static const char *mainWindowClass =
"UiMainWindow";
55
56
57 void ui_window_init(
void) {
58 hInstance = GetModuleHandle(
NULL);
59
60 WNDCLASSEX wc = {
sizeof(
WNDCLASSEX) };
61 wc.lpfnWndProc = ui_default_eventproc;
62 wc.hInstance = hInstance;
63 wc.lpszClassName = mainWindowClass;
64 wc.hCursor = LoadCursor(
NULL,
IDC_ARROW);
65 wc.hbrBackground = GetSysColorBrush(
COLOR_BTNFACE);
66
67 if(!RegisterClassExA(&wc)) {
68 MessageBox(
NULL,
"RegisterClassEx failed",
"Error",
MB_ICONERROR);
69 exit(
-1);
70 }
71 }
72
73 static UiObject* create_window(
const char *title, bool simple) {
74 UiObject *obj = uic_object_new_toplevel();
75
76 HWND hwnd = CreateWindowExA(
77 0,
78 "UiMainWindow",
79 title,
80 WS_OVERLAPPEDWINDOW,
81 CW_USEDEFAULT,
82 CW_USEDEFAULT,
83 800,
84 600,
85 NULL,
86 NULL,
87 hInstance,
88 NULL);
89
90 if (!simple) {
91 HMENU menubar = ui_create_main_menu(obj);
92 if (menubar) {
93 SetMenu(hwnd, menubar);
94 }
95 }
96
97 UpdateWindow(hwnd);
98
99 UiContainerX *container = ui_box_container_create(obj, hwnd,
UI_BOX_VERTICAL,
0,
INSETS_ZERO);
100 uic_object_push_container(obj, container);
101 UiBoxContainer *box = (UiBoxContainer*)container;
102
103 UiWindow *widget = w32_widget_create(&w32_toplevel_widget_class, hwnd,
sizeof(UiWindow));
104 widget->obj = obj;
105 widget->widget.layout = (W32LayoutFunc)ui_grid_layout;
106 widget->widget.layoutmanager = box->layout;
107 obj->widget = (W32Widget*)widget;
108 obj->ref =
1;
109
110 return obj;
111 }
112
113 UiObject *ui_window(
const char *title) {
114 return create_window(title, window_data,
FALSE);
115 }
116
117 UiObject *ui_simple_window(
const char *title) {
118 return create_window(title, window_data,
TRUE);
119 }
120
121 int ui_window_widget_event(W32Widget *widget,
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam) {
122
123 return 0;
124 }
125
126 void ui_window_widget_show(W32Widget *w,
BOOLEAN show) {
127 ShowWindow(w->hwnd, show ?
SW_SHOWNORMAL :
SW_HIDE);
128 }
129