wg_dropdown.cpp

00001 // wg_dropdown.cpp
00002 //
00003 // CDropDown 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_dropdown.h"
00027 #include "wg_message_server.h"
00028 #include "wg_resources.h"
00029 #include "wg_view.h"
00030 
00031 
00032 namespace wGui
00033 {
00034 
00035 CDropDown::CDropDown(const CRect& WindowRect, CWindow* pParent, bool bAllowEdit, unsigned int iItemHeight, CFontEngine* pFontEngine) :
00036      CWindow(WindowRect, pParent),
00037      m_bAllowEdit(bAllowEdit)
00038 {
00039      m_pEditBox = new CEditBox(CRect(0, 0, m_WindowRect.Width() - m_WindowRect.Height(), m_WindowRect.Height()), this, pFontEngine);
00040      if (!m_bAllowEdit)
00041      {
00042           m_pEditBox->SetReadOnly(true);
00043           // Override the normal read-only BG color
00044           m_pEditBox->SetBackgroundColor(COLOR_WHITE);
00045      }
00046 
00047      m_pListBox = new CListBox(CRect(0, m_WindowRect.Height(), m_WindowRect.Width(), m_WindowRect.Height() + iItemHeight * 5 + 1),
00048           this, true, iItemHeight, pFontEngine);
00049      m_pListBox->SetVisible(false);
00050      m_pListBox->SetDropDown(this);
00051 
00052      m_pDropButton = new CPictureButton(
00053           CRect(m_WindowRect.Width() - m_WindowRect.Height() + 1, 0, m_WindowRect.Width(), m_WindowRect.Height()),
00054           this, CwgBitmapResourceHandle(WGRES_DOWN_ARROW_BITMAP));
00055      CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONDOWN);
00056      CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_SINGLELCLICK);
00057      CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_VALUECHANGE);
00058      Draw();
00059 }
00060 
00061 
00062 CDropDown::~CDropDown(void)
00063 {
00064 
00065 }
00066 
00067 
00068 void CDropDown::SetListboxHeight(int iItemCount)
00069 {
00070      m_pListBox->SetWindowRect(
00071           CRect(0, m_WindowRect.Height(), m_WindowRect.Width(), m_WindowRect.Height() + m_pListBox->GetItemHeight() * iItemCount + 1));
00072 }
00073 
00074 
00075 void CDropDown::SetWindowRect(const CRect& WindowRect)
00076 {
00077      CWindow::SetWindowRect(WindowRect);
00078      m_pListBox->SetWindowRect(CRect(0, m_WindowRect.Height(), m_WindowRect.Width(), m_WindowRect.Height() + m_pListBox->GetItemHeight() * 5 + 1));
00079      m_pDropButton->SetWindowRect(CRect(m_WindowRect.Width() - m_WindowRect.Height() + 1, 0, m_WindowRect.Width(), m_WindowRect.Height()));
00080      m_pEditBox->SetWindowRect(CRect(0, 0, m_WindowRect.Width() - m_WindowRect.Height(), m_WindowRect.Height()));
00081 }
00082 
00083 
00084 void CDropDown::SetWindowText(std::wstring sWindowText)
00085 {
00086      m_pEditBox->SetWindowText(sWindowText);
00087 }
00088 
00089 
00090 std::wstring CDropDown::GetWindowText()
00091 {
00092      return m_pEditBox->GetWindowText();
00093 }
00094 
00095 
00096 void CDropDown::MoveWindow(const CPoint& MoveDistance)
00097 {
00098      CWindow::MoveWindow(MoveDistance);
00099      m_pListBox->MoveWindow(MoveDistance);
00100 }
00101 
00102 
00103 void CDropDown::SetVisible(bool bVisible)
00104 {
00105      // if the visibility mode of the control changes, we should go ahead and hide the listbox
00106      HideListBox();
00107      CWindow::SetVisible(bVisible);
00108 }
00109 
00110 
00111 bool CDropDown::HandleMessage(CMessage* pMessage)
00112 {
00113      bool bHandled = false;
00114      CRect SubRect(m_WindowRect);
00115      SubRect.Grow(-3);
00116 
00117      if (pMessage)
00118      {
00119           switch(pMessage->MessageType())
00120           {
00121           case CMessage::MOUSE_BUTTONDOWN:
00122           {
00123                CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);
00124                if (pMouseMessage->Button == CMouseMessage::LEFT)
00125                {
00126                     if (m_pListBox->IsVisible() &&
00127                          m_pDropButton->GetWindowRect().SizeRect().HitTest(m_pDropButton->ViewToWindow(pMouseMessage->Point)) != CRect::RELPOS_INSIDE &&
00128                          m_pListBox->GetWindowRect().SizeRect().HitTest(m_pListBox->ViewToWindow(pMouseMessage->Point)) != CRect::RELPOS_INSIDE)
00129                     {
00130                          HideListBox();
00131                     }
00132                }
00133                break;
00134           }
00135           case CMessage::CTRL_SINGLELCLICK:
00136           {
00137                if (pMessage->Destination() == this)
00138                {
00139                     if (pMessage->Source() == m_pDropButton)
00140                     {
00141                          if (m_pListBox->IsVisible())
00142                          {
00143                               HideListBox();
00144                          }
00145                          else
00146                          {
00147                               ShowListBox();
00148                          }
00149                          bHandled = true;
00150                     }
00151                }
00152                break;
00153           }
00154           case CMessage::CTRL_VALUECHANGE:
00155           {
00156                TIntMessage* pCtrlMessage = dynamic_cast<TIntMessage*>(pMessage);
00157                if (pCtrlMessage && pMessage->Destination() == this)
00158                {
00159                     if (pCtrlMessage->Source() == m_pListBox)
00160                     {
00161                          const SListItem& ListItem = m_pListBox->GetItem(pCtrlMessage->Value());
00162                          SetWindowText(ListItem.sItemText);
00163                          HideListBox();
00164                          CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 0));
00165                          bHandled = true;
00166                     }
00167                     else if (pCtrlMessage->Source() == m_pEditBox)
00168                     {
00169                          m_pListBox->SetAllSelections(false);
00170                          HideListBox();
00171                          CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 0));
00172                          bHandled = true;
00173                     }
00174                }
00175                break;
00176           }
00177           default :
00178                bHandled = CWindow::HandleMessage(pMessage);
00179                break;
00180           }
00181      }
00182 
00183      return bHandled;
00184 }
00185 
00186 
00187 void CDropDown::ShowListBox(void)
00188 {
00189      if (!m_pListBox->IsVisible())
00190      {
00191           CView* pView = GetView();
00192           if (pView)
00193           {
00194                pView->SetFloatingWindow(m_pListBox);
00195           }
00196           m_pListBox->SetVisible(true);
00197      }
00198 }
00199 
00200 
00201 void CDropDown::HideListBox(void)
00202 {
00203      if (m_pListBox->IsVisible())
00204      {
00205           m_pListBox->SetVisible(false);
00206           CView* pView = GetView();
00207           if (pView && pView->GetFloatingWindow() == m_pListBox)
00208           {
00209                pView->SetFloatingWindow(0);
00210           }
00211      }
00212 }
00213 
00214 }
00215 
00216 

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