Subversion Repositories Kolibri OS

Rev

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