Subversion Repositories Kolibri OS

Rev

Rev 8462 | Rev 8492 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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