432a1699eae72d7741e32d77599f7678fbcc6231
[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
153     http http_loader;
154     GInputStream *image = http_loader.load_url(url, &error);
155     
156         if (!image) return NULL;
157         
158         pixbuf = gdk_pixbuf_new_from_stream(image, NULL, &error);
159         if (error) {
160             g_log(NULL, G_LOG_LEVEL_ERROR, "lh_widget::get_image: Could not create pixbuf %s", error->message);
161             //g_object_unref(pixbuf);
162             pixbuf = NULL;
163             g_clear_error(&error);
164         }
165         g_input_stream_close(image, NULL, NULL);
166
167 /*      if (redraw_on_ready) {
168                 redraw();
169         }*/
170         
171         return pixbuf;
172 }
173
174 void lh_widget::open_html(const gchar *contents)
175 {
176         m_html = litehtml::document::createFromString(contents, this, &m_context);
177         m_rendered_width = 0;
178         if (m_html != NULL) {
179                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget::open_html created document");
180                 redraw();
181         }
182 }
183
184 void lh_widget::draw(cairo_t *cr)
185 {
186         double x1, x2, y1, y2;
187         double width, height;
188
189         if (m_html == NULL)
190                 return;
191
192         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
193
194         width = x2 - x1;
195         height = y2 - y1;
196
197         litehtml::position pos;
198         pos.width = (int)width;
199         pos.height = (int)height;
200         pos.x = (int)x1;
201         pos.y = (int)y1;
202
203         m_html->draw((litehtml::uint_ptr)cr, 0, 0, &pos);
204 }
205
206 void lh_widget::redraw()
207 {
208         GtkAllocation rect;
209         gint width, height;
210         GdkWindow *gdkwin;
211         cairo_t *cr;
212
213         if (m_html == NULL) {
214                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::redraw: No document!");
215                 return;
216         }
217
218         /* Get width of the viewport. */
219         gdkwin = gtk_viewport_get_view_window(GTK_VIEWPORT(m_viewport));
220         gdk_drawable_get_size(gdkwin, &width, NULL);
221
222         /* If the available width has changed, rerender the HTML content. */
223         if (m_rendered_width != width) {
224                 g_log(NULL, G_LOG_LEVEL_MESSAGE,
225                                 "lh_widget::redraw: width changed: %d != %d",
226                                 m_rendered_width, width);
227
228                 /* Update our internally stored width, mainly so that
229                  * lh_widget::get_client_rect() gives correct width during the
230                  * render. */
231                 m_rendered_width = width;
232
233                 /* Re-render HTML for this width. */
234                 m_html->media_changed();
235                 m_html->render(m_rendered_width);
236                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "render is %dx%d",
237                                 m_html->width(), m_html->height());
238
239                 /* Change drawing area's size to match what was rendered. */
240                 gtk_widget_set_size_request(m_drawing_area,
241                                 m_html->width(), m_html->height());
242         }
243
244         paint_white();
245
246         /* Paint the rendered HTML. */
247         gdkwin = gtk_widget_get_window(m_drawing_area);
248         if (gdkwin == NULL) {
249                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::redraw: No GdkWindow to draw on!");
250                 return;
251         }
252         cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
253         draw(cr);
254
255         cairo_destroy(cr);
256 }
257
258 void lh_widget::paint_white()
259 {
260         GdkWindow *gdkwin = gtk_widget_get_window(m_drawing_area);
261         if (gdkwin == NULL) {
262                 g_log(NULL, G_LOG_LEVEL_WARNING, "lh_widget::clear: No GdkWindow to draw on!");
263                 return;
264         }
265         cairo_t *cr = gdk_cairo_create(GDK_DRAWABLE(gdkwin));
266
267         /* Paint white background. */
268         gint width, height;
269         gdk_drawable_get_size(gdkwin, &width, &height);
270         cairo_rectangle(cr, 0, 0, width, height);
271         cairo_set_source_rgb(cr, 255, 255, 255);
272         cairo_fill(cr);
273
274         cairo_destroy(cr);
275 }
276 void lh_widget::clear()
277 {
278         paint_white();
279         m_rendered_width = 0;
280 }
281
282 void lh_widget::set_cursor(const litehtml::tchar_t* cursor)
283 {
284     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget set_cursor %s:%s", m_cursor, cursor);
285     if (cursor)
286     {
287         if (m_cursor != cursor)
288         {
289             m_cursor = cursor;
290             update_cursor();
291         }
292     }
293 }
294
295 void lh_widget::update_cursor()
296 {
297     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget update_cursor %s", m_cursor);
298     GdkCursorType cursType = GDK_ARROW;
299     if(m_cursor == _t("pointer"))
300     {
301         cursType = GDK_HAND2;
302     }
303     if(cursType == GDK_ARROW)
304     {
305         gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), NULL);
306     } else
307     {
308         gdk_window_set_cursor(gtk_widget_get_window(m_drawing_area), gdk_cursor_new(cursType));
309     }
310 }
311
312 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
313                 gpointer user_data)
314 {
315         lh_widget *w = (lh_widget *)user_data;
316         w->redraw();
317         return FALSE;
318 }
319
320 static void size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation,
321                 gpointer user_data)
322 {
323         lh_widget *w = (lh_widget *)user_data;
324
325         g_log(NULL, G_LOG_LEVEL_MESSAGE, "size_allocate_cb: %dx%d",
326                         allocation->width, allocation->height);
327
328         w->setHeight(allocation->height);
329         w->redraw();
330 }
331
332 static gboolean button_press_event(GtkWidget *widget, GdkEventButton *event,
333                 gpointer user_data)
334 {
335     litehtml::position::vector redraw_boxes;
336     lh_widget *w = (lh_widget *)user_data;
337     
338     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_button_press_event");
339
340     if(w->m_html)
341     {    
342         if(w->m_html->on_lbutton_down((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
343         {
344             for(auto& pos : redraw_boxes)
345             {
346                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
347                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
348             }
349         }
350         }
351         
352         return true;
353 }
354
355 static gboolean motion_notify_event(GtkWidget *widget, GdkEventButton *event,
356         gpointer user_data)
357 {
358     litehtml::position::vector redraw_boxes;
359     lh_widget *w = (lh_widget *)user_data;
360     
361     g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_motion_notify_event");
362
363     if(w->m_html)
364     {    
365         if(w->m_html->on_mouse_over((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
366         {
367             for (auto& pos : redraw_boxes)
368             {
369                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
370                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
371             }
372         }
373         }
374         
375         return true;
376 }
377
378 static gboolean button_release_event(GtkWidget *widget, GdkEventButton *event,
379         gpointer user_data)
380 {
381     litehtml::position::vector redraw_boxes;
382     lh_widget *w = (lh_widget *)user_data;
383     GError* error = NULL;
384
385         g_log(NULL, G_LOG_LEVEL_MESSAGE, "lh_widget on_button_release_event");
386         
387         if(w->m_html)
388         {
389             w->m_clicked_url.clear();
390         if(w->m_html->on_lbutton_up((int) event->x, (int) event->y, (int) event->x, (int) event->y, redraw_boxes))
391         {
392             for (auto& pos : redraw_boxes)
393             {
394                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "x: %d y:%d w: %d h: %d", pos.x, pos.y, pos.width, pos.height);
395                 gtk_widget_queue_draw_area(widget, pos.x, pos.y, pos.width, pos.height);
396             }
397         }
398         
399         if (!w->m_clicked_url.empty())
400         {
401                 g_log(NULL, G_LOG_LEVEL_MESSAGE, "Open in browser: %s", w->m_clicked_url.c_str());
402                 gtk_show_uri(gdk_screen_get_default(),
403                              w->m_clicked_url.c_str(),
404                              GDK_CURRENT_TIME, &error);
405                 if (error) {
406                     g_log(NULL, G_LOG_LEVEL_ERROR, "Failed opening url(%s): %s", w->m_clicked_url, error->message);
407                     g_clear_error(&error);
408                 }
409         }
410     }
411
412         return true;
413 }
414
415 ///////////////////////////////////////////////////////////
416 extern "C" {
417
418 lh_widget_wrapped *lh_widget_new()
419 {
420         return new lh_widget;
421 }
422
423 GtkWidget *lh_widget_get_widget(lh_widget_wrapped *w)
424 {
425         return w->get_widget();
426 }
427
428 void lh_widget_open_html(lh_widget_wrapped *w, const gchar *path)
429 {
430         w->open_html(path);
431 }
432
433 void lh_widget_clear(lh_widget_wrapped *w)
434 {
435         w->clear();
436 }
437
438 void lh_widget_destroy(lh_widget_wrapped *w)
439 {
440         delete w;
441 }
442
443 } /* extern "C" */