Subversion Repositories Kolibri OS

Rev

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