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

CATMenu.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATMenu.cpp
00003 /// \brief Text label for GUI
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 "CATMenu.h"
00015 #include "CATWindow.h"
00016 #include "CATEventDefs.h"
00017 //---------------------------------------------------------------------------
00018 // CATMenu constructor (inherited from CATControl->CATXMLObject)
00019 // \param element - Type name ("Label")
00020 // \param attribs - attribute information for the window
00021 // \param parent - parent XML object (should be a "Window" element)
00022 // \param rootDir - root directory of skin for bin/png load
00023 //---------------------------------------------------------------------------
00024 CATMenu::CATMenu(    const CATString&             element, 
00025                      const CATString&             rootDir)
00026 : CATControl(element,  rootDir)
00027 {
00028     fUpdateTextOnSel = true;
00029     fShowSel         = false;
00030     fCurSel          = 0;   
00031     // Values are meaningless in menus
00032     fForceWidth      = false;
00033     fValue           = 0;
00034     fMinValue        = 0;
00035     fMaxValue        = 0;
00036     fMultiline       = false;
00037     fShowBg          = true;
00038     fMenuDirty       = true;
00039     fMenuId          = 0;
00040     fIdCount         = 100; // Arbitrary starting point for menu id's   
00041     fItalics         = false;
00042     fWindowBuilt     = false;
00043     fOutline         = true;
00044     fMenuStyle       = CATMENUSTYLE_DOWN;
00045 }
00046 
00047 //---------------------------------------------------------------------------
00048 // CATMenu destructor
00049 //---------------------------------------------------------------------------
00050 CATMenu::~CATMenu()
00051 {
00052     ClearMenu();
00053 
00054 }
00055 
00056 
00057 /// IsFocusable() returns true if the control can receive
00058 /// focus, and false otherwise.
00059 bool CATMenu::IsFocusable() const
00060 {
00061     return true;
00062 }
00063 
00064 /// ParseAttributes() parses the known attributes for an object.
00065 CATResult CATMenu::ParseAttributes()
00066 {
00067     CATResult result = CATControl::ParseAttributes();
00068     CATString attrib;
00069 
00070     fShowSel         = GetAttribute(L"ShowSelection",fShowSel);
00071     fUpdateTextOnSel = GetAttribute(L"UpdateText",   fUpdateTextOnSel);
00072     fOutline         = GetAttribute(L"Outline",      fOutline);
00073     fWindowBuilt     = GetAttribute(L"WindowBuilt",  fWindowBuilt);
00074     fForceWidth      = GetAttribute(L"ForceWidth",   fForceWidth);
00075 
00076     attrib = GetAttribute(L"MenuStyle");
00077     if (!attrib.IsEmpty())
00078     {
00079         if (attrib.Compare("Up") == 0)
00080         {
00081             fMenuStyle = CATMENUSTYLE_UP;
00082         }
00083         else if (attrib.Compare("Down") == 0)
00084         {
00085             fMenuStyle = CATMENUSTYLE_DOWN;
00086         }
00087         else if (attrib.Compare("UpMouse") == 0)
00088         {
00089             fMenuStyle = CATMENUSTYLE_UPMOUSE;
00090         }
00091         else if (attrib.Compare("DownMouse") == 0)
00092         {
00093             fMenuStyle = CATMENUSTYLE_DOWNMOUSE;
00094         }
00095         else
00096         {
00097             CATTRACE("Unknown menu style....");
00098         }
00099     }
00100 
00101     return result;
00102 }
00103 
00104 // PostDraw() draws any stuff that requires an OS-specific draw
00105 // context.
00106 void CATMenu::PostDraw(CATDRAWCONTEXT drawContext, const CATRect& dirtyRect)
00107 {
00108     if (this->IsVisible() == false)
00109     {
00110         return;
00111     }
00112 
00113 
00114     if ((fText.IsEmpty() == false) || (fImage == 0))
00115     {
00116         CATColor foreColor(fForegroundColor);
00117         CATColor backColor(fBackgroundColor);
00118 
00119         if (this->IsEnabled() == false)
00120         {
00121             foreColor = fFgDisColor;      
00122             backColor = fBgDisColor;
00123         }
00124 
00125         CATRect textRect = fRect;
00126 
00127         if (fOutline && (fImage == 0))
00128         {
00129 #ifdef _WIN32
00130             CATRect frameRect = fRect;
00131             this->GetWindow()->WidgetToWindow(this,frameRect);
00132 
00133             HBRUSH frameColor = (HBRUSH)(this->IsEnabled() ? (::GetStockObject(BLACK_BRUSH)) : 
00134                                                              (::GetStockObject(GRAY_BRUSH))) ;            
00135 
00136             ::FrameRect(drawContext, (RECT*)(CATRECT*)&frameRect,frameColor);
00137 
00138             textRect.left++;
00139             textRect.top++;
00140             textRect.right--;
00141             textRect.bottom--;
00142 #endif
00143         }
00144 
00145         if (this->fPressed)
00146         {
00147             textRect.top  += fTextOffsetPressed.x;
00148             textRect.left += fTextOffsetPressed.y;
00149         }
00150         else
00151         {
00152             textRect.top  += fTextOffset.x;
00153             textRect.left += fTextOffset.y;
00154         }
00155 
00156         CATRect dRect = dirtyRect;
00157         this->GetWindow()->WidgetToWindow(this,textRect);
00158         this->GetWindow()->WidgetToWindow(this,dRect);
00159         this->GetWindow()->PostDrawText( fText,
00160                                          drawContext, 
00161                                          textRect, 
00162                                          dRect,
00163                                          foreColor,
00164                                          fFontName,
00165                                          fFontSize,
00166                                          fMultiline,
00167                                          fShowBg?&backColor:0,
00168                                          fItalics,
00169                                          fTextCentered, fAutoScaleText);
00170     }
00171 }
00172 
00173 
00174 // SetString 
00175 void CATMenu::SetString ( const CATString& text )
00176 {
00177     if (text != fText)
00178     {
00179         fText = text;
00180         this->MarkDirty();
00181     }
00182 }
00183 
00184 // GetString retrieves current string, which is either the nulltext
00185 // or whatever is selected.
00186 CATString CATMenu::GetString () const
00187 {
00188     return fText;
00189 }
00190 
00191 CATResult CATMenu::OnEvent(const CATEvent& event, CATInt32& retVal)
00192 {
00193     return CATControl::OnEvent(event,retVal);
00194 }
00195 
00196 // Tree building / modification
00197 CATMENUITEM* CATMenu::GetCurItem()
00198 {
00199     return fCurSel;
00200 }
00201 
00202 CATResult CATMenu::SetCurItem(CATMENUITEM* item)
00203 {
00204     fMenuDirty = true;
00205 
00206 
00207     fCurSel = item;
00208 
00209     if ((item) && (fUpdateTextOnSel))
00210     {
00211         fText = item->DisplayText;
00212     }
00213 
00214     this->MarkDirty();
00215     return CAT_SUCCESS;
00216 }
00217 
00218 CATUInt32 CATMenu::GetNumTopNodes()
00219 {
00220     return this->fRootList.size();
00221 }
00222 
00223 CATMENUITEM* CATMenu::GetTopNode( CATUInt32 index )
00224 {
00225     if (index >= fRootList.size())
00226         return 0;
00227 
00228     return fRootList[index];
00229 }
00230 
00231 
00232 CATResult CATMenu::InsertSeperator( CATMENUITEM* parent, CATUInt32 index)
00233 {
00234     CATMENUITEM* sepItem = 0;
00235     return this->Insert( "",parent, sepItem,0,false,index);
00236 }
00237 
00238 CATResult CATMenu::Insert(    const CATString&        displayText,
00239                           CATMENUITEM*      parent,
00240                           CATMENUITEM*&     newItemRef,
00241                           void*            dataPtr,
00242                           bool             subMenu,
00243                           CATUInt32          index,
00244                           const CATString&       altCommand)
00245 {
00246     fMenuDirty = true;
00247 
00248     newItemRef = new CATMENUITEM;
00249 
00250     if (newItemRef == 0)
00251     {
00252         return CATRESULT(CAT_ERR_OUT_OF_MEMORY);
00253     }
00254 
00255     newItemRef->BaseMenu    = this;
00256     newItemRef->DataPtr     = dataPtr;
00257     newItemRef->DisplayText = displayText;
00258     newItemRef->IsSubMenu   = false;
00259     newItemRef->ItemHandle  = 0;
00260     newItemRef->Parent      = parent;
00261     newItemRef->IsSubMenu = subMenu;
00262     newItemRef->AltCommand = altCommand;
00263 
00264     if (parent)
00265     {
00266         parent->Children.push_back(newItemRef);
00267     }
00268     else
00269     {
00270         this->fRootList.push_back(newItemRef);      
00271     }
00272 
00273     return CAT_SUCCESS;
00274 }
00275 
00276 
00277 CATResult CATMenu::Remove( CATMENUITEM*&     item)
00278 {
00279     fMenuDirty = true;
00280 
00281     if (item == 0)
00282     {
00283         return CATRESULT(CAT_ERR_INVALID_PARAM);
00284     }
00285 
00286     CATUInt32 numChildren = 0;
00287 
00288     if (item->Parent)
00289     {
00290         std::vector<CATMENUITEM*>::iterator iter = item->Parent->Children.begin();
00291         while (iter != item->Parent->Children.end())
00292         {
00293             if (item == (*iter))
00294             {
00295                 iter = item->Parent->Children.erase(iter);
00296                 ClearMenuItem(item);
00297                 delete item;
00298                 item = 0;
00299                 return CAT_SUCCESS;
00300             }
00301             ++iter;
00302         }
00303     }
00304     else
00305     {
00306         std::vector<CATMENUITEM*>::iterator iter = fRootList.begin();      
00307         while (iter != fRootList.end())
00308         {
00309             if (item == (*iter))
00310             {
00311                 iter = fRootList.erase(iter);
00312                 ClearMenuItem(item);
00313                 delete item;
00314                 item = 0;
00315                 return CAT_SUCCESS;
00316             }
00317         }
00318     }   
00319     return CATRESULT(CAT_ERR_LIST_ITEM_NOT_FOUND);
00320 }
00321 
00322 void CATMenu::ClearMenuItem(CATMENUITEM* item)
00323 
00324 {
00325     while (item->Children.size())
00326     {
00327         CATMENUITEM* curItem = item->Children[0];
00328         ClearMenuItem(curItem);
00329         delete curItem;
00330         item->Children.erase(item->Children.begin());
00331     }
00332 }
00333 
00334 CATResult CATMenu::Clear()
00335 {
00336     while (fRootList.size())
00337     {
00338         CATMENUITEM* curItem = fRootList[0];
00339         ClearMenuItem(curItem);
00340         delete curItem;
00341         fRootList.erase(fRootList.begin());
00342     }
00343     fCurSel     = 0;   
00344 
00345     fMenuDirty  = true;
00346     return CAT_SUCCESS;
00347 }
00348 
00349 
00350 
00351 //---------------------------------------------------------------------------
00352 // GetCommand() returns the command for the control
00353 //---------------------------------------------------------------------------
00354 CATCommand CATMenu::GetCommand() const
00355 {   
00356     CATString stringParam = this->GetString();
00357 
00358     if (this->fCurSel)
00359     {
00360         stringParam = fCurSel->DisplayText;
00361     }
00362 
00363     return CATCommand(this->fCmdString, 1, stringParam, this->fTarget, this->fCmdType);
00364 }
00365 
00366 CATString CATMenu::GetHint() const
00367 {
00368     CATString retString;
00369     retString = CATControl::GetHint();
00370     if (fShowHintValue)
00371     {
00372         retString << " ( " << this->GetString() << " )";
00373     }
00374     return retString;
00375 }
00376 
00377 void CATMenu::SetItalics(bool italics)
00378 {
00379     fItalics = italics;
00380     this->MarkDirty();
00381 }
00382 
00383 bool CATMenu::GetItalics() const
00384 {
00385     return fItalics;
00386 }
00387 
00388 bool CATMenu::HasSelectionMark()
00389 {
00390     return fShowSel;
00391 }
00392 
00393 void CATMenu::OnMouseClick()
00394 {        
00395     if (fWindowBuilt == true)
00396     {
00397         this->GetWindow()->OnBuildMenu(this);
00398     }
00399 
00400     DoMenu();
00401 }
00402 
00403 bool CATMenu::ForceWidth() const
00404 {
00405     return fForceWidth;
00406 }

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