Audacity  2.2.2
LabelDialog.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LabelDialog.cpp
6 
7  Dominic Mazzoni
8 
9 *******************************************************************//*******************************************************************/
15 
16 #include "Audacity.h"
17 #include "LabelDialog.h"
18 
19 #include <wx/button.h>
20 #include <wx/defs.h>
21 #include <wx/choice.h>
22 #include <wx/dc.h>
23 #include <wx/dialog.h>
24 #include <wx/filedlg.h>
25 #include <wx/grid.h>
26 #include <wx/intl.h>
27 #include <wx/settings.h>
28 #include <wx/sizer.h>
29 #include <wx/stattext.h>
30 #include <wx/textdlg.h>
31 
32 #include "ShuttleGui.h"
33 #include "Internat.h"
34 #include "LabelTrack.h"
35 #include "Prefs.h"
36 #include "Project.h"
37 #include "ViewInfo.h"
39 #include "widgets/ErrorDialog.h"
40 #include "widgets/HelpSystem.h"
41 
42 #include "FileNames.h"
43 #include <limits>
44 
45 enum Column
46 {
54 };
55 
56 
57 
58 class RowData
59 {
60  public:
61  RowData(int index_, const wxString &title_, const SelectedRegion &selectedRegion_)
62  : index(index_), title(title_), selectedRegion(selectedRegion_)
63  {}
64 
65  int index;
66  wxString title;
68 };
69 
70 enum {
71  ID_INSERTA = 11000,
76 };
77 
78 BEGIN_EVENT_TABLE(LabelDialog, wxDialogWrapper)
79  EVT_GRID_SELECT_CELL(LabelDialog::OnSelectCell)
80  EVT_GRID_CELL_CHANGED(LabelDialog::OnCellChange)
83  EVT_BUTTON(ID_REMOVE, LabelDialog::OnRemove)
86  EVT_BUTTON(wxID_OK, LabelDialog::OnOK)
87  EVT_BUTTON(wxID_CANCEL, LabelDialog::OnCancel)
88  EVT_COMMAND(wxID_ANY, EVT_TIMETEXTCTRL_UPDATED, LabelDialog::OnUpdate)
89  EVT_COMMAND(wxID_ANY, EVT_FREQUENCYTEXTCTRL_UPDATED,
90  LabelDialog::OnFreqUpdate)
91  EVT_BUTTON(wxID_HELP, LabelDialog::OnHelp)
93 
94 LabelDialog::LabelDialog(wxWindow *parent,
95  TrackFactory &factory,
96  TrackList *tracks,
97  LabelTrack *selectedTrack,
98  int index,
99  ViewInfo &viewinfo,
100  double rate,
101  const NumericFormatId & format,
102  const NumericFormatId &freqFormat)
103 : wxDialogWrapper(parent,
104  wxID_ANY,
105  _("Edit Labels"),
106  wxDefaultPosition,
107  wxSize(800, 600),
108  wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
109  mFactory(factory),
110  mTracks(tracks)
111  , mSelectedTrack(selectedTrack)
112  , mIndex(index)
113  , mViewInfo(&viewinfo),
114  mRate(rate),
115  mFormat(format)
116  , mFreqFormat(freqFormat)
117 {
118  SetName(GetTitle());
119  Populate();
120 }
121 
123 {
124 }
125 
127 {
128  // Build the initial (empty) grid
129  mGrid->CreateGrid(0, Col_Max);
130  mGrid->SetDefaultCellAlignment(wxALIGN_LEFT, wxALIGN_CENTER);
131 
132  /* i18n-hint: (noun). A track contains waves, audio etc.*/
133  mGrid->SetColLabelValue(0,_("Track"));
134  /* i18n-hint: (noun)*/
135  mGrid->SetColLabelValue(1,_("Label"));
136  /* i18n-hint: (noun) of a label*/
137  mGrid->SetColLabelValue(2,_("Start Time"));
138  /* i18n-hint: (noun) of a label*/
139  mGrid->SetColLabelValue(3,_("End Time"));
140  /* i18n-hint: (noun) of a label*/
141  mGrid->SetColLabelValue(4,_("Low Frequency"));
142  /* i18n-hint: (noun) of a label*/
143  mGrid->SetColLabelValue(5,_("High Frequency"));
144 
145  // Create and remember editors. No need to DELETE these as the wxGrid will
146  // do it for us. (The DecRef() that is needed after GetDefaultEditorForType
147  // becomes the duty of the wxGridCellAttr objects after we set them in the grid.)
148  mChoiceEditor = (ChoiceEditor *) mGrid->GetDefaultEditorForType(GRID_VALUE_CHOICE);
149  mTimeEditor = static_cast<NumericEditor*>
150  (mGrid->GetDefaultEditorForType(GRID_VALUE_TIME));
151  mFrequencyEditor = static_cast<NumericEditor *>
152  (mGrid->GetDefaultEditorForType(GRID_VALUE_FREQUENCY));
153 
154  // Initialize and set the track name column attributes
155  wxGridCellAttr *attr;
156  mGrid->SetColAttr(Col_Track, (attr = safenew wxGridCellAttr));
157  attr->SetEditor(mChoiceEditor);
158  mTrackNames.Add(_("New..."));
159 
160  // Initialize and set the time column attributes
161  mGrid->SetColAttr(Col_Stime, (attr = safenew wxGridCellAttr));
162  // Don't need DecRef() after this GetDefaultRendererForType.
163  attr->SetRenderer(mGrid->GetDefaultRendererForType(GRID_VALUE_TIME));
164  attr->SetEditor(mTimeEditor);
165  attr->SetAlignment(wxALIGN_CENTER, wxALIGN_CENTER);
166 
167  mGrid->SetColAttr(Col_Etime, attr->Clone());
168 
169  // Initialize and set the frequency column attributes
170  mGrid->SetColAttr(Col_Lfreq, (attr = safenew wxGridCellAttr));
171  // Don't need DecRef() after this GetDefaultRendererForType.
172  attr->SetRenderer(mGrid->GetDefaultRendererForType(GRID_VALUE_FREQUENCY));
173  attr->SetEditor(mFrequencyEditor);
174  attr->SetAlignment(wxALIGN_CENTER, wxALIGN_CENTER);
175 
176  mGrid->SetColAttr(Col_Hfreq, attr->Clone());
177 
178  // Seems there's a bug in wxGrid. Adding only 1 row does not
179  // allow SetCellSize() to work properly and you will not get
180  // the expected 1 row by 4 column cell.
181  //
182  // So, we set the minimum row height to 0 and basically hide
183  // the extra row by setting its height to 0. And not allowing the
184  // rows to be manually resized prevents the user from ever seeing
185  // the extra row.
186  mGrid->SetRowMinimalAcceptableHeight(0);
187  mGrid->EnableDragRowSize(false);
188 
189  // Locate all labels in current track list
190  FindAllLabels();
191 
192  // Populate the grid
194 
195  // Resize the label name column and ensure it doesn't go below an
196  // arbitrary width.
197  //
198  // This should not be in TransferDataToWindow() since a user might
199  // resize the column and we'd resize it back to the minimum.
200  mGrid->AutoSizeColumn(Col_Label, false );
201  mGrid->SetColSize(Col_Label, wxMax(150, mGrid->GetColSize(Col_Label)));
202  mGrid->SetColMinimalWidth(Col_Label, mGrid->GetColSize(Col_Label));
203 
204 }
205 
206 
209 {
210 
211  //------------------------- Main section --------------------
212  ShuttleGui S(this, eIsCreating);
214  // ----------------------- End of main section --------------
215 
216  // Go populate the macros list.
217  PopulateLabels();
218 
219  // Layout the works
220  Layout();
221  //Fit();
222 
223  // Resize width based on width of columns and the vertical scrollbar
224  wxRect r = mGrid->GetGridColLabelWindow()->GetRect();
225  wxScrollBar sb(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSB_VERTICAL);
226  r.width += sb.GetSize().GetWidth() + 6;
227 
228  // Add the size of the right column of buttons too...
229  wxWindow * w = FindWindowById( ID_IMPORT, this );
230  wxASSERT( w );
231  if( w )
232  r.width += w->GetSize().GetWidth();
233 
234  SetClientSize(r.width, 300);
235 
236  // Make sure it doesn't go below this size
237  r = GetRect();
238  SetSizeHints(r.GetWidth(), r.GetHeight());
239 
240  // Bug 1465
241  // There might be a saved size, in which case use that.
242  ReadSize();
243 
244  // Center on display
245  Center();
246 }
247 
249 {
250  S.AddFixedText(_("Press F2 or double click to edit cell contents."));
251  S.StartHorizontalLay(wxEXPAND,1);
252  {
253  S.StartVerticalLay(wxEXPAND,1);
254  {
255  mGrid = safenew Grid(this, wxID_ANY);
256  S.Prop(1).AddWindow( mGrid );
257  }
258  S.EndVerticalLay();
259  S.StartVerticalLay(0);
260  {
261  //S.Id(ID_INSERTA).AddButton(_("&Insert"), wxALIGN_LEFT);
262  S.Id(ID_INSERTB).AddButton(_("&Insert"), wxALIGN_LEFT);
263  //S.Id(EditButtonID).AddButton(_("&Edit"), wxALIGN_LEFT);
264  S.Id(ID_REMOVE).AddButton(_("De&lete"), wxALIGN_LEFT);
265  S.Id(ID_IMPORT).AddButton(_("I&mport..."), wxALIGN_LEFT);
266  S.Id(ID_EXPORT).AddButton(_("&Export..."), wxALIGN_LEFT);
267  }
268  S.EndVerticalLay();
269  }
270  S.EndHorizontalLay();
271 
272  S.StartHorizontalLay(wxALIGN_RIGHT, false);
273  {
275  }
276  S.EndHorizontalLay();
277 }
278 
279 void LabelDialog::OnHelp(wxCommandEvent & WXUNUSED(event))
280 {
281  wxString page = GetHelpPageName();
282  HelpSystem::ShowHelp(this, page, true);
283 }
284 
285 
287 {
288  int cnt = mData.size();
289  int i;
290 
291  // Set the editor parameters. Do this each time since they may change
292  // due to NEW tracks and change in NumericTextCtrl format. Rate won't
293  // change but might as well leave it here.
299 
300  // Disable redrawing until we're done
301  mGrid->BeginBatch();
302 
303  // Delete all rows
304  if (mGrid->GetNumberRows()) {
305  mGrid->DeleteRows(0, mGrid->GetNumberRows());
306  }
307 
308  // Add the exact number that we'll need
309  mGrid->InsertRows(0, cnt);
310 
311  // Populate the rows
312  for (i = 0; i < cnt; i++) {
313  RowData &rd = mData[i];
314 
315  // Set the cell contents
316  mGrid->SetCellValue(i, Col_Track, TrackName(rd.index));
317  mGrid->SetCellValue(i, Col_Label, rd.title);
318  mGrid->SetCellValue(i, Col_Stime,
319  wxString::Format(wxT("%g"), rd.selectedRegion.t0()));
320  mGrid->SetCellValue(i, Col_Etime,
321  wxString::Format(wxT("%g"), rd.selectedRegion.t1()));
322  mGrid->SetCellValue(i, Col_Lfreq,
323  wxString::Format(wxT("%g"), rd.selectedRegion.f0()));
324  mGrid->SetCellValue(i, Col_Hfreq,
325  wxString::Format(wxT("%g"), rd.selectedRegion.f1()));
326  }
327 
328  // Autosize all the rows
329  mGrid->AutoSizeRows(true);
330 
331  // Resize the track name column. Use a wxChoice to determine the maximum
332  // width needed.
333  wxChoice tc(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, mTrackNames);
334  mGrid->SetColSize(Col_Track, tc.GetSize().x);
335  mGrid->SetColMinimalWidth(Col_Track, tc.GetSize().x);
336 
337  // Autosize the time columns and set their minimal widths
338  mGrid->AutoSizeColumn(Col_Stime);
339  mGrid->AutoSizeColumn(Col_Etime);
340  mGrid->AutoSizeColumn(Col_Lfreq);
341  mGrid->AutoSizeColumn(Col_Hfreq);
342 
343  // We're done, so allow the grid to redraw
344  mGrid->EndBatch();
345 
346  return true;
347 }
348 
349 bool LabelDialog::Show(bool show)
350 {
351  bool ret = wxDialogWrapper::Show(show);
352 
353  // Set initial row
354  // (This will not work until the grid is actually displayed)
355  if (show && mInitialRow != -1) {
356  mGrid->SetGridCursor(mInitialRow, Col_Label);
357  }
358 
359  return ret;
360 }
361 
363 {
364  int cnt = mData.size();
365  int i;
367  Track *t;
368  int tndx = 0;
369 
370  // Clear label tracks of labels
371  for (t = iter.First(); t; t = iter.Next()) {
372  if (t->GetKind() == Track::Label) {
373  ++tndx;
374  LabelTrack *lt = static_cast<LabelTrack*>(t);
375  if (!mSelectedTrack) {
376  for (i = lt->GetNumLabels() - 1; i >= 0 ; i--) {
377  lt->DeleteLabel(i);
378  }
379  }
380  else if (mSelectedTrack == lt && mIndex > -1) {
381  lt->DeleteLabel(mIndex);
382  }
383  else
384  // Do nothing to the nonselected tracks
385  ;
386  }
387  }
388 
389  // Create any added tracks
390  while (tndx < (int)mTrackNames.GetCount() - 1) {
391 
392  // Extract the name
393  wxString name = mTrackNames[tndx + 1].AfterFirst(wxT('-')).Mid(1);
394 
395  // Create the NEW track and add to track list
396  auto newTrack = mFactory.NewLabelTrack();
397  newTrack->SetName(name);
398  mTracks->Add(std::move(newTrack));
399  tndx++;
400  }
401 
402  // Repopulate with updated labels
403  for (i = 0; i < cnt; i++) {
404  RowData &rd = mData[i];
405 
406  // Look for track with matching index
407  tndx = 1;
408  for (t = iter.First(); t; t = iter.Next()) {
409  if (t->GetKind() == Track::Label && rd.index == tndx++) {
410  break;
411  }
412  }
413  wxASSERT(t);
414  if (!t)
415  return false;
416 
417  // Add the label to it
418  static_cast<LabelTrack *>(t)->AddLabel(rd.selectedRegion, rd.title,-2);
419  static_cast<LabelTrack *>(t)->Unselect();
420  }
421 
422  return true;
423 }
424 
426 {
427  if (mGrid->IsCellEditControlShown()) {
428  mGrid->HideCellEditControl();
429  mGrid->SaveEditControlValue();
430  }
431 
432  return true;
433 }
434 
435 wxString LabelDialog::TrackName(int & index, const wxString &dflt)
436 {
437  // Generate a NEW track name if the passed index is out of range
438  if (index < 1 || index >= (int)mTrackNames.GetCount()) {
439  index = mTrackNames.GetCount();
440  mTrackNames.Add(wxString::Format(wxT("%d - %s"), index, dflt));
441  }
442 
443  // Return the track name
444  return mTrackNames[index];
445 }
446 
448 {
450  Track *t;
451 
452 
453  // Add labels from all label tracks
454  for (t = iter.First(); t; t = iter.Next()) {
455  if (t->GetKind() == Track::Label)
456  AddLabels(static_cast<const LabelTrack *>(t));
457  }
458 
459  FindInitialRow();
460 
461  if (mData.size() == 0) {
462  wxCommandEvent e;
463  OnInsert(e);
464  }
465 }
466 
468 {
469  wxString lab;
470  int tndx = 0;
471  int i;
472 
473  // Add a NEW track name
474  TrackName(tndx, t->GetName());
475 
476  // If editor was invoked for one label, add that one only, else add all.
477  if (!mSelectedTrack || mSelectedTrack == t) {
478  for (i = 0; i < t->GetNumLabels(); i++) {
479  const LabelStruct *ls = t->GetLabel(i);
480 
481  if (mIndex < 0 || mIndex == i)
482  mData.push_back(RowData(tndx, ls->title, ls->selectedRegion));
483  }
484  }
485 }
486 
488 {
489  int cnt = mData.size();
490  mInitialRow = -1;
491 
492  if (cnt == 0)
493  return;
494 
495  // find closest previous label
496 
497  double distMin = std::numeric_limits<double>::max();
498  double dist;
499  double t0 = mViewInfo->selectedRegion.t0();
500  int i;
501  for (i = 0; i < cnt; i++)
502  {
503  dist = t0 - mData[i].selectedRegion.t0();
504  if (dist >= 0.0 && dist < distMin)
505  {
506  mInitialRow = i;
507  distMin = dist;
508  }
509  }
510 
511  // if no previous label was found, find first label
512 
513  if (mInitialRow == -1)
514  {
515  double t0Min = std::numeric_limits<double>::max();
516  for (i = 0; i < cnt; i++)
517  {
518  if (mData[i].selectedRegion.t0() < t0Min)
519  {
520  mInitialRow = i;
521  t0Min = mData[i].selectedRegion.t0();
522  }
523  }
524  }
525 }
526 
527 void LabelDialog::OnUpdate(wxCommandEvent &event)
528 {
529  // Remember the NEW format and repopulate grid
531  NumericConverter::TIME, event.GetString() );
533 
534  event.Skip(false);
535 }
536 
537 void LabelDialog::OnFreqUpdate(wxCommandEvent &event)
538 {
539  // Remember the NEW format and repopulate grid
541  NumericConverter::FREQUENCY, event.GetString() );
543 
544  event.Skip(false);
545 }
546 
547 void LabelDialog::OnInsert(wxCommandEvent &event)
548 {
549  int cnt = mData.size();
550  int row = 0;
551  int index = 0;
552 
553  // Make sure the edit control isn't active before inserting any rows
554  if (mGrid->IsCellEditControlShown()) {
555  mGrid->HideCellEditControl();
556  }
557 
558  // Attempt to guess which track the label should reside on
559  if (cnt > 0) {
560  row = mGrid->GetGridCursorRow();
561  if (row > 0 && row >= cnt) {
562  index = mTrackNames.Index(mGrid->GetCellValue(row - 1, Col_Track));
563  }
564  else {
565  index = mTrackNames.Index(mGrid->GetCellValue(row, Col_Track));
566  }
567  }
568 
569  // Insert NEW label before or after the current row
570  if (event.GetId() == ID_INSERTA && row < cnt) {
571  row++;
572  }
573  mData.insert(mData.begin() + row, RowData(index, wxT(""), SelectedRegion()));
574 
575  // Repopulate the grid
577 
578  // Reposition cursor to NEW row/col and put user into edit mode to
579  // set the label name
580  mGrid->SetGridCursor(row, Col_Label);
581  mGrid->EnableCellEditControl(true);
582  mGrid->ShowCellEditControl();
583 }
584 
585 void LabelDialog::OnRemove(wxCommandEvent & WXUNUSED(event))
586 {
587  int row = mGrid->GetGridCursorRow();
588  int col = mGrid->GetGridCursorCol();
589  int cnt = mData.size();
590 
591  // Don't try to remove if no labels exist
592  if (cnt == 0) {
593  return;
594  }
595 
596  // Make sure the edit control isn't active before removing rows
597  if (mGrid->IsCellEditControlShown()) {
598  mGrid->HideCellEditControl();
599  }
600 
601  // Remove the row
602  //RowData &rd = mData[row];
603  mData.erase(mData.begin() + row);
604 
605  // Repopulate the grid
607 
608  // Reposition the cursor
609  if (row > 0 && row >= --cnt) {
610  row--;
611  }
612  mGrid->SetGridCursor(row, col);
613 
614  // Make sure focus isn't lost
615  if (mData.size() == 0 && wxWindow::FindFocus() == mGrid->GetGridWindow()) {
616  wxWindow *ok = wxWindow::FindWindowById( wxID_OK, this);
617  if (ok) {
618  ok->SetFocus();
619  }
620  }
621 }
622 
623 void LabelDialog::OnImport(wxCommandEvent & WXUNUSED(event))
624 {
625  // Ask user for a filename
626  wxString fileName =
628  _("Select a text file containing labels"),
629  wxEmptyString, // Path
630  wxT(""), // Name
631  wxT(".txt"), // Extension
632  _("Text files (*.txt)|*.txt|All files|*"),
633  wxRESIZE_BORDER, // Flags
634  this); // Parent
635 
636  // They gave us one...
637  if (fileName != wxT("")) {
638  wxTextFile f;
639 
640  // Get at the data
641  f.Open(fileName);
642  if (!f.IsOpened()) {
644  wxString::Format( _("Could not open file: %s"), fileName ));
645  }
646  else {
647  // Create a temporary label track and load the labels
648  // into it
649  auto lt = mFactory.NewLabelTrack();
650  lt->Import(f);
651 
652  // Add the labels to our collection
653  AddLabels(lt.get());
654 
655  // Done with the temporary track
656  }
657 
658  // Repopulate the grid
660  }
661 }
662 
663 void LabelDialog::OnExport(wxCommandEvent & WXUNUSED(event))
664 {
665  int cnt = mData.size();
666 
667  // Silly user (could just disable the button, but that's a hassle ;-))
668  if (cnt == 0) {
669  AudacityMessageBox(_("No labels to export."));
670  return;
671  }
672 
673  // Extract the actual name.
674  wxString fName = mTrackNames[mTrackNames.GetCount() - 1].AfterFirst(wxT('-')).Mid(1);
675 
677  _("Export Labels As:"),
678  wxEmptyString,
679  fName,
680  wxT("txt"),
681  wxT("*.txt"),
682  wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxRESIZE_BORDER,
683  this);
684 
685  if (fName == wxT(""))
686  return;
687 
688  // Move existing files out of the way. Otherwise wxTextFile will
689  // append to (rather than replace) the current file.
690 
691  if (wxFileExists(fName)) {
692 #ifdef __WXGTK__
693  wxString safetyFileName = fName + wxT("~");
694 #else
695  wxString safetyFileName = fName + wxT(".bak");
696 #endif
697 
698  if (wxFileExists(safetyFileName))
699  wxRemoveFile(safetyFileName);
700 
701  wxRename(fName, safetyFileName);
702  }
703 
704  wxTextFile f(fName);
705 #ifdef __WXMAC__
706  wxFile{}.Create(fName);
707 #else
708  f.Create();
709 #endif
710  f.Open();
711  if (!f.IsOpened()) {
713  wxString::Format( _("Couldn't write to file: %s"), fName ) );
714  return;
715  }
716 
717  // Transfer our collection to a temporary label track
718  auto lt = mFactory.NewLabelTrack();
719  int i;
720 
721  for (i = 0; i < cnt; i++) {
722  RowData &rd = mData[i];
723 
724  lt->AddLabel(rd.selectedRegion, rd.title,-2);
725  }
726 
727  // Export them and clean
728  lt->Export(f);
729 
730 #ifdef __WXMAC__
731  f.Write(wxTextFileType_Mac);
732 #else
733  f.Write();
734 #endif
735  f.Close();
736 }
737 
738 void LabelDialog::OnSelectCell(wxGridEvent &event)
739 {
741  Track *t = iter.First();
742  while( t )
743  {
744  t->SetSelected( true );
745  t = iter.Next();
746  }
747 
748  if (!mData.empty())
749  {
750  RowData &rd = mData[event.GetRow()];
752 
754  }
755 
756  event.Skip();
757 }
758 
759 void LabelDialog::OnCellChange(wxGridEvent &event)
760 {
761  static bool guard = false;
762  int row = event.GetRow();
763 
764  // Guard against recursion which can happen when a change to the "NEW label" row
765  // is made. When InsertRow() is done in TransferDataToWindow(), checks are made
766  // within wxGrid to see if the edit control is active and since it hasn't yet
767  // been marked inactive on the first time through here, we get entered again.
768  // Sort of a double change. I think this is probably a bug in wxGrid.
769  if (guard) {
770  return;
771  }
772  guard = true;
773 
774  // The change was to an existing label, so go process it based
775  // on which column was changed.
776  RowData *rd = &mData[row];
777  switch (event.GetCol())
778  {
779  case Col_Track:
780  OnChangeTrack(event, row, rd);
781  break;
782 
783  case Col_Label:
784  OnChangeLabel(event, row, rd);
785  break;
786 
787  case Col_Stime:
788  OnChangeStime(event, row, rd);
789  break;
790 
791  case Col_Etime:
792  OnChangeEtime(event, row, rd);
793  break;
794 
795  case Col_Lfreq:
796  OnChangeLfreq(event, row, rd);
797  break;
798 
799  case Col_Hfreq:
800  OnChangeHfreq(event, row, rd);
801  break;
802  }
803 
804  // Done...no need for protection anymore
805  guard = false;
806 
807  return;
808 }
809 
810 void LabelDialog::OnChangeTrack(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
811 {
812  wxString val = mGrid->GetCellValue(row, Col_Track);
813 
814  // User selected the "New..." choice so ask for a NEW name
815  if (mTrackNames.Index(val) == 0) {
817  _("New Label Track"),
818  _("Enter track name"),
819  /* i18n-hint: (noun) it's the name of a kind of track.*/
820  _("Label Track"));
821 
822  // User canceled so repopulating the grid will set the track
823  // name to the orignal value
824  if (d.ShowModal() == wxID_CANCEL) {
826  return;
827  }
828 
829  // Force generation of a NEW track name
830  rd->index = 0;
831  TrackName(rd->index, d.GetValue());
832  }
833  else {
834  // Remember the tracks index
835  rd->index = mTrackNames.Index(val);
836  }
837 
838  // Repopulate the grid
840 
841  return;
842 }
843 
844 void LabelDialog::OnChangeLabel(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
845 {
846  // Remember the value...no need to repopulate
847  rd->title = mGrid->GetCellValue(row, Col_Label);
848 
849  return;
850 }
851 
852 void LabelDialog::OnChangeStime(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
853 {
854  // Remember the value...no need to repopulate
855  double t {};
856  mGrid->GetCellValue(row, Col_Stime).ToDouble(&t);
857  rd->selectedRegion.setT0(t, false);
858  mGrid->SetCellValue(row, Col_Etime, wxString::Format(wxT("%g"),
859  rd->selectedRegion.t1()));
860 
861  return;
862 }
863 
864 void LabelDialog::OnChangeEtime(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
865 {
866  // Remember the value...no need to repopulate
867  double t {};
868  mGrid->GetCellValue(row, Col_Etime).ToDouble(&t);
869  rd->selectedRegion.setT1(t, false);
870  mGrid->SetCellValue(row, Col_Stime, wxString::Format(wxT("%g"),
871  rd->selectedRegion.t0()));
872 
873  return;
874 }
875 
876 void LabelDialog::OnChangeLfreq(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
877 {
878  // Remember the value...no need to repopulate
879  double f;
880  mGrid->GetCellValue(row, Col_Lfreq).ToDouble(&f);
881  rd->selectedRegion.setF0(f, false);
882  mGrid->SetCellValue(row, Col_Hfreq, wxString::Format(wxT("%g"),
883  rd->selectedRegion.f1()));
884 
885  return;
886 }
887 
888 void LabelDialog::OnChangeHfreq(wxGridEvent & WXUNUSED(event), int row, RowData *rd)
889 {
890  // Remember the value...no need to repopulate
891  double f;
892  mGrid->GetCellValue(row, Col_Hfreq).ToDouble(&f);
893  rd->selectedRegion.setF1(f, false);
894  mGrid->SetCellValue(row, Col_Lfreq, wxString::Format(wxT("%g"),
895  rd->selectedRegion.f0()));
896 
897  return;
898 }
899 
901  wxSize sz = GetSize();
902  int prefWidth, prefHeight;
903  gPrefs->Read(wxT("/LabelEditor/Width"), &prefWidth, sz.x);
904  gPrefs->Read(wxT("/LabelEditor/Height"), &prefHeight, sz.y);
905 
906  wxRect screenRect(wxGetClientDisplayRect());
907  wxSize prefSize = wxSize(prefWidth, prefHeight);
908  prefSize.DecTo(screenRect.GetSize());
909  SetSize(prefSize);
910 }
911 
913  wxSize sz = GetSize();
914  gPrefs->Write(wxT("/LabelEditor/Width"), sz.x);
915  gPrefs->Write(wxT("/LabelEditor/Height"), sz.y);
916  gPrefs->Flush();
917 }
918 
919 void LabelDialog::OnOK(wxCommandEvent & WXUNUSED(event))
920 {
921  if (mGrid->IsCellEditControlShown()) {
922  mGrid->SaveEditControlValue();
923  mGrid->HideCellEditControl();
924  return;
925  }
926 
927  // Standard handling
928  if (Validate() && TransferDataFromWindow()) {
929  WriteSize();
930  EndModal(wxID_OK);
931  }
932 
933  return;
934 }
935 
936 void LabelDialog::OnCancel(wxCommandEvent & WXUNUSED(event))
937 {
938  if (mGrid->IsCellEditControlShown()) {
939  auto editor = mGrid->GetCellEditor(mGrid->GetGridCursorRow(),
940  mGrid->GetGridCursorCol());
941  editor->Reset();
942  // To avoid memory leak, don't forget DecRef()!
943  editor->DecRef();
944  mGrid->HideCellEditControl();
945  return;
946  }
947 
948  WriteSize();
949  // Standard handling
950  EndModal(wxID_CANCEL);
951 
952  return;
953 }
void OnExport(const wxString &Format)
bool Show(bool show=true) override
void RedrawProject(const bool bForceWaveTracks=false)
Definition: Project.cpp:1381
wxArrayString mTrackNames
Definition: LabelDialog.h:107
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
A list of TrackListNode items.
Definition: Track.h:623
void FindAllLabels()
EVT_COMMAND(wxID_ANY, EVT_FREQUENCYTEXTCTRL_UPDATED, LabelDialog::OnFreqUpdate) LabelDialog
Definition: LabelDialog.cpp:89
double t0() const
ViewInfo is used mainly to hold the zooming, selection and scroll information. It also has some statu...
Definition: ViewInfo.h:141
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI...
Definition: ShuttleGui.h:409
#define GRID_VALUE_TIME
Definition: Grid.h:41
SelectedRegion selectedRegion
Definition: ViewInfo.h:160
LabelTrack * mSelectedTrack
Definition: LabelDialog.h:104
void OnRemove(wxCommandEvent &event)
void OnOK(wxCommandEvent &event)
#define GRID_VALUE_CHOICE
Definition: Grid.h:126
void PopulateOrExchange(ShuttleGui &S)
NumericFormatId mFreqFormat
Definition: LabelDialog.h:109
wxWindow * AddWindow(wxWindow *pWindow, int Flags=wxALIGN_CENTRE|wxALL)
Definition: ShuttleGui.cpp:288
void OnChangeLfreq(wxGridEvent &event, int row, RowData *rd)
double mRate
Definition: LabelDialog.h:108
void OnChangeLabel(wxGridEvent &event, int row, RowData *rd)
bool Validate() override
void OnCancel(wxCommandEvent &event)
void DeleteLabel(int index)
Track * Add(std::unique_ptr< TrackKind > &&t)
Add a Track, giving it a fresh id.
Definition: Track.cpp:906
wxString title
Definition: LabelTrack.h:92
NumericEditor * mFrequencyEditor
Definition: LabelDialog.h:98
void PopulateLabels()
int AudacityMessageBox(const wxString &message, const wxString &caption=AudacityMessageBoxCaptionStr(), long style=wxOK|wxCENTRE, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord)
Definition: ErrorDialog.h:92
wxString title
Definition: LabelDialog.cpp:66
NumericFormatId mFormat
Definition: LabelDialog.h:109
void OnExport(wxCommandEvent &event)
RowDataArray mData
Definition: LabelDialog.h:100
int GetNumLabels() const
Wrap wxTextEntryDialog so that caption IS translatable.
Definition: ErrorDialog.h:108
double t1() const
void OnCellChange(wxGridEvent &event)
#define safenew
Definition: Audacity.h:230
virtual void SetSelected(bool s)
Definition: Track.cpp:99
virtual int GetKind() const
Definition: Track.h:334
Used to create a WaveTrack, or a LabelTrack.. Implementation of the functions of this class are dispe...
Definition: Track.h:867
void OnChangeEtime(wxGridEvent &event, int row, RowData *rd)
void EndHorizontalLay()
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:113
void AddLabels(const LabelTrack *t)
double f0() const
#define GRID_VALUE_FREQUENCY
Definition: Grid.h:42
bool TransferDataToWindow() override
void EndVerticalLay()
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:44
void OnFreqUpdate(wxCommandEvent &event)
void OnImport(wxCommandEvent &event)
void FindInitialRow()
int format
Definition: ExportPCM.cpp:56
void SetFormat(const NumericFormatId &format)
Definition: Grid.cpp:144
Defines a selected portion of a project.
static NumericFormatId LookupFormat(Type type, const wxString &id)
void OnImport(const CommandContext &context)
SelectedRegion selectedRegion
Definition: LabelDialog.cpp:67
void OnChangeTrack(wxGridEvent &event, int row, RowData *rd)
bool setT1(double t, bool maySwap=true)
void StartHorizontalLay(int PositionFlags=wxALIGN_CENTRE, int iProp=1)
ViewInfo * mViewInfo
Definition: LabelDialog.h:106
const LabelStruct * GetLabel(int index) const
Dialog for editing labels.
Definition: LabelDialog.h:34
ShuttleGui & Id(int id)
Fundamental data object of Audacity, placed in the TrackPanel. Classes derived form it include the Wa...
Definition: Track.h:102
void OnInsert(wxCommandEvent &event)
void SetChoices(const wxArrayString &choices)
Definition: Grid.cpp:362
static void ShowHelp(wxWindow *parent, const wxString &localFileName, const wxString &remoteURL, bool bModal=false, bool alwaysDefaultBrowser=false)
Definition: HelpSystem.cpp:194
void AddFixedText(const wxString &Str, bool bCenter=false)
Definition: ShuttleGui.cpp:397
wxString GetName() const
Definition: Track.h:275
virtual Track * First(TrackList *val=nullptr)
Definition: Track.cpp:418
RowData(int index_, const wxString &title_, const SelectedRegion &selectedRegion_)
Definition: LabelDialog.cpp:61
IdentInterfaceSymbol pairs a persistent string identifier used internally with an optional...
bool setF1(double f, bool maySwap=true)
bool TransferDataFromWindow() override
Column
Definition: LabelDialog.cpp:45
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
An iterator for a TrackList.
Definition: Track.h:406
static wxString SelectFile(Operation op, const wxString &message, const wxString &default_path, const wxString &default_filename, const wxString &default_extension, const wxString &wildcard, int flags, wxWindow *parent)
Definition: FileNames.cpp:411
_("Move Track &Down")+wxT("\t")+(GetActiveProject() -> GetCommandManager() ->GetKeyFromName(wxT("TrackMoveDown")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveTopID, _("Move Track to &Top")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveTop")).Raw()), OnMoveTrack) POPUP_MENU_ITEM(OnMoveBottomID, _("Move Track to &Bottom")+wxT("\t")+(GetActiveProject() ->GetCommandManager() ->GetKeyFromName(wxT("TrackMoveBottom")).Raw()), OnMoveTrack)#define SET_TRACK_NAME_PLUGIN_SYMBOLclass SetTrackNameCommand:public AudacityCommand
void OnChangeHfreq(wxGridEvent &event, int row, RowData *rd)
ChoiceEditor * mChoiceEditor
Definition: LabelDialog.h:96
void WriteSize()
const wxChar * name
Definition: Distortion.cpp:94
void Populate()
Creates the dialog and its contents.
virtual Track * Next(bool skiplinked=false)
Definition: Track.cpp:460
AUDACITY_DLL_API AudacityProject * GetActiveProject()
Definition: Project.cpp:309
SelectedRegion selectedRegion
Definition: LabelTrack.h:91
virtual wxString GetHelpPageName()
Definition: LabelDialog.h:63
double f1() const
ShuttleGui & Prop(int iProp)
Definition: ShuttleGui.h:418
TrackList * mTracks
Definition: LabelDialog.h:103
void AddStandardButtons(long buttons=eOkButton|eCancelButton, wxButton *extra=NULL)
bool setT0(double t, bool maySwap=true)
END_EVENT_TABLE()
Supplies an accessible grid based on wxGrid.
Definition: Grid.h:193
Grid * mGrid
Definition: LabelDialog.h:95
std::unique_ptr< LabelTrack > NewLabelTrack()
Definition: LabelTrack.cpp:99
NumericEditor * mTimeEditor
Definition: LabelDialog.h:97
virtual void OnHelp(wxCommandEvent &event)
void SetRate(double rate)
Definition: Grid.cpp:149
void OnSelectCell(wxGridEvent &event)
wxButton * AddButton(const wxString &Text, int PositionFlags=wxALIGN_CENTRE)
Definition: ShuttleGui.cpp:341
void OnChangeStime(wxGridEvent &event, int row, RowData *rd)
void ReadSize()
wxString TrackName(int &index, const wxString &dflt=_("Label Track"))
void OnUpdate(wxCommandEvent &event)
TrackFactory & mFactory
Definition: LabelDialog.h:102
bool setF0(double f, bool maySwap=true)
void StartVerticalLay(int iProp=1)