summaryrefslogtreecommitdiff
path: root/third_party/spiro/x3/x3win32.c
blob: 45e7d3ca7f9283f4ce60dc22017a63bbb4ec63c9 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "x3.h"
#include "x3common.h"
#include <stdio.h> /* for printf only, probably remove in production */

HINSTANCE theInstance = NULL;

void x3init_win32(HINSTANCE hInstance)
{
    theInstance = hInstance;
}

void x3widget_init(x3widget *w, const x3type *type)
{
    w->type = type;
    w->name = NULL;
    w->parent = NULL;
    w->var = x3winnone;
    w->u.hwnd = NULL;
    w->n_children = 0;
    w->children = NULL;
}

LRESULT CALLBACK x3WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch (iMsg) {
    case WM_DESTROY:
	if (--x3n_winopen <= 0) {
	    PostQuitMessage(0);
	    return 0;
	}
	break;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

void x3window_sizereq(x3widget *w)
{
}

void x3window_sizealloc(x3widget *w, x3rect *r)
{
    int i;
    RECT rect;
    x3rect child_r;

    GetClientRect(w->u.hwnd, &rect);
    child_r.x0 = 0;
    child_r.x1 = rect.right - rect.left;
    child_r.y0 = 0;
    child_r.y1 = rect.bottom - rect.top;
    printf("x3window_sizealloc (%ld, %ld) - (%ld, %ld)\n",
	   rect.left, rect.top, rect.right, rect.bottom);
    for (i = 0; i < w->n_children; i++) {
	x3widget *child = w->children[i];
	if (child->type->sizealloc)
	    child->type->sizealloc(child, &child_r);
	child->flags &= ~x3flag_needsizealloc;
    }
}

x3type x3windowtype = { x3window_sizereq,
			x3window_sizealloc,
			x3add_default };

x3widget *x3window(x3windowflags flags, char *label,
		   x3window_callback callback, void *data)
{
    HWND hwnd;
    DWORD style = WS_OVERLAPPEDWINDOW;
    x3widget *result = (x3widget *)malloc(sizeof(x3widget));
    WNDCLASSEX wndclass;

    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = x3WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = theInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = "x3win";
    wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&wndclass);

    hwnd = CreateWindowEx(0, "x3win", "My window",
			  style, 100, 100, 300, 300,
			  NULL, NULL,
			  theInstance, NULL);
    x3widget_init(result, &x3windowtype);
    result->var = x3winhwnd;
    result->u.hwnd = hwnd;
    x3_nwinopen++;
    //ShowWindow(hwnd, SW_SHOWNORMAL);
    return result;
			 
}

void x3_window_show(x3widget *w)
{
    ShowWindow(w->u.hwnd, SW_SHOW);
}

static HWND x3hwnd_of(x3widget *w)
{
    while (w->parent) w = w->parent;
    return w->var == x3winhwnd ? w->u.hwnd : NULL;
}

static x3widget *x3widget_new_hwnd(x3widget *parent, char *name,
				   const x3type *type,
				   HWND hwnd)
{
    x3widget *result = (x3widget *)malloc(sizeof(x3widget));
    x3widget_init(result, type);
    result->name = name ? strdup(name) : NULL;
    result->var = x3winhwnd;
    result->u.hwnd = hwnd;
    x3add(parent, result);
    x3qsizereq(result);
    return result;
}

void x3button_sizereq(x3widget *w)
{
    w->sizerequest.x0 = 0;
    w->sizerequest.y0 = 0;
    w->sizerequest.x1 = 100;
    w->sizerequest.y1 = 20;
#ifdef VERBOSE
    printf("button sizereq = (%d, %d) - (%d, %d)\n",
	   w->sizerequest.x0, w->sizerequest.y0,
	   w->sizerequest.x1, w->sizerequest.y1);
#endif
}

void x3button_sizealloc(x3widget *w, x3rect *r)
{
    printf("button sizealloc = (%g, %g) - (%g, %g)\n",
	   r->x0, r->y0, r->x1, r->y1);
    if (w->var == x3winhwnd) {
	SetWindowPos(w->u.hwnd, HWND_TOP,
		     r->x0, r->y0, r->x1 - r->x0, r->y1 - r->y0,
		     SWP_NOZORDER);
    }
}

x3type x3buttontype = { x3button_sizereq,
			x3button_sizealloc,
			x3add_default };

x3widget *x3button(x3widget *parent, char *cmd, char *label)
{
    HWND hwnd;

    hwnd = CreateWindow("button", label,
			WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
			10, 10, 100, 20, x3hwnd_of(parent), NULL,
			theInstance, NULL);
    return x3widget_new_hwnd(parent, cmd, &x3buttontype, hwnd);
}

void x3main(void)
{
    MSG msg;

    x3sync();
    while (GetMessage(&msg, NULL, 0, 0)) {
	TranslateMessage(&msg);
	DispatchMessage(&msg);
    }
}