Subversion Repositories Kolibri OS

Rev

Rev 7757 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include "..\TWB\colors.h"
  2. #include "..\TWB\anchors.h"
  3. #include "..\TWB\parce_tag.h"
  4. #include "..\TWB\special.h"
  5. #include "..\TWB\img_cache.h"
  6. dword page_bg;
  7. dword link_color_inactive;
  8. dword link_color_active;
  9. #include "..\TWB\links.h"
  10.  
  11. enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT};
  12.  
  13. struct _style {
  14.         bool
  15.         b, u, s, h,
  16.         pre,
  17.         blq,
  18.         li,
  19.         li_tab,
  20.         button,
  21.         image,
  22.         align;
  23.         dword bg_color;
  24.  
  25.         dword main_title;
  26. };
  27.  
  28. struct TWebBrowser {
  29.         llist list;
  30.         _style style;
  31.         DrawBufer DrawBuf;
  32.         dword draw_y, stolbec;
  33.         int zoom;
  34.         dword o_bufpointer;
  35.         void SetPageDefaults();
  36.         void AddCharToTheLine();
  37.         void ParseHtml();
  38.         void SetStyle();
  39.         void DrawStyle();
  40.         void DrawPage();
  41.         void DrawScroller();
  42.         void NewLine();
  43.         bool CheckForLineBreak();
  44.         void BufEncode();
  45. } WB1;
  46.  
  47. char line[500];
  48.  
  49. bool link, cur_encoding, t_html, t_body;
  50.  
  51.  
  52. dword bufpointer=0;
  53. dword bufsize=0;
  54.  
  55. char header[150];
  56.  
  57. int body_magrin=6;
  58. int basic_line_h=22;
  59.  
  60. scroll_bar scroll_wv = { 15,NULL,NULL,NULL,0,2,NULL,0,0,0xeeeeee,0xBBBbbb,0xeeeeee};
  61.  
  62. //============================================================================================
  63. void TWebBrowser::DrawStyle()
  64. {
  65.         dword start_x, line_length, stolbec_len;
  66.        
  67.         if (style.main_title)
  68.         {
  69.                 strncpy(#header, #line, sizeof(header)-1);
  70.                 strncat(#header, " - ", sizeof(header)-1);
  71.                 strncat(#header, #version, sizeof(header)-1);
  72.                 line = 0;
  73.                 return;
  74.         }
  75.         if (t_html) && (!t_body) {
  76.                 line = 0;
  77.                 return;
  78.         }
  79.        
  80.         if (line)
  81.         {
  82.                 start_x = stolbec * list.font_w + body_magrin + list.x;
  83.                 stolbec_len = strlen(#line) * zoom;
  84.                 line_length = stolbec_len * list.font_w;
  85.  
  86.                 if (debug_mode) {
  87.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, 0xDDDddd);
  88.                 }
  89.  
  90.                 if (style.bg_color!=page_bg) {
  91.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h, style.bg_color);
  92.                 }
  93.  
  94.                 if (style.image) {
  95.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h-1, 0xF9DBCB);
  96.                 }
  97.                 if (style.button) {
  98.                         DrawBuf.DrawBar(start_x, draw_y, line_length, list.item_h - calc(zoom*2), 0xCCCccc);
  99.                         DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, 0x999999);
  100.                 }
  101.  
  102.                 DrawBuf.WriteText(start_x, draw_y, list.font_type, text_colors[text_color_index], #line);
  103.                 if (style.b) DrawBuf.WriteText(start_x+1, draw_y, list.font_type, text_colors[text_color_index], #line);
  104.                 if (style.s) DrawBuf.DrawBar(start_x, list.item_h / 2 - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
  105.                 if (style.u) DrawBuf.DrawBar(start_x, list.item_h - zoom - zoom + draw_y, line_length, zoom, text_colors[text_color_index]);
  106.                 if (link) {
  107.                         if (line[0]==' ') && (line[1]==NULL) {} else {
  108.                                 DrawBuf.DrawBar(start_x, draw_y + list.item_h - calc(zoom*2), line_length, zoom, link_color_inactive);
  109.                                 PageLinks.AddText(start_x, draw_y + list.y, line_length, list.item_h - calc(zoom*2), UNDERLINE, zoom);                         
  110.                         }
  111.                 }
  112.                 stolbec += stolbec_len;
  113.                 if (debug_mode) debug(#line);
  114.                 line = NULL;
  115.         }
  116. }
  117. //============================================================================================
  118. void TWebBrowser::SetPageDefaults()
  119. {
  120.         style.b = style.u = style.s = style.h = style.blq = t_html = t_body = style.pre =
  121.         style.li = link = text_color_index = text_colors[0] = style.li_tab = style.main_title = false;
  122.         style.align = ALIGN_LEFT;
  123.         link_color_inactive = 0x0000FF;
  124.         link_color_active = 0xFF0000;
  125.         page_bg = 0xFFFFFF;
  126.         style.bg_color = page_bg;
  127.         DrawBuf.Fill(0, page_bg);
  128.         PageLinks.Clear();
  129.         anchors.clear();
  130.         header = NULL;
  131.         cur_encoding = CH_NULL;
  132.         draw_y = body_magrin;
  133.         stolbec = 0;
  134.         line = 0;
  135.         zoom = 1;
  136.         //hold original buffer
  137.         if (o_bufpointer) o_bufpointer=free(o_bufpointer);
  138.         o_bufpointer = malloc(bufsize);
  139.         memmov(o_bufpointer, bufpointer, bufsize);
  140. }
  141. //============================================================================================
  142. void TWebBrowser::AddCharToTheLine(unsigned char _char)
  143. {
  144.         dword line_len;
  145.         if (_char<=15) _char=' ';
  146.         line_len = strlen(#line);
  147.         if (!style.pre) && (_char == ' ')
  148.         {
  149.                 if (line[line_len-1]==' ') return; //no double spaces
  150.                 if (!stolbec) && (!line) return; //no paces at the beginning of the line
  151.         }
  152.         if (line_len < sizeof(line)) chrcat(#line, _char);
  153.         CheckForLineBreak();
  154. }
  155. //============================================================================================
  156. void TWebBrowser::ParseHtml(){
  157.         word bukva[2];
  158.         char unicode_symbol[10];
  159.         dword unicode_symbol_result;
  160.         dword j;
  161.         bool ignor_param=false;
  162.         int tab_len;
  163.         dword bufpos;
  164.         SetPageDefaults();
  165.         if (strstri(bufpointer, "<body")==-1) {
  166.                 t_body = true;
  167.                 if (strstri(bufpointer, "<html")==-1)  style.pre = true; //show linebreaks for a plaint text
  168.         }
  169.         for (bufpos=bufpointer ; (bufpos < bufpointer+bufsize) && (ESBYTE[bufpos]!=0) ; bufpos++;)
  170.         {
  171.                 bukva = ESBYTE[bufpos];
  172.                 switch (bukva)
  173.                 {
  174.                 case 0x0a:
  175.                         if (style.pre) {
  176.                                 DrawStyle();
  177.                                 NewLine();
  178.                         } else {
  179.                                 AddCharToTheLine(0x0a);
  180.                         }
  181.                         break;
  182.                 case 0x09:
  183.                         if (style.pre) {
  184.                                 tab_len = strlen(#line) + stolbec % 4;
  185.                                 if (!tab_len) tab_len = 4; else tab_len = 4 - tab_len;
  186.                                 for (j=0; j<tab_len; j++;) chrcat(#line,' ');
  187.                         } else {
  188.                                 AddCharToTheLine(0x09);
  189.                         }
  190.                         break;
  191.                 case '&': //&nbsp; and so on
  192.                         for (j=1, unicode_symbol=0; (ESBYTE[bufpos+j]<>';') && (j<8); j++)
  193.                         {
  194.                                 bukva = ESBYTE[bufpos+j];
  195.                                 chrcat(#unicode_symbol, bukva);
  196.                         }
  197.                         if (GetUnicodeSymbol(#line, #unicode_symbol, sizeof(line)-1)) {
  198.                                 bufpos += j;
  199.                                 CheckForLineBreak();
  200.                         } else {
  201.                                 AddCharToTheLine('&');
  202.                         }
  203.                         break;
  204.                 case '<':
  205.                         bufpos++;
  206.                         if (!strncmp(bufpos,"!--",3))
  207.                         {
  208.                                 bufpos+=3;
  209.                                 while (strncmp(bufpos,"-->",3)!=0) && (bufpos < bufpointer + bufsize)
  210.                                 {
  211.                                         bufpos++;
  212.                                 }
  213.                                 bufpos+=2;
  214.                                 break;
  215.                         }
  216.                         tag.reset();
  217.                         if (ESBYTE[bufpos] == '/') {
  218.                                 tag.opened = false;
  219.                                 bufpos++;
  220.                         }
  221.  
  222.                         ignor_param=false;
  223.                         while (ESBYTE[bufpos] !='>') && (bufpos < bufpointer + bufsize) //ïîëó÷àåì òåã è åãî ïàðàìåòðû
  224.                         {
  225.                                 bukva = ESBYTE[bufpos];
  226.                                 if (bukva == '\9') || (bukva == '\x0a') || (bukva == '\x0d') bukva = ' ';
  227.                                 if (!ignor_param) && (bukva <>' ')
  228.                                 {
  229.                                         if (strlen(#tag.name)+1<sizeof(tag.name)) chrcat(#tag.name, bukva);
  230.                                 }
  231.                                 else
  232.                                 {
  233.                                         ignor_param = true;
  234.                                         if (strlen(#tag.params)+1<sizeof(tag.params)) strcat(#tag.params, #bukva);
  235.                                         //      chrncat(#tag.params, bukva, sizeof(tag.params)-1);
  236.                                 }
  237.                                 bufpos++;
  238.                         }
  239.                         strlwr(#tag.name);
  240.  
  241.                         // ignore text inside the next tags
  242.                         if (tag.is("script")) || (tag.is("style")) || (tag.is("binary")) || (tag.is("select"))  {
  243.                                 sprintf(#tag.params, "</%s>", #tag.name);
  244.                                 j = strstri(bufpos, #tag.params);
  245.                                 if (j!=-1) bufpos = j-1;
  246.                                 break;
  247.                         }
  248.  
  249.                         if (tag.name[strlen(#tag.name)-1]=='/') tag.name[strlen(#tag.name)-1]=NULL; //for br/ !!!!!!!!
  250.                         if (tag.params) tag.parse_params();
  251.  
  252.                         if (tag.name) {
  253.                                 CheckForLineBreak();
  254.                                 DrawStyle();
  255.                                 if (tag.name) SetStyle();
  256.                         }
  257.                         break;
  258.                 default:
  259.                         AddCharToTheLine(ESBYTE[bufpos]);
  260.                 }
  261.         }
  262.         DrawStyle();
  263.         NewLine();
  264.         list.count = draw_y;
  265.         list.CheckDoesValuesOkey();
  266.         anchors.current = NULL;
  267.         if (!header) {
  268.                 strncpy(#header, #version, sizeof(header)-1);
  269.                 DrawTitle(#header);
  270.         }
  271. }
  272. //============================================================================================
  273. bool TWebBrowser::CheckForLineBreak()
  274. {
  275.         int line_break_pos;
  276.         char new_line_text[4096];
  277.         //Do we need a line break?
  278.         if (strlen(#line)*zoom + stolbec < list.column_max) return false;
  279.         //Yes, we do. Lets calculate where...
  280.         line_break_pos = strrchr(#line, ' ');
  281.         //Is a new line fits in the current line?
  282.         if (line_break_pos*zoom + stolbec > list.column_max) {
  283.                 line_break_pos = list.column_max/zoom - stolbec;
  284.                 while(line_break_pos) && (line[line_break_pos]!=' ') line_break_pos--;
  285.         }
  286.         //Maybe a new line is too big for the whole new line? Then we have to split it
  287.         if (!line_break_pos) && (style.li_tab*5 + strlen(#line) * zoom >= list.column_max) {
  288.                 line_break_pos = list.column_max/zoom - stolbec;
  289.         }
  290.         strcpy(#new_line_text, #line + line_break_pos);
  291.         line[line_break_pos] = 0x00;           
  292.        
  293.         DrawStyle();
  294.  
  295.         strcpy(#line, #new_line_text);
  296.         NewLine();
  297.         //if (strlen(#line)*zoom + stolbec > list.column_max)CheckForLineBreak();
  298.         return true;
  299. }
  300. //============================================================================================
  301. void TWebBrowser::SetStyle() {
  302.         char img_path[4096]=0;
  303.         int meta_encoding;
  304.  
  305.         dword value;
  306.  
  307.         if (value = tag.get_value_of("name=")) || (value = tag.get_value_of("id=")) {
  308.                 anchors.add(value, draw_y);
  309.                 if (anchors.current) && (streq(value, #anchors.current+1)) {
  310.                         list.first = draw_y;
  311.                         anchors.current = NULL;
  312.                 }
  313.         }
  314.         if (tag.is("html")) {
  315.                 t_html = tag.opened;
  316.                 return;
  317.         }
  318.         if (tag.is("q"))
  319.         {
  320.                 if (tag.opened) {
  321.                         meta_encoding = strlen(#line);
  322.                         if (line[meta_encoding-1] != ' ') chrcat(#line, ' ');
  323.                         chrcat(#line, '\"');
  324.                 }
  325.                 if (!tag.opened) strcat(#line, "\" ");
  326.                 return;
  327.         }
  328.         if (tag.is("title")) {
  329.                 style.main_title = tag.opened;
  330.                 if (!tag.opened) DrawTitle(#header);
  331.                 return;
  332.         }
  333.         if (tag.is("body")) {
  334.                 t_body = tag.opened;
  335.                 if (value = tag.get_value_of("link="))  link_color_inactive = GetColor(value);
  336.                 if (value = tag.get_value_of("alink=")) link_color_active = GetColor(value);
  337.                 if (value = tag.get_value_of("text="))  text_colors[0]=GetColor(value);
  338.                 if (value = tag.get_value_of("bgcolor=")) {
  339.                         style.bg_color = page_bg = GetColor(value);
  340.                         DrawBuf.Fill(0, page_bg);
  341.                 }
  342.                 return;
  343.         }
  344.         if (tag.is("a")) {
  345.                 if (tag.opened)
  346.                 {
  347.                         if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
  348.                         if (value = tag.get_value_of("href=")) && (!strstr(value,"javascript:"))
  349.                         {
  350.                                 text_color_index++;
  351.                                 text_colors[text_color_index] = text_colors[text_color_index-1];
  352.                                 link = 1;
  353.                                 text_colors[text_color_index] = link_color_inactive;
  354.                                 PageLinks.AddLink(value);
  355.                         }
  356.                 } else {
  357.                         link = 0;
  358.                         IF(text_color_index > 0) text_color_index--;
  359.                 }
  360.                 return;
  361.         }
  362.         if (tag.is("font")) {
  363.                 style.bg_color = page_bg;
  364.                 if (tag.opened)
  365.                 {
  366.                         text_color_index++;
  367.                         text_colors[text_color_index] = text_colors[text_color_index-1];
  368.                         if (value = tag.get_value_of("color=")) text_colors[text_color_index] = GetColor(value);
  369.                         if (value = tag.get_value_of("bg=")) style.bg_color = GetColor(value);
  370.                 }
  371.                 else if (text_color_index > 0) text_color_index--;
  372.                 return;
  373.         }
  374.         if (tag.is("div")) {
  375.                 if (streq(#tag.prior,"div")) && (tag.opened) return;
  376.                 NewLine();
  377.                 return;
  378.         }
  379.         if (tag.is("header")) || (tag.is("article")) || (tag.is("footer")) || (tag.is("figure")) {
  380.                 NewLine();
  381.                 return;
  382.         }
  383.         if (tag.is("p")) {
  384.                 IF (tag.prior[0] == 'h') || (streq(#tag.prior,"td")) || (streq(#tag.prior,"p")) return;
  385.                 NewLine();
  386.                 return;
  387.         }
  388.         if (tag.is("br")) { NewLine(); return; }
  389.         if (tag.is("td")) { if (tag.opened) AddCharToTheLine(' '); return; }
  390.         if (tag.is("tr")) { if (tag.opened) NewLine(); return; }
  391.         if (tag.is("b")) || (tag.is("strong")) || (tag.is("big")) { style.b = tag.opened; return; }
  392.         if (tag.is("button")) { style.button = tag.opened; stolbec++; return; }
  393.         if (tag.is("u")) || (tag.is("ins")) { style.u=tag.opened; return;}
  394.         if (tag.is("s")) || (tag.is("strike")) || (tag.is("del")) { style.s=tag.opened; return; }
  395.         if (tag.is("dd")) { stolbec += 5; return; }
  396.         if (tag.is("blockquote")) { style.blq = tag.opened; return; }
  397.         if (tag.is("pre")) || (tag.is("code")) { style.pre = tag.opened; return; }
  398.         if (tag.is("img")) {
  399.                 if (value = tag.get_value_of("src=")) strlcpy(#img_path, value, sizeof(img_path)-1);
  400.                 if (value = tag.get_value_of("title=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
  401.                 if (value = tag.get_value_of("alt=")) && (strlen(value)<sizeof(line)-3) && (value) sprintf(#line, "[%s]", value);
  402.                 if (!img_path) { line=0; return; }
  403.                 style.image = true;
  404.                 text_color_index++;
  405.                 text_colors[text_color_index] = 0x9A6F29;
  406.                 if (!line) {
  407.                         if (!strncmp(#img_path, "data:", 5)) img_path=0;
  408.                         replace_char(#img_path, '?', NULL, strlen(#img_path));
  409.                         sprintf(#line, "[%s]", #img_path+strrchr(#img_path, '/'));
  410.                         line[50]= NULL;
  411.                 }
  412.                 while (CheckForLineBreak()) {};
  413.                 DrawStyle();
  414.                 text_color_index--;
  415.                 style.image = false;
  416.                 //ImgCache.Images( list.x, draw_y, WB1.list.w);
  417.                 return;
  418.         }
  419.         if (tag.is("h4")) {
  420.                 NewLine();
  421.                 style.h = tag.opened;
  422.                 style.b = tag.opened;
  423.         }
  424.         if (tag.is("h1")) || (tag.is("h2")) || (tag.is("h3")) || (tag.is("caption")) {
  425.                 style.h = tag.opened;
  426.                 if (tag.opened) {
  427.                         NewLine();
  428.                         draw_y += 10;
  429.                         WB1.zoom=2;
  430.                         WB1.list.font_type |= 10011001b;
  431.                         list.item_h = basic_line_h * 2 - 2;
  432.                         if (tag.is("h1")) style.b = true;
  433.                 } else {
  434.                         if (tag.is("h1")) style.b = false;
  435.                         NewLine();
  436.                         WB1.zoom=1;
  437.                         WB1.list.font_type = 10011000b;
  438.                         list.item_h = basic_line_h;
  439.                 }
  440.                 return;
  441.         }
  442.         if (tag.is("dt")) {
  443.                 style.li = tag.opened;
  444.                 if (tag.opened) NewLine();
  445.                 return;
  446.         }
  447.         if (tag.is("li")) || (tag.is("dt"))
  448.         {
  449.                 style.li = tag.opened;
  450.                 if (tag.opened)
  451.                 {
  452.                         if (style.li_tab==0) style.li_tab++;
  453.                         NewLine();
  454.                         stolbec = style.li_tab * 5 - 2;
  455.                         strcpy(#line, "\31 ");
  456.                         //stolbec-=2;
  457.                 }
  458.                 return;
  459.         }
  460.         if (tag.is("ul")) || (tag.is("ol")) {
  461.                 if (!tag.opened)
  462.                 {
  463.                         style.li = false;
  464.                         if (style.li_tab>0) style.li_tab--;
  465.                         NewLine();
  466.                 }
  467.                 else {
  468.                         if (style.li_tab<5) style.li_tab++;
  469.                 }
  470.         }
  471.         if (tag.is("hr")) {
  472.                 if (value = tag.get_value_of("color=")) EDI = GetColor(value); else EDI = 0x999999;
  473.                 $push edi;
  474.                 NewLine();
  475.                 $pop edi;
  476.                 draw_y += 10;
  477.                 DrawBuf.DrawBar(5, draw_y - 1, list.w-10, 1, EDI);
  478.                 NewLine();
  479.                 draw_y += 10;
  480.                 return;
  481.         }
  482.         if (tag.is("meta")) || (tag.is("?xml")) {
  483.                 meta_encoding = CH_NULL;
  484.                 if (value = tag.get_value_of("charset=")) || (value = tag.get_value_of("content=")) || (value = tag.get_value_of("encoding="))
  485.                 {
  486.                         value += strrchr(value, '='); //search in content=
  487.                         strlwr(value);
  488.                         if      (streq(value,"utf-8"))        || (streq(value,"utf8"))        meta_encoding = CH_UTF8;
  489.                         else if (streq(value,"windows-1251")) || (streq(value,"windows1251")) meta_encoding = CH_CP1251;
  490.                         else if (streq(value,"dos"))          || (streq(value,"cp-866"))      meta_encoding = CH_CP866;
  491.                         else if (streq(value,"iso-8859-5"))   || (streq(value,"iso8859-5"))   meta_encoding = CH_ISO8859_5;
  492.                         else if (streq(value,"koi8-r"))       || (streq(value,"koi8-u"))      meta_encoding = CH_KOI8;
  493.                 }
  494.                 if (meta_encoding!=CH_NULL) {
  495.                         BufEncode(meta_encoding);
  496.                         if (header) {
  497.                                 ChangeCharset(charsets[cur_encoding], "CP866", #header);
  498.                                 DrawTitle(#header);
  499.                         }
  500.                 }
  501.                 return;
  502.         }
  503. }
  504. //============================================================================================
  505. void TWebBrowser::BufEncode(dword set_new_encoding)
  506. {
  507.         if (cur_encoding != set_new_encoding) {
  508.                 cur_encoding = set_new_encoding;
  509.                 bufpointer = ChangeCharset(charsets[cur_encoding], "CP866", bufpointer);               
  510.         }
  511. }
  512. //============================================================================================
  513. void TWebBrowser::DrawScroller()
  514. {
  515.         scroll_wv.max_area = list.count;
  516.         scroll_wv.cur_area = list.visible;
  517.         scroll_wv.position = list.first;
  518.         scroll_wv.all_redraw = 0;
  519.         scroll_wv.start_x = list.x + list.w;
  520.         scroll_wv.start_y = list.y;
  521.         scroll_wv.size_y = list.h;
  522.         scrollbar_v_draw(#scroll_wv);
  523. }
  524. //============================================================================================
  525. void TWebBrowser::NewLine()
  526. {
  527.         dword onleft, ontop;
  528.         static int empty_line=0;
  529.  
  530.         if (!stolbec) && (draw_y==body_magrin) return;
  531.        
  532.         if (style.li) && (stolbec == style.li_tab * 5) {
  533.                 if (empty_line<1) empty_line++;
  534.                 else return;
  535.         } else if (!stolbec) {
  536.                 if (empty_line<1) empty_line++;
  537.                 else return;
  538.         } else {
  539.                 empty_line=0;
  540.         }
  541.  
  542.         onleft = list.x + body_magrin;
  543.         ontop = draw_y + list.y;
  544.         if (t_html) && (!t_body) return;
  545.         draw_y += list.item_h;
  546.         if (style.blq) stolbec = 6; else stolbec = 0;
  547.         if (style.li) stolbec = style.li_tab * 5;
  548.         if (debug_mode) debugln(NULL);
  549. }
  550. //============================================================================================
  551. void TWebBrowser::DrawPage()
  552. {
  553.         PutPaletteImage(list.first * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);  
  554.         DrawScroller();
  555. }