Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

D:/Programming/GUI Editor (Source)/GUIListBox.cpp

00001 // GUIListBox.cpp: implementation of the CGUIListBox class. 00002 // 00004 #include "StdAfx.h" 00005 #include "GUI.h" 00006 00008 // Construction/Destruction 00010 00011 CGUIListBox::CGUIListBox() 00012 { 00013 Initialize(); 00014 } 00015 00016 CGUIListBox::~CGUIListBox() 00017 { 00018 00019 } 00020 00021 void CGUIListBox::Initialize() 00022 { 00023 m_iSpacing = 1; 00024 m_iListType = 0; 00025 m_iCurSel = -1; 00026 SetType(GUI_ListBox); 00027 00028 m_strElementType = "ListBox"; 00029 } 00030 00031 int CGUIListBox::Create(CGUIElement *pParent, tRect WidgetRect, CTexture *pTexture, CMaterial *pMaterial, bool bBorder, CGUIButton *pDefaultEntryBtn) 00032 { 00033 if(!CGUIElement::Create(pParent, WidgetRect, pTexture, pMaterial, bBorder)) 00034 return 0; 00035 00036 if(pDefaultEntryBtn !=NULL) 00037 { 00038 if(!IsChild(pDefaultEntryBtn)) 00039 { 00040 pDefaultEntryBtn->SetParent(this); 00041 pDefaultEntryBtn->RemoveChild(pDefaultEntryBtn->GetChild(GUI_Border)); 00042 00043 push_back(pDefaultEntryBtn); 00044 } 00045 } 00046 00047 return Resize(WidgetRect); 00048 } 00049 00050 int CGUIListBox::LoadXML(TiXmlNode *pDataNode, CString strFilename) 00051 { 00052 TiXmlElement* pXMLElement = NULL; 00053 const char *pcValue = NULL; 00054 00055 int iRetValue = CGUIElement::LoadXML(pDataNode, strFilename); // LoadXML the data of derivative 00056 if(iRetValue !=1) 00057 return iRetValue; 00058 00059 pXMLElement = m_pXMLElement; 00060 00061 pcValue = pXMLElement->Attribute("CurSel"); 00062 if(pcValue !=NULL) 00063 { 00064 SetCurSel(atoi(pcValue)); 00065 pcValue = NULL; 00066 } 00067 else 00068 { 00069 CGlobalLogger::GetSingleton().Write("\t**********General Warning\t**********\n"); 00070 CGlobalLogger::GetSingleton().Write("ListBox (Name: %s, ID: %d) doesn't have Current iSelection (CurSel) Attribute specified. Assuming default...", GetName(), GetID()); 00071 } 00072 00073 return iRetValue; 00074 } 00075 00076 int CGUIListBox::SaveXML(TiXmlNode *pDataNode, CString strFilename) 00077 { 00078 // Nowhere to save! Nothing to save! 00079 if(pDataNode == NULL && strFilename == "") 00080 return -1; 00081 00082 char Buf[128] = ""; 00083 TiXmlElement *pSaveXMLElement = NULL; 00084 00085 if(pDataNode != NULL) 00086 pSaveXMLElement = pDataNode->ToElement(); 00087 else 00088 pSaveXMLElement = new TiXmlElement("ListBox"); 00089 00090 pSaveXMLElement->SetAttribute("Type", m_iListType); 00091 pSaveXMLElement->SetAttribute("CurSel", m_iCurSel); 00092 pSaveXMLElement->SetAttribute("Spacing", m_iSpacing); 00093 00094 int iRetValue = CGUIElement::SaveXML(pSaveXMLElement, strFilename); 00095 00096 if(pDataNode == NULL) 00097 SAFEDEL(pSaveXMLElement) 00098 00099 return iRetValue; 00100 } 00101 00102 void CGUIListBox::Draw() 00103 { 00104 CGUIElement *pIter = begin(); 00105 set_ptr(pIter); 00106 00107 while(pIter !=NULL) 00108 { 00109 if(pIter->GetChild(GUI_Border) !=NULL) 00110 pIter->RemoveChild(pIter->GetChild(GUI_Border)); 00111 00112 pIter = next(); 00113 } 00114 00115 CGUIElement::Draw(); 00116 } 00117 00118 void CGUIListBox::ProcessMessage(tGUIMessage& Message) 00119 { 00120 switch(Message.uiMessage) 00121 { 00122 case GUI_Message_ButtonPressed: 00123 { 00124 if((Message.pSender == this) || IsChild(Message.pSender)) // If this button is a child of a listbox, do: 00125 { 00126 UINT Count = 0;; // Find it by count and set current selection (by index count) 00127 for (UINT i=0; i<GetChildCount(); i++) 00128 { 00129 CGUIElement *pElement = GetChild(i); 00130 if(pElement->GetType() == GUI_Button) 00131 { 00132 if(Message.pSender == pElement) 00133 { 00134 SetCurSel(Count); 00135 break; 00136 } 00137 Count++; 00138 } 00139 } 00140 } 00141 00142 break; 00143 } 00144 00145 // Resize/move buttons if listbox was moved! 00146 case GUI_Message_MoveX: 00147 case GUI_Message_MoveY: 00148 case GUI_Message_MoveXY: 00149 { 00150 if(Message.pSender == this) 00151 Resize(GetRect()); 00152 break; 00153 } 00154 } 00155 } 00156 00157 int CGUIListBox::Resize(tRect& NewRect) 00158 { 00159 UINT iRetValue = CGUIElement::Resize(NewRect); 00160 00161 UINT MaxWidth = 0; 00162 00163 if(m_iListType == 1) // maximize listbox to maximum length entry in it 00164 { 00165 // Find longest text entry size 00166 for (UINT i=0; i<GetChildCount(); i++) 00167 { 00168 UINT MaxWidth = 0; 00169 tRect PrevRect = GetRect(); 00170 00171 CGUIElement *pElement = GetChild(i); 00172 00173 if(pElement->GetType() == GUI_Button) 00174 { 00175 CGUIButton *pButton = (CGUIButton *)pElement; 00176 00177 if(pButton->GetFont() !=NULL) 00178 { 00179 CBitmapFont *pBitmapFont = (CBitmapFont *)pButton->GetFont(); 00180 for (UINT i=0; i<pButton->GetLineCount(); i++) 00181 { 00182 UINT LineLength = pButton->GetLineLength(i); 00183 if(LineLength > MaxWidth) 00184 MaxWidth = LineLength; 00185 } 00186 } 00187 } 00188 } 00189 00190 tRect CurrentRect = GetRect(); 00191 tRect NewThisRect = CurrentRect; 00192 NewThisRect.right = NewThisRect.left + MaxWidth; 00193 SetRect(NewThisRect); 00194 } 00195 00196 00197 tRect PrevRect = GetRect(); 00198 PrevRect.bottom = PrevRect.top; 00199 00200 // Find longest text entry size 00201 for (UINT i=0; i<GetChildCount(); i++) 00202 { 00203 CGUIElement *pElement = GetChild(i); 00204 00205 if(pElement->GetType() == GUI_Button) 00206 { 00207 CGUIButton *pButton = (CGUIButton *)pElement; 00208 00209 if(pButton->GetFont() !=NULL) 00210 { 00211 CBitmapFont *pBitmapFont = (CBitmapFont *)pButton->GetFont(); 00212 00213 tRect NewEntryRect; 00214 NewEntryRect.left = GetRect().left; 00215 NewEntryRect.right = GetRect().right; 00216 NewEntryRect.top = PrevRect.bottom - m_iSpacing; 00217 NewEntryRect.bottom = NewEntryRect.top - pBitmapFont->GetQuadHeight() - 1; 00218 00219 pButton->SetRect(NewEntryRect); 00220 00221 if(((NewEntryRect.top > GetRect().bottom) && (NewEntryRect.bottom < GetRect().bottom)) || NewEntryRect.top <= GetRect().bottom) 00222 { 00223 pButton->Hide(); 00224 pButton->HideText(); 00225 } 00226 else 00227 { 00228 pButton->Show(); 00229 pButton->ShowText(); 00230 } 00231 00232 PrevRect = NewEntryRect; 00233 } 00234 } 00235 } 00236 00237 return iRetValue; 00238 } 00239 00240 void CGUIListBox::SetCurSel(int iSelection) 00241 { 00242 if(iSelection >=0) 00243 { 00244 UINT Count = 0; 00245 CGUIElement *pIterElement = begin(); 00246 set_ptr(pIterElement); 00247 00248 while(pIterElement !=NULL) 00249 { 00250 if(pIterElement->GetType() == GUI_Button) 00251 { 00252 if(Count == iSelection) 00253 ((CGUIButton *)pIterElement)->SetState(false, 1); 00254 else 00255 ((CGUIButton *)pIterElement)->SetState(false, 0); 00256 00257 Count++; 00258 } 00259 00260 pIterElement = next(); 00261 } 00262 } 00263 } 00264 00265 int CGUIListBox::GetCurSel() 00266 { 00267 return m_iCurSel; 00268 } 00269 00270 CGUIElement *CGUIListBox::InsertItem(CString& strItemText, UINT uiInsertAfter) 00271 { 00272 CGUIButton *pNewEntry = new CGUIButton(); 00273 pNewEntry->Create(this, tRect()); 00274 00275 CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||", AfxGetMainWnd()); 00276 if(FileDialog.DoModal() == IDOK) 00277 { 00278 if(pNewEntry->LoadXML(NULL, GetFullPath(FileDialog.GetPathName()))) 00279 { 00280 pNewEntry->SetText(strItemText); 00281 push_back(pNewEntry); 00282 00283 Resize(GetRect()); 00284 00285 return pNewEntry; 00286 } 00287 } 00288 00289 return NULL; 00290 } 00291 00292 bool CGUIListBox::IsOfType(eEntityType eType) 00293 { 00294 if(eType == Entity_GUIListBox) 00295 return true; 00296 00297 return CGUIElement::IsOfType(eType); 00298 } 00299 00300 bool CGUIListBox::PropertyChanging( const void* pvProperty , void* pvNewValue ) 00301 { 00302 bool bChangeOK = CGUIElement::PropertyChanging(pvProperty, pvNewValue); 00303 00304 if(pvProperty == &m_iCurSel) 00305 { 00306 int TempNewValue = *(int *)pvNewValue; 00307 if(TempNewValue == 1000) 00308 { 00309 CGUIElement *pNewItem = InsertItem(CString("New Entry")); 00310 m_iCurSel = -1; 00311 return false; 00312 } 00313 else 00314 SetCurSel(TempNewValue); 00315 } 00316 00317 return bChangeOK; 00318 } 00319 00320 void CGUIListBox::GetProperties( EPropList& PropList ) 00321 { 00322 CGUIElement::GetProperties(PropList); 00323 00324 PropList.AddTab("ListBox"); 00325 PropList.AddPropInt(this, "Spacing", &m_iSpacing); 00326 00327 EPropertyCombo *pSelectedCombo = PropList.AddPropCombo(this, "Selected", &m_iCurSel); 00328 00329 pSelectedCombo->AddString("New Item...", 1000); 00330 00331 int Count = 0; 00332 CGUIElement *pIter = begin(); 00333 set_ptr(pIter); 00334 00335 while(pIter !=NULL) 00336 { 00337 if(pIter->GetType() == GUI_Button) 00338 { 00339 CString tmp; 00340 tmp.Format("%d", Count); 00341 pSelectedCombo->AddString(tmp, Count); 00342 Count++; 00343 } 00344 00345 pIter = next(); 00346 } 00347 00348 PropList.AddPropSeparator(this, "Adjustments"); 00349 EPropertyCombo *pAdjustCombo = PropList.AddPropCombo(this, "Options", &m_iListType); 00350 pAdjustCombo->AddString("Same Width", 0); 00351 pAdjustCombo->AddString("Lengthiest", 1); 00352 }

Generated on Sun Jul 17 21:34:27 2005 for OpenGL GUI by doxygen 1.3.8