Subversion Repositories Kolibri OS

Rev

Rev 7291 | Blame | Last modification | View Log | Download | RSS feed

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