Subversion Repositories Kolibri OS

Rev

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