Subversion Repositories Kolibri OS

Rev

Rev 8462 | Rev 8491 | 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. char line[500];
  16.  
  17. struct STYLE {
  18.         bool
  19.         b, u, s, h,
  20.         font,
  21.         pre,
  22.         blq,
  23.         button,
  24.         image;
  25.         LIST tag_list;
  26.         dword title;
  27.         dword cur_line_h;
  28.         void reset();
  29. };
  30.  
  31. void STYLE::reset()
  32. {
  33.         b = u = s = h = blq = pre = title = false;
  34.         font = false;
  35.         cur_line_h = NULL;
  36.         tag_list.reset();
  37. }
  38.  
  39. struct TWebBrowser {
  40.         llist list;
  41.         STYLE style;
  42.         dword draw_y, draw_x, draw_w, left_gap;
  43.         dword o_bufpointer;
  44.         int cur_encoding, custom_encoding;
  45.         bool link, t_html, t_body;
  46.         dword bufpointer;
  47.         dword bufsize;
  48.         dword is_html;
  49.         collection img_url;
  50.         char header[150];
  51.         char redirect[URL_SIZE];
  52.  
  53.         void SetStyle();
  54.         void Render();
  55.         bool RenderImage();
  56.  
  57.         void SetPageDefaults();
  58.         void AddCharToTheLine();
  59.         void ParseHtml();
  60.         bool CheckForLineBreak();
  61.         void NewLine();
  62.         void ChangeEncoding();
  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();
  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.         cur_encoding = CH_CP866;
  105.         draw_y = BODY_MARGIN;
  106.         draw_x = left_gap = BODY_MARGIN;
  107.         draw_w = list.w - BODY_MARGIN;
  108.         line = 0;
  109.         redirect = '\0';
  110.         //hold original buffer
  111.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  112.         o_bufpointer = malloc(bufsize);
  113.         memmov(o_bufpointer, bufpointer, bufsize);
  114.         if (custom_encoding != -1) {
  115.                 cur_encoding = custom_encoding;
  116.                 bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  117.         }
  118.         list.SetFont(8, 14, 10011000b);
  119. }
  120. //============================================================================================
  121. void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
  122.         char unicode_symbol[10];
  123.         dword j;
  124.         int tab_len;
  125.         dword bufpos;
  126.         bufsize = _bufsize;
  127.  
  128.         if (list.w!=canvas.bufw) canvas.Init(list.x, list.y, list.w, 400*20);
  129.  
  130.         if (bufpointer != _bufpointer) {
  131.                 bufpointer = malloc(bufsize);
  132.                 memmov(bufpointer, _bufpointer, bufsize);
  133.         } else {
  134.                 custom_encoding = CH_CP866;    
  135.         }
  136.         SetPageDefaults();
  137.         is_html = true;
  138.         if (!strstri(bufpointer, "<body")) {
  139.                 t_body = true;
  140.                 if (!strstri(bufpointer, "<html")) && (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
  141.                         style.pre = true; //show linebreaks for a plaint text
  142.                         is_html = false;
  143.                 }
  144.         }
  145.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  146.         {
  147.                 switch (ESBYTE[bufpos])
  148.                 {
  149.                 case 0x0a:
  150.                         if (style.pre) {
  151.                                 Render();
  152.                                 NewLine();
  153.                         } else {
  154.                                 AddCharToTheLine(' ');
  155.                         }
  156.                         break;
  157.                 case 0x09:
  158.                         if (style.pre) {
  159.                                 tab_len = draw_x - left_gap / list.font_w + strlen(#line) % 4;
  160.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  161.                                 for (j=0; j<tab_len; j++;) chrcat(#line,' ');
  162.                         } else {
  163.                                 AddCharToTheLine(' ');
  164.                         }
  165.                         break;
  166.                 case '&': //&nbsp; and so on
  167.                         for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++)
  168.                         {
  169.                                 chrcat(#unicode_symbol, ESBYTE[bufpos+j]);
  170.                         }
  171.                         if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
  172.                                 bufpos += j;
  173.                                 CheckForLineBreak();
  174.                         } else {
  175.                                 AddCharToTheLine('&');
  176.                         }
  177.                         break;
  178.                 case '<':
  179.                         if (!is_html) goto _DEFAULT;
  180.                         if (!strchr("!/?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", ESBYTE[bufpos+1])) goto _DEFAULT;
  181.                         bufpos++;
  182.                         if (tag.parse(#bufpos, bufpointer + bufsize)) {
  183.                                 CheckForLineBreak();
  184.                                 Render();
  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, bufsize);
  193.                                         return;
  194.                                 }
  195.                         }
  196.                         break;
  197.                 default:
  198.                         _DEFAULT:
  199.                         AddCharToTheLine(ESBYTE[bufpos]);
  200.                 }
  201.         }
  202.         Render();
  203.         NewLine();
  204.         list.count = draw_y;
  205.  
  206.         canvas.bufh = math.max(list.visible, draw_y);
  207.         buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
  208.  
  209.         list.CheckDoesValuesOkey();
  210.         anchors.current = NULL;
  211.         custom_encoding = -1;
  212.         if (!header) {
  213.                 strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
  214.                 DrawTitle(#header);
  215.         }
  216. }
  217. //============================================================================================
  218. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  219. {
  220.         dword line_len;
  221.         if (_char<=15) _char=' ';
  222.         line_len = strlen(#line);
  223.         if (!style.pre) && (_char == ' ')
  224.         {
  225.                 if (line[line_len-1]==' ') return; //no double spaces
  226.                 if (draw_x==left_gap) && (!line) return; //no paces at the beginning of the line
  227.                 if (link) && (line_len==0) return;
  228.         }
  229.         if (line_len < sizeof(line)) chrcat(#line, _char);
  230.         CheckForLineBreak();
  231. }
  232. //============================================================================================
  233. bool TWebBrowser::CheckForLineBreak()
  234. {
  235.         int break_pos;
  236.         char next_line[4096];
  237.         int zoom = list.font_w / BASIC_CHAR_W;
  238.         //Do we need a line break?
  239.         if (strlen(#line) * list.font_w + draw_x < draw_w) return false;
  240.         //Yes, we do. Lets calculate where...
  241.         break_pos = strrchr(#line, ' ');
  242.  
  243.         //Is a new line fits in the current line?
  244.         if (break_pos * list.font_w + draw_x > draw_w) {
  245.                 break_pos = draw_w - draw_x /list.font_w;
  246.                 while(break_pos) && (line[break_pos]!=' ') break_pos--;
  247.         }
  248.         //Maybe a new line is too big for the whole new line? Then we have to split it
  249.         if (!break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) {
  250.                 break_pos = draw_w  - draw_x / list.font_w;
  251.         }
  252.  
  253.         strcpy(#next_line, #line + break_pos);
  254.         line[break_pos] = 0x00;        
  255.        
  256.         Render();
  257.  
  258.         strcpy(#line, #next_line);
  259.         NewLine();
  260.         return true;
  261. }
  262. //============================================================================================
  263. void TWebBrowser::NewLine()
  264. {
  265.         static bool empty_line = true;
  266.  
  267.         if (draw_x==left_gap) && (draw_y==BODY_MARGIN) return;
  268.         if (t_html) && (!t_body) return;
  269.        
  270.         if (draw_x == style.tag_list.level * 5 * list.font_w + left_gap) {
  271.                 if (!empty_line) empty_line=true; else return;
  272.         } else {
  273.                 empty_line = false;
  274.         }
  275.  
  276.         draw_y += style.cur_line_h;
  277.         style.cur_line_h = list.item_h;
  278.         if (style.blq) draw_x = 6 * list.font_w; else draw_x = 0;
  279.         draw_x += style.tag_list.level * 5 * list.font_w + left_gap;
  280. }
  281. //============================================================================================
  282. void TWebBrowser::ChangeEncoding(int _new_encoding)
  283. {
  284.         if (cur_encoding == _new_encoding) return;
  285.         cur_encoding = _new_encoding;
  286.         bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  287.         if (header) {
  288.                 ChangeCharset(cur_encoding, "CP866", #header);
  289.                 DrawTitle(#header);
  290.         }
  291. }
  292. //============================================================================================
  293. scroll_bar scroll_wv =
  294. { 15,NULL,NULL,NULL,0,2,NULL,
  295.   0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  296.  
  297. void TWebBrowser::DrawPage()
  298. {
  299.         if (list.w!=canvas.bufw) {
  300.                 ParseHtml(bufpointer, bufsize);
  301.         }
  302.         canvas.Show(list.first, list.h);
  303.  
  304.         scroll_wv.max_area = list.count;
  305.         scroll_wv.cur_area = list.visible;
  306.         scroll_wv.position = list.first;
  307.         scroll_wv.all_redraw = 0;
  308.         scroll_wv.start_x = list.x + list.w;
  309.         scroll_wv.start_y = list.y;
  310.         scroll_wv.size_y = list.h;
  311.  
  312.         if (list.count <= list.visible) {
  313.                 DrawBar(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x,
  314.                 scroll_wv.size_y, bg_colors.get(0) & 0x00FFFFFF);
  315.         } else {
  316.                 scrollbar_v_draw(#scroll_wv);          
  317.         }
  318. }