Audacity 3.2.0
Printing.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Printing.cpp
6
7 Dominic Mazzoni
8
9*******************************************************************//*******************************************************************/
15
16
17#include <wx/defs.h>
18#include <wx/dc.h>
19#include <wx/print.h>
20#include <wx/printdlg.h>
21
22#include "AColor.h"
24#include "ProjectWindows.h"
25#include "TrackArtist.h"
26#include "ViewInfo.h"
27#include "Track.h"
28#include "widgets/Ruler.h"
29#include "AudacityMessageBox.h"
31#include "widgets/TimeFormat.h"
32
34
36
37#include "CommandContext.h"
38#include "MenuRegistry.h"
39#include "CommonCommandFlags.h"
40#include "Project.h"
41#include "TrackPanel.h"
42
43namespace {
44// Globals, so that we remember settings from session to session
45wxPrintData &gPrintData()
46{
47 static wxPrintData theData;
48 return theData;
49}
50
51class AudacityPrintout final : public wxPrintout
52{
53 public:
55 TrackList *tracks, TrackPanel &panel):
56 wxPrintout(title),
57 mPanel(panel)
58 , mTracks(tracks)
59 {
60 }
61 bool OnPrintPage(int page);
62 bool HasPage(int page);
63 bool OnBeginDocument(int startPage, int endPage);
64 void GetPageInfo(int *minPage, int *maxPage,
65 int *selPageFrom, int *selPageTo);
66
67 private:
70};
71
72bool AudacityPrintout::OnPrintPage(int WXUNUSED(page))
73{
74 wxDC *dc = GetDC();
75 if (!dc)
76 return false;
77
78 int width, height;
79 dc->GetSize(&width, &height);
80
81 int rulerScreenHeight = 40;
82 int screenTotalHeight =
83 ChannelView::GetTotalHeight(*mTracks) + rulerScreenHeight;
84
85 double scale = height / (double)screenTotalHeight;
86
87 int rulerPageHeight = (int)(rulerScreenHeight * scale);
89 ruler.SetBounds(0, 0, width, rulerPageHeight);
90 ruler.SetOrientation(wxHORIZONTAL);
91 ruler.SetRange(0.0, mTracks->GetEndTime());
92 ruler.SetLabelEdges(true);
93 ruler.Draw(*dc);
94
95 TrackArtist artist( &mPanel );
96 artist.SetBackgroundBrushes(*wxWHITE_BRUSH, *wxWHITE_BRUSH,
97 *wxWHITE_PEN, *wxWHITE_PEN);
98 const double screenDuration = mTracks->GetEndTime();
99 SelectedRegion region{};
100 artist.pSelectedRegion = &region;
101 ZoomInfo zoomInfo(0.0, width / screenDuration);
102 artist.pZoomInfo = &zoomInfo;
103 int y = rulerPageHeight;
104
105 for (auto l : *mTracks) {
106 for (auto n : l->Channels()) {
107 auto &view = ChannelView::Get(*n);
108 wxRect r;
109 r.x = 0;
110 r.y = 0;
111 r.width = width;
112 // Note that the views as printed might not have the same proportional
113 // heights as displayed on the screen, because the fixed-sized separators
114 // are counted in those heights but not printed
115 auto trackHeight = (int)(view.GetHeight() * scale);
116 r.height = trackHeight;
117
118 const auto subViews = view.GetSubViews( r );
119 if (subViews.empty())
120 continue;
121
122 auto iter = subViews.begin(), end = subViews.end(), next = iter;
123 auto yy = iter->first;
124 for ( ; iter != end; iter = next ) {
125 ++next;
126 auto nextY = ( next == end )
127 ? trackHeight
128 : next->first;
129 r.y = y + yy;
130 r.SetHeight( nextY - yy );
131 yy = nextY;
132
134 *dc, {}, {}, &artist
135 };
136 iter->second->Draw( context, r, TrackArtist::PassTracks );
137 }
138
139 dc->SetPen(*wxBLACK_PEN);
140 AColor::Line(*dc, 0, y, width, y);
141
142 y += trackHeight;
143 }
144 };
145
146 return true;
147}
148
149bool AudacityPrintout::HasPage(int page)
150{
151 return (page==1);
152}
153
154bool AudacityPrintout::OnBeginDocument(int startPage, int endPage)
155{
156 return wxPrintout::OnBeginDocument(startPage, endPage);
157}
158
159void AudacityPrintout::GetPageInfo(int *minPage, int *maxPage,
160 int *selPageFrom, int *selPageTo)
161{
162 *minPage = 1;
163 *maxPage = 1;
164 *selPageFrom = 1;
165 *selPageTo = 1;
166}
167
168void HandlePageSetup(wxWindow *parent)
169{
170 wxPageSetupData pageSetupData;
171
172 wxPageSetupDialog pageSetupDialog(parent, &pageSetupData);
173 pageSetupDialog.ShowModal();
174
175 gPrintData() = pageSetupDialog.GetPageSetupData().GetPrintData();
176}
177
179 wxWindow *parent, const wxString &name, TrackList *tracks,
180 TrackPanel &panel)
181{
182 wxPrintDialogData printDialogData(gPrintData());
183
184 wxPrinter printer(&printDialogData);
185 AudacityPrintout printout(name, tracks, panel);
186 if (!printer.Print(parent, &printout, true)) {
187 if (wxPrinter::GetLastError() == wxPRINTER_ERROR) {
189 XO("There was a problem printing."),
190 XO("Print"),
191 wxOK);
192 }
193 else {
194 // Do nothing, the user cancelled...
195 }
196 }
197 else {
198 gPrintData() = printer.GetPrintDialogData().GetPrintData();
199 }
200}
201
202void OnPageSetup(const CommandContext &context)
203{
204 auto &project = context.project;
205 auto &window = GetProjectFrame( project );
206 HandlePageSetup(&window);
207}
208
209void OnPrint(const CommandContext &context)
210{
211 auto &project = context.project;
212 auto name = project.GetProjectName();
213 auto &tracks = TrackList::Get( project );
214 auto &window = GetProjectFrame( project );
216}
217
218using namespace MenuRegistry;
220{
221 static auto items = std::shared_ptr{
222 Section( "Print",
223 Command( wxT("PageSetup"), XXO("Pa&ge Setup..."), OnPageSetup,
225 /* i18n-hint: (verb) It's item on a menu. */
226 Command( wxT("Print"), XXO("&Print..."), OnPrint,
228 ) };
229 return items;
230}
231
233 { "File", { OrderingHint::Before, "Exit" } }
234};
235
236}
wxT("CloseDown"))
int AudacityMessageBox(const TranslatableString &message, const TranslatableString &caption, long style, wxWindow *parent, int x, int y)
const ReservedCommandFlag & AudioIONotBusyFlag()
const ReservedCommandFlag & TracksExistFlag()
const TranslatableString name
Definition: Distortion.cpp:76
XO("Cut/Copy/Paste")
XXO("&Cut/Copy/Paste Toolbar")
static const auto title
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
accessors for certain important windows associated with each project
const auto tracks
const auto project
declares abstract base class Track, TrackList, and iterators over TrackList
static void Line(wxDC &dc, wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
Definition: AColor.cpp:187
static int GetTotalHeight(const TrackList &list)
Definition: ChannelView.cpp:55
static ChannelView & Get(Channel &channel)
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & project
static const LinearUpdater & Instance()
Generates classes whose instances register items at construction.
Definition: Registry.h:388
Used to display a Ruler.
Definition: Ruler.h:34
void SetOrientation(int orient)
Definition: Ruler.cpp:141
void Draw(wxDC &dc) const
Definition: Ruler.cpp:441
void SetLabelEdges(bool labelEdges)
Definition: Ruler.cpp:179
void SetBounds(int left, int top, int right, int bottom)
Definition: Ruler.cpp:304
void SetRange(double min, double max)
Definition: Ruler.cpp:152
Defines a selected portion of a project.
static const TimeFormat & Instance()
Definition: TimeFormat.cpp:15
This class handles the actual rendering of WaveTracks (both waveforms and spectra),...
Definition: TrackArtist.h:39
void SetBackgroundBrushes(wxBrush unselectedBrushIn, wxBrush selectedBrushIn, wxPen unselectedPenIn, wxPen selectedPenIn)
Definition: TrackArtist.h:60
ZoomInfo * pZoomInfo
Definition: TrackArtist.h:123
const SelectedRegion * pSelectedRegion
Definition: TrackArtist.h:122
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:975
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:347
The TrackPanel class coordinates updates and operations on the main part of the screen which contains...
Definition: TrackPanel.h:63
static TrackPanel & Get(AudacityProject &project)
Definition: TrackPanel.cpp:233
AudacityPrintout(wxString title, TrackList *tracks, TrackPanel &panel)
Definition: Printing.cpp:54
constexpr auto Section
Definition: MenuRegistry.h:436
constexpr auto Command
Definition: MenuRegistry.h:456
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
std::unique_ptr< detail::IndirectItem< Item > > Indirect(const std::shared_ptr< Item > &ptr)
A convenience function.
Definition: Registry.h:175
void OnPrint(const CommandContext &context)
Definition: Printing.cpp:209
void OnPageSetup(const CommandContext &context)
Definition: Printing.cpp:202
void HandlePageSetup(wxWindow *parent)
Definition: Printing.cpp:168
void HandlePrint(wxWindow *parent, const wxString &name, TrackList *tracks, TrackPanel &panel)
Definition: Printing.cpp:178