(svn r8997) [cpp_gui] -Codechange: common window features are now controlled by FeatureFlags enum. cpp_gui
authorKUDr
Sat, 03 Mar 2007 20:25:37 +0000
branchcpp_gui
changeset 6283 7072ee68c676
parent 6282 c5b92f2d924f
child 6284 45d0233e7d79
(svn r8997) [cpp_gui] -Codechange: common window features are now controlled by FeatureFlags enum.
-Caption bar, close box and resize box are created automatically depending on features specified in window ctor.
src/intro_gui.cpp
src/widget/widget.h
src/widget/widget_caption.cpp
src/widget/widget_composite.cpp
src/window.cpp
src/window.h
--- a/src/intro_gui.cpp	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/intro_gui.cpp	Sat Mar 03 20:25:37 2007 +0000
@@ -164,7 +164,7 @@
 	gui::WidgetPtr m_button1;
 
 	WindowT()
-		: BaseWindow(WC_TEST1)
+		: BaseWindow(WC_TEST1, STR_015B_OPENTTD, COLOUR_BROWN, gui::FF_NONE)
 	{
 	}
 
@@ -173,25 +173,14 @@
 		/* set common window properties */
 		SetTopLeft(Point16(140, 40));
 		SetSize(Point16(336, 195));
-		desc_flags = WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS;
-		m_color = COLOUR_BROWN;
-
-		/* add caption bar (later this will be done automatically by window core */
-		gui::CaptionBar *capt_bar = new gui::CaptionBar(this, -100, RESIZE_LR, COLOUR_BROWN, 0x84, STR_0282_CONSTRUCT_BUBBLE_GENERATOR, STR_015B_OPENTTD);
-		AddWidget(capt_bar);
-
-		/* add resize box (later this will be done automatically by window core */
-		gui::ResizeBox *resize_box = new gui::ResizeBox(this);
-		AddWidget(resize_box);
 
 		/* add controls */
-		m_button1 = new gui::TextButton(this, 22, RESIZE_NONE, COLOUR_YELLOW, Rect16(104, 175, 231, 186), STR_0305_QUIT_OPENTTD, STR_0304_QUIT);
+		m_button1 = new gui::TextButton(this, 22, gui::FF_NONE, COLOUR_YELLOW, Rect16(104, 175, 231, 186), STR_0305_QUIT_OPENTTD, STR_0304_QUIT);
 		m_button1->SetAnchors(PIN_RIGHT | PIN_BOTTOM);
 		AddWidget(m_button1);
 
 		/* connect control callbacks */
 		m_button1->AddReflectHandlerT(&WindowT::Button1_OnClick);
-//		m_button1->AddOnClickHandlerT(&WindowT::Button1_OnClick);
 	}
 
 	void Button1_OnClick(gui::EvtLeftClick &e)
--- a/src/widget/widget.h	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/widget/widget.h	Sat Mar 03 20:25:37 2007 +0000
@@ -38,6 +38,17 @@
 
 namespace gui {
 
+enum FeatureFlags {
+	FF_NONE                = 0,
+	FF_NO_CAPTION_BAR      = (1 <<  0),
+	FF_NO_CLOSE_BOX        = (1 <<  1),
+	FF_NO_STICKY_BOX       = (1 <<  2),
+	FF_NO_RESIZE_BOX       = (1 <<  3),
+	FF_UNMOVABLE           = (1 <<  4),
+};
+
+DECLARE_ENUM_AS_BIT_SET(FeatureFlags);
+
 struct Widget;
 typedef CCountedPtr<Widget> WidgetPtr;
 
@@ -54,7 +65,7 @@
 	WidgetId        m_id;               ///< Widget id in its container
 	Rect16          m_rect;             ///< The position offsets relative to the container
 	uint16          m_data;             ///< The String/Image or special code (list-matrices) of a widget
-	byte            m_display_flags;    ///< Resize direction, alignment, etc. during resizing, see @ResizeFlags
+	FeatureFlags    m_feature_flags;    ///< @see FeatureFlags
 	byte            m_color;            ///< Widget color, see docs/ottd-colourtext-palette.png
 	bool            m_is_closing : 1;   ///< Widget was logically destroyed
 	bool            m_dont_clip : 1;    ///< should not be clipped by parent (container)
@@ -63,16 +74,16 @@
 	Handlers        m_handlers;         ///< dynamically registered event handlers
 
 	Widget()
-		: m_container(NULL), m_id(0), m_rect(), m_data(0), m_display_flags(0), m_color(0)
+		: m_container(NULL), m_id(0), m_rect(), m_data(0), m_feature_flags(FF_NONE), m_color(0)
 		, m_is_closing(false), m_tooltips(0), m_anchors(PIN_NONE)
 	{}
 
-	Widget(CompositeWidget *container, WidgetId id, byte display_flags, byte color, const Rect16 &rect, StringID tooltips)
+	Widget(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips)
 		: m_container(container)
 		, m_id(id)
 		, m_rect(rect)
 		, m_data(0)
-		, m_display_flags(display_flags)
+		, m_feature_flags(feature_flags)
 		, m_color(color)
 		, m_is_closing(false)
 		, m_dont_clip(false)
@@ -186,8 +197,8 @@
 		: Widget()
 	{}
 
-	CompositeWidget(CompositeWidget *container, WidgetId id, byte display_flags, byte color, const Rect16 &rect, StringID tooltips)
-		: Widget(container, id, display_flags, color, rect, tooltips)
+	CompositeWidget(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips)
+		: Widget(container, id, feature_flags, color, rect, tooltips)
 	{}
 
 	void AddWidget(Widget *wd);
@@ -196,6 +207,7 @@
 
 	/*virtual*/ Widget* WidgetFromPt(const Point16 &pt);
 
+	virtual void CreateNcWidgets() {};
 	virtual void CreateWidgets() = 0;
 	virtual void Close();
 
@@ -219,8 +231,8 @@
 		, m_pushed(false)
 	{}
 
-	Button(CompositeWidget *container, WidgetId id, byte display_flags, byte color, const Rect16 &rect, StringID tooltips)
-		: Widget(container, id, display_flags, color, rect, tooltips)
+	Button(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips)
+		: Widget(container, id, feature_flags, color, rect, tooltips)
 		, m_pushed(false)
 	{}
 
@@ -242,8 +254,8 @@
 		: Button()
 	{}
 
-	TextButton(CompositeWidget *container, WidgetId id, byte display_flags, byte color, const Rect16 &rect, StringID tooltips, StringID text)
-		: Button(container, id, display_flags, color, rect, tooltips)
+	TextButton(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 &rect, StringID tooltips, StringID text)
+		: Button(container, id, feature_flags, color, rect, tooltips)
 		, m_text(text)
 	{}
 
@@ -262,7 +274,7 @@
 	{}
 
 	CloseBox(CompositeWidget *container)
-		: Button(container, -102, 0, container->m_color, Rect16(), 0)
+		: Button(container, -102, FF_NONE, container->m_color, Rect16(), 0)
 	{}
 
 	/*virtual*/ void OnCreate(EvtCreate &ev);
@@ -274,27 +286,20 @@
 	typedef Widget super;
 
 protected:
-	StringID      m_text;
-	byte          m_text_color;
 	Point16       m_moving_offset;
 	CaptureTicket m_ticket_moving;
 
 public:
 	Caption()
 		: Widget()
-		, m_text(0)
-		, m_text_color(0xFF)
 	{}
 
-	Caption(CompositeWidget *container, WidgetId id, byte display_flags, byte color, const Rect16 rect, byte text_color, StringID tooltips, StringID text)
-		: Widget(container, id, display_flags, color, rect, tooltips)
-		, m_text(text)
-		, m_text_color(text_color)
+	Caption(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color, const Rect16 rect, byte text_color, StringID tooltips, StringID text)
+		: Widget(container, id, feature_flags, color, rect, tooltips)
 	{}
 
 	/*virtual*/ void DrawBackground(EvtPaint &ev);
 
-	/*virtual*/ void OnCreate(EvtCreate &ev);
 	/*virtual*/ void OnPaint(EvtPaint &ev);
 	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
 	void OnCaptureMoving(EvtMouseOver &e);
@@ -305,28 +310,16 @@
 
 	static const int16 DEFAULT_HEIGHT = 14;
 
-protected:
-	StringID      m_text;
-	byte          m_text_color;
-	Point16       m_moving_offset;
-	CaptureTicket m_ticket_moving;
-
 public:
 	CaptionBar()
 		: CompositeWidget()
-		, m_text(0)
-		, m_text_color(0xFF)
 	{}
 
-	CaptionBar(CompositeWidget *container, WidgetId id, byte display_flags, byte color, byte text_color, StringID tooltips, StringID text)
-		: CompositeWidget(container, id, display_flags, color, Rect16(0, 0, container->Width() - 1, DEFAULT_HEIGHT - 1), tooltips)
-		, m_text(text)
-		, m_text_color(text_color)
+	CaptionBar(CompositeWidget *container, WidgetId id, FeatureFlags feature_flags, byte color)
+		: CompositeWidget(container, id, feature_flags, color, Rect16(0, 0, container->Right(), DEFAULT_HEIGHT - 1), 0)
 	{}
 
 	/*virtual*/ void CreateWidgets();
-
-	/*virtual*/ void OnCreate(EvtCreate &ev);
 };
 
 
@@ -346,7 +339,7 @@
 	{}
 
 	ResizeBox(CompositeWidget *container)
-		: Widget(container, -101, 0, container->m_color, Rect16(), 0)
+		: Widget(container, -101, FF_NONE, container->m_color, Rect16(), 0)
 	{}
 
 	/*virtual*/ void DrawBackground(EvtPaint &ev);
--- a/src/widget/widget_caption.cpp	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/widget/widget_caption.cpp	Sat Mar 03 20:25:37 2007 +0000
@@ -23,23 +23,25 @@
 
 /*virtual*/ void CaptionBar::CreateWidgets()
 {
+	BaseWindow *w = GetWindow();
+	assert(w != NULL);
+
+	bool make_close_box = ((m_feature_flags & FF_NO_CLOSE_BOX) == FF_NONE);
+	Rect16 rc_caption = GetRect();
+
 	/* add close box */
-	CloseBox *close_box = new CloseBox(this);
-	AddWidget(close_box);
+	if (make_close_box) {
+		CloseBox *close_box = new CloseBox(this);
+		AddWidget(close_box);
+		rc_caption.SetLeft(rc_caption.Left() + 11);
+	}
 
 	/* add caption */
-	Rect16 rc_caption = GetRect();
-	rc_caption.SetLeft(rc_caption.Left() + 11);
-	Caption *caption = new Caption(this, -100, RESIZE_LR, m_color, rc_caption, 0x84, STR_018C_WINDOW_TITLE_DRAG_THIS, STR_015B_OPENTTD);
+	Caption *caption = new Caption(this, -100, FF_NONE, m_color, rc_caption, 0x84, STR_018C_WINDOW_TITLE_DRAG_THIS, w->m_caption_text);
+	caption->SetAnchors(PIN_LEFT | PIN_TOP | PIN_RIGHT);
 	AddWidget(caption);
 }
 
-/*virtual*/ void CaptionBar::OnCreate(EvtCreate &ev)
-{
-	SetAnchors(PIN_LEFT | PIN_TOP | PIN_RIGHT);
-	super::OnCreate(ev);
-}
-
 /*virtual*/ void Caption::DrawBackground(EvtPaint &ev)
 {
 	BaseWindow *w = GetWindow();
@@ -54,21 +56,27 @@
 	}
 }
 
-/*virtual*/ void Caption::OnCreate(EvtCreate &ev)
-{
-	SetAnchors(PIN_LEFT | PIN_TOP | PIN_RIGHT);
-	super::OnCreate(ev);
-}
-
 /*virtual*/ void Caption::OnPaint(EvtPaint &ev)
 {
+	BaseWindow *w = GetWindow();
+	assert(w != NULL);
+
 	DrawBackground(ev);
-	DrawStringCenteredTruncated(Left() + 2, Right() - 2, Top() + 2, m_text, m_text_color);
+	DrawStringCenteredTruncated(Left() + 2, Right() - 2, Top() + 2, w->m_caption_text, 0x84);
 	ev.SetHandled();
 }
 
 /*virtual*/ void Caption::OnLeftButtonDown(EvtLeftButtonDown &ev)
 {
+	BaseWindow *w = GetWindow();
+	assert(w != NULL);
+
+	/* if window is unmovable, do nothing */
+	if ((w->m_feature_flags & FF_UNMOVABLE) != FF_NONE) {
+		super::OnLeftButtonDown(ev);
+		return;
+	}
+
 	m_moving_offset = ev.m_pt;
 	ev.SetHandled();
 	m_ticket_moving = CaptureEventsT(this, &Caption::OnCaptureMoving);
--- a/src/widget/widget_composite.cpp	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/widget/widget_composite.cpp	Sat Mar 03 20:25:37 2007 +0000
@@ -83,8 +83,10 @@
 
 /*virtual*/ void CompositeWidget::OnCreate(EvtCreate &ev)
 {
-	/* this is composite widget - it should create its own children */
+	/* create standard children */
 	CreateWidgets();
+	/* if there are any non-client widgets, create them */
+	CreateNcWidgets();
 
 	/* notify all children that we are creating */
 	for (WidgetIterator it_next = m_widgets.begin(); it_next != m_widgets.end(); ) {
--- a/src/window.cpp	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/window.cpp	Sat Mar 03 20:25:37 2007 +0000
@@ -5,6 +5,7 @@
 #include "stdafx.h"
 #include <stdarg.h>
 #include "openttd.h"
+#include "table/strings.h"
 #include "debug.h"
 #include "functions.h"
 #include "map.h"
@@ -268,6 +269,23 @@
 	return true;
 }
 
+/*virtual*/ void BaseWindow::CreateNcWidgets()
+{
+	super::CreateNcWidgets();
+	if ((m_feature_flags & gui::FF_NO_CAPTION_BAR) == gui::FF_NONE) {
+		/* add caption bar */
+		gui::FeatureFlags feature_flags = m_feature_flags & (gui::FF_NO_CLOSE_BOX | gui::FF_NO_STICKY_BOX);
+		gui::CaptionBar *capt_bar = new gui::CaptionBar(this, -100, feature_flags, m_color);
+		capt_bar->SetAnchors(PIN_LEFT | PIN_TOP | PIN_RIGHT);
+		AddWidget(capt_bar);
+	}
+	if ((m_feature_flags & gui::FF_NO_RESIZE_BOX) == gui::FF_NONE) {
+		/* add resize box */
+		gui::ResizeBox *resize_box = new gui::ResizeBox(this);
+		AddWidget(resize_box);
+	}
+}
+
 /*virtual*/ void BaseWindow::CreateWidgets()
 {
 	/* only old windows are allowed to use this fake 'CreateWidgets()' method << new gui must have its own */
--- a/src/window.h	Sat Mar 03 16:11:18 2007 +0000
+++ b/src/window.h	Sat Mar 03 20:25:37 2007 +0000
@@ -394,9 +394,11 @@
 	WindowNumber window_number;
 
 protected:
-	Point16 m_wnd_pos;
+	Point16    m_wnd_pos;
 
 public:
+	StringID   m_caption_text;
+
 	Scrollbar hscroll, vscroll, vscroll2;
 	ResizeInfo resize;
 
@@ -418,9 +420,11 @@
 	ZeroInitEnd m_zero_init_end; ///< end of zero initialization area
 
 protected:
-	BaseWindow(WindowClass cls)
+	BaseWindow(WindowClass cls, StringID caption_text = 0, byte color = COLOUR_GREY, gui::FeatureFlags feature_flags = gui::FF_NONE)
 		: m_zero_init_area(m_zero_init_end)
+		, CompositeWidget(NULL, 0, feature_flags, color, Rect16(), 0)
 		, window_class(cls)
+		, m_caption_text(caption_text)
 	{}
 
 public:
@@ -450,6 +454,7 @@
 	virtual bool Create(WindowNumber num);
 	virtual bool Create(const WindowDesc *desc, WindowNumber num = 0);
 
+	/*virtual*/ void CreateNcWidgets();
 	/*virtual*/ void CreateWidgets(); ///< @TODO remove it when old gui infrastructure will no longer be used
 
 	static BaseWindow* Allocate(int x, int y, int width, int height, WindowProc *proc, WindowClass cls, const OldWidget *widget);