Subversion Repositories Kolibri OS

Rev

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