1 #include "x3.h"
2 #include "x3common.h"
3 #include <stdio.h> /* for printf only, probably remove in production */
4
5 HINSTANCE theInstance = NULL;
6
x3init_win32(HINSTANCE hInstance)7 void x3init_win32(HINSTANCE hInstance)
8 {
9 theInstance = hInstance;
10 }
11
x3widget_init(x3widget * w,const x3type * type)12 void x3widget_init(x3widget *w, const x3type *type)
13 {
14 w->type = type;
15 w->name = NULL;
16 w->parent = NULL;
17 w->var = x3winnone;
18 w->u.hwnd = NULL;
19 w->n_children = 0;
20 w->children = NULL;
21 }
22
x3WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)23 LRESULT CALLBACK x3WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
24 {
25 switch (iMsg) {
26 case WM_DESTROY:
27 if (--x3n_winopen <= 0) {
28 PostQuitMessage(0);
29 return 0;
30 }
31 break;
32 }
33 return DefWindowProc(hwnd, iMsg, wParam, lParam);
34 }
35
x3window_sizereq(x3widget * w)36 void x3window_sizereq(x3widget *w)
37 {
38 }
39
x3window_sizealloc(x3widget * w,x3rect * r)40 void x3window_sizealloc(x3widget *w, x3rect *r)
41 {
42 int i;
43 RECT rect;
44 x3rect child_r;
45
46 GetClientRect(w->u.hwnd, &rect);
47 child_r.x0 = 0;
48 child_r.x1 = rect.right - rect.left;
49 child_r.y0 = 0;
50 child_r.y1 = rect.bottom - rect.top;
51 printf("x3window_sizealloc (%ld, %ld) - (%ld, %ld)\n",
52 rect.left, rect.top, rect.right, rect.bottom);
53 for (i = 0; i < w->n_children; i++) {
54 x3widget *child = w->children[i];
55 if (child->type->sizealloc)
56 child->type->sizealloc(child, &child_r);
57 child->flags &= ~x3flag_needsizealloc;
58 }
59 }
60
61 x3type x3windowtype = { x3window_sizereq,
62 x3window_sizealloc,
63 x3add_default };
64
x3window(x3windowflags flags,char * label,x3window_callback callback,void * data)65 x3widget *x3window(x3windowflags flags, char *label,
66 x3window_callback callback, void *data)
67 {
68 HWND hwnd;
69 DWORD style = WS_OVERLAPPEDWINDOW;
70 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
71 WNDCLASSEX wndclass;
72
73 wndclass.cbSize = sizeof(wndclass);
74 wndclass.style = CS_HREDRAW | CS_VREDRAW;
75 wndclass.lpfnWndProc = x3WndProc;
76 wndclass.cbClsExtra = 0;
77 wndclass.cbWndExtra = 0;
78 wndclass.hInstance = theInstance;
79 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
80 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
81 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
82 wndclass.lpszMenuName = NULL;
83 wndclass.lpszClassName = "x3win";
84 wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
85
86 RegisterClassEx(&wndclass);
87
88 hwnd = CreateWindowEx(0, "x3win", "My window",
89 style, 100, 100, 300, 300,
90 NULL, NULL,
91 theInstance, NULL);
92 x3widget_init(result, &x3windowtype);
93 result->var = x3winhwnd;
94 result->u.hwnd = hwnd;
95 x3_nwinopen++;
96 //ShowWindow(hwnd, SW_SHOWNORMAL);
97 return result;
98
99 }
100
x3_window_show(x3widget * w)101 void x3_window_show(x3widget *w)
102 {
103 ShowWindow(w->u.hwnd, SW_SHOW);
104 }
105
x3hwnd_of(x3widget * w)106 static HWND x3hwnd_of(x3widget *w)
107 {
108 while (w->parent) w = w->parent;
109 return w->var == x3winhwnd ? w->u.hwnd : NULL;
110 }
111
x3widget_new_hwnd(x3widget * parent,char * name,const x3type * type,HWND hwnd)112 static x3widget *x3widget_new_hwnd(x3widget *parent, char *name,
113 const x3type *type,
114 HWND hwnd)
115 {
116 x3widget *result = (x3widget *)malloc(sizeof(x3widget));
117 x3widget_init(result, type);
118 result->name = name ? strdup(name) : NULL;
119 result->var = x3winhwnd;
120 result->u.hwnd = hwnd;
121 x3add(parent, result);
122 x3qsizereq(result);
123 return result;
124 }
125
x3button_sizereq(x3widget * w)126 void x3button_sizereq(x3widget *w)
127 {
128 w->sizerequest.x0 = 0;
129 w->sizerequest.y0 = 0;
130 w->sizerequest.x1 = 100;
131 w->sizerequest.y1 = 20;
132 #ifdef VERBOSE
133 printf("button sizereq = (%d, %d) - (%d, %d)\n",
134 w->sizerequest.x0, w->sizerequest.y0,
135 w->sizerequest.x1, w->sizerequest.y1);
136 #endif
137 }
138
x3button_sizealloc(x3widget * w,x3rect * r)139 void x3button_sizealloc(x3widget *w, x3rect *r)
140 {
141 printf("button sizealloc = (%g, %g) - (%g, %g)\n",
142 r->x0, r->y0, r->x1, r->y1);
143 if (w->var == x3winhwnd) {
144 SetWindowPos(w->u.hwnd, HWND_TOP,
145 r->x0, r->y0, r->x1 - r->x0, r->y1 - r->y0,
146 SWP_NOZORDER);
147 }
148 }
149
150 x3type x3buttontype = { x3button_sizereq,
151 x3button_sizealloc,
152 x3add_default };
153
x3button(x3widget * parent,char * cmd,char * label)154 x3widget *x3button(x3widget *parent, char *cmd, char *label)
155 {
156 HWND hwnd;
157
158 hwnd = CreateWindow("button", label,
159 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
160 10, 10, 100, 20, x3hwnd_of(parent), NULL,
161 theInstance, NULL);
162 return x3widget_new_hwnd(parent, cmd, &x3buttontype, hwnd);
163 }
164
x3main(void)165 void x3main(void)
166 {
167 MSG msg;
168
169 x3sync();
170 while (GetMessage(&msg, NULL, 0, 0)) {
171 TranslateMessage(&msg);
172 DispatchMessage(&msg);
173 }
174 }
175