src/widget/widget_button.cpp
author KUDr
Sat, 03 Mar 2007 16:11:18 +0000
branchcpp_gui
changeset 6282 c5b92f2d924f
parent 6271 0ad100a98853
child 6289 be3d8bd9fb02
permissions -rw-r--r--
(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.
/* $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"

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 &ev)
{
	/* start capturing mouse move events */
	m_ticket_pressed = CaptureEventsT(this, &Button::OnCapturePressed);

	/* push the button */
	m_pushed = true;
	Invalidate();

	ev.SetHandled();
}



}; // namespace gui