DWinProgramming /Samples/Chap13/PopPad/PopPrnt.d

Language D Lines 218
MD5 Hash c9f1fce5839d5fa25bcd2294c1f24ffe
Repository git://github.com/AndrejMitrovic/DWinProgramming.git View Raw File View Project SPDX
  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
/+
 + Copyright (c) Charles Petzold, 1998.
 + Ported to the D Programming Language by Andrej Mitrovic, 2011.
 +/

module PopPrnt;

import core.memory;
import core.runtime;
import core.thread;
import std.conv;
import std.math;
import std.range;
import std.string;
import std.utf;

auto toUTF16z(S)(S s)
{
    return toUTFz!(const(wchar)*)(s);
}

pragma(lib, "gdi32.lib");
pragma(lib, "comdlg32.lib");
pragma(lib, "winmm.lib");
import win32.windef;
import win32.winuser;
import win32.wingdi;
import win32.winbase;
import win32.commdlg;
import win32.mmsystem;

import resource;

BOOL bUserAbort;
HWND hDlgPrint;

extern (Windows)
BOOL PrintDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_INITDIALOG:
            EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
            return TRUE;

        case WM_COMMAND:
            bUserAbort = TRUE;
            EnableWindow(GetParent(hDlg), TRUE);
            DestroyWindow(hDlg);
            hDlgPrint = NULL;
            return TRUE;
        
        default:
    }

    return FALSE;
}

extern (Windows)
BOOL AbortProc(HDC hPrinterDC, int iCode)
{
    MSG msg;

    while (!bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return !bUserAbort;
}

BOOL PopPrntPrintFile(HINSTANCE hInst, HWND hwnd, HWND hwndEdit, PTSTR szTitleName)
{
    static DOCINFO  di = DOCINFO(DOCINFO.sizeof);
    static PRINTDLG pd;
    BOOL bSuccess;
    int  yChar, iCharsPerLine, iLinesPerPage, iTotalLines, iTotalPages, iPage, iLine, iLineNum;
    PTSTR pstrBuffer;
    TCHAR[64 + MAX_PATH] szJobName;
    TEXTMETRIC tm;
    WORD iColCopy, iNoiColCopy;

    // Invoke Print common dialog box
    pd.hwndOwner   = hwnd;
    pd.hDevMode    = NULL;
    pd.hDevNames   = NULL;
    pd.hDC         = NULL;
    pd.Flags       = PD_ALLPAGES | PD_COLLATE |
                     PD_RETURNDC | PD_NOSELECTION;
    
    pd.nFromPage = 0;
    pd.nToPage = 0;
    pd.nMinPage = 0;
    pd.nMaxPage = 0;
    pd.nCopies = 1;
    pd.hInstance = NULL;
    pd.lCustData = 0L;
    pd.lpfnPrintHook       = NULL;
    pd.lpfnSetupHook       = NULL;
    pd.lpPrintTemplateName = NULL;
    pd.lpSetupTemplateName = NULL;
    pd.hPrintTemplate      = NULL;
    pd.hSetupTemplate      = NULL;

    if (!PrintDlg(&pd))
        return TRUE;

    iTotalLines = SendMessage(hwndEdit, EM_GETLINECOUNT, 0, 0);
    if (iTotalLines == 0)
        return TRUE;

    // Calculate necessary metrics for file
    GetTextMetrics(pd.hDC, &tm);
    yChar = tm.tmHeight + tm.tmExternalLeading;

    iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES) / tm.tmAveCharWidth;
    iLinesPerPage = GetDeviceCaps(pd.hDC, VERTRES) / yChar;
    iTotalPages   = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage;

    // Allocate a buffer for each line of text
    pstrBuffer = cast(typeof(pstrBuffer))GC.malloc(TCHAR.sizeof * (iCharsPerLine + 1));

    // Display the printing dialog box
    EnableWindow(hwnd, FALSE);

    bSuccess   = TRUE;
    bUserAbort = FALSE;

    hDlgPrint = CreateDialog(hInst, "PrintDlgBox", hwnd, &PrintDlgProc);

    SetDlgItemText(hDlgPrint, IDC_FILENAME, szTitleName);
    
    // @BUG@ WindowsAPI callbacks are not defined properly:
    //     alias BOOL function(HDC, int) ABORTPROC;
    //
    // should be:
    //     alias extern(Windows) BOOL function(HDC, int) ABORTPROC;    
    SetAbortProc(pd.hDC, cast(BOOL function(HDC, int))&AbortProc);

    // Start the document
    GetWindowText(hwnd, szJobName.ptr, szJobName.sizeof);
    di.lpszDocName = szJobName.ptr;

    if (StartDoc(pd.hDC, &di) > 0)
    {
        // Collation requires this loop and iNoiColCopy
        for (iColCopy = 0;
             iColCopy < (cast(WORD)pd.Flags & PD_COLLATE ? pd.nCopies : 1);
             iColCopy++)
        {
            for (iPage = 0; iPage < iTotalPages; iPage++)
            {
                for (iNoiColCopy = 0;
                     iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies);
                     iNoiColCopy++)
                {
                    // Start the page
                    if (StartPage(pd.hDC) < 0)
                    {
                        bSuccess = FALSE;
                        break;
                    }

                    // For each page, print the lines
                    for (iLine = 0; iLine < iLinesPerPage; iLine++)
                    {
                        iLineNum = iLinesPerPage * iPage + iLine;

                        if (iLineNum > iTotalLines)
                            break;

                        *cast(int*)pstrBuffer = iCharsPerLine;

                        TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer,
                                cast(int)SendMessage(hwndEdit, EM_GETLINE,
                                                 cast(WPARAM)iLineNum, cast(LPARAM)pstrBuffer));
                    }

                    if (EndPage(pd.hDC) < 0)
                    {
                        bSuccess = FALSE;
                        break;
                    }

                    if (bUserAbort)
                        break;
                }

                if (!bSuccess || bUserAbort)
                    break;
            }

            if (!bSuccess || bUserAbort)
                break;
        }
    }
    else
        bSuccess = FALSE;

    if (bSuccess)
        EndDoc(pd.hDC);

    if (!bUserAbort)
    {
        EnableWindow(hwnd, TRUE);
        DestroyWindow(hDlgPrint);
    }

    GC.free(pstrBuffer);
    DeleteDC(pd.hDC);

    return bSuccess && !bUserAbort;
}
Back to Top