Subversion Repositories Kolibri OS

Rev

Rev 8278 | Rev 8292 | 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.7";
  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.check_content_type()) && (!strncmp(#http.content_type,"application",11)) {
  244.                                 EventOpenDownloader(history.current());
  245.                                 StopLoading();
  246.                                 history.back();
  247.                                 EventRefreshPage();
  248.                         }
  249.                         if (http.receive_result != 0) break;
  250.                         if (http.status_code >= 300) && (http.status_code < 400)
  251.                         {
  252.                                 // Handle redirects
  253.                                 if (redirect_count<=5) {
  254.                                         redirect_count++;
  255.                                         http.handle_redirect();
  256.                                         history.back();
  257.                                         if (http_get_type==PAGE) OpenPage(#http.redirect_url);
  258.                                         else if (http_get_type==IMG) http.get(#http.redirect_url);
  259.                                 } else {
  260.                                         notify("'Too many redirects.' -E");
  261.                                         StopLoading();
  262.                                         redirect_count = 0;
  263.                                 }
  264.                         } else {
  265.                                 // Loading the page is complete, free resources
  266.                                 redirect_count = 0;
  267.                                 http.hfree();
  268.                                 if (http_get_type==PAGE) {
  269.                                         cache.add(history.current(), http.content_pointer, http.content_received, PAGE);
  270.                                         LoadInternalPage(http.content_pointer, http.content_received);
  271.                                 }
  272.                                 else if (http_get_type==IMG) {
  273.                                         cache.add(WB1.page_img.current_url(), http.content_pointer, http.content_received, IMG);
  274.                                         WB1.page_img.set_data(cache.data.get_last(), http.content_received);
  275.                                         GetImg();
  276.                                 }
  277.                         }
  278.                         break;
  279.                 default:
  280.                         if (ESDWORD[shared_url] != '\0') {
  281.                                 EventOpenNewTab(shared_url);
  282.                                 ESDWORD[shared_url] = '\0';
  283.                                 ActivateWindow(GetProcessSlot(Form.ID));
  284.                         }
  285.         }
  286. }
  287.  
  288. bool ProcessCtrlKeyEvent()
  289. {
  290.         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
  291.         {
  292.                 case SCAN_CODE_KEY_O: EventOpenDialog(); return true;
  293.                 case SCAN_CODE_KEY_H: ProcessEvent(VIEW_HISTORY); return true;
  294.                 case SCAN_CODE_KEY_U: EventViewSource(); return true;
  295.                 case SCAN_CODE_KEY_T: EventOpenNewTab(URL_SERVICE_HOMEPAGE); return true;
  296.                 case SCAN_CODE_KEY_N: RunProgram(#program_path, NULL); return true;
  297.                 case SCAN_CODE_KEY_J: ProcessEvent(DOWNLOAD_MANAGER); return true;
  298.                 case SCAN_CODE_KEY_R: ProcessEvent(REFRESH_BUTTON); return true;
  299.                 case SCAN_CODE_ENTER: EventSeachWeb(); return true;
  300.                 case SCAN_CODE_LEFT:  ProcessEvent(BACK_BUTTON); return true;
  301.                 case SCAN_CODE_RIGHT: ProcessEvent(FORWARD_BUTTON); return true;
  302.                 case SCAN_CODE_KEY_W: EventCloseActiveTab(); return true;
  303.                 case SCAN_CODE_TAB:   EventActivateNextTab(); return true;
  304.                 default: return false;
  305.         }
  306. }
  307.  
  308. void SetElementSizes()
  309. {
  310.         address_box.width = Form.cwidth - address_box.left - 52 - 16;
  311.         WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.width - 10 - scroll_wv.size_x,
  312.                 Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
  313.         WB1.list.wheel_size = 7 * BASIC_LINE_H;
  314.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
  315.         WB1.list.visible = WB1.list.h;
  316. }
  317.  
  318.  
  319. void draw_window()
  320. {
  321.         int i;
  322.         bool burger_active = false;
  323.         if (menu_id == OPEN_FILE) burger_active = true;
  324.  
  325.         SetElementSizes();
  326.  
  327.         DrawBar(0,0, Form.cwidth,PADDING, sc.work);
  328.         DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
  329.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.work_dark, sc.work, 180));
  330.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.work_graph);
  331.         DrawBar(0, PADDING, address_box.left-2, TSZE+1, sc.work);
  332.         DrawBar(address_box.left+address_box.width+18, PADDING, Form.cwidth-address_box.left-address_box.width-18, TSZE+1, sc.work);
  333.  
  334.         DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
  335.         DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
  336.         DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
  337.  
  338.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.work_graph);
  339.  
  340.         DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
  341.                 WB1.list.h-1, scroll_wv.bckg_col);
  342.  
  343.         if (!BrowserWidthChanged()) {
  344.                 WB1.DrawPage();
  345.                 DrawOmnibox();
  346.         }
  347.         DrawProgress();
  348.         DrawStatusBar();
  349.         DrawTabsBar();
  350. }
  351.  
  352. bool BrowserWidthChanged()
  353. {
  354.         dword source_mode_holder;
  355.         if (WB1.list.w!=DrawBuf.bufw) {
  356.                 DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, 400*20);
  357.                 if (!strncmp(history.current(),"http",4)) {
  358.                         //nihuya ne izyashnoe reshenie, no pust' poka butet tak
  359.                         source_mode_holder = source_mode;
  360.                         LoadInternalPage(#loading_text, sizeof(loading_text));
  361.                         source_mode = source_mode_holder;
  362.                 }
  363.                 OpenPage(history.current());
  364.                 return true;
  365.         }
  366.         return false;
  367. }
  368.  
  369.  
  370. void EventChangeEncodingAndLoadPage(int _new_encoding)
  371. {
  372.         dword newbuf, newsize;
  373.         WB1.custom_encoding = _new_encoding;
  374.         newsize = strlen(WB1.o_bufpointer);
  375.         newbuf = malloc(newsize);
  376.         memmov(newbuf, WB1.o_bufpointer, newsize);
  377.         LoadInternalPage(newbuf, newsize);
  378.         free(newbuf);
  379. }
  380.  
  381.  
  382. void ProcessEvent(dword id__)
  383. {
  384.         switch (id__)
  385.         {
  386.                 case 1:
  387.                         ExitProcess();
  388.                         break;
  389.                 case ENCODINGS...ENCODINGS+6:
  390.                         EventChangeEncodingAndLoadPage(id__-ENCODINGS);
  391.                         return;
  392.                 case NEW_WINDOW:
  393.                         RunProgram(#program_path, NULL);
  394.                         return;
  395.                 case SCAN_CODE_BS:
  396.                 case BACK_BUTTON:
  397.                         if (history.back()) {
  398.                                 OpenPage(history.current());
  399.                         }
  400.                         return;
  401.                 case FORWARD_BUTTON:
  402.                         if (history.forward()) {
  403.                                 OpenPage(history.current());
  404.                         }
  405.                         return;
  406.                 case GOTOURL_BUTTON:
  407.                 case SCAN_CODE_ENTER:
  408.                         EventSubmitOmnibox();
  409.                         return;
  410.                 case REFRESH_BUTTON:
  411.                         EventRefreshPage();
  412.                         return;
  413.                 case CHANGE_ENCODING:
  414.                         EventShowEncodingsList();
  415.                         return;
  416.                 case SANDWICH_BUTTON:
  417.                         EventShowMainMenu();
  418.                         return;
  419.                 case VIEW_SOURCE:
  420.                         EventViewSource();
  421.                         break;
  422.                 case EDIT_SOURCE:
  423.                         if (check_is_the_adress_local(history.current())) {
  424.                                 RunProgram("/rd/1/tinypad", history.current());
  425.                         } else {
  426.                                 CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
  427.                                 if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
  428.                         }
  429.                         return;
  430.                 case VIEW_HISTORY:
  431.                         OpenPage(URL_SERVICE_HISTORY);
  432.                         return;
  433.                 case DOWNLOAD_MANAGER:
  434.                         EventOpenDownloader("");
  435.                         return;
  436.                 case UPDATE_BROWSER:
  437.                         EventUpdateBrowser();
  438.                         return;
  439.                 case CLEAR_CACHE:
  440.                         cache.clear();
  441.                         notify(#clear_cache_ok);
  442.                         EventRefreshPage();
  443.                         return;
  444.                 case IN_NEW_TAB:
  445.                         EventClickLink(TARGET_NEW_TAB);
  446.                         return;
  447.                 case IN_NEW_WINDOW:
  448.                         EventClickLink(TARGET_NEW_WINDOW);
  449.                         return;
  450.                 case COPY_LINK_URL:
  451.                         Clipboard__CopyText(GetAbsoluteActiveURL());
  452.                         notify("'URL copied to clipboard'O");
  453.                         return;
  454.                 case DOWNLOAD_LINK_CONTENTS:
  455.                         EventOpenDownloader( GetAbsoluteActiveURL() );
  456.                         return;
  457.                 case OPEN_FILE:
  458.                         EventOpenDialog();
  459.                         return;
  460.                 case SCAN_CODE_F12:
  461.                         debug_mode ^= 1;
  462.                         if (debug_mode) notify("'Debug mode ON'-I");
  463.                         else notify("'Debug mode OFF'-I");
  464.                         return;
  465.                 case NEW_TAB:
  466.                         if (http.transfer) break;
  467.                         EventOpenNewTab(URL_SERVICE_HOMEPAGE);
  468.                         return;
  469.                 case TAB_ID...TAB_ID+TABS_MAX:
  470.                         if (http.transfer) break;
  471.                         if (mouse.mkm) {
  472.                                 EventTabClose(id__ - TAB_ID);
  473.                         } else {
  474.                                 EventTabClick(id__ - TAB_ID);
  475.                         }
  476.                         return;
  477.                 case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX:
  478.                         EventTabClose(id__ - TAB_CLOSE_ID);
  479.                         return;
  480.         }
  481. }
  482.  
  483. void StopLoading()
  484. {
  485.         if (http.transfer)
  486.         {
  487.                 EAX = http.transfer;
  488.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  489.                 $push   EAX                                                     // save it on the stack
  490.                 http_free stdcall (http.transfer);      // abort connection
  491.                 $pop    EAX                                                    
  492.                 free(EAX);                                              // free data
  493.                 http.transfer=0;
  494.                 pause(10);
  495.         }
  496.         wv_progress_bar.value = 0;
  497.         DrawOmnibox();
  498. }
  499.  
  500. //rewrite into
  501. //bool strrpl(dword dst, from, into, dst_len);
  502. bool ReplaceSpaceInUrl(dword url, size) {
  503.         unsigned int i, j;
  504.         bool was_changed=false;
  505.         for (i=url+size-3; i>url; i--)
  506.         {
  507.                 if (ESBYTE[i]!=' ') continue;
  508.                 for (j=url+size-3; j>=i; j--) {
  509.                         ESBYTE[j+3]=ESBYTE[j+2];
  510.                         ESBYTE[j+2]=ESBYTE[j+1];
  511.                         ESBYTE[j+1]=ESBYTE[j];
  512.                 }
  513.                 ESBYTE[i] = '%';
  514.                 ESBYTE[i+1] = '2';
  515.                 ESBYTE[i+2] = '0';
  516.                 was_changed = true;
  517.         }
  518.         return was_changed;
  519. }
  520.  
  521. bool HandleUrlFiles(dword _path, _data)
  522. {
  523.         dword url_from_file;
  524.         if (!UrlExtIs(_path, "url")) return false;
  525.         if (! url_from_file = strstri(_data, "URL=")) return false;
  526.         replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
  527.         OpenPage(url_from_file);        
  528.         return true;   
  529. }
  530.  
  531. bool GetLocalFileData(dword _path)
  532. {
  533.         dword data, size;
  534.         file_size stdcall (_path);
  535.         if (!EBX) return false;
  536.  
  537.         size = EBX;
  538.         data = malloc(size);
  539.         ReadFile(0, size, data, _path);
  540.         if (!HandleUrlFiles(_path, data)) {
  541.                 LoadInternalPage(data, size);
  542.         }
  543.         free(data);
  544.         return true;
  545. }
  546.  
  547. void OpenPage(dword _open_URL)
  548. {
  549.         char new_url[URL_SIZE+1];
  550.         char new_url_full[URL_SIZE+1];
  551.         int unz_id;
  552.  
  553.         StopLoading();
  554.  
  555.         SetOmniboxText(_open_URL);
  556.  
  557.         strncpy(#new_url, _open_URL, URL_SIZE);
  558.  
  559.         //Exclude # from the URL to the load page
  560.         //We will bring it back when we get the buffer
  561.         if (strrchr(#new_url, '#')) {
  562.                 anchors.take_anchor_from(#new_url);
  563.         }
  564.  
  565.         history.add(#new_url);
  566.  
  567.         /*
  568.         There could be several possible types of addresses:
  569.         - cached page (only http/https)
  570.         - internal page
  571.         - web page
  572.         - local file
  573.         So we need to detect what incoming address is
  574.         and then halndle it in the propper way.
  575.         */
  576.  
  577.         if (cache.has(#new_url)) {
  578.                 //CACHED PAGE
  579.                 LoadInternalPage(cache.current_buf, cache.current_size);
  580.  
  581.         } else if (!strncmp(#new_url,"WebView:",8)) {
  582.                 //INTERNAL PAGE
  583.                 if (streq(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#buildin_page_home, sizeof(buildin_page_home));
  584.                 else if (streq(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#buildin_page_help, sizeof(buildin_page_help));
  585.                 else if (streq(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
  586.                 else LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  587.  
  588.         } else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
  589.                 //WEB PAGE
  590.                 if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
  591.                         strcpy(#editURL, #new_url);
  592.                 }
  593.  
  594.                 http_get_type = PAGE;
  595.                 if (!strncmp(#new_url,"http:",5)) {
  596.                         http.get(#new_url);
  597.                 } else if (!strncmp(#new_url,"https://",8)) {
  598.                         strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
  599.                         strncat(#new_url_full, #new_url, URL_SIZE);
  600.                         http.get(#new_url_full);
  601.                 }
  602.  
  603.                 DrawOmnibox();
  604.  
  605.                 if (!http.transfer) {
  606.                         StopLoading();
  607.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  608.                 }
  609.         } else {
  610.                 //LOCAL PAGE
  611.                 if (UrlExtIs(#new_url,".docx")) {
  612.                         DeleteFile("/tmp0/1/temp/word/document.xml");
  613.                         CreateDir("/tmp0/1/temp");
  614.                         unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
  615.                         while (GetProcessSlot(unz_id)) pause(2);
  616.                         strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
  617.                 }
  618.                 if (!GetLocalFileData(#new_url)) {
  619.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  620.                 }
  621.         }
  622. }
  623.  
  624. dword EventOpenDownloader(dword _url)
  625. {
  626.         //char download_params[URL_SIZE+50];
  627.         return RunProgram("/sys/network/dl", _url);
  628. }
  629.  
  630. bool EventClickAnchor()
  631. {
  632.         dword aURL = links.active_url;
  633.  
  634.         if (anchors.get_pos_by_name(aURL+1)!=-1) {
  635.                 WB1.list.first = anchors.get_pos_by_name(aURL+1);
  636.                 //WB1.list.CheckDoesValuesOkey();
  637.                 strcpy(#editURL, history.current());
  638.                 strcat(#editURL, aURL);
  639.                 DrawOmnibox();
  640.                 WB1.DrawPage();
  641.                 return true;
  642.         }
  643.         return false;
  644. }
  645.  
  646. void EventClickLink(dword _target)
  647. {
  648.         char new_url[URL_SIZE+1];
  649.         char new_url_full[URL_SIZE+1];
  650.         dword aURL = GetAbsoluteActiveURL();
  651.         if (!aURL) return;
  652.  
  653.         strcpy(#new_url, aURL);
  654.  
  655.         if (ESBYTE[aURL]=='#') {
  656.                 if (_target == TARGET_SAME_TAB) {
  657.                         EventClickAnchor();
  658.                         return;
  659.                 } else {
  660.                         strcpy(#new_url, history.current());
  661.                         strcat(#new_url, aURL);
  662.                 }
  663.         }
  664.  
  665.         if (_target == TARGET_NEW_TAB) {
  666.                 EventOpenNewTab(#new_url);
  667.                 return;
  668.         }
  669.  
  670.         if (_target == TARGET_NEW_WINDOW) {
  671.                 strcpy(#new_url_full, "-new ");
  672.                 strncat(#new_url_full, #new_url, URL_SIZE);
  673.                 RunProgram(#program_path, #new_url_full);
  674.                 return;
  675.         }
  676.  
  677.         if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
  678.                 notify(#new_url);
  679.                 return;
  680.         }
  681.  
  682.         if (http.transfer) {
  683.                 StopLoading();
  684.                 history.back();
  685.         }
  686.  
  687.         if (strrchr(#new_url, '#')!=0) {
  688.                 anchors.take_anchor_from(#new_url);
  689.                 OpenPage(#new_url);
  690.                 return;
  691.         }
  692.  
  693.         if (!strncmp(#new_url,"WebView:",8)) {
  694.                 OpenPage(#new_url);
  695.                 return;
  696.         }
  697.  
  698.         if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
  699.         {
  700.                 if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
  701.                 {      
  702.                         if (strchr(#new_url, '|')) {
  703.                                 ESBYTE[strchr(#new_url, '|')] = NULL;
  704.                                 RunProgram(#new_url, strlen(#new_url)+1+#new_url);
  705.                         } else {
  706.                                 RunProgram("/sys/@open", #new_url);
  707.                         }
  708.                         return;
  709.                 }
  710.         } else {
  711.                 if (UrlExtIs(#new_url,".png")==true) || (UrlExtIs(#new_url,".jpg")==true)
  712.                 || (UrlExtIs(#new_url,".zip")==true) || (UrlExtIs(#new_url,".kex")==true) || (UrlExtIs(#new_url,".pdf")==true)
  713.                 || (UrlExtIs(#new_url,".7z")==true) {
  714.                         EventOpenDownloader(#new_url);
  715.                         return;
  716.                 }
  717.         }
  718.         OpenPage(#new_url);
  719. }
  720.  
  721. void EventSubmitOmnibox()
  722. {
  723.         char new_url[URL_SIZE+1];
  724.         if (!editURL[0]) return;
  725.         if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
  726.         || (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
  727.                 OpenPage(#editURL);
  728.         } else {
  729.                 strcpy(#new_url, "http://");
  730.                 strncat(#new_url, #editURL, URL_SIZE-1);
  731.                 OpenPage(#new_url);
  732.         }
  733. }
  734.  
  735. void LoadInternalPage(dword _bufdata, _in_bufsize){
  736.         if (!_bufdata) || (!_in_bufsize) {
  737.                 LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  738.         } else {
  739.                 WB1.list.first = 0; //scroll page to the top
  740.                 DrawOmnibox();
  741.                 if(!strrchr(#editURL, '#')) {
  742.                         strcat(#editURL, #anchors.current);
  743.                         DrawOmnibox();
  744.                 }
  745.                 WB1.ParseHtml(_bufdata, _in_bufsize);
  746.                 DrawStatusBar();
  747.                 DrawActiveTab();
  748.                 if (source_mode) {
  749.                         source_mode = false;
  750.                         WB1.custom_encoding = CH_CP866;
  751.                         ShowSource(WB1.bufpointer, _in_bufsize);
  752.                 } else {
  753.                         WB1.DrawPage();
  754.                 }
  755.                 //GetImg();
  756.         }
  757. }
  758.  
  759. bool UrlExtIs(dword base, ext)
  760. {
  761.         if (!strcmpi(base + strlen(base) - strlen(ext), ext)) return true;
  762.         return false;
  763. }
  764.  
  765. void DrawProgress()
  766. {
  767.         dword persent;
  768.         if (http.transfer == 0) return;
  769.         if (wv_progress_bar.max) {
  770.                 persent = wv_progress_bar.value*100/wv_progress_bar.max;
  771.         } else {
  772.                 persent = 10;
  773.         }
  774.         DrawBar(address_box.left-1, address_box.top+20, persent*address_box.width+16/100, 2, 0x72B7EB);
  775. }
  776.  
  777. void EventShowPageMenu()
  778. {
  779.         open_lmenu(mouse.x, mouse.y, MENU_TOP_LEFT, NULL, #rmb_menu);
  780.         menu_id = VIEW_SOURCE;
  781. }
  782.  
  783. void EventShowLinkMenu()
  784. {
  785.         open_lmenu(mouse.x, mouse.y, MENU_TOP_LEFT, NULL, #link_menu);
  786.         menu_id = IN_NEW_TAB;
  787. }
  788.  
  789. void EventShowMainMenu()
  790. {
  791.         open_lmenu(Form.cwidth - PADDING -4, PADDING + TSZE + 3,
  792.                 MENU_TOP_RIGHT, NULL, #main_menu);
  793.         menu_id = OPEN_FILE;
  794. }
  795.  
  796. void EventShowEncodingsList()
  797. {
  798.         open_lmenu(Form.cwidth-4, Form.cheight - STATUSBAR_H + 12,
  799.                 MENU_BOT_RIGHT, WB1.cur_encoding + 1,
  800.                 "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
  801.         menu_id = ENCODINGS;
  802. }
  803.  
  804. void ProcessMenuClick()
  805. {
  806.         int click_id;
  807.         if (menu_id) {
  808.                 if (click_id = get_menu_click()) {
  809.                         click_id += menu_id - 1;
  810.                         ProcessEvent(click_id);
  811.                 }
  812.                 if (!menu_process_id) menu_id = NULL;
  813.         }
  814. }
  815.  
  816. void EventUpdateProgressBar()
  817. {
  818.         wv_progress_bar.max = http.content_length;
  819.         if (wv_progress_bar.value != http.content_received)
  820.         {
  821.                 wv_progress_bar.value = http.content_received; 
  822.                 DrawProgress();
  823.         }
  824. }
  825.  
  826. void EventSeachWeb()
  827. {
  828.         char new_url[URL_SIZE+1];
  829.         replace_char(#editURL, ' ', '_', URL_SIZE);
  830.         strcpy(#new_url, "https://www.google.com/search?q=");
  831.         strncat(#new_url, #editURL, URL_SIZE);
  832.         OpenPage(#new_url);
  833. }
  834.  
  835. void EventOpenDialog()
  836. {
  837.         OpenDialog_start stdcall (#o_dialog);
  838.         if (o_dialog.status) {
  839.                 OpenPage(#openfile_path);
  840.         }
  841. }
  842.  
  843. void EventViewSource()
  844. {
  845.         char source_view_param[URL_SIZE+1];
  846.         //strcpy(#source_view_param, "-source ");
  847.         //strncat(#source_view_param, history.current(), URL_SIZE);
  848.         //RunProgram(#program_path, #source_view_param);
  849.         source_mode = true;
  850.         EventOpenNewTab(history.current());
  851. }
  852.  
  853. void EventRefreshPage()
  854. {
  855.         if (http.transfer) {
  856.                 StopLoading();
  857.                 draw_window();
  858.         } else {
  859.                 OpenPage(history.current());
  860.         }
  861. }
  862.  
  863. dword GetFileSize(dword _path)
  864. {
  865.         BDVK bdvk;
  866.         if (GetFileInfo(_path, #bdvk)!=0) {
  867.                 return 0;
  868.         } else {
  869.                 return bdvk.sizelo;
  870.         }
  871. }
  872.  
  873. void EventUpdateBrowser()
  874. {
  875.         dword downloader_id, slot_n;
  876.         dword current_size;
  877.         dword new_size;
  878.         int error;
  879.  
  880.         draw_window();
  881.  
  882.         downloader_id = EventOpenDownloader(#update_param);
  883.         do {
  884.                 slot_n = GetProcessSlot(downloader_id);
  885.                 pause(10);
  886.         } while (slot_n!=0);
  887.  
  888.         current_size = GetFileSize(#program_path);
  889.         new_size = GetFileSize("/tmp0/1/Downloads/WebView.com");
  890.  
  891.         if (!new_size) || (new_size<5000) {
  892.                 notify(#update_download_error);
  893.                 return;
  894.         }
  895.  
  896.         if (current_size == new_size) {
  897.                 notify(#update_is_current);
  898.                 return;
  899.         }
  900.  
  901.         if (error = CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
  902.                 notify(#update_can_not_copy);
  903.         } else {
  904.                 notify(#update_ok);
  905.                 RunProgram(#program_path, history.current());
  906.                 ExitProcess();
  907.         }
  908. }
  909.  
  910. void DrawStatusBar()
  911. {
  912.         dword status_y = Form.cheight - STATUSBAR_H + 4;
  913.         dword status_w = Form.cwidth - 90;
  914.         DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
  915.         if (links.active_url) {
  916.                 ESI = math.min(status_w/6, strlen(links.active_url));
  917.                 WriteText(10, status_y, 0, sc.work_text, links.active_url);
  918.         }
  919.         if (http.transfer>0) && (http_get_type==IMG) {
  920.                 //
  921.         }
  922.         DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
  923.         WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
  924. }
  925.  
  926. void DrawOmnibox()
  927. {
  928.         int imgxoff;
  929.        
  930.         DrawOvalBorder(address_box.left-2, address_box.top-3, address_box.width+18, 24, sc.work_graph,
  931.                 sc.work_graph, sc.work_graph, sc.work_dark);
  932.         DrawBar(address_box.left-1, address_box.top-2, address_box.width+18, 1, 0xD8DCD8);
  933.         DrawBar(address_box.left-1, address_box.top-1, address_box.width+18, 1, address_box.color);
  934.         DrawBar(address_box.left-1, address_box.top, 1, 22, address_box.color);
  935.  
  936.         if (address_box.flags & ed_focus) address_box.flags = ed_focus; else address_box.flags = 0;
  937.         EditBox_UpdateText(#address_box, address_box.flags);
  938.         edit_box_draw stdcall(#address_box);
  939.         if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
  940.         _PutImage(address_box.left+address_box.width+1, address_box.top-1, 16, 23, imgxoff + #editbox_icons);
  941.         DefineHiddenButton(address_box.left+address_box.width-1, address_box.top-2, 17, 23, REFRESH_BUTTON);
  942.  
  943.         DrawProgress();
  944. }
  945.  
  946. void SetOmniboxText(dword _text)
  947. {
  948.         strcpy(#editURL, _text);
  949.         address_box.flags=0;
  950.         DrawOmnibox();
  951. }
  952.  
  953. dword GetAbsoluteActiveURL()
  954. {
  955.         char abs_url[URL_SIZE];
  956.         if (links.active_url) {
  957.                 strncpy(#abs_url, links.active_url, URL_SIZE);
  958.                 get_absolute_url(#abs_url, history.current());         
  959.                 return #abs_url;
  960.         }
  961.         return 0;
  962. }
  963.  
  964. dword GetImg()
  965. {
  966.         while (WB1.page_img.next_url()) {
  967.                 if (cache.has(WB1.page_img.current_url())) continue;
  968.                 http_get_type = IMG;
  969.                 http.get(WB1.page_img.current_url());
  970.                 return;
  971.         }
  972.         DrawOmnibox();
  973.         WB1.DrawPage();
  974. }
  975.  
  976. stop: