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

📄 control.c

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

#if !defined(CONTROL_H)
#include "giCell\Wins\include\Control.h"
#endif

#if !defined(WINDOW_H)
#include "giCell\Wins\include\Window.h"
#endif

#if !defined(SCROLLBAR_H)
#include "giCell\Wins\include\Scrolbar.h"
#endif

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

CBOOL CtrlViewProc(VMSG *pMsg, PPAINT pPaintFun, CWORD *pStatus)
{  
    VMSG msg;
    
    switch (pMsg->MsgID) {
    case VMSG_PAINT:
        (*pPaintFun)(pMsg->pView); return (true);
    case VMSG_GETFOCUS:
        if (((*pStatus) & (CTRL_FOCUSSED)) == 0) {
            WINDOW *pWin = GetParentWin(pMsg->pView);
            if ((pWin->Status & WIN_FOCUSSED) == 0) {
                msg.pView = pWin->pMainView;
                msg.MsgID = VMSG_GETFOCUS;
                SendViewMsg(msg.pView, &msg);
            }

            (*pStatus) |= CTRL_FOCUSSED;
            SetFocussedView(pMsg->pView);
        } 
        break;
    case VMSG_LOSTFOCUS:
        (*pStatus) &= ~(CTRL_FOCUSSED|CTRL_MOUSE_CATCH); break;
    case MSM_MS_ENTER:
        SetMouseCursor(ARROW);
        (*pStatus) |= CTRL_MOUSE_ENTER; break;
    case MSM_MS_LEAVE:
        (*pStatus) &= ~(CTRL_MOUSE_ENTER); break;
    case MSM_LB_DOWN:
        if (((*pStatus) & CTRL_FOCUSSED)) {
            (*pStatus) |= (CTRL_MOUSE_CATCH|CTRL_MOUSE_ENTER);
        }
        break;
    default:
        return (false);
    }

    UpdateView(pMsg->pView);
    return (true);
}

void DrawFocusRect(const RECT *pRect, const int off, COLOR clr)
{
    int  i, j, w, h;
    RECT r = (*pRect);

    r.left += off; r.right -= off; r.top += off; r.bottom -= off;
    w = r.right - r.left + 1; h = r.bottom - r.top + 1;
    
    for (i = 1; i < w; i += 2) {
        SetPixel(r.left + i, r.top, clr);
    }
    for (j = 1; j < h; j += 2) {
        SetPixel(r.left, r.top + j, clr);
    }

    if ((w+h) & 1) {
        i = 0; j = 0;
    } else {
        i = 1; j = 1;
    }
    for (; i < w; i += 2) {
        SetPixel(r.right - i, r.bottom, clr);
    }
    for (; j < h; j += 2) {
        SetPixel(r.right, r.bottom - j, clr);
    }
}

void DrawDotHLine(int y, int left, int right, COLOR clr)
{
    int i, c = right-left+1;
    for (i = 0; i < c; i += 2) SetPixel(left + i, y, clr);
}

void DrawDotVLine(int x, int top, int bottom, COLOR clr)
{
    int i, c = bottom-top+1;
    for (i = 0; i < c; i += 2) SetPixel(x, top + i, clr);
}

void GetOrgFromAlign(int xSize, int ySize, const RECT *pRect, CWORD Align, POINT *pPt)
{
    int w, h;

    w = pRect->right - pRect->left + 1;
    h = pRect->bottom - pRect->top + 1;
    
    switch (GET_ALIGN_V(Align)) {
    case CTRL_ALIGN_TOP: pPt->y = pRect->top; break;
    case CTRL_ALIGN_BOTTOM: pPt->y = pRect->bottom - ySize; break;
    case CTRL_ALIGN_VCENTER: pPt->y = pRect->top + ((h-ySize) >> 1); break;
    default: pPt->y = pRect->top + ((h-ySize) >> 1);  /* default is center */
    }

    switch (GET_ALIGN_H(Align)) {
    case CTRL_ALIGN_LEFT: pPt->x = pRect->left; return;
    case CTRL_ALIGN_RIGHT: pPt->x = pRect->left - (xSize - w); return;
    case CTRL_ALIGN_HCENTER: pPt->x = pRect->left + ((w-xSize) >> 1); return;
    default: pPt->x = pRect->left + ((w-xSize) >> 1); /* default is center */
    }
}

void CtrlDrawText(const BYTE *pStr, const RECT *pRect, CWORD Align, COLOR clr)
{
    POINT pt;
    
    GetOrgFromAlign(TextWidth(pStr), TextHeight(pStr), pRect, Align, &pt);
    DrawText(pt.x, pt.y, pStr, clr); 
}

int GetRectXSize(const RECT *pRect)
{ 
    return (pRect->right - pRect->left + 1);
}

int GetRectYSize(const RECT *pRect)
{ 
    return (pRect->bottom - pRect->top + 1);
}

VIEW* CreateControl(int left, int top, int width, int height, VIEW *pParent,
                    WORD Style, PVIEWFUN pViewFun, CWORD nBytes)
{
    WINDOW *pWin;
    VIEW *pView = CreateSubView(left, top, width, height, pParent,
                                Style, pViewFun, nBytes);
    if (pView == NULL) return (NULL);
   
    pWin = GetParentWin(pView);
    if (pWin != NULL) {
        VMSG msg;
        msg.pView = pWin->pMainView;
        msg.MsgID = WINM_SCROLL;
        SendViewMsg(msg.pView, &msg);
    }

    return (pView);
}

void DrawTriangle(CWORD Align, int x, int y, int Size, int Inc, COLOR clr)
{
    if (Align & CTRL_ALIGN_VCENTER) {
        for (; Size >= 0; Size--, x += Inc)
            DrawHLine(x, y - Size, y + Size, clr);
    } else {
        for (; Size >= 0; Size--, x += Inc)
            DrawVLine(x, y - Size, y + Size, clr);
    }
}

int GetCharX(int orgX, int chIndex, const BYTE *s)
{
    int i, xSize = orgX;

    for (i = 0; i < chIndex; i ++)
        xSize += CharWidth(*(s + i));
    return (xSize);
}

int GetItemByY(const int orgY, const int ItemH, const int Max, const int yCursor)
{
    int i;

    if (yCursor <= orgY) {
        return (0);
    } else if (yCursor >= (orgY+(ItemH*Max))) {
        return (Max-1);
    } else {         
        i = ((yCursor-orgY)/ItemH);
        if (i <= 0) i = 0;
        if (i >= Max) i = (Max-1);
        return (i);
    }  
}

void SetScrollBars(VIEW *pView, void *pVHScrolbars, int xMax, int yMax, int off)
{ 
    int xSize, ySize;
    CWORD flag = 0;
    OBJ_SCROLBAR *pScrolbar = pVHScrolbars;
    SCROLLBAR *pVbar = pScrolbar->pVbar;
    SCROLLBAR *pHbar = pScrolbar->pHbar;
    RECT save, rect, *r;

    r = &(pView->viewRect); save = (*r);
    r->left += off; r->top += off;
    r->right -= off; r->bottom -= off;
    xSize = xMax; ySize = yMax;

    if (GetRectYSize(r) < ySize) {
        flag |= 1;
        r->right -= BOX_W;
    }
    if (GetRectXSize(r) < xSize) {
        flag |= 2;
        r->bottom -= BOX_W; 
    }
    if (GetRectYSize(r) < ySize) {
        if ((flag & 1) == 0) {
            flag |= 1;
            r->right -= BOX_W; 
        }
    }    
    if (GetRectXSize(r) < xSize) {
        if ((flag & 2) == 0) {
            flag |= 2;
            r->bottom -= BOX_W;
        }
    }

    if ((flag & 1) == 0) pVbar->Value = 0;
    if ((flag & 2) == 0) pHbar->Value = 0;
    if (flag == 0) {
        xSize = GetRectXSize(&save); ySize = GetRectYSize(&save);
    }
    
    pScrolbar->MaxRect.left = save.left+off-pHbar->Value;
    pScrolbar->MaxRect.top = save.top+off-pVbar->Value; 
    pScrolbar->MaxRect.right = pScrolbar->MaxRect.left+xSize-1;
    pScrolbar->MaxRect.bottom = pScrolbar->MaxRect.top+ySize-1; 
    if (pScrolbar->MaxRect.right < r->right)
        MoveRect(&(pScrolbar->MaxRect), r->right-pScrolbar->MaxRect.right, 0);
    if (pScrolbar->MaxRect.bottom < r->bottom)
        MoveRect(&(pScrolbar->MaxRect), 0, r->bottom-pScrolbar->MaxRect.bottom);
    rect = (*r);
    pView->viewRect = save;
    save.left += off; save.top += off;
    save.right -= off; save.bottom -= off;

    if (flag == 3) {
        SetScrolbar(pVbar, CTRL_PAGE, &save, &rect, &(pScrolbar->MaxRect), true);
        SetScrolbar(pHbar, CTRL_PAGE, &save, &rect, &(pScrolbar->MaxRect), true);
    } else if (flag == 1) {
        SetScrolbar(pVbar, CTRL_PAGE, &save, &rect, &(pScrolbar->MaxRect), false);
    }  else if (flag == 2) {
        SetScrolbar(pHbar, CTRL_PAGE, &save, &rect, &(pScrolbar->MaxRect), false);
    } 

    if (flag & 1) {
        if (GetRectXSize(&(pHbar->pView->viewRect)) >= BOX_H)
            if (GetRectYSize(&(pVbar->pView->viewRect)) >= BOX_H) {
                if (IsViewVisible(pVbar->pView) == false)
                    ShowView(pVbar->pView);
                goto set_hscroll_bar;
            }
    }      
    HideView(pVbar->pView);
    
set_hscroll_bar:
    if (flag & 2) {
        if (GetRectYSize(&(pVbar->pView->viewRect)) >= BOX_H) {
            if (IsViewVisible(pHbar->pView) == false)
                ShowView(pHbar->pView);
            return;
        }
    }
    HideView(pHbar->pView);
}

void _cdecl_ OnControlScroll(VIEW *pSBview, const int Off)
{
    VIEW *pParent = pSBview->pParent;
    SCROLLBAR *pScrollbar = OBJ_FROM_VIEW(pSBview);

    if (pParent == NULL) return;
    if (Off != 0) {
        RECT r = pParent->viewRect;
        if (pScrollbar->Align == CTRL_ALIGN_VCENTER)
            r.right = pSBview->viewRect.left-1;
        else
            r.bottom = pSBview->viewRect.top-1;
        UpdateViewRect(pParent, &r);
    }
}

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

⌨️ 快捷键说明

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