Subversion Repositories Kolibri OS

Rev

Rev 9602 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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