Subversion Repositories Kolibri OS

Rev

Rev 8292 | Rev 8305 | 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.7a";
  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()) // application || image
  242.                         if (http.content_type[0] == 'a') || (http.content_type[0] == 'i') {
  243.                                 EventOpenDownloader(history.current());
  244.                                 StopLoading();
  245.                                 history.back();
  246.                                 EventRefreshPage();
  247.                         }
  248.                         if (http.receive_result != 0) break;
  249.                         if (http.status_code >= 300) && (http.status_code < 400)
  250.                         {
  251.                                 // Handle redirects
  252.                                 if (redirect_count<=5) {
  253.                                         redirect_count++;
  254.                                         http.handle_redirect();
  255.                                         history.back();
  256.                                         if (http_get_type==PAGE) OpenPage(#http.redirect_url);
  257.                                         else if (http_get_type==IMG) http.get(#http.redirect_url);
  258.                                 } else {
  259.                                         notify("'Too many redirects.' -E");
  260.                                         StopLoading();
  261.                                         redirect_count = 0;
  262.                                 }
  263.                         } else {
  264.                                 // Loading the page is complete, free resources
  265.                                 redirect_count = 0;
  266.                                 http.hfree();
  267.                                 if (http_get_type==PAGE) {
  268.                                         cache.add(history.current(), http.content_pointer, http.content_received, PAGE);
  269.                                         LoadInternalPage(http.content_pointer, http.content_received);
  270.                                 }
  271.                                 else if (http_get_type==IMG) {
  272.                                         cache.add(WB1.page_img.current_url(), http.content_pointer, http.content_received, IMG);
  273.                                         WB1.page_img.set_data(cache.data.get_last(), http.content_received);
  274.                                         GetImg();
  275.                                 }
  276.                         }
  277.                         break;
  278.                 default:
  279.                         if (ESDWORD[shared_url] != '\0') {
  280.                                 EventOpenNewTab(shared_url);
  281.                                 ESDWORD[shared_url] = '\0';
  282.                                 ActivateWindow(GetProcessSlot(Form.ID));
  283.                         }
  284.         }
  285. }
  286.  
  287. bool ProcessCtrlKeyEvent()
  288. {
  289.         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
  290.         {
  291.                 case SCAN_CODE_KEY_O: EventOpenDialog(); return true;
  292.                 case SCAN_CODE_KEY_H: ProcessEvent(VIEW_HISTORY); return true;
  293.                 case SCAN_CODE_KEY_U: EventViewSource(); return true;
  294.                 case SCAN_CODE_KEY_T: EventOpenNewTab(URL_SERVICE_HOMEPAGE); return true;
  295.                 case SCAN_CODE_KEY_N: RunProgram(#program_path, NULL); return true;
  296.                 case SCAN_CODE_KEY_J: ProcessEvent(DOWNLOAD_MANAGER); return true;
  297.                 case SCAN_CODE_KEY_R: ProcessEvent(REFRESH_BUTTON); return true;
  298.                 case SCAN_CODE_ENTER: EventSeachWeb(); return true;
  299.                 case SCAN_CODE_LEFT:  ProcessEvent(BACK_BUTTON); return true;
  300.                 case SCAN_CODE_RIGHT: ProcessEvent(FORWARD_BUTTON); return true;
  301.                 case SCAN_CODE_KEY_W: EventCloseActiveTab(); return true;
  302.                 case SCAN_CODE_TAB:   EventActivateNextTab(); return true;
  303.                 default: return false;
  304.         }
  305. }
  306.  
  307. void SetElementSizes()
  308. {
  309.         address_box.width = Form.cwidth - address_box.left - 52 - 16;
  310.         WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.width - 10 - scroll_wv.size_x,
  311.                 Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
  312.         WB1.list.wheel_size = 7 * BASIC_LINE_H;
  313.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
  314.         WB1.list.visible = WB1.list.h;
  315. }
  316.  
  317.  
  318. void draw_window()
  319. {
  320.         int i;
  321.         bool burger_active = false;
  322.         if (menu_id == OPEN_FILE) burger_active = true;
  323.  
  324.         SetElementSizes();
  325.  
  326.         DrawBar(0,0, Form.cwidth,PADDING, sc.work);
  327.         DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
  328.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.work_dark, sc.work, 180));
  329.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.work_graph);
  330.         DrawBar(0, PADDING, address_box.left-2, TSZE+1, sc.work);
  331.         DrawBar(address_box.left+address_box.width+18, PADDING, Form.cwidth-address_box.left-address_box.width-18, TSZE+1, sc.work);
  332.  
  333.         DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
  334.         DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
  335.         DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
  336.  
  337.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.work_graph);
  338.  
  339.         DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
  340.                 WB1.list.h-1, scroll_wv.bckg_col);
  341.  
  342.         if (!BrowserWidthChanged()) {
  343.                 WB1.DrawPage();
  344.                 DrawOmnibox();
  345.         }
  346.         DrawProgress();
  347.         DrawStatusBar();
  348.         DrawTabsBar();
  349. }
  350.  
  351. bool BrowserWidthChanged()
  352. {
  353.         dword source_mode_holder;
  354.         if (WB1.list.w!=DrawBuf.bufw) {
  355.                 DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, 400*20);
  356.                 if (!strncmp(history.current(),"http",4)) {
  357.                         //nihuya ne izyashnoe reshenie, no pust' poka butet tak
  358.                         source_mode_holder = source_mode;
  359.                         LoadInternalPage(#loading_text, sizeof(loading_text));
  360.                         source_mode = source_mode_holder;
  361.                 }
  362.                 OpenPage(history.current());
  363.                 return true;
  364.         }
  365.         return false;
  366. }
  367.  
  368.  
  369. void EventChangeEncodingAndLoadPage(int _new_encoding)
  370. {
  371.         dword newbuf, newsize;
  372.         WB1.custom_encoding = _new_encoding;
  373.         newsize = strlen(WB1.o_bufpointer);
  374.         newbuf = malloc(newsize);
  375.         memmov(newbuf, WB1.o_bufpointer, newsize);
  376.         LoadInternalPage(newbuf, newsize);
  377.         free(newbuf);
  378. }
  379.  
  380.  
  381. void ProcessEvent(dword id__)
  382. {
  383.         switch (id__)
  384.         {
  385.                 case 1:
  386.                         ExitProcess();
  387.                         break;
  388.                 case ENCODINGS...ENCODINGS+6:
  389.                         EventChangeEncodingAndLoadPage(id__-ENCODINGS);
  390.                         return;
  391.                 case NEW_WINDOW:
  392.                         RunProgram(#program_path, NULL);
  393.                         return;
  394.                 case SCAN_CODE_BS:
  395.                 case BACK_BUTTON:
  396.                         if (history.back()) {
  397.                                 OpenPage(history.current());
  398.                         }
  399.                         return;
  400.                 case FORWARD_BUTTON:
  401.                         if (history.forward()) {
  402.                                 OpenPage(history.current());
  403.                         }
  404.                         return;
  405.                 case GOTOURL_BUTTON:
  406.                 case SCAN_CODE_ENTER:
  407.                         EventSubmitOmnibox();
  408.                         return;
  409.                 case REFRESH_BUTTON:
  410.                         EventRefreshPage();
  411.                         return;
  412.                 case CHANGE_ENCODING:
  413.                         EventShowEncodingsList();
  414.                         return;
  415.                 case SANDWICH_BUTTON:
  416.                         EventShowMainMenu();
  417.                         return;
  418.                 case VIEW_SOURCE:
  419.                         EventViewSource();
  420.                         break;
  421.                 case EDIT_SOURCE:
  422.                         if (check_is_the_adress_local(history.current())) {
  423.                                 RunProgram("/rd/1/tinypad", history.current());
  424.                         } else {
  425.                                 CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
  426.                                 if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
  427.                         }
  428.                         return;
  429.                 case VIEW_HISTORY:
  430.                         OpenPage(URL_SERVICE_HISTORY);
  431.                         return;
  432.                 case DOWNLOAD_MANAGER:
  433.                         EventOpenDownloader("");
  434.                         return;
  435.                 case UPDATE_BROWSER:
  436.                         EventUpdateBrowser();
  437.                         return;
  438.                 case CLEAR_CACHE:
  439.                         cache.clear();
  440.                         notify(#clear_cache_ok);
  441.                         EventRefreshPage();
  442.                         return;
  443.                 case IN_NEW_TAB:
  444.                         EventClickLink(TARGET_NEW_TAB);
  445.                         return;
  446.                 case IN_NEW_WINDOW:
  447.                         EventClickLink(TARGET_NEW_WINDOW);
  448.                         return;
  449.                 case COPY_LINK_URL:
  450.                         Clipboard__CopyText(GetAbsoluteActiveURL());
  451.                         notify("'URL copied to clipboard'O");
  452.                         return;
  453.                 case DOWNLOAD_LINK_CONTENTS:
  454.                         EventOpenDownloader( GetAbsoluteActiveURL() );
  455.                         return;
  456.                 case OPEN_FILE:
  457.                         EventOpenDialog();
  458.                         return;
  459.                 case SCAN_CODE_F12:
  460.                         debug_mode ^= 1;
  461.                         if (debug_mode) notify("'Debug mode ON'-I");
  462.                         else notify("'Debug mode OFF'-I");
  463.                         return;
  464.                 case NEW_TAB:
  465.                         if (http.transfer) break;
  466.                         EventOpenNewTab(URL_SERVICE_HOMEPAGE);
  467.                         return;
  468.                 case TAB_ID...TAB_ID+TABS_MAX:
  469.                         if (http.transfer) break;
  470.                         if (mouse.mkm) {
  471.                                 EventTabClose(id__ - TAB_ID);
  472.                         } else {
  473.                                 EventTabClick(id__ - TAB_ID);
  474.                         }
  475.                         return;
  476.                 case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX:
  477.                         EventTabClose(id__ - TAB_CLOSE_ID);
  478.                         return;
  479.         }
  480. }
  481.  
  482. void StopLoading()
  483. {
  484.         if (http.transfer)
  485.         {
  486.                 EAX = http.transfer;
  487.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  488.                 $push   EAX                                                     // save it on the stack
  489.                 http_free stdcall (http.transfer);      // abort connection
  490.                 $pop    EAX                                                    
  491.                 free(EAX);                                              // free data
  492.                 http.transfer=0;
  493.                 pause(10);
  494.         }
  495.         wv_progress_bar.value = 0;
  496.         DrawOmnibox();
  497. }
  498.  
  499. //rewrite into
  500. //bool strrpl(dword dst, from, into, dst_len);
  501. bool ReplaceSpaceInUrl(dword url, size) {
  502.         unsigned int i, j;
  503.         bool was_changed=false;
  504.         for (i=url+size-3; i>url; i--)
  505.         {
  506.                 if (ESBYTE[i]!=' ') continue;
  507.                 for (j=url+size-3; j>=i; j--) {
  508.                         ESBYTE[j+3]=ESBYTE[j+2];
  509.                         ESBYTE[j+2]=ESBYTE[j+1];
  510.                         ESBYTE[j+1]=ESBYTE[j];
  511.                 }
  512.                 ESBYTE[i] = '%';
  513.                 ESBYTE[i+1] = '2';
  514.                 ESBYTE[i+2] = '0';
  515.                 was_changed = true;
  516.         }
  517.         return was_changed;
  518. }
  519.  
  520. bool HandleUrlFiles(dword _path, _data)
  521. {
  522.         dword url_from_file;
  523.         if (!UrlExtIs(_path, "url")) return false;
  524.         if (! url_from_file = strstri(_data, "URL=")) return false;
  525.         replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
  526.         OpenPage(url_from_file);        
  527.         return true;   
  528. }
  529.  
  530. bool GetLocalFileData(dword _path)
  531. {
  532.         dword data, size;
  533.         file_size stdcall (_path);
  534.         if (!EBX) return false;
  535.  
  536.         size = EBX;
  537.         data = malloc(size);
  538.         ReadFile(0, size, data, _path);
  539.         if (!HandleUrlFiles(_path, data)) {
  540.                 LoadInternalPage(data, size);
  541.         }
  542.         free(data);
  543.         return true;
  544. }
  545.  
  546. void OpenPage(dword _open_URL)
  547. {
  548.         char new_url[URL_SIZE+1];
  549.         char new_url_full[URL_SIZE+1];
  550.         int unz_id;
  551.  
  552.         StopLoading();
  553.  
  554.         SetOmniboxText(_open_URL);
  555.  
  556.         strncpy(#new_url, _open_URL, URL_SIZE);
  557.  
  558.         //Exclude # from the URL to the load page
  559.         //We will bring it back when we get the buffer
  560.         if (strrchr(#new_url, '#')) {
  561.                 anchors.take_anchor_from(#new_url);
  562.         }
  563.  
  564.         history.add(#new_url);
  565.  
  566.         /*
  567.         There could be several possible types of addresses:
  568.         - cached page (only http/https)
  569.         - internal page
  570.         - web page
  571.         - local file
  572.         So we need to detect what incoming address is
  573.         and then halndle it in the propper way.
  574.         */
  575.  
  576.         if (cache.has(#new_url)) {
  577.                 //CACHED PAGE
  578.                 LoadInternalPage(cache.current_buf, cache.current_size);
  579.  
  580.         } else if (!strncmp(#new_url,"WebView:",8)) {
  581.                 //INTERNAL PAGE
  582.                 if (streq(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#buildin_page_home, sizeof(buildin_page_home));
  583.                 else if (streq(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#buildin_page_help, sizeof(buildin_page_help));
  584.                 else if (streq(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
  585.                 else LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  586.  
  587.         } else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
  588.                 //WEB PAGE
  589.                 if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
  590.                         strcpy(#editURL, #new_url);
  591.                 }
  592.  
  593.                 http_get_type = PAGE;
  594.                 if (!strncmp(#new_url,"http:",5)) {
  595.                         http.get(#new_url);
  596.                 } else if (!strncmp(#new_url,"https://",8)) {
  597.                         strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
  598.                         strncat(#new_url_full, #new_url, URL_SIZE);
  599.                         http.get(#new_url_full);
  600.                 }
  601.  
  602.                 DrawOmnibox();
  603.  
  604.                 if (!http.transfer) {
  605.                         StopLoading();
  606.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  607.                 }
  608.         } else {
  609.                 //LOCAL PAGE
  610.                 if (UrlExtIs(#new_url,".docx")) {
  611.                         DeleteFile("/tmp0/1/temp/word/document.xml");
  612.                         CreateDir("/tmp0/1/temp");
  613.                         unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
  614.                         while (GetProcessSlot(unz_id)) pause(2);
  615.                         strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
  616.                 }
  617.                 if (!GetLocalFileData(#new_url)) {
  618.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  619.                 }
  620.         }
  621. }
  622.  
  623. dword EventOpenDownloader(dword _url)
  624. {
  625.         //char download_params[URL_SIZE+50];
  626.         return RunProgram("/sys/network/dl", _url);
  627. }
  628.  
  629. bool EventClickAnchor()
  630. {
  631.         dword aURL = links.active_url;
  632.  
  633.         if (anchors.get_pos_by_name(aURL+1)!=-1) {
  634.                 WB1.list.first = anchors.get_pos_by_name(aURL+1);
  635.                 //WB1.list.CheckDoesValuesOkey();
  636.                 strcpy(#editURL, history.current());
  637.                 strcat(#editURL, aURL);
  638.                 DrawOmnibox();
  639.                 WB1.DrawPage();
  640.                 return true;
  641.         }
  642.         return false;
  643. }
  644.  
  645. void EventClickLink(dword _target)
  646. {
  647.         char new_url[URL_SIZE+1];
  648.         char new_url_full[URL_SIZE+1];
  649.         dword aURL = GetAbsoluteActiveURL();
  650.         if (!aURL) return;
  651.  
  652.         strcpy(#new_url, aURL);
  653.  
  654.         if (ESBYTE[aURL]=='#') {
  655.                 if (_target == TARGET_SAME_TAB) {
  656.                         EventClickAnchor();
  657.                         return;
  658.                 } else {
  659.                         strcpy(#new_url, history.current());
  660.                         strcat(#new_url, aURL);
  661.                 }
  662.         }
  663.  
  664.         if (_target == TARGET_NEW_TAB) {
  665.                 EventOpenNewTab(#new_url);
  666.                 return;
  667.         }
  668.  
  669.         if (_target == TARGET_NEW_WINDOW) {
  670.                 strcpy(#new_url_full, "-new ");
  671.                 strncat(#new_url_full, #new_url, URL_SIZE);
  672.                 RunProgram(#program_path, #new_url_full);
  673.                 return;
  674.         }
  675.  
  676.         if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
  677.                 notify(#new_url);
  678.                 return;
  679.         }
  680.  
  681.         if (http.transfer) {
  682.                 StopLoading();
  683.                 history.back();
  684.         }
  685.  
  686.         if (strrchr(#new_url, '#')!=0) {
  687.                 anchors.take_anchor_from(#new_url);
  688.                 OpenPage(#new_url);
  689.                 return;
  690.         }
  691.  
  692.         if (!strncmp(#new_url,"WebView:",8)) {
  693.                 OpenPage(#new_url);
  694.                 return;
  695.         }
  696.  
  697.         if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
  698.         {
  699.                 if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
  700.                 {      
  701.                         if (strchr(#new_url, '|')) {
  702.                                 ESBYTE[strchr(#new_url, '|')] = NULL;
  703.                                 RunProgram(#new_url, strlen(#new_url)+1+#new_url);
  704.                         } else {
  705.                                 RunProgram("/sys/@open", #new_url);
  706.                         }
  707.                         return;
  708.                 }
  709.         }
  710.         OpenPage(#new_url);
  711. }
  712.  
  713. void EventSubmitOmnibox()
  714. {
  715.         char new_url[URL_SIZE+1];
  716.         if (!editURL[0]) return;
  717.         if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
  718.         || (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
  719.                 OpenPage(#editURL);
  720.         } else {
  721.                 strcpy(#new_url, "http://");
  722.                 strncat(#new_url, #editURL, URL_SIZE-1);
  723.                 OpenPage(#new_url);
  724.         }
  725. }
  726.  
  727. void LoadInternalPage(dword _bufdata, _in_bufsize){
  728.         if (!_bufdata) || (!_in_bufsize) {
  729.                 LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  730.         } else {
  731.                 WB1.list.first = 0; //scroll page to the top
  732.                 DrawOmnibox();
  733.                 if(!strrchr(#editURL, '#')) {
  734.                         strcat(#editURL, #anchors.current);
  735.                         DrawOmnibox();
  736.                 }
  737.                 WB1.ParseHtml(_bufdata, _in_bufsize);
  738.                 // REJECTED. Reason: infinite redirect at Google Results.
  739.                 /*
  740.                 if (WB1.redirect) { //<meta http-equiv="refresh" content="0; url=http://site.com">
  741.                         get_absolute_url(#WB1.redirect, history.current());
  742.                         history.back();
  743.                         OpenPage(#WB1.redirect);
  744.                 }
  745.                 */
  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: