ubit
ulength.hpp
1 /************************************************************************
2  *
3  * ulength.hpp: lengths and units
4  * Ubit GUI Toolkit - Version 6
5  * (C) 2009 | Eric Lecolinet | TELECOM ParisTech | http://www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _ulength_hpp_
18 #define _ulength_hpp_ 1
19 #include <ubit/uobject.hpp>
20 namespace ubit {
21 
22  class UUnit {
23  public:
24  enum Type {PX, EM, EX, IN, CM, MM, PT, PC, PERCENT, PERCENT_CTR, AUTO, IGNORE};
25 
26  UUnit(const UUnit& u) : type(u.type) {}
27  explicit UUnit(UUnit::Type t) : type(t) {}
28 
29  UUnit& operator=(const UUnit& u) {type = u.type; return *this;}
30  UUnit& operator=(UUnit::Type t) {type = t; return *this;}
31 
32  UUnit::Type getType() const {return (UUnit::Type)type;}
33 
34  bool operator==(UUnit u) const {return u.type == type;}
35  bool operator==(UUnit::Type t) const {return t == type;}
36 
37  bool operator!=(UUnit u) const {return u.type != type;}
38  bool operator!=(UUnit::Type t) const {return t != type;}
39 
40  unsigned char type;
41  };
42 
43  extern const UUnit
44  UPX,
45  UEM,
46  UEX,
47  UIN,
48  UCM,
49  UMM,
50  UPT,
51  UPC,
52  UPERCENT,
53  UPERCENT_CTR;
54 
55 
56  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59  class ULength {
60  public:
61  struct Modes {
62  enum {NONE=0};
63  unsigned char val;
64  explicit Modes(unsigned char m) : val(m) {}
65  };
66 
67  ULength(float value = 0.0) : val(value), unit(UUnit::PX), modes(0) {}
69 
70  ULength(float value, UUnit u) : val(value), unit(u), modes(0) {}
72 
73  ULength(float value, UUnit u, Modes m) : val(value), unit(u), modes(m) {}
75 
76  ULength(const UStr& text_spec);
78 
79  bool operator==(const ULength&) const;
80  bool operator!=(const ULength&) const;
81 
82  operator float() const {return val;}
83 
84  float getValue() const {return val;}
85  UUnit getUnit() const {return unit;}
86  Modes getModes() const {return modes;}
87 
88  ULength& setValue(float value) {val = value; return *this;}
89  ULength& setUnit(UUnit u) {unit = u; return *this;}
90  ULength& setUnit(const char* unit);
91  ULength& setModes(Modes m) {modes = m; return *this;}
92 
93  ULength& set(const UStr& text_spec);
95 
96  UStr toString() const;
97 
98  float toPixels(UDisp*, const UFontDesc&, float view_len, float parview_len) const;
99 
100  friend std::ostream& operator<<(std::ostream& s, const ULength& l);
101  friend ULength operator|(float len, UUnit u);
102  friend ULength operator|(ULength len, UUnit u);
103  friend ULength operator|(ULength len, ULength::Modes m);
104 
105  float val;
106  UUnit unit;
107  Modes modes;
108  };
109 
110  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 
112  std::ostream& operator<<(std::ostream&, const ULength&);
114 
115  inline ULength operator|(float len, UUnit u) {return ULength(len, u);}
124  inline ULength operator|(ULength len, UUnit u) {return ULength(len).setUnit(u);}
125 
126  inline ULength operator|(ULength len, ULength::Modes m) {return ULength(len).setModes(m);}
127 
128  extern const ULength
129  UIGNORE,
130  UAUTO;
131 
132 
133  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134 
135  struct UPosSpec {
136  UPosSpec(ULength _x, ULength _y) : x(_x), y(_y) {}
137  ULength x, y;
138  };
139 
140  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 
142  struct USizeSpec {
143  USizeSpec(ULength w, ULength h) : width(w), height(h) {}
144  ULength width, height;
145  };
146 
147  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150  struct UPaddingSpec {
151  UPaddingSpec(ULength horiz, ULength vert) : top(vert), right(horiz), bottom(vert), left(horiz) {}
152  // attention: doit etre refenie si cette classe est heritee
153  void set(ULength horiz, ULength vert) {left = right = horiz; top = bottom = vert;}
154  void add(const UPaddingSpec&);
155  ULength top, right, bottom, left;
156  };
157 
158 }
159 #endif
160 
ULength(float value=0.0)
creates a new length with the specified value, unit = PX and modes = 0.
Definition: ulength.hpp:67
Length of positions (UPos), sizes (USize) and padding (UPadding)
Definition: ulength.hpp:59
Definition: ulength.hpp:135
Display Context.
Definition: udisp.hpp:44
[impl] Internal representation for fonts.
Definition: ufontImpl.hpp:24
ULength(float value, UUnit u)
creates a new length with the specified value and unit (modes = 0).
Definition: ulength.hpp:70
Definition: ulength.hpp:22
Box padding.
Definition: ulength.hpp:150
Definition: uhardfont.hpp:31
Definition: ulength.hpp:142
Definition: ulength.hpp:61
ULength(float value, UUnit u, Modes m)
creates a new length with the specified value, unit and modes.
Definition: ulength.hpp:73
Ubit String.
Definition: ustr.hpp:72