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