TextBox.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Mar 2002 19:00:14 +0100
changeset 2088 c8a9d7712cc2
parent 1654 790df576b51b
child 2437 c9ba20e3367a
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1992 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

"{ Package: 'stx:libwidg2' }"

EnterBox subclass:#TextBox
	instanceVariableNames:'textView'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-DialogBoxes'
!

!TextBox class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1992 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    this class implements a pop-up box to enter some text with 2 buttons,
    one to cancel, another to start some action.
    It is basically an enterBox, but allows entering of more than one line
    of text.
"
!

examples
"
  Example (using ok-action callBack):
                                                                        [exBegin]
    |textBox|

    textBox := TextBox new.
    textBox title:'enter some text'.
    textBox action:[:text | Transcript showCR:('the entered text was:\' , text) withCRs].
    textBox showAtPointer.
                                                                        [exEnd]


  Example (asking afterwards):
                                                                        [exBegin]
    |textBox|

    textBox := TextBox new.
    textBox title:'enter some text'.
    textBox showAtPointer.
    textBox accepted ifTrue:[
        Transcript showCR:'accepted text is:'.
        Transcript showCR:textBox contents
    ].
                                                                        [exEnd]

  Example - readonly text (useful for status display):
                                                                        [exBegin]
    |textBox|

    textBox := TextBox new.
    textBox initialText:('Makefile' asFilename contents).
    textBox title:'Makefile:'.
    textBox readOnly:true.
    textBox noCancel.
    textBox label:'Makefile'.
    textBox extent:(600@250); sizeFixed:true.
    textBox showAtPointer.
                                                                        [exEnd]
"
! !

!TextBox class methodsFor:'common dialogs'!

openOn:someText
    "open a textBox on some text, 
     return (the possibly modified) text if accepted; nil otherwise."

    ^ self openOn:someText title:'Enter text'

    "
     TextBox openOn:'hello'
    "
!

openOn:someText title:titleString
    "open a textBox on some text, 
     return (the possibly modified) text if accepted; nil otherwise."

    |box returnValue|

    box := self new.
    box title:titleString.
    box initialText:someText.
    box action:[:text | returnValue := text].
    box showAtPointer.
    ^ returnValue.

    "
     TextBox openOn:'hello' title:'hi there'
    "
! !

!TextBox class methodsFor:'defaults'!

defaultExtent
    ^ (Display pixelPerMillimeter * (120 @ 90)) rounded
! !

!TextBox methodsFor:'accessing'!

contents
    "return my contents"

    ^ textView contents
!

initialText:aString
    "define the initial text in the enterfield"

    textView contents:aString
!

readOnly:aBoolean
    "make my text readOnly or readWrite"

    textView readOnly:aBoolean
!

textView
    ^ textView
! !

!TextBox methodsFor:'initialization'!

initialize
    |space2 space3 innerWidth|

    super initialize.

    space2 := 2 * ViewSpacing.
    space3 := 3 * ViewSpacing.

    "kludge: preset extent to something useful since other subviews
     depend on it (extent blocks are not evaluated until view is realized)
     - avoid visible resizing when realized the first time"

    innerWidth := width - space2.

    textView := HVScrollableView for:EditTextView miniScrollerH:true in:self.
    textView origin:(ViewSpacing @ (space2 + labelField height))
	     extent:(innerWidth @ (height - ViewSpacing -
				   labelField height - ViewSpacing -
				   buttonPanel height - space3) ).
    textView origin:[ViewSpacing @ (space2 + labelField height)]
	     extent:[(width - space2) @ (height - ViewSpacing -
				   labelField height - ViewSpacing -
				   buttonPanel height - space3) ].

    self delegate:(KeyboardForwarder toView:textView scrolledView)

    "TextBox new showAtPointer"
! !

!TextBox methodsFor:'queries'!

preferredExtent
    "return the extent needed to make everything visible"

    |wWanted hWanted wPanel|

    wWanted := labelField width + ViewSpacing + ViewSpacing.
    (wWanted > width) ifFalse:[
	wWanted := width
    ].
    wPanel := ViewSpacing * 3.
    buttonPanel subViews do:[:aView |
	wPanel := wPanel + aView width + ViewSpacing
    ].
    wPanel > wWanted ifTrue:[
	wWanted := wPanel
    ].
    hWanted := ViewSpacing + labelField height +
	       ViewSpacing + textView height +
	       (ViewSpacing * 6) + buttonPanel preferredExtent y +
	       ViewSpacing.
    ^ (wWanted @ hWanted)
! !

!TextBox class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/TextBox.st,v 1.16 2002-03-26 18:00:14 cg Exp $'
! !