forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinting.cpp
189 lines (152 loc) · 4.6 KB
/
Printing.cpp
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
Printing.cpp
Dominic Mazzoni
*******************************************************************//*!
\class SneedacityPrintout
\brief Derived from wxPrintout, this class helps with printing.
*//*******************************************************************/
#include "Printing.h"
#include <wx/defs.h>
#include <wx/dc.h>
#include <wx/intl.h>
#include <wx/print.h>
#include <wx/printdlg.h>
#include "AColor.h"
#include "TrackArtist.h"
#include "ViewInfo.h"
#include "Track.h"
#include "widgets/Ruler.h"
#include "widgets/SneedacityMessageBox.h"
#include "TrackPanelDrawingContext.h"
#include "tracks/ui/TrackView.h"
// Globals, so that we remember settings from session to session
wxPrintData &gPrintData()
{
static wxPrintData theData;
return theData;
}
class SneedacityPrintout final : public wxPrintout
{
public:
SneedacityPrintout(wxString title,
TrackList *tracks, TrackPanel &panel):
wxPrintout(title),
mTracks(tracks)
, mPanel(panel)
{
}
bool OnPrintPage(int page);
bool HasPage(int page);
bool OnBeginDocument(int startPage, int endPage);
void GetPageInfo(int *minPage, int *maxPage,
int *selPageFrom, int *selPageTo);
private:
TrackPanel &mPanel;
TrackList *mTracks;
};
bool SneedacityPrintout::OnPrintPage(int WXUNUSED(page))
{
wxDC *dc = GetDC();
if (!dc)
return false;
int width, height;
dc->GetSize(&width, &height);
int rulerScreenHeight = 40;
int screenTotalHeight =
TrackView::GetTotalHeight( *mTracks ) + rulerScreenHeight;
double scale = height / (double)screenTotalHeight;
int rulerPageHeight = (int)(rulerScreenHeight * scale);
Ruler ruler;
ruler.SetBounds(0, 0, width, rulerPageHeight);
ruler.SetOrientation(wxHORIZONTAL);
ruler.SetRange(0.0, mTracks->GetEndTime());
ruler.SetFormat(Ruler::TimeFormat);
ruler.SetLabelEdges(true);
ruler.Draw(*dc);
TrackArtist artist( &mPanel );
artist.SetBackgroundBrushes(*wxWHITE_BRUSH, *wxWHITE_BRUSH,
*wxWHITE_PEN, *wxWHITE_PEN);
const double screenDuration = mTracks->GetEndTime();
SelectedRegion region{};
artist.pSelectedRegion = ®ion;
ZoomInfo zoomInfo(0.0, width / screenDuration);
artist.pZoomInfo = &zoomInfo;
int y = rulerPageHeight;
for (auto n : mTracks->Any()) {
auto &view = TrackView::Get( *n );
wxRect r;
r.x = 0;
r.y = 0;
r.width = width;
auto trackHeight = (int)(view.GetHeight() * scale);
r.height = trackHeight;
const auto subViews = view.GetSubViews( r );
if (subViews.empty())
continue;
auto iter = subViews.begin(), end = subViews.end(), next = iter;
auto yy = iter->first;
for ( ; iter != end; iter = next ) {
++next;
auto nextY = ( next == end )
? trackHeight
: next->first;
r.y = y + yy;
r.SetHeight( nextY - yy );
yy = nextY;
TrackPanelDrawingContext context{
*dc, {}, {}, &artist
};
iter->second->Draw( context, r, TrackArtist::PassTracks );
}
dc->SetPen(*wxBLACK_PEN);
AColor::Line(*dc, 0, y, width, y);
y += trackHeight;
};
return true;
}
bool SneedacityPrintout::HasPage(int page)
{
return (page==1);
}
bool SneedacityPrintout::OnBeginDocument(int startPage, int endPage)
{
return wxPrintout::OnBeginDocument(startPage, endPage);
}
void SneedacityPrintout::GetPageInfo(int *minPage, int *maxPage,
int *selPageFrom, int *selPageTo)
{
*minPage = 1;
*maxPage = 1;
*selPageFrom = 1;
*selPageTo = 1;
}
void HandlePageSetup(wxWindow *parent)
{
wxPageSetupData pageSetupData;
wxPageSetupDialog pageSetupDialog(parent, &pageSetupData);
pageSetupDialog.ShowModal();
gPrintData() = pageSetupDialog.GetPageSetupData().GetPrintData();
}
void HandlePrint(
wxWindow *parent, const wxString &name, TrackList *tracks,
TrackPanel &panel)
{
wxPrintDialogData printDialogData(gPrintData());
wxPrinter printer(&printDialogData);
SneedacityPrintout printout(name, tracks, panel);
if (!printer.Print(parent, &printout, true)) {
if (wxPrinter::GetLastError() == wxPRINTER_ERROR) {
SneedacityMessageBox(
XO("There was a problem printing."),
XO("Print"),
wxOK);
}
else {
// Do nothing, the user cancelled...
}
}
else {
gPrintData() = printer.GetPrintDialogData().GetPrintData();
}
}