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

📄 slider.c

📁 基于TI公司Cortex-M3的uart超级通信开发
💻 C
📖 第 1 页 / 共 2 页
字号:
        sValueRect.sXMin = pWidget->sPosition.sXMin;
        sValueRect.sXMax = pWidget->sPosition.sXMax;
        sValueRect.sYMin = sPos;
        sValueRect.sYMax = pWidget->sPosition.sYMax;

        //
        // Determine the rectangle corresponding to the top (empty) portion
        // of the slider.
        //
        sEmptyRect.sXMin = pWidget->sPosition.sXMin;
        sEmptyRect.sXMax = pWidget->sPosition.sXMax;
        sEmptyRect.sYMin = pWidget->sPosition.sYMin;
        sEmptyRect.sYMax = max(sEmptyRect.sYMin, sValueRect.sYMin - 1);
    }
    else
    {
        //
        // Determine the rectangle corresponding to the bottom (value) portion
        // of the slider.
        //
        sValueRect.sYMin = pWidget->sPosition.sYMin;
        sValueRect.sYMax = pWidget->sPosition.sYMax;
        sValueRect.sXMin = pWidget->sPosition.sXMin;
        sValueRect.sXMax = sPos;

        //
        // Determine the rectangle corresponding to the top (empty) portion
        // of the slider.
        //
        sEmptyRect.sYMin = pWidget->sPosition.sYMin;
        sEmptyRect.sYMax = pWidget->sPosition.sYMax;
        sEmptyRect.sXMax = pWidget->sPosition.sXMax;
        sEmptyRect.sXMin = min(sEmptyRect.sXMax, sValueRect.sXMax + 1);
    }

    //
    // Compute the center of the slider.  This will be needed later if drawing
    // text or an image.
    //
    lX = (pWidget->sPosition.sXMin +
          ((pWidget->sPosition.sXMax - pWidget->sPosition.sXMin + 1) / 2));
    lY = (pWidget->sPosition.sYMin +
          ((pWidget->sPosition.sYMax - pWidget->sPosition.sYMin + 1) / 2));

    //
    // Get the required clipping rectangle for the active/value part of
    // the slider.
    //
    bIntersect = GrRectIntersectGet(&sClipRect, &sValueRect, &sActiveClip);

    //
    // Does any part of the value rectangle intersect with the region we are
    // supposed to be redrawing?
    //
    if(bIntersect)
    {
        //
        // Yes - we have something to draw.
        //

        //
        // Set the new clipping rectangle.
        //
        GrContextClipRegionSet(&sCtx, &sActiveClip);

        //
        // Do we need to fill the active area with a color?
        //
        if(pSlider->ulStyle & SL_STYLE_FILL)
        {
            GrContextForegroundSet(&sCtx, pSlider->ulFillColor);
            GrRectFill(&sCtx, &sValueRect);
        }

        //
        // Do we need to draw an image in the active area?
        //
        if(pSlider->ulStyle & SL_STYLE_IMG)
        {
            GrContextForegroundSet(&sCtx, pSlider->ulTextColor);
            GrContextBackgroundSet(&sCtx, pSlider->ulFillColor);
            GrImageDraw(&sCtx, pSlider->pucImage,
                        lX - (GrImageWidthGet(pSlider->pucImage) / 2),
                        lY - (GrImageHeightGet(pSlider->pucImage) / 2));
        }

        //
        // Do we need to render a text string over the top of the active area?
        //
        if(pSlider->ulStyle & SL_STYLE_TEXT)
        {
            GrContextFontSet(&sCtx, pSlider->pFont);
            GrContextForegroundSet(&sCtx, pSlider->ulTextColor);
            GrContextBackgroundSet(&sCtx, pSlider->ulFillColor);
            GrStringDrawCentered(&sCtx, pSlider->pcText, -1, lX, lY,
                                 pSlider->ulStyle & SL_STYLE_TEXT_OPAQUE);
        }
    }

    //
    // Now get the required clipping rectangle for the background portion of
    // the slider.
    //
    bIntersect = GrRectIntersectGet(&sClipRect, &sEmptyRect, &sActiveClip);

    //
    // Does any part of the background rectangle intersect with the region we
    // are supposed to be redrawing?
    //
    if(bIntersect)
    {
        //
        // Yes - we have something to draw.
        //

        //
        // Set the new clipping rectangle.
        //
        GrContextClipRegionSet(&sCtx, &sActiveClip);

        //
        // Do we need to fill the active area with a color?
        //
        if(pSlider->ulStyle & SL_STYLE_BACKG_FILL)
        {
            GrContextForegroundSet(&sCtx, pSlider->ulBackgroundFillColor);
            GrRectFill(&sCtx, &sEmptyRect);
        }

        //
        // Do we need to draw an image in the active area?
        //
        if(pSlider->ulStyle & SL_STYLE_BACKG_IMG)
        {
            GrContextForegroundSet(&sCtx, pSlider->ulBackgroundTextColor);
            GrContextBackgroundSet(&sCtx, pSlider->ulBackgroundFillColor);
            GrImageDraw(&sCtx, pSlider->pucBackgroundImage,
                    lX - (GrImageWidthGet(pSlider->pucBackgroundImage) / 2),
                    lY - (GrImageHeightGet(pSlider->pucBackgroundImage) / 2));
        }

        //
        // Do we need to render a text string over the top of the active area?
        //
        if(pSlider->ulStyle & SL_STYLE_BACKG_TEXT)
        {
            GrContextFontSet(&sCtx, pSlider->pFont);
            GrContextForegroundSet(&sCtx, pSlider->ulBackgroundTextColor);
            GrContextBackgroundSet(&sCtx, pSlider->ulBackgroundFillColor);
            GrStringDrawCentered(&sCtx, pSlider->pcText, -1, lX, lY,
                                 pSlider->ulStyle & SL_STYLE_BACKG_TEXT_OPAQUE);
        }
    }
}

//*****************************************************************************
//
//! Handles pointer events for slider.
//!
//! \param pWidget is a pointer to the slider widget.
//! \param ulMsg is the pointer event message.
//! \param lX is the X coordinate of the pointer event.
//! \param lY is the Y coordinate of the pointer event.
//!
//! This function processes pointer event messages for a slider.  This is
//! called in response to a \b #WIDGET_MSG_PTR_DOWN, \b #WIDGET_MSG_PTR_MOVE,
//! and \b #WIDGET_MSG_PTR_UP messages.
//!
//! If the message is \b #WIDGET_MSG_PTR_MOVE or is \b #WIDGET_MSG_PTR_DOWN and
//! the coordinates are within the bounds of the slider, the slider value is
//! updated and, if changed, the slider's OnChange callback function is called.
//!
//! \return Returns 1 if the message was consumed by the slider and 0
//! otherwise.
//
//*****************************************************************************
static long
SliderClick(tWidget *pWidget, unsigned long ulMsg, long lX, long lY)
{
    tSliderWidget *pSlider;
    tRectangle sRedrawRect;
    short sPos;
    long lNewVal;

    //
    // Check the arguments.
    //
    ASSERT(pWidget);

    //
    // Convert the generic widget pointer into a slider widget pointer.
    //
    pSlider = (tSliderWidget *)pWidget;

    //
    // If the slider is locked, ignore all pointer messages.
    //
    if(pSlider->ulStyle & SL_STYLE_LOCKED)
    {
        return(0);
    }

    //
    // See if the given coordinates are within the extents of the slider.
    //
    if((ulMsg == WIDGET_MSG_PTR_MOVE) ||
       ((ulMsg == WIDGET_MSG_PTR_DOWN) &&
        (lX >= pWidget->sPosition.sXMin) &&
        (lX <= pWidget->sPosition.sXMax) &&
        (lY >= pWidget->sPosition.sYMin) &&
        (lY <= pWidget->sPosition.sYMax)))
    {
        //
        // Map the pointer position to a slider value.
        //
        lNewVal = SliderPositionToValue(pSlider,
                             (pSlider->ulStyle & SL_STYLE_VERTICAL) ? lY : lX);

        //
        // Convert back to ensure that the dirty rectangle we calculate here
        // uses the same values as will be used when the widget is next
        // painted.
        //
        sPos = SliderValueToPosition(pSlider, lNewVal);

        //
        // Did the value change?
        //
        if(lNewVal != pSlider->lValue)
        {
            //
            // Yes - the value changed so report it to the app and redraw the
            // slider.
            //
            if(pSlider->pfnOnChange)
            {
                (pSlider->pfnOnChange)(pWidget, lNewVal);
            }

            //
            // Determine the rectangle that we need to redraw to update the
            // slider to the new position.
            //
            if(pSlider->ulStyle & SL_STYLE_VERTICAL)
            {
                //
                // Vertical slider case.
                //
                sRedrawRect.sYMin = min(pSlider->sPos, sPos);
                sRedrawRect.sYMax = max(pSlider->sPos, sPos);
                sRedrawRect.sXMin = pWidget->sPosition.sXMin;
                sRedrawRect.sXMax = pWidget->sPosition.sXMax;
            }
            else
            {
                //
                // Horizontal slider case.
                //
                sRedrawRect.sXMin = min(pSlider->sPos, sPos);
                sRedrawRect.sXMax = max(pSlider->sPos, sPos);
                sRedrawRect.sYMin = pWidget->sPosition.sYMin;
                sRedrawRect.sYMax = pWidget->sPosition.sYMax;
            }

            //
            // Update the widget value and position.
            //
            pSlider->lValue = lNewVal;
            pSlider->sPos = sPos;

            //
            // Redraw the area of the control that has changed.
            //
            SliderPaint(pWidget, &sRedrawRect);
        }

        //
        // These coordinates are within the extents of the slider widget.
        //
        return(1);
    }

    //
    // These coordinates are not within the extents of the slider widget.
    //
    return(0);
}

//*****************************************************************************
//
//! Handles messages for a slider widget.
//!
//! \param pWidget is a pointer to the slider widget.
//! \param ulMsg is the message.
//! \param ulParam1 is the first parameter to the message.
//! \param ulParam2 is the second parameter to the message.
//!
//! This function receives messages intended for this slider widget and
//! processes them accordingly.  The processing of the message varies based on
//! the message in question.
//!
//! Unrecognized messages are handled by calling WidgetDefaultMsgProc().
//!
//! \return Returns a value appropriate to the supplied message.
//
//*****************************************************************************
long
SliderMsgProc(tWidget *pWidget, unsigned long ulMsg,
                         unsigned long ulParam1, unsigned long ulParam2)
{
    //
    // Check the arguments.
    //
    ASSERT(pWidget);

    //
    // Determine which message is being sent.
    //
    switch(ulMsg)
    {
        //
        // The widget paint request has been sent.
        //
        case WIDGET_MSG_PAINT:
        {
            //
            // Handle the widget paint request.
            //
            SliderPaint(pWidget, &(pWidget->sPosition));

            //
            // Return one to indicate that the message was successfully
            // processed.
            //
            return(1);
        }

        //
        // One of the pointer requests has been sent.
        //
        case WIDGET_MSG_PTR_DOWN:
        case WIDGET_MSG_PTR_MOVE:
        case WIDGET_MSG_PTR_UP:
        {
            //
            // Handle the pointer request, returning the appropriate value.
            //
            return(SliderClick(pWidget, ulMsg, ulParam1, ulParam2));
        }

        //
        // An unknown request has been sent.
        //
        default:
        {
            //
            // Let the default message handler process this message.
            //
            return(WidgetDefaultMsgProc(pWidget, ulMsg, ulParam1, ulParam2));
        }
    }
}

//*****************************************************************************
//
//! Initializes a slider widget.
//!
//! \param pWidget is a pointer to the slider widget to initialize.
//! \param pDisplay is a pointer to the display on which to draw the slider.
//! \param lX is the X coordinate of the upper left corner of the slider.
//! \param lY is the Y coordinate of the upper left corner of the slider.
//! \param lWidth is the width of the slider.
//! \param lHeight is the height of the slider.
//!
//! This function initializes the provided slider widget.
//!
//! \return None.
//
//*****************************************************************************
void
SliderInit(tSliderWidget *pWidget, const tDisplay *pDisplay, long lX, long lY,
           long lWidth, long lHeight)
{
    unsigned long ulIdx;

    //
    // Check the arguments.
    //
    ASSERT(pWidget);
    ASSERT(pDisplay);

    //
    // Clear out the widget structure.
    //
    for(ulIdx = 0; ulIdx < sizeof(tSliderWidget); ulIdx += 4)
    {
        ((unsigned long *)pWidget)[ulIdx / 4] = 0;
    }

    //
    // Set the size of the slider widget structure.
    //
    pWidget->sBase.lSize = sizeof(tSliderWidget);

    //
    // Mark this widget as fully disconnected.
    //
    pWidget->sBase.pParent = 0;
    pWidget->sBase.pNext = 0;
    pWidget->sBase.pChild = 0;

    //
    // Save the display pointer.
    //
    pWidget->sBase.pDisplay = pDisplay;

    //
    // Set the extents of this slider.
    //
    pWidget->sBase.sPosition.sXMin = lX;
    pWidget->sBase.sPosition.sYMin = lY;
    pWidget->sBase.sPosition.sXMax = lX + lWidth - 1;
    pWidget->sBase.sPosition.sYMax = lY + lHeight - 1;

    //
    // Use the slider message handler to process messages to this widget.
    //
    pWidget->sBase.pfnMsgProc = SliderMsgProc;
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

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