00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00028
00029
00030
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
00043
00044 CATComboBox::~CATComboBox()
00045 {
00046 }
00047
00048
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(
00064 CATPROGRESSCB progressCB,
00065 void* progressParam,
00066 CATFloat32 progMin,
00067 CATFloat32 progMax)
00068 {
00069 CATResult result = CATLayer::Load(progressCB, progressParam, progMin, progMax);
00070
00071
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
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
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
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
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 }