src/widget/widget.h
branchcpp_gui
changeset 6301 e0251f797d59
parent 6297 4bf29d14edba
child 6302 bd80897189ba
equal deleted inserted replaced
6300:1f0d2abac815 6301:e0251f797d59
     3 #ifndef WIDGET_H
     3 #ifndef WIDGET_H
     4 #define WIDGET_H
     4 #define WIDGET_H
     5 
     5 
     6 
     6 
     7 #include <list>
     7 #include <list>
     8 #include <vector>
     8 //#include <vector>
       
     9 #include <map>
     9 #include "../macros.h"
    10 #include "../macros.h"
    10 #include "../string.h"
    11 #include "../string.h"
    11 #include "../order.h"
    12 #include "../order.h"
    12 #include "../rail.h"
    13 #include "../rail.h"
    13 #include "../airport.h"
    14 #include "../airport.h"
    44 	FF_NO_CAPTION_BAR      = (1 <<  0),
    45 	FF_NO_CAPTION_BAR      = (1 <<  0),
    45 	FF_NO_CLOSE_BOX        = (1 <<  1),
    46 	FF_NO_CLOSE_BOX        = (1 <<  1),
    46 	FF_NO_STICKY_BOX       = (1 <<  2),
    47 	FF_NO_STICKY_BOX       = (1 <<  2),
    47 	FF_NO_RESIZE_BOX       = (1 <<  3),
    48 	FF_NO_RESIZE_BOX       = (1 <<  3),
    48 	FF_UNMOVABLE           = (1 <<  4),
    49 	FF_UNMOVABLE           = (1 <<  4),
    49 	FF_TRANSPARENT         = (1 <<  5),
    50 	FF_NO_FRAME            = (1 <<  5),
    50 	FF_TOGGLE_BUTTON       = (1 <<  6),
    51 	FF_IGNORE_PARENT_FRAME = (1 <<  6),
    51 	FF_STICKED             = (1 <<  7),
    52 	FF_TOGGLE_BUTTON       = (1 <<  7),
    52 	FF_FIXED_WIDTH         = (1 <<  8),
    53 	FF_STICKED             = (1 <<  8),
    53 	FF_FIXED_HEIGHT        = (1 <<  9),
    54 
    54 
    55 	FF_MAX_WIDTH           = (1 << 16),
       
    56 	FF_MAX_HEIGHT          = (1 << 17),
       
    57 	FF_MIN_WIDTH           = (1 << 18),
       
    58 	FF_MIN_HEIGHT          = (1 << 19),
       
    59 	FF_BEST_WIDTH          = (1 << 20),
       
    60 	FF_BEST_HEIGHT         = (1 << 21),
       
    61 	FF_FIXED_WIDTH         = (1 << 22),
       
    62 	FF_FIXED_HEIGHT        = (1 << 23),
       
    63 	FF_SAME_WIDTH          = (1 << 24),
       
    64 	FF_SAME_HEIGHT         = (1 << 25),
       
    65 	FF_SAME_DIST_X         = (1 << 26),
       
    66 	FF_SAME_DIST_Y         = (1 << 27),
       
    67 
       
    68 	FF_ALIGN_LEFT          = (1 << 28),
       
    69 	FF_ALIGN_RIGHT         = (1 << 29),
       
    70 	FF_ALIGN_TOP           = (1 << 30),
       
    71 	FF_ALIGN_BOTTOM        = (1 << 31),
       
    72 
       
    73 	FF_MAX_SIZE            = FF_MAX_WIDTH   | FF_MAX_HEIGHT,
       
    74 	FF_MIN_SIZE            = FF_MIN_WIDTH   | FF_MIN_HEIGHT,
       
    75 	FF_BEST_SIZE           = FF_BEST_WIDTH  | FF_BEST_HEIGHT,
    55 	FF_FIXED_SIZE          = FF_FIXED_WIDTH | FF_FIXED_HEIGHT,
    76 	FF_FIXED_SIZE          = FF_FIXED_WIDTH | FF_FIXED_HEIGHT,
       
    77 	FF_SAME_SIZE           = FF_SAME_WIDTH  | FF_SAME_HEIGHT,
       
    78 
       
    79 	FF_ALIGN_HCENTER       = FF_ALIGN_LEFT | FF_ALIGN_RIGHT,
       
    80 	FF_ALIGN_VCENTER       = FF_ALIGN_TOP | FF_ALIGN_BOTTOM,
    56 };
    81 };
    57 
    82 
    58 DECLARE_ENUM_AS_BIT_SET(FeatureFlags);
    83 DECLARE_ENUM_AS_BIT_SET(FeatureFlags);
    59 
    84 
    60 struct Widget;
    85 struct Widget;
    67 
    92 
    68 struct Widget : public SimpleCountedObject {
    93 struct Widget : public SimpleCountedObject {
    69 	typedef AdaptT<EventHandlerDelegatePtr> Handler;
    94 	typedef AdaptT<EventHandlerDelegatePtr> Handler;
    70 	typedef std::list<Handler> Handlers;
    95 	typedef std::list<Handler> Handlers;
    71 
    96 
       
    97 	/*------------------------------------------------------------------------------------------*/
       
    98 	ZeroInitBegin   m_zero_init_area;   ///< following members get cleared by constructor
    72 	CompositeWidget *m_container;       ///< widget container (can be any panel or window)
    99 	CompositeWidget *m_container;       ///< widget container (can be any panel or window)
    73 	WidgetId        m_id;               ///< Widget id in its container
   100 	WidgetId        m_id;               ///< Widget id in its container
    74 	Rect16          m_rect;             ///< The position offsets relative to the container
       
    75 	uint16          m_data;             ///< The String/Image or special code (list-matrices) of a widget
       
    76 	FeatureFlags    m_feature_flags;    ///< @see FeatureFlags
   101 	FeatureFlags    m_feature_flags;    ///< @see FeatureFlags
    77 	byte            m_color;            ///< Widget color, see docs/ottd-colourtext-palette.png
   102 	uint16          m_color;            ///< Widget color, see docs/ottd-colourtext-palette.png
       
   103 	StringID        m_tooltips;         ///< Tooltips that are shown when right clicking on a widget
       
   104 	Anchors         m_anchors;          ///< Resize/move when container resizes?
    78 	bool            m_is_closing : 1;   ///< Widget was logically destroyed
   105 	bool            m_is_closing : 1;   ///< Widget was logically destroyed
    79 	bool            m_dont_clip : 1;    ///< should not be clipped by parent (container)
   106 	bool            m_dont_clip : 1;    ///< should not be clipped by parent (container)
    80 	StringID        m_tooltips;         ///< Tooltips that are shown when right clicking on a widget
   107 	ZeroInitEnd     m_zero_init_end;    ///< end of zero initialization area
    81 	Anchors         m_anchors;          ///< Resize/move when container resizes?
   108 	/*------------------------------------------------------------------------------------------*/
    82 	Point16         m_min_size;         ///< Minimum size
   109 	Rect16          m_rect;             ///< The position offsets relative to the container and size
    83 	Point16         m_max_size;         ///< Maximum size
   110 	Size16          m_ext_border;       ///< Border around widget (for layout purposes)
    84 	Point16         m_size_step;        ///< When resizing, what step is the best
   111 	Size16          m_min_size;         ///< Minimum size
       
   112 	Size16          m_max_size;         ///< Maximum size
       
   113 	Size16          m_size_step;        ///< When resizing, what step is the best
       
   114 
    85 	Handlers        m_handlers;         ///< dynamically registered event handlers
   115 	Handlers        m_handlers;         ///< dynamically registered event handlers
    86 
   116 
    87 	Widget()
   117 	Widget()
    88 		: m_container(NULL), m_id(0), m_rect(), m_data(0), m_feature_flags(FF_NONE), m_color(0)
   118 		: m_zero_init_area(m_zero_init_end)
    89 		, m_is_closing(false), m_tooltips(0), m_anchors(PIN_NONE)
   119 	{}
    90 		, m_max_size(Point16::max()), m_size_step(1, 1)
   120 
    91 	{}
   121 	Widget(CompositeWidget *container, FeatureFlags feature_flags, StringID tooltips = 0, uint16 color = COLOUR_PARENT)
    92 
   122 		: m_zero_init_area(m_zero_init_end)
    93 	Widget(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips)
   123 		, m_container(container)
    94 		: m_container(container)
       
    95 		, m_id(id)
       
    96 		, m_rect(rect)
       
    97 		, m_data(0)
       
    98 		, m_feature_flags(feature_flags)
   124 		, m_feature_flags(feature_flags)
    99 		, m_color(color)
   125 		, m_color(color)
   100 		, m_is_closing(false)
   126 		, m_is_closing(false)
   101 		, m_dont_clip(false)
   127 		, m_dont_clip(false)
   102 		, m_tooltips(tooltips)
   128 		, m_tooltips(tooltips)
   103 		, m_anchors(PIN_NONE)
   129 		, m_anchors(PIN_NONE)
   104 		, m_max_size(Point16::max())
       
   105 		, m_size_step(1, 1)
   130 		, m_size_step(1, 1)
   106 	{}
   131 	{}
   107 
   132 
   108 	int16 Left() const;
   133 	int16 Left() const;
   109 	int16 Top() const;
   134 	int16 Top() const;
   111 	int16 Bottom() const;
   136 	int16 Bottom() const;
   112 	int16 Width() const;
   137 	int16 Width() const;
   113 	int16 Height() const;
   138 	int16 Height() const;
   114 	const Point16& TopLeft() const;
   139 	const Point16& TopLeft() const;
   115 	const Point16& BottomRight() const;
   140 	const Point16& BottomRight() const;
   116 	Point16 Size() const;
   141 	Size16 GetSize() const;
   117 	Point16 CenterPt() const;
   142 	Point16 CenterPt() const;
   118 	const Rect16& GetRect() const;
   143 	const Rect16& GetRect() const;
   119 	Rect16 GetLocalRect() const;
   144 	Rect16 GetLocalRect() const;
       
   145 
       
   146 	Point16 GetTopLeftInWindow() const;
   120 
   147 
   121 	void SetLeft(int16 val);
   148 	void SetLeft(int16 val);
   122 	void SetTop(int16 val);
   149 	void SetTop(int16 val);
   123 	void SetRight(int16 val);
   150 	void SetRight(int16 val);
   124 	void SetBottom(int16 val);
   151 	void SetBottom(int16 val);
   126 	void SetHeight(int16 val);
   153 	void SetHeight(int16 val);
   127 	void SetTopLeft(const Point16 &pt);
   154 	void SetTopLeft(const Point16 &pt);
   128 	void SetBottomRight(const Point16 &pt);
   155 	void SetBottomRight(const Point16 &pt);
   129 	void SetSize(const Point16 &pt);
   156 	void SetSize(const Point16 &pt);
   130 	void SetRect(const Rect16 &rect);
   157 	void SetRect(const Rect16 &rect);
   131 
   158 	void SetSlotRect(const Rect16 &rect);
   132 	byte GetColor() const;
   159 	bool CheckMinSize();
   133 	void SetColor(byte val);
   160 
       
   161 	uint16 GetColor() const;
       
   162 	byte GetBkColor() const;
       
   163 	void SetColor(uint16 val);
   134 
   164 
   135 	void SetAnchors(Anchors a);
   165 	void SetAnchors(Anchors a);
   136 
   166 
   137 	WidgetId GetId() const;
   167 	WidgetId GetId() const;
   138 
   168 
   139 	void AddHandler(EventHandlerDelegate *d);
   169 	void AddHandler(EventHandlerDelegate *d);
   140 	void CallHandlers(EventBase &e);
   170 	void CallHandlers(EventBase &e);
   141 
   171 
   142 	void Invalidate() {}; ///< now we redraw gui all the time, so no processing is needed here
   172 	void Invalidate() {}; ///< now we redraw gui all the time, so no processing is needed here
   143 	static /*static*/ void FillRect(const Rect16 &rc, int color);
   173 	static Size16 GetStringSize(StringID id);
       
   174 //	static void FillRect(const Rect16 &rc, int color);
   144 	static void DrawFrameRect(int left, int top, int right, int bottom, int ctab, FrameFlags flags);
   175 	static void DrawFrameRect(int left, int top, int right, int bottom, int ctab, FrameFlags flags);
   145 	void DrawFrameRect(int ctab, FrameFlags flags);
   176 	void DrawFrameRect(int ctab, FrameFlags flags);
   146 	void DrawSprite(SpriteID img, SpriteID pal, const Point16 &local_pos);
   177 	void DrawSprite(SpriteID img, SpriteID pal, const Point16 &local_pos);
   147 
   178 
   148 	virtual BaseWindow* GetWindow();
   179 	virtual BaseWindow* GetWindow();
   149 	virtual Widget* WidgetFromPt(const Point16 &pt);
   180 	virtual Widget* WidgetFromPt(const Point16 &pt);
   150 
   181 
   151 	virtual void Close();
   182 	virtual void Close();
   152 
   183 
       
   184 	virtual void QuerySizes() {};
       
   185 	virtual void DoLayout() {};
   153 	virtual void DrawBackground(EvtPaint &ev);
   186 	virtual void DrawBackground(EvtPaint &ev);
   154 
   187 
   155 	/**
   188 	/**
   156 	 * OnCreate() handler called when window gets created. Override this method in your
   189 	 * OnCreate() handler called when window gets created. Override this method in your
   157 	 * Window subclass and place widgets into your window from there.
   190 	 * Window subclass and place widgets into your window from there.
   201 	}
   234 	}
   202 
   235 
   203 };
   236 };
   204 
   237 
   205 struct CompositeWidget : public Widget {
   238 struct CompositeWidget : public Widget {
   206 	struct Slot {
   239 	typedef Widget super;
       
   240 	struct Slot : SimpleCountedObject {
   207 		WidgetPtr    m_wi;
   241 		WidgetPtr    m_wi;
   208 		uint8        m_span;
   242 
       
   243 		Slot(Widget *wi)
       
   244 			: m_wi(wi)
       
   245 		{}
       
   246 
       
   247 		virtual ~Slot() {};
   209 	};
   248 	};
   210 
   249 
   211 	typedef Widget super;
   250 	typedef CCountedPtr<Slot> SlotPtr;
   212 	typedef std::vector<Slot> Widgets;
   251 
       
   252 	typedef std::list<AdaptT<SlotPtr> > Widgets;
   213 	typedef Widgets::iterator WidgetIterator;
   253 	typedef Widgets::iterator WidgetIterator;
   214 	typedef Widgets::reverse_iterator WidgetReverseIterator;
   254 	typedef Widgets::reverse_iterator WidgetReverseIterator;
   215 
   255 
   216 protected:
   256 protected:
   217 	Widgets m_widgets;
   257 	Widgets         m_widgets;
       
   258 public:
       
   259 	Size16          m_int_border;       ///< Border inside (for layout purposes)
   218 
   260 
   219 public:
   261 public:
   220 	CompositeWidget()
   262 	CompositeWidget()
   221 		: Widget()
   263 		: Widget()
   222 	{}
   264 	{}
   223 
   265 
   224 	CompositeWidget(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips)
   266 	CompositeWidget(CompositeWidget *container, FeatureFlags feature_flags, StringID tooltips = 0, uint16 color = COLOUR_PARENT)
   225 		: Widget(container, id, feature_flags, color, rect, tooltips)
   267 		: Widget(container, feature_flags, tooltips, color)
   226 	{}
   268 	{}
   227 
   269 
   228 	void AddWidget(Widget *wd);
   270 	Size16 CalcInternalBorder();
   229 	WidgetIterator FindWidget(WidgetId id);
   271 
   230 	Widget* GetWidget(WidgetId id);
   272 	virtual int NumWidgets();
   231 	Widget* RemoveWidget(WidgetId id);
   273 	virtual bool RemoveWidget(int idx);
       
   274 	virtual WidgetIterator AddSlot(Slot *ws);
       
   275 	//WidgetIterator FindWidget(WidgetId id);
       
   276 	//Widget* GetWidget(WidgetId id);
       
   277 	//Widget* RemoveWidget(WidgetId id);
   232 
   278 
   233 	/*virtual*/ Widget* WidgetFromPt(const Point16 &pt);
   279 	/*virtual*/ Widget* WidgetFromPt(const Point16 &pt);
   234 
   280 
   235 	virtual void CreateNcWidgets() {};
   281 	virtual void CreateNcWidgets() {};
   236 	virtual void CreateWidgets() = 0;
   282 	virtual void CreateWidgets() {};
   237 	virtual void Close();
   283 	/*virtual*/ void Close();
       
   284 
       
   285 	/*virtual*/ void QuerySizes();
       
   286 	/*virtual*/ void DoLayout();
   238 
   287 
   239 	/*virtual*/ void OnCreate(EvtCreate &ev);
   288 	/*virtual*/ void OnCreate(EvtCreate &ev);
   240 	/*virtual*/ void OnPaint(EvtPaint &ev);
   289 	/*virtual*/ void OnPaint(EvtPaint &ev);
   241 	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
   290 	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
   242 	/*virtual*/ void OnRightButtonDown(EvtRightButtonDown &ev);
   291 	/*virtual*/ void OnRightButtonDown(EvtRightButtonDown &ev);
   243 	/*virtual*/ void OnResize(EvtResize &ev);
   292 	/*virtual*/ void OnResize(EvtResize &ev);
   244 };
   293 };
   245 
   294 
       
   295 struct Panel : CompositeWidget {
       
   296 	typedef CompositeWidget super;
       
   297 
       
   298 	enum Placement {
       
   299 		NONE,
       
   300 		LEFT,
       
   301 		TOP,
       
   302 		RIGHT,
       
   303 		BOTTOM,
       
   304 		CENTER,
       
   305 	};
       
   306 
       
   307 	static const FeatureFlags DEFAULT_FEATURES = FF_NONE;
       
   308 
       
   309 	Panel()
       
   310 		: CompositeWidget()
       
   311 	{}
       
   312 
       
   313 	Panel(CompositeWidget *container, FeatureFlags feature_flags = DEFAULT_FEATURES, StringID tooltips = 0, uint16 color = COLOUR_PARENT)
       
   314 		: CompositeWidget(container, feature_flags, tooltips, color)
       
   315 	{}
       
   316 
       
   317 	virtual CompositeWidget::WidgetIterator AddWidget(Widget *wd, Placement pp);
       
   318 
       
   319 	/*virtual*/ void QuerySizes();
       
   320 	/*virtual*/ void DoLayout();
       
   321 };
       
   322 
   246 }; // namespace gui
   323 }; // namespace gui
   247 
   324 
   248 #endif /* WIDGET_H */
   325 #endif /* WIDGET_H */