Subversion Repositories Kolibri OS

Rev

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