Audacity 3.2.0
Ruler.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 Ruler.h
6
7 Dominic Mazzoni
8
9**********************************************************************/
10
11#ifndef __AUDACITY_RULER__
12#define __AUDACITY_RULER__
13
14#include "wxPanelWrapper.h" // to inherit
15#include "NumberScale.h" // member variable
16
17#include <wx/colour.h> // member variable
18#include <wx/pen.h> // member variable
19
20class wxDC;
21class wxFont;
22
23class Envelope;
24class ZoomInfo;
25
26class AUDACITY_DLL_API Ruler {
27 public:
28
35 };
36
37 //
38 // Constructor / Destructor
39 //
40
41 Ruler();
42 ~Ruler();
43
44 //
45 // Required Ruler Parameters
46 //
47
48 void SetBounds(int left, int top, int right, int bottom);
49
50 // wxHORIZONTAL || wxVERTICAL
51 void SetOrientation(int orient);
52
53 // min is the value at (x, y)
54 // max is the value at (x+width, y+height)
55 // (at the center of the pixel, in both cases)
56 void SetRange(double min, double max);
57
58 // An overload needed for the special case of fisheye
59 // min is the value at (x, y)
60 // max is the value at (x+width, y+height)
61 // hiddenMin, hiddenMax are the values that would be shown without the fisheye.
62 // (at the center of the pixel, in both cases)
63 void SetRange(double min, double max, double hiddenMin, double hiddenMax);
64
65 //
66 // Optional Ruler Parameters
67 //
68
69 // If twoTone is true, cause zero and positive numbers to appear black, negative in another color.
70 void SetTwoTone(bool twoTone);
71
72 // IntFormat, RealFormat, or TimeFormat
73 void SetFormat(RulerFormat format);
74
75 // Specify the name of the units (like "dB") if you
76 // want numbers like "1.6" formatted as "1.6 dB".
77 void SetUnits(const TranslatableString &units);
78 void SetDbMirrorValue( const double d );
79
80 // Logarithmic
81 void SetLog(bool log);
82
83 // Minimum number of pixels between labels
84 void SetSpacing(int spacing);
85
86 // If this is true, the edges of the ruler will always
87 // receive a label. If not, the nearest round number is
88 // labeled (which may or may not be the edge).
89 void SetLabelEdges(bool labelEdges);
90
91 // Makes a vertical ruler hug the left side (instead of right)
92 // and a horizontal ruler hug the top (instead of bottom)
93 void SetFlip(bool flip);
94
95 // Set it to false if you don't want minor labels.
96 void SetMinor(bool value);
97
98 // Good defaults are provided, but you can override here
99 void SetFonts(const wxFont &minorFont, const wxFont &majorFont, const wxFont &minorMinorFont);
100 struct Fonts {
101 wxFont major, minor, minorMinor;
102 int lead;
103 };
104 Fonts GetFonts() const;
105
106 void SetNumberScale(const NumberScale &scale);
107
108 // The ruler will not draw text within this (pixel) range.
109 // Use this if you have another graphic object obscuring part
110 // of the ruler's area. The values start and end are interpreted
111 // relative to the Ruler's local coordinates.
112 void OfflimitsPixels(int start, int end);
113
114 //
115 // Calculates and returns the maximum size required by the ruler
116 //
117 void GetMaxSize(wxCoord *width, wxCoord *height);
118
119
120 // The following functions should allow a custom ruler setup:
121 // autosize is a GREAT thing, but for some applications it's
122 // useful the definition of a label array and label step by
123 // the user.
124 void SetCustomMode(bool value);
125 // If this is the case, you should provide an array of labels, start
126 // label position, and labels step. The range eventually specified will be
127 // ignored.
129 const TranslatableStrings &labels, int start, int step);
131 const TranslatableStrings &labels, int start, int step);
132
133 void SetUseZoomInfo(int leftOffset, const ZoomInfo *zoomInfo);
134
135 //
136 // Drawing
137 //
138
139 // Note that it will not erase for you...
140 void Draw(wxDC& dc) const;
141 void Draw(wxDC& dc, const Envelope* envelope) const;
142 // If length <> 0, draws lines perpendiculars to ruler corresponding
143 // to selected ticks (major, minor, or both), in an adjacent window.
144 // You may need to use the offsets if you are using part of the dc for rulers, borders etc.
145 void DrawGrid(wxDC& dc, int length, bool minor = true, bool major = true, int xOffset = 0, int yOffset = 0) const;
146
147 // So we can have white ticks on black...
148 void SetTickColour( const wxColour & colour)
149 { mTickColour = colour; mPen.SetColour( colour );}
150
151 // Force regeneration of labels at next draw time
152 void Invalidate();
153
154 private:
155 struct TickSizes;
156
157 class Label {
158 public:
159 double value;
160 int pos;
161 int lx, ly;
163
164 void Draw(wxDC &dc, bool twoTone, wxColour c) const;
165 };
166 using Labels = std::vector<Label>;
167
168 using Bits = std::vector< bool >;
169
170 void ChooseFonts( wxDC &dc ) const;
171
172 void UpdateCache( wxDC &dc, const Envelope* envelope ) const;
173
174 struct Updater;
175
176public:
177 bool mbTicksOnly; // true => no line the length of the ruler
179
180private:
181 wxColour mTickColour;
182 wxPen mPen;
183
184 int mLeft, mTop, mRight, mBottom;
186
187 std::unique_ptr<Fonts> mpUserFonts;
188 mutable std::unique_ptr<Fonts> mpFonts;
189
190 double mMin, mMax;
191 double mHiddenMin, mHiddenMax;
192
194
195 static std::pair< wxRect, Label > MakeTick(
196 Label lab,
197 wxDC &dc, wxFont font,
198 std::vector<bool> &bits,
199 int left, int top, int spacing, int lead,
200 bool flip, int orientation );
201
202 struct Cache;
203 mutable std::unique_ptr<Cache> mpCache;
204
205 // Returns 'zero' label coordinate (for grid drawing)
206 int FindZero( const Labels &labels ) const;
207
208 int GetZeroPosition() const;
209
216 bool mLog;
217 bool mFlip;
224
226};
227
228class AUDACITY_DLL_API RulerPanel final : public wxPanelWrapper {
229 DECLARE_DYNAMIC_CLASS(RulerPanel)
230
231 public:
232 using Range = std::pair<double, double>;
233
234 struct Options {
235 bool log { false };
236 bool flip { false };
237 bool labelEdges { false };
238 bool ticksAtExtremes { false };
239 bool hasTickColour{ false };
240 wxColour tickColour;
241
243
244 Options &Log( bool l )
245 { log = l; return *this; }
246
247 Options &Flip( bool f )
248 { flip = f; return *this; }
249
251 { labelEdges = l; return *this; }
252
254 { ticksAtExtremes = t; return *this; }
255
256 Options &TickColour( const wxColour c )
257 { tickColour = c; hasTickColour = true; return *this; }
258 };
259
260 RulerPanel(wxWindow* parent, wxWindowID id,
261 wxOrientation orientation,
262 const wxSize &bounds,
263 const Range &range,
265 const TranslatableString &units,
266 const Options &options = {},
267 const wxPoint& pos = wxDefaultPosition,
268 const wxSize& size = wxDefaultSize);
269
270 ~RulerPanel();
271
272 void DoSetSize(int x, int y,
273 int width, int height,
274 int sizeFlags = wxSIZE_AUTO) override;
275
276 void OnErase(wxEraseEvent &evt);
277 void OnPaint(wxPaintEvent &evt);
278 void OnSize(wxSizeEvent &evt);
279 void SetTickColour( wxColour & c){ ruler.SetTickColour( c );}
280
281 // We don't need or want to accept focus.
282 bool AcceptsFocus() const override { return false; }
283 // So that wxPanel is not included in Tab traversal - see wxWidgets bug 15581
284 bool AcceptsFocusFromKeyboard() const override { return false; }
285
286 public:
287
289
290private:
291 DECLARE_EVENT_TABLE()
292};
293
294#endif //define __AUDACITY_RULER__
int min(int a, int b)
int format
Definition: ExportPCM.cpp:56
std::vector< TranslatableString > TranslatableStrings
static void OnSize(wxSizeEvent &evt)
Definition: VSTEffect.cpp:2207
Piecewise linear or piecewise exponential function from double to double.
Definition: Envelope.h:72
An array of these created by the Ruler is used to determine what and where text annotations to the nu...
Definition: Ruler.h:157
int lx
Definition: Ruler.h:161
int pos
Definition: Ruler.h:160
TranslatableString text
Definition: Ruler.h:162
double value
Definition: Ruler.h:159
Used to display a Ruler.
Definition: Ruler.h:26
wxColour mTickColour
Definition: Ruler.h:181
void SetTickColour(const wxColour &colour)
Definition: Ruler.h:148
TranslatableString mUnits
Definition: Ruler.h:220
Bits mUserBits
Definition: Ruler.h:193
double mHiddenMax
Definition: Ruler.h:191
bool mLabelEdges
Definition: Ruler.h:214
wxPen mPen
Definition: Ruler.h:182
std::unique_ptr< Cache > mpCache
Definition: Ruler.h:202
NumberScale mNumberScale
Definition: Ruler.h:225
double mMax
Definition: Ruler.h:190
std::vector< Label > Labels
Definition: Ruler.h:166
const ZoomInfo * mUseZoomInfo
Definition: Ruler.h:222
bool mbTicksOnly
Definition: Ruler.h:174
bool mHasSetSpacing
Definition: Ruler.h:213
int mOrientation
Definition: Ruler.h:210
int mBottom
Definition: Ruler.h:184
double mDbMirrorValue
Definition: Ruler.h:212
void SetCustomMinorLabels(const TranslatableStrings &labels, int start, int step)
bool mLog
Definition: Ruler.h:216
int mLength
Definition: Ruler.h:185
int mSpacing
Definition: Ruler.h:211
bool mbTicksAtExtremes
Definition: Ruler.h:178
std::vector< bool > Bits
Definition: Ruler.h:168
bool mCustom
Definition: Ruler.h:218
RulerFormat mFormat
Definition: Ruler.h:215
int mLeftOffset
Definition: Ruler.h:223
std::unique_ptr< Fonts > mpUserFonts
Definition: Ruler.h:187
bool mFlip
Definition: Ruler.h:217
std::unique_ptr< Fonts > mpFonts
Definition: Ruler.h:188
bool mbMinor
Definition: Ruler.h:219
void SetCustomMajorLabels(const TranslatableStrings &labels, int start, int step)
RulerFormat
Definition: Ruler.h:29
@ TimeFormat
Definition: Ruler.h:33
@ LinearDBFormat
Definition: Ruler.h:34
@ RealFormat
Definition: Ruler.h:31
@ IntFormat
Definition: Ruler.h:30
@ RealLogFormat
Definition: Ruler.h:32
bool mTwoTone
Definition: Ruler.h:221
RulerPanel class allows you to work with a Ruler like any other wxWindow.
Definition: Ruler.h:228
Ruler ruler
Definition: Ruler.h:288
void SetTickColour(wxColour &c)
Definition: Ruler.h:279
bool AcceptsFocusFromKeyboard() const override
Definition: Ruler.h:284
std::pair< double, double > Range
Definition: Ruler.h:232
bool AcceptsFocus() const override
Definition: Ruler.h:282
Holds a msgid for the translation catalog; may also bind format arguments.
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
std::vector< CommandFlagOptions > & Options()
Definition: Menus.cpp:535
int lead
Definition: Ruler.h:102
wxFont major
Definition: Ruler.h:101
Options & TickColour(const wxColour c)
Definition: Ruler.h:256
Options & TicksAtExtremes(bool t)
Definition: Ruler.h:253
wxColour tickColour
Definition: Ruler.h:240
Options & LabelEdges(bool l)
Definition: Ruler.h:250
Options & Flip(bool f)
Definition: Ruler.h:247
Options & Log(bool l)
Definition: Ruler.h:244