Subversion Repositories Kolibri OS

Rev

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