Subversion Repositories Kolibri OS

Rev

Rev 8490 | Rev 8492 | 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];
8491 leency 49
	char line[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();
8462 leency 79
	void tag_table();
80
	void tag_td();
81
	void tag_tr();
7759 leency 82
};
4486 leency 83
 
8462 leency 84
#include "TWB\render.h"
8439 leency 85
#include "TWB\set_style.h"
5747 leency 86
//============================================================================================
7746 leency 87
void TWebBrowser::SetPageDefaults()
88
{
8439 leency 89
	t_html = t_body = link = false;
90
	style.reset();
7759 leency 91
	link_color_default = 0x0000FF;
4491 leency 92
	link_color_active = 0xFF0000;
8454 leency 93
	style.cur_line_h = list.item_h;
7970 leency 94
	links.clear();
7752 leency 95
	anchors.clear();
8396 leency 96
	img_url.drop();
8016 leency 97
	text_colors.drop();
98
	text_colors.add(0);
8451 leency 99
	bg_colors.drop();
100
	bg_colors.add(DEFAULT_BG_COL);
101
	canvas.Fill(0, DEFAULT_BG_COL);
7758 leency 102
	header = NULL;
7759 leency 103
	cur_encoding = CH_CP866;
104
	draw_y = BODY_MARGIN;
8462 leency 105
	draw_x = left_gap = BODY_MARGIN;
106
	draw_w = list.w - BODY_MARGIN;
4411 leency 107
	line = 0;
8302 leency 108
	redirect = '\0';
7757 leency 109
	//hold original buffer
110
	if (o_bufpointer) o_bufpointer=free(o_bufpointer);
111
	o_bufpointer = malloc(bufsize);
112
	memmov(o_bufpointer, bufpointer, bufsize);
7759 leency 113
	if (custom_encoding != -1) {
114
		cur_encoding = custom_encoding;
7781 leency 115
		bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
7759 leency 116
	}
8490 leency 117
	list.SetFont(8, 14, 10011000b);
7746 leency 118
}
119
//============================================================================================
7759 leency 120
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
7752 leency 121
	int tab_len;
7746 leency 122
	dword bufpos;
7759 leency 123
	bufsize = _bufsize;
8439 leency 124
 
8457 leency 125
	if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
126
 
7771 leency 127
	if (bufpointer != _bufpointer) {
128
		bufpointer = malloc(bufsize);
129
		memmov(bufpointer, _bufpointer, bufsize);
130
	} else {
131
		custom_encoding = CH_CP866;
132
	}
7746 leency 133
	SetPageDefaults();
7945 leency 134
	is_html = true;
135
	if (!strstri(bufpointer, "
7749 leency 136
		t_body = true;
7991 leency 137
		if (!strstri(bufpointer, "
7945 leency 138
			style.pre = true; //show linebreaks for a plaint text
139
			is_html = false;
140
		}
7749 leency 141
	}
5773 leency 142
	for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
4411 leency 143
	{
8330 leency 144
		switch (ESBYTE[bufpos])
4411 leency 145
		{
146
		case 0x0a:
7752 leency 147
			if (style.pre) {
8491 leency 148
				RenderTextbuf();
5781 leency 149
				NewLine();
7752 leency 150
			} else {
8491 leency 151
				goto _DEFAULT;
4411 leency 152
			}
7752 leency 153
			break;
154
		case 0x09:
155
			if (style.pre) {
8462 leency 156
				tab_len = draw_x - left_gap / list.font_w + strlen(#line) % 4;
7752 leency 157
				if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
8491 leency 158
				while (tab_len) {chrcat(#line,' '); tab_len--;}
7752 leency 159
			} else {
8491 leency 160
				goto _DEFAULT;
4411 leency 161
			}
7752 leency 162
			break;
4411 leency 163
		case '&': //  and so on
8491 leency 164
			bufpos = GetUnicodeSymbol(#line, sizeof(TWebBrowser.line), bufpos+1, bufpointer+bufsize);
4411 leency 165
			break;
166
		case '<':
8330 leency 167
			if (!is_html) goto _DEFAULT;
8490 leency 168
			if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT;
5749 leency 169
			bufpos++;
8440 leency 170
			if (tag.parse(#bufpos, bufpointer + bufsize)) {
8491 leency 171
				RenderTextbuf();
8439 leency 172
				$push cur_encoding
173
				SetStyle();
174
				$pop eax
175
				// The thing is that UTF if longer than other encodings.
176
				// So if encoding was changed from UTF to DOS than $bufpos position got wrong,
177
				// and we have to start parse from the very beginning
178
				if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
179
					ParseHtml(bufpointer, bufsize);
180
					return;
8407 leency 181
				}
7746 leency 182
			}
4411 leency 183
			break;
184
		default:
8330 leency 185
			_DEFAULT:
7752 leency 186
			AddCharToTheLine(ESBYTE[bufpos]);
4411 leency 187
		}
188
	}
8491 leency 189
	RenderTextbuf();
190
	list.count = draw_y + style.cur_line_h;
8425 leency 191
 
8491 leency 192
	canvas.bufh = math.max(list.visible, list.count);
8439 leency 193
	buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
8425 leency 194
 
7755 leency 195
	list.CheckDoesValuesOkey();
196
	anchors.current = NULL;
7759 leency 197
	custom_encoding = -1;
7758 leency 198
	if (!header) {
7759 leency 199
		strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
7758 leency 200
		DrawTitle(#header);
201
	}
4411 leency 202
}
5747 leency 203
//============================================================================================
8444 leency 204
void TWebBrowser::AddCharToTheLine(unsigned char _char)
205
{
8491 leency 206
	dword line_len = strlen(#line);
8444 leency 207
	if (_char<=15) _char=' ';
208
	if (!style.pre) && (_char == ' ')
209
	{
210
		if (line[line_len-1]==' ') return; //no double spaces
8462 leency 211
		if (draw_x==left_gap) && (!line) return; //no paces at the beginning of the line
8444 leency 212
		if (link) && (line_len==0) return;
213
	}
8491 leency 214
	if (line_len < sizeof(TWebBrowser.line)) chrcat(#line+line_len, _char);
215
	if (line_len+1 * list.font_w + draw_x >= draw_w) RenderTextbuf();
8444 leency 216
}
217
//============================================================================================
4692 leency 218
void TWebBrowser::NewLine()
4475 leency 219
{
8457 leency 220
	static bool empty_line = true;
4692 leency 221
 
8462 leency 222
	if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return;
8457 leency 223
	if (t_html) && (!t_body) return;
7742 leency 224
 
8462 leency 225
	if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) {
8457 leency 226
		if (!empty_line) empty_line=true; else return;
7742 leency 227
	} else {
8457 leency 228
		empty_line = false;
7742 leency 229
	}
6794 leency 230
 
8451 leency 231
	draw_y += style.cur_line_h;
232
	style.cur_line_h = list.item_h;
8457 leency 233
	if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0;
8462 leency 234
	draw_x += style.tag_list.level * 5 * list.font_w + left_gap;
4488 leency 235
}
5747 leency 236
//============================================================================================
8444 leency 237
void TWebBrowser::ChangeEncoding(int _new_encoding)
238
{
239
	if (cur_encoding == _new_encoding) return;
240
	cur_encoding = _new_encoding;
241
	bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
242
	if (header) {
243
		ChangeCharset(cur_encoding, "CP866", #header);
244
		DrawTitle(#header);
245
	}
246
}
247
//============================================================================================
8491 leency 248
scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
249
  0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
8439 leency 250
 
5766 leency 251
void TWebBrowser::DrawPage()
252
{
8457 leency 253
	if (list.w!=canvas.bufw) {
254
		ParseHtml(bufpointer, bufsize);
255
	}
256
	canvas.Show(list.first, list.h);
257
 
8439 leency 258
	scroll_wv.max_area = list.count;
259
	scroll_wv.cur_area = list.visible;
260
	scroll_wv.position = list.first;
261
	scroll_wv.all_redraw = 0;
262
	scroll_wv.start_x = list.x + list.w;
263
	scroll_wv.start_y = list.y;
264
	scroll_wv.size_y = list.h;
265
 
8490 leency 266
	if (list.count <= list.visible) {
267
		DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
268
		scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
269
	} else {
270
		scrollbar_v_draw(#scroll_wv);
271
	}
5766 leency 272
}