'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 11 May 2008 at 10:26:30 pm'!
!classDefinition: #SliderLW category: #'LightWidgets-Kernel'!
LightWidget subclass: #SliderLW
	instanceVariableNames: 'showAxis valueBounds cursorForm'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Kernel'!

!SliderLW commentStamp: '<historical>' prior: 0!
This is a simple slider that goes from  0 to 100, it provides an axis that can be hidden or shown .


self new openInWorld
self allInstances do:[:e| e delete]
!

!classDefinition: #VolumeControl category: #'LightWidgets-Examples'!
Object subclass: #VolumeControl
	instanceVariableNames: 'volume'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Examples'!
!classDefinition: #VolumeSliderLW category: #'LightWidgets-Examples'!
CompositeLW subclass: #VolumeSliderLW
	instanceVariableNames: 'slider show showsAxis count'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Examples'!

!VolumeSliderLW commentStamp: '<historical>' prior: 0!
This examples demonstrates the use of a SliderLW, it manages a VolumeControl that goes from 0 to 100. 
It also intends to show the GUI programming with LightWidgets



self openExample!


!LightWidget methodsFor: 'view of a model or target' stamp: 'jmv 5/11/2008 22:22'!
modelChanged
	"The model changed is some way.
	This is usually the place to call #targetAspect to fetch the current value of the aspect from the
		model, and to store it in some Model Extension.
	We must update all Model Extension instance variables with values from the model (i.e. target)
		or with appropriate defaults.
	We must update ourselves and all subviews to reflect the model's new state"

	self updateView! !


!ButtonLW methodsFor: 'updating' stamp: 'jmv 5/11/2008 22:22'!
modelChanged
	"The model changed is some way.
	This is usually the place to call #targetAspect to fetch the current value of the aspect from the
		model, and to store it in some Model Extension.
	We must update all Model Extension instance variables with values from the model (i.e. target)
		or with appropriate defaults.
	We must update ourselves and all subviews to reflect the model's new state"

	pressed _ self targetAspect == true.
	
	contentsOrGetter notNil ifTrue: [
		contentsOrGetter isSymbol ifTrue: [
			label _ target perform: contentsOrGetter ]
		ifFalse: [
			label _ contentsOrGetter asString ]].
	
	super modelChanged! !


!LabelLW methodsFor: 'updating' stamp: 'jmv 5/11/2008 22:22'!
modelChanged
	"The model changed is some way.
	This is usually the place to call #targetAspect to fetch the current value of the aspect from the
		model, and to store it in some Model Extension.
	We must update all Model Extension instance variables with values from the model (i.e. target)
		or with appropriate defaults.
	We must update ourselves and all subviews to reflect the model's new state"
	
	contents _ self targetAspect.
	super modelChanged! !


!SliderLW methodsFor: 'accessing'!
backgroundColor
	^Color white! !

!SliderLW methodsFor: 'accessing'!
borderColor
	^ Color gray ! !

!SliderLW methodsFor: 'accessing'!
extent: aPoint
	bounds extent = aPoint ifTrue: [^ self].
	bounds _ bounds topLeft extent: aPoint.
	self layoutChanged.
	self updateView
	
	 
	
	! !

!SliderLW methodsFor: 'accessing'!
hideAxis
	showAxis _ false.
	self updateView! !

!SliderLW methodsFor: 'accessing'!
position: aPoint
	super position: aPoint.
	self updateView

	! !

!SliderLW methodsFor: 'accessing'!
showAxis
	showAxis _ true.
	self updateView! !

!SliderLW methodsFor: 'user actions'!
cursorMovedBack
	(target notNil and:[aspect notNil ] and:[ self targetAspect = 0] ) ifTrue:[ ^self].
	target perform: 	action with: self targetAspect - 1! !

!SliderLW methodsFor: 'user actions'!
cursorMovedForth
	(target notNil and:[aspect notNil ] and:[ self targetAspect = 100] ) ifTrue:[ ^self].
	target perform: 	action with: self targetAspect + 1! !

!SliderLW methodsFor: 'drawing'!
drawAxisOn: aCanvas	
	| h w xIncrement bw a b borderColor axisBoxHeigthHalf y0 y1 |
	bw _ self borderWidth.
	borderColor _ self borderColor.	
	h _ bounds extent y.
	w _ bounds  extent x.
	xIncrement _ ( w- (2* bw )) /10.
	a _ bounds origin + (bw @  ( bw + (h/2) + 2)).
	b _ a + ((w - (2*bw))@0).
	aCanvas line: a to: b color: borderColor.
	axisBoxHeigthHalf _ bounds extent y / 16.
	y0 _ 0 @ axisBoxHeigthHalf negated.
	y1 _ 0 @ axisBoxHeigthHalf. 
	(0 to: 10) do:[:i|	
					aCanvas drawString:  i asString at: a.
					aCanvas line: a + y0 to:  a + y1 color: borderColor.
					a _ a + (xIncrement@0)]
	! !

!SliderLW methodsFor: 'drawing' stamp: 'jmv 5/11/2008 19:56'!
drawCursorOn: aCanvas
	"aCanvas fillOval: cursorBounds color: borderColor muchLighter borderWidth:1.0 borderColor: borderColor darker."

	| bw h w xIncrement cursorW valueBoxExtent v valueOffset cursorLocation boxExtent cform |
	bw _ self borderWidth.
	h _ bounds extent y.
	w _ bounds  extent x.

	xIncrement _ ( w- (2* bw )) /10.
	cursorW _ xIncrement-2.
	valueBoxExtent _ w - (2 * bw) - cursorW.
	(target notNil and: [aspect notNil]) ifTrue:[v _ self targetAspect] ifFalse:[ v_0].
	valueOffset _  v * (valueBoxExtent/100).
	
	cursorLocation _ bounds origin +(  (bw+valueOffset)@((bounds extent y /3) asFloat)).
	showAxis
		ifTrue:[ boxExtent _ cursorW  @(h/4)]
		ifFalse:[ boxExtent _ cursorW @ (bounds extent y -( 2*bw))].
	cform _ self cursorForm: boxExtent.
	aCanvas drawImage: cform at: cursorLocation
	! !

!SliderLW methodsFor: 'drawing'!
drawOn: aCanvas
	| borderColor |
	borderColor _ self borderColor.
	aCanvas fillRectangle: bounds color: self backgroundColor.
	aCanvas fillRectangle: valueBounds color: borderColor muchLighter.
	showAxis ifTrue:[ self drawAxisOn: aCanvas].
	self drawCursorOn: aCanvas.
	aCanvas frameRectangle: bounds width: self borderWidth color: borderColor! !

!SliderLW methodsFor: 'event handling'!
keyDown: evt
	"Handle a keystroke event."
	(evt commandKeyPressed not and:[evt  keyCharacter = Character arrowRight]) ifTrue: [self cursorMovedForth].
	(evt commandKeyPressed not and:[evt  keyCharacter = Character arrowLeft]) ifTrue: [self cursorMovedBack].
	super keyStroke: evt
	
 ! !

!SliderLW methodsFor: 'updating'!
updateValueBox
	| plus axisBoxHeigth |
	axisBoxHeigth _ (bounds extent y) / 4.
	showAxis 
		ifTrue:[ plus _ (bounds extent y - axisBoxHeigth) /2] 
		ifFalse:[plus _ bounds extent y  /2].
	valueBounds _ (bounds origin + (0@plus asFloat))  extent: (bounds extent x @  (axisBoxHeigth/2)). ! !

!SliderLW methodsFor: 'updating'!
updateView
	self updateValueBox.
	self changed 
! !

!SliderLW methodsFor: 'initialization'!
initialize
	| w h |
	super initialize.
	w _ 200.
	h_50.
	bounds _ 10@10 extent: w@h.
	showAxis _ true.
	self updateView
	 
! !

!SliderLW methodsFor: 'private' stamp: 'F Olivero 5/9/2008 14:50'!
borderWidth 
	^ 4.0! !

!SliderLW methodsFor: 'private' stamp: 'jmv 5/11/2008 20:00'!
cursorForm: extent
	| w h |
	w _ extent x asInteger.
	h _ extent y asInteger.
	(cursorForm isNil or: [(cursorForm width = w) not]) ifTrue: [
		cursorForm _ (Form fromFileNamed: 'cursor.jpeg') scaledToSize: w@h ].
	^cursorForm! !

!SliderLW methodsFor: 'events-processing'!
handlesKeyboard: evt
	^true! !


!VolumeControl methodsFor: 'as yet unclassified'!
initialize
	volume _ 0! !

!VolumeControl methodsFor: 'as yet unclassified'!
volume
	^volume ! !

!VolumeControl methodsFor: 'as yet unclassified'!
volume: v
	volume _ v.
	self triggerEvent:#selfChanged! !


!VolumeSliderLW methodsFor: 'initialization'!
addSubmorphs
	slider _ SliderLW new.
	slider position: 30@50.
	self addMorph: slider.

	count _ LabelLW new.
	count position: 100@100.
	self addMorph: count.
	
	show _ ButtonLW new.
	show bounds: (20@10 extent: 120@30).
	self addMorph: show
	
	! !

!VolumeSliderLW methodsFor: 'initialization'!
initialize
	super initialize.
	bounds _ 10@10 extent: 250@150.
	showsAxis  _ false.
	self addSubmorphs
	 ! !

!VolumeSliderLW methodsFor: 'event handlers'!
toggleAxis
	showsAxis _ showsAxis not.
	self updateView
	! !

!VolumeSliderLW methodsFor: 'updating'!
updateCount
	count target: target aspect:#volume  ! !

!VolumeSliderLW methodsFor: 'updating'!
updateShow
	| m |
	showsAxis ifTrue:[ m _ 'hide axis'] ifFalse:[ m _ 'show axis'].
	show
		target: self action: #toggleAxis;
		label: m  ! !

!VolumeSliderLW methodsFor: 'updating'!
updateSlider
	slider
		target: target aspect:#volume; 
		action: #volume:.
	showsAxis ifTrue:[slider showAxis] ifFalse:[ slider hideAxis].  ! !

!VolumeSliderLW methodsFor: 'updating'!
updateView
	self
		updateShow;
		updateSlider;
		updateCount  ! !


!VolumeSliderLW class methodsFor: 'instance creation'!
openExample
	| vc example |
	vc _ VolumeControl new.
	example _ self new.
	example beMainViewOn: vc.
	^ example openInWorld
! !

SliderLW removeSelector: #cursorForm!
