Subversion Repositories Kolibri OS

Rev

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