wg_button.cpp

00001 // wg_button.cpp
00002 //
00003 // CButton 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 "wutil_debug.h"
00027 #include "wg_button.h"
00028 #include "wg_application.h"
00029 #include "wg_message_server.h"
00030 #include "std_ex.h"
00031 #include <algorithm>
00032 
00033 
00034 namespace wGui
00035 {
00036 
00037 CButton::CButton(const CRect& WindowRect, CWindow* pParent, std::wstring sText, CFontEngine* pFontEngine) :
00038      CWindow(WindowRect, pParent),
00039      m_eButtonState(UP),
00040      m_MouseButton(0)
00041 {
00042      m_sWindowText = sText;
00043      if (pFontEngine)
00044      {
00045           m_pFontEngine = pFontEngine;
00046      }
00047      else
00048      {
00049           m_pFontEngine = CApplication::Instance()->GetDefaultFontEngine();
00050      }
00051      std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(
00052           m_pFontEngine, sText, CRenderedString::VALIGN_CENTER, CRenderedString::HALIGN_CENTER));
00053      m_pRenderedString = pRenderedString;
00054      m_BackgroundColor = CApplication::Instance()->GetDefaultForegroundColor();
00055      CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONUP);
00056      Draw();
00057 }
00058 
00059 
00060 CButton::~CButton(void)
00061 {
00062 
00063 }
00064 
00065 
00066 void CButton::SetButtonState(EState eState)
00067 {
00068      if (m_eButtonState != eState)
00069      {
00070           m_eButtonState = eState;
00071           Draw();
00072      }
00073 }
00074 
00075 
00076 void CButton::Draw(void) const
00077 {
00078      CWindow::Draw();
00079 
00080      if (m_pSDLSurface)
00081      {
00082           CPoint FontCenterPoint = m_WindowRect.SizeRect().Center();
00083           CRect SubRect(m_WindowRect.SizeRect());
00084           SubRect.Grow(-1);
00085           CPainter Painter(m_pSDLSurface, CPainter::PAINT_REPLACE);
00086           Painter.DrawRect(m_WindowRect.SizeRect(), false, COLOR_BLACK);
00087           CRGBColor FontColor = DEFAULT_LINE_COLOR;
00088           switch (m_eButtonState)
00089           {
00090           case UP:
00091                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00092                Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Bottom(), COLOR_DARKGRAY);
00093                Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Right(), COLOR_DARKGRAY);
00094                break;
00095           case DOWN:
00096                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00097                Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Top(), COLOR_DARKGRAY);
00098                Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Left(), COLOR_DARKGRAY);
00099                FontCenterPoint = FontCenterPoint + CPoint(1, 1);
00100                break;
00101           case DISABLED:
00102                FontColor = DEFAULT_DISABLED_LINE_COLOR;
00103                break;
00104           default:
00105                break;
00106           }
00107           SubRect.Grow(-2);
00108           if (m_pRenderedString.get())
00109           {
00110                m_pRenderedString->Draw(m_pSDLSurface, SubRect, FontCenterPoint, FontColor);
00111           }
00112      }
00113 }
00114 
00115 
00116 void CButton::SetWindowText(const std::wstring& sWindowText)
00117 {
00118      std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(
00119           m_pFontEngine, sWindowText, CRenderedString::VALIGN_CENTER, CRenderedString::HALIGN_CENTER));
00120      m_pRenderedString = pRenderedString;
00121      CWindow::SetWindowText(sWindowText);
00122 }
00123 
00124 
00125 bool CButton::OnMouseButtonDown(CPoint Point, unsigned int Button)
00126 {
00127      bool bResult = CWindow::OnMouseButtonDown(Point, Button);
00128 
00129      if (!bResult && m_bVisible && (m_eButtonState == UP) && (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00130      {
00131           SetButtonState(DOWN);
00132           m_MouseButton = Button;
00133           bResult = true;
00134      }
00135 
00136      return bResult;
00137 }
00138 
00139 
00140 bool CButton::OnMouseButtonUp(CPoint Point, unsigned int Button)
00141 {
00142      bool bResult = CWindow::OnMouseButtonUp(Point, Button);
00143 
00144      if (!bResult && m_bVisible && (m_eButtonState == DOWN) &&
00145           (m_MouseButton == Button) && (m_ClientRect.HitTest(ViewToWindow(Point)) == CRect::RELPOS_INSIDE))
00146      {
00147           SetButtonState(UP);
00148           CMessage::EMessageType MessageType =  CMessage::UNKNOWN;
00149           switch (m_MouseButton)
00150           {
00151           case CMouseMessage::LEFT:
00152                MessageType = CMessage::CTRL_SINGLELCLICK;
00153                break;
00154           case CMouseMessage::RIGHT:
00155                MessageType = CMessage::CTRL_SINGLERCLICK;
00156                break;
00157           case CMouseMessage::MIDDLE:
00158                MessageType = CMessage::CTRL_SINGLEMCLICK;
00159                break;
00160           }
00161           CMessageServer::Instance().QueueMessage(new TIntMessage(MessageType, m_pParentWindow, this, 0));
00162           bResult = true;
00163      }
00164 
00165      return bResult;
00166 }
00167 
00168 
00169 bool CButton::HandleMessage(CMessage* pMessage)
00170 {
00171      bool bHandled = false;
00172 
00173      if (pMessage)
00174      {
00175           switch(pMessage->MessageType())
00176           {
00177           case CMessage::MOUSE_BUTTONUP:
00178           {
00179                CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);
00180                if (pMouseMessage && m_eButtonState == DOWN)
00181                {
00182                     SetButtonState(UP);
00183                     bHandled = true;
00184                }
00185                break;
00186           }
00187           default :
00188                bHandled = CWindow::HandleMessage(pMessage);
00189                break;
00190           }
00191      }
00192 
00193      return bHandled;
00194 }
00195 
00196 
00197 CPictureButton::CPictureButton(const CRect& WindowRect, CWindow* pParent, std::wstring sPictureFile) :
00198      CButton(WindowRect, pParent, sPictureFile, 0)
00199 {
00200      std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapFileResourceHandle(sPictureFile));
00201      m_phBitmap = phBitmap;
00202      Draw();
00203 }
00204 
00205 
00206 CPictureButton::CPictureButton(const CRect& WindowRect, CWindow* pParent, const CBitmapResourceHandle& hBitmap) :
00207      CButton(WindowRect, pParent, L"<bitmap>", 0)
00208 {
00209      std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapResourceHandle(hBitmap));
00210      m_phBitmap = phBitmap;
00211      Draw();
00212 }
00213 
00214 
00215 CPictureButton::~CPictureButton(void)
00216 {
00217 
00218 }
00219 
00220 
00221 void CPictureButton::SetPicture(std::wstring sPictureFile)
00222 {
00223      SetPicture(CBitmapFileResourceHandle(sPictureFile));
00224 }
00225 
00226 
00227 void CPictureButton::SetPicture(const CBitmapResourceHandle& hBitmap)
00228 {
00229      std::auto_ptr<CBitmapResourceHandle> phBitmap(new CBitmapResourceHandle(hBitmap));
00230      m_phBitmap = phBitmap;
00231      Draw();
00232 }
00233 
00234 
00235 void CPictureButton::Draw(void) const
00236 {
00237      CWindow::Draw();
00238 
00239      if (m_pSDLSurface)
00240      {
00241           CRect SubRect(m_WindowRect.SizeRect());
00242           SubRect.Grow(-1);
00243           CPainter Painter(m_pSDLSurface, CPainter::PAINT_REPLACE);
00244           Painter.DrawRect(m_WindowRect.SizeRect(), false, COLOR_BLACK);
00245           switch (m_eButtonState)
00246           {
00247           case UP:
00248                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00249                Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Bottom(), COLOR_DARKGRAY);
00250                Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Right(), COLOR_DARKGRAY);
00251                break;
00252           case DOWN:
00253                Painter.DrawRect(SubRect, false, COLOR_LIGHTGRAY);
00254                Painter.DrawHLine(SubRect.Left(), SubRect.Right(), SubRect.Top(), COLOR_DARKGRAY);
00255                Painter.DrawVLine(SubRect.Top(), SubRect.Bottom(), SubRect.Left(), COLOR_DARKGRAY);
00256                SubRect = SubRect + CPoint(1, 1);
00257                break;
00258           case DISABLED:
00259                break;
00260           default:
00261                break;
00262           }
00263           SubRect.Grow(-1);
00264           SDL_Rect SourceRect;
00265           SourceRect.x = stdex::safe_static_cast<short int>((m_phBitmap->Bitmap()->w - SubRect.Width()) / 2 < 0 ? 0 : (m_phBitmap->Bitmap()->w - SubRect.Width()) / 2);
00266           SourceRect.y = stdex::safe_static_cast<short int>((m_phBitmap->Bitmap()->h - SubRect.Height()) / 2 < 0 ? 0 : (m_phBitmap->Bitmap()->w - SubRect.Height()) / 2);
00267           SourceRect.w = stdex::safe_static_cast<short int>(std::min(SubRect.Width(), m_phBitmap->Bitmap()->w));
00268           SourceRect.h = stdex::safe_static_cast<short int>(std::min(SubRect.Height(), m_phBitmap->Bitmap()->h));
00269           SDL_Rect DestRect;
00270           DestRect.x = stdex::safe_static_cast<short int>((SubRect.Width() - m_phBitmap->Bitmap()->w) / 2 < 0 ? SubRect.Left() : SubRect.Left() + (SubRect.Width() - m_phBitmap->Bitmap()->w) / 2);
00271           DestRect.y = stdex::safe_static_cast<short int>((SubRect.Height() - m_phBitmap->Bitmap()->h) / 2 < 0 ? SubRect.Top() : SubRect.Top() + (SubRect.Height() - m_phBitmap->Bitmap()->h) / 2);
00272           DestRect.w = stdex::safe_static_cast<short int>(std::min(SubRect.Width(), m_phBitmap->Bitmap()->w));
00273           DestRect.h = stdex::safe_static_cast<short int>(std::min(SubRect.Height(), m_phBitmap->Bitmap()->h));
00274           SDL_BlitSurface(m_phBitmap->Bitmap(), &SourceRect, m_pSDLSurface, &DestRect);
00275      }
00276 }
00277 
00278 }
00279 
00280 

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