Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 4096*25
  2.  
  3. //===================================================//
  4. //                                                   //
  5. //                       LIB                         //
  6. //                                                   //
  7. //===================================================//
  8.  
  9. #include "../lib/io.h"
  10. #include "../lib/gui.h"
  11. #include "../lib/list_box.h"
  12. #include "../lib/kfont.h"
  13.  
  14. #include "../lib/obj/box_lib.h"
  15. #include "../lib/obj/libini.h"
  16. #include "../lib/obj/libimg.h"
  17. #include "../lib/obj/iconv.h"
  18. #include "../lib/obj/proc_lib.h"
  19.  
  20. #include "../lib/patterns/simple_open_dialog.h"
  21.  
  22. //===================================================//
  23. //                                                   //
  24. //                       DATA                        //
  25. //                                                   //
  26. //===================================================//
  27.  
  28. #define TOOLBAR_H 34
  29. #define TOOLBAR_ICON_WIDTH  26
  30. #define TOOLBAR_ICON_HEIGHT 24
  31.  
  32. #define DEFAULT_EDITOR "/sys/tinypad"
  33.  
  34. #define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
  35. #define VERSION "Text Reader v1.3"
  36. #define ABOUT "Idea: Leency, punk_joker
  37. Code: Leency, Veliant, KolibriOS Team
  38.  
  39. Hotkeys:
  40. Ctrl+O - open file
  41. Ctrl+I - show file properties
  42. Ctrl+Up - bigger font
  43. Ctrl+Down - smaller font
  44. Ctrl+Tab - select charset
  45. Ctrl+E - reopen current file in another app
  46.  
  47. Press any key..."
  48.  
  49. dword color_schemes[] = {
  50. 0xFFFfff, 0,
  51. 0xF0F0F0, 0,
  52. 0xE9E5DA, 0,
  53. 0xF0F0C7, 0,
  54. 0xFCF0DA, 0x574531,
  55. 0xFDF6E3, 0x101A21,
  56. 0x282C34, 0xABB2BF,
  57. 0x282923, 0xD8D8D2
  58. };
  59.  
  60. char color_scheme_names[] =
  61. "White & Black
  62. Grey & Black      RtfRead
  63. Khaki & Black     QNX
  64. Lemon & Black     Fb2Read
  65. Antique & Black   Pocket
  66. Linen & Black     Horst
  67. DarkGrey & Grey   Godot
  68. DarkGrey & Grey   Monokai";
  69.  
  70. char default_dir[] = "/rd/1";
  71. od_filter filter2 = { 8, "TXT\0\0" };
  72.  
  73. 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};
  74. llist list;
  75.  
  76. proc_info Form;
  77. char title[4196];
  78.  
  79. bool help_opened = false;
  80. int charsets_mx;
  81. int reopenin_mx;
  82. int colscheme_mx;
  83.  
  84. int curcol_scheme;
  85.  
  86. enum {
  87.         OPEN_FILE,
  88.         MAGNIFY_MINUS,
  89.         MAGNIFY_PLUS,
  90.         CHANGE_ENCODING,
  91.         RUN_EDIT,
  92.         SHOW_INFO,
  93.         SHOW_FILE_PROPERTIES,
  94.         COLOR_SCHEME
  95. };
  96.  
  97. int encoding;
  98.  
  99. dword bg_color;
  100. dword text_color;
  101.  
  102. //===================================================//
  103. //                                                   //
  104. //                 INTERNAL INCLUDES                 //
  105. //                                                   //
  106. //===================================================//
  107.  
  108. #include "ini.h"
  109. #include "prepare_page.h"
  110.  
  111. //===================================================//
  112. //                                                   //
  113. //                       CODE                        //
  114. //                                                   //
  115. //===================================================//
  116.  
  117. void InitDlls()
  118. {
  119.         load_dll(boxlib,    #box_lib_init,   0);
  120.         load_dll(libio,     #libio_init,     1);
  121.         load_dll(libimg,    #libimg_init,    1);
  122.         load_dll(libini,    #lib_init,       1);
  123.         load_dll(iconv_lib, #iconv_open,     0);
  124.         load_dll(Proc_lib,  #OpenDialog_init,0);
  125. }
  126.  
  127. void main()
  128. {      
  129.         InitDlls();    
  130.         OpenDialog_init stdcall (#o_dialog);
  131.         LoadIniSettings();
  132.         EventSetColorScheme(curcol_scheme);
  133.         kfont.init(DEFAULT_FONT);
  134.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  135.         OpenFile(#param);
  136.         list.no_selection = true;
  137.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  138.         loop()
  139.         {
  140.                 switch(WaitEvent())
  141.                 {
  142.                         case evMouse:
  143.                                 HandleMouseEvent();
  144.                                 break;
  145.                         case evKey:
  146.                                 HandleKeyEvent();
  147.                                 break;
  148.                         case evButton:
  149.                                 HandleButtonEvent();
  150.                                 break;
  151.                         case evReDraw:
  152.                                 EventMenuClick();
  153.                                 draw_window();
  154.                 }
  155.         }
  156. }
  157.  
  158. //===================================================//
  159. //                                                   //
  160. //                      EVENTS                       //
  161. //                                                   //
  162. //===================================================//
  163.  
  164. void HandleButtonEvent()
  165. {
  166.        
  167.         byte btn = GetButtonID();
  168.         if (btn==1) {
  169.                 SaveIniSettings();
  170.                 ExitProcess();
  171.         }
  172.         btn-=10;
  173.         switch(btn)
  174.         {
  175.                 case OPEN_FILE:
  176.                         EventOpenFile();
  177.                         break;
  178.                 case SHOW_FILE_PROPERTIES:
  179.                         EventShowFileProperties();
  180.                         break;
  181.                 case MAGNIFY_PLUS:
  182.                         EventMagnifyPlus();
  183.                         break;
  184.                 case MAGNIFY_MINUS:
  185.                         EventMagnifyMinus();
  186.                         break;
  187.                 case CHANGE_ENCODING:
  188.                         EventShowEncodingList();
  189.                         break;
  190.                 case RUN_EDIT:
  191.                         EventShowReopenMenu();
  192.                         break;
  193.                 case COLOR_SCHEME:
  194.                         EventShowColorSchemesList();
  195.                         break;
  196.                 case SHOW_INFO:
  197.                         EventShowInfo();
  198.                         break;
  199.         }
  200. }
  201.  
  202.  
  203. void HandleKeyEvent()
  204. {
  205.         if (help_opened) {
  206.                 help_opened = false;
  207.                 DrawPage();
  208.                 return;
  209.         }
  210.         GetKeys();
  211.         if (key_scancode == SCAN_CODE_F1) {
  212.                 EventShowInfo();
  213.                 return;
  214.         }
  215.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  216.                 switch (key_scancode)
  217.                 {
  218.                         case SCAN_CODE_KEY_O:
  219.                                 EventOpenFile();
  220.                                 break;
  221.                         case SCAN_CODE_KEY_I:
  222.                                 EventShowFileProperties();
  223.                                 break;
  224.                         case SCAN_CODE_UP:
  225.                                 EventMagnifyPlus();
  226.                                 break;
  227.                         case SCAN_CODE_DOWN:
  228.                                 EventMagnifyMinus();
  229.                                 break;
  230.                         case SCAN_CODE_KEY_E:
  231.                                 EventShowReopenMenu();
  232.                                 break;
  233.                         case SCAN_CODE_TAB:
  234.                                 EventChangeEncoding();
  235.                                 break;
  236.                 }
  237.                 return;
  238.         }
  239.         if (list.ProcessKey(key_scancode))
  240.                 DrawPage();
  241. }
  242.  
  243.  
  244. void HandleMouseEvent()
  245. {
  246.         mouse.get();
  247.         list.wheel_size = 7;
  248.         if (list.MouseScroll(mouse.vert)) {
  249.                 DrawPage();
  250.                 return;
  251.         }
  252.         scrollbar_v_mouse (#scroll);
  253.         if (list.first != scroll.position) {
  254.                 list.first = scroll.position;
  255.                 DrawPage();
  256.         }
  257. }
  258.  
  259. /* ----------------------------------------------------- */
  260.  
  261. void EventOpenFile()
  262. {
  263.         OpenDialog_start stdcall (#o_dialog);
  264.         if (o_dialog.status) {
  265.                 OpenFile(#openfile_path);
  266.                 PreparePage();
  267.         }
  268. }
  269.  
  270. void EventShowFileProperties()
  271. {
  272.         char ss_param[4096];
  273.         if (!param) return;
  274.         sprintf(#ss_param, "-p %s", #param);
  275.         RunProgram("/sys/File managers/Eolite", #ss_param);
  276. }
  277.  
  278. void EventMagnifyPlus()
  279. {
  280.         kfont.size.pt++;
  281.         if(!kfont.changeSIZE())
  282.                 kfont.size.pt--;
  283.         else
  284.                 PreparePage();
  285. }
  286.  
  287. void EventMagnifyMinus()
  288. {
  289.         kfont.size.pt--;
  290.         if(!kfont.changeSIZE())
  291.                 kfont.size.pt++;
  292.         else
  293.                 PreparePage();
  294. }
  295.  
  296. void EventShowEncodingList()
  297. {
  298.         menu.selected = encoding + 1;
  299.         menu.show(Form.left+5 + charsets_mx, Form.top+29+skin_height, 130,
  300.                 "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866", 10);
  301. }
  302.  
  303. void EventShowReopenMenu()
  304. {
  305.         menu.selected = 0;
  306.         menu.show(Form.left+5 + reopenin_mx, Form.top+29+skin_height, 130,
  307.                 "Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther", 20);
  308. }
  309.  
  310. void EventShowColorSchemesList()
  311. {
  312.         menu.selected = curcol_scheme + 1;
  313.         menu.show(Form.left+5 + colscheme_mx, Form.top+29+skin_height, 175, #color_scheme_names, 30);
  314. }
  315.  
  316. void EventSetColorScheme(dword _setn)
  317. {
  318.         curcol_scheme = _setn;
  319.         bg_color   = color_schemes[curcol_scheme*2];
  320.         text_color = color_schemes[curcol_scheme*2+1];
  321. }
  322.  
  323. void EventShowInfo() {
  324.         help_opened = true;
  325.         DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
  326.         WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
  327.         WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
  328. }
  329.  
  330. void EventChangeEncoding(dword id)
  331. {
  332.         encoding = id;
  333.         OpenFile(#openfile_path);
  334.         PreparePage();
  335.         draw_window();
  336. }
  337.  
  338. void EventOpenFileInAnotherProgram(dword _app)
  339. {
  340.         RunProgram(_app, #param);
  341. }
  342.  
  343. void EventMenuClick()
  344. {
  345.         byte open_param[4096];
  346.         switch(menu.cur_y)
  347.         {
  348.                 //Encoding
  349.                 case 10...15:
  350.                         EventChangeEncoding(menu.cur_y-10);
  351.                         break;
  352.                 //Reopen
  353.                 case 20:
  354.                         EventOpenFileInAnotherProgram("/sys/tinypad");
  355.                         break;
  356.                 case 21:
  357.                         EventOpenFileInAnotherProgram("/sys/develop/t_edit");
  358.                         break;
  359.                 case 22:
  360.                         EventOpenFileInAnotherProgram("/sys/network/webview");
  361.                         break;
  362.                 case 23:
  363.                         EventOpenFileInAnotherProgram("/sys/fb2read");
  364.                         break;
  365.                 case 24:
  366.                         EventOpenFileInAnotherProgram("/sys/develop/heed");
  367.                         break;
  368.                 case 25:
  369.                         sprintf(#open_param,"~%s",#param);
  370.                         RunProgram("/sys/@open", #open_param);
  371.                         break;
  372.                 //ColorSchemes
  373.                 case 30...38:
  374.                         EventSetColorScheme(menu.cur_y-30);
  375.                         PreparePage();
  376.                         break;
  377.         }
  378.         menu.cur_y = 0;
  379. }
  380.  
  381. //===================================================//
  382. //                                                   //
  383. //               DRAWS AND OTHER FUNCS               //
  384. //                                                   //
  385. //===================================================//
  386.  
  387. void OpenFile(dword f_path)
  388. {
  389.         int tmp;
  390.         if (ESBYTE[f_path]) {
  391.                 strcpy(#param, f_path);
  392.                 io.read(#param);
  393.                 strcpy(#title, #param);
  394.                 strcat(#title, " - Text Reader");
  395.         }
  396.         else {
  397.                 if (list.count) return;
  398.                 io.buffer_data = INTRO_TEXT;
  399.                 strcpy(#title, "Text Reader");
  400.         }
  401.         if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
  402.         list.KeyHome();
  403.         list.ClearList();
  404. }
  405.  
  406. void draw_window()
  407. {
  408.         #define BUTTONS_GAP 6
  409.         #define BLOCKS_GAP 15
  410.         #define TOOLBAR_BUTTON_WIDTH 26
  411.         incn x;
  412.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  413.         GetProcessInfo(#Form, SelfInfo);
  414.         if (Form.status_window>2) return;
  415.  
  416.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  417.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  418.        
  419.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
  420.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  421.        
  422.         x.n = 0;
  423.         DrawToolbarButton(OPEN_FILE,       x.inc(8));
  424.         DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
  425.  
  426.         DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
  427.         DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
  428.         DrawToolbarButton(COLOR_SCHEME,    x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); colscheme_mx = x.n;
  429.  
  430.         DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); charsets_mx = x.n;
  431.         DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); reopenin_mx = x.n;
  432.         DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
  433.        
  434.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  435.                 (Form.cheight-TOOLBAR_H == list.h) &&
  436.                 (list.count)
  437.         )
  438.         {
  439.                 DrawPage();
  440.         } else {
  441.                 PreparePage();
  442.         }              
  443.        
  444.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  445. }
  446.  
  447. void DrawPage()
  448. {
  449.         kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  450.         DrawScroller();
  451. }
  452.  
  453. void DrawToolbarButton(char image_id, int x)
  454. {
  455.         DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH-2, TOOLBAR_ICON_HEIGHT-2, 10+image_id + BT_HIDE, 0);
  456.         img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
  457. }
  458.  
  459. void DrawScroller()
  460. {
  461.         scroll.max_area = list.count;
  462.         scroll.cur_area = list.visible;
  463.         scroll.position = list.first;
  464.         scroll.all_redraw = 0;
  465.         scroll.start_x = list.x + list.w;
  466.         scroll.start_y = list.y;
  467.         scroll.size_y = list.h;
  468.         scrollbar_v_draw(#scroll);
  469. }