Subversion Repositories Kolibri OS

Rev

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