freepy.c
资源名称:大熊猫输入法原码.zip [点击查看]
上传用户:szljw888
上传日期:2010-04-11
资源大小:124k
文件大小:15k
源码类别:
输入法编程
开发平台:
C/C++
- /* 大熊猫输入法(Free Chinese Input) 由YJMSIR(yjmsir@163.com)编写
- * 协议: GPL
- * freeinput( A Chinese Input Method) by YJMSIR(yjmsir@163.com)
- * Licence: GPL
- */
- #include "freepy.h"
- #define CS_FREEPY (CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS)
- /**********************************************************************/
- /* DLLEntry */
- /**********************************************************************/
- BOOL WINAPI DllMain (
- HINSTANCE hInstDLL,
- DWORD dwFunction,
- LPVOID lpNot)
- {
- switch(dwFunction)
- {
- char szDirFile[MAX_PATH];
- case DLL_PROCESS_ATTACH:
- /* for debug ********************************/
- #ifdef _MY_DEBUG
- if(nDebugLevel){
- if( (DebugLogFile=_tfopen( DEBUGLOGFILE, "w"))==NULL)
- MessageBox(NULL,"can not open Debuglogfile","debug",MB_OK);
- DebugLog(1,(DebugLogFile,"Entry inn"));
- }
- #endif
- /********************************************/
- hInst = hInstDLL;
- ReadRegRecord();
- GetRelativeTableDirList();
- ReadGlobalIni();
- sprintf(szDirFile,"%swbx.mb",CurrentTableDir);
- if(!strcmp(CurrentTableDir,"") || !IsFileExist(szDirFile))
- sprintf(CurrentTableDir,"%smb\%s\",InstallFromDir,RelativeTableDirList[0]);
- ReadUserIni();
- if(wConversionSet & CONVERSION_SET_TENLINE) CAND_PAGESIZE=10;
- hUIFont = CreateFont(IsWindows98()?20:16, 0, 0, 0, FW_NORMAL,
- FALSE, FALSE, FALSE, ANSI_CHARSET,
- OUT_TT_PRECIS, CLIP_TT_ALWAYS, ANTIALIASED_QUALITY,
- DEFAULT_PITCH, _T("宋体"));
- if(!IMERegisterClass(hInst)) return FALSE;
- if(HaveInitDictionary) InitDictionary();//重要
- break;
- case DLL_PROCESS_DETACH:
- DeleteObject(hUIFont);
- UnregisterClass(UICLASSNAME,hInst);
- UnregisterClass(COMPCLASSNAME,hInst);
- UnregisterClass(STATUSCLASSNAME,hInst);
- DestroyDictionary();
- /* for debug ********************************/
- #ifdef _MY_DEBUG
- if(nDebugLevel){
- DebugLog(1,(DebugLogFile,"Entry outn"));
- if(DebugLogFile!=NULL)
- fclose(DebugLogFile);
- }
- #endif
- /********************************************/
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- }
- return TRUE;
- }
- BOOL IMERegisterClass( HANDLE hInstance )
- {
- WNDCLASSEX wc;
- //
- // register class of UI window.
- //
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_FREEPY | CS_IME;
- wc.lpfnWndProc = UIWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 2 * sizeof(LONG);
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hIcon = NULL;
- wc.lpszMenuName = (LPTSTR)NULL;
- wc.lpszClassName = UICLASSNAME;
- wc.hbrBackground = NULL;
- wc.hIconSm = NULL;
- if( !RegisterClassEx( (LPWNDCLASSEX)&wc ) )
- return FALSE;
- //
- // register class of composition window.
- //
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_FREEPY | CS_IME;
- wc.lpfnWndProc = CompWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = UICHILDEXTRASIZE;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hIcon = NULL;
- wc.lpszMenuName = (LPSTR)NULL;
- wc.lpszClassName = COMPCLASSNAME;
- wc.hbrBackground = NULL;
- wc.hIconSm = NULL;
- if( !RegisterClassEx( (LPWNDCLASSEX)&wc ) )
- return FALSE;
- //
- // register class of status window.
- //
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_FREEPY | CS_IME;
- wc.lpfnWndProc = StatusWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = UICHILDEXTRASIZE;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hIcon = NULL;
- wc.lpszMenuName = (LPSTR)NULL;
- wc.lpszClassName = STATUSCLASSNAME;
- wc.hbrBackground = NULL;
- wc.hIconSm = NULL;
- if( !RegisterClassEx( (LPWNDCLASSEX)&wc))
- return FALSE;
- return TRUE;
- }
- /**********************************************************************/
- /* */
- /* UIWndProc() */
- /* */
- /* IME UI window procedure */
- /* */
- /**********************************************************************/
- LRESULT WINAPI UIWndProc(
- HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam)
- {
- HIMC hUICurIMC;
- LPINPUTCONTEXT lpIMC;
- LPUIEXTRA lpUIExtra;
- HGLOBAL hUIExtra;
- LONG lRet = 0L;
- DebugLog(1,(DebugLogFile,"UIWndn"));
- hUICurIMC = (HIMC)GetWindowLong(hWnd,IMMGWL_IMC);
- //
- // Even if there is no current UI. these messages should not be pass to
- // DefWindowProc().
- //
- if (!hUICurIMC)
- {
- switch (message)
- {
- case WM_IME_STARTCOMPOSITION:
- case WM_IME_ENDCOMPOSITION:
- case WM_IME_COMPOSITION:
- case WM_IME_NOTIFY:
- case WM_IME_CONTROL:
- case WM_IME_COMPOSITIONFULL:
- case WM_IME_SELECT:
- case WM_IME_CHAR:
- return 0L;
- default:
- break;
- }
- }
- switch (message)
- {
- case WM_CREATE:
- //DebugLog(1,(DebugLogFile,"UIWnd:WM_CREATEn"));
- //
- // Allocate UI's extra memory block.
- //
- hUIExtra = GlobalAlloc(GHND,sizeof(UIEXTRA));
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- lpUIExtra->uiComp.pt.x = -1;//test
- lpUIExtra->uiComp.pt.y = -1;
- CreateCompWindow(hWnd,lpUIExtra);
- GlobalUnlock(hUIExtra);
- SetWindowLong(hWnd,IMMGWL_PRIVATE,(DWORD)hUIExtra);
- if(!HaveSetTimerForSpeed){
- SetTimer(hWnd,2,60000,NULL);//1分钟的平均值
- HaveSetTimerForSpeed=1;
- }
- if(!HaveInitDictionary)
- SetTimer(hWnd,3,500,NULL);//0.5秒钟
- break;
- case WM_TIMER :
- if(!HaveInitDictionary){
- KillTimer (hWnd, 3);
- InitDictionary();
- HaveInitDictionary=1;
- //UpdateStatusByMessage(hUICurIMC);
- break;
- }
- else{
- WuBiSpeed=InputAccumulateNum/2;//前一分钟的输入数量
- InputAccumulateNum=0;
- //if(wConversionSet & CONVERSION_SET_WBSPEED)//) && (WuBiSpeed!=0)
- _stprintf(SpeedMessage,"(%d字/分)",WuBiSpeed);
- //else
- //_tcscpy(SpeedMessage,_T(""));
- }
- break;
- case WM_IME_SETCONTEXT:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_SETCONTEXTn"));
- if (wParam)
- {
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- if (hUICurIMC)
- {
- //
- // input context was changed.
- // if there are the child windows, the diplay have to be
- // updated.
- //
- lpIMC = ImmLockIMC(hUICurIMC);
- if (lpIMC)
- {
- //DebugLog(8,(DebugLogFile,"(lpIMCn"));
- MoveCompWindow(hWnd,lpUIExtra,lpIMC);
- //if(!(wConversionSet &CONVERSION_SET_DOUBLELINE))
- //MoveCandWindow(hWnd,lpUIExtra,lpIMC);
- //else
- //HideCandWindow(lpUIExtra);
- }
- else
- {
- if(wConversionSet & CONVERSION_SET_AUTOHIDE){
- HideCompWindow(lpUIExtra);
- //HideCandWindow(lpUIExtra);
- }
- }
- ImmUnlockIMC(hUICurIMC);
- }
- else // it is NULL input context.
- {
- if(wConversionSet & CONVERSION_SET_AUTOHIDE){
- HideCompWindow(lpUIExtra);
- //HideCandWindow(lpUIExtra);
- }
- }
- GlobalUnlock(hUIExtra);
- }
- break;
- case WM_IME_STARTCOMPOSITION:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_STARTCOMPOSITIONn"));
- //
- // Start composition! Ready to display the composition string.
- //
- break;
- case WM_IME_COMPOSITION:
- //DebugLog(8,(DebugLogFile,"UIWnd:WM_IME_COMPOSITIONn"));
- //
- // Update to display the composition string.
- //
- lpIMC = ImmLockIMC(hUICurIMC);
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- //DebugLog(8,(DebugLogFile,"lpUIExtran"));
- MoveCompWindow(hWnd,lpUIExtra,lpIMC);
- GlobalUnlock(hUIExtra);
- ImmUnlockIMC(hUICurIMC);
- break;
- case WM_IME_ENDCOMPOSITION:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_ENDCOMPOSITIONn"));
- //
- // Finish to display the composition string.
- //
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- if(wConversionSet & CONVERSION_SET_AUTOHIDE){
- HideCompWindow(lpUIExtra);
- }
- GlobalUnlock(hUIExtra);
- break;
- case WM_IME_COMPOSITIONFULL:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_COMPOSITIONFULLn"));
- break;
- case WM_IME_SELECT:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_SELECTn"));
- break;
- case WM_IME_CONTROL:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_CONTROLn"));
- lRet = ControlHandle(hUICurIMC, hWnd,message,wParam,lParam);
- break;
- case WM_IME_NOTIFY:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFYn"));
- lRet = NotifyHandle(hUICurIMC, hWnd,message,wParam,lParam);
- break;
- case WM_DESTROY:
- //DebugLog(1,(DebugLogFile,"UIWnd:WM_DESTROYn"));
- KillTimer (hWnd, 2);
- HaveSetTimerForSpeed=0;
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- if (IsWindow(lpUIExtra->uiStatus.hWnd))
- DestroyWindow(lpUIExtra->uiStatus.hWnd);
- //if (IsWindow(lpUIExtra->uiCand.hWnd))
- // DestroyWindow(lpUIExtra->uiCand.hWnd);
- if (IsWindow(lpUIExtra->uiComp.hWnd))
- DestroyWindow(lpUIExtra->uiComp.hWnd);
- GlobalUnlock(hUIExtra);
- GlobalFree(hUIExtra);
- //PostQuitMessage (0) ;
- break;
- case WM_UI_COMPMOVE:
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- lpUIExtra->uiComp.pt.x = (long)LOWORD(lParam);
- lpUIExtra->uiComp.pt.y = (long)HIWORD(lParam);
- GlobalUnlock(hUIExtra);
- break;
- default:
- return DefWindowProc(hWnd,message,wParam,lParam);
- }
- return lRet;
- }
- /**********************************************************************/
- /* */
- /* NotifyHandle() */
- /* */
- /* Handle WM_IME_NOTIFY messages. */
- /* */
- /**********************************************************************/
- LONG NotifyHandle(HIMC hUICurIMC, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- LONG lRet = 0L;
- LPINPUTCONTEXT lpIMC;
- HGLOBAL hUIExtra;
- LPUIEXTRA lpUIExtra;
- if (!(lpIMC = ImmLockIMC(hUICurIMC)))
- return 0L;
- hUIExtra = (HGLOBAL)GetWindowLong(hWnd,IMMGWL_PRIVATE);
- lpUIExtra = (LPUIEXTRA)GlobalLock(hUIExtra);
- switch (wParam)
- {
- case IMN_CLOSESTATUSWINDOW:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_CLOSESTATUSWINDOWn"));
- if (IsWindow(lpUIExtra->uiStatus.hWnd)) {
- ShowWindow(lpUIExtra->uiStatus.hWnd,SW_HIDE);
- }
- break;
- case IMN_OPENSTATUSWINDOW:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_OPENSTATUSWINDOWn"));
- CreateStatusWindow( hWnd, lpUIExtra);
- break;
- case IMN_OPENCANDIDATE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_OPENCANDIDATEn"));
- break;
- case IMN_CHANGECANDIDATE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_CHANGECANDIDATEn"));
- break;
- case IMN_CLOSECANDIDATE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_CLOSECANDIDATEn"));
- break;
- case IMN_SETCONVERSIONMODE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETCONVERSIONMODEn"));
- UpdateStatusWindow(lpUIExtra);
- break;
- case IMN_SETSENTENCEMODE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETSENTENCEMODEn"));
- break;
- case IMN_SETOPENSTATUS:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETOPENSTATUSn"));
- if(!IsIMEOpen(hUICurIMC)) {
- MakeResultString(hUICurIMC,FALSE);
- }
- UpdateStatusWindow(lpUIExtra);
- break;
- case IMN_SETCANDIDATEPOS:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETCANDIDATEPOSn"));
- break;
- case IMN_SETCOMPOSITIONFONT:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETCOMPOSITIONFONTn"));
- break;
- case IMN_SETCOMPOSITIONWINDOW:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETCOMPOSITIONWINDOWn"));
- if(wConversionSet & CONVERSION_SET_FOLLOW) {
- POINT ptSrc;
- SIZE szOffset;
- HDC hDC;
- HFONT oldFont;
- RECT screenrc;
- ptSrc = lpIMC->cfCompForm.ptCurrentPos;
- //MessageBoxPrintf("cfCompForm","x:%d y:%d",ptSrc.x,ptSrc.y);
- if(ptSrc.x==0 &&ptSrc.y==0){
- SystemParametersInfo(SPI_GETWORKAREA,
- 0,
- &screenrc,
- 0);
- ptSrc.x=(screenrc.right+screenrc.left)/3;
- ptSrc.y=screenrc.bottom+80;
- }
- else
- ClientToScreen(lpIMC->hWnd, &ptSrc);
- hDC = GetDC(lpIMC->hWnd);
- oldFont = SelectObject(hDC, hUIFont);
- GetTextExtentPoint(hDC,"A",1,&szOffset);
- SelectObject(hDC, oldFont);
- ReleaseDC(lpIMC->hWnd,hDC);
- lpUIExtra->uiComp.pt.x = ptSrc.x + szOffset.cx;
- lpUIExtra->uiComp.pt.y = ptSrc.y + szOffset.cy;
- }
- else{
- lpUIExtra->uiComp.pt.x = lpUIExtra->uiStatus.pt.x + lpUIExtra->uiStatus.sz.cx;
- lpUIExtra->uiComp.pt.y = lpUIExtra->uiStatus.pt.y;
- }
- if (IsWindow(lpUIExtra->uiComp.hWnd))
- InvalidateRect(lpUIExtra->uiComp.hWnd,NULL,FALSE);
- break;
- case IMN_GUIDELINE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_GUIDELINEn"));
- break;
- case IMN_SETSTATUSWINDOWPOS://yjm
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_SETSTATUSWINDOWPOSn"));
- //MessageBox(NULL,_T("IMN_SETSTATUSWINDOWPOS"),_T("谢谢关注!"),MB_OK);
- if(wConversionSet & CONVERSION_SET_FOLLOW) {
- POINT ptSrc;
- RECT screenrc;
- ptSrc = lpIMC->ptStatusWndPos;
- //MessageBoxPrintf("ptStatusWndPos","x:%d y:%d",ptSrc.x,ptSrc.y);
- GetClientRect(hWnd,&screenrc);
- ptSrc.x=screenrc.right-100;
- ptSrc.y=screenrc.top -20;
- ClientToScreen(lpIMC->hWnd, &ptSrc);
- lpUIExtra->uiStatus.pt.x = ptSrc.x;
- lpUIExtra->uiStatus.pt.y = ptSrc.y;
- }
- if (IsWindow(lpUIExtra->uiStatus.hWnd))
- InvalidateRect(lpUIExtra->uiStatus.hWnd,NULL,FALSE);
- break;
- case IMN_PRIVATE:
- DebugLog(1,(DebugLogFile,"UIWnd:WM_IME_NOTIFY:IMN_PRIVATEn"));
- break;
- default:
- break;
- }
- GlobalUnlock(hUIExtra);
- ImmUnlockIMC(hUICurIMC);
- return lRet;
- }
- /**********************************************************************/
- /* */
- /* ControlHandle() */
- /* */
- /* Handle WM_IME_CONTROL messages. */
- /* */
- /**********************************************************************/
- LONG ControlHandle(HIMC hUICurIMC, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- return 0L;
- }