guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gsize.hpp
1 //
2 // Size of a Gadget.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef GuitSize_hpp
9 #define GuitSize_hpp
10 #include <gvarprop.hpp>
11 namespace guit {
12 
13 class GSizeValue : public GDim {
14 public:
15  GSizeValue();
16  GSizeValue(float w, float h, char wunit, char hunit);
17  GSizeValue(GDim const&, char wunit, char hunit);
18 
19  static GSizeValue blend(GSizeValue const& a, GSizeValue const& b, float coeff);
20 
21  bool operator==(GSizeValue const&) const;
22  bool operator!=(GSizeValue const&) const;
23 
24  char wunit{}, hunit{};
25 };
26 
27 inline bool gconvert(GSizeValue const& from, GSizeValue& to) {to = from; return true;}
28 bool gconvert(GString const& from, GSizeValue& to);
29 bool gconvert(GSizeValue const& from, GString& to);
30 
31 
47 class GSize : public GVarProp_<GSizeValue> {
48 public:
50  enum Unit : int8_t {Undef, Pixels, Chars, Auto, Percent};
51 
52  GSize* toSize() override {return this;}
53  GSize const* toSize() const override {return this;}
54 
55  GSize();
56  GSize(float width, float height, Unit wunit = Pixels, Unit hunit = Pixels);
57  GSize(GDim const&, Unit wunit = Pixels, Unit hunit = Pixels);
58  GSize(GSize const&);
59  GSize(GString const& format);
60  GSize* clone(bool copy_value) const override;
61 
62  GSize& operator=(GSize const& from) {set(from); return *this;}
63  GSize& operator=(GString const& format) {set(format); return *this;}
64 
65  using Super::set;
66  void set(float width, float height, Unit wunit = Pixels, Unit hunit = Pixels);
67  void set(GProp const&) override;
68 
69  virtual void blend(GSize const& a, GSize const& b, float alpha);
70  void blend(GProp const& a, GProp const& b, float alpha) override;
71 
72  float w() const {return value_.w;}
73  float h() const {return value_.h;}
74 
76  using Field = GNumPropField<GSize,float>;
77 
81  Field wField() {return Field(*this, value_.w);}
82  Field hField() {return Field(*this, value_.h);}
84 
85  using GType = GPropType_<GSize,GProp>;
86  static GType& Type();
87  GType& type() const override {return Type();}
88 
89 protected:
90  friend class GRender;
91  bool onAdd(Gadget*) override;
92  void onRemove(Gadget*) override;
93  void apply(Gadget*, GRender&, Specif) final;
94  void updateGadget(Gadget&) override;
95 };
96 
99 GSize& Size();
100 GSize& Size(GString const& value);
102 
103 
104 
106 class GDragSize : public GSize {
107 public:
108  GDragSize(float w, float h) : GSize(w, h, Pixels, Pixels) {}
109  GDragSize(GDim const& dim) : GSize(dim, Pixels, Pixels) {}
110  GDragSize(GDragSize const&);
111  GDragSize(GString const& = "");
112  GDragSize* clone(bool copy_value) const override;
113 
114  /*
115  void fixW(bool val = true) {fixw_ = val;}
116  void fixH(bool val = true) {fixh_ = val;}
117  bool isWFixed() const {return fixw_;}
118  bool isHFixed() const {return fixh_;}
119  */
120 
122  static GType& Type();
123  GType& type() const override {return Type();}
124 
127  bool fixw_{}, fixh_{};
128  GHandle place_{};
129  bool onAdd(Gadget*) override;
130  void onRemove(Gadget*) override;
131  void fire(GEvent&) override;
133 };
134 
135 }
136 #endif