Subversion Repositories Kolibri OS

Rev

Rev 4534 | Go to most recent revision | Blame | 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 0x100000
  12. #include "..\lib\kolibri.h"
  13. #include "..\lib\strings.h"
  14. #include "..\lib\figures.h"
  15. #include "..\lib\encoding.h"
  16. #include "..\lib\file_system.h"
  17. #include "..\lib\mem.h"
  18. #include "..\lib\dll.h"
  19. #include "..\lib\draw_buf.h"
  20. #include "..\lib\list_box.h"
  21. #include "..\lib\cursor.h"
  22.  
  23. //*.obj libraries
  24. #include "..\lib\lib.obj\box_lib.h"
  25. #include "..\lib\lib.obj\libio_lib.h"
  26. #include "..\lib\lib.obj\libimg_lib.h"
  27. #include "..\lib\lib.obj\http.h"
  28. //images
  29. #include "img\toolbar_icons.c"
  30. #include "img\URLgoto.txt";
  31.  
  32. #ifdef LANG_RUS
  33.         char version[]=" ’¥ªáâ®¢ë© ¡à ã§¥à 0.99.65";
  34.         ?define IMAGES_CACHE_CLEARED "Šíè ª à⨭®ª ®ç¨é¥­"
  35.         ?define T_LAST_SLIDE "â® ¯®á«¥¤­¨© á« ©¤"
  36.         char loading[] = "Loading...";
  37.         char page_not_found[] = "‘âà ­¨æ  ­¥ ­ ©¤¥­ . ‚®¬®¦­®, URL ᮤ¥à¦¨â ®è¨¡ªã.";
  38.         char page_not_found_no_internet[] = "‘âà ­¨æ  ­¥ ­ ©¤¥­ . ‚®¬®¦­®, URL ᮤ¥à¦¨â ®è¨¡ªã.<br>ˆ«¨ ­¥â ¤®áâ㯠 ¢ ˆ­â¥à­¥âë.";
  39. #else
  40.         char version[]=" Text-based Browser 0.99.65";
  41.         ?define IMAGES_CACHE_CLEARED "Images cache cleared"
  42.         ?define T_LAST_SLIDE "This slide is the last"
  43.         char loading[] = "Loading...";
  44.         char page_not_found[] = "Page not found. Maybe, URL contains some errors.";
  45.         char page_not_found_no_internet[] = "Page not found. Maybe, URL contains some errors.<br>Or Internet unavailable for your configuration.";
  46. #endif
  47.  
  48. proc_info Form;
  49. #define WIN_W 640
  50. #define WIN_H 480
  51.  
  52. char search_path[]="http://nigma.ru/index.php?s=";
  53.  
  54. char stak[4096];
  55. mouse m;
  56. int action_buf;
  57.  
  58.  
  59. #include "..\TWB\TWB.c"
  60. #include "menu_rmb.h"
  61.  
  62. char editURL[sizeof(URL)];
  63. int     mouse_twb;
  64. edit_box address_box= {250,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#editURL,#mouse_twb,2,19,19};
  65.  
  66.  
  67. enum { BACK=300, FORWARD, REFRESH, HOME, NEWTAB, GOTOURL, SEARCHWEB, INPUT_CH, INPUT_BT, BTN_UP, BTN_DOWN };
  68.  
  69.  
  70. void main()
  71. {
  72.         int key, btn;
  73.         int half_scroll_size;
  74.         int scroll_used=0, show_menu;
  75.        
  76.         mem_Init();
  77.         CursorPointer.Load(#CursorFile);
  78.         if (load_dll2(boxlib, #box_lib_init,0)!=0) {notify("System Error: library doesn't exists /rd/1/lib/box_lib.obj"); ExitProcess();}
  79.         if (load_dll2(libio, #libio_init,1)!=0) notify("Error: library doesn't exists - libio");
  80.         if (load_dll2(libimg, #libimg_init,1)!=0) notify("Error: library doesn't exists - libimg");
  81.        
  82.         if (!URL) strcpy(#URL, "/sys/index.htm");
  83.         Form.width=WIN_W;
  84.         Form.height=WIN_H;
  85.         SetElementSizes();
  86.         OpenPage();
  87.  
  88.         SetEventMask(0xa7);
  89.         loop()
  90.         {
  91.                 WaitEventTimeout(2);
  92.                 switch(EAX & 0xFF)
  93.                 {
  94.                         CASE evMouse:
  95.                                 if (!CheckActiveProcess(Form.ID)) break;
  96.  
  97.                                 edit_box_mouse stdcall (#address_box);
  98.  
  99.                                 m.get();
  100.                                 PageLinks.Hover(m.x, m.y, link_color_inactive, link_color_active, bg_color);
  101.                                
  102.                                 if (m.y>WB1.list.y) && (m.y<Form.height) && (filesize)
  103.                                 {
  104.                                         if (m.pkm)
  105.                                         {
  106.                                                 show_menu = 1;
  107.                                         }
  108.                                         if (!m.pkm) && (show_menu)
  109.                                         {
  110.                                                 show_menu = 0;
  111.                                                 SwitchToAnotherThread();
  112.                                                 CreateThread(#menu_rmb,#stak+4092);
  113.                                                 break;
  114.                                         }
  115.                                 }
  116.  
  117.                                 if (m.vert)
  118.                                 {
  119.                                         if (WB1.list.MouseScroll(m.vert)) WB1.ParseHTML(buf);
  120.                                 }
  121.                                
  122.                                 if (!m.lkm) scroll_used=0;
  123.                                 if (m.x>=scroll_wv.start_x) && (m.x<=scroll_wv.start_x+scroll_wv.size_x)
  124.                                 && (m.y>=scroll_wv.start_y+scroll_wv.btn_height) && (-scroll_wv.btn_height+scroll_wv.start_y+scroll_wv.size_y>m.y)
  125.                                 && (WB1.list.count>WB1.list.visible) && (m.lkm)
  126.                                 {
  127.                                         scroll_used=1;
  128.                                 }
  129.                                
  130.                                 if (scroll_used)
  131.                                 {
  132.                                         half_scroll_size = WB1.list.h - 16 * WB1.list.visible / WB1.list.count - 3 /2;
  133.                                         if (half_scroll_size+WB1.list.y>m.y) || (m.y<0) || (m.y>4000) m.y=half_scroll_size+WB1.list.y;
  134.                                         btn=WB1.list.first;
  135.                                         WB1.list.first = m.y -half_scroll_size -WB1.list.y * WB1.list.count / WB1.list.h;
  136.                                         if (WB1.list.visible+WB1.list.first>WB1.list.count) WB1.list.first=WB1.list.count-WB1.list.visible;
  137.                                         if (btn<>WB1.list.first) WB1.ParseHTML(buf);
  138.                                 }
  139.  
  140.                                 break;
  141.                         case evButton:
  142.                                 btn=GetButtonID();
  143.                                 if (btn==1)
  144.                                 {
  145.                                         KillProcess(downloader_id);
  146.                                         ExitProcess();
  147.                                 }
  148.                                 ELSE
  149.                                 {
  150.                                         Scan(btn);
  151.                                 }
  152.                                 break;
  153.                         case evKey:
  154.                                 key = GetKey();
  155.                                
  156.                                 if (address_box.flags & 0b10) SWITCH(key)
  157.                                         { CASE 52: CASE 53: CASE 54: goto _EDIT_MARK; }
  158.  
  159.                                 Scan(key);
  160.                                
  161.                                 _EDIT_MARK:
  162.                                 if (key<>0x0d) && (key<>183) && (key<>184) {EAX=key<<8; edit_box_key stdcall(#address_box);}
  163.                                 break;
  164.                         case evReDraw:
  165.                                 if (action_buf) { Scan(action_buf); action_buf=0;}
  166.                                 Draw_Window();
  167.                                 break;
  168.                         case evNetwork:
  169.                                 return;
  170.                                 //open page
  171.                                 http_get stdcall (#URL, 0);    
  172.                                 http_transfer = EAX;
  173.                                 IF (http_transfer<0) notify("Error from HTTP lib");
  174.                                 //
  175.                                 if (http_transfer != 0) {
  176.                                         http_process stdcall (http_transfer);
  177.                                         $push EAX
  178.                                         ESI = http_transfer;
  179.                                         if (!ESI.http_msg.content_received) break;
  180.                                         buf = ESI.http_msg.content_ptr;
  181.                                         debug(buf);    
  182.                                         filesize = ESI.http_msg.content_received;
  183.                                         debugi(filesize);
  184.                                         WB1.ParseHTML(buf);                                                    
  185.                                         $pop EAX       
  186.                                         if (EAX == 0) {                
  187.                                                 http_free stdcall (http_transfer);
  188.                                                 http_transfer=0;                               
  189.                                         }
  190.                                 }
  191.                         default:
  192.                                 if (downloader_id<>0)
  193.                                 {
  194.                                         if (GetProcessSlot(downloader_id)<>0) break;
  195.                                         downloader_id=0;
  196.                                         WB1.list.first = WB1.list.count = 0;
  197.                                         WB1.ReadHtml(_WIN);
  198.                                         Draw_Window();
  199.                                 }
  200.                 }
  201.         }
  202. }
  203.  
  204. void SetElementSizes()
  205. {
  206.         address_box.width = Form.width - 266;
  207.         WB1.list.SetSizes(0, 44, Form.width - 10 - scroll_wv.size_x, Form.cheight - 44, 0, 10);
  208.         WB1.list.column_max = WB1.list.w - 30 / 6;
  209.         WB1.list.visible = WB1.list.h - 3 / WB1.list.line_h - 2;
  210.         WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, WB1.list.h, WB1.list.line_h);
  211. }
  212.  
  213.  
  214. void Draw_Window()
  215. {
  216.         int j;
  217.         DefineAndDrawWindow(215,100,WIN_W,WIN_H,0x73,0xE4DFE1,0,0);
  218.  
  219.         GetProcessInfo(#Form, SelfInfo);
  220.         if (Form.status_window>2)
  221.         {
  222.                 DrawTitle(#header);
  223.                 return;
  224.         }
  225.         if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
  226.         if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
  227.        
  228.         PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
  229.         if (GetProcessSlot(downloader_id)<>0) _PutImage(88,10, 24,24, #stop_btn);
  230.        
  231.         DrawBar(200,0,Form.cwidth-200,43,0xE4DFE1);
  232.         DrawBar(0,42,Form.cwidth,1,0xE2DBDC);
  233.         DrawBar(0,43,Form.cwidth,1,0xD2CED0);
  234.         for (j=0; j<5; j++) DefineButton(j*37+11, 7, 29, 29, 300+j+BT_HIDE, 0xE4DFE1);
  235.         _PutImage(Form.cwidth-48,14, 40,19, #URLgoto);
  236.         DefineButton(Form.cwidth-28,15, 18, 16, GOTOURL+BT_HIDE, 0xE4DFE1);
  237.         DefineButton(Form.cwidth-47,15, 17, 16, SEARCHWEB+BT_HIDE, 0xE4DFE1);
  238.         DrawRectangle(205,14,Form.cwidth-205-49,18,0x94AECE); //around adress bar
  239.         DrawRectangle(206,15,Form.cwidth-205-50,16,0xE4ECF3);
  240.  
  241.         SetElementSizes();
  242.         ShowPage();
  243.  
  244.         DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+1, 16, 16, BTN_UP+BT_HIDE, 0xE4DFE1);
  245.         DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+scroll_wv.size_y-18, 16, 16, BTN_DOWN+BT_HIDE, 0xE4DFE1);
  246. }
  247.  
  248.  
  249. void Scan(int id)
  250. {
  251.         if (id >= 400) ProcessLinks(id);
  252.        
  253.         switch (id)
  254.         {
  255.                 case 011: //Ctrk+K
  256.                         WB1.ReadHtml(_KOI);
  257.                         WB1.ParseHTML(buf);
  258.                         return;
  259.  
  260.                 case 021: //Ctrl+U
  261.                         WB1.ReadHtml(_UTF);
  262.                         WB1.ParseHTML(buf);
  263.                         return;
  264.  
  265.                 case 004: //Ctrl+D
  266.                         WB1.ReadHtml(_DOS);
  267.                         WB1.ParseHTML(buf);
  268.                         return;
  269.  
  270.                 case 005: //Win encoding
  271.                         WB1.ReadHtml(_WIN);
  272.                         WB1.ParseHTML(buf);
  273.                         return;
  274.  
  275.                 case 009: //free img cache
  276.                         ImgCache.Free();
  277.                         notify(IMAGES_CACHE_CLEARED);
  278.                         WB1.ParseHTML(buf);
  279.                         return;
  280.  
  281.                 case BACK:
  282.                         if (!BrowserHistory.GoBack()) return;
  283.                         OpenPage();
  284.                         return;
  285.                 case FORWARD:
  286.                         if (!BrowserHistory.GoForward()) return;
  287.                         OpenPage();
  288.                         return;
  289.                 case 052:  //F3
  290.                         if (strncmp(#URL,"http:",5)<>0) RunProgram("/rd/1/tinypad", #URL);
  291.                         else RunProgram("/rd/1/tinypad", #download_path);
  292.                         return;
  293.                 case 054: //F5
  294.                         IF(address_box.flags & 0b10) WB1.ParseHTML(buf);
  295.                         return;
  296.  
  297.                 case REFRESH:
  298.                         if (GetProcessSlot(downloader_id)<>0)
  299.                         {
  300.                                 KillProcess(downloader_id);
  301.                                 pause(20);
  302.                                 Draw_Window();
  303.                                 return;
  304.                         }
  305.                         anchor_line_num=WB1.list.first;
  306.                         anchor[0]='|';
  307.                         OpenPage();
  308.                         return;
  309.                 case 014:
  310.                 case 020:
  311.                 case NEWTAB:
  312.                         MoveSize(190,80,OLD,OLD);
  313.                         RunProgram(#program_path, #URL);
  314.                         return;
  315.                        
  316.                 case HOME:
  317.                         strcpy(#editURL, "http://kolibrios.org/en/index.htm");
  318.                 case GOTOURL:
  319.                 case 0x0D: //enter
  320.                         if ((strstr(#editURL,"ttp://")==0) && (editURL[0]!='/')) strcpy(#URL,"http://"); else URL[0] = 0;
  321.                         strcat(#URL, #editURL);
  322.                         OpenPage();
  323.                         return;
  324.                 case SEARCHWEB:
  325.                         strcpy(#URL, #search_path);
  326.                         strcat(#URL, #editURL);
  327.                         OpenPage();
  328.                         return;
  329.  
  330.                 case 183: //PgDown
  331.                         if (WB1.list.count < WB1.list.visible) return;
  332.                         IF(WB1.list.first == WB1.list.count - WB1.list.visible) return;
  333.                         WB1.list.first += WB1.list.visible + 2;
  334.                         IF(WB1.list.visible + WB1.list.first > WB1.list.count) WB1.list.first = WB1.list.count - WB1.list.visible;
  335.                         WB1.ParseHTML(buf);
  336.                         return;
  337.  
  338.                 case 184: //PgUp
  339.                         if (WB1.list.count < WB1.list.visible) return;
  340.                         IF(WB1.list.first == 0) return;
  341.                         WB1.list.first -= WB1.list.visible - 2;
  342.                         IF(WB1.list.first < 0) WB1.list.first = 0;
  343.                         WB1.ParseHTML(buf);
  344.                         return;
  345.  
  346.                 case 178:
  347.                 case BTN_UP: //ìîòàåì ââåðõ
  348.                         if (WB1.list.first <= 0) return;
  349.                         WB1.list.first--;
  350.                         WB1.ParseHTML(buf);
  351.                         return;
  352.  
  353.                 case 177:
  354.                 case BTN_DOWN: //ìîòàåì âíèç
  355.                         if (WB1.list.visible + WB1.list.first >= WB1.list.count) return;
  356.                         WB1.list.first++;
  357.                         WB1.ParseHTML(buf);
  358.                         return;
  359.  
  360.                 case 180: //home
  361.                         if (WB1.list.KeyHome()) WB1.ParseHTML(buf);
  362.                         return;
  363.  
  364.                 case 181: //end
  365.                         if (WB1.list.count < WB1.list.visible) return;
  366.                         if (WB1.list.KeyEnd()) WB1.ParseHTML(buf);
  367.                         return;
  368.         }
  369. }
  370.  
  371.  
  372.  
  373. void ProcessLinks(int id)
  374. {
  375.         strcpy(#URL, PageLinks.GetURL(id-401));
  376.        
  377.         //$1 - Condition Script
  378.         if (URL[0] == '$')
  379.         {
  380.                 if (URL[1]=='-') && (condition_href) condition_href--;
  381.                 if (URL[1]=='+')
  382.                 {
  383.                         if (condition_href<condition_max) condition_href++; else notify(T_LAST_SLIDE);
  384.                 }
  385.                 if (URL[1]!='-') && (URL[1]!='+') condition_href = atoi(#URL+1);
  386.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  387.                 ShowPage();
  388.                 return;
  389.         }
  390.         //#1
  391.         if (URL[0] == '#')
  392.         {
  393.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));              
  394.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  395.                 WB1.list.first=WB1.list.count-WB1.list.visible;
  396.                 ShowPage();
  397.                 return;
  398.         }
  399.         //liner.ru#1
  400.         if (strrchr(#URL, '#')!=-1)
  401.         {
  402.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));
  403.                 URL[strrchr(#URL, '#')-1] = 0x00;
  404.         }
  405.        
  406.         WB1.GetNewUrl();
  407.        
  408.         if (!strcmp(#URL + strlen(#URL) - 4, ".gif")) || (!strcmp(#URL + strlen(#URL) - 4, ".png")) || (!strcmp(#URL + strlen(#URL) - 4, ".jpg"))
  409.         {
  410.                 //if (strstr(#URL,"http:"))
  411.                 RunProgram("/sys/media/kiv", #URL);
  412.                 strcpy(#editURL, BrowserHistory.CurrentUrl());
  413.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  414.                 return;
  415.         }
  416.         if (!strcmpn(#URL,"mailto:", 7))
  417.         {
  418.                 notify(#URL);
  419.                 strcpy(#editURL, BrowserHistory.CurrentUrl());
  420.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  421.                 return;
  422.         }
  423.  
  424.         OpenPage();
  425.         return;
  426. }
  427.  
  428. void OpenPage()
  429. {
  430.         if (GetProcessSlot(downloader_id)<>0) PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
  431.         KillProcess(downloader_id);
  432.         strcpy(#editURL, #URL);
  433.         BrowserHistory.AddUrl();
  434.         strcpy(#header, #version);
  435.         pre_text =0;
  436.         if (!strncmp(#URL,"http:",5))
  437.         {
  438.                 KillProcess(downloader_id);
  439.                 DeleteFile(#download_path);
  440.                 IF (URL[strlen(#URL)-1]=='/') URL[strlen(#URL)-1]=NULL;
  441.                 downloader_id = RunProgram("/sys/network/downloader", #URL);
  442.                 IF (downloader_id<0) notify("Error running Downloader. Internet unavilable.");
  443.                 Draw_Window();
  444.                 return;
  445.         }
  446.         WB1.list.first = WB1.list.count =0;
  447.         WB1.ReadHtml(_WIN);
  448.         ShowPage();
  449. }
  450.  
  451. void ShowPage()
  452. {
  453.         address_box.size = address_box.pos = strlen(#editURL);
  454.         address_box.offset=0;
  455.         edit_box_draw stdcall(#address_box);
  456.  
  457.         if (!filesize)
  458.         {
  459.                 PageLinks.Clear();
  460.                 if (GetProcessSlot(downloader_id)<>0)
  461.                 {
  462.                                 filesize = sizeof(loading);
  463.                                 WB1.ParseHTML(#loading);
  464.                 }
  465.                 else
  466.                 {
  467.                         if (strncmp(#URL,"http:",5)==0)
  468.                         {
  469.                                 filesize = sizeof(page_not_found_no_internet);
  470.                                 WB1.ParseHTML(#page_not_found_no_internet);
  471.                         }
  472.                         else
  473.                         {
  474.                                 filesize = sizeof(page_not_found);
  475.                                 WB1.ParseHTML(#page_not_found);
  476.                         }
  477.                 }
  478.                 //return;
  479.         }
  480.         else
  481.                 WB1.ParseHTML(buf);
  482.  
  483.         if (!header) strcpy(#header, #version);
  484.         if (!strcmp(#version, #header)) DrawTitle(#header);
  485. }
  486.  
  487.  
  488. stop:
  489.