Subversion Repositories Kolibri OS

Rev

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