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