Subversion Repositories Kolibri OS

Rev

Rev 8492 | Rev 8500 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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