OptionBox.st
author claus
Mon, 06 Feb 1995 01:53:30 +0100
changeset 77 565b052f5277
parent 63 f4eaf04d1eaf
child 85 c229bb31d758
permissions -rw-r--r--
*** empty log message ***

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

ModalBox subclass:#OptionBox
	 instanceVariableNames:'formLabel textLabel buttons actions'
	 classVariableNames:'WarnBitmap'
	 poolDictionaries:''
	 category:'Views-DialogBoxes'
!

OptionBox comment:'
COPYRIGHT (c) 1991 by Claus Gittinger
	     All Rights Reserved

$Header: /cvs/stx/stx/libwidg/OptionBox.st,v 1.10 1995-02-06 00:52:53 claus Exp $
'!

!OptionBox class methodsFor:'documentation'!

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

version
"
$Header: /cvs/stx/stx/libwidg/OptionBox.st,v 1.10 1995-02-06 00:52:53 claus Exp $
"
!

documentation
"
    OptionBoxes are like YesNoBoxes but with as many buttons as you like;
    will finally be a superclass of WarnBox and YesNoBox - or maybe merged
    all into DialogBox..
    Used for multiway questions.
"
! !

!OptionBox class methodsFor:'instance creation'!

title:titleString numberOfOptions:nOptions
    "create a new optionBox with title, aTitleString and nOptions options"

    |box|

    box := (self basicNew) numberOfOptions:nOptions.
    box device:Display.
    box initialize.
    box title:titleString.
    ^ box
! !

!OptionBox methodsFor:'accessing'!

title:aString numberOfOptions:nOptions
    "set the boxes title and number of options"

    aString ~= textLabel label ifTrue:[
	textLabel label:aString.
	textLabel resize.
    ].
    buttons grow:nOptions.
    actions grow:nOptions
!

title:aString
    "set the boxes title"

    aString ~= textLabel label ifTrue:[
	textLabel label:aString withoutSeparators.
	textLabel resize.
	self resize
    ]
!

formLabel
    "return the label-view which displays a bitmap"

    ^ formLabel
!

form:aFormOrImage
    "set the image shown in the label-view"

    formLabel form:aFormOrImage
!

numberOfOptions:nOptions
    "set the number of options"

    buttons := Array new:nOptions.
    actions := Array new:nOptions
!

numberOfOptions
    "return the number of options"

    ^ buttons size
!

buttons
    "return the buttons collection"

    ^ buttons
!

buttonTitles:titles
    "set the button titles"

    |index|

    index := 1.
    titles do:[:aString |
	(buttons at:index) label:aString.
	(buttons at:index) resize.
	index := index + 1
    ].
    self resize
!

actions:actionBlocks
    "define the actions"

    actions := actionBlocks
!

buttonTitles:titles actions:actionBlocks
    "define both button titles and actions"

    self buttonTitles:titles.
    actions := actionBlocks.
    self resize
! !

!OptionBox methodsFor:'initializing'!

initialize
    |nButt buttonPanel|

    super initialize.

    formLabel := Label in:self.
    self initFormBitmap.
    formLabel borderWidth:0.
    formLabel origin:(ViewSpacing @ ViewSpacing).

    textLabel := Label label:'Warning' in:self.
    textLabel borderWidth:0.
    textLabel origin:((ViewSpacing + formLabel width + ViewSpacing) @ ViewSpacing).

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

    nButt := buttons size.

    1 to:nButt do:[:index |
	|button|

	button := Button label:'press'
			action:[
				   |action|

				   (buttons at:index) turnOffWithoutRedraw.
				   self hide.
				   action := actions at:index.
				   action notNil ifTrue:[
				       action value
				   ]
			       ]
			    in:buttonPanel "self".
	buttons at:index put:button.
    ]
!

initFormBitmap
    WarnBitmap isNil ifTrue:[
	WarnBitmap := Form fromFile:'Warning.xbm' resolution:100 on:Display 
    ].
    formLabel form:WarnBitmap
!

focusSequence
    ^ buttons
! !

!OptionBox methodsFor:'queries'!

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

    ^ (buttons last originRelativeTo:self) + (buttons last extent // 2)
!

preferedExtent 
    "return a size to make everything fit into myself"

    |w w1 w2 h maxH|

    w1 := ViewSpacing + formLabel width + ViewSpacing + textLabel width + ViewSpacing.
    w2 := buttons inject:ViewSpacing into:[:sum :butt | sum + butt widthIncludingBorder  + ViewSpacing].
    w := w1 max:w2.

    maxH := 0.
    buttons do:[:button |
	maxH := maxH max:(button heightIncludingBorder)
    ].

    h := ViewSpacing
	 + ((formLabel height) max:(textLabel height))
	 + ViewSpacing + ViewSpacing
	 + maxH
	 + ViewSpacing.

    ^ w @ h
! !