#include <windows.h>
#include "imm.h"
#include "imedefs.h"

/**********************************************************************/
/* GetCandWnd                                                         */
/* Return Value :                                                     */
/*      window handle of candidatte                                   */
/**********************************************************************/
HWND PASCAL GetCandWnd(
    HWND hUIWnd)                // UI window
{
    HGLOBAL  hUIPrivate;
    LPUIPRIV lpUIPrivate;
    HWND     hCandWnd;

    hUIPrivate = (HGLOBAL)GetWindowLong(hUIWnd, IMMGWL_PRIVATE);
    if (!hUIPrivate) {          // can not darw candidate window
        return (HWND)NULL;
    }

    lpUIPrivate = (LPUIPRIV)GlobalLock(hUIPrivate);
    if (!lpUIPrivate) {         // can not draw candidate window
        return (HWND)NULL;
    }

    hCandWnd = lpUIPrivate->hCandWnd;

    GlobalUnlock(hUIPrivate);
    return (hCandWnd);
}

void PASCAL CalcCandPos(
    HIMC    hIMC,
    HWND    hUIWnd,
    LPPOINT lpptWnd)            // the composition window position
{
    POINT          ptNew, ptSTWPos;

    ptNew.x = lpptWnd->x + lpImeL->xCompWi + UI_MARGIN;
    if (ptNew.x + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        // exceed screen width
        ptNew.x = lpptWnd->x - sImeG.xCandWi - UI_MARGIN;
    }

    ptNew.y = lpptWnd->y + lpImeL->cyCompBorder - sImeG.cyCandBorder;
    if (ptNew.y + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
        // exceed screen high
        ptNew.y = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
    }

    if(!sImeG.IC_Trace) {
	    HWND hCompWnd;

   		ImmGetStatusWindowPos(hIMC, (LPPOINT)&ptSTWPos);
	    hCompWnd = GetCompWnd(hUIWnd);
	    if (hCompWnd) {
			ptNew.x = ptSTWPos.x + sImeG.xStatusWi + lpImeL->xCompWi + 2 * UI_MARGIN;
		    if((ptSTWPos.x + sImeG.xStatusWi + sImeG.xCandWi + lpImeL->xCompWi + 2 * UI_MARGIN)>
			  sImeG.rcWorkAreaCa.right) {
		      if (ptSTWPos.x >= (sImeG.xCandWi + lpImeL->xCompWi + 2 * UI_MARGIN)) { 
				ptNew.x = ptSTWPos.x - lpImeL->xCompWi - sImeG.xCandWi - 2 * UI_MARGIN;
			  } else {
				ptNew.x = ptSTWPos.x + sImeG.xStatusWi + UI_MARGIN;
			  }
			}


		    ptNew.y = ptSTWPos.y + lpImeL->cyCompBorder - sImeG.cyCandBorder;
		    if (ptNew.y + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
		        ptNew.y = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
		    }
	    } else {
			ptNew.x = ptSTWPos.x + sImeG.xStatusWi + UI_MARGIN;
		    if(((ptSTWPos.x + sImeG.xStatusWi + sImeG.xCandWi + UI_MARGIN)>=
			  sImeG.rcWorkAreaCa.right)
		      && (ptSTWPos.x >= sImeG.xCandWi + UI_MARGIN)) { 
				ptNew.x = ptSTWPos.x - sImeG.xCandWi - UI_MARGIN;
			}

		    ptNew.y = ptSTWPos.y + lpImeL->cyCompBorder - sImeG.cyCandBorder;
		    if (ptNew.y + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
		        ptNew.y = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
		    }
		}
    }
    
    lpptWnd->x = ptNew.x;
    lpptWnd->y = ptNew.y;

    return;
}

/**********************************************************************/
/* AdjustCandPos                                                      */
/**********************************************************************/
void AdjustCandPos(
    HIMC    hIMC,
    LPPOINT lpptWnd)            // the composition window position
{
    LPINPUTCONTEXT lpIMC;
	LONG           ptFontHi;
    UINT           uEsc;

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return;
    }

    if (lpIMC->lfFont.A.lfHeight > 0) {
        ptFontHi = lpIMC->lfFont.A.lfHeight;
    } else if (lpIMC->lfFont.A.lfWidth == 0) {
        ptFontHi = lpImeL->yCompHi;
    } else {
        ptFontHi = -lpIMC->lfFont.A.lfHeight;
    }

    if (ptFontHi > lpImeL->yCompHi * 8) {
        ptFontHi = lpImeL->yCompHi * 8;
    }

    if (ptFontHi < sImeG.yChiCharHi) {
        ptFontHi = sImeG.yChiCharHi;
    }

    uEsc = (UINT)((lpIMC->lfFont.A.lfEscapement + 450) / 900 % 4);

    // find the location after IME do an adjustment
    ptFontHi = ptFontHi * ptInputEsc[uEsc].y;

    if(lpptWnd->y + ptFontHi + sImeG.yCandHi <= sImeG.rcWorkAreaCa.bottom) {
    	lpptWnd->y += ptFontHi;
	} else {
    	lpptWnd->y -= (ptFontHi + sImeG.yCandHi);
	}

    ImmUnlockIMC(hIMC);
	return;
}

/**********************************************************************/
/* AdjustCandRectBoundry                                              */
/**********************************************************************/
void PASCAL AdjustCandRectBoundry(
    LPINPUTCONTEXT lpIMC,
    LPPOINT        lpptCandWnd)            // the caret position
{
    RECT  rcExclude, rcUIRect, rcInterSect;
    UINT  uEsc;

    // be a normal rectangle, not a negative rectangle
    if (lpIMC->cfCandForm[0].rcArea.left > lpIMC->cfCandForm[0].rcArea.right) {
        LONG tmp;

        tmp = lpIMC->cfCandForm[0].rcArea.left;
        lpIMC->cfCandForm[0].rcArea.left = lpIMC->cfCandForm[0].rcArea.right;
        lpIMC->cfCandForm[0].rcArea.right = tmp;
    }

    if (lpIMC->cfCandForm[0].rcArea.top > lpIMC->cfCandForm[0].rcArea.bottom) {
        LONG tmp;

        tmp = lpIMC->cfCandForm[0].rcArea.top;
        lpIMC->cfCandForm[0].rcArea.top = lpIMC->cfCandForm[0].rcArea.bottom;
        lpIMC->cfCandForm[0].rcArea.bottom = tmp;
    }

    // translate from client coordinate to screen coordinate
    rcExclude = lpIMC->cfCandForm[0].rcArea;

    rcExclude.left += lpptCandWnd->x - lpIMC->cfCandForm[0].ptCurrentPos.x;
    rcExclude.right += lpptCandWnd->x - lpIMC->cfCandForm[0].ptCurrentPos.x;

    rcExclude.top += lpptCandWnd->y - lpIMC->cfCandForm[0].ptCurrentPos.y;
    rcExclude.bottom += lpptCandWnd->y - lpIMC->cfCandForm[0].ptCurrentPos.y;

    // if original point is OK, we use it
    *(LPPOINT)&rcUIRect = *lpptCandWnd;

    if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
        rcUIRect.left = sImeG.rcWorkAreaCa.left;
    } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
    } else {
    }

    if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
        rcUIRect.top = sImeG.rcWorkAreaCa.top;
    } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
        rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
    } else {
    }

    rcUIRect.right = rcUIRect.left + sImeG.xCandWi;
    rcUIRect.bottom = rcUIRect.top + sImeG.yCandHi;

    if (!IntersectRect(&rcInterSect, &rcExclude, &rcUIRect)) {
        *lpptCandWnd = *(LPPOINT)&rcUIRect;
        return;
    }

    uEsc = (UINT)((lpIMC->lfFont.A.lfEscapement + 450) / 900 % 4);

    if (uEsc & 0x0001) {
        // 900 & 2700 we need change x coordinate
        if (ptInputEsc[uEsc].x > 0) {
            rcUIRect.left = rcExclude.right;
        } else {
            rcUIRect.left = rcExclude.left - sImeG.xCandWi;
        }
    } else {
        // 0 & 1800 we do not change x coordinate
        rcUIRect.left = lpptCandWnd->x;
    }

    if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
        rcUIRect.left = sImeG.rcWorkAreaCa.left;
    } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
    } else {
    }

    if (uEsc & 0x0001) {
        // 900 & 2700 we do not change y coordinate
        rcUIRect.top = lpptCandWnd->y;
    } else {
        // 0 & 1800 we need change y coordinate
        if (ptInputEsc[uEsc].y > 0) {
            rcUIRect.top = rcExclude.bottom;
        } else {
            rcUIRect.top = rcExclude.top - sImeG.yCandHi;
        }
    }

    if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
        rcUIRect.top = sImeG.rcWorkAreaCa.top;
    } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
        rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
    } else {
    }

    rcUIRect.right = rcUIRect.left + sImeG.xCandWi;
    rcUIRect.bottom = rcUIRect.top + sImeG.yCandHi;

    // the candidate window not overlapped with exclude rectangle
    // so we found a position
    if (!IntersectRect(&rcInterSect, &rcExclude, &rcUIRect)) {
        *lpptCandWnd = *(LPPOINT)&rcUIRect;
        return;
    }

    // adjust according to
    *(LPPOINT)&rcUIRect = *lpptCandWnd;

    if (uEsc & 0x0001) {
        // 900 & 2700 we prefer adjust x
        if (ptInputEsc[uEsc].x > 0) {
            rcUIRect.left = rcExclude.right;
        } else {
            rcUIRect.left = rcExclude.left - sImeG.xCandWi;
        }

        if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
        } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        } else {
            if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
                rcUIRect.top = sImeG.rcWorkAreaCa.top;
            } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
                rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
            } else {
            }

            *lpptCandWnd = *(LPPOINT)&rcUIRect;
            return;
        }

        // negative try
        if (ptInputEsc[uEsc].x > 0) {
            rcUIRect.left = rcExclude.left - sImeG.xCandWi;
        } else {
            rcUIRect.left = rcExclude.right;
        }

        if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
        } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        } else {
            if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
                rcUIRect.top = sImeG.rcWorkAreaCa.top;
            } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
                rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
            } else {
            }

            *lpptCandWnd = *(LPPOINT)&rcUIRect;
            return;
        }

        // negative try failure again, we use positive plus display adjust
        if (ptInputEsc[uEsc].x > 0) {
            rcUIRect.left = rcExclude.right;
        } else {
            rcUIRect.left = rcExclude.left - sImeG.xCandWi;
        }

        if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
            rcUIRect.left = sImeG.rcWorkAreaCa.left;
        } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
            rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
        } else {
        }

        if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
            rcUIRect.top = sImeG.rcWorkAreaCa.top;
        } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
            rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
        } else {
        }

        *lpptCandWnd = *(LPPOINT)&rcUIRect;
    } else {
        // 0 & 1800 we prefer adjust y
        if (ptInputEsc[uEsc].y > 0) {
            rcUIRect.top = rcExclude.bottom;
        } else {
            rcUIRect.top = rcExclude.top - sImeG.yCandHi;
        }

        if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
        } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
        } else {
            if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
                rcUIRect.left = sImeG.rcWorkAreaCa.left;
            } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
                rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
            } else {
            }

            *lpptCandWnd = *(LPPOINT)&rcUIRect;
            return;
        }

        if (ptInputEsc[uEsc].y > 0) {
            rcUIRect.top = rcExclude.top - sImeG.yCandHi;
        } else {
            rcUIRect.top = rcExclude.bottom;
        }

        if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
        } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.right) {
        } else {
            if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
                rcUIRect.left = sImeG.rcWorkAreaCa.left;
            } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
                rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
            } else {
            }

            *lpptCandWnd = *(LPPOINT)&rcUIRect;
            return;
        }

        if (ptInputEsc[uEsc].y > 0) {
            rcUIRect.top = rcExclude.bottom;
        } else {
            rcUIRect.top = rcExclude.top - sImeG.yCandHi;
        }

        if (rcUIRect.left < sImeG.rcWorkAreaCa.left) {
            rcUIRect.left = sImeG.rcWorkAreaCa.left;
        } else if (rcUIRect.left + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
            rcUIRect.left = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
        } else {
        }

        if (rcUIRect.top < sImeG.rcWorkAreaCa.top) {
            rcUIRect.top = sImeG.rcWorkAreaCa.top;
        } else if (rcUIRect.top + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
            rcUIRect.top = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
        } else {
        }

        *lpptCandWnd = *(LPPOINT)&rcUIRect;
    }

    return;
}

/**********************************************************************/
/* AdjustCandBoundry                                                  */
/**********************************************************************/
void PASCAL AdjustCandBoundry(
    LPPOINT lpptCandWnd)            // the position
{
    if (lpptCandWnd->x < sImeG.rcWorkAreaCa.left) {
        lpptCandWnd->x = sImeG.rcWorkAreaCa.left;
    } else if (lpptCandWnd->x + sImeG.xCandWi > sImeG.rcWorkAreaCa.right) {
        lpptCandWnd->x = sImeG.rcWorkAreaCa.right - sImeG.xCandWi;
    }

    if (lpptCandWnd->y < sImeG.rcWorkAreaCa.top) {
        lpptCandWnd->y = sImeG.rcWorkAreaCa.top;
    } else if (lpptCandWnd->y + sImeG.yCandHi > sImeG.rcWorkAreaCa.bottom) {
        lpptCandWnd->y = sImeG.rcWorkAreaCa.bottom - sImeG.yCandHi;
    }

    return;
}

/**********************************************************************/
/* SetCandPosition()                                                  */
/**********************************************************************/
LRESULT PASCAL SetCandPosition(
    HWND hCandWnd)
{
    HWND           hUIWnd;
    HIMC           hIMC;
    LPINPUTCONTEXT lpIMC;
    POINT          ptNew;

    hUIWnd = GetWindow(hCandWnd, GW_OWNER);

    hIMC = (HIMC)GetWindowLong(hUIWnd, IMMGWL_IMC);
    if (!hIMC) {
        return (1L);
    }

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return (1L);
    }

    ptNew = lpIMC->cfCandForm[0].ptCurrentPos;

    ClientToScreen((HWND)lpIMC->hWnd, &ptNew);

  	if(!sImeG.IC_Trace) {
		sImeG.rcWorkAreaCa=sImeG.rcWorkAreaCo=sImeG.rcWorkAreaSt;
	   	CalcCandPos(hIMC, hUIWnd, &ptNew);
		AdjustCandBoundry(&ptNew);
	}
	else

    if (lpIMC->cfCandForm[0].dwStyle & CFS_FORCE_POSITION) {
    } else if (lpIMC->cfCandForm[0].dwStyle == CFS_CANDIDATEPOS) {

		{
			HMONITOR hMonitor;
			MONITORINFO info;

			hMonitor=MonitorFromPoint(ptNew,MONITOR_DEFAULTTOPRIMARY);
			info.cbSize=sizeof(MONITORINFO);
			GetMonitorInfo(hMonitor,&info);
			sImeG.rcWorkAreaCo=sImeG.rcWorkAreaCa=info.rcWork;
		}
        AdjustCandBoundry(&ptNew);
    } else if (lpIMC->cfCandForm[0].dwStyle == CFS_EXCLUDE) {
	   	if(!sImeG.IC_Trace) {
			sImeG.rcWorkAreaCa=sImeG.rcWorkAreaCo=sImeG.rcWorkAreaSt;
	   		CalcCandPos(hIMC, hUIWnd, &ptNew);
		}
        AdjustCandBoundry(&ptNew);
    }

    SetWindowPos(hCandWnd, NULL, ptNew.x, ptNew.y,
        0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);

    ImmUnlockIMC(hIMC);

    return (0L);
}

/**********************************************************************/
/* ShowCand()                                                         */
/**********************************************************************/
void PASCAL ShowCand(           // Show the candidate window
    HWND    hUIWnd,
    int     nShowCandCmd)
{
    HGLOBAL  hUIPrivate;
    LPUIPRIV lpUIPrivate;

    hUIPrivate = (HGLOBAL)GetWindowLong(hUIWnd, IMMGWL_PRIVATE);
    if (!hUIPrivate) {          // can not darw candidate window
        return;
    }

    lpUIPrivate = (LPUIPRIV)GlobalLock(hUIPrivate);
    if (!lpUIPrivate) {         // can not draw candidate window
        return;
    }

    if (lpUIPrivate->nShowCandCmd == nShowCandCmd) {
        goto SwCandNoChange;
    }

    if (nShowCandCmd == SW_HIDE) {
        lpUIPrivate->fdwSetContext &= ~(ISC_HIDE_CAND_WINDOW);
    }

    if (!lpUIPrivate->hCandWnd) {
        // not in show candidate window mode
    } else if (lpUIPrivate->nShowCandCmd == nShowCandCmd) {

    } else {
	    if(nShowCandCmd == SW_HIDE) {
	    	uOpenCand = 0;
		} else {
		    HIMC           hIMC;
		    POINT          ptSTWPos;
			int            Comp_CandWndLen;

	    	uOpenCand = 1;

		    // reset status window for LINE_UI(FIX_UI)
		    hIMC = (HIMC)GetWindowLong(hUIWnd, IMMGWL_IMC);
		    if (!hIMC) {
		        goto ShowCand;
		    }

   			ImmGetStatusWindowPos(hIMC, (LPPOINT)&ptSTWPos);
			Comp_CandWndLen = 0;
			if(uOpenCand) {
				Comp_CandWndLen += sImeG.xCandWi + UI_MARGIN;
				if(uStartComp) {
					Comp_CandWndLen += lpImeL->xCompWi + UI_MARGIN;
				}
				if(ptSTWPos.x + sImeG.xStatusWi + Comp_CandWndLen > sImeG.rcWorkAreaCa.right) {
				    PostMessage(GetCompWnd(hUIWnd), WM_IME_NOTIFY, IMN_SETCOMPOSITIONWINDOW, 0);
				}
			}
		}
        
ShowCand:
        ShowWindow(lpUIPrivate->hCandWnd, nShowCandCmd);
        lpUIPrivate->nShowCandCmd = nShowCandCmd;
    }

SwCandNoChange:
    GlobalUnlock(hUIPrivate);
    return;
}

/**********************************************************************/
/* OpenCand                                                           */
/**********************************************************************/
void PASCAL OpenCand(
    HWND hUIWnd)
{
    HGLOBAL        hUIPrivate;
    LPUIPRIV       lpUIPrivate;
    HIMC           hIMC;
    LPINPUTCONTEXT lpIMC;
    POINT          ptWnd;

    hUIPrivate = (HGLOBAL)GetWindowLong(hUIWnd, IMMGWL_PRIVATE);
    if (!hUIPrivate) {          // can not darw candidate window
        return;
    }

    lpUIPrivate = (LPUIPRIV)GlobalLock(hUIPrivate);
    if (!lpUIPrivate) {         // can not draw candidate window
        return;
    }

    lpUIPrivate->fdwSetContext |= ISC_SHOWUICANDIDATEWINDOW;

    hIMC = (HIMC)GetWindowLong(hUIWnd, IMMGWL_IMC);
    if (!hIMC) {
        goto OpenCandUnlockUIPriv;
    }

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        goto OpenCandUnlockUIPriv;
    }

    if (lpIMC->cfCandForm[0].dwIndex == 0) {
        
        ptWnd = lpIMC->cfCandForm[0].ptCurrentPos;

        ClientToScreen(lpIMC->hWnd, &ptWnd);

        if (lpIMC->cfCandForm[0].dwStyle & CFS_FORCE_POSITION) {
        } else if (lpIMC->cfCandForm[0].dwStyle == CFS_EXCLUDE) {
   			POINT ptCaret;

            AdjustCandBoundry(&ptWnd);
	    	if((!sImeG.IC_Trace)
	    	  || (!GetCaretPos(&ptCaret))) {

			    if(GetCompWnd(hUIWnd)) {
		            ptWnd.x = ptWnd.y = 0;
            		ClientToScreen(lpIMC->hWnd, &ptWnd);
        			ptWnd.x -= lpImeL->cxCompBorder + 1;
        			ptWnd.y -= lpImeL->cyCompBorder + 1;
				} else {
        			ptWnd.x = lpImeL->cxCompBorder + 1;
        			ptWnd.y = lpImeL->cyCompBorder + 1;
				}
	   			CalcCandPos(hIMC, hUIWnd, &ptWnd);

		        lpIMC->cfCandForm[0].dwStyle |= CFS_CANDIDATEPOS;
		        lpIMC->cfCandForm[0].ptCurrentPos = ptWnd;
		        ScreenToClient(lpIMC->hWnd, &lpIMC->cfCandForm[0].ptCurrentPos);
			} else {
				AdjustCandPos(hIMC, &ptWnd);
			}
        } else if (lpIMC->cfCandForm[0].dwStyle == CFS_CANDIDATEPOS) {
            AdjustCandBoundry(&ptWnd);
        } else {
	        if (lpUIPrivate->nShowCompCmd != SW_HIDE) {
	            ptWnd.x = ptWnd.y = 0;
	            ClientToScreen(lpUIPrivate->hCompWnd, &ptWnd);
	        } else {
	            ptWnd = lpIMC->cfCompForm.ptCurrentPos;
	            ClientToScreen(lpIMC->hWnd, &ptWnd);
	        }

	        ptWnd.x -= lpImeL->cxCompBorder + 1;
	        ptWnd.y -= lpImeL->cyCompBorder + 1;

	   		CalcCandPos(hIMC, hUIWnd, &ptWnd);

	        lpIMC->cfCandForm[0].dwStyle |= CFS_CANDIDATEPOS;
	        lpIMC->cfCandForm[0].ptCurrentPos = ptWnd;
	        ScreenToClient(lpIMC->hWnd, &lpIMC->cfCandForm[0].ptCurrentPos);
        }
    } else {
        // make cand windows trace comp window !
        if (lpUIPrivate->nShowCompCmd != SW_HIDE) {
            ptWnd.x = ptWnd.y = 0;
            ClientToScreen(lpUIPrivate->hCompWnd, &ptWnd);
        } else {
            ptWnd = lpIMC->cfCompForm.ptCurrentPos;
            ClientToScreen(lpIMC->hWnd, &ptWnd);
        }

        ptWnd.x -= lpImeL->cxCompBorder + 1;
        ptWnd.y -= lpImeL->cyCompBorder + 1;

   		CalcCandPos(hIMC, hUIWnd, &ptWnd);

        lpIMC->cfCandForm[0].dwStyle |= CFS_CANDIDATEPOS;
        lpIMC->cfCandForm[0].ptCurrentPos = ptWnd;
        ScreenToClient(lpIMC->hWnd, &lpIMC->cfCandForm[0].ptCurrentPos);
    }

    ImmUnlockIMC(hIMC);

    if (lpUIPrivate->hCandWnd) {
        SetWindowPos(lpUIPrivate->hCandWnd, NULL,
            ptWnd.x, ptWnd.y,
            0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOZORDER);
    } else {
        lpUIPrivate->hCandWnd = CreateWindowEx(
                0L,
	            szCandClassName, NULL, 
                WS_POPUP|WS_DISABLED|WS_BORDER,
				ptWnd.x,
				ptWnd.y,
				sImeG.xCandWi, sImeG.yCandHi,
				hUIWnd, (HMENU)NULL, hInst, NULL);

        SetWindowLong(lpUIPrivate->hCandWnd, UI_MOVE_OFFSET,
            WINDOW_NOT_DRAG);
        SetWindowLong(lpUIPrivate->hCandWnd, UI_MOVE_XY, 0L);
    }

    ShowCand(hUIWnd, SW_SHOWNOACTIVATE);

OpenCandUnlockUIPriv:
    GlobalUnlock(hUIPrivate);
    return;
}

/**********************************************************************/
/* CloseCand                                                          */
/**********************************************************************/
void PASCAL CloseCand(
    HWND hUIWnd)
{
    uOpenCand = 0;
    ShowCand(hUIWnd, SW_HIDE);
    return;
}

/**********************************************************************/
/* DestroyCandWindow                                                  */
/**********************************************************************/
void PASCAL DestroyCandWindow(
    HWND hCandWnd)
{
    HGLOBAL  hUIPrivate;
    LPUIPRIV lpUIPrivate;

    if (GetWindowLong(hCandWnd, UI_MOVE_OFFSET) != WINDOW_NOT_DRAG) {
        // undo the drag border
        DrawDragBorder(hCandWnd,
            GetWindowLong(hCandWnd, UI_MOVE_XY),
            GetWindowLong(hCandWnd, UI_MOVE_OFFSET),
			sImeG.rcWorkAreaCa);
    }

    hUIPrivate = (HGLOBAL)GetWindowLong(GetWindow(hCandWnd, GW_OWNER),
        IMMGWL_PRIVATE);
    if (!hUIPrivate) {          // can not darw candidate window
        return;
    }

    lpUIPrivate = (LPUIPRIV)GlobalLock(hUIPrivate);
    if (!lpUIPrivate) {         // can not draw candidate window
        return;
    }

    lpUIPrivate->nShowCandCmd = SW_HIDE;

    lpUIPrivate->hCandWnd = (HWND)NULL;

    GlobalUnlock(hUIPrivate);
    return;
}

/**********************************************************************/
/* MouseSelectCandStr()                                               */
/**********************************************************************/
void PASCAL MouseSelectCandStr(
    HWND    hCandWnd,
    LPPOINT lpCursor)
{
    HIMC            hIMC;
    LPINPUTCONTEXT  lpIMC;
    LPCANDIDATEINFO lpCandInfo;
    LPCANDIDATELIST lpCandList;
    DWORD           dwValue;

    hIMC = (HIMC)GetWindowLong(GetWindow(hCandWnd, GW_OWNER), IMMGWL_IMC);
    if (!hIMC) {
        return;
    }

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return;
    }

    if (!lpIMC->hCandInfo) {
        ImmUnlockIMC(hIMC);
        return;
    }

    lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo);
    if (!lpCandInfo) {
        ImmUnlockIMC(hIMC);
        return;
    }

    dwValue = (lpCursor->y - sImeG.rcCandText.top) / sImeG.yChiCharHi;

    lpCandList = (LPCANDIDATELIST)((LPBYTE)lpCandInfo +
        lpCandInfo->dwOffset[0]);

    dwValue = dwValue + lpCandList->dwSelection /
        lpCandList->dwPageSize * lpCandList->dwPageSize;

    if (dwValue >= lpCandList->dwCount) {
        // invalid choice
        MessageBeep((UINT)-1);
    } else {
        NotifyIME(hIMC, NI_SELECTCANDIDATESTR, 0, dwValue);
    }

    ImmUnlockIMCC(lpIMC->hCandInfo);

    ImmUnlockIMC(hIMC);

    return;
}

/**********************************************************************/
/* CandPageDownUP()                                                   */
/**********************************************************************/
void PASCAL CandPageDownUP(
    HWND hCandWnd,
    UINT uCandDownUp)
{
    HIMC            hIMC;
    LPINPUTCONTEXT  lpIMC;
    LPPRIVCONTEXT   lpImcP;
    LPCANDIDATEINFO lpCandInfo;
    LPCANDIDATELIST lpCandList;
    HDC             hDC;
    HBITMAP         hCandHpBmp, hCandUpBmp, hCandDpBmp, hCandEpBmp;
    HBITMAP         hOldBmp;
    HDC             hMemDC;

	// change candlist
    hIMC = (HIMC)GetWindowLong(GetWindow(hCandWnd, GW_OWNER), IMMGWL_IMC);
    if (!hIMC) {
        return;
    }

    // get lpIMC
    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return;
    }

    // get lpImcP
    lpImcP = (LPPRIVCONTEXT)ImmLockIMCC(lpIMC->hPrivate);
    if (!lpImcP) {
        return;
    }

    // get lpCandInfo
    lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo);

    if (!lpCandInfo) {
        return;
    }
                                                 
    // get lpCandList and init dwCount & dwSelection
    lpCandList = (LPCANDIDATELIST)
        ((LPBYTE)lpCandInfo + lpCandInfo->dwOffset[0]);
	
	switch(uCandDownUp) {
	case uCandHome:
		ChooseCand(0x24, lpIMC, lpCandInfo, lpImcP);
        NotifyIME(hIMC, NI_CHANGECANDIDATELIST, 0, 0);
		break;
	case uCandUp:
		ChooseCand('-', lpIMC, lpCandInfo, lpImcP);
        NotifyIME(hIMC, NI_CHANGECANDIDATELIST, 0, 0);
		break;
	case uCandDown:
		ChooseCand('=', lpIMC, lpCandInfo, lpImcP);
        NotifyIME(hIMC, NI_CHANGECANDIDATELIST, 0, 0);
		break;
	case uCandEnd:
		ChooseCand(0x23, lpIMC, lpCandInfo, lpImcP);
        NotifyIME(hIMC, NI_CHANGECANDIDATELIST, 0, 0);
		break;
	default:
		break;
	}

    ImmUnlockIMCC(lpIMC->hPrivate);
    ImmUnlockIMCC(lpIMC->hCandInfo);
    ImmUnlockIMC(hIMC);

    // draw button down
	hDC = GetDC(hCandWnd);
    hCandHpBmp = LoadBitmap(hInst, "CandHp");
    hCandUpBmp = LoadBitmap(hInst, "CandUp");
    hCandDpBmp = LoadBitmap(hInst, "CandDp");
    hCandEpBmp = LoadBitmap(hInst, "CandEp");

    hMemDC = CreateCompatibleDC(hDC);

	switch(uCandDownUp) {
	case uCandHome:
	    hOldBmp = SelectObject(hMemDC, hCandHpBmp);
	    BitBlt(hDC, sImeG.rcCandBTH.left, sImeG.rcCandBTH.top,
	        sImeG.rcCandBTH.right - sImeG.rcCandBTH.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);
		break;
	case uCandUp:
	    hOldBmp = SelectObject(hMemDC, hCandUpBmp);
	    BitBlt(hDC, sImeG.rcCandBTU.left, sImeG.rcCandBTU.top,
	        sImeG.rcCandBTU.right - sImeG.rcCandBTU.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);
		break;
	case uCandDown:
	    hOldBmp = SelectObject(hMemDC, hCandDpBmp);
	    BitBlt(hDC, sImeG.rcCandBTD.left, sImeG.rcCandBTD.top,
	        sImeG.rcCandBTD.right - sImeG.rcCandBTD.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);

		break;
	case uCandEnd:
	    hOldBmp = SelectObject(hMemDC, hCandEpBmp);
	    BitBlt(hDC, sImeG.rcCandBTE.left, sImeG.rcCandBTE.top,
	        sImeG.rcCandBTE.right - sImeG.rcCandBTE.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);
		break;
	default:
		break;
	}
		
    SelectObject(hMemDC, hOldBmp);
    DeleteDC(hMemDC);
    DeleteObject(hCandEpBmp);
    DeleteObject(hCandDpBmp);
    DeleteObject(hCandUpBmp);
    DeleteObject(hCandHpBmp);
	ReleaseDC(hCandWnd, hDC);

	return;
}

/**********************************************************************/
/* CandSetCursor()                                                    */
/**********************************************************************/
void PASCAL CandSetCursor(
    HWND   hCandWnd,
    LPARAM lParam)
{
    POINT ptCursor;
    RECT  rcWnd;

    if (GetWindowLong(hCandWnd, UI_MOVE_OFFSET) !=
        WINDOW_NOT_DRAG) {
        SetCursor(LoadCursor(NULL, IDC_SIZEALL));
        return;
    }

    if (HIWORD(lParam) == WM_LBUTTONDOWN) {

        GetCursorPos(&ptCursor);
        ScreenToClient(hCandWnd, &ptCursor);

        if (PtInRect(&sImeG.rcCandText, ptCursor)
           && sImeG.IC_Trace) {
            SetCursor(LoadCursor(hInst, szHandCursor));
            MouseSelectCandStr(hCandWnd, &ptCursor);
            return;
        } else if (PtInRect(&sImeG.rcCandBTH, ptCursor)) {
            CandPageDownUP(hCandWnd, uCandHome);
            return;
        } else if (PtInRect(&sImeG.rcCandBTU, ptCursor)) {
            CandPageDownUP(hCandWnd, uCandUp);
            return;
        } else if (PtInRect(&sImeG.rcCandBTD, ptCursor)) {
            CandPageDownUP(hCandWnd, uCandDown);
            return;
        } else if (PtInRect(&sImeG.rcCandBTE, ptCursor)) {
            CandPageDownUP(hCandWnd, uCandEnd);
            return;
        } else {
            SetCursor(LoadCursor(NULL, IDC_SIZEALL));
        }
    } else if (HIWORD(lParam) == WM_LBUTTONUP) {
	    HDC             hDC;
	    HBITMAP         hCandHBmp, hCandUBmp, hCandDBmp, hCandEBmp;
	    HBITMAP         hOldBmp;
	    HDC             hMemDC;

		hDC = GetDC(hCandWnd);

    	hCandHBmp = LoadBitmap(hInst, "CandH");
    	hCandUBmp = LoadBitmap(hInst, "CandU");
    	hCandDBmp = LoadBitmap(hInst, "CandD");
    	hCandEBmp = LoadBitmap(hInst, "CandE");
    	hMemDC = CreateCompatibleDC(hDC);

	    hOldBmp = SelectObject(hMemDC, hCandHBmp);
	    BitBlt(hDC, sImeG.rcCandBTH.left, sImeG.rcCandBTH.top,
	        sImeG.rcCandBTH.right - sImeG.rcCandBTH.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);

	    SelectObject(hMemDC, hCandUBmp);
	    BitBlt(hDC, sImeG.rcCandBTU.left, sImeG.rcCandBTU.top,
	        sImeG.rcCandBTU.right - sImeG.rcCandBTU.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);

	    SelectObject(hMemDC, hCandDBmp);
	    BitBlt(hDC, sImeG.rcCandBTD.left, sImeG.rcCandBTD.top,
	        sImeG.rcCandBTD.right - sImeG.rcCandBTD.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);

	    SelectObject(hMemDC, hCandEBmp);
	    BitBlt(hDC, sImeG.rcCandBTE.left, sImeG.rcCandBTE.top,
	        sImeG.rcCandBTE.right - sImeG.rcCandBTE.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);

	    SelectObject(hMemDC, hOldBmp);
	    DeleteObject(hCandEBmp);
	    DeleteObject(hCandDBmp);
	    DeleteObject(hCandUBmp);
	    DeleteObject(hCandEBmp);
	    DeleteDC(hMemDC);
		ReleaseDC(hCandWnd, hDC);

        return;
    } else {

        GetCursorPos(&ptCursor);
        ScreenToClient(hCandWnd, &ptCursor);

        if (PtInRect(&sImeG.rcCandText, ptCursor)) {
            SetCursor(LoadCursor(hInst, szHandCursor));
	        return;
        } else if (PtInRect(&sImeG.rcCandBTH, ptCursor)) {
            SetCursor(LoadCursor(hInst, szHandCursor));
        } else if (PtInRect(&sImeG.rcCandBTU, ptCursor)) {
            SetCursor(LoadCursor(hInst, szHandCursor));
        } else if (PtInRect(&sImeG.rcCandBTD, ptCursor)) {
            SetCursor(LoadCursor(hInst, szHandCursor));
        } else if (PtInRect(&sImeG.rcCandBTE, ptCursor)) {
            SetCursor(LoadCursor(hInst, szHandCursor));
        } else {
            SetCursor(LoadCursor(NULL, IDC_SIZEALL));
        }

        return;
    }

    SetCapture(hCandWnd);
    GetCursorPos(&ptCursor);
    SetWindowLong(hCandWnd, UI_MOVE_XY,
        MAKELONG(ptCursor.x, ptCursor.y));
    GetWindowRect(hCandWnd, &rcWnd);
    SetWindowLong(hCandWnd, UI_MOVE_OFFSET,
        MAKELONG(ptCursor.x - rcWnd.left, ptCursor.y - rcWnd.top));

    DrawDragBorder(hCandWnd, MAKELONG(ptCursor.x, ptCursor.y),
        GetWindowLong(hCandWnd, UI_MOVE_OFFSET),sImeG.rcWorkAreaCa);

    return;
}

/**********************************************************************/
/* CandButtonUp()                                                     */
/**********************************************************************/
BOOL PASCAL CandButtonUp(
    HWND hCandWnd)
{
    LPARAM         lTmpCursor, lTmpOffset;
    POINT          pt;
    HWND           hUIWnd;
    HIMC           hIMC;
    LPINPUTCONTEXT lpIMC;

    if (GetWindowLong(hCandWnd, UI_MOVE_OFFSET) == WINDOW_NOT_DRAG) {
        return (FALSE);
    }

    lTmpCursor = GetWindowLong(hCandWnd, UI_MOVE_XY);

    // calculate the org by the offset
    lTmpOffset = GetWindowLong(hCandWnd, UI_MOVE_OFFSET);

    pt.x = (*(LPPOINTS)&lTmpCursor).x - (*(LPPOINTS)&lTmpOffset).x;
    pt.y = (*(LPPOINTS)&lTmpCursor).y - (*(LPPOINTS)&lTmpOffset).y;

    DrawDragBorder(hCandWnd, lTmpCursor, lTmpOffset, sImeG.rcWorkAreaCa);
    SetWindowLong(hCandWnd, UI_MOVE_OFFSET, WINDOW_NOT_DRAG);
    ReleaseCapture();

    hUIWnd = GetWindow(hCandWnd, GW_OWNER);

    hIMC = (HIMC)GetWindowLong(hUIWnd, IMMGWL_IMC);
    if (!hIMC) {
        return (FALSE);
    }

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return (FALSE);
    }

    AdjustCandBoundry(&pt);

    ScreenToClient(lpIMC->hWnd, &pt);

    lpIMC->cfCandForm[0].dwStyle |= CFS_CANDIDATEPOS;
    lpIMC->cfCandForm[0].ptCurrentPos = pt;

    ImmUnlockIMC(hIMC);

    PostMessage(hCandWnd, WM_IME_NOTIFY, IMN_SETCANDIDATEPOS, 0x0001);

    return (TRUE);
}

/**********************************************************************/
/* UpdateCandWindow()                                                 */
/**********************************************************************/
void PASCAL PaintCandWindow(
    HWND hCandWnd,
    HDC  hDC)
{
    HIMC            hIMC;
    LPINPUTCONTEXT  lpIMC;
    LPCANDIDATEINFO lpCandInfo;
    LPCANDIDATELIST lpCandList;
    LPPRIVCONTEXT   lpImcP;
    HGDIOBJ         hOldFont;
    DWORD           dwStart, dwEnd;
    TCHAR           szStrBuf[2 * MAXSTRLEN * sizeof(WCHAR) / sizeof(TCHAR) + 1];
    int             i;
    HBITMAP         hCandIconBmp, hCandInfBmp;
    HBITMAP         hOldBmp, hCandHBmp, hCandUBmp, hCandDBmp, hCandEBmp;
    HDC             hMemDC;

    hIMC = (HIMC)GetWindowLong(GetWindow(hCandWnd, GW_OWNER), IMMGWL_IMC);
    if (!hIMC) {
        return;
    }

    lpIMC = (LPINPUTCONTEXT)ImmLockIMC(hIMC);
    if (!lpIMC) {
        return;
    }

    if (!lpIMC->hCandInfo) {
        goto UpCandW2UnlockIMC;
    }

    lpCandInfo = (LPCANDIDATEINFO)ImmLockIMCC(lpIMC->hCandInfo);
    if (!lpCandInfo) {
        goto UpCandW2UnlockIMC;
    }

    if (!lpIMC->hPrivate) {
        goto UpCandW2UnlockCandInfo;
    }

    lpImcP = (LPPRIVCONTEXT)ImmLockIMCC(lpIMC->hPrivate);
    if (!lpImcP) {
        goto UpCandW2UnlockCandInfo;
    }

	// set font
    if (sImeG.fDiffSysCharSet) {
        LOGFONT lfFont;

        hOldFont = GetCurrentObject(hDC, OBJ_FONT);
        GetObject(hOldFont, sizeof(lfFont), &lfFont);
        lfFont.lfCharSet = NATIVE_CHARSET;
        lfFont.lfFaceName[0] = '\0';
        SelectObject(hDC, CreateFontIndirect(&lfFont));
    }

    lpCandList = (LPCANDIDATELIST)((LPBYTE)lpCandInfo +
        lpCandInfo->dwOffset[0]);

    dwStart = lpCandList->dwSelection;
    dwEnd = dwStart + lpCandList->dwPageSize;

    if (dwEnd > lpCandList->dwCount) {
        dwEnd = lpCandList->dwCount;
    }

    // draw CandWnd Layout
	if (sImeG.IC_Trace) {
		RECT rcWnd;

	    GetClientRect(hCandWnd, &rcWnd);
	    DrawConcaveRect(hDC,
		    rcWnd.left + 2,
		    rcWnd.top + UI_CANDINF + 2,
	    	rcWnd.right - 3,
	    	rcWnd.bottom - 3);
	} else {
		RECT rcWnd;

	    GetClientRect(hCandWnd, &rcWnd);
	    DrawConcaveRect(hDC,
		    sImeG.rcCandText.left - 1,
		    rcWnd.top + 2,
	    	sImeG.rcCandText.right + 1,
	    	rcWnd.bottom - 3);
	}

    SetTextColor(hDC, RGB(0x00, 0x00, 0x00));
    SetBkColor(hDC, RGB(0xC0, 0xC0, 0xC0));

    if (sImeG.IC_Trace) {
	    ExtTextOut(hDC, sImeG.rcCandText.left, sImeG.rcCandText.top,
	        ETO_OPAQUE, &sImeG.rcCandText, NULL, 0, NULL);
	    szStrBuf[0] = '1';
	    szStrBuf[1] = ':';

	    for (i = 0; dwStart < dwEnd; dwStart++, i++) {
	        int  iLen;

	        szStrBuf[0] = szDigit[i + CAND_START];

			if(sImeL.dwRegImeIndex == INDEX_GB || sImeL.dwRegImeIndex == INDEX_GBK){
		        iLen = lstrlen((LPTSTR)((LPBYTE)lpCandList +
		            lpCandList->dwOffset[dwStart]));

				if (iLen > 10 * sizeof(WCHAR) / sizeof(TCHAR)) {
					iLen = 10 * sizeof(WCHAR) / sizeof(TCHAR);
					CopyMemory(&szStrBuf[2],
						((LPBYTE)lpCandList + lpCandList->dwOffset[dwStart]),
						iLen * sizeof(TCHAR) - sizeof(TCHAR) * 2);
					szStrBuf[iLen] = '.';
					szStrBuf[iLen + 1] = '.';
				} else {
					CopyMemory(&szStrBuf[2],
						((LPBYTE)lpCandList + lpCandList->dwOffset[dwStart]),
						iLen);
				}
		   	}else if(sImeL.dwRegImeIndex == INDEX_UNICODE){
				WORD  wCode;
				wCode = *(LPUNAWORD)((LPBYTE)lpCandList + lpCandList->dwOffset[dwStart]);

				szStrBuf[2]= LOBYTE(wCode);
				szStrBuf[3]= HIBYTE(wCode);
				szStrBuf[4]='\0';
				iLen = sizeof(WORD);
			}

	        ExtTextOut(hDC, sImeG.rcCandText.left,
	            sImeG.rcCandText.top + i * sImeG.yChiCharHi,
	            (UINT)NULL, NULL,
	            szStrBuf,
	            iLen + 2, NULL);
    
	       // QW/GB info
	       {

	         int   iMyLen;
	         WORD  wCode, UniSeq; 
	         BYTE  AbSeq[5];
	         BYTE  GbSeq[5];
			 TCHAR szMyStrBuf[10 * sizeof(WCHAR) / sizeof(TCHAR)];
			 RECT  GBARInfo;

	         wCode = *(LPUNAWORD)((LPBYTE)lpCandList + lpCandList->dwOffset[dwStart]);
			 MultiByteToWideChar(936, (DWORD)NULL, (LPSTR)&wCode, 2, &UniSeq, 1);
			 			 
	         wCode = HIBYTE(wCode) | (LOBYTE(wCode) << 8);

	         wsprintf (GbSeq,"%04lx",wCode);
			
	         wCode -= 0xa0a0;

	         wsprintf (AbSeq,"%02d%02d",HIBYTE(wCode),LOBYTE(wCode));
	                                          // get AreaPosition string
			 	
		switch(sImeL.dwRegImeIndex){
		case INDEX_GB:
	            lstrcpy (szMyStrBuf,"(");
	            lstrcat (szMyStrBuf,GbSeq);
	            lstrcat (szMyStrBuf,", ");
	            lstrcat (szMyStrBuf,AbSeq);
	            lstrcat (szMyStrBuf,")");
	            iMyLen = 12;

			break;
		case INDEX_GBK:
		        lstrcpy (szMyStrBuf,"    ");
	            lstrcat (szMyStrBuf,"(");
	            lstrcat (szMyStrBuf,GbSeq);
	            lstrcat (szMyStrBuf,")");
	            iMyLen = 10;

			break;
		case INDEX_UNICODE:
	            lstrcpy (szMyStrBuf,"(");
	            lstrcat (szMyStrBuf,GbSeq);
	            lstrcat (szMyStrBuf,", ");
				wsprintf (AbSeq,"%04lx",UniSeq);  
	            lstrcat (szMyStrBuf, AbSeq);
	            lstrcat (szMyStrBuf,")");
	            iMyLen = 12;
			break;
		}
        
			 GBARInfo.top = sImeG.rcCandText.top + i * sImeG.yChiCharHi;
			 GBARInfo.left = sImeG.rcCandText.left;
			 GBARInfo.right = sImeG.rcCandText.right;
			 GBARInfo.bottom = sImeG.rcCandText.bottom;
			 DrawText(hDC, szMyStrBuf, lstrlen(szMyStrBuf),
			         &GBARInfo, DT_RIGHT | DT_SINGLELINE);
	       }
		}
	} else {
		int  nX;

	    ExtTextOut(hDC, sImeG.rcCandText.left, sImeG.rcCandText.top + 1,
	        ETO_OPAQUE, &sImeG.rcCandText, NULL, 0, NULL);
        nX = 0;
	    for (i = 0; dwStart < dwEnd; dwStart++, i++) {
	        int  iLen;
	        int j, k;
			char AnsiStr[MAXCODE+1];
		    SIZE StrSize;

	        // display numbers
	        AnsiStr[0] = szDigit[i + CAND_START];
	        AnsiStr[1] = ':';
	        AnsiStr[2] = 0;
	        ExtTextOut(hDC, sImeG.rcCandText.left + nX,
		            sImeG.rcCandText.top + 1,
					ETO_CLIPPED, &sImeG.rcCandText,
		            AnsiStr,
		            lstrlen(AnsiStr), NULL);
			if(!GetTextExtentPoint(hDC, (LPCSTR)AnsiStr, lstrlen(AnsiStr), &StrSize))
				memset(&StrSize, 0, sizeof(SIZE));
			nX += StrSize.cx;

	        // display chinese word and code
	        iLen = lstrlen((LPTSTR)((LPBYTE)lpCandList +
	            lpCandList->dwOffset[dwStart]));

            CopyMemory(szStrBuf,
                ((LPBYTE)lpCandList + lpCandList->dwOffset[dwStart]),
                iLen);

			for(j=0; j<iLen; j++) {
	        	if(szStrBuf[j] & 0x80) {
					j++;
					continue;
				}
				break;
			}

			k = j;
			for(j=0; j<iLen - k; j++) {
				AnsiStr[j] = szStrBuf[j+k];
			}

			AnsiStr[j] = 0x20;
			j++;
			AnsiStr[j] = 0;
			szStrBuf[k] = 0;


	        ExtTextOut(hDC, sImeG.rcCandText.left + nX,
		            sImeG.rcCandText.top + 1,
					ETO_CLIPPED, &sImeG.rcCandText,
		            szStrBuf,
		            lstrlen(szStrBuf), NULL);

			if(!GetTextExtentPoint(hDC, (LPCSTR)szStrBuf, lstrlen(szStrBuf), &StrSize))
				memset(&StrSize, 0, sizeof(SIZE));
			nX += StrSize.cx;

	        ExtTextOut(hDC, sImeG.rcCandText.left + nX,
		            sImeG.rcCandText.top + 1,
					ETO_CLIPPED, &sImeG.rcCandText,
		            AnsiStr,
		            lstrlen(AnsiStr), NULL);

			if(!GetTextExtentPoint(hDC, (LPCSTR)AnsiStr, lstrlen(AnsiStr), &StrSize))
				memset(&StrSize, 0, sizeof(SIZE));
			nX += StrSize.cx;

	    }
	}
    
    // load all bitmap
	if (sImeG.IC_Trace) {
		hCandInfBmp = LoadBitmap(hInst, "Candinf");
	} else {
	    hCandInfBmp = NULL;
	}

    hCandHBmp = LoadBitmap(hInst, "CandH");
    hCandUBmp = LoadBitmap(hInst, "CandU");
    hCandDBmp = LoadBitmap(hInst, "CandD");
    hCandEBmp = LoadBitmap(hInst, "CandE");
    hCandIconBmp = LoadBitmap(hInst, "CandSel");

    hMemDC = CreateCompatibleDC(hDC);

    hOldBmp = SelectObject(hMemDC, hCandIconBmp);

    BitBlt(hDC, sImeG.rcCandIcon.left, sImeG.rcCandIcon.top,
        sImeG.rcCandIcon.right - sImeG.rcCandIcon.left,
        STATUS_DIM_Y,
        hMemDC, 0, 0, SRCCOPY);

    if(hCandInfBmp) {
	    SelectObject(hMemDC, hCandInfBmp);

	    BitBlt(hDC, sImeG.rcCandInf.left, sImeG.rcCandInf.top,
	        sImeG.rcCandInf.right - sImeG.rcCandInf.left,
	        STATUS_DIM_Y,
	        hMemDC, 0, 0, SRCCOPY);
	}

    SelectObject(hMemDC, hCandHBmp);

    BitBlt(hDC, sImeG.rcCandBTH.left, sImeG.rcCandBTH.top,
        sImeG.rcCandBTH.right - sImeG.rcCandBTH.left,
        STATUS_DIM_Y,
        hMemDC, 0, 0, SRCCOPY);

    SelectObject(hMemDC, hCandUBmp);

    BitBlt(hDC, sImeG.rcCandBTU.left, sImeG.rcCandBTU.top,
        sImeG.rcCandBTU.right - sImeG.rcCandBTU.left,
        STATUS_DIM_Y,
        hMemDC, 0, 0, SRCCOPY);

    SelectObject(hMemDC, hCandDBmp);

    BitBlt(hDC, sImeG.rcCandBTD.left, sImeG.rcCandBTD.top,
        sImeG.rcCandBTD.right - sImeG.rcCandBTD.left,
        STATUS_DIM_Y,
        hMemDC, 0, 0, SRCCOPY);

    SelectObject(hMemDC, hCandEBmp);

    BitBlt(hDC, sImeG.rcCandBTE.left, sImeG.rcCandBTE.top,
        sImeG.rcCandBTE.right - sImeG.rcCandBTE.left,
        STATUS_DIM_Y,
        hMemDC, 0, 0, SRCCOPY);

    SelectObject(hMemDC, hOldBmp);
    DeleteDC(hMemDC);
    DeleteObject(hCandIconBmp);
    DeleteObject(hCandEBmp);
    DeleteObject(hCandDBmp);
    DeleteObject(hCandUBmp);
    DeleteObject(hCandHBmp);
    DeleteObject(hCandInfBmp);

    if (sImeG.fDiffSysCharSet) {
        DeleteObject(SelectObject(hDC, hOldFont));
    }

    ImmUnlockIMCC(lpIMC->hPrivate);
UpCandW2UnlockCandInfo:
    ImmUnlockIMCC(lpIMC->hCandInfo);
UpCandW2UnlockIMC:
    ImmUnlockIMC(hIMC);

    return;
}

/**********************************************************************/
/* CandWndProc()                                                      */
/**********************************************************************/
LRESULT CALLBACK CandWndProc(
    HWND   hCandWnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam)
{
    switch (uMsg) {
    case WM_DESTROY:
        DestroyCandWindow(hCandWnd);
    case WM_SETCURSOR:
        CandSetCursor(hCandWnd, lParam);
        break;
    case WM_MOUSEMOVE:
        if (GetWindowLong(hCandWnd, UI_MOVE_OFFSET) != WINDOW_NOT_DRAG) {
            POINT ptCursor;

            DrawDragBorder(hCandWnd,
                GetWindowLong(hCandWnd, UI_MOVE_XY),
                GetWindowLong(hCandWnd, UI_MOVE_OFFSET),
				sImeG.rcWorkAreaCa);
            GetCursorPos(&ptCursor);

			{
				HMONITOR hMonitor;
				MONITORINFO info;

				hMonitor=MonitorFromPoint(ptCursor,MONITOR_DEFAULTTOPRIMARY);
				info.cbSize=sizeof(MONITORINFO);
				GetMonitorInfo(hMonitor,&info);
				sImeG.rcWorkAreaCa=info.rcWork;
			}

            SetWindowLong(hCandWnd, UI_MOVE_XY,
                MAKELONG(ptCursor.x, ptCursor.y));
            DrawDragBorder(hCandWnd, MAKELONG(ptCursor.x, ptCursor.y),
                GetWindowLong(hCandWnd, UI_MOVE_OFFSET),
				sImeG.rcWorkAreaCa);
        } else {
            return DefWindowProc(hCandWnd, uMsg, wParam, lParam);
        }
        break;
    case WM_LBUTTONUP:
        if (!CandButtonUp(hCandWnd)) {
            return DefWindowProc(hCandWnd, uMsg, wParam, lParam);
        }
        break;
    case WM_IME_NOTIFY:
        if (wParam != IMN_SETCANDIDATEPOS) {
        } else {
            return SetCandPosition(hCandWnd);
        }
        break;
    case WM_PAINT:
        {
            HDC         hDC;
            PAINTSTRUCT ps;

            hDC = BeginPaint(hCandWnd, &ps);
            PaintCandWindow(hCandWnd, hDC);
            EndPaint(hCandWnd, &ps);
        }
        break;
    case WM_MOUSEACTIVATE:
        return (MA_NOACTIVATE);
    default:
        return DefWindowProc(hCandWnd, uMsg, wParam, lParam);
    }

    return (0L);
}
