DialogBox.st
author claus
Mon, 06 Feb 1995 01:52:01 +0100
changeset 76 81e3409404d2
child 91 e8db16616e97
permissions -rw-r--r--
Initial revision

"
 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.1 1995-02-06 00:52:01 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
"
    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"

    self hideAndEvaluate:okAction
!

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

    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:[
	^ self extent // 2
    ].
    buttonPanel setChildPositionsIfChanged.
    ^ (okButton originRelativeTo:self) + (okButton extent // 2)
!

preferedExtent 
    "return my prefered extent"

    |w h|

    okButton isNil ifTrue:[
	^ super preferedExtent
    ].
    w := okButton preferedExtent x + (ViewSpacing * 2).
    h := ViewSpacing
	 + okButton preferedExtent 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 action:[
		       okButton turnOffWithoutRedraw.
		       self 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 action:[
			   abortButton turnOffWithoutRedraw.
			   self 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:[
	    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:[
	    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.
	self resize
    ]
! !