Subversion Repositories Kolibri OS

Rev

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