Subversion Repositories Kolibri OS

Rev

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