src/widget/widget_caption.cpp
author KUDr
Sat, 21 Apr 2007 08:23:57 +0000
branchcpp_gui
changeset 6308 646711c5feaa
parent 6301 e0251f797d59
permissions -rw-r--r--
(svn r9708) [cpp_gui] -Sync with trunk (r9633:9707)
/* $Id$ */

#include "../stdafx.h"
#include <stdarg.h>
#include "../openttd.h"
#include "../debug.h"
#include "../functions.h"
#include "../map.h"
#include "../player.h"
#include "../window.h"
#include "../gfx.h"
#include "../viewport.h"
#include "../console.h"
#include "../variables.h"
#include "../table/sprites.h"
#include "../genworld.h"
#include "../helpers.hpp"
#include "window_events.hpp"
#include "widget_types.h"


namespace gui {

struct CloseBox : public Button {
	typedef Button super;

	static const int16 DEFAULT_WIDTH  = 11;
	static const int16 DEFAULT_HEIGHT = 14;
	static FeatureFlags DEFAULT_FEATURES() {return FF_MIN_SIZE | FF_IGNORE_PARENT_FRAME;}

public:
	CloseBox(CompositeWidget *container)
		: Button(container, DEFAULT_FEATURES(), STR_018B_CLOSE_WINDOW, COLOUR_PARENT)
	{
		m_min_size = m_max_size = Size16(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	///*virtual*/ void OnCreate(EvtCreate &ev)
	//{
	//	// move itself to the left of the parent
	//	super::OnCreate(ev);
	//}

	/*virtual*/ void OnPaint(EvtPaint &ev)
	{
		assert(GetSize() == Point16(DEFAULT_WIDTH, DEFAULT_HEIGHT));

		DrawBackground(ev);
		DrawString(2, 2, STR_00C5, 0);
	}

	/*virtual*/ void OnLeftClick(EvtLeftClick &ev)
	{
		BaseWindow *w = GetWindow();
		assert(w != NULL);
		w->Close();
	}
};

struct StickyBox : public ImageButton2 {
	typedef ImageButton2 super;

	static const int16 DEFAULT_WIDTH  = 12;
	static const int16 DEFAULT_HEIGHT = 14;
	static const FeatureFlags DEFAULT_FEATURES() {return FF_TOGGLE_BUTTON | FF_MIN_SIZE;}

public:
	StickyBox(CompositeWidget *container)
		: ImageButton2(container, SPR_PIN_DOWN, Point(0, 1), SPR_PIN_UP, Point(0, 1), DEFAULT_FEATURES(), STR_STICKY_BUTTON, COLOUR_PARENT)
	{
		m_min_size = m_max_size = Size16(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/*virtual*/ void OnCreate(EvtCreate &ev)
	{
		// move itself to the right side of the parent
		super::OnCreate(ev);
	}

	/*virtual*/ void OnLeftClick(EvtLeftClick &ev)
	{
		BaseWindow *w = GetWindow();
		assert(w != NULL);

		/* set or reset FF_STICKED according to our 'pushed' state */
		w->m_feature_flags = (w->m_feature_flags & ~FF_STICKED) | (m_pushed ? FF_STICKED : FF_NONE);
	}

};

struct Caption : public Widget {
	typedef Widget super;

	static const FeatureFlags DEFAULT_FEATURES = FF_MIN_HEIGHT;

protected:
	Point16       m_moving_offset;
	CaptureTicket m_ticket_moving;

public:
	Caption()
		: Widget()
	{}

	Caption(CompositeWidget *container, FeatureFlags feature_flags = DEFAULT_FEATURES, StringID tooltips = STR_018C_WINDOW_TITLE_DRAG_THIS, uint16 color = COLOUR_PARENT)
		: Widget(container, feature_flags, tooltips, color)
	{}

	/*virtual*/ void QuerySizes()
	{
		BaseWindow *w = GetWindow();
		assert(w != NULL);

		static Point16 border_size(6, 4);
		m_min_size = GetStringSize(w->m_caption_text) + border_size;
	}

	/*virtual*/ void DrawBackground(EvtPaint &ev)
	{
		BaseWindow *w = GetWindow();
		assert(w != NULL);

		byte bk_color = GetBkColor();
		byte caption_color = w->caption_color;
		assert(Height() == 14); // XXX - to ensure the same sizes are used everywhere!
		DrawFrameRect(bk_color, FR_BORDERONLY);
		DrawFrameRect(1, 1, Width() - 2, Height() - 2, bk_color, (caption_color == 0xFF) ? FR_LOWERED | FR_DARKENED : FR_LOWERED | FR_DARKENED | FR_BORDERONLY);
		if (caption_color != 0xFF) {
			GfxFillRect(2, 2, Width() - 3, Height() - 3, _colour_gradient[_player_colors[caption_color]][4]);
		}
	}

	/*virtual*/ void OnPaint(EvtPaint &ev)
	{
		BaseWindow *w = GetWindow();
		assert(w != NULL);

		DrawBackground(ev);
		DrawStringCenteredTruncated(2, Width() - 3, 2, w->m_caption_text, 0x84);
		ev.SetHandled();
	}

	/*virtual*/ void 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 = GetTopLeftInWindow() + ev.m_pt;
		ev.SetHandled();
		m_ticket_moving = CaptureEventsT(this, &Caption::OnCaptureMoving);
		Invalidate();
	}

	void OnCaptureMoving(EvtMouseOver &e)
	{
		if (!_left_button_down) {
			m_ticket_moving.Release();

			EvtLeftClick ev(Point(0, 0));
			ev.m_widget = this;
			CallHandlers(ev);
			return;
		}
		GetWindow()->SetTopLeft(e.m_pt - m_moving_offset);
		e.SetHandled();
		Invalidate();
	}
};


/*virtual*/ void CaptionBar::CreateWidgets()
{
	BaseWindow *w = GetWindow();
	assert(w != NULL);

	bool make_close_box  = ((m_feature_flags & FF_NO_CLOSE_BOX ) == FF_NONE);
	bool make_sticky_box = ((m_feature_flags & FF_NO_STICKY_BOX) == FF_NONE);

	/* add close box */
	if (make_close_box ) AddWidget(new CloseBox (this), Panel::LEFT);
	/* add sticky box */
	if (make_sticky_box) AddWidget(new StickyBox(this), Panel::RIGHT);
	/* add caption */
	AddWidget(new Caption(this), Panel::CENTER);
}





}; // namespace gui