11 Replies Latest reply on Apr 14, 2016 4:12 AM by Ch. Efstathios

    How can I get the current paragraph?

    Ch. Efstathios Level 1

      I want to get the current paragraph where the insertion point is.

       

      How can I do this in FDK?

        • 1. Re: How can I get the current paragraph?
          Mike-Hardy Level 3

          Read the section Getting and setting the insertion point or text selection in the FDK Programmer’s Guide.

           

          The code below is from memory, but should give you the idea of what is required. You should add error checking, and test it.

           

          F_ObjHandleT activeDoc;
          F_TextRangeT textRange;
          activeDoc = F_ApiGetId(0, FV_SessionId, FP_ActiveDoc);
          textRange = F_ApiGetTextRange(FV_SessionId, activeDoc, FP_TextSelection);
          /*
          * textRange.beg.objId is the ID of the paragraph or text line at the start of the current selection
          * textRange.end.objId is the ID of the paragraph or text line at the end of the current selection
          */
          if (F_ApiGetObjectType(activeDoc, textRange.beg.objId) == FO_Pgf) {
               // we have a paragraph
          }
          else {
               // we have a text line
          }
          
          
          • 2. Re: How can I get the current paragraph?
            Ch. Efstathios Level 1

            I have done the following solution:

             

            Do you believe that we can do it better or fix any bug in it?

             

            bool FMClient::GetCurrentParagraphWithInsertionPoint(ParagraphWithPointT & paragraphWithPoint)
                {
                    F_ObjHandleT docId = F_ApiGetId(FV_SessionId, FV_SessionId, FP_ActiveDoc);
            
                    if (!docId)
                    {
                        return false;
                    }
            
                    F_TextRangeT insertionPoint = F_ApiGetTextRange(FV_SessionId, docId, FP_TextSelection);
            
                    if (! insertionPoint.beg.objId)
                    {
                        return false;
                    }
            
                    if (insertionPoint.beg.objId != insertionPoint.end.objId)
                    {
                        return false;
                    }
            
                    if (insertionPoint.beg.offset != insertionPoint.end.offset)
                    {
                        return false;
                    }
            
                    CExpandingString paragraph;
            
                    F_TextItemsT textItems = F_ApiGetText(docId, insertionPoint.beg.objId, FTI_String);
            
                    for (UIntT i = 0; i < textItems.len; i++)
                    {
                        paragraph.Append(textItems.val[i].u.sdata);
                    }
            
                    F_ApiDeallocateTextItems(&textItems);
            
                    paragraphWithPoint.paragraph = paragraph.TakeContents();
            
                    paragraphWithPoint.point = insertionPoint.beg.offset;
            
                    return true;
                }
            

             

            I get from the active document the selection point (in case there is no selection point or the selection point is between different objects or the begin and end offsets are different we return false).

             

            After that, I get all text items with string from the object of the selection and append all text to a dynamic allocated string.

             

            Lastly, I return the paragraph as concatenated dynamic string and also the point where the cursor where inside this paragraph.

             

            Do you believe we can do it better?

             

            Also if you could help me about this problem it would be great: https://forums.adobe.com/thread/2138451

            • 3. Re: How can I get the current paragraph?
              Ch. Efstathios Level 1

              Currently in my solution I do not use the FO_Pgf. Is this a problem?

              • 4. Re: How can I get the current paragraph?
                Ch. Efstathios Level 1

                In your code: if (F_ApiGetObjectType(activeDoc, textRange.beg.objId) == FO_Pgf) {      // we have a paragraph  }  else {      // we have a text line  } Is it necessary to check for FO_Pgf? What is the different of a paragraph and text line? In my situation I just get all text lines from the object where the insertion point is and most of the time all these text items are form a paragraph (I am not sure)...

                • 5. Re: How can I get the current paragraph?
                  Russ Ward Level 4

                  Ch,

                   

                  I can answer some of these questions but if you decided that this thread is answered, please give the credit to Mike.

                   

                  Regarding the check for an FO_Pgf object, you should not need that. I think there is some obscure case where F_ApiGetTextRange() gives you an object that is not a paragraph... I forget exactly but I think it has to do with a text line graphic object or something like that. I could be wrong. In any case, if you have a normal text flow with an insertion point, you will get a paragraph object.

                   

                  This line will abort the operation if any text is selected. Do you really want that?

                  if (insertionPoint.beg.objId != insertionPoint.end.objId) 



                  This line will abort the operation if multiple paragraphs are selected. Do you really want that? Without this check, you would still get the text of the first selected paragraph.

                  if (insertionPoint.beg.offset != insertionPoint.end.offset)


                  Otherwise, the code looks OK to me, at least as far as the FDK calls. Are you having problems with it?


                  Russ

                  • 6. Re: How can I get the current paragraph?
                    Ch. Efstathios Level 1

                    Thanks for the answer!

                    How do I give the credit to Mike? By accepting his answer as correct?

                    The following code:

                      if (insertionPoint.beg.objId != insertionPoint.end.objId

                    is for getting the paragraph only and only when the user has not selected any text. If the user selected any text these two values are different and I don't want it because I want to know exactly in which place the cursor is. So when these two values are identicaly I know the exact place inside the paragraph where cursor is (zero based index in the string).

                     

                    The following code: 

                    if (insertionPoint.beg.objId != insertionPoint.end.objId)

                     

                    is for getting only the current paragraph and not multiple paragraphs I want to get the current only paragraph where the cursor blinks without any text selection from the user. The first check is because I want to get the index where the insertion point inside the text of the paragraph.

                     

                    As far as " I think there is some obscure case where F_ApiGetTextRange() gives you an object that is not a paragraph... I forget exactly but I think it has to do with a text line graphic object or something like that." is concerned I think that I have no problem since I try to extract all text from the object and If there are no text items with FTI_String I get back an empty string. This is not might a problem for me. So, we might leave it that way.

                    • 7. Re: How can I get the current paragraph?
                      Mike-Hardy Level 3

                      Nothing to add to the excellent response from Russ, except just to confirm that if the underlying object is not an FO_Pgf, it is a text line in a graphic, instered using the text tool.

                      • 8. Re: How can I get the current paragraph?
                        Ch. Efstathios Level 1

                        In such a case if the user clicks in the text line in the graphic the method will get the text right?

                         

                        This is not a bad case for me. Since I want to get also any text in a graphic. I would like to get also the text of images.

                         

                        This will work with graphics containing text lines if we leave it as is?

                        • 9. Re: How can I get the current paragraph?
                          Ch. Efstathios Level 1

                          It works successfully also with text lines in graphics. I have tested it. Thanks. We leave it as is.

                          • 10. Re: How can I get the current paragraph?
                            Ch. Efstathios Level 1

                            Can you please help me with another problem that I am facing? Here it is: https://forums.adobe.com/message/8685352#8685352