All My Brain Where stuff from my brain lands

May 22, 2008

GTK+ programs with GtkBuilder and dynamic signal handlers.

Filed under: Programming — Tags: , , , , , , , — Dennis @ 1:31 pm

Well, I decided to review some GTK+ and Gnome development lately. With GTK+, a nice way to create a user interface is with the Glade Interface Designer. Glade produces an xml file with a glade-interface element that can be loaded by libglade. You can then change attributes of the user interface without having to recompile your program. You just edit the design xml file and you’re good to go. Here are a couple things I noted during the process that weren’t too clear from the documentation:

  1. You have to work with C, not C++, if you want to dynamically load the signal handlers. Many times, it is convenient to write a program mostly in C, but use the C++ compiler and take advantage of a few of the niceties in C++. You can’t do that in this case. According to the libglade documentation, you’ll need to add -export-dynamic flag so that the dynamic loader for the signal handlers can locate the functions in the executable with the gmodule library. That doesn’t work with a C++ compiled executable. You’ll have to stick with C. If you really want to use GTK+ with C++, you can always try the glademm bindings instead of the default C bindings. You could also use gtkmm and write the entire program w/ C++ instead.

    UPDATE: I think you could get around this by using extern “C” around the function calls for the signal handlers. Then you could still use the automatic signal handlers while compiling with g++.

  2. As of GTK+ 2.0, libglade is deprecated. There is a new GtkBuilder interface that does the same thing. You no longer need to compile and link against the libglade libraries. I searched all over for a new interface designer to replace Glade but found a lot of references to people still using Glade. The xml file produced by glade is not exactly the same and not compatible with the GtkBuilder interface however. I finally found that there is a conversion function included with GTK+ 2.0 that converts the glade file to a GtkBuilder xml file.

    gtk-builder-convert <input file> <output file>

Anyway, with those two items taken care of, here is a really simple program and a really simple Makefile I put together to show how to get a GTK+ 2.0 program window showing.


void close_app(GtkWidget* widget,gpointer user_data) {
int main (int argc, char **argv) {
 GtkBuilder *gtkBuilder;
 GtkWidget *mainwin;
 /* Initialize the widget set */
 gtk_init (&argc, &argv);
 /* Create the main window */
 gtkBuilder= gtk_builder_new();
 gtk_builder_connect_signals ( gtkBuilder, NULL );
 mainwin= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"window1"));
 g_object_unref ( G_OBJECT(gtkBuilder) );
 /* Show the application window */
 gtk_widget_show_all ( mainwin );
 /* Enter the main event loop, and wait for user interaction */
 gtk_main ();
 /* The user lost interest */
 return 0;

LIBS=$(shell pkg-config --cflags --libs gtk+-2.0)
CFLAGS=-Wall -g -export-dynamic
hello: hello.c hello.ui
    gcc -o hello hello.c $(LIBS) $(CFLAGS)
    gtk-builder-convert hello.ui
For the sake of space, I’ll leave the xml contents out. Suffice it to say that there is a window, “window1”, and I set the destroy handler to “close_app”.

The Makefile converts to hello.ui, which is loaded by the hello program at runtime to produce the UI. You can of course build a window dynamically at runtime and you can also choose to connect the signal handlers manually. It just struck me as a lot less C code to do it like this.

March 31, 2008

Using a Makefile to generate Latex documents

Filed under: Software — Tags: , , , — Dennis @ 9:28 am

So, you’re using Latex to compile a paper, article, or book. If you have any type of table of contents, index, or bibliography, you’ve probably noticed that you have to run latex two (or sometimes 3) times to generate the final document. In addition, you probably have to run a conversion program to get your document into its final format (like pdf).

If you’re like me, I was getting tired of typing those commands every time I wanted to view a final copy of my paper. I decided to combine a couple great tools to “make” the job easier!

Makefiles aren’t just for compiling code. You can use them to generate commands that produce any final document. There really isn’t a limit as to what you can produce other than the commands available on your system.

For my current Latex document, I am creating an index.


I’m not going to go into the details of index creation with Latex here. There are good references all over for that [1]. Basically, you just add \index{Some Entry} at each point in your document that you want an item indexed. Then at the end, you include a “\printindex” command at the point you want your index to be output in your document.

After those commands are in place, your “latex mydocument” command produces a mydocument.idx file. You’ll also see a notice that there is no mydocument.ind file. That is because the final index file that is included is not the idx (which is just all of your entries with their page number), but an ind file which is the idx file’s entries, aggregated according to entry, and then combined with the appropriate style information to be printed correctly.

You have to run the following to create your .ind file:

makeindex mydocument

After you make the index, you can run latex again and your \printindex command has an ind file to include. You’re now set with a dvi file that you can convert into another form for publishing.

Now, lets automate all that. The format for a Makefile rule is:


All you have to do is add a rule for each output that you created with the process manually.

Here is a simple Makefile that I am using to create my document. The rules work in reverse order from the way I listed them in this post. I’ve added a rule for each step that I listed for creating my document and index. Don’t forget to name it “Makefile”

mybook.pdf : mybook.dvi
   dvipdf mybook
mybook.dvi : mybook.ind
   latex mybook
mybook.ind : mybook.idx
   makeindex mybook
mybook.idx : *.tex
   latex mybook

Once your Makefile is saved, all you have to do is run “make”. Of course, you might have figures or other resources that need to be included in your prerequisites for your sources, but the general idea ought to be the same for most projects. If you have questions or comments, feel free to leave them here.


Powered by WordPress

%d bloggers like this: