00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CATGuiObj.h"
00016 #include "CATApp.h"
00017 #include "CATFileSystem.h"
00018 #include "CATStream.h"
00019 #include "CATEvent.h"
00020 #include "CATAccessible.h"
00021 #include "CATWindow.h"
00022
00023
00024
00025
00026
00027
00028 CATGuiObj::CATGuiObj( const CATString& element,
00029 const CATString& rootDir)
00030 : CATXMLObject(element)
00031 {
00032
00033 fAccessible = 0;
00034 fImage = 0;
00035 fEnabled = true;
00036 fVisible = true;
00037 fShowHintValue = true;
00038 fRootDir = rootDir;
00039 }
00040
00041
00042 CATResult CATGuiObj::Load(CATPROGRESSCB progressCB,
00043 void* progressParam,
00044 CATFloat32 progMin,
00045 CATFloat32 progMax)
00046 {
00047 CATResult result = CAT_SUCCESS;
00048 CATResult testResult = CAT_SUCCESS;
00049
00050 ParseAttributes();
00051 if (CATFAILED(result = this->RectFromAttribs()))
00052 {
00053 return result;
00054 }
00055
00056
00057 CATXMLObject* curChild = 0;
00058
00059 CATUInt32 i;
00060 CATUInt32 numChildren = GetNumChildren();
00061
00062 if (progressCB)
00063 {
00064 progressCB( progMin, this->GetName(), progressParam);
00065 }
00066
00067 if (numChildren)
00068 {
00069 CATFloat32 progStep = (progMax - progMin) / (CATFloat32)numChildren;
00070
00071 for (i = 0; i < numChildren; i++)
00072 {
00073
00074 if (0 != (curChild = GetChild(i)))
00075 {
00076 CATGuiObj* curObj = (CATGuiObj*)curChild;
00077 if (CAT_SUCCESS != (testResult = curObj->Load( progressCB,
00078 progressParam,
00079 progMin + progStep*i,
00080 progMin + (progStep*(i+1)) )))
00081 {
00082 result = testResult;
00083 }
00084 }
00085 }
00086 }
00087
00088 return result;
00089 }
00090
00091
00092
00093 CATGuiObj::~CATGuiObj()
00094 {
00095 if (fAccessible)
00096 {
00097 fAccessible->ObjectDeath();
00098 fAccessible = 0;
00099 }
00100
00101 if (fImage)
00102 {
00103 CATImage::ReleaseImage(fImage);
00104 }
00105 }
00106
00107
00108
00109
00110 CATResult CATGuiObj::ParseAttributes()
00111 {
00112 CATResult result = CAT_SUCCESS;
00113 CATResult tmpResult = CAT_SUCCESS;
00114 CATString attrib;
00115
00116 fName = GetAttribute(L"Name");
00117 fHintText = GetAttribute(L"HintText");
00118 fShowHintValue = GetAttribute(L"ShowHintValue",fShowHintValue);
00119
00120 attrib = GetAttribute(L"Image");
00121 if (attrib.IsEmpty() == false)
00122 {
00123 tmpResult = LoadSkinImage(attrib,fImage);
00124 if (CATFAILED(tmpResult))
00125 result = tmpResult;
00126 }
00127
00128 attrib = GetAttribute(L"Enabled");
00129 if (attrib.IsEmpty() == false)
00130 SetEnabled((bool)attrib);
00131
00132 attrib = GetAttribute(L"ColorFore");
00133 if (attrib.IsEmpty() == false)
00134 {
00135 CATUInt32 rawColor = attrib.FromHex();
00136
00137 this->fForegroundColor.r = (CATUInt8)((rawColor & 0xff0000) >>16);
00138 this->fForegroundColor.g = (CATUInt8)((rawColor & 0xff00) >> 8);
00139 this->fForegroundColor.b = (CATUInt8)(rawColor & 0xff);
00140 this->fForegroundColor.a = 255;
00141 }
00142
00143 attrib = GetAttribute(L"ColorBack");
00144 if (attrib.IsEmpty() == false)
00145 {
00146 CATUInt32 rawColor = attrib.FromHex();
00147
00148 this->fBackgroundColor.r = (CATUInt8)((rawColor & 0xff0000) >>16);
00149 this->fBackgroundColor.g = (CATUInt8)((rawColor & 0xff00) >>8);
00150 this->fBackgroundColor.b = (CATUInt8)((rawColor & 0xff));
00151 this->fBackgroundColor.a = 255;
00152 }
00153
00154 return result;
00155 }
00156
00157
00158
00159
00160 CATResult CATGuiObj::LoadSkinImage(const CATString& filename, CATImage*& imagePtr)
00161 {
00162
00163 CATFileSystem* fs = gApp->GetGlobalFileSystem();
00164
00165 imagePtr = 0;
00166
00167
00168
00169 CATString imageFile = fs->BuildPath(fRootDir,filename);
00170 CATStream* stream = 0;
00171 CATResult result = CAT_SUCCESS;
00172
00173
00174
00175 if (CATSUCCEEDED(result = gApp->GetResourceImage(imageFile, imagePtr)))
00176 {
00177 return result;
00178 }
00179
00180
00181 if (CATSUCCEEDED(result = fs->OpenFile(imageFile,CATStream::READ_ONLY,stream)))
00182 {
00183 result = CATImage::Load(stream,imagePtr);
00184 fs->ReleaseFile(stream);
00185
00186 if (CATSUCCEEDED(result))
00187 {
00188
00189 gApp->AddResourceImage(imageFile,imagePtr);
00190 }
00191 }
00192
00193 if (CATFAILED(result))
00194 {
00195 result = CATRESULTFILE(result,imageFile);
00196 CATASSERT(false,"Unable to load skin image!");
00197 }
00198
00199 return result;
00200 }
00201
00202
00203
00204
00205
00206
00207 CATString CATGuiObj::GetName() const
00208 {
00209 return this->fName;
00210 }
00211
00212
00213
00214
00215 CATString CATGuiObj::GetHint() const
00216 {
00217 return this->fHintText;
00218 }
00219
00220
00221
00222
00223
00224
00225 CATResult CATGuiObj::RectFromAttribs()
00226 {
00227 CATResult result = CAT_SUCCESS;
00228 CATRect parentRect;
00229 if (this->fParent != 0)
00230 {
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 parentRect = ((CATGuiObj*)fParent)->GetRect();
00253 parentRect.ZeroOrigin();
00254
00255 if (parentRect.Width() == 0)
00256 {
00257 parentRect = GetPrimaryMonitorRect();
00258 }
00259 }
00260
00261 CATInt32 width = (CATInt32)this->GetAttribute(L"Width");
00262 CATInt32 height = (CATInt32)this->GetAttribute(L"Height");
00263
00264 CATString posString = this->GetAttribute(L"XPos");
00265
00266 CATInt32 xPos;
00267 if ((posString.Compare(L"CenterObject") == 0))
00268 {
00269 if (fImage != 0)
00270 xPos = parentRect.CenterX() - (fImage->Width()/2);
00271 else
00272 xPos = parentRect.CenterX() - (width/2);
00273 }
00274 else if (posString.Compare(L"Center",6) == 0)
00275 {
00276 xPos = parentRect.CenterX();
00277 if (posString.Length() > 6)
00278 {
00279 CATString xOffString = posString.Right(6);
00280 xPos += ((CATInt32)xOffString);
00281 }
00282 }
00283 else
00284 xPos = ((CATInt32)posString);
00285
00286 posString = this->GetAttribute(L"YPos");
00287 CATInt32 yPos;
00288 if ((posString.Compare(L"CenterObject") == 0) )
00289 {
00290 if (fImage != 0)
00291 yPos = parentRect.CenterY() - (fImage->Height()/2);
00292 else
00293 yPos = parentRect.CenterY() - height/2;
00294 }
00295 else if (posString.Compare(L"Center",6) == 0)
00296 {
00297 yPos = parentRect.CenterY();
00298 if (posString.Length() > 6)
00299 {
00300 CATString yOffString = posString.Right(6);
00301 yPos += ((CATInt32)yOffString);
00302 }
00303 }
00304 else
00305 yPos = ((CATInt32)posString);
00306
00307
00308 CATInt32 xMin = ((CATInt32)this->GetAttribute(L"XMin"));
00309 CATInt32 yMin = ((CATInt32)this->GetAttribute(L"YMin"));
00310 CATInt32 xMax = ((CATInt32)this->GetAttribute(L"XMax"));
00311 CATInt32 yMax = ((CATInt32)this->GetAttribute(L"YMax"));
00312
00313 fMinWidth = this->GetAttribute(L"MinWidth", 10);
00314 fMinHeight = this->GetAttribute(L"MinHeight",10);
00315 fMaxWidth = this->GetAttribute(L"MaxWidth", 0);
00316 fMaxHeight = this->GetAttribute(L"MaxHeight",0);
00317
00318
00319
00320
00321
00322 bool xPinUsed = false;
00323 bool yPinUsed = false;
00324 CATInt32 xPin = 0;
00325 CATInt32 yPin = 0;
00326
00327
00328
00329 CATString tmpString = this->GetAttribute(L"XPin");
00330 if (tmpString.IsEmpty() == false)
00331 {
00332 xPinUsed = true;
00333 if (tmpString.Compare("Center",6) == 0)
00334 {
00335
00336 xPin = -parentRect.CenterX();
00337 if (tmpString.Length() > 6)
00338 {
00339 CATString xOffString = tmpString.Right(6);
00340 xPin += ((CATInt32)xOffString);
00341 }
00342
00343 }
00344 else
00345 {
00346 xPin = ((CATInt32)tmpString);
00347 }
00348 }
00349
00350 tmpString = this->GetAttribute(L"YPin");
00351 if (tmpString.IsEmpty() == false)
00352 {
00353 yPinUsed = true;
00354 if (tmpString.Compare("Center",6) == 0)
00355 {
00356
00357 yPin = parentRect.CenterY();
00358 if (tmpString.Length() > 6)
00359 {
00360 CATString yOffString = tmpString.Right(6);
00361 yPos += ((CATInt32)yOffString);
00362 }
00363 }
00364 else
00365 {
00366 yPin = ((CATInt32)tmpString);
00367 }
00368 }
00369
00370
00371
00372 if (xPinUsed)
00373 {
00374 if ((xPin > 0) || (width != 0) || (xPos < 0))
00375 {
00376 CATString misuseInfo;
00377 misuseInfo << "Error in skin XML for control: " << this->GetName();
00378 result = CATRESULTDESC(CAT_ERR_SKIN_XPIN_MISUSE,misuseInfo);
00379 xPin = 0;
00380 }
00381 }
00382
00383 if (yPinUsed)
00384 {
00385 if ((yPin > 0) || (height != 0) || (yPos < 0))
00386 {
00387 CATString misuseInfo;
00388 misuseInfo << "Error in skin XML for control: " << this->GetName();
00389 result = CATRESULTDESC(CAT_ERR_SKIN_YPIN_MISUSE,misuseInfo);
00390 yPin = 0;
00391 }
00392 }
00393
00394
00395
00396 if (this->fImage)
00397 {
00398 if (width == 0)
00399 {
00400 width = fImage->Width();
00401 }
00402
00403 if (height == 0)
00404 {
00405 height = fImage->Height();
00406 }
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 if (xPinUsed)
00423 {
00424 fRect.left = xPos;
00425 fRect.right = parentRect.right + xPin + 1;
00426 if ((xMax) && (xMax < fRect.right))
00427 {
00428 fRect.right = xMax;
00429 }
00430 }
00431 else if ((xPos >= 0) || (parentRect.Width() == 0))
00432 {
00433 fRect.left = xPos;
00434 fRect.right = width + xPos;
00435 }
00436 else
00437 {
00438 fRect.right = parentRect.right + xPos;
00439 fRect.left = fRect.right - width;
00440 if ((xMin) && (fRect.left < xMin))
00441 {
00442 fRect.left = xMin;
00443 fRect.right = fRect.left + width;
00444 }
00445 }
00446
00447 if (yPinUsed)
00448 {
00449 fRect.top = yPos;
00450 fRect.bottom = parentRect.bottom + yPin + 1;
00451 if ((yMax) && (yMax < fRect.bottom))
00452 {
00453 fRect.bottom = yMax;
00454 }
00455 }
00456 else if ((yPos >= 0) || (parentRect.Height() == 0))
00457 {
00458 fRect.top = yPos;
00459 fRect.bottom = height + yPos;
00460 }
00461 else
00462 {
00463 fRect.bottom = parentRect.bottom + yPos;
00464 fRect.top = fRect.bottom - height;
00465 if ((yMin) && (fRect.top < yMin))
00466 {
00467 fRect.top = yMin;
00468 fRect.bottom = fRect.top + height;
00469 }
00470 }
00471
00472
00473
00474
00475
00476
00477 if (fMinWidth != 0)
00478 {
00479 if (fRect.Width() < fMinWidth)
00480 {
00481 fRect.right = fRect.left + fMinWidth;
00482 }
00483 }
00484
00485 else if (fRect.Width() < 1)
00486 {
00487 fRect.right = fRect.left + 1;
00488 }
00489
00490 if (fMaxWidth != 0)
00491 {
00492 if (fRect.Width() > fMaxWidth)
00493 {
00494 fRect.right = fRect.left + fMaxWidth;
00495 }
00496 }
00497
00498
00499 if (fMinHeight != 0)
00500 {
00501 if (fRect.Height() < fMinHeight)
00502 {
00503 fRect.bottom = fRect.top + fMinHeight;
00504 }
00505 }
00506
00507 else if (fRect.Height() < 1)
00508 {
00509 fRect.bottom = fRect.top + 1;
00510 }
00511
00512
00513 if (fMaxHeight != 0)
00514 {
00515 if (fRect.Height() > fMaxHeight)
00516 {
00517 fRect.bottom = fRect.top + fMaxHeight;
00518 }
00519 }
00520
00521
00522
00523 CATResult testResult = CAT_SUCCESS;
00524 CATXMLObject* curChild = 0;
00525 CATUInt32 numChildren = this->GetNumChildren();
00526 for (CATUInt32 i = 0; i < numChildren; i++)
00527 {
00528 if (0 != (curChild = GetChild(i)))
00529 {
00530 CATGuiObj* curControl = (CATGuiObj*)curChild;
00531 CATRect curRect;
00532
00533 if (CAT_SUCCESS != (testResult = curControl->RectFromAttribs()))
00534 {
00535 result = testResult;
00536 }
00537
00538 }
00539 }
00540
00541
00542
00543 if (fParent)
00544 {
00545 ((CATGuiObj*)fParent)->MarkDirty();
00546 }
00547
00548 return result;
00549 }
00550
00551
00552
00553
00554 CATResult CATGuiObj::OnEvent(const CATEvent& event, CATInt32& retVal)
00555 {
00556 CATResult result = CAT_SUCCESS;
00557 CATResult testResult = CAT_SUCCESS;
00558
00559
00560 CATXMLObject* curChild = 0;
00561 CATUInt32 numChildren = this->GetNumChildren();
00562
00563 for (CATUInt32 i = 0; i < numChildren; i++)
00564 {
00565 if (0 != (curChild = GetChild(i)))
00566 {
00567 CATGuiObj* curControl = (CATGuiObj*)curChild;
00568 CATRect curRect;
00569
00570 if (CAT_SUCCESS != (testResult = curControl->OnEvent(event, retVal)))
00571 {
00572 result = testResult;
00573 }
00574
00575 }
00576 }
00577
00578 return CAT_SUCCESS;
00579 }
00580
00581
00582
00583 CATRect CATGuiObj::GetRect( )
00584 {
00585 return fRect;
00586 }
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597 void CATGuiObj::GetMinMax ( CATInt32& minWidth, CATInt32& minHeight,
00598 CATInt32& maxWidth, CATInt32& maxHeight)
00599 {
00600 minWidth = this->fMinWidth;
00601 minHeight = this->fMinHeight;
00602 maxWidth = this->fMaxWidth;
00603 maxHeight = this->fMaxHeight;
00604 }
00605
00606 bool CATGuiObj::IsEnabled() const
00607 {
00608 bool enabled = fEnabled;
00609
00610
00611 if ((enabled) && (this->fParent))
00612 {
00613 enabled = ((CATGuiObj*)fParent)->IsEnabled();
00614 }
00615
00616 return enabled;
00617 }
00618
00619 void CATGuiObj::SetEnabled(bool enabled)
00620 {
00621 fEnabled = enabled;
00622
00623 if (fParent)
00624 {
00625 ((CATGuiObj*)fParent)->MarkDirty();
00626 }
00627 }
00628
00629
00630
00631
00632
00633 bool CATGuiObj::GetPostRects(CATStack<CATRect>& rectStack)
00634 {
00635 bool retVal = false;
00636
00637 CATXMLObject* curChild = 0;
00638 CATUInt32 index;
00639 CATUInt32 numChildren = this->GetNumChildren();
00640 for (index = 0; index < numChildren; index++)
00641 {
00642 if (0 != (curChild = GetChild(index)))
00643 {
00644 CATGuiObj* curControl = (CATGuiObj*)curChild;
00645 retVal |= curControl->GetPostRects(rectStack);
00646 }
00647 }
00648
00649 return retVal;
00650 }
00651
00652
00653 bool CATGuiObj::ForEachControl(CATCONTROLFUNCB callback, void* userParam)
00654 {
00655 CATXMLObject* curChild = 0;
00656 CATUInt32 index;
00657 CATUInt32 numChildren = this->GetNumChildren();
00658 for (index = 0; index < numChildren; index++)
00659 {
00660 if (0 != (curChild = GetChild(index)))
00661 {
00662 CATGuiObj* curControl = (CATGuiObj*)curChild;
00663 if (!curControl->ForEachControl(callback,userParam))
00664 return false;
00665 }
00666 }
00667
00668 return true;
00669 }
00670
00671
00672 CATGuiObj* CATGuiObj::Find( const CATString& objectName,
00673 const CATString& objectType)
00674 {
00675
00676 if (this->GetName().Compare(objectName) == 0)
00677 {
00678 if (objectType.IsEmpty())
00679 {
00680 return this;
00681 }
00682 else if (objectType.Compare(this->GetType()) == 0)
00683 {
00684 return this;
00685 }
00686 }
00687
00688 CATXMLObject* curChild = 0;
00689 CATUInt32 index;
00690 CATUInt32 numChildren = this->GetNumChildren();
00691
00692 for (index = 0; index < numChildren; index++)
00693 {
00694 if (0 != (curChild = GetChild(index)))
00695 {
00696 CATGuiObj* curControl = (CATGuiObj*)curChild;
00697 CATGuiObj* foundControl = curControl->Find( objectName );
00698 if (foundControl)
00699 {
00700 return foundControl;
00701 }
00702 }
00703 }
00704 return 0;
00705 }
00706
00707 CATRect CATGuiObj::GetRectAbs( bool screenCoordinates)
00708 {
00709 CATRect absRect = this->fRect;
00710 if (fParent != 0)
00711 {
00712 CATRect parRect = ((CATGuiObj*)fParent)->GetRectAbs(screenCoordinates);
00713 absRect.Offset( parRect.left, parRect.top );
00714 }
00715
00716 return absRect;
00717 }
00718
00719 bool CATGuiObj::IsVisible(const CATGuiObj* object) const
00720 {
00721 if (fVisible)
00722 {
00723 return ((CATGuiObj*)fParent)->IsVisible(this);
00724 }
00725 return false;
00726 }
00727
00728
00729 void CATGuiObj::SetVisible(bool visible)
00730 {
00731 this->fVisible = visible;
00732
00733 this->MarkDirty(0,true);
00734 }
00735
00736
00737 void CATGuiObj::OnCommand( CATCommand& command, CATControl* ctrl)
00738 {
00739 if (fParent)
00740 {
00741 ((CATGuiObj*)fParent)->OnCommand( command, ctrl);
00742 }
00743 }
00744
00745 CATAccessible* CATGuiObj::GetAccessible()
00746 {
00747 CATTRACE( (CATString)"Getting accessible on " << this->fName);
00748 if (fAccessible)
00749 {
00750 fAccessible->AddRef();
00751 return fAccessible;
00752 }
00753
00754 fAccessible = new CATAccessible(this, this->GetWindow());
00755
00756 return fAccessible;
00757 }
00758
00759 CATUInt32 CATGuiObj::GetAccessRole()
00760 {
00761 return 0;
00762 }
00763
00764 CATUInt32 CATGuiObj::GetAccessState()
00765 {
00766 CATUInt32 state = 0;
00767 state |= (this->IsEnabled() ? 0 : CAT_STATE_SYSTEM_UNAVAILABLE);
00768 state |= (this->IsVisible() ? 0 : CAT_STATE_SYSTEM_INVISIBLE);
00769 return state;
00770 }