2 * fontview display class
6 * Copyright (C) 2007 Timo Kreuzer <timo <dot> kreuzer <at> reactos <dot> org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 const WCHAR g_szFontDisplayClassName
[] = L
"FontDisplayClass";
32 LRESULT CALLBACK
DisplayProc(HWND
, UINT
, WPARAM
, LPARAM
);
34 /* Internal data storage type */
38 WCHAR szTypeFaceName
[LF_FULLFACESIZE
];
39 WCHAR szFormat
[MAX_FORMAT
];
40 WCHAR szString
[MAX_STRING
];
45 HFONT hFonts
[MAX_SIZES
];
46 int nSizes
[MAX_SIZES
];
47 int nHeights
[MAX_SIZES
];
50 /* This is the only public function, it registers the class */
52 Display_InitClass(HINSTANCE hInstance
)
56 /* Set the fontdisplay window class structure */
57 wincl
.cbSize
= sizeof(WNDCLASSEX
);
58 wincl
.style
= CS_DBLCLKS
;
59 wincl
.lpfnWndProc
= DisplayProc
;
62 wincl
.hInstance
= hInstance
;
64 wincl
.hCursor
= LoadCursor (NULL
, IDC_ARROW
);
65 wincl
.hbrBackground
= GetStockObject(WHITE_BRUSH
);
66 wincl
.lpszMenuName
= NULL
;
67 wincl
.lpszClassName
= g_szFontDisplayClassName
;
70 /* Register the window class, and if it fails return FALSE */
71 if (!RegisterClassExW (&wincl
))
79 Display_DrawText(HDC hDC
, DISPLAYDATA
* pData
, int nYPos
)
85 WCHAR szCaption
[LF_FULLFACESIZE
+ 20];
87 /* This is the location on the DC where we draw */
90 hOldFont
= SelectObject(hDC
, pData
->hCaptionFont
);
91 GetTextMetrics(hDC
, &tm
);
93 swprintf(szCaption
, L
"%s%s", pData
->szTypeFaceName
, pData
->szFormat
);
94 TextOutW(hDC
, 0, y
, szCaption
, (INT
)wcslen(szCaption
));
95 y
+= tm
.tmHeight
+ SPACING1
;
97 /* Draw a seperation Line */
98 SelectObject(hDC
, GetStockObject(BLACK_PEN
));
99 MoveToEx(hDC
, 0, y
, NULL
);
100 LineTo(hDC
, 10000, y
);
103 /* TODO: Output font info */
105 /* Output Character set */
106 SelectObject(hDC
, pData
->hCharSetFont
);
107 GetTextMetrics(hDC
, &tm
);
108 swprintf(szCaption
, L
"abcdefghijklmnopqrstuvwxyz");
109 TextOutW(hDC
, 0, y
, szCaption
, (INT
)wcslen(szCaption
));
110 y
+= tm
.tmHeight
+ 1;
112 swprintf(szCaption
, L
"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
113 TextOutW(hDC
, 0, y
, szCaption
, (INT
)wcslen(szCaption
));
114 y
+= tm
.tmHeight
+ 1;
116 swprintf(szCaption
, L
"0123456789.:,;(\"~!@#$%^&*')");
117 TextOutW(hDC
, 0, y
, szCaption
, (INT
)wcslen(szCaption
));
118 y
+= tm
.tmHeight
+ 1;
120 /* Draw a seperation Line */
121 SelectObject(hDC
, GetStockObject(BLACK_PEN
));
122 MoveToEx(hDC
, 0, y
, NULL
);
123 LineTo(hDC
, 10000, y
);
126 /* Output the strings for different sizes */
127 for (i
= 0; i
< MAX_SIZES
; i
++)
129 SelectObject(hDC
, pData
->hFonts
[i
]);
130 TextOutW(hDC
, 20, y
, pData
->szString
, (INT
)wcslen(pData
->szString
));
131 GetTextMetrics(hDC
, &tm
);
132 y
+= tm
.tmHeight
+ 1;
133 SelectObject(hDC
, pData
->hSizeFont
);
134 swprintf(szSize
, L
"%d", pData
->nSizes
[i
]);
135 TextOutW(hDC
, 0, y
- 13 - tm
.tmDescent
, szSize
, (INT
)wcslen(szSize
));
137 SelectObject(hDC
, hOldFont
);
145 const LOGFONTW
*lpelfe
,
146 const TEXTMETRICW
*lptm
,
150 PNEWTEXTMETRICW pntmw
= (PNEWTEXTMETRICW
)lptm
;
151 PBOOL pfOpenType
= (PBOOL
)lParam
;
153 if (FontType
& TRUETYPE_FONTTYPE
)
155 if (pntmw
->ntmFlags
& (NTM_TT_OPENTYPE
| NTM_PS_OPENTYPE
))
165 Display_SetTypeFace(HWND hwnd
, PEXTLOGFONTW pExtLogFont
)
175 /* Set the new type face name */
176 pData
= (DISPLAYDATA
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
177 _snwprintf(pData
->szTypeFaceName
, LF_FULLFACESIZE
, pExtLogFont
->elfFullName
);
179 /* Create the new fonts */
181 DeleteObject(pData
->hCharSetFont
);
183 logfont
= pExtLogFont
->elfLogFont
;
184 logfont
.lfHeight
= -MulDiv(16, GetDeviceCaps(GetDC(NULL
), LOGPIXELSY
), 72);
185 pData
->hCharSetFont
= CreateFontIndirectW(&logfont
);
187 /* Get font format */
188 SelectObject(hDC
, pData
->hCharSetFont
);
189 GetTextMetrics(hDC
, &tm
);
190 if (tm
.tmPitchAndFamily
& TMPF_TRUETYPE
)
192 BOOL fOpenType
= FALSE
;
194 EnumFontFamiliesExW(hDC
, &logfont
,
195 EnumFontFamProcW
, (LPARAM
)&fOpenType
, 0);
198 swprintf(pData
->szFormat
, L
" (OpenType)");
200 swprintf(pData
->szFormat
, L
" (TrueType)");
202 else if (tm
.tmPitchAndFamily
& TMPF_VECTOR
)
204 swprintf(pData
->szFormat
, L
" (Vector)");
208 swprintf(pData
->szFormat
, L
" (Raster)");
211 for (i
= 0; i
< MAX_SIZES
; i
++)
213 DeleteObject(pData
->hFonts
[i
]);
214 logfont
.lfHeight
= -MulDiv(pData
->nSizes
[i
], GetDeviceCaps(hDC
, LOGPIXELSY
), 72);
215 pData
->hFonts
[i
] = CreateFontIndirectW(&logfont
);
218 /* Calculate new page dimensions */
219 pData
->nPageHeight
= Display_DrawText(hDC
, pData
, 0);
220 ReleaseDC(hwnd
, hDC
);
222 /* Set the vertical scrolling range and page size */
223 GetClientRect(hwnd
, &rect
);
224 si
.cbSize
= sizeof(si
);
225 si
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_POS
| SIF_TRACKPOS
;
227 si
.nMax
= pData
->nPageHeight
;
228 si
.nPage
= rect
.bottom
;
231 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
237 Display_SetString(HWND hwnd
, LPARAM lParam
)
241 pData
= (DISPLAYDATA
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
242 _snwprintf(pData
->szString
, MAX_STRING
, (WCHAR
*)lParam
);
244 InvalidateRect(hwnd
, NULL
, TRUE
);
250 Display_OnCreate(HWND hwnd
)
253 const int nSizes
[MAX_SIZES
] = {8, 12, 18, 24, 36, 48, 60, 72};
255 EXTLOGFONTW ExtLogFont
= {{50, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
,
256 ANSI_CHARSET
, OUT_DEFAULT_PRECIS
,
257 CLIP_DEFAULT_PRECIS
, PROOF_QUALITY
,
258 DEFAULT_PITCH
, L
"Ms Shell Dlg"},
261 /* Create data structure */
262 pData
= malloc(sizeof(DISPLAYDATA
));
263 ZeroMemory(pData
, sizeof(DISPLAYDATA
));
265 /* Set the window's GWLP_USERDATA to our data structure */
266 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
)pData
);
268 for (i
= 0; i
< MAX_SIZES
; i
++)
270 pData
->nSizes
[i
] = nSizes
[i
];
273 pData
->hCaptionFont
= CreateFontIndirectW(&ExtLogFont
.elfLogFont
);
274 ExtLogFont
.elfLogFont
.lfHeight
= 12;
275 pData
->hSizeFont
= CreateFontIndirectW(&ExtLogFont
.elfLogFont
);
277 Display_SetString(hwnd
, (LPARAM
)L
"Jackdaws love my big sphinx of quartz. 1234567890");
279 Display_SetTypeFace(hwnd
, &ExtLogFont
);
285 Display_OnPaint(HWND hwnd
)
291 pData
= (DISPLAYDATA
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
293 /* Get the Scroll position */
294 si
.cbSize
= sizeof(si
);
296 GetScrollInfo(hwnd
, SB_VERT
, &si
);
298 BeginPaint(hwnd
, &ps
);
300 /* Erase background */
301 FillRect(ps
.hdc
, &ps
.rcPaint
, GetStockObject(WHITE_BRUSH
));
304 Display_DrawText(ps
.hdc
, pData
, si
.nPos
);
312 Display_OnSize(HWND hwnd
)
318 GetClientRect(hwnd
, &rect
);
320 /* Get the old scroll pos */
321 si
.cbSize
= sizeof(si
);
323 GetScrollInfo(hwnd
, SB_VERT
, &si
);
326 /* Set the new page size */
328 si
.nPage
= rect
.bottom
;
329 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
331 /* Get the new scroll pos */
333 GetScrollInfo(hwnd
, SB_VERT
, &si
);
335 /* If they don't match ... */
336 if (nOldPos
!= si
.nPos
)
338 /* ... scroll the window */
339 ScrollWindowEx(hwnd
, 0, nOldPos
- si
.nPos
, NULL
, NULL
, NULL
, NULL
, SW_INVALIDATE
);
347 Display_OnVScroll(HWND hwnd
, WPARAM wParam
)
352 si
.cbSize
= sizeof(si
);
353 si
.fMask
= SIF_POS
| SIF_RANGE
| SIF_TRACKPOS
;
354 GetScrollInfo(hwnd
, SB_VERT
, &si
);
356 switch(LOWORD(wParam
))
371 case SB_THUMBPOSITION
:
378 nPos
= max(nPos
, si
.nMin
);
379 nPos
= min(nPos
, si
.nMax
);
382 ScrollWindowEx(hwnd
, 0, si
.nPos
- nPos
, NULL
, NULL
, NULL
, NULL
, SW_INVALIDATE
);
383 si
.cbSize
= sizeof(si
);
386 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
394 Display_OnDestroy(HWND hwnd
)
399 pData
= (DISPLAYDATA
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
401 /* Delete the fonts */
402 DeleteObject(pData
->hCaptionFont
);
403 DeleteObject(pData
->hCharSetFont
);
404 DeleteObject(pData
->hSizeFont
);
406 for (i
= 0; i
< MAX_SIZES
; i
++)
408 DeleteObject(pData
->hFonts
[i
]);
411 /* Free the data structure */
418 Display_OnPrint(HWND hwnd
)
424 /* Clears the memory before using it */
425 ZeroMemory(&pfont
, sizeof(pfont
));
427 pfont
.lStructSize
= sizeof(pfont
);
428 pfont
.hwndOwner
= hwnd
;
429 pfont
.hDevMode
= NULL
;
430 pfont
.hDevNames
= NULL
;
431 pfont
.Flags
= PD_USEDEVMODECOPIESANDCOLLATE
| PD_RETURNDC
;
433 pfont
.nFromPage
= 0xFFFF;
434 pfont
.nToPage
= 0xFFFF;
436 pfont
.nMaxPage
= 0xFFFF;
438 /* Opens up the print dialog box */
439 if (PrintDlg(&pfont
))
445 pData
= malloc(sizeof(DISPLAYDATA
));
446 ZeroMemory(pData
, sizeof(DISPLAYDATA
));
448 /* Sets up the font layout */
449 pData
= (DISPLAYDATA
*)GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
451 docinfo
.cbSize
= sizeof(DOCINFO
);
452 docinfo
.lpszDocName
= "Printing Font";
453 docinfo
.lpszOutput
= NULL
;
454 docinfo
.lpszDatatype
= NULL
;
457 /* We start printing */
458 StartDoc(pfont
.hDC
, &docinfo
);
460 /* Grabs the text metrics for the printer */
461 GetTextMetrics(pfont
.hDC
, &tm
);
463 /* Start out with 0 for the y position for the page */
466 /* Starts out with the current page */
467 StartPage(pfont
.hDC
);
469 /* Used when printing for more than one copy */
470 for (copies
= 0; copies
< pfont
.nCopies
; copies
++)
473 TextOutW(pfont
.hDC
, 10, yPos
, L
"Testing...1...2...3", 19);
475 /* TODO: Determine if using Display_DrawText() will work for both rendering out to the
476 window and to the printer output */
478 Display_DrawText(pfont
.hDC
, pData
, yPos
);
481 /* Ends the current page */
484 /* If we are making more than one copy, start a new page */
485 if (copies
!= pfont
.nCopies
)
488 StartPage(pfont
.hDC
);
492 /* The printing is now over */
497 /* Frees the memory since we no longer need it for now */
506 DisplayProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
511 return Display_OnCreate(hwnd
);
514 return Display_OnPaint(hwnd
);
517 return Display_OnSize(hwnd
);
520 return Display_OnVScroll(hwnd
, wParam
);
522 case FVM_SETTYPEFACE
:
523 return Display_SetTypeFace(hwnd
, (PEXTLOGFONTW
)lParam
);
526 return Display_SetString(hwnd
, lParam
);
529 return Display_OnDestroy(hwnd
);
532 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);