Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 4096*80
  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 = {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. int     mouse_address_box;
  60. edit_box address_box = {250,56,34,0xffffff,0x94AECE,0xffffff,0xffffff,0,UML,#address,#mouse_address_box,2,19,19};
  61.  
  62. CustomCursor CursorPointer;
  63. dword CursorFile = FROM "pointer.cur";
  64.  
  65. #include "favicon.h"
  66. #include "ini.h"
  67. #include "gui.h"
  68. #include "label.h"
  69. #include "link.h"
  70. #include "tag.h"
  71. #include "prepare_page.h"
  72. //#include "special_symbols.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.         CursorPointer.Load(#CursorFile);
  93.         kfont.init(DEFAULT_FONT);
  94.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  95.         LoadIniSettings();
  96.         list.no_selection = true;
  97.         SetEventMask(10000000000000000000000001100111b);
  98.         loop()
  99.         {
  100.                 switch(WaitEvent())
  101.                 {
  102.                         case evMouse:
  103.                                 HandleMouseEvent();
  104.                                 break;
  105.                         case evKey:
  106.                                 HandleKeyEvent();
  107.                                 break;
  108.                         case evButton:
  109.                                 HandleButtonEvent();
  110.                                 break;
  111.                         case evReDraw:
  112.                                 draw_window();
  113.                                 if (menu.list.cur_y>=10) && (menu.list.cur_y<20) {
  114.                                         encoding = menu.list.cur_y - 10;
  115.                                         EventPageRefresh();
  116.                                         menu.list.cur_y = 0;
  117.                                 }
  118.                                 if (menu.list.cur_y>=20) {
  119.                                         menu.list.cur_y-=20;
  120.                                         if (menu.list.cur_y==0) EventPageRefresh();
  121.                                         if (menu.list.cur_y==1) EventRunEdit();
  122.                                         if (menu.list.cur_y==2) EventShowHistory();
  123.                                         if (menu.list.cur_y==3) EventShowDownloader();
  124.                                         if (menu.list.cur_y==4) EventShowInfo();
  125.                                         menu.list.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.         switch (key_scancode)
  173.         {
  174.                 case 059:
  175.                         EventShowInfo();
  176.                         return;
  177.                 case SCAN_CODE_ENTER:
  178.                         EventOpenAddress(#address);
  179.                         return;
  180.                 case SCAN_CODE_BS:
  181.                         if (! address_box.flags & 0b10) {
  182.                                 EventGoBack();
  183.                                 return;
  184.                         }
  185.         }
  186.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  187.                 switch (key_scancode)
  188.                 {
  189.                         case 024: //O
  190.                                 EventOpenDialog();
  191.                                 break;
  192.                         case SCAN_CODE_UP:
  193.                                 EventMagnifyPlus();
  194.                                 break;
  195.                         case SCAN_CODE_DOWN:
  196.                                 EventMagnifyMinus();
  197.                                 break;
  198.                         case 018: //E
  199.                                 EventRunEdit();
  200.                                 break;
  201.                         case 035: //H
  202.                                 EventShowHistory();
  203.                                 break;
  204.                         case SCAN_CODE_TAB:
  205.                                 EventChangeEncoding();
  206.                                 break;
  207.                 }
  208.         }
  209.         if (list.ProcessKey(key_scancode)) {
  210.                 DrawPage();
  211.                 return;
  212.         }
  213.         if (key_ascii != 0x0d)
  214.         && (key_ascii != ASCII_KEY_PGDN)
  215.         && (key_ascii != ASCII_KEY_PGUP) {
  216.                 EAX = key_editbox;
  217.                 edit_box_key stdcall(#address_box);
  218.         }
  219. }
  220.  
  221.  
  222. void HandleMouseEvent()
  223. {
  224.         edit_box_mouse stdcall (#address_box);
  225.         mouse.get();
  226.         list.wheel_size = 7;
  227.         link.hover();
  228.         if (list.MouseScroll(mouse.vert)) {
  229.                 DrawPage();
  230.                 return;
  231.         }
  232.         scrollbar_v_mouse (#scroll);
  233.         if (list.first != scroll.position) {
  234.                 list.first = scroll.position;
  235.                 DrawPage();
  236.         }
  237. }
  238.  
  239.  
  240. /* ----------------------------------------------------- */
  241.  
  242. void EventOpenDialog()
  243. {
  244.         OpenDialog_start stdcall (#o_dialog);
  245.         if (o_dialog.status) EventOpenAddress(#openfile_path);
  246. }
  247.  
  248. void EventOpenAddress(dword _new_address)
  249. {
  250. char temp[UML];
  251. char favicon_address[UML];
  252.         if (!ESBYTE[_new_address]) return;
  253.         debugln("====================================");
  254.         debug("address: ");
  255.         debugln(_new_address);
  256.         strlcpy(#address, _new_address, UML);
  257.         strlwr(#address);
  258.         DrawAddressBox();
  259.  
  260.         /*
  261.         There could be several possible types of addresses:
  262.         - build in page
  263.         - local file
  264.         - url
  265.         So we need to detect what incoming address is
  266.         and then halndle it in the propper way.
  267.         */
  268.  
  269.         io.buffer_data = 0;
  270.         favicon.get(NULL);
  271.  
  272.         // - build in page
  273.         if (!strncmp(#address,"aelia:",6)) {
  274.                 debugln("this is buildin page");
  275.                 if (!strcmp(#address,"aelia:home")) io.buffer_data = #buidin_page_home;
  276.                 if (!strcmp(#address,"aelia:about")) io.buffer_data = #buidin_page_about;
  277.                 if (!strcmp(#address,"aelia:history")) io.buffer_data = MakePageWithHistory();
  278.         }
  279.         // - local file
  280.         else if (check_is_the_adress_local(#address)==true) {
  281.                 debugln("this is local address");
  282.                 io.read(#address);
  283.         }
  284.         // - url
  285.         else {
  286.                 debugln("this is url");
  287.                 if (strncmp(#address,"http://",7)!=0) {
  288.                         strcpy(#temp, "http://");
  289.                         strlcpy(#temp, #address, UML);
  290.                         strlcpy(#address, #temp, UML);
  291.                         DrawAddressBox();
  292.                 }
  293.                 if (!downloader.Start(#address)) {
  294.                         downloader.Stop();
  295.                 } else {
  296.                         while (downloader.state!=STATE_COMPLETED)
  297.                         {
  298.                                 downloader.MonitorProgress();
  299.                                 if (downloader.httpd.content_length>0)
  300.                                         DrawProgress(STEP_2_COUNT_PAGE_HEIGHT-STEP_1_DOWNLOAD_PAGE*
  301.                                                 downloader.httpd.content_received/downloader.httpd.content_length);
  302.                                 else
  303.                                         DrawProgress(STEP_2_COUNT_PAGE_HEIGHT-STEP_1_DOWNLOAD_PAGE/2);
  304.                         }
  305.                         strcpy(#address,downloader.url);
  306.                         DrawAddressBox();
  307.                         io.buffer_data = downloader.bufpointer;
  308.                         get_absolute_url(#favicon_address, #address, "/favicon.ico");
  309.                         favicon.get(#favicon_address);
  310.                 }
  311.         }
  312.  
  313.         if (!io.buffer_data) {
  314.                 debugln("page not found");
  315.                 io.buffer_data = #buidin_page_not_found;
  316.         }
  317.  
  318.         history.add(#address);
  319.         favicon.draw(address_box.left-18, address_box.top-1);
  320.  
  321.         /*
  322.         Great! So we have the page in our buffer.
  323.         We don't know is it a plain text or html.
  324.         So we need to parse it and draw.
  325.         */
  326.  
  327.         list.KeyHome();
  328.         list.ClearList();
  329.         PreparePage();
  330. }
  331.  
  332. void EventMagnifyPlus()
  333. {
  334.         kfont.size.pt++;
  335.         if(!kfont.changeSIZE())
  336.                 kfont.size.pt--;
  337.         else
  338.                 PreparePage();
  339. }
  340.  
  341. void EventMagnifyMinus()
  342. {
  343.         kfont.size.pt--;
  344.         if(!kfont.changeSIZE())
  345.                 kfont.size.pt++;
  346.         else
  347.                 PreparePage();
  348. }
  349.  
  350. void EventRunEdit()
  351. {
  352.         if (check_is_the_adress_local(history.current())==true) {
  353.                 io.run(DEFAULT_EDITOR, history.current());
  354.         }
  355.         else {
  356.                 //io.write(strlen(io.buffer_data), io.buffer_data, DEFAULT_PREVIEW_PATH); // <--- doesn't work, smth odd, need to check
  357.                 CreateFile(strlen(io.buffer_data), io.buffer_data, DEFAULT_PREVIEW_PATH);
  358.                 io.run(DEFAULT_EDITOR, DEFAULT_PREVIEW_PATH);
  359.         }
  360. }
  361.  
  362. void EventChangeEncoding()
  363. {
  364.         menu.selected = encoding + 1;
  365.         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);
  366. }
  367.  
  368. void EventShowInfo() {
  369.         EventOpenAddress("aelia:about");
  370. }
  371.  
  372. void EventShowHistory()
  373. {
  374.         EventOpenAddress("aelia:history");
  375. }
  376.  
  377. void EventGoBack()
  378. {
  379.         if (history.back()) EventOpenAddress(history.current());
  380. }
  381.  
  382. void EventGoForward()
  383. {
  384.         if (history.forward()) EventOpenAddress(history.current());
  385. }
  386.  
  387. void EventShowSandwichMenu()
  388. {
  389.         menu.selected = 0;
  390.         menu.show(Form.left+Form.cwidth-130,Form.top+TOOLBAR_H+skin_height-10, 130, SANDWICH_MENU, 20);
  391. }
  392.  
  393. void EventPageRefresh()
  394. {
  395.         EventOpenAddress(history.current());
  396. }
  397.  
  398. void EventShowDownloader()
  399. {
  400.         if (!downloader_opened) {
  401.                 downloader_edit = NULL;
  402.                 CreateThread(#Downloader,#downloader_stak+4092);
  403.         }
  404. }
  405.  
  406. /* ------------------------------------------- */
  407.  
  408.  
  409. void draw_window()
  410. {
  411.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  412.         GetProcessInfo(#Form, SelfInfo);
  413.         if (Form.status_window>2) return;
  414.  
  415.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  416.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  417.        
  418.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 2, 0xe1e1e1);
  419.         DrawBar(0, TOOLBAR_H - 2, Form.cwidth, 1, 0xcecece);
  420.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  421.        
  422.         DrawToolbarButton(GO_BACK,         8);
  423.         DrawToolbarButton(GO_FORWARD,      33);
  424.         DrawToolbarButton(OPEN_FILE,       68);
  425.         DrawToolbarButton(MAGNIFY_PLUS,    Form.cwidth - 125);
  426.         DrawToolbarButton(MAGNIFY_MINUS,   Form.cwidth - 100);
  427.         DrawToolbarButton(CHANGE_ENCODING, Form.cwidth - 64);
  428.         DrawToolbarButton(SANDWICH,        Form.cwidth - 31);
  429.  
  430.         DrawAddressBox();
  431.  
  432.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  433.                 (Form.cheight-TOOLBAR_H == list.h) &&
  434.                 (list.count)
  435.         )
  436.         {
  437.                 DrawPage();
  438.         }
  439.         else
  440.         {
  441.                 if (!kfont.raw) {                           //this code need to be run
  442.                         if (param) EventOpenAddress(#param);    //only once at browser sturtup
  443.                         else EventOpenAddress("aelia:home");
  444.                 }
  445.                 else PreparePage();
  446.         }
  447.  
  448.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  449. }
  450.  
  451. void DrawPage()
  452. {
  453.         list.CheckDoesValuesOkey();
  454.         if (list.count) {
  455.                 kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  456.         }
  457.         DrawScroller();
  458. }
  459.  
  460. void DrawAddressBox()
  461. {
  462.         address_box.left = 97+19;
  463.         address_box.top = 11;
  464.         address_box.width = Form.cwidth - address_box.left - 138;
  465.         DrawRectangle(address_box.left-4-19, address_box.top-5, address_box.width+6+19, 23, 0x8C8C8C);
  466.         DrawWideRectangle(address_box.left-3-19, address_box.top-3, address_box.width+5+19, 21, 4, address_box.color);
  467.         address_box.size = address_box.pos = address_box.shift = address_box.shift_old = strlen(#address);
  468.         address_box.offset = 0;
  469.         edit_box_draw stdcall(#address_box);
  470.         favicon.draw(address_box.left-18, address_box.top-1);
  471.         DrawBar(address_box.left-2, address_box.top+1, 2, 13, 0xFFFfff);
  472. }