2007-11-09 Xan Lopez <xan@gnome.org>
[WebKit-https.git] / WebKit / gtk / Api / webkitgtkpage.cpp
1 /*
2  * Copyright (C) 2007 Holger Hans Peter Freyther
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30
31 #include "webkitgtkpage.h"
32 #include "webkitgtk-marshal.h"
33 #include "webkitgtkprivate.h"
34
35 #include "NotImplemented.h"
36 #include "ChromeClientGtk.h"
37 #include "ContextMenuClientGtk.h"
38 #include "DragClientGtk.h"
39 #include "EditorClientGtk.h"
40 #include "EventHandler.h"
41 #include "HitTestRequest.h"
42 #include "HitTestResult.h"
43 #include "GraphicsContext.h"
44 #include "InspectorClientGtk.h"
45 #include "FrameLoader.h"
46 #include "FrameView.h"
47 #include "PlatformKeyboardEvent.h"
48 #include "PlatformWheelEvent.h"
49 #include "SubstituteData.h"
50
51
52 using namespace WebKit;
53 using namespace WebCore;
54
55 extern "C" {
56
57 enum {
58     /* normal signals */
59     LOAD_STARTED,
60     LOAD_PROGRESS_CHANGED,
61     LOAD_FINISHED,
62     TITLE_CHANGED,
63     HOVERING_OVER_LINK,
64     STATUS_BAR_TEXT_CHANGED,
65     ICOND_LOADED,
66     SELECTION_CHANGED,
67     LAST_SIGNAL
68 };
69
70 static guint webkit_page_signals[LAST_SIGNAL] = { 0, };
71
72 G_DEFINE_TYPE(WebKitPage, webkit_page, GTK_TYPE_CONTAINER)
73
74 static gboolean webkit_page_expose_event(GtkWidget* widget, GdkEventExpose* event)
75 {
76     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
77     GdkRectangle clip;
78     gdk_region_get_clipbox(event->region, &clip);
79     cairo_t* cr = gdk_cairo_create(event->window);
80     GraphicsContext ctx(cr);
81     ctx.setGdkExposeEvent(event);
82     if (frame->renderer()) {
83         frame->view()->layoutIfNeededRecursive();
84         frame->view()->paint(&ctx, clip);
85     }
86     cairo_destroy(cr);
87
88     return FALSE;
89 }
90
91 static gboolean webkit_page_key_event(GtkWidget* widget, GdkEventKey* event)
92 {
93     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
94     return frame->eventHandler()->keyEvent(PlatformKeyboardEvent(event));
95 }
96
97 static gboolean webkit_page_button_event(GtkWidget* widget, GdkEventButton* event)
98 {
99     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
100
101     if (event->type == GDK_BUTTON_RELEASE)
102         return frame->eventHandler()->handleMouseReleaseEvent(PlatformMouseEvent(event));
103
104     // FIXME: need to keep track of subframe focus for key events
105     gtk_widget_grab_focus(GTK_WIDGET(widget));
106     return frame->eventHandler()->handleMousePressEvent(PlatformMouseEvent(event));
107 }
108
109 static gboolean webkit_page_motion_event(GtkWidget* widget, GdkEventMotion* event)
110 {
111     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
112     return frame->eventHandler()->mouseMoved(PlatformMouseEvent(event));
113 }
114
115 static gboolean webkit_page_scroll_event(GtkWidget* widget, GdkEventScroll* event)
116 {
117     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
118     PlatformWheelEvent wheelEvent(event);
119     return frame->eventHandler()->handleWheelEvent(wheelEvent);
120 }
121
122 static void webkit_page_size_allocate(GtkWidget* widget, GtkAllocation* allocation)
123 {
124     GTK_WIDGET_CLASS(webkit_page_parent_class)->size_allocate(widget,allocation);
125
126     Frame* frame = core(getFrameFromPage(WEBKIT_PAGE(widget)));
127     frame->view()->resize(allocation->width, allocation->height);
128     frame->forceLayout();
129     frame->view()->adjustViewSize();
130     frame->sendResizeEvent();
131 }
132
133 static void webkit_page_realize(GtkWidget* widget)
134 {
135     GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
136
137     GdkWindowAttr attributes;
138     attributes.window_type = GDK_WINDOW_CHILD;
139     attributes.x = widget->allocation.x;
140     attributes.y = widget->allocation.y;
141     attributes.width = widget->allocation.width;
142     attributes.height = widget->allocation.height;
143     attributes.wclass = GDK_INPUT_OUTPUT;
144     attributes.visual = gtk_widget_get_visual (widget);
145     attributes.colormap = gtk_widget_get_colormap (widget);
146     attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK
147                             | GDK_EXPOSURE_MASK
148                             | GDK_BUTTON_PRESS_MASK
149                             | GDK_BUTTON_RELEASE_MASK
150                             | GDK_POINTER_MOTION_MASK
151                             | GDK_KEY_PRESS_MASK
152                             | GDK_KEY_RELEASE_MASK
153                             | GDK_BUTTON_MOTION_MASK
154                             | GDK_BUTTON1_MOTION_MASK
155                             | GDK_BUTTON2_MOTION_MASK
156                             | GDK_BUTTON3_MOTION_MASK;
157
158     gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
159     widget->window = gdk_window_new(gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
160     gdk_window_set_user_data(widget->window, widget);
161 }
162
163 static void webkit_page_set_scroll_adjustments(WebKitPage* page, GtkAdjustment* hadj, GtkAdjustment* vadj)
164 {
165     FrameView* view = core(getFrameFromPage(page))->view();
166     view->setGtkAdjustments(hadj, vadj);
167 }
168
169 static void webkit_page_container_add(GtkContainer* container, GtkWidget* widget)
170 {
171     WebKitPage* page = WEBKIT_PAGE(container);
172     WebKitPagePrivate* private_data = WEBKIT_PAGE_GET_PRIVATE(page);
173
174     private_data->children.add(widget);
175     if (GTK_WIDGET_REALIZED(container))
176         gtk_widget_set_parent_window(widget, GTK_WIDGET(page)->window);
177     gtk_widget_set_parent(widget, GTK_WIDGET(container));
178 }
179
180 static void webkit_page_container_remove(GtkContainer* container, GtkWidget* widget)
181 {
182     WebKitPagePrivate* private_data = WEBKIT_PAGE_GET_PRIVATE(WEBKIT_PAGE(container));
183
184     if (private_data->children.contains(widget)) {
185         gtk_widget_unparent(widget);
186         private_data->children.remove(widget);
187     }
188 }
189
190 static void webkit_page_container_forall(GtkContainer* container, gboolean, GtkCallback callback, gpointer callbackData)
191 {
192     WebKitPagePrivate* privateData = WEBKIT_PAGE_GET_PRIVATE(WEBKIT_PAGE(container));
193
194     HashSet<GtkWidget*> children = privateData->children;
195     HashSet<GtkWidget*>::const_iterator end = children.end();
196     for (HashSet<GtkWidget*>::const_iterator current = children.begin(); current != end; ++current)
197         (*callback)(*current, callbackData);
198 }
199
200 static WebKitPage* webkit_page_real_create_page(WebKitPage*)
201 {
202     notImplemented();
203     return 0;
204 }
205
206 static WEBKIT_NAVIGATION_REQUEST_RESPONSE webkit_page_real_navigation_requested(WebKitPage*, WebKitFrame* frame, WebKitNetworkRequest*)
207 {
208     notImplemented();
209     return WEBKIT_ACCEPT_NAVIGATION_REQUEST;
210 }
211
212 static gchar* webkit_page_real_choose_file(WebKitPage*, WebKitFrame*, const gchar* old_name)
213 {
214     notImplemented();
215     return g_strdup(old_name);
216 }
217
218 static void webkit_page_real_java_script_alert(WebKitPage*, WebKitFrame*, const gchar*)
219 {
220     notImplemented();
221 }
222
223 static gboolean webkit_page_real_java_script_confirm(WebKitPage*, WebKitFrame*, const gchar*)
224 {
225     notImplemented();
226     return FALSE;
227 }
228
229 /**
230  * WebKitPage::java_script_prompt
231  *
232  * @return: NULL to cancel the prompt
233  */
234 static gchar* webkit_page_real_java_script_prompt(WebKitPage*, WebKitFrame*, const gchar*, const gchar* defaultValue)
235 {
236     notImplemented();
237     return g_strdup(defaultValue);
238 }
239
240 static void webkit_page_real_java_script_console_message(WebKitPage*, const gchar*, unsigned int, const gchar*)
241 {
242     notImplemented();
243 }
244
245 static void webkit_page_finalize(GObject* object)
246 {
247     webkit_page_stop_loading(WEBKIT_PAGE(object));
248
249     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(WEBKIT_PAGE(object));
250     delete pageData->page;
251     delete pageData->settings;
252     g_object_unref(pageData->mainFrame);
253     delete pageData->userAgent;
254
255     G_OBJECT_CLASS(webkit_page_parent_class)->finalize(object);
256 }
257
258 static void webkit_page_class_init(WebKitPageClass* pageClass)
259 {
260     g_type_class_add_private(pageClass, sizeof(WebKitPagePrivate));
261
262
263     /*
264      * signals
265      */
266     /**
267      * WebKitPage::load-started
268      * @page: the object on which the signal is emitted
269      * @frame: the frame going to do the load
270      *
271      * When a WebKitFrame begins to load this signal is emitted.
272      */
273     webkit_page_signals[LOAD_STARTED] = g_signal_new("load_started",
274             G_TYPE_FROM_CLASS(pageClass),
275             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
276             0,
277             NULL,
278             NULL,
279             g_cclosure_marshal_VOID__OBJECT,
280             G_TYPE_NONE, 1,
281             WEBKIT_TYPE_FRAME);
282
283     /**
284      * WebKitPage::load-progress-changed
285      * @page: The WebKitPage
286      * @progress: Global progress
287      */
288     webkit_page_signals[LOAD_PROGRESS_CHANGED] = g_signal_new("load_progress_changed",
289             G_TYPE_FROM_CLASS(pageClass),
290             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
291             0,
292             NULL,
293             NULL,
294             g_cclosure_marshal_VOID__INT,
295             G_TYPE_NONE, 1,
296             G_TYPE_INT);
297     
298     webkit_page_signals[LOAD_FINISHED] = g_signal_new("load_finished",
299             G_TYPE_FROM_CLASS(pageClass),
300             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
301             0,
302             NULL,
303             NULL,
304             g_cclosure_marshal_VOID__OBJECT,
305             G_TYPE_NONE, 1,
306             WEBKIT_TYPE_FRAME);
307
308     webkit_page_signals[TITLE_CHANGED] = g_signal_new("title_changed",
309             G_TYPE_FROM_CLASS(pageClass),
310             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
311             0,
312             NULL,
313             NULL,
314             webkit_marshal_VOID__STRING_STRING,
315             G_TYPE_NONE, 2,
316             G_TYPE_STRING, G_TYPE_STRING);
317
318     webkit_page_signals[HOVERING_OVER_LINK] = g_signal_new("hovering_over_link",
319             G_TYPE_FROM_CLASS(pageClass),
320             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
321             0,
322             NULL,
323             NULL,
324             webkit_marshal_VOID__STRING_STRING,
325             G_TYPE_NONE, 2,
326             G_TYPE_STRING,
327             G_TYPE_STRING);
328
329     webkit_page_signals[STATUS_BAR_TEXT_CHANGED] = g_signal_new("status_bar_text_changed",
330             G_TYPE_FROM_CLASS(pageClass),
331             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
332             0,
333             NULL,
334             NULL,
335             g_cclosure_marshal_VOID__STRING,
336             G_TYPE_NONE, 1,
337             G_TYPE_STRING);
338
339     webkit_page_signals[ICOND_LOADED] = g_signal_new("icon_loaded",
340             G_TYPE_FROM_CLASS(pageClass),
341             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
342             0,
343             NULL,
344             NULL,
345             g_cclosure_marshal_VOID__VOID,
346             G_TYPE_NONE, 0);
347
348     webkit_page_signals[SELECTION_CHANGED] = g_signal_new("selection_changed",
349             G_TYPE_FROM_CLASS(pageClass),
350             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
351             0,
352             NULL,
353             NULL,
354             g_cclosure_marshal_VOID__VOID,
355             G_TYPE_NONE, 0);
356
357
358     /*
359      * implementations of virtual methods
360      */
361     pageClass->create_page = webkit_page_real_create_page;
362     pageClass->navigation_requested = webkit_page_real_navigation_requested;
363     pageClass->choose_file = webkit_page_real_choose_file;
364     pageClass->java_script_alert = webkit_page_real_java_script_alert;
365     pageClass->java_script_confirm = webkit_page_real_java_script_confirm;
366     pageClass->java_script_prompt = webkit_page_real_java_script_prompt;
367     pageClass->java_script_console_message = webkit_page_real_java_script_console_message;
368
369     G_OBJECT_CLASS(pageClass)->finalize = webkit_page_finalize;
370
371     GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(pageClass);
372     widgetClass->realize = webkit_page_realize;
373     widgetClass->expose_event = webkit_page_expose_event;
374     widgetClass->key_press_event = webkit_page_key_event;
375     widgetClass->key_release_event = webkit_page_key_event;
376     widgetClass->button_press_event = webkit_page_button_event;
377     widgetClass->button_release_event = webkit_page_button_event;
378     widgetClass->motion_notify_event = webkit_page_motion_event;
379     widgetClass->scroll_event = webkit_page_scroll_event;
380     widgetClass->size_allocate = webkit_page_size_allocate;
381
382     GtkContainerClass* containerClass = GTK_CONTAINER_CLASS(pageClass);
383     containerClass->add = webkit_page_container_add;
384     containerClass->remove = webkit_page_container_remove;
385     containerClass->forall = webkit_page_container_forall;
386
387     /*
388      * make us scrollable (e.g. addable to a GtkScrolledWindow)
389      */
390     pageClass->set_scroll_adjustments = webkit_page_set_scroll_adjustments;
391     GTK_WIDGET_CLASS(pageClass)->set_scroll_adjustments_signal = g_signal_new("set_scroll_adjustments",
392             G_TYPE_FROM_CLASS(pageClass),
393             (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
394             G_STRUCT_OFFSET(WebKitPageClass, set_scroll_adjustments),
395             NULL, NULL,
396             webkit_marshal_VOID__OBJECT_OBJECT,
397             G_TYPE_NONE, 2,
398             GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
399 }
400
401 static void webkit_page_init(WebKitPage* page)
402 {
403     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(WEBKIT_PAGE(page));
404     pageData->page = new Page(new WebKit::ChromeClient(page), new WebKit::ContextMenuClient, new WebKit::EditorClient(page), new WebKit::DragClient, new WebKit::InspectorClient);
405
406     Settings* settings = pageData->page->settings();
407     settings->setLoadsImagesAutomatically(true);
408     settings->setMinimumFontSize(5);
409     settings->setMinimumLogicalFontSize(5);
410     settings->setShouldPrintBackgrounds(true);
411     settings->setJavaScriptEnabled(true);
412     settings->setDefaultFixedFontSize(14);
413     settings->setDefaultFontSize(14);
414     settings->setSerifFontFamily("Times New Roman");
415     settings->setSansSerifFontFamily("Arial");
416     settings->setFixedFontFamily("Courier");
417     settings->setStandardFontFamily("Arial");
418
419     GTK_WIDGET_SET_FLAGS(page, GTK_CAN_FOCUS);
420     pageData->mainFrame = WEBKIT_FRAME(webkit_frame_new(page));
421 }
422
423 GtkWidget* webkit_page_new(void)
424 {
425     WebKitPage* page = WEBKIT_PAGE(g_object_new(WEBKIT_TYPE_PAGE, NULL));
426
427     return GTK_WIDGET(page);
428 }
429
430 void webkit_page_set_settings(WebKitPage* page, WebKitSettings* settings)
431 {
432     notImplemented();
433 }
434
435 WebKitSettings* webkit_page_get_settings(WebKitPage* page)
436 {
437     notImplemented();
438     return 0;
439 }
440
441 void webkit_page_go_backward(WebKitPage* page)
442 {
443     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
444     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
445     frameData->frame->loader()->goBackOrForward(-1);
446 }
447
448 void webkit_page_go_forward(WebKitPage* page)
449 {
450     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
451     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
452     frameData->frame->loader()->goBackOrForward(1);
453 }
454
455 gboolean webkit_page_can_go_backward(WebKitPage* page)
456 {
457     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
458     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
459     return frameData->frame->loader()->canGoBackOrForward(-1);
460 }
461
462 gboolean webkit_page_can_go_forward(WebKitPage* page)
463 {
464     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
465     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
466     return frameData->frame->loader()->canGoBackOrForward(1);
467 }
468
469 void webkit_page_open(WebKitPage* page, const gchar* url)
470 {
471     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
472     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
473
474     DeprecatedString string = DeprecatedString::fromUtf8(url);
475     frameData->frame->loader()->load(ResourceRequest(KURL(string)));
476 }
477
478 void webkit_page_reload(WebKitPage* page)
479 {
480     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
481     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
482     frameData->frame->loader()->reload();
483 }
484
485 void webkit_page_load_string(WebKitPage* page, const gchar* content, const gchar* contentMimeType, const gchar* contentEncoding, const gchar* baseUrl)
486 {
487     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
488     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
489     
490     KURL url(DeprecatedString::fromUtf8(baseUrl));
491     RefPtr<SharedBuffer> sharedBuffer = new SharedBuffer(strdup(content), strlen(content));    
492     SubstituteData substituteData(sharedBuffer.release(), String(contentMimeType), String(contentEncoding), KURL("about:blank"), url);
493
494     frameData->frame->loader()->load(ResourceRequest(url), substituteData);
495 }
496
497 void webkit_page_load_html_string(WebKitPage* page, const gchar* content, const gchar* baseUrl)
498 {
499     webkit_page_load_string(page, content, "text/html", "UTF-8", baseUrl);
500 }
501
502 void webkit_page_stop_loading(WebKitPage* page)
503 {
504     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
505     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
506     
507     if (FrameLoader* loader = frameData->frame->loader())
508         loader->stopAllLoaders();
509         
510 }
511
512 WebKitFrame* webkit_page_get_main_frame(WebKitPage* page)
513 {
514     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
515     return pageData->mainFrame;
516 }
517
518 void webkit_page_execute_script(WebKitPage* page, const gchar* script)
519 {
520     WebKitPagePrivate* pageData = WEBKIT_PAGE_GET_PRIVATE(page);
521     WebKitFramePrivate* frameData = WEBKIT_FRAME_GET_PRIVATE(pageData->mainFrame);
522
523     if (FrameLoader* loader = frameData->frame->loader())
524         loader->executeScript(String::fromUTF8(script), true);
525 }
526 }