Hello world

Lets start with a simple "Hello world!" example. The following program creates a Gnome application window filled with GtkHTML widget loaded with simple content defined in the html_source C string.

Example 1.


#include <gnome.h>
#include <gtkhtml/gtkhtml.h>

const gchar *html_source = "<B><FONT COLOR=Blue>Hello world!</FONT></B>";

gint
main (gint argc, gchar *args [])
{
	GtkWidget *app;
	GtkWidget *html;
	GtkWidget *scrolled_window;

	/* prepare our environment, we need gnome and gconf */
	gnome_init ("Example_1", "1.0", argc, args);
	gconf_init (argc, args);

	/* create GtkHTML widget with preloaded content */
	html = gtk_html_new_from_string (html_source, -1);

	/* create GNOME app and put GtkHTML in scrolled window in it */
	app = gnome_app_new ("Example_1", "Example 1");
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add (GTK_CONTAINER (scrolled_window), html);
	gnome_app_set_contents (GNOME_APP (app), scrolled_window);
	gtk_window_set_default_size (GTK_WINDOW (app), 320, 80);
	gtk_widget_show_all (app);

	/* run the main loop */
	gtk_main ();
}

Figure 1. Screenshot of running Example 1

In this example we use the
GtkWidget *gtk_html_new_from_string (const gchar *str, gint len);
constructor which creates a new GtkHTML widget loaded with the HTML document stored in str parameter. len is the length of str argument, if -1 is passed, then length is not used and is computed in the constructor. (The reason for this is to avoid repeatedly computing of length)

The next thing to note is that we have packed the GtkHTML widget into a scrolled window. This is pretty important. Older versions of GtkHTML were even segfaulting when the widget was not packed into a GtkScrolledWindow widget.

Also note, that GtkHTML requires GNOME libraries and GConf, thus you need to initialize them before the first call to the GtkHTML library and use gnome.h and gtkhtml/gtkhtml.h include files.