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