00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "CATTreeCtrl.h"
00018 #include "CATWindow.h"
00019 #include "CATEvent.h"
00020
00021 #include "CATEventDefs.h"
00022
00023 CATTreeCtrl::~CATTreeCtrl()
00024 {
00025 Clear();
00026 }
00027
00028
00029 void CATTreeCtrl::SetFocused(bool focused)
00030 {
00031 if ((focused == false) && (this->fFocused))
00032 {
00033 CATString oldParam = fCmdParam;
00034
00035 this->OSGetText(fCmdParam);
00036
00037 if (oldParam.Compare(fCmdParam) != 0)
00038 {
00039 ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00040 }
00041 }
00042
00043 CATControlWnd::SetFocused(focused);
00044 }
00045
00046 void CATTreeCtrl::OnEscapeChange()
00047 {
00048 this->MarkDirty();
00049 }
00050
00051 CATTREEINFO* CATTreeCtrl::GetCurItem()
00052 {
00053 return fCurSel;
00054 }
00055
00056 CATUInt32 CATTreeCtrl::GetNumRootItems()
00057 {
00058 return this->fRootList.size();
00059 }
00060
00061 CATTREEINFO* CATTreeCtrl::GetRootItem(CATUInt32 index)
00062 {
00063 if (index >= fRootList.size())
00064 return 0;
00065 return fRootList[index];
00066 }
00067
00068 CATResult CATTreeCtrl::SetCurItem(CATTREEINFO* newSel, bool sendCommand)
00069 {
00070 fCurSel = newSel;
00071 this->OSSetCurSel(newSel);
00072
00073 if (sendCommand)
00074 {
00075 fRespondSelect = false;
00076 ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00077 fRespondSelect = true;
00078 }
00079
00080 return CAT_SUCCESS;
00081 }
00082
00083 CATTREEINFO* CATTreeCtrl::GetItemFromDataPtr( void* dataPtr, CATTREEINFO* rootItem)
00084 {
00085 CATTREEINFO* curItem = 0;
00086 if (rootItem == 0)
00087 {
00088 for (CATUInt32 i = 0; i < fRootList.size(); i++)
00089 {
00090 curItem = fRootList[i];
00091 if (dataPtr == curItem->DataPtr)
00092 {
00093 return curItem;
00094 }
00095
00096 if (0 != (curItem = GetItemFromDataPtr(dataPtr,curItem)))
00097 {
00098 return curItem;
00099 }
00100 }
00101 }
00102 else
00103 {
00104 for (CATUInt32 i = 0; i < rootItem->Children.size(); i++)
00105 {
00106 curItem = rootItem->Children[i];
00107 if (dataPtr == curItem->DataPtr)
00108 {
00109 return curItem;
00110 }
00111
00112 if (0 != (curItem = GetItemFromDataPtr(dataPtr,curItem)))
00113 {
00114 return curItem;
00115 }
00116 }
00117 }
00118
00119
00120 return 0;
00121 }
00122
00123 CATResult CATTreeCtrl::Insert( const CATString& displayText,
00124 CATTREEINFO* parent,
00125 CATTREEINFO*& newInfo,
00126 void* dataPtr,
00127 CATUInt32 index,
00128 CATUInt32 imageNumber)
00129 {
00130 CATResult result = CAT_SUCCESS;
00131
00132 newInfo = new CATTREEINFO;
00133 newInfo->DataPtr = dataPtr;
00134 newInfo->DisplayText = displayText;
00135 newInfo->ItemHandle = 0;
00136 newInfo->Parent = parent;
00137 newInfo->ImageNumber = imageNumber;
00138
00139 if (parent == 0)
00140 {
00141 if (index == -1)
00142 {
00143 index = fRootList.size();
00144 }
00145 this->fRootList.insert(fRootList.begin()+index,newInfo);
00146 }
00147 else
00148 {
00149 if (index == -1)
00150 {
00151 index = parent->Children.size();
00152 }
00153 parent->Children.insert(parent->Children.begin() + index, newInfo);
00154 }
00155
00156
00157 if (fControlWnd == 0)
00158 {
00159 return CAT_SUCCESS;
00160 }
00161 return OSAddItem(displayText,newInfo,index);
00162 }
00163
00164 CATResult CATTreeCtrl::Remove( CATTREEINFO*& item)
00165 {
00166 if (item == 0)
00167 {
00168 CATASSERT(item != 0, "Invalid item remove requested.");
00169 return CATRESULT(CAT_ERR_INVALID_PARAM);
00170 }
00171
00172
00173 CATResult result = CAT_SUCCESS;
00174 result = OSRemoveItem(item);
00175 if (CATFAILED(result))
00176 {
00177 return result;
00178 }
00179
00180 CATTREEINFO* curItem = 0;
00181 CATUInt32 numChildren = 0;
00182
00183
00184 if (item->Parent == 0)
00185 {
00186 std::vector<CATTREEINFO*>::iterator iter = fRootList.begin();
00187 while (iter != fRootList.end())
00188 {
00189 if ( (*iter) == item)
00190 {
00191 fRootList.erase(iter);
00192 ClearTreeItem(item);
00193 delete item;
00194 item = 0;
00195 return CAT_SUCCESS;
00196 }
00197
00198 ++iter;
00199 }
00200 return CATRESULT(CAT_ERR_TREE_ITEM_NOT_FOUND);
00201 }
00202
00203 CATTREEINFO* parent = item->Parent;
00204 numChildren = parent->Children.size();
00205
00206 std::vector<CATTREEINFO*>::iterator iter = parent->Children.begin();
00207 while (iter != parent->Children.end())
00208 {
00209 if ((*iter) != item)
00210 {
00211 parent->Children.erase(iter);
00212 ClearTreeItem(item);
00213 delete item;
00214 item = 0;
00215 return CAT_SUCCESS;
00216 }
00217 ++iter;
00218 }
00219
00220 return CATRESULT(CAT_ERR_TREE_ITEM_NOT_FOUND);
00221 }
00222
00223 void CATTreeCtrl::ClearTreeItem(CATTREEINFO* item)
00224
00225 {
00226 while (item->Children.size())
00227 {
00228 CATTREEINFO* curItem = item->Children[0];
00229 ClearTreeItem(curItem);
00230 delete curItem;
00231 item->Children.erase(item->Children.begin());
00232 }
00233 }
00234
00235 CATResult CATTreeCtrl::Clear()
00236 {
00237 fCurSel = 0;
00238
00239 while (fRootList.size())
00240 {
00241 CATTREEINFO* curItem = fRootList[0];
00242 ClearTreeItem(curItem);
00243 delete curItem;
00244 fRootList.erase(fRootList.begin());
00245 }
00246
00247 if (fControlWnd != 0)
00248 OSClearTree();
00249
00250 return CAT_SUCCESS;
00251 }
00252
00253
00254
00255 CATResult CATTreeCtrl::OnEvent(const CATEvent& event, CATInt32& retVal)
00256 {
00257 return OSEvent(event,retVal);
00258 }
00259
00260 CATString CATTreeCtrl::GetHint() const
00261 {
00262 CATString retString;
00263 retString = CATControl::GetHint();
00264 if (fShowHintValue)
00265 {
00266 retString << L" ( " << this->GetString() << L" )";
00267 }
00268 return retString;
00269 }
00270
00271
00272
00273
00274 CATCommand CATTreeCtrl::GetCommand() const
00275 {
00276 return CATCommand(this->fCmdString, 1, this->GetString(), this->fTarget, this->fCmdType);
00277 }
00278
00279
00280
00281 CATString CATTreeCtrl::GetString() const
00282 {
00283 if (this->fCurSel != 0)
00284 {
00285 return fCurSel->DisplayText;
00286 }
00287
00288 return "No Selection";
00289 }
00290
00291 CATResult CATTreeCtrl::SetItemText(CATTREEINFO* item, const CATString& text)
00292 {
00293 if (item == 0)
00294 {
00295 return CATRESULT(CAT_ERR_INVALID_PARAM);
00296 }
00297
00298 item->DisplayText = text;
00299 return OSUpdateText(item);
00300 }
00301