Subversion Repositories Kolibri OS

Rev

Rev 8572 | Rev 9263 | 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];
8492 leency 49
	char linebuf[500];
8439 leency 50
	char redirect[URL_SIZE];
7759 leency 51
 
9103 leency 52
	bool secondrun;
53
 
8462 leency 54
	void SetStyle();
8491 leency 55
	void RenderTextbuf();
56
	void RenderLine();
8462 leency 57
	bool RenderImage();
58
 
7746 leency 59
	void SetPageDefaults();
7752 leency 60
	void ParseHtml();
8500 leency 61
	void Reparse();
7762 leency 62
	void NewLine();
63
	void ChangeEncoding();
8491 leency 64
	void AddCharToTheLine();
4411 leency 65
	void DrawPage();
8439 leency 66
 
67
	void tag_a();
68
	void tag_p();
69
	void tag_img();
70
	void tag_div();
71
	void tag_h1234_caption();
72
	void tag_ol_ul_dt();
73
	void tag_li();
74
	void tag_q();
75
	void tag_hr();
76
	void tag_code();
77
	void tag_meta_xml();
78
	void tag_body();
79
	void tag_iframe();
80
	void tag_title();
81
	void tag_font();
8492 leency 82
	void tag_table_reset();
8462 leency 83
	void tag_table();
84
	void tag_td();
85
	void tag_tr();
7759 leency 86
};
4486 leency 87
 
8462 leency 88
#include "TWB\render.h"
8439 leency 89
#include "TWB\set_style.h"
5747 leency 90
//============================================================================================
7746 leency 91
void TWebBrowser::SetPageDefaults()
92
{
8439 leency 93
	t_html = t_body = link = false;
94
	style.reset();
7759 leency 95
	link_color_default = 0x0000FF;
4491 leency 96
	link_color_active = 0xFF0000;
8454 leency 97
	style.cur_line_h = list.item_h;
7970 leency 98
	links.clear();
7752 leency 99
	anchors.clear();
8396 leency 100
	img_url.drop();
8016 leency 101
	text_colors.drop();
102
	text_colors.add(0);
8451 leency 103
	bg_colors.drop();
104
	bg_colors.add(DEFAULT_BG_COL);
105
	canvas.Fill(0, DEFAULT_BG_COL);
7758 leency 106
	header = NULL;
7759 leency 107
	draw_y = BODY_MARGIN;
8462 leency 108
	draw_x = left_gap = BODY_MARGIN;
109
	draw_w = list.w - BODY_MARGIN;
8492 leency 110
	linebuf = 0;
8302 leency 111
	redirect = '\0';
8490 leency 112
	list.SetFont(8, 14, 10011000b);
8492 leency 113
	tag_table_reset();
9103 leency 114
	is_html = true;
115
	if (!strstri(bufpointer, "
116
		t_body = true;
117
		if (!strstri(bufpointer, "
118
		&& (!strstr(bufpointer, "
119
			style.pre = true; //show linebreaks for a plaint text
120
			is_html = false;
121
		}
122
	}
7746 leency 123
}
124
//============================================================================================
8500 leency 125
void TWebBrowser::Reparse()
126
{
127
	ParseHtml(bufpointer, bufsize);
128
}
129
//============================================================================================
7759 leency 130
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
7752 leency 131
	int tab_len;
7746 leency 132
	dword bufpos;
7759 leency 133
	bufsize = _bufsize;
8439 leency 134
 
8457 leency 135
	if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
136
 
8492 leency 137
	if (bufpointer == _bufpointer) {
138
		custom_encoding = cur_encoding;
139
	} else {
7771 leency 140
		bufpointer = malloc(bufsize);
141
		memmov(bufpointer, _bufpointer, bufsize);
8492 leency 142
 
143
		//hold original buffer
144
		o_bufpointer = malloc(bufsize);
145
		memmov(o_bufpointer, bufpointer, bufsize);
146
 
147
		cur_encoding = CH_CP866;
148
		if (custom_encoding != -1) {
149
			cur_encoding = custom_encoding;
150
			bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
151
			bufsize = strlen(bufpointer);
152
		}
7771 leency 153
	}
8492 leency 154
 
155
 
9103 leency 156
	tr_col_count.drop();
157
	secondrun = false;
158
 
159
	_PARSE_START_:
160
 
7746 leency 161
	SetPageDefaults();
9103 leency 162
	for (bufpos=bufpointer; bufpos < bufpointer+bufsize; bufpos++;)
4411 leency 163
	{
9103 leency 164
		if (style.pre) {
165
			if (ESBYTE[bufpos] == 0x0a) {
8491 leency 166
				RenderTextbuf();
5781 leency 167
				NewLine();
9103 leency 168
				continue;
4411 leency 169
			}
9103 leency 170
			if (ESBYTE[bufpos] == 0x09) {
8492 leency 171
				tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4;
7752 leency 172
				if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
8492 leency 173
				while (tab_len) {chrcat(#linebuf,' '); tab_len--;}
9103 leency 174
				continue;
4411 leency 175
			}
9103 leency 176
		}
177
		if (ESBYTE[bufpos] == '&')	{
8492 leency 178
			bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize);
9103 leency 179
			continue;
180
		}
181
		if (ESBYTE[bufpos] == '<') && (is_html) {
182
			if (strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) {
183
				bufpos++;
184
				if (tag.parse(#bufpos, bufpointer + bufsize)) {
185
 
186
					/*
187
					if (secondrun)
188
					{
189
						if (streq(#tag.name, "tr")) debugln(" ");
190
						if (!tag.opened) {
191
							debugch('/');
192
						} else {
193
							debugch(' ');
194
						}
195
						debugln(sprintf(#param, "%s   x%i y%i %s",
196
							#tag.name, draw_x, draw_y, #linebuf));
197
					}
198
					//*/
199
 
200
					RenderTextbuf();
201
					$push cur_encoding
202
					SetStyle();
203
					$pop eax
204
					// The thing is that UTF if longer than other encodings.
205
					// So if encoding was changed from UTF to DOS than $bufpos position got wrong,
206
					// and we have to start parse from the very beginning
207
					if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
208
						ParseHtml(bufpointer, strlen(bufpointer));
209
						return;
210
					}
8407 leency 211
				}
9103 leency 212
				continue;
7746 leency 213
			}
4411 leency 214
		}
9103 leency 215
		AddCharToTheLine(ESBYTE[bufpos]);
4411 leency 216
	}
9103 leency 217
 
218
	if (!secondrun) {
219
		secondrun = true;
220
		goto _PARSE_START_;
221
	}
222
 
8491 leency 223
	RenderTextbuf();
224
	list.count = draw_y + style.cur_line_h;
8425 leency 225
 
8491 leency 226
	canvas.bufh = math.max(list.visible, list.count);
8439 leency 227
	buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
8425 leency 228
 
7755 leency 229
	list.CheckDoesValuesOkey();
230
	anchors.current = NULL;
7758 leency 231
	if (!header) {
7759 leency 232
		strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
7758 leency 233
		DrawTitle(#header);
234
	}
4411 leency 235
}
5747 leency 236
//============================================================================================
8444 leency 237
void TWebBrowser::AddCharToTheLine(unsigned char _char)
238
{
8492 leency 239
	dword line_len = strlen(#linebuf);
8444 leency 240
	if (_char<=15) _char=' ';
241
	if (!style.pre) && (_char == ' ')
242
	{
8492 leency 243
		if (linebuf[line_len-1]==' ') return; //no double spaces
244
		if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line
8444 leency 245
		if (link) && (line_len==0) return;
246
	}
8499 leency 247
	if (line_len < sizeof(TWebBrowser.linebuf)) {
248
		chrcat(#linebuf+line_len, _char);
249
	} else {
250
		RenderTextbuf();
251
	}
8444 leency 252
}
253
//============================================================================================
254
void TWebBrowser::ChangeEncoding(int _new_encoding)
255
{
256
	if (cur_encoding == _new_encoding) return;
257
	cur_encoding = _new_encoding;
258
	bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
259
	if (header) {
260
		ChangeCharset(cur_encoding, "CP866", #header);
261
		DrawTitle(#header);
262
	}
263
}
264
//============================================================================================
8491 leency 265
scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
266
  0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
8439 leency 267
 
5766 leency 268
void TWebBrowser::DrawPage()
269
{
8500 leency 270
	if (list.w!=canvas.bufw) Reparse();
8457 leency 271
	canvas.Show(list.first, list.h);
272
 
8439 leency 273
	scroll_wv.max_area = list.count;
274
	scroll_wv.cur_area = list.visible;
275
	scroll_wv.position = list.first;
276
	scroll_wv.all_redraw = 0;
277
	scroll_wv.start_x = list.x + list.w;
278
	scroll_wv.start_y = list.y;
279
	scroll_wv.size_y = list.h;
280
 
8490 leency 281
	if (list.count <= list.visible) {
282
		DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
283
		scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
284
	} else {
285
		scrollbar_v_draw(#scroll_wv);
286
	}
5766 leency 287
}