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