Subversion Repositories Kolibri OS

Rev

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