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