Coyote's Guide to IDL Programming

Open IDL Graphics Windows

QUESTION: How can I tell if a graphics window is still open and available before I try to write graphics into it?

ANSWER: On Windows and UNIX machines you can use the WINDOW_STATE keyword to the DEVICE command. It will return an array containing one element for each possible window. Array element j contains a 1 if window j is open, otherwise it contains a 0.

   IDL> Device, Window_State=theseWindows
   IDL> Print, 'Open Windows: ', Where(theseWindows EQ 1)

You can download the function WindowAvailable from the Coyote Library. Passed a graphics window index number, it will tell you if the window is currently open and available (returns a 1), or not (returns a 0). This function allows you to write code like this:

   IF WindowAvailable(1) THEN BEGIN
      Plot, data
      Window, 1, Title='Plot Window'
      Plot, data

For a more general solution, you can use an unmanaged draw widget as the graphics window. Here is a small program that creates an unmanaged draw widget for you to write into. The function returns the graphics window index number (which you use with the WSet command to make the window the current graphics window). The parameter drawID is an output parameter that contains the identifier of the draw widget. This is what you will need to know to determine if the graphics window is still open.

   FUNCTION Graphics_Window, drawID, XSize=xsize, YSize=ysize
   IF N_Elements(xsize) EQ 0 THEN xsize = 400
   IF N_Elements(ysize) EQ 0 THEN ysize = 400
   tlb = Widget_Base()
   drawID = Widget_Draw(tlb, XSize=xsize, YSize=ysize)
   Widget_Control, tlb, /Realize
   Widget_Control, drawID, Get_Value=windowIndex
   RETURN, windowIndex

To create a 400 by 600 window and draw a plot into it, you would type this:

   wid = Graphics_Window(thisWindow, XSize=400, YSize=600)
   WSet, wid
   Plot, data

Later, to test if this is still a good and valid window, you type this:

   IF Widget_Info(thisWindow, /Valid_ID) THEN Plot, data

You could automate this process with a small utility routine:

   FUNCTION GoodWindow, identifier
   IF N_Elements(identifier) EQ 0 THEN RETURN, 0
   RETURN, Widget_Info(identifier, /Valid_ID)

Then your code to check if the window is still open looks like this:

   IF GoodWindow(thisWindow) THEN Plot, data

[Return to IDL Programming Tips]