From 88393dcbc2cfad14c8201959a6f97e64dafb4471 Mon Sep 17 00:00:00 2001 From: Jonas Kümmerlin Date: Thu, 27 Dec 2018 17:06:56 +0100 Subject: add win2k and VC6 (with PSDK2003) support --- Makefile.mingw | 2 +- Makefile.vc6 | 13 +++++++ alf/alf.cpp | 95 ++++++++++++++++++++++++++++++++++++++------- alf/alf.h | 29 ++++++++++++-- widgetfactory.c | 111 ----------------------------------------------------- widgetfactory.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 234 insertions(+), 129 deletions(-) create mode 100644 Makefile.vc6 delete mode 100644 widgetfactory.c create mode 100644 widgetfactory.cpp diff --git a/Makefile.mingw b/Makefile.mingw index 1fa7276..3276c92 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -8,7 +8,7 @@ out/widgetfactory.exe: out/widgetfactory.o out/alf.o out/alf.o: alf/alf.cpp alf/alf.h $(CXX) $(CFLAGS) -c -o $@ $< -out/widgetfactory.o: widgetfactory.c alf/alf.h +out/widgetfactory.o: widgetfactory.cpp alf/alf.h $(CXX) $(CFLAGS) -c -o $@ $< clean: diff --git a/Makefile.vc6 b/Makefile.vc6 new file mode 100644 index 0000000..b5d8940 --- /dev/null +++ b/Makefile.vc6 @@ -0,0 +1,13 @@ +CXX = cl.exe +CFLAGS = -O2 -GA -W3 -DUNICODE -D_UNICODE -D_WIN32=0x0500 -D_WIN32_WINNT=0x0500 -D_WIN32_IE=0x0500 -nologo +LDFLAGS = /link kernel32.lib user32.lib comctl32.lib shell32.lib gdi32.lib version.lib + +out/widgetfactory.exe: out/widgetfactory.obj out/alf.obj + $(CXX) $(CFLAGS) -Fe$@ $** $(LDFLAGS) + +out/alf.obj: alf/alf.cpp alf/alf.h + $(CXX) $(CFLAGS) -c -Fo$@ alf/alf.cpp + +out/widgetfactory.obj: widgetfactory.cpp alf/alf.h + $(CXX) $(CFLAGS) -c -Fo$@ widgetfactory.cpp + diff --git a/alf/alf.cpp b/alf/alf.cpp index 9d7b55a..717f2df 100644 --- a/alf/alf.cpp +++ b/alf/alf.cpp @@ -6,6 +6,11 @@ #include #include +#ifdef _MSC_VER +// MSVC6 for scoping hack +# define for if(0){}else for +#endif + typedef struct ALFListHeader { struct ALFListHeader *prev; struct ALFListHeader *next; @@ -87,6 +92,7 @@ typedef struct { } ALFLayout; typedef struct { + ALFAPP app; ALFWindowVTable *vtbl; void *closure; ALFWindowFonts fonts; @@ -97,10 +103,18 @@ typedef struct { HWND hwndFocus; } ALFWindowPriv; +struct ALFAppPriv { + HINSTANCE hInstance; + ALFCompatFunctions *compatFn; +}; + +/* 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; @@ -436,6 +450,10 @@ ALF_DefWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) return 0; } + if (msg == ALF_WM_GETAPPLICATION) { + return (LRESULT)priv->app; + } + if (msg == WM_ACTIVATE) { if (!HIWORD(wparam)) { // if !minimized if (LOWORD(wparam)) { @@ -569,8 +587,42 @@ ALF_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM 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); + + ALFCompatFunctions *compatfn = (ALFCompatFunctions*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, sizeof(ALFCompatFunctions)); + +#define COMPAT(dll, entrypoint, ordinal) \ + do { \ + FARPROC p = GetProcAddress(GetModuleHandleA(#dll), #entrypoint); \ + if (!p && ordinal) \ + p = GetProcAddress(GetModuleHandleA(#dll), (char*)ordinal); \ + CopyMemory(&compatfn->entrypoint, &p, sizeof(void*)); \ + } while (0) + + COMPAT(comctl32.dll, SetWindowSubclass, 410); + COMPAT(comctl32.dll, DefSubclassProc, 413); + COMPAT(comctl32.dll, RemoveWindowSubclass, 412); + +#undef COMPAT + + app->compatFn = compatfn; + + return app; +} + LPTSTR -ALF_RegisterWindowClass(HINSTANCE hInstance, const ALFWindowClassParams *params) +ALF_RegisterWindowClass(ALFAPP app, const ALFWindowClassParams *params) { WNDCLASS cls; ZeroMemory(&cls, sizeof(cls)); @@ -578,12 +630,12 @@ ALF_RegisterWindowClass(HINSTANCE hInstance, const ALFWindowClassParams *params) // TODO: autogenerate class name cls.style = params->classStyle; - cls.hInstance = hInstance; + 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*); + cls.cbClsExtra = sizeof(void*)*2; cls.lpfnWndProc = DefWindowProc; ATOM classatom = RegisterClass(&cls); @@ -593,16 +645,16 @@ ALF_RegisterWindowClass(HINSTANCE hInstance, const ALFWindowClassParams *params) 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, hInstance, 0); + 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(HINSTANCE hInstance, LPCTSTR className, const ALFWindowInstanceParams *params) +HWND ALF_InstantiateWindow(ALFAPP app, LPCTSTR className, const ALFWindowInstanceParams* params) { return CreateWindowEx(params->windowExStyle, className, @@ -612,10 +664,16 @@ ALF_InstantiateWindow(HINSTANCE hInstance, LPCTSTR className, const ALFWindowIns 300, 100, //FIXME params->hwndParent, NULL, - hInstance, + app->hInstance, (void*)params); } +ALFAPP +ALF_ApplicationFromWindow(HWND hwnd) +{ + return (ALFAPP)SendMessage(hwnd, ALF_WM_GETAPPLICATION, 0, 0); +} + int ALF_ShowModal(HWND win) { @@ -717,6 +775,8 @@ ALF__LabelSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_ (void)uIdSubclass; (void)dwRefData; + ALFAPP app = (ALFAPP)dwRefData; + if (uMsg == ALF_WM_QUERYSIZE) { HDC hdcLabel = GetDC(hwnd); HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); @@ -756,7 +816,7 @@ ALF__LabelSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_ ReleaseDC(hwnd, hdcLabel); } - return DefSubclassProc(hwnd, uMsg, wParam, lParam); + return app->compatFn->DefSubclassProc(hwnd, uMsg, wParam, lParam); } HWND @@ -771,7 +831,8 @@ ALF_AddLabel(HWND win, WORD id, UINT x, UINT y, const WCHAR *text) (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE), NULL); - SetWindowSubclass(hwndLabel, ALF__LabelSubclassProc, 0, 0); + ALFAPP app = ALF_ApplicationFromWindow(win); + app->compatFn->SetWindowSubclass(hwndLabel, ALF__LabelSubclassProc, 0, (DWORD_PTR)app); ALFAddWidgetParams p; ZeroMemory(&p, sizeof(p)); @@ -796,6 +857,8 @@ ALF__EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_P (void)uIdSubclass; (void)dwRefData; + ALFAPP app = (ALFAPP)dwRefData; + if (uMsg == ALF_WM_QUERYSIZE) { HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); if (font) { @@ -816,7 +879,7 @@ ALF__EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_P return 0; } - return DefSubclassProc(hwnd, uMsg, wParam, lParam); + return app->compatFn->DefSubclassProc(hwnd, uMsg, wParam, lParam); } HWND @@ -832,7 +895,9 @@ ALF_AddEdit(HWND win, WORD id, UINT x, UINT y, const WCHAR *text) (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE), NULL); - SetWindowSubclass(hwndEdit, ALF__EditSubclassProc, 0, 0); + ALFAPP app = ALF_ApplicationFromWindow(win); + + app->compatFn->SetWindowSubclass(hwndEdit, ALF__EditSubclassProc, 0, (DWORD_PTR)app); ALFAddWidgetParams p; ZeroMemory(&p, sizeof(p)); @@ -855,6 +920,8 @@ ALF__ButtonSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT (void)uIdSubclass; (void)dwRefData; + ALFAPP app = (ALFAPP)dwRefData; + if (uMsg == ALF_WM_QUERYSIZE) { HDC hdc = GetDC(hwnd); HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); @@ -901,7 +968,7 @@ ALF__ButtonSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT ReleaseDC(hwnd, hdc); } - return DefSubclassProc(hwnd, uMsg, wParam, lParam); + return app->compatFn->DefSubclassProc(hwnd, uMsg, wParam, lParam); } HWND @@ -917,7 +984,9 @@ ALF_AddButton(HWND win, WORD id, UINT x, UINT y, const WCHAR *text) (HINSTANCE)GetWindowLongPtr(win, GWLP_HINSTANCE), NULL); - SetWindowSubclass(hwndButton, ALF__ButtonSubclassProc, 0, 0); + ALFAPP app = ALF_ApplicationFromWindow(win); + + app->compatFn->SetWindowSubclass(hwndButton, ALF__ButtonSubclassProc, 0, (DWORD_PTR)app); ALFAddWidgetParams p; ZeroMemory(&p, sizeof(p)); diff --git a/alf/alf.h b/alf/alf.h index b224538..d56afa5 100644 --- a/alf/alf.h +++ b/alf/alf.h @@ -42,7 +42,7 @@ typedef struct { #define ALF_WM_GETMODALRESULT (ALF_WM__BASE + 8) #define ALF_WM_CENTIPOINTTOPX (ALF_WM__BASE + 9) #define ALF_WM_SETFOCUS (ALF_WM__BASE + 10) - +#define ALF_WM_GETAPPLICATION (ALF_WM__BASE + 11) typedef struct { const WCHAR *className; @@ -67,14 +67,35 @@ typedef struct { UINT margins[4]; } ALFAddWidgetParams; +typedef LRESULT (CALLBACK *ALF_COMPAT_SUBCLASSPROC)(HWND,UINT,WPARAM,LPARAM,UINT_PTR,DWORD_PTR); +typedef struct { + LRESULT (WINAPI *SetWindowSubclass)(HWND, ALF_COMPAT_SUBCLASSPROC, UINT_PTR, DWORD_PTR); + LRESULT (WINAPI *DefSubclassProc)(HWND, UINT, WPARAM, LPARAM); + BOOL (WINAPI *RemoveWindowSubclass)(HWND, ALF_COMPAT_SUBCLASSPROC, UINT_PTR); +} ALFCompatFunctions; + +typedef struct ALFAppPriv *ALFAPP; + +ALFAPP +ALF_CreateApplication(HINSTANCE hInstance); + +void +ALF_TeardownApplication(ALFAPP app); + +const ALFCompatFunctions * +ALF_CompatFunctionTable(ALFAPP app); + LPTSTR -ALF_RegisterWindowClass(HINSTANCE hInstance, const ALFWindowClassParams *params); +ALF_RegisterWindowClass(ALFAPP app, const ALFWindowClassParams *params); void -ALF_UnregisterWindowClass(HINSTANCE hInstance, LPCTSTR className); +ALF_UnregisterWindowClass(ALFAPP app, LPCTSTR className); HWND -ALF_InstantiateWindow(HINSTANCE hInstance, LPCTSTR className, const ALFWindowInstanceParams *params); +ALF_InstantiateWindow(ALFAPP app, LPCTSTR className, const ALFWindowInstanceParams *params); + +ALFAPP +ALF_ApplicationFromWindow(HWND hwnd); int ALF_CentipointsToPixels(HWND win, int cptValue); diff --git a/widgetfactory.c b/widgetfactory.c deleted file mode 100644 index 94c063f..0000000 --- a/widgetfactory.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "alf/alf.h" - -enum { - ID_LBLHELLO, - ID_LBL2, - ID_LBL3, - ID_ED1, - ID_B1, - ID_B2, - ID__MAX -}; - -static HBRUSH red; -static HBRUSH green; -static HBRUSH blue; -static HBRUSH white; - -LRESULT -handleMessage(void *closure, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -{ - (void)closure; - - if (msg == WM_CTLCOLORSTATIC) { - DWORD id = GetDlgCtrlID((HWND)lparam); - HDC hdcStatic = (HDC)wparam; - if (id == ID_LBLHELLO) { - SetTextColor(hdcStatic, RGB(0,0,0)); - SetBkColor(hdcStatic, RGB(255,0,0)); - return (LRESULT)red; - } else if (id == ID_LBL2) { - SetTextColor(hdcStatic, RGB(0,0,0)); - SetBkColor(hdcStatic, RGB(0,255,0)); - return (LRESULT)green; - } /*else if (id == ID_LBL3) { - SetTextColor(hdcStatic, RGB(255,255,255)); - SetBkColor(hdcStatic, RGB(0,0,255)); - return (LRESULT)blue; - } else { - SetTextColor(hdcStatic, RGB(0,0,0)); - SetBkColor(hdcStatic, RGB(255,255,255)); - return (LRESULT)white; - }*/ - } - - return ALF_DefWindowProc(hwnd, msg, wparam, lparam); -} - -int CALLBACK -wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) -{ - (void)hPrevInstance; - (void)lpCmdLine; - (void)nCmdShow; - - BOOL (*pSetProcessDpiAware)(void); - pSetProcessDpiAware = (BOOL (*)(void))GetProcAddress(GetModuleHandle(L"user32.dll"), "SetProcessDPIAware"); - if (pSetProcessDpiAware) - pSetProcessDpiAware(); - - red = CreateSolidBrush(RGB(255,0,0)); - green = CreateSolidBrush(RGB(0,255,0)); - blue = CreateSolidBrush(RGB(0,0,255)); - white = CreateSolidBrush(RGB(255,255,255)); - - ALFWindowClassParams cparams; - ZeroMemory(&cparams, sizeof(cparams)); - - cparams.className = TEXT("DummyClass"); - cparams.vtbl.message = handleMessage; - - ALF_RegisterWindowClass(hInstance, &cparams); - - ALFWindowInstanceParams params; - ZeroMemory(¶ms, sizeof(params)); - params.windowStyle = WS_OVERLAPPEDWINDOW; - - HWND win = ALF_InstantiateWindow(hInstance, TEXT("DummyClass"), ¶ms); - - ALF_AddLabel(win, ID_LBL2, 1, 0, L"Hello, 2!\nblub"); - - HWND hwndLabel = CreateWindow( - L"STATIC", - L"Hello World!", - WS_CHILD | WS_VISIBLE | SS_LEFT, - 0, 0, 50, 25, - win, - (HMENU)ID_LBLHELLO, - hInstance, - NULL); - ALF_AddWidget(win, 0, 0, hwndLabel, 5000, 1000, ALF_MESSAGEFONT); - - ALF_AddLabel(win, ID_LBL3, 0, 1, L"Good Morning my &Angel!"); - - ALF_AddEdit(win, ID_ED1, 1, 1, L"Happy Birthday!"); - - ALF_AddButton(win, ID_B1, 2, 1, L"&Go!"); - ALF_AddButton(win, ID_B2, 0, 2, L"Oh m&y god,\r\nwho the hell cares?"); - - ALF_RecalculateLayout(win); - ALF_SetDefaultButton(win, ID_B1); - - //EnableWindow(ALF_WidgetHwndById(win, ID_LBL3), FALSE); - - ALF_ResizeWindow(win, 1, 1); - - ALF_ShowModal(win); - - DestroyWindow(win); - - return 0; -} diff --git a/widgetfactory.cpp b/widgetfactory.cpp new file mode 100644 index 0000000..478d57f --- /dev/null +++ b/widgetfactory.cpp @@ -0,0 +1,113 @@ +#include "alf/alf.h" + +enum { + ID_LBLHELLO, + ID_LBL2, + ID_LBL3, + ID_ED1, + ID_B1, + ID_B2, + ID__MAX +}; + +static HBRUSH red; +static HBRUSH green; +static HBRUSH blue; +static HBRUSH white; + +LRESULT +handleMessage(void *closure, HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) +{ + (void)closure; + + if (msg == WM_CTLCOLORSTATIC) { + DWORD id = GetDlgCtrlID((HWND)lparam); + HDC hdcStatic = (HDC)wparam; + if (id == ID_LBLHELLO) { + SetTextColor(hdcStatic, RGB(0,0,0)); + SetBkColor(hdcStatic, RGB(255,0,0)); + return (LRESULT)red; + } else if (id == ID_LBL2) { + SetTextColor(hdcStatic, RGB(0,0,0)); + SetBkColor(hdcStatic, RGB(0,255,0)); + return (LRESULT)green; + } /*else if (id == ID_LBL3) { + SetTextColor(hdcStatic, RGB(255,255,255)); + SetBkColor(hdcStatic, RGB(0,0,255)); + return (LRESULT)blue; + } else { + SetTextColor(hdcStatic, RGB(0,0,0)); + SetBkColor(hdcStatic, RGB(255,255,255)); + return (LRESULT)white; + }*/ + } + + return ALF_DefWindowProc(hwnd, msg, wparam, lparam); +} + +int CALLBACK +wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) +{ + (void)hPrevInstance; + (void)lpCmdLine; + (void)nCmdShow; + + BOOL (*pSetProcessDpiAware)(void); + pSetProcessDpiAware = (BOOL (*)(void))GetProcAddress(GetModuleHandle(L"user32.dll"), "SetProcessDPIAware"); + if (pSetProcessDpiAware) + pSetProcessDpiAware(); + + red = CreateSolidBrush(RGB(255,0,0)); + green = CreateSolidBrush(RGB(0,255,0)); + blue = CreateSolidBrush(RGB(0,0,255)); + white = CreateSolidBrush(RGB(255,255,255)); + + ALFWindowClassParams cparams; + ZeroMemory(&cparams, sizeof(cparams)); + + cparams.className = TEXT("DummyClass"); + cparams.vtbl.message = handleMessage; + + ALFAPP app = ALF_CreateApplication(hInstance); + + ALF_RegisterWindowClass(app, &cparams); + + ALFWindowInstanceParams params; + ZeroMemory(¶ms, sizeof(params)); + params.windowStyle = WS_OVERLAPPEDWINDOW; + + HWND win = ALF_InstantiateWindow(app, TEXT("DummyClass"), ¶ms); + + ALF_AddLabel(win, ID_LBL2, 1, 0, L"Hello, 2!\nblub"); + + HWND hwndLabel = CreateWindow( + L"STATIC", + L"Hello World!", + WS_CHILD | WS_VISIBLE | SS_LEFT, + 0, 0, 50, 25, + win, + (HMENU)ID_LBLHELLO, + hInstance, + NULL); + ALF_AddWidget(win, 0, 0, hwndLabel, 5000, 1000, ALF_MESSAGEFONT); + + ALF_AddLabel(win, ID_LBL3, 0, 1, L"Good Morning my &Angel!"); + + ALF_AddEdit(win, ID_ED1, 1, 1, L"Happy Birthday!"); + + ALF_AddButton(win, ID_B1, 2, 1, L"&Go!"); + ALF_AddButton(win, ID_B2, 0, 2, L"Oh m&y god,\r\nwho the hell cares?"); + + ALF_RecalculateLayout(win); + ALF_SetDefaultButton(win, ID_B1); + + //EnableWindow(ALF_WidgetHwndById(win, ID_LBL3), FALSE); + + ALF_ResizeWindow(win, 1, 1); + + ALF_ShowModal(win); + + DestroyWindow(win); + + return 0; +} -- cgit v1.2.3