src/widget/widget_caption.cpp
author KUDr
Sat, 03 Mar 2007 20:25:37 +0000
branchcpp_gui
changeset 6283 7072ee68c676
parent 6282 c5b92f2d924f
child 6289 be3d8bd9fb02
permissions -rw-r--r--
(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.
/* $Id$ */

#include "../stdafx.h"
#include <stdarg.h>
#include "../openttd.h"
#include "table/strings.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"


namespace gui {

/*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 */
	if (make_close_box) {
		CloseBox *close_box = new CloseBox(this);
		AddWidget(close_box);
		rc_caption.SetLeft(rc_caption.Left() + 11);
	}

	/* add caption */
	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 Caption::DrawBackground(EvtPaint &ev)
{
	BaseWindow *w = GetWindow();
	assert(w != NULL);

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

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

	DrawBackground(ev);
	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);
	Invalidate();
}

void Caption::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();
}


}; // namespace gui