#include "TWB\colors.h"
#include "TWB\anchors.h"
#include "TWB\parse_tag.h"
#include "TWB\special.h"
#include "TWB\img.h"
#include "TWB\tag_list.h"
dword page_bg;
dword link_color_default;
dword link_color_active;
#include "TWB\links.h"
#define BODY_MARGIN 6
#define BASIC_LINE_H 18
DrawBufer DrawBuf;
char line[500];
struct _style {
bool
b, u, s, h,
font,
pre,
blq,
button,
image;
dword bg_color;
LIST tag_list;
dword tag_title;
};
struct TWebBrowser {
llist list;
_style style;
_img img;
dword draw_y, stolbec;
int zoom;
dword o_bufpointer;
int cur_encoding, custom_encoding;
bool link, t_html, t_body;
dword bufpointer;
dword bufsize;
dword is_html;
void Paint();
void SetPageDefaults();
void AddCharToTheLine();
void ParseHtml();
void SetStyle();
bool CheckForLineBreak();
void NewLine();
void DrawScroller();
void ChangeEncoding();
void DrawPage();
char header[150];
char redirect[URL_SIZE];
};
scroll_bar scroll_wv = { 15,NULL,NULL,NULL,0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
//============================================================================================
void TWebBrowser::Paint()
{
dword start_x, line_length, stolbec_len;
dword text_color__;
if (style.tag_title)
{
strncpy(#header, #line, sizeof(TWebBrowser.header)-1);
strncat(#header, " - ", sizeof(TWebBrowser.header)-1);
strncat(#header, #version, sizeof(TWebBrowser.header)-1);
line = 0;
return;
}
if (t_html) && (!t_body) {
line = 0;
return;
}
if (line)
{
start_x = stolbec * list.font_w + BODY_MARGIN + list.x;
stolbec_len = strlen(#line) * zoom;
line_length = stolbec_len * list.font_w;
if (debug_mode) {
DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, 0xDDDddd);
}
if (style.bg_color!=page_bg) {
DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, style.bg_color);
}
if (style.image) {
DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h-1, 0xF9DBCB);
}
if (style.button) {
DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h - calc(zoom*2), 0xCCCccc);
DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, 0x999999);
}
text_color__ = text_colors.get_last();
if (link) && (text_color__ == text_colors.get(0)) text_color__ = link_color_default;
DrawBuf.WriteText(start_x, draw_y, list.font_type, text_color__, #line, NULL);
if (style.b) DrawBuf.WriteText(start_x+1, draw_y, list.font_type, text_color__, #line, NULL);
if (style.s) DrawBuf.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_color__);
if (style.u) DrawBuf.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_color__);
if (link) {
if (line[0]==' ') && (line[1]==NULL) {} else {
DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2)-1, line_length, zoom, link_color_default);
links.add_text(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2)-1, zoom);
}
}
stolbec += stolbec_len;
if (debug_mode) debug(#line);
line = NULL;
}
}
//============================================================================================
void TWebBrowser::SetPageDefaults()
{
style.b = style.u = style.s = style.h = style.blq = t_html = t_body = style.pre =
link = style.tag_title = style.font = false;
style.tag_list.reset();
link_color_default = 0x0000FF;
link_color_active = 0xFF0000;
page_bg = 0xFFFFFF;
style.bg_color = page_bg;
DrawBuf.Fill(0, page_bg);
links.clear();
anchors.clear();
img.clear();
text_colors.drop();
text_colors.add(0);
header = NULL;
cur_encoding = CH_CP866;
draw_y = BODY_MARGIN;
stolbec = 0;
line = 0;
zoom = 1;
redirect = '\0';
//hold original buffer
if (o_bufpointer) o_bufpointer=free(o_bufpointer);
o_bufpointer = malloc(bufsize);
memmov(o_bufpointer, bufpointer, bufsize);
if (custom_encoding != -1) {
cur_encoding = custom_encoding;
bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
}
}
//============================================================================================
void TWebBrowser::AddCharToTheLine(unsigned char _char)
{
dword line_len;
if (_char<=15) _char=' ';
line_len = strlen(#line);
if (!style.pre) && (_char == ' ')
{
if (line[line_len-1]==' ') return; //no double spaces
if (!stolbec) && (!line) return; //no paces at the beginning of the line
}
if (line_len < sizeof(line)) chrcat(#line, _char);
CheckForLineBreak();
}
//============================================================================================
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
word bukva[2];
char unicode_symbol[10];
dword unicode_symbol_result;
dword j;
bool ignor_param=false;
int tab_len;
dword bufpos;
bufsize = _bufsize;
if (bufpointer != _bufpointer) {
bufpointer = malloc(bufsize);
memmov(bufpointer, _bufpointer, bufsize);
} else {
custom_encoding = CH_CP866;
}
SetPageDefaults();
is_html = true;
if (!strstri(bufpointer, "
';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++)
{
bukva = ESBYTE[bufpos+j];
chrcat(#unicode_symbol, bukva);
}
if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
bufpos += j;
CheckForLineBreak();
} else {
AddCharToTheLine('&');
}
break;
case '<':
if (!is_html) goto _DEFAULT;
bufpos++;
switch (ESBYTE[bufpos]) {
case '!':
case '/':
case 'a'...'z':
case 'A'...'Z':
goto _TAG;
default:
goto _DEFAULT;
}
_TAG:
if (!strncmp(bufpos,"!--",3))
{
bufpos+=3;
//STRSTR
while (strncmp(bufpos,"-->",3)!=0) && (bufpos < bufpointer + bufsize)
{
bufpos++;
}
bufpos+=2;
break;
}
tag.reset();
if (ESBYTE[bufpos] == '/') {
tag.opened = false;
bufpos++;
}
ignor_param=false;
while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //ïîëó÷àåì òåã è åãî ïàðàìåòðû
//&& (!chrnum(#tag.params,'\"') % 2) //
{
bukva = ESBYTE[bufpos];
if (__isWhite(bukva)) bukva = ' ';
if (!ignor_param) && (bukva!=' ') {
if (strlen(#tag.name)+1", #tag.name);
if (j = strstri(bufpos, #tag.params)) bufpos = j-1;
break;
}
if (tag.name[strlen(#tag.name)-1]=='/') tag.name[strlen(#tag.name)-1]=NULL; //for br/ !!!!!!!!
if (tag.params) tag.parse_params();
if (tag.name) {
CheckForLineBreak();
Paint();
if (tag.name) SetStyle();
}
break;
default:
_DEFAULT:
AddCharToTheLine(ESBYTE[bufpos]);
}
}
Paint();
NewLine();
list.count = draw_y;
list.CheckDoesValuesOkey();
anchors.current = NULL;
custom_encoding = -1;
if (!header) {
strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
DrawTitle(#header);
}
}
//============================================================================================
bool TWebBrowser::CheckForLineBreak()
{
int line_break_pos;
char new_line_text[4096];
//Do we need a line break?
if (strlen(#line)*zoom + stolbec < list.column_max) return false;
//Yes, we do. Lets calculate where...
line_break_pos = strrchr(#line, ' ');
//Is a new line fits in the current line?
if (line_break_pos*zoom + stolbec > list.column_max) {
line_break_pos = list.column_max/zoom - stolbec;
while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--;
}
//Maybe a new line is too big for the whole new line? Then we have to split it
if (!line_break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) {
line_break_pos = list.column_max/zoom - stolbec;
}
strcpy(#new_line_text, #line + line_break_pos);
line[line_break_pos] = 0x00;
Paint();
strcpy(#line, #new_line_text);
NewLine();
//while (CheckForLineBreak()==true) {};
return true;
}
//============================================================================================
void TWebBrowser::SetStyle() {
char img_path[4096]=0;
dword value;
if (value = tag.get_value_of("name=")) || (value = tag.get_value_of("id=")) {
anchors.add(value, draw_y);
if (anchors.current) && (streq(value, #anchors.current+1)) {
list.first = draw_y;
anchors.current = NULL;
}
}
if (tag.is("html")) {
t_html = tag.opened;
return;
}
if (tag.is("title")) {
style.tag_title = tag.opened;
if (!tag.opened) DrawTitle(#header);
return;
}
if (tag.is("body")) {
t_body = tag.opened;
if (value = tag.get_value_of("link=")) link_color_default = GetColor(value);
if (value = tag.get_value_of("alink=")) link_color_active = GetColor(value);
if (value = tag.get_value_of("text=")) text_colors.set(0, GetColor(value));
if (value = tag.get_value_of("bgcolor=")) {
style.bg_color = page_bg = GetColor(value);
DrawBuf.Fill(0, page_bg);
}
// Autodetecting encoding if no encoding was set
if (tag.opened) && (custom_encoding==-1) && (cur_encoding == CH_CP866) {
if (strstr(bufpointer, "\208\190")) ChangeEncoding(CH_UTF8);
else if (chrnum(bufpointer, '\246')>5) ChangeEncoding(CH_CP1251);
}
return;
}
if (tag.is("br")) { NewLine(); return; }
if (tag.is("b")) || (tag.is("strong")) || (tag.is("big")) || (tag.is("w:b")) {
style.b = tag.opened;
return;
}
if (tag.is("w:r")) && (!tag.opened) { style.b = false; return; }
if (tag.is("a")) {
if (tag.opened)
{
if (value = tag.get_value_of("href=")) && (!strstr(value,"javascript:"))
{
link = true;
links.add_link(value);
}
} else {
link = false;
style.bg_color = page_bg;
}
return;
}
if (tag.is("iframe")) && (value = tag.get_value_of("src=")) {
NewLine();
strcpy(#line, "IFRAME: ");
Paint();
link=true;
links.add_link(value);
strncpy(#line, value, sizeof(line)-1);
while (CheckForLineBreak()) {};
Paint();
link=false;
NewLine();
}
if (tag.is("font")) {
style.font = tag.opened;
style.bg_color = page_bg;
if (tag.opened)
{
if (value = tag.get_value_of("bg=")) style.bg_color = GetColor(value);
if (value = tag.get_value_of("color=")) {
text_colors.add(GetColor(value));
} else {
text_colors.add(text_colors.get_last());
}
}
else text_colors.pop();
return;
}
if (tag.is("div")) {
if (streq(#tag.prior,"div")) && (tag.opened) return;
if (!tag.opened) && (style.font) text_colors.pop();
NewLine();
return;
}
if (tag.is("header")) || (tag.is("article")) || (tag.is("footer")) || (tag.is("figure")) {
NewLine();
return;
}
if (tag.is("p")) || (tag.is("w:p")) {
IF (tag.prior[0] == 'h') || (streq(#tag.prior,"td")) || (streq(#tag.prior,"p")) return;
NewLine();
return;
}
if (tag.is("pre")) { style.pre = tag.opened; return; }
if (tag.is("td")) { if (tag.opened) AddCharToTheLine(' '); return; }
if (tag.is("tr")) { if (tag.opened) NewLine(); return; }
if (tag.is("button")) { style.button = tag.opened; stolbec++; return; }
if (tag.is("u")) || (tag.is("ins")) { style.u=tag.opened; return;}
if (tag.is("s")) || (tag.is("strike")) || (tag.is("del")) { style.s=tag.opened; return; }
if (tag.is("dl")) {
if (tag.opened) NewLine();
return;
}
if (tag.is("dd")) {
//NewLine();
//if (tag.opened) stolbec += 5; //stolbec overflow!
return;
}
if (tag.is("blockquote")) { style.blq = tag.opened; return; }
if (tag.is("code")) {
if (tag.opened) style.bg_color = 0xe4ffcb; else style.bg_color = page_bg;
style.pre = tag.opened; return;
}
if (tag.is("img")) {
value = tag.get_value_of("src=");
if (!value) goto NOIMG;
strlcpy(#img_path, value, sizeof(img_path)-1);
get_absolute_url(#img_path, history.current());
if (cache.has(#img_path))
{
img_decode stdcall (cache.current_buf, cache.current_size, 0);
if (!EAX) goto NOIMG;
EDI = EAX;
img.w.add(ESDWORD[EDI+4]);
img.h.add(ESDWORD[EDI+8]);
img_destroy stdcall(EDI);
img.url.add(#img_path);
if (img.w.get_last() / 6 + 1 + stolbec > list.column_max) {
NewLine();
}
img.x.add(stolbec*list.font_w+3);
img.y.add(draw_y);
stolbec += img.w.get_last() / 6 + 1;
if (img.h.get_last() > list.item_h) {
draw_y += img.h.get_last() - list.item_h;
NewLine();
}
if (link) links.add_text(
img.x.get_last() + list.x,
img.y.get_last() + list.y,
img.w.get_last(),
img.h.get_last(),
0);
//debugval(img.url.get_last(), img.y.get_last());
return;
} else {
img.url.add(#img_path);
}
NOIMG:
/*
if (streqrp(value, "data:")) {
EAX = strstr(value, "base64,");
if (value == EAX) return;
value = EAX;
//cache.add(history.current(), http.content_pointer, http.content_received, PAGE);
base64_decode stdcall (#pass_b64, value, strlen(value));
} else
*/
if (value = tag.get_value_of("title=")) && (strlen(value) list.first) && (img_y - list.h < list.first)
&& (cache.has(img.url.get(i))) img.draw(list.x, list.y, list.w, list.h, list.first, i);
}
}