Subversion Repositories Kolibri OS

Rev

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