~ubuntu-branches/ubuntu/gutsy/icu/gutsy

« back to all changes in this revision

Viewing changes to source/samples/layout/layout.cpp

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2005-05-21 22:44:31 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: package-import@ubuntu.com-20050521224431-r7rktfhnu1n4tf1g
Tags: 2.1-2.1
Rename icu-doc to icu21-doc. icu-doc is built by the icu28 package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 */
13
13
 
14
14
#include <windows.h>
 
15
#include <stdio.h>
15
16
 
16
17
//#include "LETypes.h"
17
18
//#include "LEFontInstance.h"
29
30
#include "UnicodeReader.h"
30
31
#include "scrptrun.h"
31
32
 
 
33
#include "resource.h"
 
34
 
32
35
#define ARRAY_LENGTH(array) (sizeof array / sizeof array[0])
33
36
 
 
37
struct Context
 
38
{
 
39
    le_int32 width;
 
40
    le_int32 height;
 
41
    Paragraph *paragraph;
 
42
};
 
43
 
34
44
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
35
45
 
 
46
#define APP_NAME "LayoutSample"
 
47
 
 
48
TCHAR szAppName[] = TEXT(APP_NAME);
 
49
 
 
50
void PrettyTitle(HWND hwnd, char *fileName)
 
51
{
 
52
    char title[MAX_PATH + 64];
 
53
 
 
54
    sprintf(title, "%s - %s", APP_NAME, fileName);
 
55
 
 
56
    SetWindowTextA(hwnd, title);
 
57
}
 
58
 
 
59
void InitParagraph(HWND hwnd, Context *context)
 
60
{
 
61
    SCROLLINFO si;
 
62
 
 
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);
 
68
        }
 
69
 
 
70
        si.cbSize = sizeof si;
 
71
        si.fMask = SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL;
 
72
        si.nMin = 0;
 
73
        si.nMax = context->paragraph->getLineCount() - 1;
 
74
        si.nPage = context->height / context->paragraph->getLineHeight();
 
75
        SetScrollInfo(hwnd, SB_VERT, &si, true);
 
76
    }
 
77
}
 
78
 
36
79
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
37
80
{
38
81
    HWND hwnd;
 
82
    HACCEL hAccel;
39
83
    MSG msg;
40
84
    WNDCLASS wndclass;
41
 
    TCHAR szAppName[] = TEXT("LayoutDemo");
42
 
    TCHAR szTitle[] = TEXT("LayoutDemo: Demo of LayoutEngine");
43
85
    RFIErrorCode status = RFI_NO_ERROR;
44
86
 
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;
55
97
 
56
98
    if (!RegisterClass(&wndclass)) {
59
101
        return 0;
60
102
    }
61
103
 
62
 
    hwnd = CreateWindow(szAppName, szTitle,
63
 
        WS_OVERLAPPEDWINDOW | WS_VSCROLL,
64
 
        CW_USEDEFAULT, CW_USEDEFAULT,
65
 
        600, 400,
66
 
        NULL, NULL, hInstance, NULL);
 
104
    hAccel = LoadAccelerators(hInstance, szAppName);
 
105
 
 
106
    hwnd = CreateWindow(szAppName, NULL,
 
107
                        WS_OVERLAPPEDWINDOW | WS_VSCROLL,
 
108
                        CW_USEDEFAULT, CW_USEDEFAULT,
 
109
                        600, 400,
 
110
                        NULL, NULL, hInstance, NULL);
67
111
 
68
112
    ShowWindow(hwnd, iCmdShow);
69
113
    UpdateWindow(hwnd);
70
114
 
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);
 
119
        }
74
120
    }
75
121
 
76
122
    return msg.wParam;
79
125
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
80
126
{
81
127
    HDC hdc;
82
 
    Paragraph *paragraph;
 
128
    Context *context;
83
129
    static le_int32 windowCount = 0;
84
130
    static GDIFontMap *fontMap = NULL;
85
131
    static GDIGUISupport *guiSupport = new GDIGUISupport();
98
144
            return 0;
99
145
        }
100
146
 
101
 
        paragraph = Paragraph::paragraphFactory("Sample.txt", fontMap, guiSupport, hdc);
102
 
        SetWindowLong(hwnd, 0, (LONG) paragraph);
 
147
    context = new Context();
 
148
 
 
149
    context->width  = 600;
 
150
    context->height = 400;
 
151
 
 
152
        context->paragraph = Paragraph::paragraphFactory("Sample.txt", fontMap, guiSupport, hdc);
 
153
        SetWindowLong(hwnd, 0, (LONG) context);
103
154
 
104
155
        windowCount += 1;
105
156
        ReleaseDC(hwnd, hdc);
 
157
 
 
158
    PrettyTitle(hwnd, "Sample.txt");
106
159
        return 0;
107
160
    }
108
161
 
109
162
    case WM_SIZE:
110
163
    {
111
 
        le_int32 width = LOWORD(lParam);
112
 
        le_int32 height = HIWORD(lParam);
113
 
        SCROLLINFO si;
114
 
 
115
 
        
116
 
        paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
117
 
 
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);
123
 
            }
124
 
 
125
 
            si.cbSize = sizeof si;
126
 
            si.fMask = SIF_RANGE | SIF_PAGE | SIF_DISABLENOSCROLL;
127
 
            si.nMin = 0;
128
 
            si.nMax = paragraph->getLineCount() - 1;
129
 
            si.nPage = height / paragraph->getLineHeight();
130
 
            SetScrollInfo(hwnd, SB_VERT, &si, true);
131
 
        }
132
 
 
 
164
        context = (Context *) GetWindowLong(hwnd, 0);
 
165
    context->width  = LOWORD(lParam);
 
166
    context->height = HIWORD(lParam);
 
167
 
 
168
    InitParagraph(hwnd, context);
133
169
        return 0;
134
170
    }
135
171
 
182
218
        SetScrollInfo(hwnd, SB_VERT, &si, true);
183
219
        GetScrollInfo(hwnd, SB_VERT, &si);
184
220
 
185
 
        paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
 
221
        context = (Context *) GetWindowLong(hwnd, 0);
186
222
 
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);
190
226
        }
191
227
 
199
235
        le_int32 firstLine, lastLine;
200
236
 
201
237
        hdc = BeginPaint(hwnd, &ps);
 
238
        SetBkMode(hdc, TRANSPARENT);
202
239
 
203
240
        si.cbSize = sizeof si;
204
241
        si.fMask = SIF_ALL;
206
243
 
207
244
        firstLine = si.nPos;
208
245
 
209
 
        paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
 
246
        context = (Context *) GetWindowLong(hwnd, 0);
210
247
 
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);
216
253
 
217
 
            paragraph->draw(hdc, firstLine, lastLine);
 
254
            context->paragraph->draw(hdc, firstLine, lastLine);
218
255
        }
219
256
 
220
257
        EndPaint(hwnd, &ps);
221
258
        return 0;
222
259
    }
223
260
 
 
261
    case WM_COMMAND:
 
262
        switch (LOWORD(wParam)) {
 
263
        case IDM_FILE_OPEN:
 
264
        {
 
265
            OPENFILENAMEA ofn;
 
266
            char szFileName[MAX_PATH], szTitleName[MAX_PATH];
 
267
            static char szFilter[] = "Text Files (.txt)\0*.txt\0"
 
268
                                     "All Files (*.*)\0*.*\0\0";
 
269
 
 
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;
 
284
            ofn.nFileOffset       = 0;
 
285
            ofn.nFileExtension    = 0;
 
286
            ofn.lpstrDefExt       = "txt";
 
287
            ofn.lCustData         = 0L;
 
288
            ofn.lpfnHook          = NULL;
 
289
            ofn.lpTemplateName    = NULL;
 
290
 
 
291
            szFileName[0] = '\0';
 
292
 
 
293
            hdc = GetDC(hwnd);
 
294
 
 
295
            if (GetOpenFileNameA(&ofn)) {
 
296
                Paragraph *newParagraph = Paragraph::paragraphFactory(szFileName, fontMap, guiSupport, hdc);
 
297
 
 
298
                if (newParagraph != NULL) {
 
299
                    context = (Context *) GetWindowLong(hwnd, 0);
 
300
 
 
301
                    if (context->paragraph != NULL) {
 
302
                        delete context->paragraph;
 
303
                    }
 
304
 
 
305
                    context->paragraph = newParagraph;
 
306
                    InitParagraph(hwnd, context);
 
307
                    PrettyTitle(hwnd, szTitleName);
 
308
                    InvalidateRect(hwnd, NULL, true);
 
309
 
 
310
                }
 
311
            }
 
312
 
 
313
            ReleaseDC(hwnd, hdc);
 
314
 
 
315
            return 0;
 
316
        }
 
317
 
 
318
        case IDM_FILE_EXIT:
 
319
        case IDM_FILE_CLOSE:
 
320
            SendMessage(hwnd, WM_CLOSE, 0, 0);
 
321
            return 0;
 
322
 
 
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);
 
328
            return 0;
 
329
 
 
330
        }
 
331
        break;
 
332
 
 
333
 
224
334
    case WM_DESTROY:
225
335
    {
226
 
        paragraph = (Paragraph *) GetWindowLong(hwnd, 0);
 
336
        context = (Context *) GetWindowLong(hwnd, 0);
227
337
 
228
 
        if (paragraph != NULL) {
229
 
            delete paragraph;
 
338
        if (context->paragraph != NULL) {
 
339
            delete context->paragraph;
230
340
        }
231
341
 
 
342
        delete context;
 
343
 
232
344
        if (--windowCount <= 0) {
233
345
            delete fontMap;
234
346