1 /* 2 * Copyright (C) 2006, 2007 Apple Inc. 3 * Copyright (C) 2007 Alp Toker <alp (at) atoker.com> 4 * Copyright (C) 2011 Igalia S.L. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <WebKit2/WebKit2.h> 29 #include <gtk/gtk.h> 30 31 static void activateUriEntryCallback(GtkWidget *entry, gpointer data) 32 { 33 WKViewRef webView = g_object_get_data(G_OBJECT(entry), "web-view"); 34 const gchar *uri = gtk_entry_get_text(GTK_ENTRY(entry)); 35 WKPageLoadURL(WKViewGetPage(webView), WKURLCreateWithURL(uri)); 36 } 37 38 static void destroyCallback(GtkWidget *widget, GtkWidget *window) 39 { 40 gtk_main_quit(); 41 } 42 43 static void goBackCallback(GtkWidget *widget, WKViewRef webView) 44 { 45 WKPageGoBack(WKViewGetPage(webView)); 46 } 47 48 static void goForwardCallback(GtkWidget *widget, WKViewRef webView) 49 { 50 WKPageGoForward(WKViewGetPage(webView)); 51 } 52 53 static GtkWidget *createToolbar(GtkWidget *uriEntry, WKViewRef webView) 54 { 55 GtkWidget *toolbar = gtk_toolbar_new(); 56 57 #if GTK_CHECK_VERSION(2, 15, 0) 58 gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL); 59 #else 60 gtk_toolbar_set_orientation(GTK_TOOLBAR(toolbar), GTK_ORIENTATION_HORIZONTAL); 61 #endif 62 gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_BOTH_HORIZ); 63 64 GtkToolItem *item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK); 65 g_signal_connect(item, "clicked", G_CALLBACK(goBackCallback), (gpointer)webView); 66 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 67 68 item = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD); 69 g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(goForwardCallback), (gpointer)webView); 70 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 71 72 item = gtk_tool_item_new(); 73 gtk_tool_item_set_expand(item, TRUE); 74 gtk_container_add(GTK_CONTAINER(item), uriEntry); 75 g_signal_connect(uriEntry, "activate", G_CALLBACK(activateUriEntryCallback), NULL); 76 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 77 78 g_object_set_data(G_OBJECT(uriEntry), "web-view", (gpointer)webView); 79 item = gtk_tool_button_new_from_stock(GTK_STOCK_OK); 80 g_signal_connect_swapped(item, "clicked", G_CALLBACK(activateUriEntryCallback), (gpointer)uriEntry); 81 gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, -1); 82 83 return toolbar; 84 } 85 86 static WKViewRef createWebView() 87 { 88 return WKViewCreate(WKContextGetSharedProcessContext(), 0); 89 } 90 91 static GtkWidget *createWindow(WKViewRef webView) 92 { 93 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 94 gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); 95 gtk_widget_set_name(window, "MiniBrowser"); 96 97 GtkWidget *uriEntry = gtk_entry_new(); 98 99 GtkWidget *vbox = gtk_vbox_new(FALSE, 0); 100 gtk_box_pack_start(GTK_BOX(vbox), createToolbar(uriEntry, webView), FALSE, FALSE, 0); 101 gtk_box_pack_start(GTK_BOX(vbox), WKViewGetWindow(webView), TRUE, TRUE, 0); 102 103 gtk_container_add(GTK_CONTAINER(window), vbox); 104 105 g_signal_connect(window, "destroy", G_CALLBACK(destroyCallback), NULL); 106 107 return window; 108 } 109 110 static gchar *argumentToURL(const char *filename) 111 { 112 GFile *gfile = g_file_new_for_commandline_arg(filename); 113 gchar *fileURL = g_file_get_uri(gfile); 114 g_object_unref(gfile); 115 116 return fileURL; 117 } 118 119 int main(int argc, char *argv[]) 120 { 121 gtk_init(&argc, &argv); 122 123 if (!g_thread_supported()) 124 g_thread_init(NULL); 125 126 WKViewRef webView = createWebView(); 127 GtkWidget *mainWindow = createWindow(webView); 128 129 gchar* url = argumentToURL(argc > 1 ? argv[1] : "http://www.webkitgtk.org/"); 130 WKPageLoadURL(WKViewGetPage(webView), WKURLCreateWithURL(url)); 131 g_free(url); 132 133 gtk_widget_grab_focus(WKViewGetWindow(webView)); 134 gtk_widget_show_all(mainWindow); 135 gtk_main(); 136 137 return 0; 138 } 139