Subversion Repositories Kolibri OS

Rev

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