Rev 8444 | Rev 8454 | 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 |
12 | #define BASIC_LINE_H 18 |
||
4416 | leency | 13 | |
8439 | leency | 14 | CANVAS canvas; |
7771 | leency | 15 | char line[500]; |
16 | |||
8439 | leency | 17 | struct STYLE { |
7752 | leency | 18 | bool |
6794 | leency | 19 | b, u, s, h, |
8016 | leency | 20 | font, |
5746 | leency | 21 | pre, |
22 | blq, |
||
7742 | leency | 23 | button, |
7759 | leency | 24 | image; |
25 | LIST tag_list; |
||
8439 | leency | 26 | dword title; |
8451 | leency | 27 | dword cur_line_h; |
8439 | leency | 28 | void reset(); |
5746 | leency | 29 | }; |
30 | |||
8439 | leency | 31 | void STYLE::reset() |
32 | { |
||
33 | b = u = s = h = blq = pre = title = false; |
||
34 | font = false; |
||
8451 | leency | 35 | cur_line_h = NULL; |
8439 | leency | 36 | tag_list.reset(); |
37 | } |
||
38 | |||
4411 | leency | 39 | struct TWebBrowser { |
4540 | leency | 40 | llist list; |
8439 | leency | 41 | STYLE style; |
7752 | leency | 42 | dword draw_y, stolbec; |
6803 | leency | 43 | int zoom; |
7752 | leency | 44 | dword o_bufpointer; |
7759 | leency | 45 | int cur_encoding, custom_encoding; |
46 | bool link, t_html, t_body; |
||
47 | dword bufpointer; |
||
48 | dword bufsize; |
||
7945 | leency | 49 | dword is_html; |
8396 | leency | 50 | collection img_url; |
8439 | leency | 51 | char header[150]; |
52 | char redirect[URL_SIZE]; |
||
7759 | leency | 53 | |
7771 | leency | 54 | void Paint(); |
7746 | leency | 55 | void SetPageDefaults(); |
7752 | leency | 56 | void AddCharToTheLine(); |
57 | void ParseHtml(); |
||
5766 | leency | 58 | void SetStyle(); |
7762 | leency | 59 | bool CheckForLineBreak(); |
60 | void NewLine(); |
||
61 | void ChangeEncoding(); |
||
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(); |
||
7759 | leency | 79 | }; |
4486 | leency | 80 | |
8439 | leency | 81 | #include "TWB\set_style.h" |
4411 | leency | 82 | |
5747 | leency | 83 | //============================================================================================ |
7771 | leency | 84 | void TWebBrowser::Paint() |
4411 | leency | 85 | { |
6803 | leency | 86 | dword start_x, line_length, stolbec_len; |
7762 | leency | 87 | dword text_color__; |
4411 | leency | 88 | |
8439 | leency | 89 | if (style.title) |
4411 | leency | 90 | { |
7759 | leency | 91 | strncpy(#header, #line, sizeof(TWebBrowser.header)-1); |
92 | strncat(#header, " - ", sizeof(TWebBrowser.header)-1); |
||
93 | strncat(#header, #version, sizeof(TWebBrowser.header)-1); |
||
4411 | leency | 94 | line = 0; |
95 | return; |
||
96 | } |
||
7758 | leency | 97 | if (t_html) && (!t_body) { |
98 | line = 0; |
||
99 | return; |
||
100 | } |
||
4411 | leency | 101 | |
6803 | leency | 102 | if (line) |
4411 | leency | 103 | { |
7759 | leency | 104 | start_x = stolbec * list.font_w + BODY_MARGIN + list.x; |
6803 | leency | 105 | stolbec_len = strlen(#line) * zoom; |
5768 | leency | 106 | line_length = stolbec_len * list.font_w; |
8451 | leency | 107 | style.cur_line_h = math.max(style.cur_line_h, list.item_h); |
4411 | leency | 108 | |
8451 | leency | 109 | if (bg_colors.get_last() - bg_colors.get(0)) { |
110 | canvas.DrawBar(start_x, draw_y, line_length, list.item_h, bg_colors.get_last()); |
||
7743 | leency | 111 | } |
112 | |||
7742 | leency | 113 | if (style.image) { |
8439 | leency | 114 | canvas.DrawBar(start_x, draw_y, line_length, list.item_h-1, 0xF9DBCB); |
7742 | leency | 115 | } |
116 | if (style.button) { |
||
8439 | leency | 117 | canvas.DrawBar(start_x, draw_y, line_length, list.item_h - calc(zoom*2), 0xCCCccc); |
118 | canvas.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, 0x999999); |
||
7742 | leency | 119 | } |
120 | |||
8016 | leency | 121 | text_color__ = text_colors.get_last(); |
122 | if (link) && (text_color__ == text_colors.get(0)) text_color__ = link_color_default; |
||
7762 | leency | 123 | |
8439 | leency | 124 | canvas.WriteText(start_x, draw_y, list.font_type, text_color__, #line, NULL); |
125 | if (style.b) canvas.WriteText(start_x+1, draw_y, list.font_type, text_color__, #line, NULL); |
||
126 | if (style.s) canvas.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_color__); |
||
127 | if (style.u) canvas.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_color__); |
||
5519 | leency | 128 | if (link) { |
7747 | leency | 129 | if (line[0]==' ') && (line[1]==NULL) {} else { |
8439 | leency | 130 | canvas.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2)-1, line_length, zoom, link_color_default); |
7970 | leency | 131 | links.add_text(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2)-1, zoom); |
7746 | leency | 132 | } |
4411 | leency | 133 | } |
4718 | leency | 134 | stolbec += stolbec_len; |
8439 | leency | 135 | if (debug_mode) debugln(#line); |
7750 | leency | 136 | line = NULL; |
4411 | leency | 137 | } |
138 | } |
||
5747 | leency | 139 | //============================================================================================ |
7746 | leency | 140 | void TWebBrowser::SetPageDefaults() |
141 | { |
||
8439 | leency | 142 | t_html = t_body = link = false; |
143 | style.reset(); |
||
7759 | leency | 144 | link_color_default = 0x0000FF; |
4491 | leency | 145 | link_color_active = 0xFF0000; |
8451 | leency | 146 | //style.cur_line_h = list.item_h; |
7970 | leency | 147 | links.clear(); |
7752 | leency | 148 | anchors.clear(); |
8396 | leency | 149 | img_url.drop(); |
8016 | leency | 150 | text_colors.drop(); |
151 | text_colors.add(0); |
||
8451 | leency | 152 | bg_colors.drop(); |
153 | bg_colors.add(DEFAULT_BG_COL); |
||
154 | canvas.Fill(0, DEFAULT_BG_COL); |
||
7758 | leency | 155 | header = NULL; |
7759 | leency | 156 | cur_encoding = CH_CP866; |
157 | draw_y = BODY_MARGIN; |
||
4411 | leency | 158 | stolbec = 0; |
159 | line = 0; |
||
7752 | leency | 160 | zoom = 1; |
8302 | leency | 161 | redirect = '\0'; |
7757 | leency | 162 | //hold original buffer |
163 | if (o_bufpointer) o_bufpointer=free(o_bufpointer); |
||
164 | o_bufpointer = malloc(bufsize); |
||
165 | memmov(o_bufpointer, bufpointer, bufsize); |
||
7759 | leency | 166 | if (custom_encoding != -1) { |
167 | cur_encoding = custom_encoding; |
||
7781 | leency | 168 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
7759 | leency | 169 | } |
7746 | leency | 170 | } |
171 | //============================================================================================ |
||
7759 | leency | 172 | void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){ |
7752 | leency | 173 | char unicode_symbol[10]; |
7746 | leency | 174 | dword j; |
7752 | leency | 175 | int tab_len; |
7746 | leency | 176 | dword bufpos; |
7759 | leency | 177 | bufsize = _bufsize; |
8439 | leency | 178 | |
7771 | leency | 179 | if (bufpointer != _bufpointer) { |
180 | bufpointer = malloc(bufsize); |
||
181 | memmov(bufpointer, _bufpointer, bufsize); |
||
182 | } else { |
||
183 | custom_encoding = CH_CP866; |
||
184 | } |
||
7746 | leency | 185 | SetPageDefaults(); |
7945 | leency | 186 | is_html = true; |
187 | if (!strstri(bufpointer, " |
||
7749 | leency | 188 | t_body = true; |
7991 | leency | 189 | if (!strstri(bufpointer, " |
7945 | leency | 190 | style.pre = true; //show linebreaks for a plaint text |
191 | is_html = false; |
||
192 | } |
||
7749 | leency | 193 | } |
5773 | leency | 194 | for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;) |
4411 | leency | 195 | { |
8330 | leency | 196 | switch (ESBYTE[bufpos]) |
4411 | leency | 197 | { |
198 | case 0x0a: |
||
7752 | leency | 199 | if (style.pre) { |
7771 | leency | 200 | Paint(); |
5781 | leency | 201 | NewLine(); |
7752 | leency | 202 | } else { |
203 | AddCharToTheLine(0x0a); |
||
4411 | leency | 204 | } |
7752 | leency | 205 | break; |
206 | case 0x09: |
||
207 | if (style.pre) { |
||
208 | tab_len = strlen(#line) + stolbec % 4; |
||
209 | if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len; |
||
4411 | leency | 210 | for (j=0; j |
7752 | leency | 211 | } else { |
212 | AddCharToTheLine(0x09); |
||
4411 | leency | 213 | } |
7752 | leency | 214 | break; |
4411 | leency | 215 | case '&': // and so on |
8319 | leency | 216 | for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++) |
4411 | leency | 217 | { |
8410 | leency | 218 | chrcat(#unicode_symbol, ESBYTE[bufpos+j]); |
4411 | leency | 219 | } |
7771 | leency | 220 | if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) { |
7749 | leency | 221 | bufpos += j; |
7750 | leency | 222 | CheckForLineBreak(); |
7749 | leency | 223 | } else { |
7752 | leency | 224 | AddCharToTheLine('&'); |
7749 | leency | 225 | } |
4411 | leency | 226 | break; |
227 | case '<': |
||
8330 | leency | 228 | if (!is_html) goto _DEFAULT; |
5749 | leency | 229 | bufpos++; |
8330 | leency | 230 | switch (ESBYTE[bufpos]) { |
8439 | leency | 231 | case '!': case '/': case '?': |
232 | case 'a'...'z': case 'A'...'Z': |
||
8330 | leency | 233 | goto _TAG; |
234 | default: |
||
235 | goto _DEFAULT; |
||
236 | } |
||
237 | _TAG: |
||
8440 | leency | 238 | if (tag.parse(#bufpos, bufpointer + bufsize)) { |
7750 | leency | 239 | CheckForLineBreak(); |
7771 | leency | 240 | Paint(); |
8439 | leency | 241 | $push cur_encoding |
242 | SetStyle(); |
||
243 | $pop eax |
||
244 | // The thing is that UTF if longer than other encodings. |
||
245 | // So if encoding was changed from UTF to DOS than $bufpos position got wrong, |
||
246 | // and we have to start parse from the very beginning |
||
247 | if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) { |
||
248 | ParseHtml(bufpointer, bufsize); |
||
249 | return; |
||
8407 | leency | 250 | } |
7746 | leency | 251 | } |
4411 | leency | 252 | break; |
253 | default: |
||
8330 | leency | 254 | _DEFAULT: |
7752 | leency | 255 | AddCharToTheLine(ESBYTE[bufpos]); |
4411 | leency | 256 | } |
257 | } |
||
7771 | leency | 258 | Paint(); |
5766 | leency | 259 | NewLine(); |
7752 | leency | 260 | list.count = draw_y; |
8425 | leency | 261 | |
8439 | leency | 262 | canvas.bufh = math.max(list.visible, draw_y); |
263 | buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8); |
||
8425 | leency | 264 | |
7755 | leency | 265 | list.CheckDoesValuesOkey(); |
266 | anchors.current = NULL; |
||
7759 | leency | 267 | custom_encoding = -1; |
7758 | leency | 268 | if (!header) { |
7759 | leency | 269 | strncpy(#header, #version, sizeof(TWebBrowser.header)-1); |
7758 | leency | 270 | DrawTitle(#header); |
271 | } |
||
4411 | leency | 272 | } |
5747 | leency | 273 | //============================================================================================ |
8444 | leency | 274 | void TWebBrowser::AddCharToTheLine(unsigned char _char) |
275 | { |
||
276 | dword line_len; |
||
277 | if (_char<=15) _char=' '; |
||
278 | line_len = strlen(#line); |
||
279 | if (!style.pre) && (_char == ' ') |
||
280 | { |
||
281 | if (line[line_len-1]==' ') return; //no double spaces |
||
282 | if (!stolbec) && (!line) return; //no paces at the beginning of the line |
||
283 | if (link) && (line_len==0) return; |
||
284 | } |
||
285 | if (line_len < sizeof(line)) chrcat(#line, _char); |
||
286 | CheckForLineBreak(); |
||
287 | } |
||
288 | //============================================================================================ |
||
7750 | leency | 289 | bool TWebBrowser::CheckForLineBreak() |
4718 | leency | 290 | { |
7750 | leency | 291 | int line_break_pos; |
4718 | leency | 292 | char new_line_text[4096]; |
7758 | leency | 293 | //Do we need a line break? |
7750 | leency | 294 | if (strlen(#line)*zoom + stolbec < list.column_max) return false; |
7758 | leency | 295 | //Yes, we do. Lets calculate where... |
7750 | leency | 296 | line_break_pos = strrchr(#line, ' '); |
7758 | leency | 297 | //Is a new line fits in the current line? |
7750 | leency | 298 | if (line_break_pos*zoom + stolbec > list.column_max) { |
299 | line_break_pos = list.column_max/zoom - stolbec; |
||
300 | while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--; |
||
301 | } |
||
7758 | leency | 302 | //Maybe a new line is too big for the whole new line? Then we have to split it |
7759 | leency | 303 | if (!line_break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) { |
7758 | leency | 304 | line_break_pos = list.column_max/zoom - stolbec; |
7742 | leency | 305 | } |
7750 | leency | 306 | strcpy(#new_line_text, #line + line_break_pos); |
7758 | leency | 307 | line[line_break_pos] = 0x00; |
308 | |||
7771 | leency | 309 | Paint(); |
7758 | leency | 310 | |
4718 | leency | 311 | strcpy(#line, #new_line_text); |
312 | NewLine(); |
||
7750 | leency | 313 | return true; |
4718 | leency | 314 | } |
5747 | leency | 315 | //============================================================================================ |
4692 | leency | 316 | void TWebBrowser::NewLine() |
4475 | leency | 317 | { |
7742 | leency | 318 | static int empty_line=0; |
4692 | leency | 319 | |
7759 | leency | 320 | if (!stolbec) && (draw_y==BODY_MARGIN) return; |
7742 | leency | 321 | |
7759 | leency | 322 | if (style.tag_list.level) && (stolbec == style.tag_list.level * 5) { |
7742 | leency | 323 | if (empty_line<1) empty_line++; |
324 | else return; |
||
7749 | leency | 325 | } else if (!stolbec) { |
326 | if (empty_line<1) empty_line++; |
||
327 | else return; |
||
7742 | leency | 328 | } else { |
329 | empty_line=0; |
||
330 | } |
||
6794 | leency | 331 | |
4637 | leency | 332 | if (t_html) && (!t_body) return; |
8451 | leency | 333 | draw_y += style.cur_line_h; |
334 | style.cur_line_h = list.item_h; |
||
5746 | leency | 335 | if (style.blq) stolbec = 6; else stolbec = 0; |
7759 | leency | 336 | stolbec += style.tag_list.level * 5; |
4488 | leency | 337 | } |
5747 | leency | 338 | //============================================================================================ |
8444 | leency | 339 | void TWebBrowser::ChangeEncoding(int _new_encoding) |
340 | { |
||
341 | if (cur_encoding == _new_encoding) return; |
||
342 | cur_encoding = _new_encoding; |
||
343 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
||
344 | if (header) { |
||
345 | ChangeCharset(cur_encoding, "CP866", #header); |
||
346 | DrawTitle(#header); |
||
347 | } |
||
348 | } |
||
349 | //============================================================================================ |
||
8439 | leency | 350 | scroll_bar scroll_wv = |
351 | { 15,NULL,NULL,NULL,0,2,NULL, |
||
352 | 0,0,0xeeeeee,0xBBBbbb,0xeeeeee}; |
||
353 | |||
5766 | leency | 354 | void TWebBrowser::DrawPage() |
355 | { |
||
8439 | leency | 356 | scroll_wv.max_area = list.count; |
357 | scroll_wv.cur_area = list.visible; |
||
358 | scroll_wv.position = list.first; |
||
359 | scroll_wv.all_redraw = 0; |
||
360 | scroll_wv.start_x = list.x + list.w; |
||
361 | scroll_wv.start_y = list.y; |
||
362 | scroll_wv.size_y = list.h; |
||
363 | scrollbar_v_draw(#scroll_wv); |
||
364 | |||
365 | canvas.Show(list.first, list.h); |
||
5766 | leency | 366 | }1)>1)>>>=15)>': |