From 97d35571c1eafb37d21f4a011912b9bf0122959f Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 14 Apr 2016 21:35:50 -0400 Subject: [PATCH] Started writing a replacement for the standard ChooseFont() dialog that uses DirectWrite instead of GDI. --- windows/GNUfiles.mk | 3 +- windows/fontbutton.c | 1 + windows/fontdialog.cpp | 93 ++++++++++++++++++++++++++++++++++++++++ windows/resources.h | 6 +++ windows/resources.rc | 32 ++++++++++++++ windows/uipriv_windows.h | 3 ++ 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 windows/fontdialog.cpp diff --git a/windows/GNUfiles.mk b/windows/GNUfiles.mk index 82a7ec8c..b88c61b6 100644 --- a/windows/GNUfiles.mk +++ b/windows/GNUfiles.mk @@ -43,7 +43,8 @@ CFILES += \ CXXFILES += \ windows/drawtext.cpp \ - windows/dwrite.cpp + windows/dwrite.cpp \ + windows/fontdialog.cpp HFILES += \ windows/area.h \ diff --git a/windows/fontbutton.c b/windows/fontbutton.c index f20de68a..32f51dc5 100644 --- a/windows/fontbutton.c +++ b/windows/fontbutton.c @@ -36,6 +36,7 @@ static BOOL onWM_COMMAND(uiControl *c, HWND hwnd, WORD code, LRESULT *lResult) 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); // TODO CF_FORCEFONTEXIST? CF_INACTIVEFONTS? CF_NOSCRIPTSEL? CF_USESTYLE? if (b->already) diff --git a/windows/fontdialog.cpp b/windows/fontdialog.cpp new file mode 100644 index 00000000..eddc22a2 --- /dev/null +++ b/windows/fontdialog.cpp @@ -0,0 +1,93 @@ +// 14 april 2016 +#include "uipriv_windows.h" + +struct fontDialog { + HWND hwnd; + HWND familyCombobox; + HWND styleCombobox; + HWND sizeCombobox; + HWND smallCapsCheckbox; + // TODO desc; + fontCollection *fc; +}; + +static struct fontDialog *beginFontDialog(HWND hwnd, LPARAM lParam) +{ + struct fontDialog *f; + + f = uiNew(struct fontDialog); + f->hwnd = hwnd; + + f->familyCombobox = GetDlgItem(f->hwnd, rcFontFamilyCombobox); + if (f->familyCombobox == NULL) + logLastError("error getting font family combobox handle in beginFontDialog()"); + f->styleCombobox = GetDlgItem(f->hwnd, rcFontStyleCombobox); + if (f->styleCombobox == NULL) + logLastError("error getting font style combobox handle in beginFontDialog()"); + f->sizeCombobox = GetDlgItem(f->hwnd, rcFontSizeCombobox); + if (f->sizeCombobox == NULL) + logLastError("error getting font size combobox handle in beginFontDialog()"); + f->smallCapsCheckbox = GetDlgItem(f->hwnd, rcFontSmallCapsCheckbox); + if (f->smallCapsCheckbox == NULL) + logLastError("error getting small caps checkbox handle in beginFontDialog()"); + + return f; +} + +static void endFontDialog(struct fontDialog *f, INT_PTR code) +{ + if (EndDialog(f->hwnd, code) == 0) + logLastError("error ending font dialog in endFontDialog()"); + uiFree(f); +} + +static INT_PTR tryFinishDialog(struct fontDialog *f, WPARAM wParam) +{ + // cancelling + if (LOWORD(wParam) != IDOK) { + endFontDialog(f, 1); + return TRUE; + } + + // TODO + + endFontDialog(f, 2); + return TRUE; +} + +static INT_PTR CALLBACK fontDialogDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + struct fontDialog *f; + + f = (struct fontDialog *) GetWindowLongPtrW(hwnd, DWLP_USER); + if (f == NULL) { + if (uMsg == WM_INITDIALOG) { + f = beginFontDialog(hwnd, lParam); + SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR) f); + return TRUE; + } + return FALSE; + } + + switch (uMsg) { + case WM_COMMAND: + if (HIWORD(wParam) != BN_CLICKED) + return FALSE; + return tryFinishDialog(f, wParam); + } + return FALSE; +} + +void showFontDialog(HWND parent) +{ + switch (DialogBoxParamW(hInstance, MAKEINTRESOURCE(rcFontDialog), parent, fontDialogDlgProc, (LPARAM) NULL)) { + case 1: + // TODO cancel + break; + case 2: + // TODO OK + break; + default: + logLastError("error running font dialog in showFontDialog()"); + } +} diff --git a/windows/resources.h b/windows/resources.h index f560cca2..a1250cfc 100644 --- a/windows/resources.h +++ b/windows/resources.h @@ -1,3 +1,9 @@ // 30 may 2015 #define rcTabPageDialog 100 +#define rcFontDialog 101 + +#define rcFontFamilyCombobox 1000 +#define rcFontStyleCombobox 1001 +#define rcFontSizeCombobox 1002 +#define rcFontSmallCapsCheckbox 1003 diff --git a/windows/resources.rc b/windows/resources.rc index 58e945f0..b8e83cc7 100644 --- a/windows/resources.rc +++ b/windows/resources.rc @@ -16,3 +16,35 @@ EXSTYLE WS_EX_CONTROLPARENT BEGIN // nothing END + +// this is for our custom DirectWrite-based font dialog (see fontdialog.cpp) +// this is based on the "New Font Dialog with Syslink" in Microsoft's font.dlg +// TODO look at localization +rcFontDialog DIALOGEX 13, 54, 243, 234 +STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_3DLOOK +CAPTION "Font" +FONT 9, "Segoe UI" +BEGIN + LTEXT "&Font:", -1, 7, 7, 98, 9 + COMBOBOX rcFontFamilyCombobox, 7, 16, 98, 76, + CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL | + CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS + + LTEXT "Font st&yle:", -1, 114, 7, 74, 9 + COMBOBOX rcFontStyleCombobox, 114, 16, 74, 76, + CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL | + WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS + + LTEXT "&Size:", -1, 198, 7, 36, 9 + COMBOBOX rcFontSizeCombobox, 198, 16, 36, 76, + CBS_SIMPLE | CBS_AUTOHSCROLL | CBS_DISABLENOSCROLL | + CBS_SORT | WS_VSCROLL | WS_TABSTOP | CBS_HASSTRINGS + + AUTOCHECKBOX "S&mall caps", rcFontSmallCapsCheckbox, + 13, 111, 90, 10, WS_TABSTOP + + GROUPBOX "Sample", -1, 114, 97, 120, 43, WS_GROUP + + DEFPUSHBUTTON "OK", IDOK, 141, 215, 45, 14, WS_GROUP + PUSHBUTTON "Cancel", IDCANCEL, 190, 215, 45, 14, WS_GROUP +END diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index c0090306..caa4c43e 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -155,6 +155,9 @@ extern void fontCollectionFree(fontCollection *fc); // drawtext.cpp extern void doDrawText(ID2D1RenderTarget *rt, ID2D1Brush *black, double x, double y, uiDrawTextLayout *layout); +// fontdialog.cpp +extern void showFontDialog(HWND parent); + #ifdef __cplusplus } #endif