FontMenu.st
author Claus Gittinger <cg@exept.de>
Thu, 12 Sep 2002 15:58:36 +0200
changeset 2186 05b16a66bb3d
parent 2184 16d3b72ca431
child 2189 cdbea1ba3466
permissions -rw-r--r--
updateFromModel

"
 COPYRIGHT (c) 1995 by eXept Software AG
              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:libtool2' }"

MenuPanel subclass:#FontMenu
	instanceVariableNames:'enabledChannel fontFamily fontFace fontStyle fontSize'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-UIPainter'
!

!FontMenu class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by eXept Software AG
              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
"
    FontMenu used by UIPainter

    [author:]
        Claus Atzkern

    [see also:]
        UIPainter
        FontMenuSpec
"


!

examples
"
    simple example
                                                                                [exBegin]                                      
    |top|

    top := StandardSystemView new.
    top extent:250@30.
    self origin:0.0@0.0 corner:1.0@1.0 in:top.
    top open.
                                                                                [exEnd]
"
! !

!FontMenu class methodsFor:'menu spec'!

menu

    <resource: #menu>

    ^
     
       #(#Menu
          
           #(
             #(#MenuItem
                #label: ''
                #indication: false
            )
             #(#MenuItem
                #label: '-'
            )
            (#MenuItem
            #label: 'family'
            #value: #fontFamily:
            #isButton: true
            #submenu:
               #(#Menu #( #(#MenuItem #label: 'helvetica' value:#helvetica)
                          #(#MenuItem #label: 'courier'   value:#courier)
                          #(#MenuItem #label: 'times'     value:#times)
                          #(#MenuItem #label: 'clean'     value:#clean)
                          #(#MenuItem #label: 'fixed'     value:#fixed)
                          #(#MenuItem #label: 'lucida'    value:#lucida)
                        )
                        nil
                        nil
                )
            )
            (#MenuItem
            #label: 'face'
            #value: #fontFace:
            #isButton: true
            #submenu:
               #(#Menu #( #(#MenuItem #label: 'bold'   value:#bold)     
                          #(#MenuItem #label: 'medium' value:#medium)
                        )
                        nil
                        nil
                )
            )
            (#MenuItem
            #label: 'style'
            #value: #fontStyle:
            #isButton: true
            #submenu:
               #(#Menu #( #(#MenuItem #label: 'roman'   value:#roman)     
                          #(#MenuItem #label: 'italic'  value:#italic)
                          #(#MenuItem #label: 'oblique' value:#oblique)
                         )
                        nil
                        nil
                )
            )
            (#MenuItem
            #label: 'size'
            #value: #fontSize:
            #isButton: true
            #submenu:
               #(#Menu #( #(#MenuItem #label: '6'  value:6)
                          #(#MenuItem #label: '8'  value:8)
                          #(#MenuItem #label: '10' value:10)
                          #(#MenuItem #label: '12' value:12)
                          #(#MenuItem #label: '14' value:14)
                          #(#MenuItem #label: '16' value:16)
                          #(#MenuItem #label: '18' value:18)
                          #(#MenuItem #label: 'other ...' value:#size)
                        )
                        nil
                        nil
                )
            )
          ) nil
          nil
      )
! !

!FontMenu methodsFor:'accepting'!

accept:anItem
    "accept current selected item
    "
    |item selector arg|

    selector := self selection value.

    (item := super accept:anItem) isNil ifTrue:[
        ^ self
    ].

    (arg := item value) isNil ifTrue:[
        model notNil ifTrue:[
            enabledChannel value ifTrue:[
                arg := self fontDescription
            ].
            model value:arg
        ].
      ^ self
    ].

    (arg == #size and:[(arg := self readSize) isNil]) ifTrue:[
        ^ self
    ].

    (self perform:selector with:arg) ifTrue:[
        model notNil ifTrue:[model value:(self fontDescription)]
    ]
!

readSize
    "read a size; in case of cancel nil is returned otherwise a number.
    "
    |size string|

    string := EnterBox request:'size: '.

    string size ~~ 0 ifTrue:[
        size := SmallInteger readFrom:string onError:nil.

        (size notNil and:[size >= 6 and:[size <= 48]]) ifTrue:[
            ^ size
        ]
    ].
  ^ nil
! !

!FontMenu methodsFor:'accessing'!

fontDescription
    "get font description
    "
  ^ FontDescription family:fontFamily face:fontFace style:fontStyle size:fontSize
!

fontDescription:aFontDesc
    "set font description
    "
    aFontDesc isNil ifTrue:[
        enabledChannel value:false.
    ] ifFalse:[
        self disabledRedrawDo:[
            enabledChannel value:true.
            (aFontDesc isSymbol or:[aFontDesc isString]) ifFalse:[
                self fontFamily:(aFontDesc family).
                self   fontFace:(aFontDesc face).
                self  fontStyle:(aFontDesc style).
                self   fontSize:(aFontDesc size).
            ]
        ]
    ]
!

fontFace
    "get face, a string
    "
    ^ fontFace
!

fontFace:aFace
    "set face, a string; update item.
     if the face changed true is returned otherwise nil
    "
    (aFace notNil and:[aFace ~= fontFace]) ifTrue:[
        (self itemAt:#fontFace:) label:(fontFace := aFace).
      ^ true
    ].
  ^ false
!

fontFamily
    "get family, a string
    "
    ^ fontFamily
!

fontFamily:aFamily
    "set family, a string; update item
     if the family changed true is returned otherwise nil
    "
    (aFamily notNil and:[aFamily ~= fontFamily]) ifTrue:[
        (self itemAt:#fontFamily:) label:(fontFamily := aFamily).
      ^ true
    ].
  ^ false

!

fontSize
    "get size, a number
    "
  ^ fontSize
!

fontSize:aSize
    "set size, a number; update item
     if the size changed true is returned otherwise nil
    "
    (aSize notNil and:[aSize ~= fontSize]) ifTrue:[
        fontSize := aSize.
        (self itemAt:#fontSize:) label:(aSize printString).
      ^ true
    ].
  ^ false
!

fontStyle
    "get style, a string
    "
  ^ fontStyle

!

fontStyle:aStyle
    "set style, a string; update item
     if the style changed true is returned otherwise nil
    "
    (aStyle notNil and:[aStyle ~= fontStyle]) ifTrue:[
        (self itemAt:#fontStyle:) label:(fontStyle := aStyle).
      ^ true
    ].
  ^ false
! !

!FontMenu methodsFor:'accessing-channels'!

model:aValueHolder
    "set my model
    "
    super model:aValueHolder.
    model notNil ifTrue:[
        self updateFromModel
    ].
! !

!FontMenu methodsFor:'change & update'!

updateFromModel
    self fontDescription:(model value)
! !

!FontMenu methodsFor:'initialization'!

destroy
    "release dependencies
    "
    self model:nil.
    super destroy.

!

initialize
    "setup menu
    "
    super initialize.
    self verticalLayout:false.
    self fitFirstPanel:false.
    self menu:(self class menu).
    enabledChannel := false asValue.

    self fontDescription:(self font).

    self do:[:anItem|
        anItem hasIndication ifFalse:[
            anItem enabled:enabledChannel
        ] ifTrue:[
            anItem indication:enabledChannel
        ]
    ].


! !

!FontMenu class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/FontMenu.st,v 1.12 2002-09-12 13:58:27 cg Exp $'
! !