Subversion Repositories Kolibri OS

Rev

Rev 7450 | 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.22"
  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 - reopen current file in another app
  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. int reopenin_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. dword bg_color = 0xF0F0F0;
  63. dword text_color = 0;
  64.  
  65. #include "ini.h"
  66. #include "prepare_page.h"
  67.  
  68. void InitDlls()
  69. {
  70.         load_dll(boxlib,    #box_lib_init,   0);
  71.         load_dll(libio,     #libio_init,     1);
  72.         load_dll(libimg,    #libimg_init,    1);
  73.         load_dll(libini,    #lib_init,       1);
  74.         load_dll(iconv_lib, #iconv_open,     0);
  75.         load_dll(Proc_lib,  #OpenDialog_init,0);
  76. }
  77.  
  78.  
  79. void main()
  80. {      
  81.         InitDlls();    
  82.         OpenDialog_init stdcall (#o_dialog);
  83.         LoadIniSettings();
  84.         kfont.init(DEFAULT_FONT);
  85.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  86.         OpenFile(#param);
  87.         list.no_selection = true;
  88.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  89.         loop()
  90.         {
  91.                 switch(WaitEvent())
  92.                 {
  93.                         case evMouse:
  94.                                 HandleMouseEvent();
  95.                                 break;
  96.                         case evKey:
  97.                                 HandleKeyEvent();
  98.                                 break;
  99.                         case evButton:
  100.                                 HandleButtonEvent();
  101.                                 break;
  102.                         case evReDraw:
  103.                                 EventMenuClick();
  104.                                 draw_window();
  105.                 }
  106.         }
  107. }
  108.  
  109.  
  110. void HandleButtonEvent()
  111. {
  112.        
  113.         byte btn = GetButtonID();
  114.         if (btn==1) {
  115.                 SaveIniSettings();
  116.                 ExitProcess();
  117.         }
  118.         btn-=10;
  119.         switch(btn)
  120.         {
  121.                 case OPEN_FILE:
  122.                         EventOpenFile();
  123.                         break;
  124.                 case SHOW_FILE_PROPERTIES:
  125.                         EventShowFileProperties();
  126.                         break;
  127.                 case MAGNIFY_PLUS:
  128.                         EventMagnifyPlus();
  129.                         break;
  130.                 case MAGNIFY_MINUS:
  131.                         EventMagnifyMinus();
  132.                         break;
  133.                 case CHANGE_ENCODING:
  134.                         EventShowEncodingList();
  135.                         break;
  136.                 case RUN_EDIT:
  137.                         EventShowEdit();
  138.                         break;
  139.                 case SHOW_INFO:
  140.                         EventShowInfo();
  141.                         break;
  142.         }
  143. }
  144.  
  145.  
  146. void HandleKeyEvent()
  147. {
  148.         if (help_opened) {
  149.                 help_opened = false;
  150.                 DrawPage();
  151.                 return;
  152.         }
  153.         GetKeys();
  154.         if (key_scancode == SCAN_CODE_F1) {
  155.                 EventShowInfo();
  156.                 return;
  157.         }
  158.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  159.                 switch (key_scancode)
  160.                 {
  161.                         case SCAN_CODE_KEY_O:
  162.                                 EventOpenFile();
  163.                                 break;
  164.                         case SCAN_CODE_KEY_I:
  165.                                 EventShowFileProperties();
  166.                                 break;
  167.                         case SCAN_CODE_UP:
  168.                                 EventMagnifyPlus();
  169.                                 break;
  170.                         case SCAN_CODE_DOWN:
  171.                                 EventMagnifyMinus();
  172.                                 break;
  173.                         case SCAN_CODE_KEY_E:
  174.                                 EventShowEdit();
  175.                                 break;
  176.                         case SCAN_CODE_TAB:
  177.                                 EventChangeEncoding();
  178.                                 break;
  179.                 }
  180.                 return;
  181.         }
  182.         if (list.ProcessKey(key_scancode))
  183.                 DrawPage();
  184. }
  185.  
  186.  
  187. void HandleMouseEvent()
  188. {
  189.         mouse.get();
  190.         list.wheel_size = 7;
  191.         if (list.MouseScroll(mouse.vert)) {
  192.                 DrawPage();
  193.                 return;
  194.         }
  195.         scrollbar_v_mouse (#scroll);
  196.         if (list.first != scroll.position) {
  197.                 list.first = scroll.position;
  198.                 DrawPage();
  199.         }
  200. }
  201.  
  202.  
  203. /* ----------------------------------------------------- */
  204.  
  205. void EventOpenFile()
  206. {
  207.         OpenDialog_start stdcall (#o_dialog);
  208.         if (o_dialog.status) {
  209.                 OpenFile(#openfile_path);
  210.                 PreparePage();
  211.         }
  212. }
  213.  
  214. void EventShowFileProperties()
  215. {
  216.         char ss_param[4096];
  217.         if (!param) return;
  218.         sprintf(#ss_param, "-p %s", #param);
  219.         RunProgram("/sys/File managers/Eolite", #ss_param);
  220. }
  221.  
  222. void EventMagnifyPlus()
  223. {
  224.         kfont.size.pt++;
  225.         if(!kfont.changeSIZE())
  226.                 kfont.size.pt--;
  227.         else
  228.                 PreparePage();
  229. }
  230.  
  231. void EventMagnifyMinus()
  232. {
  233.         kfont.size.pt--;
  234.         if(!kfont.changeSIZE())
  235.                 kfont.size.pt++;
  236.         else
  237.                 PreparePage();
  238. }
  239.  
  240. void EventShowEdit()
  241. {
  242.         menu.selected = 0;
  243.         menu.show(Form.left+5 + reopenin_menu_left, Form.top+29+skin_height, 130,
  244.                 "Tinypad\nTextEdit\nWebView\nFB2Read\nHexView", 20);
  245. }
  246.  
  247. void EventShowEncodingList()
  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. void EventChangeEncoding(dword id)
  262. {
  263.         encoding = id;
  264.         OpenFile(#openfile_path);
  265.         PreparePage();
  266.         draw_window();
  267. }
  268.  
  269. void EventOpenFileInAnotherProgram(dword _app)
  270. {
  271.         RunProgram(_app, #param);
  272. }
  273.  
  274. void EventMenuClick()
  275. {
  276.         switch(menu.cur_y)
  277.         {
  278.                 //Encoding
  279.                 case 10...15:
  280.                         EventChangeEncoding(menu.cur_y-10);
  281.                         break;
  282.                 //Reopen
  283.                 case 20:
  284.                         EventOpenFileInAnotherProgram("/sys/tinypad");
  285.                         break;
  286.                 case 21:
  287.                         EventOpenFileInAnotherProgram("/sys/develop/t_edit");
  288.                         break;
  289.                 case 22:
  290.                         EventOpenFileInAnotherProgram("/sys/network/webview");
  291.                         break;
  292.                 case 23:
  293.                         EventOpenFileInAnotherProgram("/sys/fb2read");
  294.                         break;
  295.                 case 24:
  296.                         EventOpenFileInAnotherProgram("/sys/develop/heed");
  297.                         break;
  298.         }
  299.         menu.cur_y = 0;
  300. }
  301.  
  302. /* ------------------------------------------- */
  303.  
  304.  
  305. void OpenFile(dword f_path)
  306. {
  307.         int tmp;
  308.         if (ESBYTE[f_path]) {
  309.                 strcpy(#param, f_path);
  310.                 io.read(#param);
  311.                 strcpy(#title, #param);
  312.                 strcat(#title, " - Text Reader");
  313.         }
  314.         else {
  315.                 if (list.count) return;
  316.                 io.buffer_data = INTRO_TEXT;
  317.                 strcpy(#title, "Text Reader");
  318.         }
  319.         if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
  320.         list.KeyHome();
  321.         list.ClearList();
  322. }
  323.  
  324. void draw_window()
  325. {
  326.         #define PADDING 6
  327.         #define TOOLBAR_BUTTON_WIDTH 26
  328.         incn x;
  329.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  330.         GetProcessInfo(#Form, SelfInfo);
  331.         if (Form.status_window>2) return;
  332.  
  333.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  334.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  335.        
  336.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
  337.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  338.        
  339.         x.n = 0;
  340.         DrawToolbarButton(OPEN_FILE,       x.inc(8));
  341.         DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING));
  342.         DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
  343.         DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
  344.         DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
  345.                 charsets_menu_left = x.n;
  346.         DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + PADDING + PADDING));
  347.                 reopenin_menu_left = x.n;
  348.         DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
  349.        
  350.        
  351.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  352.                 (Form.cheight-TOOLBAR_H == list.h) &&
  353.                 (list.count)
  354.         )
  355.         {
  356.                 DrawPage();
  357.         } else {
  358.                 PreparePage();
  359.         }
  360.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  361. }
  362.  
  363. void DrawPage()
  364. {
  365.         kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  366.         DrawScroller();
  367. }
  368.  
  369. void DrawToolbarButton(char image_id, int x)
  370. {
  371.         DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
  372.         img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
  373. }
  374.  
  375. void DrawScroller()
  376. {
  377.         scroll.max_area = list.count;
  378.         scroll.cur_area = list.visible;
  379.         scroll.position = list.first;
  380.         scroll.all_redraw = 0;
  381.         scroll.start_x = list.x + list.w;
  382.         scroll.start_y = list.y;
  383.         scroll.size_y = list.h;
  384.         scrollbar_v_draw(#scroll);
  385. }