Re: Plotting, text drawing, etc.

On Mon, 7 Feb 2005, Ben Johnson wrote:

> On Mon, Feb 07, 2005 at 07:28:12PM -0800, James Frye wrote:
> > Ali,
> > 
> > Thanks.  I'll have a look for future reference, but I figured out a quick
> > & dirty hack that does what I need.  I have the GTK menu program start
> > gnuplot as a pipe, then re-parent gnuplot's plot window into a GTK widget.
> > So I stuff gnuplot commands & data into the pipe, and the resulting plot
> > shows up where I want it.
> > 
> > Constructive laziness :-)
> That sounds like a perfect solution to me.  I'd been reading about the
> OLE-like Gnome stuff and how it's really so incredibly easy to embed
> features of one program in another.  I think this is extremely cool and
> I would love to read more about what you did, and see a brief example if
> you don't mind sharing.  :)  Thanks.


It's not really OLE, but XWindow reparenting, which was around long before
:-) I could put the whole thing somewhere if people are interested,
otherwise here's a quick outline.

You need a package named "gnuplot_i" (written by N.  Devillard).  It's a
few hundred lines of C code that handles all the details of running
gnuplot in a pipe, and encapsulates gnuplot commands for e.g.  an xy plot
into a function call.  I've improved on it a little, since I've used it
regularly for doing simple plots within a program.  but the original
should work.

Do the necessary gnuplot_i setup, giving it a unique title:

  sprintf (cmd, "-geometry 800x600 -title '%s%d'", argv [0], getpid ());
  h1 = gnuplot_init (cmd);

and use GTK to make your GUI.  Create your plotting area as a socket, set
its size, add it to the gui, and eventually call gtk_main ().

  sock = gtk_socket_new ();
  gtk_widget_set_size_request (GTK_WIDGET (sock), 600, 300);
  gtk_box_pack_start (GTK_BOX (main_vbox), sock, TRUE, TRUE, 0);

In your "redraw" function (the one that actually does plotting, create
your plot from data, and call one of the gnuplot_i functions to draw it.
This will pop up an X window.
Now you need a function that uses XQueryTree and XFetchName to scan the
window list for the gnuplot window (which will have the unique name you
created).  This returns an X Window ID.  Then you just call
gtk_socket_steal and the plot window gets sucked into the GUI app.  Note
that you do the search only once, and (at least on my system) have to sleep
for a bit before looking for the new window.  So the code looks like this:

  ...make plot...

  if (first)
    sleep (1);
    xid = GetXWinID (title);
  if (xid != 0)
    gtk_socket_steal (GTK_SOCKET (sock), xid);

  first = FALSE;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]