Subversion Repositories Kolibri OS

Rev

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