Subversion Repositories Kolibri OS

Rev

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