src/widget/widget_button.cpp
author KUDr
Sat, 10 Mar 2007 08:53:59 +0000
branchcpp_gui
changeset 6295 a88d8c2cff6e
parent 6289 be3d8bd9fb02
child 6301 e0251f797d59
permissions -rw-r--r--
(svn r9091) [cpp_gui] -Codechange: Widgets now use container's coordinate space instead of window's space
/* $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 {

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;
	}
	/* stop capturing mouse move events */
	m_ticket_pressed.Release();

	/* 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)
{
	DrawFrameRect(m_color, m_pushed ? FR_LOWERED : FR_NONE);
}

/*virtual*/ void Button::OnLeftButtonDown(EvtLeftButtonDown &e)
{
	if ((m_feature_flags & FF_TOGGLE_BUTTON) != FF_NONE) {
		/* toggle button */
		m_pushed = !m_pushed;

		/* issue click event */
		EvtLeftClick ev(e.m_pt);
		ev.m_widget = this;
		OnLeftClick(ev);
	} else {
		/* push button */
		m_pushed = true;

		/* start capturing mouse move events */
		m_ticket_pressed = CaptureEventsT(this, &Button::OnCapturePressed);
	}
	Invalidate();
	e.SetHandled();
}



}; // namespace gui