summaryrefslogtreecommitdiff
path: root/LiteEditor/clPrintout.cpp
blob: 72016b107d3a8fa1f88a8ee0e30531b344cc4417 (plain)
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
#include "clPrintout.h"
#include "cl_editor.h"

//----------------------------------------------------------------------------
// clPrintout
//----------------------------------------------------------------------------
extern wxPageSetupDialogData* g_pageSetupData;

clPrintout::clPrintout(LEditor* edit, const wxString& title)
    : wxPrintout(title)
    , m_minPage(0)
    , m_maxPage(0)
{
    m_edit = edit;
    m_printed = 0;
}

bool clPrintout::OnPrintPage(int page)
{

    wxDC* dc = GetDC();
    if(!dc) return false;

    // scale DC
    PrintScaling(dc);

    // print page
    if(page == 1) m_printed = 0;
    m_printed = m_edit->FormatRange(1, m_printed, m_edit->GetLength(), dc, dc, m_printRect, m_pageRect);

    return true;
}

bool clPrintout::OnBeginDocument(int startPage, int endPage)
{

    if(!wxPrintout::OnBeginDocument(startPage, endPage)) {
        return false;
    }

    return true;
}

void clPrintout::GetPageInfo(int* minPage, int* maxPage, int* selPageFrom, int* selPageTo)
{
    // initialize values
    *minPage = 0;
    *maxPage = 0;
    *selPageFrom = 0;
    *selPageTo = 0;

    // scale DC if possible
    wxDC* dc = GetDC();
    if(!dc) return;
    PrintScaling(dc);

    // get print page informations and convert to printer pixels
    wxSize ppiScr;
    GetPPIScreen(&ppiScr.x, &ppiScr.y);
    wxSize page = g_pageSetupData->GetPaperSize();
    page.x = static_cast<int>(page.x * ppiScr.x / 25.4);
    page.y = static_cast<int>(page.y * ppiScr.y / 25.4);
    m_pageRect = wxRect(0, 0, page.x, page.y);

    // get margins informations and convert to printer pixels
    wxPoint pt = g_pageSetupData->GetMarginTopLeft();
    int left = pt.x;
    int top = pt.y;
    pt = g_pageSetupData->GetMarginBottomRight();
    int right = pt.x;
    int bottom = pt.y;

    top = static_cast<int>(top * ppiScr.y / 25.4);
    bottom = static_cast<int>(bottom * ppiScr.y / 25.4);
    left = static_cast<int>(left * ppiScr.x / 25.4);
    right = static_cast<int>(right * ppiScr.x / 25.4);

    m_printRect = wxRect(left, top, page.x - (left + right), page.y - (top + bottom));

    // count pages
    while((m_printed < m_edit->GetLength())) {
        m_printed = m_edit->FormatRange(0, m_printed, m_edit->GetLength(), dc, dc, m_printRect, m_pageRect);
        *maxPage += 1;
    }
    
    if(*maxPage > 0) *minPage = 1;
    *selPageFrom = *minPage;
    *selPageTo = *maxPage;

    m_minPage = *minPage;
    m_maxPage = *maxPage;
}

bool clPrintout::HasPage(int page) { return (page >= m_minPage) && (page <= m_maxPage); }

bool clPrintout::PrintScaling(wxDC* dc)
{

    // check for dc, return if none
    if(!dc) return false;

    // get printer and screen sizing values
    wxSize ppiScr;
    GetPPIScreen(&ppiScr.x, &ppiScr.y);
    if(ppiScr.x == 0) { // most possible guess 96 dpi
        ppiScr.x = 96;
        ppiScr.y = 96;
    }
    wxSize ppiPrt;
    GetPPIPrinter(&ppiPrt.x, &ppiPrt.y);
    if(ppiPrt.x == 0) { // scaling factor to 1
        ppiPrt.x = ppiScr.x;
        ppiPrt.y = ppiScr.y;
    }
    wxSize dcSize = dc->GetSize();
    wxSize pageSize;
    GetPageSizePixels(&pageSize.x, &pageSize.y);

    // set user scale
    float scale_x = (float)(ppiPrt.x * dcSize.x) / (float)(ppiScr.x * pageSize.x);
    float scale_y = (float)(ppiPrt.y * dcSize.y) / (float)(ppiScr.y * pageSize.y);
    dc->SetUserScale(scale_x, scale_y);

    return true;
}