00001 /*========================================================================= 00002 00003 Module: $RCSfile: vtkKWDirectoryExplorer.h,v $ 00004 00005 Copyright (c) Kitware, Inc. 00006 All rights reserved. 00007 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00008 00009 This software is distributed WITHOUT ANY WARRANTY; without even 00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00011 PURPOSE. See the above copyright notice for more information. 00012 00013 =========================================================================*/ 00014 // .NAME vtkKWDirectoryExplorer - a widget for exploring directories 00015 // .SECTION Description 00016 // This class contains a hierarchy structure to explore directories of a 00017 // file systems. It also includes a toolbar, which offers Back/Forward 00018 // browsing of visited directories and functions to Create/Delete directories. 00019 // .SECTION Thanks 00020 // This work is part of the National Alliance for Medical Image 00021 // Computing (NAMIC), funded by the National Institutes of Health 00022 // through the NIH Roadmap for Medical Research, Grant U54 EB005149. 00023 // Information on the National Centers for Biomedical Computing 00024 // can be obtained from http://nihroadmap.nih.gov/bioinformatics. 00025 // .SECTION See Also 00026 // vtkKWFileBrowserDialog vtkKWFileBrowserWidget 00027 00028 #ifndef __vtkKWDirectoryExplorer_h 00029 #define __vtkKWDirectoryExplorer_h 00030 00031 #include "vtkKWCompositeWidget.h" 00032 00033 class vtkKWIcon; 00034 class vtkKWToolbar; 00035 class vtkKWPushButton; 00036 class vtkKWTreeWithScrollbars; 00037 class vtkKWPushButtonWithMenu; 00038 class vtkKWMenu; 00039 class vtkKWDirectoryExplorerInternals; 00040 00041 class KWWidgets_EXPORT vtkKWDirectoryExplorer : public vtkKWCompositeWidget 00042 { 00043 public: 00044 static vtkKWDirectoryExplorer* New(); 00045 vtkTypeRevisionMacro(vtkKWDirectoryExplorer,vtkKWCompositeWidget); 00046 void PrintSelf(ostream& os, vtkIndent indent); 00047 00048 // Description: 00049 // Open a directory given the full path. 00050 // Return 1 on success, 0 otherwise 00051 virtual int OpenDirectory(const char* path); 00052 00053 // Description: 00054 // Rename a directory name, given the current and new name 00055 // of the directory. 00056 // Return 1 on success, 0 otherwise 00057 virtual int RenameDirectory(const char* currentname, const char* newname); 00058 00059 // Description: 00060 // Remove/delete a directory. 00061 // Return 1 on success, 0 otherwise 00062 virtual int DeleteDirectory(const char* dirname); 00063 00064 // Description: 00065 // Set/Get the selection mode 00066 // Valid constants can be found in vtkKWOptions::SelectionModeType. 00067 virtual void SetSelectionMode(int); 00068 virtual void SetSelectionModeToSingle(); 00069 virtual void SetSelectionModeToMultiple(); 00070 00071 // Description: 00072 // Return if one (or more) directory is/are selected. 00073 virtual int HasSelection(); 00074 00075 // Description: 00076 // Get/Set the selected director(ies). 00077 virtual const char *GetSelectedDirectory() 00078 {return GetNthSelectedDirectory(0);} 00079 virtual int GetNumberOfSelectedDirectories(); 00080 virtual const char *GetNthSelectedDirectory(int i); 00081 virtual void SelectDirectory(const char*); 00082 virtual void DeselectDirectory(const char*); 00083 virtual void ClearSelection(); 00084 00085 // Description: 00086 // Set/Get the maximum number of directories that will be stored 00087 // in back/forward history list. 00088 virtual void SetMaximumNumberOfDirectoriesInHistory(int); 00089 vtkGetMacro(MaximumNumberOfDirectoriesInHistory, int); 00090 00091 // Description: 00092 // Set/Get the selection background and foreground colors. 00093 virtual void GetSelectionBackgroundColor(double *r, double *g, double *b); 00094 virtual double* GetSelectionBackgroundColor(); 00095 virtual void SetSelectionBackgroundColor(double r, double g, double b); 00096 virtual void SetSelectionBackgroundColor(double rgb[3]) 00097 { this->SetSelectionBackgroundColor(rgb[0], rgb[1], rgb[2]); }; 00098 virtual void GetSelectionForegroundColor(double *r, double *g, double *b); 00099 virtual double* GetSelectionForegroundColor(); 00100 virtual void SetSelectionForegroundColor(double r, double g, double b); 00101 virtual void SetSelectionForegroundColor(double rgb[3]) 00102 { this->SetSelectionForegroundColor(rgb[0], rgb[1], rgb[2]); }; 00103 00104 // Description: 00105 // Convenience method to Set/Get the width (in chars) and height (in lines) 00106 // of the tree widget. 00107 virtual void SetTreeWidth(int); 00108 virtual int GetTreeWidth(); 00109 virtual void SetTreeHeight(int); 00110 virtual int GetTreeHeight(); 00111 00112 // Description: 00113 // Convenience method to Set/Get the tree background color. 00114 virtual void GetTreeBackgroundColor(double *r, double *g, double *b); 00115 virtual double* GetTreeBackgroundColor(); 00116 virtual void SetTreeBackgroundColor(double r, double g, double b); 00117 virtual void SetTreeBackgroundColor(double rgb[3]) 00118 { this->SetTreeBackgroundColor(rgb[0], rgb[1], rgb[2]); }; 00119 00120 // Description: 00121 // Specifies selection-related commands to associate with the widget. 00122 // DirectorySelected is called whenever the selection is changed or cleared. 00123 // The 'object' argument is the object that will have the method called on 00124 // it. The 'method' argument is the name of the method to be called and any 00125 // arguments in string form. If the object is NULL, the method is still 00126 // evaluated as a simple command. 00127 // - DirectorySelected is passed the first selected directory (const char *). 00128 // - DirectoryOpened and DirectoryClosed are passed the directory being 00129 // opened or closed (const char *). 00130 // - DirectoryAdded and DirectoryDeleted are passed the directory that was 00131 // added or removed (const char *). 00132 // - DirectoryRenamed is passed the preview and new name (path) of the 00133 // directory that was renamed (const char *, const char *). 00134 virtual void SetDirectorySelectedCommand(vtkObject *obj, const char *method); 00135 virtual void SetDirectoryOpenedCommand(vtkObject *obj, const char *method); 00136 virtual void SetDirectoryClosedCommand(vtkObject *obj, const char *method); 00137 virtual void SetDirectoryCreatedCommand(vtkObject *obj, const char *method); 00138 virtual void SetDirectoryDeletedCommand(vtkObject *obj, const char *method); 00139 virtual void SetDirectoryRenamedCommand(vtkObject *obj, const char *method); 00140 00141 // Description: 00142 // Events are passed the same parameters as the commands, if any. 00143 //BTX 00144 enum 00145 { 00146 DirectorySelectedEvent = 10000, 00147 DirectoryOpenedEvent, 00148 DirectoryClosedEvent, 00149 DirectoryCreatedEvent, 00150 DirectoryDeletedEvent 00151 }; 00152 //ETX 00153 00154 // Description: 00155 // Set focus to the directory widget component of this widget. 00156 virtual void Focus(); 00157 virtual int HasFocus(); 00158 00159 // Description: 00160 // Get the toolbar object. 00161 vtkGetObjectMacro(Toolbar, vtkKWToolbar); 00162 00163 // Description: 00164 // Get the button objects. 00165 // DO NOT modify the callbacks, these accessors are provided to change 00166 // the button icons, for example. 00167 vtkGetObjectMacro(CreateFolderButton, vtkKWPushButton); 00168 vtkGetObjectMacro(BackButton, vtkKWPushButtonWithMenu); 00169 vtkGetObjectMacro(ForwardButton, vtkKWPushButtonWithMenu); 00170 vtkGetObjectMacro(UpButton, vtkKWPushButton); 00171 00172 // Description: 00173 // Add event binding to the internal component widget 00174 // for the directory hierarchy, so that these events will be 00175 // invoked directly from the directory component of this widget. 00176 virtual void AddBindingToInternalWidget( 00177 const char* event, vtkObject *obj, const char* method); 00178 00179 // Description: 00180 // Callback, do NOT use. 00181 // When the dropdown menu button of the Back/Forward buttons 00182 // is clicked, navigate to the corresponding directory is the history list. 00183 // 'node' is a pointer to the unique directory node string, 00184 // 'offsetFromCurrentIndex' is the offset from current directory index in 00185 // history list. 00186 virtual void OpenDirectoryNodeCallback( 00187 const char* node, int offsetFromCurrentIndex); 00188 00189 // Description: 00190 // Callback, do NOT use. 00191 // When the node selection is changed in the directory tree, the 00192 // corresponding directory is refreshed, loading any new files or 00193 // directories underneath it. 00194 virtual void SingleClickOnNodeCallback(const char* node); 00195 00196 // Description: 00197 // Callback, do NOT use. 00198 // Right-click to popup context menu. 00199 virtual void RightClickCallback( int x, int y, const char* node); 00200 00201 // Description: 00202 // Callback, do NOT use. 00203 // Rename directory node from right-click context menu. 00204 virtual int RenameCallback(); 00205 00206 // Description: 00207 // Callback, do NOT use. 00208 // Launch native explorer from right-click context menu. 00209 virtual void RightClickExploreCallback(const char*); 00210 00211 // Description: 00212 // Callback, do NOT use. 00213 // When the Navigation keys (Up/Down, Home/End, 00214 // Prior(PageUp)/Next(PageDown)) are pressed. This is used to setup 00215 // logic to update the new selected node after the KeyPress events. 00216 virtual void TreeKeyNavigationCallback(); 00217 00218 // Description: 00219 // Callback, do NOT use. 00220 // When a node is opened in the directory tree, the corresponding 00221 // directory is refreshed, loading new files and directories. 00222 virtual void DirectoryOpenedCallback(const char* node); 00223 00224 // Description: 00225 // Callback, do NOT use. 00226 // When a node selection is changed in the directory tree. 00227 virtual void DirectorySelectedCallback(); 00228 00229 // Description: 00230 // Callback, do NOT use. 00231 // When a node is closed in the directory tree. 00232 // Also need refreshing the contents of the corresponding directory. 00233 virtual void DirectoryClosedCallback(const char* node); 00234 00235 // Description: 00236 // Callback, do NOT use. 00237 // When the "Create New Folder" button is clicked, a dialog is displayed 00238 // prompting the user for the name of the new directory. If the user inputs 00239 // a valid dir name, create the new directory, add it to the dir tree, and 00240 // select the node. 00241 virtual void CreateNewFolderCallback(); 00242 00243 // Description: 00244 // Callback, do NOT use. 00245 // When the "Back to previous directory" button is clicked, navigate 00246 // to the previously visited directory. A history of visited directories 00247 // is saved in memory. 00248 virtual void BackToPreviousDirectoryCallback(); 00249 00250 // Description: 00251 // Callback, do NOT use. 00252 // When the "Go to next directory" button is clicked, navigate 00253 // to next directory in the history list. 00254 virtual void ForwardToNextDirectoryCallback(); 00255 00256 // Description: 00257 // Callback, do NOT use. 00258 // When the "Go up one directory" button is clicked, navigate 00259 // to the parent directory of the currently selected directory. 00260 virtual void GoUpDirectoryCallback(); 00261 00262 // Description: 00263 // Callback, do NOT use. 00264 // When the 'Delete' key is pressed, remove the selected item from the dir 00265 // tree/file list. 00266 virtual int RemoveSelectedNodeCallback(); 00267 00268 // Description: 00269 // Callback, do NOT use. 00270 virtual void LoadRootDirectoryCallback(); 00271 00272 // Description: 00273 // Update the "enable" state of the object and its internal parts. 00274 // Depending on different Ivars (this->Enabled, the application's 00275 // Limited Edition Mode, etc.), the "enable" state of the object 00276 // is updated and propagated to its internal parts/subwidgets. 00277 // This will, for example, enable/disable parts of the widget UI, 00278 // enable/disable the visibility of 3D widgets, etc. 00279 virtual void UpdateEnableState(); 00280 virtual void Update(); 00281 00282 // Description: 00283 // Given the prefix of a sub-folder name of the current selected directory, 00284 // this function will make the corresponding child node for this 00285 // sub-folder visible in the window. 00286 virtual void ScrollToDirectory(const char* prefix); 00287 00288 protected: 00289 vtkKWDirectoryExplorer(); 00290 ~vtkKWDirectoryExplorer(); 00291 00292 // Description: 00293 // Create the widget. 00294 virtual void CreateWidget(); 00295 00296 // Description: 00297 // Load root directory. For Win32, this will load the drives; 00298 // for *nix, this will load root "/" directory 00299 virtual void LoadRootDirectory(); 00300 00301 // Description: 00302 // Close all the nodes up to root level: drives for Win32; '/' for *nix 00303 virtual void BackToRoot(); 00304 00305 // Description: 00306 // When the node selection is changed in the directory tree, the 00307 // corresponding directory is refreshed, loading any new files or 00308 // directories underneath it. 00309 virtual void SelectDirectoryNode(const char* node, int opennode = 1); 00310 00311 // Description: 00312 // If the directory is already loaded in the tree, navigate to 00313 // and reload this input directory; if not, this function will 00314 // call OpenDirectory(path) to open this directory. 00315 // 'node' is a pointer to the node string (can be NULL), 'path' a pointer 00316 // to the absolute path to the directory. 00317 // Return the node associated with this directory. 00318 virtual const char* ReloadDirectory(const char* node, const char* path); 00319 00320 // Description: 00321 // Reload a directory node given the parent node, and the fullpath. 00322 // This is used to open the sub directory node of a parent node. 00323 // 'select' is a flag to indicate whether to select this sub node after 00324 // opening it. 00325 // Return the node opened. 00326 virtual const char* ReloadDirectory(const char* parentnode, 00327 const char* dirname, 00328 int select); 00329 00330 // Description: 00331 // Open a subdirectory of a parent tree node. 00332 // 'parentnode' is the parent node of this directory, 'fullname' is the full 00333 // path name of this sub-directory, 'select' a flag to indicate whether to 00334 // select this sub node after opening it. 00335 virtual void OpenSubDirectory(const char* parentnode, 00336 const char* fullname, 00337 int select = 1); 00338 00339 // Description: 00340 // This is the main funtion used to find all the directories and 00341 // adding all the corresponding node to the dir tree. 00342 virtual void OpenDirectoryNode(const char* node, 00343 int select = 1, 00344 int opennode = 1); 00345 00346 // Description: 00347 // Open the node, including all its parent nodes. 00348 virtual void OpenWholeTree(const char* node); 00349 00350 // Description: 00351 // Calling OpenDirectoryNode after centain logic to determine 00352 // whether to open the whole tree. 00353 virtual void ReloadDirectoryNode(const char* node); 00354 00355 // Description: 00356 // This is the actual funtion adding all the nodes to the tree, 00357 // called from OpenDirectoryNode. 00358 virtual void UpdateDirectoryNode(const char* node); 00359 00360 // Description: 00361 // Add a node to the dir tree. 00362 virtual void AddDirectoryNode( 00363 const char* parentnode, 00364 const char* node, 00365 const char* text, 00366 const char* fullname, 00367 vtkKWIcon *nodeicon); 00368 00369 // Description: 00370 // Get one node from a multiple selection of nodes. An 00371 // error will be generated if the index is out of range. 00372 const char *GetNthSelectedNode(int i); 00373 00374 // Description: 00375 // Update the directory history list given the most recent node 00376 virtual void UpdateMostRecentDirectoryHistory(const char* node); 00377 00378 // Description: 00379 // Update directory history list according to the 00380 // MaximumNumberOfDirectoriesInHistory 00381 virtual void PruneMostRecentDirectoriesInHistory(); 00382 00383 // Description: 00384 // Remove a directory node from the most recent history list 00385 virtual void RemoveDirectoryFromHistory(const char* node); 00386 00387 // Description: 00388 // Populate the context menu. 00389 virtual void PopulateContextMenu(vtkKWMenu *menu, const char*); 00390 00391 // Description: 00392 // Check if the node is one of the selected nodes. 00393 int IsNodeSelected(const char* node); 00394 00395 // Description: 00396 // Open the directory. 00397 // -dirname, the directory that is going to be opened 00398 // -select, is the flag to whether select this directory 00399 // return a node for this directory 00400 const char* OpenDirectoryInternal(const char* dirname, int select); 00401 00402 // Description: 00403 // Commands 00404 char *DirectorySelectedCommand; 00405 char *DirectoryOpenedCommand; 00406 char *DirectoryClosedCommand; 00407 char *DirectoryCreatedCommand; 00408 char *DirectoryDeletedCommand; 00409 char *DirectoryRenamedCommand; 00410 00411 // Description: 00412 // Invoke Commands 00413 virtual void InvokeDirectorySelectedCommand(const char* path); 00414 virtual void InvokeDirectoryOpenedCommand(const char* path); 00415 virtual void InvokeDirectoryClosedCommand(const char* path); 00416 virtual void InvokeDirectoryCreatedCommand(const char* path); 00417 virtual void InvokeDirectoryDeletedCommand(const char* path); 00418 virtual void InvokeDirectoryRenamedCommand( 00419 const char* oldname, const char* newname); 00420 00421 // Description: 00422 // Internal PIMPL class for STL purposes. 00423 vtkKWDirectoryExplorerInternals *Internals; 00424 00425 // Description: 00426 // GUI 00427 vtkKWToolbar *Toolbar; 00428 vtkKWTreeWithScrollbars *DirectoryTree; 00429 vtkKWPushButton *CreateFolderButton; 00430 vtkKWPushButtonWithMenu *BackButton; 00431 vtkKWPushButtonWithMenu *ForwardButton; 00432 vtkKWPushButton *UpButton; 00433 vtkKWMenu *ContextMenu; 00434 00435 // Description: 00436 // Member variables 00437 int MaximumNumberOfDirectoriesInHistory; 00438 00439 private: 00440 00441 static vtkIdType IdCounter; 00442 00443 vtkKWDirectoryExplorer(const vtkKWDirectoryExplorer&); // Not implemented 00444 void operator=(const vtkKWDirectoryExplorer&); // Not implemented 00445 }; 00446 #endif