⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wg_button.cpp

📁 一个小巧的嵌入式图形系统wGUI, 可以用VC编译
💻 CPP
字号:
// wg_button.cpp//// CButton class implementation////// Copyright (c) 2002 Rob Wiskow// rob-dev@boxedchaos.com//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//#include "wgui_include_config.h"#include "wg_button.h"#include "wg_application.h"#include "wg_message_server.h"#include <algorithm>namespace wGui{CButton::CButton(const CRect& WindowRect, CWindow* pParent, std::string sText, CFontEngine* pFontEngine) :	CWindow(WindowRect, pParent),	m_eButtonState(UP),	m_MouseButton(0){  m_sClassName = "CButton";	m_sWindowText = sText;	if (pFontEngine)	{		m_pFontEngine = pFontEngine;	}	else	{		m_pFontEngine = CApplication::Instance()->GetDefaultFontEngine();	}	std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(		m_pFontEngine, sText, CRenderedString::VALIGN_CENTER, CRenderedString::HALIGN_CENTER));	m_pRenderedString = pRenderedString;	m_BGColor = DEFAULT_FG_COLOR;	CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP);}CButton::~CButton(void){}void CButton::SetButtonState(EState eState){	if (m_eButtonState != eState)	{		m_eButtonState = eState;		StartDrawProc();	}}void CButton::Draw(void) const{	CWindow::Draw();	CPoint FontCenterPoint = m_WindowRect.Center();	CRect SubRect(m_WindowRect);	SubRect.Grow(-1);	CPainter Painter(m_pSDLSurface);	Painter.DrawRect(m_WindowRect, false, COLOR_BLACK);	CRGBColor FontColor = DEFAULT_LINE_COLOR;	switch (m_eButtonState)	{	case UP:		Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);		Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Bottom(), COLOR_DARKGRAY);		Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Right(), COLOR_DARKGRAY);		break;	case DOWN:		Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);		Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Top(), COLOR_DARKGRAY);		Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Left(), COLOR_DARKGRAY);		FontCenterPoint = FontCenterPoint + CPoint(1, 1);		break;	case DISABLED:		FontColor = DEFAULT_DISABLED_LINE_COLOR;		break;	default:		break;	}	SubRect.Grow(-2);	m_pRenderedString->Draw(m_pSDLSurface, SubRect, FontCenterPoint, FontColor);}void CButton::SetWindowText(const std::string& sWindowText){	CWindow::SetWindowText(sWindowText);	std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(		m_pFontEngine, sWindowText, CRenderedString::VALIGN_CENTER, CRenderedString::HALIGN_CENTER));	m_pRenderedString = pRenderedString;	StartDrawProc();}bool CButton::OnMouseButtonDown(CPoint Point, unsigned int Button){	bool bResult = false; 	if (! CWindow::OnMouseButtonDown(Point, Button) && m_bVisible && (m_WindowRect.HitTest(Point) == CRect::RELPOS_INSIDE) &&		(m_eButtonState == UP))	{		SetButtonState(DOWN);		m_MouseButton = Button;		bResult = true;	}	return bResult;}bool CButton::OnMouseButtonUp(CPoint Point, unsigned int Button){	bool bResult = false;	if (! CWindow::OnMouseButtonUp(Point, Button) && m_bVisible && (m_WindowRect.HitTest(Point) == CRect::RELPOS_INSIDE) &&		(m_eButtonState == DOWN) && (m_MouseButton == Button))	{		CMessage::EMessageType MessageType =  CMessage::UNKNOWN;		switch (m_MouseButton)		{		case CMouseMessage::LEFT:			MessageType = CMessage::CTRL_LCLICK;			break;		case CMouseMessage::RIGHT:			MessageType = CMessage::CTRL_RCLICK;			break;		case CMouseMessage::MIDDLE:			MessageType = CMessage::CTRL_MCLICK;			break;		}		CMessageServer::Instance().QueueMessage(new TIntMessage(MessageType, m_pParentWindow, this, 0));		bResult = true;	}	return bResult;}bool CButton::HandleMessage(CMessage* pMessage){	bool bHandled = false;	if (pMessage)	{		switch(pMessage->MessageType())		{		case CMessage::MOUSE_BUTTONUP:		{			CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);			if (pMouseMessage && m_eButtonState == DOWN)			{				SetButtonState(UP);				bHandled = true;			}			break;		}		default :			bHandled = CWindow::HandleMessage(pMessage);			break;		}	}	return bHandled;}CPictureButton::CPictureButton(const CRect& WindowRect, CWindow* pParent, std::string sPictureFile) :	CButton(WindowRect, pParent, sPictureFile, 0){  m_sClassName = "CPictureButton";	std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapFileResourceHandle(sPictureFile));	m_phBitmap = phBitmap;}CPictureButton::CPictureButton(const CRect& WindowRect, CWindow* pParent, const CBitmapResourceHandle& hBitmap) :	CButton(WindowRect, pParent, "<bitmap>", 0){  m_sClassName = "CPictureButton";	std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapResourceHandle(hBitmap));	m_phBitmap = phBitmap;}CPictureButton::~CPictureButton(void){}void CPictureButton::SetPicture(std::string sPictureFile){	SetPicture(CBitmapFileResourceHandle(sPictureFile));}void CPictureButton::SetPicture(const CBitmapResourceHandle& hBitmap){	std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapResourceHandle(hBitmap));	m_phBitmap = phBitmap;	StartDrawProc();}void CPictureButton::Draw(void) const{	CWindow::Draw();	CRect SubRect(m_WindowRect);	SubRect.Grow(-1);	CPainter Painter(m_pSDLSurface);	Painter.DrawRect(m_WindowRect, false, COLOR_BLACK);	switch (m_eButtonState)	{	case UP:		Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);		Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Bottom(), COLOR_DARKGRAY);		Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Right(), COLOR_DARKGRAY);		break;	case DOWN:		Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);		Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Top(), COLOR_DARKGRAY);		Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Left(), COLOR_DARKGRAY);		SubRect = SubRect + CPoint(1, 1);		break;	case DISABLED:		break;	default:		break;	}	SubRect.Grow(-1);	SDL_Rect SourceRect;	SourceRect.x = static_cast<short int>((m_phBitmap->Bitmap()->w - SubRect.Width()) / 2 < 0 ? 0 : (m_phBitmap->Bitmap()->w - SubRect.Width()) / 2);	SourceRect.y = static_cast<short int>((m_phBitmap->Bitmap()->h - SubRect.Height()) / 2 < 0 ? 0 : (m_phBitmap->Bitmap()->w - SubRect.Height()) / 2);	SourceRect.w = static_cast<short int>(std::min(SubRect.Width(), m_phBitmap->Bitmap()->w));	SourceRect.h = static_cast<short int>(std::min(SubRect.Height(), m_phBitmap->Bitmap()->h));	SDL_Rect DestRect;	DestRect.x = static_cast<short int>((SubRect.Width() - m_phBitmap->Bitmap()->w) / 2 < 0 ? SubRect.Left() : SubRect.Left() + (SubRect.Width() - m_phBitmap->Bitmap()->w) / 2);	DestRect.y = static_cast<short int>((SubRect.Height() - m_phBitmap->Bitmap()->h) / 2 < 0 ? SubRect.Top() : SubRect.Top() + (SubRect.Height() - m_phBitmap->Bitmap()->h) / 2);	DestRect.w = static_cast<short int>(std::min(SubRect.Width(), m_phBitmap->Bitmap()->w));	DestRect.h = static_cast<short int>(std::min(SubRect.Height(), m_phBitmap->Bitmap()->h));	SDL_BlitSurface(m_phBitmap->Bitmap(), &SourceRect, m_pSDLSurface, &DestRect);}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -