Subversion Repositories Kolibri OS

Rev

Rev 6735 | 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. dword block_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\absolute_url.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.item_h + body_magrin;
  94.                 stolbec_len = 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.item_h); } // bug with zoom>1
  101.                 if (style.s) DrawBuf.DrawBar(start_x, list.item_h / 2 - DrawBuf.zoom + start_y, line_length, DrawBuf.zoom, text_colors[text_color_index]);
  102.                 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]);
  103.                 if (link) {
  104.                         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]);
  105.                         PageLinks.AddText(#line, line_length, list.item_h*style.h + list.item_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 = block_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_NULL;
  140.         }
  141.         else
  142.         {
  143.                 style.pre = 1;
  144.                 cur_encoding = CH_NULL;
  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) //otherwise go to 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)+1<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.                         Perenos();
  206.                         DrawStyle();
  207.                         line = NULL;
  208.                         if (tag) SetStyle();
  209.                         strlcpy(#oldtag, #tag, sizeof(oldtag));
  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.         if (list.first == 0) list.count = stroka;
  228.         DrawPage();
  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.         if (tag[0] == '/')
  257.         {
  258.                  opened = 0;
  259.                  strcpy(#tag, #tag+1);
  260.         }
  261.         else opened = 1;
  262.         if (istag("html")) {
  263.                 t_html = opened;
  264.                 return;
  265.         }
  266.         if (istag("script")) || (istag("style")) || (istag("binary")) || (istag("select")) { ignor_text = opened; return; }
  267.         if (istag("form")) if (!opened) ignor_text = false;
  268.         if(istag("title")) {
  269.                 if (opened) header=NULL;
  270.                 else if (!stroka) DrawTitle(#header);
  271.                 return;
  272.         }
  273.         if (ignor_text) return;
  274.        
  275.         IF(istag("q"))
  276.         {
  277.                 if (opened)     strcat(#line, " \"");
  278.                 if (!opened) strcat(#line, "\" ");
  279.                 return;
  280.         }
  281.         if (anchor) if (isattr("id=")) || (isattr("name=")) { //very bad: if the tag is not the last it wound work
  282.                 if (!strcmp(#anchor, #val))     anchor_line_num=list.first+stroka;
  283.         }      
  284.         if (istag("body")) {
  285.                 t_body = opened;
  286.                 do{
  287.                         if (isattr("link=")) link_color_inactive = GetColor(#val);
  288.                         if (isattr("alink=")) link_color_active = GetColor(#val);
  289.                         if (isattr("text=")) text_colors[0]=GetColor(#val);
  290.                         if (isattr("bgcolor="))
  291.                         {
  292.                                 bg_color = block_bg_color = GetColor(#val);
  293.                                 DrawBuf.Fill(bg_color);
  294.                         }
  295.                 } while(GetNextParam());
  296.                 if (opened) && (cur_encoding==CH_NULL) debugln("Document has no information about encoding, UTF will be used");
  297.                 return;
  298.         }
  299.         if (istag("a")) {
  300.                 if (opened)
  301.                 {
  302.                         if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
  303.                         do{
  304.                                 if (isattr("href=")) && (!strstr(#val,"javascript:"))
  305.                                 {
  306.                                         text_color_index++;
  307.                                         text_colors[text_color_index] = text_colors[text_color_index-1];
  308.                                         link = 1;
  309.                                         text_colors[text_color_index] = link_color_inactive;
  310.                                         PageLinks.AddLink(#val, DrawBuf.zoom * stolbec * list.font_w + left1, top1-DrawBuf.zoom);
  311.                                 }
  312.                         } while(GetNextParam());
  313.                 }
  314.                 else {
  315.                         link = 0;
  316.                         IF(text_color_index > 0) text_color_index--;
  317.                 }
  318.                 return;
  319.         }
  320.         if (istag("font")) {
  321.                 if (opened)
  322.                 {
  323.                         text_color_index++;
  324.                         text_colors[text_color_index] = text_colors[text_color_index-1];
  325.                         do{
  326.                                 if (isattr("color=")) text_colors[text_color_index] = GetColor(#val);
  327.                         } while(GetNextParam());
  328.                 }
  329.                 else if (text_color_index > 0) text_color_index--;
  330.                 return;
  331.         }
  332.         if (istag("bg")) {
  333.                 if (opened) {block_bg_color=GetColor(#val); NewLine();}
  334.                 if (!opened) block_bg_color=bg_color;
  335.         }
  336.         if (istag("div")) || (istag("header")) || (istag("article")) || (istag("footer")) {
  337.                 IF(oldtag[0] != 'h') NewLine();
  338.                 return;
  339.         }
  340.         if (istag("p")) {
  341.                 IF(oldtag[0] == 'h') return;
  342.                 NewLine();
  343.                 IF(opened) NewLine();
  344.                 return;
  345.         }
  346.         if (istag("br")) { NewLine(); return; }
  347.         if (istag("tr")) { if (opened) NewLine(); return; }
  348.         if (istag("b")) || (istag("strong")) || (istag("big")) { style.b = opened; return; }
  349.         if (istag("i")) || (istag("em")) || (istag("subtitle")) { style.i=opened; return; }
  350.         if (istag("u")) || (istag("ins")) { style.u=opened; return;}
  351.         if (istag("s")) || (istag("strike")) || (istag("del")) { style.s=opened; return; }
  352.         if (istag("dd")) { stolbec += 5; return; }
  353.         if (istag("blockquote")) { style.blq = opened; return; }
  354.         if (istag("pre")) || (istag("code")) { style.pre = opened; return; }
  355.         if (istag("img")) { ImgCache.Images( left1, top1, WB1.list.w); return; }
  356.         if (istag("h1")) || (istag("h2")) || (istag("h3")) || (istag("h4")) || (istag("caption")) {
  357.                 style.h = opened;
  358.                 NewLine();
  359.                 if (opened)
  360.                 {
  361.                         WB1.DrawBuf.zoom=2;
  362.                         WB1.list.font_type |= 10011001b;
  363.                         if (isattr("align=")) && (isval("center")) style.align = ALIGN_CENTER;
  364.                         if (isattr("align=")) && (isval("right")) style.align = ALIGN_RIGHT;
  365.                         if (stroka>1) NewLine();
  366.                 }
  367.                 else
  368.                 {
  369.                         WB1.DrawBuf.zoom=1;
  370.                         WB1.list.font_type = 10011000b;
  371.                         style.align = ALIGN_LEFT;
  372.                 }
  373.                 return;
  374.         }
  375.         if (istag("dt")) {
  376.                 style.li = opened;
  377.                 if (opened) NewLine();
  378.                 return;
  379.         }
  380.         if (istag("li")) || (istag("dt"))
  381.         {
  382.                 style.li = opened;
  383.                 if (opened)
  384.                 {
  385.                         NewLine();
  386.                         DrawBuf.DrawBar(style.li_tab * 5 * list.font_w * DrawBuf.zoom + list.x, stroka +1 * list.item_h - 3
  387.                          - DrawBuf.zoom - DrawBuf.zoom, DrawBuf.zoom*2, DrawBuf.zoom*2, 0x454545);
  388.                 }
  389.                 return;
  390.         }
  391.         if (istag("ul")) || (istag("ol")) {
  392.                 if (!opened)
  393.                 {
  394.                         style.li = opened;
  395.                         style.li_tab--;
  396.                         NewLine();
  397.                 }
  398.                 else style.li_tab++;
  399.         }
  400.         if (istag("hr")) {
  401.                 if (isattr("color=")) EDI = GetColor(#val); else EDI = 0x999999;
  402.                 $push edi;
  403.                 NewLine();
  404.                 $pop edi;
  405.                 DrawBuf.DrawBar(5, list.item_h*stroka+4, list.w-10, 1, EDI);
  406.                 NewLine();
  407.                 return;
  408.         }
  409.         if (istag("meta")) || (istag("?xml")) {
  410.                 meta_encoding = CH_NULL;
  411.                 do{
  412.                         if (isattr("charset=")) || (isattr("content=")) || (isattr("encoding="))
  413.                         {
  414.                                 strcpy(#val, #val[strrchr(#val, '=')]); //search in content=
  415.                                 strlwr(#val);
  416.                                 if      (isval("utf-8"))        || (isval("utf8"))        meta_encoding = CH_UTF8;
  417.                                 else if (isval("koi8-r"))       || (isval("koi8-u"))      meta_encoding = CH_KOI8;
  418.                                 else if (isval("windows-1251")) || (isval("windows1251")) meta_encoding = CH_CP1251;
  419.                                 else if (isval("iso-8859-5"))   || (isval("iso8859-5"))   meta_encoding = CH_ISO8859_5;
  420.                                 else if (isval("dos"))          || (isval("cp-866"))      meta_encoding = CH_CP866;
  421.                         }
  422.                 } while(GetNextParam());
  423.                 if (meta_encoding!=CH_NULL) BufEncode(meta_encoding);
  424.                 return;
  425.         }
  426. }
  427.  
  428. void TWebBrowser::BufEncode(int set_new_encoding)
  429. {
  430.         int bufpointer_realsize;
  431.         if (o_bufpointer==0)
  432.         {
  433.                 o_bufpointer = malloc(bufsize);
  434.                 strcpy(o_bufpointer, bufpointer);
  435.         }
  436.         else
  437.         {
  438.                 strcpy(bufpointer, o_bufpointer);
  439.         }
  440.         if (cur_encoding!=set_new_encoding) {
  441.                 cur_encoding = set_new_encoding;
  442.                 debugln(charsets[cur_encoding]);
  443.                 bufpointer = ChangeCharset(charsets[cur_encoding], "CP866", bufpointer);
  444.         }
  445. }
  446. //============================================================================================
  447. void TWebBrowser::DrawScroller()
  448. {
  449.         scroll_wv.max_area = list.count;
  450.         scroll_wv.cur_area = list.visible;
  451.         scroll_wv.position = list.first;
  452.  
  453.         scroll_wv.all_redraw = 0;
  454.         scroll_wv.start_x = list.x + list.w;
  455.         scroll_wv.start_y = list.y;
  456.  
  457.         scroll_wv.size_y = list.h;
  458.         scroll_wv.start_x = list.w * DrawBuf.zoom + list.x;
  459.  
  460.         scrollbar_v_draw(#scroll_wv);
  461. }
  462. //============================================================================================
  463. void TWebBrowser::NewLine()
  464. {
  465.         int onleft, ontop;
  466.  
  467.         onleft = list.x + 5;
  468.         ontop = stroka * list.item_h + list.y + 5;
  469.         if (t_html) && (!t_body) return;
  470.         if (block_bg_color!=bg_color) DrawBuf.DrawBar(0, stroka+1*list.item_h+5, DrawBuf.bufw, list.item_h, block_bg_color);
  471.         if (stroka * list.item_h + 5 >= 0) && ( stroka + 1 * list.item_h + 5 < list.h) && (!anchor)
  472.         {
  473.                 if (style.align == ALIGN_CENTER) && (DrawBuf.zoom==1) DrawBuf.AlignCenter(onleft,ontop,list.w,list.item_h,stolbec * list.font_w);
  474.                 if (style.align == ALIGN_RIGHT) && (DrawBuf.zoom==1)  DrawBuf.AlignRight(onleft,ontop,list.w,list.item_h,stolbec * list.font_w);
  475.         }
  476.         stroka++;
  477.         if (style.blq) stolbec = 6; else stolbec = 0;
  478.         if (style.li) stolbec = style.li_tab * 5;
  479. }
  480. //============================================================================================
  481. int istag(dword text) { if (!strcmp(#tag,text)) return true; else return false; }
  482. int isattr(dword text) { if (!strcmp(#attr,text)) return true; else return false; }
  483. int isval(dword text) { if (!strcmp(#val,text)) return true; else return false; }
  484. //============================================================================================
  485. void TWebBrowser::DrawPage()
  486. {
  487.         PutPaletteImage(list.first * list.item_h * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);    
  488.         DrawScroller();
  489. }