#include "alfpriv.h" #include static LONG _alf_initLock = 0; static LONG _alf_initCounter = 0; void ALF_Initialize(void) { // acquire init lock while (InterlockedExchange(&_alf_initLock, 1)) { Sleep(0); } // we have the lock! if (!_alf_initCounter++) { InitCommonControls(); ALF_LoadCompatFunctions(); ALF_RegisterToplevelClass(); ALF_RegisterControlClass(); ALF_Compat_BufferedPaintInit(); } // release init lock InterlockedExchange(&_alf_initLock, 0); } void ALF_UnInitialize(void) { // acquire init lock while (InterlockedExchange(&_alf_initLock, 1)) { Sleep(0); } // we have the lock! if (!--_alf_initCounter) { ALF_Compat_BufferedPaintUnInit(); ALF_UnregisterToplevelClass(); UnregisterClass(_alf_controlClass, ALF_HINSTANCE); ALF_UnloadCompatFunctions(); } // release init lock InterlockedExchange(&_alf_initLock, 0); } void * ALF_Alloc(SIZE_T nmemb, SIZE_T size) { if (size && nmemb > (SIZE_T)-1 / size) RaiseException(STATUS_NO_MEMORY, 0, 0, NULL); return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, nmemb * size); } void * ALF_ReAlloc(void *ptr, SIZE_T nmemb, SIZE_T size) { if (size && nmemb > (SIZE_T)-1 / size) RaiseException(STATUS_NO_MEMORY, 0, 0, NULL); if (ptr) return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, ptr, nmemb * size); else return ALF_Alloc(nmemb, size); } void ALF_Free(const void *p) { HeapFree(GetProcessHeap(), 0, (void*)p); } int ALF_CentipointsToPixels(int cptValue, int dpi) { return MulDiv(cptValue, dpi, 7200); } int ALF_GetDpi(HWND window) { return (int)SendMessage(window, ALF_WM_GETDPI, 0, 0); } struct ALF_ControlHwndById_Closure { HWND result; WORD needle; }; static BOOL CALLBACK ALF_ControlHwndById_EnumChildProc(HWND hwnd, LPARAM lParam) { struct ALF_ControlHwndById_Closure *closure = (struct ALF_ControlHwndById_Closure*)lParam; if ((WORD)GetWindowLongPtr(hwnd, GWLP_ID) == closure->needle) { closure->result = hwnd; return FALSE; } return TRUE; } HWND ALF_ControlHwndById(HWND win, WORD id) { struct ALF_ControlHwndById_Closure closure = { 0, id }; EnumChildWindows(win, ALF_ControlHwndById_EnumChildProc, (LPARAM)&closure); return closure.result; } void ALF_SetText(HWND hwnd, const TCHAR *text) { SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)text); } TCHAR * // free with ALF_Free ALF_Text(HWND hwnd) { int len = GetWindowTextLength(hwnd); if (len > 0) { TCHAR *buf = ALF_New(TCHAR, (SIZE_T)len+1); if (GetWindowText(hwnd, buf, len+1)) { return buf; } else { ALF_Free(buf); } } return ALF_New(TCHAR, 1); } BOOL ALF_ShouldMessageBubble(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { (void)wparam; (void)lparam; if (!GetParent(hwnd)) return FALSE; return msg == ALF_WM_GETDPI || msg == WM_COMMAND || msg == WM_NOTIFY || msg == WM_MEASUREITEM || msg == WM_DRAWITEM || msg == WM_CTLCOLORBTN || msg == WM_CTLCOLOREDIT || msg == WM_CTLCOLORLISTBOX || msg == WM_CTLCOLORSCROLLBAR || msg == WM_CTLCOLORSTATIC || msg == WM_NEXTDLGCTL; } void ALF_FillRect(HDC dc, const RECT *rc, ALFColor color) { if (color == ALF_COLOR_TRANSPARENT) return; COLORREF gdicolor = ALF_ColorToGdi(color); COLORREF oldBkColor = SetBkColor(dc, gdicolor); ExtTextOut(dc, 0, 0, ETO_OPAQUE, rc, NULL, 0, NULL); SetBkColor(dc, oldBkColor); } COLORREF ALF_ColorToGdi(ALFColor color) { if (color == ALF_COLOR_TRANSPARENT) { return (COLORREF)-1; } else if (color & 0x80000000) { return GetSysColor((color & 0x7f000000) >> 24); } else { return (COLORREF)color; } } void ALF_InvalidateBackground(HWND win) { SendMessage(win, ALF_WM_BACKGROUNDCHANGE, 0, 0); } void ALF_SetBackgroundColor(HWND win, ALFColor color) { HWND parent = GetParent(win); if (parent) { ALF_Layout_RemoveControlFlag(parent, win, ALF_LAYOUT_INHERITBGCOLOR); } SendMessage(win, ALF_WM_SETBGCOLOR, 0, (LPARAM)color); } ALFColor ALF_BackgroundColor(HWND win) { return (ALFColor)SendMessage(win, ALF_WM_GETBGCOLOR, 0, 0); } ALFColor ALF_TextColor(HWND win) { return (ALFColor)SendMessage(win, ALF_WM_GETTEXTCOLOR, 0, 0); } void ALF_SetTextColor(HWND win, ALFColor color) { SendMessage(win, ALF_WM_SETTEXTCOLOR, 0, (LPARAM)color); } void ALF_SetFocus(HWND target) { if (GetWindowStyle(target) & WS_CHILD) SendMessage(GetParent(target), WM_NEXTDLGCTL, (WPARAM)target, TRUE); else SendMessage(target, WM_NEXTDLGCTL, (WPARAM)target, TRUE); } int ALF_MessageBox(HWND hwnd, const TCHAR *text, const TCHAR *caption, UINT type) { ALFApplication *app = NULL; if (GetWindowThreadProcessId(hwnd, NULL) == GetCurrentThreadId()) app = ALF_Toplevel_Application(hwnd); if (app) ALF_Application_DisableWindowsForModal(app, hwnd); int retval = MessageBox(hwnd, text, caption, type); if (app) ALF_Application_ReenableWindowsForModal(app, hwnd); return retval; }