summaryrefslogtreecommitdiff
path: root/alf/alflabel.cpp
diff options
context:
space:
mode:
authorJonas Kümmerlin <jonas@kuemmerlin.eu>2020-04-22 21:57:11 +0200
committerJonas Kümmerlin <jonas@kuemmerlin.eu>2020-04-22 21:57:11 +0200
commitd17d8f49777d802b56aa332077afbfa83af691e1 (patch)
tree2122f0c9fae3330a0943b17d61484a54e49e92ed /alf/alflabel.cpp
parent5c8711b522df31d7278aa9a3f05f5fa2349651b4 (diff)
label: refactor paint and size calculation into own functions
Diffstat (limited to 'alf/alflabel.cpp')
-rw-r--r--alf/alflabel.cpp235
1 files changed, 124 insertions, 111 deletions
diff --git a/alf/alflabel.cpp b/alf/alflabel.cpp
index adc70d4..37f1e83 100644
--- a/alf/alflabel.cpp
+++ b/alf/alflabel.cpp
@@ -56,6 +56,124 @@ ALF__Label_Text_DrawStateProc(HDC hdc, LPARAM lData, WPARAM wData, int cx, int c
return TRUE;
}
+static void
+ALF_Label_Paint(HWND hwnd, ALFLabelPriv *priv, HDC hdc, RECT *r)
+{
+ if (priv->bgcolor == ALF_COLOR_TRANSPARENT) {
+ ALF_Compat_DrawThemeParentBackground(hwnd, hdc, r);
+ } else {
+ ALF_FillRect(hdc, r, priv->bgcolor);
+ }
+
+ HFONT oldFont = SelectFont(hdc, priv->font);
+
+ SetTextColor(hdc, ALF_ColorToGdi(priv->textcolor));
+ SetBkMode(hdc, TRANSPARENT);
+
+ // calc drawtext style
+ UINT format = DT_EXPANDTABS;
+
+ LRESULT uiState = SendMessage(hwnd, WM_QUERYUISTATE, 0, 0);
+ if (uiState & UISF_HIDEACCEL)
+ format |= DT_HIDEPREFIX;
+
+ RECT rc;
+ GetClientRect(hwnd, &rc);
+
+ switch (priv->style & ALF_LABEL_HALIGN_MASK) {
+ case ALF_LABEL_ALIGN_HCENTER:
+ format |= DT_CENTER;
+ break;
+ case ALF_LABEL_ALIGN_RIGHT:
+ format |= DT_RIGHT;
+ break;
+ }
+ switch (priv->style & ALF_LABEL_VALIGN_MASK) {
+ case ALF_LABEL_ALIGN_BOTTOM:
+ format |= DT_BOTTOM;
+ break;
+ case ALF_LABEL_ALIGN_VCENTER:
+ format |= DT_VCENTER;
+ break;
+ }
+
+ if ((priv->style & ALF_LABEL_HALIGN_MASK) == ALF_LABEL_ALIGN_LEFT_LIKE_EDIT)
+ rc.left += ALF__LabelLeftPadding(hwnd, hdc);
+ if ((priv->style & ALF_LABEL_VALIGN_MASK) == ALF_LABEL_ALIGN_TOP_LIKE_EDIT)
+ rc.top += ALF__LabelTopPadding(hwnd);
+
+ TCHAR *text = ALF_Text(hwnd);
+
+ if (!IsWindowEnabled(hwnd)) {
+ if (!ALF_Compat_IsMinWindowsVersion(4, 0)) {
+ // Win32s/NT 3.51 uses gray text, but a different gray than Win9x
+ SetTextColor(hdc, GetSysColor(COLOR_BTNSHADOW));
+
+ DrawText(hdc, text, -1, &rc, format);
+ } else if (ALF_Compat_IsWin9x()) {
+ // Win9x just uses gray text. DSS_DISABLED is broken there, too,
+ // so we can't get the NT look even if we wanted to
+ SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
+
+ DrawText(hdc, text, -1, &rc, format);
+ } else {
+ // FIXME! This doesn't look good when using uxtheme, even though windows does it the same way
+ // need to investigate whether drawing with disabled button style looks nicer
+ DrawState(hdc, NULL,
+ ALF__Label_Text_DrawStateProc,
+ (LPARAM)text, (WPARAM)format,
+ rc.left,
+ rc.top,
+ rc.right - rc.left,
+ rc.bottom - rc.top,
+ DST_COMPLEX | DSS_DISABLED);
+ }
+ } else {
+ DrawText(hdc, text, -1, &rc, format);
+ }
+
+ ALF_Free(text);
+
+ SelectFont(hdc, oldFont);
+}
+
+static void
+ALF_Label_CalculateSize(HWND hwnd, ALFLabelPriv *priv, SIZE *pSize)
+{
+ int textlen = GetWindowTextLength(hwnd);
+ if (textlen) {
+ HDC hdcLabel = GetDC(hwnd);
+ HFONT oldFont = SelectFont(hdcLabel, priv->font);
+
+ // calc drawtext style
+ UINT format = DT_LEFT | DT_EXPANDTABS | DT_CALCRECT;
+
+ RECT r = { 0, 0, 100, 100 };
+
+ TCHAR *textbuf = ALF_New(TCHAR, (SIZE_T)textlen + 1);
+ GetWindowText(hwnd, textbuf, textlen+1);
+
+ DrawText(hdcLabel, textbuf, -1, &r, format);
+
+ ALF_Free(textbuf);
+
+ if (pSize->cx == 0) {
+ pSize->cx = r.right - r.left;
+ if ((priv->style & ALF_LABEL_HALIGN_MASK) == ALF_LABEL_ALIGN_LEFT_LIKE_EDIT)
+ pSize->cx += ALF__LabelLeftPadding(hwnd, hdcLabel);
+ }
+ if (pSize->cy == 0) {
+ pSize->cy = r.bottom - r.top;
+ if ((priv->style & ALF_LABEL_VALIGN_MASK) == ALF_LABEL_ALIGN_TOP_LIKE_EDIT)
+ pSize->cy += ALF__LabelTopPadding(hwnd);
+ }
+
+ SelectFont(hdcLabel, oldFont);
+
+ ReleaseDC(hwnd, hdcLabel);
+ }
+}
+
static LRESULT CALLBACK
ALF__LabelWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
@@ -105,123 +223,18 @@ ALF__LabelWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
- if (priv->bgcolor == ALF_COLOR_TRANSPARENT) {
- ALF_Compat_DrawThemeParentBackground(hwnd, hdc, &ps.rcPaint);
- } else {
- ALF_FillRect(hdc, &ps.rcPaint, priv->bgcolor);
- }
-
- HFONT oldFont = NULL;
- if (priv->font)
- oldFont = SelectFont(hdc, priv->font);
-
- SetTextColor(hdc, ALF_ColorToGdi(priv->textcolor));
- SetBkMode(hdc, TRANSPARENT);
-
- // calc drawtext style
- UINT format = DT_EXPANDTABS;
+ ALF_Label_Paint(hwnd, priv, hdc, &ps.rcPaint);
- LRESULT uiState = SendMessage(hwnd, WM_QUERYUISTATE, 0, 0);
- if (uiState & UISF_HIDEACCEL)
- format |= DT_HIDEPREFIX;
+ EndPaint(hwnd, &ps);
+ return TRUE;
+ } else if (uMsg == WM_PRINTCLIENT) {
RECT rc;
GetClientRect(hwnd, &rc);
- switch (priv->style & ALF_LABEL_HALIGN_MASK) {
- case ALF_LABEL_ALIGN_HCENTER:
- format |= DT_CENTER;
- break;
- case ALF_LABEL_ALIGN_RIGHT:
- format |= DT_RIGHT;
- break;
- }
- switch (priv->style & ALF_LABEL_VALIGN_MASK) {
- case ALF_LABEL_ALIGN_BOTTOM:
- format |= DT_BOTTOM;
- break;
- case ALF_LABEL_ALIGN_VCENTER:
- format |= DT_VCENTER;
- break;
- }
-
- if ((priv->style & ALF_LABEL_HALIGN_MASK) == ALF_LABEL_ALIGN_LEFT_LIKE_EDIT)
- rc.left += ALF__LabelLeftPadding(hwnd, hdc);
- if ((priv->style & ALF_LABEL_VALIGN_MASK) == ALF_LABEL_ALIGN_TOP_LIKE_EDIT)
- rc.top += ALF__LabelTopPadding(hwnd);
-
- TCHAR *text = ALF_Text(hwnd);
-
- if (!IsWindowEnabled(hwnd)) {
- if (!ALF_Compat_IsMinWindowsVersion(4, 0)) {
- // Win32s/NT 3.51 uses gray text, but a different gray than Win9x
- SetTextColor(hdc, GetSysColor(COLOR_BTNSHADOW));
-
- DrawText(hdc, text, -1, &rc, format);
- } else if (ALF_Compat_IsWin9x()) {
- // Win9x just uses gray text. DSS_DISABLED is broken there, too,
- // so we can't get the NT look even if we wanted to
- SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT));
-
- DrawText(hdc, text, -1, &rc, format);
- } else {
- // FIXME! This doesn't look good when using uxtheme, even though windows does it the same way
- // need to investigate whether drawing with disabled button style looks nicer
- DrawState(hdc, NULL,
- ALF__Label_Text_DrawStateProc,
- (LPARAM)text, (WPARAM)format,
- rc.left,
- rc.top,
- rc.right - rc.left,
- rc.bottom - rc.top,
- DST_COMPLEX | DSS_DISABLED);
- }
- } else {
- DrawText(hdc, text, -1, &rc, format);
- }
-
- ALF_Free(text);
-
- if (oldFont)
- SelectFont(hdc, oldFont);
-
- EndPaint(hwnd, &ps);
-
- return TRUE;
+ ALF_Label_Paint(hwnd, priv, (HDC)wParam, &rc);
} else if (uMsg == ALF_WM_QUERYSIZE) {
- int textlen = GetWindowTextLength(hwnd);
- SIZE *pSize = (SIZE*)(void*)lParam;
- if (textlen) {
- HDC hdcLabel = GetDC(hwnd);
- HFONT oldFont = SelectFont(hdcLabel, priv->font);
-
- // calc drawtext style
- UINT format = DT_LEFT | DT_EXPANDTABS | DT_CALCRECT;
-
- RECT r = { 0, 0, 100, 100 };
-
- TCHAR *textbuf = ALF_New(TCHAR, (SIZE_T)textlen + 1);
- GetWindowText(hwnd, textbuf, textlen+1);
-
- DrawText(hdcLabel, textbuf, -1, &r, format);
-
- ALF_Free(textbuf);
-
- if (pSize->cx == 0) {
- pSize->cx = r.right - r.left;
- if ((priv->style & ALF_LABEL_HALIGN_MASK) == ALF_LABEL_ALIGN_LEFT_LIKE_EDIT)
- pSize->cx += ALF__LabelLeftPadding(hwnd, hdcLabel);
- }
- if (pSize->cy == 0) {
- pSize->cy = r.bottom - r.top;
- if ((priv->style & ALF_LABEL_VALIGN_MASK) == ALF_LABEL_ALIGN_TOP_LIKE_EDIT)
- pSize->cy += ALF__LabelTopPadding(hwnd);
- }
-
- SelectFont(hdcLabel, oldFont);
-
- ReleaseDC(hwnd, hdcLabel);
- }
+ ALF_Label_CalculateSize(hwnd, priv, (SIZE*)lParam);
} else if (uMsg == WM_WINDOWPOSCHANGED) {
WINDOWPOS *p = (WINDOWPOS *)lParam;
if (!(p->flags & SWP_NOSIZE)) {