Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CATGUI/CATControl.cpp Source File

CATControl.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATControl.cpp
00003 /// \brief Base GUI control class
00004 /// \ingroup CATGUI
00005 /// 
00006 /// Copyright (c) 2003-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-25 05:11:25 -0600 (Fri, 25 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 #include "CATControl.h"
00015 #include "CATCursor.h"
00016 #include "CATWindow.h"
00017 #include "CATApp.h"
00018 #include "CATFileSystem.h"
00019 #include "CATStream.h"
00020 #include "CATCommand.h"
00021 #include "CATEventDefs.h"
00022 //---------------------------------------------------------------------------
00023 // CATControl constructor (inherited from CATXMLObject)
00024 // \param element - Type name (e.g. "Button", "Label", etc.)
00025 // \param attribs - attribute information for the window
00026 // \param parent - parent XML object (should be a "Window" element)
00027 //---------------------------------------------------------------------------
00028 CATControl::CATControl(  const CATString&             element, 
00029                          const CATString&               rootDir)
00030 : CATWidget(element, rootDir)
00031 {
00032     fEnabled             = true;
00033     fPressed             = false;
00034     fValue               = 0.0f;
00035     fDefValue            = 0.0f;
00036     fMinValue            = 0.0f;
00037     fMaxValue            = 1.0f;
00038     fForegroundColor     = CATColor(0,0,0);
00039     fBackgroundColor     = CATColor(192,192,192);
00040     fFgDisColor          = CATColor(192,192,192);
00041     fBgDisColor          = CATColor(128,128,128);
00042     fImageDisabled       = 0;
00043     fImagePressed        = 0;
00044     fImageFocus          = 0;   
00045     fImageActive         = 0;
00046     fActive              = false;
00047     fFocused             = false;
00048     fShowBg              = false;
00049     fMultiline           = false;
00050     fAutoScaleText       = false;
00051     fTextCentered        = false;
00052     fTextOffset.x        = 0;
00053     fTextOffset.y        = 0;
00054     fTextOffsetPressed.x = 2;
00055     fTextOffsetPressed.y = 2;
00056 }
00057 
00058 //---------------------------------------------------------------------------
00059 CATResult CATControl::Load(     // Optional progress callback information
00060                            CATPROGRESSCB                progressCB,
00061                            void*                            progressParam,
00062                            CATFloat32                       progMin,
00063                            CATFloat32                       progMax)
00064 {
00065     CATResult result = CATGuiObj::Load(progressCB, progressParam, progMin, progMax);
00066 
00067 
00068     if (CATFAILED(result))
00069     {
00070         return result;
00071     }
00072 
00073     // Sanity check images
00074     if (fImage != 0)
00075     {
00076         CATResult testResult = CAT_SUCCESS;
00077 
00078         if (fImageDisabled)
00079         {
00080             testResult = CheckImageSize(fImageDisabled);
00081             if (testResult != CAT_SUCCESS)
00082                 result = testResult;
00083         }
00084 
00085         if (fImageFocus)
00086         {
00087             testResult = CheckImageSize(fImageFocus);
00088             if (testResult != CAT_SUCCESS)
00089                 result = testResult;
00090         }
00091 
00092         if (fImageActive)
00093         {
00094             testResult = CheckImageSize(fImageFocus);
00095             if (testResult != CAT_SUCCESS)
00096                 result = testResult;
00097         }
00098 
00099         if (fImagePressed)
00100         {
00101             testResult = CheckImageSize(fImagePressed);
00102             if (testResult != CAT_SUCCESS)
00103                 result = testResult;
00104         }
00105     }
00106 
00107     return result;
00108 }
00109 
00110 // CheckImageSize()
00111 // Checks an image against fImage. If their sizes do not match,
00112 // then it creates a matching one with a red background and copies the 
00113 // the available portion of image into it.
00114 //
00115 // This is done to allow the app to run with mismatched images, while
00116 // still making it obvious that the skin should be fixed.
00117 CATResult CATControl::CheckImageSize(CATImage *& image, const CATImage* baseImage)
00118 {
00119     const CATImage* checkImage = (baseImage == 0)?fImage:baseImage;
00120 
00121     // Check image size should only be called if we have multiple
00122     // images....
00123     if ((checkImage == 0) && (image != 0))
00124     {
00125         return CATRESULTDESC(CAT_ERR_CONTROL_NO_IMAGE, fName);
00126     }
00127     if ((checkImage->Width() != image->Width()) ||
00128         (checkImage->Height() != image->Height()))
00129     {      
00130         CATRect imageRect(0,0,checkImage->Width(), checkImage->Height());
00131         CATImage* newDisabled = 0;
00132 
00133         if (CATSUCCEEDED(CATImage::CreateImage(newDisabled,checkImage->Width(), checkImage->Height(),false,false)))
00134         {
00135             newDisabled->FillRect(imageRect, CATColor(255,0,0));
00136             newDisabled->CopyOver(image,
00137                 0,0,0,0,
00138                 CATMin(checkImage->Width(), image->Width()),
00139                 CATMin(checkImage->Height(), image->Height()));
00140             CATImage::ReleaseImage(image);
00141             image = newDisabled;
00142         }
00143         else
00144         {
00145             CATImage::ReleaseImage(image);
00146         }
00147 
00148         return CATRESULTDESC(CAT_STAT_CONTROL_IMAGE_SIZE_MISMATCH, fName);
00149     }
00150     return CAT_SUCCESS;
00151 }
00152 //---------------------------------------------------------------------------
00153 // CATControl destructor
00154 //---------------------------------------------------------------------------
00155 CATControl::~CATControl()
00156 {
00157     if (fImageDisabled)
00158     {
00159         CATImage::ReleaseImage(fImageDisabled);
00160     }
00161 
00162     if (fImagePressed)
00163     {
00164         CATImage::ReleaseImage(fImagePressed);      
00165     }
00166 
00167     if (fImageFocus)
00168     {
00169         CATImage::ReleaseImage(fImageFocus);
00170     }
00171 
00172     if (fImageActive)
00173     {
00174         CATImage::ReleaseImage(fImageActive);
00175     }
00176 }
00177 
00178 
00179 /// ParseAttributes() parses the known attributes for an object.
00180 CATResult CATControl::ParseAttributes()
00181 {
00182     CATResult result = CATWidget::ParseAttributes();
00183     CATString attrib;
00184 
00185     fText         = GetAttribute(L"Text");
00186     fMultiline    = GetAttribute(L"Multiline",fMultiline );
00187     fShowBg       = GetAttribute(L"ShowBG",   fShowBg    );
00188 
00189     fTextCentered = GetAttribute(L"TextCentered",fTextCentered);
00190     fTextOffset.x = GetAttribute(L"TextOffsetX",fTextOffset.x);
00191     fTextOffset.y = GetAttribute(L"TextOffsetY",fTextOffset.y);
00192 
00193     fTextOffsetPressed.x = GetAttribute(L"TextOffsetPressedX",fTextOffsetPressed.x);
00194     fTextOffsetPressed.y = GetAttribute(L"TextOffsetPressedY",fTextOffsetPressed.y);
00195 
00196     fFontName      = GetAttribute(L"FontName",fFontName);
00197     fFontSize      = GetAttribute(L"FontSize",fFontSize);
00198     fDefValue      = GetAttribute(L"DefValue",fDefValue);
00199     
00200     fAutoScaleText = GetAttribute(L"AutoScaleText",fAutoScaleText);
00201 
00202     fValue        = fDefValue;
00203     fMinValue     = GetAttribute(L"MinValue",fMinValue);
00204     fMaxValue     = GetAttribute(L"MaxValue",fMaxValue);
00205 
00206     if (fValue < fMinValue)
00207         fValue = fMinValue;
00208     if (fValue > fMaxValue)
00209         fValue = fMaxValue;
00210 
00211     fCmdString    = GetAttribute(L"Command",fCmdString);
00212     fTarget       = GetAttribute(L"Target", fTarget);
00213     fCmdType      = GetAttribute(L"CommandType", fCmdType);
00214     fCmdParam     = GetAttribute(L"Parameter", fCmdParam);
00215 
00216     attrib = GetAttribute(L"ColorBackDis");
00217     if (attrib.IsEmpty() == 0)
00218     {
00219         CATUInt32 rawColor = attrib.FromHex();
00220         this->fBgDisColor.r = (CATUInt8)((rawColor & 0xff0000) >>16);
00221         this->fBgDisColor.g = (CATUInt8)((rawColor & 0xff00) >>8);
00222         this->fBgDisColor.b = (CATUInt8)((rawColor & 0xff));
00223 
00224         this->fBgDisColor.a = 255;
00225     }
00226     attrib = GetAttribute(L"ColorForeDis");
00227     if (attrib.IsEmpty() == 0)
00228     {
00229         CATUInt32 rawColor = attrib.FromHex();
00230         this->fFgDisColor.r = (CATUInt8)((rawColor & 0xff0000) >>16);
00231         this->fFgDisColor.g = (CATUInt8)((rawColor & 0xff00) >>8);
00232         this->fFgDisColor.b = (CATUInt8)((rawColor & 0xff));
00233 
00234         this->fFgDisColor.a = 255;
00235     }
00236 
00237 
00238     CATCURSORTYPE cursorType = CATCURSOR_ARROW;
00239     attrib = GetAttribute(L"Cursor");
00240     // Select based on value here...
00241     if          (0 == attrib.Compare("NoAction"))  cursorType = CATCURSOR_NOACTION;
00242     else if (0 == attrib.Compare("Wait"))      cursorType = CATCURSOR_WAIT;
00243     else if (0 == attrib.Compare("Text"))      cursorType = CATCURSOR_TEXT;
00244     else if (0 == attrib.Compare("Hand"))      cursorType = CATCURSOR_HAND;
00245     else if (0 == attrib.Compare("LeftRight")) cursorType = CATCURSOR_LEFTRIGHT;
00246     else if (0 == attrib.Compare("TopBottom")) cursorType = CATCURSOR_TOPBOTTOM;
00247     else if (0 == attrib.Compare("Size"))      cursorType = CATCURSOR_SIZE;
00248     else if (0 == attrib.Compare("Move"))      cursorType = CATCURSOR_MOVE;
00249 
00250     this->fCursor.SetType(cursorType);
00251 
00252     attrib = GetAttribute(L"ImageDisabled");
00253     if (!attrib.IsEmpty())
00254     {
00255         CATResult tmpResult = LoadSkinImage(attrib,fImageDisabled);
00256         if (CATFAILED(tmpResult))
00257             result = tmpResult;
00258     }
00259     attrib = GetAttribute(L"ImagePressed");
00260     if (!attrib.IsEmpty())
00261     {
00262         CATResult tmpResult = LoadSkinImage(attrib,fImagePressed);
00263         if (CATFAILED(tmpResult))
00264             result = tmpResult;
00265     }
00266 
00267     attrib = GetAttribute(L"ImageFocus");
00268     if (!attrib.IsEmpty())
00269     {
00270         CATResult tmpResult = LoadSkinImage(attrib,fImageFocus);
00271         if (CATFAILED(tmpResult))
00272             result = tmpResult;
00273     }
00274 
00275     attrib = GetAttribute(L"ImageActive");
00276     if (!attrib.IsEmpty())
00277     {
00278         CATResult tmpResult = LoadSkinImage(attrib,fImageActive);
00279         if (CATFAILED(tmpResult))
00280             result = tmpResult;
00281     }
00282 
00283     return result;
00284 }
00285 
00286 //---------------------------------------------------------------------------
00287 // GetWindow() retrieves the parent window.
00288 //---------------------------------------------------------------------------
00289 CATWindow* CATControl::GetWindow() const
00290 {
00291     return ((CATGuiObj*)fParent)->GetWindow();
00292 }
00293 
00294 //---------------------------------------------------------------------------
00295 // GetValue() retrieves the value of the control.
00296 // \return CATFloat32 - control value 
00297 //---------------------------------------------------------------------------
00298 CATFloat32 CATControl::GetValue() const
00299 {
00300     return fValue;
00301 }
00302 
00303 void CATControl::SetString ( const CATString& text )
00304 {
00305     fText = text;
00306 }
00307 
00308 CATString CATControl::GetString () const
00309 {
00310     return fText;
00311 }
00312 
00313 //---------------------------------------------------------------------------
00314 // SetValue() sets the value of the control 
00315 // \param newValue - float from 0 to 1 for new value.
00316 //---------------------------------------------------------------------------
00317 void CATControl::SetValue(CATFloat32 newValue, bool sendCommand)
00318 {
00319     fValue = newValue;
00320 
00321     this->MarkDirty();
00322 
00323     if (sendCommand)
00324     {
00325         ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00326     }   
00327 }
00328 
00329 //---------------------------------------------------------------------------
00330 // ResetValue() resets the control to its default value
00331 //---------------------------------------------------------------------------
00332 void CATControl::ResetValue()
00333 {
00334     fValue = this->fDefValue;
00335     this->MarkDirty();
00336 }
00337 
00338 
00339 //---------------------------------------------------------------------------
00340 // GetColorFore() retrieves the foreground color for the control
00341 //---------------------------------------------------------------------------
00342 CATColor CATControl::GetColorFore() const
00343 {
00344     if (this->IsEnabled() == false)
00345     {
00346         return fFgDisColor;
00347     }
00348 
00349     return this->fForegroundColor;
00350 }
00351 
00352 //---------------------------------------------------------------------------
00353 // GetColorBack() retrieves the background color for the control
00354 //---------------------------------------------------------------------------
00355 CATColor CATControl::GetColorBack() const
00356 {
00357     if (this->IsEnabled() == false)
00358     {
00359         return fBgDisColor;
00360     }
00361     return this->fBackgroundColor;
00362 }
00363 
00364 //---------------------------------------------------------------------------
00365 // SetColorFore() sets the foreground color for the control
00366 //---------------------------------------------------------------------------
00367 void CATControl::SetColorFore(const CATColor& color)
00368 {
00369     this->fForegroundColor = color;
00370     this->MarkDirty();
00371 }
00372 
00373 void CATControl::SetColorForeDisabled(const CATColor& color)
00374 {
00375     this->fFgDisColor = color;
00376     this->MarkDirty();
00377 }
00378 
00379 //---------------------------------------------------------------------------
00380 // SetColorFore() sets the foreground color for the control
00381 //---------------------------------------------------------------------------
00382 void CATControl::SetColorBack(const CATColor& color)
00383 {
00384     this->fBackgroundColor = color;
00385     this->MarkDirty();
00386 }
00387 
00388 void CATControl::SetColorBackDisabled(const CATColor& color)
00389 {
00390     this->fBgDisColor = color;
00391     this->MarkDirty();
00392 }
00393 
00394 
00395 //---------------------------------------------------------------------------
00396 // GetCursor() retrieves the object's mouse cursor.
00397 //---------------------------------------------------------------------------
00398 CATCursor* CATControl::GetCursor()
00399 {
00400     return &this->fCursor;
00401 }
00402 
00403 //---------------------------------------------------------------------------
00404 // IsFocusable() returns true if the control can receive
00405 // focus, and false otherwise.
00406 //---------------------------------------------------------------------------
00407 bool CATControl::IsFocusable() const
00408 {
00409     if (this->IsVisible() == false)
00410         return false;
00411 
00412     return true;
00413 }
00414 
00415 //---------------------------------------------------------------------------
00416 // SetFocused() sets the control's focused state.
00417 //
00418 // \param focused - if true, then the control is given focus.
00419 //                  if false, focus is removed.      
00420 //---------------------------------------------------------------------------
00421 void CATControl::SetFocused(bool focused)
00422 {
00423     // Just in case it was pressed via key but not released
00424     this->fPressed = false;
00425 
00426     this->fFocused = focused;
00427     this->MarkDirty();
00428 }
00429 
00430 //---------------------------------------------------------------------------
00431 // SetActive() sets the control's active state.
00432 //
00433 // \param active - if true, then the control is under the mouse.
00434 //                  if false, active flag is removed.      
00435 //---------------------------------------------------------------------------
00436 void CATControl::SetActive(bool active)
00437 {
00438     if (fActive != active)
00439     {
00440         this->fActive = active;
00441         this->MarkDirty();
00442     }
00443 }
00444 
00445 //---------------------------------------------------------------------------
00446 // IsFocused() returns true if the control has the current focus.
00447 //---------------------------------------------------------------------------
00448 bool CATControl::IsFocused() const
00449 {
00450     return this->fFocused;
00451 }
00452 
00453 //---------------------------------------------------------------------------
00454 // IsPressed() returns true if the control is pressed
00455 //---------------------------------------------------------------------------
00456 bool CATControl::IsPressed() const
00457 {
00458     return this->fPressed;
00459 }
00460 
00461 //---------------------------------------------------------------------------
00462 // IsFocused() returns true if the control has the mouse over it.
00463 //---------------------------------------------------------------------------
00464 bool CATControl::IsActive() const
00465 {
00466     return this->fActive;
00467 }
00468 
00469 
00470 
00471 //---------------------------------------------------------------------------
00472 // Draw() draws the control into the parent window
00473 // \param dirtyRect - portion of control (in window coordinates)
00474 //        that requires redrawing.
00475 //---------------------------------------------------------------------------
00476 void CATControl::Draw(CATImage* image, const CATRect& dirtyRect)
00477 {
00478     if (this->IsVisible() == false)
00479     {
00480         return;
00481     }
00482 
00483     // sanity check parent image / dirty rectangle
00484     CATRect imgRect(0,0,image->Width(), image->Height());
00485     CATASSERT(imgRect.Inside(dirtyRect), "Update rect is outside of img rect!");
00486 
00487     // Find intersection between dirty rect and us
00488     CATRect drawRect;
00489     bool   drawn = false;
00490 
00491     // Gracefully degrade depending on flags and whether the images are
00492     // available.
00493     if (this->fRect.Intersect(dirtyRect, &drawRect))
00494     {  
00495         CATRect ourRect;
00496         if ( (this->IsEnabled() == false) && (this->fImageDisabled))
00497         {
00498             if (drawRect.Intersect(CATRect( fRect.left, 
00499                                             fRect.top, 
00500                                             fRect.left + fImageDisabled->Width(),
00501                                             fRect.top  + fImageDisabled->Height()),
00502                                     &ourRect))
00503             {
00504                 ourRect.Offset(-fRect.left, -fRect.top);
00505 
00506                 image->Overlay( this->fImageDisabled,
00507                                 drawRect.left, 
00508                                 drawRect.top, 
00509                                 ourRect.left,
00510                                 ourRect.top,
00511                                 ourRect.Width(),
00512                                 ourRect.Height());
00513                 drawn = true;
00514             }        
00515         }
00516         else 
00517         {
00518             if (this->IsPressed() && (this->fImagePressed))
00519             {
00520                 if (drawRect.Intersect(CATRect(fRect.left, 
00521                                                fRect.top, 
00522                                                fRect.left + fImagePressed->Width(),
00523                                                fRect.top  + fImagePressed->Height()),
00524                                        &ourRect))
00525                 {
00526                     ourRect.Offset(-fRect.left, -fRect.top);
00527 
00528                     image->Overlay( this->fImagePressed,
00529                                     drawRect.left,
00530                                     drawRect.top,
00531                                     ourRect.left,
00532                                     ourRect.top,
00533                                     ourRect.Width(),
00534                                     ourRect.Height());
00535                     drawn = true;
00536                 }
00537             }
00538 
00539             if ((!drawn) &&  (IsFocused() || IsPressed()) && (this->fImageFocus))
00540             {
00541                 if (drawRect.Intersect( CATRect(fRect.left, 
00542                                                 fRect.top, 
00543                                                 fRect.left + fImageFocus->Width(),
00544                                                 fRect.top  + fImageFocus->Height()),
00545                                         &ourRect))
00546                 {
00547                     ourRect.Offset(-fRect.left, -fRect.top);
00548 
00549                     image->Overlay( this->fImageFocus,
00550                                     drawRect.left,
00551                                     drawRect.top,
00552                                     ourRect.left,
00553                                     ourRect.top,
00554                                     ourRect.Width(),
00555                                     ourRect.Height());
00556                     drawn = true;
00557                 }
00558             }
00559 
00560 
00561             if ((!drawn) && (IsActive() && (this->fImageActive)))
00562             {
00563                 if (drawRect.Intersect( CATRect(fRect.left, 
00564                                                 fRect.top, 
00565                                                 fRect.left + fImageActive->Width(),
00566                                                 fRect.top  + fImageActive->Height()),
00567                                         &ourRect))
00568                 {
00569                     ourRect.Offset(-fRect.left, -fRect.top);
00570 
00571                     image->Overlay( this->fImageActive,
00572                                     drawRect.left,
00573                                     drawRect.top,
00574                                     ourRect.left,
00575                                     ourRect.top,
00576                                     ourRect.Width(),
00577                                     ourRect.Height());
00578                     drawn = true;
00579                 }
00580             }
00581         }
00582 
00583         if ((!drawn) && (this->fImage != 0))
00584         {
00585             if (drawRect.Intersect( CATRect(fRect.left, 
00586                                             fRect.top, 
00587                                             fRect.left + fImage->Width(),
00588                                             fRect.top  + fImage->Height()),
00589                                     &ourRect))
00590             {
00591                 ourRect.Offset(-fRect.left, -fRect.top);
00592 
00593                 image->Overlay( this->fImage,
00594                                 drawRect.left,
00595                                 drawRect.top,
00596                                 ourRect.left,
00597                                 ourRect.top,
00598                                 ourRect.Width(),
00599                                 ourRect.Height());
00600                 drawn = true;
00601             }
00602         }
00603 
00604         if (!drawn)
00605         {
00606             // No images or fallbacks.  Ok, fine, draw a manual box.
00607 
00608             if (this->IsEnabled() == false)
00609             {
00610                 image->FillRect(drawRect, this->fBgDisColor);
00611             }
00612             else
00613             {
00614                 image->FillRect(drawRect, this->fBackgroundColor);
00615             }
00616         }
00617     }
00618 }
00619 
00620 //---------------------------------------------------------------------------
00621 // PostDraw() draws the control into the parent window using OS-specific
00622 // code.
00623 // \param dirtyRect - portion of control (in window coordinates)
00624 //        that requires redrawing.
00625 //---------------------------------------------------------------------------
00626 void CATControl::PostDraw(CATDRAWCONTEXT context, const CATRect& dirtyRect)
00627 {
00628     if (this->IsVisible() == false)
00629     {
00630         return;
00631     }
00632 
00633     if ((fText.IsEmpty() == false) || (fShowBg))
00634     {
00635         CATColor foreColor(fForegroundColor);
00636         CATColor backColor(fBackgroundColor);
00637 
00638         if (this->IsEnabled() == false)
00639         {
00640             foreColor = fFgDisColor;      
00641             backColor = fBgDisColor;
00642         }
00643 
00644         CATRect textRect = fRect;
00645 
00646         if (this->fPressed)
00647         {
00648             textRect.top  += fTextOffsetPressed.x;
00649             textRect.left += fTextOffsetPressed.y;
00650         }
00651         else
00652         {
00653             textRect.top  += fTextOffset.x;
00654             textRect.left += fTextOffset.y;
00655         }
00656         
00657         CATRect dRect = dirtyRect;
00658         this->GetWindow()->WidgetToWindow(this,textRect);
00659         this->GetWindow()->WidgetToWindow(this,dRect);
00660 
00661         this->GetWindow()->PostDrawText(fText,
00662                                         context, 
00663                                         textRect, 
00664                                         dRect,
00665                                         foreColor,
00666                                         fFontName,
00667                                         fFontSize,
00668                                         fMultiline,
00669                                         fShowBg?&backColor:0,
00670                                         false,
00671                                         fTextCentered,
00672                                         fAutoScaleText);
00673     }
00674 }
00675 
00676 
00677 //---------------------------------------------------------------------------
00678 // GetCommand() returns the command for the control
00679 //---------------------------------------------------------------------------
00680 CATCommand CATControl::GetCommand() const
00681 {
00682     return CATCommand(  this->fCmdString, 
00683                         this->GetValue(), 
00684                         this->fCmdParam, 
00685                         this->fTarget, 
00686                         this->fCmdType);
00687 }
00688 
00689 //---------------------------------------------------------------------------
00690 // 
00691 //---------------------------------------------------------------------------
00692 CATResult CATControl::OnEvent(const CATEvent& event, CATInt32& retVal)
00693 {
00694     switch (event.fEventCode)
00695     {
00696     case  CATEVENT_GUI_VAL_CHANGE:
00697         // If our command string is the same as the GUI value, then
00698         // we should ensure that our value matches the one
00699         // in the event.
00700         //
00701         // fStringParam1 - command string
00702         // fStringParam2 - String parameter of command
00703         // fStringParam3 - String value of command, or empty if none
00704         // fFloatParam1  - Value of control
00705         // fVoidParam - ptr to control that caused it, or null.
00706         if (this->fCmdString.Compare(event.fStringParam1) == 0)
00707         {
00708             // MAke sure we're not the control that sent it...
00709             if (this != (CATControl*)event.fVoidParam)
00710             {
00711 
00712                 // Don't send another command!
00713                 this->SetValue(event.fFloatParam1, false);
00714                 retVal++;
00715             }
00716         }
00717         break;
00718 
00719     case  CATEVENT_GUI_VAL_CHANGE_MATCHPARAM_ONLY:
00720         // As above, except only reflect change if parameter
00721         // matches as well as command string.
00722         //
00723         // fStringParam1 - command string
00724         // fStringParam2 - String parameter of command
00725         // fFloatParam1  - Value of control
00726         // fVoidParam - ptr to control that caused it, or null.
00727         if (this->fCmdString.Compare(event.fStringParam1) == 0)
00728         {
00729             if (this->fCmdParam.Compare(event.fStringParam2) == 0)
00730             {
00731                 // MAke sure we're not the control that sent it...
00732                 if (this != (CATControl*)event.fVoidParam)
00733                 {
00734 
00735                     // Don't send another command!
00736                     this->SetValue(event.fFloatParam1, false);
00737                     retVal++;
00738                 }
00739             }
00740         }
00741         break;
00742     case CATEVENT_TAB_SHOW:
00743     case CATEVENT_TAB_HIDE:
00744         this->MarkDirty(0,true);
00745         break;
00746 
00747     default:
00748         break;
00749     }
00750 
00751     return CATWidget::OnEvent(event,retVal);   
00752 }
00753 
00754 //---------------------------------------------------------------------------
00755 void CATControl::TrackMouseMove(const CATPOINT& point, bool leftButton, CATMODKEY modKey)
00756 {
00757     if (leftButton)
00758     {
00759 
00760         if (fPressed && (!this->fRect.InRect(point)))
00761         {
00762             this->fPressed = false;
00763             this->fActive  = true;
00764             this->MarkDirty();
00765         }
00766         else if ((!fPressed) && (this->fRect.InRect(point)))
00767         {
00768             this->fPressed = true;
00769             this->fActive  = false;
00770             this->MarkDirty();
00771         }   
00772     }
00773 }
00774 
00775 void CATControl::TrackMouseTimer(CATMODKEY modKey)
00776 {
00777 }
00778 
00779 //---------------------------------------------------------------------------
00780 
00781 void CATControl::TrackMouseWheel(  const CATPOINT& point,
00782                                  CATFloat32        wheelMove,
00783                                  CATMODKEY       modKey)
00784 {
00785     // Will this one be used?   
00786 }      
00787 //---------------------------------------------------------------------------
00788 void CATControl::TrackMouseDown(const CATPOINT& point, CATMODKEY modKey)
00789 {   
00790     this->fPressed = true;
00791     this->fActive = false;
00792     this->MarkDirty();
00793 }      
00794 //---------------------------------------------------------------------------
00795 void CATControl::TrackMouseRelease(const CATPOINT& point, CATMODKEY modKey)
00796 {   
00797     if (this->fRect.InRect(point))
00798     {
00799         // Received a click
00800         this->OnMouseClick();
00801         this->fActive  = true;
00802         this->fPressed = false;
00803         this->MarkDirty();
00804     }
00805     else
00806     {         
00807         this->fActive  = false;
00808         this->fPressed = false;
00809         this->MarkDirty();
00810     }                  
00811 }      
00812 //---------------------------------------------------------------------------
00813 // Mouse wheel over control, but not already tracked
00814 
00815 void CATControl::OnMouseWheel( const CATPOINT& point,
00816                               CATFloat32        wheelMove,
00817                               CATMODKEY         modKey)
00818 {
00819 
00820 }
00821 //---------------------------------------------------------------------------
00822 void CATControl::OnMouseClick()
00823 {   
00824     this->SetFocused(false);
00825     ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00826 }
00827 
00828 void CATControl::ResetCursorToDefault()
00829 {
00830     CATString value = this->GetAttribute(L"Cursor");
00831     CATCURSORTYPE cursorType = CATCURSOR_ARROW;
00832     // Select based on value here...
00833     if (0 == value.Compare("NoAction"))       cursorType = CATCURSOR_NOACTION;
00834     else if (0 == value.Compare("Wait"))      cursorType = CATCURSOR_WAIT;
00835     else if (0 == value.Compare("Text"))      cursorType = CATCURSOR_TEXT;
00836     else if (0 == value.Compare("Hand"))      cursorType = CATCURSOR_HAND;
00837     else if (0 == value.Compare("LeftRight")) cursorType = CATCURSOR_LEFTRIGHT;
00838     else if (0 == value.Compare("TopBottom")) cursorType = CATCURSOR_TOPBOTTOM;
00839     else if (0 == value.Compare("Size"))      cursorType = CATCURSOR_SIZE;
00840     else if (0 == value.Compare("Move"))      cursorType = CATCURSOR_MOVE;
00841 
00842     this->fCursor.SetType(cursorType);
00843 }
00844 
00845 void CATControl::OnKeyDown(const CATKeystroke& keystroke)
00846 {
00847     if (keystroke.GetNormalKey() == 0x20)
00848     {
00849         this->fPressed = true;
00850         this->fActive = false;
00851         this->MarkDirty();
00852     }         
00853 }
00854 
00855 void CATControl::OnKeyUp(const CATKeystroke& keystroke)
00856 {
00857     if (this->fPressed)
00858     {
00859         if (keystroke.GetNormalKey() == 0x20)
00860         {         
00861             this->fPressed = false;
00862             this->fActive =  false;
00863             this->MarkDirty();         
00864             this->OnMouseClick();
00865         }
00866     }
00867 }
00868 
00869 void CATControl::OnKeyPress(const CATKeystroke& keystroke)
00870 {
00871 }
00872 
00873 // OnParentCreate() is called when the parent window is created.
00874 // 
00875 // Most controls won't need this, but any that create their own
00876 // windows should do so at this point.
00877 void CATControl::OnParentCreate()
00878 {
00879 }
00880 
00881 
00882 // OnParentDestroy() is called as the parent window is destroyed.
00883 //
00884 // Controls that create their own windows during OnParentCreate()
00885 // should destroy them during OnParentDestroy()
00886 void CATControl::OnParentDestroy()
00887 {
00888 }
00889 
00890 // Bounds check the value, return true if was already good, false if had to change.
00891 bool CATControl::BoundsCheckValue()
00892 {
00893     bool wasValid = true;
00894 
00895     if (fValue > fMaxValue)
00896     {
00897         fValue = fMaxValue;
00898         wasValid = false;
00899     }
00900     if (fValue < fMinValue)
00901     {
00902         fValue = fMinValue;
00903         wasValid = false;
00904     }
00905     return wasValid;
00906 }
00907 
00908 CATFloat32 CATControl::GetValPercent()
00909 {
00910     BoundsCheckValue();
00911     return (fValue - fMinValue) / (fMaxValue - fMinValue);
00912 }
00913 
00914 CATFloat32 CATControl::GetValRange()
00915 {
00916     return fMaxValue - fMinValue;
00917 }
00918 
00919 CATFloat32 CATControl::GetMinValue()
00920 {
00921     return fMinValue;
00922 }
00923 CATFloat32 CATControl::GetMaxValue()
00924 {
00925     return fMaxValue;
00926 }
00927 
00928 void CATControl::SetMinMax(CATFloat32 minValue, CATFloat32 maxValue)
00929 {  
00930     CATASSERT( minValue <= maxValue, "Min/Max are flipped!");
00931     if (minValue > maxValue)
00932     {
00933         CATSwap(minValue,maxValue);
00934     }
00935 
00936     fMinValue = minValue;
00937     fMaxValue = maxValue;
00938     BoundsCheckValue();
00939     this->MarkDirty();
00940 }
00941 
00942 void CATControl::GetFontInfo(CATString& fontName, CATFloat32& fontSize)
00943 {
00944     fontName = fFontName;
00945     fontSize = fFontSize;
00946 }
00947 
00948 // OnMouseDoubleClick() is called the mouse is double clicked.
00949 //
00950 // \param modKey - Key state modifiers for ctrl/shift/alt/etc.
00951 void CATControl::OnMouseDoubleClick(CATMODKEY modKey)
00952 {
00953     // By default, treat as another click.
00954     this->OnMouseClick();
00955 }
00956 
00957 CATControl* CATControl::HitTest(const CATPOINT& point)
00958 {
00959     if (IsEnabled() && IsFocusable() && GetRect().InRect(point) )
00960     {            
00961         return this;
00962     }
00963 
00964     return 0;
00965 }
00966 
00967 
00968 bool CATControl::ForEachControl(CATCONTROLFUNCB callback, void* userParam)
00969 {
00970     // Call the control callback, since we're a control...
00971     if (callback)
00972     {
00973         return callback(this,userParam);
00974     }
00975 
00976     return true;
00977 }
00978 
00979 /// Utility func to filter '&'s and make them appear in menus
00980 /// instead of becoming menu mnemonics.
00981 CATString CATControl::FilterGUIString( const CATString& unfiltered)
00982 {
00983     CATString tmpText = unfiltered;     
00984 
00985 #ifdef _WIN32
00986     // Filter out hotkeys for now. We need '&'s in name strings
00987     // that aren't *only* used for menus.
00988     CATUInt32 offset = 0;           
00989     if (tmpText.Find( '&',offset))
00990     {
00991         tmpText = "";               
00992         CATUInt32 numChars = unfiltered.LengthCalc();
00993         for (CATUInt32 iChar = 0; iChar < numChars; iChar++)
00994         {
00995             CATWChar curChar = unfiltered.GetWChar(iChar);
00996             if (curChar == '&')
00997             {
00998                 tmpText << "&&";
00999             }
01000             else
01001             {
01002                 tmpText << curChar;
01003             }
01004         }               
01005     }
01006 #endif
01007 
01008     return tmpText;
01009 }
01010 
01011 void CATControl::OnRightMouseClick()
01012 {
01013     // Nadda for most controls.
01014     // Probably add edit-box type value mod for knob/slider 
01015 }
01016 
01017 CATUInt32 CATControl::GetAccessState()
01018 {
01019     CATUInt32 state = CATGuiObj::GetAccessState();
01020 
01021     state |= this->IsActive()    ? CAT_STATE_SYSTEM_HOTTRACKED:0;
01022     state |= this->IsPressed()   ? CAT_STATE_SYSTEM_PRESSED:0;
01023     state |= this->IsFocused()   ? CAT_STATE_SYSTEM_FOCUSED:0;
01024     state |= this->IsFocusable() ? CAT_STATE_SYSTEM_FOCUSABLE:0;
01025 
01026     return state;
01027 }

Generated on Mon Feb 11 04:09:52 2008 for Game Accessibility Suite by doxygen 1.5.4