Subversion Repositories Kolibri OS

Rev

Rev 4650 | 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.75";
  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";
  39. #else
  40.         char version[]=" Text-based Browser 0.99.75";
  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";       
  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.                                         if (EAX == 0) {
  171.                                                 ESI = http_transfer;
  172.                                                 // Handle redirects
  173.                                                 if (ESI.http_msg.status >= 300) && (ESI.http_msg.status < 400)
  174.                                                 {
  175.                                                         redirected++;
  176.                                                         if (redirected<=5)
  177.                                                         {
  178.                                                                 http_find_header_field stdcall (http_transfer, #str_location);
  179.                                                                 if (EAX!=0) {
  180.                                                                         ESI = EAX;
  181.                                                                         EDI = #URL;
  182.                                                                         do {
  183.                                                                                 $lodsb;
  184.                                                                                 $stosb;
  185.                                                                         } while (AL != 0) && (AL != 13) && (AL != 10));
  186.                                                                         DSBYTE[EDI-1]='\0';
  187.                                                                 }
  188.                                                         }
  189.                                                         else
  190.                                                         {
  191.                                                         //TODO: display error (too many redirects)
  192.                                                         }
  193.                                                 }
  194.                                                 else
  195.                                                 {
  196.                                                         redirected = 0;
  197.                                                 }
  198.                                                 // Loading the page is complete, free resources
  199.                                                 if (redirected>0)
  200.                                                 {
  201.                                                         http_free stdcall (http_transfer);
  202.                                                         http_transfer=0;
  203.                                                         WB1.GetNewUrl();
  204.                                                         strcpy(#editURL, #URL);
  205.                                                         BrowserHistory.current--;
  206.                                                         OpenPage();
  207.                                                 }
  208.                                                 else
  209.                                                 {
  210.                                                         ESI = http_transfer;
  211.                                                         bufpointer = ESI.http_msg.content_ptr;
  212.                                                         bufsize = ESI.http_msg.content_received;
  213.                                                         http_free stdcall (http_transfer);
  214.                                                         http_transfer=0;
  215.                                                         SetPageDefaults();
  216.                                                         Draw_Window();          // stop button => refresh button
  217.                                                 }
  218.                                         }
  219.                                 }
  220.                 }
  221.         }
  222. }
  223.  
  224. void SetElementSizes()
  225. {
  226.         address_box.width = Form.width - 266;
  227.         WB1.list.SetSizes(0, 44, Form.width - 10 - scroll_wv.size_x, Form.cheight - 44, 0, 10);
  228.         WB1.list.column_max = WB1.list.w - scroll_wv.size_x / 6;
  229.         WB1.list.visible = WB1.list.h - 3 / WB1.list.line_h - 2;
  230.         WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, WB1.list.h, WB1.list.line_h);
  231. }
  232.  
  233.  
  234. void Draw_Window()
  235. {
  236.         int j;
  237.         DefineAndDrawWindow(215,100,WIN_W,WIN_H,0x73,0xE4DFE1,0,0);
  238.  
  239.         GetProcessInfo(#Form, SelfInfo);
  240.         if (Form.status_window>2)
  241.         {
  242.                 DrawTitle(#header);
  243.                 return;
  244.         }
  245.         if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
  246.         if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
  247.        
  248.         PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
  249.         if (http_transfer > 0) _PutImage(88,10, 24,24, #stop_btn);
  250.        
  251.         DrawBar(200,0,Form.cwidth-200,43,0xE4DFE1);
  252.         DrawBar(0,42,Form.cwidth,1,0xE2DBDC);
  253.         DrawBar(0,43,Form.cwidth,1,0xD2CED0);
  254.         for (j=0; j<5; j++) DefineButton(j*37+11, 7, 29, 29, 300+j+BT_HIDE, 0xE4DFE1);
  255.         _PutImage(Form.cwidth-48,14, 40,19, #URLgoto);
  256.         DefineButton(Form.cwidth-28,15, 18, 16, GOTOURL+BT_HIDE, 0xE4DFE1);
  257.         DefineButton(Form.cwidth-47,15, 17, 16, SEARCHWEB+BT_HIDE, 0xE4DFE1);
  258.         DrawRectangle(205,14,Form.cwidth-205-49,18,0x94AECE); //around adress bar
  259.         DrawRectangle(206,15,Form.cwidth-205-50,16,0xE4ECF3);
  260.  
  261.         SetElementSizes();
  262.         ShowPage();
  263.  
  264.         DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+1, 16, 16, BTN_UP+BT_HIDE, 0xE4DFE1);
  265.         DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+scroll_wv.size_y-18, 16, 16, BTN_DOWN+BT_HIDE, 0xE4DFE1);
  266. }
  267.  
  268.  
  269. void ChangeCharset(byte new_charset)
  270. {
  271.         BufEncode(new_charset);
  272.         WB1.Parse();   
  273. }
  274.  
  275. void Scan(int id)
  276. {
  277.         action_buf=0;
  278.         if (id >= 400)
  279.         {
  280.                 ProcessLinks(id);
  281.                 return;
  282.         }
  283.         switch (id)
  284.         {
  285.                 case 011: //Ctrk+K
  286.                         ChangeCharset(_KOI);
  287.                         return;
  288.  
  289.                 case 021: //Ctrl+U
  290.                         ChangeCharset(_UTF);
  291.                         return;
  292.  
  293.                 case 004: //Ctrl+D
  294.                         ChangeCharset(_DOS);
  295.                         return;
  296.  
  297.                 case 005: //Win encoding
  298.                         ChangeCharset(_WIN);
  299.                         return;
  300.  
  301.                 case 009: //free img cache
  302.                         ImgCache.Free();
  303.                         notify(IMAGES_CACHE_CLEARED);
  304.                         WB1.Parse();
  305.                         return;
  306.  
  307.                 case 003: //history
  308.                         strcpy(#URL, URL_HISTORY);
  309.                         OpenPage();
  310.                         return;
  311.  
  312.                 case BACK:
  313.                         if (!BrowserHistory.GoBack()) return;
  314.                         OpenPage();
  315.                         return;
  316.                 case FORWARD:
  317.                         if (!BrowserHistory.GoForward()) return;
  318.                         OpenPage();
  319.                         return;
  320.                 case 052:  //F3
  321.                         if (strncmp(#URL,"http:",5)==0)
  322.                         {
  323.                                 WriteFile(bufsize, bufpointer, "/tmp0/1/webview.tmp");
  324.                                 if (EAX==0) RunProgram("/rd/1/tinypad", "/tmp0/1/webview.tmp");
  325.                         }
  326.                         else
  327.                         {
  328.                                 RunProgram("/rd/1/tinypad", #URL);
  329.                         }
  330.                         return;
  331.                 case 054: //F5
  332.                         IF(address_box.flags & 0b10) WB1.Parse();
  333.                         return;
  334.  
  335.                 case REFRESH:
  336.                         if (http_transfer > 0)
  337.                         {
  338.                                 StopLoading();
  339.                                 Draw_Window();
  340.                         }
  341.                         else OpenPage();
  342.                         return;
  343.                 case 014:
  344.                 case 020:
  345.                 case NEWTAB:
  346.                         MoveSize(190,80,OLD,OLD);
  347.                         RunProgram(#program_path, #URL);
  348.                         return;
  349.                        
  350.                 case HOME:
  351.                         strcpy(#editURL, "http://kolibrios.org/");
  352.                 case GOTOURL:
  353.                 case 0x0D: //enter
  354.                         if ((strstr(#editURL,"ttp://")==0) && (editURL[0]!='/')) strcpy(#URL,"http://"); else URL[0] = 0;
  355.                         strcat(#URL, #editURL);
  356.                         OpenPage();
  357.                         return;
  358.                 case SEARCHWEB:
  359.                         strcpy(#URL, #search_path);
  360.                         strcat(#URL, #editURL);
  361.                         OpenPage();
  362.                         return;
  363.  
  364.                 case 183: //PgDown
  365.                         if (WB1.list.count < WB1.list.visible) return;
  366.                         IF(WB1.list.first == WB1.list.count - WB1.list.visible) return;
  367.                         WB1.list.first += WB1.list.visible + 2;
  368.                         IF(WB1.list.visible + WB1.list.first > WB1.list.count) WB1.list.first = WB1.list.count - WB1.list.visible;
  369.                         WB1.Parse();
  370.                         return;
  371.  
  372.                 case 184: //PgUp
  373.                         if (WB1.list.count < WB1.list.visible) return;
  374.                         IF(WB1.list.first == 0) return;
  375.                         WB1.list.first -= WB1.list.visible - 2;
  376.                         IF(WB1.list.first < 0) WB1.list.first = 0;
  377.                         WB1.Parse();
  378.                         return;
  379.  
  380.                 case 178:
  381.                 case BTN_UP:
  382.                         if (WB1.list.first <= 0) return;
  383.                         WB1.list.first--;
  384.                         WB1.Parse();
  385.                         return;
  386.  
  387.                 case 177:
  388.                 case BTN_DOWN:
  389.                         if (WB1.list.visible + WB1.list.first >= WB1.list.count) return;
  390.                         WB1.list.first++;
  391.                         WB1.Parse();
  392.                         return;
  393.  
  394.                 case 180: //home
  395.                         if (WB1.list.KeyHome()) WB1.Parse();
  396.                         return;
  397.  
  398.                 case 181: //end
  399.                         if (WB1.list.count < WB1.list.visible) return;
  400.                         if (WB1.list.KeyEnd()) WB1.Parse();
  401.                         return;
  402.         }
  403. }
  404.  
  405.  
  406.  
  407. void ProcessLinks(int id)
  408. {
  409.         strcpy(#URL, PageLinks.GetURL(id-401));
  410.         //$1 - Condition Script
  411.         if (URL[0] == '$')
  412.         {
  413.                 if (URL[1]=='-') && (condition_href) condition_href--;
  414.                 if (URL[1]=='+')
  415.                 {
  416.                         if (condition_href<condition_max) condition_href++; else notify(T_LAST_SLIDE);
  417.                 }
  418.                 if (URL[1]!='-') && (URL[1]!='+') condition_href = atoi(#URL+1);
  419.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  420.                 ShowPage();
  421.                 return;
  422.         }
  423.         //#1
  424.         if (URL[0] == '#')
  425.         {
  426.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));              
  427.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  428.                 WB1.list.first=WB1.list.count-WB1.list.visible;
  429.                 ShowPage();
  430.                 return;
  431.         }
  432.         //liner.ru#1
  433.         if (strrchr(#URL, '#')!=-1)
  434.         {
  435.                 strcpy(#anchor, #URL+strrchr(#URL, '#'));
  436.                 URL[strrchr(#URL, '#')-1] = 0x00;
  437.         }
  438.        
  439.         WB1.GetNewUrl();
  440.        
  441.         if (!strcmp(#URL + strlen(#URL) - 4, ".gif")) || (!strcmp(#URL + strlen(#URL) - 4, ".png")) || (!strcmp(#URL + strlen(#URL) - 4, ".jpg"))
  442.         {
  443.                 //if (strstr(#URL,"http:"))
  444.                 RunProgram("/sys/media/kiv", #URL);
  445.                 strcpy(#editURL, BrowserHistory.CurrentUrl());
  446.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  447.                 return;
  448.         }
  449.         if (!strcmpn(#URL,"mailto:", 7))
  450.         {
  451.                 notify(#URL);
  452.                 strcpy(#editURL, BrowserHistory.CurrentUrl());
  453.                 strcpy(#URL, BrowserHistory.CurrentUrl());
  454.                 return;
  455.         }
  456.         OpenPage();
  457.         return;
  458. }
  459.  
  460. void StopLoading()
  461. {
  462.         if (http_transfer<>0)
  463.         {
  464.                 EAX = http_transfer;
  465.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  466.                 $push   EAX                                                     // save it on the stack
  467.                 http_free stdcall (http_transfer);      // abort connection
  468.                 $pop    EAX                                                    
  469.                 mem_Free(EAX);                                          // free data
  470.                 http_transfer=0;
  471.                 bufsize = 0;
  472.                 bufpointer = mem_Free(bufpointer);
  473.         }
  474.         PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
  475. }
  476.  
  477. void SetPageDefaults()
  478. {
  479.         strcpy(#header, #version);
  480.         pre_text = 0;
  481.         WB1.list.count = WB1.list.first = 0;
  482.         stroka = 0;
  483.         cur_encoding = _DEFAULT;
  484.         if (o_bufpointer) o_bufpointer = free(o_bufpointer);
  485.         anchor_line_num=WB1.list.first;
  486.         anchor[0]='|';
  487. }
  488.  
  489. void OpenPage()
  490. {
  491.         StopLoading();
  492.         strcpy(#editURL, #URL);
  493.         BrowserHistory.AddUrl();
  494.         if (strncmp(#URL,"http:",5)==0)
  495.         {
  496.                 _PutImage(88,10, 24,24, #stop_btn);
  497.                 http_get stdcall (#URL, #accept_language);
  498.                 http_transfer = EAX;
  499.                 if (http_transfer == 0)
  500.                 {
  501.                         StopLoading();
  502.                         bufsize = 0;
  503.                         bufpointer = mem_Free(bufpointer);
  504.                         ShowPage();
  505.                         return;
  506.                 }
  507.         }
  508.         else
  509.         {
  510.                 file_size stdcall (#URL);
  511.                 bufsize = EBX;
  512.                 if (bufsize)
  513.                 {
  514.                         bufpointer = mem_Free(bufpointer);
  515.                         bufpointer = mem_Alloc(bufsize);
  516.                         SetPageDefaults();
  517.                         ReadFile(0, bufsize, bufpointer, #URL);                        
  518.                 }
  519.                 ShowPage();
  520.         }
  521. }
  522.  
  523. void ShowPage()
  524. {
  525.         address_box.size = address_box.pos = strlen(#editURL);
  526.         address_box.offset=0;
  527.         edit_box_draw stdcall(#address_box);
  528.  
  529.         if (strcmp(#URL, URL_HISTORY)==0) ShowHistory(); else
  530.         if (!bufsize)
  531.         {
  532.                 PageLinks.Clear();
  533.                 if (http_transfer<>0)
  534.                 {
  535.                         WB1.Prepare(#loading, sizeof(loading));
  536.                 }
  537.                 else
  538.                         WB1.Prepare(#page_not_found, sizeof(page_not_found));
  539.         }
  540.         else
  541.                 WB1.Parse();
  542.  
  543.         if (!header) strcpy(#header, #version);
  544.         if (!strcmp(#version, #header)) DrawTitle(#header);
  545. }
  546.  
  547.  
  548.  
  549.  
  550. stop:
  551.