2 *******************************************************************************
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 1999-2007, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: Layout.cpp
16 * created on: 08/03/2000
17 * created by: Eric R. Mader
40 typedef struct Context Context
;
42 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
44 #define APP_NAME "LayoutSample"
46 TCHAR szAppName
[] = TEXT(APP_NAME
);
48 void PrettyTitle(HWND hwnd
, char *fileName
)
50 char title
[MAX_PATH
+ 64];
52 sprintf(title
, "%s - %s", APP_NAME
, fileName
);
54 SetWindowTextA(hwnd
, title
);
57 void InitParagraph(HWND hwnd
, Context
*context
)
61 if (context
->paragraph
!= NULL
) {
62 // FIXME: does it matter what we put in the ScrollInfo
63 // if the window's been minimized?
64 if (context
->width
> 0 && context
->height
> 0) {
65 pf_breakLines(context
->paragraph
, context
->width
, context
->height
);
68 si
.cbSize
= sizeof si
;
69 si
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_DISABLENOSCROLL
;
71 si
.nMax
= pf_getLineCount(context
->paragraph
) - 1;
72 si
.nPage
= context
->height
/ pf_getLineHeight(context
->paragraph
);
73 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
77 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, PSTR szCmdLine
, int iCmdShow
)
83 LEErrorCode status
= LE_NO_ERROR
;
85 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
86 wndclass
.lpfnWndProc
= WndProc
;
87 wndclass
.cbClsExtra
= 0;
88 wndclass
.cbWndExtra
= sizeof(LONG
);
89 wndclass
.hInstance
= hInstance
;
90 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
91 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
92 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject(WHITE_BRUSH
);
93 wndclass
.lpszMenuName
= szAppName
;
94 wndclass
.lpszClassName
= szAppName
;
96 if (!RegisterClass(&wndclass
)) {
97 MessageBox(NULL
, TEXT("This demo only runs on Windows 2000!"), szAppName
, MB_ICONERROR
);
102 hAccel
= LoadAccelerators(hInstance
, szAppName
);
104 hwnd
= CreateWindow(szAppName
, NULL
,
105 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
106 CW_USEDEFAULT
, CW_USEDEFAULT
,
108 NULL
, NULL
, hInstance
, NULL
);
110 ShowWindow(hwnd
, iCmdShow
);
113 while (GetMessage(&msg
, NULL
, 0, 0)) {
114 if (!TranslateAccelerator(hwnd
, hAccel
, &msg
)) {
115 TranslateMessage(&msg
);
116 DispatchMessage(&msg
);
120 UnregisterClass(szAppName
, hInstance
);
124 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
128 static le_int32 windowCount
= 0;
129 static fm_fontMap
*fontMap
= NULL
;
130 static rs_surface
*surface
= NULL
;
131 static gs_guiSupport
*guiSupport
= NULL
;
132 static le_font
*font
= NULL
;
137 LEErrorCode fontStatus
= LE_NO_ERROR
;
140 guiSupport
= gs_gdiGuiSupportOpen();
141 surface
= rs_gdiRenderingSurfaceOpen(hdc
);
143 fontMap
= fm_gdiFontMapOpen(surface
, "FontMap.GDI", 24, guiSupport
, &fontStatus
);
144 font
= le_scriptCompositeFontOpen(fontMap
);
146 if (LE_FAILURE(fontStatus
)) {
147 ReleaseDC(hwnd
, hdc
);
151 context
= NEW_ARRAY(Context
, 1);
153 context
->width
= 600;
154 context
->height
= 400;
156 context
->paragraph
= pf_factory("Sample.txt", font
, guiSupport
);
157 SetWindowLongPtr(hwnd
, 0, (LONG_PTR
) context
);
160 ReleaseDC(hwnd
, hdc
);
162 PrettyTitle(hwnd
, "Sample.txt");
168 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
169 context
->width
= LOWORD(lParam
);
170 context
->height
= HIWORD(lParam
);
172 InitParagraph(hwnd
, context
);
181 si
.cbSize
= sizeof si
;
183 GetScrollInfo(hwnd
, SB_VERT
, &si
);
187 switch (LOWORD(wParam
))
214 si
.nPos
= si
.nTrackPos
;
222 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
223 GetScrollInfo(hwnd
, SB_VERT
, &si
);
225 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
227 if (context
->paragraph
!= NULL
&& si
.nPos
!= vertPos
) {
228 ScrollWindow(hwnd
, 0, pf_getLineHeight(context
->paragraph
) * (vertPos
- si
.nPos
), NULL
, NULL
);
239 le_int32 firstLine
, lastLine
;
241 hdc
= BeginPaint(hwnd
, &ps
);
242 SetBkMode(hdc
, TRANSPARENT
);
244 si
.cbSize
= sizeof si
;
246 GetScrollInfo(hwnd
, SB_VERT
, &si
);
250 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
252 if (context
->paragraph
!= NULL
) {
253 rs_gdiRenderingSurfaceSetHDC(surface
, hdc
);
255 // NOTE: si.nPos + si.nPage may include a partial line at the bottom
256 // of the window. We need this because scrolling assumes that the
257 // partial line has been painted.
258 lastLine
= min (si
.nPos
+ (le_int32
) si
.nPage
, pf_getLineCount(context
->paragraph
) - 1);
260 pf_draw(context
->paragraph
, surface
, firstLine
, lastLine
);
268 switch (LOWORD(wParam
)) {
272 char szFileName
[MAX_PATH
], szTitleName
[MAX_PATH
];
273 static char szFilter
[] = "Text Files (.txt)\0*.txt\0"
274 "All Files (*.*)\0*.*\0\0";
276 ofn
.lStructSize
= sizeof (OPENFILENAMEA
);
277 ofn
.hwndOwner
= hwnd
;
278 ofn
.hInstance
= NULL
;
279 ofn
.lpstrFilter
= szFilter
;
280 ofn
.lpstrCustomFilter
= NULL
;
281 ofn
.nMaxCustFilter
= 0;
282 ofn
.nFilterIndex
= 0;
283 ofn
.lpstrFile
= szFileName
;
284 ofn
.nMaxFile
= MAX_PATH
;
285 ofn
.lpstrFileTitle
= szTitleName
;
286 ofn
.nMaxFileTitle
= MAX_PATH
;
287 ofn
.lpstrInitialDir
= NULL
;
288 ofn
.lpstrTitle
= NULL
;
289 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
291 ofn
.nFileExtension
= 0;
292 ofn
.lpstrDefExt
= "txt";
295 ofn
.lpTemplateName
= NULL
;
297 szFileName
[0] = '\0';
299 if (GetOpenFileNameA(&ofn
)) {
300 pf_flow
*newParagraph
;
303 rs_gdiRenderingSurfaceSetHDC(surface
, hdc
);
305 newParagraph
= pf_factory(szFileName
, font
, guiSupport
);
307 if (newParagraph
!= NULL
) {
308 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
310 if (context
->paragraph
!= NULL
) {
311 pf_close(context
->paragraph
);
314 context
->paragraph
= newParagraph
;
315 InitParagraph(hwnd
, context
);
316 PrettyTitle(hwnd
, szTitleName
);
317 InvalidateRect(hwnd
, NULL
, TRUE
);
322 //ReleaseDC(hwnd, hdc);
329 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
332 case IDM_HELP_ABOUTLAYOUTSAMPLE
:
333 MessageBox(hwnd
, TEXT("Windows Layout Sample 0.1\n")
334 TEXT("Copyright (C) 1998-2005 By International Business Machines Corporation and others.\n")
335 TEXT("Author: Eric Mader"),
336 szAppName
, MB_ICONINFORMATION
| MB_OK
);
345 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
347 if (context
!= NULL
&& context
->paragraph
!= NULL
) {
348 pf_close(context
->paragraph
);
351 DELETE_ARRAY(context
);
353 if (--windowCount
<= 0) {
355 rs_gdiRenderingSurfaceClose(surface
);
356 gs_gdiGuiSupportClose(guiSupport
);
365 return DefWindowProc(hwnd
, message
, wParam
, lParam
);