a92735a3347da8c48aa4cd033d35d50f8a9bc30c
[claws.git] / src / plugins / litehtml_viewer / lh_widget.cpp
1 /*
2  * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3  * Copyright(C) 1999-2015 the Claws Mail Team
4  * == Fancy Plugin ==
5  * This file Copyright (C) 2009-2015 Salvatore De Paolis
6  * <iwkse@claws-mail.org> and the Claws Mail Team
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write tothe Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <glib/gstdio.h>
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <curl/curl.h>
32 #include <gdk/gdk.h>
33
34 #include "utils.h"
35
36 #include "litehtml/litehtml.h"
37
38 #include "lh_prefs.h"
39 #include "lh_widget.h"
40 #include "lh_widget_wrapped.h"
41 #include "http.h"
42
43 extern "C" {
44 const gchar *prefs_common_get_uri_cmd(void);
45 }
46
47 char master_css[] = {
48 #include "css.inc"
49 };
50
51 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
52                 gpointer user_data);
53 static void size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation,
54                 gpointer user_data);
55 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
56                 gpointer user_data);
57 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
58         gpointer user_data);
59 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
60         gpointer user_data);
61 static void open_link_cb(GtkMenuItem *item, gpointer user_data);
62 static void copy_link_cb(GtkMenuItem *item, gpointer user_data);
63
64 lh_widget::lh_widget()
65 {
66         GtkWidget *item;
67
68         /* scrolled window */
69         m_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
70         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_scrolled_window),
71                         GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
72         g_signal_connect(m_scrolled_window, "size-allocate",
73                         G_CALLBACK(size_allocate_cb), this);
74
75         /* viewport */
76         GtkScrolledWindow *scw = GTK_SCROLLED_WINDOW(m_scrolled_window);
77         m_viewport = gtk_viewport_new(
78                         gtk_scrolled_window_get_hadjustment(scw),
79                         gtk_scrolled_window_get_vadjustment(scw));
80         gtk_container_add(GTK_CONTAINER(m_scrolled_window), m_viewport);
81
82         /* drawing area */
83         m_drawing_area = gtk_drawing_area_new();
84         gtk_container_add(GTK_CONTAINER(m_viewport), m_drawing_area);
85         g_signal_connect(m_drawing_area, "expose-event",
86                         G_CALLBACK(expose_event_cb), this);
87         g_signal_connect(m_drawing_area, "motion_notify_event",
88                         G_CALLBACK(motion_notify_event), this);
89         g_signal_connect(m_drawing_area, "button_press_event",
90                         G_CALLBACK(button_press_event), this);
91         g_signal_connect(m_drawing_area, "button_release_event",
92                         G_CALLBACK(button_release_event), this);
93
94         gtk_widget_show_all(m_scrolled_window);
95
96         /* context menu */
97         m_context_menu = gtk_menu_new();
98
99         item = gtk_menu_item_new_with_label(_("Open Link"));
100         g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(open_link_cb), this);
101         gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
102
103         item = gtk_menu_item_new_with_label(_("Copy Link Location"));
104         g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copy_link_cb), this);
105         gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
106
107         m_html = NULL;
108         m_rendered_width = 0;
109         m_context.load_master_stylesheet(master_css);
110
111         gtk_widget_set_events(m_drawing_area,
112                                 GDK_BUTTON_RELEASE_MASK
113                               | GDK_BUTTON_PRESS_MASK
114                               | GDK_POINTER_MOTION_MASK);
115 }
116
117 lh_widget::~lh_widget()
118 {
119         g_object_unref(m_drawing_area);
120         m_drawing_area = NULL;
121         g_object_unref(m_scrolled_window);
122         m_scrolled_window = NULL;
123         m_html = NULL;
124 }
125
126 GtkWidget *lh_widget::get_widget() const
127 {
128         return m_scrolled_window;
129 }
130
131 void lh_widget::set_caption(const litehtml::tchar_t* caption)
132 {
133         debug_print("lh_widget set_caption\n");
134         return;
135 }
136
137 void lh_widget::set_base_url(const litehtml::tchar_t* base_url)
138 {
139         debug_print("lh_widget set_base_url\n");
140         return;
141 }
142
143 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
144 {
145         debug_print("lh_widget on_anchor_click. url -> %s\n", url);
146         m_clicked_url = url;
147         
148         return;
149 }
150
151 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
152 {
153         debug_print("lh_widget import_css\n");
154         baseurl = master_css;
155 }
156
157 void lh_widget::get_client_rect(litehtml::position& client) const
158 {
159         if (m_drawing_area == NULL)
160                 return;
161
162         client.width = m_rendered_width;
163         client.height = m_height;
164         client.x = 0;
165         client.y = 0;
166
167 //      debug_print("lh_widget::get_client_rect: %dx%d\n",
168 //                      client.width, client.height);
169 }
170
171 GdkPixbuf *lh_widget::get_image(const litehtml::tchar_t* url, bool redraw_on_ready)
172 {
173         GError *error = NULL;
174         GdkPixbuf *pixbuf = NULL;
175         http* http_loader = NULL;
176
177         if (!lh_prefs_get()->enable_remote_content) {
178                 debug_print("blocking download of image from '%s'\n", url);
179                 return NULL;
180         }
181
182         debug_print("Loading... %s\n", url);
183         gchar *msg = g_strdup_printf("Loading %s ...", url);
184         lh_widget_statusbar_push(msg);
185         g_free(msg);
186         
187         http_loader = new http();
188         GInputStream *image = http_loader->load_url(url, &error);
189     
190         if (error || !image) {
191             if (error) {
192                 g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
193                 g_clear_error(&error);
194             }
195             goto statusbar_pop;
196         }
197
198         pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
199         if (error) {
200             g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
201             pixbuf = NULL;
202             g_clear_error(&error);
203         }
204
205 /*      if (redraw_on_ready) {
206                 redraw();
207         }*/
208
209 statusbar_pop:
210         lh_widget_statusbar_pop();
211         if (http_loader) {
212                 delete http_loader;
213         }
214         
215         return pixbuf;
216 }
217
218 void lh_widget::open_html(const gchar *contents)
219 {
220         lh_widget_statusbar_push("Loading HTML part ...");
221         m_html = litehtml::document::createFromString(contents, this, &m_context);
222         m_rendered_width = 0;
223         if (m_html != NULL) {
224                 debug_print("lh_widget::open_html created document\n");
225                 redraw();
226         }
227         lh_widget_statusbar_pop();
228 }
229
230 void lh_widget::draw(cairo_t *cr)
231 {
232         double x1, x2, y1, y2;
233         double width, height;
234
235         if (m_html == NULL)
236                 return;
237
238         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
239
240         width = x2 - x1;
241         height = y2 - y1;
242
243         litehtml::position pos;
244         pos.width = (int)width;
245         pos.height = (int)height;
246         pos.x = (int)x1;
247         pos.y = (int)y1;
248
249         m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
250 }
251
252 void lh_widget::redraw()
253 {
254         GtkAllocation rect;
255         gint width, height;
256         GdkWindow *gdkwin;
257         cairo_t *cr;
258
259         paint_white();
260
261         if (m_html == NULL)
262                 return;
263
264         /* Get width of the viewport. */
265         gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
266         gdk_drawable_get_size(gdkwin, &width, NULL);
267
268         /* If the available width has changed, rerender the HTML content. */
269         if (m_rendered_width != width) {
270                 debug_print("lh_widget::redraw: width changed: %d != %d\n",
271                                 m_rendered_width, width);
272
273                 /* Update our internally stored width, mainly so that
274                  * lh_widget::get_client_rect() gives correct width during the
275                  * render. */
276                 m_rendered_width = width;
277
278                 /* Re-render HTML for this width. */
279                 m_html->media_changed();
280                 m_html->render(m_rendered_width);
281                 debug_print("render is %dx%d\n", m_html->width(), m_html->height());
282
283                 /* Change drawing area's size to match what was rendered. */
284                 gtk_widget_set_size_request(m_drawing_area,
285                                 m_html->width(), m_html->height());
286         }
287
288         /* Paint the rendered HTML. */
289         gdkwin = gtk_widget_get_window(m_drawing_area);
290         if (gdkwin == NULL) {
291                 g_warning("lh_widget::redraw: No GdkWindow to draw on!");
292                 return;
293         }
294         cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
295         draw(cr);
296
297         cairo_destroy(cr);
298 }
299
300 void lh_widget::paint_white()
301 {
302         GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
303         if (gdkwin == NULL) {
304                 g_warning("lh_widget::clear: No GdkWindow to draw on!");
305                 return;
306         }
307         cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
308
309         /* Paint white background. */
310         gint width, height;
311         gdk_drawable_get_size(gdkwin, &width, &height);
312         cairo_rectangle(cr, 0, 0, width, height);
313         cairo_set_source_rgb(cr, 255, 255, 255);
314         cairo_fill(cr);
315
316         cairo_destroy(cr);
317 }
318 void lh_widget::clear()
319 {
320         m_html = nullptr;
321         paint_white();
322         m_rendered_width = 0;
323 }
324
325 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
326 {
327         if (cursor) {
328                 if (m_cursor != cursor) {
329                         m_cursor = cursor;
330                         update_cursor();
331                 }
332         }
333 }
334
335 void lh_widget::update_cursor()
336 {
337         gint x, y;
338         const litehtml::tchar_t *href;
339         GdkWindow *w = gdk_display_get_window_at_pointer(gdk_display_get_default(),
340                         &x, &y);
341         GdkCursorType cursType = GDK_ARROW;
342
343         if (m_cursor == _t("pointer")) {
344                 cursType = GDK_HAND2;
345         }
346
347         if (cursType == GDK_ARROW) {
348                 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
349         } else {
350                 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
351         }
352
353         if (w != gtk_widget_get_window(m_drawing_area))
354                 return;
355
356         /* If it's an anchor, show its "href" attribute in statusbar,
357          * otherwise clear statusbar. */
358         if ((href = get_href_at(x, y)) != NULL) {
359                 lh_widget_statusbar_push(href);
360         } else {
361                 lh_widget_statusbar_pop();
362         }
363 }
364
365 const litehtml::tchar_t *lh_widget::get_href_at(const gint x, const gint y) const
366 {
367         litehtml::element::ptr over_el, el;
368
369         if (m_html == NULL)
370                 return NULL;
371
372         over_el = m_html->root()->get_element_by_point(x, y, x, y);
373         if (over_el == NULL)
374                 return NULL;
375
376         /* If it's not an anchor, check if it has a parent anchor
377          * (e.g. it's an image within an anchor) and grab a pointer
378          * to that. */
379         if (strcmp(over_el->get_tagName(), "a") && over_el->parent()) {
380                 el = over_el->parent();
381                 while (el && el != m_html->root() && strcmp(el->get_tagName(), "a")) {
382                         el = el->parent();
383                 }
384
385                 if (el && el != m_html->root())
386                         over_el = el;
387                 else
388                         return NULL;
389         }
390
391         /* At this point, over_el is pointing at an anchor tag, so let's
392          * grab its href attribute. */
393         return over_el->get_attr(_t("href"));
394 }
395
396 void lh_widget::print()
397 {
398     debug_print("lh_widget print\n");
399     gtk_widget_realize(GTK_WIDGET(m_drawing_area));
400 }
401
402 void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
403                 GdkEventButton *event)
404 {
405         cm_return_if_fail(url != NULL);
406         cm_return_if_fail(event != NULL);
407
408         debug_print("lh_widget showing context menu for '%s'\n", url);
409
410         m_clicked_url = url;
411         gtk_widget_show_all(m_context_menu);
412         gtk_menu_popup(GTK_MENU(m_context_menu), NULL, NULL, NULL, NULL,
413                         event->button, event->time);
414 }
415
416 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
417                 gpointer user_data)
418 {
419         lh_widget *w = (lh_widget *)user_data;
420         w->redraw();
421         return FALSE;
422 }
423
424 static void size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation,
425                 gpointer user_data)
426 {
427         lh_widget *w = (lh_widget *)user_data;
428
429         debug_print("size_allocate_cb: %dx%d\n",
430                         allocation->width, allocation->height);
431
432         w->setHeight(allocation->height);
433         w->redraw();
434 }
435
436 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
437                 gpointer user_data)
438 {
439         litehtml::position::vector redraw_boxes;
440         lh_widget *w = (lh_widget *)user_data;
441
442         if (w->m_html == NULL)
443                 return false;
444
445         debug_print("lh_widget on_button_press_event\n");
446
447         if (event->type == GDK_2BUTTON_PRESS ||
448                         event->type == GDK_3BUTTON_PRESS)
449                 return true;
450
451         /* Right-click */
452         if (event->button == 3) {
453                 const litehtml::tchar_t *url = w->get_href_at((gint)event->x, (gint)event->y);
454
455                 if (url != NULL)
456                         w->popup_context_menu(url, event);
457
458                 return true;
459         }
460
461         if(w->m_html->on_lbutton_down((int) event->x, (int) event->y,
462                                 (int) event->x, (int) event->y, redraw_boxes)) {
463                 for(auto& pos : redraw_boxes) {
464                         debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
465                         gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
466                 }
467         }
468         
469         return true;
470 }
471
472 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
473         gpointer user_data)
474 {
475     litehtml::position::vector redraw_boxes;
476     lh_widget *w = (lh_widget *)user_data;
477     
478     //debug_print("lh_widget on_motion_notify_event\n");
479
480     if(w->m_html)
481     {    
482         //if(m_cursor == _t("pointer"))
483         if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
484         {
485             for (auto& pos : redraw_boxes)
486             {
487                 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
488                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
489             }
490         }
491         }
492         
493         return true;
494 }
495
496 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
497         gpointer user_data)
498 {
499     litehtml::position::vector redraw_boxes;
500     lh_widget *w = (lh_widget *)user_data;
501     GError* error = NULL;
502
503         if (w->m_html == NULL)
504                 return false;
505
506         debug_print("lh_widget on_button_release_event\n");
507
508         if (event->type == GDK_2BUTTON_PRESS ||
509                         event->type == GDK_3BUTTON_PRESS)
510                 return true;
511
512         /* Right-click */
513         if (event->button == 3)
514                 return true;
515
516         w->m_clicked_url.clear();
517
518     if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
519     {
520         for (auto& pos : redraw_boxes)
521         {
522             debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
523             gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
524         }
525     }
526
527     if (!w->m_clicked_url.empty())
528     {
529             debug_print("Open in browser: %s\n", w->m_clicked_url.c_str());
530             open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
531     }
532
533         return true;
534 }
535
536 static void open_link_cb(GtkMenuItem *item, gpointer user_data)
537 {
538         lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
539
540         open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
541 }
542
543 static void copy_link_cb(GtkMenuItem *item, gpointer user_data)
544 {
545 //      lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
546 }
547
548 ///////////////////////////////////////////////////////////
549 extern "C" {
550
551 lh_widget_wrapped *lh_widget_new()
552 {
553         return new lh_widget;
554 }
555
556 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
557 {
558         return w->get_widget();
559 }
560
561 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
562 {
563         w->open_html(path);
564 }
565
566 void lh_widget_clear(lh_widget_wrapped *w)
567 {
568         w->clear();
569 }
570
571 void lh_widget_destroy(lh_widget_wrapped *w)
572 {
573         delete w;
574 }
575
576 void lh_widget_print(lh_widget_wrapped *w) {
577         w->print();
578 }
579
580 } /* extern "C" */