Subversion Repositories Kolibri OS

Rev

Rev 5982 | Rev 5987 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //HTML Viewer in C--
  2. //Copyright 2007-2013 by Veliant & Leency
  3. //Asper, lev, Lrz, Barsuk, Nable...
  4. //home icon - rachel fu, GPL licence
  5.  
  6. #ifndef AUTOBUILD
  7.         #include "lang.h--"
  8. #endif
  9.  
  10. //libraries
  11. #define MEMSIZE 1060000
  12. #include "..\lib\gui.h"
  13. #include "..\lib\draw_buf.h"
  14. #include "..\lib\list_box.h"
  15. #include "..\lib\cursor.h"
  16. #include "..\lib\collection.h"
  17. #include "..\lib\font.h"
  18.  
  19. //*.obj libraries
  20. #include "..\lib\obj\box_lib.h"
  21. #include "..\lib\obj\libio_lib.h"
  22. #include "..\lib\obj\libimg_lib.h"
  23. #include "..\lib\obj\http.h"
  24. #include "..\lib\obj\iconv.h"
  25. //useful patterns
  26. #include "..\lib\patterns\libimg_load_skin.h"
  27. #include "..\lib\patterns\history.h"
  28.  
  29. char homepage[] = FROM "html\\homepage.htm";
  30.  
  31. #ifdef LANG_RUS
  32.         char version[]=" ’¥ªáâ®¢ë© ¡à ã§¥à 1.40";
  33.         ?define IMAGES_CACHE_CLEARED "Šíè ª à⨭®ª ®ç¨é¥­"
  34.         ?define T_LAST_SLIDE "â® ¯®á«¥¤­¨© á« ©¤"
  35.         char loading[] = "‡ £à㧪  áâà ­¨æë...<br>";
  36.         char page_not_found[] = FROM "html\page_not_found_ru.htm";
  37.         char accept_language[]= "Accept-Language: ru\n";
  38. #else
  39.         char version[]=" Text-based Browser 1.40";
  40.         ?define IMAGES_CACHE_CLEARED "Images cache cleared"
  41.         ?define T_LAST_SLIDE "This slide is the last"
  42.         char loading[] = "Loading...<br>";
  43.         char page_not_found[] = FROM "html\page_not_found_en.htm";
  44.         char accept_language[]= "Accept-Language: en\n";       
  45. #endif
  46.  
  47. #define URL_SERVICE_HISTORY "WebView://history"
  48. #define URL_SERVICE_HOME "WebView://home"
  49. #define URL_SERVICE_SOURCE "WebView://source:"
  50.  
  51. proc_info Form;
  52.  
  53. //char search_path[]="http://nigma.ru/index.php?s=";
  54. int redirected = 0;
  55.  
  56. char stak[4096];
  57.  
  58. int action_buf;
  59.  
  60. dword http_transfer = 0;
  61. dword http_buffer;
  62.  
  63. dword TOOLBAR_H = 33;
  64. dword STATUSBAR_H = 15;
  65. dword col_bg;
  66. dword panel_color;
  67. dword border_color;
  68.  
  69. progress_bar wv_progress_bar;
  70. byte souce_mode = false;
  71.  
  72. enum {
  73.         BACK_BUTTON=1000,
  74.         FORWARD_BUTTON,
  75.         REFRESH_BUTTON,
  76.         GOTOURL_BUTTON,
  77.         SANDWICH_BUTTON
  78. };
  79.  
  80. enum {
  81.         VIEW_SOURCE=1100,
  82.         EDIT_SOURCE,
  83.         VIEW_HISTORY,
  84.         FREE_IMG_CACHE,
  85.         DOWNLOAD_MANAGER
  86. };
  87.  
  88. #include "..\TWB\TWB.c"
  89. #include "menu.h"
  90. #include "history.h"
  91. #include "show_src.h"
  92. #include "network_get.h"
  93. #include "downloader.h"
  94.  
  95. char editURL[sizeof(URL)];
  96. int     mouse_twb;
  97. edit_box address_box = {250,56,34,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#editURL,#mouse_twb,2,19,19};
  98.  
  99.  
  100. void main()
  101. {
  102.         dword btn;
  103.         int half_scroll_size;
  104.         int scroll_used=0, show_menu;
  105.         CursorPointer.Load(#CursorFile);
  106.         load_dll(boxlib, #box_lib_init,0);
  107.         load_dll(libio, #libio_init,1);
  108.         load_dll(libimg, #libimg_init,1);
  109.         load_dll(libHTTP, #http_lib_init,1);
  110.         load_dll(iconv_lib, #iconv_open,0);
  111.         //load_dll(kmenu, #akmenu_init,0);
  112.         Libimg_LoadImage(#skin, abspath("wv_skin.png"));
  113.         SetSkinColors();
  114.         CreateDir("/tmp0/1/downloads");
  115.         if (param) strcpy(#URL, #param); else strcpy(#URL, URL_SERVICE_HOME);
  116.         WB1.DrawBuf.zoom = 1;
  117.         WB1.list.SetFont(8, 14, 10111000b);
  118.         WB1.list.no_selection = true;
  119.         //font init
  120.         font.bg_color   = 0xFFFFFF;
  121.         font.load(DEFAULT_FONT);
  122.         if (!font.data) {
  123.                 notify("'Error: Font is not loaded.' -E");
  124.                 ExitProcess();
  125.         }
  126.         //
  127.         SetEventMask(0xa7);
  128.         BEGIN_LOOP_APPLICATION:
  129.                 WaitEventTimeout(2);
  130.                 switch(EAX & 0xFF)
  131.                 {
  132.                         CASE evMouse:
  133.                                 if (!CheckActiveProcess(Form.ID)) break;
  134.                                 edit_box_mouse stdcall (#address_box);
  135.                                 mouse.get();
  136.                                 if (WB1.list.MouseOver(mouse.x, mouse.y))
  137.                                 {
  138.                                         PageLinks.Hover(mouse.x, WB1.list.first*WB1.list.item_h + mouse.y, link_color_inactive, link_color_active, bg_color);
  139.                                         if (bufsize) && (mouse.pkm) && (mouse.up) { CreateThread(#menu_rmb,#stak+4092); break; }
  140.                                         if (WB1.list.MouseScroll(mouse.vert)) WB1.DrawPage();
  141.                                 }
  142.                                 scrollbar_v_mouse (#scroll_wv);
  143.                                 if (WB1.list.first != scroll_wv.position)
  144.                                 {
  145.                                         WB1.list.first = scroll_wv.position;
  146.                                         WB1.DrawPage();
  147.                                         break;
  148.                                 }
  149.                                 break;
  150.  
  151.                         case evButton:
  152.                                 btn=GetButtonID();
  153.                                 if (btn==1)     ExitProcess();
  154.                                 Scan(btn);
  155.                                 break;
  156.  
  157.                         case evKey:
  158.                                 GetKeys();
  159.                                 if (address_box.flags & 0b10)  
  160.                                 {
  161.                                         if (key_ascii == ASCII_KEY_ENTER) Scan(key_scancode); else
  162.                                         if (key_ascii != 0x0d) && (key_ascii != 183) && (key_ascii != 184) {EAX = key_ascii << 8; edit_box_key stdcall(#address_box);}
  163.                                 }
  164.                                 else
  165.                                 {
  166.                                         Scan(key_scancode);
  167.                                 }
  168.                                 break;
  169.  
  170.                         case evReDraw:
  171.                                 if (action_buf) Scan(action_buf);
  172.                                 DefineAndDrawWindow(GetScreenWidth()-800/2,GetScreenHeight()-600/2,800,600,0x73,col_bg,0,0);
  173.                                 GetProcessInfo(#Form, SelfInfo);
  174.                                 if (Form.status_window>2) { DrawTitle(#header); break; }
  175.                                 if (Form.height<120) { MoveSize(OLD,OLD,OLD,120); break; }
  176.                                 if (Form.width<280) { MoveSize(OLD,OLD,280,OLD); break; }
  177.                                 Draw_Window();
  178.                                 break;
  179.                                
  180.                         case evNetwork:
  181.                                 if (http_transfer > 0) {
  182.                                         http_receive stdcall (http_transfer);
  183.                                         $push EAX
  184.                                         ESI = http_transfer;
  185.                                         wv_progress_bar.max = ESI.http_msg.content_length;
  186.                                         if (wv_progress_bar.value != ESI.http_msg.content_received)
  187.                                         {
  188.                                                 wv_progress_bar.value = ESI.http_msg.content_received; 
  189.                                                 DrawProgress();
  190.                                         }
  191.                                         $pop EAX
  192.                                         if (EAX == 0) {
  193.                                                 ESI = http_transfer;
  194.                                                 // Handle redirects
  195.                                                 if (ESI.http_msg.status >= 300) && (ESI.http_msg.status < 400)
  196.                                                 {
  197.                                                         redirected++;
  198.                                                         if (redirected<=5)
  199.                                                         {
  200.                                                                 http_find_header_field stdcall (http_transfer, "location\0");
  201.                                                                 if (EAX!=0) {
  202.                                                                         ESI = EAX;
  203.                                                                         EDI = #URL;
  204.                                                                         do {
  205.                                                                                 $lodsb;
  206.                                                                                 $stosb;
  207.                                                                         } while (AL != 0) && (AL != 13) && (AL != 10));
  208.                                                                         DSBYTE[EDI-1]='\0';
  209.                                                                 }
  210.                                                         }
  211.                                                         else
  212.                                                         {
  213.                                                                 notify("Too many redirects");
  214.                                                                 StopLoading();
  215.                                                                 break;
  216.                                                         }
  217.                                                 }
  218.                                                 else
  219.                                                 {
  220.                                                         redirected = 0;
  221.                                                 }
  222.                                                 // Loading the page is complete, free resources
  223.                                                 if (redirected>0)
  224.                                                 {
  225.                                                         http_free stdcall (http_transfer);
  226.                                                         http_transfer=0;
  227.                                                         PageLinks.GetAbsoluteURL(#URL);
  228.                                                         History.back();
  229.                                                         strcpy(#editURL, #URL);
  230.                                                         DrawEditBox();
  231.                                                         OpenPage();
  232.                                                 }
  233.                                                 else
  234.                                                 {
  235.                                                         History.add(#URL);
  236.                                                         ESI = http_transfer;
  237.                                                         bufpointer = ESI.http_msg.content_ptr;
  238.                                                         bufsize = ESI.http_msg.content_received;
  239.                                                         http_free stdcall (http_transfer);
  240.                                                         http_transfer=0;
  241.                                                         SetPageDefaults();
  242.                                                         ShowPage();
  243.                                                 }
  244.                                         }
  245.                                 }
  246.                 }
  247.         goto BEGIN_LOOP_APPLICATION;
  248. }
  249.  
  250. void SetElementSizes()
  251. {
  252.         address_box.top = TOOLBAR_H/2-7;
  253.         address_box.width = Form.cwidth - address_box.left - 25 - 22;
  254.         WB1.list.SetSizes(0, TOOLBAR_H, Form.width - 10 - scroll_wv.size_x / WB1.DrawBuf.zoom,
  255.                 Form.cheight - TOOLBAR_H - STATUSBAR_H, WB1.list.font_h + WB1.DrawBuf.zoom + WB1.DrawBuf.zoom * WB1.DrawBuf.zoom);
  256.         WB1.list.wheel_size = 7;
  257.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w;
  258.         WB1.list.visible = WB1.list.h - 5 / WB1.list.item_h;
  259.         if (WB1.list.w!=WB1.DrawBuf.bufw) WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, WB1.list.h * 30);
  260. }
  261.  
  262.  
  263.  
  264. void Draw_Window()
  265. {
  266.         int list__w;
  267.         DrawBar(0,0, Form.cwidth,TOOLBAR_H-2, panel_color);
  268.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, 0xD7D0D3);
  269.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, border_color);
  270.         SetElementSizes();
  271.         DrawRectangle(address_box.left-3, address_box.top-3, address_box.width+5, 20,border_color);
  272.         DefineButton(address_box.left-50, address_box.top-2, 23, skin.h-2, BACK_BUTTON+BT_HIDE, 0);
  273.         DefineButton(address_box.left-26, address_box.top-2, 23, skin.h-2, FORWARD_BUTTON+BT_HIDE, 0);
  274.         img_draw stdcall(skin.image, address_box.left-51, address_box.top-3, 48, skin.h, 3, 0);
  275.         DefineButton(address_box.left+address_box.width+1, address_box.top-3, 16, skin.h-1, REFRESH_BUTTON+BT_HIDE+BT_NOFRAME, 0);
  276.         DefineButton(Form.cwidth-24, address_box.top-3, 19, skin.h-1, SANDWICH_BUTTON+BT_HIDE, 0);
  277.         img_draw stdcall(skin.image, Form.cwidth-22, address_box.top-3, 16, skin.h, 85, 0);
  278.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,STATUSBAR_H, col_bg);
  279.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, border_color);
  280.         if (!header) OpenPage(); else { WB1.DrawPage(); DrawEditBox(); }
  281.         DrawRectangle(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, scroll_wv.size_y-1, scroll_wv.bckg_col);
  282.         DrawProgress();
  283.  
  284.         /*
  285.         list__w = 200;
  286.         font.buffer_size = 0;
  287.         font.size.height = 200;
  288.         font.color = 0x000000;
  289.         font.size.text = 11;
  290.         font.write_buf(10,10,list__w,font.size.height, "Hello World!");
  291.         font.color = 0xFF00FF;
  292.         font.size.text = 12;
  293.         font.write_buf(10,23,list__w,font.size.height, "How are you?");
  294.         font.color = 0x2E74BB;
  295.         font.size.text = 15;
  296.         font.write_buf(11,40,list__w,font.size.height, "Fine");
  297.         font.apply_smooth();
  298.         _PutImage(0,0,list__w,font.size.height,font.buffer);
  299.         */
  300. }
  301.  
  302.  
  303. void Scan(dword id__)
  304. {
  305.         action_buf=0;
  306.         if (WB1.list.ProcessKey(id__)) WB1.DrawPage();
  307.         else switch (id__)
  308.         {
  309.                 case SCAN_CODE_BS:
  310.                 case BACK_BUTTON:
  311.                         if (History.back()) {
  312.                                 strcpy(#URL, History.current());
  313.                                 OpenPage();
  314.                         }
  315.                         return;
  316.                 case FORWARD_BUTTON:
  317.                         if (History.forward()) {
  318.                                 strcpy(#URL, History.current());
  319.                                 OpenPage();
  320.                         }
  321.                         return;
  322.                 case GOTOURL_BUTTON:
  323.                 case SCAN_CODE_ENTER:
  324.                         if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/') || (!strncmp(#editURL,"WebView:",9))
  325.                         {
  326.                                 strcpy(#URL, #editURL);
  327.                         }
  328.                         else
  329.                         {
  330.                                 strlcpy(#URL,"http://",7);
  331.                                 strcat(#URL, #editURL);
  332.                         }
  333.                         OpenPage();
  334.                         return;
  335.                 case 063: //F5
  336.                         IF(address_box.flags & 0b10) return;
  337.                 case REFRESH_BUTTON:
  338.                         if (http_transfer > 0)
  339.                         {
  340.                                 StopLoading();
  341.                                 Draw_Window();
  342.                         }
  343.                         else OpenPage();
  344.                         return;
  345.                 case SANDWICH_BUTTON:
  346.                         mouse.y = TOOLBAR_H-6;
  347.                         mouse.x = Form.cwidth - 167;
  348.                         CreateThread(#menu_rmb,#stak+4092);
  349.                         return;
  350.                 case VIEW_SOURCE:
  351.                         WB1.list.first = 0;
  352.                         ShowSource();
  353.                         WB1.LoadInternalPage(bufpointer, bufsize);
  354.                         break;
  355.                 case EDIT_SOURCE:
  356.                         if (!strncmp(#URL,"http:",5))
  357.                         {
  358.                                 WriteFile(bufsize, bufpointer, "/tmp0/1/WebView_tmp.htm");
  359.                                 if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
  360.                         }
  361.                         else RunProgram("/rd/1/tinypad", #URL);
  362.                         return;
  363.                 case FREE_IMG_CACHE:
  364.                         ImgCache.Free();
  365.                         notify(IMAGES_CACHE_CLEARED);
  366.                         WB1.DrawPage();
  367.                         return;
  368.                 case VIEW_HISTORY:
  369.                         strcpy(#URL, URL_SERVICE_HISTORY);
  370.                         OpenPage();
  371.                         return;
  372.                 case DOWNLOAD_MANAGER:
  373.                         if (!downloader_opened) {
  374.                                 strlcpy(#DL_URL, "http://",7);
  375.                                 CreateThread(#Downloader,#downloader_stak+4092);
  376.                         }
  377.                         return;
  378.         }
  379. }
  380.  
  381.  
  382. void StopLoading()
  383. {
  384.         if (http_transfer)
  385.         {
  386.                 EAX = http_transfer;
  387.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  388.                 $push   EAX                                                     // save it on the stack
  389.                 http_free stdcall (http_transfer);      // abort connection
  390.                 $pop    EAX                                                    
  391.                 free(EAX);                                              // free data
  392.                 http_transfer=0;
  393.                 bufsize = 0;
  394.                 bufpointer = free(bufpointer);
  395.         }
  396.         wv_progress_bar.value = 0;
  397.         img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, 52, 0);
  398. }
  399.  
  400. void SetPageDefaults()
  401. {
  402.         strcpy(#header, #version);
  403.         WB1.list.count = WB1.list.first = 0;
  404.         stroka = 0;
  405.         cur_encoding = CH_NULL;
  406.         if (o_bufpointer) o_bufpointer = free(o_bufpointer);
  407.         anchor_line_num=WB1.list.first;
  408.         anchor[0]='|';
  409. }
  410.  
  411. void OpenPage()
  412. {
  413.         StopLoading();
  414.         souce_mode = false;
  415.         strcpy(#editURL, #URL);
  416.         History.add(#URL);
  417.         if (!strncmp(#URL,"WebView:",8))
  418.         {
  419.                 SetPageDefaults();
  420.                 if (!strcmp(#URL, URL_SERVICE_HOME)) WB1.LoadInternalPage(#homepage, sizeof(homepage));
  421.                 else if (!strcmp(#URL, URL_SERVICE_HISTORY)) ShowHistory();
  422.                 DrawEditBox();
  423.                 return;
  424.         }
  425.         if (!strncmp(#URL,"http:",5))
  426.         {
  427.                 img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, 131, 0);
  428.                 http_get stdcall (#URL, 0, 0, #accept_language);
  429.                 http_transfer = EAX;
  430.                 if (!http_transfer)
  431.                 {
  432.                         StopLoading();
  433.                         bufsize = 0;
  434.                         bufpointer = free(bufpointer);
  435.                         ShowPage();
  436.                         return;
  437.                 }
  438.         }
  439.         else
  440.         {
  441.                 file_size stdcall (#URL);
  442.                 bufsize = EBX;
  443.                 if (bufsize)
  444.                 {
  445.                         free(bufpointer);
  446.                         bufpointer = malloc(bufsize);
  447.                         SetPageDefaults();
  448.                         ReadFile(0, bufsize, bufpointer, #URL);
  449.                 }
  450.                 ShowPage();
  451.         }
  452. }
  453.  
  454. DrawEditBox()
  455. {
  456.         DrawWideRectangle(address_box.left-2, address_box.top-2, address_box.width+3, 19, 2, address_box.color);
  457.         address_box.size = address_box.pos = address_box.shift = address_box.shift_old = strlen(#editURL);
  458.         address_box.offset = 0;
  459.         edit_box_draw stdcall(#address_box);
  460.         if (http_transfer > 0) EAX = 131; else EAX = 52;
  461.         img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, EAX, 0);
  462. }
  463.  
  464.  
  465. void ShowPage()
  466. {
  467.         DrawEditBox();
  468.         if (!bufsize)
  469.         {
  470.                 if (http_transfer) WB1.LoadInternalPage(#loading, sizeof(loading));
  471.                 else WB1.LoadInternalPage(#page_not_found, sizeof(page_not_found));
  472.         }
  473.         else
  474.         {
  475.                 WB1.Prepare();
  476.         }
  477.         //if (!header) strcpy(#header, #version);
  478.         if (!strcmp(#version, #header)) DrawTitle(#header);
  479. }
  480.  
  481. byte UrlExtIs(dword ext)
  482. {
  483.         if (!strcmpi(#URL + strlen(#URL) - strlen(ext), ext)) return true;
  484.         return false;
  485. }
  486.  
  487. int SetSkinColors()
  488. {
  489.         dword image_data;
  490.         image_data = DSDWORD[skin.image+24];
  491.         col_bg = DSDWORD[image_data];
  492.         panel_color  = DSDWORD[skin.w*4*4 + image_data];
  493.         border_color = DSDWORD[skin.w*4*7 + image_data];
  494.         wv_progress_bar.progress_color = DSDWORD[skin.w*4*10 + image_data];
  495.         $and col_bg, 0x00ffffff
  496.         $and panel_color, 0x00ffffff
  497.         $and border_color, 0x00ffffff
  498.         $and wv_progress_bar.progress_color, 0x00ffffff
  499. }
  500.  
  501. void DrawProgress()
  502. {
  503.         unsigned long btn;
  504.         if (http_transfer == 0) return;
  505.         if (wv_progress_bar.max) btn = address_box.width*wv_progress_bar.value/wv_progress_bar.max; else btn = 30;
  506.         DrawBar(address_box.left-2, address_box.top+15, btn, 2, wv_progress_bar.progress_color);
  507. }
  508.  
  509. void ClickLink()
  510. {
  511.         if (http_transfer > 0)
  512.         {
  513.                 StopLoading();
  514.                 History.back();
  515.         }
  516.  
  517.         strcpy(#URL, PageLinks.GetURL(PageLinks.active));      
  518.         //#1
  519.         if (URL[0] == '#')
  520.         {
  521.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));              
  522.                 strcpy(#URL, History.current());
  523.                 WB1.list.first=WB1.list.count-WB1.list.visible;
  524.                 ShowPage();
  525.                 return;
  526.         }
  527.         //liner.ru#1
  528.         if (strrchr(#URL, '#')!=-1)
  529.         {
  530.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));
  531.                 URL[strrchr(#URL, '#')-1] = 0x00;
  532.         }
  533.        
  534.         PageLinks.GetAbsoluteURL(#URL);
  535.        
  536.         if (UrlExtIs(".png")==1) || (UrlExtIs(".gif")==1) || (UrlExtIs(".jpg")==1) || (UrlExtIs(".zip")==1) || (UrlExtIs(".kex")==1)
  537.         || (UrlExtIs(".7z")==1) || (UrlExtIs("netcfg")==1)
  538.         {
  539.                 //notify(#URL);
  540.                 if (!strncmp(#URL,"http://", 7))
  541.                 {
  542.                         strcpy(#DL_URL, #URL);
  543.                         CreateThread(#Downloader,#downloader_stak+4092);
  544.                 }
  545.                 else RunProgram("@open", #URL);
  546.                 strcpy(#editURL, History.current());
  547.                 strcpy(#URL, History.current());
  548.                 return;
  549.         }
  550.         if (!strncmp(#URL,"mailto:", 7))
  551.         {
  552.                 notify(#URL);
  553.                 strcpy(#editURL, History.current());
  554.                 strcpy(#URL, History.current());
  555.                 return;
  556.         }
  557.         OpenPage();
  558.         return;
  559. }
  560.  
  561.  
  562. char downloader_stak[4096];
  563. stop: