Subversion Repositories Kolibri OS

Rev

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