Audacity  3.0.3
ToolsToolBar.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  ToolsToolBar.cpp
6 
7  Dominic Mazzoni
8  Shane T. Mueller
9  Leland Lucius
10 
11  See ToolsToolBar.h for details
12 
13 *******************************************************************//*******************************************************************/
31 
32 
33 
34 #include "ToolsToolBar.h"
35 #include "ToolManager.h"
36 
37 // For compilers that support precompilation, includes "wx/wx.h".
38 #include <wx/wxprec.h>
39 
40 #include <wx/setup.h> // for wxUSE_* macros
41 
42 #ifndef WX_PRECOMP
43 #include <wx/defs.h>
44 #include <wx/event.h>
45 #include <wx/intl.h>
46 #include <wx/sizer.h>
47 #endif
48 #include <wx/tooltip.h>
49 
50 #include "Prefs.h"
51 #include "AllThemeResources.h"
52 #include "ImageManipulation.h"
53 #include "Project.h"
54 #include "../ProjectSettings.h"
55 #include "../ProjectWindow.h"
56 #include "../tracks/ui/Scrubbing.h"
57 
58 #include "../widgets/AButton.h"
59 
60 
62 
66 
67 BEGIN_EVENT_TABLE(ToolsToolBar, ToolBar)
71  ToolsToolBar::OnTool)
73 
74 //Standard constructor
76 : ToolBar(project, ToolsBarID, XO("Tools"), wxT("Tools"))
77 {
78  using namespace ToolCodes;
79 
80  //Read the following wxASSERTs as documentating a design decision
81  wxASSERT( selectTool == selectTool - firstTool );
82  wxASSERT( envelopeTool == envelopeTool - firstTool );
83  wxASSERT( zoomTool == zoomTool - firstTool );
84  wxASSERT( drawTool == drawTool - firstTool );
85  wxASSERT( multiTool == multiTool - firstTool );
86  bool multiToolActive = false;
87  gPrefs->Read(wxT("/GUI/ToolBars/Tools/MultiToolActive"), &multiToolActive);
88 
89  if (multiToolActive)
90  mCurrentTool = multiTool;
91  else
92  mCurrentTool = selectTool;
93 
94  project.Bind(EVT_PROJECT_SETTINGS_CHANGE,
96 }
97 
99 {
100  static_assert(
102  "mismatch in number of tools" );
103 }
104 
106 {
107  auto &toolManager = ToolManager::Get( project );
108  return *static_cast<ToolsToolBar*>( toolManager.GetToolBar(ToolsBarID) );
109 }
110 
112 {
113  return Get( const_cast<AudacityProject&>( project )) ;
114 }
115 
117 {
118 
119 // JKC:
120 // Under Win98 Tooltips appear to be buggy, when you have a lot of
121 // tooltip messages flying around. I found that just creating a
122 // twelfth tooltip caused Audacity to crash when it tried to show
123 // any tooltip.
124 //
125 // Win98 does NOT recover from this crash - for any application which is
126 // using tooltips will also crash thereafter... so you must reboot.
127 // Rather weird.
128 //
129 // Getting windows to process more of its stacked up messages seems
130 // to workaround the problem. The problem is not fully understood though
131 // (as of April 2003).
132 
133  // Vaughan, October 2003: Now we're crashing on Win2K if
134  // "Quit when closing last window" is unchecked, when we come back
135  // through here, on either of the wxSafeYield calls.
136  // James confirms that commenting them out does not cause his original problem
137  // to reappear, so they're commented out now.
138  // wxSafeYield(); //Deal with some queued up messages...
139 
140  #if wxUSE_TOOLTIPS
141 
142  using namespace ToolCodes;
143 
144  static const struct Entry {
145  int tool;
146  CommandID commandName;
147  TranslatableString untranslatedLabel;
148  } table[] = {
149  { selectTool, wxT("SelectTool"), XO("Selection Tool") },
150  { envelopeTool, wxT("EnvelopeTool"), XO("Envelope Tool") },
151  { zoomTool, wxT("ZoomTool"), XO("Zoom Tool") },
152  { drawTool, wxT("DrawTool"), XO("Draw Tool") },
153  { multiTool, wxT("MultiTool"), XO("Multi-Tool") },
154  };
155 
156  for (const auto &entry : table) {
157  ComponentInterfaceSymbol command{
158  entry.commandName, entry.untranslatedLabel };
160  *mTool[entry.tool], &command, 1u );
161  }
162 
163  #endif
164 
165  // wxSafeYield();
166  return;
167 }
168 
170 {
173 }
174 
176  ToolsToolBar *pBar, teBmps eTool,
177  int id, const TranslatableString &label)
178 {
179  AButton *button = ToolBar::MakeButton(pBar,
180  bmpRecoloredUpSmall,
181  bmpRecoloredDownSmall,
182  bmpRecoloredUpHiliteSmall,
183  bmpRecoloredDownSmall, // Not bmpRecoloredHiliteSmall as down is inactive.
184  eTool, eTool, eTool,
185  wxWindowID(id + FirstToolID),
186  wxDefaultPosition, true,
187  theTheme.ImageSize( bmpRecoloredUpSmall ));
188  button->SetLabel( label );
189  pBar->mToolSizer->Add( button );
190  return button;
191 }
192 
193 
195 {
196  SetBackgroundColour( theTheme.Colour( clrMedium ) );
198 
199  Add(mToolSizer = safenew wxGridSizer(2, 3, 1, 1));
200 
201  /* Tools */
202  using namespace ToolCodes;
203  mTool[ selectTool ] = MakeTool( this, bmpIBeam, selectTool, XO("Selection Tool") );
204  mTool[ envelopeTool ] = MakeTool( this, bmpEnvelope, envelopeTool, XO("Envelope Tool") );
205  mTool[ drawTool ] = MakeTool( this, bmpDraw, drawTool, XO("Draw Tool") );
206  mTool[ zoomTool ] = MakeTool( this, bmpZoom, zoomTool, XO("Zoom Tool") );
207  mTool[ multiTool ] = MakeTool( this, bmpMulti, multiTool, XO("Multi-Tool") );
208 
209  DoToolChanged();
210 
212 }
213 
214 void ToolsToolBar::OnTool(wxCommandEvent & evt)
215 {
216  // This will cause callback to OnToolChanged
217  auto iTool = evt.GetId() - ToolCodes::firstTool - FirstToolID;
218  auto pButton = mTool[iTool];
219  if (pButton->IsDown())
221  else
222  // Don't stay up
223  pButton->PushDown();
224 }
225 
226 void ToolsToolBar::OnToolChanged(wxCommandEvent &evt)
227 {
228  evt.Skip(); // Let other listeners hear the event too
229  if (evt.GetInt() != ProjectSettings::ChangedTool)
230  return;
231  DoToolChanged();
233 }
234 
236 {
237  auto &projectSettings = ProjectSettings::Get( mProject );
238  using namespace ToolCodes;
239  mCurrentTool = projectSettings.GetTool() - firstTool;
240  for (int i = 0; i < numTools; i++)
241  if (i == mCurrentTool)
242  mTool[i]->PushDown();
243  else
244  mTool[i]->PopUp();
245 
246  gPrefs->Write(wxT("/GUI/ToolBars/Tools/MultiToolActive"),
247  mTool[multiTool]->IsDown());
248  gPrefs->Flush();
249 }
250 
251 void ToolsToolBar::Create(wxWindow * parent)
252 {
253  ToolBar::Create(parent);
254  UpdatePrefs();
255 }
256 
258  []( AudacityProject &project ){
259  return ToolBar::Holder{ safenew ToolsToolBar{ project } }; }
260 };
261 
262 namespace {
264  /* i18n-hint: Clicking this menu item shows a toolbar
265  that has some tools in it */
266  ToolsBarID, wxT("ShowToolsTB"), XXO("T&ools Toolbar"),
267 };
268 }
269 
270 // Following code injects menu items for changing the current tool
271 
272 #include "../TrackPanel.h"
273 
274 // private helper classes and functions
275 namespace {
276 
278 void SetTool(AudacityProject &project, int tool)
279 {
280  auto toolbar = &ToolsToolBar::Get( project );
281  if (toolbar) {
282  ProjectSettings::Get(project).SetTool(tool);
283  TrackPanel::Get( project ).Refresh(false);
284  }
285 }
286 
287 }
288 
290 namespace ToolActions {
291 
292 // exported helper functions
293 // none
294 
295 // Menu handler functions
296 
299 void OnSelectTool(const CommandContext &context)
300 {
302 }
303 
305 void OnEnvelopeTool(const CommandContext &context)
306 {
308 }
309 
310 void OnDrawTool(const CommandContext &context)
311 {
313 }
314 
316 void OnZoomTool(const CommandContext &context)
317 {
319 }
320 
321 void OnMultiTool(const CommandContext &context)
322 {
324 }
325 
326 void OnPrevTool(const CommandContext &context)
327 {
328  auto &project = context.project;
329  auto &trackPanel = TrackPanel::Get( project );
330  auto &settings = ProjectSettings::Get( project );
331 
332  settings.SetTool(
333  (settings.GetTool() + (ToolCodes::numTools - 1 )) % ToolCodes::numTools);
334  trackPanel.Refresh(false);
335 }
336 
337 void OnNextTool(const CommandContext &context)
338 {
339  auto &project = context.project;
340  auto &trackPanel = TrackPanel::Get( project );
341  auto &settings = ProjectSettings::Get( project );
342 
343  settings.SetTool( (settings.GetTool() + 1) % ToolCodes::numTools );
344  trackPanel.Refresh(false);
345 }
346 
347 }; // struct Handler
348 
350  // Handler is not stateful. Doesn't need a factory registered with
351  // AudacityProject.
352  static ToolActions::Handler instance;
353  return instance;
354 };
355 
356 #define FN(X) (& ToolActions::Handler :: X)
357 
358 using namespace MenuTable;
360 {
361  static BaseItemSharedPtr menu{
363  Menu( wxT("Tools"), XXO("T&ools"),
364  Command( wxT("SelectTool"), XXO("&Selection Tool"), FN(OnSelectTool),
365  AlwaysEnabledFlag, wxT("F1") ),
366  Command( wxT("EnvelopeTool"), XXO("&Envelope Tool"),
367  FN(OnEnvelopeTool), AlwaysEnabledFlag, wxT("F2") ),
368  Command( wxT("DrawTool"), XXO("&Draw Tool"), FN(OnDrawTool),
369  AlwaysEnabledFlag, wxT("F3") ),
370  Command( wxT("ZoomTool"), XXO("&Zoom Tool"), FN(OnZoomTool),
371  AlwaysEnabledFlag, wxT("F4") ),
372  Command( wxT("MultiTool"), XXO("&Multi Tool"), FN(OnMultiTool),
373  AlwaysEnabledFlag, wxT("F6") ),
374  Command( wxT("PrevTool"), XXO("&Previous Tool"), FN(OnPrevTool),
375  AlwaysEnabledFlag, wxT("A") ),
376  Command( wxT("NextTool"), XXO("&Next Tool"), FN(OnNextTool),
377  AlwaysEnabledFlag, wxT("D") )
378  ) ) };
379  return menu;
380 }
381 
382 #undef FN
383 
385  wxT("Optional/Extra/Part1"),
387 };
388 }
ToolActions::Handler::OnEnvelopeTool
void OnEnvelopeTool(const CommandContext &context)
Handler to set the Envelope tool active.
Definition: ToolsToolBar.cpp:305
TranslatableString
Holds a msgid for the translation catalog; may also bind format arguments.
Definition: TranslatableString.h:32
AButton::SetLabel
void SetLabel(const TranslatableString &label)
Definition: AButton.cpp:274
teBmps
int teBmps
Definition: ImageManipulation.h:48
ToolManager.h
ToolCodes::drawTool
@ drawTool
Definition: ProjectSettings.h:42
ToolCodes::selectTool
@ selectTool
Definition: ProjectSettings.h:40
ToolsToolBar::~ToolsToolBar
virtual ~ToolsToolBar()
Definition: ToolsToolBar.cpp:98
MenuTable::FinderScope
Definition: CommandManager.h:485
anonymous_namespace{ToolsToolBar.cpp}::SetTool
void SetTool(AudacityProject &project, int tool)
Called by handlers that set tools.
Definition: ToolsToolBar.cpp:278
ToolCodes
Definition: ProjectSettings.h:36
gPrefs
FileConfig * gPrefs
Definition: Prefs.cpp:70
AllThemeResources.h
ToolsToolBar::numTools
@ numTools
Definition: ToolsToolBar.h:62
Project.h
AButton
A wxButton with mouse-over behaviour.
Definition: AButton.h:25
ToolBar::mProject
AudacityProject & mProject
Definition: ToolBar.h:235
ToolCodes::zoomTool
@ zoomTool
Definition: ProjectSettings.h:43
AButton::PopUp
void PopUp()
Definition: AButton.cpp:605
ToolManager::Get
static ToolManager & Get(AudacityProject &project)
Definition: ToolManager.cpp:356
entry
static ProjectFileIORegistry::WriterEntry entry
Definition: ProjectSettings.cpp:197
Registry::Shared
std::unique_ptr< SharedItem > Shared(const BaseItemSharedPtr &ptr)
Definition: Registry.h:93
TrackPanel::Get
static TrackPanel & Get(AudacityProject &project)
Definition: TrackPanel.cpp:227
XO
#define XO(s)
Definition: Internat.h:31
ToolActions::Handler::OnSelectTool
void OnSelectTool(const CommandContext &context)
Handler to set the select tool active.
Definition: ToolsToolBar.cpp:299
ProjectSettings::Get
static ProjectSettings & Get(AudacityProject &project)
Definition: ProjectSettings.cpp:44
ToolCodes::firstTool
@ firstTool
Definition: ProjectSettings.h:51
ToolActions::findCommandHandler
static CommandHandlerObject & findCommandHandler(AudacityProject &)
Definition: ToolsToolBar.cpp:349
ThemeBase::ImageSize
wxSize ImageSize(int iIndex)
Definition: Theme.cpp:1224
ToolActions::Handler::OnPrevTool
void OnPrevTool(const CommandContext &context)
Definition: ToolsToolBar.cpp:326
ToolBar::Holder
wxWindowPtr< ToolBar > Holder
Definition: ToolBar.h:102
ProjectWindow::Get
static ProjectWindow & Get(AudacityProject &project)
Definition: ProjectWindow.cpp:535
AlwaysEnabledFlag
constexpr CommandFlag AlwaysEnabledFlag
Definition: CommandFlag.h:35
MenuTable::AttachedItem
Definition: CommandManager.h:708
ToolsToolBar::MakeTool
static AButton * MakeTool(ToolsToolBar *pBar, teBmps eTool, int id, const TranslatableString &label)
Definition: ToolsToolBar.cpp:175
AttachedToolBarMenuItem
Definition: ToolManager.h:224
ToolActions::Handler::OnZoomTool
void OnZoomTool(const CommandContext &context)
Handler to set the Zoom tool active.
Definition: ToolsToolBar.cpp:316
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
ToolsToolBar::UpdatePrefs
void UpdatePrefs() override
Definition: ToolsToolBar.cpp:169
Entry
Definition: EditToolBar.cpp:225
ToolsToolBar::Create
void Create(wxWindow *parent) override
Definition: ToolsToolBar.cpp:251
ProjectSettings::SetTool
void SetTool(int tool)
Definition: ProjectSettings.cpp:172
ToolsToolBar::mToolSizer
wxGridSizer * mToolSizer
Definition: ToolsToolBar.h:64
ToolActions
Namespace for functions for View Toolbar menu.
Definition: ToolsToolBar.cpp:290
ToolsToolBar::mCurrentTool
int mCurrentTool
Definition: ToolsToolBar.h:65
ToolsBarID
@ ToolsBarID
Definition: ToolBar.h:73
wxEVT_COMMAND_BUTTON_CLICKED
wxEVT_COMMAND_BUTTON_CLICKED
Definition: AdornedRulerPanel.cpp:494
ToolCodes::numTools
@ numTools
Definition: ProjectSettings.h:50
XXO
#define XXO(s)
Definition: Internat.h:44
ToolsToolBar::OnToolChanged
void OnToolChanged(wxCommandEvent &evt)
Definition: ToolsToolBar.cpp:226
ToolCodes::multiTool
@ multiTool
Definition: ProjectSettings.h:44
CommandContext
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
Definition: CommandContext.h:34
label
TranslatableString label
Definition: Tags.cpp:756
ToolsToolBar::OnTool
void OnTool(wxCommandEvent &evt)
Definition: ToolsToolBar.cpp:214
ToolsToolBar::Get
static ToolsToolBar & Get(AudacityProject &project)
Definition: ToolsToolBar.cpp:105
anonymous_namespace{ToolsToolBar.cpp}::sAttachment
AttachedToolBarMenuItem sAttachment
Definition: ToolsToolBar.cpp:263
ToolBar::SetButtonToolTip
static void SetButtonToolTip(AudacityProject &project, AButton &button, const ComponentInterfaceSymbol commands[], size_t nCommands)
Definition: ToolBar.cpp:898
Registry::BaseItemSharedPtr
std::shared_ptr< BaseItem > BaseItemSharedPtr
Definition: Registry.h:72
ToolBar::UpdatePrefs
void UpdatePrefs() override
Definition: ToolBar.cpp:605
AButton::PushDown
void PushDown()
Definition: AButton.cpp:597
factory
static RegisteredToolbarFactory factory
Definition: ToolsToolBar.cpp:257
ToolsToolBar::mTool
AButton * mTool[numTools]
Definition: ToolsToolBar.h:63
ToolBar::Create
virtual void Create(wxWindow *parent)
Definition: ToolBar.cpp:475
theTheme
THEME_API Theme theTheme
Definition: Theme.cpp:79
ToolsToolBar::Populate
void Populate() override
Definition: ToolsToolBar.cpp:194
ToolsToolBar
A kind of ToolBar with Tools on it.
Definition: ToolsToolBar.h:35
EVT_COMMAND_RANGE
EVT_COMMAND_RANGE(ToolCodes::firstTool+FirstToolID, ToolsToolBar::numTools - 1+FirstToolID, wxEVT_COMMAND_BUTTON_CLICKED, ToolsToolBar::OnTool) ToolsToolBar
Methods for ToolsToolBar.
Definition: ToolsToolBar.cpp:68
RegisteredToolbarFactory
Definition: ToolBar.h:261
ToolActions::Handler::OnDrawTool
void OnDrawTool(const CommandContext &context)
Definition: ToolsToolBar.cpp:310
ProjectSettings::ChangedTool
@ ChangedTool
Definition: ProjectSettings.h:69
ToolsToolBar::DoToolChanged
void DoToolChanged()
Definition: ToolsToolBar.cpp:235
FileConfig::Flush
virtual bool Flush(bool bCurrentOnly=false) wxOVERRIDE
Definition: FileConfig.cpp:143
ToolCodes::envelopeTool
@ envelopeTool
Definition: ProjectSettings.h:41
TaggedIdentifier< CommandIdTag, false >
MenuTable::Command
std::unique_ptr< CommandItem > Command(const CommandID &name, const TranslatableString &label_in, void(Handler::*pmf)(const CommandContext &), CommandFlag flags, const CommandManager::Options &options={}, CommandHandlerFinder finder=FinderScope::DefaultFinder())
Definition: CommandManager.h:675
ToolsToolBar::RegenerateTooltips
void RegenerateTooltips() override
Definition: ToolsToolBar.cpp:116
ToolActions::Handler::OnMultiTool
void OnMultiTool(const CommandContext &context)
Definition: ToolsToolBar.cpp:321
ToolActions::Handler::OnNextTool
void OnNextTool(const CommandContext &context)
Definition: ToolsToolBar.cpp:337
TrackPanel::Refresh
void Refresh(bool eraseBackground=true, const wxRect *rect=(const wxRect *) NULL) override
Definition: TrackPanel.cpp:793
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
ToolBar::MakeButtonBackgroundsSmall
static void MakeButtonBackgroundsSmall()
Definition: ToolBar.cpp:802
ToolActions::Handler
Definition: ToolsToolBar.cpp:297
CommandHandlerObject
wxEvtHandler CommandHandlerObject
Definition: CommandFunctors.h:28
MenuTable::Menu
std::unique_ptr< MenuItem > Menu(const Identifier &internalName, const TranslatableString &title, Args &&... args)
Definition: CommandManager.h:623
ToolBar
Works with ToolManager and ToolDock to provide a dockable window in which buttons can be placed.
Definition: ToolBar.h:98
ToolActions::sAttachment2
AttachedItem sAttachment2
Definition: ToolsToolBar.cpp:384
ThemeBase::Colour
wxColour & Colour(int iIndex)
Definition: Theme.cpp:1189
ToolBar::MakeButton
static AButton * MakeButton(wxWindow *parent, teBmps eUp, teBmps eDown, teBmps eHilite, teBmps eDownHi, teBmps eStandardUp, teBmps eStandardDown, teBmps eDisabled, wxWindowID id, wxPoint placement, bool processdownevents, wxSize size)
Definition: ToolBar.cpp:840
FirstToolID
const int FirstToolID
Definition: ToolsToolBar.h:33
Prefs.h
FN
#define FN(X)
Definition: ToolsToolBar.cpp:356
ProjectWindow::RedrawProject
void RedrawProject(const bool bForceWaveTracks=false)
Definition: ProjectWindow.cpp:710
MenuTable
Definition: CommandManager.h:416
ToolActions::ExtraToolsMenu
BaseItemSharedPtr ExtraToolsMenu()
Definition: ToolsToolBar.cpp:359
safenew
#define safenew
Definition: MemoryX.h:10
settings
static Settings & settings()
Definition: TrackInfo.cpp:86
CommandContext::project
AudacityProject & project
Definition: CommandContext.h:64
ToolsToolBar.h
END_EVENT_TABLE
END_EVENT_TABLE()
ToolBar::Add
void Add(wxWindow *window, int proportion=0, int flag=wxALIGN_TOP, int border=0, wxObject *userData=NULL)
Definition: ToolBar.cpp:686
ImageManipulation.h
IMPLEMENT_CLASS
IMPLEMENT_CLASS(ToolsToolBar, ToolBar)