ViewStyle.st
author Claus Gittinger <cg@exept.de>
Wed, 22 Aug 2018 12:58:11 +0200
changeset 8451 6eafe0433763
parent 7711 11ee38c0349c
child 7723 620e91f9b082
child 8518 f7fd6d8ee737
permissions -rw-r--r--
#QUALITY by cg class: WindowSensor comment/format in: #basicAddDamage:view:

"
 COPYRIGHT (c) 1993 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.
"
"{ Package: 'stx:libview' }"

"{ NameSpace: Smalltalk }"

ResourcePack subclass:#ViewStyle
	instanceVariableNames:'name is3D viewGrey'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Support'
!

!ViewStyle class methodsFor:'documentation'!

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

documentation
"
    instances of this class keep all view-style specific information.
    The current viewStyle is kept in Views-classvariable called 'StyleSheet'
    and is instantiated with 'View defaultStyle:aStyleSymbol', which reads
    a stylesheet from a file '<aStyleSymbol>.style' (usually in the 'resources'
    directory.

    [see also:]
	View
	( Configuration & Customization :html: custom/TOP.html#VIEWSTYLE )

    [author:]
	Claus Gittinger
"
! !

!ViewStyle class methodsFor:'instance creation'!

fromFile:aFileNameArg
    "get the preferences definitions from a file"

    |aFileName prefs failed nm nmInStyles dir path baseName|

    aFileName := aFileNameArg asFilename.
    (aFileName hasSuffix:'style') ifTrue:[
        baseName := aFileName withoutSuffix baseName.
        nm := aFileName.
    ] ifFalse:[
        baseName := aFileName baseName.
        nm := aFileName withSuffix:'style'.
    ].
    nm isAbsolute ifFalse:[
        nmInStyles := 'styles' asFilename / nm
    ].

    prefs := self new.
    prefs at:#name put:baseName. 

    nmInStyles notNil ifTrue:[
        path := Smalltalk getResourceFileName:nmInStyles forPackage:'stx:libview'.
    ].
    path isNil ifTrue:[
        path := Smalltalk getResourceFileName:nm forPackage:'stx:libview'.
    ].
    path notNil ifTrue:[
        dir := path asFilename directory pathName.
    ].
    failed := (prefs readFromFile:nm directory:dir) isNil.
    prefs at:#fileReadFailed put:failed. 
    ^ prefs

    "
     ViewStyle fromFile:'motif'  
     ViewStyle fromFile:'motif.style'  
     ViewStyle fromFile:'normal.style' 
     ViewStyle fromFile:'iris.style'
     ViewStyle fromFile:'iris.style' asFilename
    "

    "Modified: / 10.12.1999 / 16:05:30 / cg"
! !

!ViewStyle class methodsFor:'constants'!

adwaita
    ^ #Adwaita

    "Created: / 24-11-2016 / 17:54:58 / cg"
!

macosx_yosemite
    ^ #'macosx_yosemite'

    "Created: / 24-11-2016 / 17:55:46 / cg"
!

msWindows8
    ^ #mswindows8
!

msWindowsVista
    ^ #mswindowsVista
!

msWindowsXP
    ^ #mswindowsXP
!

normal
    ^ #normal
! !

!ViewStyle methodsFor:'accessing'!

at:aKey
    |sCls val|

    sCls := thisContext sender receiver.
    sCls isBehavior ifFalse:[sCls := sCls class].
    (sCls isSubclassOf:SimpleView) ifTrue:[
	val := self at:(sCls name , '.' , aKey) default:nil.
	val notNil ifTrue:[^ val].
    ].
    ^ self at:aKey default:nil

    "Modified: 10.9.1995 / 10:59:38 / claus"
!

at:aKey default:default
    "translate a string; if not present, return default.
     Here, two keys are tried, iff the key is of the form 'foo.bar',
     'fooBar' is also tried.
     This has been added for a smooth migration towards names with a form of
     'classname.itemKey' in the stylesheets."

    |v i k2|

    (self includesKey:aKey) ifTrue:[
	^ (super at:aKey ifAbsent:default) value
    ].
    (i := aKey indexOf:$.) ~~ 0 ifTrue:[
	k2 := (aKey copyTo:i-1) , (aKey copyFrom:i+1) asUppercaseFirst.
	(self includesKey:k2) ifTrue:[^ super at:k2 ifAbsent:default].
    ].
    ^ default value

    "Created: / 14.10.1997 / 00:21:15 / cg"
    "Modified: / 15.9.1998 / 21:47:13 / cg"
!

colorAt:aKey
    "retrieve a color resource - also acquire a device color
     to avoid repeated color allocations later"

    |value device deviceColor|

    device := Display.

    value := self at:aKey default:nil.
    value isInteger ifTrue:[
        value := Color rgbValue:value
    ].
    (value notNil and:[device notNil]) ifTrue:[
        deviceColor := value onDevice:device.
        deviceColor notNil ifTrue:[^ deviceColor].
    ].
    ^ value
!

colorAt:aKey default:default
    "retrieve a color resource - also acquire a device color
     to avoid repeated color allocations later"

    |value device deviceColor|

    device := Display.

    value := self at:aKey default:default.
    value isInteger ifTrue:[
        value := Color rgbValue:value
    ].
    (value notNil and:[device notNil]) ifTrue:[
        deviceColor := value onDevice:device.
        deviceColor notNil ifTrue:[^ deviceColor].
    ].
    ^ value
!

deviceResourceAt:aKey default:default
    "retrieve a resource - also acquire a device version
     for the default display, to avoid repeated allocations later"

    |aResource deviceResource device|

    device := Display.

    aResource := self at:aKey default:default.
    (aResource notNil and:[device notNil]) ifTrue:[
        deviceResource := aResource onDevice:device.
        deviceResource notNil ifTrue:[^ deviceResource].
    ].
    ^ aResource

    "Modified: / 5.9.1998 / 20:25:19 / cg"
!

doesNotUnderstand:aMessage
    ^ self at:(aMessage selector) default:nil
!

fontAt:aKey
    "retrieve a font resource - also acquire a device font
     to avoid repeated font allocations later"

    ^ self deviceResourceAt:aKey default:nil 
!

fontAt:aKey default:default
    "retrieve a font resource - also acquire a device font
     to avoid repeated font allocations later"

    ^ self deviceResourceAt:aKey default:default
!

is3D
    is3D isNil ifTrue:[
	is3D := self at:#is3D default:false.
    ].
    ^ is3D
!

name
    name isNil ifTrue:[
	name := self at:#name default:'noname'.
    ].
    ^ name
!

viewGrey
    viewGrey isNil ifTrue:[
        viewGrey := self at:#viewGrey default:nil.
    ].
    ^ viewGrey
! !

!ViewStyle methodsFor:'error handling'!

nonexistingFileRead
    "here, a non-existing stylesheet is treated as an error"

    fileReadFailed := true.
    ^ self

    "Created: 6.9.1997 / 11:40:16 / cg"
! !

!ViewStyle methodsFor:'queries'!

isWindowsStyle
    "answer true if this is a MS-Windows style.
     XXX Currently we simply check the name"

    |nm|

    nm := name ? self name.

    ^ nm == #win95 
    or:[nm == #win98
    or:[nm == #winXP
    or:[nm == #winVista ]]]
!

isWindowsVistaStyle
    ^ (name ? self name) == #winVista

    "Modified (format): / 19-11-2016 / 15:49:48 / cg"
!

isWindowsXPStyle
    "answer true if this is a MS-Windows-XP (or later) style.
     XXX Currently we simply check the name"

    |nm|

    nm := name ? self name.

    ^ nm == #winXP 
    or:[nm == #winVista]
! !

!ViewStyle methodsFor:'special'!

newDerivedStyle
    "create and return a new style, which inherits from
     the receiver, but possibly overrides individual entries.
     This may be useful to give a single button some different
     look (in the future - read the comment in SimpleView>>viewStyle:)"

    ^ self class new
        superPack:self; 
        at:#is3D put:(self is3D);
        at:viewGrey put:(self viewGrey);
        yourself

    "
     |panel b1 b2 newStyle|

     panel := HorizontalPanelView new.
     panel add:(b1 := Button label:'oldStyle').
     panel add:(b2 := Button label:'newStyle').
     newStyle := b1 styleSheet newDerivedStyle.
     newStyle at:'button.activeBackgroundColor' put:Color blue.
     b2 styleSheet:newStyle.        

     panel open
    "
! !

!ViewStyle class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !