Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
4411 leency 1
 
5749 leency 2
 
4677 leency 3
enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT};
4416 leency 4
 
5746 leency 5
struct _style {
4416 leency 6
 
5746 leency 7
	b, i, u, s,
8
	pre,
9
	blq,
10
	li,
11
	li_tab,
12
	align;
13
};
14
15
struct TWebBrowser {
16
 
4411 leency 17
	_style style;
4540 leency 18
	dword draw_line_width;
5746 leency 19
	DrawBufer DrawBuf;
5678 leency 20
	void Prepare();
4475 leency 21
	void SetStyle();
5766 leency 22
	void DrawStyle();
23
	void DrawPage();
24
	void DrawScroller();
4411 leency 25
	void LoadInternalPage();
26
	void NewLine();
5747 leency 27
	void Perenos();
4650 leency 28
	void BufEncode();
4718 leency 29
} WB1;
5749 leency 30
4686 leency 31
4486 leency 32
 
5746 leency 33
 
34
 
35
	ignor_text,
36
	cur_encoding,
37
	t_html,
38
	t_body;
39
40
dword bufpointer;
41
 
5749 leency 42
dword bufsize;
43
44
dword text_colors[300];
4411 leency 45
 
4417 leency 46
dword link_color_inactive;
47
dword link_color_active;
4491 leency 48
dword bg_color;
49
4417 leency 50
int stroka;
4411 leency 51
 
4417 leency 52
int tab_len;
53
int anchor_line_num;
54
55
char URL[10000];
4636 leency 56
 
5746 leency 57
char line[500];
58
char tagparam[10000];
4417 leency 59
char tag[100];
5746 leency 60
char attr[1200];
4417 leency 61
char val[4096];
5746 leency 62
char anchor[256];
63
4417 leency 64
#include "..\TWB\history.h"
4411 leency 65
 
4416 leency 66
#include "..\TWB\colors.h"
4686 leency 67
#include "..\TWB\unicode_tags.h"
4411 leency 68
#include "..\TWB\img_cache.h"
69
#include "..\TWB\parce_tag.h"
70
71
72
 
4475 leency 73
 
5747 leency 74
{
5766 leency 75
	int start_x, start_y, line_length, stolbec_len, body_magrin=5;
4411 leency 76
5766 leency 77
	if (!header)
4411 leency 78
 
79
		ChangeCharset("UTF-8", "CP866", #line);
80
		strcpy(#header, #line);
5718 leency 81
		strcat(#header, " -");
4411 leency 82
		strcat(#header, #version);
83
		line = 0;
84
		return;
85
	}
86
	if (t_html) && (!t_body) return;
87
4637 leency 88
	if (line) && (!anchor)
4411 leency 89
 
5766 leency 90
		start_x = stolbec * list.font_w + body_magrin * DrawBuf.zoom + list.x;
4411 leency 91
		start_y = stroka * list.line_h + body_magrin;
5766 leency 92
		stolbec_len = utf8_strlen(#line);
93
		line_length = stolbec_len * list.font_w * DrawBuf.zoom;
5712 leency 94
5710 leency 95
		WriteBufText(start_x, start_y, list.font_type, text_colors[text_color_index], #line, buf_data);
4411 leency 96
 
5766 leency 97
		if (style.i) { stolbec++; DrawBuf.Skew(start_x, start_y, line_length, list.line_h); } // bug with zoom>1
98
		if (style.s) DrawBuf.DrawBar(start_x, list.line_h / 2 - DrawBuf.zoom + start_y, line_length, DrawBuf.zoom, text_colors[text_color_index]);
99
		if (style.u) DrawBuf.DrawBar(start_x, list.line_h - DrawBuf.zoom - DrawBuf.zoom + start_y, line_length, DrawBuf.zoom, text_colors[text_color_index]);
100
		if (link) {
101
			DrawBuf.DrawBar(start_x, list.line_h - DrawBuf.zoom - DrawBuf.zoom + start_y, line_length, DrawBuf.zoom, text_colors[text_color_index]);
5519 leency 102
			UnsafeDefineButton(start_x-2, start_y-1 + list.y, line_length + 3, DrawBuf.zoom * list.font_h, PageLinks.count + 400 + BT_HIDE, 0xB5BFC9);
5766 leency 103
			PageLinks.AddText(#line, line_length, list.line_h, UNDERLINE);
104
		}
5678 leency 105
		stolbec += stolbec_len;
4411 leency 106
	}
4718 leency 107
}
4411 leency 108
//============================================================================================
109
void TWebBrowser::LoadInternalPage(dword bufpos, in_filesize){
5747 leency 110
	bufsize = in_filesize;
111
	bufpointer = bufpos;
112
	Prepare();
113
}
5766 leency 114
//============================================================================================
5747 leency 115
void TWebBrowser::Prepare(){
116
	word bukva[2];
5766 leency 117
	int j;
4411 leency 118
	byte ignor_param;
4718 leency 119
	char temp[768];
4411 leency 120
	dword bufpos = bufpointer;
121
	int line_len;
4636 leency 122
4726 leency 123
	style.b = style.i = style.u = style.s = style.blq = t_html = t_body =
4411 leency 124
 
5746 leency 125
	style.align = ALIGN_LEFT;
5749 leency 126
	link_color_inactive = 0x0000FF;
5746 leency 127
	link_color_active = 0xFF0000;
4491 leency 128
	bg_color = 0xFFFFFF;
129
	DrawBuf.Fill(bg_color);
4411 leency 130
	PageLinks.Clear();
4475 leency 131
	strcpy(#header, #version);
4504 leency 132
	stroka = -list.first;
4411 leency 133
	stolbec = 0;
4415 leency 134
	line = 0;
4411 leency 135
136
	draw_line_width = list.w * DrawBuf.zoom;
137
 
5678 leency 138
	//for plaint text use CP866 for other UTF
5519 leency 139
 
5749 leency 140
	{
141
		style.pre = 0;
142
		cur_encoding = CH_CP866;
143
		//WB1.list.SetFont(8, 14, 10111000b);
144
		//list.line_h = list.font_h + 4;
145
	}
146
	else
147
	{
148
		style.pre = 1;
149
		cur_encoding = CH_UTF8;
150
		//WB1.list.SetFont(8, 14, 10001000b);
151
		//list.line_h = list.font_h + 4;
152
	}
153
154
	for ( ; (bufpointer+bufsize > bufpos) && (ESBYTE[bufpos]!=0); bufpos++;)
4411 leency 155
 
4718 leency 156
		bukva = ESBYTE[bufpos];
4411 leency 157
		if (ignor_text) && (bukva!='<') continue;
4508 leency 158
		switch (bukva)
4417 leency 159
		{
4411 leency 160
		case 0x0a:
161
			if (style.pre)
162
			{
5746 leency 163
				chrcat(#line, ' ');
4411 leency 164
				bukva = temp = NULL;
4686 leency 165
				Perenos();
4411 leency 166
				break;
4718 leency 167
			}
4726 leency 168
		case '\9':
4411 leency 169
			if (style.pre) //иначе идём на 0x0d
170
			{
5746 leency 171
				tab_len = strlen(#line) % 4;
4411 leency 172
				if (!tab_len) tab_len = 4;
4725 leency 173
				for (j=0; j
174
				break;
4411 leency 175
			}
176
			goto DEFAULT_MARK;
177
178
		case '&': //  and so on
179
 
180
			tag=0;
4508 leency 181
			for (j=0; (ESBYTE[bufpos]<>';') && (j<7);   j++, bufpos++;)
4411 leency 182
			{
4508 leency 183
				bukva = ESBYTE[bufpos];
4411 leency 184
				chrcat(#tag, bukva);
4508 leency 185
			}
4411 leency 186
			if (bukva = GetUnicodeSymbol()) goto DEFAULT_MARK;
187
			break;
4417 leency 188
		case '<':
4411 leency 189
			bufpos++;
190
			tag = attr = tagparam = ignor_param = NULL;
5749 leency 191
			if (ESBYTE[bufpos] == '!') //фильтрация внутри , дерзко
5746 leency 192
			{
4508 leency 193
				bufpos++;
4411 leency 194
				if (ESBYTE[bufpos] == '-')
4508 leency 195
				{
196
				HH_:
4411 leency 197
					do
198
					{
199
						bufpos++;
200
						if (bufpointer + bufsize <= bufpos) break 2;
4508 leency 201
					}
4550 leency 202
					while (ESBYTE[bufpos] <>'-');
4411 leency 203
4508 leency 204
					bufpos++;
4411 leency 205
 
4508 leency 206
				}
207
			}
4411 leency 208
			while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //получаем тег и его параметры
209
			{
4550 leency 210
				bukva = ESBYTE[bufpos];
4411 leency 211
				if (bukva == '\9') || (bukva == '\x0a') || (bukva == '\x0d') bukva = ' ';
4508 leency 212
				if (!ignor_param) && (bukva <>' ')
4411 leency 213
				{
214
					if (strlen(#tag)
215
				}
4726 leency 216
				else
4411 leency 217
				{
218
					ignor_param = true;
219
					if (!ignor_text) && (strlen(#tagparam)+1
220
				}
221
				bufpos++;
222
			}
4508 leency 223
			strlwr(#tag);
4411 leency 224
225
			if (tag[strlen(#tag)-1]=='/') tag[strlen(#tag)-1]=NULL; //for br/
226
 
227
4726 leency 228
			if (stolbec + utf8_strlen(#line) > list.column_max) Perenos();
4411 leency 229
 
5712 leency 230
			line = NULL;
5766 leency 231
			if (tag) SetStyle(WB1.DrawBuf.zoom * 5 + list.x, stroka * list.line_h + list.y + 5); //обработка тегов
4677 leency 232
			tag = attr = tagparam = ignor_param = NULL;
5766 leency 233
			break;
5746 leency 234
		default:
4411 leency 235
			DEFAULT_MARK:
236
			if (bukva<=15) bukva=' ';
237
			line_len = utf8_strlen(#line);
238
			if (!style.pre) && (bukva == ' ')
5712 leency 239
			{
5746 leency 240
				if (line[line_len-1]==' ') break; //no double spaces
4411 leency 241
				if (!stolbec) && (!line) break; //no paces at the beginning of the line
4726 leency 242
			}
243
			if (line_len < sizeof(line)) chrcat(#line, bukva);
4411 leency 244
			if (stolbec + line_len > list.column_max) Perenos();
4726 leency 245
		}
246
	}
4411 leency 247
	DrawStyle();
248
	NewLine();
5766 leency 249
	DrawPage();
250
	if (list.first == 0) list.count = stroka;
4692 leency 251
	if (anchor) //если посреди текста появится новый якорь - будет бесконечный цикл
4415 leency 252
	{
4411 leency 253
		anchor=NULL;
254
		list.first=anchor_line_num;
255
		Prepare();
4415 leency 256
	}
5766 leency 257
}
4411 leency 258
//============================================================================================
259
void TWebBrowser::Perenos()
5747 leency 260
{
4718 leency 261
	int perenos_num;
262
	char new_line_text[4096];
263
	perenos_num = strrchr(#line, ' ');
264
	if (!perenos_num) && (utf8_strlen(#line)>list.column_max) perenos_num=list.column_max;
265
	strcpy(#new_line_text, #line + perenos_num);
5712 leency 266
	line[perenos_num] = 0x00;
4718 leency 267
	DrawStyle();
268
	strcpy(#line, #new_line_text);
5766 leency 269
	NewLine();
4718 leency 270
}
271
//============================================================================================
272
char oldtag[100];
5747 leency 273
void TWebBrowser::SetStyle(int left1, top1) {
4411 leency 274
	dword hr_color;
5766 leency 275
	byte opened;
4411 leency 276
	byte meta_encoding;
4491 leency 277
	//проверяем тег открывается или закрывается
4636 leency 278
	if (tag[0] == '/')
4411 leency 279
	{
280
		 opened = 0;
281
		 strcpy(#tag, #tag+1);
4491 leency 282
	}
4411 leency 283
	else opened = 1;
284
4491 leency 285
	if (istag("html")) {
5749 leency 286
 
287
		return;
4637 leency 288
	}
4411 leency 289
	if (istag("script")) || (istag("style")) || (istag("binary")) || (istag("select")) { ignor_text = opened; return; }
290
	if (istag("form")) if (!opened) ignor_text = false;
5749 leency 291
	if(istag("title")) {
5746 leency 292
		if (opened) header=NULL;
5749 leency 293
		else if (!stroka) DrawTitle(#header); //тег закрылся - вывели строку
4491 leency 294
		return;
4411 leency 295
	}
296
	if (ignor_text) return;
297
298
	IF(istag("q"))
299
 
5746 leency 300
		if (opened)	strcat(#line, " \"");
4491 leency 301
		if (!opened) strcat(#line, "\" ");
5746 leency 302
		return;
303
	}
5749 leency 304
	if (anchor) && (isattr("id=")) { //очень плохо!!! потому что если не последний тег, работать не будет
4491 leency 305
		if (!strcmp(#anchor, #val))	anchor_line_num=list.first+stroka;
5749 leency 306
	}
5746 leency 307
	if (istag("body")) {
5749 leency 308
		t_body = opened;
309
		do{
4637 leency 310
			if (isattr("link=")) link_color_inactive = GetColor(#val);
4411 leency 311
			if (isattr("alink=")) link_color_active = GetColor(#val);
5746 leency 312
			if (isattr("text=")) text_colors[0]=GetColor(#val);
313
			if (isattr("bgcolor="))
314
			{
315
				bg_color=GetColor(#val);
4411 leency 316
				DrawBuf.Fill(bg_color);
5746 leency 317
			}
4475 leency 318
		} while(GetNextParam());
4411 leency 319
		if (opened) && (cur_encoding==CH_NULL) debugln("Document has no information about encoding, UTF will be used");
320
		return;
5749 leency 321
	}
4411 leency 322
	if (istag("a")) {
323
		if (opened)
5749 leency 324
		{
4491 leency 325
			if (link) IF(text_color_index > 0) text_color_index--; //если предыдущий тег а не был закрыт
4411 leency 326
			do{
327
				if (isattr("href="))
328
				{
5746 leency 329
					if (stroka - 1 > list.visible) || (stroka < -2) return;
4411 leency 330
					text_color_index++;
4415 leency 331
					text_colors[text_color_index] = text_colors[text_color_index-1];
4411 leency 332
					link = 1;
333
					text_colors[text_color_index] = link_color_inactive;
334
					PageLinks.AddLink(#val, DrawBuf.zoom * stolbec * list.font_w + left1, top1-DrawBuf.zoom);
4491 leency 335
				}
5746 leency 336
				if (anchor) && (isattr("name="))
4411 leency 337
				{
5746 leency 338
					if (!strcmp(#anchor, #val))
4411 leency 339
					{
5746 leency 340
						anchor_line_num=list.first+stroka;
4411 leency 341
					}
4415 leency 342
				}
4411 leency 343
			} while(GetNextParam());
344
		}
345
		else {
346
			link = 0;
347
			IF(text_color_index > 0) text_color_index--;
348
		}
349
		return;
350
	}
351
	if (istag("font")) {
352
		if (opened)
5749 leency 353
		{
4491 leency 354
			text_color_index++;
4411 leency 355
			text_colors[text_color_index] = text_colors[text_color_index-1];
356
			do{
357
				if (isattr("color=")) text_colors[text_color_index] = GetColor(#val);
358
			} while(GetNextParam());
5749 leency 359
		}
4411 leency 360
		else if (text_color_index > 0) text_color_index--;
361
		return;
5749 leency 362
	}
4411 leency 363
	if (istag("div")) || (istag("header")) || (istag("article")) || (istag("footer")) {
364
		IF(oldtag[0] != 'h') NewLine();
5746 leency 365
		if (isattr("bgcolor="))
5749 leency 366
		{
5746 leency 367
			bg_color=GetColor(#val);
5493 leency 368
			DrawBuf.Fill(bg_color);
5746 leency 369
		}
5493 leency 370
		return;
371
	}
4411 leency 372
	if (istag("p")) {
373
		IF(oldtag[0] == 'h') return;
5746 leency 374
		NewLine();
4411 leency 375
		IF(opened) NewLine();
4692 leency 376
		return;
377
	}
4411 leency 378
	if (istag("br")) { NewLine(); return; }
379
	if (istag("tr")) { if (opened) { NewLine(); strcat(#line, "| "); } return; }
5749 leency 380
	if (istag("td")) || (istag("th")) { if (!opened) strcat(#line, " | "); return; }
381
	if (istag("b")) || (istag("strong")) || (istag("big")) { style.b = opened; return; }
382
	if (istag("i")) || (istag("em")) || (istag("subtitle")) { style.i=opened; return; }
383
	if (istag("u")) || (istag("ins")) { style.u=opened; return;}
384
	if (istag("s")) || (istag("strike")) || (istag("del")) { style.s=opened; return; }
385
	if (istag("dd")) { stolbec += 5; return; }
386
	if (istag("blockquote")) { style.blq = opened; return; }
387
	if (istag("pre")) || (istag("code")) { style.pre = opened; return; }
388
	if (istag("img")) { ImgCache.Images( left1, top1, WB1.list.w); return; }
389
	/*
390
	if (istag("center"))
4475 leency 391
	{
5746 leency 392
		if (opened) style.align = ALIGN_CENTER;
4411 leency 393
		if (!opened)
5746 leency 394
		{
4491 leency 395
			NewLine();
4411 leency 396
			style.align = ALIGN_LEFT;
4692 leency 397
		}
5746 leency 398
		return;
4411 leency 399
	}
400
	if (istag("right"))
401
	{
5746 leency 402
		if (opened) style.align = ALIGN_RIGHT;
4411 leency 403
		if (!opened)
5746 leency 404
		{
4491 leency 405
			NewLine();
4411 leency 406
			style.align = ALIGN_LEFT;
4692 leency 407
		}
5746 leency 408
		return;
4411 leency 409
	}
410
	*/
411
	if (istag("h1")) || (istag("h2")) || (istag("h3")) || (istag("h4")) || (istag("caption")) {
4475 leency 412
		NewLine();
5749 leency 413
		if (opened) && (stroka>1) NewLine();
4692 leency 414
		strcpy(#oldtag, #tag);
415
		if (opened)
4411 leency 416
		{
4491 leency 417
			if (isattr("align=")) && (isval("center")) style.align = ALIGN_CENTER;
4411 leency 418
			if (isattr("align=")) && (isval("right")) style.align = ALIGN_RIGHT;
5746 leency 419
			style.b = 1;
420
		}
421
		if (!opened)
4411 leency 422
		{
4491 leency 423
			style.align = ALIGN_LEFT;
4411 leency 424
			style.b = 0;
5746 leency 425
		}
426
		return;
4411 leency 427
	}
428
	else
429
		oldtag=NULL;
430
	if (istag("dt")) {
431
		style.li = opened;
5749 leency 432
		if (opened) NewLine();
5746 leency 433
		return;
5749 leency 434
	}
4411 leency 435
	if (istag("li")) || (istag("dt")) //надо сделать вложенные списки
436
	{
5746 leency 437
		style.li = opened;
4411 leency 438
		if (opened)
5746 leency 439
		{
4491 leency 440
			NewLine();
4411 leency 441
			if (stroka > -1) && (stroka - 2 < list.visible)
4692 leency 442
				DrawBuf.DrawBar(style.li_tab * 5 * list.font_w * DrawBuf.zoom + list.x, list.line_h
5678 leency 443
					/ 2 - DrawBuf.zoom - DrawBuf.zoom, DrawBuf.zoom*2, DrawBuf.zoom*2, 0x555555);
5747 leency 444
		}
445
		return;
4411 leency 446
	}
447
	if (istag("ul")) || (istag("ol")) {
448
		if (!opened)
5749 leency 449
		{
450
			style.li = opened;
451
			style.li_tab--;
452
			NewLine();
453
		}
454
		else style.li_tab++;
455
	}
456
	if (istag("hr")) {
457
		if (anchor) || (stroka < -1)
458
		{
4411 leency 459
			stroka+=2;
460
			return;
461
		}
462
		if (strcmp(#attr, "color=") == 0) hr_color = GetColor(#val); else hr_color = 0x999999;
463
		NewLine();
5746 leency 464
		DrawBuf.DrawBar(5, list.line_h/2, list.w-10, 1, hr_color);
4692 leency 465
		NewLine();
4486 leency 466
	}
4692 leency 467
	if (istag("meta")) || (istag("?xml")) {
4411 leency 468
		do{
5749 leency 469
			if (isattr("charset=")) || (isattr("content=")) || (isattr("encoding="))
4411 leency 470
			{
5746 leency 471
				strcpy(#val, #val[strrchr(#val, '=')]); //поиск в content=
4411 leency 472
				strlwr(#val);
5746 leency 473
				if      (isval("utf-8"))        || (isval("utf8"))        meta_encoding = CH_UTF8;
474
				else if (isval("koi8-r"))       || (isval("koi8-u"))      meta_encoding = CH_KOI8;
5749 leency 475
				else if (isval("windows-1251")) || (isval("windows1251")) meta_encoding = CH_CP1251;
5746 leency 476
				else if (isval("iso-8859-5"))   || (isval("iso8859-5"))   meta_encoding = CH_ISO8859_5;
477
				else if (isval("dos"))          || (isval("cp-866"))      meta_encoding = CH_CP866;
478
				if (cur_encoding!=meta_encoding) BufEncode(meta_encoding);
479
				return;
5749 leency 480
			}
4636 leency 481
		} while(GetNextParam());
4411 leency 482
		return;
483
	}
484
}
485
486
void TWebBrowser::BufEncode(int set_new_encoding)
487
 
5749 leency 488
	int bufpointer_realsize;
5704 leency 489
	cur_encoding = set_new_encoding;
490
	if (o_bufpointer==0)
491
	{
492
		o_bufpointer = malloc(bufsize);
493
		strcpy(o_bufpointer, bufpointer);
494
	}
495
	else
496
	{
497
		strcpy(bufpointer, o_bufpointer);
498
	}
499
}
500
//============================================================================================
501
void TWebBrowser::DrawScroller()
5747 leency 502
{
4674 leency 503
	scroll_wv.max_area = list.count;
4411 leency 504
	scroll_wv.cur_area = list.visible;
4416 leency 505
	scroll_wv.position = list.first;
506
507
	scroll_wv.all_redraw = 0;
4411 leency 508
 
5519 leency 509
	scroll_wv.start_y = list.y;
4486 leency 510
4508 leency 511
	scroll_wv.size_y = list.h;
4411 leency 512
 
5678 leency 513
514
	scrollbar_v_draw(#scroll_wv);
5519 leency 515
 
4416 leency 516
//============================================================================================
4411 leency 517
void TWebBrowser::NewLine()
5747 leency 518
{
4692 leency 519
	int onleft, ontop;
4475 leency 520
5678 leency 521
	onleft = list.x + 5;
4692 leency 522
 
523
	if (t_html) && (!t_body) return;
524
	if (stroka * list.line_h + 5 >= 0) && ( stroka + 1 * list.line_h + 5 < list.h) && (!anchor)
4637 leency 525
	{
5678 leency 526
		if (style.align == ALIGN_CENTER) && (DrawBuf.zoom==1) DrawBuf.AlignCenter(onleft,ontop,list.w,list.line_h,stolbec * list.font_w);
4475 leency 527
		if (style.align == ALIGN_RIGHT) && (DrawBuf.zoom==1)  DrawBuf.AlignRight(onleft,ontop,list.w,list.line_h,stolbec * list.font_w);
5746 leency 528
	}
529
	stroka++;
4475 leency 530
	if (style.blq) stolbec = 6; else stolbec = 0;
531
	if (style.li) stolbec = style.li_tab * 5;
5746 leency 532
}
533
//============================================================================================
4488 leency 534
int istag(dword text) { if (!strcmp(#tag,text)) return 1; else return 0; }
5747 leency 535
int isattr(dword text) { if (!strcmp(#attr,text)) return 1; else return 0; }
5746 leency 536
int isval(dword text) { if (!strcmp(#val,text)) return 1; else return 0; }
537
//============================================================================================
538
void TWebBrowser::DrawPage()
5766 leency 539
{
540
	PutPaletteImage(list.first * list.line_h * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);
541
	DrawScroller();
542
}
543
>
544