2 * Claws Mail -- A GTK+ based, lightweight, and fast e-mail client
3 * Copyright(C) 1999-2015 the Claws Mail Team
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.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
27 #include <glib/gstdio.h>
29 #include <sys/types.h>
31 #include <curl/curl.h>
36 #include "litehtml/litehtml.h"
39 #include "lh_widget.h"
40 #include "lh_widget_wrapped.h"
44 const gchar *prefs_common_get_uri_cmd(void);
51 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
53 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
55 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
57 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
59 static void open_link_cb(GtkMenuItem *item, gpointer user_data);
60 static void copy_link_cb(GtkMenuItem *item, gpointer user_data);
62 lh_widget::lh_widget()
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);
110 gtk_widget_set_events(m_drawing_area,
111 GDK_BUTTON_RELEASE_MASK
112 | GDK_BUTTON_PRESS_MASK
113 | GDK_POINTER_MOTION_MASK);
116 lh_widget::~lh_widget()
118 g_object_unref(m_drawing_area);
119 m_drawing_area = NULL;
120 g_object_unref(m_scrolled_window);
121 m_scrolled_window = NULL;
125 GtkWidget *lh_widget::get_widget() const
127 return m_scrolled_window;
130 void lh_widget::set_caption(const litehtml::tchar_t* caption)
132 debug_print("lh_widget set_caption\n");
136 void lh_widget::set_base_url(const litehtml::tchar_t* base_url)
138 debug_print("lh_widget set_base_url '%s'\n",
139 (base_url ? base_url : "(null)"));
140 m_base_url = base_url;
144 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
146 debug_print("lh_widget on_anchor_click. url -> %s\n", url);
148 m_clicked_url = fullurl(url);
152 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
154 debug_print("lh_widget import_css\n");
155 baseurl = master_css;
158 void lh_widget::get_client_rect(litehtml::position& client) const
160 if (m_drawing_area == NULL)
163 client.width = m_rendered_width;
164 client.height = m_height;
168 // debug_print("lh_widget::get_client_rect: %dx%d\n",
169 // client.width, client.height);
172 GdkPixbuf *lh_widget::get_image(const litehtml::tchar_t* url, bool redraw_on_ready)
174 GError *error = NULL;
175 GdkPixbuf *pixbuf = NULL;
176 http* http_loader = NULL;
178 if (!lh_prefs_get()->enable_remote_content) {
179 debug_print("blocking download of image from '%s'\n", url);
183 debug_print("Loading... %s\n", url);
184 gchar *msg = g_strdup_printf("Loading %s ...", url);
185 lh_widget_statusbar_push(msg);
188 http_loader = new http();
189 GInputStream *image = http_loader->load_url(url, &error);
191 if (error || !image) {
193 g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
194 g_clear_error(&error);
199 pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
201 g_warning("lh_widget::get_image: Could not create pixbuf %s", error->message);
203 g_clear_error(&error);
206 /* if (redraw_on_ready) {
211 lh_widget_statusbar_pop();
219 void lh_widget::open_html(const gchar *contents)
221 gint num = clear_images(lh_prefs_get()->image_cache_size * 1024 * 1000);
224 debug_print("LH: cleared %d images from image cache\n", num);
228 lh_widget_statusbar_push("Loading HTML part ...");
229 m_html = litehtml::document::createFromString(contents, this, &m_context);
230 m_rendered_width = 0;
231 if (m_html != NULL) {
232 debug_print("lh_widget::open_html created document\n");
233 adj = gtk_scrolled_window_get_hadjustment(
234 GTK_SCROLLED_WINDOW(m_scrolled_window));
235 gtk_adjustment_set_value(adj, 0.0);
236 adj = gtk_scrolled_window_get_vadjustment(
237 GTK_SCROLLED_WINDOW(m_scrolled_window));
238 gtk_adjustment_set_value(adj, 0.0);
241 lh_widget_statusbar_pop();
244 void lh_widget::draw(cairo_t *cr)
246 double x1, x2, y1, y2;
247 double width, height;
252 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
257 litehtml::position pos;
258 pos.width = (int)width;
259 pos.height = (int)height;
263 m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
266 void lh_widget::redraw()
278 /* Get width of the viewport. */
279 gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
280 width = gdk_window_get_width(gdkwin);
281 m_height = gdk_window_get_height(gdkwin);
283 /* If the available width has changed, rerender the HTML content. */
284 if (m_rendered_width != width) {
285 debug_print("lh_widget::redraw: width changed: %d != %d\n",
286 m_rendered_width, width);
288 /* Update our internally stored width, mainly so that
289 * lh_widget::get_client_rect() gives correct width during the
291 m_rendered_width = width;
293 /* Re-render HTML for this width. */
294 m_html->media_changed();
295 m_html->render(m_rendered_width);
296 debug_print("render is %dx%d\n", m_html->width(), m_html->height());
298 /* Change drawing area's size to match what was rendered. */
299 gtk_widget_set_size_request(m_drawing_area,
300 m_html->width(), m_html->height());
303 /* Paint the rendered HTML. */
304 gdkwin = gtk_widget_get_window(m_drawing_area);
305 if (gdkwin == NULL) {
306 g_warning("lh_widget::redraw: No GdkWindow to draw on!");
309 cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
315 void lh_widget::paint_white()
317 GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
318 if (gdkwin == NULL) {
319 g_warning("lh_widget::clear: No GdkWindow to draw on!");
322 cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
324 /* Paint white background. */
326 gdk_drawable_get_size(gdkwin, &width, &height);
327 cairo_rectangle(cr, 0, 0, width, height);
328 cairo_set_source_rgb(cr, 255, 255, 255);
333 void lh_widget::clear()
337 m_rendered_width = 0;
339 m_clicked_url.clear();
342 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
345 if (m_cursor != cursor) {
352 void lh_widget::update_cursor()
355 const litehtml::tchar_t *href;
356 GdkWindow *w = gdk_display_get_window_at_pointer(gdk_display_get_default(),
358 GdkCursorType cursType = GDK_ARROW;
360 if (m_cursor == _t("pointer")) {
361 cursType = GDK_HAND2;
364 if (cursType == GDK_ARROW) {
365 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
367 gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
370 if (w != gtk_widget_get_window(m_drawing_area))
373 /* If it's an anchor, show its "href" attribute in statusbar,
374 * otherwise clear statusbar. */
375 if ((href = get_href_at(x, y)) != NULL) {
376 lh_widget_statusbar_push(fullurl(href).c_str());
378 lh_widget_statusbar_pop();
382 const litehtml::tchar_t *lh_widget::get_href_at(const gint x, const gint y) const
384 litehtml::element::ptr over_el, el;
389 over_el = m_html->root()->get_element_by_point(x, y, x, y);
393 /* If it's not an anchor, check if it has a parent anchor
394 * (e.g. it's an image within an anchor) and grab a pointer
396 if (strcmp(over_el->get_tagName(), "a") && over_el->parent()) {
397 el = over_el->parent();
398 while (el && el != m_html->root() && strcmp(el->get_tagName(), "a")) {
402 if (el && el != m_html->root())
408 /* At this point, over_el is pointing at an anchor tag, so let's
409 * grab its href attribute. */
410 return over_el->get_attr(_t("href"));
413 void lh_widget::print()
415 debug_print("lh_widget print\n");
416 gtk_widget_realize(GTK_WIDGET(m_drawing_area));
419 void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
420 GdkEventButton *event)
422 cm_return_if_fail(url != NULL);
423 cm_return_if_fail(event != NULL);
425 debug_print("lh_widget showing context menu for '%s'\n", url);
428 gtk_widget_show_all(m_context_menu);
429 gtk_menu_popup(GTK_MENU(m_context_menu), NULL, NULL, NULL, NULL,
430 event->button, event->time);
433 void lh_widget::update_font()
435 PangoFontDescription *pd =
436 pango_font_description_from_string(lh_prefs_get()->default_font);
437 gboolean absolute = pango_font_description_get_size_is_absolute(pd);
440 m_font_name = g_strdup(pango_font_description_get_family(pd));
441 m_font_size = pango_font_description_get_size(pd);
443 pango_font_description_free(pd);
446 m_font_size /= PANGO_SCALE;
448 debug_print("Font set to '%s', size %d\n", m_font_name, m_font_size);
451 const litehtml::tstring lh_widget::fullurl(const litehtml::tchar_t *url) const
453 if (*url == '#' && !m_base_url.empty())
454 return m_base_url + url;
459 ////////////////////////////////////////////////
460 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
463 lh_widget *w = (lh_widget *)user_data;
468 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
471 litehtml::position::vector redraw_boxes;
472 lh_widget *w = (lh_widget *)user_data;
474 if (w->m_html == NULL)
477 debug_print("lh_widget on_button_press_event\n");
479 if (event->type == GDK_2BUTTON_PRESS ||
480 event->type == GDK_3BUTTON_PRESS)
484 if (event->button == 3) {
485 const litehtml::tchar_t *url = w->get_href_at((gint)event->x, (gint)event->y);
488 w->popup_context_menu(url, event);
493 if(w->m_html->on_lbutton_down((int) event->x, (int) event->y,
494 (int) event->x, (int) event->y, redraw_boxes)) {
495 for(auto& pos : redraw_boxes) {
496 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
497 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
504 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
507 litehtml::position::vector redraw_boxes;
508 lh_widget *w = (lh_widget *)user_data;
510 //debug_print("lh_widget on_motion_notify_event\n");
514 //if(m_cursor == _t("pointer"))
515 if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
517 for (auto& pos : redraw_boxes)
519 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
520 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
528 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
531 litehtml::position::vector redraw_boxes;
532 lh_widget *w = (lh_widget *)user_data;
533 GError* error = NULL;
535 if (w->m_html == NULL)
538 debug_print("lh_widget on_button_release_event\n");
540 if (event->type == GDK_2BUTTON_PRESS ||
541 event->type == GDK_3BUTTON_PRESS)
545 if (event->button == 3)
548 w->m_clicked_url.clear();
550 if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
552 for (auto& pos : redraw_boxes)
554 debug_print("x: %d y:%d w: %d h: %d\n", pos.x, pos.y, pos.width, pos.height);
555 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
559 if (!w->m_clicked_url.empty())
561 debug_print("Open in browser: %s\n", w->m_clicked_url.c_str());
562 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
568 static void open_link_cb(GtkMenuItem *item, gpointer user_data)
570 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
572 open_uri(w->m_clicked_url.c_str(), prefs_common_get_uri_cmd());
575 static void copy_link_cb(GtkMenuItem *item, gpointer user_data)
577 lh_widget_wrapped *w = (lh_widget_wrapped *)user_data;
579 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY),
580 w->m_clicked_url.c_str(), -1);
581 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
582 w->m_clicked_url.c_str(), -1);
585 ///////////////////////////////////////////////////////////
588 lh_widget_wrapped *lh_widget_new()
590 return new lh_widget;
593 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
595 return w->get_widget();
598 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
603 void lh_widget_clear(lh_widget_wrapped *w)
608 void lh_widget_destroy(lh_widget_wrapped *w)
613 void lh_widget_print(lh_widget_wrapped *w) {