guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gadget.hpp
1 //
2 // Base class of GUI gadgets.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Gadget_hpp
9 #define Guit_Gadget_hpp
10 #include <gobject.hpp>
11 #include <gflow.hpp>
12 #include <gproplist.hpp>
13 #include <gadgettype.hpp>
14 namespace guit {
15 
17 enum struct GHandle : uint8_t {
18  Outside, Inside,
19  Left, Right, Top, Bottom,
20  TopLeft, TopRight, BottomLeft, BottomRight,
21  Point1, Point2 // for GLineShape
22 };
23 
24 
27 enum struct GClone : uint8_t {
28  Self = 0,
29  Children = 1<<0,
30  Props = 1<<1,
31  ShareProps = 1<<2,
32  Sync = 1<<5
33 };
34 GClone operator|(GClone,GClone);
35 uint8_t operator&(GClone,GClone);
36 
37 
40 enum struct GIOMode {
41  Auto,
42  Values,
43  Content
44 };
45 
46 
49 class Gadget : public GObject {
50 protected:
51 
66  Gadget(GadgetType& css_and_title);
67 
68  Gadget(GadgetType&, GString const& css_and_title);
69 
70 public:
71  using IsGadget = bool;
72  using IsGadgetOrVarProp = bool;
73  using IsGadgetChild = bool;
74 
83  virtual ~Gadget();
84 
86  virtual Gadget& clone(GClone const&) = 0;
87 
92  using GType = GadgetType;
93  static GType& Type();
94  virtual GType& type() const {return Type();}
96 
99  GString typeName() const override {return type().name_;}
100  GId typeId() const {return type().id_;}
102 
105  Gadget* toGadget() override {return this;}
106  Gadget const* toGadget() const override {return this;}
107  virtual GItem* toItem() {return nullptr;}
108  virtual GTextItem* toTextItem() {return nullptr;}
109  virtual GValueItem* toValueItem() {return nullptr;}
110  virtual GShape* toShape() {return nullptr;}
111  virtual GChoiceBox* toChoiceBox() {return nullptr;}
112  virtual GScrollBox* toScrollBox() {return nullptr;}
113  virtual GChartBox* toChartBox() {return nullptr;}
114  virtual GWindow* toWindow() {return nullptr;}
115  virtual GMenu* toMenu() {return nullptr;}
116  virtual GMenuBar* toMenuBar() {return nullptr;}
117  virtual GTable* toTable() {return nullptr;}
118  virtual GTRow* toTRow() {return nullptr;}
119  virtual GTHead* toTHead() {return nullptr;}
121 
124  void blink(GString const& message = "", GTime duration = 350);
125 
128  virtual bool contain(GProp* prop) const;
129  virtual bool contain(GProp& prop) const {return contain(&prop);}
131 
142  virtual GProp* findProp(GPropType&) const;
143 
144  template <class T>
145  T* findProp() const {return dynamic_cast<T*>(findProp(T::Type()));}
147 
158  virtual GProp& prop(GPropType&, bool in_css = false);
159 
160  template <class T>
161  T& prop(bool in_css = false) {return dynamic_cast<T&>(prop(T::Type(), in_css));}
163 
168  GPropList const& props() const {return props_;}
169 
173  void cssProps(class GPropMap&);
174 
178  virtual GString text() const;
179 
195  virtual void add(GString const& str);
196  void add(GProp* prop);
197  void add(GProp& prop) {addProp(prop,true);}
198  void add(GPropList const& proplist);
200 
209  void add(class GStateTransition&);
210  void addFun(GFunction const&);
211  void addFun(GEventFunction const&);
212  void addFun(GCaller&);
214 
226  virtual void add(GMenu* child);
227  virtual void add(GMenu& child) {add(&child);}
228  virtual void add(GWindow*);
229  virtual void add(GWindow& child) {add(&child);}
231 
239  bool remove(GProp* prop);
240  bool remove(GProp& prop);
241  virtual bool remove(GTrigger&);
242  virtual bool remove(GPropType&);
244 
247  virtual void removeProps();
248 
251  virtual void cleanup();
252 
253 
255  bool isMultiLine() const {return modes_.multiLine;}
256 
261  bool isRichText() const {return modes_.richText;}
262  Gadget& setRichText(bool = true);
264 
271  Gadget& catchEvents(bool = true);
272  bool isCatchingEvents() const {return modes_.catchEvents;}
274 
278  Gadget& ignoreEvents(bool = true);
279  bool isIgnoringEvents() const {return modes_.ignoreEvents;}
281 
285  virtual Gadget& check(bool = true);
286  bool isChecked() const {return modes_.checked;}
288 
293  Gadget& setCheckable(bool = true);
294  bool isCheckable() const {return modes_.checkable;}
296 
301  virtual Gadget& enable(bool = true);
302  bool isSelfEnabled() const {return modes_.enabled;}
303  bool isTrulyEnabled() const {return settings_.trulyEnabled;}
305 
310  virtual Gadget& show(bool = true);
311  bool isShown() const {return modes_.shown;}
313 
317  virtual void update();
318 
320  enum struct Update {IfNeeded, Needed, Now, Css};
321 
330  virtual void updateLayout(Update = Update::IfNeeded);
331 
338  GBox* getParent() const {return parent_ ? parent_->toBox() : nullptr;}
339  virtual Gadget* getPseudoParent() const {return parent_;}
341 
343  virtual GWindow* getWindow() const;
344 
346  virtual bool isTopLevel() const {return false;}
347 
352  virtual bool hasValue() const {return false;}
353 
357  virtual GProp* valueProp() {return nullptr;}
358 
362  virtual GString stringValue() const {return emptystring_;}
363 
367  virtual void setValue(GString const& value) {}
368 
371  virtual void clearValues();
372 
375  bool read(GString const& filename, GIOMode = GIOMode::Auto);
376  bool read(std::istream&, GIOMode = GIOMode::Auto);
377  //friend std::istream& operator>>(std::istream&, Gadget&);
379 
382  bool write(GString const& filename, GIOMode) const;
383  bool write(std::ostream&, GIOMode) const;
384  //friend std::ostream& operator<<(std::ostream&, Gadget const&);
386 
390  bool isColumn() const {return modes_.dir == GFlow::Column;}
391  bool isRow() const {return modes_.dir == GFlow::Row;}
392  bool isPie() const {return modes_.dir == GFlow::Pie;}
394 
419  virtual void move(GPoint const&);
420  virtual void move(float x, float y);
422 
434  virtual void resize(float w, float h);
435  virtual void resize(GDim const&);
437 
446  virtual void reshapeInteractively();
447  virtual void reshapeInteractively(GMouseEvent&);
449 
453  virtual GPoint pos() const;
454  virtual float x() const;
455  virtual float y() const;
457 
460  int zlayer() const {return layout_.layer;}
461 
465  GPoint wpos() const {return GPoint(frame_.x,frame_.y);}
466  float wx() const {return frame_.x;}
467  float wy() const {return frame_.y;}
469 
472  GPoint screenPos() const;
473  float screenX() const;
474  float screenY() const;
476 
479  float w() const {return frame_.w;}
480  float h() const {return frame_.h;}
481  GDim dim() const {return GDim(frame_.w, frame_.h);}
483 
485  GRect const& wframe() {return frame_;}
486 
491  float angleStart() const;
492  float angleExtent() const;
493  void setAngleStart(float);
494  void setAngleExtent(float);
496 
502  enum struct Place : int8_t {
503  Content = 0,
504  Top = -10, Bottom = -20, Left = -30, Right = -40, ToolBar = -50, ScrollBar = -60
505  };
506  virtual Place place() const {return Place::Content;}
508 
509  bool unclippedLayers() const {return modes_.unclippedLayers;}
510  Gadget& setUnclippedLayers(bool = true);
511 
513  virtual void keepInside(GBox& box, GPoint& point);
514 
516  virtual GHandle findHandle(GPoint const& wpos);
517 
519  bool isActive() const {return modes_.active;}
520 
523  bool isActivable() const {return modes_.activable;}
524  Gadget& setActivable(bool = true);
526 
529  bool isDownActivable() const {return modes_.downActivable;}
530  Gadget& setDownActivable(bool = true);
532 
535  bool isFocus() const {return modes_.focus;}
536  bool isFocusable() const {return modes_.focusable;}
538 
542  bool isInternal() const {return modes_.internal;}
543  void setInternal() {modes_.internal = 1;}
545 
547  GadgetModes const& modes() const {return modes_;}
548 
550  GEventType const& eventMask() const {return eventmask_;}
551 
553  virtual void doFire(GEvent&);
554 
562  virtual void doPaint(GRenderGraphics&, GRender* parent_render);
563 
567  virtual Gadget* doPick(GPoint const& wpos, class GPickMode&);
568 
570  virtual void onAdd(GBox* parent) {}
571 
573  virtual void onRemove(GBox* parent) {}
574 
578  class GadgetStyle* style() const {return style_;}
579  virtual void init(GRender* parrender);
580  virtual void initClone(Gadget& from, GClone const&);
581  virtual void addProp(GProp&, bool add_to_list);
582  virtual void addProp2(GProp&, bool add_to_list);
583  virtual bool removeProp(GProp&, bool rem_from_proplist);
584  virtual bool removeProp2(GProp&, bool rem_from_proplist);
585  void removeNotifiers(void*) override;
586  virtual void addOpener(Gadget& opened, GTrigger&, int btn, bool autoplace);
587 
588  virtual void readValues(std::istream&, class GIOBuffer&);
589  virtual void writeValues(std::ostream&, class GIOBuffer&) const;
590 
591  virtual void readAll(std::istream&, class GIOBuffer&);
592  virtual void writeAll(std::ostream&, class GIOBuffer&) const;
593 
594  virtual void readAux(std::istream&, class GIOBuffer&) {}
595  virtual void writeAux(std::ostream&, class GIOBuffer&) const {}
596 
597  int8_t rowFlex(Gadget const* parent);
598  int8_t columnFlex(Gadget const* parent);
599  void setRowFlex(Gadget const* parent, int8_t val);
600  void setColumnFlex(Gadget const* parent, int8_t val);
601 
602  enum SelectFrom {FromButton, FromChoice, FromSelect};
603  // internal fct for (un)selecting gadget.
604  virtual void selectImpl(bool state, SelectFrom from);
605 
606  // finds the ancestor that must be used for updating this gadget.
607  virtual Gadget* getUpdateParent();
608 
609  // called by update() to compute preferred size.
610  virtual void computeSize(GRender* parent_render, bool use_size_prop);
611 
612  // called by update() to lay out gadget and children.
613  virtual void pack(GRender* parent_render) {}
614 
615  // called by update() to lay out gadgets that have a GPos
616  virtual void packPos(float parentx, float parenty);
617 
618  // called by move() or GPos prop to move gadget.
619  virtual void moveImpl(float wx, float wy, bool callcb);
620 
621  // calls on::move and/or or::resize callbacks.
622  virtual void movedResizedCB(GRect const& initialframe);
623 
624  void setShape(class GBorderValue*);
625 
626  // called when using handles to resize the gadget interactively.
627  // note: also changes the position when dragging left resize handles
628  virtual GHandle handleDown(GPoint const& wpos, bool inter);
629  virtual void handleDrag(float dx, float dy, GHandle const&);
630  virtual void handleUp(GPoint const& wpos, GHandle const&);
631 
632  // paints handle frame when resizing the gadget interactively.
633  virtual void paintHandleFrame(GRenderGraphics&);
634  void paintHandle(GRenderGraphics& g, float x, float y, float handle);
635  bool pickHandle(GPoint const& pt, float x, float y);
636 
637  // keep the gadget inside its parent when resizing it.
638  virtual void keepTopLeftInParent(GBox& par, GPoint&, float& dx, float& dy);
639  virtual void keepBottomRightInParent(GBox& par, GPoint& p, float& w, float& h);
640 
641  static const GString emptystring_;
642  // order optimized for memory occupation
643  GadgetSettings settings_{};
644  GadgetModes modes_{};
645  GadgetLayout layout_{};
646  GEventType eventmask_{};
647  GRect frame_;
648  Gadget* parent_{};
649  GPropList props_;
650  gptr<class GadgetStyle> style_;
651  GadgetShape* shape_{};
653 };
654 
655 
657 using GadgetList = std::vector<gptr<Gadget>>;
658 
659 }
660 #endif
661 
662 
663 #include <gadd.hpp>