📄 ppnanalistbase.cpp
字号:
#include "ppListView.h"
//////////////////////////////////////////////////////////////////////////
/////////////////// NANAListBase ///////////////////////////////
//////////////////////////////////////////////////////////////////////////
/* draw a scroll bar */
void NANAListBase::Draw_ScrollBar(NANARect rcBar,float fDisplayPercent,float fFirstItemPercent)
{
if(!NeedScrollBar()) return;
RectFill(rcBar.w_nX1,rcBar.w_nY1,rcBar.w_nX2,rcBar.w_nY2,COLOR_BKGROUND);
if(w_bMouseDownOnScrollBar_Up)
DrawICON(ICON20_SCRUP_D,rcBar.w_nX1,rcBar.w_nY1,0);
else
DrawICON(ICON20_SCRUP_U,rcBar.w_nX1,rcBar.w_nY1,0);
if(w_bMouseDownOnScrollBar_Down)
DrawICON(ICON20_SCRDOWN_D,rcBar.w_nX1,rcBar.w_nY2- GetIcon(ICON20_SCRDOWN_D)->height,0);
else
DrawICON(ICON20_SCRDOWN_U,rcBar.w_nX1,rcBar.w_nY2- GetIcon(ICON20_SCRDOWN_U)->height,0);
int nSpaceHeight = rcBar.GetHeight()-GetIcon(ICON20_SCRUP_D)->height-GetIcon(ICON20_SCRDOWN_D)->height;
int nBarHeight = nSpaceHeight * fDisplayPercent;
int nBarTop = rcBar.w_nY1+GetIcon(ICON20_SCRUP_D)->height + nSpaceHeight*fFirstItemPercent;
int nBarBottom = nBarTop+nBarHeight;
if(w_bMouseDownOnScrollBar_Up||w_bMouseDownOnScrollBar_Down)
Rect(rcBar.w_nX1+2,nBarTop,rcBar.w_nX1+GetIcon(ICON20_SCRUP_D)->width-3,nBarBottom,COLOR_BKGROUND,COLOR_BORDER);
else
Rect(rcBar.w_nX1+2,nBarTop,rcBar.w_nX1+GetIcon(ICON20_SCRUP_D)->width-3,nBarBottom,COLOR_BORDER,COLOR_BKGROUND);
w_ScrollUpRc.w_nX1 = rcBar.w_nX1;
w_ScrollUpRc.w_nY1 = rcBar.w_nY1;
w_ScrollUpRc.w_nX2 = w_ScrollUpRc.w_nX1 + GetIcon(ICON20_SCRUP_D)->width;
w_ScrollUpRc.w_nY2 = w_ScrollUpRc.w_nY1 + GetIcon(ICON20_SCRUP_D)->height;
w_ScrollDownRc.w_nX1 = rcBar.w_nX1;
w_ScrollDownRc.w_nY1 = rcBar.w_nY2- GetIcon(ICON20_SCRDOWN_D)->height;
w_ScrollDownRc.w_nX2 = w_ScrollDownRc.w_nX1 + GetIcon(ICON20_SCRDOWN_D)->width;
w_ScrollDownRc.w_nY2 = w_ScrollDownRc.w_nY1 + GetIcon(ICON20_SCRDOWN_D)->height;
}
/* Draw the list frame ,grid box ,title if need.
Align the item by the item's size and w_nStyle */
void NANAListBase::OnPaint(void)
{
NANARect rc;
NANARect LineRC;
NANARect ItemRc;
int nLineHight;
int nItemWidth;
int i;
GetWindowRect(&rc);
if (w_style & LIST_STYLE_THIN_FRAME)
{
RectFrame(rc.w_nX1,rc.w_nY1,rc.GetWidth(),rc.GetHeight());
}
if(w_style & LIST_STYLE_THICK_FRAME)
{
RectFrame(rc.w_nX1,rc.w_nY1,rc.GetWidth(),rc.GetHeight());
RectFrame(rc.w_nX1+1,rc.w_nY1+1,rc.GetWidth()-2,rc.GetHeight()-2);
}
// display title if need
if(( w_nStyleFlag & LIST_STYLE_REPORT ) == LIST_STYLE_REPORT)
{
DrawListBoxTitle(rc);
rc.w_nY1 += w_Font->GBFontSize + LIST_CAPTION_TOP_MARGIN + LIST_CAPTION_TEXT_MARGIN;
}
if(w_nItemCount == 0)
{
return ;
}
// display items
// Get scrall bar area
LineRC.w_nX1 = rc.w_nX2- LIST_SCROLL_WIDTH - LIST_FRAME_WIDTH;
LineRC.w_nX2= rc.w_nX2 - LIST_FRAME_WIDTH;
LineRC.w_nY1= rc.w_nY1 + LIST_FRAME_WIDTH + LIST_TOP_MARGIN ;
LineRC.w_nY2= rc.w_nY2 - LIST_FRAME_WIDTH;
// Get grid area
w_GridRc.w_nX1 = rc.w_nX1 + LIST_FRAME_WIDTH;
w_GridRc.w_nX2 = rc.w_nX2 - LIST_SCROLL_WIDTH - LIST_FRAME_WIDTH - 1;
w_GridRc.w_nY1 = LineRC.w_nY1;
w_GridRc.w_nY2 = LineRC.w_nY2;
// Get item size
// we think than all item have same height
NANAListItemBase * pFirst;
pFirst = GetItem(0);
pFirst->GetSize(&nItemWidth,&nLineHight);
// Get display lines
w_nDisplayLines = rc.GetHeight()/nLineHight;
// Get display item per line
if(nItemWidth < 0)
{
w_nItemPerLine = 1;
nItemWidth = w_GridRc.GetWidth();
}else
{
w_nItemPerLine = w_GridRc.GetWidth()/nItemWidth;
}
w_nDisplayItems = w_nItemPerLine * w_nDisplayLines;
w_nRealWidth =(int) ((float)w_GridRc.GetWidth()/(float)w_nItemPerLine) ; // to fill the area
w_nRealHeight =(int) ((float)w_GridRc.GetHeight()/(float)w_nDisplayLines);
// Draw bar
Draw_ScrollBar(LineRC,
(float)w_nDisplayItems/(float)w_nItemCount,
(float)w_nFirstDisplayItem/(float)w_nItemCount);
NANAListItemBase *pItem = GetItem(w_nFirstDisplayItem);
for(i=0;((i<w_nDisplayItems) && (pItem));i++)
{
// Get every Item's rec ,and draw itself
ItemRc.w_nX1 = w_GridRc.w_nX1 + w_nRealWidth * (i % w_nItemPerLine);
ItemRc.w_nX1 = ItemRc.w_nX1 + (w_nRealWidth -nItemWidth)/2;
ItemRc.w_nX2 = ItemRc.w_nX1 + nItemWidth;
ItemRc.w_nY1 = w_GridRc.w_nY1 + w_nRealHeight * (i / w_nItemPerLine);
ItemRc.w_nY1 = ItemRc.w_nY1 + (w_nRealHeight - nLineHight)/2;
ItemRc.w_nY2 = ItemRc.w_nY1 + nLineHight;
pItem->Draw(ItemRc,
(w_nSelectedItem == GetItemIndex(pItem))?ITEMSTATE_SELECTED:ITEMSTATE_NORMAL
,w_style);
pItem =(NANAListItemBase *)pItem->GetNextNode(1);
}
}
/* Delete the node by its pointer.
The indexs of the nodes behinde it are decrease by 1 */
void NANAListBase::DeleteItem(NANAListItemBase * pItem)
{
if(pItem == NULL)
{
return;
}
DEBUG_Assert(pItem->w_ParentList == this);
pItem->RemoveMe();
w_nItemCount--;
delete pItem;
}
/* Get a node pointer by index.
The node next to w_HeadNode is indexed 0 */
NANAListItemBase * NANAListBase::GetItem(int nItem)
{
//第一个node是w_ListHeader本身,因此第一个数据项是
//nItem+1
return (NANAListItemBase*)w_ListHeader.GetNextNode(nItem+1);
}
/* Get the node position of node list .
The node next to the w_HeadNode is 0. */
int NANAListBase::GetItemIndex(NANAListItemBase *pItem)
{
DEBUG_Assert(pItem->w_ParentList == this);
return (w_ListHeader.GetStepsTo(pItem)) - 1;
}
/* add a new item to the tail of node list */
void NANAListBase::AddItem(NANAListItemBase *pnewItem)
{
DEBUG_Assert(NULL != pnewItem);
pnewItem->w_ParentList = this;
w_ListTail.InsertBefore(pnewItem);
w_nItemCount ++;
}
/* Get the column's width */
int NANAListBase::GetColumnWidth(int nColumn)
{
DEBUG_Assert( nColumn<w_nColumnCount );
return w_nColumnWidth[nColumn];
}
/* Initialize how many column the list view has */
void NANAListBase::SetColumnCount(int nColumn)
{
int i;
for(i=0;i<MAX_COLUMN;i++)
{
w_cTitle[i] = -1;
w_nColumnWidth[i] = 0;
}
w_nColumnCount = nColumn;
}
/* Set the column's width */
void NANAListBase::SetColumnWidth(int nColumn,float fPercentage)
{
NANARect rct;
GetWindowRect(&rct);
w_nColumnWidth[nColumn] = (int)(fPercentage * rct.GetWidth());
}
/* Set the Column's caption */
void NANAListBase::SetColumnCaption(int nColumn,int nStringID)
{
w_cTitle[nColumn] = nStringID;
}
/* Alloc a the w_ListHeader,w_ListTail */
NANAListBase::NANAListBase()
{
int i;
w_Font = &NANA_FONT16;
w_nDisplayLines = 0;
w_nItemPerLine = 0;
w_nItemCount = 0;
w_nColumnCount = 0;
w_nFirstDisplayItem = 0;
w_ItemStyle = LIST_STYLE_STRUCT | LIST_STYLE_THICK_FRAME;
for(i=0;i<MAX_COLUMN;i++)
{
w_cTitle[i] = -1;
w_nColumnWidth[i] = 0;
}
w_ListHeader.InsertAfter(&w_ListTail);
w_nItemCount = 0 ;
w_nColumnCount = 0;
w_nSelectedItem = -1;
w_ListHeader.SetParentList(this);
w_ListTail.SetParentList(this);
w_style = LIST_STYLE_REPORT; // the defualt is report
w_bMouseDownOnScrollBar_Up = 0;
w_bMouseDownOnScrollBar_Down = 0;
}
NANAListBase::~NANAListBase()
{
}
int NANAListBase::ItemHit(int x,int y)
{
int i=-1;
int nLine,nColumn;
if(w_GridRc.IsPointInside(x,y))
{
nColumn = (x - w_GridRc.w_nX1) / w_nRealWidth;
nLine = (y - w_GridRc.w_nY1) / w_nRealHeight;
i = nLine * w_nItemPerLine + nColumn + w_nFirstDisplayItem;
}
return (i < w_nItemCount)?i:-1;
}
int NANAListBase::NeedScrollBar(void)
{
return (w_nItemCount > w_nDisplayItems);
}
void NANAListBase::OnMouseDown(int x,int y)
{
int i;
i = ItemHit(x,y);
if(i>=0)
{
w_nSelectedItem = i;
SetUIDirty(1);
}else{
if(NeedScrollBar())
{
if(w_ScrollUpRc.IsPointInside(x,y))
{
w_bMouseDownOnScrollBar_Up = 1;
SetUIDirty(1);
}else
if(w_ScrollDownRc.IsPointInside(x,y))
{
w_bMouseDownOnScrollBar_Down = 1;
SetUIDirty(1);
}
}
}
}
void NANAListBase::OnMouseUp(int x,int y)
{
int i;
//更新滚动条绘制,并且完成滚动
if(NeedScrollBar())
{
if(w_bMouseDownOnScrollBar_Down)
{
if(w_nFirstDisplayItem + w_nDisplayItems < w_nItemCount)
{
w_nFirstDisplayItem += w_nItemPerLine;
}
w_bMouseDownOnScrollBar_Down = 0;
SetUIDirty(1);
}
if(w_bMouseDownOnScrollBar_Up)
{
if(w_nFirstDisplayItem>=w_nItemPerLine)
{
w_nFirstDisplayItem -= w_nItemPerLine;
}
w_bMouseDownOnScrollBar_Up = 0;
SetUIDirty(1);
}
}
//通知父窗口
i = ItemHit(x,y);
if(i>=0)
{
NotifyWindow(GetParentWnd(),UGNC_CLICK,(int)GetItem(w_nSelectedItem),(int)w_nSelectedItem);
}else{
w_nSelectedItem = -1;
SetUIDirty(1);
}
}
void NANAListBase::DrawListBoxTitle(NANARect rc)
{
int i;
int nStringWidth;
char * pTitleString;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -