Subversion Repositories Kolibri OS

Rev

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