Beispiel #1
0
MsgRouting
AwtCheckbox::OwnerDrawItem(UINT /*ctrlId*/, DRAWITEMSTRUCT& drawInfo)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    if (env->EnsureLocalCapacity(4) < 0) {
        return mrConsume;
    }

    jobject self = GetPeer(env);
    jobject target = env->GetObjectField(self, AwtObject::targetID);

    HDC hDC = drawInfo.hDC;
    RECT rect = drawInfo.rcItem;
    int checkSize;
    UINT nState;
    SIZE size;

    jobject font = GET_FONT(target, self);
    jstring str = (jstring)env->GetObjectField(target, AwtCheckbox::labelID);
    size = AwtFont::getMFStringSize(hDC, font, str);

    jobject group = env->GetObjectField(target, AwtCheckbox::groupID);
    if (group != NULL)
        nState = DFCS_BUTTONRADIO;
    else
        nState = DFCS_BUTTONCHECK;

    if (GetState())
        nState |= DFCS_CHECKED;
    else
        nState &= ~DFCS_CHECKED;

    if (drawInfo.itemState & ODS_SELECTED)
        nState |= DFCS_PUSHED;

    if (drawInfo.itemAction & ODA_DRAWENTIRE) {
        VERIFY(::FillRect (hDC, &rect, GetBackgroundBrush()));
    }

    /* draw check mark */
    checkSize = GetCheckSize();
    RECT boxRect;

    boxRect.left = (GetRTL()) ? rect.right - checkSize : rect.left;
    boxRect.top = (rect.bottom - rect.top - checkSize)/2;
    boxRect.right = boxRect.left + checkSize;
    boxRect.bottom = boxRect.top + checkSize;
    ::DrawFrameControl(hDC, &boxRect, DFC_BUTTON, nState);

    /*
     * draw string
     *
     * 4 is a heuristic number
     */
    rect.left = rect.left + checkSize + checkSize/4;
    if (drawInfo.itemAction & ODA_DRAWENTIRE) {
        BOOL bEnabled = isEnabled();

        int x = (GetRTL()) ? rect.right - (checkSize + checkSize / 4 + size.cx)
                           : rect.left;
        int y = (rect.top + rect.bottom - size.cy) / 2;
        if (bEnabled) {
            AwtComponent::DrawWindowText(hDC, font, str, x, y);
        } else {
            AwtComponent::DrawGrayText(hDC, font, str, x, y);
        }
    }

    /* Draw focus rect */
    RECT focusRect;
    const int margin = 2; /*  2 is a heuristic number */

    focusRect.left = (GetRTL()) ? rect.right - checkSize - checkSize / 4 -
                                      2 * margin - size.cx
                                : rect.left - margin;
    focusRect.top = (rect.top+rect.bottom-size.cy)/2;
    focusRect.right = (GetRTL()) ? rect.right - checkSize - checkSize / 4 +
                                      margin
                                 : focusRect.left + size.cx + 2 * margin;
    focusRect.bottom = focusRect.top + size.cy;

    /*  draw focus rect */
    if ((drawInfo.itemState & ODS_FOCUS) &&
        ((drawInfo.itemAction & ODA_FOCUS)||
         (drawInfo.itemAction &ODA_DRAWENTIRE))) {
        VERIFY(::DrawFocusRect(hDC, &focusRect));
    }
    /*  erase focus rect */
    else if (!(drawInfo.itemState & ODS_FOCUS) &&
             (drawInfo.itemAction & ODA_FOCUS)) {
        VERIFY(::DrawFocusRect(hDC, &focusRect));
    }

    /*  Notify any subclasses */
    rect = drawInfo.rcItem;
    DoCallback("handlePaint", "(IIII)V", rect.left, rect.top,
               rect.right-rect.left, rect.bottom-rect.top);

    env->DeleteLocalRef(target);
    env->DeleteLocalRef(font);
    env->DeleteLocalRef(str);
    env->DeleteLocalRef(group);

    return mrConsume;
}
MsgRouting
AwtCheckbox::WmDrawItem(UINT /*ctrlId*/, DRAWITEMSTRUCT far& drawInfo)
{
    JNIEnv *env; 

    /* associate JNIEnv with the current thread */
    if (JVM -> AttachCurrentThread((void**) &env, 0) != 0) {
       return mrDoDefault; // CHECK: is this the right return type??
    }

    CriticalSection::Lock l(GetLock());

    jobject self = GetPeer();
    jobject target = 
       env -> GetObjectField(self, WCachedIDs.PPCObjectPeer_targetFID);

    HDC hDC = drawInfo.hDC;
    RECT rect = drawInfo.rcItem;
    int checkSize;
    UINT nState;
    SIZE size;
    jobject hJavaFont;
    jstring hJavaString;

    hJavaFont = GET_FONT(target);
    hJavaString = (jstring)env->CallObjectMethod(target,
                                  WCachedIDs.java_awt_Checkbox_getLabelMID); 
    size = AwtFont::getMFStringSize(hDC,hJavaFont,hJavaString);
 
    if (env->CallObjectMethod(target, WCachedIDs.java_awt_Checkbox_getCheckboxGroupMID) != NULL)
        nState = DFCS_BUTTONRADIO;
    else
        nState = DFCS_BUTTONCHECK;

    if (GetState())
        nState |= DFCS_CHECKED;
    else
        nState &= ~DFCS_CHECKED;

    if (drawInfo.itemState & ODS_SELECTED)
        nState |= DFCS_PUSHED;

    if (drawInfo.itemAction & ODA_DRAWENTIRE) {
        VERIFY(::FillRect (hDC, &rect, GetBackgroundBrush()));
    }

    //draw check mark
    checkSize = GetCheckSize();
    RECT boxRect;
    boxRect.left = rect.left;
    boxRect.top = (rect.bottom - rect.top - checkSize)/2;
    boxRect.right = boxRect.left + checkSize;
    boxRect.bottom = boxRect.top + checkSize;
    ::DrawFrameControl(hDC, &boxRect, DFC_BUTTON, nState);

    //draw string
    rect.left = rect.left + checkSize + checkSize/4; //4 is a heuristic number
    if (drawInfo.itemAction & ODA_DRAWENTIRE) {
        BOOL bEnabled = isEnabled();

        int x = rect.left;
        int y = (rect.top+rect.bottom-size.cy)/2;
        if (bEnabled) {
            AwtComponent::DrawWindowText(hDC, hJavaFont, hJavaString, x, y);
        } else {
            AwtComponent::DrawGrayText(hDC, hJavaFont, hJavaString, x, y);
        }
    }

    //Draw focus rect
    RECT focusRect;
    const int margin = 2; // 2 is a heuristic number
    focusRect.left = rect.left - margin;
    focusRect.top = (rect.top+rect.bottom-size.cy)/2;
    focusRect.right = focusRect.left + size.cx + 2*margin;
    focusRect.bottom = focusRect.top + size.cy;
    
    // draw focus rect
    if ((drawInfo.itemState & ODS_FOCUS) &&
        ((drawInfo.itemAction & ODA_FOCUS)||
         (drawInfo.itemAction &ODA_DRAWENTIRE))) {        
              VERIFY(::DrawFocusRect(hDC, &focusRect));
    }
    // erase focus rect
    else if (!(drawInfo.itemState & ODS_FOCUS) &&
             (drawInfo.itemAction & ODA_FOCUS)) {
        VERIFY(::DrawFocusRect(hDC, &focusRect));
    }

    // Notify any subclasses
    rect = drawInfo.rcItem;

    env -> CallVoidMethod(GetPeer(),
                          WCachedIDs.PPCComponentPeer_handlePaintMID,
                          rect.left, rect.top, 
                          rect.right-rect.left, rect.bottom-rect.top);

    return mrConsume;
}