Subversion Repositories Kolibri OS

Rev

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

  1. //HTML Viewer in C--
  2. //Copyright 2007-2013 by Veliant & Leency
  3. //Asper, lev, Lrz, Barsuk, Nable...
  4. //home icon - rachel fu, GPL licence
  5.  
  6. #ifndef AUTOBUILD
  7.         #include "lang.h--"
  8. #endif
  9.  
  10. //libraries
  11. #define MEMSIZE 4096 * 256
  12. #include "..\lib\gui.h"
  13. #include "..\lib\draw_buf.h"
  14. #include "..\lib\list_box.h"
  15. #include "..\lib\cursor.h"
  16. #include "..\lib\collection.h"
  17. #include "..\lib\menu.h"
  18.  
  19. //*.obj libraries
  20. #include "..\lib\obj\box_lib.h"
  21. #include "..\lib\obj\libio_lib.h"
  22. #include "..\lib\obj\libimg_lib.h"
  23. #include "..\lib\obj\http.h"
  24. #include "..\lib\obj\iconv.h"
  25. //useful patterns
  26. #include "..\lib\patterns\libimg_load_skin.h"
  27. #include "..\lib\patterns\history.h"
  28. #include "..\lib\patterns\http_downloader.h"
  29.  
  30. char homepage[] = FROM "html\\homepage.htm""\0";
  31.  
  32. #ifdef LANG_RUS
  33. char version[]="’¥ªáâ®¢ë© ¡à ã§¥à 1.5";
  34. ?define IMAGES_CACHE_CLEARED "Šíè ª à⨭®ª ®ç¨é¥­"
  35. ?define T_LAST_SLIDE "â® ¯®á«¥¤­¨© á« ©¤"
  36. char loading[] = "‡ £à㧪  áâà ­¨æë...<br>";
  37. char page_not_found[] = FROM "html\\page_not_found_ru.htm""\0";
  38. char accept_language[]= "Accept-Language: ru\n";
  39. char rmb_menu[] =
  40. "®á¬®âà¥âì ¨á室­¨ª
  41. ¥¤ ªâ¨à®¢ âì ¨á室­¨ª
  42. ˆáâ®à¨ï
  43. Žç¨áâ¨âì ªíè ª à⨭®ª
  44. Œ¥­¥¤¦¥à § £à㧮ª";
  45. #else
  46. char version[]="Text-based Browser 1.5";
  47. ?define IMAGES_CACHE_CLEARED "Images cache cleared"
  48. ?define T_LAST_SLIDE "This slide is the last"
  49. char loading[] = "Loading...<br>";
  50. char page_not_found[] = FROM "html\\page_not_found_en.htm""\0";
  51. char accept_language[]= "Accept-Language: en\n";
  52. char rmb_menu[] =
  53. "View source
  54. Edit source
  55. History
  56. Free image cache
  57. Download Manager";
  58. #endif
  59.  
  60. #define URL_SERVICE_HISTORY "WebView://history"
  61. #define URL_SERVICE_HOME "WebView://home"
  62. #define URL_SERVICE_SOURCE "WebView://source:"
  63.  
  64. proc_info Form;
  65.  
  66. //char search_path[]="http://nigma.ru/index.php?s=";
  67. int redirected = 0;
  68.  
  69. char stak[4096];
  70.  
  71. int action_buf;
  72.  
  73. dword http_transfer = 0;
  74. dword http_buffer;
  75.  
  76. dword TOOLBAR_H = 33;
  77. dword STATUSBAR_H = 15;
  78. dword col_bg;
  79. dword panel_color;
  80. dword border_color;
  81.  
  82. progress_bar wv_progress_bar;
  83. byte souce_mode = false;
  84.  
  85. enum {
  86.         BACK_BUTTON=1000,
  87.         FORWARD_BUTTON,
  88.         REFRESH_BUTTON,
  89.         GOTOURL_BUTTON,
  90.         SANDWICH_BUTTON
  91. };
  92.  
  93. enum {
  94.         VIEW_SOURCE=1100,
  95.         EDIT_SOURCE,
  96.         VIEW_HISTORY,
  97.         FREE_IMG_CACHE,
  98.         DOWNLOAD_MANAGER
  99. };
  100.  
  101. #include "..\TWB\TWB.c"
  102. #include "history.h"
  103. #include "show_src.h"
  104. #include "download_manager.h"
  105.  
  106. char editURL[sizeof(URL)];
  107. int     mouse_twb;
  108. edit_box address_box = {250,56,34,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#editURL,#mouse_twb,2,19,19};
  109.  
  110.  
  111. void main()
  112. {
  113.         CursorPointer.Load(#CursorFile);
  114.         load_dll(boxlib, #box_lib_init,0);
  115.         load_dll(libio, #libio_init,1);
  116.         load_dll(libimg, #libimg_init,1);
  117.         load_dll(libHTTP, #http_lib_init,1);
  118.         load_dll(iconv_lib, #iconv_open,0);
  119.         Libimg_LoadImage(#skin, abspath("wv_skin.png"));
  120.         SetSkinColors();
  121.         CreateDir("/tmp0/1/downloads");
  122.         if (param) strcpy(#URL, #param); else strcpy(#URL, URL_SERVICE_HOME);
  123.         WB1.DrawBuf.zoom = 1;
  124.         WB1.list.SetFont(8, 14, 10011000b);
  125.         WB1.list.no_selection = true;
  126.         SetEventMask(0xa7);
  127.         BEGIN_LOOP_APPLICATION:
  128.                 WaitEventTimeout(2);
  129.                 switch(EAX & 0xFF)
  130.                 {
  131.                         CASE evMouse:
  132.                                 if (!CheckActiveProcess(Form.ID)) break;
  133.                                 edit_box_mouse stdcall (#address_box);
  134.                                 mouse.get();
  135.                                 if (WB1.list.MouseOver(mouse.x, mouse.y))
  136.                                 {
  137.                                         PageLinks.Hover(mouse.x, WB1.list.first*WB1.list.item_h + mouse.y, link_color_inactive, link_color_active, bg_color);
  138.                                         if (bufsize) && (mouse.pkm) && (mouse.up) {
  139.                                                 menu.show(Form.left+mouse.x-6,Form.top+mouse.y+skin_height+3, 180, #rmb_menu, VIEW_SOURCE);
  140.                                                 break;
  141.                                         }
  142.                                         if (WB1.list.MouseScroll(mouse.vert)) WB1.DrawPage();
  143.                                 }
  144.                                 scrollbar_v_mouse (#scroll_wv);
  145.                                 if (WB1.list.first != scroll_wv.position)
  146.                                 {
  147.                                         WB1.list.first = scroll_wv.position;
  148.                                         WB1.DrawPage();
  149.                                         break;
  150.                                 }
  151.                                 break;
  152.  
  153.                         case evButton:
  154.                                 ProcessEvent(GetButtonID());
  155.                                 break;
  156.  
  157.                         case evKey:
  158.                                 GetKeys();
  159.                                 if (address_box.flags & 0b10)  
  160.                                 {
  161.                                         if (key_ascii == ASCII_KEY_ENTER) ProcessEvent(key_scancode); else {
  162.                                                 EAX = key_editbox;
  163.                                                 edit_box_key stdcall(#address_box);
  164.                                         }
  165.                                 }
  166.                                 else
  167.                                 {
  168.                                         if (WB1.list.ProcessKey(key_scancode)) WB1.DrawPage();
  169.                                         else ProcessEvent(key_scancode);
  170.                                 }
  171.                                 break;
  172.  
  173.                         case evReDraw:
  174.                                 if (menu.list.cur_y) {
  175.                                         ProcessEvent(menu.list.cur_y);
  176.                                         menu.list.cur_y = 0;
  177.                                 }
  178.                                 DefineAndDrawWindow(GetScreenWidth()-800/2,GetScreenHeight()-600/2,800,600,0x73,col_bg,0,0);
  179.                                 GetProcessInfo(#Form, SelfInfo);
  180.                                 if (Form.status_window>2) { DrawTitle(#header); break; }
  181.                                 if (Form.height<120) { MoveSize(OLD,OLD,OLD,120); break; }
  182.                                 if (Form.width<280) { MoveSize(OLD,OLD,280,OLD); break; }
  183.                                 Draw_Window();
  184.                                 break;
  185.                                
  186.                         case evNetwork:
  187.                                 if (http_transfer > 0) {
  188.                                         http_receive stdcall (http_transfer);
  189.                                         $push EAX
  190.                                         ESI = http_transfer;
  191.                                         wv_progress_bar.max = ESI.http_msg.content_length;
  192.                                         if (wv_progress_bar.value != ESI.http_msg.content_received)
  193.                                         {
  194.                                                 wv_progress_bar.value = ESI.http_msg.content_received; 
  195.                                                 DrawProgress();
  196.                                         }
  197.                                         $pop EAX
  198.                                         if (EAX == 0) {
  199.                                                 ESI = http_transfer;
  200.                                                 // Handle redirects
  201.                                                 if (ESI.http_msg.status >= 300) && (ESI.http_msg.status < 400)
  202.                                                 {
  203.                                                         redirected++;
  204.                                                         if (redirected<=5)
  205.                                                         {
  206.                                                                 http_find_header_field stdcall (http_transfer, "location\0");
  207.                                                                 if (EAX!=0) {
  208.                                                                         ESI = EAX;
  209.                                                                         EDI = #URL;
  210.                                                                         do {
  211.                                                                                 $lodsb;
  212.                                                                                 $stosb;
  213.                                                                         } while (AL != 0) && (AL != 13) && (AL != 10);
  214.                                                                         DSBYTE[EDI-1]='\0';
  215.                                                                 }
  216.                                                         }
  217.                                                         else
  218.                                                         {
  219.                                                                 notify("Too many redirects");
  220.                                                                 StopLoading();
  221.                                                                 break;
  222.                                                         }
  223.                                                 }
  224.                                                 else
  225.                                                 {
  226.                                                         redirected = 0;
  227.                                                 }
  228.                                                 // Loading the page is complete, free resources
  229.                                                 if (redirected>0)
  230.                                                 {
  231.                                                         http_free stdcall (http_transfer);
  232.                                                         http_transfer=0;
  233.                                                         GetAbsoluteURL(#URL);
  234.                                                         history.back();
  235.                                                         strcpy(#editURL, #URL);
  236.                                                         DrawEditBoxWebView();
  237.                                                         OpenPage();
  238.                                                 }
  239.                                                 else
  240.                                                 {
  241.                                                         history.add(#URL);
  242.                                                         ESI = http_transfer;
  243.                                                         bufpointer = ESI.http_msg.content_ptr;
  244.                                                         bufsize = ESI.http_msg.content_received;
  245.                                                         http_free stdcall (http_transfer);
  246.                                                         http_transfer=0;
  247.                                                         SetPageDefaults();
  248.                                                         ShowPage();
  249.                                                 }
  250.                                         }
  251.                                 }
  252.                 }
  253.         goto BEGIN_LOOP_APPLICATION;
  254. }
  255.  
  256. void SetElementSizes()
  257. {
  258.         address_box.top = TOOLBAR_H/2-7;
  259.         address_box.width = Form.cwidth - address_box.left - 25 - 22;
  260.         WB1.list.SetSizes(0, TOOLBAR_H, Form.width - 10 - scroll_wv.size_x / WB1.DrawBuf.zoom,
  261.                 Form.cheight - TOOLBAR_H - STATUSBAR_H, WB1.list.font_h + WB1.DrawBuf.zoom + WB1.DrawBuf.zoom * WB1.DrawBuf.zoom);
  262.         WB1.list.wheel_size = 7;
  263.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w;
  264.         WB1.list.visible = WB1.list.h - 5 / WB1.list.item_h;
  265.         if (WB1.list.w!=WB1.DrawBuf.bufw) {
  266.                 if (WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, 2048 * WB1.list.item_h) == false)
  267.                 die("Memory allocation error! Seems to be too little RAM.");
  268.                 ProcessEvent(REFRESH_BUTTON);
  269.         }
  270. }
  271.  
  272.  
  273.  
  274. void Draw_Window()
  275. {
  276.         DrawBar(0,0, Form.cwidth,TOOLBAR_H-2, panel_color);
  277.         DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, 0xD7D0D3);
  278.         DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, border_color);
  279.         SetElementSizes();
  280.         DrawRectangle(address_box.left-3, address_box.top-3, address_box.width+5, 20,border_color);
  281.         DefineButton(address_box.left-50, address_box.top-2, 23, skin.h-2, BACK_BUTTON+BT_HIDE, 0);
  282.         DefineButton(address_box.left-26, address_box.top-2, 23, skin.h-2, FORWARD_BUTTON+BT_HIDE, 0);
  283.         img_draw stdcall(skin.image, address_box.left-51, address_box.top-3, 48, skin.h, 3, 0);
  284.         DefineButton(address_box.left+address_box.width+1, address_box.top-3, 16, skin.h-1, REFRESH_BUTTON+BT_HIDE+BT_NOFRAME, 0);
  285.         DefineButton(Form.cwidth-24, address_box.top-3, 19, skin.h-1, SANDWICH_BUTTON+BT_HIDE, 0);
  286.         img_draw stdcall(skin.image, Form.cwidth-22, address_box.top-3, 16, skin.h, 85, 0);
  287.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,STATUSBAR_H, col_bg);
  288.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, border_color);
  289.         if (!header) OpenPage(); else { WB1.DrawPage(); DrawEditBoxWebView(); }
  290.         DrawRectangle(scroll_wv.start_x, scroll_wv.start_y, scroll_wv.size_x, scroll_wv.size_y-1, scroll_wv.bckg_col);
  291.         DrawProgress();
  292. }
  293.  
  294.  
  295. void ProcessEvent(dword id__)
  296. {
  297.         switch (id__)
  298.         {
  299.                 case 1:
  300.                         ExitProcess();
  301.                         return;
  302.                 case SCAN_CODE_BS:
  303.                 case BACK_BUTTON:
  304.                         if (history.back()) {
  305.                                 strcpy(#URL, history.current());
  306.                                 OpenPage();
  307.                         }
  308.                         return;
  309.                 case FORWARD_BUTTON:
  310.                         if (history.forward()) {
  311.                                 strcpy(#URL, history.current());
  312.                                 OpenPage();
  313.                         }
  314.                         return;
  315.                 case GOTOURL_BUTTON:
  316.                 case SCAN_CODE_ENTER:
  317.                         if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/') || (!strncmp(#editURL,"WebView:",9))
  318.                         {
  319.                                 strcpy(#URL, #editURL);
  320.                         }
  321.                         else
  322.                         {
  323.                                 strlcpy(#URL,"http://",7);
  324.                                 strcat(#URL, #editURL);
  325.                         }
  326.                         OpenPage();
  327.                         return;
  328.                 case 063: //F5
  329.                         IF(address_box.flags & 0b10) return;
  330.                 case REFRESH_BUTTON:
  331.                         if (http_transfer > 0)
  332.                         {
  333.                                 StopLoading();
  334.                                 Draw_Window();
  335.                         }
  336.                         else OpenPage();
  337.                         return;
  338.                 case SANDWICH_BUTTON:
  339.                         mouse.y = TOOLBAR_H-6;
  340.                         mouse.x = Form.cwidth - 167;
  341.                         menu.show(Form.left+mouse.x-6,Form.top+mouse.y+skin_height+3, 180, #rmb_menu, VIEW_SOURCE);
  342.                         return;
  343.                 case VIEW_SOURCE:
  344.                         WB1.list.first = 0;
  345.                         ShowSource();
  346.                         WB1.LoadInternalPage(bufpointer, bufsize);
  347.                         break;
  348.                 case EDIT_SOURCE:
  349.                         if (!strncmp(#URL,"http:",5))
  350.                         {
  351.                                 WriteFile(bufsize, bufpointer, "/tmp0/1/WebView_tmp.htm");
  352.                                 if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
  353.                         }
  354.                         else RunProgram("/rd/1/tinypad", #URL);
  355.                         return;
  356.                 case FREE_IMG_CACHE:
  357.                         ImgCache.Free();
  358.                         notify(IMAGES_CACHE_CLEARED);
  359.                         WB1.DrawPage();
  360.                         return;
  361.                 case VIEW_HISTORY:
  362.                         strcpy(#URL, URL_SERVICE_HISTORY);
  363.                         OpenPage();
  364.                         return;
  365.                 case DOWNLOAD_MANAGER:
  366.                         if (!downloader_opened) {
  367.                                 downloader_edit = NULL;
  368.                                 CreateThread(#Downloader,#downloader_stak+4092);
  369.                         }
  370.                         return;
  371.         }
  372. }
  373.  
  374.  
  375. void StopLoading()
  376. {
  377.         if (http_transfer)
  378.         {
  379.                 EAX = http_transfer;
  380.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  381.                 $push   EAX                                                     // save it on the stack
  382.                 http_free stdcall (http_transfer);      // abort connection
  383.                 $pop    EAX                                                    
  384.                 free(EAX);                                              // free data
  385.                 http_transfer=0;
  386.                 bufsize = 0;
  387.                 bufpointer = free(bufpointer);
  388.         }
  389.         wv_progress_bar.value = 0;
  390.         img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, 52, 0);
  391. }
  392.  
  393. void SetPageDefaults()
  394. {
  395.         strcpy(#header, #version);
  396.         WB1.list.count = WB1.list.first = 0;
  397.         stroka = 0;
  398.         cur_encoding = CH_NULL;
  399.         if (o_bufpointer) o_bufpointer = free(o_bufpointer);
  400.         anchor_line_num=WB1.list.first;
  401.         anchor[0]='|';
  402. }
  403.  
  404. void OpenPage()
  405. {
  406.         StopLoading();
  407.         souce_mode = false;
  408.         strcpy(#editURL, #URL);
  409.         history.add(#URL);
  410.         if (!strncmp(#URL,"WebView:",8))
  411.         {
  412.                 SetPageDefaults();
  413.                 if (!strcmp(#URL, URL_SERVICE_HOME)) WB1.LoadInternalPage(#homepage, sizeof(homepage));
  414.                 else if (!strcmp(#URL, URL_SERVICE_HISTORY)) ShowHistory();
  415.                 DrawEditBoxWebView();
  416.                 return;
  417.         }
  418.         if (!strncmp(#URL,"http:",5))
  419.         {
  420.                 img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, 131, 0);
  421.                 http_get stdcall (#URL, 0, 0, #accept_language);
  422.                 http_transfer = EAX;
  423.                 if (!http_transfer)
  424.                 {
  425.                         StopLoading();
  426.                         bufsize = 0;
  427.                         bufpointer = free(bufpointer);
  428.                         ShowPage();
  429.                         return;
  430.                 }
  431.         }
  432.         else
  433.         {
  434.                 file_size stdcall (#URL);
  435.                 bufsize = EBX;
  436.                 if (bufsize)
  437.                 {
  438.                         free(bufpointer);
  439.                         bufpointer = malloc(bufsize);
  440.                         SetPageDefaults();
  441.                         ReadFile(0, bufsize, bufpointer, #URL);
  442.                 }
  443.                 ShowPage();
  444.         }
  445. }
  446.  
  447. DrawEditBoxWebView()
  448. {
  449.         DrawWideRectangle(address_box.left-2, address_box.top-2, address_box.width+3, 19, 2, address_box.color);
  450.         address_box.size = address_box.pos = address_box.shift = address_box.shift_old = strlen(#editURL);
  451.         address_box.offset = 0;
  452.         edit_box_draw stdcall(#address_box);
  453.         if (http_transfer > 0) EAX = 131; else EAX = 52;
  454.         img_draw stdcall(skin.image, address_box.left+address_box.width+1, address_box.top-3, 17, skin.h, EAX, 0);
  455. }
  456.  
  457.  
  458. void ShowPage()
  459. {
  460.         DrawEditBoxWebView();
  461.         if (!bufsize)
  462.         {
  463.                 if (http_transfer) WB1.LoadInternalPage(#loading, sizeof(loading));
  464.                 else WB1.LoadInternalPage(#page_not_found, sizeof(page_not_found));
  465.         }
  466.         else
  467.         {
  468.                 WB1.Prepare();
  469.         }
  470.         //if (!header) strcpy(#header, #version);
  471.         if (!strcmp(#version, #header)) DrawTitle(#header);
  472. }
  473.  
  474. byte UrlExtIs(dword ext)
  475. {
  476.         if (!strcmpi(#URL + strlen(#URL) - strlen(ext), ext)) return true;
  477.         return false;
  478. }
  479.  
  480. int SetSkinColors()
  481. {
  482.         dword image_data;
  483.         image_data = DSDWORD[skin.image+24];
  484.         col_bg = DSDWORD[image_data];
  485.         panel_color  = DSDWORD[skin.w*4*4 + image_data];
  486.         border_color = DSDWORD[skin.w*4*7 + image_data];
  487.         wv_progress_bar.progress_color = DSDWORD[skin.w*4*10 + image_data];
  488.         $and col_bg, 0x00ffffff
  489.         $and panel_color, 0x00ffffff
  490.         $and border_color, 0x00ffffff
  491.         $and wv_progress_bar.progress_color, 0x00ffffff
  492. }
  493.  
  494. void DrawProgress()
  495. {
  496.         unsigned long btn;
  497.         if (http_transfer == 0) return;
  498.         if (wv_progress_bar.max) btn = address_box.width*wv_progress_bar.value/wv_progress_bar.max; else btn = 30;
  499.         DrawBar(address_box.left-2, address_box.top+15, btn, 2, wv_progress_bar.progress_color);
  500. }
  501.  
  502. void ClickLink()
  503. {
  504.         if (http_transfer > 0)
  505.         {
  506.                 StopLoading();
  507.                 history.back();
  508.         }
  509.  
  510.         strcpy(#URL, PageLinks.GetURL(PageLinks.active));      
  511.         //#1
  512.         if (URL[0] == '#')
  513.         {
  514.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));              
  515.                 strcpy(#URL, history.current());
  516.                 WB1.list.first=WB1.list.count-WB1.list.visible;
  517.                 ShowPage();
  518.                 return;
  519.         }
  520.         //liner.ru#1
  521.         if (strrchr(#URL, '#')!=-1)
  522.         {
  523.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));
  524.                 URL[strrchr(#URL, '#')-1] = 0x00;
  525.         }
  526.        
  527.         GetAbsoluteURL(#URL);
  528.        
  529.         if (UrlExtIs(".png")==1) || (UrlExtIs(".gif")==1) || (UrlExtIs(".jpg")==1) || (UrlExtIs(".zip")==1) || (UrlExtIs(".kex")==1)
  530.         || (UrlExtIs(".7z")==1) || (UrlExtIs("netcfg")==1)
  531.         {
  532.                 //notify(#URL);
  533.                 if (!strncmp(#URL,"http://", 7))
  534.                 {
  535.                         strcpy(#downloader_edit, #URL);
  536.                         CreateThread(#Downloader,#downloader_stak+4092);
  537.                 }
  538.                 else RunProgram("@open", #URL);
  539.                 strcpy(#editURL, history.current());
  540.                 strcpy(#URL, history.current());
  541.                 return;
  542.         }
  543.         if (!strncmp(#URL,"mailto:", 7))
  544.         {
  545.                 notify(#URL);
  546.                 strcpy(#editURL, history.current());
  547.                 strcpy(#URL, history.current());
  548.                 return;
  549.         }
  550.         OpenPage();
  551.         return;
  552. }
  553.  
  554. stop: