wg_messagebox.cpp

00001 // wg_messagebox.cpp
00002 //
00003 // CMessageBox 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_messagebox.h"
00027 
00028 namespace wGui
00029 {
00030 
00031 CMessageBox::CMessageBox(CView* pParent, CFontEngine* pFontEngine, const std::wstring& sTitle, const std::wstring& sMessage, int iButtons) :
00032      CFrame(CRect(20, 20, 320, 100), pParent, pFontEngine, sTitle),
00033      m_iButtons(iButtons)
00034 {
00035      m_pMessageLabel = new CLabel(CRect(10, 10, 290, 26), this, sMessage);
00036      CPoint BottomRight(290, 56);
00037      if (iButtons & CMessageBox::BUTTON_CANCEL)
00038      {
00039           m_ButtonMap.insert(std::make_pair(CMessageBox::BUTTON_CANCEL, new CButton(CRect(BottomRight - CPoint(50, 18), BottomRight), this, L"Cancel")));
00040           BottomRight = BottomRight - CPoint(60, 0);
00041      }
00042      if (iButtons & CMessageBox::BUTTON_OK)
00043      {
00044           m_ButtonMap.insert(std::make_pair(CMessageBox::BUTTON_OK, new CButton(CRect(BottomRight - CPoint(50, 18), BottomRight), this, L"Ok")));
00045           BottomRight = BottomRight - CPoint(60, 0);
00046      }
00047      if (iButtons & CMessageBox::BUTTON_NO)
00048      {
00049           m_ButtonMap.insert(std::make_pair(CMessageBox::BUTTON_NO, new CButton(CRect(BottomRight - CPoint(50, 18), BottomRight), this, L"No")));
00050           BottomRight = BottomRight - CPoint(60, 0);
00051      }
00052      if (iButtons & CMessageBox::BUTTON_YES)
00053      {
00054           m_ButtonMap.insert(std::make_pair(CMessageBox::BUTTON_YES, new CButton(CRect(BottomRight - CPoint(50, 18), BottomRight), this, L"Yes")));
00055           BottomRight = BottomRight - CPoint(60, 0);
00056      }
00057      FitToText();
00058 }
00059 
00060 
00061 void CMessageBox::FitToText(void)
00062 {
00063      CPoint TextRect;
00064      m_pMessageLabel->GetRenderedString()->GetMetrics(&TextRect);
00065      CRect LabelRect = m_pMessageLabel->GetWindowRect();
00066      CPoint Offset(TextRect.XPos() - LabelRect.Width(), TextRect.YPos() - LabelRect.Height());
00067      m_pMessageLabel->SetWindowRect(CRect(10, 10, 10 + TextRect.XPos(), 10 + TextRect.YPos()));
00068      SetWindowRect(CRect(m_WindowRect.TopLeft(), m_WindowRect.BottomRight() + Offset));
00069      for (std::map<EButton, CButton*>::iterator iter = m_ButtonMap.begin(); iter != m_ButtonMap.end(); ++iter)
00070      {
00071           iter->second->SetWindowRect(iter->second->GetWindowRect() + Offset);
00072      }
00073 }
00074 
00075 
00076 bool CMessageBox::HandleMessage(CMessage* pMessage)
00077 {
00078      bool bHandled = false;
00079 
00080      if (pMessage)
00081      {
00082           switch(pMessage->MessageType())
00083           {
00084           case CMessage::CTRL_SINGLELCLICK:
00085           {
00086                if (pMessage->Destination() == this)
00087                {
00088                     for (std::map<EButton, CButton*>::iterator iter = m_ButtonMap.begin(); iter != m_ButtonMap.end(); ++iter)
00089                     {
00090                          if (pMessage->Source() == iter->second)
00091                          {
00092                               CMessageServer::Instance().QueueMessage(new CValueMessage<CMessageBox::EButton>(CMessage::CTRL_MESSAGEBOXRETURN, m_pParentWindow, 0, iter->first));
00093                               CloseFrame();
00094                               bHandled = true;
00095                               break;
00096                          }
00097                     }
00098                }
00099                break;
00100           }
00101           default:
00102                break;
00103           }
00104           if (!bHandled)
00105           {
00106                bHandled = CFrame::HandleMessage(pMessage);
00107           }
00108      }
00109 
00110      return bHandled;
00111 }
00112 
00113 }

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