TabulatorSpecification.st
author Claus Gittinger <cg@exept.de>
Thu, 22 Feb 1996 21:49:59 +0100
changeset 128 cd2b82b1c222
parent 98 de14b996ee80
child 158 431e38dfc5ab
permissions -rw-r--r--
checkin from browser

"
 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.
"

Object subclass:#TabulatorSpecification
	instanceVariableNames:'tabUnit unitReference tabPositions tabTypes'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Support'
!

!TabulatorSpecification class methodsFor:'documentation'!

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.
"
!

documentation
"
    This is a helper class for table widgets and tabular data in
    lists.
    A tabulatorSpecification keeps track of where the tabs are,
    and how they align. They are to be used in conjunction with
    MultiColumnListEntry or the upcoming TableWidget.
    However, they may also be useful to represent tabs in a
    paragraph of text.
"
!

examples 
"
    Example use (in a ListView):

	|listView tabSpec entry|

	listView := ListView new.

	tabSpec := TabulatorSpecification new.
	tabSpec unit:#inch.
	tabSpec positions:#(0     1     2.5    3.5    4       5        ).
	tabSpec align:    #(#left #left #right #right #center #decimal ).

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'left';
	      colAt:2 put:'left';
	      colAt:3 put:'right';
	      colAt:4 put:'right';
	      colAt:5 put:'center';
	      colAt:6 put:'.decimal'.

	listView at:1 put:entry.

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'col1';
	      colAt:2 put:'col2';
	      colAt:3 put:'col3';
	      colAt:4 put:'col4';
	      colAt:5 put:'col5';
	      colAt:6 put:'col6.decimal'.

	listView at:2 put:entry.

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'foo';
	      colAt:2 put:'fooBar';
	      colAt:3 put:'bar';
	      colAt:4 put:'barFoo';
	      colAt:5 put:'baz';
	      colAt:6 put:'1234.56'.

	listView at:3 put:entry.
	listView open


    defining field positions in millimeter :

	|listView tabSpec entry|

	listView := ListView new.

	tabSpec := TabulatorSpecification new.
	tabSpec unit:#mm.
	tabSpec positions:#(0 10 20 40).
	tabSpec align:    #left.          

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'1';
	      colAt:2 put:'2';
	      colAt:3 put:'3';
	      colAt:4 put:'4'.

	listView at:1 put:entry.

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'aa';
	      colAt:2 put:'bb';
	      colAt:3 put:'cc';
	      colAt:4 put:'dd'.

	listView at:2 put:entry.

	listView open

    defining field widths in millimeter :

	|listView tabSpec entry|

	listView := ListView new.

	tabSpec := TabulatorSpecification new.
	tabSpec unit:#mm.
	tabSpec widths:#(10 10 20 10).
	tabSpec align:    #left.        

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'1';
	      colAt:2 put:'2';
	      colAt:3 put:'3';
	      colAt:4 put:'4'.

	listView at:1 put:entry.

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'aa';
	      colAt:2 put:'bb';
	      colAt:3 put:'cc';
	      colAt:4 put:'dd'.

	listView at:2 put:entry.

	listView open

    defining field widths in pixels :

	|listView tabSpec entry|

	listView := ListView new.

	tabSpec := TabulatorSpecification new.
	tabSpec unit:#pixel.
	tabSpec widths:#(50 30 30 50).
	tabSpec align:    #left.        

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'1';
	      colAt:2 put:'2';
	      colAt:3 put:'3';
	      colAt:4 put:'4'.

	listView at:1 put:entry.

	entry := MultiColListEntry new.
	entry tabulatorSpecification:tabSpec.
	entry colAt:1 put:'aa';
	      colAt:2 put:'bb';
	      colAt:3 put:'cc';
	      colAt:4 put:'dd'.

	listView at:2 put:entry.

	listView open
"
! !

!TabulatorSpecification class methodsFor:'instance creation'!

unit:unit positions:positions
    ^ (self new) unit:unit; positions:positions

    "
     TabulatorSpecification unit:#inch positions:#(0 3)
    "

    "Modified: 30.8.1995 / 16:37:50 / claus"
! !

!TabulatorSpecification methodsFor:'accessing'!

align
    "return the align-vector"

    ^ tabTypes
!

align:types
    "
     an array of tab-types; each one is
	#left
	#right
	#center
	#decimal
     or a symbol which gives align of all tabs

    "
    tabTypes := types
!

positions
    "return the position-vector"

    ^ tabPositions
!

positions:tabs
    "set the position-vector"

    tabPositions := tabs
!

size
    "return the number of tabs in this spec"

    ^ tabPositions size
!

unit
    "return the unit"

    ^ tabUnit
!

unit:aSymbol
    "set the unit.
     allowed are: #inch, #mm, #cm, #pixel and #col"

    tabUnit := aSymbol
!

unitRelativeTo:someObject
    "set for a relative unit. someObject should return its width
     and the tabs are set fraction-relative to this number (in pixel)."

    tabUnit := #relative.
    unitReference := someObject
!

widths
    "return a width-vector"

    |prev|

    prev := 0.
    ^ tabPositions collect:[:p | |w| w := p - prev. prev := p. w].

    "
     |spec|

     spec := TabulatorSpecification new.
     spec unit:#inch.
     spec positions:#(0     1     2.5    3.5    4       5        ).
     spec align:    #(#left #left #right #right #center #decimal ).
     spec widths
    "
!

widths:fieldWidths
    "set the position-vector from a vector of field widths"

    |pos|

    pos := 0.
    tabPositions := fieldWidths collect:[:w | 
					    |p|

					    p := pos.
					    pos := pos + w.
					    p].
! !

!TabulatorSpecification methodsFor:'queries'!

pixelsPerUnitOn:aGC
    |device mm|

    "
     return the number of device pixels one unit of my tabs
     takes on aGC
    "
    tabUnit isNil ifTrue:[
        tabUnit := #col
    ].
    tabUnit == #relative ifTrue:[
        ^ unitReference width
    ].
    tabUnit == #col ifTrue:[
        ^ aGC font width
    ].
    device := aGC device.
    tabUnit == #inch ifTrue:[
        ^ device horizontalPixelPerInch
    ].
    mm := device horizontalPixelPerMillimeter.
    tabUnit == #mm ifTrue:[
        ^ mm
    ].
    tabUnit == #cm ifTrue:[
        ^ mm * 10
    ].
    "
     assume pixels
    "
    ^ 1.

    "Modified: 22.2.1996 / 16:48:44 / cg"
!

positionOfTab:index forString:aString on:aGC
    "return the position (in device units) of the string to be drawn
     at position index."

    |pos type idx left|

    pos := self positionOfTab:index on:aGC.
    pos isNil ifTrue:[^ nil].

    type := self typeOfTab:index.

    type == #right ifTrue:[
	^ pos - (aGC font widthOf:aString).
    ].
    type == #center ifTrue:[
	^ pos - ((aGC font widthOf:aString) // 2).
    ].
    type == #decimal ifTrue:[
	idx := aString indexOf:$..
	idx == 0 ifTrue:[
	     ^ pos - (aGC font widthOf:aString).
	].
	left := aString copyTo:(idx-1).
	^ pos - (aGC font widthOf:left).
    ].
    "default is left"
    ^ pos
!

positionOfTab:index on:aGC
    "return the position (in device units) of the tab at index"

    |unit pos|

    tabPositions isNil ifTrue:[^ nil].

    unit := self pixelsPerUnitOn:aGC.
    pos := ((tabPositions at:index) * unit).
    ^ pos
!

typeOfTab:index
    "return the type of the tab at position index."

    tabPositions isNil ifTrue:[^ #left].
    tabTypes notNil ifTrue:[
	(tabTypes isMemberOf:Symbol) ifTrue:[
	    ^ tabTypes
	].
	^ tabTypes at:index.
    ].
    "default is left"
    ^ #left
! !

!TabulatorSpecification class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/TabulatorSpecification.st,v 1.9 1996-02-22 20:49:59 cg Exp $'
! !