Subversion Repositories Kolibri OS

Rev

Rev 7043 | 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.                         if (!strcmp(#header, #version)) {
  299.                                 ChangeCharset(charsets[cur_encoding], "CP866", #header);
  300.                                 sprintf(#header, "%s - %s", #header, #version);
  301.                         }
  302.                         DrawTitle(#header);
  303.                 }
  304.                 return;
  305.         }
  306.         if (istag("a")) {
  307.                 if (opened)
  308.                 {
  309.                         if (link) IF(text_color_index > 0) text_color_index--; //åñëè ïðåäûäóùèé òåã à íå áûë çàêðûò
  310.                         do{
  311.                                 if (isattr("href=")) && (!strstr(#val,"javascript:"))
  312.                                 {
  313.                                         text_color_index++;
  314.                                         text_colors[text_color_index] = text_colors[text_color_index-1];
  315.                                         link = 1;
  316.                                         text_colors[text_color_index] = link_color_inactive;
  317.                                         PageLinks.AddLink(#val);
  318.                                 }
  319.                         } while(GetNextParam());
  320.                 }
  321.                 else {
  322.                         link = 0;
  323.                         IF(text_color_index > 0) text_color_index--;
  324.                 }
  325.                 return;
  326.         }
  327.         if (istag("font")) {
  328.                 if (opened)
  329.                 {
  330.                         text_color_index++;
  331.                         text_colors[text_color_index] = text_colors[text_color_index-1];
  332.                         do{
  333.                                 if (isattr("color=")) text_colors[text_color_index] = GetColor(#val);
  334.                         } while(GetNextParam());
  335.                 }
  336.                 else if (text_color_index > 0) text_color_index--;
  337.                 return;
  338.         }
  339.         if (istag("div")) || (istag("header")) || (istag("article")) || (istag("footer")) {
  340.                 IF(oldtag[0] != 'h') NewLine();
  341.                 return;
  342.         }
  343.         if (istag("p")) {
  344.                 IF(oldtag[0] == 'h') return;
  345.                 NewLine();
  346.                 IF(opened) NewLine();
  347.                 return;
  348.         }
  349.         if (istag("br")) { NewLine(); return; }
  350.         if (istag("tr")) { if (opened) NewLine(); return; }
  351.         if (istag("b")) || (istag("strong")) || (istag("big")) { style.b = opened; return; }
  352.         if (istag("u")) || (istag("ins")) { style.u=opened; return;}
  353.         if (istag("s")) || (istag("strike")) || (istag("del")) { style.s=opened; return; }
  354.         if (istag("dd")) { stolbec += 5; return; }
  355.         if (istag("blockquote")) { style.blq = opened; return; }
  356.         if (istag("pre")) || (istag("code")) { style.pre = opened; return; }
  357.         if (istag("img")) { ImgCache.Images( left1, draw_y, WB1.list.w); return; }
  358.         if (istag("h1")) || (istag("h2")) || (istag("h3")) || (istag("h4")) || (istag("caption")) {
  359.                 style.h = opened;
  360.                 if (opened)
  361.                 {
  362.                         NewLine();
  363.                         draw_y += 10;
  364.                         WB1.zoom=2;
  365.                         WB1.list.font_type |= 10011001b;
  366.                         if (isattr("align=")) && (isval("center")) style.align = ALIGN_CENTER;
  367.                         if (isattr("align=")) && (isval("right")) style.align = ALIGN_RIGHT;
  368.                         list.item_h = basic_line_h * 2;
  369.                 }
  370.                 else
  371.                 {
  372.                         NewLine();
  373.                         WB1.zoom=1;
  374.                         WB1.list.font_type = 10011000b;
  375.                         style.align = ALIGN_LEFT;
  376.                         list.item_h = basic_line_h;
  377.                 }
  378.                 return;
  379.         }
  380.         if (istag("dt")) {
  381.                 style.li = opened;
  382.                 if (opened) NewLine();
  383.                 return;
  384.         }
  385.         if (istag("li")) || (istag("dt"))
  386.         {
  387.                 style.li = opened;
  388.                 if (opened)
  389.                 {
  390.                         NewLine();
  391.                         strcpy(#line, "\31 \0");
  392.                 }
  393.                 return;
  394.         }
  395.         if (istag("ul")) || (istag("ol")) {
  396.                 if (!opened)
  397.                 {
  398.                         style.li = opened;
  399.                         style.li_tab--;
  400.                         NewLine();
  401.                 }
  402.                 else style.li_tab++;
  403.         }
  404.         if (istag("hr")) {
  405.                 if (isattr("color=")) EDI = GetColor(#val); else EDI = 0x999999;
  406.                 $push edi;
  407.                 NewLine();
  408.                 $pop edi;
  409.                 DrawBuf.DrawBar(5, draw_y - 1, list.w-10, 1, EDI);
  410.                 NewLine();
  411.                 return;
  412.         }
  413.         if (istag("meta")) || (istag("?xml")) {
  414.                 meta_encoding = CH_NULL;
  415.                 do{
  416.                         if (isattr("charset=")) || (isattr("content=")) || (isattr("encoding="))
  417.                         {
  418.                                 strcpy(#val, #val[strrchr(#val, '=')]); //search in content=
  419.                                 strlwr(#val);
  420.                                 if      (isval("utf-8"))        || (isval("utf8"))        meta_encoding = CH_UTF8;
  421.                                 else if (isval("koi8-r"))       || (isval("koi8-u"))      meta_encoding = CH_KOI8;
  422.                                 else if (isval("windows-1251")) || (isval("windows1251")) meta_encoding = CH_CP1251;
  423.                                 else if (isval("iso-8859-5"))   || (isval("iso8859-5"))   meta_encoding = CH_ISO8859_5;
  424.                                 else if (isval("dos"))          || (isval("cp-866"))      meta_encoding = CH_CP866;
  425.                         }
  426.                 } while(GetNextParam());
  427.                 if (meta_encoding!=CH_NULL) BufEncode(meta_encoding);
  428.                 return;
  429.         }
  430. }
  431.  
  432. void TWebBrowser::BufEncode(dword set_new_encoding)
  433. {
  434.         if (cur_encoding == set_new_encoding) return;
  435.         if (o_bufpointer==0)
  436.         {
  437.                 o_bufpointer = malloc(bufsize);
  438.                 strcpy(o_bufpointer, bufpointer);
  439.         }
  440.         else
  441.         {
  442.                 strcpy(bufpointer, o_bufpointer);
  443.         }
  444.         debugval("cur_encoding    ", cur_encoding);
  445.         debugval("set_new_encoding", set_new_encoding);
  446.         cur_encoding = set_new_encoding;
  447.         bufpointer = ChangeCharset(charsets[cur_encoding], "CP866", bufpointer);
  448. }
  449. //============================================================================================
  450. void TWebBrowser::DrawScroller()
  451. {
  452.         scroll_wv.max_area = list.count;
  453.         scroll_wv.cur_area = list.visible;
  454.         scroll_wv.position = list.first;
  455.         scroll_wv.all_redraw = 0;
  456.         scroll_wv.start_x = list.x + list.w;
  457.         scroll_wv.start_y = list.y;
  458.         scroll_wv.size_y = list.h;
  459.         scrollbar_v_draw(#scroll_wv);
  460. }
  461. //============================================================================================
  462. void TWebBrowser::NewLine()
  463. {
  464.         dword onleft, ontop;
  465.  
  466.         if (!stolbec) && (draw_y==body_magrin) return;
  467.  
  468.         onleft = list.x + body_magrin;
  469.         ontop = draw_y + list.y;
  470.         if (t_html) && (!t_body) return;
  471.         draw_y += list.item_h;
  472.         if (style.blq) stolbec = 6; else stolbec = 0;
  473.         if (style.li) stolbec = style.li_tab * 5;
  474. }
  475. //============================================================================================
  476. bool istag(dword text) { if (!strcmp(#tag,text)) return true; else return false; }
  477. bool isattr(dword text) { if (!strcmp(#attr,text)) return true; else return false; }
  478. bool isval(dword text) { if (!strcmp(#val,text)) return true; else return false; }
  479. //============================================================================================
  480. void TWebBrowser::DrawPage()
  481. {
  482.         PutPaletteImage(list.first * DrawBuf.bufw * 4 + buf_data+8, DrawBuf.bufw, list.h, DrawBuf.bufx, DrawBuf.bufy, 32, 0);  
  483.         DrawScroller();
  484. }