Subversion Repositories Kolibri OS

Rev

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