Subversion Repositories Kolibri OS

Rev

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