Subversion Repositories Kolibri OS

Rev

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