Subversion Repositories Kolibri OS

Rev

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