转载请注明出处,谢谢!
该示例演示了webkitgtk使用的基本过程,其中setenv主要是设置界面显示的输出设备,由于很多server没有输出显示输出设备,因此,
可以通过Xvfb虚拟,具体可搜索Xvfb相关资料。
同时在编译该示例时,需要设置头文件路径等,可以通:
export C_INCLUDE_PATH=XXXXX
export CPLUS_INCLUDE_PATH = XXXXX
将头文件路径导入,具体涉及到:gtk-2.0、gtkglext-1.0、gtkmm-2.4、cairo、atk-1.0、atkmm-1.6、libsoup-gnome-2.4、libsoup-2.4....
.等等,可以在编译过程中根据错误提示添加。
编译: gcc -o ckit ckit.cpp `pkg-config --cflags --libs webkit-2.0`
#include <stdlib.h>
#include <stdio.h>
#include <webkit/webkit.h>
#include <gtk/gtk.h>
static void load_finished(WebKitWebView *view, gpointer data)
{
printf("called_load_fininshed\n");
printf("get_url:%s\n", webkit_web_view_get_uri(view) );
printf("get_tiele:%s\n", webkit_web_view_get_title(view) );
}
int main(int argc, char **argv)
{
for(int i=1; i<argc; i++)
{
printf("input_pm[%d]:%s\n", i, argv[i]);
}
if( setenv ("DISPLAY", ":0", 0) != 0 )
{
printf("set display:1 failed\n");
return -1;
}
printf("check thread_support\n");
if (!g_thread_supported ())
g_thread_init (NULL);
printf("init gtk\n");
gtk_init (&argc, &argv);
/*
WebKitWebFrame *web_frame = NULL;
*/
GtkWidget *web_view = NULL;
GtkWidget *window = NULL;
GtkWidget *scrolled = NULL;
printf("create a top window\n");
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
printf("set window size\n");
gtk_window_set_default_size (GTK_WINDOW (window), 1024, 768);
scrolled = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
printf("create a webview\n");
web_view = webkit_web_view_new ();
printf("add webview to window\n");
gtk_container_add (GTK_CONTAINER (scrolled), web_view);
gtk_container_add (GTK_CONTAINER (window), scrolled);
printf("show window\n");
gtk_widget_show_all (window);
printf("set call back of webpage loaded\n");
g_signal_connect(web_view, "load-finished", G_CALLBACK(load_finished), NULL);
printf("load a web page\n");
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "http://www.baidu.com");
printf("gtk_main\n");
gtk_main();
return 0;
}