Subversion Repositories Kolibri OS

Rev

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