Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.61 KB | None | 0 0
  1. #include "pinyin.h"
  2. #include "common.h"
  3.  
  4. void IMSwitch(void *n)
  5. /* Input method switch (en <=> zh) */
  6. {
  7.     State *s;
  8.     s = (State *)n;
  9.     if(s -> isedit)
  10.     {
  11.         s -> chinese = !s -> chinese;
  12.         s -> chipunc = s -> chinese;
  13.         //s -> fullwidth = s -> chinese;
  14.     }
  15. }
  16.  
  17. void PuncSwitch(void *n)
  18. /* Punctuation switch (en <=> zh) */
  19. {
  20.     State *s;
  21.     s = (State *)n;
  22.     if(s -> isedit && s -> chinese)
  23.     {
  24.         s -> chipunc = !s -> chipunc;
  25.     }
  26. }
  27.  
  28. void WidthSwitch(void *n)
  29. /* Width switch (fullwidth <=> halfwidth) */
  30. {
  31.     State *s;
  32.     s = (State *)n;
  33.     if(s -> isedit)
  34.     {
  35.         s -> fullwidth = !s -> fullwidth;
  36.     }
  37. }
  38.  
  39. void CheckIMSwitch(void *n)
  40. /* Check if a switch should be made */
  41. {
  42.     State *s;
  43.     s = (State *)n;
  44.    
  45.     if(s -> ischildwindow)
  46.     /* Force child window to use half-width English input */
  47.     {
  48.         s -> chinese = 0;
  49.         s -> chipunc = 0;
  50.         s -> fullwidth = 0;
  51.         return;
  52.     }
  53.    
  54.     /* Check mouse click event */
  55.     if(s -> isedit && s -> mouse . key &&
  56.        PinRA(s -> mouse . x, s -> mouse . y,
  57.              getmaxx() - 23, getmaxy() - 20, getmaxx() - 6, getmaxy() - 4))
  58.         IMSwitch(n);
  59.     if(s -> isedit && s -> mouse . key &&
  60.        PinRA(s -> mouse . x, s -> mouse . y,
  61.              getmaxx() - 40, getmaxy() - 20, getmaxx() - 24, getmaxy() - 4))
  62.         PuncSwitch(n);
  63.     if(s -> isedit && s -> mouse . key &&
  64.        PinRA(s -> mouse . x, s -> mouse . y,
  65.              getmaxx() - 57, getmaxy() - 20, getmaxx() - 41, getmaxy() - 4))
  66.         WidthSwitch(n);
  67. }
  68.  
  69. void PinyinInput(void *n)
  70. /* Pinyin input function */
  71. {
  72.     static char buf[50] = {0};
  73.     /* Buffer to save current inputed pinyin */
  74.    
  75.     //static char sheng, yun;
  76.     /* Cache to save current inputed initial consonant and vowel */
  77.    
  78.     static char last_chinese = 0;
  79.     /* Cache to save last chinese indicator */
  80.    
  81.     int i, j, k = 0;
  82.    
  83.     int new_sheng = 0, new_yun = 0;
  84.     /* Cache to save new initial consonant and vowel for comparison */
  85.    
  86.     int tmpx;
  87.     /* Save available location for input panel */
  88.    
  89.     char temp[2] = {0};
  90.     /* Temp character to show (a character and a '\0') */
  91.    
  92.     char currectpinyin = 0;
  93.     /* Check if the input is a complete pinyin */
  94.    
  95.     static char lastcurrectpinyin = 0;
  96.     /* Check last state of currectpinyin indicator */
  97.    
  98.     char code[57][5] = {
  99.     /* The main table of pinyin */
  100.         {""},
  101.         {"b"},
  102.         {"c"},
  103.         {"ch"},
  104.         {"d"},
  105.         {"f"},
  106.         {"g"},
  107.         {"h"},
  108.         {"j"},
  109.         {"k"},
  110.         {"l"},
  111.         {"m"},
  112.         {"n"},
  113.         {"p"},
  114.         {"q"},
  115.         {"r"},
  116.         {"s"},
  117.         {"sh"},
  118.         {"t"},
  119.         {"w"},
  120.         {"x"},
  121.         {"y"},
  122.         {"z"},
  123.         {"zh"},
  124.         {"a"},
  125.         {"ai"},
  126.         {"an"},
  127.         {"ang"},
  128.         {"ao"},
  129.         {"e"},
  130.         {"ei"},
  131.         {"en"},
  132.         {"eng"},
  133.         {"er"},
  134.         {"i"},
  135.         {"ia"},
  136.         {"ian"},
  137.         {"iang"},
  138.         {"iao"},
  139.         {"ie"},
  140.         {"in"},
  141.         {"ing"},
  142.         {"iong"},
  143.         {"iu"},
  144.         {"o"},
  145.         {"ong"},
  146.         {"ou"},
  147.         {"u"},
  148.         {"ua"},
  149.         {"uai"},
  150.         {"uan"},
  151.         {"uang"},
  152.         {"ue"},
  153.         {"ui"},
  154.         {"un"},
  155.         {"uo"},
  156.         {"v"}
  157.     };
  158.    
  159.     State *s;
  160.     s = (State *)n;
  161.    
  162.     if(!s -> isedit)
  163.         return;
  164.        
  165.     if(s -> cursorx + 200 > s -> editx2)
  166.     /* If cursor follow is out of place, re-locate it into the edit area */
  167.         tmpx = s -> editx2 - 200;
  168.     else
  169.         tmpx = s -> cursorx;
  170.        
  171.     if(!s -> chinese && s -> chinese != last_chinese)
  172.     /* Re-initialize the indicators */
  173.     {
  174.         s -> ispinyin = 0;
  175.         s -> sheng = 0, s -> yun = 0;
  176.         memset(buf, 0, 50);
  177.         OutPutXY(n);
  178.         last_chinese = s -> chinese;
  179.         return;
  180.     }
  181.     else if(!s -> chinese)
  182.         return;
  183.     last_chinese = s -> chinese;
  184.     /* Save last state of chinese indicator */
  185.    
  186.     j = s -> ispinyin;
  187.     for(i = 0; i < 26; i ++)
  188.     {
  189.         if((s -> keyboard . key & 0xFF00) == s -> map . chs[i] &&
  190.             !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  191.         /* Pressed a key among 'A' to 'Z' */
  192.         {
  193.             if((s -> keyboard . modifier & LEFTSHIFT) || (s -> keyboard . modifier & RIGHTSHIFT))
  194.             /* Shift is pressed, so get a capital letter */
  195.                 buf[s -> ispinyin ++] = 'A'+i;
  196.             else
  197.             /* Save a lower-case letter */
  198.                 buf[s -> ispinyin ++] = 'a'+i;
  199.         }
  200.     }
  201.    
  202.     if((s -> keyboard . key & 0xFF00) == EQUAL &&
  203.         !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  204.     /* Set candidate pinyin list to next page */
  205.         s -> pinyinpage ++;
  206.     else if((s -> keyboard . key & 0xFF00) == MINUS &&
  207.             !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  208.     /* Set candidate pinyin list to next page */
  209.         s -> pinyinpage --;
  210.     else if((s -> keyboard . key & 0xFF00) == BACKSPACE &&
  211.             !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT)
  212.             && s -> ispinyin)
  213.     /* Backspace supported */
  214.     {
  215.         s -> ispinyin --;
  216.         buf[s -> ispinyin] = 0;
  217.         if(s -> ispinyin == 0)
  218.         {
  219.             memset(buf, 0, 50);
  220.             CleanUp(n);
  221.             if(!s -> dotted)
  222.             {
  223.                 setactivepage(!s -> page);
  224.                 DrawMainWindow(n);
  225.                 DrawStatusBox(n);
  226.                 DoEvent(s -> redraw, n);
  227.                 CleanUp(n);
  228.                 setvisualpage(!s -> page);
  229.                 s -> page = !s -> page;
  230.             }
  231.             else
  232.             {
  233.                 OutPutXY(n);
  234.                 CleanUp(n);
  235.             }
  236.             return;
  237.         }
  238.         j --;
  239.         setfillstyle(SOLID_FILL, 7);
  240.         MouseAway(n);
  241.         bar(tmpx + 4 + 8 * j, s -> cursory + 20, tmpx + 12 + 8 * j, s -> cursory + 32);
  242.         MouseAway(n);
  243.     }
  244.     else if(s -> keyboard . key != -1 && j == s -> ispinyin && s -> ispinyin)
  245.     /* Hit other keys */
  246.     {
  247.         if((s -> keyboard . key & 0xFF00) == ENTER &&
  248.             !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  249.         /* Hit enter, treat as English input */
  250.             Insert(n, buf);
  251.        
  252.         s -> ispinyin = 0;
  253.         //s -> sheng = 0, s -> yun = 0;
  254.         memset(buf, 0, 50);
  255.         /* Reset all flags */
  256.         if(j > 0)
  257.         {
  258.             CleanUp(n);
  259.             if(!s -> dotted)
  260.             {
  261.                 setactivepage(!s -> page);
  262.                 DrawMainWindow(n);
  263.                 DrawStatusBox(n);
  264.                 DoEvent(s -> redraw, n);
  265.                 CleanUp(n);
  266.                 setvisualpage(!s -> page);
  267.                 s -> page = !s -> page;
  268.             }
  269.             else
  270.             {
  271.                 OutPutXY(n);
  272.                 CleanUp(n);
  273.             }
  274.         }
  275.         //s -> keyboard . key = -1;
  276.         return;
  277.     }
  278.     else if(j == 0 && s -> ispinyin == 1)
  279.     /* First letter entered, draw the input panel */
  280.     {
  281.         MouseAway(n);
  282.        
  283.         DrawButton(n, tmpx, s -> cursory + 16, tmpx + 200, s -> cursory + 56, buf);
  284.         SetColor(8);
  285.         line(tmpx + 6, s -> cursory + 34, tmpx + 194, s -> cursory + 34);
  286.         SetColor(15);
  287.         line(tmpx + 6, s -> cursory + 35, tmpx + 194, s -> cursory + 35);
  288.         MouseAway(n);
  289.     }
  290.     else if(j != s -> ispinyin)
  291.     /* Press an alphabets, saving it to buffer */
  292.     {
  293.         temp[0] = buf[j];
  294.         OutTextxy(n, tmpx + 4 + 8 * j, s -> cursory + 20, temp, 0);
  295.     }
  296.    
  297.     for(i = 1; i < 24; i ++)
  298.     /* Find the initial consonant that inputed */
  299.     {
  300.         for(j = 0; j < strlen(code[i]); j ++)
  301.         {
  302.             if(code[i][j] != buf[j])
  303.             /* Not matched */
  304.                 break;
  305.             else if(j == strlen(code[i]) - 1)
  306.             /* Fully matched */
  307.             {
  308.                 currectpinyin ++;
  309.                 new_sheng = i;
  310.                 k = j + 1;
  311.             }
  312.         }
  313.     }
  314.     if(new_sheng != s -> sheng)
  315.     /* Initial consonant changed, reset page count */
  316.     {
  317.         s -> sheng = new_sheng;
  318.         s -> pinyinpage = 0;
  319.     }
  320.    
  321.     for(i = 24; i < 57; i ++)
  322.     /* Looking for inputed vowel */
  323.     {
  324.         for(j = 0; j < strlen(code[i]); j ++)
  325.         {
  326.             if(code[i][j] != buf[j + k])
  327.                 break;
  328.             else if(j == strlen(code[i]) - 1)
  329.             {
  330.                 currectpinyin ++;
  331.                 new_yun = i;
  332.             }
  333.         }
  334.     }
  335.     if(new_yun != s -> yun)
  336.     /* Vowel changed, reset the page count */
  337.     {
  338.         s -> yun = new_yun;
  339.         s -> pinyinpage = 0;
  340.     }
  341.    
  342.     if(currectpinyin == 1 && currectpinyin != lastcurrectpinyin)
  343.     {
  344.         MouseAway(n);
  345.         DrawButton(n, tmpx, s -> cursory + 16, tmpx + 200, s -> cursory + 56, buf);
  346.         SetColor(8);
  347.         line(tmpx + 6, s -> cursory + 34, tmpx + 194, s -> cursory + 34);
  348.         SetColor(15);
  349.         line(tmpx + 6, s -> cursory + 35, tmpx + 194, s -> cursory + 35);
  350.         OutTextxy(n, tmpx + 4, s -> cursory + 20, buf, 0);
  351.         MouseAway(n);
  352.     }
  353.     lastcurrectpinyin = currectpinyin;
  354. }
  355.  
  356. void PinyinShow(void *n)
  357. /* Show candidate Chinese characters */
  358. {
  359.     State *s;
  360.     char filename[20];
  361.     /* Buffer to save dict file name */
  362.    
  363.     char buf[1500] = {0}, temp[50], tmpinsert[3] = {0};
  364.     /* Read buffer, temp buffer, and insert buffer */
  365.    
  366.     char a, b;
  367.     int yun, tmpx;
  368.     static char lastyun = -1, lastsheng = -1, lastpage = 0, lastpinyin = 0;
  369.     /* Saving last time states */
  370.    
  371.     int t = 0, i;
  372.     FILE *fp;
  373.    
  374.     s = (State *)n;
  375.     if(!s -> isedit || !s -> ispinyin)
  376.     /* Not in correct mode */
  377.     {
  378.         if(!lastpinyin)
  379.         {
  380.             lastyun = -1, lastsheng = -1;
  381.             lastpinyin = s -> ispinyin;
  382.             return;
  383.         }
  384.     }
  385.     if(lastyun == s -> yun && lastsheng == s -> sheng && lastpage == s -> pinyinpage)
  386.         if(!(!s -> ispinyin && lastpinyin))
  387.         {
  388.             lastpinyin = s -> ispinyin;
  389.             return;
  390.         }
  391.     lastyun = s -> yun, lastsheng = s -> sheng, lastpage = s -> pinyinpage;
  392.    
  393.     if(s -> cursorx + 200 > s -> editx2)
  394.         tmpx = s -> editx2 - 200;
  395.     else
  396.         tmpx = s -> cursorx;
  397.        
  398.     sprintf(filename, "pinyin\\%d.txt", s -> sheng);
  399.     /* Open dict file for reading */
  400.    
  401.     if((fp = fopen(filename, "r")) == NULL)
  402.         printf("ERROR!");
  403.     while((fscanf(fp, "%c%c|%d\n", &a, &b, &yun)) != EOF)
  404.     {
  405.         if(yun == s -> yun && t < 1500)
  406.         {
  407.             buf[t ++] = a;
  408.             buf[t ++] = b;
  409.         }
  410.         else if(t != 0)
  411.             break;
  412.     }
  413.     fclose(fp);
  414.    
  415.     if(t == 0)
  416.     /* Characters not found */
  417.     {
  418.         lastpinyin = s -> ispinyin;
  419.         return;
  420.     }
  421.     if(!s -> ispinyin && lastpinyin)
  422.     {
  423.         for(i = 1; i < 6; i ++)
  424.         /* Check if selected one of the candidates */
  425.             if((s -> keyboard . key & 0xFF00) == s -> nummap . num[i] &&
  426.                !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  427.             {
  428.                 if(s -> pinyinpage * 10 + i * 2 - 1 >= t)
  429.                     break;
  430.                 tmpinsert[0] = buf[s -> pinyinpage * 10 + i * 2 - 2];
  431.                 tmpinsert[1] = buf[s -> pinyinpage * 10 + i * 2 - 1];
  432.                 Insert(n, tmpinsert);
  433.                 s -> keyboard . key = -1;
  434.                 break;
  435.             }
  436.         if((s -> keyboard . key & 0xFF00) == SPACE &&
  437.            !(s -> keyboard . modifier & CTRL) && !(s -> keyboard . modifier & ALT))
  438.         /* Space to select the default one */
  439.         {
  440.             if(s -> pinyinpage * 10 + 1 < t)
  441.             {
  442.                 tmpinsert[0] = buf[s -> pinyinpage * 10];
  443.                 tmpinsert[1] = buf[s -> pinyinpage * 10 + 1];
  444.                 Insert(n, tmpinsert);
  445.                 s -> keyboard . key = -1;
  446.             }
  447.         }
  448.         //s -> sheng = 0, s -> yun = 0;
  449.         //lastyun = s -> yun, lastsheng = s -> sheng;
  450.         lastpinyin = s -> ispinyin;
  451.         return;
  452.     }
  453.     setfillstyle(SOLID_FILL, 7);
  454.     MouseAway(n);
  455.     bar(tmpx + 4, s -> cursory + 38, tmpx + 196, s -> cursory + 54);
  456.     MouseAway(n);
  457.     while(s -> pinyinpage * 10 >= t)
  458.         s -> pinyinpage --;
  459.     if(s -> pinyinpage < 0)
  460.         s -> pinyinpage = 0;
  461.     for(i = s -> pinyinpage * 10; i < t && i < (s -> pinyinpage + 1) * 10; i += 2)
  462.     /* Show the candidates */
  463.     {
  464.         sprintf(temp, "%d.%c%c", (i - s -> pinyinpage * 10) / 2 + 1, buf[i], buf[i + 1]);
  465.         MouseAway(n);
  466.         OutTextxy(n, tmpx + 4 + (i - s -> pinyinpage * 10) * 20, s -> cursory + 38, temp, 0);
  467.         MouseAway(n);
  468.     }
  469.     lastpinyin = s -> ispinyin;
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement