libui/windows/fontbutton.cpp

123 lines
3.0 KiB
C++
Raw Normal View History

2016-04-14 16:12:13 -05:00
// 14 april 2016
#include "uipriv_windows.hpp"
2016-04-14 16:12:13 -05:00
struct uiFontButton {
uiWindowsControl c;
HWND hwnd;
struct fontDialogParams params;
2016-04-14 16:12:13 -05:00
BOOL already;
void (*onChanged)(uiFontButton *, void *);
void *onChangedData;
2016-04-14 16:12:13 -05:00
};
2016-04-29 12:50:08 -05:00
static void uiFontButtonDestroy(uiControl *c)
{
2016-04-29 12:50:08 -05:00
uiFontButton *b = uiFontButton(c);
uiWindowsUnregisterWM_COMMANDHandler(b->hwnd);
destroyFontDialogParams(&(b->params));
2016-04-29 12:50:08 -05:00
uiWindowsEnsureDestroyWindow(b->hwnd);
uiFreeControl(uiControl(b));
}
2016-04-14 16:12:13 -05:00
static void updateFontButtonLabel(uiFontButton *b)
{
WCHAR *text;
text = fontDialogParamsToString(&(b->params));
setWindowText(b->hwnd, text);
uiFree(text);
2016-04-14 16:12:13 -05:00
// changing the text might necessitate a change in the button's size
uiWindowsControlMinimumSizeChanged(uiWindowsControl(b));
2016-04-14 16:12:13 -05:00
}
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
{
uiFontButton *b = uiFontButton(c);
HWND parent;
2016-04-14 16:12:13 -05:00
if (code != BN_CLICKED)
return FALSE;
parent = parentToplevel(b->hwnd);
if (showFontDialog(parent, &(b->params))) {
2016-04-14 16:12:13 -05:00
updateFontButtonLabel(b);
(*(b->onChanged))(b, b->onChangedData);
2016-04-14 16:12:13 -05:00
}
*lResult = 0;
return TRUE;
}
2016-04-29 12:50:08 -05:00
uiWindowsControlAllDefaultsExceptDestroy(uiFontButton)
2016-04-14 16:12:13 -05:00
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define buttonHeight 14
2016-04-29 12:50:08 -05:00
static void uiFontButtonMinimumSize(uiWindowsControl *c, intmax_t *width, intmax_t *height)
2016-04-14 16:12:13 -05:00
{
uiFontButton *b = uiFontButton(c);
SIZE size;
2016-04-29 12:50:08 -05:00
uiWindowsSizing sizing;
int y;
2016-04-14 16:12:13 -05:00
// try the comctl32 version 6 way
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;
return;
}
// 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(b->hwnd) + (2 * GetSystemMetrics(SM_CXEDGE));
2016-04-29 12:50:08 -05:00
y = buttonHeight;
uiWindowsGetSizing(b->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &y);
*height = y;
2016-04-14 16:12:13 -05:00
}
static void defaultOnChanged(uiFontButton *b, void *data)
2016-04-14 16:12:13 -05:00
{
// do nothing
}
uiDrawTextFont *uiFontButtonFont(uiFontButton *b)
{
// we don't own b->params.font; we have to add a reference
// we don't own b->params.familyName either; we have to copy it
return mkTextFont(b->params.font, TRUE, b->params.familyName, TRUE, b->params.size);
}
void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data)
2016-04-14 16:12:13 -05:00
{
b->onChanged = f;
b->onChangedData = data;
2016-04-14 16:12:13 -05:00
}
uiFontButton *uiNewFontButton(void)
{
uiFontButton *b;
2016-04-29 12:50:08 -05:00
uiWindowsNewControl(uiFontButton, b);
2016-04-14 16:12:13 -05:00
b->hwnd = uiWindowsEnsureCreateControlHWND(0,
L"button", L"you should not be seeing this",
2016-04-14 16:12:13 -05:00
BS_PUSHBUTTON | WS_TABSTOP,
hInstance, NULL,
TRUE);
loadInitialFontDialogParams(&(b->params));
2016-04-14 16:12:13 -05:00
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b));
uiFontButtonOnChanged(b, defaultOnChanged, NULL);
2016-04-14 16:12:13 -05:00
updateFontButtonLabel(b);
2016-04-14 16:12:13 -05:00
return b;
}