FileSelectionBox.st
changeset 296 e1b9431b3aef
parent 240 75a3b67bd91d
child 351 24a527f86c7b
--- a/FileSelectionBox.st	Thu Jan 18 01:11:18 1996 +0100
+++ b/FileSelectionBox.st	Thu Jan 18 22:28:46 1996 +0100
@@ -37,21 +37,36 @@
 "
     this class implements file selection boxes. Instances show a list of
     files, and perform an action block with the selected pathname as
-    argument when ok is clicked.
+    argument when ok is clicked. It is also possible, to open the box
+    without action and ask it afterward if it has been left with ok
+    (i.e. the ST-80 way, asking 'aBox accepted ifTrue:[...]')
 
     There is an optional PatternField, which shows itself when a pattern
     is defined (i.e. if there is no pattern, it is hidden). 
-    If there is a pattern, only files matching the pattern will be shown in 
-    the list (and directories).
+    If there is a pattern, only files matching the pattern are shown in 
+    the list. Directories are not affected by the patternField.
 
     In addition, there is an optional matchBlock (actually this is defined 
-    in the FileSelectionList). Only filenames for which this matchblock
-    returns true will be presented.
+    in the FileSelectionList). Only names for which this matchblock
+    returns true will be presented. The matchBlock affects both regular files
+    and names of directories. The argument passed to the matchBlock is the full
+    pathname.
+
+    All of the actual work is done in the fileList; see the documentation
+    of FileSelectionList for more options 
+    (you can access a boxes fileList via 'aBox>>listView' and get access to all
+     of those fancy settings)
+    For example, by accessing the list, it is possible to hide all directories 
+    ('aBox listView ignoreDirectories:true'), to hide the parentDirectory alone 
+    ('aBox listView ignoreParentDirectory') and to turn off the marking 
+    of subdirectories ('aBox listView markSubdirectories:false').
 "
 !
 
 examples 
 "
+  simple standard queries
+
     very simple:
 	|name|
 
@@ -73,16 +88,18 @@
 	Transcript showCr:name
 
 
-    more detailed setup:
 
-	FileSelectionBox new openModal
+  more detailed setup
 
     setting title:
 
 	|box|
 	box := FileSelectionBox new.
 	box title:'Which file ?'.
-	box open
+	box open.
+	box accepted ifTrue:[
+	    Transcript showCr:'you selected: ' , box
+	]
 
     setting a matchpattern:
 
@@ -110,6 +127,57 @@
 	box matchBlock:[:name | OperatingSystem isDirectory:name].
 	box open
 
+    dont show the parent directory:
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which directory ?'.
+	box listView ignoreParentDirectory:true.
+	box open
+
+    dont show any directory:
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which file ?'.
+	box listView ignoreDirectories:true.
+	box open
+
+    dont show any directory or hidden file:
+    (notice the basename extraction - we are not interrested in the full pathName)
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which file ?'.
+	box listView ignoreDirectories:true.
+	box matchBlock:[:name | (name asFilename baseName startsWith:'.') not].
+	box open
+
+    dont allow direct filename entry:
+    (i.e. avoid the user choosing files from other directories)
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which one ?'.
+	box enterField beInvisible.
+	box open.
+	box accepted ifTrue:[
+	    Transcript showCr:'path is ' , box pathName
+	].
+
+    combined with above directory ignoring,
+    this limits selection of files from a single directory:
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which file ?'.
+	box enterField beInvisible.
+	box listView ignoreDirectories:true.
+	box open.
+	box accepted ifTrue:[
+	    Transcript showCr:'path is ' , box pathName
+	].
+
     finally, an action:
 
 	|box|
@@ -119,6 +187,44 @@
 	box matchBlock:[:name | OperatingSystem isDirectory:name].
 	box action:[:name | Transcript showCr:name].
 	box open
+
+  concrete examples:
+
+    only show files beginning with lowercase characters
+    and ending in '.c':
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which directory ?'.
+	box matchBlock:[:name |
+			    box pathName asFilename isDirectory
+			    or:[name first isLowercase
+				and:[name endsWith:'.c']]
+		       ].
+	box open.
+	box accepted ifTrue:[
+	    Transcript showCr:'full path:  ' , box pathName.
+	    Transcript showCr:'files name: ' , box fileName.
+	    Transcript showCr:'directory : ' , box directory.
+	]
+
+    somewhat wierd example:
+    only show files, if the directory we are in begins with 'lib':
+
+	|box|
+	box := FileSelectionBox new.
+	box title:'Which directory ?'.
+	box pattern:'l*'.
+	box matchBlock:[:name | 
+			    box directory asString beginsWith:'lib'
+		       ].
+	box open
+	box accepted ifTrue:[
+	    Transcript showCr:'full path:  ' , box pathName.
+	    Transcript showCr:'files name: ' , box fileName.
+	    Transcript showCr:'directory : ' , box directory.
+	]
+
 "
 ! !
 
@@ -198,6 +304,24 @@
 
 !FileSelectionBox methodsFor:'accessing'!
 
+pathName
+    "same as contents - return the full pathname of the selected file,
+     or the pathname of the directory if nothing has been entered"
+
+    ^ self contents
+!
+
+fileName
+    "if some filename has been entered, return it (without the directory path)
+     otherwise, return nil"
+
+    |string|
+
+    string := super contents.
+    string isNil ifTrue:[^ nil].
+    ^ self pathName
+!
+
 contents
     "return the current entered value (i.e. the enterFields string).
      redefined to return the full pathname."
@@ -215,6 +339,12 @@
     ^ (selectionList directory pathName asFilename construct:string) asString
 !
 
+directory
+    "return the directory which is currently shown"
+
+    ^ selectionList directory
+!
+
 directory:directoryName
     "change the directory shown in the list."
 
@@ -453,5 +583,5 @@
 !FileSelectionBox class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg/FileSelectionBox.st,v 1.26 1995-12-07 22:09:55 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg/FileSelectionBox.st,v 1.27 1996-01-18 21:28:46 cg Exp $'
 ! !