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
17 #include "paragraph.h"
19 #include "GDIGUISupport.h"
20 #include "GDIFontMap.h"
21 #include "UnicodeReader.h"
22 #include "ScriptCompositeFontInstance.h"
26 #define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
35 LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
37 #define APP_NAME "LayoutSample"
39 TCHAR szAppName
[] = TEXT(APP_NAME
);
41 void PrettyTitle(HWND hwnd
, char *fileName
)
43 char title
[MAX_PATH
+ 64];
45 sprintf(title
, "%s - %s", APP_NAME
, fileName
);
47 SetWindowTextA(hwnd
, title
);
50 void InitParagraph(HWND hwnd
, Context
*context
)
54 if (context
->paragraph
!= NULL
) {
55 // FIXME: does it matter what we put in the ScrollInfo
56 // if the window's been minimized?
57 if (context
->width
> 0 && context
->height
> 0) {
58 context
->paragraph
->breakLines(context
->width
, context
->height
);
61 si
.cbSize
= sizeof si
;
62 si
.fMask
= SIF_RANGE
| SIF_PAGE
| SIF_DISABLENOSCROLL
;
64 si
.nMax
= context
->paragraph
->getLineCount() - 1;
65 si
.nPage
= context
->height
/ context
->paragraph
->getLineHeight();
66 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
70 int WINAPI
WinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, PSTR szCmdLine
, int iCmdShow
)
76 LEErrorCode status
= LE_NO_ERROR
;
78 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
79 wndclass
.lpfnWndProc
= WndProc
;
80 wndclass
.cbClsExtra
= 0;
81 wndclass
.cbWndExtra
= sizeof(LONG
);
82 wndclass
.hInstance
= hInstance
;
83 wndclass
.hIcon
= LoadIcon(NULL
, IDI_APPLICATION
);
84 wndclass
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
85 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject(WHITE_BRUSH
);
86 wndclass
.lpszMenuName
= szAppName
;
87 wndclass
.lpszClassName
= szAppName
;
89 if (!RegisterClass(&wndclass
)) {
90 MessageBox(NULL
, TEXT("This demo only runs on Windows 2000!"), szAppName
, MB_ICONERROR
);
95 hAccel
= LoadAccelerators(hInstance
, szAppName
);
97 hwnd
= CreateWindow(szAppName
, NULL
,
98 WS_OVERLAPPEDWINDOW
| WS_VSCROLL
,
99 CW_USEDEFAULT
, CW_USEDEFAULT
,
101 NULL
, NULL
, hInstance
, NULL
);
103 ShowWindow(hwnd
, iCmdShow
);
106 while (GetMessage(&msg
, NULL
, 0, 0)) {
107 if (!TranslateAccelerator(hwnd
, hAccel
, &msg
)) {
108 TranslateMessage(&msg
);
109 DispatchMessage(&msg
);
113 UnregisterClass(szAppName
, hInstance
);
117 LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
121 static le_int32 windowCount
= 0;
122 static GDIFontMap
*fontMap
= NULL
;
123 static GDISurface
*surface
= NULL
;
124 static GDIGUISupport
*guiSupport
= new GDIGUISupport();
125 static ScriptCompositeFontInstance
*font
= NULL
;
130 LEErrorCode fontStatus
= LE_NO_ERROR
;
133 surface
= new GDISurface(hdc
);
135 fontMap
= new GDIFontMap(surface
, "FontMap.GDI", 24, guiSupport
, fontStatus
);
136 font
= new ScriptCompositeFontInstance(fontMap
);
138 if (LE_FAILURE(fontStatus
)) {
139 ReleaseDC(hwnd
, hdc
);
143 context
= new Context();
145 context
->width
= 600;
146 context
->height
= 400;
148 context
->paragraph
= Paragraph::paragraphFactory("Sample.txt", font
, guiSupport
);
149 SetWindowLongPtr(hwnd
, GWLP_USERDATA
, (LONG_PTR
) context
);
152 ReleaseDC(hwnd
, hdc
);
154 PrettyTitle(hwnd
, "Sample.txt");
160 context
= (Context
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
161 context
->width
= LOWORD(lParam
);
162 context
->height
= HIWORD(lParam
);
164 InitParagraph(hwnd
, context
);
173 si
.cbSize
= sizeof si
;
175 GetScrollInfo(hwnd
, SB_VERT
, &si
);
179 switch (LOWORD(wParam
))
206 si
.nPos
= si
.nTrackPos
;
214 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
215 GetScrollInfo(hwnd
, SB_VERT
, &si
);
217 context
= (Context
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
219 if (context
->paragraph
!= NULL
&& si
.nPos
!= vertPos
) {
220 ScrollWindow(hwnd
, 0, context
->paragraph
->getLineHeight() * (vertPos
- si
.nPos
), NULL
, NULL
);
231 le_int32 firstLine
, lastLine
;
233 hdc
= BeginPaint(hwnd
, &ps
);
234 SetBkMode(hdc
, TRANSPARENT
);
236 si
.cbSize
= sizeof si
;
238 GetScrollInfo(hwnd
, SB_VERT
, &si
);
242 context
= (Context
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
244 if (context
->paragraph
!= NULL
) {
245 surface
->setHDC(hdc
);
247 // NOTE: si.nPos + si.nPage may include a partial line at the bottom
248 // of the window. We need this because scrolling assumes that the
249 // partial line has been painted.
250 lastLine
= min (si
.nPos
+ (le_int32
) si
.nPage
, context
->paragraph
->getLineCount() - 1);
252 context
->paragraph
->draw(surface
, firstLine
, lastLine
);
260 switch (LOWORD(wParam
)) {
264 char szFileName
[MAX_PATH
], szTitleName
[MAX_PATH
];
265 static char szFilter
[] = "Text Files (.txt)\0*.txt\0"
266 "All Files (*.*)\0*.*\0\0";
268 ofn
.lStructSize
= sizeof (OPENFILENAMEA
);
269 ofn
.hwndOwner
= hwnd
;
270 ofn
.hInstance
= NULL
;
271 ofn
.lpstrFilter
= szFilter
;
272 ofn
.lpstrCustomFilter
= NULL
;
273 ofn
.nMaxCustFilter
= 0;
274 ofn
.nFilterIndex
= 0;
275 ofn
.lpstrFile
= szFileName
;
276 ofn
.nMaxFile
= MAX_PATH
;
277 ofn
.lpstrFileTitle
= szTitleName
;
278 ofn
.nMaxFileTitle
= MAX_PATH
;
279 ofn
.lpstrInitialDir
= NULL
;
280 ofn
.lpstrTitle
= NULL
;
281 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
283 ofn
.nFileExtension
= 0;
284 ofn
.lpstrDefExt
= "txt";
287 ofn
.lpTemplateName
= NULL
;
289 szFileName
[0] = '\0';
291 if (GetOpenFileNameA(&ofn
)) {
293 surface
->setHDC(hdc
);
295 Paragraph
*newParagraph
= Paragraph::paragraphFactory(szFileName
, font
, guiSupport
);
297 if (newParagraph
!= NULL
) {
298 context
= (Context
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
300 if (context
->paragraph
!= NULL
) {
301 delete context
->paragraph
;
304 context
->paragraph
= newParagraph
;
305 InitParagraph(hwnd
, context
);
306 PrettyTitle(hwnd
, szTitleName
);
307 InvalidateRect(hwnd
, NULL
, TRUE
);
312 //ReleaseDC(hwnd, hdc);
319 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
322 case IDM_HELP_ABOUTLAYOUTSAMPLE
:
323 MessageBox(hwnd
, TEXT("Windows Layout Sample 0.1\n")
324 TEXT("Copyright (C) 1998-2005 By International Business Machines Corporation and others.\n")
325 TEXT("Author: Eric Mader"),
326 szAppName
, MB_ICONINFORMATION
| MB_OK
);
335 context
= (Context
*) GetWindowLongPtr(hwnd
, GWLP_USERDATA
);
337 if (context
!= NULL
&& context
->paragraph
!= NULL
) {
338 delete context
->paragraph
;
343 if (--windowCount
<= 0) {
354 return DefWindowProc(hwnd
, message
, wParam
, lParam
);