Subversion Repositories Kolibri OS

Rev

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