5e26e3c829c7a5f419136be691aebdfa7ea180fa
[WebKit-https.git] / WebKit / gtk / WebView / webkitwebview.cpp
1 /*
2  *  Copyright (C) 2007 Holger Hans Peter Freyther
3  *  Copyright (C) 2007 Christian Dywan <christian@twotoasts.de>
4  *  Copyright (C) 2007 Xan Lopez <xan@gnome.org>
5  *  Copyright (C) 2007 Alp Toker <alp@atoker.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "config.h"
23
24 #include "webkitwebview.h"
25 #include "webkit-marshal.h"
26 #include "webkitprivate.h"
27
28 #include "NotImplemented.h"
29 #include "CString.h"
30 #include "ChromeClientGtk.h"
31 #include "ContextMenu.h"
32 #include "ContextMenuClientGtk.h"
33 #include "ContextMenuController.h"
34 #include "Cursor.h"
35 #include "DragClientGtk.h"
36 #include "Editor.h"
37 #include "EditorClientGtk.h"
38 #include "EventHandler.h"
39 #include "FocusController.h"
40 #include "HitTestRequest.h"
41 #include "HitTestResult.h"
42 #include "GraphicsContext.h"
43 #include "InspectorClientGtk.h"
44 #include "FrameLoader.h"
45 #include "FrameView.h"
46 #include "Editor.h"
47 #include "PasteboardHelper.h"
48 #include "PlatformKeyboardEvent.h"
49 #include "PlatformWheelEvent.h"
50 #include "SubstituteData.h"
51
52 #include <gdk/gdkkeysyms.h>
53
54 using namespace WebKit;
55 using namespace WebCore;
56
57 extern "C" {
58
59 enum {
60     /* normal signals */
61     NAVIGATION_REQUESTED,
62     WINDOW_OBJECT_CLEARED,
63     LOAD_STARTED,
64     LOAD_COMMITTED,
65     LOAD_PROGRESS_CHANGED,
66     LOAD_FINISHED,
67     TITLE_CHANGED,
68     HOVERING_OVER_LINK,
69     STATUS_BAR_TEXT_CHANGED,
70     ICOND_LOADED,
71     SELECTION_CHANGED,
72     CONSOLE_MESSAGE,
73     SCRIPT_ALERT,
74     SCRIPT_CONFIRM,
75     SCRIPT_PROMPT,
76     SELECT_ALL,
77     COPY_CLIPBOARD,
78     PASTE_CLIPBOARD,
79     CUT_CLIPBOARD,
80     LAST_SIGNAL
81 };
82
83 enum {
84     PROP_0,
85
86     PROP_COPY_TARGET_LIST,
87     PROP_PASTE_TARGET_LIST
88 };
89
90 static guint webkit_web_view_signals[LAST_SIGNAL] = { 0, };
91
92 G_DEFINE_TYPE(WebKitWebView, webkit_web_view, GTK_TYPE_CONTAINER)
93
94 static void webkit_web_view_context_menu_position_func(GtkMenu*, gint* x, gint* y, gboolean* pushIn, WebKitWebViewPrivate* data)
95 {
96     *pushIn = FALSE;
97     *x = data->lastPopupXPosition;
98     *y = data->lastPopupYPosition;
99 }
100
101 static gboolean webkit_web_view_forward_context_menu_event(WebKitWebView* webView, const PlatformMouseEvent& event)
102 {
103     Page* page = core(webView);
104     page->contextMenuController()->clearContextMenu();
105     Frame* focusedFrame = page->focusController()->focusedOrMainFrame();
106     focusedFrame->view()->setCursor(pointerCursor());
107     bool handledEvent = focusedFrame->eventHandler()->sendContextMenuEvent(event);
108     if (!handledEvent)
109         return FALSE;
110
111     ContextMenu* coreMenu = page->contextMenuController()->contextMenu();
112     if (!coreMenu)
113         return FALSE;
114
115     if (!coreMenu->platformDescription())
116         return FALSE;
117
118
119     WebKitWebViewPrivate* pageData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
120     pageData->lastPopupXPosition = event.globalX();
121     pageData->lastPopupYPosition = event.globalY();
122     gtk_menu_popup(GTK_MENU(coreMenu->platformDescription()), NULL, NULL,
123                    reinterpret_cast<GtkMenuPositionFunc>(webkit_web_view_context_menu_position_func), pageData,
124                    event.button() + 1, gtk_get_current_event_time());
125     return TRUE;
126 }
127
128 static gboolean webkit_web_view_popup_menu_handler(GtkWidget* widget)
129 {
130     static const int contextMenuMargin = 1;
131
132     // The context menu event was generated from the keyboard, so show the context menu by the current selection.
133     Page* page = core(WEBKIT_WEB_VIEW(widget));
134     FrameView* view = page->mainFrame()->view();
135     Position start = page->mainFrame()->selectionController()->selection().start();
136     Position end = page->mainFrame()->selectionController()->selection().end();
137
138     int rightAligned = FALSE;
139     IntPoint location;
140
141     if (!start.node() || !end.node())
142         location = IntPoint(rightAligned ? view->contentsWidth() - contextMenuMargin : contextMenuMargin, contextMenuMargin);
143     else {
144         RenderObject* renderer = start.node()->renderer();
145         if (!renderer)
146             return FALSE;
147
148         // Calculate the rect of the first line of the selection (cribbed from -[WebCoreFrameBridge firstRectForDOMRange:]).
149         int extraWidthToEndOfLine = 0;
150         IntRect startCaretRect = renderer->caretRect(start.offset(), DOWNSTREAM, &extraWidthToEndOfLine);
151         IntRect endCaretRect = renderer->caretRect(end.offset(), UPSTREAM);
152
153         IntRect firstRect;
154         if (startCaretRect.y() == endCaretRect.y())
155             firstRect = IntRect(MIN(startCaretRect.x(), endCaretRect.x()),
156                                 startCaretRect.y(),
157                                 abs(endCaretRect.x() - startCaretRect.x()),
158                                 MAX(startCaretRect.height(), endCaretRect.height()));
159         else
160             firstRect = IntRect(startCaretRect.x(),
161                                 startCaretRect.y(),
162                                 startCaretRect.width() + extraWidthToEndOfLine,
163                                 startCaretRect.height());
164
165         location = IntPoint(rightAligned ? firstRect.right() : firstRect.x(), firstRect.bottom());
166     }
167
168     int x, y;
169     gdk_window_get_origin(GTK_WIDGET(view->containingWindow())->window, &x, &y);
170
171     // FIXME: The IntSize(0, -1) is a hack to get the hit-testing to result in the selected element.
172     // Ideally we'd have the position of a context menu event be separate from its target node.
173     location = view->contentsToWindow(location) + IntSize(0, -1);
174     IntPoint global = location + IntSize(x, y);
175     PlatformMouseEvent event(location, global, NoButton, MouseEventPressed, 0, false, false, false, false, gtk_get_current_event_time());
176
177     return webkit_web_view_forward_context_menu_event(WEBKIT_WEB_VIEW(widget), event);
178 }
179
180 static void webkit_web_view_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) {
181     WebKitWebView* webView = WEBKIT_WEB_VIEW(object);
182
183     switch(prop_id) {
184     case PROP_COPY_TARGET_LIST:
185         g_value_set_boxed(value, webkit_web_view_get_copy_target_list(webView));
186         break;
187     case PROP_PASTE_TARGET_LIST:
188         g_value_set_boxed(value, webkit_web_view_get_paste_target_list(webView));
189         break;
190     default:
191         break;
192     }
193 }
194
195 static gboolean webkit_web_view_expose_event(GtkWidget* widget, GdkEventExpose* event)
196 {
197     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
198     GdkRectangle clip;
199     gdk_region_get_clipbox(event->region, &clip);
200     cairo_t* cr = gdk_cairo_create(event->window);
201     GraphicsContext ctx(cr);
202     ctx.setGdkExposeEvent(event);
203     if (frame->renderer()) {
204         frame->view()->layoutIfNeededRecursive();
205         frame->view()->paint(&ctx, clip);
206     }
207     cairo_destroy(cr);
208
209     return FALSE;
210 }
211
212 static gboolean webkit_web_view_key_press_event(GtkWidget* widget, GdkEventKey* event)
213 {
214     Frame* frame = core(WEBKIT_WEB_VIEW(widget))->focusController()->focusedOrMainFrame();
215     PlatformKeyboardEvent keyboardEvent(event);
216
217     if (frame->eventHandler()->keyEvent(keyboardEvent))
218         return TRUE;
219
220     FrameView* view = frame->view();
221     SelectionController::EAlteration alteration;
222     if (event->state & GDK_SHIFT_MASK)
223         alteration = SelectionController::EXTEND;
224     else
225         alteration = SelectionController::MOVE;
226
227     // TODO: We probably want to use GTK+ key bindings here and perhaps take an
228     // approach more like the Win and Mac ports for key handling.
229     switch (event->keyval) {
230     case GDK_Down:
231         view->scrollBy(0, LINE_STEP);
232         return TRUE;
233     case GDK_Up:
234         view->scrollBy(0, -LINE_STEP);
235         return TRUE;
236     case GDK_Right:
237         view->scrollBy(LINE_STEP, 0);
238         return TRUE;
239     case GDK_Left:
240         view->scrollBy(-LINE_STEP, 0);
241         return TRUE;
242     case GDK_Home:
243         frame->selectionController()->modify(alteration, SelectionController::BACKWARD, DocumentBoundary, true);
244         return TRUE;
245     case GDK_End:
246         frame->selectionController()->modify(alteration, SelectionController::FORWARD, DocumentBoundary, true);
247         return TRUE;
248     }
249
250     /* Chain up to our parent class for binding activation */
251     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->key_press_event(widget, event);
252 }
253
254 static gboolean webkit_web_view_key_release_event(GtkWidget* widget, GdkEventKey* event)
255 {
256     Frame* frame = core(WEBKIT_WEB_VIEW(widget))->focusController()->focusedOrMainFrame();
257     PlatformKeyboardEvent keyboardEvent(event);
258
259     if (frame->eventHandler()->keyEvent(keyboardEvent))
260         return TRUE;
261
262     /* Chain up to our parent class for binding activation */
263     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->key_release_event(widget, event);
264 }
265
266 static gboolean webkit_web_view_button_press_event(GtkWidget* widget, GdkEventButton* event)
267 {
268     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
269
270     // FIXME: need to keep track of subframe focus for key events
271     gtk_widget_grab_focus(GTK_WIDGET(widget));
272
273     if (event->button == 3)
274         return webkit_web_view_forward_context_menu_event(WEBKIT_WEB_VIEW(widget), PlatformMouseEvent(event));
275     else
276         return frame->eventHandler()->handleMousePressEvent(PlatformMouseEvent(event));
277 }
278
279 static gboolean webkit_web_view_button_release_event(GtkWidget* widget, GdkEventButton* event)
280 {
281     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
282
283     WebKitWebView* web_view = WEBKIT_WEB_VIEW(widget);
284     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(web_view);
285     Frame* focusedFrame = webViewData->corePage->focusController()->focusedFrame();
286
287     if (focusedFrame->editor()->canEdit()) {
288         GdkWindow* window = gtk_widget_get_parent_window(widget);
289         gtk_im_context_set_client_window(webViewData->imContext, window);
290 #ifdef MAEMO_CHANGES
291         hildon_gtk_im_context_filter_event(webViewData->imContext, (GdkEvent*)event);
292         hildon_gtk_im_context_show(webViewData->imContext);
293 #endif
294     }
295
296     return frame->eventHandler()->handleMouseReleaseEvent(PlatformMouseEvent(event));
297 }
298
299 static gboolean webkit_web_view_motion_event(GtkWidget* widget, GdkEventMotion* event)
300 {
301     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
302     return frame->eventHandler()->mouseMoved(PlatformMouseEvent(event));
303 }
304
305 static gboolean webkit_web_view_scroll_event(GtkWidget* widget, GdkEventScroll* event)
306 {
307     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
308     PlatformWheelEvent wheelEvent(event);
309     return frame->eventHandler()->handleWheelEvent(wheelEvent);
310 }
311
312 static void webkit_web_view_size_allocate(GtkWidget* widget, GtkAllocation* allocation)
313 {
314     GTK_WIDGET_CLASS(webkit_web_view_parent_class)->size_allocate(widget,allocation);
315
316     Frame* frame = core(webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(widget)));
317     frame->view()->resize(allocation->width, allocation->height);
318     frame->forceLayout();
319     frame->view()->adjustViewSize();
320 }
321
322 static void webkit_web_view_realize(GtkWidget* widget)
323 {
324     GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
325
326     GdkWindowAttr attributes;
327     attributes.window_type = GDK_WINDOW_CHILD;
328     attributes.x = widget->allocation.x;
329     attributes.y = widget->allocation.y;
330     attributes.width = widget->allocation.width;
331     attributes.height = widget->allocation.height;
332     attributes.wclass = GDK_INPUT_OUTPUT;
333     attributes.visual = gtk_widget_get_visual (widget);
334     attributes.colormap = gtk_widget_get_colormap (widget);
335     attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK
336                             | GDK_EXPOSURE_MASK
337                             | GDK_BUTTON_PRESS_MASK
338                             | GDK_BUTTON_RELEASE_MASK
339                             | GDK_POINTER_MOTION_MASK
340                             | GDK_KEY_PRESS_MASK
341                             | GDK_KEY_RELEASE_MASK
342                             | GDK_BUTTON_MOTION_MASK
343                             | GDK_BUTTON1_MOTION_MASK
344                             | GDK_BUTTON2_MOTION_MASK
345                             | GDK_BUTTON3_MOTION_MASK;
346
347     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
348     widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
349     gdk_window_set_user_data(widget->window, widget);
350 }
351
352 static void webkit_web_view_set_scroll_adjustments(WebKitWebView* webView, GtkAdjustment* hadj, GtkAdjustment* vadj)
353 {
354     FrameView* view = core(webkit_web_view_get_main_frame(webView))->view();
355     view->setGtkAdjustments(hadj, vadj);
356 }
357
358 static void webkit_web_view_container_add(GtkContainer* container, GtkWidget* widget)
359 {
360     WebKitWebView* webView = WEBKIT_WEB_VIEW(container);
361     WebKitWebViewPrivate* private_data = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
362
363     private_data->children.add(widget);
364     if (GTK_WIDGET_REALIZED(container))
365         gtk_widget_set_parent_window(widget, GTK_WIDGET(webView)->window);
366     gtk_widget_set_parent(widget, GTK_WIDGET(container));
367 }
368
369 static void webkit_web_view_container_remove(GtkContainer* container, GtkWidget* widget)
370 {
371     WebKitWebViewPrivate* private_data = WEBKIT_WEB_VIEW_GET_PRIVATE(WEBKIT_WEB_VIEW(container));
372
373     if (private_data->children.contains(widget)) {
374         gtk_widget_unparent(widget);
375         private_data->children.remove(widget);
376     }
377 }
378
379 static void webkit_web_view_container_forall(GtkContainer* container, gboolean, GtkCallback callback, gpointer callbackData)
380 {
381     WebKitWebViewPrivate* privateData = WEBKIT_WEB_VIEW_GET_PRIVATE(WEBKIT_WEB_VIEW(container));
382
383     HashSet<GtkWidget*> children = privateData->children;
384     HashSet<GtkWidget*>::const_iterator end = children.end();
385     for (HashSet<GtkWidget*>::const_iterator current = children.begin(); current != end; ++current)
386         (*callback)(*current, callbackData);
387 }
388
389 static WebKitWebView* webkit_web_view_real_create_web_view(WebKitWebView*)
390 {
391     notImplemented();
392     return 0;
393 }
394
395 static WebKitNavigationResponse webkit_web_view_real_navigation_requested(WebKitWebView*, WebKitWebFrame* frame, WebKitNetworkRequest*)
396 {
397     notImplemented();
398     return WEBKIT_NAVIGATION_RESPONSE_ACCEPT;
399 }
400
401 static void webkit_web_view_real_window_object_cleared(WebKitWebView*, WebKitWebFrame*, JSGlobalContextRef context, JSObjectRef window_object)
402 {
403     notImplemented();
404 }
405
406 static gchar* webkit_web_view_real_choose_file(WebKitWebView*, WebKitWebFrame*, const gchar* old_name)
407 {
408     notImplemented();
409     return g_strdup(old_name);
410 }
411
412 typedef enum {
413     WEBKIT_SCRIPT_DIALOG_ALERT,
414     WEBKIT_SCRIPT_DIALOG_CONFIRM,
415     WEBKIT_SCRIPT_DIALOG_PROMPT
416  } WebKitScriptDialogType;
417
418 static gboolean webkit_web_view_script_dialog(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, WebKitScriptDialogType type, const gchar* defaultValue, gchar** value)
419 {
420     GtkMessageType messageType;
421     GtkButtonsType buttons;
422     gint defaultResponse;
423     GtkWidget* window;
424     GtkWidget* dialog;
425     GtkWidget* entry = 0;
426     gboolean didConfirm = FALSE;
427
428     switch (type) {
429     case WEBKIT_SCRIPT_DIALOG_ALERT:
430         messageType = GTK_MESSAGE_WARNING;
431         buttons = GTK_BUTTONS_CLOSE;
432         defaultResponse = GTK_RESPONSE_CLOSE;
433         break;
434     case WEBKIT_SCRIPT_DIALOG_CONFIRM:
435         messageType = GTK_MESSAGE_QUESTION;
436         buttons = GTK_BUTTONS_YES_NO;
437         defaultResponse = GTK_RESPONSE_YES;
438         break;
439     case WEBKIT_SCRIPT_DIALOG_PROMPT:
440         messageType = GTK_MESSAGE_QUESTION;
441         buttons = GTK_BUTTONS_OK_CANCEL;
442         defaultResponse = GTK_RESPONSE_OK;
443         break;
444     default:
445         g_warning("Unknown value for WebKitScriptDialogType.");
446         return FALSE;
447     }
448
449     window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
450     dialog = gtk_message_dialog_new(GTK_WIDGET_TOPLEVEL(window) ? GTK_WINDOW(window) : 0, GTK_DIALOG_DESTROY_WITH_PARENT, messageType, buttons, "%s", message);
451     gchar* title = g_strconcat("JavaScript - ", webkit_web_frame_get_uri(frame), NULL);
452     gtk_window_set_title(GTK_WINDOW(dialog), title);
453     g_free(title);
454
455     if (type == WEBKIT_SCRIPT_DIALOG_PROMPT) {
456         entry = gtk_entry_new();
457         gtk_entry_set_text(GTK_ENTRY(entry), defaultValue);
458         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), entry);
459         gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
460         gtk_widget_show(entry);
461     }
462
463     gtk_dialog_set_default_response(GTK_DIALOG(dialog), defaultResponse);
464     gint response = gtk_dialog_run(GTK_DIALOG(dialog));
465
466     switch (response) {
467     case GTK_RESPONSE_YES:
468         didConfirm = TRUE;
469         break;
470     case GTK_RESPONSE_OK:
471         didConfirm = TRUE;
472         if (entry)
473             *value = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
474         else
475             *value = 0;
476         break;
477     case GTK_RESPONSE_NO:
478     case GTK_RESPONSE_CANCEL:
479         didConfirm = FALSE;
480         break;
481
482     }
483     gtk_widget_destroy(GTK_WIDGET(dialog));
484     return didConfirm;
485 }
486
487 static gboolean webkit_web_view_real_script_alert(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message)
488 {
489     webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_ALERT, 0, 0);
490     return TRUE;
491 }
492
493 static gboolean webkit_web_view_real_script_confirm(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, gboolean* didConfirm)
494 {
495     *didConfirm = webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_CONFIRM, 0, 0);
496     return TRUE;
497 }
498
499 static gboolean webkit_web_view_real_script_prompt(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, const gchar* defaultValue, gchar** value)
500 {
501     if (!webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_PROMPT, defaultValue, value))
502         *value = NULL;
503     return TRUE;
504 }
505
506 static gboolean webkit_web_view_real_console_message(WebKitWebView* webView, const gchar* message, unsigned int line, const gchar* sourceId)
507 {
508     g_print("console message: %s @%d: %s\n", sourceId, line, message);
509     return TRUE;
510 }
511
512 static void webkit_web_view_real_select_all(WebKitWebView* webView)
513 {
514     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
515     frame->editor()->command("SelectAll").execute();
516 }
517
518 static void webkit_web_view_real_cut_clipboard(WebKitWebView* webView)
519 {
520     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
521     frame->editor()->command("Cut").execute();
522 }
523
524 static void webkit_web_view_real_copy_clipboard(WebKitWebView* webView)
525 {
526     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
527     frame->editor()->command("Copy").execute();
528 }
529
530 static void webkit_web_view_real_paste_clipboard(WebKitWebView* webView)
531 {
532     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
533     frame->editor()->command("Paste").execute();
534 }
535
536 static void webkit_web_view_finalize(GObject* object)
537 {
538     webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(object));
539
540     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(WEBKIT_WEB_VIEW(object));
541     delete webViewData->corePage;
542     delete webViewData->settings;
543     g_object_unref(webViewData->mainFrame);
544     g_object_unref(webViewData->imContext);
545     gtk_target_list_unref(webViewData->copy_target_list);
546     gtk_target_list_unref(webViewData->paste_target_list);
547     delete webViewData->userAgent;
548
549     G_OBJECT_CLASS(webkit_web_view_parent_class)->finalize(object);
550 }
551
552 static void webkit_web_view_class_init(WebKitWebViewClass* webViewClass)
553 {
554     GtkBindingSet* binding_set;
555
556     webkit_init();
557
558     g_type_class_add_private(webViewClass, sizeof(WebKitWebViewPrivate));
559
560     /*
561      * Signals
562      */
563
564     webkit_web_view_signals[NAVIGATION_REQUESTED] = g_signal_new("navigation-requested",
565             G_TYPE_FROM_CLASS(webViewClass),
566             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
567             G_STRUCT_OFFSET (WebKitWebViewClass, navigation_requested),
568             NULL,
569             NULL,
570             webkit_marshal_INT__OBJECT_OBJECT,
571             G_TYPE_INT, 2,
572             G_TYPE_OBJECT,
573             G_TYPE_OBJECT);
574
575     /**
576      * WebKitWebView::window-object-cleared:
577      * @web_view: the object on which the signal is emitted
578      * @frame: the #WebKitWebFrame to which @window_object belongs
579      * @context: the #JSGlobalContextRef holding the global object and other
580      * execution state; equivalent to the return value of
581      * webkit_web_frame_get_global_context(@frame)
582      *
583      * @window_object: the #JSObjectRef representing the frame's JavaScript
584      * window object
585      *
586      * Emitted when the JavaScript window object in a #WebKitWebFrame has been
587      * cleared in preparation for a new load. This is the preferred place to
588      * set custom properties on the window object using the JavaScriptCore API.
589      */
590     webkit_web_view_signals[WINDOW_OBJECT_CLEARED] = g_signal_new("window-object-cleared",
591             G_TYPE_FROM_CLASS(webViewClass),
592             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
593             G_STRUCT_OFFSET (WebKitWebViewClass, window_object_cleared),
594             NULL,
595             NULL,
596             webkit_marshal_VOID__OBJECT_POINTER_POINTER,
597             G_TYPE_NONE, 3,
598             WEBKIT_TYPE_WEB_FRAME,
599             G_TYPE_POINTER,
600             G_TYPE_POINTER);
601
602     /**
603      * WebKitWebView::load-started:
604      * @web_view: the object on which the signal is emitted
605      * @frame: the frame going to do the load
606      *
607      * When a #WebKitWebFrame begins to load this signal is emitted.
608      */
609     webkit_web_view_signals[LOAD_STARTED] = g_signal_new("load-started",
610             G_TYPE_FROM_CLASS(webViewClass),
611             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
612             0,
613             NULL,
614             NULL,
615             g_cclosure_marshal_VOID__OBJECT,
616             G_TYPE_NONE, 1,
617             WEBKIT_TYPE_WEB_FRAME);
618
619     /**
620      * WebKitWebView::load-committed:
621      * @web_view: the object on which the signal is emitted
622      * @frame: the main frame that received the first data
623      *
624      * When a #WebKitWebFrame loaded the first data this signal is emitted.
625      */
626     webkit_web_view_signals[LOAD_COMMITTED] = g_signal_new("load-committed",
627             G_TYPE_FROM_CLASS(webViewClass),
628             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
629             0,
630             NULL,
631             NULL,
632             g_cclosure_marshal_VOID__OBJECT,
633             G_TYPE_NONE, 1,
634             WEBKIT_TYPE_WEB_FRAME);
635
636
637     /**
638      * WebKitWebView::load-progress-changed:
639      * @web_view: the #WebKitWebView
640      * @progress: the global progress
641      */
642     webkit_web_view_signals[LOAD_PROGRESS_CHANGED] = g_signal_new("load-progress-changed",
643             G_TYPE_FROM_CLASS(webViewClass),
644             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
645             0,
646             NULL,
647             NULL,
648             g_cclosure_marshal_VOID__INT,
649             G_TYPE_NONE, 1,
650             G_TYPE_INT);
651
652     webkit_web_view_signals[LOAD_FINISHED] = g_signal_new("load-finished",
653             G_TYPE_FROM_CLASS(webViewClass),
654             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
655             0,
656             NULL,
657             NULL,
658             g_cclosure_marshal_VOID__OBJECT,
659             G_TYPE_NONE, 1,
660             WEBKIT_TYPE_WEB_FRAME);
661
662     /**
663      * WebKitWebView::title-changed:
664      * @web_view: the object on which the signal is emitted
665      * @frame: the main frame
666      * @title: the new title
667      *
668      * When a #WebKitWebFrame changes the document title this signal is emitted.
669      */
670     webkit_web_view_signals[TITLE_CHANGED] = g_signal_new("title-changed",
671             G_TYPE_FROM_CLASS(webViewClass),
672             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
673             0,
674             NULL,
675             NULL,
676             webkit_marshal_VOID__OBJECT_STRING,
677             G_TYPE_NONE, 2,
678             WEBKIT_TYPE_WEB_FRAME,
679             G_TYPE_STRING);
680
681     webkit_web_view_signals[HOVERING_OVER_LINK] = g_signal_new("hovering-over-link",
682             G_TYPE_FROM_CLASS(webViewClass),
683             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
684             0,
685             NULL,
686             NULL,
687             webkit_marshal_VOID__STRING_STRING,
688             G_TYPE_NONE, 2,
689             G_TYPE_STRING,
690             G_TYPE_STRING);
691
692     webkit_web_view_signals[STATUS_BAR_TEXT_CHANGED] = g_signal_new("status-bar-text-changed",
693             G_TYPE_FROM_CLASS(webViewClass),
694             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
695             0,
696             NULL,
697             NULL,
698             g_cclosure_marshal_VOID__STRING,
699             G_TYPE_NONE, 1,
700             G_TYPE_STRING);
701
702     webkit_web_view_signals[ICOND_LOADED] = g_signal_new("icon-loaded",
703             G_TYPE_FROM_CLASS(webViewClass),
704             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
705             0,
706             NULL,
707             NULL,
708             g_cclosure_marshal_VOID__VOID,
709             G_TYPE_NONE, 0);
710
711     webkit_web_view_signals[SELECTION_CHANGED] = g_signal_new("selection-changed",
712             G_TYPE_FROM_CLASS(webViewClass),
713             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
714             0,
715             NULL,
716             NULL,
717             g_cclosure_marshal_VOID__VOID,
718             G_TYPE_NONE, 0);
719
720     /**
721      * WebKitWebView::console-message:
722      * @web_view: the object on which the signal is emitted
723      * @message: the message text
724      * @line: the line where the error occured
725      * @source_id: the source id
726      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
727      *
728      * A JavaScript console message was created.
729      */
730     webkit_web_view_signals[CONSOLE_MESSAGE] = g_signal_new("console-message",
731             G_TYPE_FROM_CLASS(webViewClass),
732             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
733             G_STRUCT_OFFSET(WebKitWebViewClass, console_message),
734             g_signal_accumulator_true_handled,
735             NULL,
736             webkit_marshal_BOOLEAN__STRING_INT_STRING,
737             G_TYPE_BOOLEAN, 3,
738             G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
739
740     /**
741      * WebKitWebView::script-alert:
742      * @web_view: the object on which the signal is emitted
743      * @frame: the relevant frame
744      * @message: the message text
745      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
746      *
747      * A JavaScript alert dialog was created.
748      */
749     webkit_web_view_signals[SCRIPT_ALERT] = g_signal_new("script-alert",
750             G_TYPE_FROM_CLASS(webViewClass),
751             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
752             G_STRUCT_OFFSET(WebKitWebViewClass, script_alert),
753             g_signal_accumulator_true_handled,
754             NULL,
755             webkit_marshal_BOOLEAN__OBJECT_STRING,
756             G_TYPE_BOOLEAN, 2,
757             G_TYPE_OBJECT, G_TYPE_STRING);
758
759     /**
760      * WebKitWebView::script-confirm:
761      * @web_view: the object on which the signal is emitted
762      * @frame: the relevant frame
763      * @message: the message text
764      * @confirmed: whether the dialog has been confirmed
765      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
766      *
767      * A JavaScript confirm dialog was created, providing Yes and No buttons.
768      */
769     webkit_web_view_signals[SCRIPT_CONFIRM] = g_signal_new("script-confirm",
770             G_TYPE_FROM_CLASS(webViewClass),
771             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
772             G_STRUCT_OFFSET(WebKitWebViewClass, script_confirm),
773             g_signal_accumulator_true_handled,
774             NULL,
775             webkit_marshal_BOOLEAN__OBJECT_STRING_BOOLEAN,
776             G_TYPE_BOOLEAN, 3,
777             G_TYPE_OBJECT, G_TYPE_STRING, G_TYPE_BOOLEAN);
778
779     /**
780      * WebKitWebView::script-prompt:
781      * @web_view: the object on which the signal is emitted
782      * @frame: the relevant frame
783      * @message: the message text
784      * @default: the default value
785      * @text: To be filled with the return value or NULL if the dialog was cancelled.
786      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
787      *
788      * A JavaScript prompt dialog was created, providing an entry to input text.
789      */
790     webkit_web_view_signals[SCRIPT_PROMPT] = g_signal_new("script-prompt",
791             G_TYPE_FROM_CLASS(webViewClass),
792             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
793             G_STRUCT_OFFSET(WebKitWebViewClass, script_prompt),
794             g_signal_accumulator_true_handled,
795             NULL,
796             webkit_marshal_BOOLEAN__OBJECT_STRING_STRING_STRING,
797             G_TYPE_BOOLEAN, 4,
798             G_TYPE_OBJECT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
799
800     /**
801      * WebKitWebView::select-all:
802      * @web_view: the object which received the signal
803      *
804      * The ::select-all signal is a keybinding signal which gets emitted to
805      * select the complete contents of the text view.
806      *
807      * The default bindings for this signal is Ctrl-a.
808      */
809     webkit_web_view_signals[SELECT_ALL] = g_signal_new("select-all",
810             G_TYPE_FROM_CLASS(webViewClass),
811             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
812             G_STRUCT_OFFSET(WebKitWebViewClass, select_all),
813             NULL, NULL,
814             g_cclosure_marshal_VOID__VOID,
815             G_TYPE_NONE, 0);
816
817     /**
818      * WebKitWebView::cut-clipboard:
819      * @web_view: the object which received the signal
820      *
821      * The ::cut-clipboard signal is a keybinding signal which gets emitted to
822      * cut the selection to the clipboard.
823      *
824      * The default bindings for this signal are Ctrl-x and Shift-Delete.
825      */
826     webkit_web_view_signals[CUT_CLIPBOARD] = g_signal_new("cut-clipboard",
827             G_TYPE_FROM_CLASS(webViewClass),
828             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
829             G_STRUCT_OFFSET(WebKitWebViewClass, cut_clipboard),
830             NULL, NULL,
831             g_cclosure_marshal_VOID__VOID,
832             G_TYPE_NONE, 0);
833
834     /**
835      * WebKitWebView::copy-clipboard:
836      * @web_view: the object which received the signal
837      *
838      * The ::copy-clipboard signal is a keybinding signal which gets emitted to
839      * copy the selection to the clipboard.
840      *
841      * The default bindings for this signal are Ctrl-c and Ctrl-Insert.
842      */
843     webkit_web_view_signals[COPY_CLIPBOARD] = g_signal_new("copy-clipboard",
844             G_TYPE_FROM_CLASS(webViewClass),
845             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
846             G_STRUCT_OFFSET(WebKitWebViewClass, copy_clipboard),
847             NULL, NULL,
848             g_cclosure_marshal_VOID__VOID,
849             G_TYPE_NONE, 0);
850
851     /**
852      * WebKitWebView::paste-clipboard:
853      * @web_view: the object which received the signal
854      *
855      * The ::paste-clipboard signal is a keybinding signal which gets emitted to
856      * paste the contents of the clipboard into the Web view.
857      *
858      * The default bindings for this signal are Ctrl-v and Shift-Insert.
859      */
860     webkit_web_view_signals[PASTE_CLIPBOARD] = g_signal_new("paste-clipboard",
861             G_TYPE_FROM_CLASS(webViewClass),
862             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
863             G_STRUCT_OFFSET(WebKitWebViewClass, paste_clipboard),
864             NULL, NULL,
865             g_cclosure_marshal_VOID__VOID,
866             G_TYPE_NONE, 0);
867
868     /*
869      * implementations of virtual methods
870      */
871     webViewClass->create_web_view = webkit_web_view_real_create_web_view;
872     webViewClass->navigation_requested = webkit_web_view_real_navigation_requested;
873     webViewClass->window_object_cleared = webkit_web_view_real_window_object_cleared;
874     webViewClass->choose_file = webkit_web_view_real_choose_file;
875     webViewClass->script_alert = webkit_web_view_real_script_alert;
876     webViewClass->script_confirm = webkit_web_view_real_script_confirm;
877     webViewClass->script_prompt = webkit_web_view_real_script_prompt;
878     webViewClass->console_message = webkit_web_view_real_console_message;
879     webViewClass->select_all = webkit_web_view_real_select_all;
880     webViewClass->cut_clipboard = webkit_web_view_real_cut_clipboard;
881     webViewClass->copy_clipboard = webkit_web_view_real_copy_clipboard;
882     webViewClass->paste_clipboard = webkit_web_view_real_paste_clipboard;
883
884     G_OBJECT_CLASS(webViewClass)->finalize = webkit_web_view_finalize;
885
886     GObjectClass* objectClass = G_OBJECT_CLASS(webViewClass);
887     objectClass->get_property = webkit_web_view_get_property;
888
889     GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(webViewClass);
890     widgetClass->realize = webkit_web_view_realize;
891     widgetClass->expose_event = webkit_web_view_expose_event;
892     widgetClass->key_press_event = webkit_web_view_key_press_event;
893     widgetClass->key_release_event = webkit_web_view_key_release_event;
894     widgetClass->button_press_event = webkit_web_view_button_press_event;
895     widgetClass->button_release_event = webkit_web_view_button_release_event;
896     widgetClass->motion_notify_event = webkit_web_view_motion_event;
897     widgetClass->scroll_event = webkit_web_view_scroll_event;
898     widgetClass->size_allocate = webkit_web_view_size_allocate;
899     widgetClass->popup_menu = webkit_web_view_popup_menu_handler;
900
901     GtkContainerClass* containerClass = GTK_CONTAINER_CLASS(webViewClass);
902     containerClass->add = webkit_web_view_container_add;
903     containerClass->remove = webkit_web_view_container_remove;
904     containerClass->forall = webkit_web_view_container_forall;
905
906     /*
907      * make us scrollable (e.g. addable to a GtkScrolledWindow)
908      */
909     webViewClass->set_scroll_adjustments = webkit_web_view_set_scroll_adjustments;
910     GTK_WIDGET_CLASS(webViewClass)->set_scroll_adjustments_signal = g_signal_new("set-scroll-adjustments",
911             G_TYPE_FROM_CLASS(webViewClass),
912             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
913             G_STRUCT_OFFSET(WebKitWebViewClass, set_scroll_adjustments),
914             NULL, NULL,
915             webkit_marshal_VOID__OBJECT_OBJECT,
916             G_TYPE_NONE, 2,
917             GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
918
919     /*
920      * Key bindings
921      */
922
923     binding_set = gtk_binding_set_by_class(webViewClass);
924
925     gtk_binding_entry_add_signal(binding_set, GDK_a, GDK_CONTROL_MASK,
926                                  "select_all", 0);
927
928     /* Cut/copy/paste */
929
930     gtk_binding_entry_add_signal(binding_set, GDK_x, GDK_CONTROL_MASK,
931                                  "cut_clipboard", 0);
932     gtk_binding_entry_add_signal(binding_set, GDK_c, GDK_CONTROL_MASK,
933                                  "copy_clipboard", 0);
934     gtk_binding_entry_add_signal(binding_set, GDK_v, GDK_CONTROL_MASK,
935                                  "paste_clipboard", 0);
936
937     gtk_binding_entry_add_signal(binding_set, GDK_Delete, GDK_SHIFT_MASK,
938                                  "cut_clipboard", 0);
939     gtk_binding_entry_add_signal(binding_set, GDK_Insert, GDK_CONTROL_MASK,
940                                  "copy_clipboard", 0);
941     gtk_binding_entry_add_signal(binding_set, GDK_Insert, GDK_SHIFT_MASK,
942                                  "paste_clipboard", 0);
943
944     /* Properties */
945     GParamFlags flags = (GParamFlags)(G_PARAM_READABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB);
946     g_object_class_install_property(objectClass, PROP_COPY_TARGET_LIST,
947                                     g_param_spec_boxed("copy-target-list",
948                                                        "Target list",
949                                                        "The list of targets this Web view supports for copying to the clipboard",
950                                                        GTK_TYPE_TARGET_LIST,
951                                                        flags));
952
953     g_object_class_install_property(objectClass, PROP_PASTE_TARGET_LIST,
954                                     g_param_spec_boxed("paste-target-list",
955                                                        "Target list",
956                                                        "The list of targets this Web view supports for pasting to the clipboard",
957                                                        GTK_TYPE_TARGET_LIST,
958                                                        flags));
959 }
960
961 static void webkit_web_view_init(WebKitWebView* webView)
962 {
963     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(WEBKIT_WEB_VIEW(webView));
964     webViewData->imContext = gtk_im_multicontext_new();
965     webViewData->corePage = new Page(new WebKit::ChromeClient(webView), new WebKit::ContextMenuClient, new WebKit::EditorClient(webView), new WebKit::DragClient, new WebKit::InspectorClient);
966
967     Settings* settings = webViewData->corePage->settings();
968     settings->setLoadsImagesAutomatically(true);
969     settings->setMinimumFontSize(5);
970     settings->setDOMPasteAllowed(true);
971     settings->setMinimumLogicalFontSize(5);
972     settings->setShouldPrintBackgrounds(true);
973     settings->setJavaScriptEnabled(true);
974     settings->setDefaultFixedFontSize(14);
975     settings->setDefaultFontSize(14);
976     settings->setSerifFontFamily("Times New Roman");
977     settings->setSansSerifFontFamily("Arial");
978     settings->setFixedFontFamily("Courier New");
979     settings->setStandardFontFamily("Arial");
980
981     GTK_WIDGET_SET_FLAGS(webView, GTK_CAN_FOCUS);
982     webViewData->mainFrame = WEBKIT_WEB_FRAME(webkit_web_frame_new(webView));
983     webViewData->lastPopupXPosition = webViewData->lastPopupYPosition = -1;
984     webViewData->editable = false;
985
986 #if GTK_CHECK_VERSION(2,10,0)
987     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
988 #else
989     GdkAtom textHtml = gdk_atom_intern("text/html", false);
990 #endif
991     /* Targets for copy */
992     webViewData->copy_target_list = gtk_target_list_new(NULL, 0);
993     gtk_target_list_add(webViewData->copy_target_list, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
994     gtk_target_list_add_text_targets(webViewData->copy_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
995
996     /* Targets for pasting */
997     webViewData->paste_target_list = gtk_target_list_new(NULL, 0);
998     gtk_target_list_add(webViewData->paste_target_list, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
999     gtk_target_list_add_text_targets(webViewData->paste_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
1000
1001 }
1002
1003 GtkWidget* webkit_web_view_new(void)
1004 {
1005     WebKitWebView* webView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW, NULL));
1006
1007     return GTK_WIDGET(webView);
1008 }
1009
1010 void webkit_web_view_set_settings(WebKitWebView* webView, WebKitSettings* settings)
1011 {
1012     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1013     g_return_if_fail(settings);
1014
1015     notImplemented();
1016 }
1017
1018 WebKitSettings* webkit_web_view_get_settings(WebKitWebView* webView)
1019 {
1020     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1021
1022     notImplemented();
1023     return NULL;
1024 }
1025
1026 void webkit_web_view_go_backward(WebKitWebView* webView)
1027 {
1028     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1029
1030     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1031     frame->loader()->goBackOrForward(-1);
1032 }
1033
1034 void webkit_web_view_go_forward(WebKitWebView* webView)
1035 {
1036     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1037
1038     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1039     frame->loader()->goBackOrForward(1);
1040 }
1041
1042 gboolean webkit_web_view_can_go_backward(WebKitWebView* webView)
1043 {
1044     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1045
1046     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1047     return frame->loader()->canGoBackOrForward(-1);
1048 }
1049
1050 gboolean webkit_web_view_can_go_forward(WebKitWebView* webView)
1051 {
1052     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1053
1054     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1055     return frame->loader()->canGoBackOrForward(1);
1056 }
1057
1058 void webkit_web_view_open(WebKitWebView* webView, const gchar* uri)
1059 {
1060     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1061     g_return_if_fail(uri);
1062
1063     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1064     DeprecatedString string = DeprecatedString::fromUtf8(uri);
1065     frame->loader()->load(ResourceRequest(KURL(string)));
1066 }
1067
1068 void webkit_web_view_reload(WebKitWebView* webView)
1069 {
1070     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1071
1072     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1073     frame->loader()->reload();
1074 }
1075
1076 void webkit_web_view_load_string(WebKitWebView* webView, const gchar* content, const gchar* contentMimeType, const gchar* contentEncoding, const gchar* baseUri)
1077 {
1078     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1079     g_return_if_fail(content);
1080
1081     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1082
1083     KURL url(baseUri ? DeprecatedString::fromUtf8(baseUri) : "");
1084     RefPtr<SharedBuffer> sharedBuffer = new SharedBuffer(strdup(content), strlen(content));
1085     SubstituteData substituteData(sharedBuffer.release(), contentMimeType ? String(contentMimeType) : "text/html", contentEncoding ? String(contentEncoding) : "UTF-8", KURL("about:blank"), url);
1086
1087     frame->loader()->load(ResourceRequest(url), substituteData);
1088 }
1089
1090 void webkit_web_view_load_html_string(WebKitWebView* webView, const gchar* content, const gchar* baseUri)
1091 {
1092     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1093     g_return_if_fail(content);
1094
1095     webkit_web_view_load_string(webView, content, NULL, NULL, baseUri);
1096 }
1097
1098 void webkit_web_view_stop_loading(WebKitWebView* webView)
1099 {
1100     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1101
1102     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1103
1104     if (FrameLoader* loader = frame->loader())
1105         loader->stopAllLoaders();
1106 }
1107
1108 /**
1109  * webkit_web_view_search_text:
1110  * @web_view: a #WebKitWebView
1111  * @text: a string to look for
1112  * @forward: whether to find forward or not
1113  * @case_sensitive: whether to respect the case of text
1114  * @wrap: whether to continue looking at the beginning after reaching the end
1115  *
1116  * Looks for a specified string inside #web_view.
1117  *
1118  * Return value: %TRUE on success or %FALSE on failure
1119  */
1120 gboolean webkit_web_view_search_text(WebKitWebView* webView, const gchar* string, gboolean caseSensitive, gboolean forward, gboolean shouldWrap)
1121 {
1122     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1123     g_return_val_if_fail(string, FALSE);
1124
1125     TextCaseSensitivity caseSensitivity = caseSensitive ? TextCaseSensitive : TextCaseInsensitive;
1126     FindDirection direction = forward ? FindDirectionForward : FindDirectionBackward;
1127
1128     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1129     return webViewData->corePage->findString(String::fromUTF8(string), caseSensitivity, direction, shouldWrap);
1130 }
1131
1132 /**
1133  * webkit_web_view_mark_text_matches:
1134  * @web_view: a #WebKitWebView
1135  * @string: a string to look for
1136  * @case_sensitive: whether to respect the case of text
1137  * @limit: the maximum number of strings to look for or %0 for all
1138  *
1139  * Attempts to highlight all occurances of #string inside #web_view.
1140  *
1141  * Return value: the number of strings highlighted
1142  */
1143 guint webkit_web_view_mark_text_matches(WebKitWebView* webView, const gchar* string, gboolean caseSensitive, guint limit)
1144 {
1145     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
1146     g_return_val_if_fail(string, 0);
1147
1148     TextCaseSensitivity caseSensitivity = caseSensitive ? TextCaseSensitive : TextCaseInsensitive;
1149
1150     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1151     return webViewData->corePage->markAllMatchesForText(String::fromUTF8(string), caseSensitivity, false, limit);
1152 }
1153
1154 /**
1155  * webkit_web_view_set_highlight_text_matches:
1156  * @web_view: a #WebKitWebView
1157  * @highlight: whether to highlight text matches
1158  *
1159  * Highlights text matches previously marked by webkit_web_view_mark_text_matches.
1160  */
1161 void webkit_web_view_set_highlight_text_matches(WebKitWebView* webView, gboolean shouldHighlight)
1162 {
1163     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1164
1165     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1166     WebKitWebFramePrivate* frameData = WEBKIT_WEB_FRAME_GET_PRIVATE(webViewData->mainFrame);
1167     frameData->frame->setMarkedTextMatchesAreHighlighted(shouldHighlight);
1168 }
1169
1170 /**
1171  * webkit_web_view_unmark_text_matches:
1172  * @web_view: a #WebKitWebView
1173  *
1174  * Removes highlighting previously set by webkit_web_view_mark_text_matches.
1175  */
1176 void webkit_web_view_unmark_text_matches(WebKitWebView* webView)
1177 {
1178     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1179
1180     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1181     return webViewData->corePage->unmarkAllTextMatches();
1182 }
1183
1184 WebKitWebFrame* webkit_web_view_get_main_frame(WebKitWebView* webView)
1185 {
1186     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1187
1188     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1189     return webViewData->mainFrame;
1190 }
1191
1192 void webkit_web_view_execute_script(WebKitWebView* webView, const gchar* script)
1193 {
1194     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1195     g_return_if_fail(script);
1196
1197     Frame* frame = core(webkit_web_view_get_main_frame(webView));
1198     if (FrameLoader* loader = frame->loader())
1199         loader->executeScript(String::fromUTF8(script), true);
1200 }
1201
1202 /**
1203  * webkit_web_view_cut_clipboard:
1204  * @web_view: a #WebKitWebView
1205  *
1206  * Determines whether or not it is currently possible to cut to the clipboard.
1207  *
1208  * Return value: %TRUE if a selection can be cut, %FALSE if not
1209  */
1210 gboolean webkit_web_view_can_cut_clipboard(WebKitWebView* webView)
1211 {
1212     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1213
1214     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1215     return frame->editor()->canCut() || frame->editor()->canDHTMLCut();
1216 }
1217
1218 /**
1219  * webkit_web_view_copy_clipboard:
1220  * @web_view: a #WebKitWebView
1221  *
1222  * Determines whether or not it is currently possible to copy to the clipboard.
1223  *
1224  * Return value: %TRUE if a selection can be copied, %FALSE if not
1225  */
1226 gboolean webkit_web_view_can_copy_clipboard(WebKitWebView* webView)
1227 {
1228     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1229
1230     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1231     return frame->editor()->canCopy() || frame->editor()->canDHTMLCopy();
1232 }
1233
1234 /**
1235  * webkit_web_view_paste_clipboard:
1236  * @web_view: a #WebKitWebView
1237  *
1238  * Determines whether or not it is currently possible to paste from the clipboard.
1239  *
1240  * Return value: %TRUE if a selection can be pasted, %FALSE if not
1241  */
1242 gboolean webkit_web_view_can_paste_clipboard(WebKitWebView* webView)
1243 {
1244     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1245
1246     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1247     return frame->editor()->canPaste() || frame->editor()->canDHTMLPaste();
1248 }
1249
1250 /**
1251  * webkit_web_view_cut_clipboard:
1252  * @web_view: a #WebKitWebView
1253  *
1254  * Cuts the current selection inside the @web_view to the clipboard.
1255  */
1256 void webkit_web_view_cut_clipboard(WebKitWebView* webView)
1257 {
1258     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1259
1260     if (webkit_web_view_can_cut_clipboard(webView))
1261         g_signal_emit(webView, webkit_web_view_signals[CUT_CLIPBOARD], 0);
1262 }
1263
1264 /**
1265  * webkit_web_view_copy_clipboard:
1266  * @web_view: a #WebKitWebView
1267  *
1268  * Copies the current selection inside the @web_view to the clipboard.
1269  */
1270 void webkit_web_view_copy_clipboard(WebKitWebView* webView)
1271 {
1272     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1273
1274     if (webkit_web_view_can_copy_clipboard(webView))
1275         g_signal_emit(webView, webkit_web_view_signals[COPY_CLIPBOARD], 0);
1276 }
1277
1278 /**
1279  * webkit_web_view_paste_clipboard:
1280  * @web_view: a #WebKitWebView
1281  *
1282  * Pastes the current contents of the clipboard to the @web_view.
1283  */
1284 void webkit_web_view_paste_clipboard(WebKitWebView* webView)
1285 {
1286     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1287
1288     if (webkit_web_view_can_paste_clipboard(webView))
1289         g_signal_emit(webView, webkit_web_view_signals[PASTE_CLIPBOARD], 0);
1290 }
1291
1292 /**
1293  * webkit_web_view_delete_selection:
1294  * @web_view: a #WebKitWebView
1295  *
1296  * Deletes the current selection inside the @web_view.
1297  */
1298 void webkit_web_view_delete_selection(WebKitWebView* webView)
1299 {
1300     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1301
1302     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1303     frame->editor()->performDelete();
1304 }
1305
1306 /**
1307  * webkit_web_view_has_selection:
1308  * @web_view: a #WebKitWebView
1309  *
1310  * Determines whether text was selected.
1311  *
1312  * Return value: %TRUE if there is selected text, %FALSE if not
1313  */
1314 gboolean webkit_web_view_has_selection(WebKitWebView* webView)
1315 {
1316     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1317
1318     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1319     return !webViewData->corePage->selection().isNone();
1320 }
1321
1322 /**
1323  * webkit_web_view_get_selected_text:
1324  * @web_view: a #WebKitWebView
1325  *
1326  * Retrieves the selected text if any.
1327  *
1328  * Return value: a newly allocated string with the selection or %NULL
1329  */
1330 gchar* webkit_web_view_get_selected_text(WebKitWebView* webView)
1331 {
1332     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
1333
1334     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1335     return g_strdup(frame->selectedText().utf8().data());
1336 }
1337
1338 /**
1339  * webkit_web_view_select_all:
1340  * @web_view: a #WebKitWebView
1341  *
1342  * Attempts to select everything inside the @web_view.
1343  */
1344 void webkit_web_view_select_all(WebKitWebView* webView)
1345 {
1346     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1347
1348     g_signal_emit(webView, webkit_web_view_signals[SELECT_ALL], 0);
1349 }
1350
1351 /**
1352  * webkit_web_view_get_editable:
1353  * @web_view: a #WebKitWebView
1354  *
1355  * Returns whether the user is allowed to edit the document.
1356  *
1357  * Returns %TRUE if @web_view allows the user to edit the HTML document, %FALSE if
1358  * it doesn't. You can change @web_view's document programmatically regardless of
1359  * this setting.
1360  *
1361  * Return value: a #gboolean indicating the editable state
1362  */
1363 gboolean webkit_web_view_get_editable(WebKitWebView* webView)
1364 {
1365     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1366
1367     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1368     ASSERT(webViewData);
1369
1370     return webViewData->editable;
1371 }
1372
1373 /**
1374  * webkit_web_view_set_editable:
1375  * @web_view: a #WebKitWebView
1376  * @flag: a #gboolean indicating the editable state
1377  *
1378  * Sets whether @web_view allows the user to edit its HTML document.
1379  *
1380  * If @flag is %TRUE, @web_view allows the user to edit the document. If @flag is
1381  * %FALSE, an element in @web_view's document can only be edited if the
1382  * CONTENTEDITABLE attribute has been set on the element or one of its parent
1383  * elements. You can change @web_view's document programmatically regardless of
1384  * this setting. By default a #WebKitWebView is not editable.
1385
1386  * Normally, an HTML document is not editable unless the elements within the
1387  * document are editable. This function provides a low-level way to make the
1388  * contents of a #WebKitWebView editable without altering the document or DOM
1389  * structure.
1390  */
1391 void webkit_web_view_set_editable(WebKitWebView* webView, gboolean flag)
1392 {
1393     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1394     flag = flag != FALSE;
1395
1396     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1397     ASSERT(webViewData);
1398
1399     Frame* mainFrame = core(webViewData->mainFrame);
1400     g_return_if_fail(mainFrame);
1401
1402     // TODO: What happens when the frame is replaced?
1403     if (flag == webViewData->editable)
1404         return;
1405
1406     webViewData->editable = flag;
1407
1408     if (flag) {
1409         mainFrame->applyEditingStyleToBodyElement();
1410         // TODO: If the WebKitWebView is made editable and the selection is empty, set it to something.
1411         //if (!webkit_web_view_get_selected_dom_range(webView))
1412         //    mainFrame->setSelectionFromNone();
1413     } else
1414         mainFrame->removeEditingStyleFromBodyElement();
1415 }
1416
1417 /**
1418  * webkit_web_view_get_copy_target_list:
1419  * @web_view: a #WebKitWebView
1420  *
1421  * This function returns the list of targets this #WebKitWebView can
1422  * provide for clipboard copying and as DND source. The targets in the list are
1423  * added with %info values from the #WebKitWebViewTargetInfo enum,
1424  * using gtk_target_list_add() and
1425  * gtk_target_list_add_text_targets().
1426  *
1427  * Return value: the #GtkTargetList
1428  **/
1429 GtkTargetList* webkit_web_view_get_copy_target_list(WebKitWebView* webView)
1430 {
1431     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1432
1433     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1434     return webViewData->copy_target_list;
1435 }
1436
1437 /**
1438  * webkit_web_view_get_paste_target_list:
1439  * @web_view: a #WebKitWebView
1440  *
1441  * This function returns the list of targets this #WebKitWebView can
1442  * provide for clipboard pasting and as DND destination. The targets in the list are
1443  * added with %info values from the #WebKitWebViewTargetInfo enum,
1444  * using gtk_target_list_add() and
1445  * gtk_target_list_add_text_targets().
1446  *
1447  * Return value: the #GtkTargetList
1448  **/
1449 GtkTargetList* webkit_web_view_get_paste_target_list(WebKitWebView* webView)
1450 {
1451     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1452
1453     WebKitWebViewPrivate* webViewData = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1454     return webViewData->paste_target_list;
1455 }
1456
1457 }