00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CATListBox.h"
00016 #include "CATWindow.h"
00017 #include "CATEvent.h"
00018
00019 #include "CATEventDefs.h"
00020
00021 CATListBox::~CATListBox()
00022 {
00023 while (fList.size())
00024 {
00025 delete fList[0];
00026 fList.erase(fList.begin());
00027 }
00028 }
00029
00030 void CATListBox::SetFocused(bool focused)
00031 {
00032 if ((focused == false) && (this->fFocused))
00033 {
00034 CATString oldParam = fCmdParam;
00035
00036 this->OSGetText(fCmdParam);
00037
00038 if (oldParam.Compare(fCmdParam) != 0)
00039 {
00040 ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00041 }
00042 }
00043
00044 CATControlWnd::SetFocused(focused);
00045 }
00046
00047 void CATListBox::OnEscapeChange()
00048 {
00049 this->SetCurSel(fCurSel);
00050 this->fValue = (CATFloat32)fCurSel;
00051 this->MarkDirty();
00052 }
00053
00054 CATInt32 CATListBox::GetCount() const
00055 {
00056 return this->fList.size();
00057 }
00058
00059 CATInt32 CATListBox::GetCurIndex() const
00060 {
00061 return this->fCurSel;
00062 }
00063
00064 CATResult CATListBox::Insert( const CATString& displayText,
00065 void* dataPtr,
00066 CATInt32 index)
00067 {
00068 CATResult result = CAT_SUCCESS;
00069
00070
00071
00072 CATLISTINFO* listInfo = new CATLISTINFO;
00073 listInfo->DisplayText = displayText;
00074 listInfo->ListInfo = dataPtr;
00075 listInfo->BaseListBox = this;
00076
00077 if (index == -1)
00078 index = this->fList.size();
00079
00080 std::vector<CATLISTINFO*>::iterator iter = fList.begin();
00081 iter += index;
00082
00083 iter = this->fList.insert(iter,listInfo);
00084
00085 fMaxValue++;
00086 return OSAddItem(index,displayText,listInfo);
00087 }
00088
00089 CATResult CATListBox::Remove( CATInt32 index)
00090 {
00091 if (index >= (CATInt32)fList.size())
00092 return CAT_ERR_INVALID_PARAM;
00093
00094 std::vector<CATLISTINFO*>::iterator iter = fList.begin();
00095 iter += index;
00096 delete (*iter);
00097 fList.erase(iter);
00098
00099 fMaxValue--;
00100
00101 return OSRemoveItem(index);
00102 }
00103
00104 CATResult CATListBox::RemoveByName( const CATString& displayText)
00105 {
00106 CATResult result = CAT_SUCCESS;
00107 CATLISTINFO* listInfo = 0;
00108
00109 for (CATUInt32 i = 0; i < fList.size(); i++)
00110 {
00111 listInfo = fList[i];
00112 if (listInfo->DisplayText.Compare(displayText) == 0)
00113 {
00114 return this->Remove(i);
00115 }
00116 }
00117
00118 return CATRESULT(CAT_ERR_LIST_ITEM_NOT_FOUND);
00119 }
00120
00121
00122 CATResult CATListBox::Get( CATInt32 index,
00123 CATString& displayTextRef,
00124 void*& dataRef) const
00125 {
00126 CATLISTINFO* listInfo = fList[index];
00127 displayTextRef = listInfo->DisplayText;
00128 dataRef = listInfo->ListInfo;
00129 return CAT_SUCCESS;
00130 }
00131
00132 CATResult CATListBox::GetByName( const CATString& displayTextRef,
00133 void*& dataRef) const
00134 {
00135 CATResult result = CAT_SUCCESS;
00136 CATLISTINFO* listInfo = 0;
00137
00138 for (CATUInt32 i = 0; i < fList.size(); i++)
00139 {
00140 listInfo = fList[i];
00141 if (listInfo->DisplayText.Compare(displayTextRef) == 0)
00142 {
00143 dataRef = listInfo->ListInfo;
00144 return CAT_SUCCESS;
00145 }
00146 }
00147
00148 return CATResult(CAT_ERR_LIST_ITEM_NOT_FOUND);
00149 }
00150
00151 CATResult CATListBox::Clear()
00152 {
00153 while (fList.size())
00154 {
00155 delete fList[0];
00156 fList.erase(fList.begin());
00157 }
00158
00159 OSClearList();
00160 fMaxValue = -1;
00161 return CAT_SUCCESS;
00162 }
00163
00164
00165 CATResult CATListBox::SetCurSel( CATInt32 index)
00166 {
00167 if ((index >= -1) && (index < (CATInt32)fList.size()))
00168 {
00169 this->fCurSel = index;
00170 this->fValue = (CATFloat32)fCurSel;
00171 return OSSetCurSel(index);
00172 }
00173 else
00174 {
00175 fCurSel = -1;
00176 this->fValue = (CATFloat32)fCurSel;
00177 return OSSetCurSel(-1);
00178 }
00179 }
00180
00181
00182 CATResult CATListBox::SetCurSelByName( const CATString& displayText)
00183 {
00184 CATResult result = CAT_SUCCESS;
00185 CATLISTINFO* listInfo = 0;
00186
00187 for (CATUInt32 i = 0; i < fList.size(); i++)
00188 {
00189 listInfo = fList[i];
00190 if (listInfo->DisplayText.Compare(displayText) == 0)
00191 {
00192 return this->SetCurSel(i);
00193 }
00194 }
00195
00196 return CATRESULT(CAT_ERR_LIST_ITEM_NOT_FOUND);
00197 }
00198
00199
00200
00201 CATResult CATListBox::OnEvent(const CATEvent& event, CATInt32& retVal)
00202 {
00203 CATResult result = CAT_SUCCESS;
00204
00205
00206
00207
00208 if ((event.fStringParam2.IsEmpty()) || (event.fStringParam2.Compare(this->fName) == 0))
00209 {
00210 switch (event.fEventCode)
00211 {
00212
00213
00214
00215
00216
00217
00218
00219
00220 case CATEVENT_LISTBOX_ADD:
00221 return this->Insert(event.fStringParam1,event.fVoidParam,event.fIntParam1);
00222
00223
00224
00225
00226
00227
00228
00229 case CATEVENT_LISTBOX_REMOVE_INDEX:
00230 return this->Remove(event.fIntParam1);
00231
00232
00233
00234
00235
00236
00237
00238 case CATEVENT_LISTBOX_REMOVE_STRING:
00239 return this->RemoveByName(event.fStringParam1);
00240
00241
00242
00243
00244
00245
00246
00247 case CATEVENT_LISTBOX_SET_SEL:
00248 return this->SetCurSel(event.fIntParam1);
00249
00250
00251
00252
00253
00254 case CATEVENT_LISTBOX_SET_SEL_STRING:
00255 return this->SetCurSelByName(event.fStringParam1);
00256
00257
00258
00259
00260
00261
00262 case CATEVENT_LISTBOX_CLEAR:
00263 return this->Clear();
00264
00265
00266
00267
00268
00269
00270 case CATEVENT_LISTBOX_GET_SEL:
00271 retVal = this->GetCurIndex();
00272 return CAT_SUCCESS;
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 case CATEVENT_LISTBOX_GET_SEL_DATA:
00283
00284 if ((event.fVoidParam != 0) && (this->fCurSel >= 0) && (this->fCurSel < this->GetCount()))
00285 {
00286 CATString dummyText;
00287 return this->Get(this->fCurSel, dummyText, *((void**)event.fVoidParam));
00288 }
00289 return CATRESULT(CAT_ERR_LIST_OUT_OF_RANGE);
00290 }
00291 }
00292
00293
00294 return OSEvent(event,retVal);
00295 }
00296
00297
00298 CATString CATListBox::GetHint() const
00299 {
00300 CATString retString;
00301 retString = CATControl::GetHint();
00302 if (fShowHintValue)
00303 {
00304 if (this->GetCurIndex() != -1)
00305 {
00306 retString << " ( " << this->GetText() << " )";
00307 }
00308 }
00309 return retString;
00310 }
00311
00312 CATString CATListBox::GetText( CATInt32 index) const
00313 {
00314 CATString retString;
00315
00316 if (index == -1)
00317 {
00318 index = this->fCurSel;
00319 }
00320
00321 if (index == -1)
00322 return retString;
00323
00324 void* data = 0;
00325 this->Get(index, retString, data);
00326
00327 return retString;
00328 }
00329
00330
00331
00332
00333 CATCommand CATListBox::GetCommand() const
00334 {
00335 return CATCommand(this->fCmdString, this->fValue, this->GetString(), this->fTarget, this->fCmdType);
00336 }
00337
00338 CATString CATListBox::GetString() const
00339 {
00340 return this->GetText();
00341 }