diff --git a/common/general.h b/common/general.h new file mode 100644 index 00000000..c9253267 --- /dev/null +++ b/common/general.h @@ -0,0 +1,9 @@ +#ifndef H_COMMON_GENERAL +#define H_COMMON_GENERAL + +int max(int first, int second) +{ + return (first < second) ? second : first; +} + +#endif diff --git a/examples/controlgallery/main.c b/examples/controlgallery/main.c index c0d536c1..4ba5cc8a 100644 --- a/examples/controlgallery/main.c +++ b/examples/controlgallery/main.c @@ -37,6 +37,11 @@ static uiControl *makeBasicControlsPage(void) uiBoxAppend(hbox, uiControl(uiNewCheckbox("Checkbox")), 0); + uiButton *btn = uiNewButton("Wide"); + uiButtonSetMinSize(btn, 150, -1); + uiBoxAppend(hbox, + uiControl(btn), + 0); uiBoxAppend(vbox, uiControl(uiNewLabel("This is a label. Right now, labels can only span one line.")), diff --git a/ui.h b/ui.h index 40aea949..00419a11 100644 --- a/ui.h +++ b/ui.h @@ -138,6 +138,7 @@ _UI_EXTERN char *uiButtonText(uiButton *b); _UI_EXTERN void uiButtonSetText(uiButton *b, const char *text); _UI_EXTERN void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *b, void *data), void *data); _UI_EXTERN uiButton *uiNewButton(const char *text); +_UI_EXTERN void uiButtonSetMinSize(uiButton *b, int width, int height); typedef struct uiBox uiBox; #define uiBox(this) ((uiBox *) (this)) diff --git a/windows/button.cpp b/windows/button.cpp index d8913ec7..9affd985 100644 --- a/windows/button.cpp +++ b/windows/button.cpp @@ -1,11 +1,14 @@ // 7 april 2015 #include "uipriv_windows.hpp" +#include "../common/general.h" struct uiButton { uiWindowsControl c; HWND hwnd; void (*onClicked)(uiButton *, void *); void *onClickedData; + int minHeight; + int minWidth; }; static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult) @@ -44,8 +47,8 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height) size.cx = 0; // explicitly ask for ideal size size.cy = 0; if (SendMessageW(b->hwnd, BCM_GETIDEALSIZE, 0, (LPARAM) (&size)) != FALSE) { - *width = size.cx; - *height = size.cy; + *width = max(size.cx, (LONG)(b->minWidth)); + *height = max(size.cy, (LONG)(b->minHeight)); return; } @@ -57,6 +60,9 @@ static void uiButtonMinimumSize(uiWindowsControl *c, int *width, int *height) uiWindowsGetSizing(b->hwnd, &sizing); uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y); *height = y; + + *width = max(*width, b->minWidth); + *height = max(y, b->minHeight); } static void defaultOnClicked(uiButton *b, void *data) @@ -76,6 +82,13 @@ void uiButtonSetText(uiButton *b, const char *text) uiWindowsControlMinimumSizeChanged(uiWindowsControl(b)); } +void uiButtonSetMinSize(uiButton *b, int width, int height) +{ + b->minHeight = height; + b->minWidth = width; + uiWindowsControlMinimumSizeChanged(uiWindowsControl(b)); +} + void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data) { b->onClicked = f; @@ -88,6 +101,8 @@ uiButton *uiNewButton(const char *text) WCHAR *wtext; uiWindowsNewControl(uiButton, b); + b->minHeight = -1; + b->minWidth = -1; wtext = toUTF16(text); b->hwnd = uiWindowsEnsureCreateControlHWND(0,