Subversion Repositories Kolibri OS

Rev

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