⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customlist.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2003-2007 Funambol, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */
// CustomList.cpp : implementation file
//
#include "stdafx.h"
#include "ui.h"
#include "CustomList.h"
#include "uiDlg.h"
#include "HwndFunctions.h"

#include "pim/maincpp.h"
#include "notify/timed_msgbox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCustomList

CCustomList::CCustomList()
{
    brushHollow.CreateStockObject(HOLLOW_BRUSH);

    nItemWidth   = 400;
    nItemHeight  = 30;

    locked = false;

}

CCustomList::~CCustomList()
{
    // release all list item.
    while( pItemList.GetCount() )
    {
        CExtItem* pItem =  pItemList.GetAt(0);
        delete pItem;
        pItemList.RemoveAt(0,1);
    }
    DestroyWindow();
}

BEGIN_MESSAGE_MAP(CCustomList, CListCtrl)
	//{{AFX_MSG_MAP(CCustomList)
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
    ON_WM_LBUTTONDOWN()
	ON_WM_CREATE()
    ON_MESSAGE(WM_SETFONT, OnSetFont)
    ON_WM_MEASUREITEM_REFLECT( )
    //ON_WM_DRAWITEM( )
    ON_WM_TIMER()
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCustomList message handlers

void CCustomList::Init()
{
    // Insert at least one column to the list control
    VERIFY(InsertColumn(0, _T(""), LVCFMT_LEFT) == 0);
	SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
}

void CCustomList::PreSubclassWindow()
{
	Init();

	CListCtrl::PreSubclassWindow();
}

int CCustomList::GetCurSelItem() const
{
    ASSERT(::IsWindow(m_hWnd));

    // Return the index of the item with the focus
    return GetNextItem(-1, LVNI_FOCUSED);
}

BOOL CCustomList::SetCurSelItem(int nIndex)
{
    ASSERT(::IsWindow(m_hWnd));

    // Set the focus to the item with the specified index
    return SetItemState(nIndex, LVIS_FOCUSED, LVIS_FOCUSED);
}

void CCustomList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    ASSERT(::IsWindow(m_hWnd));
    ASSERT(lpDrawItemStruct != 0);
    if( (! lpDrawItemStruct) || (! lpDrawItemStruct->hDC) ){
        return;
    }
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    ASSERT(pDC != NULL);
    if(!pDC){
        return;
    }

    // fix width
    lpDrawItemStruct->rcItem.right = nItemWidth;

    int nItem = lpDrawItemStruct->itemID;

    int nSavedDC = pDC->SaveDC();
    if(nSavedDC == 0)
        return;

    // a memory DC, we first draw into memory and then copy the content to the device screen
    CDC tempdc; tempdc.CreateCompatibleDC(pDC);

    // Get item image and state info
    LV_ITEM lvi;
    lvi.mask = LVIF_IMAGE | LVIF_STATE;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.stateMask = 0xFFFF;     // get all state flags
    GetItem(&lvi);

    // Should the item be highlighted
    BOOL bHighlight =  lvi.state & LVIS_FOCUSED;

    CRect rectFull(lpDrawItemStruct->rcItem);
    // icon and text rect
    //CRect rectIcon(rectFull.left,rectFull.top,rectFull.left+40,rectFull.top+40);
    CRect rectIcon(rectFull.left,rectFull.top,rectFull.left+30,rectFull.top+30);
    CRect rectText(rectIcon.right,rectFull.top,rectFull.right,rectFull.bottom);

    CExtItem* pItem = NULL;
    if(lpDrawItemStruct->itemID > (UINT)pItemList.GetUpperBound())
        return;

    pItem = pItemList.GetAt(lpDrawItemStruct->itemID);

    if(! pItem){
        LOG.error("CustomList: item is NULL.");
        return;
    }

    tempdc.SetBkMode(TRANSPARENT);

    // we initialize the memory DC
    CRect rectClient;
    CBitmap bmpTemp;

    GetClientRect(&rectClient);
    bmpTemp.CreateCompatibleBitmap(pDC,rectClient.Width(), rectClient.Height());

    HBITMAP pOldBitmap = (HBITMAP) tempdc.SelectObject(bmpTemp);

    pDC->SetBkMode(TRANSPARENT);

    if(bHighlight){
        // item has been selected - draw selected frame
        CRect rect(rectFull);

        tempdc.Rectangle(rect);

        rect.DeflateRect(1,1,1,1);

        tempdc.FillSolidRect(rect, LIST_COLOR_SELECTED_ITEM);	// fill item frame

        // draw icon
        if(pItem->getIcon()){
            tempdc.DrawIcon(rectIcon.left+2,rectIcon.top+2,pItem->getIcon());
        }

        if(pItem->isEnabled()){
            tempdc.SetTextColor(LIST_COLOR_TEXT);
        }
        else{
            tempdc.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
        }

        // we select default font from device CDC, not memory CDC
        CFont* pOldPDCFont = pDC->SelectObject(&fontBold);
        CFont* pOldtempDCFont = tempdc.SelectObject(&fontBold);

        rect.CopyRect(rectText);
        rect.DeflateRect(2,2);
        #if defined(WIN32_PLATFORM_PSPC)
            rect.OffsetRect(-6,6);
        #else
            rect.OffsetRect(-6,4);
        #endif

        if( pItem->getName() != ""){
            tempdc.DrawText(pItem->getName(), lstrlen(pItem->getName()),
                rect,DT_LEFT | DT_SINGLELINE);
        }

        tempdc.SelectObject(pOldPDCFont);
        pDC->SelectObject(pOldPDCFont);
        pOldtempDCFont->DeleteObject();

        #if defined(WIN32_PLATFORM_PSPC)
            rect.OffsetRect(OFFSET_SYNC_STATUS_TEXT_PPC, 0);
        #else
            rect.OffsetRect(OFFSET_SYNC_STATUS_TEXT_SPH, 1);
        #endif

        if( pItem->getText() != ""){
            tempdc.DrawText(pItem->getText(), lstrlen(pItem->getText()),
                rect,DT_LEFT | DT_SINGLELINE);
        }
    }
    else{
        //  NOT HIGHLIGHTED
        // item has been selected or draw all item
        // fill background for setted background color
        CRect rect(rectFull);

        tempdc.FillSolidRect(rect, LIST_COLOR_BACKGROUND);	// fill item frame

        // draw icon
        if(pItem->getIcon())
            tempdc.DrawIcon(rectIcon.left+2,rectIcon.top+2,pItem->getIcon());

        if(pItem->isEnabled()){
            tempdc.SetTextColor(LIST_COLOR_TEXT);
        }
        else{
            tempdc.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
        }

        CFont* pOldPDCFont = pDC->SelectObject(&fontBold);
        CFont* pOldtempdcFont = tempdc.SelectObject(&fontBold);

        rect.CopyRect(rectText);
        rect.DeflateRect(2,2);
        #if defined(WIN32_PLATFORM_PSPC)
            rect.OffsetRect(-6,6);
        #else
            rect.OffsetRect(-6,4);
        #endif

        if( pItem->getName() != ""){
            tempdc.DrawText(pItem->getName(),lstrlen(pItem->getName()),
                rect,DT_LEFT | DT_SINGLELINE);
        }

        tempdc.SelectObject(pOldPDCFont);
        pDC->SelectObject(pOldPDCFont);
        pOldtempdcFont->DeleteObject();

        #if defined(WIN32_PLATFORM_PSPC)
            rect.OffsetRect(OFFSET_SYNC_STATUS_TEXT_PPC, 0);
        #else
            rect.OffsetRect(OFFSET_SYNC_STATUS_TEXT_SPH, 1);
        #endif

        if( pItem->getText() != ""){
            tempdc.DrawText(pItem->getText(), lstrlen(pItem->getText()),
                rect,DT_LEFT | DT_SINGLELINE);
        }

    }

    // we finished drawing into memory, now we copy it to the device screen
    CRect rectBlt(lpDrawItemStruct->rcItem);
    pDC->BitBlt(rectBlt.TopLeft().x, rectBlt.TopLeft().y, rectBlt.Width(),
            rectBlt.Height(), &tempdc, rectBlt.TopLeft().x,rectBlt.TopLeft().y,SRCCOPY);

    tempdc.SelectObject(pOldBitmap);
    bmpTemp.DeleteObject();

    tempdc.DeleteDC();
    //pDC->RestoreDC(nSavedDC);
}

void CCustomList::RepaintSelectedItems()
{
    CRect rcBounds, rcLabel;

    // Invalidate focused item so it can repaint
    int nItem = GetNextItem(-1, LVNI_FOCUSED);

    if(nItem != -1)
    {
        GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
        GetItemRect(nItem, rcLabel, LVIR_LABEL);
        rcBounds.left = rcLabel.left;

        InvalidateRect(rcBounds, FALSE);
    }

    // Invalidate selected items depending on LVS_SHOWSELALWAYS
    if(!(GetStyle() & LVS_SHOWSELALWAYS))
    {
        for(nItem = GetNextItem(-1, LVNI_SELECTED);
        nItem != -1; nItem = GetNextItem(nItem, LVNI_SELECTED))
        {
            GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
            GetItemRect(nItem, rcLabel, LVIR_LABEL);
            rcBounds.left = rcLabel.left;

            InvalidateRect(rcBounds, FALSE);
        }
    }
#ifdef _DEBUG
    else
        ASSERT(FALSE);
#endif

    UpdateWindow();
}

void CCustomList::OnSetFocus(CWnd* pOldWnd)
{
	CListCtrl::OnSetFocus(pOldWnd);

    // check if we are getting focus from label edit box
    if (pOldWnd != NULL && pOldWnd->GetParent() == this){
        return;
    }

    // repaint items that should change appearance
    if ((GetStyle() & LVS_TYPEMASK) == LVS_REPORT){
        RepaintSelectedItems();
    }
    #ifdef _DEBUG
    else
        ASSERT(FALSE);
    #endif
}

void CCustomList::OnKillFocus(CWnd* pNewWnd)
{
	CListCtrl::OnKillFocus(pNewWnd);

    // check if we are losing focus to label edit box
    if (pNewWnd != NULL && pNewWnd->GetParent() == this){
        return;
    }

    // repaint items that should change appearance
    if ((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
        RepaintSelectedItems();
#ifdef _DEBUG
    else
        ASSERT(FALSE);
#endif
}

void CCustomList::OnLButtonDown(UINT nFlags, CPoint point){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -