Subversion Repositories Kolibri OS

Rev

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