From 5e5df97642a3e6b89059ab09e760ae483584ea58 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 17 Apr 2015 10:40:52 -0400 Subject: [PATCH] Migrated windows/button.c. This might be better; I don't know yet... --- ui_windows.h | 4 ---- windows/button.c | 23 ++++++++++++----------- windows/uipriv_windows.h | 5 +++++ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ui_windows.h b/ui_windows.h index 806cac15..f249ae15 100644 --- a/ui_windows.h +++ b/ui_windows.h @@ -7,10 +7,6 @@ This file assumes that you have included and "ui.h" beforehand. It p #ifndef __UI_UI_WINDOWS_H__ #define __UI_UI_WINDOWS_H__ -// Correctness macros. -#define uiControlHWND(c) ((HWND) uiControlHandle(c)) -#define uiParentHWND(p) ((HWND) uiParentHandle(p)) - // uiWindowsNewControl() initializes the given uiControl with the given Windows API control inside. // You will need to provide the preferredSize() method yourself. typedef struct uiWindowsNewControlParams uiWindowsNewControlParams; diff --git a/windows/button.c b/windows/button.c index fd7afa3d..42a5f0e7 100644 --- a/windows/button.c +++ b/windows/button.c @@ -3,6 +3,7 @@ struct button { uiButton b; + HWND hwnd; void (*onClicked)(uiButton *, void *); void *onClickedData; }; @@ -35,15 +36,13 @@ static void onWM_DESTROY(uiControl *c) static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height) { - HWND hwnd; + struct button *b = (struct button *) c; SIZE size; - hwnd = uiControlHWND(c); - // try the comctl32 version 6 way size.cx = 0; // explicitly ask for ideal size size.cy = 0; - if (SendMessageW(hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) { + if (SendMessageW(b->hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) { *width = size.cx; *height = size.cy; return; @@ -52,7 +51,7 @@ static void preferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t * // that didn't work; fall back to using Microsoft's metrics // Microsoft says to use a fixed width for all buttons; this isn't good enough // use the text width instead, with some edge padding - *width = uiWindowsWindowTextWidth(hwnd) + (2 * GetSystemMetrics(SM_CXEDGE)); + *width = uiWindowsWindowTextWidth(b->hwnd) + (2 * GetSystemMetrics(SM_CXEDGE)); *height = uiDlgUnitsToY(buttonHeight, d->sys->baseY); } @@ -61,17 +60,17 @@ static void defaultOnClicked(uiButton *b, void *data) // do nothing } -static char *getText(uiButton *b) +static char *buttonText(uiButton *b) { return uiWindowsControlText(uiControl(b)); } -static void setText(uiButton *b, const char *text) +static void buttonSetText(uiButton *b, const char *text) { uiWindowsControlSetText(uiControl(b), text); } -static void setOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) +static void buttonOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data) { struct button *b = (struct button *) bb; @@ -100,13 +99,15 @@ uiButton *uiNewButton(const char *text) uiWindowsNewControl(uiControl(b), &p); uiFree(wtext); + b->hwnd = HWND(b); + b->onClicked = defaultOnClicked; uiControl(b)->PreferredSize = preferredSize; - uiButton(b)->Text = getText; - uiButton(b)->SetText = setText; - uiButton(b)->OnClicked = setOnClicked; + uiButton(b)->Text = buttonText; + uiButton(b)->SetText = buttonSetText; + uiButton(b)->OnClicked = buttonOnClicked; return uiButton(b); } diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index 4ba0f5af..eb06a0a4 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -36,6 +36,11 @@ enum { msgUpdateChild, // fake because Windows seems to SWP_NOSIZE MoveWindow()s and SetWindowPos()s that don't change the window size (even if SWP_NOSIZE isn't specified) }; +#define HWND(c) ((HWND) uiControlHandle(uiControl(c))) +// TODO get rid of this +#define uiControlHWND(c) HWND(c) +#define uiParentHWND(p) ((HWND) uiParentHandle(p)) + // debug_windows.c extern HRESULT logLastError(const char *); extern HRESULT logHRESULT(const char *, HRESULT);