📄 wg_scrollbar.cpp
字号:
// wg_scrollbar.cpp//// CScrollBar 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_scrollbar.h"#include "wg_message_server.h"#include "wg_error.h"#include "wg_resources.h"namespace wGui{CScrollBar::CScrollBar(const CRect& WindowRect, CWindow* pParent, EScrollBarType ScrollBarType) : CWindow(WindowRect, pParent), m_ScrollBarType(ScrollBarType), m_iMin(0), m_iMax(100), m_iPosition(0), m_bDragging(false){ m_sClassName = "CScrollBar"; m_BGColor = DEFAULT_FG_COLOR; switch (m_ScrollBarType) { case VERTICAL: m_pBtnUpLeft = new CPictureButton( CRect(WindowRect.TopLeft(), CPoint(WindowRect.Right(), WindowRect.Top() + WindowRect.Width())), this, CwgBitmapResourceHandle(WGRES_UP_ARROW_BITMAP)); m_pBtnDownRight = new CPictureButton( CRect(CPoint(WindowRect.Left(), WindowRect.Bottom() - WindowRect.Width()), WindowRect.BottomRight()), this, CwgBitmapResourceHandle(WGRES_DOWN_ARROW_BITMAP)); m_ClientRect = CRect(0, m_pBtnUpLeft->GetWindowRect().Height() + 1, m_WindowRect.Width() - 1, m_WindowRect.Height() - m_pBtnUpLeft->GetWindowRect().Height() - 1); break; case HORIZONTAL: m_pBtnUpLeft = new CPictureButton( CRect(WindowRect.TopLeft(), CPoint(WindowRect.Left() + WindowRect.Height(), WindowRect.Bottom())), this, CwgBitmapResourceHandle(WGRES_LEFT_ARROW_BITMAP)); m_pBtnDownRight = new CPictureButton( CRect(CPoint(WindowRect.Right() - WindowRect.Height(), WindowRect.Top()), WindowRect.BottomRight()), this, CwgBitmapResourceHandle(WGRES_RIGHT_ARROW_BITMAP)); m_ClientRect = CRect(m_pBtnUpLeft->GetWindowRect().Width() + 1, 0, m_WindowRect.Width() - m_pBtnUpLeft->GetWindowRect().Width() - 1, m_WindowRect.Height() - 1); break; default: throw(Wg_Ex_App("CScrollBar::CScrollBar: Unrecognized ScrollBar Type.")); break; } m_ThumbRect = GetClientRect(); SetPosition(0); CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP); CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_MOVE); CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_LCLICK);}CScrollBar::~CScrollBar(void){}void CScrollBar::SetLimits(int iMin, int iMax){ m_iMin = iMin; m_iMax = iMax; StartDrawProc();}void CScrollBar::SetPosition(int iPosition){ m_iPosition = iPosition; if (m_iPosition < m_iMin) { m_iPosition = m_iMin; } if (m_iPosition > m_iMax) { m_iPosition = m_iMax; } switch (m_ScrollBarType) { case VERTICAL: { int yPos = static_cast<int>(GetClientRect().Top() + (GetClientRect().Height() - 16) * (static_cast<double>(m_iPosition - m_iMin) / (m_iMax - m_iMin)) - 1); m_ThumbRect.SetTop(yPos); m_ThumbRect.SetBottom(yPos + 16); break; } case HORIZONTAL: { int xPos = static_cast<int>(GetClientRect().Left() + (GetClientRect().Width() - 16) * (static_cast<double>(m_iPosition - m_iMin) / (m_iMax - m_iMin)) - 1); m_ThumbRect.SetLeft(xPos); m_ThumbRect.SetRight(xPos + 16); break; } default: throw(Wg_Ex_App("CScrollBar::SetPosition: Unrecognized ScrollBar Type.")); break; } StartDrawProc();}void CScrollBar::Draw(void) const{ CWindow::Draw(); CPainter Painter(m_pSDLSurface); Painter.DrawRect(m_ThumbRect, false, COLOR_BLACK); CRect SubRect(m_ThumbRect); SubRect.Grow(-1); Painter.DrawRect(SubRect, true, COLOR_LIGHTGRAY, DEFAULT_FG_COLOR); Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Bottom(), COLOR_DARKGRAY); Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Right(), COLOR_DARKGRAY);}void CScrollBar::SetWindowRect(const CRect& WindowRect){ CWindow::SetWindowRect(WindowRect); // Resposition the thumb rect and the button controls SetPosition(m_iPosition); switch (m_ScrollBarType) { case VERTICAL: { m_ThumbRect.SetLeft(m_WindowRect.Left()); m_ThumbRect.SetRight(m_WindowRect.Right()); m_pBtnUpLeft->SetWindowRect(CRect(m_WindowRect.TopLeft(), CPoint(m_WindowRect.Right(), m_WindowRect.Top() + m_WindowRect.Width()))); m_pBtnDownRight->SetWindowRect(CRect( CPoint(m_WindowRect.Left(), m_WindowRect.Bottom() - m_WindowRect.Width()), m_WindowRect.BottomRight())); m_ClientRect.SetTop(m_pBtnUpLeft->GetWindowRect().Height() + 1); m_ClientRect.SetBottom(m_WindowRect.Height() - m_pBtnUpLeft->GetWindowRect().Height() - 1); break; } case HORIZONTAL: { m_ThumbRect.SetTop(m_WindowRect.Top()); m_ThumbRect.SetBottom(m_WindowRect.Bottom()); m_pBtnUpLeft->SetWindowRect(CRect(m_WindowRect.TopLeft(), CPoint(m_WindowRect.Left() + m_WindowRect.Height(), m_WindowRect.Bottom()))); m_pBtnDownRight->SetWindowRect(CRect( CPoint(m_WindowRect.Right() - m_WindowRect.Height(), m_WindowRect.Top()), m_WindowRect.BottomRight())); m_ClientRect.SetLeft(m_pBtnUpLeft->GetWindowRect().Width() + 1); m_ClientRect.SetRight(m_WindowRect.Width() - m_pBtnUpLeft->GetWindowRect().Width() - 1); break; } default: throw(Wg_Ex_App("CScrollBar::SetWindowRect: Unrecognized ScrollBar Type.")); break; }}void CScrollBar::MoveWindow(const CPoint& MoveDistance){ CWindow::MoveWindow(MoveDistance); m_ThumbRect = m_ThumbRect + MoveDistance;}bool CScrollBar::OnMouseButtonDown(CPoint Point, unsigned int Button){ bool bResult = false; if (! CWindow::OnMouseButtonDown(Point, Button) && m_bVisible && (m_ThumbRect.HitTest(Point) == CRect::RELPOS_INSIDE) && (Button == CMouseMessage::LEFT)) { m_bDragging = true; bResult = true; } return bResult;}bool CScrollBar::HandleMessage(CMessage* pMessage){ bool bHandled = false; if (pMessage) { switch(pMessage->MessageType()) { case CMessage::MOUSE_BUTTONUP: { CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage); if (pMouseMessage && m_bDragging && pMouseMessage->Button == CMouseMessage::LEFT) { m_bDragging = false; CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, m_iPosition)); bHandled = true; } break; } case CMessage::MOUSE_MOVE: if (m_bDragging) { CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage); if (pMouseMessage) { int iOldPosition = m_iPosition; switch (m_ScrollBarType) { case VERTICAL: SetPosition((pMouseMessage->Point.YPos() - GetClientRect().Top()) * (m_iMax - m_iMin) / GetClientRect().Height() + m_iMin); break; case HORIZONTAL: SetPosition((pMouseMessage->Point.XPos() - GetClientRect().Left()) * (m_iMax - m_iMin) / GetClientRect().Width() + m_iMin); break; default: throw(Wg_Ex_App("CScrollBar::HandleMessage: Unrecognized ScrollBar Type.")); break; } if (iOldPosition != m_iPosition) { CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGING, m_pParentWindow, this, m_iPosition)); } } } break; case CMessage::CTRL_LCLICK: { if (pMessage->Destination() == this) { if (pMessage->Source() == m_pBtnUpLeft) { SetPosition(m_iPosition - 1); bHandled = true; } else if (pMessage->Source() == m_pBtnDownRight) { SetPosition(m_iPosition + 1); bHandled = true; } CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, m_iPosition)); } break; } default : bHandled = CWindow::HandleMessage(pMessage); break; } } return bHandled;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -