Subversion Repositories Kolibri OS

Rev

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