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
35#include "tracks/ui/TrackView.h"
36
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 TrackView::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 n : mTracks->Any()) {
106 auto &view = TrackView::Get( *n );
107 wxRect r;
108 r.x = 0;
109 r.y = 0;
110 r.width = width;
111 // Note that the views as printed might not have the same proportional
112 // heights as displayed on the screen, because the fixed-sized separators
113 // are counted in those heights but not printed
114 auto trackHeight = (int)(view.GetHeight() * scale);
115 r.height = trackHeight;
116
117 const auto subViews = view.GetSubViews( r );
118 if (subViews.empty())
119 continue;
120
121 auto iter = subViews.begin(), end = subViews.end(), next = iter;
122 auto yy = iter->first;
123 for ( ; iter != end; iter = next ) {
124 ++next;
125 auto nextY = ( next == end )
126 ? trackHeight
127 : next->first;
128 r.y = y + yy;
129 r.SetHeight( nextY - yy );
130 yy = nextY;
131
133 *dc, {}, {}, &artist
134 };
135 iter->second->Draw( context, r, TrackArtist::PassTracks );
136 }
137
138 dc->SetPen(*wxBLACK_PEN);
139 AColor::Line(*dc, 0, y, width, y);
140
141 y += trackHeight;
142 };
143
144 return true;
145}
146
147bool AudacityPrintout::HasPage(int page)
148{
149 return (page==1);
150}
151
152bool AudacityPrintout::OnBeginDocument(int startPage, int endPage)
153{
154 return wxPrintout::OnBeginDocument(startPage, endPage);
155}
156
157void AudacityPrintout::GetPageInfo(int *minPage, int *maxPage,
158 int *selPageFrom, int *selPageTo)
159{
160 *minPage = 1;
161 *maxPage = 1;
162 *selPageFrom = 1;
163 *selPageTo = 1;
164}
165
166void HandlePageSetup(wxWindow *parent)
167{
168 wxPageSetupData pageSetupData;
169
170 wxPageSetupDialog pageSetupDialog(parent, &pageSetupData);
171 pageSetupDialog.ShowModal();
172
173 gPrintData() = pageSetupDialog.GetPageSetupData().GetPrintData();
174}
175
177 wxWindow *parent, const wxString &name, TrackList *tracks,
178 TrackPanel &panel)
179{
180 wxPrintDialogData printDialogData(gPrintData());
181
182 wxPrinter printer(&printDialogData);
183 AudacityPrintout printout(name, tracks, panel);
184 if (!printer.Print(parent, &printout, true)) {
185 if (wxPrinter::GetLastError() == wxPRINTER_ERROR) {
187 XO("There was a problem printing."),
188 XO("Print"),
189 wxOK);
190 }
191 else {
192 // Do nothing, the user cancelled...
193 }
194 }
195 else {
196 gPrintData() = printer.GetPrintDialogData().GetPrintData();
197 }
198}
199
200void OnPageSetup(const CommandContext &context)
201{
202 auto &project = context.project;
203 auto &window = GetProjectFrame( project );
204 HandlePageSetup(&window);
205}
206
207void OnPrint(const CommandContext &context)
208{
209 auto &project = context.project;
210 auto name = project.GetProjectName();
211 auto &tracks = TrackList::Get( project );
212 auto &window = GetProjectFrame( project );
213 HandlePrint(&window, name, &tracks, TrackPanel::Get( project ));
214}
215
216using namespace MenuTable;
218{
219 static BaseItemSharedPtr items{
220 Section( "Print",
221 Command( wxT("PageSetup"), XXO("Pa&ge Setup..."), OnPageSetup,
223 /* i18n-hint: (verb) It's item on a menu. */
224 Command( wxT("Print"), XXO("&Print..."), OnPrint,
226 ) };
227 return items;
228}
229
230AttachedItem sAttachment{ { "File", { OrderingHint::Before, "Exit" } },
232};
233
234}
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
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
static const LinearUpdater & Instance()
Used to display a Ruler.
Definition: Ruler.h:28
void SetOrientation(int orient)
Definition: Ruler.cpp:140
void Draw(wxDC &dc) const
Definition: Ruler.cpp:438
void SetLabelEdges(bool labelEdges)
Definition: Ruler.cpp:178
void SetBounds(int left, int top, int right, int bottom)
Definition: Ruler.cpp:303
void SetRange(double min, double max)
Definition: Ruler.cpp:151
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: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:1201
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:360
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:231
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:54
constexpr auto Section
constexpr auto Command
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
std::unique_ptr< IndirectItem > Indirect(const BaseItemSharedPtr &ptr)
A convenience function.
Definition: Registry.h:97
std::shared_ptr< BaseItem > BaseItemSharedPtr
Definition: Registry.h:74
void OnPrint(const CommandContext &context)
Definition: Printing.cpp:207
void OnPageSetup(const CommandContext &context)
Definition: Printing.cpp:200
void HandlePageSetup(wxWindow *parent)
Definition: Printing.cpp:166
BaseItemSharedPtr PrintingItems()
Definition: Printing.cpp:217
void HandlePrint(wxWindow *parent, const wxString &name, TrackList *tracks, TrackPanel &panel)
Definition: Printing.cpp:176