Subversion Repositories Kolibri OS

Rev

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