Hooked the Windows font button and font dialog together at last. More TODOs. Now we just need to actually convert everything to DirectWrite and implement the label.

This commit is contained in:
Pietro Gagliardi 2016-04-17 22:09:18 -04:00
parent 40d673fb01
commit 7cf8420682
4 changed files with 30 additions and 40 deletions

1
ui.h
View File

@ -472,7 +472,6 @@ typedef enum uiDrawTextWeight {
uiDrawTextWeightUltraHeavy,
} uiDrawTextWeight;
// TODO drop Oblique?
typedef enum uiDrawTextItalic {
uiDrawTextItalicNormal,
uiDrawTextItalicOblique,

View File

@ -6,8 +6,7 @@
struct uiFontButton {
uiWindowsControl c;
HWND hwnd;
LOGFONTW font;
INT pointSize;
uiDrawTextFontDescriptor desc;
BOOL already;
};
@ -20,7 +19,6 @@ uiWindowsDefineControlWithOnDestroy(
static void updateFontButtonLabel(uiFontButton *b)
{
// TODO
SetWindowTextW(b->hwnd, b->font.lfFaceName);
// changing the text might necessitate a change in the button's size
uiWindowsControlQueueRelayout(uiWindowsControl(b));
}
@ -28,38 +26,20 @@ static void updateFontButtonLabel(uiFontButton *b)
static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult)
{
uiFontButton *b = uiFontButton(c);
CHOOSEFONTW cf;
HWND parent;
char *oldFamily;
if (code != BN_CLICKED)
return FALSE;
ZeroMemory(&cf, sizeof (CHOOSEFONTW));
cf.lStructSize = sizeof (CHOOSEFONTW);
cf.hwndOwner = GetAncestor(b->hwnd, GA_ROOT); // TODO didn't we have a function for this
showFontDialog(cf.hwndOwner);
cf.lpLogFont = &(b->font);
ZeroMemory(&(b->font), sizeof(LOGFONTW));
b->font.lfFaceName[0]='A';
b->font.lfFaceName[1]='r';
b->font.lfFaceName[2]='i';
b->font.lfFaceName[3]='a';
b->font.lfFaceName[4]='l';
b->font.lfFaceName[5]=0;
b->font.lfHeight=-15*96/72;
// TODO CF_FORCEFONTEXIST? CF_INACTIVEFONTS? CF_NOSCRIPTSEL? CF_USESTYLE?
// if (b->already)
cf.Flags = CF_INITTOLOGFONTSTRUCT;
if (ChooseFontW(&cf) != FALSE) {
parent = GetAncestor(b->hwnd, GA_ROOT); // TODO didn't we have a function for this
oldFamily = (char *) (b->desc.Family);
if (showFontDialog(parent, &(b->desc))) {
if (b->already) // don't free the static Arial string
uiFree(oldFamily);
b->already = TRUE;
updateFontButtonLabel(b);
// TODO event
} else {
DWORD err;
err = CommDlgExtendedError();
if (err != 0)
// TODO
logLastError("TODO help");
}
*lResult = 0;
@ -120,6 +100,13 @@ uiFontButton *uiNewFontButton(void)
hInstance, NULL,
TRUE);
// arbitrary defaults that will do
b->desc.Family = "Arial";
b->desc.Size = 10; // from the real font dialog
b->desc.Weight = uiDrawTextWeightNormal;
b->desc.Italic = uiDrawTextItalicNormal;
b->desc.Stretch = uiDrawTextStretchNormal;
uiWindowsRegisterWM_COMMANDHandler(b->hwnd, onWM_COMMAND, uiControl(b));
//TODO uiButtonOnClicked(b, defaultOnClicked, NULL);

View File

@ -2,7 +2,9 @@
#include "uipriv_windows.h"
// TODOs
// - quote the Choose Font sample here for reference
// - the Choose Font sample defaults to Regular/Italic/Bold/Bold Italic in some case (no styles?); do we? find out what the case is
// - do we set initial family and style topmost as well?
struct fontDialog {
HWND hwnd;
@ -10,7 +12,7 @@ struct fontDialog {
HWND styleCombobox;
HWND sizeCombobox;
// TODO desc;
uiDrawTextFontDescriptor *desc;
fontCollection *fc;
@ -433,6 +435,7 @@ static struct fontDialog *beginFontDialog(HWND hwnd, LPARAM lParam)
f = uiNew(struct fontDialog);
f->hwnd = hwnd;
f->desc = (uiDrawTextFontDescriptor *) lParam;
f->familyCombobox = GetDlgItem(f->hwnd, rcFontFamilyCombobox);
if (f->familyCombobox == NULL)
@ -516,8 +519,9 @@ static INT_PTR tryFinishDialog(struct fontDialog *f, WPARAM wParam)
return TRUE;
}
// TODO
// OK
// TODO fill f->desc here
f->desc->Size = f->curSize;
endFontDialog(f, 2);
return TRUE;
}
@ -581,16 +585,16 @@ static INT_PTR CALLBACK fontDialogDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, L
return FALSE;
}
void showFontDialog(HWND parent)
BOOL showFontDialog(HWND parent, uiDrawTextFontDescriptor *desc)
{
switch (DialogBoxParamW(hInstance, MAKEINTRESOURCE(rcFontDialog), parent, fontDialogDlgProc, (LPARAM) NULL)) {
case 1:
// TODO cancel
break;
case 2:
// TODO OK
switch (DialogBoxParamW(hInstance, MAKEINTRESOURCE(rcFontDialog), parent, fontDialogDlgProc, (LPARAM) desc)) {
case 1: // cancel
return FALSE;
case 2: // ok
// make the compiler happy by putting the return after the switch
break;
default:
logLastError("error running font dialog in showFontDialog()");
}
return TRUE;
}

View File

@ -158,7 +158,7 @@ extern WCHAR *fontCollectionCorrectString(fontCollection *fc, IDWriteLocalizedSt
extern void doDrawText(ID2D1RenderTarget *rt, ID2D1Brush *black, double x, double y, uiDrawTextLayout *layout);
// fontdialog.cpp
extern void showFontDialog(HWND parent);
extern BOOL showFontDialog(HWND parent, uiDrawTextFontDescriptor *desc);
// d2dscratch.c
extern ATOM registerD2DScratchClass(HICON, HCURSOR);