Subversion Repositories Kolibri OS

Rev

Rev 8490 | Rev 8492 | 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 line[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();
  80.         void tag_td();
  81.         void tag_tr();
  82. };
  83.  
  84. #include "TWB\render.h"
  85. #include "TWB\set_style.h"
  86. //============================================================================================
  87. void TWebBrowser::SetPageDefaults()
  88. {
  89.         t_html = t_body = link = false;
  90.         style.reset();
  91.         link_color_default = 0x0000FF;
  92.         link_color_active = 0xFF0000;
  93.         style.cur_line_h = list.item_h;
  94.         links.clear();
  95.         anchors.clear();
  96.         img_url.drop();
  97.         text_colors.drop();
  98.         text_colors.add(0);
  99.         bg_colors.drop();
  100.         bg_colors.add(DEFAULT_BG_COL);
  101.         canvas.Fill(0, DEFAULT_BG_COL);
  102.         header = NULL;
  103.         cur_encoding = CH_CP866;
  104.         draw_y = BODY_MARGIN;
  105.         draw_x = left_gap = BODY_MARGIN;
  106.         draw_w = list.w - BODY_MARGIN;
  107.         line = 0;
  108.         redirect = '\0';
  109.         //hold original buffer
  110.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  111.         o_bufpointer = malloc(bufsize);
  112.         memmov(o_bufpointer, bufpointer, bufsize);
  113.         if (custom_encoding != -1) {
  114.                 cur_encoding = custom_encoding;
  115.                 bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  116.         }
  117.         list.SetFont(8, 14, 10011000b);
  118. }
  119. //============================================================================================
  120. void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
  121.         int tab_len;
  122.         dword bufpos;
  123.         bufsize = _bufsize;
  124.  
  125.         if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
  126.  
  127.         if (bufpointer != _bufpointer) {
  128.                 bufpointer = malloc(bufsize);
  129.                 memmov(bufpointer, _bufpointer, bufsize);
  130.         } else {
  131.                 custom_encoding = CH_CP866;    
  132.         }
  133.         SetPageDefaults();
  134.         is_html = true;
  135.         if (!strstri(bufpointer, "<body")) {
  136.                 t_body = true;
  137.                 if (!strstri(bufpointer, "<html")) && (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
  138.                         style.pre = true; //show linebreaks for a plaint text
  139.                         is_html = false;
  140.                 }
  141.         }
  142.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  143.         {
  144.                 switch (ESBYTE[bufpos])
  145.                 {
  146.                 case 0x0a:
  147.                         if (style.pre) {
  148.                                 RenderTextbuf();
  149.                                 NewLine();
  150.                         } else {
  151.                                 goto _DEFAULT;
  152.                         }
  153.                         break;
  154.                 case 0x09:
  155.                         if (style.pre) {
  156.                                 tab_len = draw_x - left_gap / list.font_w + strlen(#line) % 4;
  157.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  158.                                 while (tab_len) {chrcat(#line,' '); tab_len--;}
  159.                         } else {
  160.                                 goto _DEFAULT;
  161.                         }
  162.                         break;
  163.                 case '&': //&nbsp; and so on
  164.                         bufpos = GetUnicodeSymbol(#line, sizeof(TWebBrowser.line), bufpos+1, bufpointer+bufsize);
  165.                         break;
  166.                 case '<':
  167.                         if (!is_html) goto _DEFAULT;
  168.                         if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT;
  169.                         bufpos++;
  170.                         if (tag.parse(#bufpos, bufpointer + bufsize)) {
  171.                                 RenderTextbuf();
  172.                                 $push cur_encoding
  173.                                 SetStyle();
  174.                                 $pop eax
  175.                                 // The thing is that UTF if longer than other encodings.
  176.                                 // So if encoding was changed from UTF to DOS than $bufpos position got wrong,
  177.                                 // and we have to start parse from the very beginning
  178.                                 if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
  179.                                         ParseHtml(bufpointer, bufsize);
  180.                                         return;
  181.                                 }
  182.                         }
  183.                         break;
  184.                 default:
  185.                         _DEFAULT:
  186.                         AddCharToTheLine(ESBYTE[bufpos]);
  187.                 }
  188.         }
  189.         RenderTextbuf();
  190.         list.count = draw_y + style.cur_line_h;
  191.  
  192.         canvas.bufh = math.max(list.visible, list.count);
  193.         buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
  194.  
  195.         list.CheckDoesValuesOkey();
  196.         anchors.current = NULL;
  197.         custom_encoding = -1;
  198.         if (!header) {
  199.                 strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
  200.                 DrawTitle(#header);
  201.         }
  202. }
  203. //============================================================================================
  204. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  205. {
  206.         dword line_len = strlen(#line);
  207.         if (_char<=15) _char=' ';
  208.         if (!style.pre) && (_char == ' ')
  209.         {
  210.                 if (line[line_len-1]==' ') return; //no double spaces
  211.                 if (draw_x==left_gap) && (!line) return; //no paces at the beginning of the line
  212.                 if (link) && (line_len==0) return;
  213.         }
  214.         if (line_len < sizeof(TWebBrowser.line)) chrcat(#line+line_len, _char);
  215.         if (line_len+1 * list.font_w + draw_x >= draw_w) RenderTextbuf();
  216. }
  217. //============================================================================================
  218. void TWebBrowser::NewLine()
  219. {
  220.         static bool empty_line = true;
  221.  
  222.         if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return;
  223.         if (t_html) && (!t_body) return;
  224.        
  225.         if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) {
  226.                 if (!empty_line) empty_line=true; else return;
  227.         } else {
  228.                 empty_line = false;
  229.         }
  230.  
  231.         draw_y += style.cur_line_h;
  232.         style.cur_line_h = list.item_h;
  233.         if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0;
  234.         draw_x += style.tag_list.level * 5 * list.font_w + left_gap;
  235. }
  236. //============================================================================================
  237. void TWebBrowser::ChangeEncoding(int _new_encoding)
  238. {
  239.         if (cur_encoding == _new_encoding) return;
  240.         cur_encoding = _new_encoding;
  241.         bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  242.         if (header) {
  243.                 ChangeCharset(cur_encoding, "CP866", #header);
  244.                 DrawTitle(#header);
  245.         }
  246. }
  247. //============================================================================================
  248. scroll_bar scroll_wv = { 15,NULL,NULL,NULL,
  249.   0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  250.  
  251. void TWebBrowser::DrawPage()
  252. {
  253.         if (list.w!=canvas.bufw) {
  254.                 ParseHtml(bufpointer, bufsize);
  255.         }
  256.         canvas.Show(list.first, list.h);
  257.  
  258.         scroll_wv.max_area = list.count;
  259.         scroll_wv.cur_area = list.visible;
  260.         scroll_wv.position = list.first;
  261.         scroll_wv.all_redraw = 0;
  262.         scroll_wv.start_x = list.x + list.w;
  263.         scroll_wv.start_y = list.y;
  264.         scroll_wv.size_y = list.h;
  265.  
  266.         if (list.count <= list.visible) {
  267.                 DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
  268.                 scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
  269.         } else {
  270.                 scrollbar_v_draw(#scroll_wv);          
  271.         }
  272. }