Embedded widgets

In this section I will show you how to embed Gtk+ widgets into a GtkHTML widget. I will extend the previous example by embedding a Close button, which when clicked quits the example.

First we need to insert button to HTML source. This is achieved by the OBJECT tag with the attribute CLASSID. The following string is appended to the html_source string.


"<BR><BR><DIV ALIGN=RIGHT><OBJECT CLASSID=close_button>Closebutton</OBJECT>"

The next step is to provide Gtk+ button to GtkHTML. This is also done using signals. While GtkHTML parses its content, whenever it finds an OBJECT tag, it emits an object_requested signal. The following code implements our new signal handler, which creates the Close button:


static void
close_button_clicked ()
{
	gtk_main_quit ();
}

static gboolean
object_requested (GtkHTML *html, GtkHTMLEmbedded *ew)
{
	GtkWidget *button;

	if (!strcmp (ew->classid, "close_button") == 0)
		return FALSE;

	button = gtk_button_new_with_label ("Close");
	gtk_signal_connect (GTK_OBJECT (button), "clicked",
                            GTK_SIGNAL_FUNC (close_button_clicked), NULL);
	gtk_container_add (GTK_CONTAINER (ew), button);
	gtk_widget_show (button);

	return TRUE;
}

Note that the handler has a boolean return value and returns TRUE if we have provided tje requested widget and FALSE if for some reason we cannot provide it. In such a case the HTML content between the opening and closing OBJECT tags will be used instead.

The next thing to notice is that the provided widget should already be shown (gtk_widget_show call) and added into a GtkHTMLEmbedded widget container, which is passed to callback as the 2nd (ew) parameter.

Finally, we connect our object_requested callback just after GtkHTML widget creation.


	gtk_signal_connect (GTK_OBJECT (html), "object_requested",
			    GTK_SIGNAL_FUNC (object_requested), NULL);

For Embedded widgets manipulation we need to include this header file:


#include <gtkhtml/gtkhtml-embedded.h>

This is all we need for our Embedded widget example. The following picture shows Example 3 running.

Figure 3. Screenshot of running Example 3