2016-04-14 16:12:13 -05:00
|
|
|
// 14 april 2016
|
2016-04-23 15:49:46 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
2016-04-14 16:12:13 -05:00
|
|
|
|
|
|
|
struct uiFontButton {
|
|
|
|
uiWindowsControl c;
|
|
|
|
HWND hwnd;
|
2016-04-18 12:32:48 -05:00
|
|
|
struct fontDialogParams params;
|
2016-04-14 16:12:13 -05:00
|
|
|
BOOL already;
|
2016-04-20 13:17:55 -05:00
|
|
|
void (*onChanged)(uiFontButton *, void *);
|
|
|
|
void *onChangedData;
|
2016-04-14 16:12:13 -05:00
|
|
|
};
|
|
|
|
|
2016-04-18 14:31:52 -05:00
|
|
|
static void onDestroy(uiFontButton *);
|
|
|
|
|
2016-04-14 16:12:13 -05:00
|
|
|
uiWindowsDefineControlWithOnDestroy(
|
|
|
|
uiFontButton, // type name
|
2016-04-23 15:52:53 -05:00
|
|
|
onDestroy(me); // on destroy
|
2016-04-14 16:12:13 -05:00
|
|
|
)
|
|
|
|
|
2016-04-18 14:31:52 -05:00
|
|
|
static void onDestroy(uiFontButton *b)
|
|
|
|
{
|
|
|
|
uiWindowsUnregisterWM_COMMANDHandler(b->hwnd);
|
|
|
|
destroyFontDialogParams(&(b->params));
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:12:13 -05:00
|
|
|
static void updateFontButtonLabel(uiFontButton *b)
|
|
|
|
{
|
2016-04-18 14:31:52 -05:00
|
|
|
WCHAR *text;
|
|
|
|
|
|
|
|
text = fontDialogParamsToString(&(b->params));
|
2016-04-18 00:12:25 -05:00
|
|
|
// TODO error check
|
|
|
|
SendMessageW(b->hwnd, WM_SETTEXT, 0, (LPARAM) text);
|
2016-04-18 14:31:52 -05:00
|
|
|
uiFree(text);
|
2016-04-18 00:12:25 -05:00
|
|
|
|
2016-04-14 16:12:13 -05:00
|
|
|
// changing the text might necessitate a change in the button's size
|
|
|
|
uiWindowsControlQueueRelayout(uiWindowsControl(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
|
|
|
|
{
|
|
|
|
uiFontButton *b = uiFontButton(c);
|
2016-04-17 21:09:18 -05:00
|
|
|
HWND parent;
|
2016-04-14 16:12:13 -05:00
|
|
|
|
|
|
|
if (code != BN_CLICKED)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-04-17 21:09:18 -05:00
|
|
|
parent = GetAncestor(b->hwnd, GA_ROOT); // TODO didn't we have a function for this
|
2016-04-18 12:32:48 -05:00
|
|
|
if (showFontDialog(parent, &(b->params))) {
|
2016-04-14 16:12:13 -05:00
|
|
|
updateFontButtonLabel(b);
|
2016-04-20 13:17:55 -05:00
|
|
|
(*(b->onChanged))(b, b->onChangedData);
|
2016-04-14 16:12:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
*lResult = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
|
|
|
|
#define buttonHeight 14
|
|
|
|
|
|
|
|
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
|
|
|
|
{
|
|
|
|
uiFontButton *b = uiFontButton(c);
|
|
|
|
SIZE size;
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
*height = uiWindowsDlgUnitsToY(buttonHeight, d->BaseY);
|
|
|
|
}
|
|
|
|
|
2016-04-20 13:17:55 -05:00
|
|
|
static void defaultOnChanged(uiFontButton *b, void *data)
|
2016-04-14 16:12:13 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2016-04-20 13:17:55 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:31:52 -05:00
|
|
|
// TODO document that the Handle of a Font may not be unique
|
|
|
|
|
2016-04-20 13:17:55 -05:00
|
|
|
void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data)
|
2016-04-14 16:12:13 -05:00
|
|
|
{
|
2016-04-20 13:17:55 -05:00
|
|
|
b->onChanged = f;
|
|
|
|
b->onChangedData = data;
|
2016-04-14 16:12:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiFontButton *uiNewFontButton(void)
|
|
|
|
{
|
|
|
|
uiFontButton *b;
|
|
|
|
|
2016-04-24 16:38:48 -05:00
|
|
|
b = (uiFontButton *) uiNewControl(uiFontButton);
|
2016-04-14 16:12:13 -05:00
|
|
|
|
|
|
|
b->hwnd = uiWindowsEnsureCreateControlHWND(0,
|
2016-04-18 00:12:25 -05:00
|
|
|
L"button", L"you should not be seeing this",
|
2016-04-14 16:12:13 -05:00
|
|
|
BS_PUSHBUTTON | WS_TABSTOP,
|
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
|
2016-04-18 12:32:48 -05:00
|
|
|
loadInitialFontDialogParams(&(b->params));
|
2016-04-18 00:12:25 -05:00
|
|
|
updateFontButtonLabel(b);
|
2016-04-17 21:09:18 -05:00
|
|
|
|
2016-04-14 16:12:13 -05:00
|
|
|
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b));
|
2016-04-20 13:17:55 -05:00
|
|
|
uiFontButtonOnChanged(b, defaultOnChanged, NULL);
|
2016-04-14 16:12:13 -05:00
|
|
|
|
|
|
|
uiWindowsFinishNewControl(b, uiFontButton);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|