wg_radiobutton.cpp

00001 // wg_radiobutton.cpp
00002 //
00003 // CRadioButton class implementation
00004 //
00005 //
00006 // Copyright (c) 2005-2006 Rob Wiskow, Jurgen De Backer
00007 // rob-dev@boxedchaos.com, jurgendb@users.sourceforge.net
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_radiobutton.h"
00027 #include "wg_message_server.h"
00028 #include "wg_painter.h"
00029 
00030 
00031 namespace wGui
00032 {
00033 
00034 // judb p is the upper-left corner of the radiobutton; size is the width=height
00035 CRadioButton::CRadioButton(const CRect& WindowRect, CWindow* pParent) :
00036      CWindow(WindowRect, pParent),
00037      m_eRadioButtonState(UNCHECKED),
00038      m_MouseButton(0)
00039 {
00040      m_BackgroundColor = COLOR_WHITE;   
00041      CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP);
00042      CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_SINGLELCLICK);
00043      Draw();
00044 }
00045 
00046 
00047 void CRadioButton::SetState(EState eState)
00048 {
00049      if (m_eRadioButtonState != eState)
00050      {
00051           m_eRadioButtonState = eState;
00052           Draw();
00053      }
00054 }
00055 
00056 
00057 void CRadioButton::Draw(void) const
00058 {
00059      CWindow::Draw();
00060      if (m_pSDLSurface)
00061      {
00062           CRect SubRect(m_WindowRect.SizeRect());
00063           CPainter Painter(m_pSDLSurface, CPainter::PAINT_REPLACE);
00064           if (m_eRadioButtonState != DISABLED)
00065           {
00066                Painter.DrawRect(SubRect, false, COLOR_DARKGRAY);
00067                if (m_eRadioButtonState == CHECKED)
00068                {
00069                     SubRect.Grow(-3);
00070                     Painter.DrawRect(SubRect, true, COLOR_BLACK, COLOR_BLACK);
00071                }
00072           }
00073           else
00074           {
00075                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00076           }
00077      }
00078 }
00079 
00080 
00081 bool CRadioButton::OnMouseButtonDown(CPoint Point, unsigned
00082 int Button)
00083 {
00084      bool bResult = CWindow::OnMouseButtonDown(Point, Button);
00085      if (!bResult && m_bVisible && (m_eRadioButtonState != DISABLED) &&
00086           (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00087      {
00088           m_MouseButton = Button;
00089           bResult = true;
00090      }
00091      return bResult;
00092 }
00093 
00094 
00095 bool CRadioButton::OnMouseButtonUp(CPoint Point, unsigned int Button)
00096 {
00097      bool bResult = CWindow::OnMouseButtonUp(Point, Button);
00098      if (!bResult && m_bVisible && (m_eRadioButtonState != DISABLED) &&
00099           (m_MouseButton == Button) && (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00100      {
00101           CMessage::EMessageType MessageType =  CMessage::UNKNOWN;
00102           switch (m_MouseButton)
00103           {
00104           case CMouseMessage::LEFT:
00105                MessageType = CMessage::CTRL_SINGLELCLICK;
00106                break;
00107           case CMouseMessage::RIGHT:
00108                MessageType = CMessage::CTRL_SINGLERCLICK;
00109                break;
00110           case CMouseMessage::MIDDLE:
00111                MessageType = CMessage::CTRL_SINGLEMCLICK;
00112                break;
00113           }
00114           CMessageServer::Instance().QueueMessage(new TIntMessage(MessageType, this, this, 0));
00115           bResult = true;
00116      }
00117      return bResult;
00118 }
00119 
00120 
00121 bool CRadioButton::HandleMessage(CMessage* pMessage)
00122 {
00123      bool bHandled = false;
00124 
00125      if (pMessage)
00126      {
00127           switch(pMessage->MessageType())
00128           {
00129           case CMessage::MOUSE_BUTTONUP:
00130           {
00131                CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);
00132                if (pMouseMessage && (m_ClientRect.HitTest(ViewToWindow(pMouseMessage->Point)) !=
00133                     CRect::RELPOS_INSIDE) && (m_MouseButton == pMouseMessage->Button))
00134                {
00135                     m_MouseButton = 0;
00136                     bHandled = true;
00137                }
00138                break;
00139           }
00140           case CMessage::CTRL_SINGLELCLICK:
00141                if (pMessage->Destination() == this)
00142                {
00143                     if (m_eRadioButtonState == UNCHECKED)
00144                     {
00145                          SetState(CHECKED);
00146                          // Uncheck all other 'children' of this parent that are radiobuttons:
00147                          std::list<CWindow*> ChildWindows = m_pParentWindow->GetChildWindows();
00148                          for (std::list<CWindow*>::iterator iter = ChildWindows.begin(); iter != ChildWindows.end(); ++iter)
00149                          {
00150                               // Compare the types to find out if a child is a CRadioButton.
00151                               CRadioButton* pRadioButton = dynamic_cast<CRadioButton*>(*iter);
00152                               if (pRadioButton && *iter != this)
00153                               {
00154                                    pRadioButton->SetState(UNCHECKED);
00155                               }
00156                          }                               
00157                          CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 1));
00158                          break;
00159                     }
00160                bHandled = true;
00161                }
00162                break;
00163           default :
00164                bHandled = CWindow::HandleMessage(pMessage);
00165                break;
00166           }
00167      }
00168      
00169      return bHandled;
00170 }
00171 
00172 }
00173 

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