fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[claws.git] / src / plugins / litehtml_viewer / litehtml / document.h
1 #ifndef LH_DOCUMENT_H
2 #define LH_DOCUMENT_H
3
4 #include "style.h"
5 #include "types.h"
6 #include "context.h"
7 #include "gumbo.h"
8
9 namespace litehtml
10 {
11         struct css_text
12         {
13                 typedef std::vector<css_text>   vector;
14
15                 tstring text;
16                 tstring baseurl;
17                 tstring media;
18                 
19                 css_text()
20                 {
21                 }
22
23                 css_text(const tchar_t* txt, const tchar_t* url, const tchar_t* media_str)
24                 {
25                         text    = txt ? txt : _t("");
26                         baseurl = url ? url : _t("");
27                         media   = media_str ? media_str : _t("");
28                 }
29
30                 css_text(const css_text& val)
31                 {
32                         text    = val.text;
33                         baseurl = val.baseurl;
34                         media   = val.media;
35                 }
36         };
37
38         struct stop_tags_t
39         {
40                 const litehtml::tchar_t*        tags;
41                 const litehtml::tchar_t*        stop_parent;
42         };
43
44         struct ommited_end_tags_t
45         {
46                 const litehtml::tchar_t*        tag;
47                 const litehtml::tchar_t*        followed_tags;
48         };
49
50         class html_tag;
51
52         class document : public std::enable_shared_from_this<document>
53         {
54         public:
55                 typedef std::shared_ptr<document>       ptr;
56                 typedef std::weak_ptr<document>         weak_ptr;
57         private:
58                 std::shared_ptr<element>                        m_root;
59                 document_container*                                     m_container;
60                 fonts_map                                                       m_fonts;
61                 css_text::vector                                        m_css;
62                 litehtml::css                                           m_styles;
63                 litehtml::web_color                                     m_def_color;
64                 litehtml::context*                                      m_context;
65                 litehtml::size                                          m_size;
66                 position::vector                                        m_fixed_boxes;
67                 media_query_list::vector                        m_media_lists;
68                 element::ptr                                            m_over_element;
69                 elements_vector                                         m_tabular_elements;
70                 media_features                                          m_media;
71                 tstring                             m_lang;
72                 tstring                             m_culture;
73         public:
74                 document(litehtml::document_container* objContainer, litehtml::context* ctx);
75                 virtual ~document();
76
77                 litehtml::document_container*   container()     { return m_container; }
78                 uint_ptr                                                get_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm);
79                 int                                                             render(int max_width, render_type rt = render_all);
80                 void                                                    draw(uint_ptr hdc, int x, int y, const position* clip);
81                 web_color                                               get_def_color() { return m_def_color; }
82                 int                                                             cvt_units(const tchar_t* str, int fontSize, bool* is_percent = 0) const;
83                 int                                                             cvt_units(css_length& val, int fontSize, int size = 0) const;
84                 int                                                             width() const;
85                 int                                                             height() const;
86                 void                                                    add_stylesheet(const tchar_t* str, const tchar_t* baseurl, const tchar_t* media);
87                 bool                                                    on_mouse_over(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
88                 bool                                                    on_lbutton_down(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
89                 bool                                                    on_lbutton_up(int x, int y, int client_x, int client_y, position::vector& redraw_boxes);
90                 bool                                                    on_mouse_leave(position::vector& redraw_boxes);
91                 litehtml::element::ptr                  create_element(const tchar_t* tag_name, const string_map& attributes);
92                 element::ptr                                    root();
93                 const element::ptr                                      over_element() const;
94                 void                                                    get_fixed_boxes(position::vector& fixed_boxes);
95                 void                                                    add_fixed_box(const position& pos);
96                 void                                                    add_media_list(media_query_list::ptr list);
97                 bool                                                    media_changed();
98                 bool                                                    lang_changed();
99                 bool                            match_lang(const tstring & lang);
100                 void                                                    add_tabular(const element::ptr& el);
101
102                 static litehtml::document::ptr createFromString(const tchar_t* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = 0);
103                 static litehtml::document::ptr createFromUTF8(const char* str, litehtml::document_container* objPainter, litehtml::context* ctx, litehtml::css* user_styles = 0);
104         
105         private:
106                 litehtml::uint_ptr      add_font(const tchar_t* name, int size, const tchar_t* weight, const tchar_t* style, const tchar_t* decoration, font_metrics* fm);
107
108                 void create_node(GumboNode* node, elements_vector& elements);
109                 bool update_media_lists(const media_features& features);
110                 void fix_tables_layout();
111                 void fix_table_children(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str);
112                 void fix_table_parent(element::ptr& el_ptr, style_display disp, const tchar_t* disp_str);
113         };
114
115         inline element::ptr document::root()
116         {
117                 return m_root;
118         }
119         inline const element::ptr document::over_element() const
120         {
121                 return m_over_element;
122         }
123         inline void document::add_tabular(const element::ptr& el)
124         {
125                 m_tabular_elements.push_back(el);
126         }
127         inline bool document::match_lang(const tstring & lang)
128         {
129                 return lang == m_lang || lang == m_culture;
130         }
131 }
132
133 #endif  // LH_DOCUMENT_H