29
30
#include "UnicodeReader.h"
30
31
#include "scrptrun.h"
32
35
#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
34
44
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
46
#define APP_NAME "LayoutSample"
48
TCHAR szAppName[] = TEXT(APP_NAME);
50
void PrettyTitle(HWND hwnd, char *fileName)
52
char title[MAX_PATH + 64];
54
sprintf(title, "%s - %s", APP_NAME, fileName);
56
SetWindowTextA(hwnd, title);
59
void InitParagraph(HWND hwnd, Context *context)
63
if (context->paragraph != NULL) {
64
// FIXME: does it matter what we put in the ScrollInfo
65
// if the window's been minimized?
66
if (context->width > 0 && context->height > 0) {
67
context->paragraph->breakLines(context->width, context->height);
70
si.cbSize = sizeof si;
71
si.fMask = SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL;
73
si.nMax = context->paragraph->getLineCount() - 1;
74
si.nPage = context->height / context->paragraph->getLineHeight();
75
SetScrollInfo(hwnd, SB_VERT, &si, true);
36
79
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
41
TCHAR szAppName[] = TEXT("LayoutDemo");
42
TCHAR szTitle[] = TEXT("LayoutDemo: Demo of LayoutEngine");
43
85
RFIErrorCode status = RFI_NO_ERROR;
45
wndclass.style = CS_HREDRAW | CS_VREDRAW;
46
wndclass.lpfnWndProc = WndProc;
47
wndclass.cbClsExtra = 0;
48
wndclass.cbWndExtra = sizeof(LONG);
49
wndclass.hInstance = hInstance;
50
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
51
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
87
wndclass.style = CS_HREDRAW | CS_VREDRAW;
88
wndclass.lpfnWndProc = WndProc;
89
wndclass.cbClsExtra = 0;
90
wndclass.cbWndExtra = sizeof(LONG);
91
wndclass.hInstance = hInstance;
92
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
93
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
52
94
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
53
wndclass.lpszMenuName = NULL;
95
wndclass.lpszMenuName = szAppName;
54
96
wndclass.lpszClassName = szAppName;
56
98
if (!RegisterClass(&wndclass)) {
62
hwnd = CreateWindow(szAppName, szTitle,
63
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
64
CW_USEDEFAULT, CW_USEDEFAULT,
66
NULL, NULL, hInstance, NULL);
104
hAccel = LoadAccelerators(hInstance, szAppName);
106
hwnd = CreateWindow(szAppName, NULL,
107
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
108
CW_USEDEFAULT, CW_USEDEFAULT,
110
NULL, NULL, hInstance, NULL);
68
112
ShowWindow(hwnd, iCmdShow);
69
113
UpdateWindow(hwnd);
71
115
while (GetMessage(&msg, NULL, 0, 0)) {
72
TranslateMessage(&msg);
73
DispatchMessage(&msg);
116
if (!TranslateAccelerator(hwnd, hAccel, &msg)) {
117
TranslateMessage(&msg);
118
DispatchMessage(&msg);
76
122
return msg.wParam;
101
paragraph = Paragraph::paragraphFactory("Sample.txt", fontMap, guiSupport, hdc);
102
SetWindowLong(hwnd, 0, (LONG) paragraph);
147
context = new Context();
149
context->width = 600;
150
context->height = 400;
152
context->paragraph = Paragraph::paragraphFactory("Sample.txt", fontMap, guiSupport, hdc);
153
SetWindowLong(hwnd, 0, (LONG) context);
104
155
windowCount += 1;
105
156
ReleaseDC(hwnd, hdc);
158
PrettyTitle(hwnd, "Sample.txt");
111
le_int32 width = LOWORD(lParam);
112
le_int32 height = HIWORD(lParam);
116
paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
118
if (paragraph != NULL) {
119
// FIXME: does it matter what we put in the ScrollInfo
120
// if the window's been minimized?
121
if (width > 0 && height > 0) {
122
paragraph->breakLines(width, height);
125
si.cbSize = sizeof si;
126
si.fMask = SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL;
128
si.nMax = paragraph->getLineCount() - 1;
129
si.nPage = height / paragraph->getLineHeight();
130
SetScrollInfo(hwnd, SB_VERT, &si, true);
164
context = (Context *) GetWindowLong(hwnd, 0);
165
context->width = LOWORD(lParam);
166
context->height = HIWORD(lParam);
168
InitParagraph(hwnd, context);
182
218
SetScrollInfo(hwnd, SB_VERT, &si, true);
183
219
GetScrollInfo(hwnd, SB_VERT, &si);
185
paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
221
context = (Context *) GetWindowLong(hwnd, 0);
187
if (paragraph != NULL && si.nPos != vertPos) {
188
ScrollWindow(hwnd, 0, paragraph->getLineHeight() * (vertPos - si.nPos), NULL, NULL);
223
if (context->paragraph != NULL && si.nPos != vertPos) {
224
ScrollWindow(hwnd, 0, context->paragraph->getLineHeight() * (vertPos - si.nPos), NULL, NULL);
189
225
UpdateWindow(hwnd);
207
244
firstLine = si.nPos;
209
paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
246
context = (Context *) GetWindowLong(hwnd, 0);
211
if (paragraph != NULL) {
248
if (context->paragraph != NULL) {
212
249
// NOTE: si.nPos + si.nPage may include a partial line at the bottom
213
250
// of the window. We need this because scrolling assumes that the
214
251
// partial line has been painted.
215
lastLine = min (si.nPos + (le_int32) si.nPage, paragraph->getLineCount() - 1);
252
lastLine = min (si.nPos + (le_int32) si.nPage, context->paragraph->getLineCount() - 1);
217
paragraph->draw(hdc, firstLine, lastLine);
254
context->paragraph->draw(hdc, firstLine, lastLine);
220
257
EndPaint(hwnd, &ps);
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';
295
if (GetOpenFileNameA(&ofn)) {
296
Paragraph *newParagraph = Paragraph::paragraphFactory(szFileName, fontMap, guiSupport, hdc);
298
if (newParagraph != NULL) {
299
context = (Context *) GetWindowLong(hwnd, 0);
301
if (context->paragraph != NULL) {
302
delete context->paragraph;
305
context->paragraph = newParagraph;
306
InitParagraph(hwnd, context);
307
PrettyTitle(hwnd, szTitleName);
308
InvalidateRect(hwnd, NULL, true);
313
ReleaseDC(hwnd, hdc);
320
SendMessage(hwnd, WM_CLOSE, 0, 0);
323
case IDM_HELP_ABOUTLAYOUTSAMPLE:
324
MessageBox(hwnd, TEXT("Windows Layout Sample 0.1\n")
325
TEXT("Copyright (C) 1998-2002 By International Business Machines Corporation and others.\n")
326
TEXT("Author: Eric Mader"),
327
szAppName, MB_ICONINFORMATION | MB_OK);
226
paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
336
context = (Context *) GetWindowLong(hwnd, 0);
228
if (paragraph != NULL) {
338
if (context->paragraph != NULL) {
339
delete context->paragraph;
232
344
if (--windowCount <= 0) {