Subversion Repositories Kolibri OS

Rev

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