(svn r8996) [cpp_gui] -Add: CloseBox added into CaptionBar cpp_gui
authorKUDr
Sat, 03 Mar 2007 16:11:18 +0000
branchcpp_gui
changeset 6282 c5b92f2d924f
parent 6281 2ae707873e23
child 6283 7072ee68c676
(svn r8996) [cpp_gui] -Add: CloseBox added into CaptionBar
-Add: when pressing gui button and holding the mouse button down you can now move mouse out of the button and it will get released silently. So if you release mouse button after leaving gui button the button will not raise OnLeftClick() event.
-Fix: OnCreate() is now propagated properly to base classes
-Codechange: old OnLeftClick() is now OnLeftButtonDown() (same with OnRightClick() and OnRightButtonDown()). New OnLeftClick() is now raised only by button when released.
src/intro_gui.cpp
src/widget/widget.h
src/widget/widget_base.cpp
src/widget/widget_button.cpp
src/widget/widget_caption.cpp
src/widget/widget_closebox.cpp
src/widget/widget_composite.cpp
src/widget/widget_resizebox.cpp
src/widget/window_event_base.h
src/widget/window_events.hpp
src/window.cpp
--- a/src/intro_gui.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/intro_gui.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -194,7 +194,7 @@
 //		m_button1->AddOnClickHandlerT(&WindowT::Button1_OnClick);
 	}
 
-	void Button1_OnClick(gui::EvtClick &e)
+	void Button1_OnClick(gui::EvtLeftClick &e)
 	{
 		/* move window randomly */
 		SetTopLeft(TopLeft() + Point16(rand() % 21 - 10, rand() % 21 - 10));
--- a/src/widget/widget.h	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget.h	Sat Mar 03 16:11:18 2007 +0000
@@ -130,8 +130,9 @@
 	virtual void OnDestroy(EvtDestroy &ev) {};
 	virtual void OnPaint(EvtPaint &ev) = 0;
 	virtual void OnKeyPress(EvtKeyPress &ev) {};
-	virtual void OnLeftClick(EvtClick &ev);
-	virtual void OnRightClick(EvtRightClick &ev);
+	virtual void OnLeftButtonDown(EvtLeftButtonDown &ev) {};
+	virtual void OnLeftClick(EvtLeftClick &ev);
+	virtual void OnRightButtonDown(EvtRightButtonDown &ev);
 	virtual void OnMouseOver(EvtMouseOver &ev) {};
 	virtual void OnResize(EvtResize &ev) {};
 	virtual void OnResizeParent(EvtResizeParent &ev);
@@ -147,7 +148,7 @@
 	 * For example if you are implementing 'MyWindow' class and want to handle OnLeftClick()
 	 * event for your button 'MyButton', define your handler method inside 'MyWindow' class
 	 * like:
-	 *       void MyWindow::MyButtonClickHandler(gui::EvtClick &e)
+	 *       void MyWindow::MyButtonClickHandler(gui::EvtLeftClick &e)
 	 *       {
 	 *          ...
 	 *       }
@@ -200,8 +201,8 @@
 
 	/*virtual*/ void OnCreate(EvtCreate &ev);
 	/*virtual*/ void OnPaint(EvtPaint &ev);
-	/*virtual*/ void OnLeftClick(EvtClick &ev);
-	/*virtual*/ void OnRightClick(EvtRightClick &ev);
+	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
+	/*virtual*/ void OnRightButtonDown(EvtRightButtonDown &ev);
 	/*virtual*/ void OnResize(EvtResize &ev);
 };
 
@@ -227,7 +228,7 @@
 
 	/*virtual*/ void DrawBackground(EvtPaint &ev);
 
-	/*virtual*/ void OnLeftClick(EvtClick &ev);
+	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
 };
 
 struct TextButton : public Button {
@@ -249,33 +250,54 @@
 	/*virtual*/ void OnPaint(EvtPaint &ev);
 };
 
-struct ResizeBox : public Widget {
-	typedef Widget super;
+struct CloseBox : public Button {
+	typedef Button super;
 
 	static const int16 DEFAULT_WIDTH  = 11;
-	static const int16 DEFAULT_HEIGHT = 11;
+	static const int16 DEFAULT_HEIGHT = 14;
+
+public:
+	CloseBox()
+		: Button()
+	{}
+
+	CloseBox(CompositeWidget *container)
+		: Button(container, -102, 0, container->m_color, Rect16(), 0)
+	{}
+
+	/*virtual*/ void OnCreate(EvtCreate &ev);
+	/*virtual*/ void OnPaint(EvtPaint &ev);
+	/*virtual*/ void OnLeftClick(EvtLeftClick &ev);
+};
+
+struct Caption : public Widget {
+	typedef Widget super;
 
 protected:
-	Point16       m_sizing_offset;
-	CaptureTicket m_ticket_sizing;
+	StringID      m_text;
+	byte          m_text_color;
+	Point16       m_moving_offset;
+	CaptureTicket m_ticket_moving;
 
 public:
-	ResizeBox()
+	Caption()
 		: Widget()
+		, m_text(0)
+		, m_text_color(0xFF)
 	{}
 
-	ResizeBox(CompositeWidget *container)
-		: Widget(container, -101, 0, container->m_color, Rect16(), 0)
+	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)
 	{}
 
 	/*virtual*/ void DrawBackground(EvtPaint &ev);
-	void OnCaptureSizing(EvtMouseOver &e);
 
 	/*virtual*/ void OnCreate(EvtCreate &ev);
 	/*virtual*/ void OnPaint(EvtPaint &ev);
-	/*virtual*/ void OnLeftClick(EvtClick &ev);
-	/*virtual*/ void OnResizeParent(EvtResizeParent &ev);
-
+	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
+	void OnCaptureMoving(EvtMouseOver &e);
 };
 
 struct CaptionBar : public CompositeWidget {
@@ -303,12 +325,38 @@
 	{}
 
 	/*virtual*/ void CreateWidgets();
+
+	/*virtual*/ void OnCreate(EvtCreate &ev);
+};
+
+
+struct ResizeBox : public Widget {
+	typedef Widget super;
+
+	static const int16 DEFAULT_WIDTH  = 11;
+	static const int16 DEFAULT_HEIGHT = 11;
+
+protected:
+	Point16       m_sizing_offset;
+	CaptureTicket m_ticket_sizing;
+
+public:
+	ResizeBox()
+		: Widget()
+	{}
+
+	ResizeBox(CompositeWidget *container)
+		: Widget(container, -101, 0, container->m_color, Rect16(), 0)
+	{}
+
 	/*virtual*/ void DrawBackground(EvtPaint &ev);
+	void OnCaptureSizing(EvtMouseOver &e);
 
 	/*virtual*/ void OnCreate(EvtCreate &ev);
 	/*virtual*/ void OnPaint(EvtPaint &ev);
-	/*virtual*/ void OnLeftClick(EvtClick &ev);
-	void OnCaptureMoving(EvtMouseOver &e);
+	/*virtual*/ void OnLeftButtonDown(EvtLeftButtonDown &ev);
+	/*virtual*/ void OnResizeParent(EvtResizeParent &ev);
+
 };
 
 
--- a/src/widget/widget_base.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_base.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -223,11 +223,12 @@
 	m_is_closing = true;
 }
 
-/*virtual*/ void Widget::OnLeftClick(EvtClick &ev)
+/*virtual*/ void Widget::OnLeftClick(EvtLeftClick &ev)
 {
+	CallHandlers(ev);
 }
 
-/*virtual*/ void Widget::OnRightClick(EvtRightClick &ev)
+/*virtual*/ void Widget::OnRightButtonDown(EvtRightButtonDown &ev)
 {
 	if (m_tooltips != 0) {
 		GuiShowTooltips(m_tooltips);
--- a/src/widget/widget_button.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_button.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -21,16 +21,37 @@
 
 void Button::OnCapturePressed(EvtMouseOver &e)
 {
+	BaseWindow *w = GetWindow();
+	assert(w != NULL);
+
 	if (_left_button_down) {
+		/* translate the global coordinates to our window space */
+		Point16 pt_local = e.m_pt - w->TopLeft();
+		/* determine the new button push state (is cursor inside button?) */
+		bool pushed = (WidgetFromPt(pt_local) != NULL);
+		/* did the push state change */
+		if (pushed != m_pushed) {
+			/* update button push state */
+			m_pushed = pushed;
+			Invalidate();
+		}
 		e.SetHandled();
 		return;
 	}
-	m_pushed = false;
+	/* stop capturing mouse move events */
 	m_ticket_pressed.Release();
 
-	EvtClick ev(Point(0, 0));
-	ev.m_widget = this;
-	CallHandlers(ev);
+	/* Issue OnLeftClick() event only if the button was pushed */
+	if (m_pushed) {
+		/* release button */
+		m_pushed = false;
+		Invalidate();
+
+		/* issue click event */
+		EvtLeftClick ev(e.m_pt - w->TopLeft());
+		ev.m_widget = this;
+		OnLeftClick(ev);
+	}
 }
 
 /*virtual*/ void Button::DrawBackground(EvtPaint &ev)
@@ -38,13 +59,18 @@
 	DrawFrameRect(m_color, m_pushed ? FR_LOWERED : FR_NONE);
 }
 
-/*virtual*/ void Button::OnLeftClick(EvtClick &ev)
+/*virtual*/ void Button::OnLeftButtonDown(EvtLeftButtonDown &ev)
 {
+	/* start capturing mouse move events */
+	m_ticket_pressed = CaptureEventsT(this, &Button::OnCapturePressed);
+
+	/* push the button */
 	m_pushed = true;
+	Invalidate();
+
 	ev.SetHandled();
-	m_ticket_pressed = CaptureEventsT(this, &Button::OnCapturePressed);
-	Invalidate();
 }
 
 
+
 }; // namespace gui
--- a/src/widget/widget_caption.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_caption.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -3,6 +3,7 @@
 #include "../stdafx.h"
 #include <stdarg.h>
 #include "../openttd.h"
+#include "table/strings.h"
 #include "../debug.h"
 #include "../functions.h"
 #include "../map.h"
@@ -22,10 +23,24 @@
 
 /*virtual*/ void CaptionBar::CreateWidgets()
 {
+	/* add close box */
+	CloseBox *close_box = new CloseBox(this);
+	AddWidget(close_box);
 
+	/* 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);
+	AddWidget(caption);
 }
 
-/*virtual*/ void CaptionBar::DrawBackground(EvtPaint &ev)
+/*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();
 	assert(w != NULL);
@@ -39,32 +54,33 @@
 	}
 }
 
-/*virtual*/ void CaptionBar::OnCreate(EvtCreate &ev)
+/*virtual*/ void Caption::OnCreate(EvtCreate &ev)
 {
 	SetAnchors(PIN_LEFT | PIN_TOP | PIN_RIGHT);
+	super::OnCreate(ev);
 }
 
-/*virtual*/ void CaptionBar::OnPaint(EvtPaint &ev)
+/*virtual*/ void Caption::OnPaint(EvtPaint &ev)
 {
 	DrawBackground(ev);
 	DrawStringCenteredTruncated(Left() + 2, Right() - 2, Top() + 2, m_text, m_text_color);
 	ev.SetHandled();
 }
 
-/*virtual*/ void CaptionBar::OnLeftClick(EvtClick &ev)
+/*virtual*/ void Caption::OnLeftButtonDown(EvtLeftButtonDown &ev)
 {
 	m_moving_offset = ev.m_pt;
 	ev.SetHandled();
-	m_ticket_moving = CaptureEventsT(this, &CaptionBar::OnCaptureMoving);
+	m_ticket_moving = CaptureEventsT(this, &Caption::OnCaptureMoving);
 	Invalidate();
 }
 
-void CaptionBar::OnCaptureMoving(EvtMouseOver &e)
+void Caption::OnCaptureMoving(EvtMouseOver &e)
 {
 	if (!_left_button_down) {
 		m_ticket_moving.Release();
 
-		EvtClick ev(Point(0, 0));
+		EvtLeftClick ev(Point(0, 0));
 		ev.m_widget = this;
 		CallHandlers(ev);
 		return;
--- a/src/widget/widget_closebox.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_closebox.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -3,6 +3,7 @@
 #include "../stdafx.h"
 #include <stdarg.h>
 #include "../openttd.h"
+#include "table/strings.h"
 #include "../debug.h"
 #include "../functions.h"
 #include "../map.h"
@@ -17,4 +18,35 @@
 #include "../helpers.hpp"
 #include "window_events.hpp"
 
+namespace gui {
 
+/*virtual*/ void CloseBox::OnCreate(EvtCreate &ev)
+{
+	Point16 size(DEFAULT_WIDTH, DEFAULT_HEIGHT);
+	// move itself to the left of the parent
+	SetTopLeft(m_container->TopLeft());
+	SetBottomRight(TopLeft() +  size - Point16(1, 1));
+
+	m_tooltips = STR_018B_CLOSE_WINDOW;
+
+	SetAnchors(PIN_LEFT | PIN_TOP);
+	super::OnCreate(ev);
+}
+
+/*virtual*/ void CloseBox::OnPaint(EvtPaint &ev)
+{
+	assert(Size() == Point16(DEFAULT_WIDTH, DEFAULT_HEIGHT));
+
+	DrawBackground(ev);
+	DrawString(Left() + 2, Top() + 2, STR_00C5, 0);
+}
+
+/*virtual*/ void CloseBox::OnLeftClick(EvtLeftClick &ev)
+{
+	BaseWindow *w = GetWindow();
+	assert(w != NULL);
+	w->Close();
+}
+
+}; // namespace gui
+
--- a/src/widget/widget_composite.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_composite.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -115,26 +115,26 @@
 	}
 }
 
-/*virtual*/ void CompositeWidget::OnLeftClick(EvtClick &ev)
+/*virtual*/ void CompositeWidget::OnLeftButtonDown(EvtLeftButtonDown &ev)
 {
 	//Point16 pt(ev->we.click.pt.x, ev->we.click.pt.y);
 	Widget *wd_child = WidgetFromPt(ev.m_pt);
 	if (wd_child != NULL && wd_child != this) {
-		wd_child->OnLeftClick(ev);
+		wd_child->OnLeftButtonDown(ev);
 		return;
 	}
-	super::OnLeftClick(ev);
+	super::OnLeftButtonDown(ev);
 }
 
-/*virtual*/ void CompositeWidget::OnRightClick(EvtRightClick &ev)
+/*virtual*/ void CompositeWidget::OnRightButtonDown(EvtRightButtonDown &ev)
 {
 	//Point16 pt(ev->we.click.pt.x, ev->we.click.pt.y);
 	Widget *wd_child = WidgetFromPt(ev.m_pt);
 	if (wd_child != NULL && wd_child != this) {
-		wd_child->OnRightClick(ev);
+		wd_child->OnRightButtonDown(ev);
 		return;
 	}
-	super::OnRightClick(ev);
+	super::OnRightButtonDown(ev);
 }
 
 /*virtual*/ void CompositeWidget::OnResize(EvtResize &ev)
--- a/src/widget/widget_resizebox.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/widget_resizebox.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -31,7 +31,7 @@
 	if (!_left_button_down) {
 		m_ticket_sizing.Release();
 
-		EvtClick ev(Point(0, 0));
+		EvtLeftClick ev(Point(0, 0));
 		ev.m_widget = this;
 		CallHandlers(ev);
 		return;
@@ -52,11 +52,12 @@
 	m_tooltips = STR_RESIZE_BUTTON;
 
 	SetAnchors(PIN_RIGHT | PIN_BOTTOM);
+	super::OnCreate(ev);
 }
 
 /*virtual*/ void ResizeBox::OnPaint(EvtPaint &ev)
 {
-	assert(Size() == Point16(11, 11));
+	assert(Size() == Point16(DEFAULT_WIDTH, DEFAULT_HEIGHT));
 
 	DrawBackground(ev);
 	bool sizing = m_ticket_sizing.IsActive();
@@ -64,7 +65,7 @@
 	ev.SetHandled();
 }
 
-/*virtual*/ void ResizeBox::OnLeftClick(EvtClick &ev)
+/*virtual*/ void ResizeBox::OnLeftButtonDown(EvtLeftButtonDown &ev)
 {
 	BaseWindow *w = GetWindow();
 	m_sizing_offset = w->Size() - ev.m_pt;
--- a/src/widget/window_event_base.h	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/window_event_base.h	Sat Mar 03 16:11:18 2007 +0000
@@ -12,8 +12,9 @@
 	EVT_DESTROY,
 	EVT_PAINT,
 	EVT_KEYPRESS,
-	EVT_CLICK,
-	EVT_RCLICK,
+	EVT_LBUTTON_DOWN,
+	EVT_LEFT_CLICK,
+	EVT_RBUTTON_DOWN,
 	EVT_MOUSEOVER,
 	EVT_MOUSELOOP,
 	EVT_MOUSEWHEEL,
@@ -44,8 +45,9 @@
 typedef EventT<EVT_DESTROY            > EvtDestroy;
 typedef EventT<EVT_PAINT              > EvtPaint;
 typedef EventT<EVT_KEYPRESS           > EvtKeyPress;
-typedef EventT<EVT_CLICK              > EvtClick;
-typedef EventT<EVT_RCLICK             > EvtRightClick;
+typedef EventT<EVT_LBUTTON_DOWN       > EvtLeftButtonDown;
+typedef EventT<EVT_LEFT_CLICK         > EvtLeftClick;
+typedef EventT<EVT_RBUTTON_DOWN       > EvtRightButtonDown;
 typedef EventT<EVT_MOUSEOVER          > EvtMouseOver;
 typedef EventT<EVT_MOUSELOOP          > EvtMouseLoop;
 typedef EventT<EVT_MOUSEWHEEL         > EvtMouseWheel;
--- a/src/widget/window_events.hpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/widget/window_events.hpp	Sat Mar 03 16:11:18 2007 +0000
@@ -27,7 +27,7 @@
 	{}
 };
 
-template <> struct EventT<EVT_CLICK> : public EventBaseT<EVT_CLICK, &Widget::OnLeftClick> {
+template <> struct EventT<EVT_LBUTTON_DOWN> : public EventBaseT<EVT_LBUTTON_DOWN, &Widget::OnLeftButtonDown> {
 	Point m_pt;
 
 	EventT(const PointRaw &pt)
@@ -35,7 +35,15 @@
 	{}
 };
 
-template <> struct EventT<EVT_RCLICK> : public EventBaseT<EVT_RCLICK, &Widget::OnRightClick> {
+template <> struct EventT<EVT_LEFT_CLICK> : public EventBaseT<EVT_LEFT_CLICK, &Widget::OnLeftClick> {
+	Point m_pt;
+
+	EventT(const PointRaw &pt)
+		: m_pt(pt)
+	{}
+};
+
+template <> struct EventT<EVT_RBUTTON_DOWN> : public EventBaseT<EVT_RBUTTON_DOWN, &Widget::OnRightButtonDown> {
 	Point m_pt;
 
 	EventT(const PointRaw &pt)
--- a/src/window.cpp	Sat Mar 03 11:50:45 2007 +0000
+++ b/src/window.cpp	Sat Mar 03 16:11:18 2007 +0000
@@ -1039,11 +1039,11 @@
 			break;
 
 		case WE_CLICK:
-			evt = new gui::EventT<gui::EVT_CLICK>(e->we.click.pt);
+			evt = new gui::EventT<gui::EVT_LBUTTON_DOWN>(e->we.click.pt);
 			break;
 
 		case WE_RCLICK:
-			evt = new gui::EventT<gui::EVT_RCLICK>(e->we.click.pt);
+			evt = new gui::EventT<gui::EVT_RBUTTON_DOWN>(e->we.click.pt);
 			break;
 
 		default: