Subversion Repositories Kolibri OS

Rev

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