Subversion Repositories Kolibri OS

Rev

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