Subversion Repositories Kolibri OS

Rev

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