Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

treeView.cpp

Go to the documentation of this file.
00001 /* *************************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2003-2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 ************************************************************************************* */
00018 
00019 #include "3d.h"
00020 #include "input.h"
00021 #include "controlEngine.h"
00022 #include "treeView.h"
00023 
00024 extern ControlEngine ce;
00025 
00026 TreeView::TreeView()
00027 {
00028         posY=0;
00029 }
00030 
00031 void TreeView::Create()
00032 {
00033         faceNode = ce.primitive.AddBox("TreeView",id,10,FALSE);
00034 }
00035 
00036 void TreeView::Draw ()
00037 {
00038         ce.Push(this);
00039         ce.primitive.GoTo(FACE_FRONT);
00040         if (!faceNode->elem->computeSize.y) ce.primitive.ComputeLayout(faceNode);
00041         posY=2; 
00042         DrawRecursif(data.GetRoot(),2,0);
00043         ce.Pop();
00044 }
00045 
00046 int TreeView::DrawRecursif (TreeNode *node,double margeY,int level)
00047 {
00048         int nbElem=0;
00049         int nbElemFather;
00050         TreeNode *nodeChild;
00051 
00052         for (node->childs.i=0;node->childs.i.More();node->childs.i++)
00053         {
00054                 nodeChild = node->childs.i.GetElem();
00055                 if (posY+21>=faceNode->elem->computeSize.y) return nbElem;
00056 
00057                 // creation du boutton
00058                 Button *button;
00059                 button = new Button;
00060                 nodeChild->elem->idControl = button->id;
00061                 button->materialOnUp.SetDS(0.5f+level*0.1f,0.5f+level*0.1f,0.5f+level*0.1f);
00062                 button->materialOnDown.SetDS(0,0,1);
00063                 button->Create();
00064                 button->SetText(nodeChild->elem->text);
00065                 button->text.SetWrapping(TRUE,FALSE,FALSE);
00066                 button->XDockCenter(10,0);
00067                 button->YDockTop(margeY,19);
00068                 button->faceNode->Elems(FACE_RIGHT)->material=*button->materialCurrent;
00069 
00070                 if (nodeChild->elem->contentSub)
00071                 {       // a un sous repertoire (ou peut en posseder potentielement)
00072                         button->onButton.click.connect(this,OnClick);
00073                         button->text.SetAlign(align_begin,align_begin);
00074                         AddTreeButton(button,nodeChild->elem->isCollaps); // ajout du boutton '+' / '-'
00075                         if (!nodeChild->elem->isCollaps)
00076                         {
00077                                 ce.Push(button);
00078                                         ce.primitive.GoTo(FACE_FRONT);
00079                                         nbElemFather=DrawRecursif(nodeChild,21,level+1);
00080                                 ce.Pop();
00081                         }
00082                         else nbElemFather=0;
00083                         nbElem+=nbElemFather+1;
00084                         margeY+=(nbElemFather+1)*21;
00085                         posY+=21;
00086                         button->faceNode->elem->size.y.constValue+=nbElemFather*21;
00087                 }
00088                 else
00089                 {       // on est sur un noeud final
00090                         button->onButton.click.connect(this,OnClick);
00091                         button->text.SetAlign(align_begin,align_center);
00092                         margeY+=21;
00093                         posY+=21;
00094                         nbElem++;
00095                 }               
00096         }
00097         return nbElem;
00098 }
00099 
00101 
00102 TreeNode * TreeView::FindOnData(int idControl)
00103 {
00104         return FindOnData(idControl,data.GetRoot());
00105 }
00106 
00107 TreeNode * TreeView::FindOnData(int idControl,TreeNode *node)
00108 {
00109         if (!node) return NULL;
00110         if (node->elem->idControl==idControl) return node;
00111         TreeNode *nodeChild,*nodeFind;
00112         for (node->childs.i=0;node->childs.i.More();node->childs.i++)
00113         {
00114                 nodeChild = node->childs.i.GetElem();
00115                 nodeFind = FindOnData(idControl,nodeChild);
00116                 if (nodeFind) return nodeFind;
00117         }
00118         return NULL;
00119 }
00120 
00121 int TreeView::OnClick()
00122 {
00123         Control * control = ce.pickingCursor.controlHover;
00124         Test(control);
00125         return 0;
00126 }
00127 
00128 int TreeView::OnClickPlus()
00129 {
00130         Control * control = ce.pickingCursor.controlHover;
00131         Test(control->parent);  
00132         return 0;
00133 }
00134 
00135 void TreeView::Test(Control *control)
00136 {
00137         TreeNode *node = FindOnData(control->id);
00138         //char *text = control->faceNode->Elems(FACE_FRONT)->text->GetText();
00139 
00140         if (node->elem->contentSub) 
00141         {
00142                 node->elem->isCollaps^=1;
00143                 ce.timerRefresh.onTimer.connect(this,OnNextFrame); // ca sinon pb d'inter-blocage
00144         }
00145         onTreeView.select(node);
00146 }
00147 
00148 void TreeView::GetTreePath(TreeNode *treeNode,MyString &result,BOOL isFirst)
00149 {
00150         if (!treeNode) 
00151         {
00152                 result="";
00153                 return;
00154         }
00155         GetTreePath(treeNode->parent,result,FALSE);
00156         result<<treeNode->elem->text;
00157         if (!isFirst) result<<'/';
00158 }
00159 
00160 int TreeView::OnNextFrame(Timer *timer)
00161 {
00162         timer->onTimer.disconnect(this);
00163         ce.pickingCursor.ResetCurrent();
00164         DeleteSub();
00165         Draw();
00166         return 0;
00167 }
00168 
00169 void TreeView::ConfigFacesPlus(Control *control,BOOL isCollaps)
00170 {
00171         MyListIterator<FaceNode *> i(&control->faceNode->childs);
00172         Face *face;
00173         for (i=0;i.More();i++)
00174         {
00175                 face = i.GetElem()->elem;
00176                 face->texture=isCollaps?&ce.texture.plus:&ce.texture.minus;
00177         }
00178 }
00179 
00180 void TreeView::AddTreeButton(Button *button,BOOL isCollaps)
00181 {
00182         ce.Push(button);
00183         ce.primitive.GoTo(FACE_LEFT);
00184         Button *subButton;
00185         subButton = new Button;
00186         subButton->materialOnUp = button->materialOnUp;
00187         subButton->Create(12);
00188         subButton->XDockCenter(1);
00189         subButton->YDockTop(1,17);
00190         ConfigFacesPlus(subButton,isCollaps);
00191         ce.Pop();
00192 
00193         subButton->onButton.click.connect(this,OnClickPlus);
00194 }
00195 
00196 // *****************************************************************************
00197 
00198 TreeDirectory::TreeDirectory()
00199 {
00200         addFile=FALSE;
00201         addDirectory=TRUE;
00202         filter="*.*";
00203         beginPath="c:";
00204         onTreeView.select.connect(this,OnClickDirectory);
00205 }
00206 
00207 int TreeDirectory::OnClickDirectory(TreeNode *treeNode)
00208 {
00209         if (treeNode->elem->contentSub)
00210         {
00211                 ce.timerRefresh.onTimer.disconnect(this); // ca sinon pb d'inter-blocage
00212                 MyString pathFile;
00213                 GetTreePath(treeNode,pathFile);
00214                 treeNode->SuprChilds(TRUE);
00215                 Loading(treeNode,pathFile,TRUE);
00216                 ce.timerRefresh.onTimer.connect((TreeView*)this,OnNextFrame); // ca sinon pb d'inter-blocage
00217         }
00218         return 0;
00219 }
00220 
00221 void TreeDirectory::Config(BOOL _addFile,BOOL _addDirectory,char *_filter)
00222 {
00223         addFile = _addFile;
00224         addDirectory = _addDirectory;
00225         if (_filter) filter = _filter;
00226 }
00227 
00228 void TreeDirectory::Reset()
00229 {
00230         data.SuprAll();
00231         DeleteSub();
00232 }
00233 
00234 void TreeDirectory::Loading(char *name,BOOL isCollaps)
00235 {
00236         beginPath=name;
00237         // il faut au moin une racine
00238         if (!data.GetRoot()) 
00239         {
00240                 TreeViewItem item;
00241                 item.text=name;
00242                 data.Add(item);
00243         }
00244         Loading(data.GetRoot(),name,isCollaps);
00245 }
00246 
00247 void TreeDirectory::Loading(TreeNode *node,char *name,BOOL isCollaps)
00248 {
00249         HANDLE hFichCour;
00250         WIN32_FIND_DATA wfd ;
00251         MyString path;
00252         MyString temp;
00253         TreeViewItem item;
00254         TreeNode *newNode;
00255 
00256         path<<clear<<name<<"\\"<<filter;
00257         hFichCour = FindFirstFile (path, &wfd) ;
00258         if (hFichCour == INVALID_HANDLE_VALUE) return ;
00259         do 
00260         {
00261                 if (wfd.cFileName[0]=='.') continue;
00262                 BOOL isDir = (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
00263                 if (isDir)
00264                 {               
00265                         if (!addDirectory) continue;
00266                         temp<<clear<<name<<"\\"<<wfd.cFileName;
00267                         item.isCollaps=isCollaps;
00268                         item.contentSub=TRUE;
00269                         item.text=wfd.cFileName;
00270                         newNode = data.Add(item,node);
00271                         item.askSubData=isCollaps;
00272                         if (!isCollaps) Loading(newNode,temp,isCollaps);
00273                 }
00274                 else
00275                 {
00276                         if (!addFile) continue;
00277                         item.isCollaps=FALSE;
00278                         item.contentSub=FALSE;
00279                         item.text=wfd.cFileName;
00280                         data.Add(item,node);
00281                 }
00282         } 
00283         while (FindNextFile (hFichCour, &wfd ));
00284         FindClose (hFichCour) ;
00285 }

Generated on Fri Aug 20 19:19:51 2004 for 3d Controls by doxygen 1.3.6