Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 4096*25
  2.  
  3. #include "../lib/io.h"
  4. #include "../lib/gui.h"
  5. #include "../lib/list_box.h"
  6. #include "../lib/menu.h"
  7. #include "../lib/kfont.h"
  8.  
  9. #include "../lib/obj/box_lib.h"
  10. #include "../lib/obj/libini.h"
  11. #include "../lib/obj/libimg.h"
  12. #include "../lib/obj/iconv.h"
  13. #include "../lib/obj/proc_lib.h"
  14.  
  15. #include "../lib/patterns/simple_open_dialog.h"
  16.  
  17. #define TOOLBAR_H 34
  18. #define TOOLBAR_ICON_WIDTH  26
  19. #define TOOLBAR_ICON_HEIGHT 24
  20.  
  21. #define DEFAULT_EDITOR "/sys/tinypad"
  22.  
  23. #define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
  24. #define VERSION "Text Reader v1.21"
  25. #define ABOUT "Idea: Leency, punk_joker
  26. Code: Leency, Veliant, KolibriOS Team
  27.  
  28. Hotkeys:
  29. Ctrl+O - open file
  30. Ctrl+I - show file properties
  31. Ctrl+Up - bigger font
  32. Ctrl+Down - smaller font
  33. Ctrl+Tab - select charset
  34. Ctrl+E - edit current document
  35.  
  36. Press any key..."
  37.  
  38. char default_dir[] = "/rd/1";
  39. od_filter filter2 = { 8, "TXT\0\0" };
  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. bool help_opened = false;
  48. int charsets_menu_left = 0;
  49.  
  50. enum {
  51.         OPEN_FILE,
  52.         MAGNIFY_MINUS,
  53.         MAGNIFY_PLUS,
  54.         CHANGE_ENCODING,
  55.         RUN_EDIT,
  56.         SHOW_INFO,
  57.         SHOW_FILE_PROPERTIES
  58. };
  59.  
  60. int encoding;
  61.  
  62. #include "ini.h"
  63. #include "gui.h"
  64. #include "prepare_page.h"
  65.  
  66.  
  67. void InitDlls()
  68. {
  69.         load_dll(boxlib,    #box_lib_init,   0);
  70.         load_dll(libio,     #libio_init,     1);
  71.         load_dll(libimg,    #libimg_init,    1);
  72.         load_dll(libini,    #lib_init,       1);
  73.         load_dll(iconv_lib, #iconv_open,     0);
  74.         load_dll(Proc_lib,  #OpenDialog_init,0);
  75. }
  76.  
  77.  
  78. void main()
  79. {      
  80.         InitDlls();    
  81.         OpenDialog_init stdcall (#o_dialog);
  82.         LoadIniSettings();
  83.         kfont.init(DEFAULT_FONT);
  84.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  85.         OpenFile(#param);
  86.         list.no_selection = true;
  87.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  88.         loop()
  89.         {
  90.                 switch(WaitEvent())
  91.                 {
  92.                         case evMouse:
  93.                                 HandleMouseEvent();
  94.                                 break;
  95.                         case evKey:
  96.                                 HandleKeyEvent();
  97.                                 break;
  98.                         case evButton:
  99.                                 HandleButtonEvent();
  100.                                 break;
  101.                         case evReDraw:
  102.                                 if (menu.list.cur_y) {
  103.                                         encoding = menu.list.cur_y - 10;
  104.                                         OpenFile(#param);
  105.                                         PreparePage();
  106.                                         menu.list.cur_y = NULL;
  107.                                 };
  108.                                 draw_window();
  109.                 }
  110.         }
  111. }
  112.  
  113.  
  114. void HandleButtonEvent()
  115. {
  116.        
  117.         byte btn = GetButtonID();
  118.         if (btn==1) {
  119.                 SaveIniSettings();
  120.                 ExitProcess();
  121.         }
  122.         btn-=10;
  123.         switch(btn)
  124.         {
  125.                 case OPEN_FILE:
  126.                         EventOpenFile();
  127.                         break;
  128.                 case SHOW_FILE_PROPERTIES:
  129.                         EventShowFileProperties();
  130.                         break;
  131.                 case MAGNIFY_PLUS:
  132.                         EventMagnifyPlus();
  133.                         break;
  134.                 case MAGNIFY_MINUS:
  135.                         EventMagnifyMinus();
  136.                         break;
  137.                 case CHANGE_ENCODING:
  138.                         EventChangeEncoding();
  139.                         break;
  140.                 case RUN_EDIT:
  141.                         EventRunEdit();
  142.                         break;
  143.                 case SHOW_INFO:
  144.                         EventShowInfo();
  145.                         break;
  146.         }
  147. }
  148.  
  149.  
  150. void HandleKeyEvent()
  151. {
  152.         if (help_opened) {
  153.                 help_opened = false;
  154.                 DrawPage();
  155.                 return;
  156.         }
  157.         GetKeys();
  158.         if (key_scancode == SCAN_CODE_F1) {
  159.                 EventShowInfo();
  160.                 return;
  161.         }
  162.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  163.                 switch (key_scancode)
  164.                 {
  165.                         case SCAN_CODE_KEY_O:
  166.                                 EventOpenFile();
  167.                                 break;
  168.                         case SCAN_CODE_KEY_I:
  169.                                 EventShowFileProperties();
  170.                                 break;
  171.                         case SCAN_CODE_UP:
  172.                                 EventMagnifyPlus();
  173.                                 break;
  174.                         case SCAN_CODE_DOWN:
  175.                                 EventMagnifyMinus();
  176.                                 break;
  177.                         case SCAN_CODE_KEY_E:
  178.                                 EventRunEdit();
  179.                                 break;
  180.                         case SCAN_CODE_TAB:
  181.                                 EventChangeEncoding();
  182.                                 break;
  183.                 }
  184.                 return;
  185.         }
  186.         if (list.ProcessKey(key_scancode))
  187.                 DrawPage();
  188. }
  189.  
  190.  
  191. void HandleMouseEvent()
  192. {
  193.         mouse.get();
  194.         list.wheel_size = 7;
  195.         if (list.MouseScroll(mouse.vert)) {
  196.                 DrawPage();
  197.                 return;
  198.         }
  199.         scrollbar_v_mouse (#scroll);
  200.         if (list.first != scroll.position) {
  201.                 list.first = scroll.position;
  202.                 DrawPage();
  203.         }
  204. }
  205.  
  206.  
  207. /* ----------------------------------------------------- */
  208.  
  209. void EventOpenFile()
  210. {
  211.         OpenDialog_start stdcall (#o_dialog);
  212.         if (o_dialog.status) {
  213.                 OpenFile(#openfile_path);
  214.                 PreparePage();
  215.         }
  216. }
  217.  
  218. void EventShowFileProperties()
  219. {
  220.         char ss_param[4096];
  221.         if (!param) return;
  222.         sprintf(#ss_param, "-p %s", #param);
  223.         io.run("/sys/File managers/Eolite", #ss_param);
  224. }
  225.  
  226. void EventMagnifyPlus()
  227. {
  228.         kfont.size.pt++;
  229.         if(!kfont.changeSIZE())
  230.                 kfont.size.pt--;
  231.         else
  232.                 PreparePage();
  233. }
  234.  
  235. void EventMagnifyMinus()
  236. {
  237.         kfont.size.pt--;
  238.         if(!kfont.changeSIZE())
  239.                 kfont.size.pt++;
  240.         else
  241.                 PreparePage();
  242. }
  243.  
  244. void EventRunEdit()
  245. {
  246.         io.run(DEFAULT_EDITOR, #param);
  247. }
  248.  
  249. void EventChangeEncoding()
  250. {
  251.         menu.selected = encoding + 1;
  252.         menu.show(Form.left+5 + charsets_menu_left, Form.top+29+skin_height, 130,
  253.                 "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
  254. }
  255.  
  256. void EventShowInfo() {
  257.         help_opened = true;
  258.         DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
  259.         WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
  260.         WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
  261. }
  262.  
  263. /* ------------------------------------------- */
  264.  
  265.  
  266. void OpenFile(dword f_path)
  267. {
  268.         int tmp;
  269.         if (ESBYTE[f_path]) {
  270.                 strcpy(#param, f_path);
  271.                 io.read(#param);
  272.                 strcpy(#title, #param);
  273.                 strcat(#title, " - Text Reader");
  274.         }
  275.         else {
  276.                 if (list.count) return;
  277.                 io.buffer_data = INTRO_TEXT;
  278.                 strcpy(#title, "Text Reader");
  279.         }
  280.         if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
  281.         list.KeyHome();
  282.         list.ClearList();
  283. }
  284.  
  285. void draw_window()
  286. {
  287.         #define PADDING 6
  288.         #define TOOLBAR_BUTTON_WIDTH 26
  289.         incn x;
  290.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  291.         GetProcessInfo(#Form, SelfInfo);
  292.         if (Form.status_window>2) return;
  293.  
  294.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  295.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  296.        
  297.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
  298.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  299.        
  300.         x.n = 0;
  301.         DrawToolbarButton(OPEN_FILE,       x.inc(8));
  302.         DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
  303.         DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
  304.         DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH - 1));
  305.         DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
  306.                 charsets_menu_left = x.n;
  307.         DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
  308.         DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
  309.        
  310.        
  311.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  312.                 (Form.cheight-TOOLBAR_H == list.h) &&
  313.                 (list.count)
  314.         )
  315.         {
  316.                 DrawPage();
  317.         } else {
  318.                 PreparePage();
  319.         }
  320.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  321. }
  322.  
  323. void DrawPage()
  324. {
  325.         kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  326.         DrawScroller();
  327. }
  328.  
  329. void DrawToolbarButton(char image_id, int x)
  330. {
  331.         DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
  332.         img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 0, image_id*TOOLBAR_ICON_HEIGHT);
  333. }
  334.  
  335. void DrawScroller()
  336. {
  337.         scroll.max_area = list.count;
  338.         scroll.cur_area = list.visible;
  339.         scroll.position = list.first;
  340.         scroll.all_redraw = 0;
  341.         scroll.start_x = list.x + list.w;
  342.         scroll.start_y = list.y;
  343.         scroll.size_y = list.h;
  344.         scrollbar_v_draw(#scroll);
  345. }