Subversion Repositories Kolibri OS

Rev

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