summaryrefslogtreecommitdiff
path: root/alf/alf.cpp
blob: e71e3a1456b20bd8f3bff1ab55bb7bcfa8267b31 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include "alfpriv.h"

/* ALF App and Window */

static void
ALF_InitializeWindowPriv(HWND hwnd, ALFWindowPriv *priv, ALFWindowInstanceParams *params)
{
    priv->vtbl = (ALFWindowVTable*)GetClassLongPtr(hwnd, 0);
    priv->app = (ALFAPP)GetClassLongPtr(hwnd, sizeof(void*));
    priv->closure = params->closure;
    ALF_ListInit(&priv->widgets);
    priv->defid = (WORD)-1;
}

static void
ALF_DestroyWindowPriv(ALFWindowPriv *priv)
{
    if (priv->vtbl->postdestroy)
        priv->vtbl->postdestroy(priv->closure);

    ALF_FOR_LIST(ALFWidgetPriv, list, &priv->widgets, w) {
        HeapFree(GetProcessHeap(), 0, w);
    }
    ALF_ListInit(&priv->widgets);

    HeapFree(GetProcessHeap(), 0, priv->layout.columns);
    HeapFree(GetProcessHeap(), 0, priv->layout.rows);
    HeapFree(GetProcessHeap(), 0, priv);
}

int
ALF_CentipointsToPxPriv(ALFWindowPriv *priv, int cptValue)
{
    return MulDiv(cptValue, priv->fonts.dpi, 7200);
}

static void
ALF_UpdateFontForWidget(ALFWindowPriv *priv, ALFWidgetPriv *widget)
{
    if (widget->hwnd && (widget->flags & ALF_MESSAGEFONT) == ALF_MESSAGEFONT) {
        SendMessage(widget->hwnd, WM_SETFONT, (WPARAM)priv->fonts.hMessageFont, (LPARAM)NULL);

        // XXX: Invalidating should IMHO be the decision of the control, but at
        // least the commctl32 V5 static control doesn't do it.
        InvalidateRect(widget->hwnd, NULL, TRUE);
    }
}

void
ALF_UpdateFonts(HWND win)
{
    ALFWindowPriv *priv = (ALFWindowPriv*)GetWindowLongPtr(win, 0);

    priv->fonts.dpi = priv->app->compatFn->GetDpiForWindow(win);

    NONCLIENTMETRICS ncm;
    ZeroMemory(&ncm, sizeof(ncm));
    ncm.cbSize = sizeof(ncm);

    if (priv->app->compatFn->SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0, priv->fonts.dpi)) {
        priv->fonts.lfMessageFont = ncm.lfMessageFont;
    } else {
        // FIXME! fallback to default font, 8pt MS Shell Dlg
        ZeroMemory(&priv->fonts.lfMessageFont, sizeof(priv->fonts.lfMessageFont));

        priv->fonts.lfMessageFont.lfHeight = -MulDiv(8, priv->fonts.dpi, 72);
        lstrcpyW(priv->fonts.lfMessageFont.lfFaceName, L"MS Shell Dlg");
    }

    if (priv->fonts.hMessageFont) {
        DeleteObject(priv->fonts.hMessageFont);
    }
    priv->fonts.hMessageFont = CreateFontIndirect(&priv->fonts.lfMessageFont);

    ALF_FOR_LIST(ALFWidgetPriv, list, &priv->widgets, i) {
        ALF_UpdateFontForWidget(priv, i);
    }

    if (priv->vtbl->updatefonts) {
        priv->vtbl->updatefonts(priv->closure, win, &priv->fonts);
    }
}

void
ALF_RecalculateLayout(HWND hwnd)
{
    SIZE dummy;
    SendMessage(hwnd, ALF_WM_QUERYSIZE, 0, (LPARAM)&dummy);
    SendMessage(hwnd, ALF_WM_APPLYLAYOUT, 0, 0);
}

static void
ALF_CalculateSizes(ALFWindowPriv *win)
{
    for (int i = 0; i < win->layout.nColumns; ++i) {
        ZeroMemory(&win->layout.columns[i], sizeof(win->layout.columns[i]));
    }
    for (int i = 0; i < win->layout.nRows; ++i) {
        ZeroMemory(&win->layout.rows[i], sizeof(win->layout.rows[i]));
    }

    ALF_FOR_LIST(ALFWidgetPriv, list, &win->widgets, c) {
        while ((int)c->x >= win->layout.nColumns) {
            // FIXME! overflow, use reallocarray(2) equivalent
            if (win->layout.nColumns == 0) {
                win->layout.nColumns = 1;
                win->layout.columns = (ALFRowOrColumn*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, win->layout.nColumns*sizeof(win->layout.columns[0]));
            } else {
                win->layout.nColumns *= 2;
                win->layout.columns = (ALFRowOrColumn*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, win->layout.columns, win->layout.nColumns*sizeof(win->layout.columns[0]));
            }
        }
        while ((int)c->y >= win->layout.nRows) {
            // FIXME! overflow, use reallocarray(2) equivalent
            if (win->layout.nRows == 0) {
                win->layout.nRows = 1;
                win->layout.rows = (ALFRowOrColumn*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, win->layout.nRows*sizeof(win->layout.rows[0]));
            } else {
                win->layout.nRows *= 2;
                win->layout.rows = (ALFRowOrColumn*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, win->layout.rows, win->layout.nRows*sizeof(win->layout.rows[0]));
            }
        }

        // TODO: ignore spanning cell

        SIZE qs = { ALF_CentipointsToPxPriv(win, c->cptWidth),
                    ALF_CentipointsToPxPriv(win, c->cptHeight) };
        if ((c->flags & ALF_QUERYSIZE) == ALF_QUERYSIZE) {
            SendMessage(c->hwnd, ALF_WM_QUERYSIZE, 0, (LPARAM)&qs);
        }

        qs.cx += ALF_CentipointsToPxPriv(win, c->cptMarginLeft);
        qs.cx += ALF_CentipointsToPxPriv(win, c->cptMarginRight);
        qs.cy += ALF_CentipointsToPxPriv(win, c->cptMarginTop);
        qs.cy += ALF_CentipointsToPxPriv(win, c->cptMarginBottom);

        if (qs.cx > win->layout.columns[c->x].minWidth)
            win->layout.columns[c->x].minWidth = qs.cx;
        if (qs.cy > win->layout.rows[c->y].minWidth)
            win->layout.rows[c->y].minWidth = qs.cy;

        // TODO: expand flag
    }

    // TODO: second pass for spanning cells

    // update min width bookkeeping
    win->layout.totalMinWidth = 0;
    win->layout.occupiedColumnCount = 0;
    for (int i = 0; i < win->layout.nColumns; ++i) {
        if (win->layout.columns[i].minWidth > 0) {
            win->layout.totalMinWidth += win->layout.columns[i].minWidth;
            win->layout.occupiedColumnCount++;
            // TODO: expand flag
        }
    }
    win->layout.totalMinHeight = 0;
    win->layout.occupiedRowCount = 0;
    for (int i = 0; i < win->layout.nRows; ++i) {
        if (win->layout.rows[i].minWidth > 0) {
            win->layout.totalMinHeight += win->layout.rows[i].minWidth;
            win->layout.occupiedRowCount++;
            // TODO: expand flag
        }
    }

    // TODO: split here into allocation function
}

static void
ALF_ApplyLayout(HWND hwnd, ALFWindowPriv *win)
{
    // allocate cell positions
    // distribute free space
    int extraWidth = 0;
    int extraHeight = 0;
    RECT client;
    if (GetClientRect(hwnd, &client)) {
        if (client.right - client.left > win->layout.totalMinWidth)
            extraWidth = client.right - client.left - win->layout.totalMinWidth;
        if (client.bottom - client.top > win->layout.totalMinHeight)
            extraHeight = client.bottom - client.top - win->layout.totalMinHeight;
    }

    int extraWidthPart = 0;
    int extraWidthError = 0;
    if (win->layout.occupiedColumnCount) {
        extraWidthPart = extraWidth / win->layout.occupiedColumnCount;
        extraWidthError = extraWidth - extraWidthPart * win->layout.occupiedColumnCount;
    }

    for (int i = 0; i < win->layout.nColumns; ++i) {
        win->layout.columns[i].allocatedWidth = win->layout.columns[i].minWidth;

        if (win->layout.columns[i].minWidth > 0) {
            win->layout.columns[i].allocatedWidth += extraWidthPart;
            if (extraWidthError) {
                win->layout.columns[i].allocatedWidth++;
                extraWidthError--;
            }
        }

        if (i == 0) {
            win->layout.columns[i].allocatedPosition = 0;
        } else {
            win->layout.columns[i].allocatedPosition =
                win->layout.columns[i-1].allocatedPosition + win->layout.columns[i-1].allocatedWidth;
        }
    }


    int extraHeightPart = 0;
    int extraHeightError = 0;
    if (win->layout.occupiedRowCount) {
        extraHeightPart = extraHeight / win->layout.occupiedRowCount;
        extraHeightError = extraHeight - extraHeightPart * win->layout.occupiedRowCount;
    }

    for (int i = 0; i < win->layout.nRows; ++i) {
        win->layout.rows[i].allocatedWidth = win->layout.rows[i].minWidth;

        if (win->layout.rows[i].minWidth > 0) {
            win->layout.rows[i].allocatedWidth += extraHeightPart;
            if (extraHeightError) {
                win->layout.rows[i].allocatedWidth++;
                extraHeightError--;
            }
        }

        if (i == 0) {
            win->layout.rows[i].allocatedPosition = 0;
        } else {
            win->layout.rows[i].allocatedPosition =
                win->layout.rows[i-1].allocatedPosition + win->layout.rows[i-1].allocatedWidth;
        }
    }

    HDWP hdwp = BeginDeferWindowPos(win->layout.occupiedColumnCount * win->layout.occupiedRowCount);

    ALF_FOR_LIST(ALFWidgetPriv, list, &win->widgets, c) {
        int marginleft   = ALF_CentipointsToPxPriv(win, c->cptMarginLeft);
        int marginright  = ALF_CentipointsToPxPriv(win, c->cptMarginRight);
        int margintop    = ALF_CentipointsToPxPriv(win, c->cptMarginTop);
        int marginbottom = ALF_CentipointsToPxPriv(win, c->cptMarginBottom);

        RECT r = { 0,0,0,0 };
        r.left = win->layout.columns[c->x].allocatedPosition + marginleft;
        r.right = r.left + win->layout.columns[c->x].allocatedWidth - marginleft - marginright;
        r.top = win->layout.rows[c->y].allocatedPosition + margintop;
        r.bottom = r.top + win->layout.rows[c->y].allocatedWidth - margintop - marginbottom;

        if (c->flags & ALF_APPLYSIZE) {
            hdwp = (HDWP)SendMessage(c->hwnd, ALF_WM_APPLYSIZE, (WPARAM)hdwp, (LPARAM)&r);
        } else {
            hdwp = DeferWindowPos(hdwp,
                                c->hwnd, 0,
                                r.left, r.top, r.right - r.left, r.bottom - r.top,
                                SWP_NOACTIVATE | SWP_NOZORDER);
        }
    }

    EndDeferWindowPos(hdwp);
}

static void
ALF_InternalAddWidget(HWND hwnd, ALFWindowPriv *priv, ALFAddWidgetParams *params)
{
    ALFWidgetPriv *w = (ALFWidgetPriv*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, sizeof(ALFWidgetPriv));
    w->hwnd = params->hwnd;
    w->x = params->x;
    w->y = params->y;
    w->cptWidth = params->width;
    w->cptHeight = params->height;
    w->flags = params->flags;
    w->cptMarginTop = params->margins[0];
    w->cptMarginRight = params->margins[1];
    w->cptMarginBottom = params->margins[2];
    w->cptMarginLeft = params->margins[3];

    if (GetParent(w->hwnd) != hwnd)
        SetParent(w->hwnd, hwnd);

    // TODO: QUERYUISTATE on parent, then replicate in child. But wait, XP appears to do this automatically!??

    ALF_ListInsert(priv->widgets.prev, &w->list);
    ALF_UpdateFontForWidget(priv, w);
}

static void
ALF_ApplyFocus(HWND hwnd, ALFWindowPriv *priv, BOOL restoringFocus)
{
    if (priv->hwndFocus) {
        if (!restoringFocus && SendMessage(priv->hwndFocus, WM_GETDLGCODE, 0, 0) & DLGC_HASSETSEL)
            SendMessage(priv->hwndFocus, EM_SETSEL, 0, -1);

        if (GetForegroundWindow() == hwnd)
            SetFocus(priv->hwndFocus);
    } else {
        priv->hwndFocus = GetNextDlgTabItem(hwnd, NULL, FALSE);
        if (priv->hwndFocus)
            ALF_ApplyFocus(hwnd, priv, FALSE);
    }
}

LRESULT
ALF_DefWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    ALFWindowPriv *priv = (ALFWindowPriv*)GetWindowLongPtr(hwnd, 0);

    if (msg == ALF_WM_QUERYSIZE) {
        ALF_CalculateSizes(priv);
        SIZE *ps = (SIZE*)lparam;
        ps->cx = priv->layout.totalMinWidth;
        ps->cy = priv->layout.totalMinHeight;
        return 0;
    }

    if (msg == ALF_WM_APPLYLAYOUT) {
        ALF_ApplyLayout(hwnd, priv);
        return 0;
    }

    if (msg == ALF_WM_GETMODALRESULT) {
        return (LRESULT)priv->modalResult;
    }
    if (msg == ALF_WM_SETMODALRESULT) {
        priv->modalResult = (int)wparam;
        return 0;
    }

    if (msg == ALF_WM_CENTIPOINTTOPX) {
        return (LRESULT)ALF_CentipointsToPxPriv(priv, (int)wparam);
    }

    if (msg == ALF_WM_ADDWIDGET) {
        ALF_InternalAddWidget(hwnd, priv, (ALFAddWidgetParams *)lparam);
        return 0;
    }

    if (msg == ALF_WM_SETFOCUS) {
        priv->hwndFocus = (HWND)lparam;
        ALF_ApplyFocus(hwnd, priv, FALSE);
        return 0;
    }

    if (msg == ALF_WM_GETAPPLICATION) {
        return (LRESULT)priv->app;
    }

    if (msg == WM_ACTIVATE) {
        if (!HIWORD(wparam)) { // if !minimized
            if (LOWORD(wparam)) {
                ALF_ApplyFocus(hwnd, priv, TRUE);
            } else {
                priv->hwndFocus = GetFocus();
            }
        }
        return 0;
    }

    if (msg == WM_SIZE) {
        ALF_ApplyLayout(hwnd, priv);
        return 0;
    }

    if (msg == WM_GETMINMAXINFO) {
        RECT tmp;
        ZeroMemory(&tmp, sizeof(tmp));
        tmp.right = priv->layout.totalMinWidth;
        tmp.bottom = priv->layout.totalMinHeight;

        if (priv->app->compatFn->AdjustWindowRectExForDpi(
                               &tmp,
                               GetWindowLong(hwnd, GWL_STYLE),
                               GetMenu(hwnd) != NULL,
                               GetWindowLong(hwnd, GWL_EXSTYLE),
                               priv->fonts.dpi)) {
            MINMAXINFO *i = (MINMAXINFO *)lparam;
            i->ptMinTrackSize.x = tmp.right - tmp.left;
            i->ptMinTrackSize.y = tmp.bottom - tmp.top;
            return 0;
        }
    }

    if (msg == WM_CREATE) {
        if (priv->vtbl->create) {
            priv->vtbl->create(priv->closure, hwnd);
        }
        BOOL alwaysUnderline = FALSE;
        SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &alwaysUnderline, 0);
        if (!alwaysUnderline) {
            SendMessage(hwnd, WM_UPDATEUISTATE, MAKEWPARAM(UIS_INITIALIZE, 0), 0);
        }
        ALF_UpdateFonts(hwnd);
        ALF_RecalculateLayout(hwnd);
    }

    if (msg == WM_DESTROY && priv->vtbl->destroy) {
        priv->vtbl->destroy(priv->closure, hwnd);
    }

    if (msg == WM_CLOSE) {
        if (!priv->vtbl->close || !priv->vtbl->close(priv->closure, hwnd)) {
            priv->modalResult = 2;
            ShowWindow(hwnd, FALSE);
        }
        return TRUE;
    }

    if (msg == WM_NCDESTROY) {
        SetWindowLongPtr(hwnd, 0, 0);
        ALF_DestroyWindowPriv(priv);
    }

    if (msg == WM_DPICHANGED) {
        ALF_UpdateFonts(hwnd);
        ALF_CalculateSizes(priv);
        RECT *r = (RECT*)lparam;
        SetWindowPos(hwnd, NULL, r->left, r->top, r->right-r->left, r->bottom-r->top, SWP_NOACTIVATE|SWP_NOZORDER);
    }

    if (msg == WM_THEMECHANGED || msg == WM_SETTINGCHANGE) {
        ALF_UpdateFonts(hwnd);
        ALF_RecalculateLayout(hwnd);
    }

    if (msg == DM_GETDEFID) {
        if (priv->defid == (WORD)-1) {
            return 0;
        } else {
            return MAKELONG(priv->defid, DC_HASDEFID);
        }
    }
    if (msg == DM_SETDEFID) {
        if (priv->defid != (WORD)-1) {
            HWND hwndOld = ALF_WidgetHwndById(hwnd, priv->defid);
            if (hwndOld && SendMessage(hwndOld, WM_GETDLGCODE, 0, 0) & DLGC_DEFPUSHBUTTON) {
                SendMessage(hwndOld, BM_SETSTYLE, BS_PUSHBUTTON, TRUE);
            }
        }

        priv->defid = (WORD)wparam;
        HWND hwndNew = ALF_WidgetHwndById(hwnd, priv->defid);
        if (hwndNew && SendMessage(hwndNew, WM_GETDLGCODE, 0, 0) & DLGC_UNDEFPUSHBUTTON) {
            SendMessage(hwndNew, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
        }
        return TRUE;
    }

    return DefWindowProc(hwnd, msg, wparam, lparam);
}

void
ALF_AddWidget(HWND win, UINT x, UINT y, HWND widget, UINT minWidth, UINT minHeight, DWORD flags)
{
    ALFAddWidgetParams params;
    ZeroMemory(&params, sizeof(params));
    params.hwnd = widget;
    params.x = x;
    params.y = y;
    params.width = minWidth;
    params.height = minHeight;
    params.flags = flags;
    ALF_AddWidgetEx(win, &params);
}

void
ALF_AddWidgetEx(HWND win, const ALFAddWidgetParams *p)
{
    SendMessage(win, ALF_WM_ADDWIDGET, 0, (LPARAM)p);
}

static LRESULT CALLBACK
ALF_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    if (msg == WM_NCCREATE) {
        CREATESTRUCT *cs = (CREATESTRUCT*)(void*)lparam;
        ALFWindowInstanceParams *params = (ALFWindowInstanceParams*)cs->lpCreateParams;
        ALFWindowPriv *priv = (ALFWindowPriv*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, sizeof(ALFWindowPriv));
        SetWindowLongPtr(hwnd, 0, (LONG_PTR)priv);
        ALF_InitializeWindowPriv(hwnd, priv, params);
    }

    ALFWindowPriv *priv = (ALFWindowPriv*)(void*)GetWindowLongPtr(hwnd, 0);
    if (priv != NULL) {
        if (priv->vtbl->message) {
            return priv->vtbl->message(priv->closure, hwnd, msg, wparam, lparam);
        } else {
            return ALF_DefWindowProc(hwnd, msg, wparam, lparam);
        }
    } else {
        return DefWindowProc(hwnd, msg, wparam, lparam);
    }
}

ALFAPP
ALF_CreateApplication(HINSTANCE hInstance)
{
    ALFAPP app = (ALFAPP)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, sizeof(struct ALFAppPriv));
    app->hInstance = hInstance;

    INITCOMMONCONTROLSEX icc;
    ZeroMemory(&icc, sizeof(icc));
    icc.dwSize = sizeof(icc);
    icc.dwICC = ICC_WIN95_CLASSES;

    InitCommonControlsEx(&icc);

    app->compatFn = ALF_CreateCompatFuncTable();

    return app;
}

LPTSTR
ALF_RegisterWindowClass(ALFAPP app, const ALFWindowClassParams *params)
{
    WNDCLASS cls;
    ZeroMemory(&cls, sizeof(cls));

    // TODO: autogenerate class name

    cls.style = params->classStyle;
    cls.hInstance = app->hInstance;
    cls.hCursor = LoadCursor(NULL, (LPTSTR)IDC_ARROW);
    cls.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    cls.lpszClassName = params->className;
    cls.cbWndExtra = sizeof(void*);
    cls.cbClsExtra = sizeof(void*)*2;
    cls.lpfnWndProc = DefWindowProc;

    ATOM classatom = RegisterClass(&cls);
    if (!classatom)
        return NULL;

    ALFWindowVTable *pvtbl = (ALFWindowVTable*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, sizeof(ALFWindowVTable));
    *pvtbl = params->vtbl;

    HWND tmp = CreateWindowEx(0, MAKEINTATOM(classatom), TEXT("dummy"), 0, 0, 0, 0, 0, HWND_MESSAGE, 0, app->hInstance, 0);
    SetClassLongPtr(tmp, 0, (LONG_PTR)pvtbl);
    SetClassLongPtr(tmp, sizeof(void*), (LONG_PTR)app);
    SetClassLongPtr(tmp, GCLP_WNDPROC, (LONG_PTR)ALF_WindowProc);
    DestroyWindow(tmp);

    return MAKEINTATOM(classatom);
}

HWND ALF_InstantiateWindow(ALFAPP app, LPCTSTR className, const ALFWindowInstanceParams* params)
{
    return CreateWindowEx(params->windowExStyle,
                          className,
                          L"Window",
                          params->windowStyle,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          300, 100, //FIXME
                          params->hwndParent,
                          NULL,
                          app->hInstance,
                          (void*)params);
}

ALFAPP
ALF_ApplicationFromWindow(HWND hwnd)
{
    return (ALFAPP)SendMessage(hwnd, ALF_WM_GETAPPLICATION, 0, 0);
}

int
ALF_ShowModal(HWND win)
{
    MSG msg;

    ALF_SetModalResult(win, 0);

    // TODO: disable parent window
    ShowWindow(win, SW_SHOW);

    while (GetMessage(&msg, NULL, 0, 0) > 0) {
        // TODO: call application message hooks
        // TODO: call preprocess message hook

        if (!IsDialogMessage(win, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        int mr = ALF_GetModalResult(win);
        if (mr)
            return mr;
    }

    return 0;
}


void
ALF_SetModalResult(HWND win, int result)
{
    SendMessage(win, ALF_WM_SETMODALRESULT, (WPARAM)result, 0);
}

int
ALF_GetModalResult(HWND win)
{
    return (int)SendMessage(win, ALF_WM_GETMODALRESULT, 0, 0);
}

int
ALF_CentipointsToPixels(HWND win, int cptValue)
{
    return (int)SendMessage(win, ALF_WM_CENTIPOINTTOPX, (WPARAM)cptValue, 0);
}

void
ALF_ResizeWindow(HWND win, int cptWidth, int cptHeight)
{
    int pxwidth = ALF_CentipointsToPixels(win, cptWidth);
    int pxheight = ALF_CentipointsToPixels(win, cptHeight);

    ALF_ResizeWindowPx(win, pxwidth, pxheight);
}

void
ALF_ResizeWindowPx(HWND win, int pxwidth, int pxheight)
{
    MINMAXINFO tmp = {
        { 0, 0 },
        { pxwidth, pxheight },
        { 0, 0 },
        { pxwidth, pxheight },
        { pxwidth, pxheight }
    };
    SendMessage(win, WM_GETMINMAXINFO, 0, (LPARAM)&tmp);

    if (tmp.ptMinTrackSize.x > pxwidth)
        pxwidth = tmp.ptMinTrackSize.x;
    if (tmp.ptMinTrackSize.y > pxheight)
        pxheight = tmp.ptMinTrackSize.y;

    SetWindowPos(win, NULL, 0, 0, pxwidth, pxheight, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
}

struct ALF_WidgetHwndById_Closure {
    HWND result;
    WORD needle;
};

static BOOL CALLBACK
ALF_WidgetHwndById_EnumChildProc(HWND hwnd, LPARAM lParam)
{
    struct ALF_WidgetHwndById_Closure *closure = (struct ALF_WidgetHwndById_Closure*)lParam;
    if ((WORD)GetWindowLongPtr(hwnd, GWLP_ID) == closure->needle) {
        closure->result = hwnd;
        return FALSE;
    }
    return TRUE;
}

HWND
ALF_WidgetHwndById(HWND win, WORD id)
{
    struct ALF_WidgetHwndById_Closure closure = { 0, id };
    EnumChildWindows(win, ALF_WidgetHwndById_EnumChildProc, (LPARAM)&closure);
    return closure.result;
}