Subversion Repositories Kolibri OS

Rev

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

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