bugGNU Octave - Bugs: bug #45943, commands shg and figure(N) (where...


bug #45943: commands shg and figure(N) (where figure N exists) fail to bring figure window to foreground

Submitted by:  Tasos Papastylianou <tpapastylianou>
Submitted on:  Sat 12 Sep 2015 04:49:15 PM UTC  
Category:  Plotting Severity:  1 - Wish
Priority:  3 - Low Item Group:  Feature Request
Status:  Fixed Assigned to:  None
Originator Name:  Tasos Papastylianou <tpapastylianou> Open/Closed:  Closed
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Wed 15 Aug 2018 09:22:53 PM UTC, comment #13: 

I checked in an addendum cset here (https://hg.savannah.gnu.org/hgweb/octave/rev/b785394f10d0) that doesn't create a new figure if shg is called and no figure exists.

I don't think shg should call figure (gcf).  There is a lot of option processing that goes on in figure.m just to arrive at calling _show_figure_.  However, we could make shg a C++ function if you like.  I don't think it is too important.

I should note that this works perfectly when running the GUI.  When running the CLI, in say a terminal window, then the Qt plot windows are not raised above the terminal window.  I do get an indication down in the desktop bar that one of the windows has had a status change.  I think that is probably okay.  According to the Qt documentation

On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

That's what I get in Linux if I use the CLI.

I'm going to mark this as fixed.

Rik <rik5>
Project Administrator
Wed 15 Aug 2018 07:37:42 PM UTC, comment #12: 

I pushed the following changeset:


It seems to work for me.  We may want to reorganize this a bit.  Maybe shg should replace _show_figure_ as a built-in function that takes an optional argument?  Or, maybe shg should just call figure (gcf ())?  I'm not sure what organization is best here.

John W. Eaton <jwe>
Project Administrator
Sun 27 Aug 2017 06:28:46 PM UTC, comment #11: 

In Qt, the QWidget methods "activateWindow" [1] and "raise" [2] look promising.
Unfortunately, my proficiency in Qt in general and its implementation in Octave is very limited.

[1] http://doc.qt.io/qt-4.8/qwidget.html#activateWindow
[2] http://doc.qt.io/qt-4.8/qwidget.html#raise

Markus M├╝tzel <mmuetzel>
Project Member
Wed 02 Dec 2015 12:25:14 PM UTC, comment #10: 

Apologies for re-posting. The verbatim section got eaten up on the website. Second attempt:

Suggested OS-dependent workaround for shg (and figure).

% in file shg.m
function shg ()

  if (nargin != 0)
    warning ("shg: ignoring extra arguments");

  [status, output] = system ('which xdotool');
  if status
    warning ('You don''t seem to have ''xdotool'' installed on your system.
Please install it via your distribution''s package manager in order to raise
figure windows effectively');
    tmpName = tempname;
    oldName = get (gcf, 'name');
    set (gcf, 'name', tmpName);
    if system (['xdotool search --name ' tmpName ' windowraise']);
      warning ('Could not raise figure window');


shg could then also be appended at the end of the 'figure' command definition
to ensure appropriately raising the window whenever an existing figure is

1) millisecond flash of the temporary title name before switching back to the
old one.
2) Only works on linux (possibly mac?). If someone knows of a similar tool on
windows then this is a decent workaround until a proper Qt/C++ fix takes

Note: 'windowraise', as opposed to 'windowactivate', maintains the focus on
whatever window was active prior to issuing the command. This is the intended
behaviour since shg is usually a terminal-issued command, typed to raise the
window but the user then continues typing in the terminal. 'windowactivate'
would lose the focus from the console and the user would have to pick up the
mouse and click back on the console again (which makes typing shg redundant in
this context).

Tasos Papastylianou <tpapastylianou>
Wed 02 Dec 2015 12:15:10 PM UTC, comment #9: 

Suggested OS-dependent workaround for shg (and figure).

Tasos Papastylianou <tpapastylianou>
Sun 13 Sep 2015 09:35:36 PM UTC, comment #8: 

This issue has been known about for a very long time.  The problem is that different window managers use different procedures for bringing a window to the top of the stack.  Octave has traditionally had a lot of potential backends such as gnuplot, FLTK, and Qt.

Now that Octave is fairly well committed to Qt/OpenGL someone could troll through the Qt API and see if there is a function call to make a window visible.  The next step would probably be to write a C++ function to call the Qt function, and this C++ function would be called by shg.m

Rik <rik5>
Project Administrator
Sun 13 Sep 2015 12:15:47 AM UTC, comment #7: 

on line 94 of figure.m:

## When switching to figure N, make figure visible and on top of stack

This implies the intended behaviour should be the same as matlab.

Update: setting visible off and on again as a workaround does not work within scripts / functions (unless, e.g. a small pause is added in between).

Tasos Papastylianou <tpapastylianou>
Sat 12 Sep 2015 11:53:03 PM UTC, comment #6: 

Yes, sorry, I wasn't too explicit about OS, but the behaviour is the same in both GNU/Linux and Windows.

Also, apologies, I just realised my original submission is missing content (did I forget to close the verbatim tags??)

The second (and main) part of the bug description was about the behaviour of figure(N); shg is more of a consequence of this.

Mike, you're right, I suppose the octave help doesn't explicitly state raising the window above all others as matlab does:

figure(H) makes H the current figure, forces it to become visible,
    and raises it above all other figures on the screen.

I suppose a workaround if this is wanted behaviour is to add


which has the desired result.

Tasos Papastylianou <tpapastylianou>
Sat 12 Sep 2015 11:43:51 PM UTC, comment #5: 

To me, "made visible" clearly means that the "visible" property is reset to true. This is not the same as bringing it to the foreground.

Mike Miller <mtmiller>
Project Administrator
Sat 12 Sep 2015 11:40:55 PM UTC, comment #4: 

Got it. Yes, the figure() function does not bring the selected figure to the foreground, and Matlab's help description says it should.

This may also depend heavily on the active window manager for Linux desktops.

Mike Miller <mtmiller>
Project Administrator
Sat 12 Sep 2015 11:39:39 PM UTC, comment #3: 

Also, small comment on whether this is a bug vs a feature request; the documentation for the figure function in octave reads as follows:

If called with an integer N, and no such numbered figure exists,
then a new figure with the specified number is created. If the
figure already exists then *it is made visible* and becomes the
current figure for plotting.

(emphasis mine)

So, according to the documentation the intended behaviour of the function when called on existing figures is to make it visible, as well as make it the current figure for plotting.
I would say this makes this a bug rather than a feature request.

Tasos Papastylianou <tpapastylianou>
Sat 12 Sep 2015 11:34:23 PM UTC, comment #2: 

Drawnow does not bring a plot window to the foreground in Matlab; it just silently "flushes" graphical output in the background; I believe it does so on all existing figure windows rather than just the current one, but I may be wrong.

Matlab's shg implementation is simply


which would make sense to replicate on octave.

It might seem like a silly command to have around, but it's very useful to append to commands when lecturing live on Windows, where the window manager doesn't have a 'keep window on top' option. Nick Trefethen at Oxford seems to be particularly in love with this little command in his lectures :p

So the main bug here (if it could be considered a bug) is the fact that the figure command does not bring a figure window in the foreground as expected.

Tasos Papastylianou <tpapastylianou>
Sat 12 Sep 2015 05:26:18 PM UTC, comment #1: 

Confirmed. Some thought should be put into how this will be implemented if it is pursued. Does drawnow also bring the plot window to the foreground in Matlab? As "help shg" shows, it currently just calls drawnow.

Mike Miller <mtmiller>
Project Administrator
Sat 12 Sep 2015 04:49:15 PM UTC, original submission:  

Consider this code:

% user clicks on console, thereby burying figure window under Octave GUI / CLI.

In matlab, this would bring the figure window back on the foreground. In octave this does not happen.


Tasos Papastylianou <tpapastylianou>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by tpapastylianou (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-08-15 rik5 StatusReady For Test => Fixed
        Open/ClosedOpen => Closed
    2018-08-15 jwe StatusConfirmed => Ready For Test
    2017-08-27 mtmiller Dependencies- => bugs #51866 is dependent
    2015-09-12 mtmiller Severity3 - Normal => 1 - Wish
        Priority5 - Normal => 3 - Low
        Item GroupIncorrect Result => Feature Request
        StatusNone => Confirmed
        Release4.0.0 => dev

    Back to the top

    Powered by Savane 3.5