Rev 8492 | Rev 8500 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8492 | Rev 8499 | ||
---|---|---|---|
1 | #include "TWB\colors.h" |
1 | #include "TWB\colors.h" |
2 | #include "TWB\anchors.h" |
2 | #include "TWB\anchors.h" |
3 | #include "TWB\parse_tag.h" |
3 | #include "TWB\parse_tag.h" |
4 | #include "TWB\special.h" |
4 | #include "TWB\special.h" |
5 | #include "TWB\tag_list.h" |
5 | #include "TWB\tag_list.h" |
6 | #define DEFAULT_BG_COL 0xffEBE8E9; |
6 | #define DEFAULT_BG_COL 0xffEBE8E9; |
7 | dword link_color_default; |
7 | dword link_color_default; |
8 | dword link_color_active; |
8 | dword link_color_active; |
9 | #include "TWB\links.h" |
9 | #include "TWB\links.h" |
10 | 10 | ||
11 | #define BODY_MARGIN 6 |
11 | #define BODY_MARGIN 6 |
12 | #define BASIC_CHAR_W 8 |
12 | #define BASIC_CHAR_W 8 |
13 | #define BASIC_LINE_H 18 |
13 | #define BASIC_LINE_H 18 |
14 | 14 | ||
15 | struct STYLE { |
15 | struct STYLE { |
16 | bool |
16 | bool |
17 | b, u, s, h, |
17 | b, u, s, h, |
18 | font, |
18 | font, |
19 | pre, |
19 | pre, |
20 | blq, |
20 | blq, |
21 | button, |
21 | button, |
22 | image; |
22 | image; |
23 | LIST tag_list; |
23 | LIST tag_list; |
24 | dword title; |
24 | dword title; |
25 | dword cur_line_h; |
25 | dword cur_line_h; |
26 | void reset(); |
26 | void reset(); |
27 | }; |
27 | }; |
28 | 28 | ||
29 | void STYLE::reset() |
29 | void STYLE::reset() |
30 | { |
30 | { |
31 | b = u = s = h = blq = pre = title = false; |
31 | b = u = s = h = blq = pre = title = false; |
32 | font = false; |
32 | font = false; |
33 | cur_line_h = NULL; |
33 | cur_line_h = NULL; |
34 | tag_list.reset(); |
34 | tag_list.reset(); |
35 | } |
35 | } |
36 | 36 | ||
37 | struct TWebBrowser { |
37 | struct TWebBrowser { |
38 | llist list; |
38 | llist list; |
39 | STYLE style; |
39 | STYLE style; |
40 | dword draw_y, draw_x, draw_w, left_gap; |
40 | dword draw_y, draw_x, draw_w, left_gap; |
41 | dword o_bufpointer; |
41 | dword o_bufpointer; |
42 | int cur_encoding, custom_encoding; |
42 | int cur_encoding, custom_encoding; |
43 | bool link, t_html, t_body; |
43 | bool link, t_html, t_body; |
44 | dword bufpointer; |
44 | dword bufpointer; |
45 | dword bufsize; |
45 | dword bufsize; |
46 | dword is_html; |
46 | dword is_html; |
47 | collection img_url; |
47 | collection img_url; |
48 | char header[150]; |
48 | char header[150]; |
49 | char linebuf[500]; |
49 | char linebuf[500]; |
50 | char redirect[URL_SIZE]; |
50 | char redirect[URL_SIZE]; |
51 | 51 | ||
52 | void SetStyle(); |
52 | void SetStyle(); |
53 | void RenderTextbuf(); |
53 | void RenderTextbuf(); |
54 | void RenderLine(); |
54 | void RenderLine(); |
55 | bool RenderImage(); |
55 | bool RenderImage(); |
56 | 56 | ||
57 | void SetPageDefaults(); |
57 | void SetPageDefaults(); |
58 | void ParseHtml(); |
58 | void ParseHtml(); |
59 | void NewLine(); |
59 | void NewLine(); |
60 | void ChangeEncoding(); |
60 | void ChangeEncoding(); |
61 | void AddCharToTheLine(); |
61 | void AddCharToTheLine(); |
62 | void DrawPage(); |
62 | void DrawPage(); |
63 | 63 | ||
64 | void tag_a(); |
64 | void tag_a(); |
65 | void tag_p(); |
65 | void tag_p(); |
66 | void tag_img(); |
66 | void tag_img(); |
67 | void tag_div(); |
67 | void tag_div(); |
68 | void tag_h1234_caption(); |
68 | void tag_h1234_caption(); |
69 | void tag_ol_ul_dt(); |
69 | void tag_ol_ul_dt(); |
70 | void tag_li(); |
70 | void tag_li(); |
71 | void tag_q(); |
71 | void tag_q(); |
72 | void tag_hr(); |
72 | void tag_hr(); |
73 | void tag_code(); |
73 | void tag_code(); |
74 | void tag_meta_xml(); |
74 | void tag_meta_xml(); |
75 | void tag_body(); |
75 | void tag_body(); |
76 | void tag_iframe(); |
76 | void tag_iframe(); |
77 | void tag_title(); |
77 | void tag_title(); |
78 | void tag_font(); |
78 | void tag_font(); |
79 | void tag_table_reset(); |
79 | void tag_table_reset(); |
80 | void tag_table(); |
80 | void tag_table(); |
81 | void tag_td(); |
81 | void tag_td(); |
82 | void tag_tr(); |
82 | void tag_tr(); |
83 | }; |
83 | }; |
84 | 84 | ||
85 | #include "TWB\render.h" |
85 | #include "TWB\render.h" |
86 | #include "TWB\set_style.h" |
86 | #include "TWB\set_style.h" |
87 | //============================================================================================ |
87 | //============================================================================================ |
88 | void TWebBrowser::SetPageDefaults() |
88 | void TWebBrowser::SetPageDefaults() |
89 | { |
89 | { |
90 | t_html = t_body = link = false; |
90 | t_html = t_body = link = false; |
91 | style.reset(); |
91 | style.reset(); |
92 | link_color_default = 0x0000FF; |
92 | link_color_default = 0x0000FF; |
93 | link_color_active = 0xFF0000; |
93 | link_color_active = 0xFF0000; |
94 | style.cur_line_h = list.item_h; |
94 | style.cur_line_h = list.item_h; |
95 | links.clear(); |
95 | links.clear(); |
96 | anchors.clear(); |
96 | anchors.clear(); |
97 | img_url.drop(); |
97 | img_url.drop(); |
98 | text_colors.drop(); |
98 | text_colors.drop(); |
99 | text_colors.add(0); |
99 | text_colors.add(0); |
100 | bg_colors.drop(); |
100 | bg_colors.drop(); |
101 | bg_colors.add(DEFAULT_BG_COL); |
101 | bg_colors.add(DEFAULT_BG_COL); |
102 | canvas.Fill(0, DEFAULT_BG_COL); |
102 | canvas.Fill(0, DEFAULT_BG_COL); |
103 | header = NULL; |
103 | header = NULL; |
104 | draw_y = BODY_MARGIN; |
104 | draw_y = BODY_MARGIN; |
105 | draw_x = left_gap = BODY_MARGIN; |
105 | draw_x = left_gap = BODY_MARGIN; |
106 | draw_w = list.w - BODY_MARGIN; |
106 | draw_w = list.w - BODY_MARGIN; |
107 | linebuf = 0; |
107 | linebuf = 0; |
108 | redirect = '\0'; |
108 | redirect = '\0'; |
109 | list.SetFont(8, 14, 10011000b); |
109 | list.SetFont(8, 14, 10011000b); |
110 | tag_table_reset(); |
110 | tag_table_reset(); |
111 | } |
111 | } |
112 | //============================================================================================ |
112 | //============================================================================================ |
113 | void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){ |
113 | void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){ |
114 | int tab_len; |
114 | int tab_len; |
115 | dword bufpos; |
115 | dword bufpos; |
116 | bufsize = _bufsize; |
116 | bufsize = _bufsize; |
117 | 117 | ||
118 | if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20); |
118 | if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20); |
119 | 119 | ||
120 | if (bufpointer == _bufpointer) { |
120 | if (bufpointer == _bufpointer) { |
121 | custom_encoding = cur_encoding; |
121 | custom_encoding = cur_encoding; |
122 | } else { |
122 | } else { |
123 | bufpointer = malloc(bufsize); |
123 | bufpointer = malloc(bufsize); |
124 | memmov(bufpointer, _bufpointer, bufsize); |
124 | memmov(bufpointer, _bufpointer, bufsize); |
125 | 125 | ||
126 | //hold original buffer |
126 | //hold original buffer |
127 | o_bufpointer = malloc(bufsize); |
127 | o_bufpointer = malloc(bufsize); |
128 | memmov(o_bufpointer, bufpointer, bufsize); |
128 | memmov(o_bufpointer, bufpointer, bufsize); |
129 | 129 | ||
130 | cur_encoding = CH_CP866; |
130 | cur_encoding = CH_CP866; |
131 | if (custom_encoding != -1) { |
131 | if (custom_encoding != -1) { |
132 | cur_encoding = custom_encoding; |
132 | cur_encoding = custom_encoding; |
133 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
133 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
134 | bufsize = strlen(bufpointer); |
134 | bufsize = strlen(bufpointer); |
135 | } |
135 | } |
136 | } |
136 | } |
137 | 137 | ||
138 | 138 | ||
139 | SetPageDefaults(); |
139 | SetPageDefaults(); |
140 | is_html = true; |
140 | is_html = true; |
141 | if (!strstri(bufpointer, " |
141 | if (!strstri(bufpointer, " |
142 | t_body = true; |
142 | t_body = true; |
143 | if (!strstri(bufpointer, " |
143 | if (!strstri(bufpointer, " |
144 | style.pre = true; //show linebreaks for a plaint text |
144 | style.pre = true; //show linebreaks for a plaint text |
145 | is_html = false; |
145 | is_html = false; |
146 | } |
146 | } |
147 | } |
147 | } |
148 | for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;) |
148 | for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;) |
149 | { |
149 | { |
150 | switch (ESBYTE[bufpos]) |
150 | switch (ESBYTE[bufpos]) |
151 | { |
151 | { |
152 | case 0x0a: |
152 | case 0x0a: |
153 | if (style.pre) { |
153 | if (style.pre) { |
154 | RenderTextbuf(); |
154 | RenderTextbuf(); |
155 | NewLine(); |
155 | NewLine(); |
156 | } else { |
156 | } else { |
157 | goto _DEFAULT; |
157 | goto _DEFAULT; |
158 | } |
158 | } |
159 | break; |
159 | break; |
160 | case 0x09: |
160 | case 0x09: |
161 | if (style.pre) { |
161 | if (style.pre) { |
162 | tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4; |
162 | tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4; |
163 | if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len; |
163 | if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len; |
164 | while (tab_len) {chrcat(#linebuf,' '); tab_len--;} |
164 | while (tab_len) {chrcat(#linebuf,' '); tab_len--;} |
165 | } else { |
165 | } else { |
166 | goto _DEFAULT; |
166 | goto _DEFAULT; |
167 | } |
167 | } |
168 | break; |
168 | break; |
169 | case '&': // and so on |
169 | case '&': // and so on |
170 | bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize); |
170 | bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize); |
171 | break; |
171 | break; |
172 | case '<': |
172 | case '<': |
173 | if (!is_html) goto _DEFAULT; |
173 | if (!is_html) goto _DEFAULT; |
174 | if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT; |
174 | if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT; |
175 | bufpos++; |
175 | bufpos++; |
176 | if (tag.parse(#bufpos, bufpointer + bufsize)) { |
176 | if (tag.parse(#bufpos, bufpointer + bufsize)) { |
177 | RenderTextbuf(); |
177 | RenderTextbuf(); |
178 | $push cur_encoding |
178 | $push cur_encoding |
179 | SetStyle(); |
179 | SetStyle(); |
180 | $pop eax |
180 | $pop eax |
181 | // The thing is that UTF if longer than other encodings. |
181 | // The thing is that UTF if longer than other encodings. |
182 | // So if encoding was changed from UTF to DOS than $bufpos position got wrong, |
182 | // So if encoding was changed from UTF to DOS than $bufpos position got wrong, |
183 | // and we have to start parse from the very beginning |
183 | // and we have to start parse from the very beginning |
184 | if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) { |
184 | if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) { |
185 | ParseHtml(bufpointer, strlen(bufpointer)); |
185 | ParseHtml(bufpointer, strlen(bufpointer)); |
186 | return; |
186 | return; |
187 | } |
187 | } |
188 | } |
188 | } |
189 | break; |
189 | break; |
190 | default: |
190 | default: |
191 | _DEFAULT: |
191 | _DEFAULT: |
192 | AddCharToTheLine(ESBYTE[bufpos]); |
192 | AddCharToTheLine(ESBYTE[bufpos]); |
193 | } |
193 | } |
194 | } |
194 | } |
195 | RenderTextbuf(); |
195 | RenderTextbuf(); |
196 | list.count = draw_y + style.cur_line_h; |
196 | list.count = draw_y + style.cur_line_h; |
197 | 197 | ||
198 | canvas.bufh = math.max(list.visible, list.count); |
198 | canvas.bufh = math.max(list.visible, list.count); |
199 | buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8); |
199 | buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8); |
200 | 200 | ||
201 | list.CheckDoesValuesOkey(); |
201 | list.CheckDoesValuesOkey(); |
202 | anchors.current = NULL; |
202 | anchors.current = NULL; |
203 | if (!header) { |
203 | if (!header) { |
204 | strncpy(#header, #version, sizeof(TWebBrowser.header)-1); |
204 | strncpy(#header, #version, sizeof(TWebBrowser.header)-1); |
205 | DrawTitle(#header); |
205 | DrawTitle(#header); |
206 | } |
206 | } |
207 | } |
207 | } |
208 | //============================================================================================ |
208 | //============================================================================================ |
209 | void TWebBrowser::AddCharToTheLine(unsigned char _char) |
209 | void TWebBrowser::AddCharToTheLine(unsigned char _char) |
210 | { |
210 | { |
211 | dword line_len = strlen(#linebuf); |
211 | dword line_len = strlen(#linebuf); |
212 | if (_char<=15) _char=' '; |
212 | if (_char<=15) _char=' '; |
213 | if (!style.pre) && (_char == ' ') |
213 | if (!style.pre) && (_char == ' ') |
214 | { |
214 | { |
215 | if (linebuf[line_len-1]==' ') return; //no double spaces |
215 | if (linebuf[line_len-1]==' ') return; //no double spaces |
216 | if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line |
216 | if (draw_x==left_gap) && (!linebuf) return; //no paces at the beginning of the line |
217 | if (link) && (line_len==0) return; |
217 | if (link) && (line_len==0) return; |
218 | } |
218 | } |
219 | if (line_len < sizeof(TWebBrowser.linebuf)) chrcat(#linebuf+line_len, _char); |
219 | if (line_len < sizeof(TWebBrowser.linebuf)) { |
220 | if (line_len+1 * list.font_w + draw_x >= draw_w) RenderTextbuf(); |
220 | chrcat(#linebuf+line_len, _char); |
- | 221 | } else { |
|
- | 222 | RenderTextbuf(); |
|
- | 223 | } |
|
221 | } |
224 | } |
222 | //============================================================================================ |
225 | //============================================================================================ |
223 | void TWebBrowser::NewLine() |
226 | void TWebBrowser::NewLine() |
224 | { |
227 | { |
225 | static bool empty_line = true; |
228 | static bool empty_line = true; |
226 | 229 | ||
227 | if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return; |
230 | if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return; |
228 | if (t_html) && (!t_body) return; |
231 | if (t_html) && (!t_body) return; |
229 | 232 | ||
230 | if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) { |
233 | if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) { |
231 | if (!empty_line) empty_line=true; else return; |
234 | if (!empty_line) empty_line=true; else return; |
232 | } else { |
235 | } else { |
233 | empty_line = false; |
236 | empty_line = false; |
234 | } |
237 | } |
235 | 238 | ||
236 | draw_y += style.cur_line_h; |
239 | draw_y += style.cur_line_h; |
237 | style.cur_line_h = list.item_h; |
240 | style.cur_line_h = list.item_h; |
238 | if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0; |
241 | if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0; |
239 | draw_x += style.tag_list.level * 5 * list.font_w + left_gap; |
242 | draw_x += style.tag_list.level * 5 * list.font_w + left_gap; |
240 | } |
243 | } |
241 | //============================================================================================ |
244 | //============================================================================================ |
242 | void TWebBrowser::ChangeEncoding(int _new_encoding) |
245 | void TWebBrowser::ChangeEncoding(int _new_encoding) |
243 | { |
246 | { |
244 | if (cur_encoding == _new_encoding) return; |
247 | if (cur_encoding == _new_encoding) return; |
245 | cur_encoding = _new_encoding; |
248 | cur_encoding = _new_encoding; |
246 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
249 | bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer); |
247 | if (header) { |
250 | if (header) { |
248 | ChangeCharset(cur_encoding, "CP866", #header); |
251 | ChangeCharset(cur_encoding, "CP866", #header); |
249 | DrawTitle(#header); |
252 | DrawTitle(#header); |
250 | } |
253 | } |
251 | } |
254 | } |
252 | //============================================================================================ |
255 | //============================================================================================ |
253 | scroll_bar scroll_wv = { 15,NULL,NULL,NULL, |
256 | scroll_bar scroll_wv = { 15,NULL,NULL,NULL, |
254 | 0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee}; |
257 | 0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee}; |
255 | 258 | ||
256 | void TWebBrowser::DrawPage() |
259 | void TWebBrowser::DrawPage() |
257 | { |
260 | { |
258 | if (list.w!=canvas.bufw) { |
261 | if (list.w!=canvas.bufw) { |
259 | ParseHtml(bufpointer, bufsize); |
262 | ParseHtml(bufpointer, bufsize); |
260 | } |
263 | } |
261 | canvas.Show(list.first, list.h); |
264 | canvas.Show(list.first, list.h); |
262 | 265 | ||
263 | scroll_wv.max_area = list.count; |
266 | scroll_wv.max_area = list.count; |
264 | scroll_wv.cur_area = list.visible; |
267 | scroll_wv.cur_area = list.visible; |
265 | scroll_wv.position = list.first; |
268 | scroll_wv.position = list.first; |
266 | scroll_wv.all_redraw = 0; |
269 | scroll_wv.all_redraw = 0; |
267 | scroll_wv.start_x = list.x + list.w; |
270 | scroll_wv.start_x = list.x + list.w; |
268 | scroll_wv.start_y = list.y; |
271 | scroll_wv.start_y = list.y; |
269 | scroll_wv.size_y = list.h; |
272 | scroll_wv.size_y = list.h; |
270 | 273 | ||
271 | if (list.count <= list.visible) { |
274 | if (list.count <= list.visible) { |
272 | DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, |
275 | DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, |
273 | scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF); |
276 | scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF); |
274 | } else { |
277 | } else { |
275 | scrollbar_v_draw(#scroll_wv); |
278 | scrollbar_v_draw(#scroll_wv); |
276 | } |
279 | } |
277 | }=>>=15)>': |
280 | }=>>=15)>': |
278 | >>?xml"))> |
281 | >>?xml"))> |