Duplicate graph in a function

I have two (related) questions.

1. I would like to duplicate a graph using code, i.e. the equivalent of the menu item Edit/Duplicate Graph. The forum archive has a post (https://www.wavemetrics.com/code-snippet/clone-window) from 2010 with two functions by RGerkin that I cannot get to work because they include GetWinCoords, which I assume is a custom function. Before I write my own function to get window coordinates, is there a more direct command or function to duplicate a graph programmatically now?

(I thought of creating the graph's macro using the doWindow /R command, but the manual says "However, /R does nothing if a macro or function is running.")

2. The general task I am programming is to plot a long time series as 5 very wide graphs arranged in a vertical column on a layout. Each graph would show 1/5 of the full time series. If the time series' entire range is from x0 to x1:

- the top graph's range should be (x0, (x1-x0)/5)

- the second graph's range should be ((x1-x0)/5), 2*(x1-x0)/5)

...and so on. The approach I am taking is to start from a plot that has the entire range; duplicate it; set the range to the sub-range appropriate for that graph.

If anyone wants to suggest a better way, feel free.

Pietro

WinRecreation(winStr, options) will put the recreation macro into a string.

edit note: not WinReaction 

and then you can execute that recreation macro using Execute from your code.

Note that if the graph is a subwindow, you will have to go through some gyrations if want just the gaph.

Sadly, DoIgorMenu "Edit", "Duplicate Graph" doesn't seem to be allowed :(

In reply to by ilavsky

Aha!

Then we have

function /S duplicateGraph(string win)
    win = SelectString(strlen(win)==0, win, WinName(0, 1))
    if (strlen(win) == 0)
        return ""
    endif
    DoWindow /F $win
    if (V_flag == 0)
        return ""
    endif
    DoIgorMenu "Edit", "Duplicate"
    return WinName(0, 1)
end

 

DoIgorMenu solves some nasty problems, but can create others...

And I'm pretty sure this is equivalent, explicitly using code that Edit->Duplicate uses internally:

Function dupgraph(string gname)
    gname = SelectString(strlen(gname)==0, gname, WinName(0, 1))
    if (strlen(gname) == 0)
        return nan
    endif
    String recstr = WinRecreation(gname, 0)
    Execute recstr
end

The new graph window winds up on top because the Display command in recstr just does that.