Subversion Repositories Kolibri OS

Rev

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