Audacity  2.2.0
LabelTrack.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LabelTrack.h
6 
7  Dominic Mazzoni
8  James Crook
9  Jun Wan
10 
11 **********************************************************************/
12 
13 #ifndef _LABELTRACK_
14 #define _LABELTRACK_
15 
16 #include "SelectedRegion.h"
17 #include "Track.h"
18 
19 #include <wx/brush.h>
20 #include <wx/event.h>
21 #include <wx/font.h>
22 #include <wx/pen.h>
23 #include <wx/dynarray.h>
24 #include <wx/string.h>
25 #include <wx/clipbrd.h>
26 
27 
28 class wxKeyEvent;
29 class wxMouseEvent;
30 class wxTextFile;
31 class wxWindow;
32 class wxIcon;
33 class wxBitmap;
34 class TrackList;
35 
36 class AudacityProject;
37 class DirManager;
38 class TimeWarper;
39 class ZoomInfo;
40 
41 
42 struct LabelTrackHit;
44 
46 {
47 public:
48  // Copies region
49  LabelStruct(const SelectedRegion& region, const wxString &aTitle);
50  // Copies region but then overwrites other times
51  LabelStruct(const SelectedRegion& region, double t0, double t1,
52  const wxString &aTitle);
53  void DrawLines( wxDC & dc, const wxRect & r) const;
54  void DrawGlyphs
55  ( wxDC & dc, const wxRect & r, int GlyphLeft, int GlyphRight) const;
56  void DrawText( wxDC & dc, const wxRect & r) const;
57  void DrawTextBox( wxDC & dc, const wxRect & r) const;
58  void DrawHighlight( wxDC & dc, int xPos1, int xPos2, int charHeight) const;
59  void getXPos( wxDC & dc, int * xPos1, int cursorPos) const;
60  const SelectedRegion &getSelectedRegion() const { return selectedRegion; }
61  double getDuration() const { return selectedRegion.duration(); }
62  double getT0() const { return selectedRegion.t0(); }
63  double getT1() const { return selectedRegion.t1(); }
64  // Returns true iff the label got inverted:
65  bool AdjustEdge( int iEdge, double fNewTime);
66  void MoveLabel( int iEdge, double fNewTime);
67 
68  struct BadFormatException {};
69  static LabelStruct Import(wxTextFile &file, int &index);
70 
71  void Export(wxTextFile &file) const;
72 
75  {
82  };
83 
88  TimeRelations RegionRelation(double reg_t0, double reg_t1,
89  const LabelTrack *parent = NULL) const;
90 
91 public:
93  wxString title;
94  mutable int width;
95 
96 // Working storage for on-screen layout.
97  mutable int x;
98  mutable int x1;
99  mutable int xText;
100  mutable int y;
101 
102  bool updated;
103 };
104 
105 using LabelArray = std::vector<LabelStruct>;
106 
107 const int NUM_GLYPH_CONFIGS = 3;
108 const int NUM_GLYPH_HIGHLIGHTS = 4;
109 const int MAX_NUM_ROWS =80;
110 
111 class LabelGlyphHandle;
112 class LabelTextHandle;
113 
114 class AUDACITY_DLL_API LabelTrack final : public Track
115 {
116  friend class LabelStruct;
117 
118  public:
119  bool IsGoodLabelFirstKey(const wxKeyEvent & evt);
120  bool IsGoodLabelEditKey(const wxKeyEvent & evt);
121  bool IsTextSelected();
122 
123  void CreateCustomGlyphs();
124  LabelTrack(const std::shared_ptr<DirManager> &projDirManager);
125  LabelTrack(const LabelTrack &orig);
126 
127  virtual ~ LabelTrack();
128 
129  std::vector<UIHandlePtr> DetailedHitTest
130  (const TrackPanelMouseState &state,
131  const AudacityProject *pProject, int currentTool, bool bMultiTool)
132  override;
133 
134  bool DoCaptureKey(wxKeyEvent &event);
135  unsigned CaptureKey
136  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
137 
138  unsigned KeyDown
139  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
140 
141  unsigned Char
142  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
143 
144  void SetOffset(double dOffset) override;
145 
146  static const int DefaultFontSize = 12;
147 
148  static wxFont GetFont(const wxString &faceName, int size = DefaultFontSize);
149  static void ResetFont();
150 
151  void Draw(TrackPanelDrawingContext &context, const wxRect & r,
153  const ZoomInfo &zoomInfo) const;
154 
155  int getSelectedIndex() const { return mSelIndex; }
156 
157  int GetKind() const override { return Label; }
158 
159  double GetOffset() const override;
160  double GetStartTime() const override;
161  double GetEndTime() const override;
162 
163  using Holder = std::unique_ptr<LabelTrack>;
164  Track::Holder Duplicate() const override;
165 
166  void SetSelected(bool s) override;
167 
168  bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
169  XMLTagHandler *HandleXMLChild(const wxChar *tag) override;
170  void WriteXML(XMLWriter &xmlFile) const override;
171 
172 #if LEGACY_PROJECT_FILE_SUPPORT
173  bool Load(wxTextFile * in, DirManager * dirManager) override;
174  bool Save(wxTextFile * out, bool overwrite) override;
175 #endif
176 
177  Track::Holder Cut (double t0, double t1) override;
178  Track::Holder Copy (double t0, double t1, bool forClipboard = true) const override;
179  void Clear(double t0, double t1) override;
180  void Paste(double t, const Track * src) override;
181  bool Repeat(double t0, double t1, int n);
182 
183  void Silence(double t0, double t1) override;
184  void InsertSilence(double t, double len) override;
185  void OverGlyph(LabelTrackHit &hit, int x, int y) const;
186  static wxBitmap & GetGlyph( int i);
187 
188 
189  struct Flags {
190  int mInitialCursorPos, mCurrentCursorPos, mSelIndex;
191  bool mRightDragging, mDrawCursor;
192  };
193  void ResetFlags();
194  Flags SaveFlags() const
195  {
196  return {
197  mInitialCursorPos, mCurrentCursorPos, mSelIndex,
198  mRightDragging, mDrawCursor
199  };
200  }
201  void RestoreFlags( const Flags& flags );
202 
203  int OverATextBox(int xx, int yy) const;
204  bool OverTextBox(const LabelStruct *pLabel, int x, int y) const;
205  bool CutSelectedText();
206  bool CopySelectedText();
207  bool PasteSelectedText(double sel0, double sel1);
208  static bool IsTextClipSupported();
209 
210  void HandleGlyphClick
211  (LabelTrackHit &hit,
212  const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
213  SelectedRegion *newSel);
214  void HandleTextClick
215  (const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
216  SelectedRegion *newSel);
217  bool HandleGlyphDragRelease
218  (LabelTrackHit &hit,
219  const wxMouseEvent & evt, wxRect & r, const ZoomInfo &zoomInfo,
220  SelectedRegion *newSel);
221  void HandleTextDragRelease(const wxMouseEvent & evt);
222 
223  bool OnKeyDown(SelectedRegion &sel, wxKeyEvent & event);
224  bool OnChar(SelectedRegion &sel, wxKeyEvent & event);
225 
226  void Import(wxTextFile & f);
227  void Export(wxTextFile & f) const;
228 
229  void Unselect();
230 
231  bool IsSelected() const;
232 
233  int GetNumLabels() const;
234  const LabelStruct *GetLabel(int index) const;
235 
236  //This returns the index of the label we just added.
237  int AddLabel(const SelectedRegion &region, const wxString &title = wxT(""),
238  int restoreFocus = -1);
239  //And this tells us the index, if there is a label already there.
240  int GetLabelIndex(double t, double t1);
241 
242  //This deletes the label at given index.
243  void DeleteLabel(int index);
244 
245  //get current cursor position,
246  // relative to the left edge of the track panel
247  bool CalcCursorX(int * x) const;
248 
249  void CalcHighlightXs(int *x1, int *x2) const;
250 
251  void MayAdjustLabel
252  ( LabelTrackHit &hit,
253  int iLabel, int iEdge, bool bAllowSwapping, double fNewTime);
254  void MayMoveLabel( int iLabel, int iEdge, double fNewTime);
255 
256  // This pastes labels without shifting existing ones
257  bool PasteOver(double t, const Track *src);
258 
259  // PRL: These functions were not used because they were not overrides! Was that right?
260  //Track::Holder SplitCut(double b, double e) /* not override */;
261  //bool SplitDelete(double b, double e) /* not override */;
262 
263  void ShiftLabelsOnInsert(double length, double pt);
264  void ChangeLabelsOnReverse(double b, double e);
265  void ScaleLabels(double b, double e, double change);
266  double AdjustTimeStampOnScale(double t, double b, double e, double change);
267  void WarpLabels(const TimeWarper &warper);
268 
269  // Returns tab-separated text of all labels completely within given region
270  wxString GetTextOfLabels(double t0, double t1) const;
271 
272  int FindNextLabel(const SelectedRegion& currentSelection);
273  int FindPrevLabel(const SelectedRegion& currentSelection);
274 
275  public:
276  void SortLabels(LabelTrackHit *pHit = nullptr);
277  private:
278  void ShowContextMenu();
279  void OnContextMenu(wxCommandEvent & evt);
280 
281  int mSelIndex;
284 
285  static int mIconHeight;
286  static int mIconWidth;
287  static int mTextHeight;
288  static bool mbGlyphsReady;
289  static wxBitmap mBoundaryGlyphs[NUM_GLYPH_CONFIGS * NUM_GLYPH_HIGHLIGHTS];
290 
291  static int mFontHeight;
294 
296  bool mDrawCursor;
297  int mRestoreFocus;
299 
301  // Set in copied label tracks
302  double mClipLen;
303 
304  int miLastLabel; // used by FindNextLabel and FindPrevLabel
305 
306  void ComputeLayout(const wxRect & r, const ZoomInfo &zoomInfo) const;
307  void ComputeTextPosition(const wxRect & r, int index) const;
308 
309 public:
310  int FindCurrentCursorPosition(int xPos);
311  void SetCurrentCursorPosition(int xPos);
312 
313 private:
314  void calculateFontHeight(wxDC & dc) const;
315  void RemoveSelectedText();
316 
317  static wxFont msFont;
318 
319  std::weak_ptr<LabelGlyphHandle> mGlyphHandle;
320  std::weak_ptr<LabelTextHandle> mTextHandle;
321 
322 protected:
323  std::shared_ptr<TrackControls> GetControls() override;
324  std::shared_ptr<TrackVRulerControls> GetVRulerControls() override;
325 };
326 
327 #endif
Definition: LabelTextHandle.h:23
Definition: LabelTrack.h:80
static int mIconHeight
Definition: LabelTrack.h:285
void getXPos(wxDC &dc, int *xPos1, int cursorPos) const
Definition: LabelTrack.cpp:711
virtual void Paste(double WXUNUSED(t), const Track *WXUNUSED(src))=0
A list of TrackListNode items.
Definition: Track.h:550
int mCurrentCursorPos
Definition: LabelTrack.h:292
Definition: LabelGlyphHandle.h:31
std::unique_ptr< LabelTrack > Holder
Definition: LabelTrack.h:163
Definition: LabelTrack.h:76
Creates and manages BlockFile objects.
Definition: DirManager.h:52
virtual Holder Copy(double WXUNUSED(t0), double WXUNUSED(t1), bool forClipboard=true) const =0
double t0() const
Definition: SelectedRegion.h:93
ViewInfo is used mainly to hold the zooming, selection and scroll information. It also has some statu...
Definition: ViewInfo.h:138
virtual double GetOffset() const =0
int mxMouseDisplacement
Keeps track of the currently selected label.
Definition: LabelTrack.h:282
virtual std::shared_ptr< TrackVRulerControls > GetVRulerControls()=0
TimeRelations RegionRelation(double reg_t0, double reg_t1, const LabelTrack *parent=NULL) const
Definition: LabelTrack.cpp:1356
virtual unsigned Char(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)
Definition: TrackPanel.cpp:3611
virtual void SetOffset(double o)
Definition: Track.h:267
virtual XMLTagHandler * HandleXMLChild(const wxChar *tag)=0
int mSelIndex
Definition: LabelTrack.h:190
virtual double GetEndTime() const =0
wxString title
Definition: LabelTrack.h:93
int mInitialCursorPos
current cursor position
Definition: LabelTrack.h:293
int xText
Pixel position of right hand glyph.
Definition: LabelTrack.h:99
bool updated
Pixel position of label.
Definition: LabelTrack.h:102
const int MAX_NUM_ROWS
Definition: LabelTrack.h:109
virtual Holder Cut(double WXUNUSED(t0), double WXUNUSED(t1))=0
Main class to control the export function.
TimeRelations
Relationships between selection region and labels.
Definition: LabelTrack.h:74
virtual double GetStartTime() const =0
Definition: TrackPanelMouseEvent.h:23
void DrawHighlight(wxDC &dc, int xPos1, int xPos2, int charHeight) const
Draws text-selected region within the label.
Definition: LabelTrack.cpp:699
double t1() const
Definition: SelectedRegion.h:94
int x1
Pixel position of left hand glyph.
Definition: LabelTrack.h:98
double getDuration() const
Definition: LabelTrack.h:61
int miLastLabel
Definition: LabelTrack.h:304
void Export(wxTextFile &file) const
Definition: LabelTrack.cpp:1331
static int mIconWidth
Definition: LabelTrack.h:286
LabelArray mLabels
Displacement of mouse cursor from the centre being dragged.
Definition: LabelTrack.h:283
Definition: LabelTrack.h:77
Definition: LabelTrack.h:68
virtual unsigned CaptureKey(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)
Definition: TrackPanel.cpp:3593
Definition: ViewInfo.h:31
virtual void SetSelected(bool s)
Definition: Track.cpp:111
Definition: LabelTrack.h:78
std::weak_ptr< LabelTextHandle > mTextHandle
Definition: LabelTrack.h:320
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:114
virtual void WriteXML(XMLWriter &xmlFile) const =0
std::vector< LabelStruct > LabelArray
Definition: LabelTrack.h:105
double mClipLen
when done editing
Definition: LabelTrack.h:302
int x
width of the text in pixels.
Definition: LabelTrack.h:97
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:161
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:45
bool mRightDragging
initial cursor position
Definition: LabelTrack.h:295
int mSelIndex
Definition: LabelTrack.h:281
void DrawTextBox(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:648
int getSelectedIndex() const
Definition: LabelTrack.h:155
Defines a selected portion of a project.
Definition: SelectedRegion.h:37
const int NUM_GLYPH_CONFIGS
Definition: LabelTrack.h:107
std::weak_ptr< LabelGlyphHandle > mGlyphHandle
Definition: LabelTrack.h:319
static int mFontHeight
Definition: LabelTrack.h:291
LabelStruct(const SelectedRegion &region, const wxString &aTitle)
Definition: LabelTrack.cpp:528
static LabelStruct Import(wxTextFile &file, int &index)
Definition: LabelTrack.cpp:1259
virtual bool HandleXMLTag(const wxChar *tag, const wxChar **attrs)=0
Fundamental data object of Audacity, placed in the TrackPanel. Classes derived form it include the Wa...
Definition: Track.h:85
bool mDrawCursor
flag to tell if it's a valid dragging
Definition: LabelTrack.h:296
virtual void Clear(double WXUNUSED(t0), double WXUNUSED(t1))=0
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:69
int width
Text of the label.
Definition: LabelTrack.h:94
Definition: LabelTrack.h:81
static bool mbGlyphsReady
Definition: LabelTrack.h:288
int GetKind() const override
Definition: LabelTrack.h:157
virtual void InsertSilence(double WXUNUSED(t), double WXUNUSED(len))=0
Definition: LabelTrack.h:189
double getT1() const
Definition: LabelTrack.h:63
bool mRightDragging
Definition: LabelTrack.h:191
void MoveLabel(int iEdge, double fNewTime)
Definition: LabelTrack.cpp:1244
const SelectedRegion & getSelectedRegion() const
Definition: LabelTrack.h:60
static wxFont msFont
Definition: LabelTrack.h:317
SelectedRegion selectedRegion
Definition: LabelTrack.h:92
virtual std::vector< UIHandlePtr > DetailedHitTest(const TrackPanelMouseState &, const AudacityProject *pProject, int currentTool, bool bMultiTool)=0
const int NUM_GLYPH_HIGHLIGHTS
Definition: LabelTrack.h:108
Flags SaveFlags() const
Definition: LabelTrack.h:194
Definition: LabelTrack.h:79
Transforms one point in time to another point. For example, a time stretching effect might use one to...
Definition: TimeWarper.h:61
std::unique_ptr< Track > Holder
Definition: Track.h:245
Definition: LabelGlyphHandle.h:39
static int mTextHeight
Definition: LabelTrack.h:287
virtual void Silence(double WXUNUSED(t0), double WXUNUSED(t1))=0
Definition: Track.h:231
double getT0() const
Definition: LabelTrack.h:62
void DrawText(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:623
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:22
int y
Pixel position of left hand side of text box.
Definition: LabelTrack.h:100
void DrawLines(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:562
double duration() const
Definition: SelectedRegion.h:95
virtual std::shared_ptr< TrackControls > GetControls()=0
void DrawGlyphs(wxDC &dc, const wxRect &r, int GlyphLeft, int GlyphRight) const
Definition: LabelTrack.cpp:600
virtual Holder Duplicate() const =0
bool AdjustEdge(int iEdge, double fNewTime)
Definition: LabelTrack.cpp:1234
Definition: TrackPanelDrawingContext.h:24
virtual unsigned KeyDown(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)
Definition: TrackPanel.cpp:3599