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);
130 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) {
142 if(img_i != NULL && img_i->second)
144 GdkPixbuf *bgbmp = img_i->second;
147 if(bg.image_size.width != gdk_pixbuf_get_width(bgbmp) || bg.image_size.height != gdk_pixbuf_get_height(bgbmp))
149 new_img = gdk_pixbuf_scale_simple(bgbmp, bg.image_size.width, bg.image_size.height, GDK_INTERP_BILINEAR);
153 cairo_surface_t* img = surface_from_pixbuf(bgbmp);
154 cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img);
155 cairo_matrix_t flib_m;
156 cairo_matrix_init_identity(&flib_m);
157 cairo_matrix_translate(&flib_m, -bg.position_x, -bg.position_y);
158 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
159 cairo_pattern_set_matrix (pattern, &flib_m);
163 case litehtml::background_repeat_no_repeat:
164 draw_pixbuf(cr, bgbmp, bg.position_x, bg.position_y, gdk_pixbuf_get_width(bgbmp), gdk_pixbuf_get_height(bgbmp));
167 case litehtml::background_repeat_repeat_x:
168 cairo_set_source(cr, pattern);
169 cairo_rectangle(cr, bg.clip_box.left(), bg.position_y, bg.clip_box.width, gdk_pixbuf_get_height(bgbmp));
173 case litehtml::background_repeat_repeat_y:
174 cairo_set_source(cr, pattern);
175 cairo_rectangle(cr, bg.position_x, bg.clip_box.top(), gdk_pixbuf_get_width(bgbmp), bg.clip_box.height);
179 case litehtml::background_repeat_repeat:
180 cairo_set_source(cr, pattern);
181 cairo_rectangle(cr, bg.clip_box.left(), bg.clip_box.top(), bg.clip_box.width, bg.clip_box.height);
186 cairo_pattern_destroy(pattern);
187 cairo_surface_destroy(img);
191 unlock_images_cache();
195 void container_linux::make_url(const litehtml::tchar_t* url, const litehtml::tchar_t* basepath, litehtml::tstring& out)
200 void container_linux::add_path_arc(cairo_t* cr, double x, double y, double rx, double ry, double a1, double a2, bool neg)
207 cairo_translate(cr, x, y);
208 cairo_scale(cr, 1, ry / rx);
209 cairo_translate(cr, -x, -y);
213 cairo_arc_negative(cr, x, y, rx, a1, a2);
216 cairo_arc(cr, x, y, rx, a1, a2);
222 cairo_move_to(cr, x, y);
226 void container_linux::draw_borders(litehtml::uint_ptr hdc, const litehtml::borders& borders, const litehtml::position& draw_pos, bool root)
228 cairo_t* cr = (cairo_t*) hdc;
239 if(borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden)
241 bdr_top = (int) borders.top.width;
243 if(borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden)
245 bdr_bottom = (int) borders.bottom.width;
247 if(borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden)
249 bdr_left = (int) borders.left.width;
251 if(borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden)
253 bdr_right = (int) borders.right.width;
259 set_color(cr, borders.right.color);
261 double r_top = borders.radius.top_right_x;
262 double r_bottom = borders.radius.bottom_right_x;
266 double end_angle = 2 * M_PI;
267 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_top / (double) bdr_right + 1);
270 draw_pos.right() - r_top,
271 draw_pos.top() + r_top,
273 r_top - bdr_right + (bdr_right - bdr_top),
278 draw_pos.right() - r_top,
279 draw_pos.top() + r_top,
286 cairo_move_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
287 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
292 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom() - r_bottom);
294 double start_angle = 0;
295 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_right + 1);
298 draw_pos.right() - r_bottom,
299 draw_pos.bottom() - r_bottom,
306 draw_pos.right() - r_bottom,
307 draw_pos.bottom() - r_bottom,
308 r_bottom - bdr_right,
309 r_bottom - bdr_right + (bdr_right - bdr_bottom),
314 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
315 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
321 // draw bottom border
324 set_color(cr, borders.bottom.color);
326 double r_left = borders.radius.bottom_left_x;
327 double r_right = borders.radius.bottom_right_x;
331 double start_angle = M_PI / 2.0;
332 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_left / (double) bdr_bottom + 1);
335 draw_pos.left() + r_left,
336 draw_pos.bottom() - r_left,
337 r_left - bdr_bottom + (bdr_bottom - bdr_left),
343 draw_pos.left() + r_left,
344 draw_pos.bottom() - r_left,
351 cairo_move_to(cr, draw_pos.left(), draw_pos.bottom());
352 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
357 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.bottom());
359 double end_angle = M_PI / 2.0;
360 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_right / (double) bdr_bottom + 1);
363 draw_pos.right() - r_right,
364 draw_pos.bottom() - r_right,
371 draw_pos.right() - r_right,
372 draw_pos.bottom() - r_right,
373 r_right - bdr_bottom + (bdr_bottom - bdr_right),
374 r_right - bdr_bottom,
379 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.bottom() - bdr_bottom);
380 cairo_line_to(cr, draw_pos.right(), draw_pos.bottom());
389 set_color(cr, borders.top.color);
391 double r_left = borders.radius.top_left_x;
392 double r_right = borders.radius.top_right_x;
396 double end_angle = M_PI * 3.0 / 2.0;
397 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_left / (double) bdr_top + 1);
400 draw_pos.left() + r_left,
401 draw_pos.top() + r_left,
408 draw_pos.left() + r_left,
409 draw_pos.top() + r_left,
410 r_left - bdr_top + (bdr_top - bdr_left),
416 cairo_move_to(cr, draw_pos.left(), draw_pos.top());
417 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
422 cairo_line_to(cr, draw_pos.right() - r_right, draw_pos.top() + bdr_top);
424 double start_angle = M_PI * 3.0 / 2.0;
425 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_right / (double) bdr_top + 1);
428 draw_pos.right() - r_right,
429 draw_pos.top() + r_right,
430 r_right - bdr_top + (bdr_top - bdr_right),
436 draw_pos.right() - r_right,
437 draw_pos.top() + r_right,
444 cairo_line_to(cr, draw_pos.right() - bdr_right, draw_pos.top() + bdr_top);
445 cairo_line_to(cr, draw_pos.right(), draw_pos.top());
454 set_color(cr, borders.left.color);
456 double r_top = borders.radius.top_left_x;
457 double r_bottom = borders.radius.bottom_left_x;
461 double start_angle = M_PI;
462 double end_angle = start_angle + M_PI / 2.0 / ((double) bdr_top / (double) bdr_left + 1);
465 draw_pos.left() + r_top,
466 draw_pos.top() + r_top,
468 r_top - bdr_left + (bdr_left - bdr_top),
473 draw_pos.left() + r_top,
474 draw_pos.top() + r_top,
481 cairo_move_to(cr, draw_pos.left() + bdr_left, draw_pos.top() + bdr_top);
482 cairo_line_to(cr, draw_pos.left(), draw_pos.top());
487 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom() - r_bottom);
489 double end_angle = M_PI;
490 double start_angle = end_angle - M_PI / 2.0 / ((double) bdr_bottom / (double) bdr_left + 1);
493 draw_pos.left() + r_bottom,
494 draw_pos.bottom() - r_bottom,
501 draw_pos.left() + r_bottom,
502 draw_pos.bottom() - r_bottom,
504 r_bottom - bdr_left + (bdr_left - bdr_bottom),
509 cairo_line_to(cr, draw_pos.left(), draw_pos.bottom());
510 cairo_line_to(cr, draw_pos.left() + bdr_left, draw_pos.bottom() - bdr_bottom);
518 void container_linux::transform_text(litehtml::tstring& text, litehtml::text_transform tt)
523 void container_linux::set_clip( const litehtml::position& pos, const litehtml::border_radiuses& bdr_radius, bool valid_x, bool valid_y )
525 litehtml::position clip_pos = pos;
526 litehtml::position client_pos;
527 get_client_rect(client_pos);
530 clip_pos.x = client_pos.x;
531 clip_pos.width = client_pos.width;
535 clip_pos.y = client_pos.y;
536 clip_pos.height = client_pos.height;
538 m_clips.emplace_back(clip_pos, bdr_radius);
541 void container_linux::del_clip()
549 void container_linux::apply_clip( cairo_t* cr )
551 for(const auto& clip_box : m_clips)
553 rounded_rectangle(cr, clip_box.box, clip_box.radius);
558 void container_linux::draw_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width )
567 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
568 cairo_scale (cr, width / 2.0, height / 2.0);
569 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
571 set_color(cr, color);
572 cairo_set_line_width(cr, line_width);
578 void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color )
587 cairo_translate (cr, x + width / 2.0, y + height / 2.0);
588 cairo_scale (cr, width / 2.0, height / 2.0);
589 cairo_arc (cr, 0, 0, 1, 0, 2 * M_PI);
591 set_color(cr, color);
597 std::shared_ptr<litehtml::element> container_linux::create_element(const litehtml::tchar_t *tag_name,
598 const litehtml::string_map &attributes,
599 const std::shared_ptr<litehtml::document> &doc)
604 void container_linux::rounded_rectangle( cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius )
607 if(radius.top_left_x)
609 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);
612 cairo_move_to(cr, pos.left(), pos.top());
615 cairo_line_to(cr, pos.right() - radius.top_right_x, pos.top());
617 if(radius.top_right_x)
619 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);
622 cairo_line_to(cr, pos.right(), pos.bottom() - radius.bottom_right_x);
624 if(radius.bottom_right_x)
626 cairo_arc(cr, pos.right() - radius.bottom_right_x, pos.bottom() - radius.bottom_right_x, radius.bottom_right_x, 0, M_PI / 2.0);
629 cairo_line_to(cr, pos.left() - radius.bottom_left_x, pos.bottom());
631 if(radius.bottom_left_x)
633 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);
637 void container_linux::draw_pixbuf(cairo_t* cr, const GdkPixbuf *bmp, int x, int y, int cx, int cy)
642 cairo_matrix_t flib_m;
643 cairo_matrix_init(&flib_m, 1, 0, 0, -1, 0, 0);
645 if(cx != gdk_pixbuf_get_width(bmp) || cy != gdk_pixbuf_get_height(bmp))
647 GdkPixbuf *new_img = gdk_pixbuf_scale_simple(bmp, cx, cy, GDK_INTERP_BILINEAR);
648 gdk_cairo_set_source_pixbuf(cr, new_img, x, y);
652 gdk_cairo_set_source_pixbuf(cr, bmp, x, y);
660 cairo_surface_t* container_linux::surface_from_pixbuf(const GdkPixbuf *bmp)
662 cairo_surface_t* ret = NULL;
664 if(gdk_pixbuf_get_has_alpha(bmp))
666 ret = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
669 ret = cairo_image_surface_create(CAIRO_FORMAT_RGB24, gdk_pixbuf_get_width(bmp), gdk_pixbuf_get_height(bmp));
672 // Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(ret, false));
673 // Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create(surface);
674 // Gdk::Cairo::set_source_pixbuf(ctx, bmp, 0.0, 0.0);
675 cairo_t *ctx = cairo_create(ret);
682 void container_linux::get_media_features(litehtml::media_features& media) const
684 litehtml::position client;
685 get_client_rect(client);
686 media.type = litehtml::media_type_screen;
687 media.width = client.width;
688 media.height = client.height;
689 media.device_width = gdk_screen_width();
690 media.device_height = gdk_screen_height();
692 media.monochrome = 0;
693 media.color_index = 256;
694 media.resolution = 96;
697 void container_linux::get_language(litehtml::tstring& language, litehtml::tstring& culture) const
703 void container_linux::link(const std::shared_ptr<litehtml::document> &ptr, const litehtml::element::ptr& el)