FileSelectionList.st
changeset 193 6ccc226ce3a6
parent 174 d80a6cc3f9b2
child 282 8e0e072c1981
equal deleted inserted replaced
192:fc2fc4347d5d 193:6ccc226ce3a6
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 'From Smalltalk/X, Version:2.10.4 on 1-feb-1995 at 3:54:12 pm'!
       
    14 
       
    15 SelectionInListView subclass:#FileSelectionList
    13 SelectionInListView subclass:#FileSelectionList
    16 	 instanceVariableNames:'pattern directory timeStamp directoryId directoryContents
    14 	 instanceVariableNames:'pattern directory timeStamp directoryId directoryContents
    17 		directoryFileTypes fileTypes realAction matchBlock
    15                 directoryFileTypes fileTypes realAction matchBlock
    18 		stayInDirectory ignoreParentDirectory'
    16                 stayInDirectory ignoreParentDirectory'
    19 	 classVariableNames:''
    17 	 classVariableNames:''
    20 	 poolDictionaries:''
    18 	 poolDictionaries:''
    21 	 category:'Views-Text'
    19 	 category:'Views-Text'
    22 !
    20 !
    23 
    21 
    33  inclusion of the above copyright notice.   This software may not
    31  inclusion of the above copyright notice.   This software may not
    34  be provided or otherwise made available to, or used by, any
    32  be provided or otherwise made available to, or used by, any
    35  other person.  No title to or ownership of the software is
    33  other person.  No title to or ownership of the software is
    36  hereby transferred.
    34  hereby transferred.
    37 "
    35 "
    38 !
       
    39 
       
    40 version
       
    41     ^ '$Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.15 1995-11-11 16:20:20 cg Exp $'
       
    42 !
    36 !
    43 
    37 
    44 documentation
    38 documentation
    45 "
    39 "
    46     this class implements file selection lists - its basically a
    40     this class implements file selection lists - its basically a
   212 			    name asFilename isDirectory not
   206 			    name asFilename isDirectory not
   213 			].
   207 			].
   214 	list action:[:index | Transcript showCr:'you selected: ' , list selectionValue].
   208 	list action:[:index | Transcript showCr:'you selected: ' , list selectionValue].
   215 	top open
   209 	top open
   216 "
   210 "
       
   211 !
       
   212 
       
   213 version
       
   214     ^ '$Header: /cvs/stx/stx/libwidg/FileSelectionList.st,v 1.16 1995-11-23 00:50:10 cg Exp $'
   217 ! !
   215 ! !
   218 
   216 
   219 !FileSelectionList methodsFor:'drawing'!
   217 !FileSelectionList methodsFor:'accessing'!
   220 
   218 
   221 redrawVisibleLine:visLineNr
   219 action:aBlock
   222     "if the line is one for a directory, draw a right arrow"
   220     "set the action to be performed on a selection"
   223 
   221 
   224     |l|
   222     realAction := aBlock
   225 
   223 !
   226     super redrawVisibleLine:visLineNr.
   224 
   227     l := self visibleLineToListLine:visLineNr.
   225 directory
   228     l notNil ifTrue:[
   226     "return the shown directory"
   229 	(fileTypes at:l) == #directory ifTrue:[
   227 
   230 	    self drawRightArrowInVisibleLine:visLineNr
   228     ^ directory
       
   229 !
       
   230 
       
   231 directory:nameOrDirectory
       
   232     "set the lists contents to the filenames in the directory"
       
   233 
       
   234     |oldPath name|
       
   235 
       
   236     nameOrDirectory isString ifTrue:[
       
   237 	name := nameOrDirectory
       
   238     ] ifFalse:[
       
   239 	nameOrDirectory isNil ifTrue:[
       
   240 	    directory := nil.
       
   241 	    ^ self updateList
       
   242 	].
       
   243 	name := nameOrDirectory pathName
       
   244     ].
       
   245     directory isNil ifTrue:[
       
   246 	directory := FileDirectory new.
       
   247 	oldPath := nil
       
   248     ] ifFalse:[
       
   249 	oldPath := directory pathName.
       
   250     ].
       
   251     directory pathName:name.
       
   252     realized ifTrue:[
       
   253 	(directory pathName = oldPath) ifFalse:[
       
   254 	    self updateList
   231 	]
   255 	]
   232     ]
   256     ]
   233 !
   257 !
       
   258 
       
   259 ignoreParentDirectory:aBoolean
       
   260     "set/clear the flag which controls if the parent directory (..)
       
   261      is shown in the list. The default is false (i.e. show it)"
       
   262 
       
   263     ignoreParentDirectory := aBoolean
       
   264 !
       
   265 
       
   266 matchBlock:aBlock
       
   267     "set the matchBlock - if non-nil, it controls which
       
   268      names are shown in the list."
       
   269 
       
   270     matchBlock := aBlock
       
   271 !
       
   272 
       
   273 pattern:aPattern
       
   274     "set the pattern - if it changes, update the list."
       
   275 
       
   276     pattern ~= aPattern ifTrue:[
       
   277 	pattern := aPattern.
       
   278 	realized ifTrue:[
       
   279 	    self updateList
       
   280 	].
       
   281     ].
       
   282 !
       
   283 
       
   284 selectedPathname
       
   285     "if there is a selection, return its full pathname.
       
   286      Of there is no selection, return nil."
       
   287 
       
   288     |sel|
       
   289 
       
   290     sel := self selectionValue.
       
   291     sel isNil ifTrue:[^ nil].
       
   292     ^ directory pathName , Filename separator asString , sel.
       
   293 
       
   294 !
       
   295 
       
   296 stayInDirectory:aBoolean
       
   297     "set/clear the flag which controls if selecting a directory
       
   298      should locally change (if false) or be handled just like
       
   299      the selection of a file (if true).
       
   300      The default is false (i.e. change and do not tell via action)"
       
   301 
       
   302     stayInDirectory := aBoolean
       
   303 ! !
       
   304 
       
   305 !FileSelectionList methodsFor:'drawing'!
   234 
   306 
   235 redrawFromVisibleLine:startVisLineNr to:endVisLineNr
   307 redrawFromVisibleLine:startVisLineNr to:endVisLineNr
   236     "redefined to look for directory in every line"
   308     "redefined to look for directory in every line"
   237 
   309 
   238     |l|
   310     |l|
   247 	    (fileTypes at:l) == #directory ifTrue:[
   319 	    (fileTypes at:l) == #directory ifTrue:[
   248 		self drawRightArrowInVisibleLine:visLineNr
   320 		self drawRightArrowInVisibleLine:visLineNr
   249 	    ]
   321 	    ]
   250 	]
   322 	]
   251     ]
   323     ]
   252 ! !
   324 !
   253 
   325 
   254 !FileSelectionList methodsFor:'accessing'!
   326 redrawVisibleLine:visLineNr
   255 
   327     "if the line is one for a directory, draw a right arrow"
   256 directory:nameOrDirectory
   328 
   257     "set the lists contents to the filenames in the directory"
   329     |l|
   258 
   330 
   259     |oldPath name|
   331     super redrawVisibleLine:visLineNr.
   260 
   332     l := self visibleLineToListLine:visLineNr.
   261     nameOrDirectory isString ifTrue:[
   333     l notNil ifTrue:[
   262 	name := nameOrDirectory
   334 	(fileTypes at:l) == #directory ifTrue:[
   263     ] ifFalse:[
   335 	    self drawRightArrowInVisibleLine:visLineNr
   264 	nameOrDirectory isNil ifTrue:[
       
   265 	    directory := nil.
       
   266 	    ^ self updateList
       
   267 	].
       
   268 	name := nameOrDirectory pathName
       
   269     ].
       
   270     directory isNil ifTrue:[
       
   271 	directory := FileDirectory new.
       
   272 	oldPath := nil
       
   273     ] ifFalse:[
       
   274 	oldPath := directory pathName.
       
   275     ].
       
   276     directory pathName:name.
       
   277     realized ifTrue:[
       
   278 	(directory pathName = oldPath) ifFalse:[
       
   279 	    self updateList
       
   280 	]
   336 	]
   281     ]
   337     ]
   282 !
   338 ! !
   283 
   339 
   284 directory
   340 !FileSelectionList methodsFor:'initialization'!
   285     "return the shown directory"
   341 
   286 
   342 initialize
   287     ^ directory
   343     directory := FileDirectory currentDirectory.
   288 !
   344     stayInDirectory := false.
   289 
   345     ignoreParentDirectory := false.
   290 action:aBlock
   346 
   291     "set the action to be performed on a selection"
   347     super initialize.
   292 
   348 
   293     realAction := aBlock
   349     pattern := '*'.
   294 !
   350     self initializeAction.
   295 
   351 
   296 stayInDirectory:aBoolean
   352     "nontypical use ..."
   297     "set/clear the flag which controls if selecting a directory
   353     "
   298      should locally change (if false) or be handled just like
   354      FileSelectionList new open
   299      the selection of a file (if true).
   355      (FileSelectionList new directory:'/etc') open
   300      The default is false (i.e. change and do not tell via action)"
   356      (ScrollableView for:FileSelectionList) open
   301 
   357      (HVScrollableView for:FileSelectionList) open
   302     stayInDirectory := aBoolean
   358     "
   303 !
   359 !
   304 
   360 
   305 ignoreParentDirectory:aBoolean
   361 initializeAction
   306     "set/clear the flag which controls if the parent directory (..)
   362     "setup action as: selections in list get forwarded to enterfield if not 
   307      is shown in the list. The default is false (i.e. show it)"
   363      a directory; otherwise directory is changed"
   308 
   364 
   309     ignoreParentDirectory := aBoolean
   365     actionBlock := [:lineNr |
   310 !
   366 	|entry ok|
   311 
   367 
   312 pattern:aPattern
   368 	self selection isCollection ifFalse:[
   313     "set the pattern - if it changes, update the list."
   369 	    entry := self selectionValue.
   314 
   370 	    (entry endsWith:' ...') ifTrue:[
   315     pattern ~= aPattern ifTrue:[
   371 		entry := entry copyWithoutLast:4 "copyTo:(entry size - 4)".
   316 	pattern := aPattern.
   372 	    ].
   317 	realized ifTrue:[
   373 	    (stayInDirectory not
   318 	    self updateList
   374 	    and:[(directory typeOf:entry) == #directory]) ifTrue:[
   319 	].
   375 		ok := false.
   320     ].
   376 		(directory isReadable:entry) ifFalse:[
   321 !
   377 		    self warn:(resources string:'not allowed to read directory %1' with:entry)
   322 
   378 		] ifTrue:[
   323 selectedPathname
   379 		    (directory isExecutable:entry) ifFalse:[
   324     "if there is a selection, return its full pathname.
   380 			self warn:(resources string:'not allowed to change to directory %1' with:entry)
   325      Of there is no selection, return nil."
   381 		    ] ifTrue:[
   326 
   382 			self directory:(directory pathName , Filename separator asString , entry).
   327     |sel|
   383 			ok := true.
   328 
   384 		    ]
   329     sel := self selectionValue.
   385 		].
   330     sel isNil ifTrue:[^ nil].
   386 		ok ifFalse:[
   331     ^ directory pathName , Filename separator asString , sel.
   387 		    self deselect
   332 
   388 		]
   333 !
   389 
   334 
   390 	    ] ifFalse:[
   335 matchBlock:aBlock
   391 		realAction notNil ifTrue:[
   336     "set the matchBlock - if non-nil, it controls which
   392 		    realAction value:lineNr
   337      names are shown in the list."
   393 		]
   338 
   394 	    ]
   339     matchBlock := aBlock
   395 	]
       
   396     ]
       
   397 !
       
   398 
       
   399 reinitialize
       
   400     directory := FileDirectory currentDirectory.
       
   401     super reinitialize
   340 ! !
   402 ! !
   341 
   403 
   342 !FileSelectionList methodsFor:'private'!
   404 !FileSelectionList methodsFor:'private'!
   343 
   405 
   344 updateList
   406 updateList
   408     super list:newList.
   470     super list:newList.
   409 
   471 
   410     self cursor:oldCursor.
   472     self cursor:oldCursor.
   411 !
   473 !
   412 
   474 
   413 widthForScrollBetween:firstLine and:lastLine
       
   414     "return the width in pixels for a scroll between firstLine and lastLine
       
   415      - return full width here since there might be directory marks"
       
   416 
       
   417     ^ (width - margin - margin)
       
   418 !
       
   419 
       
   420 visibleLineNeedsSpecialCare:visLineNr
   475 visibleLineNeedsSpecialCare:visLineNr
   421     |l|
   476     |l|
   422 
   477 
   423     l := self visibleLineToListLine:visLineNr.
   478     l := self visibleLineToListLine:visLineNr.
   424     l notNil ifTrue:[
   479     l notNil ifTrue:[
   425 	(fileTypes at:l) == #directory ifTrue:[^ true].
   480 	(fileTypes at:l) == #directory ifTrue:[^ true].
   426 	^ super visibleLineNeedsSpecialCare:visLineNr
   481 	^ super visibleLineNeedsSpecialCare:visLineNr
   427     ].
   482     ].
   428     ^ false
   483     ^ false
   429 ! !
   484 !
   430 
   485 
   431 !FileSelectionList methodsFor:'initialization'!
   486 widthForScrollBetween:firstLine and:lastLine
   432 
   487     "return the width in pixels for a scroll between firstLine and lastLine
   433 initializeAction
   488      - return full width here since there might be directory marks"
   434     "setup action as: selections in list get forwarded to enterfield if not 
   489 
   435      a directory; otherwise directory is changed"
   490     ^ (width - margin - margin)
   436 
       
   437     actionBlock := [:lineNr |
       
   438 	|entry ok|
       
   439 
       
   440 	self selection isCollection ifFalse:[
       
   441 	    entry := self selectionValue.
       
   442 	    (entry endsWith:' ...') ifTrue:[
       
   443 		entry := entry copyWithoutLast:4 "copyTo:(entry size - 4)".
       
   444 	    ].
       
   445 	    (stayInDirectory not
       
   446 	    and:[(directory typeOf:entry) == #directory]) ifTrue:[
       
   447 		ok := false.
       
   448 		(directory isReadable:entry) ifFalse:[
       
   449 		    self warn:(resources string:'not allowed to read directory %1' with:entry)
       
   450 		] ifTrue:[
       
   451 		    (directory isExecutable:entry) ifFalse:[
       
   452 			self warn:(resources string:'not allowed to change to directory %1' with:entry)
       
   453 		    ] ifTrue:[
       
   454 			self directory:(directory pathName , Filename separator asString , entry).
       
   455 			ok := true.
       
   456 		    ]
       
   457 		].
       
   458 		ok ifFalse:[
       
   459 		    self deselect
       
   460 		]
       
   461 
       
   462 	    ] ifFalse:[
       
   463 		realAction notNil ifTrue:[
       
   464 		    realAction value:lineNr
       
   465 		]
       
   466 	    ]
       
   467 	]
       
   468     ]
       
   469 !
       
   470 
       
   471 initialize
       
   472     directory := FileDirectory currentDirectory.
       
   473     stayInDirectory := false.
       
   474     ignoreParentDirectory := false.
       
   475 
       
   476     super initialize.
       
   477 
       
   478     pattern := '*'.
       
   479     self initializeAction.
       
   480 
       
   481     "nontypical use ..."
       
   482     "
       
   483      FileSelectionList new open
       
   484      (FileSelectionList new directory:'/etc') open
       
   485      (ScrollableView for:FileSelectionList) open
       
   486      (HVScrollableView for:FileSelectionList) open
       
   487     "
       
   488 !
       
   489 
       
   490 reinitialize
       
   491     directory := FileDirectory currentDirectory.
       
   492     super reinitialize
       
   493 ! !
   491 ! !
   494 
   492 
   495 !FileSelectionList methodsFor:'realization'!
   493 !FileSelectionList methodsFor:'realization'!
   496 
   494 
   497 realize
   495 realize