Subversion Repositories Kolibri OS

Rev

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