Make cursor and statusbar URL display smarter in litehtml plugin
[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         /* scrolled window */
64         m_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
65         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_scrolled_window),
66                         GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
67
68         /* viewport */
69         GtkScrolledWindow *scw = GTK_SCROLLED_WINDOW(m_scrolled_window);
70         m_viewport = gtk_viewport_new(
71                         gtk_scrolled_window_get_hadjustment(scw),
72                         gtk_scrolled_window_get_vadjustment(scw));
73         gtk_container_add(GTK_CONTAINER(m_scrolled_window), m_viewport);
74
75         /* drawing area */
76         m_drawing_area = gtk_drawing_area_new();
77         gtk_container_add(GTK_CONTAINER(m_viewport), m_drawing_area);
78         g_signal_connect(m_drawing_area, "expose-event",
79                         G_CALLBACK(expose_event_cb), this);
80         g_signal_connect(m_drawing_area, "motion_notify_event",
81                         G_CALLBACK(motion_notify_event), this);
82         g_signal_connect(m_drawing_area, "button_press_event",
83                         G_CALLBACK(button_press_event), this);
84         g_signal_connect(m_drawing_area, "button_release_event",
85                         G_CALLBACK(button_release_event), this);
86
87         gtk_widget_show_all(m_scrolled_window);
88
89         /* context menu */
90         m_context_menu = gtk_menu_new();
91
92         item = gtk_menu_item_new_with_label(_("Open Link"));
93         g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(open_link_cb), this);
94         gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
95
96         item = gtk_menu_item_new_with_label(_("Copy Link Location"));
97         g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copy_link_cb), this);
98         gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
99
100         m_html = NULL;
101         m_rendered_width = 0;
102         m_context.load_master_stylesheet(master_css);
103
104         m_font_name = NULL;
105         m_font_size = 0;
106
107         m_partinfo = NULL;
108
109         m_showing_url = FALSE;
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 '%s'\n",
140                         (base_url ? base_url : "(null)"));
141         m_base_url = base_url;
142         return;
143 }
144
145 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
146 {
147         debug_print("lh_widget on_anchor_click. url -> %s\n", url);
148
149         m_clicked_url = fullurl(url);
150         return;
151 }
152
153 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
154 {
155         debug_print("lh_widget import_css\n");
156         baseurl = master_css;
157 }
158
159 void lh_widget::get_client_rect(litehtml::position& client) const
160 {
161         if (m_drawing_area == NULL)
162                 return;
163
164         client.width = m_rendered_width;
165         client.height = m_height;
166         client.x = 0;
167         client.y = 0;
168
169 //      debug_print("lh_widget::get_client_rect: %dx%d\n",
170 //                      client.width, client.height);
171 }
172
173 void lh_widget::open_html(const gchar *contents)
174 {
175         gint num = clear_images(lh_prefs_get()->image_cache_size * 1024 * 1000);
176         GtkAdjustment *adj;
177
178         debug_print("LH: cleared %d images from image cache\n", num);
179
180         update_font();
181
182         lh_widget_statusbar_push("Loading HTML part ...");
183         m_html = litehtml::document::createFromString(contents, this, &m_context);
184         m_rendered_width = 0;
185         if (m_html != NULL) {
186                 debug_print("lh_widget::open_html created document\n");
187                 adj = gtk_scrolled_window_get_hadjustment(
188                                 GTK_SCROLLED_WINDOW(m_scrolled_window));
189                 gtk_adjustment_set_value(adj, 0.0);
190                 adj = gtk_scrolled_window_get_vadjustment(
191                                 GTK_SCROLLED_WINDOW(m_scrolled_window));
192                 gtk_adjustment_set_value(adj, 0.0);
193                 redraw(false);
194         }
195         lh_widget_statusbar_pop();
196 }
197
198 void lh_widget::draw(cairo_t *cr)
199 {
200         double x1, x2, y1, y2;
201         double width, height;
202
203         if (m_html == NULL)
204                 return;
205
206         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
207
208         width = x2 - x1;
209         height = y2 - y1;
210
211         litehtml::position pos;
212         pos.width = (int)width;
213         pos.height = (int)height;
214         pos.x = (int)x1;
215         pos.y = (int)y1;
216
217         m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
218 }
219
220 void lh_widget::redraw(gboolean force_render)
221 {
222         GtkAllocation rect;
223         gint width;
224         GdkWindow *gdkwin;
225         cairo_t *cr;
226
227         paint_white();
228
229         if (m_html == NULL)
230                 return;
231
232         /* Get width of the viewport. */
233         gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
234         width = gdk_window_get_width(gdkwin);
235         m_height = gdk_window_get_height(gdkwin);
236
237         /* If the available width has changed, rerender the HTML content. */
238         if (m_rendered_width != width || force_render) {
239                 debug_print("lh_widget::redraw: width changed: %d != %d\n",
240                                 m_rendered_width, width);
241
242                 /* Update our internally stored width, mainly so that
243                  * lh_widget::get_client_rect() gives correct width during the
244                  * render. */
245                 m_rendered_width = width;
246
247                 /* Re-render HTML for this width. */
248                 m_html->media_changed();
249                 m_html->render(m_rendered_width);
250                 debug_print("render is %dx%d\n", m_html->width(), m_html->height());
251
252                 /* Change drawing area's size to match what was rendered. */
253                 gtk_widget_set_size_request(m_drawing_area,
254                                 m_html->width(), m_html->height());
255         }
256
257         /* Paint the rendered HTML. */
258         gdkwin = gtk_widget_get_window(m_drawing_area);
259         if (gdkwin == NULL) {
260                 g_warning("lh_widget::redraw: No GdkWindow to draw on!");
261                 return;
262         }
263         cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
264         draw(cr);
265
266         cairo_destroy(cr);
267 }
268
269 void lh_widget::paint_white()
270 {
271         GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
272         if (gdkwin == NULL) {
273                 g_warning("lh_widget::clear: No GdkWindow to draw on!");
274                 return;
275         }
276         cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
277
278         /* Paint white background. */
279         gint width, height;
280         gdk_drawable_get_size(gdkwin, &width, &height);
281         cairo_rectangle(cr, 0, 0, width, height);
282         cairo_set_source_rgb(cr, 255, 255, 255);
283         cairo_fill(cr);
284
285         cairo_destroy(cr);
286 }
287 void lh_widget::clear()
288 {
289         m_html = nullptr;
290         paint_white();
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;
299         gint x, y;
300         GdkWindow *w = gdk_display_get_window_at_pointer(gdk_display_get_default(),
301                         &x, &y);
302
303         if (w != gtk_widget_get_window(m_drawing_area))
304                 return;
305
306         over_el = m_html->root()->get_element_by_point(x, y, x, y);
307
308         if (!over_el) {
309                 m_over_element = NULL;
310                 return;
311         }
312
313         if (cursor && over_el) {
314                 if (over_el != m_over_element) {
315                         m_over_element = over_el;
316                         update_cursor(cursor);
317                 }
318         }
319 }
320
321 void lh_widget::update_cursor(const litehtml::tchar_t* cursor)
322 {
323         const litehtml::tchar_t *href;
324         GdkCursorType cursType = GDK_ARROW;
325
326         if (cursor == _t("pointer")) {
327                 cursType = GDK_HAND2;
328         }
329
330         if (cursType == GDK_ARROW) {
331                 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
332         } else {
333                 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
334         }
335
336         /* If it's an anchor, show its "href" attribute in statusbar,
337          * otherwise clear statusbar. */
338         if (m_showing_url) {
339                 lh_widget_statusbar_pop();
340                 m_showing_url = FALSE;
341         }
342
343         if ((href = get_href_at(m_over_element)) != NULL) {
344                 lh_widget_statusbar_push(fullurl(href).c_str());
345                 m_showing_url = TRUE;
346         }
347 }
348
349 const litehtml::tchar_t *lh_widget::get_href_at(litehtml::element::ptr element) const
350 {
351         litehtml::element::ptr el;
352
353         if (element == NULL)
354                 return NULL;
355
356         /* If it's not an anchor, check if it has a parent anchor
357          * (e.g. it's an image within an anchor) and grab a pointer
358          * to that. */
359         if (strcmp(element->get_tagName(), "a") && element->parent()) {
360                 el = element->parent();
361                 while (el && el != m_html->root() && strcmp(el->get_tagName(), "a")) {
362                         el = el->parent();
363                 }
364
365                 if (!el || el == m_html->root())
366                         return NULL;
367         } else {
368                 el = element;
369         }
370
371         /* At this point, over_el is pointing at an anchor tag, so let's
372          * grab its href attribute. */
373         return el->get_attr(_t("href"));
374 }
375
376 const litehtml::tchar_t *lh_widget::get_href_at(const gint x, const gint y) const
377 {
378         litehtml::element::ptr over_el, el;
379
380         if (m_html == NULL)
381                 return NULL;
382
383         over_el = m_html->root()->get_element_by_point(x, y, x, y);
384         if (over_el == NULL)
385                 return NULL;
386
387         return get_href_at(over_el);
388 }
389
390 void lh_widget::print()
391 {
392     debug_print("lh_widget print\n");
393     gtk_widget_realize(GTK_WIDGET(m_drawing_area));
394 }
395
396 void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
397                 GdkEventButton *event)
398 {
399         cm_return_if_fail(url != NULL);
400         cm_return_if_fail(event != NULL);
401
402         debug_print("lh_widget showing context menu for '%s'\n", url);
403
404         m_clicked_url = url;
405         gtk_widget_show_all(m_context_menu);
406         gtk_menu_popup(GTK_MENU(m_context_menu), NULL, NULL, NULL, NULL,
407                         event->button, event->time);
408 }
409
410 void lh_widget::update_font()
411 {
412         PangoFontDescription *pd =
413                 pango_font_description_from_string(lh_prefs_get()->default_font);
414         gboolean absolute = pango_font_description_get_size_is_absolute(pd);
415
416         g_free(m_font_name);
417         m_font_name = g_strdup(pango_font_description_get_family(pd));
418         m_font_size = pango_font_description_get_size(pd);
419
420         pango_font_description_free(pd);
421
422         if (!absolute)
423                 m_font_size /= PANGO_SCALE;
424
425         debug_print("Font set to '%s', size %d\n", m_font_name, m_font_size);
426 }
427
428 const litehtml::tstring lh_widget::fullurl(const litehtml::tchar_t *url) const
429 {
430         if (*url == '#' && !m_base_url.empty())
431                 return m_base_url + url;
432
433         return _t(url);
434 }
435
436 void lh_widget::set_partinfo(MimeInfo *partinfo)
437 {
438         m_partinfo = partinfo;
439 }
440
441 GdkPixbuf *lh_widget::get_local_image(const litehtml::tstring url) const
442 {
443         GdkPixbuf *pixbuf;
444         const gchar *name;
445         MimeInfo *p = m_partinfo;
446
447         if (strncmp(url.c_str(), "cid:", 4) != 0) {
448                 debug_print("lh_widget::get_local_image: '%s' is not a local URI, ignoring\n", url.c_str());
449                 return NULL;
450         }
451
452         name = url.c_str() + 4;
453         debug_print("getting message part '%s'\n", name);
454
455         while ((p = procmime_mimeinfo_next(p)) != NULL) {
456                 size_t len = strlen(name);
457
458                 /* p->id is in format "<partname>" */
459                 if (!strncasecmp(name, p->id + 1, len) &&
460                                 strlen(p->id) >= len + 2 &&
461                                 *(p->id + len + 1) == '>') {
462                         GInputStream *stream;
463                         GError *error = NULL;
464
465                         stream = procmime_get_part_as_inputstream(p, &error);
466                         if (error != NULL) {
467                                 g_warning("Couldn't get image MIME part: %s\n", error->message);
468                                 g_error_free(error);
469                                 return NULL;
470                         }
471
472                         pixbuf = gdk_pixbuf_new_from_stream(stream, NULL, &error);
473                         g_object_unref(stream);
474                         if (error != NULL) {
475                                 g_warning("Couldn't load image: %s\n", error->message);
476                                 g_error_free(error);
477                                 return NULL;
478                         }
479
480                         return pixbuf;
481                 }
482         }
483
484         /* MIME part with requested name was not found */
485         return NULL;
486 }
487
488 ////////////////////////////////////////////////
489 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
490                 gpointer user_data)
491 {
492         lh_widget *w = (lh_widget *)user_data;
493         w->redraw(false);
494         return FALSE;
495 }
496
497 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
498                 gpointer user_data)
499 {
500         litehtml::position::vector redraw_boxes;
501         lh_widget *w = (lh_widget *)user_data;
502
503         if (w->m_html == NULL)
504                 return false;
505
506         //debug_print("lh_widget on_button_press_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                 const litehtml::tchar_t *url = w->get_href_at((gint)event->x, (gint)event->y);
515
516                 if (url != NULL)
517                         w->popup_context_menu(url, event);
518
519                 return true;
520         }
521
522         if(w->m_html->on_lbutton_down((int) event->x, (int) event->y,
523                                 (int) event->x, (int) event->y, redraw_boxes)) {
524                 for(auto& pos : redraw_boxes) {
525                         debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
526                         gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
527                 }
528         }
529         
530         return true;
531 }
532
533 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
534         gpointer user_data)
535 {
536     litehtml::position::vector redraw_boxes;
537     lh_widget *w = (lh_widget *)user_data;
538     
539     //debug_print("lh_widget on_motion_notify_event\n");
540
541     if(w->m_html)
542     {    
543         if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
544         {
545             for (auto& pos : redraw_boxes)
546             {
547                 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
548                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
549             }
550         }
551         }
552         
553         return true;
554 }
555
556 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
557         gpointer user_data)
558 {
559     litehtml::position::vector redraw_boxes;
560     lh_widget *w = (lh_widget *)user_data;
561     GError* error = NULL;
562
563         if (w->m_html == NULL)
564                 return false;
565
566         //debug_print("lh_widget on_button_release_event\n");
567
568         if (event->type == GDK_2BUTTON_PRESS ||
569                         event->type == GDK_3BUTTON_PRESS)
570                 return true;
571
572         /* Right-click */
573         if (event->button == 3)
574                 return true;
575
576         w->m_clicked_url.clear();
577
578     if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
579     {
580         for (auto& pos : redraw_boxes)
581         {
582             debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
583             gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
584         }
585     }
586
587     if (!w->m_clicked_url.empty())
588     {
589             debug_print("Open in browser: %s\n", w->m_clicked_url.c_str());
590             open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
591     }
592
593         return true;
594 }
595
596 static void open_link_cb(GtkMenuItem *item, gpointer user_data)
597 {
598         lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
599
600         open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
601 }
602
603 static void copy_link_cb(GtkMenuItem *item, gpointer user_data)
604 {
605         lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
606
607         gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
608                         w->m_clicked_url.c_str(), -1);
609         gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
610                         w->m_clicked_url.c_str(), -1);
611 }
612
613 ///////////////////////////////////////////////////////////
614 extern "C" {
615
616 lh_widget_wrapped *lh_widget_new()
617 {
618         return new lh_widget;
619 }
620
621 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
622 {
623         return w->get_widget();
624 }
625
626 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
627 {
628         w->open_html(path);
629 }
630
631 void lh_widget_clear(lh_widget_wrapped *w)
632 {
633         w->clear();
634 }
635
636 void lh_widget_destroy(lh_widget_wrapped *w)
637 {
638         delete w;
639 }
640
641 void lh_widget_print(lh_widget_wrapped *w) {
642         w->print();
643 }
644
645 void lh_widget_set_partinfo(lh_widget_wrapped *w, MimeInfo *partinfo)
646 {
647         w->set_partinfo(partinfo);
648 }
649
650 } /* extern "C" */