Subversion Repositories Kolibri OS

Rev

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