Sorry this is so long but it was a little complicated to explain, despite my 
trying to keep it simple.

I have an HBox subclass that contains two TextAreas.  The TA on the left is for 
displaying line numbers.  The TA on the right is for entering text.  As you add 
more text, I generate more line numbers, just like a typical editor.

The two TAs are linked so that when you scroll the text, the line numbers 
scroll too.

   BindingUtils.bindProperty( _numsTA, 'verticalScrollPosition', _textTA, 
'verticalScrollPosition' );

This works fine under normal circumstances.  However, if you select/copy a 
large chunk of text from an external source and then paste it into the TA, I 
have to generate more line numbers but the new numbers are not displayed until 
I click in the TA again.

When I stopped in the debugger in Event.CHANGE, I confirmed that 
_numsTA.verticalScrollPosition is the same as _textTA.verticalScrollPosition 
(and the number is indeed where it should be scrolled to).  But when the 
display is updated, the text portion is scrolled properly (the cursor is at the 
end of the pasted section) but the line numbers are not scrolled.  If I just 
click in the text, the numbers scroll to the right place.

In general, as you click around in the input area, or if you use the arrow keys 
to move up or down, I update the line number area so that the current line is 
highlighted.  (I'm just using htmlText for the line numbers and putting a color 
for the current line and leaving all the others black.)

I have handlers for 

   _textTA.addEventListener( KeyboardEvent.KEY_DOWN, keyDownH );
   _textTA.addEventListener( MouseEvent.CLICK, mouseClickH );

It gets a little complicated for the arrow keys.  To figure out where the 
cursor is, I use the internal UITextField of the TextArea and query the 
caretIndex property (which is inherited from flash.text.TextField).  I then use 
the UITextField's getLineIndexOfChar() method to get the line number.  The 
messy part is that in the KEY_DOWN handler, the caretIndex hasn't been updated 
yet.  It's updated *after* KEY_DOWN returns back to flash.text.  (I can't look 
at the source code for flash.text so I'm not sure where/when exactly it's 
updated.)  That means I can't really update the line number color until 
KEY_DOWN returns.  I do that by using callLater() to schedule my update.

(You might think that the line number color should be updated in KEY_UP, that 
way caretIndex would have the proper value.  You could do that but if you hold 
the arrow key down, then you wouldn't get color updates as the arrow is moving 
until you let the arrow key up.)

The updating of the current line number color and the pasting of text and the 
scrolling of the line numbers are all linked together and it's probably a 
timing issue.

When you paste (which, btw, if very hard to tell happend because while there's 
an Event.PASTE, it's not dispatched for the TextArea object, as documented - go 
figure), I do some processing in my Event.CHANGE and then call callLater() to 
schedule my line number color update.

In the code that updates the current line number, it checks if it needs more 
numbers and generates them.  In this particular case, verticalScrollPosition is 
set correctly (say, 500), even though I only have 250 lines in the number area. 
 I then generate more line numbers (more than 500) and when the update returns 
(ie, when we return from the callLater()), verticalScrollPosition is correct 
and the number of lines is correct.

The display then updates with the newly pasted text and the input area is 
scrolled properly but the numbers area is not.  A simple click in the input 
area causes the line numbers to display correctly.

It's as if the flash player knows that line number area has a 
verticalScrollPosition of 500 but thinks it only has 250 lines so it adjusts 
the verticalScrollPosition.  Is there a way I can force it to update correctly? 
 I tried adding validateNow() (and other validate* type methods) to my number 
updater (callLater), to no avail.

I also tried setting verticalScrollPosition directly in my updater, also to no 
avail.

Reply via email to