📄 wg_frame.cpp
字号:
// wg_frame.cpp//// CFrame 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_frame.h"#include "wg_application.h"namespace wGui{CFrame::CFrame(const CRect& WindowRect, CView* pParent, CFontEngine* pFontEngine, std::string sTitle, bool bResizable) : CWindow(WindowRect, pParent), m_TitleBarColor(COLOR_BLUE), m_TitleBarTextColor(DEFAULT_LINE_COLOR), m_iTitleBarHeight(16), m_bResizable(bResizable), m_pMenu(0), m_bDragMode(false), m_pSavedSurface(0){ m_sClassName = "CFrame"; m_sWindowText = sTitle; m_pFrameCloseButton = new CPictureButton(CRect(0, 0, 12, 12), this, CwgBitmapResourceHandle(WGRES_X_BITMAP)); SetWindowRect(WindowRect); // must be done after the buttons are created if (pFontEngine) { m_pFontEngine = pFontEngine; } else { m_pFontEngine = CApplication::Instance()->GetDefaultFontEngine(); } std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(m_pFontEngine, m_sWindowText, CRenderedString::VALIGN_CENTER)); m_pRenderedString = pRenderedString; CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP); CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_MOVE); CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_LCLICK);}CFrame::~CFrame(void){ if (m_pSavedSurface) { SDL_FreeSurface(m_pSavedSurface); }}void CFrame::Draw(void) const{ CWindow::Draw(); CPainter Painter(m_pSDLSurface); Painter.DrawRect(m_WindowRect, false, COLOR_LIGHTGRAY); Painter.DrawHLine(m_WindowRect.Left(), m_WindowRect.Right(), m_WindowRect.Bottom(), COLOR_DARKGRAY); Painter.DrawVLine(m_WindowRect.Top(), m_WindowRect.Bottom(), m_WindowRect.Right(), COLOR_DARKGRAY); CRect SubRect(m_WindowRect); SubRect.Grow(-1); Painter.DrawRect(SubRect, false, COLOR_BLACK); Painter.DrawRect(m_TitleBarRect, true, m_TitleBarColor, m_TitleBarColor); CRect TextClipRect(m_TitleBarRect); TextClipRect.SetRight(TextClipRect.Right() - 16); TextClipRect.Grow(-1); m_pRenderedString->Draw(m_pSDLSurface, TextClipRect, m_TitleBarRect.TopLeft() + CPoint(6, m_iTitleBarHeight / 2), m_TitleBarTextColor);}void CFrame::SetTitleBarHeight(int iTitleBarHeight){ m_iTitleBarHeight = iTitleBarHeight; SetWindowRect(m_WindowRect);}void CFrame::AttachMenu(CMenu* pMenu){ delete m_pMenu; m_pMenu = pMenu; if (m_pMenu) { m_pMenu->SetWindowRect(CRect(0, 0, m_WindowRect.Width() - 1, m_pMenu->GetWindowRect().Height())); m_ClientRect.SetTop(m_pMenu->GetWindowRect().Height() + 1); m_ClientRect.ClipTo(m_WindowRect); } else { m_ClientRect = m_WindowRect; }}void CFrame::SetWindowRect(const CRect& WindowRect){ CWindow::SetWindowRect(WindowRect); m_TitleBarRect = CRect(2, 2, WindowRect.Width() - 3, m_iTitleBarHeight) + WindowRect.TopLeft(); m_pFrameCloseButton->SetWindowRect(CRect(WindowRect.Width() - 16, m_iTitleBarHeight / 2 - 5, WindowRect.Width() - 4, m_iTitleBarHeight / 2 + 7) + WindowRect.TopLeft()); m_ClientRect = CRect(2, m_iTitleBarHeight + 2, WindowRect.Height() - m_iTitleBarHeight - 1, WindowRect.Width() - 1);}void CFrame::MoveWindow(const CPoint& MoveDistance){ CWindow::MoveWindow(MoveDistance); m_TitleBarRect = m_TitleBarRect + MoveDistance;}void CFrame::SetWindowText(const std::string& sWindowText){ CWindow::SetWindowText(sWindowText); std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(m_pFontEngine, sWindowText, CRenderedString::VALIGN_CENTER)); m_pRenderedString = pRenderedString; StartDrawProc();}bool CFrame::OnMouseButtonDown(CPoint Point, unsigned int Button){ bool bResult = false; if (! CWindow::OnMouseButtonDown(Point, Button) && m_bVisible && (m_WindowRect.HitTest(Point) == CRect::RELPOS_INSIDE)) { if (m_TitleBarRect.HitTest(Point) == CRect::RELPOS_INSIDE) { m_bDragMode = true; m_DragPointerStart = Point; } SetNewParent(m_pParentWindow); // This moves the window to the top bResult = true; } return bResult;}bool CFrame::HandleMessage(CMessage* pMessage){ bool bHandled = false; if (pMessage) { switch(pMessage->MessageType()) { case CMessage::MOUSE_BUTTONUP: { CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage); if (pMouseMessage && m_bDragMode) { m_bDragMode = false; CPoint MoveDistance = pMouseMessage->Point - m_DragPointerStart; CRect Bounds = m_pParentWindow->GetClientRect(); if (m_WindowRect.Right() + MoveDistance.XPos() > Bounds.Right()) { MoveDistance.SetX(Bounds.Right() - m_WindowRect.Right()); } if (m_WindowRect.Left() + MoveDistance.XPos() < Bounds.Left()) { MoveDistance.SetX(Bounds.Left() - m_WindowRect.Left()); } if (m_WindowRect.Bottom() + MoveDistance.YPos() > Bounds.Bottom()) { MoveDistance.SetY(Bounds.Bottom() - m_WindowRect.Bottom()); } if (m_WindowRect.Top() + MoveDistance.YPos() < Bounds.Top()) { MoveDistance.SetY(Bounds.Top() - m_WindowRect.Top()); } MoveWindow(MoveDistance); if (m_pSavedSurface) { SDL_Rect SourceRect = m_SavedSurfaceRect.SDLRect(); SourceRect.x = 0; SourceRect.y = 0; SDL_Rect DestRect = m_SavedSurfaceRect.SDLRect(); SDL_BlitSurface(m_pSavedSurface, &SourceRect, m_pSDLSurface, &DestRect); SDL_FreeSurface(m_pSavedSurface); m_pSavedSurface = 0; } CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, 0, this)); bHandled = true; } break; } case CMessage::MOUSE_MOVE: { CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage); if (pMouseMessage && m_bDragMode) { if (m_pSavedSurface) { SDL_Rect SourceRect = m_SavedSurfaceRect.SDLRect(); SourceRect.x = 0; SourceRect.y = 0; SDL_Rect DestRect = m_SavedSurfaceRect.SDLRect(); SDL_BlitSurface(m_pSavedSurface, &SourceRect, m_pSDLSurface, &DestRect); SDL_UpdateRect(m_pSDLSurface, m_SavedSurfaceRect.Left(), m_SavedSurfaceRect.Top(), m_SavedSurfaceRect.Width(), m_SavedSurfaceRect.Height()); } else { m_pSavedSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, m_WindowRect.Width(), m_WindowRect.Height(), 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); } m_SavedSurfaceRect = m_WindowRect + pMouseMessage->Point - m_DragPointerStart; SDL_Rect DestRect = m_SavedSurfaceRect.SDLRect(); DestRect.x = 0; DestRect.y = 0; SDL_Rect SourceRect = m_SavedSurfaceRect.SDLRect(); SDL_BlitSurface(m_pSDLSurface, &SourceRect, m_pSavedSurface, &DestRect); CPainter Painter(m_pSDLSurface); Painter.DrawRect(m_SavedSurfaceRect, false, COLOR_LIGHTGRAY); SDL_UpdateRect(m_pSDLSurface, m_SavedSurfaceRect.Left(), m_SavedSurfaceRect.Top(), m_SavedSurfaceRect.Width(), m_SavedSurfaceRect.Height()); } break; } case CMessage::CTRL_LCLICK: { if (pMessage->Destination() == this) { if (pMessage->Source() == m_pFrameCloseButton) { // suicide the frame by detaching it from it's parent, queing up an APP_PAINT message, then deleting itself SetNewParent(0); CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, 0, this)); delete this; bHandled = true; } } break; } default : bHandled = CWindow::HandleMessage(pMessage); break; } } return bHandled;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -