Subversion Repositories Kolibri OS

Rev

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