Subversion Repositories Kolibri OS

Rev

Rev 8443 | Rev 8451 | 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. dword page_bg;
  7. dword link_color_default;
  8. dword link_color_active;
  9. #include "TWB\links.h"
  10.  
  11. #define BODY_MARGIN 6
  12. #define BASIC_LINE_H 18
  13.  
  14. CANVAS canvas;
  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.         dword bg_color;
  26.         LIST tag_list;
  27.         dword title;
  28.         void reset();
  29. };
  30.  
  31. void STYLE::reset()
  32. {
  33.         b = u = s = h = blq = pre = title = false;
  34.         font = false;
  35.         tag_list.reset();
  36. }
  37.  
  38. struct TWebBrowser {
  39.         llist list;
  40.         STYLE style;
  41.         dword draw_y, stolbec;
  42.         int zoom;
  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 Paint();
  54.         void SetPageDefaults();
  55.         void AddCharToTheLine();
  56.         void ParseHtml();
  57.         void SetStyle();
  58.         bool CheckForLineBreak();
  59.         void NewLine();
  60.         void ChangeEncoding();
  61.         void DrawPage();
  62.  
  63.         void tag_a();
  64.         void tag_p();
  65.         void tag_img();
  66.         void tag_div();
  67.         void tag_h1234_caption();
  68.         void tag_ol_ul_dt();
  69.         void tag_li();
  70.         void tag_q();
  71.         void tag_hr();
  72.         void tag_code();
  73.         void tag_meta_xml();
  74.         void tag_body();
  75.         void tag_iframe();
  76.         void tag_title();
  77.         void tag_font();
  78. };
  79.  
  80. #include "TWB\set_style.h"
  81.  
  82. //============================================================================================
  83. void TWebBrowser::Paint()
  84. {
  85.         dword start_x, line_length, stolbec_len;
  86.         dword text_color__;
  87.        
  88.         if (style.title)
  89.         {
  90.                 strncpy(#header, #line, sizeof(TWebBrowser.header)-1);
  91.                 strncat(#header, " - ", sizeof(TWebBrowser.header)-1);
  92.                 strncat(#header, #version, sizeof(TWebBrowser.header)-1);
  93.                 line = 0;
  94.                 return;
  95.         }
  96.         if (t_html) && (!t_body) {
  97.                 line = 0;
  98.                 return;
  99.         }
  100.        
  101.         if (line)
  102.         {
  103.                 start_x = stolbec * list.font_w + BODY_MARGIN + list.x;
  104.                 stolbec_len = strlen(#line) * zoom;
  105.                 line_length = stolbec_len * list.font_w;
  106.  
  107.                 if (style.bg_color!=page_bg) {
  108.                         canvas.DrawBar(start_x, draw_y, line_length, list.item_h, style.bg_color);
  109.                 }
  110.  
  111.                 if (style.image) {
  112.                         canvas.DrawBar(start_x, draw_y, line_length, list.item_h-1, 0xF9DBCB);
  113.                 }
  114.                 if (style.button) {
  115.                         canvas.DrawBar(start_x, draw_y, line_length, list.item_h - calc(zoom*2), 0xCCCccc);
  116.                         canvas.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, 0x999999);
  117.                 }
  118.  
  119.                 text_color__ = text_colors.get_last();
  120.                 if (link) && (text_color__ == text_colors.get(0)) text_color__ = link_color_default;
  121.  
  122.                 canvas.WriteText(start_x, draw_y, list.font_type, text_color__, #line, NULL);
  123.                 if (style.b) canvas.WriteText(start_x+1, draw_y, list.font_type, text_color__, #line, NULL);
  124.                 if (style.s) canvas.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_color__);
  125.                 if (style.u) canvas.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_color__);
  126.                 if (link) {
  127.                         if (line[0]==' ') && (line[1]==NULL) {} else {
  128.                                 canvas.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2)-1, line_length, zoom, link_color_default);
  129.                                 links.add_text(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2)-1, zoom);                             
  130.                         }
  131.                 }
  132.                 stolbec += stolbec_len;
  133.                 if (debug_mode) debugln(#line);
  134.                 line = NULL;
  135.         }
  136. }
  137. //============================================================================================
  138. void TWebBrowser::SetPageDefaults()
  139. {
  140.         t_html = t_body = link = false;
  141.         style.reset();
  142.         link_color_default = 0x0000FF;
  143.         link_color_active = 0xFF0000;
  144.         style.bg_color = page_bg = 0xffEBE8E9; //E0E3E3 EBE8E9
  145.         canvas.Fill(0, page_bg);
  146.         links.clear();
  147.         anchors.clear();
  148.         img_url.drop();
  149.         text_colors.drop();
  150.         text_colors.add(0);
  151.         header = NULL;
  152.         cur_encoding = CH_CP866;
  153.         draw_y = BODY_MARGIN;
  154.         stolbec = 0;
  155.         line = 0;
  156.         zoom = 1;
  157.         redirect = '\0';
  158.         //hold original buffer
  159.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  160.         o_bufpointer = malloc(bufsize);
  161.         memmov(o_bufpointer, bufpointer, bufsize);
  162.         if (custom_encoding != -1) {
  163.                 cur_encoding = custom_encoding;
  164.                 bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  165.         }
  166. }
  167. //============================================================================================
  168. void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
  169.         char unicode_symbol[10];
  170.         dword j;
  171.         int tab_len;
  172.         dword bufpos;
  173.         bufsize = _bufsize;
  174.  
  175.         if (bufpointer != _bufpointer) {
  176.                 bufpointer = malloc(bufsize);
  177.                 memmov(bufpointer, _bufpointer, bufsize);
  178.         } else {
  179.                 custom_encoding = CH_CP866;    
  180.         }
  181.         SetPageDefaults();
  182.         is_html = true;
  183.         if (!strstri(bufpointer, "<body")) {
  184.                 t_body = true;
  185.                 if (!strstri(bufpointer, "<html")) && (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
  186.                         style.pre = true; //show linebreaks for a plaint text
  187.                         is_html = false;
  188.                 }
  189.         }
  190.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  191.         {
  192.                 switch (ESBYTE[bufpos])
  193.                 {
  194.                 case 0x0a:
  195.                         if (style.pre) {
  196.                                 Paint();
  197.                                 NewLine();
  198.                         } else {
  199.                                 AddCharToTheLine(0x0a);
  200.                         }
  201.                         break;
  202.                 case 0x09:
  203.                         if (style.pre) {
  204.                                 tab_len = strlen(#line) + stolbec % 4;
  205.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  206.                                 for (j=0; j<tab_len; j++;) chrcat(#line,' ');
  207.                         } else {
  208.                                 AddCharToTheLine(0x09);
  209.                         }
  210.                         break;
  211.                 case '&': //&nbsp; and so on
  212.                         for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++)
  213.                         {
  214.                                 chrcat(#unicode_symbol, ESBYTE[bufpos+j]);
  215.                         }
  216.                         if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
  217.                                 bufpos += j;
  218.                                 CheckForLineBreak();
  219.                         } else {
  220.                                 AddCharToTheLine('&');
  221.                         }
  222.                         break;
  223.                 case '<':
  224.                         if (!is_html) goto _DEFAULT;
  225.                         bufpos++;
  226.                         switch (ESBYTE[bufpos]) {
  227.                                 case '!': case '/': case '?':
  228.                                 case 'a'...'z': case 'A'...'Z':
  229.                                         goto _TAG;
  230.                                 default:
  231.                                         goto _DEFAULT;
  232.                         }
  233.                         _TAG:
  234.                         if (tag.parse(#bufpos, bufpointer + bufsize)) {
  235.                                 CheckForLineBreak();
  236.                                 Paint();
  237.                                 $push cur_encoding
  238.                                 SetStyle();
  239.                                 $pop eax
  240.                                 // The thing is that UTF if longer than other encodings.
  241.                                 // So if encoding was changed from UTF to DOS than $bufpos position got wrong,
  242.                                 // and we have to start parse from the very beginning
  243.                                 if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
  244.                                         ParseHtml(bufpointer, bufsize);
  245.                                         return;
  246.                                 }
  247.                         }
  248.                         break;
  249.                 default:
  250.                         _DEFAULT:
  251.                         AddCharToTheLine(ESBYTE[bufpos]);
  252.                 }
  253.         }
  254.         Paint();
  255.         NewLine();
  256.         list.count = draw_y;
  257.  
  258.         canvas.bufh = math.max(list.visible, draw_y);
  259.         buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
  260.  
  261.         list.CheckDoesValuesOkey();
  262.         anchors.current = NULL;
  263.         custom_encoding = -1;
  264.         if (!header) {
  265.                 strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
  266.                 DrawTitle(#header);
  267.         }
  268. }
  269. //============================================================================================
  270. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  271. {
  272.         dword line_len;
  273.         if (_char<=15) _char=' ';
  274.         line_len = strlen(#line);
  275.         if (!style.pre) && (_char == ' ')
  276.         {
  277.                 if (line[line_len-1]==' ') return; //no double spaces
  278.                 if (!stolbec) && (!line) return; //no paces at the beginning of the line
  279.                 if (link) && (line_len==0) return;
  280.         }
  281.         if (line_len < sizeof(line)) chrcat(#line, _char);
  282.         CheckForLineBreak();
  283. }
  284. //============================================================================================
  285. bool TWebBrowser::CheckForLineBreak()
  286. {
  287.         int line_break_pos;
  288.         char new_line_text[4096];
  289.         //Do we need a line break?
  290.         if (strlen(#line)*zoom + stolbec < list.column_max) return false;
  291.         //Yes, we do. Lets calculate where...
  292.         line_break_pos = strrchr(#line, ' ');
  293.         //Is a new line fits in the current line?
  294.         if (line_break_pos*zoom + stolbec > list.column_max) {
  295.                 line_break_pos = list.column_max/zoom - stolbec;
  296.                 while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--;
  297.         }
  298.         //Maybe a new line is too big for the whole new line? Then we have to split it
  299.         if (!line_break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) {
  300.                 line_break_pos = list.column_max/zoom - stolbec;
  301.         }
  302.         strcpy(#new_line_text, #line + line_break_pos);
  303.         line[line_break_pos] = 0x00;           
  304.        
  305.         Paint();
  306.  
  307.         strcpy(#line, #new_line_text);
  308.         NewLine();
  309.         return true;
  310. }
  311. //============================================================================================
  312. void TWebBrowser::NewLine()
  313. {
  314.         static int empty_line=0;
  315.  
  316.         if (!stolbec) && (draw_y==BODY_MARGIN) return;
  317.        
  318.         if (style.tag_list.level) && (stolbec == style.tag_list.level * 5) {
  319.                 if (empty_line<1) empty_line++;
  320.                 else return;
  321.         } else if (!stolbec) {
  322.                 if (empty_line<1) empty_line++;
  323.                 else return;
  324.         } else {
  325.                 empty_line=0;
  326.         }
  327.  
  328.         if (t_html) && (!t_body) return;
  329.         draw_y += list.item_h;
  330.         if (style.blq) stolbec = 6; else stolbec = 0;
  331.         stolbec += style.tag_list.level * 5;
  332. }
  333. //============================================================================================
  334. void TWebBrowser::ChangeEncoding(int _new_encoding)
  335. {
  336.         if (cur_encoding == _new_encoding) return;
  337.         cur_encoding = _new_encoding;
  338.         bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  339.         if (header) {
  340.                 ChangeCharset(cur_encoding, "CP866", #header);
  341.                 DrawTitle(#header);
  342.         }
  343. }
  344. //============================================================================================
  345. scroll_bar scroll_wv =
  346. { 15,NULL,NULL,NULL,0,2,NULL,
  347.   0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  348.  
  349. void TWebBrowser::DrawPage()
  350. {
  351.         scroll_wv.max_area = list.count;
  352.         scroll_wv.cur_area = list.visible;
  353.         scroll_wv.position = list.first;
  354.         scroll_wv.all_redraw = 0;
  355.         scroll_wv.start_x = list.x + list.w;
  356.         scroll_wv.start_y = list.y;
  357.         scroll_wv.size_y = list.h;
  358.         scrollbar_v_draw(#scroll_wv);
  359.  
  360.         canvas.Show(list.first, list.h);
  361. }