Subversion Repositories Kolibri OS

Rev

Rev 8500 | Rev 9103 | 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
 
8439 leency 15
struct STYLE {
7752 leency 16
	bool
6794 leency 17
	b, u, s, h,
8016 leency 18
	font,
5746 leency 19
	pre,
20
	blq,
7742 leency 21
	button,
7759 leency 22
	image;
23
	LIST tag_list;
8439 leency 24
	dword title;
8451 leency 25
	dword cur_line_h;
8439 leency 26
	void reset();
5746 leency 27
};
28
 
8439 leency 29
void STYLE::reset()
30
{
31
	b = u = s = h = blq = pre = title = false;
32
	font = false;
8451 leency 33
	cur_line_h = NULL;
8439 leency 34
	tag_list.reset();
35
}
36
 
4411 leency 37
struct TWebBrowser {
4540 leency 38
	llist list;
8439 leency 39
	STYLE style;
8462 leency 40
	dword draw_y, draw_x, draw_w, left_gap;
7752 leency 41
	dword o_bufpointer;
7759 leency 42
	int cur_encoding, custom_encoding;
43
	bool link, t_html, t_body;
44
	dword bufpointer;
45
	dword bufsize;
7945 leency 46
	dword is_html;
8396 leency 47
	collection img_url;
8439 leency 48
	char header[150];
8492 leency 49
	char linebuf[500];
8439 leency 50
	char redirect[URL_SIZE];
7759 leency 51
 
8462 leency 52
	void SetStyle();
8491 leency 53
	void RenderTextbuf();
54
	void RenderLine();
8462 leency 55
	bool RenderImage();
56
 
7746 leency 57
	void SetPageDefaults();
7752 leency 58
	void ParseHtml();
8500 leency 59
	void Reparse();
7762 leency 60
	void NewLine();
61
	void ChangeEncoding();
8491 leency 62
	void AddCharToTheLine();
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();
8492 leency 80
	void tag_table_reset();
8462 leency 81
	void tag_table();
82
	void tag_td();
83
	void tag_tr();
7759 leency 84
};
4486 leency 85
 
8462 leency 86
#include "TWB\render.h"
8439 leency 87
#include "TWB\set_style.h"
5747 leency 88
//============================================================================================
7746 leency 89
void TWebBrowser::SetPageDefaults()
90
{
8439 leency 91
	t_html = t_body = link = false;
92
	style.reset();
7759 leency 93
	link_color_default = 0x0000FF;
4491 leency 94
	link_color_active = 0xFF0000;
8454 leency 95
	style.cur_line_h = list.item_h;
7970 leency 96
	links.clear();
7752 leency 97
	anchors.clear();
8396 leency 98
	img_url.drop();
8016 leency 99
	text_colors.drop();
100
	text_colors.add(0);
8451 leency 101
	bg_colors.drop();
102
	bg_colors.add(DEFAULT_BG_COL);
103
	canvas.Fill(0, DEFAULT_BG_COL);
7758 leency 104
	header = NULL;
7759 leency 105
	draw_y = BODY_MARGIN;
8462 leency 106
	draw_x = left_gap = BODY_MARGIN;
107
	draw_w = list.w - BODY_MARGIN;
8492 leency 108
	linebuf = 0;
8302 leency 109
	redirect = '\0';
8490 leency 110
	list.SetFont(8, 14, 10011000b);
8492 leency 111
	tag_table_reset();
7746 leency 112
}
113
//============================================================================================
8500 leency 114
void TWebBrowser::Reparse()
115
{
116
	ParseHtml(bufpointer, bufsize);
117
}
118
//============================================================================================
7759 leency 119
void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
7752 leency 120
	int tab_len;
7746 leency 121
	dword bufpos;
7759 leency 122
	bufsize = _bufsize;
8439 leency 123
 
8457 leency 124
	if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
125
 
8492 leency 126
	if (bufpointer == _bufpointer) {
127
		custom_encoding = cur_encoding;
128
	} else {
7771 leency 129
		bufpointer = malloc(bufsize);
130
		memmov(bufpointer, _bufpointer, bufsize);
8492 leency 131
 
132
		//hold original buffer
133
		o_bufpointer = malloc(bufsize);
134
		memmov(o_bufpointer, bufpointer, bufsize);
135
 
136
		cur_encoding = CH_CP866;
137
		if (custom_encoding != -1) {
138
			cur_encoding = custom_encoding;
139
			bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
140
			bufsize = strlen(bufpointer);
141
		}
7771 leency 142
	}
8492 leency 143
 
144
 
7746 leency 145
	SetPageDefaults();
7945 leency 146
	is_html = true;
147
	if (!strstri(bufpointer, "
7749 leency 148
		t_body = true;
8572 leency 149
		if (!strstri(bufpointer, "
150
		&& (!strstr(bufpointer, "
7945 leency 151
			style.pre = true; //show linebreaks for a plaint text
152
			is_html = false;
153
		}
7749 leency 154
	}
5773 leency 155
	for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
4411 leency 156
	{
8330 leency 157
		switch (ESBYTE[bufpos])
4411 leency 158
		{
159
		case 0x0a:
7752 leency 160
			if (style.pre) {
8491 leency 161
				RenderTextbuf();
5781 leency 162
				NewLine();
7752 leency 163
			} else {
8491 leency 164
				goto _DEFAULT;
4411 leency 165
			}
7752 leency 166
			break;
167
		case 0x09:
168
			if (style.pre) {
8492 leency 169
				tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4;
7752 leency 170
				if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
8492 leency 171
				while (tab_len) {chrcat(#linebuf,' '); tab_len--;}
7752 leency 172
			} else {
8491 leency 173
				goto _DEFAULT;
4411 leency 174
			}
7752 leency 175
			break;
4411 leency 176
		case '&': //  and so on
8492 leency 177
			bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize);
4411 leency 178
			break;
179
		case '<':
8330 leency 180
			if (!is_html) goto _DEFAULT;
8490 leency 181
			if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT;
5749 leency 182
			bufpos++;
8440 leency 183
			if (tag.parse(#bufpos, bufpointer + bufsize)) {
8491 leency 184
				RenderTextbuf();
8439 leency 185
				$push cur_encoding
186
				SetStyle();
187
				$pop eax
188
				// The thing is that UTF if longer than other encodings.
189
				// So if encoding was changed from UTF to DOS than $bufpos position got wrong,
190
				// and we have to start parse from the very beginning
191
				if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
8492 leency 192
					ParseHtml(bufpointer, strlen(bufpointer));
8439 leency 193
					return;
8407 leency 194
				}
7746 leency 195
			}
4411 leency 196
			break;
197
		default:
8330 leency 198
			_DEFAULT:
7752 leency 199
			AddCharToTheLine(ESBYTE[bufpos]);
4411 leency 200
		}
201
	}
8491 leency 202
	RenderTextbuf();
203
	list.count = draw_y + style.cur_line_h;
8425 leency 204
 
8491 leency 205
	canvas.bufh = math.max(list.visible, list.count);
8439 leency 206
	buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
8425 leency 207
 
7755 leency 208
	list.CheckDoesValuesOkey();
209
	anchors.current = NULL;
7758 leency 210
	if (!header) {
7759 leency 211
		strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
7758 leency 212
		DrawTitle(#header);
213
	}
4411 leency 214
}
5747 leency 215
//============================================================================================
8444 leency 216
void TWebBrowser::AddCharToTheLine(unsigned char _char)
217
{
8492 leency 218
	dword line_len = strlen(#linebuf);
8444 leency 219
	if (_char<=15) _char=' ';
220
	if (!style.pre) && (_char == ' ')
221
	{
8492 leency 222
		if (linebuf[line_len-1]==' ') return; //no double spaces
223
		if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line
8444 leency 224
		if (link) && (line_len==0) return;
225
	}
8499 leency 226
	if (line_len < sizeof(TWebBrowser.linebuf)) {
227
		chrcat(#linebuf+line_len, _char);
228
	} else {
229
		RenderTextbuf();
230
	}
8444 leency 231
}
232
//============================================================================================
233
void TWebBrowser::ChangeEncoding(int _new_encoding)
234
{
235
	if (cur_encoding == _new_encoding) return;
236
	cur_encoding = _new_encoding;
237
	bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
238
	if (header) {
239
		ChangeCharset(cur_encoding, "CP866", #header);
240
		DrawTitle(#header);
241
	}
242
}
243
//============================================================================================
8491 leency 244
scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
245
  0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
8439 leency 246
 
5766 leency 247
void TWebBrowser::DrawPage()
248
{
8500 leency 249
	if (list.w!=canvas.bufw) Reparse();
8457 leency 250
	canvas.Show(list.first, list.h);
251
 
8439 leency 252
	scroll_wv.max_area = list.count;
253
	scroll_wv.cur_area = list.visible;
254
	scroll_wv.position = list.first;
255
	scroll_wv.all_redraw = 0;
256
	scroll_wv.start_x = list.x + list.w;
257
	scroll_wv.start_y = list.y;
258
	scroll_wv.size_y = list.h;
259
 
8490 leency 260
	if (list.count <= list.visible) {
261
		DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
262
		scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
263
	} else {
264
		scrollbar_v_draw(#scroll_wv);
265
	}
5766 leency 266
}