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