Subversion Repositories Kolibri OS

Rev

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