Subversion Repositories Kolibri OS

Rev

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