Subversion Repositories Kolibri OS

Rev

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