Subversion Repositories Kolibri OS

Rev

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