Subversion Repositories Kolibri OS

Rev

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

  1. //Copyright 2007-2020 by Veliant & Leency
  2. //Asper, lev, Lrz, Barsuk, Nable, hidnplayr...
  3.  
  4. //Licence restriction: compiling this app for WIN32 is forbidden.
  5.  
  6. #ifndef AUTOBUILD
  7.         #include "lang.h--"
  8. #endif
  9.  
  10. //libraries
  11. #define MEMSIZE 1024 * 1000
  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\random.h"
  18. #include "..\lib\clipboard.h"
  19.  
  20. // *.obj libraries
  21. #include "..\lib\obj\box_lib.h"
  22. #include "..\lib\obj\libio.h"
  23. #include "..\lib\obj\libimg.h"
  24. #include "..\lib\obj\http.h"
  25. #include "..\lib\obj\iconv.h"
  26. #include "..\lib\obj\proc_lib.h"
  27.  
  28. //useful patterns
  29. #include "..\lib\patterns\history.h"
  30. #include "..\lib\patterns\http_downloader.h"
  31. #include "..\lib\patterns\simple_open_dialog.h"
  32. #include "..\lib\patterns\toolbar_button.h"
  33. #include "..\lib\patterns\restart_process.h"
  34.  
  35. #include "texts.h"
  36. #include "cache.h"
  37. #include "show_src.h"
  38. #include "download_manager.h"
  39.  
  40. bool debug_mode = false;
  41.  
  42. enum {
  43.         NEW_TAB=600,
  44.         ENCODINGS=700,
  45.         BACK_BUTTON=800,
  46.         FORWARD_BUTTON,
  47.         REFRESH_BUTTON,
  48.         GOTOURL_BUTTON,
  49.         CHANGE_ENCODING,
  50.         SANDWICH_BUTTON,
  51.         VIEW_SOURCE,
  52.         EDIT_SOURCE,
  53.         OPEN_FILE,
  54.         NEW_WINDOW,
  55.         VIEW_HISTORY,
  56.         DOWNLOAD_MANAGER,
  57.         CLEAR_CACHE,
  58.         UPDATE_BROWSER,
  59.         IN_NEW_TAB,
  60.         IN_NEW_WINDOW,
  61.         COPY_LINK_URL,
  62.         DOWNLOAD_LINK_CONTENTS,
  63.         TAB_ID,
  64.         TAB_CLOSE_ID = 900
  65. };
  66.  
  67. enum { TARGET_SAME_TAB, TARGET_NEW_WINDOW, TARGET_NEW_TAB };
  68.  
  69. #include "..\TWB\TWB.c" //HTML Parser, a core component
  70.  
  71. TWebBrowser WB1;
  72. _history history;
  73.  
  74. #include "history.h"
  75.  
  76. #define PADDING 9
  77. #define TSZE 25
  78. #define STATUSBAR_H 15
  79. #define TAB_H 20
  80. dword TOOLBAR_H = PADDING+TSZE+PADDING+2;
  81.  
  82. #define URL_SIZE 4000
  83.  
  84. int action_buf;
  85.  
  86. _http http = 0;
  87.  
  88. bool source_mode = false;
  89.  
  90. progress_bar wv_progress_bar;
  91. char stak[4096];
  92. proc_info Form;
  93.  
  94. int menu_id=NULL;
  95.  
  96. #include "tabs.h"
  97.  
  98. char default_dir[] = "/rd/1";
  99. od_filter filter2 = { 22, "TXT\0HTM\0HTML\0DOCX\0\0" };
  100.  
  101. char editURL[URL_SIZE+1];
  102. edit_box address_box = {, PADDING+TSZE*2+PADDING+6, PADDING+3, 0xffffff,
  103.         0x94AECE, 0xffffff, 0xffffff,0x10000000,URL_SIZE-2,#editURL,0,,19,19};
  104.  
  105. char editbox_icons[] = FROM "editbox_icons.raw";
  106.  
  107. dword shared_url;
  108.  
  109. void LoadLibraries()
  110. {
  111.         load_dll(boxlib,    #box_lib_init,0);
  112.         load_dll(libio,     #libio_init,1);
  113.         load_dll(libimg,    #libimg_init,1);
  114.         load_dll(libHTTP,   #http_lib_init,1);
  115.         load_dll(iconv_lib, #iconv_open,0);
  116.         load_dll(Proc_lib,  #OpenDialog_init,0);
  117.         OpenDialog_init stdcall (#o_dialog);   
  118. }
  119.  
  120. void HandleParam()
  121. {
  122.         if (param) {
  123.                 if (!strncmp(#param, "-download_and_exit ", 19)) {
  124.                         download_and_exit = true;
  125.                         strcpy(#downloader_edit, #param+19);
  126.                         Downloader();
  127.                         ExitProcess();
  128.                 } else if (!strncmp(#param, "-download ", 10)) {
  129.                         strcpy(#downloader_edit, #param+10);
  130.                         Downloader();
  131.                         ExitProcess();
  132.                 } else if (!strncmp(#param, "-source ", 8)) {
  133.                         source_mode = true;
  134.                         history.add(#param + 8);
  135.                 } else if (!strncmp(#param, "-new ", 5)) {
  136.                         history.add(#param + 5);
  137.                 } else {
  138.                         if (GetProcessesCount("WEBVIEW") == 1) {
  139.                                 history.add(#param);
  140.                         } else {
  141.                                 shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
  142.                                 strncpy(shared_url, #param, URL_SIZE);
  143.                                 ExitProcess();
  144.                         }
  145.                 }
  146.         } else {
  147.                 history.add(URL_SERVICE_HOMEPAGE);
  148.         }
  149.         shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_CREATE + SHM_WRITE);
  150.         ESDWORD[shared_url] = '\0';
  151. }
  152.  
  153. void main()
  154. {
  155.         int i, redirect_count=0;
  156.         LoadLibraries();
  157.         CreateDir("/tmp0/1/Downloads");
  158.         //CreateDir("/tmp0/1/WebView_Cache");
  159.         HandleParam();
  160.         WB1.list.SetFont(8, 14, 10011000b);
  161.         WB1.list.no_selection = true;
  162.         WB1.custom_encoding = -1;
  163.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  164.         loop() switch(@WaitEventTimeout(30))
  165.         {
  166.                 case evMouse:
  167.                         edit_box_mouse stdcall (#address_box);
  168.                         mouse.get();
  169.  
  170.                         if (WB1.list.MouseScroll(mouse.vert)) WB1.DrawPage();
  171.  
  172.                         scrollbar_v_mouse (#scroll_wv);
  173.                         if (scroll_wv.delta) {
  174.                                 WB1.list.first = scroll_wv.position;
  175.                                 WB1.DrawPage();
  176.                                 break;
  177.                         }
  178.  
  179.                         if (links.hover(WB1.list.y, WB1.list.first))
  180.                         {
  181.                                 if (mouse.mkm) {
  182.                                         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
  183.                                                 EventClickLink(TARGET_NEW_WINDOW);
  184.                                         } else {
  185.                                                 EventClickLink(TARGET_NEW_TAB);
  186.                                         }
  187.                                 }
  188.                                 if (mouse.lkm) {
  189.                                         CursorPointer.Restore();
  190.                                         EventClickLink(TARGET_SAME_TAB);
  191.                                 }
  192.                                 if (mouse.pkm) {
  193.                                         CursorPointer.Restore();
  194.                                         EventShowLinkMenu();
  195.                                 }
  196.                         } else {
  197.                                 CursorPointer.Restore();
  198.                                 if (mouse.pkm) && (WB1.list.MouseOver(mouse.x, mouse.y)) {
  199.                                         EventShowPageMenu();
  200.                                 }
  201.                         }
  202.                         break;
  203.  
  204.                 case evButton:
  205.                         ProcessEvent( GetButtonID() );
  206.                         break;
  207.  
  208.                 case evKey:
  209.                         GetKeys();
  210.  
  211.                         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
  212.                                 if (key_scancode == SCAN_CODE_TAB) {EventActivatePreviousTab();break;}
  213.                         }
  214.  
  215.                         if (ProcessCtrlKeyEvent()) break;
  216.                        
  217.                         if (key_scancode == SCAN_CODE_F5) ProcessEvent(REFRESH_BUTTON);
  218.                        
  219.                         if (address_box.flags & ed_focus)  
  220.                         {
  221.                                 if (key_scancode == SCAN_CODE_ENTER) {
  222.                                         ProcessEvent(key_scancode);
  223.                                 } else {
  224.                                         EAX = key_editbox;
  225.                                         edit_box_key stdcall(#address_box);
  226.                                 }
  227.                         } else {
  228.                                 #define KEY_SCROLL_N 11
  229.                                 if (SCAN_CODE_UP   == key_scancode) for (i=0;i<KEY_SCROLL_N;i++) WB1.list.KeyUp();
  230.                                 if (SCAN_CODE_DOWN == key_scancode) for (i=0;i<KEY_SCROLL_N;i++) WB1.list.KeyDown();
  231.                                 if (key_scancode == SCAN_CODE_F6) {address_box.flags=ed_focus; DrawOmnibox();}
  232.                                 if (WB1.list.ProcessKey(key_scancode)) WB1.DrawPage();
  233.                                 else ProcessEvent(key_scancode);
  234.                         }
  235.                         break;
  236.  
  237.                 case evReDraw:
  238.                         DefineAndDrawWindow(GetScreenWidth()-800/2-random(80), //40
  239.                                 GetScreenHeight()-700/2-random(80),800,700,0x73,0,0,0);
  240.                         GetProcessInfo(#Form, SelfInfo);
  241.                         ProcessMenuClick();
  242.                         sc.get();
  243.                         if (Form.status_window>2) break;
  244.                         if (Form.height<120) { MoveSize(OLD,OLD,OLD,120); break; }
  245.                         if (Form.width<280) { MoveSize(OLD,OLD,280,OLD); break; }
  246.                         draw_window();
  247.                         break;
  248.                        
  249.                 case evNetwork:
  250.                         if (http.transfer <= 0) break;
  251.                         http.receive();
  252.                         EventUpdateProgressBar();
  253.                         if (http.receive_result != 0) break;
  254.                         if (http.status_code >= 300) && (http.status_code < 400)
  255.                         {
  256.                                 // Handle redirects
  257.                                 if (redirect_count<=5) {
  258.                                         redirect_count++;
  259.                                         http.handle_redirect();
  260.                                         http.free();
  261.                                         GetAbsoluteURL(#http.redirect_url, history.current());
  262.                                         history.back();
  263.                                         OpenPage(#http.redirect_url);
  264.                                 } else {
  265.                                         notify("'Too many redirects.' -E");
  266.                                         StopLoading();
  267.                                         redirect_count = 0;
  268.                                 }
  269.                         } else {
  270.                                 // Loading the page is complete, free resources
  271.                                 redirect_count = 0;
  272.                                 http.free();
  273.                                 pages_cache.add(history.current(), http.content_pointer, http.content_received);
  274.                                 LoadInternalPage(http.content_pointer, http.content_received);
  275.                         }
  276.                         break;
  277.                 default:
  278.                         if (ESDWORD[shared_url] != '\0') {
  279.                                 EventOpenNewTab(shared_url);
  280.                                 ESDWORD[shared_url] = '\0';
  281.                                 ActivateWindow(GetProcessSlot(Form.ID));
  282.                         }
  283.         }
  284. }
  285.  
  286. bool ProcessCtrlKeyEvent()
  287. {
  288.         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
  289.         {
  290.                 case SCAN_CODE_KEY_O:
  291.                         EventOpenDialog();
  292.                         return true;
  293.                 case SCAN_CODE_KEY_H:
  294.                         ProcessEvent(VIEW_HISTORY);
  295.                         return true;
  296.                 case SCAN_CODE_KEY_U:
  297.                         EventViewSource();
  298.                         return true;
  299.                 case SCAN_CODE_KEY_T:
  300.                         EventOpenNewTab(URL_SERVICE_HOMEPAGE);
  301.                         return true;
  302.                 case SCAN_CODE_KEY_N:
  303.                         RunProgram(#program_path, NULL);
  304.                         return true;
  305.                 case SCAN_CODE_KEY_J:
  306.                         ProcessEvent(DOWNLOAD_MANAGER);
  307.                         return true;
  308.                 case SCAN_CODE_KEY_R:
  309.                         ProcessEvent(REFRESH_BUTTON);
  310.                         return true;
  311.                 case SCAN_CODE_ENTER:
  312.                         EventSeachWeb();
  313.                         return true;
  314.                 case SCAN_CODE_LEFT:
  315.                          ProcessEvent(BACK_BUTTON);
  316.                          return true;
  317.                 case SCAN_CODE_RIGHT:
  318.                         ProcessEvent(FORWARD_BUTTON);
  319.                         return true;
  320.                 case SCAN_CODE_KEY_W:
  321.                         EventCloseActiveTab();
  322.                         return true;
  323.                 case SCAN_CODE_TAB:
  324.                         EventActivateNextTab();
  325.                         return true;
  326.         }
  327.         return false;
  328. }
  329.  
  330. void SetElementSizes()
  331. {
  332.         address_box.width = Form.cwidth - address_box.left - 52 - 16;
  333.         WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.width - 10 - scroll_wv.size_x,
  334.                 Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
  335.         WB1.list.wheel_size = 7 * BASIC_LINE_H;
  336.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
  337.         WB1.list.visible = WB1.list.h;
  338. }
  339.  
  340.  
  341. void draw_window()
  342. {
  343.         int i;
  344.         bool burger_active = false;
  345.         if (menu_id == OPEN_FILE) burger_active = true;
  346.  
  347.         SetElementSizes();
  348.  
  349.         DrawBar(0,0, Form.cwidth,PADDING, sc.work);
  350.         DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
  351.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.work_dark, sc.work, 180));
  352.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.work_graph);
  353.         DrawBar(0, PADDING, address_box.left-2, TSZE+1, sc.work);
  354.         DrawBar(address_box.left+address_box.width+18, PADDING, Form.cwidth-address_box.left-address_box.width-18, TSZE+1, sc.work);
  355.  
  356.         DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
  357.         DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
  358.         DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
  359.  
  360.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.work_graph);
  361.  
  362.         DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
  363.                 WB1.list.h-1, scroll_wv.bckg_col);
  364.  
  365.         if (!BrowserWidthChanged()) {
  366.                 WB1.DrawPage();
  367.                 DrawOmnibox();
  368.         }
  369.         DrawProgress();
  370.         DrawStatusBar();
  371.         DrawTabsBar();
  372. }
  373.  
  374. bool BrowserWidthChanged()
  375. {
  376.         dword source_mode_holder;
  377.         if (WB1.list.w!=DrawBuf.bufw) {
  378.                 DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, 400*20);
  379.                 if (!strncmp(history.current(),"http",4)) {
  380.                         //nihuya ne izyashnoe reshenie, no pust' poka butet tak
  381.                         source_mode_holder = source_mode;
  382.                         LoadInternalPage(#loading_text, sizeof(loading_text));
  383.                         source_mode = source_mode_holder;
  384.                 }
  385.                 OpenPage(history.current());
  386.                 return true;
  387.         }
  388.         return false;
  389. }
  390.  
  391.  
  392. void EventChangeEncodingAndLoadPage(int _new_encoding)
  393. {
  394.         dword newbuf, newsize;
  395.         WB1.custom_encoding = _new_encoding;
  396.         newsize = strlen(WB1.o_bufpointer);
  397.         newbuf = malloc(newsize);
  398.         memmov(newbuf, WB1.o_bufpointer, newsize);
  399.         LoadInternalPage(newbuf, newsize);
  400.         free(newbuf);
  401. }
  402.  
  403.  
  404. void ProcessEvent(dword id__)
  405. {
  406.         switch (id__)
  407.         {
  408.                 case 1:
  409.                         ExitProcess();
  410.                         break;
  411.                 case ENCODINGS...ENCODINGS+6:
  412.                         EventChangeEncodingAndLoadPage(id__-ENCODINGS);
  413.                         return;
  414.                 case NEW_WINDOW:
  415.                         RunProgram(#program_path, NULL);
  416.                         return;
  417.                 case SCAN_CODE_BS:
  418.                 case BACK_BUTTON:
  419.                         if (history.back()) {
  420.                                 OpenPage(history.current());
  421.                         }
  422.                         return;
  423.                 case FORWARD_BUTTON:
  424.                         if (history.forward()) {
  425.                                 OpenPage(history.current());
  426.                         }
  427.                         return;
  428.                 case GOTOURL_BUTTON:
  429.                 case SCAN_CODE_ENTER:
  430.                         EventSubmitOmnibox();
  431.                         return;
  432.                 case REFRESH_BUTTON:
  433.                         EventRefreshPage();
  434.                         return;
  435.                 case CHANGE_ENCODING:
  436.                         EventShowEncodingsList();
  437.                         return;
  438.                 case SANDWICH_BUTTON:
  439.                         EventShowMainMenu();
  440.                         return;
  441.                 case VIEW_SOURCE:
  442.                         EventViewSource();
  443.                         break;
  444.                 case EDIT_SOURCE:
  445.                         if (check_is_the_adress_local(history.current())) {
  446.                                 RunProgram("/rd/1/tinypad", history.current());
  447.                         } else {
  448.                                 CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
  449.                                 if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
  450.                         }
  451.                         return;
  452.                 case VIEW_HISTORY:
  453.                         OpenPage(URL_SERVICE_HISTORY);
  454.                         return;
  455.                 case DOWNLOAD_MANAGER:
  456.                         if (!downloader_opened) {
  457.                                 downloader_edit = NULL;
  458.                                 CreateThread(#Downloader,#downloader_stak+4092);
  459.                         }
  460.                         return;
  461.                 case UPDATE_BROWSER:
  462.                         EventUpdateBrowser();
  463.                         return;
  464.                 case CLEAR_CACHE:
  465.                         pages_cache.clear();
  466.                         notify(#clear_cache_ok);
  467.                         EventRefreshPage();
  468.                         return;
  469.                 case IN_NEW_TAB:
  470.                         EventClickLink(TARGET_NEW_TAB);
  471.                         return;
  472.                 case IN_NEW_WINDOW:
  473.                         EventClickLink(TARGET_NEW_WINDOW);
  474.                         return;
  475.                 case COPY_LINK_URL:
  476.                         Clipboard__CopyText(GetAbsoluteActiveURL());
  477.                         notify("'URL copied to clipboard'O");
  478.                         return;
  479.                 case DOWNLOAD_LINK_CONTENTS:
  480.                         if (!downloader_opened) {
  481.                                 strcpy(#downloader_edit, GetAbsoluteActiveURL());
  482.                                 CreateThread(#Downloader,#downloader_stak+4092);
  483.                         }
  484.                         return;
  485.                 case OPEN_FILE:
  486.                         EventOpenDialog();
  487.                         return;
  488.                 case SCAN_CODE_F12:
  489.                         debug_mode ^= 1;
  490.                         if (debug_mode) notify("'Debug mode ON'-I");
  491.                         else notify("'Debug mode OFF'-I");
  492.                         return;
  493.                 case NEW_TAB:
  494.                         if (http.transfer) break;
  495.                         EventOpenNewTab(URL_SERVICE_HOMEPAGE);
  496.                         return;
  497.                 case TAB_ID...TAB_ID+TABS_MAX:
  498.                         if (http.transfer) break;
  499.                         if (mouse.mkm) {
  500.                                 EventTabClose(id__ - TAB_ID);
  501.                         } else {
  502.                                 EventTabClick(id__ - TAB_ID);
  503.                         }
  504.                         return;
  505.                 case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX:
  506.                         EventTabClose(id__ - TAB_CLOSE_ID);
  507.                         return;
  508.         }
  509. }
  510.  
  511. void StopLoading()
  512. {
  513.         if (http.transfer)
  514.         {
  515.                 EAX = http.transfer;
  516.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  517.                 $push   EAX                                                     // save it on the stack
  518.                 http_free stdcall (http.transfer);      // abort connection
  519.                 $pop    EAX                                                    
  520.                 free(EAX);                                              // free data
  521.                 http.transfer=0;
  522.                 pause(10);
  523.         }
  524.         wv_progress_bar.value = 0;
  525.         DrawOmnibox();
  526. }
  527.  
  528. //rewrite into
  529. //bool strrpl(dword dst, from, into, dst_len);
  530. bool ReplaceSpaceInUrl(dword url, size) {
  531.         unsigned int i, j;
  532.         bool was_changed=false;
  533.         for (i=url+size-3; i>url; i--)
  534.         {
  535.                 if (ESBYTE[i]!=' ') continue;
  536.                 for (j=url+size-3; j>=i; j--) {
  537.                         ESBYTE[j+3]=ESBYTE[j+2];
  538.                         ESBYTE[j+2]=ESBYTE[j+1];
  539.                         ESBYTE[j+1]=ESBYTE[j];
  540.                 }
  541.                 ESBYTE[i] = '%';
  542.                 ESBYTE[i+1] = '2';
  543.                 ESBYTE[i+2] = '0';
  544.                 was_changed = true;
  545.         }
  546.         return was_changed;
  547. }
  548.  
  549. bool HandleUrlFiles(dword _path, _data)
  550. {
  551.         dword url_from_file;
  552.         if (!UrlExtIs(_path, "url")) return false;
  553.         if (! url_from_file = strstri(_data, "URL=")) return false;
  554.         replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
  555.         OpenPage(url_from_file);        
  556.         return true;   
  557. }
  558.  
  559. bool GetLocalFileData(dword _path)
  560. {
  561.         dword data, size;
  562.         file_size stdcall (_path);
  563.         if (!EBX) return false;
  564.  
  565.         size = EBX;
  566.         data = malloc(size);
  567.         ReadFile(0, size, data, _path);
  568.         if (!HandleUrlFiles(_path, data)) {
  569.                 LoadInternalPage(data, size);
  570.         }
  571.         free(data);
  572.         return true;
  573. }
  574.  
  575. void OpenPage(dword _open_URL)
  576. {
  577.         char new_url[URL_SIZE+1];
  578.         char new_url_full[URL_SIZE+1];
  579.         int unz_id;
  580.  
  581.         StopLoading();
  582.  
  583.         SetOmniboxText(_open_URL);
  584.  
  585.         strncpy(#new_url, _open_URL, URL_SIZE);
  586.  
  587.         //Exclude # from the URL to the load page
  588.         //We will bring it back when we get the buffer
  589.         if (strrchr(#new_url, '#')) {
  590.                 anchors.take_anchor_from(#new_url);
  591.         }
  592.  
  593.         history.add(#new_url);
  594.  
  595.         if (pages_cache.has(#new_url)) {
  596.                 //CACHED PAGE
  597.                 LoadInternalPage(pages_cache.current_page_buf, pages_cache.current_page_size);
  598.  
  599.         } else if (!strncmp(#new_url,"WebView:",8)) {
  600.                 //INTERNAL PAGE
  601.                 if (!strcmp(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#homepage, sizeof(homepage));
  602.                 else if (!strcmp(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#help, sizeof(help));
  603.                 else if (!strcmp(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
  604.                 else LoadInternalPage(#page_not_found, sizeof(page_not_found));
  605.  
  606.         } else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
  607.                 //WEB PAGE
  608.                 if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
  609.                         strcpy(#editURL, #new_url);
  610.                 }
  611.  
  612.                 if (!strncmp(#new_url,"http:",5)) {
  613.                         http.get(#new_url);
  614.                 } else if (!strncmp(#new_url,"https://",8)) {
  615.                         strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
  616.                         strncat(#new_url_full, #new_url, URL_SIZE);
  617.                         http.get(#new_url_full);
  618.                 }
  619.  
  620.                 DrawOmnibox();
  621.  
  622.                 if (!http.transfer) {
  623.                         StopLoading();
  624.                         LoadInternalPage(#page_not_found, sizeof(page_not_found));
  625.                 }
  626.         } else {
  627.                 //LOCAL PAGE
  628.                 if (UrlExtIs(#new_url,".docx")) {
  629.                         DeleteFile("/tmp0/1/temp/word/document.xml");
  630.                         CreateDir("/tmp0/1/temp");
  631.                         unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
  632.                         while (GetProcessSlot(unz_id)) pause(2);
  633.                         strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
  634.                 }
  635.                 if (!GetLocalFileData(#new_url)) {
  636.                         LoadInternalPage(#page_not_found, sizeof(page_not_found));
  637.                 }
  638.         }
  639. }
  640.  
  641.  
  642. bool EventClickAnchor()
  643. {
  644.         dword aURL = links.active_url;
  645.  
  646.         if (anchors.get_pos_by_name(aURL+1)!=-1) {
  647.                 WB1.list.first = anchors.get_pos_by_name(aURL+1);
  648.                 //WB1.list.CheckDoesValuesOkey();
  649.                 strcpy(#editURL, history.current());
  650.                 strcat(#editURL, aURL);
  651.                 DrawOmnibox();
  652.                 WB1.DrawPage();
  653.                 return true;
  654.         }
  655.         return false;
  656. }
  657.  
  658. void EventClickLink(dword _target)
  659. {
  660.         char new_url[URL_SIZE+1];
  661.         char new_url_full[URL_SIZE+1];
  662.         dword aURL = GetAbsoluteActiveURL();
  663.         if (!aURL) return;
  664.  
  665.         strcpy(#new_url, aURL);
  666.  
  667.         if (ESBYTE[aURL]=='#') {
  668.                 if (_target == TARGET_SAME_TAB) {
  669.                         EventClickAnchor();
  670.                         return;
  671.                 } else {
  672.                         strcpy(#new_url, history.current());
  673.                         strcat(#new_url, aURL);
  674.                 }
  675.         }
  676.  
  677.         if (_target == TARGET_NEW_TAB) {
  678.                 EventOpenNewTab(#new_url);
  679.                 return;
  680.         }
  681.  
  682.         if (_target == TARGET_NEW_WINDOW) {
  683.                 strcpy(#new_url_full, "-new ");
  684.                 strncat(#new_url_full, #new_url, URL_SIZE);
  685.                 RunProgram(#program_path, #new_url_full);
  686.                 return;
  687.         }
  688.  
  689.         if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
  690.                 notify(#new_url);
  691.                 return;
  692.         }
  693.  
  694.         if (http.transfer) {
  695.                 StopLoading();
  696.                 history.back();
  697.         }
  698.  
  699.         if (strrchr(#new_url, '#')!=0) {
  700.                 anchors.take_anchor_from(#new_url);
  701.                 OpenPage(#new_url);
  702.                 return;
  703.         }
  704.  
  705.         if (!strncmp(#new_url,"WebView:",8)) {
  706.                 OpenPage(#new_url);
  707.                 return;
  708.         }
  709.  
  710.         if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
  711.         {
  712.                 if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
  713.                 {      
  714.                         if (strchr(#new_url, '|')) {
  715.                                 ESBYTE[strchr(#new_url, '|')] = NULL;
  716.                                 RunProgram(#new_url, strlen(#new_url)+1+#new_url);
  717.                         } else {
  718.                                 RunProgram("/sys/@open", #new_url);
  719.                         }
  720.                         return;
  721.                 }
  722.         } else {
  723.                 if (UrlExtIs(#new_url,".png")==true) || (UrlExtIs(#new_url,".jpg")==true)
  724.                 || (UrlExtIs(#new_url,".zip")==true) || (UrlExtIs(#new_url,".kex")==true) || (UrlExtIs(#new_url,".pdf")==true)
  725.                 || (UrlExtIs(#new_url,".7z")==true) {
  726.                         if (!downloader_opened) {
  727.                                 strcpy(#downloader_edit, #new_url);
  728.                                 CreateThread(#Downloader,#downloader_stak+4092);
  729.                         }
  730.                         else notify("'WebView\nPlease, start a new download only when previous ended.'Et");
  731.                         return;
  732.                 }
  733.         }
  734.         OpenPage(#new_url);
  735. }
  736.  
  737. void EventSubmitOmnibox()
  738. {
  739.         char new_url[URL_SIZE+1];
  740.         if (!editURL[0]) return;
  741.         if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
  742.         || (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
  743.                 OpenPage(#editURL);
  744.         } else {
  745.                 strcpy(#new_url, "http://");
  746.                 strncat(#new_url, #editURL, URL_SIZE-1);
  747.                 OpenPage(#new_url);
  748.         }
  749. }
  750.  
  751. void LoadInternalPage(dword _bufdata, _in_bufsize){
  752.         if (!_bufdata) || (!_in_bufsize) {
  753.                 LoadInternalPage(#page_not_found, sizeof(page_not_found));
  754.         } else {
  755.                 WB1.list.first = 0; //scroll page to the top
  756.                 DrawOmnibox();
  757.                 if(!strrchr(#editURL, '#')) {
  758.                         strcat(#editURL, #anchors.current);
  759.                         DrawOmnibox();
  760.                 }
  761.                 WB1.ParseHtml(_bufdata, _in_bufsize);
  762.                 DrawStatusBar();
  763.                 DrawActiveTab();
  764.                 if (source_mode) {
  765.                         source_mode = false;
  766.                         WB1.custom_encoding = CH_CP866;
  767.                         ShowSource(WB1.bufpointer, _in_bufsize);
  768.                 } else {
  769.                         WB1.DrawPage();                
  770.                 }
  771.         }
  772. }
  773.  
  774. bool UrlExtIs(dword base, ext)
  775. {
  776.         if (!strcmpi(base + strlen(base) - strlen(ext), ext)) return true;
  777.         return false;
  778. }
  779.  
  780. void DrawProgress()
  781. {
  782.         dword persent;
  783.         if (http.transfer == 0) return;
  784.         if (wv_progress_bar.max) {
  785.                 persent = wv_progress_bar.value*100/wv_progress_bar.max;
  786.         } else {
  787.                 persent = 10;
  788.         }
  789.         DrawBar(address_box.left-1, address_box.top+20, persent*address_box.width+16/100, 2, 0x72B7EB);
  790. }
  791.  
  792. void EventShowPageMenu()
  793. {
  794.         open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #rmb_menu);
  795.         menu_id = VIEW_SOURCE;
  796. }
  797.  
  798. void EventShowLinkMenu()
  799. {
  800.         open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #link_menu);
  801.         menu_id = IN_NEW_TAB;
  802. }
  803.  
  804. void EventShowMainMenu()
  805. {
  806.         open_lmenu(Form.left + Form.cwidth - PADDING, Form.top + skin_height + PADDING + TSZE + 3,
  807.                 MENU_ALIGN_TOP_RIGHT, NULL, #main_menu);
  808.         menu_id = OPEN_FILE;
  809. }
  810.  
  811. void EventShowEncodingsList()
  812. {
  813.         open_lmenu(Form.left + Form.cwidth, Form.top + skin_height + Form.cheight - STATUSBAR_H + 12,
  814.                 MENU_ALIGN_BOT_RIGHT, WB1.cur_encoding + 1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
  815.         menu_id = ENCODINGS;
  816. }
  817.  
  818. void ProcessMenuClick()
  819. {
  820.         int click_id;
  821.         if (menu_id) {
  822.                 if (click_id = get_menu_click()) {
  823.                         click_id += menu_id - 1;
  824.                         ProcessEvent(click_id);
  825.                 }
  826.                 if (!menu_process_id) menu_id = NULL;
  827.         }
  828. }
  829.  
  830. void EventUpdateProgressBar()
  831. {
  832.         wv_progress_bar.max = http.content_length;
  833.         if (wv_progress_bar.value != http.content_received)
  834.         {
  835.                 wv_progress_bar.value = http.content_received; 
  836.                 DrawProgress();
  837.         }
  838. }
  839.  
  840. void EventSeachWeb()
  841. {
  842.         char new_url[URL_SIZE+1];
  843.         replace_char(#editURL, ' ', '_', URL_SIZE);
  844.         strcpy(#new_url, "https://www.google.com/search?q=");
  845.         strncat(#new_url, #editURL, URL_SIZE);
  846.         OpenPage(#new_url);
  847. }
  848.  
  849. void EventOpenDialog()
  850. {
  851.         OpenDialog_start stdcall (#o_dialog);
  852.         if (o_dialog.status) {
  853.                 OpenPage(#openfile_path);
  854.         }
  855. }
  856.  
  857. void EventViewSource()
  858. {
  859.         char source_view_param[URL_SIZE+1];
  860.         //strcpy(#source_view_param, "-source ");
  861.         //strncat(#source_view_param, history.current(), URL_SIZE);
  862.         //RunProgram(#program_path, #source_view_param);
  863.         source_mode = true;
  864.         EventOpenNewTab(history.current());
  865. }
  866.  
  867. void EventRefreshPage()
  868. {
  869.         if (http.transfer) {
  870.                 StopLoading();
  871.                 draw_window();
  872.         } else {
  873.                 OpenPage(history.current());
  874.         }
  875. }
  876.  
  877. dword GetFileSize(dword _path)
  878. {
  879.         BDVK bdvk;
  880.         if (GetFileInfo(_path, #bdvk)!=0) {
  881.                 return 0;
  882.         } else {
  883.                 return bdvk.sizelo;
  884.         }
  885. }
  886.  
  887. void EventUpdateBrowser()
  888. {
  889.         dword downloader_id, slot_n;
  890.         dword current_size;
  891.         dword new_size;
  892.         int error;
  893.  
  894.         draw_window();
  895.  
  896.         downloader_id = RunProgram(#program_path, #update_param);
  897.         do {
  898.                 slot_n = GetProcessSlot(downloader_id);
  899.                 pause(10);
  900.         } while (slot_n!=0);
  901.  
  902.         current_size = GetFileSize(#program_path);
  903.         new_size = GetFileSize("/tmp0/1/Downloads/WebView.com");
  904.  
  905.         if (!new_size) || (new_size<5000) {
  906.                 notify(#update_download_error);
  907.                 return;
  908.         }
  909.  
  910.         if (current_size == new_size) {
  911.                 notify(#update_is_current);
  912.                 return;
  913.         }
  914.  
  915.         if (error = CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
  916.                 notify(#update_can_not_copy);
  917.         } else {
  918.                 notify(#update_ok);
  919.                 RunProgram(#program_path, history.current());
  920.                 ExitProcess();
  921.         }
  922. }
  923.  
  924. void DrawStatusBar()
  925. {
  926.         dword status_y = Form.cheight - STATUSBAR_H + 4;
  927.         dword status_w = Form.cwidth - 90;
  928.         DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
  929.         if (links.active_url) {
  930.                 ESI = math.min(status_w/6, strlen(links.active_url));
  931.                 WriteText(10, status_y, 0, sc.work_text, links.active_url);
  932.         }
  933.         DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
  934.         WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
  935. }
  936.  
  937. void DrawOmnibox()
  938. {
  939.         int imgxoff;
  940.        
  941.         DrawOvalBorder(address_box.left-2, address_box.top-3, address_box.width+18, 24, sc.work_graph,
  942.                 sc.work_graph, sc.work_graph, sc.work_dark);
  943.         DrawBar(address_box.left-1, address_box.top-2, address_box.width+18, 1, 0xD8DCD8);
  944.         DrawBar(address_box.left-1, address_box.top-1, address_box.width+18, 1, address_box.color);
  945.         DrawBar(address_box.left-1, address_box.top, 1, 22, address_box.color);
  946.  
  947.         if (address_box.flags & ed_focus) address_box.flags = ed_focus; else address_box.flags = 0;
  948.         EditBox_UpdateText(#address_box, address_box.flags);
  949.         edit_box_draw stdcall(#address_box);
  950.         if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
  951.         _PutImage(address_box.left+address_box.width+1, address_box.top-1, 16, 23, imgxoff + #editbox_icons);
  952.         DefineHiddenButton(address_box.left+address_box.width-1, address_box.top-2, 17, 23, REFRESH_BUTTON);
  953.  
  954.         DrawProgress();
  955. }
  956.  
  957. void SetOmniboxText(dword _text)
  958. {
  959.         strcpy(#editURL, _text);
  960.         address_box.flags=0;
  961.         DrawOmnibox();
  962. }
  963.  
  964. dword GetAbsoluteActiveURL()
  965. {
  966.         char abs_url[URL_SIZE];
  967.         if (links.active_url) {
  968.                 strncpy(#abs_url, links.active_url, URL_SIZE);
  969.                 GetAbsoluteURL(#abs_url, history.current());           
  970.                 return #abs_url;
  971.         }
  972.         return 0;
  973. }
  974.  
  975. stop: