'From Squeak3.7 of ''4 September 2004'' [latest update: #5989] on 18 November 2008 at 1:05:58 pm'!
!classDefinition: #EntryField2LW category: #'LightWidgets-Kernel'!
LightWidget subclass: #EntryField2LW
	instanceVariableNames: 'font contents editor foreColor backColor selectedForeColor selectedBackColor'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LightWidgets-Kernel'!

!LightWidget methodsFor: 'debug and other' stamp: 'jmv 11/17/2008 15:45'!
resumeAfterDrawError

	self changed.
	self drawingFailsNot.
	self changed.! !


!CheckBoxLW methodsFor: 'unaccepted edits' stamp: 'jmv 11/17/2008 16:08'!
hasUnacceptedEdits: aBoolean
	"Set the hasUnacceptedEdits flag to the given value. "
	
	self flag: #jmv.
	"Not used in this morph, as it doesn't do accept / cancel"
	self changed! !


!EntryField2LW methodsFor: 'testing' stamp: 'jmv 11/17/2008 15:40'!
hasCaret

	^ self hasKeyboardFocus and: [editor hasCaret]! !

!EntryField2LW methodsFor: 'testing' stamp: 'jmv 11/17/2008 15:40'!
hasSelection

	^editor notNil and: [editor hasSelection]! !

!EntryField2LW methodsFor: 'drawing' stamp: 'jmv 11/17/2008 16:03'!
characterIndexAtPoint: aPoint

	| line block f |
	f _ self fontToUse.
	
	line _ OldTextLine 
		start: 1
		stop: contents size
		internalSpaces: 0
		paddingWidth: 0.
	line
		rectangle: bounds;
		lineHeight: f height baseline: f ascent.
		
	block _ (CharacterBlockScanner new text: contents asText textStyle: (TextStyle fontArray: {f}))
		characterBlockAtPoint: aPoint index: nil
		in: line.

	^ block stringIndex! !

!EntryField2LW methodsFor: 'drawing' stamp: 'jmv 11/17/2008 16:07'!
drawCaretOn: aCanvas
	|  w caretColor leftX top bottom rightX |

	top _ bounds top.
	bottom _ top + font height.
	leftX _ (font widthOfString: contents from: 1 to: editor startIndex-1) + bounds left.
	rightX := leftX + 1.
	w := self caretWidth.
	caretColor := self insertionPointColor.
	
	1 to: w do: [:i |
		"Draw caret triangles at top and bottom"
		aCanvas fillRectangle: ((leftX - w + i - 1) @ (top + i - 1) 
				extent: ((w - i) * 2 + 3) @ 1)
			color: caretColor.
		aCanvas fillRectangle: ((leftX - w + i - 1) @ (bottom - i) 
				extent: ((w - i) * 2 + 3) @ 1)
			color: caretColor].
	aCanvas fillRectangle: (leftX @ top corner: rightX @ bottom)
		color: caretColor! !

!EntryField2LW methodsFor: 'drawing' stamp: 'jmv 11/17/2008 16:11'!
drawOn: aCanvas

	aCanvas frameAndFillRectangle: bounds fillColor: self backColor borderWidth: 2 borderColor: self borderColor.
	self hasSelection ifTrue: [
		self drawSelectionOn: aCanvas ].
	self hasCaret ifTrue: [
		self drawCaretOn: aCanvas].
	aCanvas drawString: contents in: bounds font: font color: self foreColor! !

!EntryField2LW methodsFor: 'drawing' stamp: 'jmv 11/17/2008 16:07'!
drawSelectionOn: aCanvas
	| rightX leftX top bottom |

	top _ bounds top.
	bottom _ top + font height.
	leftX _ (font widthOfString: contents from: 1 to: editor startIndex-1) + bounds left.
	rightX _ (font widthOfString: contents from: 1 to: editor stopIndex-1) + bounds left.

	aCanvas fillRectangle: (leftX @ top corner: rightX @ bottom)
		color: self selectionColor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:01'!
backColor
	^self hasKeyboardFocus
		ifTrue: [ selectedBackColor ]
		ifFalse: [ backColor ]! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:01'!
backColor: aColor
	backColor _ aColor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:03'!
caretWidth
	^ 2! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:05'!
editor
	"Return my current editor, or install a new one."
	editor ifNotNil: [^ editor].
	^ self installEditorToReplace: nil! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:01'!
font: aFont
	font _ aFont! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:02'!
foreColor
	^self hasKeyboardFocus
		ifTrue: [ selectedForeColor ]
		ifFalse: [ foreColor ]! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:02'!
foreColor: aColor
	foreColor _ aColor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:03'!
insertionPointColor

	^ Display depth <= 2
		ifTrue: [Color black]
		ifFalse: [Preferences insertionPointColor]! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:06'!
installEditorToReplace: priorEditor
	"Install an editor for my contents.  This constitutes 'hasFocus'.
	If priorEditor is not nil, then initialize the new editor from its state.
	We may want to rework this so it actually uses the prior editor."

	| stateArray |
	priorEditor ifNotNil: [stateArray := priorEditor stateArray].
	editor := SimpleEditor new morph: self.
	editor changeString: contents.
	priorEditor ifNotNil: [editor stateArrayPut: stateArray].
	self changed.
	^editor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:02'!
selectedBackColor: aColor
	selectedBackColor _ aColor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:02'!
selectedForeColor: aColor
	selectedForeColor _ aColor! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:02'!
selectionColor
	| c |
	Display depth = 1 ifTrue: [^ Color veryLightGray].
	Display depth = 2 ifTrue: [^ Color gray: 0.87].
	c := Preferences textHighlightColor.
	self hasKeyboardFocus ifFalse: [c := c alphaMixed: 0.2 with: Color veryVeryLightGray].
	^ c! !

!EntryField2LW methodsFor: 'accessing' stamp: 'jmv 11/17/2008 16:06'!
updateFromContents


	"O contents deberia ser un StringHolder????"
	contents _ editor string.
	self changed! !

!EntryField2LW methodsFor: 'initialization' stamp: 'jmv 11/17/2008 16:00'!
initialize
	super initialize.
"	target _ self.
	aspect _ #visible.
	action _ #visible:."
	bounds _ 10@10 extent: 200@30.
	contents _ 'Entry Field'.
	font _ (TextStyle named: 'Vera') fontNamed: 'Vera 12'.
	foreColor _ Color white.
	selectedForeColor _ Color white.
	backColor _ Color r: 0.32 g: 0.39 b: 0.63.
	selectedBackColor _ Color r: 0.42 g: 0.5 b: 0.81.! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:04'!
handlesKeyboard

	^true! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:04'!
handlesMouseDown: evt
	^ self innerBounds containsPoint: evt cursorPoint! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:05'!
keyStroke: evt
	"Handle a keystroke event."

	(evt commandKeyPressed and: [ self focusKeyboardFor: evt ])
		ifTrue: [ ^ self ].

	"CR - check for special action
	Note: Code below assumes that this was some
	input field reacting on CR. Break the keyboard
	focus so that the receiver can be safely deleted.
	jmv - Currently not implemented"
	"
	evt keyValue = 13 ifTrue: [
		action _ self crAction.
		action ifNotNil: [
			evt hand newKeyboardFocus: nil.
			^action value ] ].
	"
	self handleInteraction: [editor readKeyboard] fromEvent: evt.
	self updateFromContents.
	super keyStroke: evt  "sends to keyStroke event handler, if any"! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:05'!
keyboardFocusChange: aBoolean 

	aBoolean ifTrue: [
		"A hand is wanting to send us characters... Ensure editor."
		self editor ].
	self changed! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:03'!
mouseDown: evt
	"Make this TextMorph be the keyboard input focus, if it isn't already,
		and repond to the text selection gesture."

	evt hand newKeyboardFocus: self.
	self handleInteraction: [
		editor mouseDown: evt index: (self characterIndexAtPoint: evt cursorPoint) ] fromEvent: evt! !

!EntryField2LW methodsFor: 'event handling' stamp: 'jmv 11/17/2008 16:04'!
mouseMove: evt
	evt redButtonPressed ifFalse: [^ self].
	self handleInteraction: [
		editor mouseMove: evt index: (self characterIndexAtPoint: evt cursorPoint) ] fromEvent: evt! !

!EntryField2LW methodsFor: 'editing' stamp: 'jmv 11/17/2008 16:04'!
handleInteraction: interactionBlock fromEvent: evt 
	"Perform the changes in interactionBlock, noting any change in selection
	and possibly a change in the size of the paragraph (ar 9/22/2001 - added for TextPrintIts)"

	"Also couple the editor to Morphic keyboard events"

	| oldEditor oldContents |
	self editor sensor: (OldKeyboardBuffer new startingEvent: evt).
	oldEditor := editor.
	oldContents := contents.
	interactionBlock value.
	oldContents == contents 
		ifTrue: 
			["this will not work if the paragraph changed"

			editor := oldEditor	"since it may have been changed while in block"].
	self changed! !

