Subversion Repositories Kolibri OS

Rev

Rev 7798 | 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  24
  30. #define TOOLBAR_ICON_HEIGHT 22
  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. 0xFDF6E3, 0x101A21,
  53. 0xFCF0DA, 0x171501,
  54. 0xF0F0C7, 0,
  55. 0x282C34, 0xABB2BF,
  56. 0x282923, 0xD8D8D2
  57. };
  58.  
  59. char color_scheme_names[] =
  60. "White & Black
  61. Grey & Black      RtfRead
  62. Linen & Black     Horst
  63. Antique & Black   Pocket
  64. Lemon & Black     Fb2Read
  65. DarkGrey & Grey   Godot
  66. DarkGrey & Grey   Monokai";
  67.  
  68. char default_dir[] = "/rd/1";
  69. od_filter filter2 = { 8, "TXT\0\0" };
  70.  
  71. 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};
  72. llist list;
  73.  
  74. proc_info Form;
  75. char title[4196];
  76.  
  77. bool help_opened = false;
  78. int charsets_mx;
  79. int reopenin_mx;
  80. int colscheme_mx;
  81.  
  82. int curcol_scheme;
  83.  
  84. enum {
  85.         OPEN_FILE,
  86.         MAGNIFY_MINUS,
  87.         MAGNIFY_PLUS,
  88.         CHANGE_ENCODING,
  89.         RUN_EDIT,
  90.         SHOW_INFO,
  91.         SHOW_FILE_PROPERTIES,
  92.         COLOR_SCHEME
  93. };
  94.  
  95. int encoding;
  96.  
  97. dword bg_color;
  98. dword text_color;
  99.  
  100. dword menu_id;
  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.         if (param) strcpy(#openfile_path, #param);
  132.         LoadIniSettings();
  133.         EventSetColorScheme(curcol_scheme);
  134.         kfont.init(DEFAULT_FONT);
  135.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  136.         OpenFile(#param);
  137.         list.no_selection = true;
  138.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  139.         loop()
  140.         {
  141.                 switch(WaitEvent())
  142.                 {
  143.                         case evMouse:
  144.                                 HandleMouseEvent();
  145.                                 break;
  146.                         case evKey:
  147.                                 HandleKeyEvent();
  148.                                 break;
  149.                         case evButton:
  150.                                 HandleButtonEvent();
  151.                                 break;
  152.                         case evReDraw:
  153.                                 if (CheckActiveProcess(Form.ID)) EventMenuClick();
  154.                                 draw_window();
  155.                 }
  156.         }
  157. }
  158.  
  159. //===================================================//
  160. //                                                   //
  161. //                      EVENTS                       //
  162. //                                                   //
  163. //===================================================//
  164.  
  165. void HandleButtonEvent()
  166. {
  167.        
  168.         byte btn = GetButtonID();
  169.         if (btn==1) {
  170.                 SaveIniSettings();
  171.                 ExitProcess();
  172.         }
  173.         btn-=10;
  174.         switch(btn)
  175.         {
  176.                 case OPEN_FILE:
  177.                         EventOpenFile();
  178.                         break;
  179.                 case SHOW_FILE_PROPERTIES:
  180.                         EventShowFileProperties();
  181.                         break;
  182.                 case MAGNIFY_PLUS:
  183.                         EventMagnifyPlus();
  184.                         break;
  185.                 case MAGNIFY_MINUS:
  186.                         EventMagnifyMinus();
  187.                         break;
  188.                 case CHANGE_ENCODING:
  189.                         EventShowEncodingList();
  190.                         break;
  191.                 case RUN_EDIT:
  192.                         EventShowReopenMenu();
  193.                         break;
  194.                 case COLOR_SCHEME:
  195.                         EventShowColorSchemesList();
  196.                         break;
  197.                 case SHOW_INFO:
  198.                         EventShowInfo();
  199.                         break;
  200.         }
  201. }
  202.  
  203.  
  204. void HandleKeyEvent()
  205. {
  206.         if (help_opened) {
  207.                 help_opened = false;
  208.                 DrawPage();
  209.                 return;
  210.         }
  211.         GetKeys();
  212.         if (key_scancode == SCAN_CODE_F1) {
  213.                 EventShowInfo();
  214.                 return;
  215.         }
  216.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  217.                 switch (key_scancode)
  218.                 {
  219.                         case SCAN_CODE_KEY_O:
  220.                                 EventOpenFile();
  221.                                 break;
  222.                         case SCAN_CODE_KEY_I:
  223.                                 EventShowFileProperties();
  224.                                 break;
  225.                         case SCAN_CODE_PLUS:
  226.                         case SCAN_CODE_UP:
  227.                                 EventMagnifyPlus();
  228.                                 break;
  229.                         case SCAN_CODE_DOWN:
  230.                         case SCAN_CODE_MINUS:
  231.                                 EventMagnifyMinus();
  232.                                 break;
  233.                         case SCAN_CODE_KEY_E:
  234.                                 EventShowReopenMenu();
  235.                                 break;
  236.                         case SCAN_CODE_TAB:
  237.                                 EventChangeEncoding();
  238.                                 break;
  239.                 }
  240.                 return;
  241.         }
  242.         if (list.ProcessKey(key_scancode))
  243.                 DrawPage();
  244. }
  245.  
  246.  
  247. void HandleMouseEvent()
  248. {
  249.         mouse.get();
  250.         list.wheel_size = 7;
  251.         if (list.MouseScroll(mouse.vert)) {
  252.                 DrawPage();
  253.                 return;
  254.         }
  255.         scrollbar_v_mouse (#scroll);
  256.         if (list.first != scroll.position) {
  257.                 list.first = scroll.position;
  258.                 DrawPage();
  259.         }
  260. }
  261.  
  262. /* ----------------------------------------------------- */
  263.  
  264. void EventOpenFile()
  265. {
  266.         OpenDialog_start stdcall (#o_dialog);
  267.         if (o_dialog.status) {
  268.                 OpenFile(#openfile_path);
  269.                 PreparePage();
  270.         }
  271. }
  272.  
  273. void EventShowFileProperties()
  274. {
  275.         char ss_param[4096];
  276.         if (!param) return;
  277.         strcpy(#ss_param, "-p ");
  278.         strcpy(#ss_param+3, #param);
  279.         RunProgram("/sys/File managers/Eolite", #ss_param);
  280. }
  281.  
  282. void EventMagnifyPlus()
  283. {
  284.         kfont.size.pt++;
  285.         if(!kfont.changeSIZE())
  286.                 kfont.size.pt--;
  287.         else
  288.                 PreparePage();
  289. }
  290.  
  291. void EventMagnifyMinus()
  292. {
  293.         kfont.size.pt--;
  294.         if(!kfont.changeSIZE())
  295.                 kfont.size.pt++;
  296.         else
  297.                 PreparePage();
  298. }
  299.  
  300. void EventShowEncodingList()
  301. {
  302.         open_lmenu(Form.left+5 + charsets_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT,
  303.                 encoding+1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
  304.         menu_id = 10;
  305. }
  306.  
  307. void EventShowReopenMenu()
  308. {
  309.         open_lmenu(Form.left+5 + reopenin_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, 0,
  310.                 "Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther");
  311.         menu_id = 20;
  312. }
  313.  
  314. void EventShowColorSchemesList()
  315. {
  316.         open_lmenu(Form.left+5 + colscheme_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT,
  317.                 curcol_scheme+1, #color_scheme_names);
  318.         menu_id = 30;
  319. }
  320.  
  321. void EventSetColorScheme(dword _setn)
  322. {
  323.         curcol_scheme = _setn;
  324.         bg_color   = color_schemes[curcol_scheme*2];
  325.         text_color = color_schemes[curcol_scheme*2+1];
  326. }
  327.  
  328. void EventShowInfo() {
  329.         help_opened = true;
  330.         DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
  331.         WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
  332.         WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
  333. }
  334.  
  335. void EventChangeEncoding(dword id)
  336. {
  337.         encoding = id;
  338.         OpenFile(#openfile_path);
  339.         PreparePage();
  340.         draw_window();
  341. }
  342.  
  343. void EventOpenFileInAnotherProgram(dword _app)
  344. {
  345.         RunProgram(_app, #param);
  346. }
  347.  
  348. void EventMenuClick()
  349. {
  350.         byte open_param[4096];
  351.         dword click_id = get_menu_click();
  352.         if (click_id) switch(click_id + menu_id - 1)
  353.         {
  354.                 //Encoding
  355.                 case 10...15:
  356.                         EventChangeEncoding(click_id-1);
  357.                         break;
  358.                 //Reopen
  359.                 case 20:
  360.                         EventOpenFileInAnotherProgram("/sys/tinypad");
  361.                         break;
  362.                 case 21:
  363.                         EventOpenFileInAnotherProgram("/sys/develop/t_edit");
  364.                         break;
  365.                 case 22:
  366.                         EventOpenFileInAnotherProgram("/sys/network/webview");
  367.                         break;
  368.                 case 23:
  369.                         EventOpenFileInAnotherProgram("/sys/fb2read");
  370.                         break;
  371.                 case 24:
  372.                         EventOpenFileInAnotherProgram("/sys/develop/heed");
  373.                         break;
  374.                 case 25:
  375.                         open_param[0]='~';
  376.                         strcpy(#open_param+1,#param);
  377.                         RunProgram("/sys/@open", #open_param);
  378.                         break;
  379.                 //ColorSchemes
  380.                 case 30...38:
  381.                         EventSetColorScheme(click_id-1);
  382.                         PreparePage();
  383.                         break;
  384.         }
  385. }
  386.  
  387. //===================================================//
  388. //                                                   //
  389. //               DRAWS AND OTHER FUNCS               //
  390. //                                                   //
  391. //===================================================//
  392.  
  393. void OpenFile(dword f_path)
  394. {
  395.         int tmp;
  396.         if (ESBYTE[f_path]) {
  397.                 strcpy(#param, f_path);
  398.                 io.read(#param);
  399.                 strcpy(#title, #param);
  400.                 strcat(#title, " - Text Reader");
  401.         }
  402.         else {
  403.                 if (list.count) return;
  404.                 io.buffer_data = INTRO_TEXT;
  405.                 strcpy(#title, "Text Reader");
  406.         }
  407.         if (encoding!=CH_CP866) ChangeCharset(encoding, "CP866", io.buffer_data);
  408.         list.KeyHome();
  409.         list.ClearList();
  410. }
  411.  
  412. void draw_window()
  413. {
  414.         #define BUTTONS_GAP 6
  415.         #define BLOCKS_GAP 15
  416.         #define TOOLBAR_BUTTON_WIDTH 26
  417.         incn x;
  418.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0);
  419.         GetProcessInfo(#Form, SelfInfo);
  420.         sc.get();
  421.         if (Form.status_window>2) return;
  422.  
  423.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  424.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  425.        
  426.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, sc.work);
  427.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, sc.work_graph);
  428.        
  429.         x.n = 0;
  430.         DrawToolbarButton(OPEN_FILE,       x.inc(8));
  431.         DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP));
  432.  
  433.         DrawToolbarButton(MAGNIFY_MINUS,   x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP));
  434.         DrawToolbarButton(MAGNIFY_PLUS,    x.inc(TOOLBAR_BUTTON_WIDTH - 1));
  435.         DrawToolbarButton(COLOR_SCHEME,    x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); colscheme_mx = x.n;
  436.  
  437.         DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); charsets_mx = x.n;
  438.         DrawToolbarButton(RUN_EDIT,        x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); reopenin_mx = x.n;
  439.         DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
  440.        
  441.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  442.                 (Form.cheight-TOOLBAR_H == list.h) &&
  443.                 (list.count)
  444.         ) {
  445.                 DrawPage();
  446.         } else {
  447.                 PreparePage();
  448.         }              
  449.        
  450.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  451. }
  452.  
  453. void DrawPage()
  454. {
  455.         kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w);
  456.         DrawScroller();
  457. }
  458.  
  459. void DrawToolbarButton(char image_id, int x)
  460. {
  461.         DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 10+image_id + BT_HIDE, 0);
  462.         //img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0);
  463.         DrawOvalBorder(x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, sc.work_graph,
  464.                 sc.work_graph,sc.work_graph, sc.work_dark);
  465.         img_draw stdcall(skin.image, x+1, 5+1, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH*image_id, 0);
  466. }
  467.  
  468. void DrawScroller()
  469. {
  470.         scroll.max_area = list.count;
  471.         scroll.cur_area = list.visible;
  472.         scroll.position = list.first;
  473.         scroll.all_redraw = 0;
  474.         scroll.start_x = list.x + list.w;
  475.         scroll.start_y = list.y;
  476.         scroll.size_y = list.h;
  477.         scrollbar_v_draw(#scroll);
  478. }