Subversion Repositories Kolibri OS

Rev

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