Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "..\lib\kolibri.h"
  2. #include "..\lib\strings.h"
  3. #include "..\lib\file_system.h"
  4. #include "..\lib\mem.h"
  5. #include "..\lib\dll.h"
  6. #include "..\lib\edit_box_lib.h"
  7.  
  8. char title[]= "Dictionary v1.0";
  9. char direction[] = "Translate direction:";
  10. char translate_caption[] = "Translate";
  11. char dict_not_found[] = "Dictionary not found";
  12. char dict_opened[] = "Dictionary loaded";
  13. char empty_word[] = "Type a word to translate";
  14. //char direction[] = "Íàïðàâëåíèå ïåðåâîäà:"w;
  15.  
  16.  
  17. int mouse_dd;
  18. edit_box edit1= {200,20,16,0xffffff,0x94AECE,0x94AECE,0x94AECE,0,248,#edword,#mouse_dd,100000000000010b};
  19.  
  20. system_colors sc;
  21. proc_info Form;
  22.  
  23. char edword[4096], translate_result[4096], search_word[4096], cur_dict[4096];
  24. #define DEFAULT_DICT_DIRECROTY "dictionaries/"
  25. #define DEFAULT_DICT "eng - rus.dict";
  26. dword files_buf;
  27. dword buff, fsize;
  28.  
  29.  
  30. void main()
  31. {  
  32.         int id;
  33.         word key;
  34.    
  35.         mem_Init();
  36.         load_dll2(boxlib, #edit_box_draw,0);
  37.        
  38.        
  39.         program_path[strrchr(#program_path, '/')] = 0; //îáðåçàåì å¸ óðë äî ïîñëåäíåãî /
  40.         strcpy(#program_path+strlen(#program_path), DEFAULT_DICT_DIRECROTY);
  41.         SetCurDir(#program_path);
  42.  
  43.         strcpy(#cur_dict, DEFAULT_DICT);
  44.         OpenDictionary(#cur_dict);
  45.        
  46.         if (param)
  47.         {
  48.                 strcpy(#edword, #param);
  49.                 edit1.size=edit1.pos=strlen(#edword);
  50.                 Translate();
  51.         }
  52.        
  53.         SetEventMask(0x27);
  54.         loop()
  55.         {
  56.                 switch(WaitEvent())
  57.                 {
  58.                 case evMouse:
  59.                         edit_box_mouse stdcall (#edit1);
  60.                         break;
  61.  
  62.                 case evButton:
  63.             id=GetButtonID();              
  64.             if (id==1) ExitProcess();
  65.                         if (id==10)
  66.                         {
  67.                                 Translate();
  68.                                 DrawTranslation();
  69.                         }
  70.  
  71.                         if (id==11)
  72.                         {
  73.                                 ShowDictList();
  74.                                 break;
  75.                         }
  76.                         if (id==12)
  77.                         {
  78.                                 Draw_Window();
  79.                                 break;
  80.                         }
  81.  
  82.                         if (id>=20)
  83.                         {
  84.                                 strcpy(#cur_dict, id-20*304+files_buf+72);
  85.                                 OpenDictionary(#cur_dict);
  86.                         }
  87.                         break;
  88.  
  89.         case evKey:
  90.                         key = GetKey();
  91.                         IF (key==013) //Enter
  92.                         {
  93.                                 Translate();
  94.                                 DrawTranslation();
  95.                         }
  96.  
  97.                         EAX=key<<8;
  98.                         edit_box_key stdcall(#edit1);
  99.                         break;
  100.                        
  101.          case evReDraw:
  102.                         Draw_Window();
  103.                         break;
  104.       }
  105.    }
  106. }
  107.  
  108.  
  109. void Draw_Window()
  110. {
  111.         sc.get();
  112.         DefineAndDrawWindow(215,120,400,250,0x73,sc.work,#title);
  113.         GetProcessInfo(#Form, SelfInfo);
  114.         if (Form.status_window>2) return; //åñëè ñâåðíóòî â çàãîëîâîê, íè÷åãî íå ðèñóåì
  115.         if (Form.height<140) MoveSize(OLD,OLD,OLD,140);
  116.         if (Form.width<400) MoveSize(OLD,OLD,400,OLD);
  117.  
  118.         DrawBar(0, 0, Form.width-9, 69, sc.work);
  119.         edit1.focus_border_color=sc.work_graph;
  120.         edit1.width=Form.width-edit1.left-edit1.left-9;
  121.         edit_box_draw stdcall(#edit1); //ðèñóåì ñòðîêó àäðåñà
  122.         DefineButton(edit1.left+edit1.width-80,35, 80,20, 10, sc.work_button);
  123.         WriteText(edit1.left+edit1.width-80+14,35+7,0x80,sc.work_button_text,#translate_caption, 0);
  124.         DrawBar(0, 69, Form.width-9, 1, sc.work_graph);
  125.        
  126.         WriteText(edit1.left,35+7,0x80,sc.work_text,#direction, 0);
  127.         DefineButton(edit1.left+130,35, 120,20, 11, sc.work_button);
  128.                 WriteText(edit1.left+130+112,35+7,0x80,sc.work_button_text,"\x19", 0);
  129.         DrawBar(edit1.left+130+1,36,  107,19, 0xFFFFFF);
  130.                 WriteText(edit1.left+130+8,35+7,0x80,0x000000,#cur_dict, 0);
  131.  
  132.  
  133.         DrawTranslation();
  134. }
  135.  
  136.  
  137. void OpenDictionary(dword dict_path)
  138. {
  139.         dword j, words_count=0;
  140.        
  141.         mem_Free(buff);
  142.         buff = mem_Alloc(3500576);
  143.         ReadFile(0, 3500576, buff, dict_path);
  144.         IF (EAX<>6)
  145.         {
  146.                 fsize=0;
  147.                 strcpy(#translate_result, #dict_not_found);
  148.                 Draw_Window();
  149.                 return;
  150.         }
  151.         fsize=EBX;
  152.        
  153.         strcpy(#search_word, dict_path);
  154.         strcpy(#translate_result, #dict_opened);
  155.         Draw_Window();
  156. }
  157.  
  158. void ShowDictList()
  159. {
  160.         int j, fcount=10;
  161.        
  162.         mem_Free(files_buf);
  163.         files_buf= mem_Alloc(32);
  164.         ReadDir(0, files_buf, #program_path);
  165.         fcount=ESDWORD[files_buf+8];
  166.         mem_Free(files_buf);
  167.         files_buf = mem_Alloc(fcount+1*304+32);
  168.        
  169.         ReadDir(fcount, files_buf, #program_path);
  170.        
  171.         fcount-=2;
  172.         mem_Move(files_buf,files_buf+608,fcount*304);
  173.                
  174.        
  175.         DefineButton(0,0, Form.width,Form.height, 12+BT_HIDE+BT_NOFRAME, sc.work_button);
  176.         for (j=0; j<fcount; j++;)
  177.         {
  178.                 DefineButton(edit1.left+130,j+1*20+35, 107,20, 20+j, sc.work_button);
  179.                 WriteText(edit1.left+130+8,j+1*20+35+7,0x80,sc.work_button_text, j*304+files_buf+72, 0);
  180.         }
  181. }
  182.  
  183. void Translate()
  184. {
  185.         dword tj;
  186.         char w_native[100], w_translation[100], bukva[1];
  187.        
  188.         byte InfType;
  189.         #define NATIVE_WORD 0
  190.         #define TRANSLATION 1
  191.         #define IGNORE      2
  192.        
  193.         if (!fsize) return;
  194.                
  195.         translate_result = 0;
  196.         strcpy(#search_word, #edword);
  197.         strupr(#search_word);
  198.        
  199.         if (!edword)
  200.         {
  201.                 strcpy(#translate_result, #empty_word);
  202.                 return;
  203.         }
  204.  
  205.         for (tj=0; tj<fsize; tj++;)
  206.         {  
  207.                 bukva = ESBYTE[buff+tj];
  208.                 switch (bukva)
  209.                 {
  210.                         case '"':
  211.                                 if (w_translation)
  212.                                 {
  213.                                         if (!strcmp(#w_native, #search_word))
  214.                                         {
  215.                                        
  216.                                                 if (translate_result) strcat(#translate_result, ", ");
  217.                                                 strcat(#translate_result, #w_translation);
  218.                                         }
  219.                                         else
  220.                                                 if (translate_result) return;
  221.                                                                                
  222.                                         w_translation = w_native = 0;
  223.                                 }
  224.                                 InfType = TRANSLATION;
  225.                                 break;                         
  226.                         case 0x0a:
  227.                                 InfType = NATIVE_WORD;
  228.                                 break;
  229.                         default:
  230.                                 if (InfType==NATIVE_WORD)
  231.                                 {
  232.                                         strcat(#w_native, #bukva);
  233.                                         //if (w_native<>search_word) InfType = IGNORE; //åñëè ïåðâàÿ áóêâà íå ñîâïàäàåò èãíîðèì âñ¸ ñëîâî
  234.                                 }
  235.                                 if (InfType==TRANSLATION) strcat(#w_translation, #bukva);
  236.                 }
  237.         }
  238.         if (!translate_result) strcpy(#translate_result, "Word is'nt found in the dictionary");
  239. }
  240.  
  241.  
  242. void DrawTranslation()
  243. {
  244.         int text_break=0;
  245.         char tt[4096]='';
  246.        
  247.         int y_pos=70, skin_height=GetSkinHeight();
  248.         char draw_buf[4096];
  249.         strcpy(#draw_buf, #translate_result);
  250.        
  251.         DrawBar(0, y_pos, Form.width-9, Form.height - y_pos-skin_height-4, 0xFFFFFF);
  252.         strttl(#draw_buf);
  253.         WriteText(10+1, y_pos+8, 0x90, 0x800080, #search_word, 0);
  254.         WriteText(10  , y_pos+8, 0x90, 0x800080, #search_word, 0);
  255.         while (draw_buf)
  256.         {
  257.                 text_break= Form.width/6-6;
  258.                 if (text_break>strlen(#draw_buf))
  259.                 {
  260.                         WriteText(10, y_pos+21, 0x80, 0, #draw_buf, 0);
  261.                         return;
  262.                 }
  263.                 while (draw_buf[text_break]<>' ') && (text_break>0) text_break--;
  264.                 strcpy(#tt, #draw_buf+text_break+1);
  265.                 draw_buf[text_break]=0x0;
  266.                 WriteText(10, y_pos+21, 0x80, 0, #draw_buf, 0);
  267.                 strcpy(#draw_buf, #tt);
  268.                 y_pos+=12;
  269.                 if (y_pos+24+skin_height+12>Form.height) return; //÷òîá íå çàëåçàëî íà íèæíèé îáîäîê
  270.         }
  271. }
  272.  
  273.  
  274. stop:
  275.