From 1ada1b346c79d485869afff992587016d175ee15 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 22 Apr 2016 19:51:33 -0400 Subject: [PATCH] Migrated resize.c. --- windows/init.cpp | 3 -- windows/{resize.c => resize.cpp} | 75 ++++++++++---------------------- windows/ui_windows_new.h | 8 ++++ windows/uipriv_windows_new.hpp | 4 ++ windows/winutil.cpp | 20 +++++++++ 5 files changed, 56 insertions(+), 54 deletions(-) rename windows/{resize.c => resize.cpp} (55%) diff --git a/windows/init.cpp b/windows/init.cpp index a2492e0d..b5ba4e9b 100644 --- a/windows/init.cpp +++ b/windows/init.cpp @@ -67,8 +67,6 @@ const char *uiInit(uiInitOptions *o) initAlloc(); - initResizes(); - nCmdShow = SW_SHOWDEFAULT; GetStartupInfoW(&si); if ((si.dwFlags & STARTF_USESHOWWINDOW) != 0) @@ -153,7 +151,6 @@ void uiUninit(void) unregisterWindowClass(); // no need to delete the default icon or cursor; see http://stackoverflow.com/questions/30603077/ uninitUtilWindow(); - uninitResizes(); uninitTypes(); uninitAlloc(); } diff --git a/windows/resize.c b/windows/resize.cpp similarity index 55% rename from windows/resize.c rename to windows/resize.cpp index abb84647..7e6572b3 100644 --- a/windows/resize.c +++ b/windows/resize.cpp @@ -1,38 +1,20 @@ // 14 may 2015 -#include "uipriv_windows.h" +#include "uipriv_windows.hpp" -static struct ptrArray *resizes; - -void initResizes(void) -{ - resizes = newPtrArray(); -} - -void uninitResizes(void) -{ - while (resizes->len != 0) - ptrArrayDelete(resizes, 0); - ptrArrayDestroy(resizes); -} +static std::map resizes; void uiWindowsControlQueueRelayout(uiWindowsControl *c) { uiControl *cc; uintmax_t i; - uiWindowsControl *d; // resizing a control requires us to reocmpute the sizes of everything in the top-level window + // TODO get rid of this call, make one for finding the root through the HWND cc = toplevelOwning(uiControl(c)); if (cc == NULL) return; c = uiWindowsControl(cc); - // make sure we're only queued once - for (i = 0 ; i < resizes->len; i++) { - d = ptrArrayIndex(resizes, uiWindowsControl *, i); - if (c == d) - return; - } - ptrArrayAppend(resizes, c); + resizes[c] = true; } void doResizes(void) @@ -41,40 +23,31 @@ void doResizes(void) HWND hwnd; RECT r; - while (resizes->len != 0) { - w = ptrArrayIndex(resizes, uiWindowsControl *, 0); + for (const auto &iter : resizes) { + w = iter.first; // don't clip content if content dynamically changed (tab page changed, etc.) // do this BEFORE removing w from the queue list to avoid double queueing ensureMinimumWindowSize(uiWindow(w)); - ptrArrayDelete(resizes, 0); hwnd = (HWND) uiControlHandle(uiControl(w)); - if (GetClientRect(hwnd, &r) == 0) - logLastError("error getting uiWindow client rect in doResizes()"); + if (GetClientRect(hwnd, &r) == 0) { + logLastError(L"error getting uiWindow client rect"); + // give up on this one; move on to the next one + continue; + } (*(w->Relayout))(w, r.left, r.top, r.right - r.left, r.bottom - r.top); // we used SWP_NOREDRAW; we need to queue a redraw ourselves // force all controls to be redrawn; this fixes things like the date-time picker's up-down not showing up until hovered over (and bypasses complications caused by WS_CLIPCHILDREN and WS_CLIPSIBLINGS, which we don't use but other controls might) if (RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN) == 0) - logLastError("error redrawing controls after a resize in doResizes()"); + logLastError(L"error redrawing controls after a resize"); // and keep going anyway } + // and wipe the list + resizes.clear(); } -void uiWindowsEnsureMoveWindow(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height) -{ - RECT r; - - r.left = x; - r.top = y; - r.right = x + width; - r.bottom = y + height; - if (SetWindowPos(hwnd, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOZORDER) == 0) - logLastError("error moving window in moveWindow()"); -} - -void setWindowInsertAfter(HWND hwnd, HWND insertAfter) -{ - if (SetWindowPos(hwnd, insertAfter, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE) == 0) - logLastError("error reordering window in setWindowInsertAfter()"); -} +/////////////////////// +// TODO REEVALUATE EVERYTHING BEYOND THIS POINT +// if we do have to keep, verify the error handling +/////////////////////// // from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing and https://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx // this X value is really only for buttons but I don't see a better one :/ @@ -93,25 +66,25 @@ uiWindowsSizing *uiWindowsNewSizing(HWND hwnd) dc = GetDC(hwnd); if (dc == NULL) - logLastError("error getting DC in uiWindowsNewSizing()"); + logLastError(L"error getting DC"); prevfont = (HFONT) SelectObject(dc, hMessageFont); if (prevfont == NULL) - logLastError("error loading control font into device context in uiWindowsNewSizing()"); + logLastError(L"error loading control font into device context"); ZeroMemory(&tm, sizeof (TEXTMETRICW)); if (GetTextMetricsW(dc, &tm) == 0) - logLastError("error getting text metrics in uiWindowsNewSizing()"); + logLastError(L"error getting text metrics"); if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0) - logLastError("error getting text extent point in uiWindowsNewSizing()"); + logLastError(L"error getting text extent point"); d->BaseX = (int) ((size.cx / 26 + 1) / 2); d->BaseY = (int) tm.tmHeight; d->InternalLeading = tm.tmInternalLeading; if (SelectObject(dc, prevfont) != hMessageFont) - logLastError("error restoring previous font into device context in uiWindowsSizing()"); + logLastError(L"error restoring previous font into device context"); if (ReleaseDC(hwnd, dc) == 0) - logLastError("error releasing DC in uiWindowsSizing()"); + logLastError(L"error releasing DC"); d->XPadding = uiWindowsDlgUnitsToX(winXPadding, d->BaseX); d->YPadding = uiWindowsDlgUnitsToY(winYPadding, d->BaseY); diff --git a/windows/ui_windows_new.h b/windows/ui_windows_new.h index 183ec924..093afa4a 100644 --- a/windows/ui_windows_new.h +++ b/windows/ui_windows_new.h @@ -19,6 +19,14 @@ _UI_EXTERN void uiWindowsSetWindowText(HWND hwnd, const char *text); // TODO document _UI_EXTERN intmax_t uiWindowsWindowTextWidth(HWND hwnd); +// TODO document +// TODO keep uiWindowsControl? +_UI_EXTERN void uiWindowsControlQueueRelayout(uiWindowsControl *c); + +// TODO document +// TODO point out this should only be used in a resize cycle +_UI_EXTERN void uiWindowsEnsureMoveWindowDuringResize(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height); + // TODO document _UI_EXTERN void uiWindowsRegisterWM_COMMANDHandler(HWND hwnd, BOOL (*handler)(uiControl *, HWND, WORD, LRESULT *), uiControl *c); _UI_EXTERN void uiWindowsUnregisterWM_COMMANDHandler(HWND hwnd); diff --git a/windows/uipriv_windows_new.hpp b/windows/uipriv_windows_new.hpp index 62a4a3d8..20ffb0f3 100644 --- a/windows/uipriv_windows_new.hpp +++ b/windows/uipriv_windows_new.hpp @@ -42,6 +42,7 @@ extern void setExStyle(HWND hwnd, DWORD exstyle); extern void clientSizeToWindowSize(HWND hwnd, intmax_t *width, intmax_t *height, BOOL hasMenubar); extern HWND parentOf(HWND child); extern HWND parentToplevel(HWND child); +extern void setWindowInsertAfter(HWND hwnd, HWND insertAfter); // text.cpp extern WCHAR *windowTextAndLen(HWND hwnd, LRESULT *len); @@ -67,3 +68,6 @@ extern void unregisterMessageFilter(void); // parent.cpp extern void paintContainerBackground(HWND hwnd, HDC dc, RECT *paintRect); extern BOOL handleParentMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *lResult); + +// resize.cpp +extern void doResizes(void); diff --git a/windows/winutil.cpp b/windows/winutil.cpp index 07051a0a..84efc074 100644 --- a/windows/winutil.cpp +++ b/windows/winutil.cpp @@ -134,3 +134,23 @@ HWND parentToplevel(HWND child) { return GetAncestor(child, GA_ROOT); } + +///////////// + +void uiWindowsEnsureMoveWindowDuringResize(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height) +{ + RECT r; + + r.left = x; + r.top = y; + r.right = x + width; + r.bottom = y + height; + if (SetWindowPos(hwnd, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOZORDER) == 0) + logLastError(L"error moving window"); +} + +void setWindowInsertAfter(HWND hwnd, HWND insertAfter) +{ + if (SetWindowPos(hwnd, insertAfter, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE) == 0) + logLastError(L"error reordering window"); +}