diff --git a/windows/drawtext.cpp b/windows/drawtext.cpp index b86e6890..853cfc89 100644 --- a/windows/drawtext.cpp +++ b/windows/drawtext.cpp @@ -57,7 +57,7 @@ struct uiDrawTextFont { double size; }; -uiDrawTextFont *mkTextFont(IDWriteFont *df, WCHAR *family, BOOL copyFamily, double size) +uiDrawTextFont *mkTextFont(IDWriteFont *df, BOOL addRef, WCHAR *family, BOOL copyFamily, double size) { uiDrawTextFont *font; WCHAR *copy; @@ -65,6 +65,8 @@ uiDrawTextFont *mkTextFont(IDWriteFont *df, WCHAR *family, BOOL copyFamily, doub font = uiNew(uiDrawTextFont); font->f = df; + if (addRef) + font->f->AddRef(); if (copyFamily) { copy = (WCHAR *) uiAlloc((wcslen(family) + 1) * sizeof (WCHAR), "WCHAR[]"); wcscpy(copy, family); @@ -260,6 +262,7 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc) logHRESULT("error loading font in uiDrawLoadClosestFont()", hr); font = mkTextFont(match, + FALSE, // we own the initial reference; no need to add another one wfamily, FALSE, // will be freed with font desc->Size); diff --git a/windows/fontbutton.cpp b/windows/fontbutton.cpp index 9747561b..79425851 100644 --- a/windows/fontbutton.cpp +++ b/windows/fontbutton.cpp @@ -7,6 +7,8 @@ struct uiFontButton { HWND hwnd; struct fontDialogParams params; BOOL already; + void (*onChanged)(uiFontButton *, void *); + void *onChangedData; }; static void onDestroy(uiFontButton *); @@ -47,7 +49,7 @@ static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult) parent = GetAncestor(b->hwnd, GA_ROOT); // TODO didn't we have a function for this if (showFontDialog(parent, &(b->params))) { updateFontButtonLabel(b); - // TODO event + (*(b->onChanged))(b, b->onChangedData); } *lResult = 0; @@ -78,25 +80,25 @@ static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width *height = uiWindowsDlgUnitsToY(buttonHeight, d->BaseY); } -#if 0 -TODO -static void defaultOnClicked(uiButton *b, void *data) +static void defaultOnChanged(uiFontButton *b, void *data) { // do nothing } -#endif -// TODO document that GetFont returns a NEW instance each time +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); +} + // TODO document that the Handle of a Font may not be unique -#if 0 -TODO -void uiButtonOnClicked(uiButton *b, void (*f)(uiButton *, void *), void *data) +void uiFontButtonOnChanged(uiFontButton *b, void (*f)(uiFontButton *, void *), void *data) { - b->onClicked = f; - b->onClickedData = data; + b->onChanged = f; + b->onChangedData = data; } -#endif uiFontButton *uiNewFontButton(void) { @@ -114,7 +116,7 @@ uiFontButton *uiNewFontButton(void) updateFontButtonLabel(b); uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b)); -//TODO uiButtonOnClicked(b, defaultOnClicked, NULL); + uiFontButtonOnChanged(b, defaultOnChanged, NULL); uiWindowsFinishNewControl(b, uiFontButton); diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index 9ea1dabd..46123e08 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -162,7 +162,7 @@ extern WCHAR *fontCollectionCorrectString(fontCollection *fc, IDWriteLocalizedSt // drawtext.cpp #ifdef __cplusplus -extern uiDrawTextFont *mkTextFont(IDWriteFont *df, WCHAR *family, BOOL copyFamily, double size); +extern uiDrawTextFont *mkTextFont(IDWriteFont *df, BOOL addRef, WCHAR *family, BOOL copyFamily, double size); struct dwriteAttr { uiDrawTextWeight weight; uiDrawTextItalic italic;