Subversion Repositories Kolibri OS

Rev

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