a4528c07a3946970c5a3a918c1748b8c50ab8b72
[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) 1999-2015 the Claws Mail Team
4  * == Fancy Plugin ==
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <curl/curl.h>
31 #include <gdk/gdk.h>
32 #include "lh_widget.h"
33 #include "lh_widget_wrapped.h"
34 #include "http.h"
35
36 char master_css[] = {
37 #include "css.inc"
38 };
39
40 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
41                 gpointer user_data);
42 static void size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation,
43                 gpointer user_data);
44 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
45                 gpointer user_data);
46 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
47         gpointer user_data);
48 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
49         gpointer user_data);
50
51 lh_widget::lh_widget()
52 {
53         /* scrolled window */
54         m_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
55         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_scrolled_window),
56                         GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
57         g_signal_connect(m_scrolled_window, "size-allocate",
58                         G_CALLBACK(size_allocate_cb), this);
59
60         /* viewport */
61         GtkScrolledWindow *scw = GTK_SCROLLED_WINDOW(m_scrolled_window);
62         m_viewport = gtk_viewport_new(
63                         gtk_scrolled_window_get_hadjustment(scw),
64                         gtk_scrolled_window_get_vadjustment(scw));
65         gtk_container_add(GTK_CONTAINER(m_scrolled_window), m_viewport);
66
67         /* drawing area */
68         m_drawing_area = gtk_drawing_area_new();
69         gtk_container_add(GTK_CONTAINER(m_viewport), m_drawing_area);
70         g_signal_connect(m_drawing_area, "expose-event",
71                         G_CALLBACK(expose_event_cb), this);
72         g_signal_connect(m_drawing_area, "motion_notify_event",
73                         G_CALLBACK(motion_notify_event), this);
74         g_signal_connect(m_drawing_area, "button_press_event",
75                         G_CALLBACK(button_press_event), this);
76         g_signal_connect(m_drawing_area, "button_release_event",
77                         G_CALLBACK(button_release_event), this);
78
79         gtk_widget_show_all(m_scrolled_window);
80
81         m_html = NULL;
82         m_rendered_width = 0;
83         m_context.load_master_stylesheet(master_css);
84
85         gtk_widget_set_events(m_drawing_area,
86                                 GDK_BUTTON_RELEASE_MASK
87                               | GDK_BUTTON_PRESS_MASK
88                               | GDK_POINTER_MOTION_MASK);
89
90 }
91
92 lh_widget::~lh_widget()
93 {
94         g_object_unref(m_drawing_area);
95         m_drawing_area = NULL;
96         g_object_unref(m_scrolled_window);
97         m_scrolled_window = NULL;
98         m_html = NULL;
99 }
100
101 GtkWidget *lh_widget::get_widget() const
102 {
103         return m_scrolled_window;
104 }
105
106 void lh_widget::set_caption(const litehtml::tchar_t* caption)
107 {
108         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget set_caption");
109         return;
110 }
111
112 void lh_widget::set_base_url(const litehtml::tchar_t* base_url)
113 {
114         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget set_base_url");
115         return;
116 }
117
118 void lh_widget::on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el)
119 {
120         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_anchor_click. url -> %s", url);
121         m_clicked_url = url;
122         
123         return;
124 }
125
126 void lh_widget::import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl)
127 {
128         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget import_css");
129         baseurl = master_css;
130 }
131
132 void lh_widget::get_client_rect(litehtml::position& client) const
133 {
134         if (m_drawing_area == NULL)
135                 return;
136
137         client.width = m_rendered_width;
138         client.height = m_height;
139         client.x = 0;
140         client.y = 0;
141
142 //      g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget::get_client_rect: %dx%d",
143 //                      client.width, client.height);
144 }
145
146 GdkPixbuf *lh_widget::get_image(const litehtml::tchar_t* url, bool redraw_on_ready)
147 {
148         GError *error = NULL;
149         GdkPixbuf *pixbuf = NULL;
150
151         g_log(NULL, G_LOG_LEVEL_MESSAGE, "Loading... %s", url);
152         lh_widget_statusbar_push(g_strconcat("Loading ", url, " ...", NULL));
153         
154         http http_loader;
155         GInputStream *image = http_loader.load_url(url, &error);
156     
157         if (error || !image) {
158             if (error) {
159                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::get_image: Could not create pixbuf %s", error->message);
160                 g_clear_error(&error);
161             }
162             goto statusbar_pop;
163         }
164
165         pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
166         if (error) {
167             g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::get_image: Could not create pixbuf %s", error->message);
168             //g_object_unref(pixbuf);
169             pixbuf = NULL;
170             g_clear_error(&error);
171         }
172         g_input_stream_close(image, NULL, NULL);
173
174 /*      if (redraw_on_ready) {
175                 redraw();
176         }*/
177
178 statusbar_pop:
179         lh_widget_statusbar_pop();
180         
181         return pixbuf;
182 }
183
184 void lh_widget::open_html(const gchar *contents)
185 {
186         lh_widget_statusbar_push(g_strconcat("Loading HTML part", " ...", NULL));
187         m_html = litehtml::document::createFromString(contents, this, &m_context);
188         m_rendered_width = 0;
189         if (m_html != NULL) {
190                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget::open_html created document");
191                 redraw();
192         }
193         lh_widget_statusbar_pop();
194 }
195
196 void lh_widget::draw(cairo_t *cr)
197 {
198         double x1, x2, y1, y2;
199         double width, height;
200
201         if (m_html == NULL)
202                 return;
203
204         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
205
206         width = x2 - x1;
207         height = y2 - y1;
208
209         litehtml::position pos;
210         pos.width = (int)width;
211         pos.height = (int)height;
212         pos.x = (int)x1;
213         pos.y = (int)y1;
214
215         m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
216 }
217
218 void lh_widget::redraw()
219 {
220         GtkAllocation rect;
221         gint width, height;
222         GdkWindow *gdkwin;
223         cairo_t *cr;
224
225         if (m_html == NULL) {
226                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::redraw: No document!");
227                 return;
228         }
229
230         /* Get width of the viewport. */
231         gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
232         gdk_drawable_get_size(gdkwin, &width, NULL);
233
234         /* If the available width has changed, rerender the HTML content. */
235         if (m_rendered_width != width) {
236                 g_log(NULL, G_LOG_LEVEL_MESSAGE,
237                                 "lh_widget::redraw: width changed: %d != %d",
238                                 m_rendered_width, width);
239
240                 /* Update our internally stored width, mainly so that
241                  * lh_widget::get_client_rect() gives correct width during the
242                  * render. */
243                 m_rendered_width = width;
244
245                 /* Re-render HTML for this width. */
246                 m_html->media_changed();
247                 m_html->render(m_rendered_width);
248                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "render is %dx%d",
249                                 m_html->width(), m_html->height());
250
251                 /* Change drawing area's size to match what was rendered. */
252                 gtk_widget_set_size_request(m_drawing_area,
253                                 m_html->width(), m_html->height());
254         }
255
256         paint_white();
257
258         /* Paint the rendered HTML. */
259         gdkwin = gtk_widget_get_window(m_drawing_area);
260         if (gdkwin == NULL) {
261                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::redraw: No GdkWindow to draw on!");
262                 return;
263         }
264         cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
265         draw(cr);
266
267         cairo_destroy(cr);
268 }
269
270 void lh_widget::paint_white()
271 {
272         GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
273         if (gdkwin == NULL) {
274                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::clear: No GdkWindow to draw on!");
275                 return;
276         }
277         cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
278
279         /* Paint white background. */
280         gint width, height;
281         gdk_drawable_get_size(gdkwin, &width, &height);
282         cairo_rectangle(cr, 0, 0, width, height);
283         cairo_set_source_rgb(cr, 255, 255, 255);
284         cairo_fill(cr);
285
286         cairo_destroy(cr);
287 }
288 void lh_widget::clear()
289 {
290         paint_white();
291         m_rendered_width = 0;
292 }
293
294 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
295 {
296     //g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget set_cursor %s:%s", m_cursor, cursor);
297     if (cursor)
298     {
299         if (m_cursor != cursor)
300         {
301             m_cursor = cursor;
302             update_cursor();
303         }
304     }
305 }
306
307 void lh_widget::update_cursor()
308 {
309     //g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget update_cursor %s", m_cursor);
310     GdkCursorType cursType = GDK_ARROW;
311     if(m_cursor == _t("pointer"))
312     {
313         cursType = GDK_HAND2;
314     }
315     if(cursType == GDK_ARROW)
316     {
317         gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
318     } else
319     {
320         gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
321     }
322 }
323
324 void lh_widget::print()
325 {
326     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget print");
327     gtk_widget_realize(GTK_WIDGET(m_drawing_area));
328 }
329
330 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
331                 gpointer user_data)
332 {
333         lh_widget *w = (lh_widget *)user_data;
334         w->redraw();
335         return FALSE;
336 }
337
338 static void size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation,
339                 gpointer user_data)
340 {
341         lh_widget *w = (lh_widget *)user_data;
342
343         g_log(NULL, G_LOG_LEVEL_MESSAGE, "size_allocate_cb: %dx%d",
344                         allocation->width, allocation->height);
345
346         w->setHeight(allocation->height);
347         w->redraw();
348 }
349
350 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
351                 gpointer user_data)
352 {
353     litehtml::position::vector redraw_boxes;
354     lh_widget *w = (lh_widget *)user_data;
355     
356     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_button_press_event");
357
358     if(w->m_html)
359     {    
360         if(w->m_html->on_lbutton_down((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
361         {
362             for(auto& pos : redraw_boxes)
363             {
364                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
365                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
366             }
367         }
368         }
369         
370         return true;
371 }
372
373 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
374         gpointer user_data)
375 {
376     litehtml::position::vector redraw_boxes;
377     lh_widget *w = (lh_widget *)user_data;
378     
379     //g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_motion_notify_event");
380
381     if(w->m_html)
382     {    
383         if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
384         {
385             for (auto& pos : redraw_boxes)
386             {
387                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
388                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
389             }
390         }
391         }
392         
393         return true;
394 }
395
396 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
397         gpointer user_data)
398 {
399     litehtml::position::vector redraw_boxes;
400     lh_widget *w = (lh_widget *)user_data;
401     GError* error = NULL;
402
403         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_button_release_event");
404         
405         if(w->m_html)
406         {
407             w->m_clicked_url.clear();
408         if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
409         {
410             for (auto& pos : redraw_boxes)
411             {
412                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
413                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
414             }
415         }
416         
417         if (!w->m_clicked_url.empty())
418         {
419                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "Open in browser: %s", w->m_clicked_url.c_str());
420                 gtk_show_uri(gdk_screen_get_default(),
421                              w->m_clicked_url.c_str(),
422                              GDK_CURRENT_TIME, &error);
423                 if (error) {
424                     g_log(NULL, G_LOG_LEVEL_WARNING, "Failed opening url(%s): %s", w->m_clicked_url, error->message);
425                     g_clear_error(&error);
426                 }
427         }
428     }
429
430         return true;
431 }
432
433 ///////////////////////////////////////////////////////////
434 extern "C" {
435
436 lh_widget_wrapped *lh_widget_new()
437 {
438         return new lh_widget;
439 }
440
441 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
442 {
443         return w->get_widget();
444 }
445
446 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
447 {
448         w->open_html(path);
449 }
450
451 void lh_widget_clear(lh_widget_wrapped *w)
452 {
453         w->clear();
454 }
455
456 void lh_widget_destroy(lh_widget_wrapped *w)
457 {
458         delete w;
459 }
460
461 void lh_widget_print(lh_widget_wrapped *w) {
462         w->print();
463 }
464
465 } /* extern "C" */