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"
42 const gchar *prefs_common_get_uri_cmd(void);
49 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
51 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
53 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
55 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
57 static void open_link_cb(GtkMenuItem *item, gpointer user_data);
58 static void copy_link_cb(GtkMenuItem *item, gpointer user_data);
60 lh_widget::lh_widget()
65 m_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
66 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_scrolled_window),
67 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
70 GtkScrolledWindow *scw = GTK_SCROLLED_WINDOW(m_scrolled_window);
71 m_viewport = gtk_viewport_new(
72 gtk_scrolled_window_get_hadjustment(scw),
73 gtk_scrolled_window_get_vadjustment(scw));
74 gtk_container_add(GTK_CONTAINER(m_scrolled_window), m_viewport);
77 m_drawing_area = gtk_drawing_area_new();
78 gtk_container_add(GTK_CONTAINER(m_viewport), m_drawing_area);
79 g_signal_connect(m_drawing_area, "expose-event",
80 G_CALLBACK(expose_event_cb), this);
81 g_signal_connect(m_drawing_area, "motion_notify_event",
82 G_CALLBACK(motion_notify_event), this);
83 g_signal_connect(m_drawing_area, "button_press_event",
84 G_CALLBACK(button_press_event), this);
85 g_signal_connect(m_drawing_area, "button_release_event",
86 G_CALLBACK(button_release_event), this);
88 gtk_widget_show_all(m_scrolled_window);
91 m_context_menu = gtk_menu_new();
93 item = gtk_menu_item_new_with_label(_("Open Link"));
94 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(open_link_cb), this);
95 gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
97 item = gtk_menu_item_new_with_label(_("Copy Link Location"));
98 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(copy_link_cb), this);
99 gtk_menu_shell_append(GTK_MENU_SHELL(m_context_menu), item);
102 m_rendered_width = 0;
103 m_context.load_master_stylesheet(master_css);
108 gtk_widget_set_events(m_drawing_area,
109 GDK_BUTTON_RELEASE_MASK
110 | GDK_BUTTON_PRESS_MASK
111 | GDK_POINTER_MOTION_MASK);
114 lh_widget::~lh_widget()
116 g_object_unref(m_drawing_area);
117 m_drawing_area = NULL;
118 g_object_unref(m_scrolled_window);
119 m_scrolled_window = NULL;
123 GtkWidget *lh_widget::get_widget() const
125 return m_scrolled_window;
128 void lh_widget::set_caption(const litehtml::tchar_t* caption)
130 debug_print("lh_widget set_caption\n");
134 void lh_widget::set_base_url(const litehtml::tchar_t* base_url)
136 debug_print("lh_widget set_base_url '%s'\n",
137 (base_url ? base_url : "(null)"));
138 m_base_url = base_url;
142 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
144 debug_print("lh_widget on_anchor_click. url -> %s\n", url);
146 m_clicked_url = fullurl(url);
150 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
152 debug_print("lh_widget import_css\n");
153 baseurl = master_css;
156 void lh_widget::get_client_rect(litehtml::position& client) const
158 if (m_drawing_area == NULL)
161 client.width = m_rendered_width;
162 client.height = m_height;
166 // debug_print("lh_widget::get_client_rect: %dx%d\n",
167 // client.width, client.height);
170 GdkPixbuf *lh_widget::get_image(const litehtml::tchar_t* url, bool redraw_on_ready)
172 GError *error = NULL;
173 GdkPixbuf *pixbuf = NULL;
174 http* http_loader = NULL;
176 if (!lh_prefs_get()->enable_remote_content) {
177 debug_print("blocking download of image from '%s'\n", url);
181 debug_print("Loading... %s\n", url);
182 gchar *msg = g_strdup_printf("Loading %s ...", url);
183 lh_widget_statusbar_push(msg);
186 http_loader = new http();
187 GInputStream *image = http_loader->load_url(url, &error);
189 if (error || !image) {
191 g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
192 g_clear_error(&error);
197 pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
199 g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
201 g_clear_error(&error);
204 /* if (redraw_on_ready) {
209 lh_widget_statusbar_pop();
217 void lh_widget::open_html(const gchar *contents)
219 gint num = clear_images(lh_prefs_get()->image_cache_size * 1024 * 1000);
222 debug_print("LH: cleared %d images from image cache\n", num);
226 lh_widget_statusbar_push("Loading HTML part ...");
227 m_html = litehtml::document::createFromString(contents, this, &m_context);
228 m_rendered_width = 0;
229 if (m_html != NULL) {
230 debug_print("lh_widget::open_html created document\n");
231 adj = gtk_scrolled_window_get_hadjustment(
232 GTK_SCROLLED_WINDOW(m_scrolled_window));
233 gtk_adjustment_set_value(adj, 0.0);
234 adj = gtk_scrolled_window_get_vadjustment(
235 GTK_SCROLLED_WINDOW(m_scrolled_window));
236 gtk_adjustment_set_value(adj, 0.0);
239 lh_widget_statusbar_pop();
242 void lh_widget::draw(cairo_t *cr)
244 double x1, x2, y1, y2;
245 double width, height;
250 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
255 litehtml::position pos;
256 pos.width = (int)width;
257 pos.height = (int)height;
261 m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
264 void lh_widget::redraw()
276 /* Get width of the viewport. */
277 gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
278 width = gdk_window_get_width(gdkwin);
279 m_height = gdk_window_get_height(gdkwin);
281 /* If the available width has changed, rerender the HTML content. */
282 if (m_rendered_width != width) {
283 debug_print("lh_widget::redraw: width changed: %d != %d\n",
284 m_rendered_width, width);
286 /* Update our internally stored width, mainly so that
287 * lh_widget::get_client_rect() gives correct width during the
289 m_rendered_width = width;
291 /* Re-render HTML for this width. */
292 m_html->media_changed();
293 m_html->render(m_rendered_width);
294 debug_print("render is %dx%d\n", m_html->width(), m_html->height());
296 /* Change drawing area's size to match what was rendered. */
297 gtk_widget_set_size_request(m_drawing_area,
298 m_html->width(), m_html->height());
301 /* Paint the rendered HTML. */
302 gdkwin = gtk_widget_get_window(m_drawing_area);
303 if (gdkwin == NULL) {
304 g_warning("lh_widget::redraw: No GdkWindow to draw on!");
307 cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
313 void lh_widget::paint_white()
315 GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
316 if (gdkwin == NULL) {
317 g_warning("lh_widget::clear: No GdkWindow to draw on!");
320 cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
322 /* Paint white background. */
324 gdk_drawable_get_size(gdkwin, &width, &height);
325 cairo_rectangle(cr, 0, 0, width, height);
326 cairo_set_source_rgb(cr, 255, 255, 255);
331 void lh_widget::clear()
335 m_rendered_width = 0;
337 m_clicked_url.clear();
340 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
343 if (m_cursor != cursor) {
350 void lh_widget::update_cursor()
353 const litehtml::tchar_t *href;
354 GdkWindow *w = gdk_display_get_window_at_pointer(gdk_display_get_default(),
356 GdkCursorType cursType = GDK_ARROW;
358 if (m_cursor == _t("pointer")) {
359 cursType = GDK_HAND2;
362 if (cursType == GDK_ARROW) {
363 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
365 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
368 if (w != gtk_widget_get_window(m_drawing_area))
371 /* If it's an anchor, show its "href" attribute in statusbar,
372 * otherwise clear statusbar. */
373 if ((href = get_href_at(x, y)) != NULL) {
374 lh_widget_statusbar_push(fullurl(href).c_str());
376 lh_widget_statusbar_pop();
380 const litehtml::tchar_t *lh_widget::get_href_at(const gint x, const gint y) const
382 litehtml::element::ptr over_el, el;
387 over_el = m_html->root()->get_element_by_point(x, y, x, y);
391 /* If it's not an anchor, check if it has a parent anchor
392 * (e.g. it's an image within an anchor) and grab a pointer
394 if (strcmp(over_el->get_tagName(), "a") && over_el->parent()) {
395 el = over_el->parent();
396 while (el && el != m_html->root() && strcmp(el->get_tagName(), "a")) {
400 if (el && el != m_html->root())
406 /* At this point, over_el is pointing at an anchor tag, so let's
407 * grab its href attribute. */
408 return over_el->get_attr(_t("href"));
411 void lh_widget::print()
413 debug_print("lh_widget print\n");
414 gtk_widget_realize(GTK_WIDGET(m_drawing_area));
417 void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
418 GdkEventButton *event)
420 cm_return_if_fail(url != NULL);
421 cm_return_if_fail(event != NULL);
423 debug_print("lh_widget showing context menu for '%s'\n", url);
426 gtk_widget_show_all(m_context_menu);
427 gtk_menu_popup(GTK_MENU(m_context_menu), NULL, NULL, NULL, NULL,
428 event->button, event->time);
431 void lh_widget::update_font()
433 PangoFontDescription *pd =
434 pango_font_description_from_string(lh_prefs_get()->default_font);
435 gboolean absolute = pango_font_description_get_size_is_absolute(pd);
438 m_font_name = g_strdup(pango_font_description_get_family(pd));
439 m_font_size = pango_font_description_get_size(pd);
441 pango_font_description_free(pd);
444 m_font_size /= PANGO_SCALE;
446 debug_print("Font set to '%s', size %d\n", m_font_name, m_font_size);
449 const litehtml::tstring lh_widget::fullurl(const litehtml::tchar_t *url) const
451 if (*url == '#' && !m_base_url.empty())
452 return m_base_url + url;
457 ////////////////////////////////////////////////
458 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
461 lh_widget *w = (lh_widget *)user_data;
466 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
469 litehtml::position::vector redraw_boxes;
470 lh_widget *w = (lh_widget *)user_data;
472 if (w->m_html == NULL)
475 //debug_print("lh_widget on_button_press_event\n");
477 if (event->type == GDK_2BUTTON_PRESS ||
478 event->type == GDK_3BUTTON_PRESS)
482 if (event->button == 3) {
483 const litehtml::tchar_t *url = w->get_href_at((gint)event->x, (gint)event->y);
486 w->popup_context_menu(url, event);
491 if(w->m_html->on_lbutton_down((int) event->x, (int) event->y,
492 (int) event->x, (int) event->y, redraw_boxes)) {
493 for(auto& pos : redraw_boxes) {
494 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
495 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
502 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
505 litehtml::position::vector redraw_boxes;
506 lh_widget *w = (lh_widget *)user_data;
508 //debug_print("lh_widget on_motion_notify_event\n");
512 if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
514 for (auto& pos : redraw_boxes)
516 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
517 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
525 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
528 litehtml::position::vector redraw_boxes;
529 lh_widget *w = (lh_widget *)user_data;
530 GError* error = NULL;
532 if (w->m_html == NULL)
535 //debug_print("lh_widget on_button_release_event\n");
537 if (event->type == GDK_2BUTTON_PRESS ||
538 event->type == GDK_3BUTTON_PRESS)
542 if (event->button == 3)
545 w->m_clicked_url.clear();
547 if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
549 for (auto& pos : redraw_boxes)
551 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
552 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
556 if (!w->m_clicked_url.empty())
558 debug_print("Open in browser: %s\n", w->m_clicked_url.c_str());
559 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
565 static void open_link_cb(GtkMenuItem *item, gpointer user_data)
567 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
569 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
572 static void copy_link_cb(GtkMenuItem *item, gpointer user_data)
574 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
576 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
577 w->m_clicked_url.c_str(), -1);
578 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
579 w->m_clicked_url.c_str(), -1);
582 ///////////////////////////////////////////////////////////
585 lh_widget_wrapped *lh_widget_new()
587 return new lh_widget;
590 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
592 return w->get_widget();
595 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
600 void lh_widget_clear(lh_widget_wrapped *w)
605 void lh_widget_destroy(lh_widget_wrapped *w)
610 void lh_widget_print(lh_widget_wrapped *w) {