Subversion Repositories Kolibri OS

Rev

Rev 7208 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4411 leency 1
 
4677 leency 2
4416 leency 3
 
5746 leency 4
4416 leency 5
 
5746 leency 6
byte
7
	b, u, s, h,
6794 leency 8
	pre,
5746 leency 9
	blq,
10
	li,
11
	li_tab,
12
	align;
13
};
14
15
 
4411 leency 16
	llist list;
4540 leency 17
	_style style;
5746 leency 18
	DrawBufer DrawBuf;
4475 leency 19
	int zoom;
6803 leency 20
	void Prepare();
5766 leency 21
	void SetStyle();
22
	void DrawStyle();
23
	void DrawPage();
4411 leency 24
	void DrawScroller();
25
	void LoadInternalPage();
5747 leency 26
	void NewLine();
4650 leency 27
	void Perenos();
4718 leency 28
	void BufEncode();
5749 leency 29
} WB1;
4686 leency 30
4486 leency 31
 
5746 leency 32
 
33
	link,
34
	ignor_text,
35
	cur_encoding,
36
	t_html,
37
	t_body;
38
39
 
5781 leency 40
dword o_bufpointer=0;
41
dword bufsize=0;
42
4411 leency 43
 
4417 leency 44
dword text_color_index;
45
dword link_color_inactive;
4491 leency 46
dword link_color_active;
47
dword bg_color;
4417 leency 48
4411 leency 49
 
6794 leency 50
int stolbec;
4417 leency 51
int tab_len;
52
int anchor_y;
6794 leency 53
4636 leency 54
 
6794 leency 55
int basic_line_h=22;
56
57
 
5746 leency 58
char header[2048];
59
char line[500];
4417 leency 60
char tagparam[10000];
5746 leency 61
char tag[100];
4417 leency 62
char oldtag[100];
5768 leency 63
char attr[1200];
5746 leency 64
char val[4096];
65
4411 leency 66
 
5990 leency 67
#include "..\TWB\links.h"
4686 leency 68
#include "..\TWB\colors.h"
4411 leency 69
#include "..\TWB\unicode_tags.h"
70
#include "..\TWB\img_cache.h"
71
#include "..\TWB\parce_tag.h"
72
73
 
4475 leency 74
 
5747 leency 75
void TWebBrowser::DrawStyle()
5766 leency 76
{
4411 leency 77
	dword start_x, line_length, stolbec_len;
6803 leency 78
4411 leency 79
 
80
	{
81
		strcpy(#header, #line);
7032 leency 82
		line = 0;
4411 leency 83
		return;
84
	}
85
	if (t_html) && (!t_body) return;
4637 leency 86
4411 leency 87
 
6803 leency 88
	{
4411 leency 89
		start_x = stolbec * list.font_w + body_magrin + list.x;
6794 leency 90
		stolbec_len = strlen(#line) * zoom;
6803 leency 91
		line_length = stolbec_len * list.font_w;
5768 leency 92
4411 leency 93
 
6794 leency 94
		if (style.b) WriteBufText(start_x+1, draw_y, list.font_type, text_colors[text_color_index], #line, buf_data);
95
		if (style.s) DrawBuf.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
6803 leency 96
		if (style.u) DrawBuf.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
97
		if (link) {
5519 leency 98
			DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, text_colors[text_color_index]);
6803 leency 99
			PageLinks.AddText(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2), UNDERLINE, zoom); //TODO: set bigger underline_h for style.h
100
		}
4411 leency 101
		stolbec += stolbec_len;
4718 leency 102
	}
4411 leency 103
}
104
//============================================================================================
5747 leency 105
void TWebBrowser::LoadInternalPage(dword bufpos, in_filesize){
106
	bufsize = in_filesize;
107
	bufpointer = bufpos;
108
	Prepare();
5766 leency 109
}
5747 leency 110
//============================================================================================
111
void TWebBrowser::Prepare(){
5766 leency 112
	word bukva[2];
4411 leency 113
	dword j;
6803 leency 114
	byte ignor_param;
4411 leency 115
	dword bufpos;
5773 leency 116
	dword line_len;
6803 leency 117
	style.b = style.u = style.s = style.h = style.blq = t_html = t_body =
6794 leency 118
	style.li = link = ignor_text = text_color_index = text_colors[0] = style.li_tab = 0;
5749 leency 119
	style.align = ALIGN_LEFT;
5746 leency 120
	link_color_inactive = 0x0000FF;
4491 leency 121
	link_color_active = 0xFF0000;
122
	bg_color = 0xFFFFFF;
6783 leency 123
	DrawBuf.Fill(bg_color);
4475 leency 124
	PageLinks.Clear();
4504 leency 125
	strcpy(#header, #version);
4411 leency 126
	draw_y = body_magrin;
6794 leency 127
	stolbec = 0;
4411 leency 128
	line = 0;
129
	zoom = 1;
6803 leency 130
	//for plaint text use CP866 for other UTF
5749 leency 131
	if (strstri(bufpointer, "html")!=-1)
6783 leency 132
	{
5749 leency 133
		debugln(" found");
6783 leency 134
		style.pre = false;
135
		cur_encoding = CH_NULL;
6045 leency 136
	}
5749 leency 137
	else
138
	{
139
		debugln("no ");
6783 leency 140
		style.pre = true;
141
		cur_encoding = CH_NULL;
6045 leency 142
	}
5749 leency 143
	for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
5773 leency 144
	{
4411 leency 145
		if (ignor_text) && (ESBYTE[bufpos]!='<') continue;
5773 leency 146
		bukva = ESBYTE[bufpos];
4508 leency 147
		switch (bukva)
4411 leency 148
		{
149
		case 0x0a:
150
			if (style.pre)
5746 leency 151
			{
4411 leency 152
				DrawStyle();
5781 leency 153
				line = NULL;
6783 leency 154
				NewLine();
5781 leency 155
				break;
4726 leency 156
			}
4411 leency 157
			goto DEFAULT_MARK;
6783 leency 158
		case '\9':
4411 leency 159
			if (style.pre) //otherwise go to 0x0d
6735 leency 160
			{
4411 leency 161
				tab_len = strlen(#line) % 4;
4725 leency 162
				if (!tab_len) tab_len = 4;
163
				for (j=0; j
4411 leency 164
				break;
165
			}
166
			goto DEFAULT_MARK;
6783 leency 167
		case '&': //  and so on
4411 leency 168
			bufpos++;
4508 leency 169
			tag=0;
4411 leency 170
			for (j=0; (ESBYTE[bufpos]<>';') && (j<7);   j++, bufpos++;)
4508 leency 171
			{
4411 leency 172
				bukva = ESBYTE[bufpos];
4508 leency 173
				chrcat(#tag, bukva);
4411 leency 174
			}
175
			if (bukva = GetUnicodeSymbol()) goto DEFAULT_MARK;
4417 leency 176
			break;
4411 leency 177
		case '<':
178
			bufpos++;
5749 leency 179
			if (!strncmp(bufpos,"!--",3))
5768 leency 180
			{
4411 leency 181
				bufpos+=3;
6986 leency 182
				while (strncmp(bufpos,"-->",3)!=0) && (bufpos < bufpointer + bufsize)
183
				{
184
					bufpos++;
185
				}
186
				bufpos+=3;
187
				break;
188
			}
4411 leency 189
			tag = attr = tagparam = ignor_param = NULL;
5768 leency 190
			while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //ïîëó÷àåì òåã è åãî ïàðàìåòðû
6735 leency 191
			{
4411 leency 192
				bukva = ESBYTE[bufpos];
4508 leency 193
				if (bukva == '\9') || (bukva == '\x0a') || (bukva == '\x0d') bukva = ' ';
4411 leency 194
				if (!ignor_param) && (bukva <>' ')
195
				{
196
					if (strlen(#tag)+1
6686 leency 197
				}
4411 leency 198
				else
199
				{
200
					ignor_param = true;
201
					if (!ignor_text) && (strlen(#tagparam)+1
202
				}
203
				bufpos++;
4508 leency 204
			}
4411 leency 205
			strlwr(#tag);
206
207
 
208
			if (tagparam) GetNextParam();
4726 leency 209
			Perenos();
5768 leency 210
			DrawStyle();
5766 leency 211
			line = NULL;
4677 leency 212
			if (tag) SetStyle();
6735 leency 213
			strlcpy(#oldtag, #tag, sizeof(oldtag));
6686 leency 214
			tag = attr = tagparam = ignor_param = NULL;
5746 leency 215
			break;
4411 leency 216
		default:
217
			DEFAULT_MARK:
218
			if (bukva<=15) bukva=' ';
219
			line_len = strlen(#line);
6021 leency 220
			if (!style.pre) && (bukva == ' ')
5746 leency 221
			{
4411 leency 222
				if (line[line_len-1]==' ') break; //no double spaces
4726 leency 223
				if (!stolbec) && (!line) break; //no paces at the beginning of the line
224
			}
4411 leency 225
			if (line_len < sizeof(line)) chrcat(#line, bukva);
4726 leency 226
			Perenos();
5768 leency 227
		}
4411 leency 228
	}
229
	DrawStyle();
5766 leency 230
	NewLine();
231
	if (list.first == 0) list.count = draw_y;
6794 leency 232
	DrawPage();
4692 leency 233
}
4411 leency 234
//============================================================================================
5747 leency 235
void TWebBrowser::Perenos()
4718 leency 236
{
237
	int perenos_num;
238
	char new_line_text[4096];
239
	if (strlen(#line)*zoom + stolbec < list.column_max) return;
6803 leency 240
	perenos_num = strrchr(#line, ' ');
4718 leency 241
	if (!perenos_num) && (strlen(#line)*zoom>list.column_max) perenos_num=list.column_max/zoom;
6803 leency 242
	strcpy(#new_line_text, #line + perenos_num);
4718 leency 243
	line[perenos_num] = 0x00;
244
	DrawStyle();
5766 leency 245
	strcpy(#line, #new_line_text);
4718 leency 246
	NewLine();
247
}
248
//============================================================================================
5747 leency 249
void TWebBrowser::SetStyle() {
5773 leency 250
	int left1 = body_magrin + list.x;
6794 leency 251
	byte opened;
4491 leency 252
	byte meta_encoding;
4636 leency 253
	if (tag[0] == '/')
4411 leency 254
	{
255
		 opened = 0;
4491 leency 256
		 strcpy(#tag, #tag+1);
4411 leency 257
	}
258
	else opened = 1;
4491 leency 259
	if (istag("html")) {
5749 leency 260
		t_html = opened;
4637 leency 261
		return;
4411 leency 262
	}
263
	if (istag("script")) || (istag("style")) || (istag("binary")) || (istag("select")) { ignor_text = opened; return; }
5749 leency 264
	if (istag("form")) if (!opened) ignor_text = false;
5746 leency 265
	if(istag("title")) {
5749 leency 266
		if (opened) header=NULL;
4491 leency 267
		return;
4411 leency 268
	}
269
	if (ignor_text) return;
270
271
 
5746 leency 272
	{
4491 leency 273
		if (opened)	strcat(#line, " \"");
5746 leency 274
		if (!opened) strcat(#line, "\" ");
275
		return;
5749 leency 276
	}
4491 leency 277
	//if (isattr("id=")) || (isattr("name=")) { //very bad: if the tag is not the last it wound work
6803 leency 278
		//add anchor
279
	//}
280
	if (istag("body")) {
5749 leency 281
		t_body = opened;
4637 leency 282
		do{
4411 leency 283
			if (isattr("link=")) link_color_inactive = GetColor(#val);
5746 leency 284
			if (isattr("alink=")) link_color_active = GetColor(#val);
285
			if (isattr("text=")) text_colors[0]=GetColor(#val);
286
			if (isattr("bgcolor="))
287
			{
4411 leency 288
				bg_color = GetColor(#val);
6783 leency 289
				DrawBuf.Fill(bg_color);
4475 leency 290
			}
4411 leency 291
		} while(GetNextParam());
292
		if (opened) && (cur_encoding==CH_NULL) {
7032 leency 293
			cur_encoding = CH_UTF8;
294
			debugln("Document has no information about encoding, UTF will be used");
295
		}
296
		if (opened) {
297
			if (strcmp(#header, #version) != 0) {
7208 leency 298
				ChangeCharset(charsets[cur_encoding], "CP866", #header);
7056 leency 299
				sprintf(#header, "%s - %s", #header, #version);
300
			}
301
			DrawTitle(#header);
7032 leency 302
		}
303
		return;
4411 leency 304
	}
305
	if (istag("a")) {
5749 leency 306
		if (opened)
4491 leency 307
		{
4411 leency 308
			if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
6735 leency 309
			do{
4411 leency 310
				if (isattr("href=")) && (!strstr(#val,"javascript:"))
6001 leency 311
				{
4411 leency 312
					text_color_index++;
313
					text_colors[text_color_index] = text_colors[text_color_index-1];
314
					link = 1;
315
					text_colors[text_color_index] = link_color_inactive;
4491 leency 316
					PageLinks.AddLink(#val);
6795 leency 317
				}
4411 leency 318
			} while(GetNextParam());
319
		}
320
		else {
321
			link = 0;
322
			IF(text_color_index > 0) text_color_index--;
323
		}
324
		return;
325
	}
326
	if (istag("font")) {
5749 leency 327
		if (opened)
4491 leency 328
		{
4411 leency 329
			text_color_index++;
330
			text_colors[text_color_index] = text_colors[text_color_index-1];
331
			do{
332
				if (isattr("color=")) text_colors[text_color_index] = GetColor(#val);
5749 leency 333
			} while(GetNextParam());
4411 leency 334
		}
335
		else if (text_color_index > 0) text_color_index--;
5749 leency 336
		return;
4411 leency 337
	}
338
	if (istag("div")) || (istag("header")) || (istag("article")) || (istag("footer")) {
5746 leency 339
		IF(oldtag[0] != 'h') NewLine();
5749 leency 340
		return;
4411 leency 341
	}
342
	if (istag("p")) {
5746 leency 343
		IF(oldtag[0] == 'h') return;
4411 leency 344
		NewLine();
4692 leency 345
		IF(opened) NewLine();
346
		return;
4411 leency 347
	}
348
	if (istag("br")) { NewLine(); return; }
5749 leency 349
	if (istag("tr")) { if (opened) NewLine(); return; }
5990 leency 350
	if (istag("b")) || (istag("strong")) || (istag("big")) { style.b = opened; return; }
5749 leency 351
	if (istag("u")) || (istag("ins")) { style.u=opened; return;}
352
	if (istag("s")) || (istag("strike")) || (istag("del")) { style.s=opened; return; }
353
	if (istag("dd")) { stolbec += 5; return; }
354
	if (istag("blockquote")) { style.blq = opened; return; }
355
	if (istag("pre")) || (istag("code")) { style.pre = opened; return; }
356
	if (istag("img")) { ImgCache.Images( left1, draw_y, WB1.list.w); return; }
6794 leency 357
	if (istag("h1")) || (istag("h2")) || (istag("h3")) || (istag("caption")) {
7208 leency 358
		style.h = opened;
5768 leency 359
		if (opened)
4491 leency 360
		{
4411 leency 361
			NewLine();
6794 leency 362
			draw_y += 10;
363
			WB1.zoom=2;
6803 leency 364
			WB1.list.font_type |= 10011001b;
6045 leency 365
			if (isattr("align=")) && (isval("center")) style.align = ALIGN_CENTER;
5746 leency 366
			if (isattr("align=")) && (isval("right")) style.align = ALIGN_RIGHT;
367
			list.item_h = basic_line_h * 2;
6794 leency 368
		}
4411 leency 369
		else
5768 leency 370
		{
4411 leency 371
			NewLine();
6794 leency 372
			WB1.zoom=1;
6803 leency 373
			WB1.list.font_type = 10011000b;
6045 leency 374
			style.align = ALIGN_LEFT;
5746 leency 375
			list.item_h = basic_line_h;
6794 leency 376
		}
4411 leency 377
		return;
378
	}
379
	if (istag("dt")) {
5749 leency 380
		style.li = opened;
5746 leency 381
		if (opened) NewLine();
5749 leency 382
		return;
4411 leency 383
	}
384
	if (istag("li")) || (istag("dt"))
5773 leency 385
	{
4411 leency 386
		style.li = opened;
5746 leency 387
		if (opened)
4491 leency 388
		{
4411 leency 389
			NewLine();
4692 leency 390
			strcpy(#line, "\31 \0");
6803 leency 391
		}
4411 leency 392
		return;
393
	}
394
	if (istag("ul")) || (istag("ol")) {
5749 leency 395
		if (!opened)
396
		{
397
			style.li = opened;
398
			style.li_tab--;
399
			NewLine();
400
		}
401
		else style.li_tab++;
402
	}
403
	if (istag("hr")) {
404
		if (isattr("color=")) EDI = GetColor(#val); else EDI = 0x999999;
5773 leency 405
		$push edi;
406
		NewLine();
4692 leency 407
		$pop edi;
5773 leency 408
		DrawBuf.DrawBar(5, draw_y - 1, list.w-10, 1, EDI);
6794 leency 409
		NewLine();
4692 leency 410
		return;
5773 leency 411
	}
4411 leency 412
	if (istag("meta")) || (istag("?xml")) {
5749 leency 413
		meta_encoding = CH_NULL;
6045 leency 414
		do{
4411 leency 415
			if (isattr("charset=")) || (isattr("content=")) || (isattr("encoding="))
5746 leency 416
			{
4411 leency 417
				strcpy(#val, #val[strrchr(#val, '=')]); //search in content=
6735 leency 418
				strlwr(#val);
5746 leency 419
				if      (isval("utf-8"))        || (isval("utf8"))        meta_encoding = CH_UTF8;
5749 leency 420
				else if (isval("koi8-r"))       || (isval("koi8-u"))      meta_encoding = CH_KOI8;
5746 leency 421
				else if (isval("windows-1251")) || (isval("windows1251")) meta_encoding = CH_CP1251;
422
				else if (isval("iso-8859-5"))   || (isval("iso8859-5"))   meta_encoding = CH_ISO8859_5;
423
				else if (isval("dos"))          || (isval("cp-866"))      meta_encoding = CH_CP866;
424
			}
4411 leency 425
		} while(GetNextParam());
426
		if (meta_encoding!=CH_NULL) BufEncode(meta_encoding);
6045 leency 427
		return;
4411 leency 428
	}
429
}
430
431
 
6803 leency 432
{
5704 leency 433
	if (cur_encoding == set_new_encoding) return;
7032 leency 434
	if (o_bufpointer==0)
5704 leency 435
	{
436
		o_bufpointer = malloc(bufsize);
437
		strcpy(o_bufpointer, bufpointer);
438
	}
439
	else
440
	{
441
		strcpy(bufpointer, o_bufpointer);
442
	}
443
	debugval("cur_encoding    ", cur_encoding);
7032 leency 444
	debugval("set_new_encoding", set_new_encoding);
445
	cur_encoding = set_new_encoding;
446
	bufpointer = ChangeCharset(charsets[cur_encoding], "CP866", bufpointer);
447
}
5704 leency 448
//============================================================================================
5747 leency 449
void TWebBrowser::DrawScroller()
4674 leency 450
{
4411 leency 451
	scroll_wv.max_area = list.count;
4416 leency 452
	scroll_wv.cur_area = list.visible;
453
	scroll_wv.position = list.first;
454
	scroll_wv.all_redraw = 0;
5519 leency 455
	scroll_wv.start_x = list.x + list.w;
4486 leency 456
	scroll_wv.start_y = list.y;
4508 leency 457
	scroll_wv.size_y = list.h;
5678 leency 458
	scrollbar_v_draw(#scroll_wv);
4416 leency 459
}
4411 leency 460
//============================================================================================
5747 leency 461
void TWebBrowser::NewLine()
4692 leency 462
{
4475 leency 463
	dword onleft, ontop;
6803 leency 464
4692 leency 465
 
6794 leency 466
467
 
468
	ontop = draw_y + list.y;
469
	if (t_html) && (!t_body) return;
4637 leency 470
	draw_y += list.item_h;
6794 leency 471
	if (style.blq) stolbec = 6; else stolbec = 0;
5746 leency 472
	if (style.li) stolbec = style.li_tab * 5;
473
}
4488 leency 474
//============================================================================================
5747 leency 475
bool istag(dword text) { if (!strcmp(#tag,text)) return true; else return false; }
6803 leency 476
bool isattr(dword text) { if (!strcmp(#attr,text)) return true; else return false; }
477
bool isval(dword text) { if (!strcmp(#val,text)) return true; else return false; }
478
//============================================================================================
5766 leency 479
void TWebBrowser::DrawPage()
480
{
481
	PutPaletteImage(list.first * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);
6794 leency 482
	DrawScroller();
5766 leency 483
}
484
>