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

📄 listbox.c

📁 rtCell 实时微内核-具有下列功能: 1. 完全抢占的实时微内核结构
💻 C
字号:
/*
 *******************************************************************************
 *                      The real-time kernel "rtCell"                          *
 *              Copyright 2005 taowentao, allrights reserved.                  *
 * File : Listbox.c                                                            *
 * By   : taowentao     2006-09-02  2007-05-20                                 *
 *******************************************************************************
 */

#if !defined(LISTBOX_H)
#include "giCell\Wins\include\Listbox.h"
#endif 

/*
 *******************************************************************************
 *                                                                             *
 *******************************************************************************
 */

static COLOR ListboxColor = GD_WHITE;
static COLOR selectColor = GD_BLUE;
static COLOR textColor[2] = {GD_BLACK, GD_WHITE};

/*
 *******************************************************************************
 *                                                                             *
 *******************************************************************************
 */ 

static void ListboxPaint(VIEW *pView)
{
    LISTBOX *pList; 
    int    i, orgX, orgY, y, itemH = LISTBOX_ITEM_HEIHGT;
    RECT   r, save = pView->viewRect;

    pList = OBJ_FROM_VIEW(pView);
    orgX = pList->pView->viewRect.left-pList->Scrolbar.pHbar->Value+LISTBOX_COFF;
    orgY = pList->pView->viewRect.top-pList->Scrolbar.pVbar->Value+LISTBOX_COFF;

    save.left += 2; save.right -= 2; save.top += 2; save.bottom -= 2;
    if ((pList->Status & CTRL_LISTBOX_COMB) == 0)
        r = save;
    else
        r = pView->viewRect;

    FillRect(&r, pList->ObjColor);
    if (SetPartClipRect(&save) == true) {        
        y = orgY;
        for (i = 0; i < pList->Count; i++, y += itemH) {
            if (i != pList->ItemSel && pList->ppStr[i] != NULL)
                DrawText(orgX, y, pList->ppStr[i], pList->textColor[0]);
        }

        y = orgY + itemH*pList->ItemSel;
        if (pList->ItemSel < pList->Count && pList->ppStr[pList->ItemSel] != NULL) {
            r.left = orgX, r.top = y;
            r.right = TextWidth(pList->ppStr[pList->ItemSel])+orgX-1;
            r.bottom = TextHeight(pList->ppStr[pList->ItemSel])+y-1;
            FillRect(&r, pList->selectColor);
            DrawText(orgX, y, pList->ppStr[pList->ItemSel], pList->textColor[1]);
            if (pList->Status & CTRL_FOCUSSED)
                DrawFocusRect(&r, -1, pList->selectColor);
        }
        RestoreClipRect(&save);
    }
    
    if ((pList->Status & CTRL_LISTBOX_COMB) == 0) {
        DrawDownRect(&(pView->viewRect));
    }
}

static void SetListboxScrollBar(LISTBOX *pList)
{
    int xMax, yMax, i, itemH = LISTBOX_ITEM_HEIHGT;

    yMax = itemH*pList->Count;
    xMax = TextWidth(pList->ppStr[0]);
    for (i = 1; i < pList->Count; i++) {
        int sx = TextWidth(pList->ppStr[i]);
        if (sx > xMax) xMax = sx; 
    }
    xMax += (LISTBOX_COFF<<1); yMax += (LISTBOX_COFF<<1);
    i = (pList->Status & CTRL_LISTBOX_COMB)? 0 : 2;
    SetScrollBars(pList->pView, &(pList->Scrolbar), xMax, yMax, i);
    UpdateView(pList->pView);
} 

void ListboxDelete(LISTBOX *pList, int item)
{
    int i;
    BYTE *p;

    for (i = 0; i < pList->Count; i++) { 
        if (i == item) {
            p = pList->ppStr[i];
            for (i = item+1; i < pList->Count; i++) {
                pList->ppStr[i-1] = pList->ppStr[i];
            }
            pList->Count --;
            pList->ppStr[pList->Count] = NULL;
            GUIFree(p);
            SetListboxScrollBar(pList);
            return;
        }
    }
}

void ListboxInsert(LISTBOX *pList, BYTE *s, int item)
{
    int i;
    BYTE *p;
    
    if (pList->Count >= pList->MaxItems) return;
    if (item != 0 && item > pList->Count) return;
    p = GUIAlloc(LISTBOX_STR_LEN+1);
    if (p == NULL) return;

    memcpy(p, s, LISTBOX_STR_LEN);
    p[LISTBOX_STR_LEN] = 0;
    for (i = pList->Count; i > item; i--) {
        pList->ppStr[i] = pList->ppStr[i-1];
    }
    pList->ppStr[item] = p;
    pList->Count ++;
    SetListboxScrollBar(pList);
}

void ListboxInsertCurrent(LISTBOX *pList, BYTE *s)
{
    ListboxInsert(pList, s, pList->ItemSel);
}

void ListboxAppend(LISTBOX *pList, BYTE *s)
{
    ListboxInsert(pList, s, pList->Count);
}

static void ListboxKeyDown(VMSG *pMsg)
{
    LISTBOX  *pList;
    KEY_INF ki;

    pList = OBJ_FROM_VIEW(pMsg->pView);
    ki.raw = pMsg->vParam;

    if (ki.inf.ctrl) {
        pList = pList;
    }
    
    switch (ki.inf.key) {  
    case KEY_RIGHT:
        break;
    case KEY_LEFT:
        break;
    default:
        break;
    } 
}

static CBOOL _cdecl_ ListboxViewProc(VMSG *pMsg)
{
    LISTBOX  *pList = OBJ_FROM_VIEW(pMsg->pView); 
    RECT    r = pList->pView->viewRect;
    POINT   pt = pMsg->pt;
    
    switch (pMsg->MsgID) {
    case KEYM_KEY_DOWN:
        if (pList->Status & CTRL_FOCUSSED) {
            ListboxKeyDown(pMsg);  
        }

        return (true);
    case MSM_LB_DOWN:
        pList->Status |= CTRL_MOUSE_ENTER;
sel_listbox_item:
        if (pList->Status & CTRL_MOUSE_ENTER) {
            int i, orgY = r.top-pList->Scrolbar.pVbar->Value+LISTBOX_COFF;
            i = GetItemByY(orgY, LISTBOX_ITEM_HEIHGT, pList->MaxItems, pt.y);
            if (pList->ppStr[i] != NULL && pList->ItemSel != i) {
                RECT ur = r;
                ur.top = orgY+pList->ItemSel*LISTBOX_ITEM_HEIHGT-8;
                ur.bottom = ur.top+LISTBOX_ITEM_HEIHGT+16;
                pList->ItemSel = i;
                UpdateViewRect(pMsg->pView, &ur);
                ur.top = orgY+pList->ItemSel*LISTBOX_ITEM_HEIHGT-8;
                ur.bottom = ur.top+LISTBOX_ITEM_HEIHGT+16;
                UpdateViewRect(pMsg->pView, &ur);
            }
        }
        return (true);
    case MSM_LB_UP:
        if (pList->Status & CTRL_LISTBOX_COMB) {
            if (pList->OnListboxSel != NULL)
                (*(pList->OnListboxSel))(pList);
        }
        return (true);
    case MSM_MS_MOVE: 
        if (pList->Status & CTRL_LISTBOX_COMB) {
            goto sel_listbox_item;
        } 
        return (true);
    case MSM_MS_ENTER:
        SetMouseCursor(ARROW);
        pList->Status |= CTRL_MOUSE_ENTER;
        return (true);
    case MSM_MS_LEAVE:
        pList->Status &= ~(CTRL_MOUSE_ENTER);
        return (true);
    default:
        return (CtrlViewProc(pMsg, ListboxPaint, &(pList->Status)));
    }
} 

LISTBOX* CreateListbox(int left, int top, int width, int height, VIEW* pParent,
		               PLISTBOXSEL OnListboxSel, CWORD Style, CWORD Count)
{
    LISTBOX *pList;
    VIEW    *pView;
    int i = 0;

    if (pParent == NULL) 
        return (NULL);

    pView = CreateControl(left, top, width, height, pParent, 0,
                          ListboxViewProc, sizeof(LISTBOX)+Count*sizeof(BYTE*));
    if (pView == NULL) return (NULL);
    
    pList = OBJ_FROM_VIEW(pView);
    pList->pView = pView;
    pList->OnListboxSel = OnListboxSel;
    
    pList->textColor[0] = textColor[0];
    pList->textColor[1] = textColor[1];    

    pList->ObjColor = ListboxColor;
    pList->selectColor = selectColor;
    pList->Status = Style;
    pList->ppStr = ((BYTE **)(pList+1));  
    for (i = Count; i > 0; i --) {        pList->ppStr[i] = NULL;    }    pList->ItemSel = 0;
    pList->Count = 0;
    pList->MaxItems = Count;

    pList->Scrolbar.pVbar = CreateScrollbar(pList->pView, OnControlScroll, CTRL_ALIGN_VCENTER);
    if (pList->Scrolbar.pVbar == NULL) {
        DeleteListbox(pList);
        return (NULL);
    }
    pList->Scrolbar.pHbar = CreateScrollbar(pList->pView, OnControlScroll, CTRL_ALIGN_HCENTER);
    if (pList->Scrolbar.pHbar == NULL) {
        DeleteListbox(pList);
        return (NULL);
    }
    i = (pList->Status & CTRL_LISTBOX_COMB)? 0 : 2;
    SetScrollBars(pList->pView, &(pList->Scrolbar), 0, 0, i);
    ShowView(pList->pView);

    return (pList);
}

const BYTE * GetListboxSelItem(LISTBOX *pListbox)
{
    if (pListbox != NULL)
        if (pListbox->ppStr[pListbox->ItemSel] != NULL)
            return (pListbox->ppStr[pListbox->ItemSel]);

    return (NULL);
}

void DeleteListbox(LISTBOX *pList)
{
    if (pList == NULL) return;
    DeleteView(pList->pView);
}

/*
 *******************************************************************************
 *                                                                             *
 *******************************************************************************
 */

⌨️ 快捷键说明

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