Fanning Software Consulting

Using Convert_Coord with cgWindow

Facebook Twitter RSS

QUESTION: I have a simple program. I want to create a plot, then convert a data point to device coordinates with Convert_Coord and place it on the plot. I can do this easily with Coyote Graphics like this.

   cgPlot, cgDemoData(1)
   d = Convert_Coord(30, 10, /Data, /To_Device)
   cgPlotS, d[0], d[1], /Device, PSym=2, SymColor='red'

This code works perfectly. But, if I want to use a resizeable graphics window, I use this code.

   cgPlot, cgDemoData(1), /Window
   d = Convert_Coord(30, 10, /Data, /To_Device)
   cgPlotS, d[0], d[1], /Device, PSym=2, SymColor='red', /AddCmd

When I do this, Convert_Coord throws the following error.

   % CONVERT_COORD: Window is closed and unavailable.

Why is it doing this?

ANSWER: For Convert_Coord to convert a point in data coordinates to device coordinates, there must be an open graphics window. But, it takes more than this. This open graphics window must be identified by the !D.Window system variable. If no graphics windows are open, this variable is set to the value -1. If it is set to -1 when Convert_Coord is called, then Convert_Coord issues the error message you see above.

If you examined !D.Window just prior to the call to Convert_Coord, you would find that it is set to 0 in the first example above, and to -1 in the second example. The second example is strange because there is clearly a graphics window on the display. Namely, cgWindow. So, the real question is, why is !D.Window set to -1 in this situation?

It is set to -1 because cgWindow goes to a great deal of effort to "disguise" itself from the outside world. This is necessary to avoid the problem of people accidentally drawing in the window and then getting totally annoyed when what they see in the window doesn't resize itself. So, basically, you can't just draw into a cgWindow. You have to make the graphics window in cgWindow the current graphics window, draw into it, and then switch the current graphics window (!D.Window) back to whatever window was current before you switched it to the cgWindow graphics window. It is the switching back that is setting !D.Window to -1.

You can solve this problem by making sure the cgWindow graphics window is the current graphics window. All you need to do is add this line of code directly in front of your Convert_Coord command:

   WSet, cgQuery(/Current)

This will make sure there is a current graphics window for Convert_Coord to use.

Version of IDL used to prepare this article: IDL 7.1.2.

Written: 7 March 2012