<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>I've been trying to learn how I can impose length and character restrictions on instances of <font face="monospace">TextModelMorph</font>. I've found a way to do it by using a subclass of <font face="monospace">TextModelMorph</font> and a subclass of <font face="monospace">InnerTextMorph</font>. These seem like common enough needs that perhaps they should be directly supported by those classes instead of requiring the use of new subclasses. I'm interested in feedback on this.</div><div><br></div><div>Here's an example of using my new class to create a single-line input that only allows digits and has a maximum size of 5 digits:</div><div><br></div><div><font face="monospace">entry := <font color="#ff0000">ValidatedTextModelMorph</font> withText: '' ::<br> acceptOnAny: true;<br> hideScrollBarsIndefinitely;<br> <font color="#ff0000">maxSize: 5;<br> charValidationBlock: [ :char | char isDigit ];</font><br> morphExtent: 100 @ 0;<br> wrapFlag: false.</font><br></div><div><font face="monospace">entry openInWorld.</font></div><div><br></div><div>Here is the <font face="monospace">ValidatedTextModelMorph</font> class. Note that it does very little, just delegating to <font face="monospace">scroller</font> which is an instance of the class returned by the <font face="monospace">innerMorphClass</font> method.</div><div><br></div><div><font face="monospace">TextModelMorph subclass: #ValidatedTextModelMorph<br> instanceVariableNames: ''<br> classVariableNames: ''<br> poolDictionaries: ''<br> category: 'Demo'<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">charValidationBlock: aBlock</font></div><div><font face="monospace"> scroller charValidationBlock: aBlock<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">innerMorphClass</font></div><div><font face="monospace"> "This method wouldn't be needed if InnerTextMorph was</font></div><div><font face="monospace"> modified to do what ValidatedInnerTextMorph does."<br> ^ ValidatedInnerTextMorph<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">maxSize: anInteger<br> scroller maxSize: anInteger</font><br></div><div><br></div><div>Here is the <font face="monospace">ValidatedInnerTextMorph</font> class:</div><div><br></div><div><font face="monospace">InnerTextMorph subclass: #ValidatedInnerTextMorph<br> instanceVariableNames: 'charValidationBlock maxSize'<br> classVariableNames: ''<br> poolDictionaries: ''<br> category: 'Demo'<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">charValidationBlock: aBlock<br> charValidationBlock := aBlock<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">maxSize: anInteger<br> maxSize := anInteger<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">keyStroke: aKeyboardEvent</font></div><div><font face="monospace"> "This is where all the magic happens!"<br> | allow |<br> <br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">allow := aKeyboardEvent keyValue < 32. "includes arrow keys and backspace"<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">allow ifFalse: [ "not a control character"<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">allow := maxSize = nil or:</span></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">[ maxSize = 0 or:</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">[ self editor selectionInterval size > 0 or:</span></div><div><span style="font-family:monospace"> "A new character always fits if characters are selected."</span></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">[ model actualContents size < maxSize ]]].</span></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><font face="monospace">allow ifTrue: [ "will fit"</font></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"><span class="gmail-Apple-converted-space"> </span></span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">charValidationBlock ifNotNil: [</span></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">allow := charValidationBlock value: aKeyboardEvent keyCharacter</span></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">]</span></div><div><span style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><font face="monospace">].</font></div><div><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">].<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><font face="monospace">allow<br></font><span style="font-family:monospace"> <span class="gmail-Apple-converted-space"> </span></span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">ifTrue: [ super keyStroke: aKeyboardEvent ]</span></div><div><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace"> </span><span class="gmail-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">ifFalse: [ Smalltalk beep ].</span></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8000001907349px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div></div>
</div>
</div>