2001-11-07 23:48:17 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: Layout.cpp
|
|
|
|
*
|
|
|
|
* created on: 08/03/2000
|
|
|
|
* created by: Eric R. Mader
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
2002-03-30 02:41:22 +00:00
|
|
|
#include <stdio.h>
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
//#include "LETypes.h"
|
|
|
|
//#include "LEFontInstance.h"
|
|
|
|
//#include "LayoutEngine.h"
|
|
|
|
//#include "unicode/loengine.h"
|
2003-02-05 00:05:40 +00:00
|
|
|
//#include "unicode/uscript.h"
|
2001-11-07 23:48:17 +00:00
|
|
|
//#include "LEScripts.h"
|
|
|
|
|
2003-02-05 00:05:40 +00:00
|
|
|
//#include "GDIFontInstance.h"
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
#include "paragraph.h"
|
|
|
|
|
|
|
|
#include "GDIGUISupport.h"
|
|
|
|
#include "GDIFontMap.h"
|
|
|
|
#include "UnicodeReader.h"
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
#include "resource.h"
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
le_int32 width;
|
|
|
|
le_int32 height;
|
|
|
|
Paragraph *paragraph;
|
|
|
|
};
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
#define APP_NAME "LayoutSample"
|
|
|
|
|
|
|
|
TCHAR szAppName[] = TEXT(APP_NAME);
|
|
|
|
|
|
|
|
void PrettyTitle(HWND hwnd, char *fileName)
|
|
|
|
{
|
|
|
|
char title[MAX_PATH + 64];
|
|
|
|
|
|
|
|
sprintf(title, "%s - %s", APP_NAME, fileName);
|
|
|
|
|
|
|
|
SetWindowTextA(hwnd, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitParagraph(HWND hwnd, Context *context)
|
|
|
|
{
|
|
|
|
SCROLLINFO si;
|
|
|
|
|
|
|
|
if (context->paragraph != NULL) {
|
|
|
|
// FIXME: does it matter what we put in the ScrollInfo
|
|
|
|
// if the window's been minimized?
|
|
|
|
if (context->width > 0 && context->height > 0) {
|
|
|
|
context->paragraph->breakLines(context->width, context->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
si.cbSize = sizeof si;
|
|
|
|
si.fMask = SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL;
|
|
|
|
si.nMin = 0;
|
|
|
|
si.nMax = context->paragraph->getLineCount() - 1;
|
|
|
|
si.nPage = context->height / context->paragraph->getLineHeight();
|
|
|
|
SetScrollInfo(hwnd, SB_VERT, &si, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
|
|
|
|
{
|
|
|
|
HWND hwnd;
|
2002-03-30 02:41:22 +00:00
|
|
|
HACCEL hAccel;
|
2001-11-07 23:48:17 +00:00
|
|
|
MSG msg;
|
|
|
|
WNDCLASS wndclass;
|
2003-03-03 23:07:01 +00:00
|
|
|
LEErrorCode status = LE_NO_ERROR;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
wndclass.style = CS_HREDRAW | CS_VREDRAW;
|
|
|
|
wndclass.lpfnWndProc = WndProc;
|
|
|
|
wndclass.cbClsExtra = 0;
|
|
|
|
wndclass.cbWndExtra = sizeof(LONG);
|
|
|
|
wndclass.hInstance = hInstance;
|
|
|
|
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
2001-11-07 23:48:17 +00:00
|
|
|
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
|
2002-03-30 02:41:22 +00:00
|
|
|
wndclass.lpszMenuName = szAppName;
|
2001-11-07 23:48:17 +00:00
|
|
|
wndclass.lpszClassName = szAppName;
|
|
|
|
|
|
|
|
if (!RegisterClass(&wndclass)) {
|
|
|
|
MessageBox(NULL, TEXT("This demo only runs on Windows 2000!"), szAppName, MB_ICONERROR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
hAccel = LoadAccelerators(hInstance, szAppName);
|
|
|
|
|
|
|
|
hwnd = CreateWindow(szAppName, NULL,
|
|
|
|
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
600, 400,
|
|
|
|
NULL, NULL, hInstance, NULL);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
ShowWindow(hwnd, iCmdShow);
|
|
|
|
UpdateWindow(hwnd);
|
|
|
|
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0)) {
|
2002-04-02 03:33:42 +00:00
|
|
|
if (!TranslateAccelerator(hwnd, hAccel, &msg)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg.wParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
HDC hdc;
|
2002-03-30 02:41:22 +00:00
|
|
|
Context *context;
|
2001-11-07 23:48:17 +00:00
|
|
|
static le_int32 windowCount = 0;
|
|
|
|
static GDIFontMap *fontMap = NULL;
|
2002-12-05 22:50:29 +00:00
|
|
|
static GDISurface *surface = NULL;
|
2001-11-07 23:48:17 +00:00
|
|
|
static GDIGUISupport *guiSupport = new GDIGUISupport();
|
|
|
|
|
|
|
|
switch (message) {
|
|
|
|
case WM_CREATE:
|
|
|
|
{
|
2003-03-03 23:07:01 +00:00
|
|
|
LEErrorCode fontStatus = LE_NO_ERROR;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
hdc = GetDC(hwnd);
|
2002-12-05 22:50:29 +00:00
|
|
|
surface = new GDISurface(hdc);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
fontMap = new GDIFontMap(surface, "FontMap.GDI", 24, guiSupport, fontStatus);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
if (LE_FAILURE(fontStatus)) {
|
|
|
|
ReleaseDC(hwnd, hdc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
context = new Context();
|
2002-03-30 02:41:22 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
context->width = 600;
|
|
|
|
context->height = 400;
|
2002-03-30 02:41:22 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
context->paragraph = Paragraph::paragraphFactory("Sample.txt", fontMap, guiSupport);
|
2002-03-30 02:41:22 +00:00
|
|
|
SetWindowLong(hwnd, 0, (LONG) context);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
windowCount += 1;
|
|
|
|
ReleaseDC(hwnd, hdc);
|
2002-03-30 02:41:22 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
PrettyTitle(hwnd, "Sample.txt");
|
2001-11-07 23:48:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WM_SIZE:
|
|
|
|
{
|
2002-03-30 02:41:22 +00:00
|
|
|
context = (Context *) GetWindowLong(hwnd, 0);
|
2002-12-05 22:50:29 +00:00
|
|
|
context->width = LOWORD(lParam);
|
|
|
|
context->height = HIWORD(lParam);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
InitParagraph(hwnd, context);
|
2001-11-07 23:48:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WM_VSCROLL:
|
|
|
|
{
|
|
|
|
SCROLLINFO si;
|
|
|
|
le_int32 vertPos;
|
|
|
|
|
|
|
|
si.cbSize = sizeof si;
|
|
|
|
si.fMask = SIF_ALL;
|
|
|
|
GetScrollInfo(hwnd, SB_VERT, &si);
|
|
|
|
|
|
|
|
vertPos = si.nPos;
|
|
|
|
|
|
|
|
switch (LOWORD(wParam))
|
|
|
|
{
|
|
|
|
case SB_TOP:
|
|
|
|
si.nPos = si.nMin;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_BOTTOM:
|
|
|
|
si.nPos = si.nMax;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_LINEUP:
|
|
|
|
si.nPos -= 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_LINEDOWN:
|
|
|
|
si.nPos += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_PAGEUP:
|
|
|
|
si.nPos -= si.nPage;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_PAGEDOWN:
|
|
|
|
si.nPos += si.nPage;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SB_THUMBTRACK:
|
|
|
|
si.nPos = si.nTrackPos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
si.fMask = SIF_POS;
|
|
|
|
SetScrollInfo(hwnd, SB_VERT, &si, true);
|
|
|
|
GetScrollInfo(hwnd, SB_VERT, &si);
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
context = (Context *) GetWindowLong(hwnd, 0);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
if (context->paragraph != NULL && si.nPos != vertPos) {
|
|
|
|
ScrollWindow(hwnd, 0, context->paragraph->getLineHeight() * (vertPos - si.nPos), NULL, NULL);
|
2001-11-07 23:48:17 +00:00
|
|
|
UpdateWindow(hwnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case WM_PAINT:
|
|
|
|
{
|
|
|
|
PAINTSTRUCT ps;
|
|
|
|
SCROLLINFO si;
|
|
|
|
le_int32 firstLine, lastLine;
|
|
|
|
|
|
|
|
hdc = BeginPaint(hwnd, &ps);
|
2002-03-14 22:21:02 +00:00
|
|
|
SetBkMode(hdc, TRANSPARENT);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
si.cbSize = sizeof si;
|
|
|
|
si.fMask = SIF_ALL;
|
|
|
|
GetScrollInfo(hwnd, SB_VERT, &si);
|
|
|
|
|
|
|
|
firstLine = si.nPos;
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
context = (Context *) GetWindowLong(hwnd, 0);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
if (context->paragraph != NULL) {
|
2002-12-05 22:50:29 +00:00
|
|
|
surface->setHDC(hdc);
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
// NOTE: si.nPos + si.nPage may include a partial line at the bottom
|
|
|
|
// of the window. We need this because scrolling assumes that the
|
|
|
|
// partial line has been painted.
|
2002-03-30 02:41:22 +00:00
|
|
|
lastLine = min (si.nPos + (le_int32) si.nPage, context->paragraph->getLineCount() - 1);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
context->paragraph->draw(surface, firstLine, lastLine);
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndPaint(hwnd, &ps);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
case WM_COMMAND:
|
2002-04-02 03:33:42 +00:00
|
|
|
switch (LOWORD(wParam)) {
|
|
|
|
case IDM_FILE_OPEN:
|
|
|
|
{
|
|
|
|
OPENFILENAMEA ofn;
|
|
|
|
char szFileName[MAX_PATH], szTitleName[MAX_PATH];
|
|
|
|
static char szFilter[] = "Text Files (.txt)\0*.txt\0"
|
|
|
|
"All Files (*.*)\0*.*\0\0";
|
|
|
|
|
|
|
|
ofn.lStructSize = sizeof (OPENFILENAMEA);
|
|
|
|
ofn.hwndOwner = hwnd;
|
|
|
|
ofn.hInstance = NULL;
|
|
|
|
ofn.lpstrFilter = szFilter;
|
|
|
|
ofn.lpstrCustomFilter = NULL;
|
|
|
|
ofn.nMaxCustFilter = 0;
|
|
|
|
ofn.nFilterIndex = 0;
|
|
|
|
ofn.lpstrFile = szFileName;
|
|
|
|
ofn.nMaxFile = MAX_PATH;
|
|
|
|
ofn.lpstrFileTitle = szTitleName;
|
|
|
|
ofn.nMaxFileTitle = MAX_PATH;
|
|
|
|
ofn.lpstrInitialDir = NULL;
|
|
|
|
ofn.lpstrTitle = NULL;
|
|
|
|
ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
|
|
|
|
ofn.nFileOffset = 0;
|
|
|
|
ofn.nFileExtension = 0;
|
|
|
|
ofn.lpstrDefExt = "txt";
|
|
|
|
ofn.lCustData = 0L;
|
|
|
|
ofn.lpfnHook = NULL;
|
|
|
|
ofn.lpTemplateName = NULL;
|
|
|
|
|
|
|
|
szFileName[0] = '\0';
|
|
|
|
|
|
|
|
if (GetOpenFileNameA(&ofn)) {
|
2002-12-05 22:50:29 +00:00
|
|
|
hdc = GetDC(hwnd);
|
|
|
|
surface->setHDC(hdc);
|
|
|
|
|
|
|
|
Paragraph *newParagraph = Paragraph::paragraphFactory(szFileName, fontMap, guiSupport);
|
2002-04-02 03:33:42 +00:00
|
|
|
|
|
|
|
if (newParagraph != NULL) {
|
|
|
|
context = (Context *) GetWindowLong(hwnd, 0);
|
|
|
|
|
|
|
|
if (context->paragraph != NULL) {
|
|
|
|
delete context->paragraph;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->paragraph = newParagraph;
|
|
|
|
InitParagraph(hwnd, context);
|
|
|
|
PrettyTitle(hwnd, szTitleName);
|
|
|
|
InvalidateRect(hwnd, NULL, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-05 22:50:29 +00:00
|
|
|
//ReleaseDC(hwnd, hdc);
|
2002-04-02 03:33:42 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IDM_FILE_EXIT:
|
|
|
|
case IDM_FILE_CLOSE:
|
|
|
|
SendMessage(hwnd, WM_CLOSE, 0, 0);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IDM_HELP_ABOUTLAYOUTSAMPLE:
|
|
|
|
MessageBox(hwnd, TEXT("Windows Layout Sample 0.1\n")
|
|
|
|
TEXT("Copyright (C) 1998-2002 By International Business Machines Corporation and others.\n")
|
|
|
|
TEXT("Author: Eric Mader"),
|
|
|
|
szAppName, MB_ICONINFORMATION | MB_OK);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2002-03-30 02:41:22 +00:00
|
|
|
|
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
case WM_DESTROY:
|
|
|
|
{
|
2002-03-30 02:41:22 +00:00
|
|
|
context = (Context *) GetWindowLong(hwnd, 0);
|
2001-11-07 23:48:17 +00:00
|
|
|
|
2002-03-30 02:41:22 +00:00
|
|
|
if (context->paragraph != NULL) {
|
|
|
|
delete context->paragraph;
|
2001-11-07 23:48:17 +00:00
|
|
|
}
|
|
|
|
|
2002-04-02 03:33:42 +00:00
|
|
|
delete context;
|
2002-03-30 02:41:22 +00:00
|
|
|
|
2001-11-07 23:48:17 +00:00
|
|
|
if (--windowCount <= 0) {
|
|
|
|
delete fontMap;
|
2002-12-05 22:50:29 +00:00
|
|
|
delete surface;
|
2001-11-07 23:48:17 +00:00
|
|
|
|
|
|
|
PostQuitMessage(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hwnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|