Subversion Repositories Kolibri OS

Rev

Rev 8451 | Rev 8457 | 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, stolbec;
  43.         int zoom;
  44.         dword o_bufpointer;
  45.         int cur_encoding, custom_encoding;
  46.         bool link, t_html, t_body;
  47.         dword bufpointer;
  48.         dword bufsize;
  49.         dword is_html;
  50.         collection img_url;
  51.         char header[150];
  52.         char redirect[URL_SIZE];
  53.  
  54.         void Paint();
  55.         void SetPageDefaults();
  56.         void AddCharToTheLine();
  57.         void ParseHtml();
  58.         void SetStyle();
  59.         bool CheckForLineBreak();
  60.         void NewLine();
  61.         void ChangeEncoding();
  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. };
  80.  
  81. #include "TWB\set_style.h"
  82.  
  83. //============================================================================================
  84. void TWebBrowser::Paint()
  85. {
  86.         unsigned px; //paint x coordinate
  87.         unsigned pw; //paint y coordinate
  88.            dword pc; //paint color
  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.                 px = stolbec * list.font_w + BODY_MARGIN + list.x;
  106.                 pw = strlen(#line) * zoom * list.font_w;
  107.                 stolbec += strlen(#line) * zoom;
  108.  
  109.                 style.cur_line_h = math.max(style.cur_line_h, list.item_h);
  110.  
  111.                 if (bg_colors.get_last() - bg_colors.get(0)) {
  112.                         canvas.DrawBar(px, draw_y, pw, list.item_h, bg_colors.get_last());
  113.                 }
  114.  
  115.                 if (style.image) {
  116.                         canvas.DrawBar(px, draw_y, pw, list.item_h-1, 0xF9DBCB);
  117.                 }
  118.                 if (style.button) {
  119.                         canvas.DrawBar(px, draw_y, pw, list.item_h - calc(zoom*2), 0xCCCccc);
  120.                         canvas.DrawBar(px, draw_y + list.item_h - calc(zoom*2), pw, zoom, 0x999999);
  121.                 }
  122.  
  123.                 pc = text_colors.get_last();
  124.                 if (link) && (pc == text_colors.get(0)) pc = link_color_default;
  125.  
  126.                 canvas.WriteText(px, draw_y, list.font_type, pc, #line, NULL);
  127.                 if (style.b) canvas.WriteText(px+1, draw_y, list.font_type, pc, #line, NULL);
  128.                 if (style.s) canvas.DrawBar(px, list.item_h / 2 - zoom + draw_y, pw, zoom, pc);
  129.                 if (style.u) canvas.DrawBar(px, list.item_h - zoom - zoom + draw_y, pw, zoom, pc);
  130.                 if (link) {
  131.                         if (line[0]==' ') && (line[1]==NULL) {} else {
  132.                                 canvas.DrawBar(px, draw_y + list.item_h - calc(zoom*2)-1, pw, zoom, link_color_default);
  133.                                 links.add_text(px, draw_y + list.y, pw, list.item_h - calc(zoom*2)-1, zoom);                           
  134.                         }
  135.                 }
  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.         stolbec = 0;
  160.         line = 0;
  161.         zoom = 1;
  162.         redirect = '\0';
  163.         //hold original buffer
  164.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  165.         o_bufpointer = malloc(bufsize);
  166.         memmov(o_bufpointer, bufpointer, bufsize);
  167.         if (custom_encoding != -1) {
  168.                 cur_encoding = custom_encoding;
  169.                 bufpointer = ChangeCharset(cur_encoding, "CP866", bufpointer);
  170.         }
  171. }
  172. //============================================================================================
  173. void TWebBrowser::ParseHtml(dword _bufpointer, _bufsize){
  174.         char unicode_symbol[10];
  175.         dword j;
  176.         int tab_len;
  177.         dword bufpos;
  178.         bufsize = _bufsize;
  179.  
  180.         if (bufpointer != _bufpointer) {
  181.                 bufpointer = malloc(bufsize);
  182.                 memmov(bufpointer, _bufpointer, bufsize);
  183.         } else {
  184.                 custom_encoding = CH_CP866;    
  185.         }
  186.         SetPageDefaults();
  187.         is_html = true;
  188.         if (!strstri(bufpointer, "<body")) {
  189.                 t_body = true;
  190.                 if (!strstri(bufpointer, "<html")) && (!strstr(bufpointer, "<?xml")) && (!strstr(bufpointer, "<xml")) {
  191.                         style.pre = true; //show linebreaks for a plaint text
  192.                         is_html = false;
  193.                 }
  194.         }
  195.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  196.         {
  197.                 switch (ESBYTE[bufpos])
  198.                 {
  199.                 case 0x0a:
  200.                         if (style.pre) {
  201.                                 Paint();
  202.                                 NewLine();
  203.                         } else {
  204.                                 AddCharToTheLine(0x0a);
  205.                         }
  206.                         break;
  207.                 case 0x09:
  208.                         if (style.pre) {
  209.                                 tab_len = strlen(#line) + stolbec % 4;
  210.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  211.                                 for (j=0; j<tab_len; j++;) chrcat(#line,' ');
  212.                         } else {
  213.                                 AddCharToTheLine(0x09);
  214.                         }
  215.                         break;
  216.                 case '&': //&nbsp; and so on
  217.                         for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (!__isWhite(ESBYTE[bufpos+j])) && (j<8); j++)
  218.                         {
  219.                                 chrcat(#unicode_symbol, ESBYTE[bufpos+j]);
  220.                         }
  221.                         if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
  222.                                 bufpos += j;
  223.                                 CheckForLineBreak();
  224.                         } else {
  225.                                 AddCharToTheLine('&');
  226.                         }
  227.                         break;
  228.                 case '<':
  229.                         if (!is_html) goto _DEFAULT;
  230.                         bufpos++;
  231.                         switch (ESBYTE[bufpos]) {
  232.                                 case '!': case '/': case '?':
  233.                                 case 'a'...'z': case 'A'...'Z':
  234.                                         goto _TAG;
  235.                                 default:
  236.                                         goto _DEFAULT;
  237.                         }
  238.                         _TAG:
  239.                         if (tag.parse(#bufpos, bufpointer + bufsize)) {
  240.                                 CheckForLineBreak();
  241.                                 Paint();
  242.                                 $push cur_encoding
  243.                                 SetStyle();
  244.                                 $pop eax
  245.                                 // The thing is that UTF if longer than other encodings.
  246.                                 // So if encoding was changed from UTF to DOS than $bufpos position got wrong,
  247.                                 // and we have to start parse from the very beginning
  248.                                 if (EAX != cur_encoding) && (cur_encoding == CH_UTF8) {
  249.                                         ParseHtml(bufpointer, bufsize);
  250.                                         return;
  251.                                 }
  252.                         }
  253.                         break;
  254.                 default:
  255.                         _DEFAULT:
  256.                         AddCharToTheLine(ESBYTE[bufpos]);
  257.                 }
  258.         }
  259.         Paint();
  260.         NewLine();
  261.         list.count = draw_y;
  262.  
  263.         canvas.bufh = math.max(list.visible, draw_y);
  264.         buf_data = realloc(buf_data, canvas.bufh * canvas.bufw * 4 + 8);
  265.  
  266.         list.CheckDoesValuesOkey();
  267.         anchors.current = NULL;
  268.         custom_encoding = -1;
  269.         if (!header) {
  270.                 strncpy(#header, #version, sizeof(TWebBrowser.header)-1);
  271.                 DrawTitle(#header);
  272.         }
  273. }
  274. //============================================================================================
  275. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  276. {
  277.         dword line_len;
  278.         if (_char<=15) _char=' ';
  279.         line_len = strlen(#line);
  280.         if (!style.pre) && (_char == ' ')
  281.         {
  282.                 if (line[line_len-1]==' ') return; //no double spaces
  283.                 if (!stolbec) && (!line) return; //no paces at the beginning of the line
  284.                 if (link) && (line_len==0) return;
  285.         }
  286.         if (line_len < sizeof(line)) chrcat(#line, _char);
  287.         CheckForLineBreak();
  288. }
  289. //============================================================================================
  290. bool TWebBrowser::CheckForLineBreak()
  291. {
  292.         int line_break_pos;
  293.         char new_line_text[4096];
  294.         //Do we need a line break?
  295.         if (strlen(#line)*zoom + stolbec < list.column_max) return false;
  296.         //Yes, we do. Lets calculate where...
  297.         line_break_pos = strrchr(#line, ' ');
  298.         //Is a new line fits in the current line?
  299.         if (line_break_pos*zoom + stolbec > list.column_max) {
  300.                 line_break_pos = list.column_max/zoom - stolbec;
  301.                 while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--;
  302.         }
  303.         //Maybe a new line is too big for the whole new line? Then we have to split it
  304.         if (!line_break_pos) && (style.tag_list.level*5 + strlen(#line) * zoom >= list.column_max) {
  305.                 line_break_pos = list.column_max/zoom - stolbec;
  306.         }
  307.         strcpy(#new_line_text, #line + line_break_pos);
  308.         line[line_break_pos] = 0x00;           
  309.        
  310.         Paint();
  311.  
  312.         strcpy(#line, #new_line_text);
  313.         NewLine();
  314.         return true;
  315. }
  316. //============================================================================================
  317. void TWebBrowser::NewLine()
  318. {
  319.         static int empty_line=0;
  320.  
  321.         if (!stolbec) && (draw_y==BODY_MARGIN) return;
  322.        
  323.         if (style.tag_list.level) && (stolbec == style.tag_list.level * 5) {
  324.                 if (empty_line<1) empty_line++;
  325.                 else return;
  326.         } else if (!stolbec) {
  327.                 if (empty_line<1) empty_line++;
  328.                 else return;
  329.         } else {
  330.                 empty_line=0;
  331.         }
  332.  
  333.         if (t_html) && (!t_body) return;
  334.         draw_y += style.cur_line_h;
  335.         style.cur_line_h = list.item_h;
  336.         if (style.blq) stolbec = 6; else stolbec = 0;
  337.         stolbec += style.tag_list.level * 5;
  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.         scroll_wv.max_area = list.count;
  358.         scroll_wv.cur_area = list.visible;
  359.         scroll_wv.position = list.first;
  360.         scroll_wv.all_redraw = 0;
  361.         scroll_wv.start_x = list.x + list.w;
  362.         scroll_wv.start_y = list.y;
  363.         scroll_wv.size_y = list.h;
  364.         scrollbar_v_draw(#scroll_wv);
  365.  
  366.         canvas.Show(list.first, list.h);
  367. }