📄 tree.c
字号:
}
static void DeleteItem(TREEITEM *pItem)
{
if (pItem->SubList.pFirst != NULL) {
CELL *pChild, *pCell = pItem->SubList.pFirst;
for (; pCell != NULL;) {
pChild = pCell;
pCell = pCell->pNext;
DeleteItem(TREEITEM_FROM_CELL(pChild));
}
}
if (pItem->pParent != NULL) {
ListDel(&(pItem->pParent->SubList), &(pItem->Cell));
}
GUIFree(pItem);
}
void DeleteTreeItem(TREE *pTree, TREEITEM *pItem)
{
if (pTree->pRootItem != NULL) {
if (TreeFindItem(pTree->pRootItem, pItem) == true) {
if (pTree->pRootItem == pItem) pTree->pRootItem = NULL;
if (pTree->ItemSel == pItem) pTree->ItemSel = NULL;
DeleteItem(pItem);
SetTreeScrollBar(pTree);
}
}
}
void InsertTreeItem(TREE *pTree, TREEITEM *pPos, TREEITEM *pNew)
{
if (pTree->pRootItem != NULL) {
if (TreeFindItem(pTree->pRootItem, pPos) == true) {
if (pPos->pParent == NULL || pTree->pRootItem == pPos) return;
ListInsert(&(pPos->pParent->SubList), &(pNew->Cell), &(pPos->Cell));
pNew->pParent = pPos->pParent;
/*if (pPos->pParent->Status & TREE_ITEM_EXPAND)*/
SetTreeScrollBar(pTree);
}
}
}
void InsertTreeItemAtCurrent(TREE *pTree, TREEITEM *pItem)
{
if (pTree->ItemSel != NULL)
InsertTreeItem(pTree, pTree->ItemSel, pItem);
}
void AppendTreeItem(TREE *pTree, TREEITEM *pParent, TREEITEM *pItem)
{
if (pTree->pRootItem != NULL) {
if (TreeFindItem(pTree->pRootItem, pParent) == true) {
ListAdd(&(pParent->SubList), &(pItem->Cell));
pItem->pParent = pParent;
/*if (pParent->Status & TREE_ITEM_EXPAND)*/
SetTreeScrollBar(pTree);
}
}
}
static void TreeKeyDown(VMSG *pMsg)
{
TREE *pTree;
TREEITEM *pItem;
KEY_INF ki;
pTree = OBJ_FROM_VIEW(pMsg->pView);
if (pTree->ItemSel == NULL) return;
pItem = pTree->ItemSel;
ki.raw = pMsg->vParam;
switch (ki.inf.scan) {
case SCANCODE_CURSORBLOCKRIGHT:
case SCANCODE_CURSORRIGHT:
if (pItem->SubList.pFirst != NULL) {
if ((pItem->Status & TREE_ITEM_EXPAND) == 0) {
pItem->Status |= TREE_ITEM_EXPAND;
SetTreeScrollBar(pTree);
return;
} else {
pTree->ItemSel = TREEITEM_FROM_CELL(pItem->SubList.pFirst);
}
break;
}
return;
case SCANCODE_CURSORBLOCKLEFT:
case SCANCODE_CURSORLEFT:
if (pItem->SubList.pFirst != NULL) {
if (pItem->Status & TREE_ITEM_EXPAND) {
pItem->Status &= ~TREE_ITEM_EXPAND;
SetTreeScrollBar(pTree);
return;
}
}
if (pItem->pParent != NULL)
pTree->ItemSel = pItem->pParent;
break;
case SCANCODE_CURSORBLOCKUP:
case SCANCODE_CURSORUP:
pItem = FindPrevItem(pTree, pItem);
if (pItem == NULL) return;
pTree->ItemSel = pItem;
break;
case SCANCODE_CURSORBLOCKDOWN:
case SCANCODE_CURSORDOWN:
pItem = FindNextItem(pTree, pItem);
if (pItem == NULL) return;
pTree->ItemSel = pItem;
break;
default:
return;
}
UpdateView(pMsg->pView);
}
static CBOOL _cdecl_ TreeViewProc(VMSG *pMsg)
{
TREE *pTree = OBJ_FROM_VIEW(pMsg->pView);
POINT pos = pMsg->pt;
switch (pMsg->MsgID) {
case KEYM_KEY_DOWN:
if (pTree->Status & CTRL_FOCUSSED) {
TreeKeyDown(pMsg);
}
return (true);
case MSM_LB_DOWN:
pTree->Status |= CTRL_MOUSE_ENTER;
sel_tree_item:
if (pTree->Status & CTRL_MOUSE_ENTER) {
int res = -1;
TREEITEM *pSelItem = pTree->ItemSel;
POINT pt;
pt.x = pTree->pView->viewRect.left-pTree->Scrolbar.pHbar->Value+TREE_COFF+TREE_ITEM_XOFF;
pt.y = pTree->pView->viewRect.top-pTree->Scrolbar.pVbar->Value+TREE_COFF+TREE_ITEM_YOFF;
if (pTree->pRootItem != NULL)
res = GetTreeItem(pTree, pTree->pRootItem, &pt, &pos);
if (res == true) {
SetTreeScrollBar(pTree);
} else if (pSelItem != pTree->ItemSel && res == false) {
UpdateView(pMsg->pView);
}
}
return (true);
case MSM_LB_UP:
if (pTree->Status & CTRL_TREE_COMB) {
if (pTree->OnTreeSel != NULL)
(*(pTree->OnTreeSel))(pTree);
}
return (true);
case MSM_MS_MOVE:
if (pTree->Status & CTRL_TREE_COMB) {
goto sel_tree_item;
}
return (true);
case MSM_MS_ENTER:
SetMouseCursor(ARROW);
pTree->Status |= CTRL_MOUSE_ENTER;
return (true);
case MSM_MS_LEAVE:
pTree->Status &= ~(CTRL_MOUSE_ENTER);
return (true);
default:
return (CtrlViewProc(pMsg, TreePaint, &(pTree->Status)));
}
}
static void SetTreeItemBMP(TREEITEM *pItem, BYTE *pBMPPath)
{
}
TREEITEM * CreateTreeItem(BYTE *pStr, BYTE *pBMPPath)
{
TREEITEM *pItem;
if (pStr == NULL) return (NULL);
if (pBMPPath != NULL) {
pItem = GUIAlloc(sizeof(TREE_EX)+(16*16*4/8));
if (pItem == NULL) return (NULL);
pItem->pBMP = ((BYTE *)(pItem+1));
SetTreeItemBMP(pItem, pBMPPath);
} else {
pItem = GUIAlloc(sizeof(TREE_EX));
if (pItem == NULL) return (NULL);
pItem->pBMP = NULL;
}
pItem->Status = (pItem->pBMP != NULL)? TREE_ITEM_BMP : 0;
pItem->pParent = NULL;
InitList(&(pItem->SubList));
pItem->Cell.ppPrev = NULL;
pItem->Cell.pNext = NULL;
memcpy(pItem->Str, pStr, TREE_ITEM_STR_LEN);
pItem->Str[TREE_ITEM_STR_LEN] = 0;
pItem->pData = NULL;
return (pItem);
}
TREE * CreateTree(int left, int top, int width, int height, TREEITEM *pRoot,
VIEW* pParent, PTREESEL OnTreeSel, CWORD Style)
{
VIEW *pView;
TREE *pTree;
int i = 0;
if (pParent == NULL || pRoot == NULL)
return (NULL);
pView = CreateControl(left, top, width, height, pParent, 0,
TreeViewProc, sizeof(TREE));
if (pView == NULL) return (NULL);
pTree = OBJ_FROM_VIEW(pView);
pTree->pView = pView;
pTree->OnTreeSel = OnTreeSel;
pTree->textColor[0] = textColor[0];
pTree->textColor[1] = textColor[1];
pTree->ObjColor = TreeColor;
pTree->selectColor = selectColor;
pTree->Status = 0;
pTree->xSize = 0;
pTree->ySize = 0;
if (Style & CTRL_TREE_COMB) pTree->Status |= CTRL_TREE_COMB;
pRoot->pParent = NULL;
pTree->pRootItem = pRoot;
pTree->ItemSel = NULL;
pTree->Scrolbar.pVbar = CreateScrollbar(pTree->pView, OnControlScroll, CTRL_ALIGN_VCENTER);
if (pTree->Scrolbar.pVbar == NULL) {
DeleteTree(pTree);
return (NULL);
}
pTree->Scrolbar.pHbar = CreateScrollbar(pTree->pView, OnControlScroll, CTRL_ALIGN_HCENTER);
if (pTree->Scrolbar.pHbar == NULL) {
DeleteTree(pTree);
return (NULL);
}
i = (pTree->Status & CTRL_TREE_COMB)? 0 : 2;
SetScrollBars(pTree->pView, &(pTree->Scrolbar), 0, 0, i);
ShowView(pTree->pView);
return (pTree);
}
const TREEITEM * GetTreeSelItem(TREE *pTree)
{
if (pTree != NULL)
return (pTree->ItemSel);
return (NULL);
}
void DeleteTree(TREE *pTree)
{
if (pTree == NULL) return;
if (pTree->pRootItem != NULL)
DeleteTreeItem(pTree, pTree->pRootItem);
DeleteView(pTree->pView);
}
/*
*******************************************************************************
* *
*******************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -