Subversion Repositories Kolibri OS

Rev

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