This is a very basic example on how to use WebkitGtk and GtkBuilder.
This example works as is but needs alot of extra work to be a fully functional web browser.
Note: I will clean up and comment the code when i find more time. The WebkitGtk Reference Api is here
http://oe.linuxtogo.org/~zecke/webkit-g ... index.html
main.c
Code:
/*
First run browser.glade through gtk-builder-convert with this command:
gtk-builder-convert browser.glade browser.xml
Then save this file as main.c and compile it using this command
(those are backticks, not single quotes):
gcc -Wall -g -o main main.c `pkg-config --cflags --libs gtk+-2.0 webkit-1.0` -export-dynamic
Then execute it using:
./main
*/
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>
static WebKitWebView* web_view;
void on_window_destroy (GtkObject *object, gpointer user_data)
{
gtk_main_quit ();
}
void on_toolbutton1_clicked( GtkWidget *widget, gpointer data )
{
webkit_web_view_go_back(web_view);
}
void on_toolbutton2_clicked( GtkWidget *widget, gpointer data )
{
webkit_web_view_go_forward(web_view);
}
void on_toolbutton3_clicked( GtkWidget *widget, gpointer data )
{
webkit_web_view_reload(web_view);
}
void on_toolbutton4_clicked( GtkWidget *widget,gpointer data )
{
webkit_web_view_stop_loading (web_view);
}
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *scrolled_window;
GtkWidget *statusbar1;
GtkWidget *toolbutton1;
GtkWidget *toolbutton2;
GtkWidget *toolbutton3;
GtkWidget *toolbutton4;
gtk_init(&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "browser.xml", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
scrolled_window = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow1"));
statusbar1 = GTK_WIDGET (gtk_builder_get_object (builder, "statusbar1"));
toolbutton1 = GTK_WIDGET (gtk_builder_get_object (builder, "toolbutton1"));
toolbutton2 = GTK_WIDGET (gtk_builder_get_object (builder, "toolbutton2"));
toolbutton3 = GTK_WIDGET (gtk_builder_get_object (builder, "toolbutton3"));
toolbutton4 = GTK_WIDGET (gtk_builder_get_object (builder, "toolbutton4"));
gtk_window_set_title(GTK_WINDOW(window), "Example Web Browser");
web_view = WEBKIT_WEB_VIEW (webkit_web_view_new ());
gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));
gchar* uri = (gchar*) (argc > 1 ? argv[1] : "http://www.google.com");
webkit_web_view_open (web_view, uri);
webkit_web_view_execute_script(WEBKIT_WEB_VIEW (web_view), "var t=setTimeout(\"alert('5 seconds!')\",5000);");
gtk_statusbar_push (GTK_STATUSBAR(statusbar1), 0, "Done");
g_signal_connect (G_OBJECT (toolbutton1), "clicked",
G_CALLBACK (on_toolbutton1_clicked), NULL);
g_signal_connect (G_OBJECT (toolbutton2), "clicked",
G_CALLBACK (on_toolbutton2_clicked), NULL);
g_signal_connect (G_OBJECT (toolbutton3), "clicked",
G_CALLBACK (on_toolbutton3_clicked), NULL);
g_signal_connect (G_OBJECT (toolbutton4), "clicked",
G_CALLBACK (on_toolbutton4_clicked), NULL);
gtk_widget_grab_focus (GTK_WIDGET (web_view));
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
browser.xmlCode:
<?xml version="1.0"?>
<interface>
<object class="GtkUIManager" id="uimanager1">
<child>
<object class="GtkActionGroup" id="actiongroup1">
<child>
<object class="GtkAction" id="menuitem1">
<property name="name">menuitem1</property>
<property name="label" translatable="yes">_File</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem1">
<property name="name">imagemenuitem1</property>
<property name="label">gtk-new</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem2">
<property name="name">imagemenuitem2</property>
<property name="label">gtk-open</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem3">
<property name="name">imagemenuitem3</property>
<property name="label">gtk-save</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem4">
<property name="name">imagemenuitem4</property>
<property name="label">gtk-save-as</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem5">
<property name="name">imagemenuitem5</property>
<property name="label">gtk-quit</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem2">
<property name="name">menuitem2</property>
<property name="label" translatable="yes">_Edit</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem6">
<property name="name">imagemenuitem6</property>
<property name="label">gtk-cut</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem7">
<property name="name">imagemenuitem7</property>
<property name="label">gtk-copy</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem8">
<property name="name">imagemenuitem8</property>
<property name="label">gtk-paste</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem9">
<property name="name">imagemenuitem9</property>
<property name="label">gtk-delete</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem3">
<property name="name">menuitem3</property>
<property name="label" translatable="yes">_View</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem5">
<property name="name">menuitem5</property>
<property name="label" translatable="yes">History</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem6">
<property name="name">menuitem6</property>
<property name="label" translatable="yes">Bookmarks</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem7">
<property name="name">menuitem7</property>
<property name="label" translatable="yes">Tools</property>
</object>
</child>
<child>
<object class="GtkAction" id="menuitem4">
<property name="name">menuitem4</property>
<property name="label" translatable="yes">_Help</property>
</object>
</child>
<child>
<object class="GtkAction" id="imagemenuitem10">
<property name="name">imagemenuitem10</property>
<property name="label">gtk-about</property>
</object>
</child>
</object>
</child>
<ui>
<menubar name="menubar1">
<menu action="menuitem1">
<menuitem action="imagemenuitem1"/>
<menuitem action="imagemenuitem2"/>
<menuitem action="imagemenuitem3"/>
<menuitem action="imagemenuitem4"/>
<separator/>
<menuitem action="imagemenuitem5"/>
</menu>
<menu action="menuitem2">
<menuitem action="imagemenuitem6"/>
<menuitem action="imagemenuitem7"/>
<menuitem action="imagemenuitem8"/>
<menuitem action="imagemenuitem9"/>
</menu>
<menuitem action="menuitem3"/>
<menuitem action="menuitem5"/>
<menuitem action="menuitem6"/>
<menuitem action="menuitem7"/>
<menu action="menuitem4">
<menuitem action="imagemenuitem10"/>
</menu>
</menubar>
</ui>
</object>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="visible">True</property>
<property name="default_width">800</property>
<property name="default_height">600</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<child>
<object class="GtkMenuBar" constructor="uimanager1" id="menubar1">
<property name="visible">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkToolbar" id="toolbar1">
<property name="visible">True</property>
<property name="toolbar_style">icons</property>
<property name="icon_size">menu</property>
<property name="icon_size_set">True</property>
<child>
<object class="GtkToolButton" id="toolbutton1">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton1</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-go-back</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton2</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-go-forward</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton3">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton3</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-refresh</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton4">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton4</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-stop</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton5">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton5</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-home</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton6">
<property name="visible">True</property>
<property name="border_width">2</property>
<property name="is_important">True</property>
<child>
<object class="GtkComboBoxEntry" id="comboboxentry1">
<property name="width_request">400</property>
<property name="visible">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton14">
<property name="visible">True</property>
<property name="border_width">2</property>
<child>
<object class="GtkComboBoxEntry" id="comboboxentry4">
<property name="visible">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkNotebook" id="notebook1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="show_tabs">False</property>
<property name="show_border">False</property>
<property name="scrollable">True</property>
<property name="enable_popup">True</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<placeholder/>
</child>
</object>
</child>
<child type="tab">
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="spacing">2</property>
<child>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="stock">gtk-missing-image</property>
<property name="icon-size">1</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="label" translatable="yes">http://www ...</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="tab_fill">False</property>
</packing>
</child>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkToolbar" id="toolbar2">
<property name="visible">True</property>
<property name="toolbar_style">both-horiz</property>
<property name="icon_size">menu</property>
<child>
<object class="GtkToolButton" id="toolbutton7">
<property name="visible">True</property>
<property name="use_underline">True</property>
<property name="icon_name">gtk-close</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton8">
<property name="visible">True</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="xpad">3</property>
<property name="label" translatable="yes">Find:</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton9">
<property name="visible">True</property>
<child>
<object class="GtkComboBoxEntry" id="comboboxentry2">
<property name="visible">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton10">
<property name="visible">True</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Previous</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-go-back</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton11">
<property name="visible">True</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Next</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-go-forward</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton12">
<property name="visible">True</property>
<property name="is_important">True</property>
<property name="label" translatable="yes">Highlight all</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-select-color</property>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<object class="GtkToolItem" id="toolbutton13">
<property name="visible">True</property>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Match case</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkStatusbar" id="statusbar1">
<property name="visible">True</property>
<property name="spacing">2</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>