|
|
1.1 root 1: // dialogs.cpp : Several simple dialogs for the ShowFont main window.
2: //
3: // This is a part of the Microsoft Foundation Classes C++ library.
4: // Copyright (C) 1992 Microsoft Corporation
5: // All rights reserved.
6: //
7: // This source code is only intended as a supplement to the
8: // Microsoft Foundation Classes Reference and Microsoft
9: // QuickHelp documentation provided with the library.
10: // See these sources for detailed information regarding the
11: // Microsoft Foundation Classes product.
12:
13: #include "showfont.h"
14:
15: static int CurrentFont = 0;
16: static int CurrentSize = 0;
17: static char FontList[MAXFONT][32];
18: static BYTE CharSet[MAXFONT];
19: static BYTE PitchAndFamily[MAXFONT];
20: static int FontIndex = 0;
21: static int SizeList[MAXSIZE];
22: static int SizeIndex = 0;
23:
24: static CStringList fontList; // list of added fonts
25:
26: /////////////////////////////////////////////////////////////////////////////
27: // forward declarations
28:
29: static void GetSizes(CWnd* wnd, int iCurrentFont);
30:
31: static CString SeparateFile(char* pszDestPath, const char* pszSrcFileName);
32:
33: /////////////////////////////////////////////////////////////////////////////
34: // About dialog
35:
36: void CMainWindow::OnAbout()
37: {
38: CModalDialog about("AboutBox", this);
39: about.DoModal();
40: }
41:
42: /////////////////////////////////////////////////////////////////////////////
43: // Select Font dialog
44:
45: class CSelFtDlg : public CModalDialog
46: {
47: public:
48: CSelFtDlg() : CModalDialog("SelectFont")
49: { }
50:
51: CListBox& TypefaceList()
52: { return *((CListBox*) GetDlgItem(ID_TYPEFACE)); }
53: CListBox& TypesizeList()
54: { return *((CListBox*) GetDlgItem(ID_SIZE)); }
55:
56: BOOL OnInitDialog()
57: {
58: CListBox& facesList = TypefaceList();
59: CListBox& sizesList = TypesizeList();
60:
61: for (int i = 0; i < FontIndex; i++)
62: {
63: // Display available fonts.
64: //
65: facesList.AddString(FontList[i]);
66: facesList.SetCurSel(0);
67: }
68: GetSizes(this, 0);
69: for (i = 0; i < SizeIndex; i++)
70: {
71: // Display font sizes.
72: //
73: char buf[LF_FACESIZE];
74: sprintf(buf, "%d", SizeList[i]);
75: sizesList.AddString(buf);
76: sizesList.SetCurSel(0);
77: }
78: return TRUE;
79: }
80:
81: afx_msg void OnOK()
82: {
83: int index;
84: if ((index = TypefaceList().GetCurSel()) == LB_ERR)
85: {
86: MessageBox("No font selected",
87: "Select Font", MB_OK | MB_ICONEXCLAMATION);
88: return;
89: }
90: CurrentFont = index;
91:
92: if ((index = TypesizeList().GetCurSel()) == LB_ERR)
93: {
94: MessageBox("No size selected",
95: "Select Font", MB_OK | MB_ICONEXCLAMATION);
96: return;
97: }
98: CurrentSize = index;
99: EndDialog(IDOK);
100: }
101:
102: afx_msg void OnTypeFaceChange()
103: {
104: int index = TypefaceList().GetCurSel();
105: if (index == LB_ERR)
106: return;
107: TypesizeList().ResetContent();
108: GetSizes(this, index);
109:
110: CListBox& sizesList = TypesizeList();
111: for (int i = 0; i < SizeIndex; i++)
112: {
113: char buf[LF_FACESIZE];
114: sprintf(buf, "%d", SizeList[i]);
115: sizesList.AddString(buf);
116: sizesList.SetCurSel(0);
117: }
118: }
119:
120: DECLARE_MESSAGE_MAP()
121: };
122:
123: BEGIN_MESSAGE_MAP(CSelFtDlg, CModalDialog)
124: ON_LBN_SELCHANGE(ID_TYPEFACE, OnTypeFaceChange)
125:
126: // Double-click on the listboxes act like clicking OK. No extra code!
127: ON_LBN_DBLCLK(ID_TYPEFACE, OnOK)
128: ON_LBN_DBLCLK(ID_SIZE, OnOK)
129: END_MESSAGE_MAP()
130:
131: void CMainWindow::OnSelectFont()
132: {
133: CSelFtDlg dlg;
134: if (dlg.DoModal() != IDOK)
135: return; // cancelled
136:
137: // change the font
138: myFont->DeleteObject();
139: myFont->CreateFont(SizeList[CurrentSize],
140: 0, 0, 0, FW_NORMAL,
141: FALSE, FALSE, FALSE,
142: CharSet[CurrentFont],
143: OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
144: PitchAndFamily[CurrentFont],
145: FontList[CurrentFont]);
146: pTheFont = myFont;
147: SetFaceName();
148: }
149:
150: /////////////////////////////////////////////////////////////////////////////
151: // TextMetrics modeless dialog
152:
153: class CMetricDlg : public CDialog // modeless
154: {
155: private:
156: const TEXTMETRIC& textMetric;
157:
158: public:
159: CMetricDlg(TEXTMETRIC& rtextMetric) : textMetric(rtextMetric)
160: {
161: VERIFY(Create("MetricBox"));
162: }
163:
164: afx_msg void OnClose()
165: {
166: delete this; // Note this does the DestroyWindow automatically.
167: }
168:
169: BOOL OnInitDialog();
170:
171: DECLARE_MESSAGE_MAP()
172: };
173:
174: BEGIN_MESSAGE_MAP(CMetricDlg, CDialog)
175: ON_WM_CLOSE()
176: END_MESSAGE_MAP()
177:
178: BOOL CMetricDlg::OnInitDialog()
179: {
180: // fill in the dialog with statistics
181: SetDlgItemInt(IDMB_HEIGHT, textMetric.tmHeight, FALSE);
182: SetDlgItemInt(IDMB_ASCENT, textMetric.tmAscent, FALSE);
183: SetDlgItemInt(IDMB_DESCENT, textMetric.tmDescent, FALSE);
184: SetDlgItemInt(IDMB_INTERNALLEADING, textMetric.tmInternalLeading, FALSE);
185: SetDlgItemInt(IDMB_EXTERNALLEADING, textMetric.tmExternalLeading, FALSE);
186: SetDlgItemInt(IDMB_AVECHARWIDTH, textMetric.tmAveCharWidth, FALSE);
187: SetDlgItemInt(IDMB_MAXCHARWIDTH, textMetric.tmMaxCharWidth, FALSE);
188: SetDlgItemInt(IDMB_WEIGHT, textMetric.tmWeight, FALSE);
189: SetDlgItemInt(IDMB_ITALIC, textMetric.tmItalic, FALSE);
190: SetDlgItemInt(IDMB_UNDERLINED, textMetric.tmUnderlined, FALSE);
191: SetDlgItemInt(IDMB_STRUCKOUT, textMetric.tmStruckOut, FALSE);
192: SetDlgItemInt(IDMB_FIRSTCHAR, textMetric.tmFirstChar, FALSE);
193: SetDlgItemInt(IDMB_LASTCHAR, textMetric.tmLastChar, FALSE);
194: SetDlgItemInt(IDMB_DEFAULTCHAR, textMetric.tmDefaultChar, FALSE);
195: SetDlgItemInt(IDMB_BREAKCHAR, textMetric.tmBreakChar, FALSE);
196: SetDlgItemInt(IDMB_PITCHANDFAMILY, textMetric.tmPitchAndFamily, FALSE);
197: SetDlgItemInt(IDMB_CHARSET, textMetric.tmCharSet, FALSE);
198: SetDlgItemInt(IDMB_OVERHANG, textMetric.tmOverhang, FALSE);
199: SetDlgItemInt(IDMB_DIGITIZEDASPECTX, textMetric.tmDigitizedAspectX, FALSE);
200: SetDlgItemInt(IDMB_DIGITIZEDASPECTY, textMetric.tmDigitizedAspectY, FALSE);
201: return TRUE;
202: }
203:
204:
205: void CMainWindow::OnShowTextMetric()
206: {
207: CClientDC dc(this);
208: CFont* oldFont = dc.SelectObject(pTheFont);
209: if (oldFont == NULL)
210: return;
211:
212: TEXTMETRIC textMetric;
213: dc.GetTextMetrics(&textMetric);
214:
215: CMetricDlg* pDlg;
216: pDlg = new CMetricDlg(textMetric);
217:
218: char szDialogTitle[100];
219: char buf[80];
220: strcpy(szDialogTitle, "Metric Font: ");
221: dc.GetTextFace(80, buf);
222: strcat(szDialogTitle, buf);
223: pDlg->SetWindowText(szDialogTitle);
224:
225: dc.SelectObject(oldFont);
226: }
227:
228: /////////////////////////////////////////////////////////////////////////////
229: // Show LogicalFont modeless dialog
230:
231: class CLogFontDlg : public CDialog // modeless
232: {
233: private:
234: const LOGFONT& logFont; // just needed for init
235:
236: public:
237: CLogFontDlg(const LOGFONT& rLogFont) : logFont(rLogFont)
238: {
239: VERIFY(Create("LogBox"));
240: }
241:
242: afx_msg void OnClose()
243: {
244: delete this; // Does DestroyWindow for us.
245: }
246:
247: BOOL OnInitDialog();
248:
249: DECLARE_MESSAGE_MAP()
250: };
251:
252: BEGIN_MESSAGE_MAP(CLogFontDlg, CDialog)
253: ON_WM_CLOSE()
254: END_MESSAGE_MAP()
255:
256: BOOL CLogFontDlg::OnInitDialog()
257: {
258: // Fill in the dialog with statistics.
259: SetDlgItemInt(IDMI_HEIGHT, logFont.lfHeight, FALSE);
260: SetDlgItemInt(IDMI_WIDTH, logFont.lfWidth, FALSE);
261: SetDlgItemInt(IDMI_ESCAPEMENT, logFont.lfEscapement, FALSE);
262: SetDlgItemInt(IDMI_ORIENTATION, logFont.lfOrientation, FALSE);
263: SetDlgItemInt(IDMI_WEIGHT, logFont.lfWeight, FALSE);
264: SetDlgItemInt(IDMI_ITALIC, logFont.lfItalic, FALSE);
265: SetDlgItemInt(IDMI_UNDERLINED, logFont.lfUnderline, FALSE);
266: SetDlgItemInt(IDMI_STRIKEOUT, logFont.lfStrikeOut, FALSE);
267: SetDlgItemInt(IDMI_CHARSET, logFont.lfCharSet, FALSE);
268: SetDlgItemInt(IDMI_OUTPRECISION, logFont.lfOutPrecision, FALSE);
269: SetDlgItemInt(IDMI_CLIPPRECISION, logFont.lfClipPrecision, FALSE);
270: SetDlgItemInt(IDMI_QUALITY, logFont.lfQuality, FALSE);
271: SetDlgItemInt(IDMI_PITCHANDFAMILY, logFont.lfPitchAndFamily, FALSE);
272: return TRUE;
273: }
274:
275: void CMainWindow::OnShowLogFont()
276: {
277: CClientDC dc(this);
278: CFont* oldFont = dc.SelectObject(&systemFont);
279: if (oldFont == NULL)
280: return;
281:
282: TEXTMETRIC TextMetric;
283:
284: dc.GetTextMetrics(&TextMetric);
285: nLineSpace = TextMetric.tmHeight + TextMetric.tmExternalLeading;
286: LOGFONT logFont;
287: pTheFont->GetObject(sizeof(LOGFONT), &logFont);
288:
289: CLogFontDlg* pDlg;
290: pDlg = new CLogFontDlg(logFont); // logFont just needed for init
291: char szDialogTitle[100];
292: strcpy(szDialogTitle, "Log Font: ");
293: strcat(szDialogTitle, (const char*)logFont.lfFaceName);
294: pDlg->SetWindowText(szDialogTitle);
295:
296: dc.SelectObject(oldFont);
297: }
298:
299: /////////////////////////////////////////////////////////////////////////////
300: // Add Font dialog
301:
302: class CAddFontDlg : public CModalDialog
303: {
304: public:
305: CAddFontDlg() : CModalDialog("Add")
306: { }
307:
308: // Attributes
309: char m_szPath[256]; // not a CString since we use it
310: // as parameter to DlgDirSelect
311:
312: // Implementation
313: BOOL OnInitDialog();
314: afx_msg void OnOK();
315: afx_msg void OnFileChange();
316: DECLARE_MESSAGE_MAP()
317: };
318:
319: BOOL CAddFontDlg::OnInitDialog()
320: {
321: SetWindowText("Add Font Resource");
322: DlgDirList(m_szPath, ID_LISTBOX, ID_PATH, 0x4010);
323: return TRUE;
324: }
325:
326: BEGIN_MESSAGE_MAP(CAddFontDlg, CModalDialog)
327: ON_LBN_SELCHANGE(ID_LISTBOX, OnFileChange)
328: ON_LBN_DBLCLK(ID_LISTBOX, OnOK)
329: END_MESSAGE_MAP()
330:
331: void CAddFontDlg::OnFileChange()
332: {
333: // If item is a directory name, append "*.fon".
334: //
335: if (DlgDirSelect(m_szPath, ID_LISTBOX))
336: strcat(m_szPath, "*.fon");
337: DlgDirList(m_szPath, ID_LISTBOX, ID_PATH, 0x4010);
338: }
339:
340: void CAddFontDlg::OnOK()
341: {
342: // Get the filename from the edit control.
343: //
344: m_szPath[0] = '\0';
345: if (DlgDirSelect(m_szPath, ID_LISTBOX) ||
346: m_szPath[0] == '\0')
347: {
348: // It's still a directory, or something is wrong.
349: //
350: MessageBox("Not a file", "Add Font", MB_OK | MB_ICONQUESTION);
351: return;
352: }
353:
354: // Assume the file is worth trying.
355: //
356: EndDialog(IDOK);
357: }
358:
359: void CMainWindow::OnAddFont()
360: {
361: // Spawn dialog to get the filename.
362: //
363: CAddFontDlg dlg;
364:
365: strcpy(dlg.m_szPath, "*.fon");
366: if (dlg.DoModal() != IDOK)
367: return; // cancelled
368:
369: // Check to see if it is a new font name.
370: //
371: for (POSITION pos = fontList.GetHeadPosition(); pos != NULL; )
372: if (fontList.GetNext(pos) == dlg.m_szPath)
373: {
374: MessageBox("Font already exists", "Add Font",
375: MB_OK | MB_ICONQUESTION);
376: return;
377: }
378:
379: // Tell Windows to add the font resource.
380: //
381: if (AddFontResource(dlg.m_szPath) == 0)
382: {
383: MessageBox("No font loaded", "Add Font", MB_OK | MB_ICONQUESTION);
384: return;
385: }
386:
387: // Let all applications know there is a new font resource.
388: //
389: ::SendMessage((HWND) 0xFFFF, WM_FONTCHANGE, NULL, (LONG) NULL);
390:
391: // Add it to our font list.
392: //
393: fontList.AddTail(dlg.m_szPath); // save copy of string
394: }
395:
396: /////////////////////////////////////////////////////////////////////////////
397: // Remove Font dialog
398:
399: class CRmvFtDlg : public CModalDialog
400: {
401: public:
402: int m_iFont; // font index (from global fontList)
403:
404: CRmvFtDlg() : CModalDialog("Remove")
405: { }
406:
407: CListBox& FileList()
408: {
409: return *((CListBox*) GetDlgItem(ID_LISTBOX));
410: }
411:
412: BOOL OnInitDialog();
413: afx_msg void OnOK();
414:
415: DECLARE_MESSAGE_MAP()
416: };
417:
418: BOOL CRmvFtDlg::OnInitDialog()
419: {
420: SetWindowText("Remove Font Resource");
421:
422: for (POSITION pos = fontList.GetHeadPosition(); pos != NULL; )
423: FileList().AddString(fontList.GetNext(pos));
424: return TRUE;
425: }
426:
427: BEGIN_MESSAGE_MAP(CRmvFtDlg, CModalDialog)
428: // A double-click on the listbox aliases for clicking the OK.
429: ON_LBN_DBLCLK(ID_LISTBOX, OnOK)
430: END_MESSAGE_MAP()
431:
432: void CRmvFtDlg::OnOK()
433: {
434: // Get the filename from the edit control.
435: //
436: m_iFont = FileList().GetCurSel();
437:
438: EndDialog(IDOK);
439: }
440:
441: void CMainWindow::OnDeleteFont()
442: {
443: if (fontList.IsEmpty())
444: {
445: MessageBox("No fonts to delete",
446: "Remove Font", MB_OK | MB_ICONQUESTION);
447: return;
448: }
449:
450: // Invoke dialog to let user select one font from list.
451: //
452: CRmvFtDlg dlg;
453: if (dlg.DoModal() != IDOK || dlg.m_iFont == -1)
454: return; // cancelled or no selection
455:
456: POSITION posFont = fontList.FindIndex(dlg.m_iFont);
457: ASSERT(posFont != NULL);
458:
459: // Remove it. Tell all the apps.
460: //
461: RemoveFontResource((LPSTR)(LPCSTR)fontList.GetAt(posFont));
462: ::SendMessage((HWND) 0xFFFF, WM_FONTCHANGE, NULL, (LONG) NULL);
463:
464: // Remove that element in the list.
465: //
466: fontList.RemoveAt(posFont);
467: }
468:
469: /////////////////////////////////////////////////////////////////////////////
470: // Cleanup on exit
471:
472: void CMainWindow::OnDestroy()
473: {
474: // Remove any fonts that were added.
475: //
476: while (!fontList.IsEmpty())
477: RemoveFontResource((LPSTR)(LPCSTR)fontList.RemoveHead());
478:
479: // Notify any other applications that the fonts have been deleted.
480: //
481: ::SendMessage((HWND) 0xFFFF, WM_FONTCHANGE, NULL, (LONG) NULL);
482:
483: // Terminate ourselves.
484: //
485: PostQuitMessage(0);
486: }
487:
488: /////////////////////////////////////////////////////////////////////////////
489: // Routines that must enumerate all fonts.
490: // (These keep the font names and sizes in global variables.)
491:
492: int FAR PASCAL _export EnumFunc(LPLOGFONT lpLogFont, LPTEXTMETRIC, short,
493: LPSTR lpData)
494: {
495: switch (LOWORD((DWORD)lpData))
496: {
497: case 0:
498: if (FontIndex >= MAXFONT)
499: return (0);
500: _fstrcpy(FontList[FontIndex], (PSTR)lpLogFont->lfFaceName);
501: CharSet[FontIndex] = lpLogFont->lfCharSet;
502: PitchAndFamily[FontIndex] = lpLogFont->lfPitchAndFamily;
503: return (++FontIndex);
504:
505: case 1:
506: if (SizeIndex >= MAXSIZE)
507: return (0);
508: SizeList[SizeIndex] = lpLogFont->lfHeight;
509: return (++SizeIndex);
510: }
511: ASSERT(FALSE);
512: return 0;
513: }
514:
515: void CMainWindow::OnFontChange()
516: {
517: FontIndex = 0;
518: SizeIndex = 0;
519: CClientDC dc(this);
520: #ifdef _NTWIN
521: ::EnumFonts(dc.m_hDC, NULL, (FONTENUMPROC)EnumFunc, NULL);
522: #else
523: ::EnumFonts(dc.m_hDC, NULL, (OLDFONTENUMPROC)EnumFunc, NULL);
524: #endif
525: }
526:
527: static void GetSizes(CWnd* wnd, int iCurrentFont)
528: {
529: SizeIndex = 0;
530: CClientDC dc(wnd);
531:
532: #ifdef _NTWIN
533: #ifdef STRICT
534: ::EnumFonts(dc.m_hDC, FontList[iCurrentFont], (FONTENUMPROC)EnumFunc, (LPARAM)1L);
535: #else
536: ::EnumFonts(dc.m_hDC, FontList[iCurrentFont], (FONTENUMPROC)EnumFunc, (LPARAM)1L);
537: #endif // STRICT
538: #else
539: #ifdef STRICT
540: ::EnumFonts(dc.m_hDC, FontList[iCurrentFont], (OLDFONTENUMPROC)EnumFunc, 1L);
541: #else
542: ::EnumFonts(dc.m_hDC, FontList[iCurrentFont], (OLDFONTENUMPROC)EnumFunc, (LPSTR)1L);
543: #endif // STRICT
544: #endif // NTWIN
545: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.