ReactOS 0.4.15-dev-7897-g78dc504
fontwnd.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS VGA Font Editor
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Implements the MDI child window for a font
5 * COPYRIGHT: Copyright 2008 Colin Finck (colin@reactos.org)
6 */
7
8#include "precomp.h"
9
10static const WCHAR szFontWndClass[] = L"VGAFontEditFontWndClass";
11
12static BOOL
14{
15 Info->Font = (PBITMAP_FONT) HeapAlloc( hProcessHeap, 0, sizeof(BITMAP_FONT) );
16
17 if(Info->OpenInfo->bCreateNew)
18 {
19 ZeroMemory( Info->Font, sizeof(BITMAP_FONT) );
20 return TRUE;
21 }
22 else
23 {
24 // Load a font
25 BOOL bRet = FALSE;
26 DWORD dwTemp;
28
29 hFile = CreateFileW(Info->OpenInfo->pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
30
32 {
34 return FALSE;
35 }
36
37 // Let's first check the file size to determine the file type
38 dwTemp = GetFileSize(hFile, NULL);
39
40 switch(dwTemp)
41 {
42 case 2048:
43 // It should be a binary font file
44 Info->OpenInfo->bBinaryFileOpened = TRUE;
45
46 if( ReadFile(hFile, Info->Font, sizeof(BITMAP_FONT), &dwTemp, NULL) )
47 bRet = TRUE;
48 else
50
51 break;
52
53 case 2052:
54 {
56
57 // Probably it's a PSFv1 file, check the header to make sure
58 if( !ReadFile(hFile, &Header, sizeof(PSF1_HEADER) , &dwTemp, NULL) )
59 {
61 break;
62 }
63 else
64 {
65 if(Header.uMagic[0] == PSF1_MAGIC0 && Header.uMagic[1] == PSF1_MAGIC1)
66 {
67 // Yes, it is a PSFv1 file.
68 // Check the mode and character size. We only support 8x8 fonts with no special mode.
69 if(Header.uCharSize == 8 && Header.uMode == 0)
70 {
71 // Perfect! The file pointer is already set correctly, so we can just read the font bitmap now.
72 if( ReadFile(hFile, Info->Font, sizeof(BITMAP_FONT), &dwTemp, NULL) )
73 bRet = TRUE;
74 else
76 }
77 else
79
80 break;
81 }
82
83 // Fall through if the magic numbers aren't there
84 }
85 }
86
87 default:
89 }
90
92 return bRet;
93 }
94}
95
96static LRESULT CALLBACK
98{
100
102
103 if(Info || uMsg == WM_CREATE)
104 {
105 switch(uMsg)
106 {
107 case WM_CHILDACTIVATE:
108 Info->MainWndInfo->CurrentFontWnd = Info;
109 SetToolbarFileButtonState(Info->MainWndInfo, TRUE);
110 SetPasteButtonState(Info->MainWndInfo);
111 break;
112
113 case WM_CREATE:
114 Info = (PFONT_WND_INFO)( ( (LPMDICREATESTRUCT) ( (LPCREATESTRUCT)lParam )->lpCreateParams )->lParam );
115 Info->hSelf = hwnd;
116
118
120
121 return 0;
122
123 case WM_USER_APPCLOSE:
124 case WM_CLOSE:
125 // The user has to close all open edit dialogs first
126 if(Info->FirstEditGlyphWnd)
127 {
128 PWSTR pszMessage;
129
130 AllocAndLoadString(&pszMessage, IDS_CLOSEEDIT);
132 HeapFree(hProcessHeap, 0, pszMessage);
133
134 return 0;
135 }
136
137 // Prompt if the current file has been modified
138 if(Info->OpenInfo->bModified)
139 {
140 INT nMsgBoxResult;
141 PWSTR pszPrompt;
142 WCHAR szFile[MAX_PATH];
143
144 GetWindowTextW(hwnd, szFile, MAX_PATH);
145 LoadAndFormatString(IDS_SAVEPROMPT, &pszPrompt, szFile);
146
147 nMsgBoxResult = MessageBoxW(hwnd, pszPrompt, szAppName, MB_YESNOCANCEL | MB_ICONQUESTION);
148 LocalFree(pszPrompt);
149
150 switch(nMsgBoxResult)
151 {
152 case IDYES:
153 DoFileSave(Info->MainWndInfo, FALSE);
154 break;
155
156 case IDCANCEL:
157 // 0 = Stop the process of closing the windows (same value for both WM_CLOSE and WM_USER_APPCLOSE)
158 return 0;
159
160 // IDNO is handled automatically
161 }
162 }
163
164 // If there is another child, it will undo the following actions through its WM_CHILDACTIVATE handler.
165 // Otherwise CurrentFontWnd will stay NULL, so the main window knows that no more childs are opened.
166 Info->MainWndInfo->CurrentFontWnd = NULL;
167 SetToolbarFileButtonState(Info->MainWndInfo, FALSE);
168 SetPasteButtonState(Info->MainWndInfo);
169
170 if(uMsg == WM_USER_APPCLOSE)
171 {
172 // First do the tasks we would do for a normal WM_CLOSE message, then return the value for WM_USER_APPCLOSE
173 // Anything other than 0 indicates that the application shall continue closing the windows
175 return 1;
176 }
177 break;
178
179 case WM_DESTROY:
180 // Remove the window from the linked list
181 if(Info->PrevFontWnd)
182 Info->PrevFontWnd->NextFontWnd = Info->NextFontWnd;
183 else
184 Info->MainWndInfo->FirstFontWnd = Info->NextFontWnd;
185
186 if(Info->NextFontWnd)
187 Info->NextFontWnd->PrevFontWnd = Info->PrevFontWnd;
188 else
189 Info->MainWndInfo->LastFontWnd = Info->PrevFontWnd;
190
191 // Free memory
192 if(Info->Font)
193 HeapFree(hProcessHeap, 0, Info->Font);
194
195 if(Info->OpenInfo->pszFileName)
196 HeapFree(hProcessHeap, 0, Info->OpenInfo->pszFileName);
197
198 HeapFree(hProcessHeap, 0, Info->OpenInfo);
200
202 return 0;
203
204 case WM_SETFOCUS:
205 // Set the keyboard focus to the FontBoxes window every time the Font window gets the focus
206 SetFocus(Info->hFontBoxesWnd);
207 break;
208
209 case WM_SIZE:
210 {
211 INT nHeight = HIWORD(lParam);
212 INT nWidth = LOWORD(lParam);
213 POINT pt;
214 RECT WndRect;
215
216 // This ugly workaround is necessary for not setting either the Height or the Width of the window with SetWindowPos
217 GetWindowRect(Info->hFontBoxesWnd, &WndRect);
218 pt.x = WndRect.left;
219 pt.y = WndRect.top;
221
222 if(nHeight < FONT_BOXES_WND_HEIGHT)
223 {
224 SCROLLINFO si;
225
226 // Set the vertical scroll bar
227 si.cbSize = sizeof(si);
228 si.fMask = SIF_RANGE | SIF_PAGE;
229 si.nMin = 0;
231 si.nPage = nHeight;
233 }
234 else
235 {
237
238 // Store the new y coordinate in pt.y as well (needed for the SetWindowPos call for setting a new x coordinate)
239 pt.y = nHeight / 2 - FONT_BOXES_WND_HEIGHT / 2;
240 SetWindowPos(Info->hFontBoxesWnd,
241 NULL,
242 pt.x,
243 pt.y,
244 0,
245 0,
247 }
248
249 if(nWidth < FONT_BOXES_WND_WIDTH)
250 {
251 SCROLLINFO si;
252
253 // Set the horizontal scroll bar
254 si.cbSize = sizeof(si);
255 si.fMask = SIF_RANGE | SIF_PAGE;
256 si.nMin = 0;
258 si.nPage = nWidth;
260 }
261 else
262 {
264
265 SetWindowPos(Info->hFontBoxesWnd,
266 NULL,
267 nWidth / 2 - FONT_BOXES_WND_WIDTH / 2,
268 pt.y,
269 0,
270 0,
272 }
273
274 // We have to call DefMDIChildProcW here as well, otherwise we won't get the Minimize/Maximize/Close buttons for a maximized MDI child.
275 break;
276 }
277
278 case WM_HSCROLL:
279 case WM_VSCROLL:
280 {
281 INT nBar;
282 INT nOrgPos;
283 SCROLLINFO si;
284
285 if(uMsg == WM_HSCROLL)
286 nBar = SB_HORZ;
287 else
288 nBar = SB_VERT;
289
290 si.cbSize = sizeof(si);
291 si.fMask = SIF_ALL;
292 GetScrollInfo(hwnd, nBar, &si);
293
294 nOrgPos = si.nPos;
295
296 switch( LOWORD(wParam) )
297 {
298 // Constant is the same as SB_LEFT for WM_HSCROLL
299 case SB_TOP:
300 si.nPos = si.nMin;
301 break;
302
303 // Constant is the same as SB_RIGHT for WM_HSCROLL
304 case SB_BOTTOM:
305 si.nPos = si.nMax;
306 break;
307
308 // Constant is the same as SB_LINELEFT for WM_HSCROLL
309 case SB_LINEUP:
310 si.nPos -= 20;
311 break;
312
313 // Constant is the same as SB_LINERIGHT for WM_HSCROLL
314 case SB_LINEDOWN:
315 si.nPos += 20;
316 break;
317
318 // Constant is the same as SB_PAGELEFT for WM_HSCROLL
319 case SB_PAGEUP:
320 si.nPos -= si.nPage;
321 break;
322
323 // Constant is the same as SB_PAGERIGHT for WM_HSCROLL
324 case SB_PAGEDOWN:
325 si.nPos += si.nPage;
326 break;
327
328 case SB_THUMBTRACK:
329 si.nPos = si.nTrackPos;
330 break;
331 }
332
333 si.fMask = SIF_POS;
334 SetScrollInfo(hwnd, nBar, &si, TRUE);
335 GetScrollInfo(hwnd, nBar, &si);
336
337 if(si.nPos != nOrgPos)
338 {
339 // This ugly workaround is necessary for not setting the x coordinate
340 POINT pt;
341 RECT WndRect;
342
343 GetWindowRect(Info->hFontBoxesWnd, &WndRect);
344 pt.x = WndRect.left;
345 pt.y = WndRect.top;
347
348 if(uMsg == WM_HSCROLL)
349 SetWindowPos(Info->hFontBoxesWnd, NULL, -si.nPos, pt.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
350 else
351 SetWindowPos(Info->hFontBoxesWnd, NULL, pt.x, -si.nPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
352 }
353
354 return 0;
355 }
356 }
357 }
358
359 return DefMDIChildProcW(hwnd, uMsg, wParam, lParam);
360}
361
362BOOL
364{
365 HWND hFontWnd;
367
369
370 if(Info)
371 {
372 Info->MainWndInfo = MainWndInfo;
373 Info->OpenInfo = OpenInfo;
374
375 if( InitFont(Info) )
376 {
377 PWSTR pch, pszWindowTitle;
378
379 if(OpenInfo->pszFileName)
380 {
381 pch = wcsrchr(OpenInfo->pszFileName, '\\');
382 pszWindowTitle = (pch ? (pch + 1) : OpenInfo->pszFileName);
383 }
384 else
385 {
386 LoadAndFormatString(IDS_DOCNAME, &pszWindowTitle, ++MainWndInfo->uDocumentCounter);
387 }
388
390 pszWindowTitle,
391 0,
396 MainWndInfo->hMdiClient,
397 hInstance,
398 (LPARAM)Info );
399
400 if(!OpenInfo->pszFileName)
401 LocalFree(pszWindowTitle);
402
403 if(hFontWnd)
404 {
405 // Add the new window to the linked list
406 Info->PrevFontWnd = Info->MainWndInfo->LastFontWnd;
407
408 if(Info->MainWndInfo->LastFontWnd)
409 Info->MainWndInfo->LastFontWnd->NextFontWnd = Info;
410 else
411 Info->MainWndInfo->FirstFontWnd = Info;
412
413 Info->MainWndInfo->LastFontWnd = Info;
414
415 return TRUE;
416 }
417 }
418
420 }
421
422 return FALSE;
423}
424
425BOOL
427{
428 WNDCLASSW wc = {0,};
429
431 wc.hInstance = hInstance;
434 wc.hbrBackground = (HBRUSH)( COLOR_BTNFACE + 1 );
436
437 return RegisterClassW(&wc) != 0;
438}
439
440VOID
442{
444}
#define IDS_READERROR
Definition: resource.h:10
INT AllocAndLoadString(OUT LPTSTR *lpTarget, IN HINSTANCE hInst, IN UINT uID)
Definition: misc.c:59
DWORD LoadAndFormatString(IN HINSTANCE hInstance, IN UINT uID, OUT LPTSTR *lpTarget,...)
Definition: misc.c:85
HINSTANCE hInstance
Definition: charmap.c:19
Definition: Header.h:9
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define wcsrchr
Definition: compat.h:16
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define HeapAlloc
Definition: compat.h:733
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define CreateFileW
Definition: compat.h:741
#define CALLBACK
Definition: compat.h:35
#define FILE_SHARE_READ
Definition: compat.h:136
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
#define pt(x, y)
Definition: drawing.c:79
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
VOID CreateFontBoxesWindow(IN PFONT_WND_INFO FontWndInfo)
Definition: fontboxeswnd.c:224
VOID UnInitFontWndClass(VOID)
Definition: fontwnd.c:441
BOOL InitFontWndClass(VOID)
Definition: fontwnd.c:426
static BOOL InitFont(IN PFONT_WND_INFO Info)
Definition: fontwnd.c:13
static LRESULT CALLBACK FontWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: fontwnd.c:97
BOOL CreateFontWindow(IN PMAIN_WND_INFO MainWndInfo, IN PFONT_OPEN_INFO OpenInfo)
Definition: fontwnd.c:363
static const WCHAR szFontWndClass[]
Definition: fontwnd.c:10
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
HANDLE hProcessHeap
Definition: kbswitch.c:37
#define pch(ap)
Definition: match.c:418
VOID SetPasteButtonState(IN PMAIN_WND_INFO Info)
Definition: mainwnd.c:389
VOID DoFileSave(IN PMAIN_WND_INFO Info, IN BOOL bSaveAs)
Definition: mainwnd.c:289
VOID SetToolbarFileButtonState(IN PMAIN_WND_INFO Info, BOOL bEnabled)
Definition: mainwnd.c:92
VOID LocalizedError(IN UINT uID,...)
Definition: misc.c:103
#define FONT_WND_MIN_HEIGHT
Definition: precomp.h:109
#define FONT_BOXES_WND_HEIGHT
Definition: precomp.h:107
struct _FONT_WND_INFO * PFONT_WND_INFO
Definition: precomp.h:36
#define FONT_BOXES_WND_WIDTH
Definition: precomp.h:106
#define WM_USER_APPCLOSE
Definition: precomp.h:115
#define FONT_WND_MIN_WIDTH
Definition: precomp.h:108
struct _BITMAP_FONT * PBITMAP_FONT
#define IDI_DOC
Definition: resource.h:15
#define IDS_UNSUPPORTEDFORMAT
Definition: resource.h:64
#define IDS_DOCNAME
Definition: resource.h:66
#define IDS_OPENERROR
Definition: resource.h:61
#define IDS_SAVEPROMPT
Definition: resource.h:67
#define IDS_UNSUPPORTEDPSF
Definition: resource.h:65
#define IDS_CLOSEEDIT
Definition: resource.h:69
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
#define LOWORD(l)
Definition: pedump.c:82
#define PSF1_MAGIC0
Definition: psf.h:11
#define PSF1_MAGIC1
Definition: psf.h:12
TCHAR szAppName[128]
Definition: solitaire.cpp:18
LPCWSTR lpszClassName
Definition: winuser.h:3185
HBRUSH hbrBackground
Definition: winuser.h:3183
HICON hIcon
Definition: winuser.h:3181
HINSTANCE hInstance
Definition: winuser.h:3180
WNDPROC lpfnWndProc
Definition: winuser.h:3177
HCURSOR hCursor
Definition: winuser.h:3182
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
#define GWLP_USERDATA
Definition: treelist.c:63
uint16_t * PWSTR
Definition: typedefs.h:56
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define HIWORD(l)
Definition: typedefs.h:247
_Must_inspect_result_ _In_ WDFCHILDLIST _In_ PWDF_CHILD_LIST_ITERATOR _Out_ WDFDEVICE _Inout_opt_ PWDF_CHILD_RETRIEVE_INFO Info
Definition: wdfchildlist.h:690
int WINAPI GetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount)
Definition: window.c:1412
#define ZeroMemory
Definition: winbase.h:1712
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define WM_CLOSE
Definition: winuser.h:1621
#define SB_THUMBTRACK
Definition: winuser.h:573
#define GetWindowLongPtrW
Definition: winuser.h:4829
#define SB_LINEUP
Definition: winuser.h:564
#define WM_HSCROLL
Definition: winuser.h:1743
#define IDCANCEL
Definition: winuser.h:831
#define WM_VSCROLL
Definition: winuser.h:1744
BOOL WINAPI GetWindowRect(_In_ HWND, _Out_ LPRECT)
#define WM_CHILDACTIVATE
Definition: winuser.h:1638
#define SIF_RANGE
Definition: winuser.h:1235
#define WM_CREATE
Definition: winuser.h:1608
BOOL WINAPI SetWindowPos(_In_ HWND, _In_opt_ HWND, _In_ int, _In_ int, _In_ int, _In_ int, _In_ UINT)
#define WM_SIZE
Definition: winuser.h:1611
#define SB_VERT
Definition: winuser.h:553
#define SB_BOTTOM
Definition: winuser.h:577
ATOM WINAPI RegisterClassW(_In_ CONST WNDCLASSW *)
#define IDC_ARROW
Definition: winuser.h:687
#define WM_SETFOCUS
Definition: winuser.h:1613
#define SIF_PAGE
Definition: winuser.h:1233
#define SWP_NOSIZE
Definition: winuser.h:1245
LRESULT WINAPI DefMDIChildProcW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
HWND WINAPI SetFocus(_In_opt_ HWND)
#define SB_PAGEDOWN
Definition: winuser.h:569
#define SIF_ALL
Definition: winuser.h:1232
#define LoadCursor
Definition: winuser.h:5812
#define SB_LINEDOWN
Definition: winuser.h:565
#define MB_ICONEXCLAMATION
Definition: winuser.h:785
#define MB_OK
Definition: winuser.h:790
#define CW_USEDEFAULT
Definition: winuser.h:225
#define SB_TOP
Definition: winuser.h:578
#define MB_ICONQUESTION
Definition: winuser.h:789
#define SIF_POS
Definition: winuser.h:1234
int WINAPI SetScrollInfo(_In_ HWND, _In_ int, _In_ LPCSCROLLINFO, _In_ BOOL)
#define WM_DESTROY
Definition: winuser.h:1609
BOOL WINAPI UnregisterClassW(_In_ LPCWSTR, HINSTANCE)
BOOL WINAPI ShowScrollBar(_In_ HWND, _In_ int, _In_ BOOL)
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
#define IDYES
Definition: winuser.h:835
#define SWP_NOZORDER
Definition: winuser.h:1247
#define SetWindowLongPtrW
Definition: winuser.h:5346
BOOL WINAPI GetScrollInfo(_In_ HWND, _In_ int, _Inout_ LPSCROLLINFO)
#define MB_YESNOCANCEL
Definition: winuser.h:818
HICON WINAPI LoadIconW(_In_opt_ HINSTANCE hInstance, _In_ LPCWSTR lpIconName)
Definition: cursoricon.c:2075
#define SB_PAGEUP
Definition: winuser.h:568
HWND WINAPI CreateMDIWindowW(_In_ LPCWSTR, _In_ LPCWSTR, _In_ DWORD, _In_ int, _In_ int, _In_ int, _In_ int, _In_opt_ HWND, _In_opt_ HINSTANCE, _In_ LPARAM)
#define SB_HORZ
Definition: winuser.h:552
#define COLOR_BTNFACE
Definition: winuser.h:928
BOOL WINAPI ScreenToClient(_In_ HWND, _Inout_ LPPOINT)
__wchar_t WCHAR
Definition: xmlstorage.h:180