Subversion Repositories Kolibri OS

Rev

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

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