Subversion Repositories Kolibri OS

Rev

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