My software package XMI-MSIM relies on GtkExtra for the visualization of the simulated X-ray fluorescence spectra.
This library contains a number of useful widgets, such as a spreadsheet widget and a number of plotting tools, for your Gtk2 based GUI’s.
Recently, I have become a minor contributor of this project, and I have been particularly active in improving the buildsystem and also generating RPM files for a number of Fedora/RHEL based distros, and DEB files for several recent releases of Ubuntu and Debian. Full instructions on how to obtain access to these packages, which are hosted by my old research group, can be found here.
In todays blog post I am presenting an example of how one can relatively easily add printing and exporting capabilities to GtkExtra’s GtkPlotCanvas widget. I will demonstrate this with a short program that I have written that contains a plot of a sine function, and two buttons that allow the user to invoke the dialog windows that will set up the exporting and the printing of the canvas, respectively.
Generating the interface
Start off with a GtkWindow that contains a GtkVBox, which in turn is packed with two widgets: a GtkHBox that contains three buttons (Print, Save As and Quit), and the GtkPlotCanvas. The code itself is pretty straightforward Gtk+, except for the bit that deals with the GtkPlotCanvas. This is a perhaps a good time to mention that I am in no way a GtkExtra expert (or even Gtk+ for that matter…), and that the following code is the result of my studying of the GtkExtra examples, and the (unfortunately very limited) documentation. Anyway, I’ll try and clarify as much as possible…
Set up the canvas
Define a canvas widget that will be used to draw the plot later and give it dimensions that correspond to those of the A4 paper size standard. Other sizes are also available (letter, executive and legal), and are defined in gtkplotpc.h.The last parameter of the gtk_plot_canvas_new function is the magnification, which acts a scale factor for the dimensions of the canvas, and will therefore determine the size of the widget in the application. However, it has no impact on the size of the print or the size of the exported image.
Next, I disable the user-interaction with the canvas and its items with the call to GTK_PLOT_CANVAS_UNSET_FLAGS, and I define the GdkColors that will be used in the plot and canvas. The last call sets explicitly the background color of the canvas to white.
Add a plot to the window…
The following calls will generate the plot and set its properties.
Most functionnames should be pretty selfexplanatory, especially when combined with the GtkExtra documentation.
I am not entirely confident about gtk_plot_new_with_size though: at least in this example the two last arguments (width and height), appear to have no influence on the result.
Anyway, after constructing the plot to your liking, it must be added to the GtkPlotCanvas that was defined in the previous section.
This is accomplished with the calls to gtk_plot_canvas_plot_new and gtk_plot_canvas_put_child.
…and finish off with the data
So far, we have defined the GtkPlotCanvas and the GtkPlot. The latter currently consists of an empty grid. I made things a bit more interesting by adding a graph of a sine function. The following lines calculate 1000 values of the sine function within the interval [0, 4π], and add them subsequently to a GtkPlotData variable, that in turn is added to the GtkPlot with gtk_plot_add_data
As with the grid in the previous section, it is possible to change the properties of the graph using a few appropriately named functions.
This part of the code finishes by drawing and showing all widgets. I am doing this here with several calls, some of which are definitely unnecessary, but the documentation and examples of GtkExtra are pretty unclear on how to do this (especially when updating, erasing canvases, plots etc). Anyway, calling all of them seems to work just fine :-)
and running gtkextra-print-export, you should get something like:
Printing the canvas
Both the printing and exporting of the canvas rely on Cairo. The most important function here is gtk_plot_canvas_export_cairo, which accepts a cairo context as argument and will ‘paint’ the canvas on it. When printing, this cairo context is produced by gtk_print_context_get_cairo_context, as is shown in the following code snippet. It contains both the ‘clicked’ callback for the Print button, as well as the ‘draw-page’ callback for the GtkPrintOperation.
Exporting the canvas to a file
In this example, I have created the possibility to export to Encapsulated PostScript, PDF and PNG. Cairo also offers SVG support, which may be useful to some. Other fileformats like JPEG may be supported through devIL…
The function in this example is a callback of the Save As button, and pops up a GtkFileChooserDialog, with three GtkFileFilters, each corresponding to one of the supported fileformats. After selecting a filter and choosing the filename, a cairo surface is created for the selected filetype. Next, a cairo context is created for the surface, which will be used to paint the canvas on using gtk_plot_canvas_export_cairo. Multipage formats such as PostScript and PDF require the extra call to cairo_show_page. The function terminates after destroying the cairo surfaces and contexts.
This is the full code: feel free to hack away at it.
It is taken from xmimsim-gui-results.c, which contains a lot more GtkExtra related code: drawing lines, changing graph properties, hiding data etc.
Keep in mind that up to today, GtkExtra is still only compatible with Gtk2. However, the developers are actively working on providing Gtk3 support as well. Any help in accomplishing this transition would be greatly appreciated!