Audacity  3.0.3
ToolDock.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  ToolDock.h
6 
7  Dominic Mazzoni
8  Shane T. Mueller
9  Leland Lucius
10 
11 **********************************************************************/
12 
13 #ifndef __AUDACITY_TOOLDOCK__
14 #define __AUDACITY_TOOLDOCK__
15 
16 #include <vector>
17 #include <wx/defs.h>
18 
19 #include "ToolBar.h"
20 #include "MemoryX.h"
21 
22 class wxCommandEvent;
23 class wxEraseEvent;
24 class wxSizeEvent;
25 class wxPaintEvent;
26 class wxPoint;
27 class wxRect;
28 class wxWindow;
29 
30 class GrabberEvent;
31 
35 
36 //
37 // ToolDock IDs
38 //
39 enum
40 {
41  NoDockID = 0,
44  DockCount = 2
45 };
46 
47 // A description of a layout of toolbars, as a forest of trees that root
48 // at the left edge of the tool dock and grow rightward
50 {
51  struct Tree;
52  using Forest = std::vector<Tree>;
53 
54 public:
55 
57  {
58  mForest.swap(that.mForest);
59  }
60 
61  void Clear()
62  {
63  mForest.clear();
64  }
65 
66  // Describe one toolbar's position in terms of its parent and preceding
67  // sibling
68  // When specifying a place at which to insert, "adopt" means insertion of
69  // an internal node displacing other nodes deeper as its children
70  struct Position {
73  bool adopt {true};
74  bool valid {true};
75 
76  // Default constructor
77  Position() {}
78 
79  explicit Position(
80  ToolBar *r,
81  ToolBar *b = nullptr,
82  bool shouldAdopt = true
83  )
84  : rightOf{ r }, below{ b }, adopt{ shouldAdopt }
85  {}
86 
87  // Constructor for the invalid value
88  explicit Position(bool /* dummy */) : valid{ false } {}
89 
90  friend inline bool operator ==
91  (const Position &lhs, const Position &rhs)
92  { return lhs.valid == rhs.valid &&
93  (!lhs.valid ||
94  (lhs.rightOf == rhs.rightOf
95  && lhs.below == rhs.below
96  && lhs.adopt == rhs.adopt
97  ));
98  }
99 
100  friend inline bool operator !=
101  (const Position &lhs, const Position &rhs)
102  { return !(lhs == rhs); }
103  };
104 
106 
107  // Point to a node in the forest and describe its position
108  struct Place {
109  Tree *pTree {};
111  };
112 
113  // This iterator visits the nodes of the forest in pre-order, and at each
114  // stop, reports its Place
115  class Iterator
116  : public ValueIterator<Place>
117  {
118  public:
119  const Place &operator * () const { return mPlace; }
120  const Place *operator -> () const { return &**this; }
122  {
123  // This is a feature: advance position even at the end
124  mPlace.position =
125  Position{ mPlace.pTree ? mPlace.pTree->pBar : nullptr };
126 
127  if (!mIters.empty())
128  {
129  auto triple = &mIters.back();
130  auto &children = triple->current->children;
131  if (children.empty()) {
132  while (++triple->current == triple->end) {
133  mIters.pop_back();
134  if (mIters.empty())
135  break;
136  triple = &mIters.back();
137  }
138  }
139  else {
140  auto b = children.begin();
141  mIters.push_back( Triple { b, b, children.end() } );
142  }
143  }
144 
145  if (mIters.empty()) {
146  mPlace.pTree = nullptr;
147  // Leave mPlace.position as above
148  }
149  else {
150  const auto &triple = mIters.back();
151  mPlace.pTree = &*triple.current;
152 
153  if (mIters.size() == 1)
154  mPlace.position.rightOf = nullptr;
155  else
156  mPlace.position.rightOf = (mIters.rbegin() + 1)->current->pBar;
157 
158  if (triple.begin == triple.current)
159  mPlace.position.below = nullptr;
160  else
161  mPlace.position.below = (triple.current - 1)->pBar;
162  }
163 
164  return *this;
165  }
166 
167  // This may be called on the end iterator, and then returns empty
168  std::vector<int> GetPath() const
169  {
170  std::vector<int> path;
171  path.reserve(mIters.size());
172  for (const auto &triple : mIters)
173  path.push_back(triple.current - triple.begin);
174  return path;
175  }
176 
177  friend inline bool operator ==
178  (const Iterator &lhs, const Iterator &rhs)
179  {
180  const auto &li = lhs.mIters;
181  const auto &ri = rhs.mIters;
182  return li.size() == ri.size() &&
183  std::equal(li.begin(), li.end(), ri.begin());
184  }
185 
186  friend inline bool operator !=
187  (const Iterator &lhs, const Iterator &rhs)
188  {
189  return !(lhs == rhs);
190  }
191 
192  private:
194  Iterator () {}
196  {
197  auto &forest = conf.mForest;
198  if (!forest.empty()) {
199  auto b = forest.begin();
200  mIters.push_back( Triple { b, b, forest.end() } );
201  mPlace.pTree = &*b;
202  }
203  }
204 
206 
207  using FIter = Forest::iterator;
208  struct Triple
209  {
211  : begin{b}, current{c}, end{e} {}
213 
214  friend inline bool operator ==
215  (const Triple &lhs, const Triple &rhs)
216  {
217  // Really need only to compare current
218  return
219  // lhs.begin == rhs.begin &&
220  lhs.current == rhs.current
221  // && lhs.end == rhs.end
222  ;
223  }
224  };
225  std::vector<Triple> mIters;
226  };
227 
228  Iterator begin() { return Iterator { *this }; }
229  Iterator end() const { return Iterator {}; }
230 
231  Position Find(const ToolBar *bar) const;
232 
233  bool Contains(const ToolBar *bar) const
234  {
235  return Find(bar) != UnspecifiedPosition;
236  }
237 
238  // Default position inserts at the end
239  void Insert(ToolBar *bar,
240  Position position = UnspecifiedPosition);
241  void InsertAtPath(ToolBar *bar, const std::vector<int> &path);
242  void Remove(const ToolBar *bar);
243 
244  // Future: might allow a state that the configuration remembers
245  // a hidden bar, but for now, it's equivalent to Contains():
246  bool Shows(const ToolBar *bar) const { return Contains(bar); }
247 
248  void Show(ToolBar *bar);
249  void Hide(ToolBar *bar);
250 
251  bool IsRightmost(const ToolBar *bar) const;
252 
253  struct Legacy {
254  std::vector<ToolBar*> bars;
255  };
256 
257  static bool Read
258  (ToolBarConfiguration *pConfiguration,
259  Legacy *pLegacy,
260  ToolBar *bar, bool &visible, bool defaultVisible);
261  void PostRead(Legacy &legacy);
262 
263  static void Write
264  (const ToolBarConfiguration *pConfiguration, const ToolBar *bar);
265 
266 private:
267 
268  void Remove(Forest &forest, Forest::iterator iter);
269  void RemoveNulls(Forest &forest);
270 
271  struct Tree
272  {
275 
276  void swap(Tree &that)
277  {
278  std::swap(pBar, that.pBar);
279  children.swap(that.children);
280  }
281  };
282 
283  Iterator FindPlace(const ToolBar *bar) const;
284  std::pair<Forest*, Forest::iterator> FindPeers(const ToolBar *bar);
285 
287 };
288 
289 class ToolDock final : public wxPanelWrapper
290 {
291 public:
292 
293  ToolDock( wxEvtHandler *manager, wxWindow *parent, int dockid );
294  ~ToolDock();
295 
296  bool AcceptsFocus() const override { return false; };
297 
298  void LoadConfig();
299  void LayoutToolBars();
300  void Expose( int type, bool show );
301  int GetOrder( ToolBar *bar );
302  void Dock( ToolBar *bar, bool deflate,
305  void Undock( ToolBar *bar );
307  PositionBar( ToolBar *t, const wxPoint & pos, wxRect & rect );
308 
310  { return mConfiguration; }
311 
312  // backup gets old contents of the configuration; the configuration is
313  // set to the wrapped configuration.
315 
316  // Reverse what was done by WrapConfiguration.
318  void Updated();
319 
320  protected:
321 
322  void OnErase( wxEraseEvent & event );
323  void OnSize( wxSizeEvent & event );
324  void OnPaint( wxPaintEvent & event );
325  void OnGrabber( GrabberEvent & event );
326  void OnMouseEvents(wxMouseEvent &event);
327 
328  private:
329  class LayoutVisitor;
330  void VisitLayout(LayoutVisitor &visitor,
331  ToolBarConfiguration *pWrappedConfiguration = nullptr);
332 
333 
334 
335  wxEvtHandler *mManager;
336 
337  // Stores adjacency relations that we want to realize in the dock layout
339 
340  // Configuration as modified by the constraint of the main window width
342 
344 
345  public:
346 
347  DECLARE_CLASS( ToolDock )
348  DECLARE_EVENT_TABLE()
349 };
350 
351 #endif
ToolBarConfiguration::Iterator::operator->
const Place * operator->() const
Definition: ToolDock.h:120
ToolDock::RestoreConfiguration
void RestoreConfiguration(ToolBarConfiguration &backup)
Definition: ToolDock.cpp:824
ToolBarConfiguration
Definition: ToolDock.h:50
ToolBarConfiguration::Place
Definition: ToolDock.h:108
ToolBar.h
ToolDock
A dynamic panel where a ToolBar can be docked.
Definition: ToolDock.h:290
BotDockID
@ BotDockID
Definition: ToolDock.h:43
ToolDock::OnMouseEvents
void OnMouseEvents(wxMouseEvent &event)
Definition: ToolDock.cpp:944
ToolBarCount
@ ToolBarCount
Definition: ToolBar.h:87
ToolBarConfiguration::Position::rightOf
ToolBar * rightOf
Definition: ToolDock.h:71
ToolBarConfiguration::Position
Definition: ToolDock.h:70
ToolBarConfiguration::Iterator::FIter
Forest::iterator FIter
Definition: ToolDock.h:207
ToolBarConfiguration::Place::pTree
Tree * pTree
Definition: ToolDock.h:109
ToolDock::mBars
ToolBar * mBars[ToolBarCount]
Definition: ToolDock.h:343
ToolBarConfiguration::Shows
bool Shows(const ToolBar *bar) const
Definition: ToolDock.h:246
wxPanelWrapper
Definition: wxPanelWrapper.h:41
ToolDock::mWrappedConfiguration
ToolBarConfiguration mWrappedConfiguration
Definition: ToolDock.h:341
ToolBarConfiguration::RemoveNulls
void RemoveNulls(Forest &forest)
Definition: ToolDock.cpp:298
ToolDock::Undock
void Undock(ToolBar *bar)
Definition: ToolDock.cpp:403
ToolBarConfiguration::Tree::children
Forest children
Definition: ToolDock.h:274
ToolBarConfiguration::Contains
bool Contains(const ToolBar *bar) const
Definition: ToolDock.h:233
NoDockID
@ NoDockID
Definition: ToolDock.h:41
ToolBarConfiguration::mForest
Forest mForest
Definition: ToolDock.h:286
ToolDock::Updated
void Updated()
Definition: ToolDock.cpp:853
ToolBarConfiguration::Iterator::Triple::end
FIter end
Definition: ToolDock.h:212
ToolBarConfiguration::Position::Position
Position(bool)
Definition: ToolDock.h:88
ToolDock::GetConfiguration
ToolBarConfiguration & GetConfiguration()
Definition: ToolDock.h:309
ToolDock::GetOrder
int GetOrder(ToolBar *bar)
ToolDock::LayoutToolBars
void LayoutToolBars()
Definition: ToolDock.cpp:663
ToolBarConfiguration::Legacy::bars
std::vector< ToolBar * > bars
Definition: ToolDock.h:254
ToolBarConfiguration::begin
Iterator begin()
Definition: ToolDock.h:228
ToolBarConfiguration::Position::Position
Position()
Definition: ToolDock.h:77
ToolBarConfiguration::Iterator::Iterator
Iterator()
Definition: ToolDock.h:194
ToolBarConfiguration::Position::Position
Position(ToolBar *r, ToolBar *b=nullptr, bool shouldAdopt=true)
Definition: ToolDock.h:79
ToolDock::Expose
void Expose(int type, bool show)
Definition: ToolDock.cpp:834
ToolBarConfiguration::Forest
std::vector< Tree > Forest
Definition: ToolDock.h:52
ToolBarConfiguration::IsRightmost
bool IsRightmost(const ToolBar *bar) const
Definition: ToolDock.cpp:237
ToolBarConfiguration::Iterator::ToolBarConfiguration
friend ToolBarConfiguration
Definition: ToolDock.h:193
ToolDock::VisitLayout
void VisitLayout(LayoutVisitor &visitor, ToolBarConfiguration *pWrappedConfiguration=nullptr)
Definition: ToolDock.cpp:482
ToolBarConfiguration::Clear
void Clear()
Definition: ToolDock.h:61
ToolBarConfiguration::Iterator::operator++
Iterator & operator++()
Definition: ToolDock.h:121
ToolDock::AcceptsFocus
bool AcceptsFocus() const override
Definition: ToolDock.h:296
ToolBarConfiguration::Iterator::Triple::Triple
Triple(FIter b, FIter c, FIter e)
Definition: ToolDock.h:210
ToolDock::OnSize
void OnSize(wxSizeEvent &event)
Definition: ToolDock.cpp:875
ToolBarConfiguration::Position::adopt
bool adopt
Definition: ToolDock.h:73
ToolBarConfiguration::Iterator::Iterator
Iterator(ToolBarConfiguration &conf)
Definition: ToolDock.h:195
ToolBarConfiguration::UnspecifiedPosition
static const Position UnspecifiedPosition
Definition: ToolDock.h:105
ToolBarConfiguration::Read
static bool Read(ToolBarConfiguration *pConfiguration, Legacy *pLegacy, ToolBar *bar, bool &visible, bool defaultVisible)
Definition: ToolDock.cpp:255
ToolDock::PositionBar
ToolBarConfiguration::Position PositionBar(ToolBar *t, const wxPoint &pos, wxRect &rect)
Definition: ToolDock.cpp:720
ToolDock::~ToolDock
~ToolDock()
Definition: ToolDock.cpp:396
GrabberEvent
Grabber Class.
Definition: Grabber.h:48
ToolBarConfiguration::Tree
Definition: ToolDock.h:272
anonymous_namespace{NoteTrack.cpp}::swap
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:753
ToolBarConfiguration::Position::below
ToolBar * below
Definition: ToolDock.h:72
ToolDock::OnPaint
void OnPaint(wxPaintEvent &event)
Definition: ToolDock.cpp:891
ToolBarConfiguration::Iterator
Definition: ToolDock.h:117
ToolBarConfiguration::Place::position
Position position
Definition: ToolDock.h:110
ToolDock::OnErase
void OnErase(wxEraseEvent &event)
Definition: ToolDock.cpp:883
ToolDock::LoadConfig
void LoadConfig()
Definition: ToolDock.cpp:443
TopDockID
@ TopDockID
Definition: ToolDock.h:42
ToolBarConfiguration::Iterator::mPlace
Place mPlace
Definition: ToolDock.h:205
ToolBarConfiguration::Iterator::operator*
const Place & operator*() const
Definition: ToolDock.h:119
ToolBarConfiguration::Iterator::Triple::current
FIter current
Definition: ToolDock.h:212
MemoryX.h
ToolBarConfiguration::Insert
void Insert(ToolBar *bar, Position position=UnspecifiedPosition)
Definition: ToolDock.cpp:98
ToolBarConfiguration::Hide
void Hide(ToolBar *bar)
Definition: ToolDock.cpp:231
ToolBarConfiguration::Write
static void Write(const ToolBarConfiguration *pConfiguration, const ToolBar *bar)
Definition: ToolDock.cpp:331
ToolDock::ToolDock
ToolDock(wxEvtHandler *manager, wxWindow *parent, int dockid)
Methods for ToolDock.
Definition: ToolDock.cpp:378
ValueIterator
A convenience for defining iterators that return rvalue types, so that they cooperate correctly with ...
Definition: MemoryX.h:366
ToolBarConfiguration::Iterator::Triple::begin
FIter begin
Definition: ToolDock.h:212
ToolDock::OnGrabber
void OnGrabber(GrabberEvent &event)
Definition: ToolDock.cpp:863
ToolBarConfiguration::PostRead
void PostRead(Legacy &legacy)
Definition: ToolDock.cpp:310
ToolBar
Works with ToolManager and ToolDock to provide a dockable window in which buttons can be placed.
Definition: ToolBar.h:98
ToolDock::WrapConfiguration
void WrapConfiguration(ToolBarConfiguration &backup)
Definition: ToolDock.cpp:817
ToolDock::mManager
wxEvtHandler * mManager
Definition: ToolDock.h:335
DockCount
@ DockCount
Definition: ToolDock.h:44
ToolBarConfiguration::Legacy
Definition: ToolDock.h:253
ToolBarConfiguration::Position::valid
bool valid
Definition: ToolDock.h:74
ToolBarConfiguration::end
Iterator end() const
Definition: ToolDock.h:229
ToolBarConfiguration::Find
Position Find(const ToolBar *bar) const
Definition: ToolDock.cpp:89
ToolBarConfiguration::Iterator::GetPath
std::vector< int > GetPath() const
Definition: ToolDock.h:168
ToolDock::mConfiguration
ToolBarConfiguration mConfiguration
Definition: ToolDock.h:338
ToolBarConfiguration::FindPeers
std::pair< Forest *, Forest::iterator > FindPeers(const ToolBar *bar)
Definition: ToolDock.cpp:64
ToolBarConfiguration::Iterator::Triple
Definition: ToolDock.h:209
ToolBarConfiguration::Iterator::mIters
std::vector< Triple > mIters
Definition: ToolDock.h:225
ToolDock::Dock
void Dock(ToolBar *bar, bool deflate, ToolBarConfiguration::Position ndx=ToolBarConfiguration::UnspecifiedPosition)
Definition: ToolDock.cpp:415
ToolBarConfiguration::Tree::swap
void swap(Tree &that)
Definition: ToolDock.h:276
ToolBarConfiguration::Show
void Show(ToolBar *bar)
Definition: ToolDock.cpp:224
ToolBarConfiguration::Swap
void Swap(ToolBarConfiguration &that)
Definition: ToolDock.h:56
ToolBarConfiguration::Remove
void Remove(const ToolBar *bar)
Definition: ToolDock.cpp:212
ToolBarConfiguration::InsertAtPath
void InsertAtPath(ToolBar *bar, const std::vector< int > &path)
Definition: ToolDock.cpp:177
ToolBarConfiguration::Tree::pBar
ToolBar * pBar
Definition: ToolDock.h:273
ToolBarConfiguration::FindPlace
Iterator FindPlace(const ToolBar *bar) const
Definition: ToolDock.cpp:54