Subversion Repositories Kolibri OS

Rev

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

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