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