COMP.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:14k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*      COMP.C                                                        */
  4. /*                                                                    */
  5. /*      Copyright (c) 1995-1997  Microsoft Corporation                */
  6. /*                                                                    */
  7. /**********************************************************************/
  8. #include "windows.h"
  9. #include "imm.h"
  10. #include "resource.h"
  11. #include "imeapps.h"
  12. #define DEBUG 1
  13. LRESULT HandleStartComposition(HWND hWnd,WPARAM wParam,LPARAM lParam)
  14. {
  15.     COMPOSITIONFORM cpf;
  16.     HIMC hIMC;
  17.  
  18. #ifdef DEBUG
  19.     OutputDebugString("WM_STARTCOMPOSITIONSTRING!!!rn");
  20. #endif
  21.     if (fdwProperty & IME_PROP_SPECIAL_UI)
  22.     {
  23.         // Normally, we need to set the composition window
  24.         // position to caret position for a special UI IME
  25.     }
  26.     else if (fdwProperty & IME_PROP_AT_CARET)
  27.     {
  28.         // If an application show composition string by itself, we do not
  29.         // need to set the position of composition window for an at caret
  30.         // IME.
  31.         return 1;
  32.     }
  33.     else
  34.     {
  35.         // Normally, we need to set the composition window
  36.         // position to caret position for a near caret IME
  37.     }
  38.     hIMC = ImmGetContext(hWnd);
  39.     if (!hIMC)
  40.     {
  41.         return 1;
  42.     }
  43.     cpf.dwStyle = CFS_POINT;
  44.     cpf.ptCurrentPos.x = ptImeUIPos.x;
  45.     cpf.ptCurrentPos.y = ptImeUIPos.y;
  46.     ImmSetCompositionWindow(hIMC,&cpf);
  47.     ImmReleaseContext(hWnd,hIMC);
  48.     return 1;
  49. }
  50. LRESULT HandleEndComposition(HWND hWnd,WPARAM wParam,LPARAM lParam)
  51. {
  52. #ifdef DEBUG
  53.     OutputDebugString("WM_ENDCOMPOSITIONSTRING!!!rn");
  54. #endif
  55.     dwCompStrLen      = 0;
  56.     dwCompAttrLen     = 0;
  57.     dwCompClsLen      = 0;
  58.     dwCompReadStrLen  = 0;
  59.     dwCompReadAttrLen = 0;
  60.     dwCompReadClsLen  = 0;
  61.     dwResultStrLen      = 0;
  62.     dwResultClsLen      = 0;
  63.     dwResultReadStrLen  = 0;
  64.     dwResultReadClsLen  = 0;
  65.     InvalidateRect(hWnd,NULL,TRUE);
  66.     return 1;
  67. }
  68. void MakePaintString(HWND hWnd, LPMYSTR lpStr,DWORD dwStrLen,LPDWORD lpCls,DWORD dwClsLen,LPMYSTR lpPaintStr)
  69. {
  70.     LPMYSTR lpPaintStart = lpPaintStr;
  71.     lpPaintStr += Mylstrlen(lpPaintStr);
  72.     if (dwStrLen)
  73.     {
  74.         if (dwClsLen)
  75.         {
  76.             lpCls[127] = 0;
  77.             while (*(lpCls+1) && *lpCls < dwStrLen)
  78.             {
  79.                 DWORD dwTextLen = *(lpCls+1) - *lpCls;
  80.                 LPMYSTR lpT = lpStr + *lpCls;
  81.                 memcpy(lpPaintStr,lpT,dwTextLen * sizeof(MYCHAR));
  82.                 lpPaintStr += dwTextLen;
  83.                 *lpPaintStr = MYTEXT(',');
  84.                 lpPaintStr++;
  85.                 lpCls++;
  86.             }
  87.             *lpPaintStr = MYTEXT('');
  88.         }
  89.         else
  90.         {
  91.             Mylstrcpy(lpPaintStr,lpStr);
  92.             Mylstrcat(lpPaintStr,MYTEXT(","));
  93.         }
  94.     }
  95. }
  96. LRESULT HandleComposition(HWND hWnd,WPARAM wParam,LPARAM lParam)
  97. {
  98.     HIMC hIMC;
  99.     BOOL fRedraw = FALSE;
  100.     hIMC = ImmGetContext(hWnd);
  101.     if (lParam & GCS_COMPSTR)
  102.     {
  103.         dwCompStrLen = MyImmGetCompositionString(hIMC,GCS_COMPSTR,szCompStr,sizeof(szCompStr));
  104.         dwCompStrLen /= sizeof(MYCHAR);
  105.         szCompStr[dwCompStrLen] = MYTEXT('');
  106.         fRedraw = TRUE;
  107.     }
  108.     else
  109.     {
  110.         dwCompStrLen = 0;
  111.         szCompStr[0] = MYTEXT('');
  112.     }
  113.     if (lParam & GCS_COMPATTR)
  114.     {
  115.         dwCompAttrLen = MyImmGetCompositionString(hIMC,GCS_COMPATTR,bCompAttr,sizeof(bCompAttr));
  116.         fRedraw = TRUE;
  117.     }
  118.     else
  119.     {
  120.         dwCompAttrLen = 0;
  121.         bCompAttr[0] = 0;
  122.     }
  123.     if (lParam & GCS_COMPCLAUSE)
  124.     {
  125.         dwCompClsLen = MyImmGetCompositionString(hIMC,GCS_COMPCLAUSE,dwCompCls,sizeof(dwCompCls));
  126.         fRedraw = TRUE;
  127.     }
  128.     else
  129.     {
  130.         dwCompClsLen = 0;
  131.         dwCompCls[0] = 0;
  132.         dwCompCls[1] = 0;
  133.     }
  134.     if (lParam & GCS_COMPREADSTR)
  135.     {
  136.         dwCompReadStrLen = MyImmGetCompositionString(hIMC,GCS_COMPREADSTR,szCompReadStr,sizeof(szCompReadStr));
  137.         dwCompReadStrLen /= sizeof(MYCHAR);
  138.         szCompReadStr[dwCompReadStrLen] = MYTEXT('');
  139.         fRedraw = TRUE;
  140.     }
  141.     else
  142.     {
  143.         dwCompReadStrLen = 0;
  144.         szCompReadStr[0] = MYTEXT('');
  145.     }
  146.     if (lParam & GCS_COMPREADATTR)
  147.     {
  148.         dwCompReadAttrLen = MyImmGetCompositionString(hIMC,GCS_COMPREADATTR,bCompReadAttr,sizeof(bCompReadAttr));
  149.         fRedraw = TRUE;
  150.     }
  151.     else
  152.     {
  153.         dwCompReadAttrLen = 0;
  154.         bCompReadAttr[0] = 0;
  155.     }
  156.     if (lParam & GCS_COMPREADCLAUSE)
  157.     {
  158.         dwCompReadClsLen = MyImmGetCompositionString(hIMC,GCS_COMPREADCLAUSE,dwCompReadCls,sizeof(dwCompReadCls));
  159.         fRedraw = TRUE;
  160.     }
  161.     else
  162.     {
  163.         dwCompReadClsLen = 0;
  164.         dwCompReadCls[0] = 0;
  165.         dwCompReadCls[1] = 0;
  166.     }
  167.     if (lParam & GCS_RESULTSTR)
  168.     {
  169.         RECT rc;
  170.         HDC hIC;
  171.         SIZE sz0,sz1;
  172.         HFONT hOldFont;
  173.         if (lParam & GCS_RESULTCLAUSE)
  174.         {
  175.             dwResultClsLen = MyImmGetCompositionString(hIMC,GCS_RESULTCLAUSE,dwResultCls,sizeof(dwResultCls));
  176.         }
  177.         else
  178.         {
  179.             dwResultClsLen = 0;
  180.             dwResultCls[0] = 0;
  181.             dwResultCls[1] = 0;
  182.         }
  183.         dwResultStrLen = MyImmGetCompositionString(hIMC,GCS_RESULTSTR,szResultStr,sizeof(szResultStr));
  184.         dwResultStrLen /= sizeof(MYCHAR);
  185.         szResultStr[dwResultStrLen] = MYTEXT('');
  186.         // szPaintResult may overflow..
  187.         GetClientRect(hWnd,&rc);
  188.         hIC = CreateIC("DISPLAY", NULL, NULL, NULL);
  189.         hOldFont = SelectObject(hIC,hFont);
  190. #ifdef USEWAPI
  191.         GetTextExtentPointW(hIC,szPaintResult,Mylstrlen(szPaintResult),&sz0);
  192.         GetTextExtentPointW(hIC,szResultStr,Mylstrlen(szResultStr),&sz1);
  193. #else
  194.         GetTextExtentPoint(hIC,szPaintResult,Mylstrlen(szPaintResult),&sz0);
  195.         GetTextExtentPoint(hIC,szResultStr,Mylstrlen(szResultStr),&sz1);
  196. #endif
  197.         if (sz0.cx + sz1.cx >= rc.right)
  198.         {
  199.             szPaintResult[0] = MYTEXT('');
  200.             szPaintResultRead[0] = MYTEXT('');
  201.         }
  202.         SelectObject(hIC,hOldFont);
  203.         DeleteDC(hIC);
  204.         MakePaintString(hWnd,szResultStr,dwResultStrLen,dwResultCls,dwResultClsLen,szPaintResult);
  205.         fRedraw = TRUE;
  206.     }
  207.     else
  208.     {
  209.         dwResultStrLen = 0;
  210.         szResultStr[0] = MYTEXT('');
  211.         dwResultClsLen = 0;
  212.         dwResultCls[0] = 0;
  213.         dwResultCls[1] = 0;
  214.     }
  215.     if (lParam & GCS_RESULTREADSTR)
  216.     {
  217.         if (lParam & GCS_RESULTREADCLAUSE)
  218.         {
  219.             dwResultReadClsLen = MyImmGetCompositionString(hIMC,GCS_RESULTREADCLAUSE,dwResultReadCls,sizeof(dwResultReadCls));
  220.             fRedraw = TRUE;
  221.         }
  222.         else
  223.         {
  224.             dwResultReadClsLen = 0;
  225.             dwResultReadCls[0] = 0;
  226.             dwResultReadCls[1] = 0;
  227.         }
  228.         dwResultReadStrLen = MyImmGetCompositionString(hIMC,GCS_RESULTREADSTR,szResultReadStr,sizeof(szResultReadStr));
  229.         dwResultReadStrLen /= sizeof(MYCHAR);
  230.         szResultReadStr[dwResultReadStrLen] = MYTEXT('');
  231.         MakePaintString(hWnd,szResultReadStr,dwResultReadStrLen,dwResultReadCls,dwResultReadClsLen,szPaintResultRead);
  232.         fRedraw = TRUE;
  233.     }
  234.     else
  235.     {
  236.         dwResultReadStrLen = 0;
  237.         szResultReadStr[0] = MYTEXT('');
  238.         dwResultReadClsLen = 0;
  239.         dwResultReadCls[0] = 0;
  240.         dwResultReadCls[1] = 0;
  241.     }
  242.     if (fRedraw)
  243.     {
  244.         InvalidateRect(hWnd,NULL,TRUE);
  245.         UpdateWindow(hWnd);
  246.     }
  247.     return 1;
  248. }
  249. LRESULT HandleChar(HWND hWnd,WPARAM wParam,LPARAM lParam)
  250. {
  251.     RECT rc;
  252.     HDC hIC;
  253.     SIZE sz0,sz1;
  254.     HFONT hOldFont;
  255.     LPMYSTR lp;
  256.     // is the previous received char is a DBCS lead byte char ?
  257.     static BOOL fIsPrevLeadByte = FALSE;
  258.     GetClientRect(hWnd,&rc);
  259.     hIC = CreateIC("DISPLAY", NULL, NULL, NULL);
  260.     hOldFont = SelectObject(hIC,hFont);
  261. #ifdef USEWAPI
  262.     GetTextExtentPointW(hIC,szPaintResult,Mylstrlen(szPaintResult),&sz0);
  263.     GetTextExtentPointW(hIC,szResultStr,Mylstrlen(szResultStr),&sz1);
  264. #else
  265.     GetTextExtentPoint(hIC,szPaintResult,Mylstrlen(szPaintResult),&sz0);
  266.     GetTextExtentPoint(hIC,szResultStr,Mylstrlen(szResultStr),&sz1);
  267. #endif
  268.     if (sz0.cx + sz1.cx >= rc.right)
  269.     {
  270.         szPaintResult[0] = MYTEXT('');
  271.         szPaintResultRead[0] = MYTEXT('');
  272.     }
  273.     SelectObject(hIC,hOldFont);
  274.     DeleteDC(hIC);
  275.     lp = szPaintResult + Mylstrlen(szPaintResult);
  276. #ifndef USEWAPI
  277.     if (fIsPrevLeadByte) {
  278.         // remove , and append second byte for showing DBCS char
  279.         if (*(lp - 1) == ',') {
  280.             lp--;
  281.         }
  282.     }
  283. #endif
  284.     // append second byte
  285.     *lp++ = (MYCHAR)(BYTE)wParam;
  286.     *lp++ = MYTEXT(',');
  287.     *lp++ = MYTEXT('');
  288.     lp = szPaintResultRead + Mylstrlen(szPaintResultRead);
  289. #ifndef USEWAPI
  290.     if (fIsPrevLeadByte) {
  291.         // remove , and append second byte for showing DBCS char
  292.         if (*(lp - 1) == ',') {
  293.             lp--;
  294.         }
  295.         fIsPrevLeadByte = FALSE;
  296.     } else {
  297.         fIsPrevLeadByte = IsDBCSLeadByte((BYTE)wParam);
  298.     }
  299. #endif
  300.     *lp++ = (BYTE)wParam;
  301.     *lp++ = MYTEXT(',');
  302.     *lp++ = MYTEXT('');
  303.     InvalidateRect(hWnd,NULL,TRUE);
  304.     UpdateWindow(hWnd);
  305.     return 1;
  306. }
  307. LRESULT HandleNotify(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  308. {
  309.     HIMC hIMC;
  310.     BOOL fOpen;
  311.     DWORD dwConvMode,dwSentMode;
  312.     switch (wParam)
  313.     {
  314.         case IMN_OPENSTATUSWINDOW:
  315.         case IMN_CLOSESTATUSWINDOW:
  316.             break;
  317.         case IMN_SETOPENSTATUS:
  318.             SetStatusItems(hWnd);
  319.             hIMC = ImmGetContext(hWnd);
  320.             fOpen = ImmGetOpenStatus(hIMC);
  321.             UpdateShowOpenStatusButton(fOpen);
  322.             ImmReleaseContext(hWnd,hIMC);
  323.             break;
  324.         case IMN_SETCONVERSIONMODE:
  325.             hIMC = ImmGetContext(hWnd);
  326.             fOpen = ImmGetOpenStatus(hIMC);
  327.             ImmGetConversionStatus(hIMC,&dwConvMode,&dwSentMode);
  328.             if (fOpen)
  329.             {
  330.                 SetConvModeParts(dwConvMode);
  331.                 UpdateModeButton(dwConvMode);
  332.             }
  333.             else
  334.                 ClearConvModeParts();
  335.             ImmReleaseContext(hWnd,hIMC);
  336.             break;
  337.         case IMN_OPENCANDIDATE:
  338.             if (!fShowCand || (lParam != 0x01))
  339.             {
  340.                 if (fdwProperty & IME_PROP_SPECIAL_UI)
  341.                 {
  342.                     // Normally, we only need to set the composition window
  343.                     // position for a special UI IME
  344.                 }
  345.                 else if (fdwProperty & IME_PROP_AT_CARET)
  346.                 {
  347.                     CANDIDATEFORM cdf;
  348.                     HIMC          hIMC;
  349.  
  350.                     hIMC = ImmGetContext(hWnd);
  351.                     cdf.dwIndex = 0;
  352.                     cdf.dwStyle = CFS_CANDIDATEPOS;
  353.                     cdf.ptCurrentPos.x = ptImeUIPos.x;
  354.                     cdf.ptCurrentPos.y = ptImeUIPos.y;
  355.                     ImmSetCandidateWindow(hIMC,&cdf);
  356.                     ImmReleaseContext(hWnd,hIMC);
  357.                 }
  358.                 else
  359.                 {
  360.                     // Normally, we only need to set the composition window
  361.                     // position for a near caret IME
  362.                 }
  363.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  364.             }
  365.         case IMN_CHANGECANDIDATE:
  366. #ifdef _DEBUG
  367. {
  368. char szDev[80];
  369. DWORD dwSize;
  370. LPCANDIDATELIST lpC;
  371. hIMC = ImmGetContext(hWnd);
  372. if (dwSize = ImmGetCandidateList(hIMC,0x0,NULL,0))
  373. {
  374.     lpC = (LPCANDIDATELIST)GlobalAlloc(GPTR,dwSize);
  375.    
  376.     ImmGetCandidateList(hIMC,0x0,lpC,dwSize);
  377.     OutputDebugString("DumpCandList!!!rn");
  378.     wsprintf((LPSTR)szDev,"dwCount %drn",lpC->dwCount);
  379.     OutputDebugString((LPSTR)szDev);
  380.     wsprintf((LPSTR)szDev,"dwSelection %drn",lpC->dwSelection);
  381.     OutputDebugString((LPSTR)szDev);
  382.     wsprintf((LPSTR)szDev,"dwPageStart %drn",lpC->dwPageStart);
  383.     OutputDebugString((LPSTR)szDev);
  384.     wsprintf((LPSTR)szDev,"dwPageSize %drn",lpC->dwPageSize);
  385.     OutputDebugString((LPSTR)szDev);
  386.     GlobalFree((HANDLE)lpC);
  387. }
  388. }
  389. #endif
  390.             if (fShowCand && (lParam == 0x01))
  391.             {
  392.                 DWORD dwSize;
  393.                 if (!lpCandList)
  394.                     lpCandList = (LPCANDIDATELIST)GlobalAlloc(GPTR,sizeof(CANDIDATELIST));
  395.                 hIMC = ImmGetContext(hWnd);
  396.                 if (dwSize = ImmGetCandidateList(hIMC,0x0,NULL,0))
  397.                 {
  398.                     GlobalFree((HANDLE)lpCandList);
  399.                     lpCandList = (LPCANDIDATELIST)GlobalAlloc(GPTR,dwSize);
  400.                    
  401.                     ImmGetCandidateList(hIMC,0x0,lpCandList,dwSize);
  402.                 }
  403.                 else
  404.                 {
  405.                     memset(lpCandList, 0, sizeof(CANDIDATELIST));
  406.                 }
  407.                 InvalidateRect(hWndCandList,NULL,TRUE);
  408.                 UpdateWindow(hWndCandList);
  409.                     
  410.                 ImmReleaseContext(hWnd,hIMC);
  411.             }
  412.             else
  413.             {
  414.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  415.             }
  416.             break;
  417.         case IMN_CLOSECANDIDATE:
  418.             if (fShowCand && (lParam == 0x01))
  419.             {
  420.                 if (!lpCandList)
  421.                     lpCandList = (LPCANDIDATELIST)GlobalAlloc(GPTR,sizeof(CANDIDATELIST));
  422.                 memset(lpCandList, 0, sizeof(CANDIDATELIST));
  423.                 InvalidateRect(hWndCandList,NULL,TRUE);
  424.                 UpdateWindow(hWndCandList);
  425.             }
  426.             else
  427.             {
  428.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  429.             }
  430.             break;
  431.        default:
  432.             return (DefWindowProc(hWnd, message, wParam, lParam));
  433.     }
  434.     return 0;
  435. }