Subversion Repositories Kolibri OS

Rev

Rev 8425 | Rev 8440 | 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.1";
  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;
  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.                                 }
  227.                         } else {
  228.                                 // Loading the page is complete, free resources
  229.                                 redirect_count = 0;
  230.                                 http.hfree();
  231.                                 if (http_get_type==PAGE) {
  232.                                         history.add(http.cur_url);
  233.                                         cache.add(http.cur_url, http.content_pointer, http.content_received, PAGE);
  234.                                         free(http.content_pointer);
  235.                                         LoadInternalPage(cache.current_buf, cache.current_size);
  236.                                 }
  237.                                 else if (http_get_type==IMG) {
  238.                                         cache.add(cur_img_url, http.content_pointer, http.content_received, IMG);
  239.                                         free(http.content_pointer);
  240.                                         GetImg(false);
  241.                                 }
  242.                         }
  243.                         break;
  244.                 default:
  245.                         if (ESDWORD[shared_url] != '\0') {
  246.                                 EventOpenNewTab(shared_url);
  247.                                 ESDWORD[shared_url] = '\0';
  248.                                 ActivateWindow(GetProcessSlot(Form.ID));
  249.                         }
  250.         }
  251. }
  252.  
  253.  
  254. //===================================================//
  255. //                                                   //
  256. //                      EVENTS                       //
  257. //                                                   //
  258. //===================================================//
  259.  
  260. void ProcessButtonClick(dword id__)
  261. {
  262.         switch (id__)
  263.         {
  264.                 case 1: ExitProcess();
  265.                 case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX: EventTabClose(id__ - TAB_CLOSE_ID); return;
  266.                 case TAB_ID...TAB_ID+TABS_MAX: EventAllTabsClick(id__ - TAB_ID); return;
  267.                 case ENCODINGS...ENCODINGS+6: EventChangeEncodingAndLoadPage(id__-ENCODINGS); return;
  268.                 case NEW_WINDOW:       RunProgram(#program_path, NULL); return;
  269.                 case NEW_TAB:          if (!http.transfer) EventOpenNewTab(URL_SERVICE_HOMEPAGE); return;
  270.                 case SCAN_CODE_BS:
  271.                 case BACK_BUTTON:      if (history.back()) OpenPage(history.current()); return;
  272.                 case FORWARD_BUTTON:   if (history.forward()) OpenPage(history.current()); return;
  273.                 case GOTOURL_BUTTON:   EventSubmitOmnibox();    return;
  274.                 case REFRESH_BUTTON:   EventRefreshPage(); return;
  275.                 case CHANGE_ENCODING:  EventShowEncodingsList(); return;
  276.                 case SANDWICH_BUTTON:  EventShowMainMenu(); return;
  277.                 case VIEW_SOURCE:      EventViewSource(); return;
  278.                 case EDIT_SOURCE:      EventEditSource(); return;
  279.                 case VIEW_HISTORY:     OpenPage(URL_SERVICE_HISTORY); return;
  280.                 case DOWNLOAD_MANAGER: EventOpenDownloader(""); return;
  281.                 case UPDATE_BROWSER:   EventUpdateBrowser(); return;
  282.                 case CLEAR_CACHE:      EventClearCache(); return;
  283.                 case IN_NEW_TAB:       EventClickLink(TARGET_NEW_TAB); return;
  284.                 case IN_NEW_WINDOW:    EventClickLink(TARGET_NEW_WINDOW); return;
  285.                 case COPY_LINK_URL:    EventCopyLinkToClipboard(); return;
  286.                 case DOWNLOAD_LINK_CT: EventOpenDownloader( GetAbsoluteActiveURL() ); return;
  287.                 case OPEN_FILE:        EventOpenDialog(); return;
  288.         }
  289. }
  290.  
  291. bool ProcessKeyEvent()
  292. {
  293.         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT)
  294.         {
  295.                 if (key_scancode == SCAN_CODE_TAB) {EventActivatePreviousTab();return;}
  296.         }
  297.  
  298.         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
  299.         {
  300.                 case SCAN_CODE_KEY_O: EventOpenDialog(); return true;
  301.                 case SCAN_CODE_KEY_H: ProcessButtonClick(VIEW_HISTORY); return true;
  302.                 case SCAN_CODE_KEY_U: EventViewSource(); return true;
  303.                 case SCAN_CODE_KEY_T: EventOpenNewTab(URL_SERVICE_HOMEPAGE); return true;
  304.                 case SCAN_CODE_KEY_N: RunProgram(#program_path, NULL); return true;
  305.                 case SCAN_CODE_KEY_J: ProcessButtonClick(DOWNLOAD_MANAGER); return true;
  306.                 case SCAN_CODE_KEY_R: ProcessButtonClick(REFRESH_BUTTON); return true;
  307.                 case SCAN_CODE_ENTER: EventSeachWeb(); return true;
  308.                 case SCAN_CODE_LEFT:  ProcessButtonClick(BACK_BUTTON); return true;
  309.                 case SCAN_CODE_RIGHT: ProcessButtonClick(FORWARD_BUTTON); return true;
  310.                 case SCAN_CODE_KEY_W: EventCloseActiveTab(); return true;
  311.                 case SCAN_CODE_TAB:   EventActivateNextTab(); return true;
  312.                 case SCAN_CODE_F5:    EventClearCache(); return;
  313.                 default: return false;
  314.         }
  315.  
  316.         switch(key_scancode)
  317.         {
  318.                 case SCAN_CODE_UP:    EventScrollUpAndDown(SCAN_CODE_UP); return;
  319.                 case SCAN_CODE_DOWN:  EventScrollUpAndDown(SCAN_CODE_DOWN); return;
  320.                 case SCAN_CODE_F6:    {omnibox_edit.flags=ed_focus; DrawOmnibox();} return;
  321.                 case SCAN_CODE_F5:    EventRefreshPage(); return;
  322.                 case SCAN_CODE_ENTER: if (omnibox_edit.flags & ed_focus) EventSubmitOmnibox(); return;
  323.                 case SCAN_CODE_F12:   EventToggleDebugMode(); return;
  324.                 case SCAN_CODE_F11:   show_images^=1; EventClearCache(); return;
  325.                 default:              if (WB1.list.ProcessKey(key_scancode)) WB1.DrawPage(); return;
  326.         }
  327. }
  328.  
  329. void SetElementSizes()
  330. {
  331.         omnibox_edit.width = Form.cwidth - omnibox_edit.left - 52 - 16;
  332.         WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.width - 10 - scroll_wv.size_x,
  333.                 Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
  334.         WB1.list.wheel_size = 7 * BASIC_LINE_H;
  335.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
  336.         WB1.list.visible = WB1.list.h;
  337. }
  338.  
  339.  
  340. void draw_window()
  341. {
  342.         bool burger_active = false;
  343.         if (menu_id == OPEN_FILE) burger_active = true;
  344.  
  345.         SetElementSizes();
  346.  
  347.         DrawBar(0,0, Form.cwidth,PADDING, sc.work);
  348.         DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
  349.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.work_dark, sc.work, 180));
  350.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.work_graph);
  351.         DrawBar(0, PADDING, omnibox_edit.left-2, TSZE+1, sc.work);
  352.         DrawBar(omnibox_edit.left+omnibox_edit.width+18, PADDING, Form.cwidth-omnibox_edit.left-omnibox_edit.width-18, TSZE+1, sc.work);
  353.  
  354.         DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
  355.         DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
  356.         DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
  357.  
  358.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.work_graph);
  359.  
  360.         DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
  361.                 WB1.list.h-1, scroll_wv.bckg_col);
  362.  
  363.         if (!BrowserWidthChanged()) {
  364.                 WB1.DrawPage();
  365.                 DrawOmnibox();
  366.         }
  367.         DrawProgress();
  368.         DrawStatusBar(NULL);
  369.         DrawTabsBar();
  370. }
  371.  
  372. bool BrowserWidthChanged()
  373. {
  374.         dword source_mode_holder;
  375.         if (WB1.list.w!=canvas.bufw) {
  376.                 canvas.Init(WB1.list.x, WB1.list.y, WB1.list.w, 400*20);
  377.                 if (!strncmp(history.current(),"http",4)) {
  378.                         //nihuya ne izyashnoe reshenie, no pust' poka butet tak
  379.                         source_mode_holder = source_mode;
  380.                         LoadInternalPage(#loading_text, sizeof(loading_text));
  381.                         source_mode = source_mode_holder;
  382.                 }
  383.                 OpenPage(history.current());
  384.                 return true;
  385.         }
  386.         return false;
  387. }
  388.  
  389.  
  390. void EventChangeEncodingAndLoadPage(int _new_encoding)
  391. {
  392.         dword newbuf, newsize;
  393.         WB1.custom_encoding = _new_encoding;
  394.         newsize = strlen(WB1.o_bufpointer);
  395.         newbuf = malloc(newsize);
  396.         memmov(newbuf, WB1.o_bufpointer, newsize);
  397.         LoadInternalPage(newbuf, newsize);
  398.         free(newbuf);
  399. }
  400.  
  401.  
  402. void EventScrollUpAndDown(int _direction)
  403. {
  404.         int i;
  405.         for (i=0;i<WB1.list.item_h*2;i++) {
  406.                 if (_direction == SCAN_CODE_UP) WB1.list.KeyUp();
  407.                 if (_direction == SCAN_CODE_DOWN) WB1.list.KeyDown();
  408.         }
  409.         WB1.DrawPage();
  410. }
  411.  
  412. void EventToggleDebugMode()
  413. {
  414.         debug_mode ^= 1;
  415.         if (debug_mode) notify("'Debug mode ON'-I");
  416.         else notify("'Debug mode OFF'-I");
  417. }
  418.  
  419. void EventAllTabsClick(dword _n)
  420. {
  421.         if (http.transfer) return;
  422.         if (mouse.mkm) {
  423.                 EventTabClose(_n);
  424.         } else {
  425.                 EventTabClick(_n);
  426.         }
  427. }
  428.  
  429. void EventEditSource()
  430. {
  431.         if (check_is_the_adress_local(history.current())) {
  432.                 RunProgram("/rd/1/quark", history.current());
  433.         } else {
  434.                 CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
  435.                 if (!EAX) RunProgram("/rd/1/quark", "/tmp0/1/WebView_tmp.htm");
  436.         }
  437. }
  438.  
  439. void EventClearCache()
  440. {
  441.         cache.clear();
  442.         notify(#clear_cache_ok);
  443.         EventRefreshPage();
  444. }
  445.  
  446. void EventCopyLinkToClipboard()
  447. {
  448.         Clipboard__CopyText(GetAbsoluteActiveURL());
  449.         notify("'URL copied to clipboard'O");
  450. }
  451.  
  452. void StopLoading()
  453. {
  454.         if (http.stop()) pause(10);
  455.         prbar.value = 0;
  456. }
  457.  
  458. //rewrite into
  459. //bool strrpl(dword dst, from, into, dst_len);
  460. bool ReplaceSpaceInUrl(dword url, size) {
  461.         unsigned int i, j;
  462.         bool was_changed=false;
  463.         for (i=url+size-3; i>url; i--)
  464.         {
  465.                 if (ESBYTE[i]!=' ') continue;
  466.                 for (j=url+size-3; j>=i; j--) {
  467.                         ESBYTE[j+3]=ESBYTE[j+2];
  468.                         ESBYTE[j+2]=ESBYTE[j+1];
  469.                         ESBYTE[j+1]=ESBYTE[j];
  470.                 }
  471.                 ESBYTE[i] = '%';
  472.                 ESBYTE[i+1] = '2';
  473.                 ESBYTE[i+2] = '0';
  474.                 was_changed = true;
  475.         }
  476.         return was_changed;
  477. }
  478.  
  479. bool HandleUrlFiles(dword _path, _data)
  480. {
  481.         dword url_from_file;
  482.         if (!UrlExtIs(_path, "url")) return false;
  483.         if (! url_from_file = strstri(_data, "URL=")) return false;
  484.         replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
  485.         OpenPage(url_from_file);        
  486.         return true;   
  487. }
  488.  
  489. bool GetLocalFileData(dword _path)
  490. {
  491.         dword data, size;
  492.         read_file(_path, #data, #size);
  493.         if (!HandleUrlFiles(_path, data)) {
  494.                 LoadInternalPage(data, size);
  495.         }
  496.         free(data);
  497.         return true;
  498. }
  499.  
  500. bool GetUrl(dword _http_url)
  501. {
  502.         char new_url_full[URL_SIZE+1];
  503.  
  504.         if (!strncmp(_http_url,"http:",5)) {
  505.                 http.get(_http_url);
  506.                 return true;
  507.         } else if (!strncmp(_http_url,"https://",8)) {
  508.                 strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
  509.                 strncat(#new_url_full, _http_url, URL_SIZE);
  510.                 http.get(#new_url_full);
  511.                 return true;
  512.         }
  513.         return false;
  514. }
  515.  
  516. void OpenPage(dword _open_URL)
  517. {
  518.         char new_url[URL_SIZE+1];
  519.         int unz_id;
  520.  
  521.         StopLoading();
  522.  
  523.         SetOmniboxText(_open_URL);
  524.  
  525.         strncpy(#new_url, _open_URL, URL_SIZE);
  526.  
  527.         //Exclude # from the URL to the load page
  528.         //We will bring it back when we get the buffer
  529.         if (strrchr(#new_url, '#')) {
  530.                 anchors.take_anchor_from(#new_url);
  531.         }
  532.  
  533.         /*
  534.         There could be several possible types of addresses:
  535.         - cached page (only http/https)
  536.         - internal page
  537.         - web page
  538.         - local file
  539.         So we need to detect what incoming address is
  540.         and then halndle it in the propper way.
  541.         */
  542.  
  543.         if (cache.has(#new_url)) {
  544.                 //CACHED PAGE
  545.                 if (cache.current_type==PAGE) {
  546.                         history.add(#new_url);
  547.                         LoadInternalPage(cache.current_buf, cache.current_size);
  548.                 }
  549.                 else {
  550.                         EventDownloadAndOpenImage(#new_url);
  551.                 }
  552.  
  553.         } else if (!strncmp(#new_url,"WebView:",8)) {
  554.                 //INTERNAL PAGE
  555.                 history.add(#new_url);
  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.                         history.add(#new_url);
  574.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  575.                 }
  576.         } else {
  577.                 //LOCAL PAGE
  578.                 history.add(#new_url);
  579.                 if (UrlExtIs(#new_url,".docx")) {
  580.                         DeleteFile("/tmp0/1/temp/word/document.xml");
  581.                         CreateDir("/tmp0/1/temp");
  582.                         unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
  583.                         while (GetProcessSlot(unz_id)) pause(2);
  584.                         strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
  585.                 }
  586.                 if (!GetLocalFileData(#new_url)) {
  587.                         LoadInternalPage(#buildin_page_error, sizeof(buildin_page_error));
  588.                 }
  589.         }
  590. }
  591.  
  592. dword EventOpenDownloader(dword _url)
  593. {
  594.         //char download_params[URL_SIZE+50];
  595.         return RunProgram("/sys/network/dl", _url);
  596. }
  597.  
  598. bool EventClickAnchor()
  599. {
  600.         dword aURL = links.active_url;
  601.  
  602.         if (anchors.get_pos_by_name(aURL+1)!=-1) {
  603.                 WB1.list.first = anchors.get_pos_by_name(aURL+1);
  604.                 WB1.list.CheckDoesValuesOkey();
  605.                 strcpy(#editURL, history.current());
  606.                 strcat(#editURL, aURL);
  607.                 DrawOmnibox();
  608.                 WB1.DrawPage();
  609.                 return true;
  610.         }
  611.         return false;
  612. }
  613.  
  614. void EventClickLink(dword _target)
  615. {
  616.         char new_url[URL_SIZE+1];
  617.         char new_url_full[URL_SIZE+1];
  618.         dword aURL = GetAbsoluteActiveURL();
  619.         if (!aURL) return;
  620.  
  621.         strcpy(#new_url, aURL);
  622.  
  623.         if (ESBYTE[aURL]=='#') {
  624.                 if (_target == TARGET_SAME_TAB) {
  625.                         EventClickAnchor();
  626.                         return;
  627.                 } else {
  628.                         strcpy(#new_url, history.current());
  629.                         strcat(#new_url, aURL);
  630.                 }
  631.         }
  632.  
  633.         if (_target == TARGET_NEW_TAB) {
  634.                 EventOpenNewTab(#new_url);
  635.                 return;
  636.         }
  637.  
  638.         if (_target == TARGET_NEW_WINDOW) {
  639.                 strcpy(#new_url_full, "-new ");
  640.                 strncat(#new_url_full, #new_url, URL_SIZE);
  641.                 RunProgram(#program_path, #new_url_full);
  642.                 return;
  643.         }
  644.  
  645.         if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
  646.                 notify(#new_url);
  647.                 return;
  648.         }
  649.  
  650.         if (http.transfer) {
  651.                 StopLoading();
  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(NULL);
  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.                 if (WB1.img_url.count) 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. void EventUpdateBrowser()
  816. {
  817.         dword downloader_id, slot_n;
  818.         dword current_size;
  819.         dword new_size;
  820.  
  821.         draw_window();
  822.  
  823.         downloader_id = EventOpenDownloader(#update_param);
  824.         do {
  825.                 slot_n = GetProcessSlot(downloader_id);
  826.                 pause(10);
  827.         } while (slot_n!=0);
  828.  
  829.         current_size = get_file_size(#program_path);
  830.         new_size = get_file_size("/tmp0/1/Downloads/WebView.com");
  831.  
  832.         if (!new_size) || (new_size<5000) { notify(#update_download_error); return; }
  833.         if (current_size == new_size) { notify(#update_is_current);     return; }
  834.  
  835.         if (CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
  836.                 notify(#update_can_not_copy);
  837.         } else {
  838.                 notify(#update_ok);
  839.                 RunProgram(#program_path, history.current());
  840.                 ExitProcess();
  841.         }
  842. }
  843.  
  844. void DrawStatusBar(dword _msg)
  845. {
  846.         dword status_y = Form.cheight - STATUSBAR_H + 4;
  847.         dword status_w = Form.cwidth - 90;
  848.         if (Form.status_window>2) return;
  849.         DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
  850.         if (_msg) {
  851.                 ESI = math.min(status_w/6, strlen(_msg));
  852.                 WriteText(10, status_y, 0, sc.work_text, _msg);
  853.         }
  854.         DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
  855.         WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
  856. }
  857.  
  858. void DrawOmnibox()
  859. {
  860.         int imgxoff;
  861.        
  862.         DrawOvalBorder(omnibox_edit.left-2, omnibox_edit.top-3, omnibox_edit.width+18, 24, sc.work_graph,
  863.                 sc.work_graph, sc.work_graph, sc.work_dark);
  864.         DrawBar(omnibox_edit.left-1, omnibox_edit.top-2, omnibox_edit.width+18, 1, 0xD8DCD8);
  865.         DrawBar(omnibox_edit.left-1, omnibox_edit.top-1, omnibox_edit.width+18, 1, omnibox_edit.color);
  866.         DrawBar(omnibox_edit.left-1, omnibox_edit.top, 1, 22, omnibox_edit.color);
  867.  
  868.         if (omnibox_edit.flags & ed_focus) omnibox_edit.flags = ed_focus; else omnibox_edit.flags = 0;
  869.         EditBox_UpdateText(#omnibox_edit, omnibox_edit.flags);
  870.         edit_box_draw stdcall(#omnibox_edit);
  871.         if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
  872.         _PutImage(omnibox_edit.left+omnibox_edit.width+1, omnibox_edit.top-1, 16, 23, imgxoff + #editbox_icons);
  873.         DefineHiddenButton(omnibox_edit.left+omnibox_edit.width-1, omnibox_edit.top-2, 17, 23, REFRESH_BUTTON);
  874.  
  875.         DrawProgress();
  876. }
  877.  
  878. void SetOmniboxText(dword _text)
  879. {
  880.         edit_box_set_text stdcall (#omnibox_edit, _text);
  881.         omnibox_edit.pos = omnibox_edit.flags = 0;
  882.         DrawOmnibox();
  883. }
  884.  
  885. dword GetAbsoluteActiveURL()
  886. {
  887.         char abs_url[URL_SIZE];
  888.         if (links.active_url) {
  889.                 strncpy(#abs_url, links.active_url, URL_SIZE);
  890.                 get_absolute_url(#abs_url, history.current());         
  891.                 return #abs_url;
  892.         }
  893.         return 0;
  894. }
  895.  
  896. void CheckContentType()
  897. {
  898.         char content_type[64];
  899.         if (http.header_field("content-type", #content_type, sizeof(content_type))) // application || image
  900.  
  901.         if (content_type[0] == 'i') {
  902.                 EventDownloadAndOpenImage(http.cur_url);
  903.                 StopLoading();
  904.         }if (content_type[0] == 'a') {
  905.                 EventOpenDownloader(history.current());
  906.                 StopLoading();
  907.         }
  908. }
  909.  
  910. void EventDownloadAndOpenImage(dword _url)
  911. {
  912.         char image_download_url[URL_SIZE];
  913.         strcpy(#image_download_url, "-eo ");
  914.         strncat(#image_download_url, _url, URL_SIZE);
  915.         EventOpenDownloader(#image_download_url);
  916. }
  917.  
  918. void HandleRedirect()
  919. {
  920.         char redirect_url[URL_SIZE];
  921.         http.header_field("location", #redirect_url, URL_SIZE);
  922.         get_absolute_url(#redirect_url, http.cur_url);
  923.         http.hfree();
  924.         if (http_get_type==PAGE) OpenPage(#redirect_url);
  925.         else if (http_get_type==IMG) GetUrl(#redirect_url);
  926. }
  927.  
  928. dword GetImg(bool _new)
  929. {
  930.         int i;
  931.         if (!show_images) return;
  932.         http_get_type = IMG;
  933.  
  934.         for (i = 0; i < WB1.img_url.count; i++)
  935.         {
  936.                 cur_img_url = WB1.img_url.get(i);
  937.                 if (cache.has(cur_img_url)==false) {
  938.                         prbar.max = WB1.img_url.count;
  939.                         prbar.value = i;
  940.                         if (GetUrl(cur_img_url)) {DrawStatusBar(cur_img_url); DrawProgress(); return;}
  941.                 }
  942.         }
  943.         if (_new) return;
  944.         DrawOmnibox();
  945.         DrawStatusBar(T_RENDERING);
  946.         WB1.ParseHtml(WB1.o_bufpointer, WB1.bufsize);
  947.         WB1.DrawPage();
  948.         debugln(sprintf(#param, T_DONE_IN_SEC, GetStartTime()-render_start_time/100));
  949.         DrawStatusBar(NULL);
  950. }
  951.  
  952. stop: