Subversion Repositories Kolibri OS

Rev

Rev 8500 | Rev 9103 | 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 Reparse();
  60.         void NewLine();
  61.         void ChangeEncoding();
  62.         void AddCharToTheLine();
  63.         void DrawPage();
  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();
  80.         void tag_table_reset();
  81.         void tag_table();
  82.         void tag_td();
  83.         void tag_tr();
  84. };
  85.  
  86. #include "TWB\render.h"
  87. #include "TWB\set_style.h"
  88. //============================================================================================
  89. void TWebBrowser::SetPageDefaults()
  90. {
  91.         t_html = t_body = link = false;
  92.         style.reset();
  93.         link_color_default = 0x0000FF;
  94.         link_color_active = 0xFF0000;
  95.         style.cur_line_h = list.item_h;
  96.         links.clear();
  97.         anchors.clear();
  98.         img_url.drop();
  99.         text_colors.drop();
  100.         text_colors.add(0);
  101.         bg_colors.drop();
  102.         bg_colors.add(DEFAULT_BG_COL);
  103.         canvas.Fill(0, DEFAULT_BG_COL);
  104.         header = NULL;
  105.         draw_y = BODY_MARGIN;
  106.         draw_x = left_gap = BODY_MARGIN;
  107.         draw_w = list.w - BODY_MARGIN;
  108.         linebuf = 0;
  109.         redirect = '\0';
  110.         list.SetFont(8, 14, 10011000b);
  111.         tag_table_reset();
  112. }
  113. //============================================================================================
  114. void TWebBrowser::Reparse()
  115. {
  116.         ParseHtml(bufpointer, bufsize);
  117. }
  118. //============================================================================================
  119. void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
  120.         int tab_len;
  121.         dword bufpos;
  122.         bufsize = _bufsize;
  123.  
  124.         if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
  125.  
  126.         if (bufpointer == _bufpointer) {
  127.                 custom_encoding = cur_encoding;
  128.         } else {
  129.                 bufpointer = malloc(bufsize);
  130.                 memmov(bufpointer, _bufpointer, bufsize);
  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.                 }
  142.         }
  143.  
  144.  
  145.         SetPageDefaults();
  146.         is_html = true;
  147.         if (!strstri(bufpointer, "<body")) {
  148.                 t_body = true;
  149.                 if (!strstri(bufpointer, "<html")) && (!strstri(bufpointer, "<head"))
  150.                 && (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
  151.                         style.pre = true; //show linebreaks for a plaint text
  152.                         is_html = false;
  153.                 }
  154.         }
  155.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  156.         {
  157.                 switch (ESBYTE[bufpos])
  158.                 {
  159.                 case 0x0a:
  160.                         if (style.pre) {
  161.                                 RenderTextbuf();
  162.                                 NewLine();
  163.                         } else {
  164.                                 goto _DEFAULT;
  165.                         }
  166.                         break;
  167.                 case 0x09:
  168.                         if (style.pre) {
  169.                                 tab_len = draw_x - left_gap / list.font_w + strlen(#linebuf) % 4;
  170.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  171.                                 while (tab_len) {chrcat(#linebuf,' '); tab_len--;}
  172.                         } else {
  173.                                 goto _DEFAULT;
  174.                         }
  175.                         break;
  176.                 case '&': //&nbsp; and so on
  177.                         bufpos = GetUnicodeSymbol(#linebuf, sizeof(TWebBrowser.linebuf), bufpos+1, bufpointer+bufsize);
  178.                         break;
  179.                 case '<':
  180.                         if (!is_html) goto _DEFAULT;
  181.                         if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT;
  182.                         bufpos++;
  183.                         if (tag.parse(#bufpos, bufpointer + bufsize)) {
  184.                                 RenderTextbuf();
  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) {
  192.                                         ParseHtml(bufpointer, strlen(bufpointer));
  193.                                         return;
  194.                                 }
  195.                         }
  196.                         break;
  197.                 default:
  198.                         _DEFAULT:
  199.                         AddCharToTheLine(ESBYTE[bufpos]);
  200.                 }
  201.         }
  202.         RenderTextbuf();
  203.         list.count = draw_y + style.cur_line_h;
  204.  
  205.         canvas.bufh = math.max(list.visible, list.count);
  206.         buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
  207.  
  208.         list.CheckDoesValuesOkey();
  209.         anchors.current = NULL;
  210.         if (!header) {
  211.                 strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
  212.                 DrawTitle(#header);
  213.         }
  214. }
  215. //============================================================================================
  216. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  217. {
  218.         dword line_len = strlen(#linebuf);
  219.         if (_char<=15) _char=' ';
  220.         if (!style.pre) && (_char == ' ')
  221.         {
  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
  224.                 if (link) && (line_len==0) return;
  225.         }
  226.         if (line_len < sizeof(TWebBrowser.linebuf)) {
  227.                 chrcat(#linebuf+line_len, _char);
  228.         } else {
  229.                 RenderTextbuf();
  230.         }
  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. //============================================================================================
  244. scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
  245.   0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  246.  
  247. void TWebBrowser::DrawPage()
  248. {
  249.         if (list.w!=canvas.bufw) Reparse();
  250.         canvas.Show(list.first, list.h);
  251.  
  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.  
  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.         }
  266. }