2 *******************************************************************************
4 * Copyright (C) 1999-2007, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: Layout.cpp
10 * created on: 08/03/2000
11 * created by: Eric R. Mader
34 typedef struct Context Context
;
36 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
38 #define APP_NAME "LayoutSample"
40 TCHAR szAppName
[] = TEXT(APP_NAME
);
42 void PrettyTitle(HWND hwnd
, char *fileName
)
44 char title
[MAX_PATH
+ 64];
46 sprintf(title
, "%s - %s", APP_NAME
, fileName
);
48 SetWindowTextA(hwnd
, title
);
51 void InitParagraph(HWND hwnd
, Context
*context
)
55 if (context
->paragraph
!= NULL
) {
56 // FIXME: does it matter what we put in the ScrollInfo
57 // if the window's been minimized?
58 if (context
->width
> 0 && context
->height
> 0) {
59 pf_breakLines(context
->paragraph
, context
->width
, context
->height
);
62 si
.cbSize
= sizeof si
;
63 si
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_DISABLENOSCROLL
;
65 si
.nMax
= pf_getLineCount(context
->paragraph
) - 1;
66 si
.nPage
= context
->height
/ pf_getLineHeight(context
->paragraph
);
67 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
71 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, PSTR szCmdLine
, int iCmdShow
)
77 LEErrorCode status
= LE_NO_ERROR
;
79 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
80 wndclass
.lpfnWndProc
= WndProc
;
81 wndclass
.cbClsExtra
= 0;
82 wndclass
.cbWndExtra
= sizeof(LONG
);
83 wndclass
.hInstance
= hInstance
;
84 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
85 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
86 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject(WHITE_BRUSH
);
87 wndclass
.lpszMenuName
= szAppName
;
88 wndclass
.lpszClassName
= szAppName
;
90 if (!RegisterClass(&wndclass
)) {
91 MessageBox(NULL
, TEXT("This demo only runs on Windows 2000!"), szAppName
, MB_ICONERROR
);
96 hAccel
= LoadAccelerators(hInstance
, szAppName
);
98 hwnd
= CreateWindow(szAppName
, NULL
,
99 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
100 CW_USEDEFAULT
, CW_USEDEFAULT
,
102 NULL
, NULL
, hInstance
, NULL
);
104 ShowWindow(hwnd
, iCmdShow
);
107 while (GetMessage(&msg
, NULL
, 0, 0)) {
108 if (!TranslateAccelerator(hwnd
, hAccel
, &msg
)) {
109 TranslateMessage(&msg
);
110 DispatchMessage(&msg
);
114 UnregisterClass(szAppName
, hInstance
);
118 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
122 static le_int32 windowCount
= 0;
123 static fm_fontMap
*fontMap
= NULL
;
124 static rs_surface
*surface
= NULL
;
125 static gs_guiSupport
*guiSupport
= NULL
;
126 static le_font
*font
= NULL
;
131 LEErrorCode fontStatus
= LE_NO_ERROR
;
134 guiSupport
= gs_gdiGuiSupportOpen();
135 surface
= rs_gdiRenderingSurfaceOpen(hdc
);
137 fontMap
= fm_gdiFontMapOpen(surface
, "FontMap.GDI", 24, guiSupport
, &fontStatus
);
138 font
= le_scriptCompositeFontOpen(fontMap
);
140 if (LE_FAILURE(fontStatus
)) {
141 ReleaseDC(hwnd
, hdc
);
145 context
= NEW_ARRAY(Context
, 1);
147 context
->width
= 600;
148 context
->height
= 400;
150 context
->paragraph
= pf_factory("Sample.txt", font
, guiSupport
);
151 SetWindowLongPtr(hwnd
, 0, (LONG_PTR
) context
);
154 ReleaseDC(hwnd
, hdc
);
156 PrettyTitle(hwnd
, "Sample.txt");
162 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
163 context
->width
= LOWORD(lParam
);
164 context
->height
= HIWORD(lParam
);
166 InitParagraph(hwnd
, context
);
175 si
.cbSize
= sizeof si
;
177 GetScrollInfo(hwnd
, SB_VERT
, &si
);
181 switch (LOWORD(wParam
))
208 si
.nPos
= si
.nTrackPos
;
216 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
217 GetScrollInfo(hwnd
, SB_VERT
, &si
);
219 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
221 if (context
->paragraph
!= NULL
&& si
.nPos
!= vertPos
) {
222 ScrollWindow(hwnd
, 0, pf_getLineHeight(context
->paragraph
) * (vertPos
- si
.nPos
), NULL
, NULL
);
233 le_int32 firstLine
, lastLine
;
235 hdc
= BeginPaint(hwnd
, &ps
);
236 SetBkMode(hdc
, TRANSPARENT
);
238 si
.cbSize
= sizeof si
;
240 GetScrollInfo(hwnd
, SB_VERT
, &si
);
244 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
246 if (context
->paragraph
!= NULL
) {
247 rs_gdiRenderingSurfaceSetHDC(surface
, hdc
);
249 // NOTE: si.nPos + si.nPage may include a partial line at the bottom
250 // of the window. We need this because scrolling assumes that the
251 // partial line has been painted.
252 lastLine
= min (si
.nPos
+ (le_int32
) si
.nPage
, pf_getLineCount(context
->paragraph
) - 1);
254 pf_draw(context
->paragraph
, surface
, firstLine
, lastLine
);
262 switch (LOWORD(wParam
)) {
266 char szFileName
[MAX_PATH
], szTitleName
[MAX_PATH
];
267 static char szFilter
[] = "Text Files (.txt)\0*.txt\0"
268 "All Files (*.*)\0*.*\0\0";
270 ofn
.lStructSize
= sizeof (OPENFILENAMEA
);
271 ofn
.hwndOwner
= hwnd
;
272 ofn
.hInstance
= NULL
;
273 ofn
.lpstrFilter
= szFilter
;
274 ofn
.lpstrCustomFilter
= NULL
;
275 ofn
.nMaxCustFilter
= 0;
276 ofn
.nFilterIndex
= 0;
277 ofn
.lpstrFile
= szFileName
;
278 ofn
.nMaxFile
= MAX_PATH
;
279 ofn
.lpstrFileTitle
= szTitleName
;
280 ofn
.nMaxFileTitle
= MAX_PATH
;
281 ofn
.lpstrInitialDir
= NULL
;
282 ofn
.lpstrTitle
= NULL
;
283 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
285 ofn
.nFileExtension
= 0;
286 ofn
.lpstrDefExt
= "txt";
289 ofn
.lpTemplateName
= NULL
;
291 szFileName
[0] = '\0';
293 if (GetOpenFileNameA(&ofn
)) {
294 pf_flow
*newParagraph
;
297 rs_gdiRenderingSurfaceSetHDC(surface
, hdc
);
299 newParagraph
= pf_factory(szFileName
, font
, guiSupport
);
301 if (newParagraph
!= NULL
) {
302 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
304 if (context
->paragraph
!= NULL
) {
305 pf_close(context
->paragraph
);
308 context
->paragraph
= newParagraph
;
309 InitParagraph(hwnd
, context
);
310 PrettyTitle(hwnd
, szTitleName
);
311 InvalidateRect(hwnd
, NULL
, TRUE
);
316 //ReleaseDC(hwnd, hdc);
323 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
326 case IDM_HELP_ABOUTLAYOUTSAMPLE
:
327 MessageBox(hwnd
, TEXT("Windows Layout Sample 0.1\n")
328 TEXT("Copyright (C) 1998-2005 By International Business Machines Corporation and others.\n")
329 TEXT("Author: Eric Mader"),
330 szAppName
, MB_ICONINFORMATION
| MB_OK
);
339 context
= (Context
*) GetWindowLongPtr(hwnd
, 0);
341 if (context
!= NULL
&& context
->paragraph
!= NULL
) {
342 pf_close(context
->paragraph
);
345 DELETE_ARRAY(context
);
347 if (--windowCount
<= 0) {
349 rs_gdiRenderingSurfaceClose(surface
);
350 gs_gdiGuiSupportClose(guiSupport
);
359 return DefWindowProc(hwnd
, message
, wParam
, lParam
);