summaryrefslogtreecommitdiffstats
path: root/private/nw/convert/logview/fvprint.c
blob: 675c46c8d784f0460c63d1a6415a25ab0e63dc1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
  +-------------------------------------------------------------------------+
  |                MDI Text File Viewer - Printing Routines                 |
  +-------------------------------------------------------------------------+
  |                        (c) Copyright 1994                               |
  |                          Microsoft Corp.                                |
  |                        All rights reserved                              |
  |                                                                         |
  | Program               : [FVPrint.c]                                     |
  | Programmer            : Arthur Hanson                                   |
  | Original Program Date : [Feb 11, 1994]                                  |
  | Last Update           : [Feb 11, 1994]                                  |
  |                                                                         |
  | Version:  0.10                                                          |
  |                                                                         |
  | Description:                                                            |
  |                                                                         |
  | History:                                                                |
  |   arth  Jul 27, 1993    0.10    Original Version.                       |
  |                                                                         |
  +-------------------------------------------------------------------------+
*/

#include "LogView.h"

BOOL fAbort;
HWND hwndPDlg;
CHAR szDevice[160];
PSTR szDriver;
PSTR szPort;
PSTR szTitle;
INT iPrinter = 0;       // level of available printer support.
                        // 0 - no printer available
                        // 1 - printer available
                        // 2 - driver supports 3.0 device initialization
HANDLE hInitData=NULL;

CHAR szExtDeviceMode[] = "EXTDEVICEMODE";


/*+-------------------------------------------------------------------------+
  | GetPrinterDC()                                                          |
  |                                                                         |
  |   Creates a printer display context for the default device.  As a side  |
  |   effect, it sets the szDevice and szPort variables.  It also sets      |
  |   iPrinter to the supported level of printing.                          |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
HDC APIENTRY GetPrinterDC(BOOL bInformation) {
    HDC hdc;
    LPDEVMODE lpdevmode = NULL;

    iPrinter = 0;

    // Get the printer information from win.ini into a buffer and null terminate it.
    GetProfileString ( TEXT("windows"), TEXT("device"), TEXT(""), szDevice, sizeof(szDevice));
    for (szDriver = szDevice; *szDriver && *szDriver != TEXT(','); szDriver++)
        ;
    if (*szDriver)
        *szDriver++ = 0;

    // From the current position in the buffer, null teminate the list of ports
    for (szPort = szDriver; *szPort && *szPort != TEXT(','); szPort++)
        ;
    if (*szPort)
        *szPort++ = 0;

    // if the device, driver and port buffers all contain meaningful data, proceed.
    if (!*szDevice || !*szDriver || !*szPort){
        *szDevice = 0;
        return NULL;
    }

    // Create the printer display context
    if (hInitData){
        // Get a pointer to the initialization data
        lpdevmode = (LPDEVMODE) LocalLock (hInitData);

        if (lstrcmp (szDevice, (LPSTR)lpdevmode)) {
            // User has changed the device... cancel this setup, as it is invalid
            // (although if we worked harder we could retain some of it).
            lpdevmode = NULL;
            LocalUnlock (hInitData);
            LocalFree (hInitData);
            hInitData = NULL;
        }
    }

    if (bInformation)
      hdc = CreateIC (szDriver, szDevice, szPort, lpdevmode);
   else
      hdc = CreateDC (szDriver, szDevice, szPort, lpdevmode);

    // Unlock initialization data
    if (hInitData)
        LocalUnlock (hInitData);

    if (!hdc)
        return NULL;


    iPrinter = 1;

    // Find out if ExtDeviceMode() is supported and set flag appropriately
    if (GetProcAddress (LoadLibrary(szDriver), szExtDeviceMode))
        iPrinter = 2;

    return hdc;

} // GetPrinterDC


/*+-------------------------------------------------------------------------+
  | AbortProc()                                                             |
  |                                                                         |
  |   Checks for user abort.                                                |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
INT APIENTRY AbortProc ( HDC hdc, WORD reserved) {
    MSG msg;

    // Allow other apps to run, or get abort messages
    while (!fAbort && PeekMessage (&msg, NULL, 0, 0, TRUE))
        if (!hwndPDlg || !IsDialogMessage (hwndPDlg, &msg)) {
            TranslateMessage (&msg);
            DispatchMessage  (&msg);
        }

    return !fAbort;

    UNREFERENCED_PARAMETER(hdc);
    UNREFERENCED_PARAMETER(reserved);

} // AbortProc


/*+-------------------------------------------------------------------------+
  | PrintDlgProc()                                                          |
  |                                                                         |
  |    Print/Cancel dialog box.                                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
BOOL APIENTRY PrintDlgProc(HWND hwnd, UINT msg, WORD wParam, LONG lParam) {
    switch (msg) {
        case WM_INITDIALOG:
            // Set up information in dialog box
            SetDlgItemText (hwnd, IDD_PRINTTITLE, (LPSTR)szTitle);
            break;

        case WM_COMMAND:
            // abort printing if the only button gets hit
            fAbort = TRUE;
            break;

        default:
            return FALSE;
    }

    return TRUE;

    UNREFERENCED_PARAMETER(wParam);
    UNREFERENCED_PARAMETER(lParam);
} // PrintDlgProc


/*+-------------------------------------------------------------------------+
  | PrintFile()                                                             |
  |                                                                         |
  |    Prints the contents of the edit control.                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
VOID APIENTRY PrintFile(HWND hwnd) {
    HDC     hdc;
    INT     yExtPage;
    CHAR    sz[32];
    int     cch;
    WORD    ich;
    PSTR    pch;
    WORD    iLine;
    WORD    nLinesEc;
    WORD    i;
    HANDLE  hT;
    HWND    hwndPDlg;
    DWORD   dy;
    INT     yExtSoFar;
    WORD    fError = TRUE;
    HWND    hwndEdit;
    HFONT   hFont, hOldFont;

    hwndEdit = (HWND)GetWindowLong(hwnd,GWL_HWNDEDIT);

    // Create the job title by loading the title string from STRINGTABLE
    cch = LoadString (hInst, IDS_PRINTJOB, sz, sizeof(sz));
    szTitle = sz + cch;
    cch += GetWindowText (hwnd, sz + cch, 32 - cch);
    sz[31] = 0;

    // Initialize the printer
    hdc = GetPrinterDC(FALSE);
    if (!hdc)
        goto getout5;

    SetMapMode(hdc, MM_TEXT);
    SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT));

    // Disable the main application window and create the Cancel dialog
    EnableWindow (hwndFrame, FALSE);

    hwndPDlg = CreateDialog (hInst, IDD_PRINT, hwnd, (DLGPROC) PrintDlgProc);

    if (!hwndPDlg)
        goto getout3;

    ShowWindow (hwndPDlg, SW_SHOW);
    UpdateWindow (hwndPDlg);

    // Allow the app. to inform GDI of the escape function to call
    if (Escape(hdc, SETABORTPROC, 0, (LPSTR)AbortProc, NULL) < 0)
        goto getout1;

    // Initialize the document
    if (Escape(hdc, STARTDOC, cch, (LPSTR)sz, NULL) < 0)
        goto getout1;

    // Get the height of one line and the height of a page
    {
    SIZE tmp;
    GetTextExtentPoint(hdc, "CC", 2, &tmp );
    dy = tmp.cy;
    }

    yExtPage = GetDeviceCaps(hdc, VERTRES);

    // Get the lines in document and and a handle to the text buffer
    iLine     = 0;
    yExtSoFar = 0;
    nLinesEc  = (WORD)SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0L);
    hT        = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);

    // While more lines print out the text
    while (iLine < nLinesEc) {
        if (yExtSoFar + (int) dy > yExtPage) {
            // Reached the end of a page. Tell the device driver to eject a page
            if (Escape(hdc, NEWFRAME, 0, NULL, NULL) < 0 || fAbort)
                goto getout2;
            yExtSoFar = 0;
        }

        // Get the length and position of the line in the buffer and lock from that
        // offset into the buffer.
        ich = (WORD)SendMessage (hwndEdit, EM_LINEINDEX, iLine, 0L);
        cch = (WORD)SendMessage (hwndEdit, EM_LINELENGTH, ich, 0L);
        pch = (PSTR)LocalLock(hT) + ich;

        // Print the line and unlock the text handle
        TextOut (hdc, 0, yExtSoFar, (LPSTR)pch, cch);
        LocalUnlock (hT);

        // Test and see if the Abort flag has been set. If yes, exit.
        if (fAbort)
            goto getout2;

        // Move down the page
        yExtSoFar += dy;
        iLine++;
    }

    // Eject the last page.
    if (Escape(hdc, NEWFRAME, 0, NULL, NULL) < 0)
        goto getout2;

    // Complete the document.
    if (Escape(hdc, ENDDOC, 0, NULL, NULL) < 0) {
getout2:
        // Ran into a problem before NEWFRAME? Abort the document
        Escape( hdc, ABORTDOC, 0, NULL, NULL);
    } else
        fError=FALSE;

getout3:
    // Close the cancel dialog and re-enable main app. window
    EnableWindow (hwndFrame, TRUE);
    DestroyWindow (hwndPDlg);

getout1:
    DeleteDC(hdc);

getout5:
#ifdef WIN16
    // Get rid of dialog procedure instances
    FreeProcInstance (lpfnPDlg);
#endif

#ifdef WIN16
getout4:
    FreeProcInstance (lpfnAbort);
getout:
#endif

    // Error? make sure the user knows...
    if (fError)
        MPError (hwnd, MB_OK | MB_ICONEXCLAMATION, IDS_PRINTERROR, (LPSTR)szTitle);

    return;
        UNREFERENCED_PARAMETER(i);

} // PrintFile


/*+-------------------------------------------------------------------------+
  | GetInitializationData()                                                 |
  |                                                                         |
  |   Gets DC initialization data from a printer driver supporting          |
  |   ExtDeviceMode(). Called in response to the File/Printer setup menu    |
  |   selection.                                                            |
  |                                                                         |
  |   This function allows the user to change the printer settings FOR      |
  |   LOGVIEW ONLY.  This allows LogView to print in a variety of settings  |
  |   without messing up any othe applications.                             |
  |                                                                         |
  +-------------------------------------------------------------------------+*/
BOOL APIENTRY GetInitializationData( HWND hwnd ) {
    LPSTR lpOld;
    LPSTR lpNew;
    FARPROC lpfn;
    HANDLE hT,hDrv;
    CHAR sz[32];
    int cb;
    INT flag;

    // Pop up dialog for user and retain data in app buffer
    flag = DM_PROMPT | DM_COPY;

    // Load the device driver and find the ExtDeviceMode() function
    wsprintf (sz, "%s.drv", (LPSTR)szDriver);
    if ((int)(hDrv = LoadLibrary (sz)) < 32)
        return FALSE;
    if (!(lpfn = (DLGPROC) GetProcAddress (hDrv, szExtDeviceMode)))
        return FALSE;

    if (hInitData) {
        // We have some old data... we want to modify the previously specified
        // setup rather than starting with the default setup.
        lpOld = (LPSTR)LocalLock(hInitData);
        flag |= DM_MODIFY;
    }
    else
        lpOld = NULL;

    // Get the number of bytes needed for the init data
    cb = (*lpfn) (hwnd, hDrv, (LPDEVMODE)NULL, (LPSTR)szDevice, (LPSTR)szPort, (LPDEVMODE)NULL, (LPSTR)NULL, 0);

    // Grab some memory for the new data and lock it.
    hT    = LocalAlloc (LHND,cb);
    if(!hT) {
        MessageBox(hwnd, TEXT("<GetInitializationData> Not enough memory."), NULL, MB_OK | MB_ICONHAND);
        LocalUnlock(hInitData);
        LocalFree(hInitData);
        FreeLibrary(hDrv);
        return(FALSE);
    }

    lpNew = (LPSTR)LocalLock (hT);

    // Post the device mode dialog. 0 flag iff user hits OK button
    if ((*lpfn) (hwnd, hDrv, (LPDEVMODE)lpNew, (LPSTR)szDevice, (LPSTR)szPort, (LPDEVMODE)lpOld, (LPSTR)NULL, flag) == IDOK)
        flag = 0;

    // Unlock the input structures
    LocalUnlock (hT);

    if (hInitData)
        LocalUnlock (hInitData);

    // If the user hit OK and everything worked, free the original init.  data and
    // retain the new one.  Otherwise, toss the new buffer.
    if (flag)
        LocalFree (hT);
    else {
        if (hInitData)
            LocalFree (hInitData);

        hInitData = hT;
    }

    FreeLibrary(hDrv);
    return (!flag);

} // GetInitializationData