Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CATGUI/CATComboBox.cpp Source File

CATComboBox.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATComboBox.cpp
00003 /// \brief GUI Layer object - contains sub-controls
00004 /// \ingroup CATGUI
00005 /// 
00006 /// Copyright (c) 2003-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-23 01:43:25 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //
00014 //---------------------------------------------------------------------------
00015 #include "CATComboBox.h"
00016 #include "CATApp.h"
00017 #include "CATFileSystem.h"
00018 #include "CATStream.h"
00019 #include "CATEvent.h"
00020 #include "CATWindow.h"
00021 #include "CATEventDefs.h"
00022 
00023 #include "CATEditBox.h"
00024 #include "CATMenu.h"
00025 #include "CATPrefs.h"
00026 //---------------------------------------------------------------------------
00027 // CATComboBox constructor (inherited from CATGuiObj)
00028 // \param element - Type name (e.g. "Button", "Label", etc.)
00029 // \param attribs - attribute information for the window
00030 // \param parent - parent XML object (should be a "Window" element)
00031 //---------------------------------------------------------------------------
00032 CATComboBox::CATComboBox( const CATString&               element, 
00033                          const CATString&               rootDir)
00034                          : CATLayer(element,  rootDir)
00035 {   
00036     fMaxMenuLength = 10;
00037     fComboEdit     = 0;
00038     fComboMenu     = 0;   
00039 }
00040 
00041 //---------------------------------------------------------------------------
00042 // CATComboBox destructor
00043 //---------------------------------------------------------------------------
00044 CATComboBox::~CATComboBox()
00045 {
00046 }
00047 
00048 /// ParseAttributes() parses the known attributes for an object.
00049 CATResult CATComboBox::ParseAttributes()
00050 {
00051     CATResult result = CATLayer::ParseAttributes();
00052 
00053     CATString attrib = GetAttribute(L"HistoryMax");
00054     if (!attrib.IsEmpty())
00055         fMaxMenuLength = (CATUInt32)attrib;
00056 
00057     fPrefName = GetAttribute(L"PrefName");
00058 
00059     return result;
00060 }
00061 
00062 
00063 CATResult CATComboBox::Load(        // Optional progress callback information
00064                             CATPROGRESSCB               progressCB,
00065                             void*                           progressParam,
00066                             CATFloat32                      progMin,
00067                             CATFloat32                      progMax)
00068 {
00069     CATResult result = CATLayer::Load(progressCB, progressParam, progMin, progMax);
00070 
00071     // Set prefname if unset
00072     if (fPrefName.IsEmpty())
00073         fPrefName = this->fName + (CATString)"_ComboList";
00074 
00075     CATUInt32 numChildren = this->GetNumChildren();
00076     for (CATUInt32 i = 0; i < numChildren; i++)
00077     {
00078         CATGuiObj* childObj = (CATGuiObj*)GetChild(i);
00079         CATString  curElem  = childObj->GetType();
00080         if (curElem.Compare("Menu") == 0)
00081         {
00082             this->fComboMenu = (CATMenu*)childObj;
00083         }
00084         else if (curElem.Compare("EditBox") == 0)
00085         {
00086             this->fComboEdit = (CATEditBox*)childObj;
00087         }
00088     }   
00089 
00090     if (fComboMenu == 0)
00091         result = CATRESULT(CAT_ERR_COMBO_MENU_NOT_FOUND);
00092     if (fComboEdit == 0)
00093         result = CATRESULT(CAT_ERR_COMBO_EDIT_NOT_FOUND);
00094 
00095     return result;
00096 }
00097 
00098 void CATComboBox::OnParentCreate()
00099 {
00100     CATLayer::OnParentCreate();       
00101     this->RebuildCombo();   
00102 }
00103 
00104 void CATComboBox::OnParentDestroy()
00105 {
00106     CATLayer::OnParentDestroy();
00107 }
00108 
00109 // Process commands from children for combo.
00110 void CATComboBox::OnCommand( CATCommand& command,
00111                             CATControl* ctrl)
00112 {  
00113     CATString param = command.GetStringParam();
00114 
00115     if (ctrl == fComboMenu)
00116     {
00117         if (fComboEdit)
00118         {
00119             fComboEdit->SetString( param );
00120         }
00121 
00122         AddComboString(param);
00123         ctrl = fComboEdit;
00124     }
00125     else if (ctrl == fComboEdit)
00126     {
00127         AddComboString(param);      
00128     }
00129     else 
00130     {
00131         // Not either of our normal controls - just pass it through.
00132         CATLayer::OnCommand(command, ctrl);
00133         return;
00134     }
00135 
00136     if ((fParent) && (fComboEdit))
00137     {
00138         ((CATGuiObj*)fParent)->OnCommand( fComboEdit->GetCommand(), fComboEdit);
00139     }
00140 }
00141 
00142 void CATComboBox::AddComboString(const CATString& newString)
00143 {
00144     fMenuList.insert(fMenuList.begin(),newString);
00145 
00146     // Cut dupes
00147     std::vector<CATString>::iterator iter = fMenuList.begin();
00148     while (iter != fMenuList.end())
00149     {
00150         if ( (*iter).Compare(newString) == 0)
00151             iter = fMenuList.erase(iter);
00152         else
00153             ++iter;
00154     }
00155 
00156     // Cap to max
00157     while (fMenuList.size() > fMaxMenuLength)
00158     {
00159         iter = fMenuList.end();
00160         --iter;
00161         fMenuList.erase(iter);      
00162     }
00163 
00164 }
00165 
00166 void CATComboBox::RebuildCombo()
00167 {
00168     if (fComboMenu == 0)
00169         return;
00170 
00171     fComboMenu->Clear();
00172 
00173     CATUInt32 numEntries = fMenuList.size();
00174     for (CATUInt32 i = 0; i < numEntries; i++)
00175     {
00176         CATString curString = fMenuList[i];      
00177         CATMENUITEM* newItem = 0;
00178         fComboMenu->Insert(  curString, 0, newItem, 0);
00179     }
00180 }
00181 
00182 CATEditBox* CATComboBox::GetEditBox()
00183 {
00184     return this->fComboEdit;
00185 }
00186 
00187 CATMenu* CATComboBox::GetMenu()
00188 {
00189     return this->fComboMenu;
00190 }
00191 
00192 CATResult CATComboBox::OnEvent( const CATEvent& event, CATInt32& retVal)
00193 {
00194     switch (event.fEventCode)
00195     {
00196     case CATEVENT_PREF_CHANGED:
00197         if (event.fStringParam1.Compare(fPrefName) == 0)
00198         {
00199             this->RebuildCombo();
00200         }
00201         retVal = 1;
00202         break;
00203     }
00204 
00205     return CATLayer::OnEvent(event, retVal);
00206 }

Generated on Mon Feb 11 04:09:51 2008 for Game Accessibility Suite by doxygen 1.5.4