Subversion Repositories Kolibri OS

Rev

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