Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 4096*25
  2.  
  3. #include "../lib/font.h"
  4. #include "../lib/io.h"
  5. #include "../lib/gui.h"
  6. #include "../lib/list_box.h"
  7. #include "../lib/obj/box_lib.h"
  8. #include "../lib/obj/libini.h"
  9. #include "../lib/obj/iconv.h"
  10. #include "../lib/obj/proc_lib.h"
  11. #include "../lib/patterns/libimg_load_skin.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_FONT   "/sys/fonts/Tahoma.kf"
  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.04"
  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. #include "ini.h"
  56. #include "menu.h"
  57. #include "gui.h"
  58. #include "prepare_page.h"
  59.  
  60.  
  61. void InitDlls()
  62. {
  63.         load_dll(boxlib,    #box_lib_init,   0);
  64.         load_dll(libio,     #libio_init,     1);
  65.         load_dll(libimg,    #libimg_init,    1);
  66.         load_dll(libini,    #lib_init,       1);
  67.         load_dll(iconv_lib, #iconv_open,     0);
  68.         load_dll(Proc_lib,  #OpenDialog_init,0);
  69. }
  70.  
  71.  
  72. void main()
  73. {      
  74.         InitDlls();
  75.        
  76.         OpenDialog_init stdcall (#o_dialog);
  77.        
  78.         font.color      = 0;
  79.         font.bg_color   = 0xFFFFFF;
  80.        
  81.         font.load(DEFAULT_FONT);
  82.        
  83.         if (!font.data) {
  84.                 io.run("/sys/@notify","'Error: Font is not loaded.' -E");
  85.                 ExitProcess();
  86.         }
  87.        
  88.         Libimg_LoadImage(#skin, abspath("toolbar.png"));
  89.        
  90.         LoadIniSettings();
  91.        
  92.         OpenFile(#param);
  93.         list.no_selection = true;
  94.         SetEventMask(10000000000000000000000001100111b);
  95.         loop()
  96.         {
  97.                 switch(WaitEvent())
  98.                 {
  99.                         case evMouse:
  100.                                 HandleMouseEvent();
  101.                                 break;
  102.                         case evKey:
  103.                                 HandleKeyEvent();
  104.                                 break;
  105.                         case evButton:
  106.                                 HandleButtonEvent();
  107.                                 break;
  108.                         case evReDraw:
  109.                                 if (action_buf) {
  110.                                         OpenFile(#param);
  111.                                         PreparePage();
  112.                                         action_buf = false;
  113.                                 };
  114.                                 draw_window();
  115.                 }
  116.         }
  117. }
  118.  
  119.  
  120. void HandleButtonEvent()
  121. {
  122.        
  123.         byte btn = GetButtonID();
  124.         if (btn==1) {
  125.                 SaveIniSettings();
  126.                 ExitProcess();
  127.         }
  128.         btn-=10;
  129.         switch(btn)
  130.         {
  131.                 case OPEN_FILE:
  132.                         EventOpenFile();
  133.                         break;
  134.                 case MAGNIFY_PLUS:
  135.                         EventMagnifyPlus();
  136.                         break;
  137.                 case MAGNIFY_MINUS:
  138.                         EventMagnifyMinus();
  139.                         break;
  140.                 case CHANGE_ENCODING:
  141.                         EventChangeEncoding();
  142.                         break;
  143.                 case RUN_EDIT:
  144.                         EventRunEdit();
  145.                         break;
  146.                 case SHOW_INFO:
  147.                         EventShowInfo();
  148.                         break;
  149.         }
  150. }
  151.  
  152.  
  153. void HandleKeyEvent()
  154. {
  155.         if (help_opened) {
  156.                 help_opened = false;
  157.                 DrawPage();
  158.                 return;
  159.         }
  160.         GetKeys();
  161.         if (key_scancode==059) {
  162.                 EventShowInfo();
  163.                 return;
  164.         }
  165.         if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
  166.                 switch (key_scancode)
  167.                 {
  168.                         case 024:
  169.                                 EventOpenFile();
  170.                                 break;
  171.                         case SCAN_CODE_UP:
  172.                                 EventMagnifyPlus();
  173.                                 break;
  174.                         case SCAN_CODE_DOWN:
  175.                                 EventMagnifyMinus();
  176.                                 break;
  177.                         case 018:
  178.                                 EventRunEdit();
  179.                                 break;
  180.                         case SCAN_CODE_TAB:
  181.                                 EventChangeEncoding();
  182.                                 break;
  183.                 }
  184.                 return;
  185.         }
  186.         if (list.ProcessKey(key_scancode))
  187.                 DrawPage();
  188. }
  189.  
  190.  
  191. void HandleMouseEvent()
  192. {
  193.         mouse.get();
  194.         list.wheel_size = 7;
  195.         if (list.MouseScroll(mouse.vert)) {
  196.                 DrawPage();
  197.                 return;
  198.         }
  199.         scrollbar_v_mouse (#scroll);
  200.         if (list.first != scroll.position) {
  201.                 list.first = scroll.position;
  202.                 DrawPage();
  203.         }
  204. }
  205.  
  206.  
  207. /* ----------------------------------------------------- */
  208.  
  209. void EventOpenFile()
  210. {
  211.         OpenDialog_start stdcall (#o_dialog);
  212.         OpenFile(#openfile_path);
  213.         PreparePage();
  214. }
  215.  
  216. void EventMagnifyPlus()
  217. {
  218.         font.size.text++;
  219.         if(!font.changeSIZE())
  220.                 font.size.text--;
  221.         else
  222.                 PreparePage();
  223. }
  224.  
  225. void EventMagnifyMinus()
  226. {
  227.         font.size.text--;
  228.         if(!font.changeSIZE())
  229.                 font.size.text++;
  230.         else
  231.                 PreparePage();
  232. }
  233.  
  234. void EventRunEdit()
  235. {
  236.         io.run(DEFAULT_EDITOR, #param);
  237. }
  238.  
  239. void EventChangeEncoding()
  240. {
  241.         CreateThread(#menu_rmb,#stak+4092);
  242. }
  243.  
  244. void EventShowInfo() {
  245.         help_opened = true;
  246.         DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
  247.         WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
  248.         WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
  249. }
  250.  
  251. /* ------------------------------------------- */
  252.  
  253.  
  254. void OpenFile(dword f_path)
  255. {
  256.         int tmp;
  257.         if (ESBYTE[f_path]) {
  258.                 strcpy(#param, f_path);
  259.                 io.read(#param);
  260.                 strcpy(#title, #param);
  261.                 strcat(#title, " - Text Reader");
  262.         }
  263.         else {
  264.                 if (list.count) return;
  265.                 io.buffer_data = INTRO_TEXT;
  266.                 strcpy(#title, "Text Reader");
  267.         }
  268.         if (encoding!=CH_CP866) ChangeCharset(charsets[encoding], "CP866", io.buffer_data);
  269.         list.KeyHome();
  270.         list.ClearList();
  271. }
  272.  
  273. void draw_window()
  274. {
  275.         DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title);
  276.         GetProcessInfo(#Form, SelfInfo);
  277.         if (Form.status_window>2) return;
  278.  
  279.         if (Form.width  < 200) { MoveSize(OLD,OLD,200,OLD); return; }
  280.         if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
  281.        
  282.         DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
  283.         DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
  284.        
  285.         DrawToolbarButton(OPEN_FILE,       8);
  286.         DrawToolbarButton(MAGNIFY_PLUS,    42);
  287.         DrawToolbarButton(MAGNIFY_MINUS,   67);
  288.         DrawToolbarButton(CHANGE_ENCODING, 101);
  289.         DrawToolbarButton(RUN_EDIT,        135);
  290.         DrawToolbarButton(SHOW_INFO,       Form.cwidth - 34);
  291.        
  292.         if ((Form.cwidth-scroll.size_x-1 == list.w) &&
  293.                 (Form.cheight-TOOLBAR_H == list.h) &&
  294.                 (list.count)
  295.         )
  296.         {
  297.                 DrawPage();
  298.         } else {
  299.                 PreparePage();
  300.         }
  301.         DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
  302. }
  303.  
  304. void DrawPage()
  305. {
  306.         _PutImage(list.x,list.y,list.w,list.h,list.first*list.item_h*list.w*3 + font.buffer);
  307.         DrawScroller();
  308. }