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

📄 customlist.cpp

📁 funambol window mobile客户端源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * 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 Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
// CustomList.cpp : implementation file
//
#include "stdafx.h"
#include "ui.h"
#include "CustomList.h"
#include "uiDlg.h"
#include "HwndFunctions.h"
#include "pim/ClientSettings.h"
#include "pim/maincpp.h"
#include "notify/timed_msgbox.h"

#include "initguid.h"
#include "imgguids.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;

    // Main logo members
    mainLogo = NULL;
    mainLogoCounter = 0;
    isMainLogoAnimated = false;
}

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

    if (mainLogo) {
    	mainLogo->Release();
        mainLogo = NULL;
        CoUninitialize();
    }

    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);

    // Initialize the main logo only if it's a portal build.
    ClientSettings* cs = getRegConfig();
    if (!cs->getBrandingParams().getShowBriefcaseSource() &&
        !cs->getBrandingParams().getShowNoteSource() ) {
        //if(cs->getIsPortal()) {
        initMainLogo();
    }
}

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);
}



#if defined(_DEVICE_RESOLUTION_AWARE)
/**
 * Returns the offset of status labels from the left edge.
 * Actually we use 1/3 of the main screen width.
 */
int CCustomList::GetTextOffSet()
{
    // Get client area          *** TODO: use a class variable to store client rect ***
    CRect rect;
    GetClientRect(&rect);

    //wchar_t tmp[100];
    //wsprintf(tmp, L"Width = %d", rect.Width());
    //AfxMessageBox(tmp);

    // minimum status with = 128px (otherwise we won't read the whole labels...)
    int statusWidth = max((int)(rect.Width() * 0.74), 128);

    // 26% of the width for the source title ("Contacts")
    // 74% of the width for the source status ("Not Synchronized")
    return (rect.Width() - statusWidth);
}
#endif


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: get square dimension from panes height
    int iconSize = rectFull.Height();
    CRect rectIcon(rectFull.left,  rectFull.top, rectFull.left+iconSize, rectFull.top+iconSize);
    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()){
            // Repositioning knowing the icons are 22x22
            // (-2px to leave some space before the label, minimum offset = 0)
            int offset = max((rectIcon.Width() - 22)/2, 0);
            tempdc.DrawIcon(rectIcon.left+offset-2, rectIcon.top+offset, 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(GetTextOffSet(), 0);
        #else
            rect.OffsetRect(GetTextOffSet(), 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()) {
            // Repositioning knowing the icons are 22x22
            // (-2px to leave some space before the label, minimum offset = 0)
            int offset = max((rectIcon.Width() - 22)/2, 0);
            tempdc.DrawIcon(rectIcon.left+offset-2, rectIcon.top+offset, 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(GetTextOffSet(), 0);
        #else
            rect.OffsetRect(GetTextOffSet(), 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){
    // if the list is locked we ignore the message
    if(locked)
        return;

    // try to see if this item is selected for the 2nd time
    int oldSel, newSel;
    oldSel = GetCurSelItem();
    Default(); // call default message handler
    newSel = GetCurSelItem();

    CRect rect; GetItemRect(newSel, &rect, LVIR_BOUNDS);

    if( (rect.PtInRect(point)) &&  (newSel == oldSel) ){
        CWnd* wndMain = AfxGetMainWnd();
        if(wndMain)
            ((CuiDlg*)wndMain)->SelectSource(newSel);
    }

    // clears non-item part of list
    Invalidate();
}

void CCustomList::setText(int id, CString text ){
    CString s1;
    s1+= "("; s1+=text;
    s1+=")";

    CExtItem* pItem = getItem(id);
    if(pItem){
        pItem->setText(s1);
        InvalidatePane(id);
    }
}

CString CCustomList::getText(int id){
    CExtItem* pItem = getItem(id);

    if(pItem){
        return pItem->getText();
    }

    return CString("");
}

void CCustomList::enableItem( int id, bool enable ){
    CExtItem* pItem = getItem(id);
    pItem->setEnabled(enable);
}

void CCustomList::setState( int id, int state ){
    CExtItem* item = getItem(id);
    if(item){
        item->setItemState(state);
    }
}

void CCustomList::InvalidatePane( int id ){
    CRect rect;
    GetItemRect(idToIndex(id),rect,LVIR_BOUNDS);
    InvalidateRect(rect);

⌨️ 快捷键说明

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