6 Replies Latest reply on Oct 30, 2009 5:09 AM by timo888

    FeatureRequest:  enhancement to ToolTipEvent: caretIndex

    timo888 Level 1

      Platform: Flex AIR

       

      Use Case:

      You have information you want to give the user about any|every individual word in the content of a TextArea (perhaps grammatical information, a translation into another language or languages, or a brief historical blurb, whatever).  Using TextArea for its CSS/HTML capabilities.

       

      One needs to know the caretIndex in order to display a custom tooltip for a particular word in the TextArea text.  Knowing the caretIndex, you can determine the word underneath it by scanning the TextArea.text property  looking for whitespace|punctuation in the vicinity of the caretIndex to pluck out the word.

       

      Could a future version of the ToolTipEvent expose the caretIndex?

       

      Thanks

        • 1. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
          Flex harUI Adobe Employee

          Unless selection spans several characters, caretIndex == selectionBeginINdex

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
            timo888 Level 1

            But caretIndex != selectionBeginIndex when the user doesn't click on the TextArea but merely hovers the mouse, right?

             

            Or you can select text and then hover elsewhere.

             

             

            P.S.

             

            I saw this post in the right margin ("MORE LIKE THIS") as I was replying:

            http://forums.adobe.com/message/879405#879405

             

            The author suggests this approach:

            var idx:int = TextField(ta.getChildAt(2)).caretIndex; //ta is the id of your TextArea

             

            Is it a documented (i.e. written in stone) feature that the TextArea has a child at offset 2?  Can this be depended on?

            • 3. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
              Flex harUI Adobe Employee

              The caret is a specific thing in selection.  You're talking about the I-beam cursor position.  I should've realized that since you were referencing ToolTipEvent.

               

              I doubt we'd add it to ToolTipEvent since ToolTips aren't always about text.

               

              I still think all you need to do subclass TextArea, get to the internal TextField and call getCharIndexAtPoint()

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              1 person found this helpful
              • 4. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
                timo888 Level 1

                Sorry about confusing the terms for the beam and the caret, my bad, not yours. Thanks for the suggestion: I didn't understand that in extending TextArea I'd gain access to an encapsulated TextField.   That should get me past one major hurdle.

                 

                The next hurdle is that the Flex Tooltips Manager requires the mouse to move outside the related display object before it will show another tooltip for the object, so this makes it essentially unsuitable for what I was trying to do. The user cannot simply move the mouse over one word, and then over another, and keep displaying tool tips one after another, word by word.

                 

                So I have to

                 

                (a) recreate the wheel  by writing timing code to see if the mouse is actually "hovering" over a word and not just moving over it in passing  (pretty hard, I expect, inasmuch as words have no focus boundaries)

                or

                (b) resort to a  click-driven or right-click-driven approach.

                 

                Option (b) wins.

                • 5. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
                  paul.williams Level 4

                  I had a go at (a). It probably needs some more work, but looks promising:

                   

                  package
                  {
                      import flash.events.MouseEvent;
                      import flash.utils.clearTimeout;
                      import flash.utils.setTimeout;
                     
                      import mx.controls.TextArea;
                      import mx.managers.ToolTipManager;

                   

                      public class TextAreaWithTips extends TextArea
                      {
                          private var hoverTimeout : uint;
                          private var hoverX : int = 0;
                          private var hoverY : int = 0;
                         
                          public function TextAreaWithTips()
                          {
                              super();
                             
                              addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
                              addEventListener( MouseEvent.ROLL_OUT, onRollOut );
                          }
                         
                          private function onMouseMove( event : MouseEvent ) : void
                          {
                              reset();
                                 
                              hoverX = event.localX;
                              hoverY = event.localY;
                             
                              hoverTimeout = setTimeout( showTip, ToolTipManager.showDelay );
                          }
                         
                          private function onRollOut( event : MouseEvent ) : void
                          {
                              reset();   
                          }
                         
                          private function reset() : void
                          {
                              clearTimeout( hoverTimeout );
                             
                              toolTip = "";
                          }
                         
                          private function showTip() : void
                          {
                              var index : int = textField.getCharIndexAtPoint( mouseX, mouseY );
                             
                              if ( index < 0 )
                              {
                                  return;   
                              }
                             
                              var wordStart : int = textField.text.lastIndexOf( " ", index );
                              var wordEnd : int = textField.text.indexOf( " ", index );
                             
                              var word : String = textField.text.substring( wordStart + 1, wordEnd );
                             
                              toolTip = "You are hovering over: " + word;
                             
                              ToolTipManager.currentTarget = this;   
                          }
                         
                      }
                  }

                  1 person found this helpful
                  • 6. Re: FeatureRequest:  enhancement to ToolTipEvent: caretIndex
                    timo888 Level 1

                    Cool, Paul.  That clearTimeOut utility is nifty   Works nicely!

                     

                    Here's where I had gotten on plucking the word out from beneath the cursor.  It doesn't do punctuation delimiters yet, or all kinds of whitespace, but it handles the firstWord|lastWord scenario where there's no space delimiting the word on one side.

                     

                    I have to look up what lastIndexOf does.

                     

                     

                    public function getWordUnderMouse():String {
                             var tlen:int = this.textField.text.length;
                             var pos:int = this.textField.getCharIndexAtPoint(this.mouseX, this.mouseY);


                               if  (pos > tlen || pos==-1){
                                    word="";
                                    return word;
                               }
                              
                                           
                                if (text.substr(pos,1)==SPACE) {       
                                    word="";
                                }else{
                                   
                                    var s:String = text.substr(pos,1);
                                   
                                    while ( (s != SPACE) && pos > 0  && pos < text.length)
                                        {
                                            pos -= 1;
                                            s = text.substr(pos,1);                       
                                           
                                        }
                                       
                                        var pos2:int = text.length;
                                        if (pos < text.length)
                                          pos2=text.indexOf(  SPACE, pos+1)
                                       
                                        if  (pos2 > 0) {
                                            word = text.substring(pos,pos2);
                                           
                                        } else {
                                            word = text.substring(pos, text.length);
                                        }
                                   
                                }
                               
                            return word;
                           
                            }