Subversion Repositories Kolibri OS

Rev

Rev 8457 | Rev 8490 | 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
	}
7746 leency 118
}
119
//============================================================================================
7759 leency 120
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
7752 leency 121
	char unicode_symbol[10];
7746 leency 122
	dword j;
7752 leency 123
	int tab_len;
7746 leency 124
	dword bufpos;
7759 leency 125
	bufsize = _bufsize;
8439 leency 126
 
8457 leency 127
	if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
128
 
7771 leency 129
	if (bufpointer != _bufpointer) {
130
		bufpointer = malloc(bufsize);
131
		memmov(bufpointer, _bufpointer, bufsize);
132
	} else {
133
		custom_encoding = CH_CP866;
134
	}
7746 leency 135
	SetPageDefaults();
7945 leency 136
	is_html = true;
137
	if (!strstri(bufpointer, "
7749 leency 138
		t_body = true;
7991 leency 139
		if (!strstri(bufpointer, "
7945 leency 140
			style.pre = true; //show linebreaks for a plaint text
141
			is_html = false;
142
		}
7749 leency 143
	}
5773 leency 144
	for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
4411 leency 145
	{
8330 leency 146
		switch (ESBYTE[bufpos])
4411 leency 147
		{
148
		case 0x0a:
7752 leency 149
			if (style.pre) {
8462 leency 150
				Render();
5781 leency 151
				NewLine();
7752 leency 152
			} else {
153
				AddCharToTheLine(0x0a);
4411 leency 154
			}
7752 leency 155
			break;
156
		case 0x09:
157
			if (style.pre) {
8462 leency 158
				tab_len = draw_x - left_gap / list.font_w + strlen(#line) % 4;
7752 leency 159
				if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
4411 leency 160
				for (j=0; j
7752 leency 161
			} else {
162
				AddCharToTheLine(0x09);
4411 leency 163
			}
7752 leency 164
			break;
4411 leency 165
		case '&': //  and so on
8319 leency 166
			for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++)
4411 leency 167
			{
8410 leency 168
				chrcat(#unicode_symbol, ESBYTE[bufpos+j]);
4411 leency 169
			}
7771 leency 170
			if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
7749 leency 171
				bufpos += j;
7750 leency 172
				CheckForLineBreak();
7749 leency 173
			} else {
7752 leency 174
				AddCharToTheLine('&');
7749 leency 175
			}
4411 leency 176
			break;
177
		case '<':
8330 leency 178
			if (!is_html) goto _DEFAULT;
5749 leency 179
			bufpos++;
8330 leency 180
			switch (ESBYTE[bufpos]) {
8439 leency 181
				case '!': case '/': case '?':
182
				case 'a'...'z': case 'A'...'Z':
8330 leency 183
					goto _TAG;
184
				default:
185
					goto _DEFAULT;
186
			}
187
			_TAG:
8440 leency 188
			if (tag.parse(#bufpos, bufpointer + bufsize)) {
7750 leency 189
				CheckForLineBreak();
8462 leency 190
				Render();
8439 leency 191
				$push cur_encoding
192
				SetStyle();
193
				$pop eax
194
				// The thing is that UTF if longer than other encodings.
195
				// So if encoding was changed from UTF to DOS than $bufpos position got wrong,
196
				// and we have to start parse from the very beginning
197
				if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
198
					ParseHtml(bufpointer, bufsize);
199
					return;
8407 leency 200
				}
7746 leency 201
			}
4411 leency 202
			break;
203
		default:
8330 leency 204
			_DEFAULT:
7752 leency 205
			AddCharToTheLine(ESBYTE[bufpos]);
4411 leency 206
		}
207
	}
8462 leency 208
	Render();
5766 leency 209
	NewLine();
7752 leency 210
	list.count = draw_y;
8425 leency 211
 
8439 leency 212
	canvas.bufh = math.max(list.visible, draw_y);
213
	buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
8425 leency 214
 
7755 leency 215
	list.CheckDoesValuesOkey();
216
	anchors.current = NULL;
7759 leency 217
	custom_encoding = -1;
7758 leency 218
	if (!header) {
7759 leency 219
		strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
7758 leency 220
		DrawTitle(#header);
221
	}
4411 leency 222
}
5747 leency 223
//============================================================================================
8444 leency 224
void TWebBrowser::AddCharToTheLine(unsigned char _char)
225
{
226
	dword line_len;
227
	if (_char<=15) _char=' ';
228
	line_len = strlen(#line);
229
	if (!style.pre) && (_char == ' ')
230
	{
231
		if (line[line_len-1]==' ') return; //no double spaces
8462 leency 232
		if (draw_x==left_gap) && (!line) return; //no paces at the beginning of the line
8444 leency 233
		if (link) && (line_len==0) return;
234
	}
235
	if (line_len < sizeof(line)) chrcat(#line, _char);
236
	CheckForLineBreak();
237
}
238
//============================================================================================
7750 leency 239
bool TWebBrowser::CheckForLineBreak()
4718 leency 240
{
8457 leency 241
	int break_pos;
242
	char next_line[4096];
243
	int zoom = list.font_w / BASIC_CHAR_W;
7758 leency 244
	//Do we need a line break?
8462 leency 245
	if (strlen(#line) * list.font_w + draw_x < draw_w) return false;
7758 leency 246
	//Yes, we do. Lets calculate where...
8457 leency 247
	break_pos = strrchr(#line, ' ');
248
 
7758 leency 249
	//Is a new line fits in the current line?
8462 leency 250
	if (break_pos * list.font_w + draw_x > draw_w) {
251
		break_pos = draw_w - draw_x /list.font_w;
8457 leency 252
		while(break_pos) && (line[break_pos]!=' ') break_pos--;
7750 leency 253
	}
7758 leency 254
	//Maybe a new line is too big for the whole new line? Then we have to split it
8457 leency 255
	if (!break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) {
8462 leency 256
		break_pos = draw_w  - draw_x / list.font_w;
7742 leency 257
	}
8457 leency 258
 
259
	strcpy(#next_line, #line + break_pos);
260
	line[break_pos] = 0x00;
7758 leency 261
 
8462 leency 262
	Render();
7758 leency 263
 
8457 leency 264
	strcpy(#line, #next_line);
4718 leency 265
	NewLine();
7750 leency 266
	return true;
4718 leency 267
}
5747 leency 268
//============================================================================================
4692 leency 269
void TWebBrowser::NewLine()
4475 leency 270
{
8457 leency 271
	static bool empty_line = true;
4692 leency 272
 
8462 leency 273
	if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return;
8457 leency 274
	if (t_html) && (!t_body) return;
7742 leency 275
 
8462 leency 276
	if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) {
8457 leency 277
		if (!empty_line) empty_line=true; else return;
7742 leency 278
	} else {
8457 leency 279
		empty_line = false;
7742 leency 280
	}
6794 leency 281
 
8451 leency 282
	draw_y += style.cur_line_h;
283
	style.cur_line_h = list.item_h;
8457 leency 284
	if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0;
8462 leency 285
	draw_x += style.tag_list.level * 5 * list.font_w + left_gap;
4488 leency 286
}
5747 leency 287
//============================================================================================
8444 leency 288
void TWebBrowser::ChangeEncoding(int _new_encoding)
289
{
290
	if (cur_encoding == _new_encoding) return;
291
	cur_encoding = _new_encoding;
292
	bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
293
	if (header) {
294
		ChangeCharset(cur_encoding, "CP866", #header);
295
		DrawTitle(#header);
296
	}
297
}
298
//============================================================================================
8439 leency 299
scroll_bar scroll_wv =
300
{ 15,NULL,NULL,NULL,0,2,NULL,
301
  0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
302
 
5766 leency 303
void TWebBrowser::DrawPage()
304
{
8457 leency 305
	if (list.w!=canvas.bufw) {
306
		ParseHtml(bufpointer, bufsize);
307
	}
308
	canvas.Show(list.first, list.h);
309
 
8439 leency 310
	scroll_wv.max_area = list.count;
311
	scroll_wv.cur_area = list.visible;
312
	scroll_wv.position = list.first;
313
	scroll_wv.all_redraw = 0;
314
	scroll_wv.start_x = list.x + list.w;
315
	scroll_wv.start_y = list.y;
316
	scroll_wv.size_y = list.h;
317
	scrollbar_v_draw(#scroll_wv);
318
 
5766 leency 319
}