-
Notifications
You must be signed in to change notification settings - Fork 1
/
kncefontdlg.cpp
240 lines (182 loc) · 7.05 KB
/
kncefontdlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "kncecomm.h"
#include <string>
#include <windows.h>
#ifdef UNICODE
namespace std { typedef wstring tstring; }
#else
namespace std { typedef string tstring; }
#endif
using namespace std;
enum {
IDC_CHOOSE_FONT_NAME_LIST = 102,
IDC_CHOOSE_FONT_SIZE_LIST = 104,
IDC_CHOOSE_FONT_STYLE_LIST = 106,
IDC_CHOOSE_FONT_EXAMPLE_LABEL = 107,
};
static BOOL WINAPI dlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
static void onInitDialog(HWND hDlg, void *params);
static void onOk(HWND hDlg);
static void onCancel(HWND hDlg);
static void onNameList(HWND hDlg, int event);
static void onSizeList(HWND hDlg, int event);
static void onStyleList(HWND hDlg, int event);
static void updateFont(HWND hDlg);
extern HINSTANCE g_hInstance;
struct KnceFontDlgData {
HFONT hFont;
KnceChooseFontParams *chooseFontParams;
HFONT hCurrentFont;
bool isInitializing;
};
bool showChooseFontDialog(HWND hOwnerWindow, KnceChooseFontParams *params) {
int ret = DialogBoxParam(g_hInstance, _T("CHOOSE_FONT"), hOwnerWindow,
(DLGPROC)dlgProc, (LPARAM)params);
return ret == IDOK;
}
BOOL WINAPI dlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG:
onInitDialog(hDlg, (void *)lParam);
return true;
case WM_COMMAND:
{
int id = LOWORD(wParam);
int event = HIWORD(wParam);
switch (id) {
case IDOK:
onOk(hDlg);
break;
case IDCANCEL:
onCancel(hDlg);
break;
case IDC_CHOOSE_FONT_NAME_LIST:
onNameList(hDlg, event);
break;
case IDC_CHOOSE_FONT_SIZE_LIST:
onSizeList(hDlg, event);
break;
case IDC_CHOOSE_FONT_STYLE_LIST:
onStyleList(hDlg, event);
break;
default:
return false;
}
return true;
}
}
return false;
}
static void onInitDialog(HWND hDlg, void *params) {
static int fontSizes[] = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26,
28, 36, 48, 72};
int i;
KnceFontDlgData *data = new KnceFontDlgData();
SetWindowLong(hDlg, GWL_USERDATA, (long)data);
data->hFont = knceCreateDefaultFont(true, 100, false, false);
knceSetDialogFont(hDlg, data->hFont);
data->chooseFontParams = (KnceChooseFontParams *)params;
HWND hFontNameList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_NAME_LIST);
KnceFontName *fontNames = NULL;
int numFonts = knceObtainFontNames(&fontNames,
data->chooseFontParams->isFixedOnly);
for (i = 0; i < numFonts; i++) {
SendMessage(hFontNameList, LB_ADDSTRING, 0,
(LPARAM)fontNames[i].fontName);
}
free(fontNames);
HWND hFontSizeList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_SIZE_LIST);
TCHAR sizeCStr[16];
int numSizes = sizeof(fontSizes) / sizeof(int);
for (i = 0; i < numSizes; i++) {
_sntprintf(sizeCStr, 16, _T("%d"), fontSizes[i]);
SendMessage(hFontSizeList, LB_ADDSTRING, 0, (LPARAM)sizeCStr);
}
HWND hFontStyleList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_STYLE_LIST);
SendMessage(hFontStyleList, LB_ADDSTRING, 0, (LPARAM)_T("Normal"));
SendMessage(hFontStyleList, LB_ADDSTRING, 0, (LPARAM)_T("Italic"));
SendMessage(hFontStyleList, LB_ADDSTRING, 0, (LPARAM)_T("Bold"));
SendMessage(hFontStyleList, LB_ADDSTRING, 0, (LPARAM)_T("Bold Italic"));
data->isInitializing = true;
HFONT inputFont = data->chooseFontParams->hFont;
if (inputFont == NULL)
data->hCurrentFont = knceCreateDefaultFont(false, 120, false, false);
else {
LOGFONT logFont;
GetObject(inputFont, sizeof(LOGFONT), &logFont);
data->hCurrentFont = CreateFontIndirect(&logFont);
}
TCHAR fontNameCStr[256];
int pointSize = 0;
int isBold = 0;
int isItalic = 0;
knceGetFontAttributes(data->hCurrentFont, fontNameCStr, 256, &pointSize,
&isBold, &isItalic);
int itemIndex = SendMessage(hFontNameList, LB_FINDSTRINGEXACT, -1,
(LPARAM)fontNameCStr);
SendMessage(hFontNameList, LB_SETCURSEL, itemIndex, 0);
_sntprintf(sizeCStr, 16, _T("%d"), pointSize / 10);
itemIndex = SendMessage(hFontSizeList, LB_FINDSTRINGEXACT, -1,
(LPARAM)sizeCStr);
SendMessage(hFontSizeList, LB_SETCURSEL, itemIndex, 0);
itemIndex = (isItalic ? 0x01 : 0x00) | (isBold ? 0x02 : 0x00);
SendMessage(hFontStyleList, LB_SETCURSEL, itemIndex, 0);
HWND hExampleLabel = GetDlgItem(hDlg, IDC_CHOOSE_FONT_EXAMPLE_LABEL);
SendMessage(hExampleLabel, WM_SETFONT, (WPARAM)data->hCurrentFont, 0);
data->isInitializing = false;
updateFont(hDlg);
ShowWindow(hDlg, SW_SHOW);
}
static void onOk(HWND hDlg) {
KnceFontDlgData *data =
(KnceFontDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
data->chooseFontParams->hFont = data->hCurrentFont;
DeleteObject(data->hFont);
EndDialog(hDlg, IDOK);
}
static void onCancel(HWND hDlg) {
KnceFontDlgData *data =
(KnceFontDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
DeleteObject(data->hCurrentFont);
DeleteObject(data->hFont);
EndDialog(hDlg, IDCANCEL);
}
static void onNameList(HWND hDlg, int event) {
if (event == LBN_SELCHANGE)
updateFont(hDlg);
}
static void onSizeList(HWND hDlg, int event) {
if (event == LBN_SELCHANGE)
updateFont(hDlg);
}
static void onStyleList(HWND hDlg, int event) {
if (event == LBN_SELCHANGE)
updateFont(hDlg);
}
static void updateFont(HWND hDlg) {
KnceFontDlgData *data =
(KnceFontDlgData *)GetWindowLong(hDlg, GWL_USERDATA);
if (data->isInitializing)
return;
DeleteObject(data->hCurrentFont);
HWND hFontNameList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_NAME_LIST);
int fontNameIndex = SendMessage(hFontNameList, LB_GETCURSEL, 0, 0);
TCHAR *fontNameBuf = new TCHAR[SendMessage(hFontNameList, LB_GETTEXTLEN,
fontNameIndex, 0) + 1];
SendMessage(hFontNameList, LB_GETTEXT, fontNameIndex, (LPARAM)fontNameBuf);
HWND hFontSizeList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_SIZE_LIST);
int fontSizeIndex = SendMessage(hFontSizeList, LB_GETCURSEL, 0, 0);
TCHAR *sizeBuf = new TCHAR[SendMessage(hFontSizeList, LB_GETTEXTLEN,
fontSizeIndex, 0) + 1];
SendMessage(hFontSizeList, LB_GETTEXT, fontSizeIndex, (LPARAM)sizeBuf);
int pointSize = _ttoi(sizeBuf) * 10;
HWND hFontStyleList = GetDlgItem(hDlg, IDC_CHOOSE_FONT_STYLE_LIST);
int fontStyleIndex = SendMessage(hFontStyleList, LB_GETCURSEL, 0, 0);
bool isBold = (fontStyleIndex & 0x02) != 0;
bool isItalic = (fontStyleIndex & 0x01) != 0;
data->hCurrentFont = knceCreateFont(fontNameBuf, pointSize, isBold,
isItalic);
delete [] fontNameBuf;
delete [] sizeBuf;
HWND hExampleLabel = GetDlgItem(hDlg, IDC_CHOOSE_FONT_EXAMPLE_LABEL);
SendMessage(hExampleLabel, WM_SETFONT, (WPARAM)data->hCurrentFont, 0);
}