无法使用GTK WebKit Webview将单击按钮连接到C函数



我正在创建一个应用程序,其中GUI使用HTML和JavaScript呈现,按钮动作需要通过USB连接的硬件进行交互。我把所有硬件相关的功能作为一个共享库(.so)文件。
操作系统:Ubuntu Server 12.04
IDE: Eclipse Juno
语言:C, JavaScript
库:GTK, Webkit, Webview

我使用DOM节点遍历来遍历HTML页面中的控件。我能够使用值获得按钮,但无法成功地将事件添加到按钮单击。HTML页面和相应的GTK C代码如下:

<html>
<head>
<script type="text/javascript">
    function read()
    {
        alert( 'Read called' );
    }
    function configure()
    {
        alert( 'configure called' );
    }
</script>
<h1> <font color="white"> GTK WebKit </font> </h1>
</head>
<body>
    <label id="labelStep1"> <font color="white" size="4"> Step 1: </font> </label>
    <input type="button" onclick="configure()" value="Configure">
    </br>
    <label id="labelStep2"> <font color="white" size="4"> Step 2: </font> </label>
    <input type="button" onclick="read()" value="Read"> <font color="white" size="6"> </font> </input>
    </br>
    </br>
</body>
</html>

GTK.c:

#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <webkit/webkitwebview.h>

static void destroyWindow(GtkWidget *pWidget1, GtkWidget *pWidget2);
static gboolean closeWebViewCB(WebKitWebView *pWebView, GtkWidget *pWindow);
static void loadStatusCB(WebKitWebView *pWebView, GParamSpec *pSpec, void *pContext);
 int main (int argc, char *argv[])
 {
    GtkWidget *window;
    GtkWidget *label;
    gtk_init (&argc, &argv);
    /* create the main, top level, window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_resize(window, 1024, 768);
    WebKitWebView *pWebView = WEBKIT_WEB_VIEW(webkit_web_view_new());
    GtkWidget *pscrolledWnd = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(pscrolledWnd), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_window_resize(pscrolledWnd, 1024, 768);
    gtk_container_add(GTK_CONTAINER(pscrolledWnd), GTK_WIDGET(pWebView));
    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(pscrolledWnd));
    webkit_web_view_load_uri(pWebView, "file:///home/developer/Desktop/Dan/workspace/GTK+/Resources/WebViewGTK.html");
    gtk_widget_grab_focus(GTK_WIDGET(pWebView));
    /* give it the title */
    gtk_window_set_title (GTK_WINDOW (window), "GTK WebKit");
    /* Connect the destroy signal of the window to gtk_main_quit
     * When the window is about to be destroyed we get a notification and
     * stop the main GTK+ loop
     */
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect(pWebView, "close-web-view", G_CALLBACK(closeWebViewCB), window);
    g_signal_connect(pWebView, "notify::load-status", G_CALLBACK(loadStatusCB), window);
    /* Create the "Hello, World" label  */
    label = gtk_label_new ("Hello, World");
    /* and insert it into the main window  */
    gtk_container_add (GTK_CONTAINER (window), label);
    /* make sure that everything, window and label, are visible */
    gtk_widget_show_all (window);
    /* start the main loop, and let it rest there until the application is closed */
    gtk_main ();
    return 0;
 }

 static void destroyWindow(GtkWidget *pWidget1, GtkWidget *pWidget2)
 {
     gtk_main_quit();
 }

 static gboolean closeWebViewCB(WebKitWebView *pWebView, GtkWidget *pWindow)
 {
     gtk_widget_destroy(pWindow);
     return 1;
 }

 static gboolean TestCB(WebKitWebView *pWebView, GtkWidget *pWindow)
 {
     GtkWidget *pMsgBox = gtk_message_dialog_new(pWindow, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_YES_NO, "TestCB Called()!");
     gtk_dialog_run(pMsgBox);
     gtk_widget_destroy(pMsgBox);
     return true;
 }


 static void handleWebPageLoadEvents(WebKitWebView *pWebView, void * pContext)
 {
     int nElemIndex         = 0;
     char *pChElemName      = NULL;
     char chMsgBoxText[255] = { 0 };
     static int nEventAddCallCount = 0;
     GtkWidget *pMsgBox = NULL;//gtk_message_dialog_new(pContext, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, chMsgBoxText /*"handleWebPageLoadEvents Called()!"*/);
     WebKitDOMDocument *pDoc = webkit_web_view_get_dom_document(pWebView);
     WebKitDOMNodeList *pDomNodeList = webkit_dom_document_get_elements_by_tag_name(pDoc, "*");
     gulong elements_Count = webkit_dom_node_list_get_length(pDomNodeList);
     for(nElemIndex = 0; nElemIndex < elements_Count; nElemIndex ++)
     {
         WebKitDOMNode *pElement = webkit_dom_node_list_item(pDomNodeList, nElemIndex);
         if(WEBKIT_DOM_IS_HTML_INPUT_ELEMENT(pElement))
         {
             g_object_get(pElement, "value", & pChElemName, NULL);
             if(NULL != pChElemName)
             {
                 if(g_str_equal("Configure", pChElemName) )
                 {
                     if(webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(pElement), pChElemName, G_CALLBACK(TestCB), false, pContext) )
                         sprintf(chMsgBoxText, "webkit_dom_event_target_add_event_listener  --> %s --> OK", pChElemName);
                     else sprintf(chMsgBoxText, "webkit_dom_event_target_add_event_listener  --> %s --> FAILED", pChElemName);
                 }
                 if(g_str_equal("Read", pChElemName) )
                 {
                     if( webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(pElement), /*pChElemName*/ "Read", G_CALLBACK(TestCB), false, pContext) )
                         sprintf(chMsgBoxText, "webkit_dom_event_target_add_event_listener  --> %s --> OK", pChElemName);
                     else sprintf(chMsgBoxText, "webkit_dom_event_target_add_event_listener  --> %s --> FAILED", pChElemName);
                 }
                 pMsgBox = gtk_message_dialog_new(pContext, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, chMsgBoxText);
                 gtk_dialog_run(pMsgBox);
                 gtk_widget_destroy(pMsgBox);
                 if(pChElemName)
                     g_free(pChElemName);
             }
         }
     }
     g_object_unref(pDomNodeList);
 }


 static void loadStatusCB(WebKitWebView *pWebView, GParamSpec *pSpec, void *pContext)
 {
     char                   Msg[64]     = { 0 };
     static unsigned int    nCounter    = 0;
     WebKitLoadStatus status = webkit_web_view_get_load_status(pWebView);
     //GObject *pObject = G_OBJECT(pWebView);
     g_object_freeze_notify(pContext);
     switch(status)
     {
     case WEBKIT_LOAD_FINISHED:
         sprintf(Msg, "WEBKIT_LOAD_FINISHED~%d", nCounter);
         handleWebPageLoadEvents(pWebView, pContext);
         break;
     case WEBKIT_LOAD_PROVISIONAL:
         sprintf(Msg, "WEBKIT_LOAD_PROVISIONAL~%d", nCounter);
         break;
     case WEBKIT_LOAD_COMMITTED:
         sprintf(Msg, "WEBKIT_LOAD_COMMITTED~%d", nCounter);
         break;
     case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
         sprintf(Msg, "WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT~%d", nCounter);
         break;
     case WEBKIT_LOAD_FAILED:
         sprintf(Msg, "WEBKIT_LOAD_FAILED~%d", nCounter);
         break;
     default:
         sprintf(Msg, "default~%d", nCounter);
         break;
     }
 }

我对GTK非常陌生,所以任何帮助都很感激。

问候。

我认为你混淆了元素名称和/或操作与HTML事件。您应该使用click event来获得关于按钮单击的通知。代码应该像

webkit_dom_event_target_add_event_listener(WEBKIT_DOM_EVENT_TARGET(pElement), "click", G_CALLBACK(TestCB), false, pContext)

在https://live.gnome.org/WebKitGtk/ProgrammingGuide/Cookbook有一个例子。

或者,如果你想使用警报,看看web视图的script-alert信号。您的回调将使用警报消息调用。基于Python的在https://github.com/nhrdl/notesMD

使用脚本警报信号的示例

最新更新