Subversion Repositories Kolibri OS

Rev

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