Subversion Repositories Kolibri OS

Rev

Rev 7743 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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