Subversion Repositories Kolibri OS

Rev

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