Subversion Repositories Kolibri OS

Rev

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