Subversion Repositories Kolibri OS

Rev

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

  1. //Calypte 0.35 - Leency
  2. //Calypte 0.15 - Punk Joker
  3.  
  4. /*
  5. TODO
  6. - word wrap
  7. - draw by line
  8. - text selection
  9. */
  10.  
  11. #define MEMSIZE 1024*200
  12.  
  13. #ifndef AUTOBUILD
  14.         #include "lang.h--"
  15. #endif
  16.  
  17. //===================================================//
  18. //                                                   //
  19. //                       LIB                         //
  20. //                                                   //
  21. //===================================================//
  22.  
  23. #include "../lib/kolibri.h"
  24. #include "../lib/fs.h"
  25. #include "../lib/gui.h"
  26. #include "../lib/list_box.h"
  27. #include "../lib/menu.h"
  28. #include "../lib/collection.h"
  29.  
  30. #include "../lib/obj/iconv.h"
  31. #include "../lib/obj/box_lib.h"
  32. #include "../lib/obj/libio.h"
  33. #include "../lib/obj/proc_lib.h"
  34. #include "../lib/obj/librasterworks.h"
  35.  
  36. #include "../lib/patterns/simple_open_dialog.h"
  37.  
  38. //===================================================//
  39. //                                                   //
  40. //                       DATA                        //
  41. //                                                   //
  42. //===================================================//
  43.  
  44. char default_dir[] = "/rd/1";
  45. od_filter filter2 = { 8, "TXT\0\0" };
  46.  
  47. /*=========  MENU  ==========*/
  48. ?define MENU1 "File"
  49. ?define MENU2 "Encoding"
  50. ?define MENU3 "Reopen"
  51.  
  52. char menu_file_list[] =
  53. "Open
  54. Close
  55. Properties
  56. Exit";
  57.  
  58. char menu_encoding_list[] =
  59. "UTF-8
  60. KOI8-RU
  61. CP1251
  62. CP1252
  63. ISO8859-5
  64. CP866";
  65.  
  66. char menu_reopen_list[] =
  67. "Tinypad
  68. TextEdit
  69. TextRead
  70. WebView
  71. FB2Read
  72. HexView";
  73.  
  74. enum
  75. {
  76.         MENU_ID_FILE=10,
  77.         FILE_SUBMENU_ID_OPEN=10,
  78.         FILE_SUBMENU_ID_CLOSE,
  79.         FILE_SUBMENU_ID_PROPERTIES,
  80.         FILE_SUBMENU_ID_EXIT,
  81.  
  82.         MENU_ID_ENCODING=20,
  83.  
  84.         MENU_ID_REOPEN=30,
  85.         FILE_SUBMENU_ID_TINYPAD=30,
  86.         FILE_SUBMENU_ID_TEXTEDIT,
  87.         FILE_SUBMENU_ID_TEXTREAD,
  88.         FILE_SUBMENU_ID_WEBVIEW,
  89.         FILE_SUBMENU_ID_FB2READ,
  90.         FILE_SUBMENU_ID_HEXVIEW
  91. };
  92.  
  93. int menu_file_x = 6;
  94. int menu_encoding_x = NULL;
  95. int menu_reopen_x = NULL;
  96. /*======== MENU END ==========*/
  97.  
  98. #define TITLE "Calypte v0.38 dev"
  99. char win_title[4096] = TITLE;
  100.  
  101. #define TOPPANELH 23
  102. #define BOTPANELH 10
  103. #define WIN_W 750
  104. #define WIN_H 550
  105. #define SCROLL_SIZE 15
  106.  
  107. proc_info Form;
  108. llist rows;
  109.  
  110. int encoding;
  111.        
  112. dword old_width,old_height;
  113.  
  114. dword bufpointer;
  115. dword bufsize;
  116.  
  117. scroll_bar scroll_v = { SCROLL_SIZE,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};
  118. scroll_bar scroll_h = { SCROLL_SIZE,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};
  119.  
  120. collection s;
  121.  
  122. //===================================================//
  123. //                                                   //
  124. //                       CODE                        //
  125. //                                                   //
  126. //===================================================//
  127.  
  128. void main()
  129. {  
  130.         int id;
  131.  
  132.         load_dll(boxlib,    #box_lib_init,   0);
  133.         load_dll(libio,     #libio_init,     1);
  134.         //load_dll(libini,    #lib_init,       1);
  135.         load_dll(iconv_lib, #iconv_open,     0);
  136.         load_dll(Proc_lib,  #OpenDialog_init,0);
  137.         load_dll(librasterworks,  #rasterworks_drawText,0);
  138.         OpenDialog_init stdcall (#o_dialog);
  139.  
  140.         if (param)
  141.         {
  142.                 draw_window();
  143.                 OpenFile(#param);
  144.                 Prepare();
  145.                 DrawText();
  146.         }
  147.        
  148.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  149.  
  150.         loop()
  151.         {
  152.           switch(WaitEvent())
  153.           {
  154.                 case evMouse:
  155.                         mouse.get();
  156.                         rows.wheel_size = 3;
  157.                         if (rows.MouseScroll(mouse.vert))
  158.                         {
  159.                                 DrawText();
  160.                         }
  161.                         break;
  162.                
  163.                 case evButton:
  164.                         id=GetButtonID();              
  165.                         if (id==1)
  166.                         {
  167.                                 ExitProcess();
  168.                         }
  169.                         if (id==MENU_ID_FILE)
  170.                         {
  171.                                 EventShowMenu(menu_file_x, #menu_file_list, MENU_ID_FILE, NULL);
  172.                         }
  173.                         if (id==MENU_ID_ENCODING)
  174.                         {
  175.                                 EventShowMenu(menu_encoding_x, #menu_encoding_list, MENU_ID_ENCODING, encoding+1);
  176.                         }
  177.                         if (id==MENU_ID_REOPEN)
  178.                         {
  179.                                 EventShowMenu(menu_reopen_x, #menu_reopen_list, MENU_ID_REOPEN, NULL);
  180.                         }
  181.                         break;
  182.                
  183.                 case evKey:
  184.                         GetKeys();
  185.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL)
  186.                         {
  187.                                 if (key_scancode == SCAN_CODE_KEY_O)
  188.                                 {
  189.                                         EventOpenFile();
  190.                                 }
  191.                                 break;
  192.                         }
  193.                         if (rows.ProcessKey(key_scancode))
  194.                         {
  195.                                 DrawText();
  196.                         }
  197.                         break;
  198.                  
  199.                  case evReDraw:
  200.                         if (menu.list.cur_y)
  201.                         {
  202.                                 EventMenuClick();
  203.                         }
  204.                         draw_window();
  205.                         break;
  206.           }
  207.    }
  208. }
  209.  
  210. int DrawMenuButton(dword x,y,id,text)
  211. {
  212.         int textlen = strlen(text)*8;
  213.         int padding = 12;
  214.         DefineHiddenButton(x, y, textlen+padding+padding, TOPPANELH-2, id);
  215.         WriteText(x+padding,y+4, 0x90, MixColors(system.color.work, system.color.work_text, 70), text);
  216.         return textlen+padding+padding;
  217. }
  218.  
  219. void draw_window()
  220. {
  221.         system.color.get();
  222.         DefineAndDrawWindow(screen.width-WIN_W/2,screen.height-WIN_H/2,WIN_W,WIN_H,0x73,0xFFFFFF,#win_title,0);
  223.         GetProcessInfo(#Form, SelfInfo);
  224.         DrawBar(0, 0, Form.cwidth, TOPPANELH-1, system.color.work);
  225.         DrawBar(0, TOPPANELH-1, Form.cwidth, 1, system.color.work_dark);
  226.         DrawBar(0, Form.cheight-BOTPANELH, Form.cwidth, BOTPANELH, system.color.work);
  227.        
  228.         menu_encoding_x = menu_file_x + DrawMenuButton(menu_file_x, 0, MENU_ID_FILE, MENU1);
  229.         menu_reopen_x = menu_encoding_x + DrawMenuButton(menu_encoding_x, 0, MENU_ID_ENCODING, MENU2);
  230.         DrawMenuButton(menu_reopen_x, 0, MENU_ID_REOPEN, MENU3);
  231.  
  232.         if (old_width!=Form.width) || (old_height!=Form.height)
  233.         {
  234.                 old_width = Form.width;
  235.                 old_height = Form.height;
  236.                
  237.                 rows.no_selection = true;
  238.                 rows.SetFont(8, 14, 0x90);
  239.                 rows.SetSizes(0, TOPPANELH, Form.cwidth - SCROLL_SIZE, Form.cheight - TOPPANELH - BOTPANELH, 20);
  240.                 rows.column_max = rows.w / rows.font_w;
  241.  
  242.                 if (bufpointer)
  243.                 {
  244.                         Prepare();
  245.                 }
  246.                 rows.CheckDoesValuesOkey();
  247.         }
  248.         DrawRectangle(rows.x+rows.w-1, rows.y, SCROLL_SIZE, rows.h-1, 0xEEEeee);
  249.         DrawText();
  250. }
  251.  
  252. void OpenFile(dword _path)
  253. {
  254.         strcpy(#param, _path);
  255.         sprintf(#win_title, "%s - %s", TITLE, #param);
  256.         rows.KeyHome();
  257.         file_size stdcall (#param);
  258.         bufsize = EBX;
  259.         if (bufsize)
  260.         {
  261.                 bufpointer = mem_Free(bufpointer);
  262.                 bufpointer = mem_Alloc(bufsize);
  263.                 if (ReadFile(0, bufsize, bufpointer, #param) != 0)
  264.                 {
  265.                         bufpointer = 0;
  266.                         notify("'Error opening file'-E");
  267.                         return;
  268.                 }
  269.         }
  270.         if (encoding!=CH_CP866)
  271.         {
  272.                 ChangeCharset(charsets[encoding], "CP866", bufpointer);
  273.         }
  274. }
  275.  
  276. enum
  277. {
  278.         PARSE_CALCULATE_ROWS_COUNT,
  279.         PARSE_DRAW_PREPARE,
  280. };
  281.  
  282. void Parse(int mode)
  283. {
  284.         int pos=0;
  285.         int sub_pos=0;
  286.         int len_str = 0;
  287.         bool do_eof = false;
  288.         word bukva[2];
  289.  
  290.         while(1)
  291.         {
  292.                 while(1)
  293.                 {
  294.                         bukva = DSBYTE[bufpointer+pos+len_str];
  295.                         if (bukva=='\0')
  296.                         {
  297.                                 do_eof = true;
  298.                                 break;
  299.                         }
  300.                         if (bukva==0x0a)
  301.                         {
  302.                                 break;
  303.                         }
  304.                         else
  305.                         {
  306.                                 len_str++;
  307.                         }
  308.                 }
  309.                 if (len_str<=rows.column_max)
  310.                 {
  311.                         if (mode==PARSE_DRAW_PREPARE)
  312.                         {
  313.                                 s.addn(bufpointer+pos, len_str);
  314.                         }
  315.                         pos += len_str+1;
  316.                 }
  317.                 else
  318.                 {
  319.                         if (mode==PARSE_DRAW_PREPARE)
  320.                         {
  321.                                 s.addn(bufpointer+pos, rows.column_max);
  322.                         }
  323.                         pos += rows.column_max;
  324.                 }
  325.                 sub_pos++;
  326.                 if (mode==PARSE_CALCULATE_ROWS_COUNT) && (do_eof)
  327.                 {
  328.                         break;
  329.                 }
  330.                 if (mode==PARSE_DRAW_PREPARE) && (pos>=bufsize-1)
  331.                 {
  332.                         break;
  333.                 }
  334.                 len_str = 0;
  335.         }
  336.         if (mode == PARSE_CALCULATE_ROWS_COUNT)
  337.         {
  338.                 rows.count = sub_pos;
  339.                 Parse(PARSE_DRAW_PREPARE);
  340.         }
  341. }
  342.  
  343. void Prepare()
  344. {
  345.         Parse(PARSE_CALCULATE_ROWS_COUNT);
  346. }
  347.  
  348. void DrawText()
  349. {
  350.         int i=0, top;
  351.  
  352.         if (rows.count<rows.visible)
  353.         {
  354.                 top = rows.count;
  355.         }
  356.         else
  357.         {
  358.                 if (rows.count-rows.first<=rows.visible)
  359.                 {
  360.                         top = rows.count-rows.first-1;
  361.                 }
  362.                 else
  363.                 {
  364.                         top = rows.visible;
  365.                 }
  366.         }
  367.  
  368.         if (bufpointer)
  369.         {
  370.                 for (i=0; i<top; i++)
  371.                 {
  372.                         DrawBar(0, i*rows.item_h+TOPPANELH, rows.w, rows.item_h, 0xFFFFFF);
  373.                         WriteText(2, i*rows.item_h+TOPPANELH, 0x90, 0x000000, s.get(i+rows.first));
  374.                 }
  375.         }
  376.         DrawBar(0, i*rows.item_h+rows.y, rows.w, -i*rows.item_h + rows.h, 0xFFFfff);
  377.         DrawVerticalScroll();
  378. }
  379.  
  380. void DrawVerticalScroll()
  381. {
  382.         scroll_v.max_area = rows.count;
  383.         scroll_v.cur_area = rows.visible;
  384.         scroll_v.position = rows.first;
  385.         scroll_v.start_y = rows.y;
  386.         scroll_v.size_y = rows.h;
  387.         scroll_v.start_x = rows.w + rows.x -1;
  388.         scroll_v.all_redraw = 0;
  389.         scrollbar_v_draw(#scroll_v);
  390. }
  391.  
  392. //===================================================//
  393. //                                                   //
  394. //                      EVENTS                       //
  395. //                                                   //
  396. //===================================================//
  397.  
  398. void EventMenuClick()
  399. {
  400.         switch(menu.list.cur_y)
  401.         {
  402.                 //File
  403.                 case FILE_SUBMENU_ID_OPEN:
  404.                         EventOpenFile();
  405.                         break;
  406.                 case FILE_SUBMENU_ID_CLOSE:
  407.                         EventCloseFile();
  408.                         break;
  409.                 case FILE_SUBMENU_ID_PROPERTIES:
  410.                         EventShowFileProperties();
  411.                         break;
  412.                 case FILE_SUBMENU_ID_EXIT:
  413.                         ExitProcess();
  414.                         break;
  415.                 //Encoding
  416.                 case MENU_ID_ENCODING...MENU_ID_ENCODING+9:
  417.                         EventChangeEncoding(menu.list.cur_y-MENU_ID_ENCODING);
  418.                         break;
  419.                 //Reopen
  420.                 case FILE_SUBMENU_ID_TINYPAD:
  421.                         EventOpenFileInAnotherProgram("/sys/tinypad");
  422.                         break;
  423.                 case FILE_SUBMENU_ID_TEXTEDIT:
  424.                         EventOpenFileInAnotherProgram("/sys/develop/t_edit");
  425.                         break;
  426.                 case FILE_SUBMENU_ID_TEXTREAD:
  427.                         EventOpenFileInAnotherProgram("/sys/txtread");
  428.                         break;
  429.                 case FILE_SUBMENU_ID_WEBVIEW:
  430.                         EventOpenFileInAnotherProgram("/sys/network/webview");
  431.                         break;
  432.                 case FILE_SUBMENU_ID_FB2READ:
  433.                         EventOpenFileInAnotherProgram("/sys/fb2read");
  434.                         break;
  435.                 case FILE_SUBMENU_ID_HEXVIEW:
  436.                         EventOpenFileInAnotherProgram("/sys/develop/heed");
  437.                         break;
  438.         }
  439.         menu.list.cur_y = 0;
  440. }
  441.  
  442. void EventShowMenu(dword _menu_item_x, _menu_list, _id, _selected)
  443. {
  444.         menu.selected = _selected;
  445.         menu.show(Form.left+5 + _menu_item_x, Form.top+skin_height + TOPPANELH, 140, _menu_list, _id);
  446. }
  447.  
  448. void EventOpenFile()
  449. {
  450.         OpenDialog_start stdcall (#o_dialog);
  451.         if (o_dialog.status)
  452.         {
  453.                 OpenFile(#openfile_path);
  454.                 Prepare();
  455.                 draw_window();
  456.         }
  457. }
  458.  
  459. void EventCloseFile()
  460. {
  461.         if (bufpointer)
  462.         {
  463.                 s.drop();
  464.                 bufpointer = mem_Free(bufpointer);
  465.                 strcpy(#win_title, TITLE);
  466.                 draw_window();
  467.         }
  468. }
  469.  
  470. void EventShowFileProperties()
  471. {
  472.         char ss_param[4096];
  473.         if (bufpointer)
  474.         {
  475.                 sprintf(#ss_param, "-p %s", #param);
  476.                 RunProgram("/sys/File managers/Eolite", #ss_param);
  477.         }
  478. }
  479.  
  480. void EventChangeEncoding(dword id)
  481. {
  482.         encoding = id;
  483.         OpenFile(#openfile_path);
  484.         Prepare();
  485.         draw_window();
  486. }
  487.  
  488. void EventOpenFileInAnotherProgram(dword _app)
  489. {
  490.         RunProgram(_app, #param);
  491. }
  492.  
  493.  
  494.  
  495. stop:
  496.