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

CATTreeCtrl_Win32.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file GGTreeCtrl_Win32.cpp
00003 /// \brief List box for GUI (win32 implementation)
00004 /// \ingroup CATGUI
00005 ///
00006 /// Copyright (c) 2004-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-23 01:43:25 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //
00014 //
00015 #include "CATTreeCtrl.h"
00016 #include "CATWindow.h"
00017 #include "CATResultCore.h"
00018 #include "CATEventDefs.h"
00019 #include "CATApp.h"
00020 
00021 CATTreeCtrl::CATTreeCtrl(  const CATString&             element, 
00022                          const CATString&             rootDir)
00023                          : CATControlWnd(element,  rootDir)
00024 {
00025     this->fValue = 1.0f;
00026     this->fCurSel = 0;
00027     fFont = 0;   
00028     fImageList = 0;
00029 
00030     this->fWindowStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_DISABLEDRAGDROP; //TVS_SINGLEEXPAND
00031     this->fWindowType = WC_TREEVIEW;
00032 
00033     // Initialize common win32 controls
00034     INITCOMMONCONTROLSEX initStruct;
00035     initStruct.dwICC = ICC_TREEVIEW_CLASSES;
00036     initStruct.dwSize = sizeof(initStruct);
00037     ::InitCommonControlsEx(&initStruct);
00038     fRespondSelect = true;
00039 }
00040 
00041 
00042 bool CATTreeCtrl::OnControlEvent( const CATEvent& event,  CATInt32& result)                                       
00043 {
00044 
00045     return false;
00046 }
00047 
00048 void CATTreeCtrl::OnParentCreate()
00049 {   
00050     CATControlWnd::OnParentCreate();
00051     TreeView_SetBkColor  (fControlWnd, RGB(this->fBackgroundColor.r, this->fBackgroundColor.g, this->fBackgroundColor.b));
00052     TreeView_SetTextColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00053     TreeView_SetLineColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00054     TreeView_SetInsertMarkColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00055 
00056     fFont = GetWindow()->OSGetFont(fFontName,fFontSize);   
00057     ::SendMessage(fControlWnd,WM_SETFONT,(WPARAM)fFont,TRUE);         
00058 
00059     OSClearTree();
00060     OSRebuildTree(&fRootList);
00061     ExpandRoot();
00062 }
00063 
00064 void CATTreeCtrl::ExpandRoot()
00065 {
00066     if (fRootList.size() > 0)
00067     {
00068         CATTREEINFO* firstItem = fRootList[0];
00069         this->ExpandItem(firstItem,true);
00070     }
00071 }
00072 
00073 void CATTreeCtrl::OnParentDestroy()
00074 {
00075     CATControlWnd::OnParentDestroy();
00076 
00077     if (fFont != 0)
00078     {
00079         GetWindow()->OSReleaseFont(fFont);
00080         fFont = 0;
00081     }
00082 }
00083 
00084 void CATTreeCtrl::OSRebuildTree(std::vector<CATTREEINFO*>* curList)
00085 {
00086     if (curList == 0)
00087         return;
00088 
00089     fRespondSelect = false;
00090 
00091     CATUInt32 numItems = curList->size();
00092     for (CATUInt32 i = 0; i < numItems; i++)
00093     {
00094         CATTREEINFO* curInfo = (*curList)[i];
00095         OSAddItem(curInfo->DisplayText, curInfo, i);
00096 
00097         if (curInfo->Children.size() > 0)
00098         {
00099             OSRebuildTree(&curInfo->Children);
00100         }
00101     }
00102 
00103     fRespondSelect = true;
00104 }
00105 
00106 void CATTreeCtrl::OSClearTree()
00107 {
00108     TreeView_DeleteAllItems(fControlWnd);
00109 }
00110 
00111 CATResult CATTreeCtrl::OSAddItem( const CATString& displayText, CATTREEINFO* listInfo, CATUInt32 index)
00112 {
00113     TVINSERTSTRUCT tvi;
00114     memset(&tvi,0,sizeof(tvi));
00115 
00116     if (listInfo->Parent)
00117     {
00118         tvi.hParent = (HTREEITEM)listInfo->Parent->ItemHandle;  
00119     }
00120     else
00121     {
00122         tvi.hParent = 0;      
00123     }
00124 
00125     if (index == -1)
00126     {
00127         tvi.hInsertAfter = TVI_LAST;
00128     }
00129     else if (index == 0)
00130     {
00131         tvi.hInsertAfter = TVI_FIRST;
00132     }
00133     else if (listInfo->Parent == 0)
00134     {
00135         tvi.hInsertAfter = TVI_ROOT;      
00136     }
00137     else
00138     {
00139         CATTREEINFO* prevItem = listInfo->Parent->Children[index-1];
00140         tvi.hInsertAfter = (HTREEITEM)prevItem->ItemHandle;
00141     }
00142     CATString tmpDisplay = displayText;
00143 
00144     tvi.item.mask = TVIF_TEXT | TVIF_PARAM;
00145     tvi.item.pszText = tmpDisplay.GetUnicodeBuffer();
00146     tvi.item.lParam = (long)listInfo;
00147 
00148     if ((listInfo->ImageNumber != -1) && (fImageList != 0))
00149     {
00150         tvi.item.iImage = listInfo->ImageNumber;
00151         tvi.item.mask |= TVIF_IMAGE;
00152     }
00153 
00154     listInfo->ItemHandle = (long)TreeView_InsertItem(fControlWnd,&tvi);
00155     tmpDisplay.ReleaseBuffer();
00156 
00157     if (listInfo->ItemHandle == 0)
00158     {
00159         CATASSERT(false,"Unable to insert item into tree!");
00160         return CATRESULT(CAT_ERR_TREE_INSERT_ERROR);
00161     }
00162     return CAT_SUCCESS;
00163 }
00164 
00165 CATResult CATTreeCtrl::OSRemoveItem( CATTREEINFO* treeItem )
00166 {
00167     if (TreeView_DeleteItem(fControlWnd, treeItem->ItemHandle))
00168     {
00169         return CAT_SUCCESS;
00170     }
00171     return CATRESULT(CAT_ERR_TREE_REMOVE_ERROR);
00172 }
00173 
00174 CATTREEINFO* CATTreeCtrl::OSGetCurSel()
00175 {
00176     HTREEITEM itemHndl = TreeView_GetSelection(fControlWnd);
00177     if (itemHndl == 0)
00178     {
00179         return 0;
00180     }
00181     TVITEM item;
00182     memset(&item,0,sizeof(item));
00183     item.hItem = itemHndl;
00184     item.mask = TVIF_PARAM;
00185 
00186     if (TreeView_GetItem(fControlWnd,&item))
00187     {
00188         CATTREEINFO* treeInfo = (CATTREEINFO*)item.lParam;
00189         return treeInfo;
00190     }
00191 
00192     CATASSERT(false,"Get selection failed, but something seems to be selected!");
00193     return 0;
00194 }
00195 
00196 CATResult CATTreeCtrl::OSSetCurSel( CATTREEINFO* newSel )
00197 {
00198     this->fRespondSelect = false;
00199 
00200     if (newSel != 0)   
00201     {
00202         TreeView_SelectItem(fControlWnd, newSel->ItemHandle);
00203     }
00204     else
00205     {
00206         TreeView_SelectItem(fControlWnd, 0);
00207     }
00208 
00209     this->fRespondSelect = true;
00210     return CAT_SUCCESS;
00211 }
00212 
00213 CATResult CATTreeCtrl::OSUpdateText( CATTREEINFO* treeItem)
00214 {
00215     if (!treeItem)
00216     {
00217         return CATRESULT(CAT_ERR_INVALID_PARAM);
00218     }
00219 
00220 
00221     TVITEM tvItem;
00222     memset(&tvItem,0,sizeof(tvItem));
00223     CATString tmpText = treeItem->DisplayText;
00224     tvItem.mask    = TVIF_TEXT;
00225     tvItem.hItem   = (HTREEITEM)treeItem->ItemHandle;
00226     tvItem.cchTextMax = tmpText.Length();
00227     tvItem.pszText = tmpText.GetUnicodeBuffer();
00228 
00229     if (TreeView_SetItem( fControlWnd, &tvItem))
00230     {
00231         tmpText.ReleaseBuffer();
00232         return CAT_SUCCESS;
00233     }
00234     tmpText.ReleaseBuffer();
00235     return CATRESULT(CAT_ERR_TREE_SET_ITEM_ERROR);
00236 }
00237 
00238 // OS-specific event handling from window
00239 CATResult CATTreeCtrl::OSEvent( const CATEvent& event, CATInt32& retVal)
00240 {
00241     // Hmm... this could be boiled down to a lot less levels, but
00242     // we may want to look at other events in a bit... leave for now,
00243     // fix levels later if unused. - MAEDEBUG   
00244     switch (event.fEventCode)
00245     {      
00246     case CATEVENT_ENABLE_CHANGE:
00247         {
00248 #ifdef _WIN32
00249             if (fControlWnd)
00250             {
00251                 ::EnableWindow(fControlWnd, this->IsEnabled()?TRUE:FALSE);
00252                 OSUpdateTreeColors();
00253             }
00254 #endif
00255         }   
00256         break;
00257     case CATEVENT_TAB_SHOW:
00258         {
00259             if ((fControlWnd) && fVisible)
00260             {
00261                 ::ShowWindow(fControlWnd,SW_SHOW);                                
00262                 this->OSUpdateTreeColors();
00263                 this->MarkDirty();
00264             }
00265         }
00266         break;
00267 
00268     case CATEVENT_TAB_HIDE:
00269         if ((fControlWnd) && fVisible)
00270         {
00271             ::ShowWindow(fControlWnd,SW_HIDE);
00272             this->OSUpdateTreeColors();
00273             this->MarkDirty();
00274         }               
00275         break;
00276     case CATEVENT_WINDOWS_EVENT:
00277         switch (event.fIntParam2)
00278         {
00279         case WM_NOTIFY:
00280             {
00281                 LPNMHDR notifyHdr = (LPNMHDR)event.fIntParam4;
00282                 if (notifyHdr->hwndFrom == this->fControlWnd)
00283                 {
00284                     switch (notifyHdr->code)
00285                     {
00286                     case NM_CUSTOMDRAW:
00287                         {
00288                             NMTVCUSTOMDRAW* custDraw = (NMTVCUSTOMDRAW*)event.fIntParam4;
00289                             switch (custDraw->nmcd.dwDrawStage)
00290                             {
00291                             case CDDS_PREPAINT:
00292                                 retVal = CDRF_NOTIFYITEMDRAW;
00293                                 break;
00294                             case CDDS_ITEMPREPAINT:
00295                                 retVal = CDRF_NEWFONT;
00296                                 if (custDraw->nmcd.uItemState & CDIS_SELECTED)
00297                                 {
00298                                     custDraw->clrText       = RGB(this->fBackgroundColor.r, this->fBackgroundColor.g, this->fBackgroundColor.b);
00299                                     custDraw->clrTextBk  = RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b);
00300                                 }
00301                                 break;
00302                             }
00303                         }
00304                         break;
00305 
00306                     case TVN_SELCHANGED:
00307                         {                           
00308                             if (fRespondSelect)
00309                             {
00310                                 NMTREEVIEW* tView = (NMTREEVIEW*)event.fIntParam4;
00311                                 CATTREEINFO* info = (CATTREEINFO*)tView->itemNew.lParam;
00312 
00313                                 if (info != 0)
00314                                 {
00315                                     this->fCurSel = info;
00316                                     this->SetString(info->DisplayText);
00317 
00318                                     this->fRespondSelect = false;
00319 
00320                                     ((CATGuiObj*)fParent)->OnCommand(this->GetCommand(), this);
00321 
00322                                     this->fRespondSelect = true;
00323 
00324                                     this->GetWindow()->OSHideToolTip();
00325                                     return CAT_SUCCESS;
00326                                 }                              
00327                             }
00328                         }
00329                         break;
00330                     }
00331                 }
00332             }
00333             break;
00334         }
00335         break;
00336     }   
00337 
00338     return CATControlWnd::OnEvent(event,retVal);
00339 }
00340 
00341 CATResult CATTreeCtrl::ExpandItem(CATTREEINFO* item, bool expand)
00342 {
00343     if (item == 0)
00344     {
00345         return CATRESULT(CAT_ERR_INVALID_PARAM);
00346     }
00347     ::ShowWindow(fControlWnd,SW_HIDE);
00348     TreeView_Expand(fControlWnd, item->ItemHandle, expand?TVE_EXPAND:TVE_COLLAPSE);
00349     ::ShowWindow(fControlWnd,IsVisible()?SW_SHOW:SW_HIDE);
00350 
00351     return CAT_SUCCESS;   
00352 }
00353 
00354 void CATTreeCtrl::SetEnabled(bool enabled)
00355 {   
00356     CATControlWnd::SetEnabled(enabled);
00357     OSUpdateTreeColors();
00358 }
00359 
00360 void CATTreeCtrl::OSUpdateTreeColors()
00361 {
00362     if (this->IsEnabled())
00363     {
00364         TreeView_SetBkColor  (fControlWnd, RGB(this->fBackgroundColor.r, this->fBackgroundColor.g, this->fBackgroundColor.b));
00365         TreeView_SetTextColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00366         TreeView_SetLineColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00367         TreeView_SetInsertMarkColor(fControlWnd, RGB(this->fForegroundColor.r, this->fForegroundColor.g, this->fForegroundColor.b));
00368     }
00369     else
00370     {      
00371         TreeView_SetBkColor  (fControlWnd, RGB(this->fBgDisColor.r, this->fBgDisColor.g, this->fBgDisColor.b));
00372         TreeView_SetTextColor(fControlWnd, RGB(this->fFgDisColor.r, this->fFgDisColor.g, this->fFgDisColor.b));
00373         TreeView_SetLineColor(fControlWnd, RGB(this->fFgDisColor.r, this->fFgDisColor.g, this->fFgDisColor.b));
00374         TreeView_SetInsertMarkColor(fControlWnd, RGB(this->fFgDisColor.r, this->fFgDisColor.g, this->fFgDisColor.b));
00375     }  
00376 }
00377 
00378 CATResult CATTreeCtrl::UseImageList(CATUInt32 imageListId, CATUInt32 width, const CATColor& transparent)
00379 {
00380     if (fImageList)
00381     {
00382         TreeView_SetImageList(fControlWnd, 0,TVSIL_NORMAL);
00383         ImageList_Destroy(fImageList);
00384         fImageList = 0;
00385     }
00386 
00387     if (imageListId == 0)
00388     {
00389         return CAT_SUCCESS;
00390     }
00391 
00392     const int kMaxNumImageListItems = 16;
00393 
00394     fImageList = ImageList_LoadBitmap(  gApp->GetInstance(), 
00395         MAKEINTRESOURCE(imageListId), 
00396         width,
00397         kMaxNumImageListItems,
00398         RGB(transparent.r, transparent.g, transparent.b));
00399 
00400     if (fImageList != 0)
00401     {
00402         TreeView_SetImageList(fControlWnd, fImageList, TVSIL_NORMAL);
00403         return CAT_SUCCESS;
00404     }
00405 
00406     return CATRESULT(CAT_ERR_IMAGELIST_FAILED);
00407 }
00408 

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