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

📄 wg_window.cpp

📁 一个小巧的嵌入式图形系统wGUI, 可以用VC编译
💻 CPP
字号:
// wg_window.cpp//// CWindow 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_window.h"#include "std_ex.h"#include "wg_painter.h"#include "wg_application.h"#include "wg_error.h"#include "wg_debug.h"#include "wg_message_server.h"#include <algorithm>namespace wGui{CWindow::CWindow(const CRect& WindowRect, CWindow* pParent) :	m_sWindowText(""),	m_WindowRect(WindowRect),	m_BGColor(DEFAULT_BG_COLOR),	m_ClientRect(0, 0, WindowRect.Width() - 1, WindowRect.Height() - 1),	m_pParentWindow(0),	m_pSDLSurface(0),	m_bVisible(true),	m_sClassName("CWindow"){	if (!CApplication::Instance())	{		throw(Wg_Ex_App("CWindow Constructor: No Application instance!"));	}	SetNewParent(pParent);	CMessageServer::Instance().RegisterMessageClient(this, CMessage::APP_PAINT);}CWindow::~CWindow(void){	// Each child window is deleted, and should in their destructors call back to this object to Deregister themselves	CMessageServer::Instance().DeregisterMessageClient(this);	unsigned int iSanityCheck = static_cast<unsigned int>(m_ChildWindows.size());	while (m_ChildWindows.size() > 0 && iSanityCheck > 0)	{		delete *(m_ChildWindows.begin());		--iSanityCheck;	}	TraceIf(iSanityCheck != 0, "Sanity check failed in CWindow:" + m_sClassName + " destructor!  Sanity is: " + stdex::itoa(iSanityCheck));	TraceIf(!m_ChildWindows.empty(), "CWindow:" + m_sClassName + " children not empty at end of destructor!");	m_ChildWindows.clear();	SetNewParent(0);}void CWindow::SetWindowRect(const CRect& WindowRect){	m_WindowRect = WindowRect;	CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, GetAncestor(PARENT), this));}void CWindow::MoveWindow(const CPoint& MoveDistance){	m_WindowRect = m_WindowRect + MoveDistance;	m_ClientRect = m_ClientRect + MoveDistance;	for (std::list<CWindow*>::iterator iter = m_ChildWindows.begin(); iter != m_ChildWindows.end(); ++iter)	{		(*iter)->MoveWindow(MoveDistance);	}	CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, GetAncestor(PARENT), this));}CRect CWindow::GetClientRect(void) const{	CRect ClientRect(m_WindowRect.TopLeft() + m_ClientRect.TopLeft(), m_WindowRect.TopLeft() + m_ClientRect.BottomRight());	ClientRect.ClipTo(m_WindowRect);	return ClientRect;}CWindow* CWindow::GetAncestor(EAncestor eAncestor) const{	CWindow* pWindow = 0;	switch (eAncestor)	{	case PARENT:		pWindow = m_pParentWindow;		break;	case ROOT:		pWindow = m_pParentWindow;		if (pWindow)		{			while (pWindow->m_pParentWindow)			{				pWindow = pWindow->m_pParentWindow;			}		}		else		{			pWindow = const_cast<CWindow*>(this);		}		break;	}	return pWindow;}bool CWindow::IsChildOf(CWindow* pWindow) const{	const CWindow* pCurrentWindow = this;	bool bIsChild = false;	while (!bIsChild && pCurrentWindow)	{		pCurrentWindow = pCurrentWindow->GetAncestor(PARENT);		if (pCurrentWindow == pWindow)		{			bIsChild = true;		}	}	return bIsChild;}void CWindow::SetVisible(bool bVisible){	m_bVisible = bVisible;	for (std::list<CWindow*>::const_iterator iter = m_ChildWindows.begin(); iter != m_ChildWindows.end(); ++iter)	{		(*iter)->SetVisible(bVisible);		if ((*iter) == CApplication::Instance()->GetKeyFocus())		{			CApplication::Instance()->SetKeyFocus(0);		}	}	CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, GetAncestor(PARENT), this));}void CWindow::SetWindowText(const std::string& sText){	m_sWindowText = sText;	StartDrawProc();}void CWindow::DrawBG(void) const{	CPainter Painter(m_pSDLSurface);	Painter.DrawRect(m_WindowRect, true, m_BGColor, m_BGColor);}void CWindow::Draw(void) const{}void CWindow::DrawChildren(void) const{	for (std::list<CWindow*>::const_iterator iter = m_ChildWindows.begin(); iter != m_ChildWindows.end(); ++iter)	{		(*iter)->DrawProc();	}}void CWindow::DrawPostChild(void) const{}void CWindow::DrawProc(void) const{	if (m_bVisible)	{		DrawBG();		Draw();		DrawChildren();		DrawPostChild();	}}void CWindow::StartDrawProc(void) const{	if (m_bVisible)	{		DrawProc();		SDL_UpdateRect(m_pSDLSurface, m_WindowRect.Left(), m_WindowRect.Top(), m_WindowRect.Width(), m_WindowRect.Height());	}}void CWindow::SetNewParent(CWindow* pNewParent){	if (m_pParentWindow)	{		m_pParentWindow->DeregisterChildWindow(this);	}	if (pNewParent)	{		pNewParent->RegisterChildWindow(this);		m_pSDLSurface = pNewParent->GetAncestor(ROOT)->m_pSDLSurface;	}	m_pParentWindow = pNewParent;}bool CWindow::OnMouseButtonDown(CPoint Point, unsigned int Button){	bool bResult = false;	if (m_bVisible && (m_WindowRect.HitTest(Point) == CRect::RELPOS_INSIDE))	{		for (std::list<CWindow*>::reverse_iterator iter = m_ChildWindows.rbegin(); iter != m_ChildWindows.rend(); ++iter)		{			bResult = (*iter)->OnMouseButtonDown(Point, Button);			if (bResult)			{				break;			}		}	}	return bResult;}bool CWindow::OnMouseButtonUp(CPoint Point, unsigned int Button){	bool bResult = false;	if (m_bVisible && (m_WindowRect.HitTest(Point) == CRect::RELPOS_INSIDE))	{		for (std::list<CWindow*>::reverse_iterator iter = m_ChildWindows.rbegin(); iter != m_ChildWindows.rend(); ++iter)		{			bResult = (*iter)->OnMouseButtonUp(Point, Button);			if (bResult)			{				break;			}		}	}	return bResult;}void CWindow::RegisterChildWindow(CWindow* pWindow){	m_ChildWindows.push_back(pWindow);}void CWindow::DeregisterChildWindow(CWindow* pWindow){	m_ChildWindows.remove(pWindow);}bool CWindow::HandleMessage(CMessage* pMessage){	bool bHandled = false;	if (pMessage)	{		switch(pMessage->MessageType())		{		case CMessage::APP_PAINT :			if (pMessage->Destination() == this)			{				StartDrawProc();				bHandled = true;			}			break;		default :			break;		}	}	return bHandled;}}

⌨️ 快捷键说明

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