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"
23#include "ProjectWindows.h"
24#include "TrackArtist.h"
25#include "ViewInfo.h"
26#include "Track.h"
27#include "widgets/Ruler.h"
29
31
32#include "tracks/ui/TrackView.h"
33
36#include "CommonCommandFlags.h"
37#include "Project.h"
38#include "TrackPanel.h"
39
40namespace {
41// Globals, so that we remember settings from session to session
42wxPrintData &gPrintData()
43{
44 static wxPrintData theData;
45 return theData;
46}
47
48class AudacityPrintout final : public wxPrintout
49{
50 public:
52 TrackList *tracks, TrackPanel &panel):
53 wxPrintout(title),
54 mPanel(panel)
55 , mTracks(tracks)
56 {
57 }
58 bool OnPrintPage(int page);
59 bool HasPage(int page);
60 bool OnBeginDocument(int startPage, int endPage);
61 void GetPageInfo(int *minPage, int *maxPage,
62 int *selPageFrom, int *selPageTo);
63
64 private:
67};
68
69bool AudacityPrintout::OnPrintPage(int WXUNUSED(page))
70{
71 wxDC *dc = GetDC();
72 if (!dc)
73 return false;
74
75 int width, height;
76 dc->GetSize(&width, &height);
77
78 int rulerScreenHeight = 40;
79 int screenTotalHeight =
80 TrackView::GetTotalHeight( *mTracks ) + rulerScreenHeight;
81
82 double scale = height / (double)screenTotalHeight;
83
84 int rulerPageHeight = (int)(rulerScreenHeight * scale);
86 ruler.SetBounds(0, 0, width, rulerPageHeight);
87 ruler.SetOrientation(wxHORIZONTAL);
88 ruler.SetRange(0.0, mTracks->GetEndTime());
90 ruler.SetLabelEdges(true);
91 ruler.Draw(*dc);
92
93 TrackArtist artist( &mPanel );
94 artist.SetBackgroundBrushes(*wxWHITE_BRUSH, *wxWHITE_BRUSH,
95 *wxWHITE_PEN, *wxWHITE_PEN);
96 const double screenDuration = mTracks->GetEndTime();
97 SelectedRegion region{};
98 artist.pSelectedRegion = &region;
99 ZoomInfo zoomInfo(0.0, width / screenDuration);
100 artist.pZoomInfo = &zoomInfo;
101 int y = rulerPageHeight;
102
103 for (auto n : mTracks->Any()) {
104 auto &view = TrackView::Get( *n );
105 wxRect r;
106 r.x = 0;
107 r.y = 0;
108 r.width = width;
109 // Note that the views as printed might not have the same proportional
110 // heights as displayed on the screen, because the fixed-sized separators
111 // are counted in those heights but not printed
112 auto trackHeight = (int)(view.GetHeight() * scale);
113 r.height = trackHeight;
114
115 const auto subViews = view.GetSubViews( r );
116 if (subViews.empty())
117 continue;
118
119 auto iter = subViews.begin(), end = subViews.end(), next = iter;
120 auto yy = iter->first;
121 for ( ; iter != end; iter = next ) {
122 ++next;
123 auto nextY = ( next == end )
124 ? trackHeight
125 : next->first;
126 r.y = y + yy;
127 r.SetHeight( nextY - yy );
128 yy = nextY;
129
131 *dc, {}, {}, &artist
132 };
133 iter->second->Draw( context, r, TrackArtist::PassTracks );
134 }
135
136 dc->SetPen(*wxBLACK_PEN);
137 AColor::Line(*dc, 0, y, width, y);
138
139 y += trackHeight;
140 };
141
142 return true;
143}
144
145bool AudacityPrintout::HasPage(int page)
146{
147 return (page==1);
148}
149
150bool AudacityPrintout::OnBeginDocument(int startPage, int endPage)
151{
152 return wxPrintout::OnBeginDocument(startPage, endPage);
153}
154
155void AudacityPrintout::GetPageInfo(int *minPage, int *maxPage,
156 int *selPageFrom, int *selPageTo)
157{
158 *minPage = 1;
159 *maxPage = 1;
160 *selPageFrom = 1;
161 *selPageTo = 1;
162}
163
164void HandlePageSetup(wxWindow *parent)
165{
166 wxPageSetupData pageSetupData;
167
168 wxPageSetupDialog pageSetupDialog(parent, &pageSetupData);
169 pageSetupDialog.ShowModal();
170
171 gPrintData() = pageSetupDialog.GetPageSetupData().GetPrintData();
172}
173
175 wxWindow *parent, const wxString &name, TrackList *tracks,
176 TrackPanel &panel)
177{
178 wxPrintDialogData printDialogData(gPrintData());
179
180 wxPrinter printer(&printDialogData);
181 AudacityPrintout printout(name, tracks, panel);
182 if (!printer.Print(parent, &printout, true)) {
183 if (wxPrinter::GetLastError() == wxPRINTER_ERROR) {
185 XO("There was a problem printing."),
186 XO("Print"),
187 wxOK);
188 }
189 else {
190 // Do nothing, the user cancelled...
191 }
192 }
193 else {
194 gPrintData() = printer.GetPrintDialogData().GetPrintData();
195 }
196}
197
198void OnPageSetup(const CommandContext &context)
199{
200 auto &project = context.project;
201 auto &window = GetProjectFrame( project );
202 HandlePageSetup(&window);
203}
204
205void OnPrint(const CommandContext &context)
206{
207 auto &project = context.project;
208 auto name = project.GetProjectName();
209 auto &tracks = TrackList::Get( project );
210 auto &window = GetProjectFrame( project );
211 HandlePrint(&window, name, &tracks, TrackPanel::Get( project ));
212}
213
214using namespace MenuTable;
216{
217 static BaseItemSharedPtr items{
218 Section( "Print",
219 Command( wxT("PageSetup"), XXO("Pa&ge Setup..."), OnPageSetup,
221 /* i18n-hint: (verb) It's item on a menu. */
222 Command( wxT("Print"), XXO("&Print..."), OnPrint,
224 ) };
225 return items;
226}
227
228AttachedItem sAttachment{ { "File", { OrderingHint::Before, "Exit" } },
230};
231
232}
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:74
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
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
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
AudacityProject & project
Used to display a Ruler.
Definition: Ruler.h:26
void SetOrientation(int orient)
Definition: Ruler.cpp:174
void Draw(wxDC &dc) const
Definition: Ruler.cpp:1428
void SetLabelEdges(bool labelEdges)
Definition: Ruler.cpp:226
void SetBounds(int left, int top, int right, int bottom)
Definition: Ruler.cpp:332
void SetRange(double min, double max)
Definition: Ruler.cpp:188
void SetFormat(RulerFormat format)
Definition: Ruler.cpp:131
@ TimeFormat
Definition: Ruler.h:33
Defines a selected portion of a project.
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:116
const SelectedRegion * pSelectedRegion
Definition: TrackArtist.h:115
A flat linked list of tracks supporting Add, Remove, Clear, and Contains, serialization of the list o...
Definition: Track.h:1338
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:486
The TrackPanel class coordinates updates and operations on the main part of the screen which contains...
Definition: TrackPanel.h:65
static TrackPanel & Get(AudacityProject &project)
Definition: TrackPanel.cpp:230
static int GetTotalHeight(const TrackList &list)
Definition: TrackView.cpp:47
static TrackView & Get(Track &)
Definition: TrackView.cpp:69
AudacityPrintout(wxString title, TrackList *tracks, TrackPanel &panel)
Definition: Printing.cpp:51
std::unique_ptr< MenuPart > Section(const Identifier &internalName, Args &&... args)
std::unique_ptr< CommandItem > Command(const CommandID &name, const TranslatableString &label_in, void(Handler::*pmf)(const CommandContext &), CommandFlag flags, const CommandManager::Options &options={}, CommandHandlerFinder finder=FinderScope::DefaultFinder())
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
std::shared_ptr< BaseItem > BaseItemSharedPtr
Definition: Registry.h:72
void OnPrint(const CommandContext &context)
Definition: Printing.cpp:205
void OnPageSetup(const CommandContext &context)
Definition: Printing.cpp:198
void HandlePageSetup(wxWindow *parent)
Definition: Printing.cpp:164
BaseItemSharedPtr PrintingItems()
Definition: Printing.cpp:215
void HandlePrint(wxWindow *parent, const wxString &name, TrackList *tracks, TrackPanel &panel)
Definition: Printing.cpp:174