OptionBox.st
author claus
Sat, 08 Jan 1994 18:27:56 +0100
changeset 21 9ef599238fea
parent 16 83d4f5c6f76c
child 38 4b9b70b2cc87
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-Interactors'
!

OptionBox comment:'

COPYRIGHT (c) 1991 by Claus Gittinger
             All Rights Reserved

$Header: /cvs/stx/stx/libwidg/OptionBox.st,v 1.6 1994-01-08 17:27:37 claus Exp $

written Nov 91 by claus
'!

!OptionBox class methodsFor:'documentation'!

documentation
"
OptionBoxes are like YesNoBoxes but with as many as you like buttons in it;
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:ModalDisplay.
    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
    ]
!

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|

    super initialize.

    WarnBitmap isNil ifTrue:[
        WarnBitmap := Form fromFile:'Warning.xbm' resolution:100 on:device
    ].

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

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

    nButt := buttons size.

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

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

                                   (buttons at:index) turnOff.
                                   self hide.
                                   action := actions at:index.
                                   action notNil ifTrue:[
                                       action value
                                   ]
                               ]
                            in:self.
        buttons at:index put:button.
        button origin:[( (index - 1) * ((width - ViewSpacing) // nButt) + (ViewSpacing // 2) )
                       @
                       (height - ViewSpacing - (buttons at:index) height)].
        button extent:[(width // nButt - ViewSpacing) 
                       @ 
                       (buttons at:index) height]
    ]
! !

!OptionBox methodsFor:'private'!

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)
!

resize
    "resize myself to make everything fit into myself"

    |w w1 w2 h extra|

    w1 := ViewSpacing + formLabel width + ViewSpacing + textLabel width + ViewSpacing.

    w2 := 0.
    buttons do:[:butt |
        w2 := w2 + butt width "labelWidth".
        w2 := w2 + ViewSpacing
    ].
    w2 := w2 + (4 * ViewSpacing).
    w := w1 max:w2.

    h := ViewSpacing
         + ((formLabel height) max:(textLabel height))
         + ViewSpacing + ViewSpacing
         + (buttons at:1) height
         + ViewSpacing.

    "extra := margin * 2."
    super extent:(w "+ extra") @ (h "+ extra")
! !