2020-12-06 00:22:38 +01:00
|
|
|
#include "TWB\colors.h"
|
|
|
|
#include "TWB\anchors.h"
|
|
|
|
#include "TWB\parse_tag.h"
|
|
|
|
#include "TWB\special.h"
|
|
|
|
#include "TWB\tag_list.h"
|
2022-01-09 11:00:41 +01:00
|
|
|
#define DEFAULT_BG_COL 0xffFBE5E4; //- peach; 0xffEBE8E9 - more grey
|
2020-04-04 01:17:28 +02:00
|
|
|
dword link_color_default;
|
2020-03-29 12:57:14 +02:00
|
|
|
dword link_color_active;
|
2020-12-06 00:22:38 +01:00
|
|
|
#include "TWB\links.h"
|
2013-12-27 13:30:42 +01:00
|
|
|
|
2020-04-04 01:17:28 +02:00
|
|
|
#define BODY_MARGIN 6
|
2020-12-21 02:59:34 +01:00
|
|
|
#define BASIC_CHAR_W 8
|
2020-04-04 01:17:28 +02:00
|
|
|
#define BASIC_LINE_H 18
|
2015-08-19 14:48:31 +02:00
|
|
|
|
2020-12-18 00:01:09 +01:00
|
|
|
struct STYLE {
|
2020-03-26 01:12:32 +01:00
|
|
|
bool
|
2016-12-11 23:58:11 +01:00
|
|
|
b, u, s, h,
|
2020-06-02 00:18:26 +02:00
|
|
|
font,
|
2015-08-19 14:48:31 +02:00
|
|
|
pre,
|
|
|
|
blq,
|
2020-03-21 15:33:54 +01:00
|
|
|
button,
|
2021-11-22 19:30:51 +01:00
|
|
|
image,
|
|
|
|
nav, header;
|
2020-04-04 01:17:28 +02:00
|
|
|
LIST tag_list;
|
2020-12-18 00:01:09 +01:00
|
|
|
dword title;
|
2020-12-19 01:50:55 +01:00
|
|
|
dword cur_line_h;
|
2020-12-18 00:01:09 +01:00
|
|
|
void reset();
|
2015-08-19 14:48:31 +02:00
|
|
|
};
|
2013-12-27 13:30:42 +01:00
|
|
|
|
2020-12-18 00:01:09 +01:00
|
|
|
void STYLE::reset()
|
|
|
|
{
|
2021-11-22 19:30:51 +01:00
|
|
|
b = u = s = h = font = pre = blq =
|
|
|
|
title = button = image = nav = header = false;
|
2020-12-19 01:50:55 +01:00
|
|
|
cur_line_h = NULL;
|
2020-12-18 00:01:09 +01:00
|
|
|
tag_list.reset();
|
|
|
|
}
|
|
|
|
|
2013-12-27 00:36:17 +01:00
|
|
|
struct TWebBrowser {
|
2014-01-27 21:16:33 +01:00
|
|
|
llist list;
|
2020-12-18 00:01:09 +01:00
|
|
|
STYLE style;
|
2020-12-21 02:59:34 +01:00
|
|
|
dword draw_y, draw_x, draw_w, left_gap;
|
2020-03-26 01:12:32 +01:00
|
|
|
dword o_bufpointer;
|
2020-04-04 01:17:28 +02:00
|
|
|
int cur_encoding, custom_encoding;
|
|
|
|
bool link, t_html, t_body;
|
|
|
|
dword bufpointer;
|
|
|
|
dword bufsize;
|
2020-05-18 12:03:45 +02:00
|
|
|
dword is_html;
|
2020-12-13 21:52:56 +01:00
|
|
|
collection img_url;
|
2020-12-18 00:01:09 +01:00
|
|
|
char header[150];
|
2020-12-29 11:31:45 +01:00
|
|
|
char linebuf[500];
|
2020-12-18 00:01:09 +01:00
|
|
|
char redirect[URL_SIZE];
|
2020-04-04 01:17:28 +02:00
|
|
|
|
2021-08-02 16:08:12 +02:00
|
|
|
bool secondrun;
|
|
|
|
|
2020-12-21 02:59:34 +01:00
|
|
|
void SetStyle();
|
2020-12-28 22:43:46 +01:00
|
|
|
void RenderTextbuf();
|
|
|
|
void RenderLine();
|
2020-12-21 02:59:34 +01:00
|
|
|
bool RenderImage();
|
|
|
|
|
2020-03-22 20:50:16 +01:00
|
|
|
void SetPageDefaults();
|
2020-03-26 01:12:32 +01:00
|
|
|
void ParseHtml();
|
2021-01-02 13:45:42 +01:00
|
|
|
void Reparse();
|
2020-04-04 21:13:03 +02:00
|
|
|
void NewLine();
|
|
|
|
void ChangeEncoding();
|
2020-12-28 22:43:46 +01:00
|
|
|
void AddCharToTheLine();
|
2020-04-04 21:13:03 +02:00
|
|
|
void DrawPage();
|
2020-12-18 00:01:09 +01:00
|
|
|
|
|
|
|
void tag_a();
|
|
|
|
void tag_p();
|
|
|
|
void tag_img();
|
|
|
|
void tag_div();
|
|
|
|
void tag_h1234_caption();
|
|
|
|
void tag_ol_ul_dt();
|
|
|
|
void tag_li();
|
|
|
|
void tag_q();
|
|
|
|
void tag_hr();
|
|
|
|
void tag_code();
|
|
|
|
void tag_meta_xml();
|
|
|
|
void tag_body();
|
|
|
|
void tag_iframe();
|
|
|
|
void tag_title();
|
|
|
|
void tag_font();
|
2020-12-29 11:31:45 +01:00
|
|
|
void tag_table_reset();
|
2020-12-21 02:59:34 +01:00
|
|
|
void tag_table();
|
|
|
|
void tag_td();
|
|
|
|
void tag_tr();
|
2020-04-04 01:17:28 +02:00
|
|
|
};
|
2014-03-12 23:56:28 +01:00
|
|
|
|
2020-12-21 02:59:34 +01:00
|
|
|
#include "TWB\render.h"
|
2020-12-18 00:01:09 +01:00
|
|
|
#include "TWB\set_style.h"
|
2015-08-19 14:54:01 +02:00
|
|
|
//============================================================================================
|
2020-03-22 20:50:16 +01:00
|
|
|
void TWebBrowser::SetPageDefaults()
|
|
|
|
{
|
2020-12-18 00:01:09 +01:00
|
|
|
t_html = t_body = link = false;
|
|
|
|
style.reset();
|
2020-04-04 01:17:28 +02:00
|
|
|
link_color_default = 0x0000FF;
|
2014-01-19 23:46:58 +01:00
|
|
|
link_color_active = 0xFF0000;
|
2020-12-19 23:43:46 +01:00
|
|
|
style.cur_line_h = list.item_h;
|
2020-05-22 23:39:04 +02:00
|
|
|
links.clear();
|
2020-03-26 01:12:32 +01:00
|
|
|
anchors.clear();
|
2020-12-13 21:52:56 +01:00
|
|
|
img_url.drop();
|
2020-06-02 00:18:26 +02:00
|
|
|
text_colors.drop();
|
|
|
|
text_colors.add(0);
|
2020-12-19 01:50:55 +01:00
|
|
|
bg_colors.drop();
|
|
|
|
bg_colors.add(DEFAULT_BG_COL);
|
|
|
|
canvas.Fill(0, DEFAULT_BG_COL);
|
2020-03-30 23:29:52 +02:00
|
|
|
header = NULL;
|
2020-04-04 01:17:28 +02:00
|
|
|
draw_y = BODY_MARGIN;
|
2020-12-21 02:59:34 +01:00
|
|
|
draw_x = left_gap = BODY_MARGIN;
|
|
|
|
draw_w = list.w - BODY_MARGIN;
|
2020-12-29 11:31:45 +01:00
|
|
|
linebuf = 0;
|
2020-12-02 15:21:06 +01:00
|
|
|
redirect = '\0';
|
2020-12-28 20:20:59 +01:00
|
|
|
list.SetFont(8, 14, 10011000b);
|
2020-12-29 11:31:45 +01:00
|
|
|
tag_table_reset();
|
2021-08-02 16:08:12 +02:00
|
|
|
is_html = true;
|
|
|
|
if (!strstri(bufpointer, "<body")) {
|
|
|
|
t_body = true;
|
2021-12-15 21:52:45 +01:00
|
|
|
if (!strstri(bufpointer, "<html")) && (!strstri(bufpointer, "<head")) && (!strstri(bufpointer, "<title"))
|
2021-08-02 16:08:12 +02:00
|
|
|
&& (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
|
|
|
|
style.pre = true; //show linebreaks for a plaint text
|
|
|
|
is_html = false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 20:50:16 +01:00
|
|
|
}
|
|
|
|
//============================================================================================
|
2021-01-02 13:45:42 +01:00
|
|
|
void TWebBrowser::Reparse()
|
|
|
|
{
|
|
|
|
ParseHtml(bufpointer, bufsize);
|
|
|
|
}
|
|
|
|
//============================================================================================
|
2020-04-04 01:17:28 +02:00
|
|
|
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
|
2020-03-26 01:12:32 +01:00
|
|
|
int tab_len;
|
2020-03-22 20:50:16 +01:00
|
|
|
dword bufpos;
|
2020-04-04 01:17:28 +02:00
|
|
|
bufsize = _bufsize;
|
2020-12-18 00:01:09 +01:00
|
|
|
|
2020-12-20 14:28:50 +01:00
|
|
|
if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
|
|
|
|
|
2020-12-29 11:31:45 +01:00
|
|
|
if (bufpointer == _bufpointer) {
|
|
|
|
custom_encoding = cur_encoding;
|
|
|
|
} else {
|
2020-04-10 18:19:52 +02:00
|
|
|
bufpointer = malloc(bufsize);
|
|
|
|
memmov(bufpointer, _bufpointer, bufsize);
|
2020-12-29 11:31:45 +01:00
|
|
|
|
|
|
|
//hold original buffer
|
|
|
|
o_bufpointer = malloc(bufsize);
|
|
|
|
memmov(o_bufpointer, bufpointer, bufsize);
|
|
|
|
|
|
|
|
cur_encoding = CH_CP866;
|
|
|
|
if (custom_encoding != -1) {
|
|
|
|
cur_encoding = custom_encoding;
|
2021-11-24 11:52:23 +01:00
|
|
|
bufpointer = ChangeCharset(cur_encoding, CH_CP866, bufpointer);
|
2020-12-29 11:31:45 +01:00
|
|
|
bufsize = strlen(bufpointer);
|
|
|
|
}
|
2020-04-10 18:19:52 +02:00
|
|
|
}
|
2020-12-29 11:31:45 +01:00
|
|
|
|
|
|
|
|
2021-11-13 18:21:44 +01:00
|
|
|
table.cols.drop();
|
2021-08-02 16:08:12 +02:00
|
|
|
secondrun = false;
|
|
|
|
|
|
|
|
_PARSE_START_:
|
|
|
|
|
2020-03-22 20:50:16 +01:00
|
|
|
SetPageDefaults();
|
2021-08-02 16:08:12 +02:00
|
|
|
for (bufpos=bufpointer; bufpos < bufpointer+bufsize; bufpos++;)
|
2013-12-27 00:36:17 +01:00
|
|
|
{
|
2021-08-02 16:08:12 +02:00
|
|
|
if (style.pre) {
|
|
|
|
if (ESBYTE[bufpos] == 0x0a) {
|
2020-12-28 22:43:46 +01:00
|
|
|
RenderTextbuf();
|
2015-09-01 11:36:58 +02:00
|
|
|
NewLine();
|
2021-08-02 16:08:12 +02:00
|
|
|
continue;
|
2013-12-27 00:36:17 +01:00
|
|
|
}
|
2021-08-02 16:08:12 +02:00
|
|
|
if (ESBYTE[bufpos] == 0x09) {
|
2020-12-29 11:31:45 +01:00
|
|
|
tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4;
|
2020-03-26 01:12:32 +01:00
|
|
|
if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
|
2020-12-29 11:31:45 +01:00
|
|
|
while (tab_len) {chrcat(#linebuf,' '); tab_len--;}
|
2021-08-02 16:08:12 +02:00
|
|
|
continue;
|
2013-12-27 00:36:17 +01:00
|
|
|
}
|
2021-08-02 16:08:12 +02:00
|
|
|
}
|
|
|
|
if (ESBYTE[bufpos] == '&') {
|
2020-12-29 11:31:45 +01:00
|
|
|
bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize);
|
2021-08-02 16:08:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ESBYTE[bufpos] == '<') && (is_html) {
|
|
|
|
if (strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) {
|
|
|
|
bufpos++;
|
|
|
|
if (tag.parse(#bufpos, bufpointer + bufsize)) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (secondrun)
|
|
|
|
{
|
|
|
|
if (streq(#tag.name, "tr")) debugln(" ");
|
|
|
|
if (!tag.opened) {
|
|
|
|
debugch('/');
|
|
|
|
} else {
|
|
|
|
debugch(' ');
|
|
|
|
}
|
|
|
|
debugln(sprintf(#param, "%s x%i y%i %s",
|
|
|
|
#tag.name, draw_x, draw_y, #linebuf));
|
|
|
|
}
|
|
|
|
//*/
|
|
|
|
RenderTextbuf();
|
2021-11-21 12:42:21 +01:00
|
|
|
if (debug_mode) { debugch('<'); if(!tag.opened)debugch('/'); debug(#tag.name); debugln(">"); }
|
2021-08-02 16:08:12 +02:00
|
|
|
$push cur_encoding
|
|
|
|
SetStyle();
|
|
|
|
$pop eax
|
|
|
|
// The thing is that UTF if longer than other encodings.
|
|
|
|
// So if encoding was changed from UTF to DOS than $bufpos position got wrong,
|
|
|
|
// and we have to start parse from the very beginning
|
|
|
|
if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
|
|
|
|
ParseHtml(bufpointer, strlen(bufpointer));
|
|
|
|
return;
|
|
|
|
}
|
2020-12-14 16:54:00 +01:00
|
|
|
}
|
2021-08-02 16:08:12 +02:00
|
|
|
continue;
|
2020-03-22 20:50:16 +01:00
|
|
|
}
|
2013-12-27 00:36:17 +01:00
|
|
|
}
|
2021-08-02 16:08:12 +02:00
|
|
|
AddCharToTheLine(ESBYTE[bufpos]);
|
2013-12-27 00:36:17 +01:00
|
|
|
}
|
2021-08-02 16:08:12 +02:00
|
|
|
|
|
|
|
if (!secondrun) {
|
|
|
|
secondrun = true;
|
|
|
|
goto _PARSE_START_;
|
|
|
|
}
|
|
|
|
|
2020-12-28 22:43:46 +01:00
|
|
|
RenderTextbuf();
|
|
|
|
list.count = draw_y + style.cur_line_h;
|
2020-12-16 17:30:39 +01:00
|
|
|
|
2020-12-28 22:43:46 +01:00
|
|
|
canvas.bufh = math.max(list.visible, list.count);
|
2020-12-18 00:01:09 +01:00
|
|
|
buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
|
2020-12-16 17:30:39 +01:00
|
|
|
|
2020-03-27 11:38:54 +01:00
|
|
|
list.CheckDoesValuesOkey();
|
|
|
|
anchors.current = NULL;
|
2020-03-30 23:29:52 +02:00
|
|
|
if (!header) {
|
2020-04-04 01:17:28 +02:00
|
|
|
strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
|
2020-03-30 23:29:52 +02:00
|
|
|
DrawTitle(#header);
|
|
|
|
}
|
2013-12-27 00:36:17 +01:00
|
|
|
}
|
2015-08-19 14:54:01 +02:00
|
|
|
//============================================================================================
|
2020-12-18 13:55:55 +01:00
|
|
|
void TWebBrowser::AddCharToTheLine(unsigned char _char)
|
|
|
|
{
|
2020-12-29 11:31:45 +01:00
|
|
|
dword line_len = strlen(#linebuf);
|
2020-12-18 13:55:55 +01:00
|
|
|
if (_char<=15) _char=' ';
|
|
|
|
if (!style.pre) && (_char == ' ')
|
|
|
|
{
|
2020-12-29 11:31:45 +01:00
|
|
|
if (linebuf[line_len-1]==' ') return; //no double spaces
|
|
|
|
if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line
|
2020-12-18 13:55:55 +01:00
|
|
|
if (link) && (line_len==0) return;
|
|
|
|
}
|
2020-12-31 03:03:58 +01:00
|
|
|
if (line_len < sizeof(TWebBrowser.linebuf)) {
|
|
|
|
chrcat(#linebuf+line_len, _char);
|
|
|
|
} else {
|
|
|
|
RenderTextbuf();
|
|
|
|
}
|
2014-03-30 13:57:13 +02:00
|
|
|
}
|
2015-08-19 14:54:01 +02:00
|
|
|
//============================================================================================
|
2020-12-18 13:55:55 +01:00
|
|
|
void TWebBrowser::ChangeEncoding(int _new_encoding)
|
|
|
|
{
|
|
|
|
if (cur_encoding == _new_encoding) return;
|
|
|
|
cur_encoding = _new_encoding;
|
2021-11-24 11:52:23 +01:00
|
|
|
bufpointer = ChangeCharset(cur_encoding, CH_CP866, bufpointer);
|
2020-12-18 13:55:55 +01:00
|
|
|
if (header) {
|
2021-11-24 11:52:23 +01:00
|
|
|
ChangeCharset(cur_encoding, CH_CP866, #header);
|
2020-12-18 13:55:55 +01:00
|
|
|
DrawTitle(#header);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//============================================================================================
|
2020-12-28 22:43:46 +01:00
|
|
|
scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
|
|
|
|
0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
|
2020-12-18 00:01:09 +01:00
|
|
|
|
2015-08-23 12:12:13 +02:00
|
|
|
void TWebBrowser::DrawPage()
|
|
|
|
{
|
2021-01-02 13:45:42 +01:00
|
|
|
if (list.w!=canvas.bufw) Reparse();
|
2020-12-20 14:28:50 +01:00
|
|
|
canvas.Show(list.first, list.h);
|
|
|
|
|
2020-12-18 00:01:09 +01:00
|
|
|
scroll_wv.max_area = list.count;
|
|
|
|
scroll_wv.cur_area = list.visible;
|
|
|
|
scroll_wv.position = list.first;
|
|
|
|
scroll_wv.all_redraw = 0;
|
|
|
|
scroll_wv.start_x = list.x + list.w;
|
|
|
|
scroll_wv.start_y = list.y;
|
|
|
|
scroll_wv.size_y = list.h;
|
|
|
|
|
2020-12-28 20:20:59 +01:00
|
|
|
if (list.count <= list.visible) {
|
|
|
|
DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
|
|
|
|
scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
|
|
|
|
} else {
|
|
|
|
scrollbar_v_draw(#scroll_wv);
|
|
|
|
}
|
2015-08-23 12:12:13 +02:00
|
|
|
}
|