Finished changing the Windows font dialog and uiFontButton to manipulate IDWriteFonts directly.

This commit is contained in:
Pietro Gagliardi 2016-04-18 17:14:33 -04:00
parent b19a8c9c46
commit 861b5f22df
4 changed files with 49 additions and 60 deletions

View File

@ -113,7 +113,7 @@ int main(int argc, char *argv[])
uiTabAppend(mainTab, "Page 5", uiControl(page5)); uiTabAppend(mainTab, "Page 5", uiControl(page5));
innerTab = newTab(); innerTab = newTab();
uiTabAppend(outerTab, "Pages 6-9", uiControl(innerTab)); uiTabAppend(outerTab, "Pages 6-10", uiControl(innerTab));
page6 = makePage6(); page6 = makePage6();
uiTabAppend(innerTab, "Page 6", uiControl(page6)); uiTabAppend(innerTab, "Page 6", uiControl(page6));

View File

@ -1,5 +1,6 @@
// 14 april 2016 // 14 april 2016
#include "uipriv_windows.h" #include "uipriv_windows.h"
#define this self // TODO
struct uiFontButton { struct uiFontButton {
uiWindowsControl c; uiWindowsControl c;

View File

@ -424,27 +424,17 @@ static LRESULT CALLBACK fontDialogSampleSubProc(HWND hwnd, UINT uMsg, WPARAM wPa
static void setupInitialFontDialogState(struct fontDialog *f) static void setupInitialFontDialogState(struct fontDialog *f)
{ {
WCHAR *wfamily;
struct dwriteAttr attr;
WCHAR wsize[512]; // this should be way more than enough WCHAR wsize[512]; // this should be way more than enough
LRESULT pos; LRESULT pos;
// first convert f->params->desc into a usable form
wfamily = toUTF16(f->params->desc.Family);
// see below for why we do this specifically
// TODO is 512 the correct number to pass to _snwprintf()?
// TODO will this revert to scientific notation?
_snwprintf(wsize, 512, L"%g", f->params->desc.Size);
attr.weight = f->params.desc->Weight;
attr.italic = f->params.desc->Italic;
attr.stretch = f->params.desc->Stretch;
attrToDWriteAttr(&attr);
// first let's load the size // first let's load the size
// the real font dialog: // the real font dialog:
// - if the chosen font size is in the list, it selects that item AND makes it topmost // - if the chosen font size is in the list, it selects that item AND makes it topmost
// - if the chosen font size is not in the list, don't bother // - if the chosen font size is not in the list, don't bother
// we'll simulate it by setting the text to a %f representation, then pretending as if it was entered // we'll simulate it by setting the text to a %f representation, then pretending as if it was entered
// TODO is 512 the correct number to pass to _snwprintf()?
// TODO will this revert to scientific notation?
_snwprintf(wsize, 512, L"%g", f->params->size);
// TODO make this a setWindowText() // TODO make this a setWindowText()
if (SendMessageW(f->sizeCombobox, WM_SETTEXT, 0, (LPARAM) wsize) != (LRESULT) TRUE) if (SendMessageW(f->sizeCombobox, WM_SETTEXT, 0, (LPARAM) wsize) != (LRESULT) TRUE)
logLastError("error setting size combobox to initial font size in setupInitialFontDialogState()"); logLastError("error setting size combobox to initial font size in setupInitialFontDialogState()");
@ -455,13 +445,12 @@ static void setupInitialFontDialogState(struct fontDialog *f)
// now we set the family and style // now we set the family and style
// we do this by first setting the previous style attributes, then simulating a font entered // we do this by first setting the previous style attributes, then simulating a font entered
f->weight = attr.dweight; f->weight = f->params->font->GetWeight();
f->style = attr.ditalic; f->style = f->params->font->GetStyle();
f->stretch = attr.dstretch; f->stretch = f->params->font->GetStretch();
if (SendMessageW(f->familyCombobox, WM_SETTEXT, 0, (LPARAM) wfamily) != (LRESULT) TRUE) if (SendMessageW(f->familyCombobox, WM_SETTEXT, 0, (LPARAM) (f->params->familyName)) != (LRESULT) TRUE)
logLastError("error setting family combobox to initial font family in setupInitialFontDialogState()"); logLastError("error setting family combobox to initial font family in setupInitialFontDialogState()");
familyEdited(f); familyEdited(f);
uiFree(wfamily);
} }
static struct fontDialog *beginFontDialog(HWND hwnd, LPARAM lParam) static struct fontDialog *beginFontDialog(HWND hwnd, LPARAM lParam)
@ -529,9 +518,7 @@ static void endFontDialog(struct fontDialog *f, INT_PTR code)
static INT_PTR tryFinishDialog(struct fontDialog *f, WPARAM wParam) static INT_PTR tryFinishDialog(struct fontDialog *f, WPARAM wParam)
{ {
WCHAR *wfamily; IDWriteFontFamily *family;
IDWriteFont *font;
struct dwriteAttr attr;
// cancelling // cancelling
if (LOWORD(wParam) != IDOK) { if (LOWORD(wParam) != IDOK) {
@ -540,24 +527,14 @@ static INT_PTR tryFinishDialog(struct fontDialog *f, WPARAM wParam)
} }
// OK // OK
wfamily = cbGetItemText(f->familyCombobox, f->curFamily); destroyFontDialogParams(f->params);
uiFree(f->params->desc.Family); f->params->font = (IDWriteFont *) cbGetItemData(f->styleCombobox, f->curStyle);
f->params->desc.Family = toUTF8(wfamily); // we need to save font from being destroyed with the combobox
uiFree(wfamily); f->params->font->AddRef();
f->params->desc.Size = f->curSize; f->params->size = f->curSize;
font = (IDWriteFont *) cbGetItemData(f->styleCombobox, f->curStyle); family = (IDWriteFontFamily *) cbGetItemData(f->familyCombobox, f->curFamily);
attr.dweight = font->GetWeight(); f->params->familyName = fontCollectionFamilyName(f->fc, family);
attr.ditalic = font->GetStyle(); f->params->styleName = fontStyleName(f->fc, f->params->font);
attr.dstretch = font->GetStretch();
dwriteAttrToAttr(&attr);
f->params->desc.Weight = attr.weight;
f->params->desc.Italic = attr.italic;
f->params->desc.Stretch = attr.stretch;
uiFree(f->params->outStyleName);
// TODO rename to wstr
wfamily = fontStyleName(f->fc, font);
f->params->outStyleName = toUTF8(wfamily);
uiFree(wfamily);
endFontDialog(f, 2); endFontDialog(f, 2);
return TRUE; return TRUE;
} }
@ -658,8 +635,6 @@ void loadInitialFontDialogParams(struct fontDialogParams *params)
struct fontCollection *fc; struct fontCollection *fc;
IDWriteFontFamily *family; IDWriteFontFamily *family;
IDWriteFont *font; IDWriteFont *font;
struct attr;
WCHAR *wstr;
HRESULT hr; HRESULT hr;
// Our preferred font is Arial 10 Regular. // Our preferred font is Arial 10 Regular.
@ -691,24 +666,32 @@ void loadInitialFontDialogParams(struct fontDialogParams *params)
if (hr != S_OK) if (hr != S_OK)
logHRESULT("error getting Regular font from Arial in loadInitialFontDialogParams()", hr); logHRESULT("error getting Regular font from Arial in loadInitialFontDialogParams()", hr);
// now convert attributes in the actual font... params->font = font;
attr.dweight = font->GetWeight(); params->size = 10;
attr.ditalic = font->GetStyle(); params->familyName = fontCollectionFamilyName(fc, family);
attr.dstretch = font->GetStretch(); params->styleName = fontStyleName(fc, font);
dwriteAttrToAttr(&attr);
// and finally fill the structure // don't release font; we still need it
wstr = fontCollectionFamilyName(fc, family);
params->desc.Family = toUTF8(wstr);
uiFree(wstr);
params->desc.Size = 10;
params->desc.Weight = attr.weight;
params->desc.Italic = attr.italic;
params->desc.Stretch = attr.stretch;
wstr = fontStyleName(fc, font);
params->outStyleName = toUTF8(wstr);
uiFree(wstr);
font->Release();
family->Release(); family->Release();
fontCollectionFree(fc); fontCollectionFree(fc);
} }
void destroyFontDialogParams(struct fontDialogParams *params)
{
params->font->Release();
uiFree(params->familyName);
uiFree(params->styleName);
}
WCHAR *fontDialogParamsToString(struct fontDialogParams *params)
{
WCHAR *text;
// TODO dynamically allocate
text = (WCHAR *) uiAlloc(512 * sizeof (WCHAR), "WCHAR[]");
_snwprintf(text, 512, L"%s %s %g",
params->familyName,
params->styleName,
params->size);
return text;
}

View File

@ -1,7 +1,12 @@
// 6 january 2015 // 6 january 2015
#include "winapi.h" #include "winapi.h"
#include "../ui.h" #include "../ui.h"
// TODO
#ifdef __cplusplus
#define this self
#endif
#include "../ui_windows.h" #include "../ui_windows.h"
#undef this // TODO
#include "../common/uipriv.h" #include "../common/uipriv.h"
#include "resources.h" #include "resources.h"
#include "compilerver.h" #include "compilerver.h"
@ -180,7 +185,7 @@ struct fontDialogParams {
extern BOOL showFontDialog(HWND parent, struct fontDialogParams *params); extern BOOL showFontDialog(HWND parent, struct fontDialogParams *params);
extern void loadInitialFontDialogParams(struct fontDialogParams *params); extern void loadInitialFontDialogParams(struct fontDialogParams *params);
extern void destroyFontDialogParams(struct fontDialogParams *params); extern void destroyFontDialogParams(struct fontDialogParams *params);
extern WCHAR fontDialogParamsToString(struct fontDialogParams *params); extern WCHAR *fontDialogParamsToString(struct fontDialogParams *params);
#endif #endif
// d2dscratch.c // d2dscratch.c