Skip to main content
K.Daube
Community Expert
Community Expert
November 30, 2016
Answered

Display on/off quirks

  • November 30, 2016
  • 1 reply
  • 435 views

Dear experts

(FM-13.0.5.547) I have discovered a strange behaviour:
In the following order the location is not restored after the actions. It seems that SetDisplay destroys it:

  SetDisplay (false);
  savedTR = goCurrentDoc.TextSelection;
// Do a lot of things (loop) on various places in the document
  RestoreTR (goCurrentDoc, savedTR);
  SetDisplay (true);


In the following order things work as expected. Cursor location is restored.

  savedTR = goCurrentDoc.TextSelection;
  SetDisplay (false);
// Do a lot of things (loop) on various places in the document
  SetDisplay (true);
  RestoreTR (goCurrentDoc, savedTR);

The two functions are

function RestoreTR (oTargetDoc, oSavedTR) {
// actions may have left us on a reference page
var bodyPage = oTargetDoc.FirstBodyPageInDoc; 
  oTargetDoc.CurrentPage = bodyPage; 
  oTargetDoc.TextSelection = oSavedTR; 
  oTargetDoc.ScrollToText(oSavedTR); 
} //--- end RestoreTR

function SetDisplay (bWanted) {
  if (bWanted) {                // switch ON
    if (app.Displaying === 0) {
      app.Displaying = 1;
    }
  } else {                      // switch OFF
    if (app.Displaying === 1) {
      app.Displaying = 0;
    }
  }
} //--- end SetDisplay

What may be the cause of this?

This topic has been closed for replies.
Correct answer frameexpert

Hi Klaus, I am not sure why this doesn't always work, but it may be that you don't always have a valid TextSelection when the script starts. Also, when you restore, you are assuming that you started on a body page. It may be better to have a "save" function that will be more specific about where you start. For example:

function saveCurrentLocation (doc) {

    var location = {};

    location.pgf = doc.TextSelection.beg.obj;

    location.offset = doc.TextSelection.beg.offset;

    location.page = doc.CurrentPage;

    return location;

}

Now your restore function may be more reliable:

function restoreLocation (doc, location) {

    var textRange, pgf;

    doc.CurrentPage = location.page;

    pgf = location.pgf;

    if (pgf.ObjectValid () === 1) {

        textRange = new TextRange (new TextLoc (pgf, location.offset),

            new TextLoc (pgf, location.offset));

        doc.TextSelection = textRange;

    }

}

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
November 30, 2016

Hi Klaus, I am not sure why this doesn't always work, but it may be that you don't always have a valid TextSelection when the script starts. Also, when you restore, you are assuming that you started on a body page. It may be better to have a "save" function that will be more specific about where you start. For example:

function saveCurrentLocation (doc) {

    var location = {};

    location.pgf = doc.TextSelection.beg.obj;

    location.offset = doc.TextSelection.beg.offset;

    location.page = doc.CurrentPage;

    return location;

}

Now your restore function may be more reliable:

function restoreLocation (doc, location) {

    var textRange, pgf;

    doc.CurrentPage = location.page;

    pgf = location.pgf;

    if (pgf.ObjectValid () === 1) {

        textRange = new TextRange (new TextLoc (pgf, location.offset),

            new TextLoc (pgf, location.offset));

        doc.TextSelection = textRange;

    }

}

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
December 1, 2016

Thank you, Rick

Your ideas always make the functions more universal!

The only missing thing was

  doc.ScrollToText(textRange);  

before leaving restoreLocation

Klaus