wg_checkbox.cpp

00001 // wg_checkbox.cpp
00002 //
00003 // CCheckBox class implementation
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 
00025 #include "wgui_include_config.h"
00026 #include "wg_checkbox.h"
00027 #include "wg_message_server.h"
00028 
00029 
00030 namespace wGui
00031 {
00032 
00033 CCheckBox::CCheckBox(const CRect& WindowRect, CWindow* pParent) :
00034      CWindow(WindowRect, pParent),
00035      m_eCheckBoxState(UNCHECKED),
00036      m_MouseButton(0)
00037 {
00038      m_BackgroundColor = COLOR_WHITE;
00039      CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP);
00040      CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_SINGLELCLICK);
00041      Draw();
00042 }
00043 
00044 
00045 CCheckBox::~CCheckBox(void)
00046 {
00047 
00048 }
00049 
00050 
00051 void CCheckBox::SetCheckBoxState(EState eState)
00052 {
00053      if (m_eCheckBoxState != eState)
00054      {
00055           m_eCheckBoxState = eState;
00056           Draw();
00057      }
00058 }
00059 
00060 
00061 void CCheckBox::Draw(void) const
00062 {
00063      CWindow::Draw();
00064 
00065      if (m_pSDLSurface)
00066      {
00067           CRect SubRect(m_WindowRect.SizeRect());
00068           SubRect.Grow(-1);
00069           CPainter Painter(m_pSDLSurface, CPainter::PAINT_REPLACE);
00070           Painter.DrawRect(m_WindowRect.SizeRect(), false, COLOR_BLACK);
00071           if (m_eCheckBoxState != DISABLED)
00072           {
00073                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00074                Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Top(), COLOR_DARKGRAY);
00075                Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Left(), COLOR_DARKGRAY);
00076                SubRect.Grow(-2);
00077                if (m_eCheckBoxState == CHECKED)
00078                {
00079                     Painter.DrawLine(SubRect.TopLeft(), SubRect.BottomRight(), DEFAULT_LINE_COLOR);
00080                     Painter.DrawLine(SubRect.BottomLeft(), SubRect.TopRight(), DEFAULT_LINE_COLOR);
00081                }
00082           }
00083      }
00084 }
00085 
00086 
00087 bool CCheckBox::OnMouseButtonDown(CPoint Point, unsigned int Button)
00088 {
00089      bool bResult = CWindow::OnMouseButtonDown(Point, Button);
00090 
00091      if (!bResult && m_bVisible && (m_eCheckBoxState != DISABLED) &&
00092           (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00093      {
00094           m_MouseButton = Button;
00095           bResult = true;
00096      }
00097 
00098      return bResult;
00099 }
00100 
00101 
00102 bool CCheckBox::OnMouseButtonUp(CPoint Point, unsigned int Button)
00103 {
00104      bool bResult = CWindow::OnMouseButtonUp(Point, Button);
00105 
00106      if (!bResult && m_bVisible && (m_eCheckBoxState != DISABLED) && (m_MouseButton == Button) &&
00107           (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00108      {
00109           CMessage::EMessageType MessageType =  CMessage::UNKNOWN;
00110           switch (m_MouseButton)
00111           {
00112           case CMouseMessage::LEFT:
00113                MessageType = CMessage::CTRL_SINGLELCLICK;
00114                break;
00115           case CMouseMessage::RIGHT:
00116                MessageType = CMessage::CTRL_SINGLERCLICK;
00117                break;
00118           case CMouseMessage::MIDDLE:
00119                MessageType = CMessage::CTRL_SINGLEMCLICK;
00120                break;
00121           }
00122           CMessageServer::Instance().QueueMessage(new TIntMessage(MessageType, this, this, 0));
00123           bResult = true;
00124      }
00125 
00126      return bResult;
00127 }
00128 
00129 
00130 bool CCheckBox::HandleMessage(CMessage* pMessage)
00131 {
00132      bool bHandled = false;
00133 
00134      if (pMessage)
00135      {
00136           switch(pMessage->MessageType())
00137           {
00138           case CMessage::MOUSE_BUTTONUP:
00139           {
00140                CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);
00141                if (pMouseMessage && (m_ClientRect.HitTest(ViewToWindow(pMouseMessage->Point)) != CRect::RELPOS_INSIDE)
00142                     && (m_MouseButton == pMouseMessage->Button))
00143                {
00144                     m_MouseButton = 0;
00145                     bHandled = true;
00146                }
00147                break;
00148           }
00149           case CMessage::CTRL_SINGLELCLICK:
00150                if (pMessage->Destination() == this)
00151                {
00152                     switch (m_eCheckBoxState)
00153                     {
00154                     case UNCHECKED:
00155                          SetCheckBoxState(CHECKED);
00156                          CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 1));
00157                          break;
00158                     case CHECKED:
00159                          SetCheckBoxState(UNCHECKED);
00160                          CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 0));
00161                          break;
00162                     default:
00163                          break;
00164                     }
00165                     bHandled = true;
00166                }
00167                break;
00168           default :
00169                bHandled = CWindow::HandleMessage(pMessage);
00170                break;
00171           }
00172      }
00173 
00174      return bHandled;
00175 }
00176 
00177 }
00178 
00179 
00180 

Generated on Wed May 16 23:11:25 2007 for wGui by  doxygen 1.5.1