Audacity 3.2.0
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
22class wxCommandEvent;
23class wxEraseEvent;
24class wxSizeEvent;
25class wxPaintEvent;
26class wxPoint;
27class wxRect;
28class wxWindow;
29
30class GrabberEvent;
31
35
36//
37// ToolDock IDs
38//
39enum
40{
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
54public:
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
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 {
111 };
112
113 // This iterator visits the nodes of the forest in pre-order, and at each
114 // stop, reports its Place
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
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:
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
266private:
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
289class ToolDock final : public wxPanelWrapper
290{
291public:
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
static const AttachedProjectObjects::RegisteredFactory manager
@ ToolBarCount
Definition: ToolBar.h:88
@ BotDockID
Definition: ToolDock.h:43
@ DockCount
Definition: ToolDock.h:44
@ TopDockID
Definition: ToolDock.h:42
@ NoDockID
Definition: ToolDock.h:41
Grabber Class.
Definition: Grabber.h:48
Iterator(ToolBarConfiguration &conf)
Definition: ToolDock.h:195
std::vector< int > GetPath() const
Definition: ToolDock.h:168
const Place & operator*() const
Definition: ToolDock.h:119
Forest::iterator FIter
Definition: ToolDock.h:207
std::vector< Triple > mIters
Definition: ToolDock.h:225
const Place * operator->() const
Definition: ToolDock.h:120
static bool Read(ToolBarConfiguration *pConfiguration, Legacy *pLegacy, ToolBar *bar, bool &visible, bool defaultVisible)
Definition: ToolDock.cpp:255
static const Position UnspecifiedPosition
Definition: ToolDock.h:105
void Show(ToolBar *bar)
Definition: ToolDock.cpp:224
Iterator begin()
Definition: ToolDock.h:228
bool Contains(const ToolBar *bar) const
Definition: ToolDock.h:233
Iterator end() const
Definition: ToolDock.h:229
static void Write(const ToolBarConfiguration *pConfiguration, const ToolBar *bar)
Definition: ToolDock.cpp:331
bool Shows(const ToolBar *bar) const
Definition: ToolDock.h:246
Iterator FindPlace(const ToolBar *bar) const
Definition: ToolDock.cpp:54
void Swap(ToolBarConfiguration &that)
Definition: ToolDock.h:56
void Insert(ToolBar *bar, Position position=UnspecifiedPosition)
Definition: ToolDock.cpp:98
void InsertAtPath(ToolBar *bar, const std::vector< int > &path)
Definition: ToolDock.cpp:177
void RemoveNulls(Forest &forest)
Definition: ToolDock.cpp:298
std::vector< Tree > Forest
Definition: ToolDock.h:52
void PostRead(Legacy &legacy)
Definition: ToolDock.cpp:310
Position Find(const ToolBar *bar) const
Definition: ToolDock.cpp:89
std::pair< Forest *, Forest::iterator > FindPeers(const ToolBar *bar)
Definition: ToolDock.cpp:64
void Hide(ToolBar *bar)
Definition: ToolDock.cpp:231
void Remove(const ToolBar *bar)
Definition: ToolDock.cpp:212
bool IsRightmost(const ToolBar *bar) const
Definition: ToolDock.cpp:237
Works with ToolManager and ToolDock to provide a dockable window in which buttons can be placed.
Definition: ToolBar.h:99
A dynamic panel where a ToolBar can be docked.
Definition: ToolDock.h:290
ToolBarConfiguration mWrappedConfiguration
Definition: ToolDock.h:341
int GetOrder(ToolBar *bar)
void Updated()
Definition: ToolDock.cpp:853
void VisitLayout(LayoutVisitor &visitor, ToolBarConfiguration *pWrappedConfiguration=nullptr)
Definition: ToolDock.cpp:482
void Undock(ToolBar *bar)
Definition: ToolDock.cpp:403
void OnErase(wxEraseEvent &event)
Definition: ToolDock.cpp:883
void OnPaint(wxPaintEvent &event)
Definition: ToolDock.cpp:891
bool AcceptsFocus() const override
Definition: ToolDock.h:296
void RestoreConfiguration(ToolBarConfiguration &backup)
Definition: ToolDock.cpp:824
void OnGrabber(GrabberEvent &event)
Definition: ToolDock.cpp:863
ToolBar * mBars[ToolBarCount]
Definition: ToolDock.h:343
void OnSize(wxSizeEvent &event)
Definition: ToolDock.cpp:875
void LoadConfig()
Definition: ToolDock.cpp:443
void WrapConfiguration(ToolBarConfiguration &backup)
Definition: ToolDock.cpp:817
ToolBarConfiguration mConfiguration
Definition: ToolDock.h:338
ToolDock(wxEvtHandler *manager, wxWindow *parent, int dockid)
Methods for ToolDock.
Definition: ToolDock.cpp:378
wxEvtHandler * mManager
Definition: ToolDock.h:335
void Expose(int type, bool show)
Definition: ToolDock.cpp:834
ToolBarConfiguration & GetConfiguration()
Definition: ToolDock.h:309
ToolBarConfiguration::Position PositionBar(ToolBar *t, const wxPoint &pos, wxRect &rect)
Definition: ToolDock.cpp:720
void LayoutToolBars()
Definition: ToolDock.cpp:663
void OnMouseEvents(wxMouseEvent &event)
Definition: ToolDock.cpp:944
void Dock(ToolBar *bar, bool deflate, ToolBarConfiguration::Position ndx=ToolBarConfiguration::UnspecifiedPosition)
Definition: ToolDock.cpp:415
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:753
Triple(FIter b, FIter c, FIter e)
Definition: ToolDock.h:210
std::vector< ToolBar * > bars
Definition: ToolDock.h:254
Position(ToolBar *r, ToolBar *b=nullptr, bool shouldAdopt=true)
Definition: ToolDock.h:79
void swap(Tree &that)
Definition: ToolDock.h:276
A convenience for defining iterators that return rvalue types, so that they cooperate correctly with ...
Definition: MemoryX.h:238