DialogBox.st
author claus
Mon, 06 Mar 1995 20:29:54 +0100
changeset 97 cbf495fe3b64
parent 95 7535cfca9509
child 100 25778412f2b0
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1994 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.
"


'From Smalltalk/X, Version:2.10.4 on 28-dec-1994 at 1:38:02 pm'!

ModalBox subclass:#DialogBox
	 instanceVariableNames:'buttonPanel okButton okAction abortButton abortAction
		acceptReturnAsOK'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Views-DialogBoxes'
!

!DialogBox class methodsFor:'documentation'!

version
"
$Header: /cvs/stx/stx/libwidg/DialogBox.st,v 1.4 1995-03-06 19:28:11 claus Exp $
"
!

documentation
"
    this class implements the common behavior of dialogboxes.

    DialogBox is an (abstract) superclass of many other boxes - see InfoBox,
    WarningBox, YesNoBox etc. for concrete examples.
    Most of them simply add buttons or other elements.

    instance variables:

	buttonPanel      <PanelView>    contains the button(s)

	okButton         <Button>       the ok-Button

	okAction         <Block>        the action to be performed when ok is pressed,
					or return is pressed.

	acceptReturnAsOK <Boolean>      if true, pressing the return-key counts
					as if ok was pressed. Default is true.

	abortButton      <Button>       the cancel-Button

	abortAction      <Block>        the action to be performed when cancel is
					pressed.
"
!

examples
"
    since DialogBox is abstract and meant as a base for InfoBox, YesNoBox etc,
    the following examples are somewhat artificial ...


    DialogBox new open

    DialogBox new addOkButton; open

    DialogBox new addAbortButton; addOkButton; open
"
!

copyright
"
 COPYRIGHT (c) 1994 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.
"
! !

!DialogBox methodsFor:'user actions'!

okPressed
    "user pressed ok-button; make myself invisible and if an action was
     specified do it"

    okButton turnOffWithoutRedraw.
    self hideAndEvaluate:okAction
!

abortPressed
    "user pressed abort button - hide myself and evaluate okAction"

    abortButton turnOffWithoutRedraw.
    self hideAndEvaluate:abortAction
!

keyPress:aKey x:x y:y
    "return-key dublicates ok-function if acceptReturnAsOK is true"

    acceptReturnAsOK ifTrue:[
	(aKey == #Return) ifTrue:[^ self okPressed]
    ].
    super keyPress:aKey x:x y:y
! !

!DialogBox methodsFor:'queries'!

positionOffset
    "return the delta, by which the box should be displayed
     from the mouse pointer. Value returned here makes center of
     okButton appear under the cursor"

    okButton isNil ifTrue:[
	^ super positionOffset
    ].
    buttonPanel setChildPositionsIfChanged.
    ^ (okButton originRelativeTo:self) + (okButton extent // 2)
!

preferedExtent 
    "return my prefered extent"

    |w h p|

    okButton isNil ifTrue:[
	^ super preferedExtent
    ].
    p := buttonPanel preferedExtent.
    w := p x.
    h := ViewSpacing
	 + "okButton preferedExtent" p y
	 + ViewSpacing.

    ^ w @ h
! !

!DialogBox methodsFor:'initialization'!

initialize
    |mm|

    super initialize.

    label := 'Dialog'.

    mm := ViewSpacing.

    acceptReturnAsOK := true.

    buttonPanel := HorizontalPanelView in:self.
    buttonPanel origin:(0.0 @ 1.0) corner:(1.0 @ 1.0).
    buttonPanel bottomInset:mm; 
		topInset:(font height + mm * 2) negated.
    buttonPanel borderWidth:0.

    buttonPanel layout:#spread.

    "
     |b|
     b := DialogBox new.
     b addAbortButton; addOkButton; showAtPointer
    "
    "
     |b|
     b := DialogBox new.
     b addOkButton; showAtPointer
    "
!

addOkButton
    "create an okButton - to be sent from redefined initialize
     methods in subclasses."

    buttonPanel subViews size == 1 ifTrue:[
	buttonPanel layout:#fitSpace.
    ].
    okButton := Button okButtonIn:buttonPanel.
    okButton model:self; change:#okPressed.
    okButton isReturnButton:acceptReturnAsOK.
!

addAbortButton
    "create an abortButton - to be sent from redefined initialize
     methods in subclasses."

    buttonPanel subViews size == 1 ifTrue:[
	buttonPanel layout:#fitSpace.
    ].
    abortButton := Button abortButtonIn:buttonPanel.
    abortButton model:self; change:#abortPressed.
!

reAdjustGeometry
    "sent late in snapin processing - gives me a chance
     to resize for changed font dimensions."

    super reAdjustGeometry.
    okButton notNil ifTrue:[okButton resize].
    abortButton notNil ifTrue:[abortButton resize].
    self resize
! !

!DialogBox methodsFor:'private'!

hideAndEvaluate:aBlock
    "make myself invisible and evaluate aBlock"

    self hide.
    aBlock notNil ifTrue:[aBlock value]
! !

!DialogBox methodsFor:'accessing'!

acceptReturnAsOK:aBoolean
    "turn on/off interpretation of return-key as ok.
     Default is on"

    acceptReturnAsOK := aBoolean.
    okButton notNil ifTrue:[
	okButton isReturnButton:aBoolean.
    ]
!

okAction:aBlock
    "define the action to be performed when ok is pressed"

    okAction := aBlock
!

action:aBlock
    "set the action to be performed when user presses ok-button;
     aBlock must be nil or a block. This method simply
     reuses okAction: and has been added for a consistent action-setting
     protocol."

    self okAction:aBlock
!

okButton
    "return the okButton"

    ^ okButton
!

okText:aString
    "define the text in the ok-button"

    |oldSize|

    aString ~= okButton label ifTrue:[
	oldSize := okButton extent.
	okButton label:aString.
	okButton resize.
	okButton extent ~= oldSize ifTrue:[
	    shown ifTrue:[self resize]
	]
    ]
!

abortAction:aBlock
    "define the action to be performed when abort is pressed"

    abortAction := aBlock
!

abortButton
    "return the abortButton"

    ^ abortButton
!

abortText:aString
    "define the label in the abort-button"

    |oldSize|

    aString ~= abortButton label ifTrue:[
	oldSize := abortButton extent.
	abortButton label:aString.
	abortButton resize.
	abortButton extent ~= oldSize ifTrue:[
	    shown ifTrue:[self resize]
	]
    ]
!

okText:okString abortText:abortString
    "set both texts displayed in the buttons"

    (abortString ~= abortButton label 
    or:[okString ~= okButton label]) ifTrue:[
	okButton label:okString.
	abortButton label:abortString.
	okButton resize.
	abortButton resize.
	shown ifTrue:[self resize]
    ]
! !