Audacity  3.0.3
CommonTrackPanelCell.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 CommonTrackPanelCell.cpp
6 
7 Paul Licameli split from TrackPanel.cpp
8 
9 **********************************************************************/
10 
11 #include "CommonTrackPanelCell.h"
12 
13 #include <wx/cursor.h>
14 #include <wx/event.h>
15 #include <wx/menu.h>
16 
17 #include "../../widgets/BasicMenu.h"
18 #include "BasicUI.h"
19 #include "../../commands/CommandContext.h"
20 #include "../../commands/CommandManager.h"
21 #include "../../HitTestResult.h"
22 #include "../../RefreshCode.h"
23 #include "../../TrackPanelMouseEvent.h"
24 #include "ViewInfo.h"
25 #include "../../widgets/wxWidgetsWindowPlacement.h"
26 
27 namespace {
29  {
30  static CommonTrackPanelCell::Hook theHook;
31  return theHook;
32  }
33 }
34 
36  -> Hook
37 {
38  auto &theHook = GetHook();
39  auto result = theHook;
40  theHook = hook;
41  return result;
42 }
43 
45 {
46 }
47 
49 (const TrackPanelMouseState &, const AudacityProject *)
50 {
51  static wxCursor defaultCursor{ wxCURSOR_ARROW };
52  return { {}, &defaultCursor, {} };
53 }
54 
56  const wxRect&, const wxPoint *, AudacityProject * )
57  -> std::vector<MenuItem>
58 {
59  return {};
60 }
61 
62 unsigned CommonTrackPanelCell::DoContextMenu( const wxRect &rect,
63  wxWindow *pParent, const wxPoint *pPoint, AudacityProject *pProject)
64 {
65  const auto items = GetMenuItems( rect, pPoint, pProject );
66  if (items.empty())
68 
69  // Set up command context with extras
70  CommandContext context{ *pProject };
71  SelectedRegion region;
72  if (pPoint) {
73  auto time = ViewInfo::Get(*pProject).PositionToTime(pPoint->x, rect.x);
74  region = { time, time };
75  context.temporarySelection.pSelectedRegion = &region;
76  }
77  context.temporarySelection.pTrack = FindTrack().get();
78 
79  auto &commandManager = CommandManager::Get(*pProject);
80  auto flags = MenuManager::Get( *pProject ).GetUpdateFlags();
81 
82  // Common dispatcher for the menu items
83  auto dispatcher = [&]( wxCommandEvent &evt ){
84  auto idx = evt.GetId() - 1;
85  if (idx >= 0 && idx < items.size()) {
86  if (auto &action = items[idx].action)
87  action( context );
88  else
89  commandManager.HandleTextualCommand(
90  items[idx].symbol.Internal(), context, flags, false);
91  }
92  };
93 
94  wxMenu menu;
95  int ii = 1;
96  for (const auto &item: items) {
97  if ( const auto &commandID = item.symbol.Internal();
98  commandID.empty() )
99  menu.AppendSeparator();
100  else {
101  // Generate a menu item with the same shortcut key as in the toolbar
102  // menu, and as determined by keyboard preferences
103  auto label =
104  commandManager.FormatLabelForMenu( commandID, &item.symbol.Msgid() );
105  menu.Append( ii, label );
106  menu.Bind( wxEVT_COMMAND_MENU_SELECTED, dispatcher );
107  bool enabled = item.enabled &&
108  (item.action || commandManager.GetEnabled( commandID ));
109  menu.Enable( ii, enabled );
110  }
111  ++ii;
112  }
113 
114  BasicUI::Point point;
115  if (pPoint)
116  point = { pPoint->x, pPoint->y };
117  BasicMenu::Handle{ &menu }.Popup(
118  wxWidgetsWindowPlacement{ pParent },
119  point
120  );
121 
123 }
124 
126 (const TrackPanelMouseEvent &evt, AudacityProject *pProject)
127 {
128  auto hook = GetHook();
129  return hook ? hook( evt, pProject ) : RefreshCode::Cancelled;
130 }
131 
132 CommonTrackCell::CommonTrackCell( const std::shared_ptr< Track > &parent )
133  : mwTrack { parent }
134 {}
135 
137 
139 {
140 }
141 
142 void CommonTrackCell::Reparent( const std::shared_ptr<Track> &parent )
143 {
144  mwTrack = parent;
145 }
146 
147 std::shared_ptr<Track> CommonTrackCell::DoFindTrack()
148 {
149  return mwTrack.lock();
150 }
151 
153 {
154 }
155 
156 bool CommonTrackCell::HandleXMLAttribute( const wxChar *, const wxChar * )
157 {
158  return false;
159 }
XMLWriter
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:23
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:241
CommonTrackCell::CopyTo
virtual void CopyTo(Track &track) const
Definition: CommonTrackPanelCell.cpp:138
BasicUI::Point
A pair of screen coordinates, x increasing rightward, y downward.
Definition: BasicUIPoint.h:18
CommonTrackPanelCell::Hook
std::function< unsigned(const TrackPanelMouseEvent &evt, AudacityProject *pProject) > Hook
Definition: CommonTrackPanelCell.h:33
RefreshCode::RefreshNone
@ RefreshNone
Definition: RefreshCode.h:21
CommonTrackPanelCell::~CommonTrackPanelCell
virtual ~CommonTrackPanelCell()=0
Definition: CommonTrackPanelCell.cpp:44
CommonTrackPanelCell::FindTrack
std::shared_ptr< Track > FindTrack()
Definition: CommonTrackPanelCell.h:46
RefreshCode::Cancelled
@ Cancelled
Definition: RefreshCode.h:23
CommonTrackPanelCell::GetMenuItems
virtual std::vector< MenuItem > GetMenuItems(const wxRect &rect, const wxPoint *pPosition, AudacityProject *pProject)
Return a list of items for DoContextMenu() (empties for separators)
Definition: CommonTrackPanelCell.cpp:55
CommonTrackPanelCell::DoContextMenu
unsigned DoContextMenu(const wxRect &rect, wxWindow *pParent, const wxPoint *pPosition, AudacityProject *pProject) override
Definition: CommonTrackPanelCell.cpp:62
wxWidgetsWindowPlacement
Window placement information for wxWidgetsBasicUI can be constructed from a wxWindow pointer.
Definition: wxWidgetsWindowPlacement.h:22
CommonTrackPanelCell::DefaultPreview
HitTestPreview DefaultPreview(const TrackPanelMouseState &, const AudacityProject *) override
Definition: CommonTrackPanelCell.cpp:49
CommonTrackPanelCell::InstallMouseWheelHook
static Hook InstallMouseWheelHook(const Hook &hook)
Definition: CommonTrackPanelCell.cpp:35
CommonTrackCell::mwTrack
std::weak_ptr< Track > mwTrack
Definition: CommonTrackPanelCell.h:120
CommonTrackCell::DoFindTrack
std::shared_ptr< Track > DoFindTrack() override
Definition: CommonTrackPanelCell.cpp:147
CommonTrackCell::WriteXMLAttributes
virtual void WriteXMLAttributes(XMLWriter &) const
Definition: CommonTrackPanelCell.cpp:152
BasicUI::Point::x
int x
Definition: BasicUIPoint.h:22
CommonTrackPanelCell.h
CommandContext
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
Definition: CommandContext.h:34
CommonTrackCell::CommonTrackCell
CommonTrackCell(const std::shared_ptr< Track > &pTrack)
Definition: CommonTrackPanelCell.cpp:132
label
TranslatableString label
Definition: Tags.cpp:756
CommonTrackPanelCell::HandleWheelRotation
unsigned HandleWheelRotation(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
Definition: CommonTrackPanelCell.cpp:126
CommonTrackCell::~CommonTrackCell
~CommonTrackCell()
HitTestPreview
Definition: HitTestResult.h:20
ViewInfo.h
BasicUI.h
Toolkit-neutral facade for basic user interface services.
Track
Abstract base class for an object holding data associated with points on a time axis.
Definition: Track.h:239
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
TrackPanelMouseEvent
Definition: TrackPanelMouseEvent.h:46
MenuManager::Get
static MenuManager & Get(AudacityProject &project)
Definition: Menus.cpp:70
TrackPanelMouseState
Definition: TrackPanelMouseEvent.h:28
CommonTrackCell::Reparent
virtual void Reparent(const std::shared_ptr< Track > &parent)
Definition: CommonTrackPanelCell.cpp:142
anonymous_namespace{CommonTrackPanelCell.cpp}::GetHook
CommonTrackPanelCell::Hook & GetHook()
Definition: CommonTrackPanelCell.cpp:28
CommonTrackCell::HandleXMLAttribute
virtual bool HandleXMLAttribute(const wxChar *attr, const wxChar *value)
Definition: CommonTrackPanelCell.cpp:156
CommandManager::Get
static CommandManager & Get(AudacityProject &project)
Definition: CommandManager.cpp:207
BasicMenu::Handle::Popup
void Popup(const BasicUI::WindowPlacement &window, const Point &pos={})
Display the menu at pos, invoke at most one action, then hide it.
Definition: BasicMenu.cpp:207
MenuManager::GetUpdateFlags
CommandFlag GetUpdateFlags(bool checkActive=false) const
Definition: Menus.cpp:545
ZoomInfo::PositionToTime
double PositionToTime(wxInt64 position, wxInt64 origin=0, bool ignoreFisheye=false) const
Definition: ZoomInfo.cpp:39
BasicMenu::Handle
Definition: BasicMenu.h:26
SelectedRegion
Defines a selected portion of a project.
Definition: SelectedRegion.h:35