6 * Copyright (C) 2007 Timo Kreuzer <timo <dot> kreuzer <at> reactos <dot> org>
7 * Copyright (C) 2016-2017 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 HINSTANCE g_hInstance
;
36 LOGFONTW g_LogFonts
[64];
38 WCHAR g_FontTitle
[1024] = L
"";
40 static const WCHAR g_szFontViewClassName
[] = L
"FontViewWClass";
42 /* GetFontResourceInfoW is undocumented */
43 BOOL WINAPI
GetFontResourceInfoW(LPCWSTR lpFileName
, DWORD
*pdwBufSize
, void* lpBuffer
, DWORD dwType
);
60 len
= LoadStringW(hInstance
, dwStringId
, (LPWSTR
)Buffer
, 1000);
64 dwFlags
|= FORMAT_MESSAGE_FROM_STRING
;
65 dwFlags
&= ~(FORMAT_MESSAGE_FROM_HMODULE
| FORMAT_MESSAGE_FROM_SYSTEM
);
66 dwRet
= FormatMessageW(dwFlags
, Buffer
, 0, dwLanguageId
, lpBuffer
, nSize
, Arguments
);
73 ErrorMsgBox(HWND hParent
, DWORD dwMessageId
, ...)
75 HLOCAL hMemCaption
= NULL
;
76 HLOCAL hMemText
= NULL
;
79 va_start(args
, dwMessageId
);
80 FormatString(FORMAT_MESSAGE_ALLOCATE_BUFFER
,
81 NULL
, dwMessageId
, 0, (LPWSTR
)&hMemText
, 0, &args
);
84 FormatString(FORMAT_MESSAGE_ALLOCATE_BUFFER
,
85 NULL
, IDS_ERROR
, 0, (LPWSTR
)&hMemCaption
, 0, NULL
);
87 MessageBoxW(hParent
, hMemText
, hMemCaption
, MB_ICONERROR
);
89 LocalFree(hMemCaption
);
94 WinMain (HINSTANCE hThisInstance
,
95 HINSTANCE hPrevInstance
,
102 WCHAR szFileName
[MAX_PATH
] = L
"";
109 switch (GetUserDefaultUILanguage())
111 case MAKELANGID(LANG_HEBREW
, SUBLANG_DEFAULT
):
112 SetProcessDefaultLayout(LAYOUT_RTL
);
119 g_hInstance
= hThisInstance
;
121 /* Get unicode command line */
122 argv
= CommandLineToArgvW(GetCommandLineW(), &argc
);
125 OPENFILENAMEW fontOpen
;
126 WCHAR filter
[MAX_PATH
*2], dialogTitle
[MAX_PATH
];
128 LoadStringW(NULL
, IDS_OPEN
, dialogTitle
, ARRAYSIZE(dialogTitle
));
129 LoadStringW(NULL
, IDS_FILTER_LIST
, filter
, ARRAYSIZE(filter
));
131 /* Clears out any values of fontOpen before we use it */
132 ZeroMemory(&fontOpen
, sizeof(fontOpen
));
134 /* Sets up the open dialog box */
135 fontOpen
.lStructSize
= sizeof(fontOpen
);
136 fontOpen
.hwndOwner
= NULL
;
137 fontOpen
.lpstrFilter
= filter
;
138 fontOpen
.lpstrFile
= szFileName
;
139 fontOpen
.lpstrTitle
= dialogTitle
;
140 fontOpen
.nMaxFile
= MAX_PATH
;
141 fontOpen
.Flags
= OFN_EXPLORER
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
;
142 fontOpen
.lpstrDefExt
= L
"ttf";
144 /* Opens up the Open File dialog box in order to chose a font file. */
145 if(GetOpenFileNameW(&fontOpen
))
147 fileName
= fontOpen
.lpstrFile
;
148 g_fileName
= fileName
;
150 /* If the user decides to close out of the open dialog effectively
151 exiting the program altogether */
157 /* Try to add the font resource from command line */
159 g_fileName
= fileName
;
162 if (!AddFontResourceW(fileName
))
164 ErrorMsgBox(0, IDS_ERROR_NOFONT
, fileName
);
168 /* Get the font name */
169 dwSize
= sizeof(g_LogFonts
);
170 ZeroMemory(g_LogFonts
, sizeof(g_LogFonts
));
171 if (!GetFontResourceInfoW(fileName
, &dwSize
, g_LogFonts
, 2))
173 ErrorMsgBox(0, IDS_ERROR_NOFONT
, fileName
);
177 for (i
= 0; i
< ARRAYSIZE(g_LogFonts
); ++i
)
179 if (g_LogFonts
[i
].lfFaceName
[0] == 0)
186 ErrorMsgBox(0, IDS_ERROR_NOFONT
, fileName
);
191 dwSize
= sizeof(g_FontTitle
);
192 ZeroMemory(g_FontTitle
, sizeof(g_FontTitle
));
193 GetFontResourceInfoW(fileName
, &dwSize
, g_FontTitle
, 1);
195 if (!Display_InitClass(hThisInstance
))
197 ErrorMsgBox(0, IDS_ERROR_NOCLASS
);
201 /* The main window class */
202 wincl
.cbSize
= sizeof (WNDCLASSEXW
);
203 wincl
.style
= CS_DBLCLKS
;
204 wincl
.lpfnWndProc
= MainWndProc
;
205 wincl
.cbClsExtra
= 0;
206 wincl
.cbWndExtra
= 0;
207 wincl
.hInstance
= hThisInstance
;
208 wincl
.hIcon
= LoadIcon (GetModuleHandle(NULL
), MAKEINTRESOURCE(IDI_TT
));
209 wincl
.hCursor
= LoadCursor (NULL
, IDC_ARROW
);
210 wincl
.hbrBackground
= (HBRUSH
)COLOR_BACKGROUND
;
211 wincl
.lpszMenuName
= NULL
;
212 wincl
.lpszClassName
= g_szFontViewClassName
;
213 wincl
.hIconSm
= LoadIcon (GetModuleHandle(NULL
), MAKEINTRESOURCE(IDI_TT
));
215 /* Register the window class, and if it fails quit the program */
216 if (!RegisterClassExW (&wincl
))
218 ErrorMsgBox(0, IDS_ERROR_NOCLASS
);
222 /* The class is registered, let's create the main window */
223 hMainWnd
= CreateWindowExW(
224 0, /* Extended possibilities for variation */
225 g_szFontViewClassName
, /* Classname */
226 g_FontTitle
, /* Title Text */
227 WS_OVERLAPPEDWINDOW
, /* default window */
228 CW_USEDEFAULT
, /* Windows decides the position */
229 CW_USEDEFAULT
, /* where the window ends up on the screen */
230 544, /* The programs width */
231 375, /* and height in pixels */
232 HWND_DESKTOP
, /* The window is a child-window to desktop */
234 hThisInstance
, /* Program Instance handler */
235 NULL
/* No Window Creation data */
237 ShowWindow(hMainWnd
, nCmdShow
);
239 /* Main message loop */
240 while (GetMessage (&msg
, NULL
, 0, 0))
242 if (IsDialogMessage(hMainWnd
, &msg
))
244 TranslateMessage(&msg
);
245 DispatchMessage(&msg
);
248 RemoveFontResourceW(argv
[1]);
250 return (int)msg
.wParam
;
254 MainWnd_OnCreate(HWND hwnd
)
256 WCHAR szQuit
[MAX_BUTTONNAME
];
257 WCHAR szPrint
[MAX_BUTTONNAME
];
258 WCHAR szString
[MAX_STRING
];
259 WCHAR szPrevious
[MAX_STRING
];
260 WCHAR szNext
[MAX_STRING
];
261 HWND hDisplay
, hButtonInstall
, hButtonPrint
, hButtonPrev
, hButtonNext
;
263 /* create the display window */
264 hDisplay
= CreateWindowExW(
265 0, /* Extended style */
266 g_szFontDisplayClassName
, /* Classname */
267 L
"", /* Title text */
268 WS_CHILD
| WS_VSCROLL
, /* Window style */
270 HEADER_SIZE
, /* Y-Pos */
272 370-HEADER_SIZE
, /* Height */
274 (HMENU
)IDC_DISPLAY
, /* Identifier */
275 g_hInstance
, /* Program Instance handler */
276 NULL
/* Window Creation data */
279 LoadStringW(g_hInstance
, IDS_STRING
, szString
, MAX_STRING
);
280 SendMessage(hDisplay
, FVM_SETSTRING
, 0, (LPARAM
)szString
);
282 /* Create the install button */
283 LoadStringW(g_hInstance
, IDS_INSTALL
, szQuit
, MAX_BUTTONNAME
);
284 hButtonInstall
= CreateWindowExW(
285 0, /* Extended style */
286 L
"button", /* Classname */
287 szQuit
, /* Title text */
288 WS_CHILD
| WS_VISIBLE
, /* Window style */
289 BUTTON_POS_X
, /* X-pos */
290 BUTTON_POS_Y
, /* Y-Pos */
291 BUTTON_WIDTH
, /* Width */
292 BUTTON_HEIGHT
, /* Height */
294 (HMENU
)IDC_INSTALL
, /* Identifier */
295 g_hInstance
, /* Program Instance handler */
296 NULL
/* Window Creation data */
298 SendMessage(hButtonInstall
, WM_SETFONT
, (WPARAM
)GetStockObject(DEFAULT_GUI_FONT
), (LPARAM
)TRUE
);
300 /* Create the print button */
301 LoadStringW(g_hInstance
, IDS_PRINT
, szPrint
, MAX_BUTTONNAME
);
302 hButtonPrint
= CreateWindowExW(
303 0, /* Extended style */
304 L
"button", /* Classname */
305 szPrint
, /* Title text */
306 WS_CHILD
| WS_VISIBLE
, /* Window style */
308 BUTTON_POS_Y
, /* Y-Pos */
309 BUTTON_WIDTH
, /* Width */
310 BUTTON_HEIGHT
, /* Height */
312 (HMENU
)IDC_PRINT
, /* Identifier */
313 g_hInstance
, /* Program Instance handler */
314 NULL
/* Window Creation data */
316 SendMessage(hButtonPrint
, WM_SETFONT
, (WPARAM
)GetStockObject(DEFAULT_GUI_FONT
), (LPARAM
)TRUE
);
318 /* Create the previous button */
319 LoadStringW(g_hInstance
, IDS_PREVIOUS
, szPrevious
, MAX_BUTTONNAME
);
320 hButtonPrev
= CreateWindowExW(
321 0, /* Extended style */
322 L
"button", /* Classname */
323 szPrevious
, /* Title text */
324 WS_CHILD
| WS_VISIBLE
, /* Window style */
326 BUTTON_POS_Y
, /* Y-Pos */
327 BUTTON_WIDTH
, /* Width */
328 BUTTON_HEIGHT
, /* Height */
330 (HMENU
)IDC_PREV
, /* Identifier */
331 g_hInstance
, /* Program Instance handler */
332 NULL
/* Window Creation data */
334 SendMessage(hButtonPrev
, WM_SETFONT
, (WPARAM
)GetStockObject(DEFAULT_GUI_FONT
), (LPARAM
)TRUE
);
336 /* Create the next button */
337 LoadStringW(g_hInstance
, IDS_NEXT
, szNext
, MAX_BUTTONNAME
);
338 hButtonNext
= CreateWindowExW(
339 0, /* Extended style */
340 L
"button", /* Classname */
341 szNext
, /* Title text */
342 WS_CHILD
| WS_VISIBLE
, /* Window style */
344 BUTTON_POS_Y
, /* Y-Pos */
345 BUTTON_WIDTH
, /* Width */
346 BUTTON_HEIGHT
, /* Height */
348 (HMENU
)IDC_NEXT
, /* Identifier */
349 g_hInstance
, /* Program Instance handler */
350 NULL
/* Window Creation data */
352 SendMessage(hButtonNext
, WM_SETFONT
, (WPARAM
)GetStockObject(DEFAULT_GUI_FONT
), (LPARAM
)TRUE
);
354 EnableWindow(hButtonPrev
, FALSE
);
356 EnableWindow(hButtonNext
, FALSE
);
358 /* Init the display window with the font name */
360 SendMessage(hDisplay
, FVM_SETTYPEFACE
, 0, (LPARAM
)&g_LogFonts
[g_FontIndex
]);
361 ShowWindow(hDisplay
, SW_SHOWNORMAL
);
367 MainWnd_OnSize(HWND hwnd
)
370 HWND hInstall
, hPrint
, hPrev
, hNext
, hDisplay
;
373 GetClientRect(hwnd
, &rc
);
375 hDWP
= BeginDeferWindowPos(5);
377 hInstall
= GetDlgItem(hwnd
, IDC_INSTALL
);
379 hDWP
= DeferWindowPos(hDWP
, hInstall
, NULL
, BUTTON_POS_X
, BUTTON_POS_Y
, BUTTON_WIDTH
, BUTTON_HEIGHT
, SWP_NOZORDER
);
381 hPrint
= GetDlgItem(hwnd
, IDC_PRINT
);
383 hDWP
= DeferWindowPos(hDWP
, hPrint
, NULL
, BUTTON_POS_X
+ BUTTON_WIDTH
+ BUTTON_PADDING
, BUTTON_POS_Y
, BUTTON_WIDTH
, BUTTON_HEIGHT
, SWP_NOZORDER
);
385 hPrev
= GetDlgItem(hwnd
, IDC_PREV
);
387 hDWP
= DeferWindowPos(hDWP
, hPrev
, NULL
, rc
.right
- (BUTTON_WIDTH
* 2 + BUTTON_PADDING
+ BUTTON_POS_X
), BUTTON_POS_Y
, BUTTON_WIDTH
, BUTTON_HEIGHT
, SWP_NOZORDER
);
389 hNext
= GetDlgItem(hwnd
, IDC_NEXT
);
391 hDWP
= DeferWindowPos(hDWP
, hNext
, NULL
, rc
.right
- (BUTTON_WIDTH
+ BUTTON_POS_X
), BUTTON_POS_Y
, BUTTON_WIDTH
, BUTTON_HEIGHT
, SWP_NOZORDER
);
393 hDisplay
= GetDlgItem(hwnd
, IDC_DISPLAY
);
395 hDWP
= DeferWindowPos(hDWP
, hDisplay
, NULL
, 0, HEADER_SIZE
, rc
.right
, rc
.bottom
- HEADER_SIZE
, SWP_NOZORDER
);
397 EndDeferWindowPos(hDWP
);
399 InvalidateRect(hwnd
, NULL
, TRUE
);
405 MainWnd_OnPaint(HWND hwnd
)
411 hDC
= BeginPaint(hwnd
, &ps
);
412 GetClientRect(hwnd
, &rc
);
413 rc
.top
= HEADER_SIZE
- 2;
414 rc
.bottom
= HEADER_SIZE
;
415 FillRect(hDC
, &rc
, GetStockObject(GRAY_BRUSH
));
421 MainWnd_OnInstall(HWND hwnd
)
425 /* First, we have to find out if the font still exists. */
426 fontExists
= GetFileAttributes((LPCSTR
)g_fileName
);
427 if (fontExists
!= 0xFFFFFFFF) /* If the file does not exist */
429 ErrorMsgBox(0, IDS_ERROR_NOFONT
, g_fileName
);
433 //CopyFile(g_fileName, NULL, TRUE);
435 MessageBox(hwnd
, TEXT("This function is unimplemented"), TEXT("Unimplemented"), MB_OK
);
441 MainWnd_OnPrev(HWND hwnd
)
447 EnableWindow(GetDlgItem(hwnd
, IDC_NEXT
), TRUE
);
448 if (g_FontIndex
== 0)
449 EnableWindow(GetDlgItem(hwnd
, IDC_PREV
), FALSE
);
451 hDisplay
= GetDlgItem(hwnd
, IDC_DISPLAY
);
452 SendMessage(hDisplay
, FVM_SETTYPEFACE
, 0, (LPARAM
)&g_LogFonts
[g_FontIndex
]);
453 InvalidateRect(hDisplay
, NULL
, TRUE
);
459 MainWnd_OnNext(HWND hwnd
)
462 if (g_FontIndex
+ 1 < g_NumFonts
)
465 EnableWindow(GetDlgItem(hwnd
, IDC_PREV
), TRUE
);
466 if (g_FontIndex
== g_NumFonts
- 1)
467 EnableWindow(GetDlgItem(hwnd
, IDC_NEXT
), FALSE
);
469 hDisplay
= GetDlgItem(hwnd
, IDC_DISPLAY
);
470 SendMessage(hDisplay
, FVM_SETTYPEFACE
, 0, (LPARAM
)&g_LogFonts
[g_FontIndex
]);
471 InvalidateRect(hDisplay
, NULL
, TRUE
);
477 MainWndProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
482 return MainWnd_OnCreate(hwnd
);
485 return MainWnd_OnPaint(hwnd
);
488 return MainWnd_OnSize(hwnd
);
491 switch(LOWORD(wParam
))
494 return MainWnd_OnInstall(hwnd
);
497 return Display_OnPrint(hwnd
);
500 return MainWnd_OnPrev(hwnd
);
503 return MainWnd_OnNext(hwnd
);
508 PostQuitMessage (0); /* send a WM_QUIT to the message queue */
511 default: /* for messages that we don't deal with */
512 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);