Subversion Repositories Kolibri OS

Rev

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