00001
00002
00004
#include "StdAfx.h"
00005
#include "GUI.h"
00006
00008
00010 CGUIDropList::CGUIDropList()
00011 {
00012
Initialize();
00013 }
00014
00015 CGUIDropList::~CGUIDropList()
00016 {
00017
00018 }
00019
00020 void CGUIDropList::Initialize()
00021 {
00022
SetType(GUI_DropList);
00023
m_iListType = 2;
00024
m_bIdentSize =
true;
00025
m_iCurSel = -1;
00026
m_bExpanded =
false;
00027
m_iSpacing = 1;
00028
00029 m_strElementType =
"DropList";
00030 }
00031
00032 int CGUIDropList::Create(
CGUIElement *pParent, tRect WidgetRect,
CCustomFont *pFont,
CTexture *pTexture,
CMaterial *pMaterial,
bool bBorder,
CGUIButton *pDefaultEntryBtn)
00033 {
00034
if(!
CGUIButton::Create(pParent, WidgetRect, pFont, pTexture, pMaterial, bBorder))
00035
return 0;
00036
00037
if(pDefaultEntryBtn !=NULL)
00038 {
00039
if(!
IsChild(pDefaultEntryBtn))
00040 {
00041 pDefaultEntryBtn->
SetParent(
this);
00042 pDefaultEntryBtn->
RemoveChild(pDefaultEntryBtn->
GetChild(GUI_Border));
00043
00044
push_back(pDefaultEntryBtn);
00045 }
00046 }
00047
00048
return Resize(WidgetRect);
00049 }
00050
00051 void CGUIDropList::Expand()
00052 {
00053
00054
for (UINT i=0; i<
GetChildCount(); i++)
00055 {
00056
CGUIElement *pElement =
GetChild(i);
00057
if(pElement->
GetType() == GUI_Button)
00058 {
00059 ((
CGUIButton *)pElement)->Show();
00060 ((
CGUIButton *)pElement)->ShowText();
00061 }
00062 }
00063
00064
m_bExpanded =
true;
00065 }
00066
00067 void CGUIDropList::Collapse()
00068 {
00069
00070
for (UINT i=0; i<
GetChildCount(); i++)
00071 {
00072
CGUIElement *pElement =
GetChild(i);
00073
if(pElement->
GetType() == GUI_Button)
00074 {
00075 ((
CGUIButton *)pElement)->Hide();
00076 ((
CGUIButton *)pElement)->HideText();
00077 }
00078 }
00079
00080
00081
m_bExpanded =
false;
00082 }
00083
00084 void CGUIDropList::Draw()
00085 {
00086
CGUIElement *pIter =
begin();
00087
set_ptr(pIter);
00088
00089
while(pIter !=NULL)
00090 {
00091
if(pIter->
GetChild(GUI_Border) !=NULL)
00092 pIter->
RemoveChild(pIter->
GetChild(GUI_Border));
00093
00094 pIter =
next();
00095 }
00096
00097
CGUIElement::Draw();
00098 }
00099
00100 int CGUIDropList::LoadXML(
TiXmlNode *pDataNode, CString strFilename)
00101 {
00102
TiXmlElement* pXMLElement = NULL;
00103
const char *pcValue = NULL;
00104
00105
int iRetValue =
CGUIButton::LoadXML(pDataNode, strFilename);
00106
if(iRetValue !=1)
00107
return iRetValue;
00108
00109 pXMLElement = m_pXMLElement;
00110
00111 pcValue = pXMLElement->
Attribute(
"Type");
00112
if(pcValue !=NULL)
00113 {
00114
m_iListType = atoi(pcValue);
00115 pcValue = NULL;
00116 }
00117
else
00118 {
00119
CGlobalLogger::GetSingleton().
Write(
"\t**********General Warning\t**********\n");
00120
CGlobalLogger::GetSingleton().
Write(
"DropList (Name: %s, ID: %d) doesn't have Type (Type) Attribute specified. Assuming default...",
GetName(),
GetID());
00121 }
00122
00123 pcValue = pXMLElement->
Attribute(
"CurSel");
00124
if(pcValue !=NULL)
00125 {
00126
SetCurSel(atoi(pcValue));
00127 pcValue = NULL;
00128 }
00129
else
00130 {
00131
CGlobalLogger::GetSingleton().
Write(
"\t**********General Warning\t**********\n");
00132
CGlobalLogger::GetSingleton().
Write(
"DropList (Name: %s, ID: %d) doesn't have Current iSelection (CurSel) Attribute specified. Assuming default...",
GetName(),
GetID());
00133 }
00134
00135 pcValue = pXMLElement->
Attribute(
"Expand");
00136
if(pcValue !=NULL)
00137 {
00138
m_bExpanded = (
bool)atoi(pcValue);
00139 pcValue = NULL;
00140 }
00141
00142
CGUIElement *pIterElement =
begin();
00143
set_ptr(pIterElement);
00144
00145
while(pIterElement !=NULL)
00146 {
00147
if(pIterElement->
GetType() == GUI_Button)
00148 {
00149
CGUIButton *pButton = (
CGUIButton *)pIterElement;
00150 pButton->
RemoveChild(pButton->
GetChild(GUI_Border));
00151 }
00152
00153 pIterElement =
next();
00154 }
00155
00156
if(
m_bExpanded)
00157
Expand();
00158
else
00159
Collapse();
00160
00161
return iRetValue;
00162 }
00163
00164 int CGUIDropList::SaveXML(
TiXmlNode *pDataNode, CString strFilename)
00165 {
00166
00167
if(pDataNode == NULL && strFilename ==
"")
00168
return -1;
00169
00170
char Buf[128] =
"";
00171
TiXmlElement *pSaveXMLElement = NULL;
00172
00173
if(pDataNode != NULL)
00174 pSaveXMLElement = pDataNode->
ToElement();
00175
else
00176 pSaveXMLElement =
new TiXmlElement(
"DropList");
00177
00178 pSaveXMLElement->
SetAttribute(
"Type",
m_iListType);
00179 pSaveXMLElement->
SetAttribute(
"CurSel",
m_iCurSel);
00180 pSaveXMLElement->
SetAttribute(
"Expand", (
int)
m_bExpanded);
00181
00182
int iRetValue =
CGUIButton::SaveXML(pSaveXMLElement, strFilename);
00183
00184
if(pDataNode == NULL && strFilename !=
"")
00185 {
00186
TiXmlDocument SaveDoc;
00187 SaveDoc.
InsertEndChild(*pSaveXMLElement);
00188
int iRetValue = SaveDoc.
SaveFile(strFilename);
00189 SAFEDEL(pSaveXMLElement)
00190
00191
return iRetValue;
00192 }
00193
00194
return iRetValue;
00195 }
00196
00197 int CGUIDropList::Resize(tRect& NewRect)
00198 {
00199 UINT MaxWidth = 0;
00200
00201
00202
for (UINT i=0; i<
GetChildCount(); i++)
00203 {
00204
CGUIElement *pElement =
GetChild(i);
00205
00206
if(pElement->
GetType() == GUI_Button)
00207 {
00208
if(((
CGUIButton *)pElement)->GetText() !=
"")
00209 {
00210 UINT TotalWidth = 0;
00211
for (UINT j=0; j<((
CGUIButton *)pElement)->GetLineCount(); j++)
00212 {
00213
CBitmapFont *pBitmapFont = (
CBitmapFont *)((
CGUIButton *)pElement)->GetFont();
00214
if(pBitmapFont !=NULL)
00215 {
00216 UINT FontLetterWidth = pBitmapFont->
GetQuadLength() + pBitmapFont->
GetSpacing();
00217 TotalWidth = ((
CGUIButton *)pElement)->GetLineLength(j) * FontLetterWidth;
00218
00219
if(MaxWidth <= TotalWidth)
00220 MaxWidth = TotalWidth;
00221 }
00222 }
00223 }
00224 }
00225
00226
if(
m_iListType !=1)
00227 {
00228
00229
if(MaxWidth < GetWidth() || MaxWidth >
GetWidth())
00230 MaxWidth =
GetWidth();
00231 }
00232
00233
00234
if(
m_iListType == 0)
00235 {
00236
00237
m_bIdentSize =
true;
00238 }
00239
else if(
m_iListType == 1)
00240
00241 {
00242 tRect CurrentRect =
GetRect();
00243 CurrentRect.right = CurrentRect.left + MaxWidth;
00244
SetRect(CurrentRect);
00245
m_bIdentSize =
true;
00246 }
00247
else if(
m_iListType == 2)
00248
00249 {
00250 tRect CurrentRect =
GetRect();
00251 CurrentRect.right = CurrentRect.left + MaxWidth;
00252
SetRect(CurrentRect);
00253
m_bIdentSize =
false;
00254 }
00255 }
00256
00257 tRect PreviousRect =
GetRect();
00258
for (UINT j=0; j<
GetChildCount(); j++)
00259 {
00260
CGUIElement *pElement =
GetChild(j);
00261
00262
if(pElement->
GetType() == GUI_Button)
00263 {
00264
00265 UINT YCount = 0;
00266
00267 UINT CurrentWidth = MaxWidth;
00268
if(!
m_bIdentSize)
00269 {
00270 UINT Width = 0;
00271
for (UINT i=0; i<((
CGUIButton *)pElement)->GetLineCount(); i++)
00272 {
00273
CBitmapFont *pBitmapFont = (
CBitmapFont *)((
CGUIButton *)pElement)->GetFont();
00274
if(pBitmapFont !=NULL)
00275 {
00276 UINT FontLetterWidth = pBitmapFont->
GetQuadLength() + pBitmapFont->
GetSpacing();
00277
00278 Width = ((
CGUIButton *)pElement)->GetLineLength(i) * FontLetterWidth;
00279
if(CurrentWidth < Width)
00280 CurrentWidth = Width;
00281 }
00282 }
00283 CurrentWidth = Width;
00284
00285
00286 }
00287
CBitmapFont *pBitmapFont = (
CBitmapFont *)((
CGUIButton *)pElement)->GetFont();
00288
00289 tRect NewElementRect;
00290 NewElementRect.left = PreviousRect.left;
00291 NewElementRect.right = PreviousRect.left + CurrentWidth;
00292 NewElementRect.top = PreviousRect.bottom -
m_iSpacing;
00293
if(pBitmapFont == NULL)
00294 NewElementRect.bottom = NewElementRect.top - 20 - 1;
00295
else
00296 NewElementRect.bottom = NewElementRect.top - pBitmapFont->
GetQuadHeight() - 1;
00297
00298 pElement->
SetRect(NewElementRect);
00299 PreviousRect = NewElementRect;
00300
00301 YCount++;
00302 }
00303 }
00304
00305
return CGUIElement::Resize(NewRect);
00306 }
00307
00308 void CGUIDropList::ProcessMessage(
tGUIMessage& Message)
00309 {
00310
switch(Message.uiMessage)
00311 {
00312
00313
case GUI_Message_MoveX:
00314
case GUI_Message_MoveY:
00315
case GUI_Message_MoveXY:
00316
00317
if(Message.
pSender ==
this)
00318
Resize(
GetRect());
00319
break;
00320
00321
case GUI_Message_ButtonPressed:
00322 {
00323
switch(Message.
pSender->
GetType())
00324 {
00325
case GUI_DropList:
00326 {
00327
if(Message.
pSender ==
this)
00328 {
00329
if(
m_bExpanded)
00330
Collapse();
00331
else
00332
Expand();
00333 }
00334
break;
00335 }
00336
00337
case GUI_Button:
00338 {
00339
00340
if(!
IsChild(Message.
pSender))
00341
break;
00342
00343 UINT Count = 0;
00344
for (UINT i=0; i<
GetChildCount(); i++)
00345 {
00346
CGUIElement *pElement =
GetChild(i);
00347
00348
if(pElement->
GetType() == GUI_Button)
00349 {
00350
if(Message.
pSender == pElement)
00351 {
00352
SetCurSel(Count);
00353
Collapse();
00354 }
00355 Count++;
00356 }
00357 }
00358
break;
00359 }
00360 }
00361 }
00362 }
00363 }
00364
00365 void CGUIDropList::SetCurSel(
int iSelection)
00366 {
00367
if(iSelection >=0)
00368 {
00369 UINT Count = 0;
00370
CGUIElement *pIterElement =
begin();
00371
set_ptr(pIterElement);
00372
00373
while(pIterElement !=NULL)
00374 {
00375
if(pIterElement->
GetType() == GUI_Button)
00376 {
00377
if(Count == iSelection)
00378 {
00379
m_iCurSel = iSelection;
00380
SetText(((
CGUIButton *)pIterElement)->
GetText());
00381 }
00382 Count++;
00383 }
00384
00385 pIterElement =
next();
00386 }
00387 }
00388 }
00389
00390 int CGUIDropList::GetCurSel()
00391 {
00392
return m_iCurSel;
00393 }
00394
00395 CGUIElement *
CGUIDropList::InsertItem(CString& strItemText, UINT uiInsertAfter)
00396 {
00397
CGUIButton *pNewEntry =
new CGUIButton();
00398 pNewEntry->
Create(
this, tRect());
00399
00400 CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,
"XML Files (*.xml)|*.xml|All Files (*.*)|*.*||", AfxGetMainWnd());
00401
if(FileDialog.DoModal() == IDOK)
00402 {
00403
if(pNewEntry->
LoadXML(NULL, GetFullPath(FileDialog.GetPathName())))
00404 {
00405 pNewEntry->
SetText(strItemText);
00406
push_back(pNewEntry);
00407
00408
Resize(
GetRect());
00409
00410
return pNewEntry;
00411 }
00412 }
00413
00414
return NULL;
00415 }
00416
00417 bool CGUIDropList::IsOfType(eEntityType eType)
00418 {
00419
if(eType == Entity_GUIDropList)
00420
return true;
00421
00422
return CGUIButton::IsOfType(eType);
00423 }
00424
00425 bool CGUIDropList::PropertyChanging(
const void* pvProperty ,
void* pvNewValue )
00426 {
00427
bool bChangeOK =
CGUIButton::PropertyChanging(pvProperty, pvNewValue);
00428
00429
if(pvProperty == &
m_iListType)
00430 {
00431
m_iListType = *(
int *)pvNewValue;
00432
Resize(
GetRect());
00433 }
00434
00435
if(pvProperty == &
m_iCurSel)
00436 {
00437
int TempNewValue = *(
int *)pvNewValue;
00438
if(TempNewValue == 1000)
00439 {
00440
CGUIElement *pNewItem =
InsertItem(CString(
"New Entry"));
00441
Collapse();
00442
m_iCurSel = -1;
00443
return false;
00444 }
00445
else
00446
SetCurSel(TempNewValue);
00447 }
00448
00449
if(pvProperty == &m_strFilename)
00450 {
00451
Collapse();
00452
Resize(
GetRect());
00453 }
00454
00455
return bChangeOK;
00456 }
00457
00458 void CGUIDropList::GetProperties( EPropList& PropList )
00459 {
00460
CGUIButton::GetProperties(PropList);
00461
00462 PropList.AddTab(
"DropList");
00463 PropList.AddPropInt(
this,
"Spacing", &
m_iSpacing);
00464
00465 EPropertyCombo *pSelectedCombo = PropList.AddPropCombo(
this,
"Selected", &
m_iCurSel);
00466
00467 pSelectedCombo->AddString(
"New Item...", 1000);
00468
00469
int Count = 0;
00470
CGUIElement *pIter =
begin();
00471
set_ptr(pIter);
00472
00473
while(pIter !=NULL)
00474 {
00475
if(pIter->
GetType() == GUI_Button)
00476 {
00477 CString tmp;
00478 tmp.Format(
"%d", Count);
00479 pSelectedCombo->AddString(tmp, Count);
00480 Count++;
00481 }
00482
00483 pIter =
next();
00484 }
00485
00486 PropList.AddPropSeparator(
this,
"Adjustments");
00487 EPropertyCombo *pAdjustCombo = PropList.AddPropCombo(
this,
"Options", &
m_iListType);
00488 pAdjustCombo->AddString(
"Same Width", 0);
00489 pAdjustCombo->AddString(
"Lengthiest", 1);
00490 pAdjustCombo->AddString(
"Lengthiest, Custom", 2);
00491 }