Subversion Repositories Kolibri OS

Rev

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