Subversion Repositories Kolibri OS

Rev

Rev 7447 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #define MEMSIZE 4096*100
  2.  
  3. #include "../lib/gui.h"
  4. #include "../lib/kfont.h"
  5. #include "../lib/io.h"
  6. #include "../lib/cursor.h"
  7.  
  8. #include "../lib/obj/box_lib.h"
  9. #include "../lib/obj/libini.h"
  10. #include "../lib/obj/iconv.h"
  11. #include "../lib/obj/libimg.h"
  12. #include "../lib/obj/proc_lib.h"
  13. #include "../lib/obj/http.h"
  14.  
  15. #include "../lib/patterns/simple_open_dialog.h"
  16. #include "../lib/patterns/history.h"
  17. #include "../lib/patterns/http_downloader.h"
  18. #include "../browser/download_manager.h"
  19.  
  20. llist list;
  21.  
  22. #include "link.h"
  23. #include "canvas.h"
  24. #include "favicon.h"
  25.  
  26. char default_dir[] = "/rd/1";
  27. od_filter filter2 = { 16, "TXT\0HTM\0HTML\0\0" };
  28.  
  29. char accept_language[]= "Accept-Language: ru\n";
  30.  
  31. #define TOOLBAR_H 36
  32. #define TOOLBAR_ICON_WIDTH  26
  33. #define TOOLBAR_ICON_HEIGHT 24
  34. #define STATUSBAR_H 15
  35.  
  36. #define DEFAULT_EDITOR "/sys/tinypad"
  37. #define DEFAULT_PREVIEW_PATH "/tmp0/1/aelia_preview.txt"
  38.  
  39. //ATTENTION: each page must have '\0' character at the end of the file
  40. char buidin_page_home[] = FROM "buidin_pages\\home.htm";
  41. char buidin_page_about[] = FROM "buidin_pages\\about.htm";
  42. char buidin_page_not_found[] = FROM "buidin_pages\\not_found.htm";
  43.  
  44. #define UML 4096*2
  45.  
  46. scroll_bar scroll = { 15,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  47.  
  48. proc_info Form;
  49. char title[4196];
  50.  
  51. enum {
  52.         OPEN_FILE,
  53.         MAGNIFY_MINUS,
  54.         MAGNIFY_PLUS,
  55.         CHANGE_ENCODING,
  56.         RUN_EDIT,
  57.         GO_BACK,
  58.         GO_FORWARD,
  59.         SANDWICH
  60. };
  61.  
  62. char address[UML];
  63. edit_box address_box = {250,56,34,0xffffff,0x94AECE,0xffffff,0xffffff,0,UML,#address,NULL,2,19,19};
  64.  
  65. bool debug_mode=false;
  66.  
  67. #include "ini.h"
  68. #include "gui.h"
  69. #include "prepare_page.h"
  70. //#include "special.h"
  71.  
  72. #define SANDWICH_MENU "Refresh page\nEdit page\nHistory\nDownloader\nAbout"
  73.  
  74. void InitDlls()
  75. {
  76.         load_dll(boxlib,    #box_lib_init,   0);
  77.         load_dll(libHTTP,   #http_lib_init,  1);
  78.         load_dll(libio,     #libio_init,     1);
  79.         load_dll(libimg,    #libimg_init,    1);
  80.         //load_dll(libini,    #lib_init,       1);
  81.         load_dll(iconv_lib, #iconv_open,     0);
  82.         load_dll(Proc_lib,  #OpenDialog_init,0);
  83. }
  84.  
  85.  
  86. void main()
  87. {  
  88.         InitDlls();    
  89.         OpenDialog_init stdcall (#o_dialog);
  90.         LoadIniSettings();
  91.         kfont.init(DEFAULT_FONT);
  92.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  93.         list.no_selection = true;
  94.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  95.         loop()
  96.         {
  97.                 switch(WaitEvent())
  98.                 {
  99.                         case evMouse:
  100.                                 HandleMouseEvent();
  101.                                 break;
  102.                         case evKey:
  103.                                 HandleKeyEvent();
  104.                                 break;
  105.                         case evButton:
  106.                                 HandleButtonEvent();
  107.                                 break;
  108.                         case evNetwork:
  109.                                 HandleNetworkEvent();
  110.                                 break;
  111.                         case evReDraw:
  112.                                 draw_window();
  113.                                 if (menu.cur_y>=10) && (menu.cur_y<20) {
  114.                                         encoding = menu.cur_y - 10;
  115.                                         EventPageRefresh();
  116.                                         menu.cur_y = 0;
  117.                                 }
  118.                                 if (menu.cur_y>=20) {
  119.                                         menu.cur_y-=20;
  120.                                         if (menu.cur_y==0) EventPageRefresh();
  121.                                         if (menu.cur_y==1) EventRunEdit();
  122.                                         if (menu.cur_y==2) EventShowHistory();
  123.                                         if (menu.cur_y==3) EventShowDownloader();
  124.                                         if (menu.cur_y==4) EventShowInfo();
  125.                                         menu.cur_y = 0;
  126.                                 }
  127.                 }
  128.         }
  129. }
  130.  
  131.  
  132. void HandleButtonEvent()
  133. {
  134.         byte btn = GetButtonID();
  135.         if (btn==1) {
  136.                 SaveIniSettings();
  137.                 ExitProcess();
  138.         }
  139.         switch(btn-10)
  140.         {
  141.                 case GO_BACK:
  142.                         EventGoBack();
  143.                         break;
  144.                 case GO_FORWARD:
  145.                         EventGoForward();
  146.                         break;
  147.                 case OPEN_FILE:
  148.                         EventOpenDialog();
  149.                         break;
  150.                 case MAGNIFY_PLUS:
  151.                         EventMagnifyPlus();
  152.                         break;
  153.                 case MAGNIFY_MINUS:
  154.                         EventMagnifyMinus();
  155.                         break;
  156.                 case CHANGE_ENCODING:
  157.                         EventChangeEncoding();
  158.                         break;
  159.                 case RUN_EDIT:
  160.                         EventRunEdit();
  161.                         break;
  162.                 case SANDWICH:
  163.                         EventShowSandwichMenu();
  164.                         break;
  165.         }
  166. }
  167.  
  168.  
  169. void HandleKeyEvent()
  170. {
  171.         GetKeys();
  172.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  173.                 switch (key_scancode)
  174.                 {
  175.                         case SCAN_CODE_UP:
  176.                                 EventMagnifyPlus();
  177.                                 return;
  178.                         case SCAN_CODE_DOWN:
  179.                                 EventMagnifyMinus();
  180.                                 return;
  181.                         case SCAN_CODE_KEY_O:
  182.                                 EventOpenDialog();
  183.                                 return;
  184.                         case SCAN_CODE_KEY_E:
  185.                                 EventRunEdit();
  186.                                 return;
  187.                         case SCAN_CODE_KEY_H:
  188.                                 EventShowHistory();
  189.                                 return;
  190.                         case SCAN_CODE_TAB:
  191.                                 EventChangeEncoding();
  192.                                 return;
  193.                 }
  194.         }
  195.         switch (key_scancode)
  196.         {
  197.                 case SCAN_CODE_F1:
  198.                         EventShowInfo();
  199.                         return;
  200.                 case SCAN_CODE_F12:
  201.                         EventChangeDebugMode();
  202.                         return;
  203.                 case SCAN_CODE_ENTER:
  204.                         EventOpenAddress(#address);
  205.                         return;
  206.                 case SCAN_CODE_BS:
  207.                         if (! address_box.flags & 0b10) {
  208.                                 EventGoBack();
  209.                                 return;
  210.                         }
  211.         }
  212.         if (list.ProcessKey(key_scancode)) && (! address_box.flags & 0b10) {
  213.                 DrawPage();
  214.                 return;
  215.         }
  216.         if (key_ascii != ASCII_KEY_ENTER)
  217.         && (key_ascii != ASCII_KEY_PGDN)
  218.         && (key_ascii != ASCII_KEY_PGUP) {
  219.                 EAX = key_editbox;
  220.                 edit_box_key stdcall(#address_box);
  221.         }
  222. }
  223.  
  224.  
  225. void HandleMouseEvent()
  226. {
  227.         edit_box_mouse stdcall (#address_box);
  228.         mouse.get();
  229.         list.wheel_size = 7;
  230.  
  231.         if (link.hover(mouse.x, mouse.y)) {
  232.                 if (-1 == link.active) {
  233.                         DrawStatusBar( " " ); //just clean status bar  
  234.                 }
  235.                 else {
  236.                         DrawStatusBar( link.get_active_url() );
  237.                 }
  238.         }
  239.  
  240.         if (mouse.key&MOUSE_LEFT) && (mouse.up) {
  241.                 if (-1 != link.active) EventOpenAddress( link.get_active_url() );
  242.         }
  243.  
  244.         if (list.MouseScroll(mouse.vert)) {
  245.                 DrawPage();
  246.                 return;
  247.         }
  248.  
  249.         scrollbar_v_mouse (#scroll);
  250.         if (list.first != scroll.position) {
  251.                 list.first = scroll.position;
  252.                 DrawPage();
  253.         }
  254. }
  255.  
  256. void HandleNetworkEvent()
  257. {
  258.         char favicon_address[UML];
  259.  
  260.         if (downloader.state == STATE_IN_PROGRESS) {
  261.                 downloader.MonitorProgress();
  262.  
  263.                 if (downloader.httpd.content_length>0)
  264.                         DrawProgress(STEP_2_COUNT_PAGE_HEIGHT-STEP_1_DOWNLOAD_PAGE*
  265.                                 downloader.httpd.content_received/downloader.httpd.content_length);
  266.                 else
  267.                         DrawProgress(STEP_2_COUNT_PAGE_HEIGHT-STEP_1_DOWNLOAD_PAGE/2);         
  268.         }
  269.        
  270.         if (downloader.state == STATE_COMPLETED)
  271.         {
  272.                 if (!strncmp(downloader.url,"http://gate.aspero.pro/",22)) {
  273.                         strcpy(#address,downloader.url + 29);
  274.                 }
  275.                 else {
  276.                         strcpy(#address,downloader.url);
  277.                 }
  278.                 downloader.Stop();
  279.                 DrawAddressBox();
  280.                 io.buffer_data = downloader.bufpointer;
  281.                 /*
  282.                 get_absolute_url(#favicon_address, #address, "/favicon.ico");
  283.                 favicon.get(#favicon_address);
  284.                 */
  285.                 PostOpenPageActions();
  286.         }
  287. }
  288.  
  289.  
  290. /* ----------------------------------------------------- */
  291.  
  292. void EventOpenDialog()
  293. {
  294.         OpenDialog_start stdcall (#o_dialog);
  295.         if (o_dialog.status) EventOpenAddress(#openfile_path);
  296. }
  297.  
  298. void EventOpenAddress(dword _new_address)
  299. {
  300. char temp[UML];
  301. char getUrl[UML];
  302.         if (!ESBYTE[_new_address]) return;
  303.         debugln("====================================");
  304.         debug("address: ");
  305.         debugln(_new_address);
  306.         strlcpy(#address, _new_address, UML);
  307.         strlwr(#address);
  308.         DrawAddressBox();
  309.  
  310.         /*
  311.         There could be several possible types of addresses:
  312.         - build in page
  313.         - local file
  314.         - url
  315.         So we need to detect what incoming address is
  316.         and then halndle it in the propper way.
  317.         */
  318.  
  319.         io.buffer_data = 0;
  320.         favicon.get(NULL);
  321.  
  322.         // - build in page
  323.         if (!strncmp(#address,"aelia:",6)) {
  324.                 debugln("this is buildin page");
  325.                 if (!strcmp(#address,"aelia:home")) io.buffer_data = #buidin_page_home;
  326.                 if (!strcmp(#address,"aelia:about")) io.buffer_data = #buidin_page_about;
  327.                 if (!strcmp(#address,"aelia:history")) io.buffer_data = MakePageWithHistory();
  328.                 PostOpenPageActions();
  329.         }
  330.         // - local file
  331.         else if (check_is_the_adress_local(#address)==true) {
  332.                 debugln("this is local address");
  333.                 io.read(#address);
  334.                 PostOpenPageActions();
  335.         }
  336.         // - url
  337.         else {
  338.                 debugln("this is url");
  339.                 if (!strncmp(#address,"https://",8)) {
  340.                         sprintf(#getUrl, "http://gate.aspero.pro/?site=%s", #address);
  341.                 }
  342.                 else if (!strncmp(#address,"http://",7)) {
  343.                         strlcpy(#getUrl, #address, UML);
  344.                 }
  345.                 else {
  346.                         strcpy(#temp, "http://");
  347.                         strlcpy(#temp, #address, UML);
  348.                         strlcpy(#address, #temp, UML);
  349.                         DrawAddressBox();
  350.                         strlcpy(#getUrl, #address, UML);
  351.                 }
  352.                 downloader.Start(#getUrl);
  353.         }
  354. }
  355.  
  356. void PostOpenPageActions()
  357. {
  358.         if (!io.buffer_data) {
  359.                 debugln("page not found");
  360.                 io.buffer_data = #buidin_page_not_found;
  361.         }
  362.  
  363.         history.add(#address);
  364.         favicon.draw(address_box.left-18, address_box.top-1);
  365.  
  366.         /*
  367.         Great! So we have the page in our buffer.
  368.         We don't know is it a plain text or html.
  369.         So we need to parse it and draw.
  370.         */
  371.  
  372.         list.KeyHome();
  373.         PreparePage();
  374. }
  375.  
  376. void EventMagnifyPlus()
  377. {
  378.         kfont.size.pt++;
  379.         if(!kfont.changeSIZE())
  380.                 kfont.size.pt--;
  381.         else
  382.                 PreparePage();
  383. }
  384.  
  385. void EventMagnifyMinus()
  386. {
  387.         kfont.size.pt--;
  388.         if(!kfont.changeSIZE())
  389.                 kfont.size.pt++;
  390.         else
  391.                 PreparePage();
  392. }
  393.  
  394. void EventRunEdit()
  395. {
  396.         if (check_is_the_adress_local(history.current())==true) {
  397.                 io.run(DEFAULT_EDITOR, history.current());
  398.         }
  399.         else {
  400.                 //io.write(strlen(io.buffer_data), io.buffer_data, DEFAULT_PREVIEW_PATH); // <--- doesn't work, smth odd, need to check
  401.                 CreateFile(strlen(io.buffer_data), io.buffer_data, DEFAULT_PREVIEW_PATH);
  402.                 io.run(DEFAULT_EDITOR, DEFAULT_PREVIEW_PATH);
  403.         }
  404. }
  405.  
  406. void EventChangeEncoding()
  407. {
  408.         menu.selected = encoding + 1;
  409.         menu.show(Form.left+Form.cwidth-97,Form.top+TOOLBAR_H+skin_height-6, 130, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
  410. }
  411.  
  412. void EventShowInfo() {
  413.         EventOpenAddress("aelia:about");
  414. }
  415.  
  416. void EventShowHistory()
  417. {
  418.         EventOpenAddress("aelia:history");
  419. }
  420.  
  421. void EventGoBack()
  422. {
  423.         if (history.back()) EventOpenAddress(history.current());
  424. }
  425.  
  426. void EventGoForward()
  427. {
  428.         if (history.forward()) EventOpenAddress(history.current());
  429. }
  430.  
  431. void EventShowSandwichMenu()
  432. {
  433.         menu.selected = 0;
  434.         menu.show(Form.left+Form.cwidth-130,Form.top+TOOLBAR_H+skin_height-10, 130, SANDWICH_MENU, 20);
  435. }
  436.  
  437. void EventPageRefresh()
  438. {
  439.         EventOpenAddress(history.current());
  440. }
  441.  
  442. void EventShowDownloader()
  443. {
  444.         if (!downloader_opened) {
  445.                 downloader_edit = NULL;
  446.                 CreateThread(#Downloader,#downloader_stak+4092);
  447.         }
  448. }
  449.  
  450. void EventChangeDebugMode()
  451. {
  452.         debug_mode ^= 1;
  453.         if (debug_mode) notify("'Debug mode ON'-I");
  454.         else notify("'Debug mode OFF'-I");
  455.         return;
  456. }
  457.  
  458. /* ------------------------------------------- */
  459.  
  460.  
  461. void draw_window()
  462. {
  463.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  464.         GetProcessInfo(#Form, SelfInfo);
  465.         if (Form.status_window>2) return;
  466.  
  467.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  468.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  469.  
  470.         system.color.get();
  471.  
  472.         list.SetSizes(0, TOOLBAR_H, Form.cwidth-scroll.size_x-1,
  473.                 Form.cheight-TOOLBAR_H-STATUSBAR_H, kfont.size.pt+4);
  474.        
  475.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 2, 0xe1e1e1);
  476.         DrawBar(0, TOOLBAR_H - 2, Form.cwidth, 1, 0xcecece);
  477.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  478.        
  479.         DrawToolbarButton(GO_BACK,         8);
  480.         DrawToolbarButton(GO_FORWARD,      33);
  481.         DrawToolbarButton(OPEN_FILE,       68);
  482.         DrawToolbarButton(MAGNIFY_PLUS,    Form.cwidth - 125);
  483.         DrawToolbarButton(MAGNIFY_MINUS,   Form.cwidth - 100);
  484.         DrawToolbarButton(CHANGE_ENCODING, Form.cwidth - 64);
  485.         DrawToolbarButton(SANDWICH,        Form.cwidth - 31);
  486.  
  487.         DrawAddressBox();
  488.  
  489.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  490.                 (Form.cheight-TOOLBAR_H == list.h) &&
  491.                 (list.count)
  492.         )
  493.         {
  494.                 DrawPage();
  495.         }
  496.         else
  497.         {
  498.                 if (!kfont.raw) {                           //this code need to be run
  499.                         if (param) EventOpenAddress(#param);    //only once at browser sturtup
  500.                         else EventOpenAddress("aelia:home");
  501.                 }
  502.                 else PreparePage();
  503.         }
  504.  
  505.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  506.         DrawStatusBar(NULL);
  507. }
  508.  
  509. void DrawPage()
  510. {
  511.         list.CheckDoesValuesOkey();
  512.         if (list.count) {
  513.                 kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  514.         }
  515.         DrawScroller();
  516. }
  517.  
  518. void DrawAddressBox()
  519. {
  520.         address_box.left = 97+19;
  521.         address_box.top = 11;
  522.         address_box.width = Form.cwidth - address_box.left - 138;
  523.         DrawRectangle(address_box.left-4-19, address_box.top-5, address_box.width+6+19, 23, 0x8C8C8C);
  524.         DrawWideRectangle(address_box.left-3-19, address_box.top-3, address_box.width+5+19, 21, 4, address_box.color);
  525.         address_box.size = address_box.pos = address_box.shift = address_box.shift_old = strlen(#address);
  526.         address_box.offset = 0;
  527.         edit_box_draw stdcall(#address_box);
  528.         favicon.draw(address_box.left-18, address_box.top-1);
  529.         DrawBar(address_box.left-2, address_box.top+1, 2, 13, 0xFFFfff);
  530. }
  531.  
  532. PathShow_data status_text = {0, 17,250, 6, 250, 0, 0, 0x0, 0xFFFfff, 0, NULL, 0};
  533. void DrawStatusBar(dword _status_text)
  534. {
  535.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,STATUSBAR_H, system.color.work);
  536.         DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, 0x8C8C8C);
  537.  
  538.         if (_status_text) {
  539.                 status_text.start_x = 7;
  540.                 status_text.start_y = Form.cheight - STATUSBAR_H + 3;
  541.                 status_text.area_size_x = Form.cwidth - status_text.start_x -3;
  542.                 status_text.font_color = system.color.work_text;
  543.                 status_text.text_pointer = _status_text;
  544.                 PathShow_prepare stdcall(#status_text);
  545.                 PathShow_draw stdcall(#status_text);
  546.         }
  547. }
  548.