#if !defined(_VIRTUALIST_H)
#define _VIRTUALIST_H

#include "PubHeader.h"
#include "otstr.h"
#include "Array.h"
#include "MemPool.h"

/////////////////////////////////////////////////////////
//
// ObGrid        多行多列的对象容器类
//
// VirtualList   多行多列的字符串容器类
//
/////////////////////////////////////////////////////////

#ifdef _DLL_PROJECT
class CLASS_EXPORT GridItem
#else
class GridItem
#endif
{
public:
    enum { MAX_COLUMN_NUM=5 };
    
protected:
    DWORD wParam;
    WORD  lParam1;
    WORD  lParam2;
    void* pColData[MAX_COLUMN_NUM];

public:
    GridItem();
   ~GridItem(); 
    
    inline bool    SetItem(long nCol,void *data);
    inline void*   GetItem(long nCol);
    inline void*& _GetItem(long nCol);

    inline void    SetParam(DWORD param);
    inline DWORD&  GetParam();

    inline void    SetLParam1(WORD param);
    inline WORD&   GetLParam1();
    inline void    SetLParam2(WORD param);
    inline WORD&   GetLParam2();
    
    inline void    Clear();

    void operator=(const GridItem& right);
    
private:
    static void*   pNullData;
};

//////////////////////////////////////////////////////////////
#ifdef _DLL_PROJECT
template class CLASS_EXPORT TArray<GridItem>;
#endif

#ifdef _DLL_PROJECT
class CLASS_EXPORT ObGrid
#else
class ObGrid
#endif
{
public:
    ObGrid();
    virtual ~ObGrid();
    
    bool  SetColumn(int num, ...);
    char* GetColumnName(long nCol);
    
    long  GetItemCount();
    
    long  InsertItem(long nItem, DWORD param=0);    // nItem=-1 mean Add, return index of the new Item

    virtual bool DeleteItem(long nItem);
    virtual void DeleteAllItems();
    
    DWORD GetItemParam (long nItem);
    bool  SetItemParam(long nItem, DWORD param);

    WORD  GetItemLParam1(long nItem);
    bool  SetItemLParam1(long nItem, WORD param);
    WORD  GetItemLParam2(long nItem);
    bool  SetItemLParam2(long nItem, WORD param);
    
    void* GetItemData(long nItem, long nCol);
    bool  SetItemData(long nItem, long nCol, void* pData);  // 注: ObGrid不维护pData内存, 需外部维护

    long  FindItem(DWORD param);
    
protected:
    int   m_ColumnNum;
    OTSTR m_ColumnName[GridItem::MAX_COLUMN_NUM];
    
    TArray<GridItem>  m_ItemList;

    static GridItem   m_NullItem;
};

///////////////////////////////////////////////////
///////////////////////////////////////////////////
enum { LFMT_AUTOFIT=1, 
       LFMT_DIS_RIGHTSEPMARK=2,  // 显示时, 前面的col超过则截断,  最后col如果超过长度,则分割符号|不显示
       LFMT_LEFT =4,
       LFMT_RIGHT=0,
       LFMT_HIDE =8
};

#ifdef _DLL_PROJECT
class CLASS_EXPORT VirtualList: public ObGrid
#else
class VirtualList: public ObGrid
#endif
{
public:
    struct DumpAttr
    {
        WORD AttrBits;
        long nInitWidth;        // 初始设置宽度, 注: Space不包含在nWidth中
        long nNowWidth;
        long nPreSpaceLen;      // 缩进
        long nSepSpaceLen;      // 列与列间隔
        char strFormat[10];
    };

public:
    VirtualList();
    virtual ~VirtualList();

    bool   DeleteItem(long nItem);
    void   DeleteAllItems();

    char*  GetItemData(long nItem, long nCol);
    bool   SetItemData(long nItem, long nCol, char* szText);

    long   FindItem(DWORD param);
    long   FindItem(char* szText, bool bCaseSensitive, bool bOnlyInFirstCol=true);

    long   GetColumnDataWidth(long nCol);

    ///////////////////////////////////////////
    bool   DumpAttr_set(int num, ...);         // Attr, Width, nColPreSpaceLen,nColSepSpaceLen
    bool   DumpAttr_setFmtBits(int num, ...);  
    bool   DumpAttr_setColumnFmtBits(long nCol, WORD Attr);  
    void   UpdateColumnWidth();
    
    OTSTR& Dump_RowSeperator();
    bool   Dump_Header(OTSTR& strContent);
    bool   Dump_Item(long nItem, OTSTR& strContent);
    bool   Dump_Item(long nItem, long nToCol, bool bEnableSepMarkCheck, OTSTR& strContent); // 只显示到第nToCol的内容, 其他显示为空
    long   GetColDumpWidth(long nCol);
    
protected:
    CMemPool m_MemPool;     
    
    DumpAttr m_ColDumpAttr[GridItem::MAX_COLUMN_NUM];
    OTSTR    m_strRowSeperator;

    void     GenerateRowSeperator();
};

// 注: StringText的内存指针不需要外部维护

#endif 
