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"
23 #include "container_linux.h"
27 #define _USE_MATH_DEFINES
31 # define M_PI 3.14159265358979323846
34 container_linux::container_linux(void)
36 m_temp_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2, 2);
37 m_temp_cr = cairo_create(m_temp_surface);
38 g_rec_mutex_init(&m_images_lock);
41 container_linux::~container_linux(void)
44 cairo_surface_destroy(m_temp_surface);
45 cairo_destroy(m_temp_cr);
46 g_rec_mutex_clear(&m_images_lock);
49 int container_linux::pt_to_px( int pt )
51 GdkScreen* screen = gdk_screen_get_default();
52 double dpi = gdk_screen_get_resolution(screen);
54 return (int) ((double) pt * dpi / 72.0);
57 void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::list_marker& marker )
59 if(!marker.image.empty())
61 /*litehtml::tstring url;
62 make_url(marker.image.c_str(), marker.baseurl, url);
65 images_map::iterator img_i = m_images.find(url.c_str());
66 if(img_i != m_images.end())
70 draw_txdib((cairo_t*) hdc, img_i->second, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height);
73 unlock_images_cache();*/
76 switch(marker.marker_type)
78 case litehtml::list_style_type_circle:
80 draw_ellipse((cairo_t*) hdc, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height, marker.color, 0.5);
83 case litehtml::list_style_type_disc:
85 fill_ellipse((cairo_t*) hdc, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height, marker.color);
88 case litehtml::list_style_type_square:
91 cairo_t* cr = (cairo_t*) hdc;
95 cairo_rectangle(cr, marker.pos.x, marker.pos.y, marker.pos.width, marker.pos.height);
97 set_color(cr, marker.color);
109 void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::background_paint& bg )
111 cairo_t* cr = (cairo_t*) hdc;
115 rounded_rectangle(cr, bg.border_box, bg.border_radius);
118 cairo_rectangle(cr, bg.clip_box.x, bg.clip_box.y, bg.clip_box.width, bg.clip_box.height);
123 set_color(cr, bg.color);
127 litehtml::tstring url;
128 make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
132 const image *img_i = NULL;
134 for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
135 const image *i = &(*ii);
136 if (i->first == url) {
143 if(img_i != NULL && img_i->second)
145 GdkPixbuf *bgbmp = img_i->second;
148 if(bg.image_size.width != gdk_pixbuf_get_width(bgbmp) || bg.image_size.height != gdk_pixbuf_get_height(bgbmp))
150 new_img = gdk_pixbuf_scale_simple(bgbmp, bg.image_size.width, bg.image_size.height, GDK_INTERP_BILINEAR);
154 cairo_surface_t* img = surface_from_pixbuf(bgbmp);
155 cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img);
156 cairo_matrix_t flib_m;
157 cairo_matrix_init_identity(&flib_m);
158 cairo_matrix_translate(&flib_m, -bg.position_x, -bg.position_y);
159 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
160 cairo_pattern_set_matrix (pattern, &flib_m);
164 case litehtml::background_repeat_no_repeat:
165 draw_pixbuf(cr, bgbmp, bg.position_x, bg.position_y, gdk_pixbuf_get_width(bgbmp), gdk_pixbuf_get_height(bgbmp));
168 case litehtml::background_repeat_repeat_x:
169 cairo_set_source(cr, pattern);
170 cairo_rectangle(cr, bg.clip_box.left(), bg.position_y, bg.clip_box.width, gdk_pixbuf_get_height(bgbmp));
174 case litehtml::background_repeat_repeat_y:
175 cairo_set_source(cr, pattern);
176 cairo_rectangle(cr, bg.position_x, bg.clip_box.top(), gdk_pixbuf_get_width(bgbmp), bg.clip_box.height);
180 case litehtml::background_repeat_repeat:
181 cairo_set_source(cr, pattern);
182 cairo_rectangle(cr, bg.clip_box.left(), bg.clip_box.top(), bg.clip_box.width, bg.clip_box.height);
187 cairo_pattern_destroy(pattern);
188 cairo_surface_destroy(img);
192 unlock_images_cache();
196 void container_linux::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out)
201 void container_linux::add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg)
208 cairo_translate(cr, x, y);
209 cairo_scale(cr, 1, ry / rx);
210 cairo_translate(cr, -x, -y);
214 cairo_arc_negative(cr, x, y, rx, a1, a2);
217 cairo_arc(cr, x, y, rx, a1, a2);
223 cairo_move_to(cr, x, y);
227 void container_linux::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root)
229 cairo_t* cr = (cairo_t*) hdc;
240 if(borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden)
242 bdr_top = (int) borders.top.width;
244 if(borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden)
246 bdr_bottom = (int) borders.bottom.width;
248 if(borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden)
250 bdr_left = (int) borders.left.width;
252 if(borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden)
254 bdr_right = (int) borders.right.width;
260 set_color(cr, borders.right.color);
262 double r_top = borders.radius.top_right_x;
263 double r_bottom = borders.radius.bottom_right_x;
267 double end_angle = 2 * M_PI;
268 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_top / (double) bdr_right + 1);
271 draw_pos.right() - r_top,
272 draw_pos.top() + r_top,
274 r_top - bdr_right + (bdr_right - bdr_top),
279 draw_pos.right() - r_top,
280 draw_pos.top() + r_top,
287 cairo_move_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
288 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
293 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom() - r_bottom);
295 double start_angle = 0;
296 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_right + 1);
299 draw_pos.right() - r_bottom,
300 draw_pos.bottom() - r_bottom,
307 draw_pos.right() - r_bottom,
308 draw_pos.bottom() - r_bottom,
309 r_bottom - bdr_right,
310 r_bottom - bdr_right + (bdr_right - bdr_bottom),
315 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
316 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
322 // draw bottom border
325 set_color(cr, borders.bottom.color);
327 double r_left = borders.radius.bottom_left_x;
328 double r_right = borders.radius.bottom_right_x;
332 double start_angle = M_PI / 2.0;
333 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_left / (double) bdr_bottom + 1);
336 draw_pos.left() + r_left,
337 draw_pos.bottom() - r_left,
338 r_left - bdr_bottom + (bdr_bottom - bdr_left),
344 draw_pos.left() + r_left,
345 draw_pos.bottom() - r_left,
352 cairo_move_to(cr, draw_pos.left(), draw_pos.bottom());
353 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
358 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.bottom());
360 double end_angle = M_PI / 2.0;
361 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_right / (double) bdr_bottom + 1);
364 draw_pos.right() - r_right,
365 draw_pos.bottom() - r_right,
372 draw_pos.right() - r_right,
373 draw_pos.bottom() - r_right,
374 r_right - bdr_bottom + (bdr_bottom - bdr_right),
375 r_right - bdr_bottom,
380 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
381 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
390 set_color(cr, borders.top.color);
392 double r_left = borders.radius.top_left_x;
393 double r_right = borders.radius.top_right_x;
397 double end_angle = M_PI * 3.0 / 2.0;
398 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_left / (double) bdr_top + 1);
401 draw_pos.left() + r_left,
402 draw_pos.top() + r_left,
409 draw_pos.left() + r_left,
410 draw_pos.top() + r_left,
411 r_left - bdr_top + (bdr_top - bdr_left),
417 cairo_move_to(cr, draw_pos.left(), draw_pos.top());
418 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
423 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.top() + bdr_top);
425 double start_angle = M_PI * 3.0 / 2.0;
426 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_right / (double) bdr_top + 1);
429 draw_pos.right() - r_right,
430 draw_pos.top() + r_right,
431 r_right - bdr_top + (bdr_top - bdr_right),
437 draw_pos.right() - r_right,
438 draw_pos.top() + r_right,
445 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
446 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
455 set_color(cr, borders.left.color);
457 double r_top = borders.radius.top_left_x;
458 double r_bottom = borders.radius.bottom_left_x;
462 double start_angle = M_PI;
463 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_top / (double) bdr_left + 1);
466 draw_pos.left() + r_top,
467 draw_pos.top() + r_top,
469 r_top - bdr_left + (bdr_left - bdr_top),
474 draw_pos.left() + r_top,
475 draw_pos.top() + r_top,
482 cairo_move_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
483 cairo_line_to(cr, draw_pos.left(), draw_pos.top());
488 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom() - r_bottom);
490 double end_angle = M_PI;
491 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_left + 1);
494 draw_pos.left() + r_bottom,
495 draw_pos.bottom() - r_bottom,
502 draw_pos.left() + r_bottom,
503 draw_pos.bottom() - r_bottom,
505 r_bottom - bdr_left + (bdr_left - bdr_bottom),
510 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom());
511 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
519 void container_linux::transform_text(litehtml::tstring& text, litehtml::text_transform tt)
524 void container_linux::set_clip( const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y )
526 litehtml::position clip_pos = pos;
527 litehtml::position client_pos;
528 get_client_rect(client_pos);
531 clip_pos.x = client_pos.x;
532 clip_pos.width = client_pos.width;
536 clip_pos.y = client_pos.y;
537 clip_pos.height = client_pos.height;
539 m_clips.emplace_back(clip_pos, bdr_radius);
542 void container_linux::del_clip()
550 void container_linux::apply_clip( cairo_t* cr )
552 for(const auto& clip_box : m_clips)
554 rounded_rectangle(cr, clip_box.box, clip_box.radius);
559 void container_linux::draw_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width )
568 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
569 cairo_scale (cr, width / 2.0, height / 2.0);
570 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
572 set_color(cr, color);
573 cairo_set_line_width(cr, line_width);
579 void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color )
588 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
589 cairo_scale (cr, width / 2.0, height / 2.0);
590 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
592 set_color(cr, color);
598 std::shared_ptr<litehtml::element> container_linux::create_element(const litehtml::tchar_t *tag_name,
599 const litehtml::string_map &attributes,
600 const std::shared_ptr<litehtml::document> &doc)
605 void container_linux::rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius )
608 if(radius.top_left_x)
610 cairo_arc(cr, pos.left() + radius.top_left_x, pos.top() + radius.top_left_x, radius.top_left_x, M_PI, M_PI * 3.0 / 2.0);
613 cairo_move_to(cr, pos.left(), pos.top());
616 cairo_line_to(cr, pos.right() - radius.top_right_x, pos.top());
618 if(radius.top_right_x)
620 cairo_arc(cr, pos.right() - radius.top_right_x, pos.top() + radius.top_right_x, radius.top_right_x, M_PI * 3.0 / 2.0, 2.0 * M_PI);
623 cairo_line_to(cr, pos.right(), pos.bottom() - radius.bottom_right_x);
625 if(radius.bottom_right_x)
627 cairo_arc(cr, pos.right() - radius.bottom_right_x, pos.bottom() - radius.bottom_right_x, radius.bottom_right_x, 0, M_PI / 2.0);
630 cairo_line_to(cr, pos.left() - radius.bottom_left_x, pos.bottom());
632 if(radius.bottom_left_x)
634 cairo_arc(cr, pos.left() + radius.bottom_left_x, pos.bottom() - radius.bottom_left_x, radius.bottom_left_x, M_PI / 2.0, M_PI);
638 void container_linux::draw_pixbuf(cairo_t* cr, const GdkPixbuf *bmp, int x, int y, int cx, int cy)
643 cairo_matrix_t flib_m;
644 cairo_matrix_init(&flib_m, 1, 0, 0, -1, 0, 0);
646 if(cx != gdk_pixbuf_get_width(bmp) || cy != gdk_pixbuf_get_height(bmp))
648 GdkPixbuf *new_img = gdk_pixbuf_scale_simple(bmp, cx, cy, GDK_INTERP_BILINEAR);
649 gdk_cairo_set_source_pixbuf(cr, new_img, x, y);
653 gdk_cairo_set_source_pixbuf(cr, bmp, x, y);
661 cairo_surface_t* container_linux::surface_from_pixbuf(const GdkPixbuf *bmp)
663 cairo_surface_t* ret = NULL;
665 if(gdk_pixbuf_get_has_alpha(bmp))
667 ret = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
670 ret = cairo_image_surface_create(CAIRO_FORMAT_RGB24, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
673 // Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(ret, false));
674 // Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create(surface);
675 // Gdk::Cairo::set_source_pixbuf(ctx, bmp, 0.0, 0.0);
676 cairo_t *ctx = cairo_create(ret);
683 void container_linux::get_media_features(litehtml::media_features& media) const
685 litehtml::position client;
686 get_client_rect(client);
687 media.type = litehtml::media_type_screen;
688 media.width = client.width;
689 media.height = client.height;
690 media.device_width = gdk_screen_width();
691 media.device_height = gdk_screen_height();
693 media.monochrome = 0;
694 media.color_index = 256;
695 media.resolution = 96;
698 void container_linux::get_language(litehtml::tstring& language, litehtml::tstring& culture) const
704 void container_linux::link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el)