2008-08-01 Jan Michael Alonzo <jmalonzo@webkit.org>
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebview.cpp
1 /*
2  *  Copyright (C) 2007 Holger Hans Peter Freyther
3  *  Copyright (C) 2007, 2008 Christian Dywan <christian@imendio.com>
4  *  Copyright (C) 2007 Xan Lopez <xan@gnome.org>
5  *  Copyright (C) 2007 Alp Toker <alp@atoker.com>
6  *  Copyright (C) 2008 Jan Alonzo <jmalonzo@unpluggable.com>
7  *  Copyright (C) 2008 Nuanti Ltd.
8  *  Copyright (C) 2008 Collabora Ltd.
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23  */
24
25 #include "config.h"
26
27 #include "webkitwebview.h"
28 #include "webkitmarshal.h"
29 #include "webkitprivate.h"
30 #include "webkitwebbackforwardlist.h"
31 #include "webkitwebhistoryitem.h"
32
33 #include "AXObjectCache.h"
34 #include "NotImplemented.h"
35 #include "BackForwardList.h"
36 #include "CString.h"
37 #include "ChromeClientGtk.h"
38 #include "ContextMenu.h"
39 #include "ContextMenuClientGtk.h"
40 #include "ContextMenuController.h"
41 #include "Cursor.h"
42 #include "Document.h"
43 #include "DragClientGtk.h"
44 #include "Editor.h"
45 #include "EditorClientGtk.h"
46 #include "EventHandler.h"
47 #include "FocusController.h"
48 #include "FrameLoaderTypes.h"
49 #include "HitTestRequest.h"
50 #include "HitTestResult.h"
51 #include "GraphicsContext.h"
52 #include "InspectorClientGtk.h"
53 #include "FrameLoader.h"
54 #include "FrameView.h"
55 #include "Editor.h"
56 #include "PasteboardHelper.h"
57 #include "PlatformKeyboardEvent.h"
58 #include "PlatformWheelEvent.h"
59 #include "SubstituteData.h"
60
61 #include <gdk/gdkkeysyms.h>
62
63 static const double defaultDPI = 96.0;
64
65 using namespace WebKit;
66 using namespace WebCore;
67
68 extern "C" {
69
70 enum {
71     /* normal signals */
72     NAVIGATION_REQUESTED,
73     WINDOW_OBJECT_CLEARED,
74     LOAD_STARTED,
75     LOAD_COMMITTED,
76     LOAD_PROGRESS_CHANGED,
77     LOAD_FINISHED,
78     TITLE_CHANGED,
79     HOVERING_OVER_LINK,
80     POPULATE_POPUP,
81     STATUS_BAR_TEXT_CHANGED,
82     ICOND_LOADED,
83     SELECTION_CHANGED,
84     CONSOLE_MESSAGE,
85     SCRIPT_ALERT,
86     SCRIPT_CONFIRM,
87     SCRIPT_PROMPT,
88     SELECT_ALL,
89     COPY_CLIPBOARD,
90     PASTE_CLIPBOARD,
91     CUT_CLIPBOARD,
92     LAST_SIGNAL
93 };
94
95 enum {
96     PROP_0,
97
98     PROP_COPY_TARGET_LIST,
99     PROP_PASTE_TARGET_LIST,
100     PROP_EDITABLE,
101     PROP_SETTINGS,
102     PROP_TRANSPARENT,
103     PROP_ZOOM_LEVEL,
104     PROP_FULL_CONTENT_ZOOM
105 };
106
107 static guint webkit_web_view_signals[LAST_SIGNAL] = { 0, };
108
109 G_DEFINE_TYPE(WebKitWebView, webkit_web_view, GTK_TYPE_CONTAINER)
110
111 static void webkit_web_view_settings_notify(WebKitWebSettings* webSettings, GParamSpec* pspec, WebKitWebView* webView);
112
113 static void webkit_web_view_context_menu_position_func(GtkMenu*, gint* x, gint* y, gboolean* pushIn, WebKitWebViewPrivate* data)
114 {
115     *pushIn = FALSE;
116     *x = data->lastPopupXPosition;
117     *y = data->lastPopupYPosition;
118 }
119
120 static gboolean webkit_web_view_forward_context_menu_event(WebKitWebView* webView, const PlatformMouseEvent& event)
121 {
122     Page* page = core(webView);
123     page->contextMenuController()->clearContextMenu();
124     Frame* focusedFrame = page->focusController()->focusedOrMainFrame();
125
126     if (!focusedFrame->view())
127         return FALSE;
128
129     focusedFrame->view()->setCursor(pointerCursor());
130     bool handledEvent = focusedFrame->eventHandler()->sendContextMenuEvent(event);
131     if (!handledEvent)
132         return FALSE;
133
134     ContextMenu* coreMenu = page->contextMenuController()->contextMenu();
135     if (!coreMenu)
136         return FALSE;
137
138     GtkMenu* menu = GTK_MENU(coreMenu->platformDescription());
139     if (!menu)
140         return FALSE;
141
142     g_signal_emit(webView, webkit_web_view_signals[POPULATE_POPUP], 0, menu);
143
144     GList* items = gtk_container_get_children(GTK_CONTAINER(menu));
145     bool empty = !g_list_nth(items, 0);
146     g_list_free(items);
147     if (empty)
148         return FALSE;
149
150     WebKitWebViewPrivate* priv = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
151     priv->lastPopupXPosition = event.globalX();
152     priv->lastPopupYPosition = event.globalY();
153     gtk_menu_popup(menu, NULL, NULL,
154                    reinterpret_cast<GtkMenuPositionFunc>(webkit_web_view_context_menu_position_func),
155                    priv, event.button() + 1, gtk_get_current_event_time());
156     return TRUE;
157 }
158
159 static gboolean webkit_web_view_popup_menu_handler(GtkWidget* widget)
160 {
161     static const int contextMenuMargin = 1;
162
163     // The context menu event was generated from the keyboard, so show the context menu by the current selection.
164     Page* page = core(WEBKIT_WEB_VIEW(widget));
165     FrameView* view = page->mainFrame()->view();
166     if (!view)
167         return FALSE;    
168
169     Position start = page->mainFrame()->selection()->selection().start();
170     Position end = page->mainFrame()->selection()->selection().end();
171
172     int rightAligned = FALSE;
173     IntPoint location;
174
175     if (!start.node() || !end.node())
176         location = IntPoint(rightAligned ? view->contentsWidth() - contextMenuMargin : contextMenuMargin, contextMenuMargin);
177     else {
178         RenderObject* renderer = start.node()->renderer();
179         if (!renderer)
180             return FALSE;
181
182         // Calculate the rect of the first line of the selection (cribbed from -[WebCoreFrameBridge firstRectForDOMRange:]).
183         int extraWidthToEndOfLine = 0;
184
185         InlineBox* startInlineBox;
186         int startCaretOffset;
187         start.getInlineBoxAndOffset(DOWNSTREAM, startInlineBox, startCaretOffset);
188         IntRect startCaretRect = renderer->caretRect(startInlineBox, startCaretOffset, &extraWidthToEndOfLine);
189
190         InlineBox* endInlineBox;
191         int endCaretOffset;
192         end.getInlineBoxAndOffset(UPSTREAM, endInlineBox, endCaretOffset);
193         IntRect endCaretRect = renderer->caretRect(endInlineBox, endCaretOffset);
194
195         IntRect firstRect;
196         if (startCaretRect.y() == endCaretRect.y())
197             firstRect = IntRect(MIN(startCaretRect.x(), endCaretRect.x()),
198                                 startCaretRect.y(),
199                                 abs(endCaretRect.x() - startCaretRect.x()),
200                                 MAX(startCaretRect.height(), endCaretRect.height()));
201         else
202             firstRect = IntRect(startCaretRect.x(),
203                                 startCaretRect.y(),
204                                 startCaretRect.width() + extraWidthToEndOfLine,
205                                 startCaretRect.height());
206
207         location = IntPoint(rightAligned ? firstRect.right() : firstRect.x(), firstRect.bottom());
208     }
209
210     int x, y;
211     gdk_window_get_origin(GTK_WIDGET(view->containingWindow())->window, &x, &y);
212
213     // FIXME: The IntSize(0, -1) is a hack to get the hit-testing to result in the selected element.
214     // Ideally we'd have the position of a context menu event be separate from its target node.
215     location = view->contentsToWindow(location) + IntSize(0, -1);
216     IntPoint global = location + IntSize(x, y);
217     PlatformMouseEvent event(location, global, NoButton, MouseEventPressed, 0, false, false, false, false, gtk_get_current_event_time());
218
219     return webkit_web_view_forward_context_menu_event(WEBKIT_WEB_VIEW(widget), event);
220 }
221
222 static void webkit_web_view_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
223 {
224     WebKitWebView* webView = WEBKIT_WEB_VIEW(object);
225
226     switch(prop_id) {
227     case PROP_COPY_TARGET_LIST:
228         g_value_set_boxed(value, webkit_web_view_get_copy_target_list(webView));
229         break;
230     case PROP_PASTE_TARGET_LIST:
231         g_value_set_boxed(value, webkit_web_view_get_paste_target_list(webView));
232         break;
233     case PROP_EDITABLE:
234         g_value_set_boolean(value, webkit_web_view_get_editable(webView));
235         break;
236     case PROP_SETTINGS:
237         g_value_set_object(value, webkit_web_view_get_settings(webView));
238         break;
239     case PROP_TRANSPARENT:
240         g_value_set_boolean(value, webkit_web_view_get_transparent(webView));
241         break;
242     case PROP_ZOOM_LEVEL:
243         g_value_set_float(value, webkit_web_view_get_zoom_level(webView));
244         break;
245     case PROP_FULL_CONTENT_ZOOM:
246         g_value_set_boolean(value, webkit_web_view_get_full_content_zoom(webView));
247         break;
248     default:
249         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
250     }
251 }
252
253 static void webkit_web_view_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec *pspec)
254 {
255     WebKitWebView* webView = WEBKIT_WEB_VIEW(object);
256
257     switch(prop_id) {
258     case PROP_EDITABLE:
259         webkit_web_view_set_editable(webView, g_value_get_boolean(value));
260         break;
261     case PROP_SETTINGS:
262         webkit_web_view_set_settings(webView, WEBKIT_WEB_SETTINGS(g_value_get_object(value)));
263         break;
264     case PROP_TRANSPARENT:
265         webkit_web_view_set_transparent(webView, g_value_get_boolean(value));
266         break;
267     case PROP_ZOOM_LEVEL:
268         webkit_web_view_set_zoom_level(webView, g_value_get_float(value));
269         break;
270     case PROP_FULL_CONTENT_ZOOM:
271         webkit_web_view_set_full_content_zoom(webView, g_value_get_boolean(value));
272         break;
273     default:
274         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
275     }
276 }
277
278 static gboolean webkit_web_view_expose_event(GtkWidget* widget, GdkEventExpose* event)
279 {
280     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
281     WebKitWebViewPrivate* priv = webView->priv;
282
283     Frame* frame = core(webView)->mainFrame();
284     GdkRectangle clip;
285     gdk_region_get_clipbox(event->region, &clip);
286     cairo_t* cr = gdk_cairo_create(event->window);
287     GraphicsContext ctx(cr);
288     ctx.setGdkExposeEvent(event);
289     if (frame->contentRenderer() && frame->view()) {
290         frame->view()->layoutIfNeededRecursive();
291
292         if (priv->transparent) {
293             cairo_save(cr);
294             cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
295             cairo_paint(cr);
296             cairo_restore(cr);
297         }
298
299         frame->view()->paint(&ctx, clip);
300     }
301     cairo_destroy(cr);
302
303     return FALSE;
304 }
305
306 static gboolean webkit_web_view_key_press_event(GtkWidget* widget, GdkEventKey* event)
307 {
308     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
309
310     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
311     PlatformKeyboardEvent keyboardEvent(event);
312
313     if (!frame->view())
314         return FALSE;
315
316     if (frame->eventHandler()->keyEvent(keyboardEvent))
317         return TRUE;
318
319     FrameView* view = frame->view();
320     SelectionController::EAlteration alteration;
321     if (event->state & GDK_SHIFT_MASK)
322         alteration = SelectionController::EXTEND;
323     else
324         alteration = SelectionController::MOVE;
325
326     // TODO: We probably want to use GTK+ key bindings here and perhaps take an
327     // approach more like the Win and Mac ports for key handling.
328     switch (event->keyval) {
329     case GDK_Down:
330         view->scrollBy(0, LINE_STEP);
331         return TRUE;
332     case GDK_Up:
333         view->scrollBy(0, -LINE_STEP);
334         return TRUE;
335     case GDK_Right:
336         view->scrollBy(LINE_STEP, 0);
337         return TRUE;
338     case GDK_Left:
339         view->scrollBy(-LINE_STEP, 0);
340         return TRUE;
341     case GDK_Home:
342         frame->selection()->modify(alteration, SelectionController::BACKWARD, DocumentBoundary, true);
343         return TRUE;
344     case GDK_End:
345         frame->selection()->modify(alteration, SelectionController::FORWARD, DocumentBoundary, true);
346         return TRUE;
347     }
348
349     /* Chain up to our parent class for binding activation */
350     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->key_press_event(widget, event);
351 }
352
353 static gboolean webkit_web_view_key_release_event(GtkWidget* widget, GdkEventKey* event)
354 {
355     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
356
357     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
358     if (!frame->view())
359         return FALSE;
360
361     PlatformKeyboardEvent keyboardEvent(event);
362
363     if (frame->eventHandler()->keyEvent(keyboardEvent))
364         return TRUE;
365
366     /* Chain up to our parent class for binding activation */
367     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->key_release_event(widget, event);
368 }
369
370 static gboolean webkit_web_view_button_press_event(GtkWidget* widget, GdkEventButton* event)
371 {
372     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
373
374     // FIXME: need to keep track of subframe focus for key events
375     gtk_widget_grab_focus(widget);
376
377     if (event->button == 3)
378         return webkit_web_view_forward_context_menu_event(webView, PlatformMouseEvent(event));
379
380     Frame* frame = core(webView)->mainFrame();
381     if (!frame->view())
382         return FALSE;
383
384     return frame->eventHandler()->handleMousePressEvent(PlatformMouseEvent(event));
385 }
386
387 static gboolean webkit_web_view_button_release_event(GtkWidget* widget, GdkEventButton* event)
388 {
389     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
390
391     Frame* focusedFrame = core(webView)->focusController()->focusedFrame();
392
393     if (focusedFrame && focusedFrame->editor()->canEdit()) {
394 #ifdef MAEMO_CHANGES
395         WebKitWebViewPrivate* priv = webView->priv;
396         hildon_gtk_im_context_filter_event(priv->imContext, (GdkEvent*)event);
397 #endif
398     }
399
400     Frame* mainFrame = core(webView)->mainFrame();
401     if (!mainFrame->view())
402         return FALSE;
403
404     return mainFrame->eventHandler()->handleMouseReleaseEvent(PlatformMouseEvent(event));
405 }
406
407 static gboolean webkit_web_view_motion_event(GtkWidget* widget, GdkEventMotion* event)
408 {
409     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
410
411     Frame* frame = core(webView)->mainFrame();
412     if (!frame->view())
413         return FALSE;
414
415     return frame->eventHandler()->mouseMoved(PlatformMouseEvent(event));
416 }
417
418 static gboolean webkit_web_view_scroll_event(GtkWidget* widget, GdkEventScroll* event)
419 {
420     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
421
422     Frame* frame = core(webView)->mainFrame();
423     if (!frame->view())
424         return FALSE;
425
426     PlatformWheelEvent wheelEvent(event);
427     return frame->eventHandler()->handleWheelEvent(wheelEvent);
428 }
429
430 static void webkit_web_view_size_allocate(GtkWidget* widget, GtkAllocation* allocation)
431 {
432     GTK_WIDGET_CLASS(webkit_web_view_parent_class)->size_allocate(widget,allocation);
433
434     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
435
436     Frame* frame = core(webView)->mainFrame();
437     if (!frame->view())
438         return;
439
440     frame->view()->resize(allocation->width, allocation->height);
441     frame->forceLayout();
442     frame->view()->adjustViewSize();
443 }
444
445 static gboolean webkit_web_view_focus_in_event(GtkWidget* widget, GdkEventFocus* event)
446 {
447     // TODO: Improve focus handling as suggested in
448     // http://bugs.webkit.org/show_bug.cgi?id=16910
449     GtkWidget* toplevel = gtk_widget_get_toplevel(widget);
450     if (GTK_WIDGET_TOPLEVEL(toplevel) && gtk_window_has_toplevel_focus(GTK_WINDOW(toplevel))) {
451         WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
452
453         Frame* frame = core(webView)->mainFrame();
454         core(webView)->focusController()->setActive(frame);
455     }
456     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->focus_in_event(widget, event);
457 }
458
459 static gboolean webkit_web_view_focus_out_event(GtkWidget* widget, GdkEventFocus* event)
460 {
461     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
462
463     core(webView)->focusController()->setActive(false);
464
465     return GTK_WIDGET_CLASS(webkit_web_view_parent_class)->focus_out_event(widget, event);
466 }
467
468 static void webkit_web_view_realize(GtkWidget* widget)
469 {
470     GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
471
472     GdkWindowAttr attributes;
473     attributes.window_type = GDK_WINDOW_CHILD;
474     attributes.x = widget->allocation.x;
475     attributes.y = widget->allocation.y;
476     attributes.width = widget->allocation.width;
477     attributes.height = widget->allocation.height;
478     attributes.wclass = GDK_INPUT_OUTPUT;
479     attributes.visual = gtk_widget_get_visual (widget);
480     attributes.colormap = gtk_widget_get_colormap (widget);
481     attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK
482                             | GDK_EXPOSURE_MASK
483                             | GDK_BUTTON_PRESS_MASK
484                             | GDK_BUTTON_RELEASE_MASK
485                             | GDK_POINTER_MOTION_MASK
486                             | GDK_KEY_PRESS_MASK
487                             | GDK_KEY_RELEASE_MASK
488                             | GDK_BUTTON_MOTION_MASK
489                             | GDK_BUTTON1_MOTION_MASK
490                             | GDK_BUTTON2_MOTION_MASK
491                             | GDK_BUTTON3_MOTION_MASK;
492
493     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
494     widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
495     gdk_window_set_user_data(widget->window, widget);
496
497     widget->style = gtk_style_attach(widget->style, widget->window);
498     gdk_window_set_background(widget->window, &widget->style->base[GTK_WIDGET_STATE(widget)]);
499
500     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
501     WebKitWebViewPrivate* priv = webView->priv;
502     gtk_im_context_set_client_window(priv->imContext, widget->window);
503 }
504
505 static void webkit_web_view_set_scroll_adjustments(WebKitWebView* webView, GtkAdjustment* hadj, GtkAdjustment* vadj)
506 {
507     FrameView* view = core(webkit_web_view_get_main_frame(webView))->view();
508
509     if (hadj)
510         g_object_ref(hadj);
511     if (vadj)
512         g_object_ref(vadj);
513
514     WebKitWebViewPrivate* priv = webView->priv;
515
516     if (priv->horizontalAdjustment)
517         g_object_unref(priv->horizontalAdjustment);
518     if (priv->verticalAdjustment)
519         g_object_unref(priv->verticalAdjustment);
520
521     priv->horizontalAdjustment = hadj;
522     priv->verticalAdjustment = vadj;
523
524     if (!view)
525         return;
526
527     view->setGtkAdjustments(hadj, vadj);
528 }
529
530 static void webkit_web_view_container_add(GtkContainer* container, GtkWidget* widget)
531 {
532     WebKitWebView* webView = WEBKIT_WEB_VIEW(container);
533     WebKitWebViewPrivate* priv = webView->priv;
534
535     priv->children.add(widget);
536     if (GTK_WIDGET_REALIZED(container))
537         gtk_widget_set_parent_window(widget, GTK_WIDGET(webView)->window);
538     gtk_widget_set_parent(widget, GTK_WIDGET(container));
539 }
540
541 static void webkit_web_view_container_remove(GtkContainer* container, GtkWidget* widget)
542 {
543     WebKitWebView* webView = WEBKIT_WEB_VIEW(container);
544     WebKitWebViewPrivate* priv = webView->priv;
545
546     if (priv->children.contains(widget)) {
547         gtk_widget_unparent(widget);
548         priv->children.remove(widget);
549     }
550 }
551
552 static void webkit_web_view_container_forall(GtkContainer* container, gboolean, GtkCallback callback, gpointer callbackData)
553 {
554     WebKitWebView* webView = WEBKIT_WEB_VIEW(container);
555     WebKitWebViewPrivate* priv = webView->priv;
556
557     HashSet<GtkWidget*> children = priv->children;
558     HashSet<GtkWidget*>::const_iterator end = children.end();
559     for (HashSet<GtkWidget*>::const_iterator current = children.begin(); current != end; ++current)
560         (*callback)(*current, callbackData);
561 }
562
563 static WebKitWebView* webkit_web_view_real_create_web_view(WebKitWebView*)
564 {
565     notImplemented();
566     return 0;
567 }
568
569 static WebKitNavigationResponse webkit_web_view_real_navigation_requested(WebKitWebView*, WebKitWebFrame* frame, WebKitNetworkRequest*)
570 {
571     notImplemented();
572     return WEBKIT_NAVIGATION_RESPONSE_ACCEPT;
573 }
574
575 static void webkit_web_view_real_window_object_cleared(WebKitWebView*, WebKitWebFrame*, JSGlobalContextRef context, JSObjectRef window_object)
576 {
577     notImplemented();
578 }
579
580 static gchar* webkit_web_view_real_choose_file(WebKitWebView*, WebKitWebFrame*, const gchar* old_name)
581 {
582     notImplemented();
583     return g_strdup(old_name);
584 }
585
586 typedef enum {
587     WEBKIT_SCRIPT_DIALOG_ALERT,
588     WEBKIT_SCRIPT_DIALOG_CONFIRM,
589     WEBKIT_SCRIPT_DIALOG_PROMPT
590  } WebKitScriptDialogType;
591
592 static gboolean webkit_web_view_script_dialog(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, WebKitScriptDialogType type, const gchar* defaultValue, gchar** value)
593 {
594     GtkMessageType messageType;
595     GtkButtonsType buttons;
596     gint defaultResponse;
597     GtkWidget* window;
598     GtkWidget* dialog;
599     GtkWidget* entry = 0;
600     gboolean didConfirm = FALSE;
601
602     switch (type) {
603     case WEBKIT_SCRIPT_DIALOG_ALERT:
604         messageType = GTK_MESSAGE_WARNING;
605         buttons = GTK_BUTTONS_CLOSE;
606         defaultResponse = GTK_RESPONSE_CLOSE;
607         break;
608     case WEBKIT_SCRIPT_DIALOG_CONFIRM:
609         messageType = GTK_MESSAGE_QUESTION;
610         buttons = GTK_BUTTONS_YES_NO;
611         defaultResponse = GTK_RESPONSE_YES;
612         break;
613     case WEBKIT_SCRIPT_DIALOG_PROMPT:
614         messageType = GTK_MESSAGE_QUESTION;
615         buttons = GTK_BUTTONS_OK_CANCEL;
616         defaultResponse = GTK_RESPONSE_OK;
617         break;
618     default:
619         g_warning("Unknown value for WebKitScriptDialogType.");
620         return FALSE;
621     }
622
623     window = gtk_widget_get_toplevel(GTK_WIDGET(webView));
624     dialog = gtk_message_dialog_new(GTK_WIDGET_TOPLEVEL(window) ? GTK_WINDOW(window) : 0, GTK_DIALOG_DESTROY_WITH_PARENT, messageType, buttons, "%s", message);
625     gchar* title = g_strconcat("JavaScript - ", webkit_web_frame_get_uri(frame), NULL);
626     gtk_window_set_title(GTK_WINDOW(dialog), title);
627     g_free(title);
628
629     if (type == WEBKIT_SCRIPT_DIALOG_PROMPT) {
630         entry = gtk_entry_new();
631         gtk_entry_set_text(GTK_ENTRY(entry), defaultValue);
632         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), entry);
633         gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
634         gtk_widget_show(entry);
635     }
636
637     gtk_dialog_set_default_response(GTK_DIALOG(dialog), defaultResponse);
638     gint response = gtk_dialog_run(GTK_DIALOG(dialog));
639
640     switch (response) {
641     case GTK_RESPONSE_YES:
642         didConfirm = TRUE;
643         break;
644     case GTK_RESPONSE_OK:
645         didConfirm = TRUE;
646         if (entry)
647             *value = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
648         else
649             *value = 0;
650         break;
651     case GTK_RESPONSE_NO:
652     case GTK_RESPONSE_CANCEL:
653         didConfirm = FALSE;
654         break;
655
656     }
657     gtk_widget_destroy(GTK_WIDGET(dialog));
658     return didConfirm;
659 }
660
661 static gboolean webkit_web_view_real_script_alert(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message)
662 {
663     webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_ALERT, 0, 0);
664     return TRUE;
665 }
666
667 static gboolean webkit_web_view_real_script_confirm(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, gboolean* didConfirm)
668 {
669     *didConfirm = webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_CONFIRM, 0, 0);
670     return TRUE;
671 }
672
673 static gboolean webkit_web_view_real_script_prompt(WebKitWebView* webView, WebKitWebFrame* frame, const gchar* message, const gchar* defaultValue, gchar** value)
674 {
675     if (!webkit_web_view_script_dialog(webView, frame, message, WEBKIT_SCRIPT_DIALOG_PROMPT, defaultValue, value))
676         *value = NULL;
677     return TRUE;
678 }
679
680 static gboolean webkit_web_view_real_console_message(WebKitWebView* webView, const gchar* message, unsigned int line, const gchar* sourceId)
681 {
682     g_print("console message: %s @%d: %s\n", sourceId, line, message);
683     return TRUE;
684 }
685
686 static void webkit_web_view_real_select_all(WebKitWebView* webView)
687 {
688     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
689     frame->editor()->command("SelectAll").execute();
690 }
691
692 static void webkit_web_view_real_cut_clipboard(WebKitWebView* webView)
693 {
694     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
695     frame->editor()->command("Cut").execute();
696 }
697
698 static void webkit_web_view_real_copy_clipboard(WebKitWebView* webView)
699 {
700     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
701     frame->editor()->command("Copy").execute();
702 }
703
704 static void webkit_web_view_real_paste_clipboard(WebKitWebView* webView)
705 {
706     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
707     frame->editor()->command("Paste").execute();
708 }
709
710 static void webkit_web_view_finalize(GObject* object)
711 {
712     WebKitWebView* webView = WEBKIT_WEB_VIEW(object);
713     WebKitWebViewPrivate* priv = webView->priv;
714
715     webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(object));
716
717     core(priv->mainFrame)->loader()->detachChildren();
718     delete priv->corePage;
719
720     if (priv->horizontalAdjustment)
721         g_object_unref(priv->horizontalAdjustment);
722     if (priv->verticalAdjustment)
723         g_object_unref(priv->verticalAdjustment);
724     g_object_unref(priv->backForwardList);
725     g_signal_handlers_disconnect_by_func(priv->webSettings, (gpointer)webkit_web_view_settings_notify, webView);
726     g_object_unref(priv->webSettings);
727     g_object_unref(priv->mainFrame);
728     g_object_unref(priv->imContext);
729     gtk_target_list_unref(priv->copy_target_list);
730     gtk_target_list_unref(priv->paste_target_list);
731     delete priv->userAgent;
732
733     G_OBJECT_CLASS(webkit_web_view_parent_class)->finalize(object);
734 }
735
736 static gboolean webkit_navigation_request_handled(GSignalInvocationHint* ihint, GValue* returnAccu, const GValue* handlerReturn, gpointer dummy)
737 {
738   gboolean continueEmission = TRUE;
739   int signalHandled = g_value_get_int(handlerReturn);
740   g_value_set_int(returnAccu, signalHandled);
741
742   if (signalHandled != WEBKIT_NAVIGATION_RESPONSE_ACCEPT)
743       continueEmission = FALSE;
744
745   return continueEmission;
746 }
747
748 static AtkObject* webkit_web_view_get_accessible(GtkWidget* widget)
749 {
750     WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);
751     AXObjectCache::enableAccessibility();
752
753     Frame* coreFrame = core(webView)->mainFrame();
754     if (!coreFrame)
755         return NULL;
756
757     Document* doc = coreFrame->document();
758     if (!doc)
759         return NULL;
760
761     AccessibilityObject* coreAccessible = doc->axObjectCache()->get(doc->renderer());
762     if (!coreAccessible || !coreAccessible->wrapper())
763         return NULL;
764
765     return coreAccessible->wrapper();
766 }
767
768 static void webkit_web_view_class_init(WebKitWebViewClass* webViewClass)
769 {
770     GtkBindingSet* binding_set;
771
772     webkit_init();
773
774     /*
775      * Signals
776      */
777
778     webkit_web_view_signals[NAVIGATION_REQUESTED] = g_signal_new("navigation-requested",
779             G_TYPE_FROM_CLASS(webViewClass),
780             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
781             G_STRUCT_OFFSET (WebKitWebViewClass, navigation_requested),
782             webkit_navigation_request_handled,
783             NULL,
784             webkit_marshal_INT__OBJECT_OBJECT,
785             G_TYPE_INT, 2,
786             G_TYPE_OBJECT,
787             G_TYPE_OBJECT);
788
789     /**
790      * WebKitWebView::window-object-cleared:
791      * @web_view: the object on which the signal is emitted
792      * @frame: the #WebKitWebFrame to which @window_object belongs
793      * @context: the #JSGlobalContextRef holding the global object and other
794      * execution state; equivalent to the return value of
795      * webkit_web_frame_get_global_context(@frame)
796      *
797      * @window_object: the #JSObjectRef representing the frame's JavaScript
798      * window object
799      *
800      * Emitted when the JavaScript window object in a #WebKitWebFrame has been
801      * cleared in preparation for a new load. This is the preferred place to
802      * set custom properties on the window object using the JavaScriptCore API.
803      */
804     webkit_web_view_signals[WINDOW_OBJECT_CLEARED] = g_signal_new("window-object-cleared",
805             G_TYPE_FROM_CLASS(webViewClass),
806             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
807             G_STRUCT_OFFSET (WebKitWebViewClass, window_object_cleared),
808             NULL,
809             NULL,
810             webkit_marshal_VOID__OBJECT_POINTER_POINTER,
811             G_TYPE_NONE, 3,
812             WEBKIT_TYPE_WEB_FRAME,
813             G_TYPE_POINTER,
814             G_TYPE_POINTER);
815
816     /**
817      * WebKitWebView::load-started:
818      * @web_view: the object on which the signal is emitted
819      * @frame: the frame going to do the load
820      *
821      * When a #WebKitWebFrame begins to load this signal is emitted.
822      */
823     webkit_web_view_signals[LOAD_STARTED] = g_signal_new("load-started",
824             G_TYPE_FROM_CLASS(webViewClass),
825             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
826             0,
827             NULL,
828             NULL,
829             g_cclosure_marshal_VOID__OBJECT,
830             G_TYPE_NONE, 1,
831             WEBKIT_TYPE_WEB_FRAME);
832
833     /**
834      * WebKitWebView::load-committed:
835      * @web_view: the object on which the signal is emitted
836      * @frame: the main frame that received the first data
837      *
838      * When a #WebKitWebFrame loaded the first data this signal is emitted.
839      */
840     webkit_web_view_signals[LOAD_COMMITTED] = g_signal_new("load-committed",
841             G_TYPE_FROM_CLASS(webViewClass),
842             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
843             0,
844             NULL,
845             NULL,
846             g_cclosure_marshal_VOID__OBJECT,
847             G_TYPE_NONE, 1,
848             WEBKIT_TYPE_WEB_FRAME);
849
850
851     /**
852      * WebKitWebView::load-progress-changed:
853      * @web_view: the #WebKitWebView
854      * @progress: the global progress
855      */
856     webkit_web_view_signals[LOAD_PROGRESS_CHANGED] = g_signal_new("load-progress-changed",
857             G_TYPE_FROM_CLASS(webViewClass),
858             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
859             0,
860             NULL,
861             NULL,
862             g_cclosure_marshal_VOID__INT,
863             G_TYPE_NONE, 1,
864             G_TYPE_INT);
865
866     webkit_web_view_signals[LOAD_FINISHED] = g_signal_new("load-finished",
867             G_TYPE_FROM_CLASS(webViewClass),
868             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
869             0,
870             NULL,
871             NULL,
872             g_cclosure_marshal_VOID__OBJECT,
873             G_TYPE_NONE, 1,
874             WEBKIT_TYPE_WEB_FRAME);
875
876     /**
877      * WebKitWebView::title-changed:
878      * @web_view: the object on which the signal is emitted
879      * @frame: the main frame
880      * @title: the new title
881      *
882      * When a #WebKitWebFrame changes the document title this signal is emitted.
883      */
884     webkit_web_view_signals[TITLE_CHANGED] = g_signal_new("title-changed",
885             G_TYPE_FROM_CLASS(webViewClass),
886             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
887             0,
888             NULL,
889             NULL,
890             webkit_marshal_VOID__OBJECT_STRING,
891             G_TYPE_NONE, 2,
892             WEBKIT_TYPE_WEB_FRAME,
893             G_TYPE_STRING);
894
895     /**
896      * WebKitWebView::hovering-over-link:
897      * @web_view: the object on which the signal is emitted
898      * @title: the link's title
899      * @uri: the URI the link points to
900      *
901      * When the cursor is over a link, this signal is emitted.
902      */
903     webkit_web_view_signals[HOVERING_OVER_LINK] = g_signal_new("hovering-over-link",
904             G_TYPE_FROM_CLASS(webViewClass),
905             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
906             0,
907             NULL,
908             NULL,
909             webkit_marshal_VOID__STRING_STRING,
910             G_TYPE_NONE, 2,
911             G_TYPE_STRING,
912             G_TYPE_STRING);
913
914     /**
915      * WebKitWebView::populate-popup:
916      * @web_view: the object on which the signal is emitted
917      * @menu: the context menu
918      *
919      * When a context menu is about to be displayed this signal is emitted.
920      *
921      * Add menu items to #menu to extend the context menu.
922      */
923     webkit_web_view_signals[POPULATE_POPUP] = g_signal_new("populate-popup",
924             G_TYPE_FROM_CLASS(webViewClass),
925             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
926             0,
927             NULL,
928             NULL,
929             g_cclosure_marshal_VOID__OBJECT,
930             G_TYPE_NONE, 1,
931             GTK_TYPE_MENU);
932
933     webkit_web_view_signals[STATUS_BAR_TEXT_CHANGED] = g_signal_new("status-bar-text-changed",
934             G_TYPE_FROM_CLASS(webViewClass),
935             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
936             0,
937             NULL,
938             NULL,
939             g_cclosure_marshal_VOID__STRING,
940             G_TYPE_NONE, 1,
941             G_TYPE_STRING);
942
943     webkit_web_view_signals[ICOND_LOADED] = g_signal_new("icon-loaded",
944             G_TYPE_FROM_CLASS(webViewClass),
945             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
946             0,
947             NULL,
948             NULL,
949             g_cclosure_marshal_VOID__VOID,
950             G_TYPE_NONE, 0);
951
952     webkit_web_view_signals[SELECTION_CHANGED] = g_signal_new("selection-changed",
953             G_TYPE_FROM_CLASS(webViewClass),
954             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
955             0,
956             NULL,
957             NULL,
958             g_cclosure_marshal_VOID__VOID,
959             G_TYPE_NONE, 0);
960
961     /**
962      * WebKitWebView::console-message:
963      * @web_view: the object on which the signal is emitted
964      * @message: the message text
965      * @line: the line where the error occured
966      * @source_id: the source id
967      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
968      *
969      * A JavaScript console message was created.
970      */
971     webkit_web_view_signals[CONSOLE_MESSAGE] = g_signal_new("console-message",
972             G_TYPE_FROM_CLASS(webViewClass),
973             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
974             G_STRUCT_OFFSET(WebKitWebViewClass, console_message),
975             g_signal_accumulator_true_handled,
976             NULL,
977             webkit_marshal_BOOLEAN__STRING_INT_STRING,
978             G_TYPE_BOOLEAN, 3,
979             G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING);
980
981     /**
982      * WebKitWebView::script-alert:
983      * @web_view: the object on which the signal is emitted
984      * @frame: the relevant frame
985      * @message: the message text
986      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
987      *
988      * A JavaScript alert dialog was created.
989      */
990     webkit_web_view_signals[SCRIPT_ALERT] = g_signal_new("script-alert",
991             G_TYPE_FROM_CLASS(webViewClass),
992             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
993             G_STRUCT_OFFSET(WebKitWebViewClass, script_alert),
994             g_signal_accumulator_true_handled,
995             NULL,
996             webkit_marshal_BOOLEAN__OBJECT_STRING,
997             G_TYPE_BOOLEAN, 2,
998             WEBKIT_TYPE_WEB_FRAME, G_TYPE_STRING);
999
1000     /**
1001      * WebKitWebView::script-confirm:
1002      * @web_view: the object on which the signal is emitted
1003      * @frame: the relevant frame
1004      * @message: the message text
1005      * @confirmed: whether the dialog has been confirmed
1006      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
1007      *
1008      * A JavaScript confirm dialog was created, providing Yes and No buttons.
1009      */
1010     webkit_web_view_signals[SCRIPT_CONFIRM] = g_signal_new("script-confirm",
1011             G_TYPE_FROM_CLASS(webViewClass),
1012             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1013             G_STRUCT_OFFSET(WebKitWebViewClass, script_confirm),
1014             g_signal_accumulator_true_handled,
1015             NULL,
1016             webkit_marshal_BOOLEAN__OBJECT_STRING_BOOLEAN,
1017             G_TYPE_BOOLEAN, 3,
1018             WEBKIT_TYPE_WEB_FRAME, G_TYPE_STRING, G_TYPE_BOOLEAN);
1019
1020     /**
1021      * WebKitWebView::script-prompt:
1022      * @web_view: the object on which the signal is emitted
1023      * @frame: the relevant frame
1024      * @message: the message text
1025      * @default: the default value
1026      * @text: To be filled with the return value or NULL if the dialog was cancelled.
1027      * @return: TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further.
1028      *
1029      * A JavaScript prompt dialog was created, providing an entry to input text.
1030      */
1031     webkit_web_view_signals[SCRIPT_PROMPT] = g_signal_new("script-prompt",
1032             G_TYPE_FROM_CLASS(webViewClass),
1033             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1034             G_STRUCT_OFFSET(WebKitWebViewClass, script_prompt),
1035             g_signal_accumulator_true_handled,
1036             NULL,
1037             webkit_marshal_BOOLEAN__OBJECT_STRING_STRING_STRING,
1038             G_TYPE_BOOLEAN, 4,
1039             WEBKIT_TYPE_WEB_FRAME, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
1040
1041     /**
1042      * WebKitWebView::select-all:
1043      * @web_view: the object which received the signal
1044      *
1045      * The ::select-all signal is a keybinding signal which gets emitted to
1046      * select the complete contents of the text view.
1047      *
1048      * The default bindings for this signal is Ctrl-a.
1049      */
1050     webkit_web_view_signals[SELECT_ALL] = g_signal_new("select-all",
1051             G_TYPE_FROM_CLASS(webViewClass),
1052             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1053             G_STRUCT_OFFSET(WebKitWebViewClass, select_all),
1054             NULL, NULL,
1055             g_cclosure_marshal_VOID__VOID,
1056             G_TYPE_NONE, 0);
1057
1058     /**
1059      * WebKitWebView::cut-clipboard:
1060      * @web_view: the object which received the signal
1061      *
1062      * The ::cut-clipboard signal is a keybinding signal which gets emitted to
1063      * cut the selection to the clipboard.
1064      *
1065      * The default bindings for this signal are Ctrl-x and Shift-Delete.
1066      */
1067     webkit_web_view_signals[CUT_CLIPBOARD] = g_signal_new("cut-clipboard",
1068             G_TYPE_FROM_CLASS(webViewClass),
1069             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1070             G_STRUCT_OFFSET(WebKitWebViewClass, cut_clipboard),
1071             NULL, NULL,
1072             g_cclosure_marshal_VOID__VOID,
1073             G_TYPE_NONE, 0);
1074
1075     /**
1076      * WebKitWebView::copy-clipboard:
1077      * @web_view: the object which received the signal
1078      *
1079      * The ::copy-clipboard signal is a keybinding signal which gets emitted to
1080      * copy the selection to the clipboard.
1081      *
1082      * The default bindings for this signal are Ctrl-c and Ctrl-Insert.
1083      */
1084     webkit_web_view_signals[COPY_CLIPBOARD] = g_signal_new("copy-clipboard",
1085             G_TYPE_FROM_CLASS(webViewClass),
1086             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1087             G_STRUCT_OFFSET(WebKitWebViewClass, copy_clipboard),
1088             NULL, NULL,
1089             g_cclosure_marshal_VOID__VOID,
1090             G_TYPE_NONE, 0);
1091
1092     /**
1093      * WebKitWebView::paste-clipboard:
1094      * @web_view: the object which received the signal
1095      *
1096      * The ::paste-clipboard signal is a keybinding signal which gets emitted to
1097      * paste the contents of the clipboard into the Web view.
1098      *
1099      * The default bindings for this signal are Ctrl-v and Shift-Insert.
1100      */
1101     webkit_web_view_signals[PASTE_CLIPBOARD] = g_signal_new("paste-clipboard",
1102             G_TYPE_FROM_CLASS(webViewClass),
1103             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1104             G_STRUCT_OFFSET(WebKitWebViewClass, paste_clipboard),
1105             NULL, NULL,
1106             g_cclosure_marshal_VOID__VOID,
1107             G_TYPE_NONE, 0);
1108
1109     /*
1110      * implementations of virtual methods
1111      */
1112     webViewClass->create_web_view = webkit_web_view_real_create_web_view;
1113     webViewClass->navigation_requested = webkit_web_view_real_navigation_requested;
1114     webViewClass->window_object_cleared = webkit_web_view_real_window_object_cleared;
1115     webViewClass->choose_file = webkit_web_view_real_choose_file;
1116     webViewClass->script_alert = webkit_web_view_real_script_alert;
1117     webViewClass->script_confirm = webkit_web_view_real_script_confirm;
1118     webViewClass->script_prompt = webkit_web_view_real_script_prompt;
1119     webViewClass->console_message = webkit_web_view_real_console_message;
1120     webViewClass->select_all = webkit_web_view_real_select_all;
1121     webViewClass->cut_clipboard = webkit_web_view_real_cut_clipboard;
1122     webViewClass->copy_clipboard = webkit_web_view_real_copy_clipboard;
1123     webViewClass->paste_clipboard = webkit_web_view_real_paste_clipboard;
1124
1125     GObjectClass* objectClass = G_OBJECT_CLASS(webViewClass);
1126     objectClass->finalize = webkit_web_view_finalize;
1127     objectClass->get_property = webkit_web_view_get_property;
1128     objectClass->set_property = webkit_web_view_set_property;
1129
1130     GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(webViewClass);
1131     widgetClass->realize = webkit_web_view_realize;
1132     widgetClass->expose_event = webkit_web_view_expose_event;
1133     widgetClass->key_press_event = webkit_web_view_key_press_event;
1134     widgetClass->key_release_event = webkit_web_view_key_release_event;
1135     widgetClass->button_press_event = webkit_web_view_button_press_event;
1136     widgetClass->button_release_event = webkit_web_view_button_release_event;
1137     widgetClass->motion_notify_event = webkit_web_view_motion_event;
1138     widgetClass->scroll_event = webkit_web_view_scroll_event;
1139     widgetClass->size_allocate = webkit_web_view_size_allocate;
1140     widgetClass->popup_menu = webkit_web_view_popup_menu_handler;
1141     widgetClass->focus_in_event = webkit_web_view_focus_in_event;
1142     widgetClass->focus_out_event = webkit_web_view_focus_out_event;
1143     widgetClass->get_accessible = webkit_web_view_get_accessible;
1144
1145     GtkContainerClass* containerClass = GTK_CONTAINER_CLASS(webViewClass);
1146     containerClass->add = webkit_web_view_container_add;
1147     containerClass->remove = webkit_web_view_container_remove;
1148     containerClass->forall = webkit_web_view_container_forall;
1149
1150     /*
1151      * make us scrollable (e.g. addable to a GtkScrolledWindow)
1152      */
1153     webViewClass->set_scroll_adjustments = webkit_web_view_set_scroll_adjustments;
1154     GTK_WIDGET_CLASS(webViewClass)->set_scroll_adjustments_signal = g_signal_new("set-scroll-adjustments",
1155             G_TYPE_FROM_CLASS(webViewClass),
1156             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
1157             G_STRUCT_OFFSET(WebKitWebViewClass, set_scroll_adjustments),
1158             NULL, NULL,
1159             webkit_marshal_VOID__OBJECT_OBJECT,
1160             G_TYPE_NONE, 2,
1161             GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
1162
1163     /*
1164      * Key bindings
1165      */
1166
1167     binding_set = gtk_binding_set_by_class(webViewClass);
1168
1169     gtk_binding_entry_add_signal(binding_set, GDK_a, GDK_CONTROL_MASK,
1170                                  "select_all", 0);
1171
1172     /* Cut/copy/paste */
1173
1174     gtk_binding_entry_add_signal(binding_set, GDK_x, GDK_CONTROL_MASK,
1175                                  "cut_clipboard", 0);
1176     gtk_binding_entry_add_signal(binding_set, GDK_c, GDK_CONTROL_MASK,
1177                                  "copy_clipboard", 0);
1178     gtk_binding_entry_add_signal(binding_set, GDK_v, GDK_CONTROL_MASK,
1179                                  "paste_clipboard", 0);
1180
1181     gtk_binding_entry_add_signal(binding_set, GDK_Delete, GDK_SHIFT_MASK,
1182                                  "cut_clipboard", 0);
1183     gtk_binding_entry_add_signal(binding_set, GDK_Insert, GDK_CONTROL_MASK,
1184                                  "copy_clipboard", 0);
1185     gtk_binding_entry_add_signal(binding_set, GDK_Insert, GDK_SHIFT_MASK,
1186                                  "paste_clipboard", 0);
1187
1188     /*
1189      * properties
1190      */
1191     g_object_class_install_property(objectClass, PROP_COPY_TARGET_LIST,
1192                                     g_param_spec_boxed("copy-target-list",
1193                                                        "Target list",
1194                                                        "The list of targets this Web view supports for copying to the clipboard",
1195                                                        GTK_TYPE_TARGET_LIST,
1196                                                        WEBKIT_PARAM_READABLE));
1197
1198     g_object_class_install_property(objectClass, PROP_PASTE_TARGET_LIST,
1199                                     g_param_spec_boxed("paste-target-list",
1200                                                        "Target list",
1201                                                        "The list of targets this Web view supports for pasting to the clipboard",
1202                                                        GTK_TYPE_TARGET_LIST,
1203                                                        WEBKIT_PARAM_READABLE));
1204
1205     g_object_class_install_property(objectClass, PROP_SETTINGS,
1206                                     g_param_spec_object("settings",
1207                                                         "Settings",
1208                                                         "An associated WebKitWebSettings instance",
1209                                                         WEBKIT_TYPE_WEB_SETTINGS,
1210                                                         WEBKIT_PARAM_READWRITE));
1211
1212     g_object_class_install_property(objectClass, PROP_EDITABLE,
1213                                     g_param_spec_boolean("editable",
1214                                                          "Editable",
1215                                                          "Whether content can be modified by the user",
1216                                                          FALSE,
1217                                                          WEBKIT_PARAM_READWRITE));
1218
1219     g_object_class_install_property(objectClass, PROP_TRANSPARENT,
1220                                     g_param_spec_boolean("transparent",
1221                                                          "Transparent",
1222                                                          "Whether content has a transparent background",
1223                                                          FALSE,
1224                                                          WEBKIT_PARAM_READWRITE));
1225
1226     /**
1227     * WebKitWebView:zoom-level:
1228     *
1229     * The level of zoom of the content.
1230     *
1231     * Since: 1.0.1
1232     */
1233     g_object_class_install_property(objectClass, PROP_ZOOM_LEVEL,
1234                                     g_param_spec_float("zoom-level",
1235                                                        "Zoom level",
1236                                                        "The level of zoom of the content",
1237                                                        G_MINFLOAT,
1238                                                        G_MAXFLOAT,
1239                                                        1.0f,
1240                                                        WEBKIT_PARAM_READWRITE));
1241
1242     /**
1243     * WebKitWebView:full-content-zoom:
1244     *
1245     * Whether the full content is scaled when zooming.
1246     *
1247     * Since: 1.0.1
1248     */
1249     g_object_class_install_property(objectClass, PROP_FULL_CONTENT_ZOOM,
1250                                     g_param_spec_boolean("full-content-zoom",
1251                                                          "Full content zoom",
1252                                                          "Whether the full content is scaled when zooming",
1253                                                          FALSE,
1254                                                          WEBKIT_PARAM_READWRITE));
1255
1256     g_type_class_add_private(webViewClass, sizeof(WebKitWebViewPrivate));
1257 }
1258
1259 static void webkit_web_view_screen_changed(WebKitWebView* webView, GdkScreen* previousScreen, gpointer userdata)
1260 {
1261     WebKitWebViewPrivate* priv = webView->priv;
1262     WebKitWebSettings* webSettings = priv->webSettings;
1263     Settings* settings = core(webView)->settings();
1264
1265     guint defaultFontSize, defaultMonospaceFontSize, minimumFontSize, minimumLogicalFontSize;
1266
1267     g_object_get(G_OBJECT(webSettings),
1268                  "default-font-size", &defaultFontSize,
1269                  "default-monospace-font-size", &defaultMonospaceFontSize,
1270                  "minimum-font-size", &minimumFontSize,
1271                  "minimum-logical-font-size", &minimumLogicalFontSize,
1272                  NULL);
1273
1274     gdouble DPI = defaultDPI;
1275 #if GTK_CHECK_VERSION(2,10,0)
1276     GdkScreen* screen = gtk_widget_has_screen(GTK_WIDGET(webView)) ? gtk_widget_get_screen(GTK_WIDGET(webView)) : gdk_screen_get_default();
1277     if (screen) {
1278         DPI = gdk_screen_get_resolution(screen);
1279         // gdk_screen_get_resolution() returns -1 when no DPI is set.
1280         if (DPI == -1)
1281             DPI = defaultDPI;
1282     }
1283 #endif
1284     ASSERT(DPI > 0);
1285     settings->setDefaultFontSize(defaultFontSize / 72.0 * DPI);
1286     settings->setDefaultFixedFontSize(defaultMonospaceFontSize / 72.0 * DPI);
1287     settings->setMinimumFontSize(minimumFontSize / 72.0 * DPI);
1288     settings->setMinimumLogicalFontSize(minimumLogicalFontSize / 72.0 * DPI);
1289 }
1290
1291 static void webkit_web_view_update_settings(WebKitWebView* webView)
1292 {
1293     WebKitWebViewPrivate* priv = webView->priv;
1294     WebKitWebSettings* webSettings = priv->webSettings;
1295     Settings* settings = core(webView)->settings();
1296
1297     gchar* defaultEncoding, *cursiveFontFamily, *defaultFontFamily, *fantasyFontFamily, *monospaceFontFamily, *sansSerifFontFamily, *serifFontFamily, *userStylesheetUri;
1298     gboolean autoLoadImages, autoShrinkImages, printBackgrounds, enableScripts, enablePlugins, resizableTextAreas;
1299
1300     g_object_get(G_OBJECT(webSettings),
1301                  "default-encoding", &defaultEncoding,
1302                  "cursive-font-family", &cursiveFontFamily,
1303                  "default-font-family", &defaultFontFamily,
1304                  "fantasy-font-family", &fantasyFontFamily,
1305                  "monospace-font-family", &monospaceFontFamily,
1306                  "sans-serif-font-family", &sansSerifFontFamily,
1307                  "serif-font-family", &serifFontFamily,
1308                  "auto-load-images", &autoLoadImages,
1309                  "auto-shrink-images", &autoShrinkImages,
1310                  "print-backgrounds", &printBackgrounds,
1311                  "enable-scripts", &enableScripts,
1312                  "enable-plugins", &enablePlugins,
1313                  "resizable-text-areas", &resizableTextAreas,
1314                  "user-stylesheet-uri", &userStylesheetUri,
1315                  NULL);
1316
1317     settings->setDefaultTextEncodingName(defaultEncoding);
1318     settings->setCursiveFontFamily(cursiveFontFamily);
1319     settings->setStandardFontFamily(defaultFontFamily);
1320     settings->setFantasyFontFamily(fantasyFontFamily);
1321     settings->setFixedFontFamily(monospaceFontFamily);
1322     settings->setSansSerifFontFamily(sansSerifFontFamily);
1323     settings->setSerifFontFamily(serifFontFamily);
1324     settings->setLoadsImagesAutomatically(autoLoadImages);
1325     settings->setShrinksStandaloneImagesToFit(autoShrinkImages);
1326     settings->setShouldPrintBackgrounds(printBackgrounds);
1327     settings->setJavaScriptEnabled(enableScripts);
1328     settings->setPluginsEnabled(enablePlugins);
1329     settings->setTextAreasAreResizable(resizableTextAreas);
1330     settings->setUserStyleSheetLocation(KURL(userStylesheetUri));
1331
1332     g_free(defaultEncoding);
1333     g_free(cursiveFontFamily);
1334     g_free(defaultFontFamily);
1335     g_free(fantasyFontFamily);
1336     g_free(monospaceFontFamily);
1337     g_free(sansSerifFontFamily);
1338     g_free(serifFontFamily);
1339     g_free(userStylesheetUri);
1340
1341     webkit_web_view_screen_changed(webView, NULL, NULL);
1342 }
1343
1344 static void webkit_web_view_settings_notify(WebKitWebSettings* webSettings, GParamSpec* pspec, WebKitWebView* webView)
1345 {
1346     Settings* settings = core(webView)->settings();
1347
1348     const gchar* name = g_intern_string(pspec->name);
1349     GValue value = { 0, { { 0 } } };
1350     g_value_init(&value, pspec->value_type);
1351     g_object_get_property(G_OBJECT(webSettings), name, &value);
1352
1353     if (name == g_intern_string("default-encoding"))
1354         settings->setDefaultTextEncodingName(g_value_get_string(&value));
1355     else if (name == g_intern_string("cursive-font-family"))
1356         settings->setCursiveFontFamily(g_value_get_string(&value));
1357     else if (name == g_intern_string("default-font-family"))
1358         settings->setStandardFontFamily(g_value_get_string(&value));
1359     else if (name == g_intern_string("fantasy-font-family"))
1360         settings->setFantasyFontFamily(g_value_get_string(&value));
1361     else if (name == g_intern_string("monospace-font-family"))
1362         settings->setFixedFontFamily(g_value_get_string(&value));
1363     else if (name == g_intern_string("sans-serif-font-family"))
1364         settings->setSansSerifFontFamily(g_value_get_string(&value));
1365     else if (name == g_intern_string("serif-font-family"))
1366         settings->setSerifFontFamily(g_value_get_string(&value));
1367     else if (name == g_intern_string("default-font-size"))
1368         settings->setDefaultFontSize(g_value_get_int(&value));
1369     else if (name == g_intern_string("default-monospace-font-size"))
1370         settings->setDefaultFixedFontSize(g_value_get_int(&value));
1371     else if (name == g_intern_string("minimum-font-size"))
1372         settings->setMinimumFontSize(g_value_get_int(&value));
1373     else if (name == g_intern_string("minimum-logical-font-size"))
1374         settings->setMinimumLogicalFontSize(g_value_get_int(&value));
1375     else if (name == g_intern_string("auto-load-images"))
1376         settings->setLoadsImagesAutomatically(g_value_get_boolean(&value));
1377     else if (name == g_intern_string("auto-shrink-images"))
1378         settings->setShrinksStandaloneImagesToFit(g_value_get_boolean(&value));
1379     else if (name == g_intern_string("print-backgrounds"))
1380         settings->setShouldPrintBackgrounds(g_value_get_boolean(&value));
1381     else if (name == g_intern_string("enable-scripts"))
1382         settings->setJavaScriptEnabled(g_value_get_boolean(&value));
1383     else if (name == g_intern_string("enable-plugins"))
1384         settings->setPluginsEnabled(g_value_get_boolean(&value));
1385     else if (name == g_intern_string("resizable-text-areas"))
1386         settings->setTextAreasAreResizable(g_value_get_boolean(&value));
1387     else if (name == g_intern_string("user-stylesheet-uri"))
1388         settings->setUserStyleSheetLocation(KURL(g_value_get_string(&value)));
1389     else if (!g_object_class_find_property(G_OBJECT_GET_CLASS(webSettings), name))
1390         g_warning("Unexpected setting '%s'", name);
1391     g_value_unset(&value);
1392 }
1393
1394 static void webkit_web_view_init(WebKitWebView* webView)
1395 {
1396     WebKitWebViewPrivate* priv = WEBKIT_WEB_VIEW_GET_PRIVATE(webView);
1397     webView->priv = priv;
1398
1399     priv->imContext = gtk_im_multicontext_new();
1400     priv->corePage = new Page(new WebKit::ChromeClient(webView), new WebKit::ContextMenuClient(webView), new WebKit::EditorClient(webView), new WebKit::DragClient, new WebKit::InspectorClient);
1401
1402     priv->horizontalAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
1403     priv->verticalAdjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
1404
1405 #if GLIB_CHECK_VERSION(2,10,0)
1406     g_object_ref_sink(priv->horizontalAdjustment);
1407     g_object_ref_sink(priv->verticalAdjustment);
1408 #else
1409     g_object_ref(priv->horizontalAdjustment);
1410     gtk_object_sink(GTK_OBJECT(priv->horizontalAdjustment));
1411     g_object_ref(priv->verticalAdjustment);
1412     gtk_object_sink(GTK_OBJECT(priv->verticalAdjustment));
1413 #endif
1414
1415     GTK_WIDGET_SET_FLAGS(webView, GTK_CAN_FOCUS);
1416     priv->mainFrame = WEBKIT_WEB_FRAME(webkit_web_frame_new(webView));
1417     priv->lastPopupXPosition = priv->lastPopupYPosition = -1;
1418     priv->editable = false;
1419
1420     priv->backForwardList = webkit_web_back_forward_list_new_with_web_view(webView);
1421
1422     priv->zoomFullContent = FALSE;
1423
1424 #if GTK_CHECK_VERSION(2,10,0)
1425     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
1426 #else
1427     GdkAtom textHtml = gdk_atom_intern("text/html", false);
1428 #endif
1429     /* Targets for copy */
1430     priv->copy_target_list = gtk_target_list_new(NULL, 0);
1431     gtk_target_list_add(priv->copy_target_list, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
1432     gtk_target_list_add_text_targets(priv->copy_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
1433
1434     /* Targets for pasting */
1435     priv->paste_target_list = gtk_target_list_new(NULL, 0);
1436     gtk_target_list_add(priv->paste_target_list, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
1437     gtk_target_list_add_text_targets(priv->paste_target_list, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
1438
1439     priv->webSettings = webkit_web_settings_new();
1440     webkit_web_view_update_settings(webView);
1441     g_signal_connect(webView, "screen-changed", G_CALLBACK(webkit_web_view_screen_changed), NULL);
1442     g_signal_connect(priv->webSettings, "notify", G_CALLBACK(webkit_web_view_settings_notify), webView);
1443 }
1444
1445 GtkWidget* webkit_web_view_new(void)
1446 {
1447     WebKitWebView* webView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW, NULL));
1448
1449     return GTK_WIDGET(webView);
1450 }
1451
1452 void webkit_web_view_set_settings(WebKitWebView* webView, WebKitWebSettings* webSettings)
1453 {
1454     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1455     g_return_if_fail(WEBKIT_IS_WEB_SETTINGS(webSettings));
1456
1457     WebKitWebViewPrivate* priv = webView->priv;
1458     g_signal_handlers_disconnect_by_func(priv->webSettings, (gpointer)webkit_web_view_settings_notify, webView);
1459     g_object_unref(priv->webSettings);
1460     g_object_ref(webSettings);
1461     priv->webSettings = webSettings;
1462     webkit_web_view_update_settings(webView);
1463     g_signal_connect(webSettings, "notify", G_CALLBACK(webkit_web_view_settings_notify), webView);
1464     g_object_notify(G_OBJECT(webView), "settings");
1465 }
1466
1467 WebKitWebSettings* webkit_web_view_get_settings(WebKitWebView* webView)
1468 {
1469     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1470
1471     WebKitWebViewPrivate* priv = webView->priv;
1472     return priv->webSettings;
1473 }
1474
1475 /**
1476  * webkit_web_view_set_maintains_back_forward_list:
1477  * @webView: a #WebKitWebView
1478  * @flag: to tell the view to maintain a back or forward list
1479  *
1480  * Set the view to maintain a back or forward list of history items.
1481  */
1482 void webkit_web_view_set_maintains_back_forward_list(WebKitWebView* webView, gboolean flag)
1483 {
1484     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1485
1486     core(webView)->backForwardList()->setEnabled(flag);
1487 }
1488
1489 /**
1490  * webkit_web_view_get_back_forward_list:
1491  * @webView: a #WebKitWebView
1492  *
1493  * Returns a #WebKitWebBackForwardList
1494  *
1495  * Return value: the #WebKitWebBackForwardList
1496  */
1497 WebKitWebBackForwardList* webkit_web_view_get_back_forward_list(WebKitWebView* webView)
1498 {
1499     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1500
1501     WebKitWebViewPrivate* priv = webView->priv;
1502
1503     if (!core(webView) || !core(webView)->backForwardList()->enabled())
1504         return NULL;
1505
1506     return priv->backForwardList;
1507 }
1508
1509 /**
1510  * webkit_web_view_go_to_back_forward_item:
1511  * @webView: a #WebKitWebView
1512  * @item: a #WebKitWebHistoryItem*
1513  *
1514  * Go to the specified #WebKitWebHistoryItem
1515  *
1516  * Return value: %TRUE if loading of item is successful, %FALSE if not
1517  */
1518 gboolean webkit_web_view_go_to_back_forward_item(WebKitWebView* webView, WebKitWebHistoryItem* item)
1519 {
1520     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1521     g_return_val_if_fail(WEBKIT_IS_WEB_HISTORY_ITEM(item), FALSE);
1522
1523     // FIXME: This always returns TRUE
1524     core(webView)->goToItem(core(item), FrameLoadTypeIndexedBackForward);
1525     return TRUE;
1526 }
1527
1528 /**
1529  * webkit_web_view_go_back:
1530  * @web_view: a #WebKitWebView
1531  *
1532  * Loads the previous history item.
1533  */
1534 void webkit_web_view_go_back(WebKitWebView* webView)
1535 {
1536     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1537
1538     core(webView)->goBack();
1539 }
1540
1541 /**
1542  * webkit_web_view_go_back_or_forward:
1543  * @web_view: a #WebKitWebView
1544  * @steps: the number of steps
1545  *
1546  * Loads the history item that is the number of @steps away from the current
1547  * item. Negative values represent steps backward while positive values
1548  * represent steps forward.
1549  */
1550 void webkit_web_view_go_back_or_forward(WebKitWebView* webView, gint steps)
1551 {
1552     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1553
1554     Frame* frame = core(webView)->mainFrame();
1555     frame->loader()->goBackOrForward(steps);
1556 }
1557
1558 /**
1559  * webkit_web_view_go_forward:
1560  * @web_view: a #WebKitWebView
1561  *
1562  * Loads the next history item.
1563  */
1564 void webkit_web_view_go_forward(WebKitWebView* webView)
1565 {
1566     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1567
1568     core(webView)->goForward();
1569 }
1570
1571 /**
1572  * webkit_web_view_can_go_back:
1573  * @web_view: a #WebKitWebView
1574  *
1575  * Determines whether #web_view has a previous history item.
1576  *
1577  * Return value: %TRUE if able to move back, %FALSE otherwise
1578  */
1579 gboolean webkit_web_view_can_go_back(WebKitWebView* webView)
1580 {
1581     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1582
1583     if (!core(webView) || !core(webView)->backForwardList()->backItem())
1584         return FALSE;
1585
1586     return TRUE;
1587 }
1588
1589 /**
1590  * webkit_web_view_can_go_back_or_forward:
1591  * @web_view: a #WebKitWebView
1592  * @steps: the number of steps
1593  *
1594  * Determines whether #web_view has a history item of @steps. Negative values
1595  * represent steps backward while positive values represent steps forward.
1596  *
1597  * Return value: %TRUE if able to move back or forward the given number of
1598  * steps, %FALSE otherwise
1599  */
1600 gboolean webkit_web_view_can_go_back_or_forward(WebKitWebView* webView, gint steps)
1601 {
1602     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1603
1604     Frame* frame = core(webView)->mainFrame();
1605     return frame->loader()->canGoBackOrForward(steps);
1606 }
1607
1608 /**
1609  * webkit_web_view_can_go_forward:
1610  * @web_view: a #WebKitWebView
1611  *
1612  * Determines whether #web_view has a next history item.
1613  *
1614  * Return value: %TRUE if able to move forward, %FALSE otherwise
1615  */
1616 gboolean webkit_web_view_can_go_forward(WebKitWebView* webView)
1617 {
1618     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1619
1620     Page* page = core(webView);
1621
1622     if (!page)
1623         return FALSE;
1624
1625     if (!page->backForwardList()->forwardItem())
1626         return FALSE;
1627
1628     return TRUE;
1629 }
1630
1631 void webkit_web_view_open(WebKitWebView* webView, const gchar* uri)
1632 {
1633     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1634     g_return_if_fail(uri);
1635
1636     Frame* frame = core(webView)->mainFrame();
1637     frame->loader()->load(ResourceRequest(KURL(String::fromUTF8(uri))));
1638 }
1639
1640 void webkit_web_view_reload(WebKitWebView* webView)
1641 {
1642     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1643
1644     core(webView)->mainFrame()->loader()->reload();
1645 }
1646
1647 void webkit_web_view_load_string(WebKitWebView* webView, const gchar* content, const gchar* contentMimeType, const gchar* contentEncoding, const gchar* baseUri)
1648 {
1649     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1650     g_return_if_fail(content);
1651
1652     Frame* frame = core(webView)->mainFrame();
1653
1654     KURL url(baseUri ? String::fromUTF8(baseUri) : "");
1655     RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(strdup(content), strlen(content));
1656     SubstituteData substituteData(sharedBuffer.release(), contentMimeType ? String(contentMimeType) : "text/html", contentEncoding ? String(contentEncoding) : "UTF-8", KURL("about:blank"), url);
1657
1658     frame->loader()->load(ResourceRequest(url), substituteData);
1659 }
1660
1661 void webkit_web_view_load_html_string(WebKitWebView* webView, const gchar* content, const gchar* baseUri)
1662 {
1663     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1664     g_return_if_fail(content);
1665
1666     webkit_web_view_load_string(webView, content, NULL, NULL, baseUri);
1667 }
1668
1669 void webkit_web_view_stop_loading(WebKitWebView* webView)
1670 {
1671     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1672
1673     Frame* frame = core(webView)->mainFrame();
1674
1675     if (FrameLoader* loader = frame->loader())
1676         loader->stopAllLoaders();
1677 }
1678
1679 /**
1680  * webkit_web_view_search_text:
1681  * @web_view: a #WebKitWebView
1682  * @text: a string to look for
1683  * @forward: whether to find forward or not
1684  * @case_sensitive: whether to respect the case of text
1685  * @wrap: whether to continue looking at the beginning after reaching the end
1686  *
1687  * Looks for a specified string inside #web_view.
1688  *
1689  * Return value: %TRUE on success or %FALSE on failure
1690  */
1691 gboolean webkit_web_view_search_text(WebKitWebView* webView, const gchar* string, gboolean caseSensitive, gboolean forward, gboolean shouldWrap)
1692 {
1693     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1694     g_return_val_if_fail(string, FALSE);
1695
1696     TextCaseSensitivity caseSensitivity = caseSensitive ? TextCaseSensitive : TextCaseInsensitive;
1697     FindDirection direction = forward ? FindDirectionForward : FindDirectionBackward;
1698
1699     return core(webView)->findString(String::fromUTF8(string), caseSensitivity, direction, shouldWrap);
1700 }
1701
1702 /**
1703  * webkit_web_view_mark_text_matches:
1704  * @web_view: a #WebKitWebView
1705  * @string: a string to look for
1706  * @case_sensitive: whether to respect the case of text
1707  * @limit: the maximum number of strings to look for or %0 for all
1708  *
1709  * Attempts to highlight all occurances of #string inside #web_view.
1710  *
1711  * Return value: the number of strings highlighted
1712  */
1713 guint webkit_web_view_mark_text_matches(WebKitWebView* webView, const gchar* string, gboolean caseSensitive, guint limit)
1714 {
1715     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
1716     g_return_val_if_fail(string, 0);
1717
1718     TextCaseSensitivity caseSensitivity = caseSensitive ? TextCaseSensitive : TextCaseInsensitive;
1719
1720     return core(webView)->markAllMatchesForText(String::fromUTF8(string), caseSensitivity, false, limit);
1721 }
1722
1723 /**
1724  * webkit_web_view_set_highlight_text_matches:
1725  * @web_view: a #WebKitWebView
1726  * @highlight: whether to highlight text matches
1727  *
1728  * Highlights text matches previously marked by webkit_web_view_mark_text_matches.
1729  */
1730 void webkit_web_view_set_highlight_text_matches(WebKitWebView* webView, gboolean shouldHighlight)
1731 {
1732     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1733
1734     core(webView)->mainFrame()->setMarkedTextMatchesAreHighlighted(shouldHighlight);
1735 }
1736
1737 /**
1738  * webkit_web_view_unmark_text_matches:
1739  * @web_view: a #WebKitWebView
1740  *
1741  * Removes highlighting previously set by webkit_web_view_mark_text_matches.
1742  */
1743 void webkit_web_view_unmark_text_matches(WebKitWebView* webView)
1744 {
1745     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1746
1747     return core(webView)->unmarkAllTextMatches();
1748 }
1749
1750 WebKitWebFrame* webkit_web_view_get_main_frame(WebKitWebView* webView)
1751 {
1752     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1753
1754     WebKitWebViewPrivate* priv = webView->priv;
1755     return priv->mainFrame;
1756 }
1757
1758 /**
1759  * webkit_web_view_get_focused_frame:
1760  * @web_view: a #WebKitWebView
1761  *
1762  * Returns the frame that has focus or an active text selection.
1763  *
1764  * Return value: The focused #WebKitWebFrame or %NULL if no frame is focused
1765  */
1766 WebKitWebFrame* webkit_web_view_get_focused_frame(WebKitWebView* webView)
1767 {
1768     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
1769
1770     Frame* focusedFrame = core(webView)->focusController()->focusedFrame();
1771     return kit(focusedFrame);
1772 }
1773
1774 void webkit_web_view_execute_script(WebKitWebView* webView, const gchar* script)
1775 {
1776     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1777     g_return_if_fail(script);
1778
1779     if (FrameLoader* loader = core(webView)->mainFrame()->loader())
1780         loader->executeScript(String::fromUTF8(script), true);
1781 }
1782
1783 /**
1784  * webkit_web_view_cut_clipboard:
1785  * @web_view: a #WebKitWebView
1786  *
1787  * Determines whether or not it is currently possible to cut to the clipboard.
1788  *
1789  * Return value: %TRUE if a selection can be cut, %FALSE if not
1790  */
1791 gboolean webkit_web_view_can_cut_clipboard(WebKitWebView* webView)
1792 {
1793     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1794
1795     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1796     return frame->editor()->canCut() || frame->editor()->canDHTMLCut();
1797 }
1798
1799 /**
1800  * webkit_web_view_copy_clipboard:
1801  * @web_view: a #WebKitWebView
1802  *
1803  * Determines whether or not it is currently possible to copy to the clipboard.
1804  *
1805  * Return value: %TRUE if a selection can be copied, %FALSE if not
1806  */
1807 gboolean webkit_web_view_can_copy_clipboard(WebKitWebView* webView)
1808 {
1809     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1810
1811     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1812     return frame->editor()->canCopy() || frame->editor()->canDHTMLCopy();
1813 }
1814
1815 /**
1816  * webkit_web_view_paste_clipboard:
1817  * @web_view: a #WebKitWebView
1818  *
1819  * Determines whether or not it is currently possible to paste from the clipboard.
1820  *
1821  * Return value: %TRUE if a selection can be pasted, %FALSE if not
1822  */
1823 gboolean webkit_web_view_can_paste_clipboard(WebKitWebView* webView)
1824 {
1825     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1826
1827     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1828     return frame->editor()->canPaste() || frame->editor()->canDHTMLPaste();
1829 }
1830
1831 /**
1832  * webkit_web_view_cut_clipboard:
1833  * @web_view: a #WebKitWebView
1834  *
1835  * Cuts the current selection inside the @web_view to the clipboard.
1836  */
1837 void webkit_web_view_cut_clipboard(WebKitWebView* webView)
1838 {
1839     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1840
1841     if (webkit_web_view_can_cut_clipboard(webView))
1842         g_signal_emit(webView, webkit_web_view_signals[CUT_CLIPBOARD], 0);
1843 }
1844
1845 /**
1846  * webkit_web_view_copy_clipboard:
1847  * @web_view: a #WebKitWebView
1848  *
1849  * Copies the current selection inside the @web_view to the clipboard.
1850  */
1851 void webkit_web_view_copy_clipboard(WebKitWebView* webView)
1852 {
1853     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1854
1855     if (webkit_web_view_can_copy_clipboard(webView))
1856         g_signal_emit(webView, webkit_web_view_signals[COPY_CLIPBOARD], 0);
1857 }
1858
1859 /**
1860  * webkit_web_view_paste_clipboard:
1861  * @web_view: a #WebKitWebView
1862  *
1863  * Pastes the current contents of the clipboard to the @web_view.
1864  */
1865 void webkit_web_view_paste_clipboard(WebKitWebView* webView)
1866 {
1867     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1868
1869     if (webkit_web_view_can_paste_clipboard(webView))
1870         g_signal_emit(webView, webkit_web_view_signals[PASTE_CLIPBOARD], 0);
1871 }
1872
1873 /**
1874  * webkit_web_view_delete_selection:
1875  * @web_view: a #WebKitWebView
1876  *
1877  * Deletes the current selection inside the @web_view.
1878  */
1879 void webkit_web_view_delete_selection(WebKitWebView* webView)
1880 {
1881     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1882
1883     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1884     frame->editor()->performDelete();
1885 }
1886
1887 /**
1888  * webkit_web_view_has_selection:
1889  * @web_view: a #WebKitWebView
1890  *
1891  * Determines whether text was selected.
1892  *
1893  * Return value: %TRUE if there is selected text, %FALSE if not
1894  */
1895 gboolean webkit_web_view_has_selection(WebKitWebView* webView)
1896 {
1897     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1898
1899     return !core(webView)->selection().isNone();
1900 }
1901
1902 /**
1903  * webkit_web_view_get_selected_text:
1904  * @web_view: a #WebKitWebView
1905  *
1906  * Retrieves the selected text if any.
1907  *
1908  * Return value: a newly allocated string with the selection or %NULL
1909  */
1910 gchar* webkit_web_view_get_selected_text(WebKitWebView* webView)
1911 {
1912     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 0);
1913
1914     Frame* frame = core(webView)->focusController()->focusedOrMainFrame();
1915     return g_strdup(frame->selectedText().utf8().data());
1916 }
1917
1918 /**
1919  * webkit_web_view_select_all:
1920  * @web_view: a #WebKitWebView
1921  *
1922  * Attempts to select everything inside the @web_view.
1923  */
1924 void webkit_web_view_select_all(WebKitWebView* webView)
1925 {
1926     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1927
1928     g_signal_emit(webView, webkit_web_view_signals[SELECT_ALL], 0);
1929 }
1930
1931 /**
1932  * webkit_web_view_get_editable:
1933  * @web_view: a #WebKitWebView
1934  *
1935  * Returns whether the user is allowed to edit the document.
1936  *
1937  * Returns %TRUE if @web_view allows the user to edit the HTML document, %FALSE if
1938  * it doesn't. You can change @web_view's document programmatically regardless of
1939  * this setting.
1940  *
1941  * Return value: a #gboolean indicating the editable state
1942  */
1943 gboolean webkit_web_view_get_editable(WebKitWebView* webView)
1944 {
1945     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
1946
1947     WebKitWebViewPrivate* priv = webView->priv;
1948
1949     return priv->editable;
1950 }
1951
1952 /**
1953  * webkit_web_view_set_editable:
1954  * @web_view: a #WebKitWebView
1955  * @flag: a #gboolean indicating the editable state
1956  *
1957  * Sets whether @web_view allows the user to edit its HTML document.
1958  *
1959  * If @flag is %TRUE, @web_view allows the user to edit the document. If @flag is
1960  * %FALSE, an element in @web_view's document can only be edited if the
1961  * CONTENTEDITABLE attribute has been set on the element or one of its parent
1962  * elements. You can change @web_view's document programmatically regardless of
1963  * this setting. By default a #WebKitWebView is not editable.
1964
1965  * Normally, an HTML document is not editable unless the elements within the
1966  * document are editable. This function provides a low-level way to make the
1967  * contents of a #WebKitWebView editable without altering the document or DOM
1968  * structure.
1969  */
1970 void webkit_web_view_set_editable(WebKitWebView* webView, gboolean flag)
1971 {
1972     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
1973
1974     WebKitWebViewPrivate* priv = webView->priv;
1975
1976     Frame* frame = core(webView)->mainFrame();
1977     g_return_if_fail(frame);
1978
1979     // TODO: What happens when the frame is replaced?
1980     flag = flag != FALSE;
1981     if (flag == priv->editable)
1982         return;
1983
1984     priv->editable = flag;
1985
1986     if (flag) {
1987         frame->applyEditingStyleToBodyElement();
1988         // TODO: If the WebKitWebView is made editable and the selection is empty, set it to something.
1989         //if (!webkit_web_view_get_selected_dom_range(webView))
1990         //    mainFrame->setSelectionFromNone();
1991     } else
1992         frame->removeEditingStyleFromBodyElement();
1993     g_object_notify(G_OBJECT(webView), "editable");
1994 }
1995
1996 /**
1997  * webkit_web_view_get_copy_target_list:
1998  * @web_view: a #WebKitWebView
1999  *
2000  * This function returns the list of targets this #WebKitWebView can
2001  * provide for clipboard copying and as DND source. The targets in the list are
2002  * added with %info values from the #WebKitWebViewTargetInfo enum,
2003  * using gtk_target_list_add() and
2004  * gtk_target_list_add_text_targets().
2005  *
2006  * Return value: the #GtkTargetList
2007  **/
2008 GtkTargetList* webkit_web_view_get_copy_target_list(WebKitWebView* webView)
2009 {
2010     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
2011
2012     WebKitWebViewPrivate* priv = webView->priv;
2013     return priv->copy_target_list;
2014 }
2015
2016 /**
2017  * webkit_web_view_get_paste_target_list:
2018  * @web_view: a #WebKitWebView
2019  *
2020  * This function returns the list of targets this #WebKitWebView can
2021  * provide for clipboard pasting and as DND destination. The targets in the list are
2022  * added with %info values from the #WebKitWebViewTargetInfo enum,
2023  * using gtk_target_list_add() and
2024  * gtk_target_list_add_text_targets().
2025  *
2026  * Return value: the #GtkTargetList
2027  **/
2028 GtkTargetList* webkit_web_view_get_paste_target_list(WebKitWebView* webView)
2029 {
2030     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), NULL);
2031
2032     WebKitWebViewPrivate* priv = webView->priv;
2033     return priv->paste_target_list;
2034 }
2035
2036 /**
2037  * webkit_web_view_get_transparent:
2038  * @web_view: a #WebKitWebView
2039  *
2040  * Returns whether the #WebKitWebView has a transparent background.
2041  *
2042  * Return value: %FALSE when the #WebKitWebView draws a solid background
2043  * (the default), otherwise %TRUE.
2044  */
2045 gboolean webkit_web_view_get_transparent(WebKitWebView* webView)
2046 {
2047     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
2048
2049     WebKitWebViewPrivate* priv = webView->priv;
2050     return priv->transparent;
2051 }
2052
2053 /**
2054  * webkit_web_view_set_transparent:
2055  * @web_view: a #WebKitWebView
2056  *
2057  * Sets whether the #WebKitWebView has a transparent background.
2058  *
2059  * Pass %FALSE to have the #WebKitWebView draw a solid background
2060  * (the default), otherwise %TRUE.
2061  */
2062 void webkit_web_view_set_transparent(WebKitWebView* webView, gboolean flag)
2063 {
2064     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2065
2066     WebKitWebViewPrivate* priv = webView->priv;
2067     priv->transparent = flag;
2068
2069     // TODO: This needs to be made persistent or it could become a problem when
2070     // the main frame is replaced.
2071     Frame* frame = core(webView)->mainFrame();
2072     g_return_if_fail(frame);
2073     frame->view()->setTransparent(flag);
2074     g_object_notify(G_OBJECT(webView), "transparent");
2075 }
2076
2077 /**
2078  * webkit_web_view_get_zoom_level:
2079  * @web_view: a #WebKitWebView
2080  *
2081  * Returns the zoom level of @web_view, i.e. the factor by which elements in
2082  * the page are scaled with respect to their original size.
2083  * If the "full-content-zoom" property is set to %FALSE (the default)
2084  * the zoom level changes the text size, or if %TRUE, scales all
2085  * elements in the page.
2086  *
2087  * Return value: the zoom level of @web_view
2088  *
2089  * Since: 1.0.1
2090  */
2091 gfloat webkit_web_view_get_zoom_level(WebKitWebView* webView)
2092 {
2093     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), 1.0f);
2094
2095     Frame* frame = core(webView)->mainFrame();
2096     if (!frame)
2097         return 1.0f;
2098
2099     return frame->zoomFactor();
2100 }
2101
2102 static void webkit_web_view_apply_zoom_level(WebKitWebView* webView, gfloat zoomLevel)
2103 {
2104     Frame* frame = core(webView)->mainFrame();
2105     if (!frame)
2106         return;
2107
2108     WebKitWebViewPrivate* priv = webView->priv;
2109     frame->setZoomFactor(zoomLevel, !priv->zoomFullContent);
2110 }
2111
2112 /**
2113  * webkit_web_view_set_zoom_level:
2114  * @web_view: a #WebKitWebView
2115  * @zoom_level: the new zoom level
2116  *
2117  * Sets the zoom level of @web_view, i.e. the factor by which elements in
2118  * the page are scaled with respect to their original size.
2119  * If the "full-content-zoom" property is set to %FALSE (the default)
2120  * the zoom level changes the text size, or if %TRUE, scales all
2121  * elements in the page.
2122  *
2123  * Since: 1.0.1
2124  */
2125 void webkit_web_view_set_zoom_level(WebKitWebView* webView, gfloat zoomLevel)
2126 {
2127     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2128
2129     webkit_web_view_apply_zoom_level(webView, zoomLevel);
2130     g_object_notify(G_OBJECT(webView), "zoom-level");
2131 }
2132
2133 /**
2134  * webkit_web_view_zoom_in:
2135  * @web_view: a #WebKitWebView
2136  *
2137  * Increases the zoom level of @web_view. The current zoom
2138  * level is incremented by the value of the "zoom-step"
2139  * property of the #WebKitWebSettings associated with @web_view.
2140  *
2141  * Since: 1.0.1
2142  */
2143 void webkit_web_view_zoom_in(WebKitWebView* webView)
2144 {
2145     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2146
2147     WebKitWebViewPrivate* priv = webView->priv;
2148     gfloat zoomMultiplierRatio;
2149     g_object_get(priv->webSettings, "zoom-step", &zoomMultiplierRatio, NULL);
2150
2151     webkit_web_view_set_zoom_level(webView, webkit_web_view_get_zoom_level(webView) + zoomMultiplierRatio);
2152 }
2153
2154 /**
2155  * webkit_web_view_zoom_out:
2156  * @web_view: a #WebKitWebView
2157  *
2158  * Decreases the zoom level of @web_view. The current zoom
2159  * level is decremented by the value of the "zoom-step"
2160  * property of the #WebKitWebSettings associated with @web_view.
2161  *
2162  * Since: 1.0.1
2163  */
2164 void webkit_web_view_zoom_out(WebKitWebView* webView)
2165 {
2166     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2167
2168     WebKitWebViewPrivate* priv = webView->priv;
2169     gfloat zoomMultiplierRatio;
2170     g_object_get(priv->webSettings, "zoom-step", &zoomMultiplierRatio, NULL);
2171
2172     webkit_web_view_set_zoom_level(webView, webkit_web_view_get_zoom_level(webView) - zoomMultiplierRatio);
2173 }
2174
2175 /**
2176  * webkit_web_view_get_full_content_zoom:
2177  * @web_view: a #WebKitWebView
2178  *
2179  * Returns whether the zoom level affects only text or all elements.
2180  *
2181  * Return value: %FALSE if only text should be scaled (the default),
2182  * %TRUE if the full content of the view should be scaled.
2183  *
2184  * Since: 1.0.1
2185  */
2186 gboolean webkit_web_view_get_full_content_zoom(WebKitWebView* webView)
2187 {
2188     g_return_val_if_fail(WEBKIT_IS_WEB_VIEW(webView), FALSE);
2189
2190     WebKitWebViewPrivate* priv = webView->priv;
2191     return priv->zoomFullContent;
2192 }
2193
2194 /**
2195  * webkit_web_view_set_full_content_zoom:
2196  * @web_view: a #WebKitWebView
2197  * @full_content_zoom: %FALSE if only text should be scaled (the default),
2198  * %TRUE if the full content of the view should be scaled.
2199  *
2200  * Sets whether the zoom level affects only text or all elements.
2201  *
2202  * Since: 1.0.1
2203  */
2204 void webkit_web_view_set_full_content_zoom(WebKitWebView* webView, gboolean zoomFullContent)
2205 {
2206     g_return_if_fail(WEBKIT_IS_WEB_VIEW(webView));
2207
2208     WebKitWebViewPrivate* priv = webView->priv;
2209     if (priv->zoomFullContent == zoomFullContent)
2210       return;
2211
2212     priv->zoomFullContent = zoomFullContent;
2213     webkit_web_view_apply_zoom_level(webView, webkit_web_view_get_zoom_level(webView));
2214
2215     g_object_notify(G_OBJECT(webView), "full-content-zoom");
2216 }
2217
2218 }