2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 2019 the Claws Mail Team
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.
20 #include "claws-features.h"
24 #include <glib/gi18n.h>
25 #include <glib/gstdio.h>
27 #include <sys/types.h>
29 #include <curl/curl.h>
34 #include "litehtml/litehtml.h"
37 #include "lh_widget.h"
38 #include "lh_widget_wrapped.h"
41 const gchar *prefs_common_get_uri_cmd(void);
48 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
50 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
52 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
54 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
56 static void open_link_cb(GtkMenuItem *item, gpointer user_data);
57 static void copy_link_cb(GtkMenuItem *item, gpointer user_data);
59 lh_widget::lh_widget()
63 m_force_render = false;
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);
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);
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);
90 gtk_widget_show_all(m_scrolled_window);
93 m_context_menu = gtk_menu_new();
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);
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);
104 m_rendered_width = 0;
105 m_context.load_master_stylesheet(master_css);
112 m_showing_url = FALSE;
114 gtk_widget_set_events(m_drawing_area,
115 GDK_BUTTON_RELEASE_MASK
116 | GDK_BUTTON_PRESS_MASK
117 | GDK_POINTER_MOTION_MASK);
120 lh_widget::~lh_widget()
122 g_object_unref(m_drawing_area);
123 m_drawing_area = NULL;
124 g_object_unref(m_scrolled_window);
125 m_scrolled_window = NULL;
130 GtkWidget *lh_widget::get_widget() const
132 return m_scrolled_window;
135 void lh_widget::set_caption(const litehtml::tchar_t* caption)
137 debug_print("lh_widget set_caption\n");
141 void lh_widget::set_base_url(const litehtml::tchar_t* base_url)
143 debug_print("lh_widget set_base_url '%s'\n",
144 (base_url ? base_url : "(null)"));
146 m_base_url = base_url;
153 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
155 debug_print("lh_widget on_anchor_click. url -> %s\n", url);
157 m_clicked_url = fullurl(url);
161 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
163 debug_print("lh_widget import_css\n");
164 baseurl = master_css;
167 void lh_widget::get_client_rect(litehtml::position& client) const
169 if (m_drawing_area == NULL)
172 client.width = m_rendered_width;
173 client.height = m_height;
177 // debug_print("lh_widget::get_client_rect: %dx%d\n",
178 // client.width, client.height);
181 void lh_widget::open_html(const gchar *contents)
183 gint num = clear_images(lh_prefs_get()->image_cache_size * 1024 * 1000);
186 debug_print("LH: cleared %d images from image cache\n", num);
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);
202 gtk_widget_queue_draw(m_drawing_area);
204 lh_widget_statusbar_pop();
207 void lh_widget::rerender()
209 m_force_render = true;
210 gtk_widget_queue_draw(m_drawing_area);
213 void lh_widget::draw(cairo_t *cr)
215 double x1, x2, y1, y2;
216 double width, height;
221 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
226 litehtml::position pos;
227 pos.width = (int)width;
228 pos.height = (int)height;
232 m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
235 void lh_widget::redraw()
245 /* Get width of the viewport. */
246 gtk_widget_get_allocation(GTK_WIDGET(m_viewport), &rect);
248 m_height = rect.height;
250 /* If the available width has changed, rerender the HTML content. */
251 if (m_rendered_width != width || std::atomic_exchange(&m_force_render, false)) {
252 debug_print("lh_widget::redraw: width changed: %d != %d\n",
253 m_rendered_width, width);
255 /* Update our internally stored width, mainly so that
256 * lh_widget::get_client_rect() gives correct width during the
258 m_rendered_width = width;
260 /* Re-render HTML for this width. */
261 m_html->media_changed();
262 m_html->render(m_rendered_width);
263 debug_print("render is %dx%d\n", m_html->width(), m_html->height());
265 /* Change drawing area's size to match what was rendered. */
266 gtk_widget_set_size_request(m_drawing_area,
267 m_html->width(), m_html->height());
270 /* Paint the rendered HTML. */
271 gdkwin = gtk_widget_get_window(m_drawing_area);
272 if (gdkwin == NULL) {
273 g_warning("lh_widget::redraw: No GdkWindow to draw on!");
276 cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
277 if(!std::atomic_exchange(&m_blank, false)) {
280 cairo_rectangle(cr, rect.x, rect.y, rect.width, rect.height);
281 cairo_set_source_rgb(cr, 255, 255, 255);
288 void lh_widget::clear()
292 m_rendered_width = 0;
294 m_clicked_url.clear();
297 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
299 litehtml::element::ptr over_el = m_html->over_element();
302 (over_el == NULL || over_el != m_over_element)) {
303 lh_widget_statusbar_pop();
304 m_showing_url = FALSE;
307 if (over_el != m_over_element) {
308 m_over_element = over_el;
309 update_cursor(cursor);
313 void lh_widget::update_cursor(const litehtml::tchar_t* cursor)
315 GdkCursorType cursType = GDK_ARROW;
316 const litehtml::tchar_t *href = get_href_at(m_over_element);
318 /* If there is a href, and litehtml is okay with showing a pointer
319 * cursor ("pointer" or "auto"), set it, otherwise keep the
320 * default arrow cursor */
321 if ((!strcmp(cursor, "pointer") || !strcmp(cursor, "auto")) &&
323 cursType = GDK_HAND2;
326 if (cursType == GDK_ARROW) {
327 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
329 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
332 /* If there is a href, show it in statusbar */
334 lh_widget_statusbar_push(fullurl(href).c_str());
335 m_showing_url = TRUE;
339 const litehtml::tchar_t *lh_widget::get_href_at(litehtml::element::ptr element) const
341 litehtml::element::ptr el;
346 /* If it's not an anchor, check if it has a parent anchor
347 * (e.g. it's an image within an anchor) and grab a pointer
349 if (strcmp(element->get_tagName(), "a") && element->parent()) {
350 el = element->parent();
351 while (el && el != m_html->root() && strcmp(el->get_tagName(), "a")) {
355 if (!el || el == m_html->root())
361 /* At this point, over_el is pointing at an anchor tag, so let's
362 * grab its href attribute. */
363 return el->get_attr(_t("href"));
366 const litehtml::tchar_t *lh_widget::get_href_at(const gint x, const gint y) const
368 litehtml::element::ptr over_el, el;
373 over_el = m_html->root()->get_element_by_point(x, y, x, y);
377 return get_href_at(over_el);
380 void lh_widget::print()
382 debug_print("lh_widget print\n");
383 gtk_widget_realize(GTK_WIDGET(m_drawing_area));
386 void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
387 GdkEventButton *event)
389 cm_return_if_fail(url != NULL);
390 cm_return_if_fail(event != NULL);
392 debug_print("lh_widget showing context menu for '%s'\n", url);
395 gtk_widget_show_all(m_context_menu);
396 gtk_menu_popup(GTK_MENU(m_context_menu), NULL, NULL, NULL, NULL,
397 event->button, event->time);
400 void lh_widget::update_font()
402 PangoFontDescription *pd =
403 pango_font_description_from_string(lh_prefs_get()->default_font);
404 gboolean absolute = pango_font_description_get_size_is_absolute(pd);
407 m_font_name = g_strdup(pango_font_description_get_family(pd));
408 m_font_size = pango_font_description_get_size(pd);
410 pango_font_description_free(pd);
413 m_font_size /= PANGO_SCALE;
415 debug_print("Font set to '%s', size %d\n", m_font_name, m_font_size);
418 const litehtml::tstring lh_widget::fullurl(const litehtml::tchar_t *url) const
420 if (*url == '#' && !m_base_url.empty())
421 return m_base_url + url;
426 void lh_widget::set_partinfo(MimeInfo *partinfo)
428 m_partinfo = partinfo;
431 GdkPixbuf *lh_widget::get_local_image(const litehtml::tstring url) const
435 MimeInfo *p = m_partinfo;
437 if (strncmp(url.c_str(), "cid:", 4) != 0) {
438 debug_print("lh_widget::get_local_image: '%s' is not a local URI, ignoring\n", url.c_str());
442 name = url.c_str() + 4;
443 debug_print("getting message part '%s'\n", name);
445 while ((p = procmime_mimeinfo_next(p)) != NULL) {
446 size_t len = strlen(name);
448 /* p->id is in format "<partname>" */
450 strlen(p->id) >= len + 2 &&
451 !strncasecmp(name, p->id + 1, len) &&
452 *(p->id + len + 1) == '>') {
453 GError *error = NULL;
455 pixbuf = procmime_get_part_as_pixbuf(p, &error);
457 g_warning("Couldn't load image: %s\n", error->message);
466 /* MIME part with requested name was not found */
470 ////////////////////////////////////////////////
471 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
474 lh_widget *w = (lh_widget *)user_data;
479 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
482 litehtml::position::vector redraw_boxes;
483 lh_widget *w = (lh_widget *)user_data;
485 if (w->m_html == NULL)
488 //debug_print("lh_widget on_button_press_event\n");
490 if (event->type == GDK_2BUTTON_PRESS ||
491 event->type == GDK_3BUTTON_PRESS)
495 if (event->button == 3) {
496 const litehtml::tchar_t *url = w->get_href_at((gint)event->x, (gint)event->y);
499 w->popup_context_menu(url, event);
504 if(w->m_html->on_lbutton_down((int) event->x, (int) event->y,
505 (int) event->x, (int) event->y, redraw_boxes)) {
506 for(auto& pos : redraw_boxes) {
507 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
508 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
515 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
518 litehtml::position::vector redraw_boxes;
519 lh_widget *w = (lh_widget *)user_data;
521 //debug_print("lh_widget on_motion_notify_event\n");
525 if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
527 for (auto& pos : redraw_boxes)
529 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
530 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
538 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
541 litehtml::position::vector redraw_boxes;
542 lh_widget *w = (lh_widget *)user_data;
544 if (w->m_html == NULL)
547 //debug_print("lh_widget on_button_release_event\n");
549 if (event->type == GDK_2BUTTON_PRESS ||
550 event->type == GDK_3BUTTON_PRESS)
554 if (event->button == 3)
557 w->m_clicked_url.clear();
559 if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
561 for (auto& pos : redraw_boxes)
563 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
564 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
568 if (!w->m_clicked_url.empty())
570 debug_print("Open in browser: %s\n", w->m_clicked_url.c_str());
571 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
577 static void open_link_cb(GtkMenuItem *item, gpointer user_data)
579 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
581 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
584 static void copy_link_cb(GtkMenuItem *item, gpointer user_data)
586 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
588 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
589 w->m_clicked_url.c_str(), -1);
590 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
591 w->m_clicked_url.c_str(), -1);
594 ///////////////////////////////////////////////////////////
597 lh_widget_wrapped *lh_widget_new()
599 return new lh_widget;
602 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
604 return w->get_widget();
607 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
612 void lh_widget_clear(lh_widget_wrapped *w)
617 void lh_widget_destroy(lh_widget_wrapped *w)
622 void lh_widget_print(lh_widget_wrapped *w) {
626 void lh_widget_set_partinfo(lh_widget_wrapped *w, MimeInfo *partinfo)
628 w->set_partinfo(partinfo);