Subversion Repositories Kolibri OS

Rev

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