Rev 8491 | Rev 8499 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8330 | leency | 1 | #include "TWB\colors.h" |
2 | #include "TWB\anchors.h" |
||
3 | #include "TWB\parse_tag.h" |
||
4 | #include "TWB\special.h" |
||
5 | #include "TWB\tag_list.h" |
||
8451 | leency | 6 | #define DEFAULT_BG_COL 0xffEBE8E9; |
7759 | leency | 7 | dword link_color_default; |
7757 | leency | 8 | dword link_color_active; |
8330 | leency | 9 | #include "TWB\links.h" |
4411 | leency | 10 | |
7759 | leency | 11 | #define BODY_MARGIN 6 |
8462 | leency | 12 | #define BASIC_CHAR_W 8 |
7759 | leency | 13 | #define BASIC_LINE_H 18 |
4416 | leency | 14 | |
8439 | leency | 15 | struct STYLE { |
7752 | leency | 16 | bool |
6794 | leency | 17 | b, u, s, h, |
8016 | leency | 18 | font, |
5746 | leency | 19 | pre, |
20 | blq, |
||
7742 | leency | 21 | button, |
7759 | leency | 22 | image; |
23 | LIST tag_list; |
||
8439 | leency | 24 | dword title; |
8451 | leency | 25 | dword cur_line_h; |
8439 | leency | 26 | void reset(); |
5746 | leency | 27 | }; |
28 | |||
8439 | leency | 29 | void STYLE::reset() |
30 | { |
||
31 | b = u = s = h = blq = pre = title = false; |
||
32 | font = false; |
||
8451 | leency | 33 | cur_line_h = NULL; |
8439 | leency | 34 | tag_list.reset(); |
35 | } |
||
36 | |||
4411 | leency | 37 | struct TWebBrowser { |
4540 | leency | 38 | llist list; |
8439 | leency | 39 | STYLE style; |
8462 | leency | 40 | dword draw_y, draw_x, draw_w, left_gap; |
7752 | leency | 41 | dword o_bufpointer; |
7759 | leency | 42 | int cur_encoding, custom_encoding; |
43 | bool link, t_html, t_body; |
||
44 | dword bufpointer; |
||
45 | dword bufsize; |
||
7945 | leency | 46 | dword is_html; |
8396 | leency | 47 | collection img_url; |
8439 | leency | 48 | char header[150]; |
8492 | leency | 49 | char linebuf[500]; |
8439 | leency | 50 | char redirect[URL_SIZE]; |
7759 | leency | 51 | |
8462 | leency | 52 | void SetStyle(); |
8491 | leency | 53 | void RenderTextbuf(); |
54 | void RenderLine(); |
||
8462 | leency | 55 | bool RenderImage(); |
56 | |||
7746 | leency | 57 | void SetPageDefaults(); |
7752 | leency | 58 | void ParseHtml(); |
7762 | leency | 59 | void NewLine(); |
60 | void ChangeEncoding(); |
||
8491 | leency | 61 | void AddCharToTheLine(); |
4411 | leency | 62 | void DrawPage(); |
8439 | leency | 63 | |
64 | void tag_a(); |
||
65 | void tag_p(); |
||
66 | void tag_img(); |
||
67 | void tag_div(); |
||
68 | void tag_h1234_caption(); |
||
69 | void tag_ol_ul_dt(); |
||
70 | void tag_li(); |
||
71 | void tag_q(); |
||
72 | void tag_hr(); |
||
73 | void tag_code(); |
||
74 | void tag_meta_xml(); |
||
75 | void tag_body(); |
||
76 | void tag_iframe(); |
||
77 | void tag_title(); |
||
78 | void tag_font(); |
||
8492 | leency | 79 | void tag_table_reset(); |
8462 | leency | 80 | void tag_table(); |
81 | void tag_td(); |
||
82 | void tag_tr(); |
||
7759 | leency | 83 | }; |
4486 | leency | 84 | |
8462 | leency | 85 | #include "TWB\render.h" |
8439 | leency | 86 | #include "TWB\set_style.h" |
5747 | leency | 87 | //============================================================================================ |
7746 | leency | 88 | void TWebBrowser::SetPageDefaults() |
89 | { |
||
8439 | leency | 90 | t_html = t_body = link = false; |
91 | style.reset(); |
||
7759 | leency | 92 | link_color_default = 0x0000FF; |
4491 | leency | 93 | link_color_active = 0xFF0000; |
8454 | leency | 94 | style.cur_line_h = list.item_h; |
7970 | leency | 95 | links.clear(); |
7752 | leency | 96 | anchors.clear(); |
8396 | leency | 97 | img_url.drop(); |
8016 | leency | 98 | text_colors.drop(); |
99 | text_colors.add(0); |
||
8451 | leency | 100 | bg_colors.drop(); |
101 | bg_colors.add(DEFAULT_BG_COL); |
||
102 | canvas.Fill(0, DEFAULT_BG_COL); |
||
7758 | leency | 103 | header = NULL; |
7759 | leency | 104 | draw_y = BODY_MARGIN; |
8462 | leency | 105 | draw_x = left_gap = BODY_MARGIN; |
106 | draw_w = list.w - BODY_MARGIN; |
||
8492 | leency | 107 | linebuf = 0; |
8302 | leency | 108 | redirect = '\0'; |
8490 | leency | 109 | list.SetFont(8, 14, 10011000b); |
8492 | leency | 110 | tag_table_reset(); |
7746 | leency | 111 | } |
112 | //============================================================================================ |
||
7759 | leency | 113 | void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){ |
7752 | leency | 114 | int tab_len; |
7746 | leency | 115 | dword bufpos; |
7759 | leency | 116 | bufsize = _bufsize; |
8439 | leency | 117 | |
8457 | leency | 118 | if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20); |
119 | |||
8492 | leency | 120 | if (bufpointer == _bufpointer) { |
121 | custom_encoding = cur_encoding; |
||
122 | } else { |
||
7771 | leency | 123 | bufpointer = malloc(bufsize); |
124 | memmov(bufpointer, _bufpointer, bufsize); |
||
8492 | leency | 125 | |
126 | //hold original buffer |
||
127 | o_bufpointer = malloc(bufsize); |
||
128 | memmov(o_bufpointer, bufpointer, bufsize); |
||
129 | |||
130 | cur_encoding = CH_CP866; |
||
131 | if (custom_encoding != -1) { |
||
132 | cur_encoding = custom_encoding; |
||
133 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
||
134 | bufsize = strlen(bufpointer); |
||
135 | } |
||
7771 | leency | 136 | } |
8492 | leency | 137 | |
138 | |||
7746 | leency | 139 | SetPageDefaults(); |
7945 | leency | 140 | is_html = true; |
141 | if (!strstri(bufpointer, " |
||
7749 | leency | 142 | t_body = true; |
7991 | leency | 143 | if (!strstri(bufpointer, " |
7945 | leency | 144 | style.pre = true; //show linebreaks for a plaint text |
145 | is_html = false; |
||
146 | } |
||
7749 | leency | 147 | } |
5773 | leency | 148 | for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;) |
4411 | leency | 149 | { |
8330 | leency | 150 | switch (ESBYTE[bufpos]) |
4411 | leency | 151 | { |
152 | case 0x0a: |
||
7752 | leency | 153 | if (style.pre) { |
8491 | leency | 154 | RenderTextbuf(); |
5781 | leency | 155 | NewLine(); |
7752 | leency | 156 | } else { |
8491 | leency | 157 | goto _DEFAULT; |
4411 | leency | 158 | } |
7752 | leency | 159 | break; |
160 | case 0x09: |
||
161 | if (style.pre) { |
||
8492 | leency | 162 | tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4; |
7752 | leency | 163 | if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len; |
8492 | leency | 164 | while (tab_len) {chrcat(#linebuf,' '); tab_len--;} |
7752 | leency | 165 | } else { |
8491 | leency | 166 | goto _DEFAULT; |
4411 | leency | 167 | } |
7752 | leency | 168 | break; |
4411 | leency | 169 | case '&': // and so on |
8492 | leency | 170 | bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize); |
4411 | leency | 171 | break; |
172 | case '<': |
||
8330 | leency | 173 | if (!is_html) goto _DEFAULT; |
8490 | leency | 174 | if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT; |
5749 | leency | 175 | bufpos++; |
8440 | leency | 176 | if (tag.parse(#bufpos, bufpointer + bufsize)) { |
8491 | leency | 177 | RenderTextbuf(); |
8439 | leency | 178 | $push cur_encoding |
179 | SetStyle(); |
||
180 | $pop eax |
||
181 | // The thing is that UTF if longer than other encodings. |
||
182 | // So if encoding was changed from UTF to DOS than $bufpos position got wrong, |
||
183 | // and we have to start parse from the very beginning |
||
184 | if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) { |
||
8492 | leency | 185 | ParseHtml(bufpointer, strlen(bufpointer)); |
8439 | leency | 186 | return; |
8407 | leency | 187 | } |
7746 | leency | 188 | } |
4411 | leency | 189 | break; |
190 | default: |
||
8330 | leency | 191 | _DEFAULT: |
7752 | leency | 192 | AddCharToTheLine(ESBYTE[bufpos]); |
4411 | leency | 193 | } |
194 | } |
||
8491 | leency | 195 | RenderTextbuf(); |
196 | list.count = draw_y + style.cur_line_h; |
||
8425 | leency | 197 | |
8491 | leency | 198 | canvas.bufh = math.max(list.visible, list.count); |
8439 | leency | 199 | buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8); |
8425 | leency | 200 | |
7755 | leency | 201 | list.CheckDoesValuesOkey(); |
202 | anchors.current = NULL; |
||
7758 | leency | 203 | if (!header) { |
7759 | leency | 204 | strncpy(#header, #version, sizeof(TWebBrowser.header)-1); |
7758 | leency | 205 | DrawTitle(#header); |
206 | } |
||
4411 | leency | 207 | } |
5747 | leency | 208 | //============================================================================================ |
8444 | leency | 209 | void TWebBrowser::AddCharToTheLine(unsigned char _char) |
210 | { |
||
8492 | leency | 211 | dword line_len = strlen(#linebuf); |
8444 | leency | 212 | if (_char<=15) _char=' '; |
213 | if (!style.pre) && (_char == ' ') |
||
214 | { |
||
8492 | leency | 215 | if (linebuf[line_len-1]==' ') return; //no double spaces |
216 | if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line |
||
8444 | leency | 217 | if (link) && (line_len==0) return; |
218 | } |
||
8492 | leency | 219 | if (line_len < sizeof(TWebBrowser.linebuf)) chrcat(#linebuf+line_len, _char); |
8491 | leency | 220 | if (line_len+1 * list.font_w + draw_x >= draw_w) RenderTextbuf(); |
8444 | leency | 221 | } |
222 | //============================================================================================ |
||
4692 | leency | 223 | void TWebBrowser::NewLine() |
4475 | leency | 224 | { |
8457 | leency | 225 | static bool empty_line = true; |
4692 | leency | 226 | |
8462 | leency | 227 | if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return; |
8457 | leency | 228 | if (t_html) && (!t_body) return; |
7742 | leency | 229 | |
8462 | leency | 230 | if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) { |
8457 | leency | 231 | if (!empty_line) empty_line=true; else return; |
7742 | leency | 232 | } else { |
8457 | leency | 233 | empty_line = false; |
7742 | leency | 234 | } |
6794 | leency | 235 | |
8451 | leency | 236 | draw_y += style.cur_line_h; |
237 | style.cur_line_h = list.item_h; |
||
8457 | leency | 238 | if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0; |
8462 | leency | 239 | draw_x += style.tag_list.level * 5 * list.font_w + left_gap; |
4488 | leency | 240 | } |
5747 | leency | 241 | //============================================================================================ |
8444 | leency | 242 | void TWebBrowser::ChangeEncoding(int _new_encoding) |
243 | { |
||
244 | if (cur_encoding == _new_encoding) return; |
||
245 | cur_encoding = _new_encoding; |
||
246 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
||
247 | if (header) { |
||
248 | ChangeCharset(cur_encoding, "CP866", #header); |
||
249 | DrawTitle(#header); |
||
250 | } |
||
251 | } |
||
252 | //============================================================================================ |
||
8491 | leency | 253 | scroll_bar scroll_wv = { 15,NULL,NULL,NULL, |
254 | 0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee}; |
||
8439 | leency | 255 | |
5766 | leency | 256 | void TWebBrowser::DrawPage() |
257 | { |
||
8457 | leency | 258 | if (list.w!=canvas.bufw) { |
259 | ParseHtml(bufpointer, bufsize); |
||
260 | } |
||
261 | canvas.Show(list.first, list.h); |
||
262 | |||
8439 | leency | 263 | scroll_wv.max_area = list.count; |
264 | scroll_wv.cur_area = list.visible; |
||
265 | scroll_wv.position = list.first; |
||
266 | scroll_wv.all_redraw = 0; |
||
267 | scroll_wv.start_x = list.x + list.w; |
||
268 | scroll_wv.start_y = list.y; |
||
269 | scroll_wv.size_y = list.h; |
||
270 | |||
8490 | leency | 271 | if (list.count <= list.visible) { |
272 | DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, |
||
273 | scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF); |
||
274 | } else { |
||
275 | scrollbar_v_draw(#scroll_wv); |
||
276 | } |
||
5766 | leency | 277 | }=>>=15)>': |