ASPiK SDK
cviewcontainer.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #ifndef __cviewcontainer__
6 #define __cviewcontainer__
7 
8 #include "vstguifwd.h"
9 #include "cview.h"
10 #include "cdrawdefs.h"
11 #if VSTGUI_TOUCH_EVENT_HANDLING
12 #include "itouchevent.h"
13 #endif
14 #include <list>
15 #include <memory>
16 
17 namespace VSTGUI {
18 
19 extern IdStringPtr kMsgLooseFocus;
20 
21 //-----------------------------------------------------------------------------
23 {
24  enum {
25  kNone = 0,
26  kDeep = 1 << 0,
27  kMouseEnabled = 1 << 1,
28  kIncludeViewContainer = 1 << 2,
29  kIncludeInvisible = 1 << 3
30  };
31 
32  explicit GetViewOptions (uint32_t options = kNone) : flags (options) {}
33 
34  GetViewOptions& deep (bool state = true) { setBit (flags, kDeep, state); return *this; }
35  GetViewOptions& mouseEnabled (bool state = true) { setBit (flags, kMouseEnabled, state); return *this; }
36  GetViewOptions& includeViewContainer (bool state = true) { setBit (flags, kIncludeViewContainer, state); return *this; }
37  GetViewOptions& includeInvisible (bool state = true) { setBit (flags, kIncludeInvisible, state); return *this; }
38 
39  bool getDeep () const { return hasBit (flags, kDeep); }
40  bool getMouseEnabled () const { return hasBit (flags, kMouseEnabled); }
41  bool getIncludeViewContainer () const { return hasBit (flags, kIncludeViewContainer); }
42  bool getIncludeInvisible () const { return hasBit (flags, kIncludeInvisible); }
43 private:
44  uint32_t flags;
45 };
46 
47 //-----------------------------------------------------------------------------
48 // CViewContainer Declaration
51 //-----------------------------------------------------------------------------
52 class CViewContainer : public CView
53 {
54 public:
55  using ViewList = std::list<SharedPointer<CView>>;
56 
57  explicit CViewContainer (const CRect& size);
58  CViewContainer (const CViewContainer& viewContainer);
59 
60  //-----------------------------------------------------------------------------
62  //-----------------------------------------------------------------------------
64  virtual bool addView (CView* pView);
65  virtual bool addView (CView* pView, const CRect& mouseableArea, bool mouseEnabled = true);
66  virtual bool addView (CView* pView, CView* pBefore);
67  virtual bool removeView (CView* pView, bool withForget = true);
68  virtual bool removeAll (bool withForget = true);
69  virtual bool isChild (CView* pView) const;
70  virtual bool isChild (CView* pView, bool deep) const;
71  virtual bool hasChildren () const;
72  virtual uint32_t getNbViews () const;
73  virtual CView* getView (uint32_t index) const;
74  virtual CView* getViewAt (const CPoint& where, const GetViewOptions& options = GetViewOptions ()) const;
75  virtual CViewContainer* getContainerAt (const CPoint& where, const GetViewOptions& options = GetViewOptions ().deep ()) const;
76  virtual bool getViewsAt (const CPoint& where, ViewList& views, const GetViewOptions& options = GetViewOptions ().deep ()) const;
77  virtual bool changeViewZOrder (CView* view, uint32_t newIndex);
78 
79  virtual bool hitTestSubViews (const CPoint& where, const CButtonState& buttons = -1);
80 
81  virtual void setAutosizingEnabled (bool state);
82  bool getAutosizingEnabled () const { return hasViewFlag (kAutosizeSubviews); }
83 
85  template<class ViewClass, class ContainerClass>
86  uint32_t getChildViewsOfType (ContainerClass& result, bool deep = false) const;
88 
89  //-----------------------------------------------------------------------------
91  //-----------------------------------------------------------------------------
93  virtual void setBackgroundColor (const CColor& color);
94  virtual CColor getBackgroundColor () const;
95  virtual void setBackgroundOffset (const CPoint& p);
96  virtual const CPoint& getBackgroundOffset () const;
97  virtual void drawBackgroundRect (CDrawContext* pContext, const CRect& _updateRect);
98 
99  virtual void setBackgroundColorDrawStyle (CDrawStyle style);
100  CDrawStyle getBackgroundColorDrawStyle () const;
102 
103  virtual bool advanceNextFocusView (CView* oldFocus, bool reverse = false);
104  virtual bool invalidateDirtyViews ();
105  virtual CRect getVisibleSize (const CRect& rect) const;
106 
107  void setTransform (const CGraphicsTransform& t);
108  const CGraphicsTransform& getTransform () const;
109 
110  void registerViewContainerListener (IViewContainerListener* listener);
111  void unregisterViewContainerListener (IViewContainerListener* listener);
112 
113  // CView
114  void draw (CDrawContext* pContext) override;
115  void drawRect (CDrawContext* pContext, const CRect& updateRect) override;
116  CMouseEventResult onMouseDown (CPoint& where, const CButtonState& buttons) override;
117  CMouseEventResult onMouseUp (CPoint& where, const CButtonState& buttons) override;
118  CMouseEventResult onMouseMoved (CPoint& where, const CButtonState& buttons) override;
119  CMouseEventResult onMouseCancel () override;
120  bool onWheel (const CPoint& where, const float& distance, const CButtonState& buttons) override;
121  bool onWheel (const CPoint& where, const CMouseWheelAxis& axis, const float& distance, const CButtonState& buttons) override;
122  bool hitTest (const CPoint& where, const CButtonState& buttons = -1) override;
123  CMessageResult notify (CBaseObject* sender, IdStringPtr message) override;
124 
125 #if VSTGUI_TOUCH_EVENT_HANDLING
126  virtual void onTouchEvent (ITouchEvent& event) override;
127  virtual bool wantsMultiTouchEvents () const override { return true; }
128  virtual void findSingleTouchEventTarget (ITouchEvent::Touch& event);
129 #endif
130 
131  bool onDrop (IDataPackage* drag, const CPoint& where) override;
132  void onDragEnter (IDataPackage* drag, const CPoint& where) override;
133  void onDragLeave (IDataPackage* drag, const CPoint& where) override;
134  void onDragMove (IDataPackage* drag, const CPoint& where) override;
135 
136  void looseFocus () override;
137  void takeFocus () override;
138 
139  bool isDirty () const override;
140 
141  void invalid () override;
142  void invalidRect (const CRect& rect) override;
143 
144  void setViewSize (const CRect& rect, bool invalid = true) override;
145  void parentSizeChanged () override;
146  bool sizeToFit () override;
147 
148  bool removed (CView* parent) override;
149  bool attached (CView* parent) override;
150 
151  CPoint& frameToLocal (CPoint& point) const override;
152  CPoint& localToFrame (CPoint& point) const override;
153 
154  //-----------------------------------------------------------------------------
155  using ChildViewConstIterator = ViewList::const_iterator;
156  using ChildViewConstReverseIterator = ViewList::const_reverse_iterator;
157 
158  //-----------------------------------------------------------------------------
159  template<bool reverse>
160  class Iterator
161  {
162  public:
163  explicit Iterator<reverse> (const CViewContainer* container) : children (container->getChildren ()) { if (reverse) riterator = children.rbegin (); else iterator = children.begin (); }
164  Iterator<reverse> (const Iterator& vi) : children (vi.children), iterator (vi.iterator), riterator (vi.riterator) {}
165 
166  Iterator<reverse>& operator++ ()
167  {
168  if (reverse)
169  ++riterator;
170  else
171  ++iterator;
172  return *this;
173  }
174 
175  Iterator<reverse> operator++ (int)
176  {
177  Iterator<reverse> old (*this);
178  if (reverse)
179  ++riterator;
180  else
181  ++iterator;
182  return old;
183  }
184 
185  Iterator<reverse>& operator-- ()
186  {
187  if (reverse)
188  --riterator;
189  else
190  --iterator;
191  return *this;
192  }
193 
194  CView* operator* () const
195  {
196  if (reverse)
197  return riterator != children.rend () ? *riterator : nullptr;
198  return iterator != children.end () ? *iterator : nullptr;
199  }
200 
201  protected:
202  const ViewList& children;
203  ChildViewConstIterator iterator;
204  ChildViewConstReverseIterator riterator;
205  };
206 
207  //-------------------------------------------
208  CLASS_METHODS(CViewContainer, CView)
209 
210  #if DEBUG
211  void dumpInfo () override;
212  virtual void dumpHierarchy ();
213  #endif
214 
215  CViewContainer* asViewContainer () final { return this; }
216  const CViewContainer* asViewContainer () const final { return this; }
217 
218 protected:
219  enum {
220  kAutosizeSubviews = 1 << (CView::kLastCViewFlag + 1)
221  };
222 
223  ~CViewContainer () noexcept override;
224  void beforeDelete () override;
225 
226  virtual bool checkUpdateRect (CView* view, const CRect& rect);
227 
228  void setMouseDownView (CView* view);
229  CView* getMouseDownView () const;
230 
231  const ViewList& getChildren () const;
232 private:
233  struct Impl;
234  std::unique_ptr<Impl> pImpl;
235 };
236 
237 using ViewIterator = CViewContainer::Iterator<false>;
238 using ReverseViewIterator = CViewContainer::Iterator<true>;
239 
240 //-----------------------------------------------------------------------------
241 template<class ViewClass, class ContainerClass>
242 inline uint32_t CViewContainer::getChildViewsOfType (ContainerClass& result, bool deep) const
243 {
244  for (auto& child : getChildren ())
245  {
246  auto vObj = child.cast<ViewClass> ();
247  if (vObj)
248  {
249  result.push_back (vObj);
250  }
251  if (deep)
252  {
253  if (auto container = child->asViewContainer ())
254  {
255  container->getChildViewsOfType<ViewClass, ContainerClass> (result);
256  }
257  }
258  }
259  return static_cast<uint32_t> (result.size ());
260 }
261 
262 } // namespace
263 
264 #endif
ViewContainer Listener Interface.
Definition: iviewlistener.h:31
virtual bool isChild(CView *pView) const
check if pView is a child view of this container
Definition: cviewcontainer.cpp:501
void invalid() override
mark whole view as invalid
Definition: cviewcontainer.cpp:618
Container Class of CView objects.
Definition: cviewcontainer.h:52
Rect structure.
Definition: crect.h:17
bool removed(CView *parent) override
view is removed from parent view
Definition: cviewcontainer.cpp:1355
virtual bool hitTestSubViews(const CPoint &where, const CButtonState &buttons=-1)
Definition: cviewcontainer.cpp:829
virtual bool addView(CView *pView)
add a child view
Definition: cviewcontainer.cpp:376
virtual void drawBackgroundRect(CDrawContext *pContext, const CRect &_updateRect)
draw the background
Definition: cviewcontainer.cpp:656
Definition: cviewcontainer.h:22
virtual bool removeView(CView *pView, bool withForget=true)
remove a child view
Definition: cviewcontainer.cpp:472
A drawing context encapsulates the drawing context of the underlying OS.
Definition: cdrawcontext.h:29
bool onWheel(const CPoint &where, const float &distance, const CButtonState &buttons) override
called if a mouse wheel event is happening over this view
Definition: cviewcontainer.cpp:972
virtual uint32_t getNbViews() const
get the number of child views
Definition: cviewcontainer.cpp:544
virtual bool getViewsAt(const CPoint &where, ViewList &views, const GetViewOptions &options=GetViewOptions().deep()) const
get all views at point where, top->down
Definition: cviewcontainer.cpp:1262
virtual bool removeAll(bool withForget=true)
remove all child views
Definition: cviewcontainer.cpp:442
Base Object with reference counter.
Definition: vstguibase.h:276
bool hitTest(const CPoint &where, const CButtonState &buttons=-1) override
Definition: cviewcontainer.cpp:858
RGBA Color structure.
Definition: ccolor.h:15
virtual CViewContainer * getContainerAt(const CPoint &where, const GetViewOptions &options=GetViewOptions().deep()) const
get the container at point where
Definition: cviewcontainer.cpp:1306
CPoint & localToFrame(CPoint &point) const override
conversion from local view coordinates to frame coordinates
Definition: cviewcontainer.cpp:1346
virtual CColor getBackgroundColor() const
get the background color
Definition: cviewcontainer.cpp:315
bool attached(CView *parent) override
view is attached to a parent view
Definition: cviewcontainer.cpp:1367
bool onDrop(IDataPackage *drag, const CPoint &where) override
called if a drag is dropped onto this view
Definition: cviewcontainer.cpp:978
Definition: customcontrols.cpp:8
virtual const CPoint & getBackgroundOffset() const
get the offset of the background bitmap
Definition: cviewcontainer.cpp:327
virtual bool changeViewZOrder(CView *view, uint32_t newIndex)
change view z order position
Definition: cviewcontainer.cpp:569
virtual void setBackgroundOffset(const CPoint &p)
set the offset of the background bitmap
Definition: cviewcontainer.cpp:321
Button and Modifier state.
Definition: cbuttonstate.h:34
void drawRect(CDrawContext *pContext, const CRect &updateRect) override
Definition: cviewcontainer.cpp:696
virtual void setAutosizingEnabled(bool state)
enable or disable autosizing subviews. Per default this is enabled.
Definition: cviewcontainer.cpp:154
void setViewSize(const CRect &rect, bool invalid=true) override
Definition: cviewcontainer.cpp:164
void parentSizeChanged() override
notification that one of the views parent has changed its size
Definition: cviewcontainer.cpp:100
Base Class of all view objects.
Definition: cview.h:44
void onDragMove(IDataPackage *drag, const CPoint &where) override
called if a drag is moved inside this view
Definition: cviewcontainer.cpp:1031
virtual bool hasChildren() const
check if container has child views
Definition: cviewcontainer.cpp:535
interface for drag&drop and clipboard data
Definition: idatapackage.h:15
void looseFocus() override
called if view should loose focus
Definition: cviewcontainer.cpp:1124
CMouseEventResult onMouseMoved(CPoint &where, const CButtonState &buttons) override
called when a mouse move event occurs
Definition: cviewcontainer.cpp:920
Graphics Transform Matrix.
Definition: cgraphicstransform.h:23
bool sizeToFit() override
resize view to optimal size
Definition: cviewcontainer.cpp:262
virtual bool checkUpdateRect(CView *view, const CRect &rect)
Definition: cviewcontainer.cpp:818
CMouseEventResult onMouseDown(CPoint &where, const CButtonState &buttons) override
called when a mouse down event occurs
Definition: cviewcontainer.cpp:866
CPoint & frameToLocal(CPoint &point) const override
conversion from frame coordinates to local view coordinates
Definition: cviewcontainer.cpp:1337
Definition: cviewcontainer.h:160
void draw(CDrawContext *pContext) override
Definition: cviewcontainer.cpp:646
void onDragLeave(IDataPackage *drag, const CPoint &where) override
called if a drag is leaving this view
Definition: cviewcontainer.cpp:1019
uint32_t getChildViewsOfType(ContainerClass &result, bool deep=false) const
Definition: cviewcontainer.h:242
void takeFocus() override
called if view should take focus
Definition: cviewcontainer.cpp:1130
bool isDirty() const override
check if view is dirty
Definition: cviewcontainer.cpp:1193
CMouseEventResult onMouseCancel() override
called when mouse tracking should be canceled
Definition: cviewcontainer.cpp:941
void invalidRect(const CRect &rect) override
mark rect as invalid
Definition: cviewcontainer.cpp:628
virtual bool advanceNextFocusView(CView *oldFocus, bool reverse=false)
Definition: cviewcontainer.cpp:1141
virtual CRect getVisibleSize(const CRect &rect) const
Definition: cviewcontainer.cpp:246
Point structure.
Definition: cpoint.h:17
virtual void setBackgroundColor(const CColor &color)
set the background color (will only be drawn if this container is not set to transparent and does not...
Definition: cviewcontainer.cpp:305
CMouseEventResult onMouseUp(CPoint &where, const CButtonState &buttons) override
called when a mouse up event occurs
Definition: cviewcontainer.cpp:903
virtual CView * getViewAt(const CPoint &where, const GetViewOptions &options=GetViewOptions()) const
get the view at point where
Definition: cviewcontainer.cpp:1220
CViewContainer(const CRect &size)
Definition: cviewcontainer.cpp:53
virtual CView * getView(uint32_t index) const
get the child view at index
Definition: cviewcontainer.cpp:554
void onDragEnter(IDataPackage *drag, const CPoint &where) override
called if a drag is entering this view
Definition: cviewcontainer.cpp:1004
CMessageResult notify(CBaseObject *sender, IdStringPtr message) override
Definition: cviewcontainer.cpp:349