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

📄 listbox.c

📁 基于TI公司Cortex-M3的uart超级通信开发
💻 C
📖 第 1 页 / 共 2 页
字号:
//*****************************************************************************
//
// listbox.c - A listbox widget.
//
// Copyright (c) 2008-2009 Luminary Micro, Inc.  All rights reserved.
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  You may not combine
// this software with "viral" open-source software in order to form a larger
// program.  Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 5228 of the Stellaris Graphics Library.
//
//*****************************************************************************

#include "driverlib/debug.h"
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/listbox.h"

//*****************************************************************************
//
//! \addtogroup listbox_api
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
// Make sure that the abs() macro is defined.
//
//*****************************************************************************
#ifndef abs
#define abs(a) (((a) >= 0) ? (a) : (-(a)))
#endif

//*****************************************************************************
//
// Make sure min and max are defined.
//
//*****************************************************************************
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef max
#define max(a, b) (((a) < (b)) ? (b) : (a))
#endif

//*****************************************************************************
//
//! Draws the contents of a listbox.
//!
//! \param pWidget is a pointer to the listbox widget to be drawn.
//!
//! This function draws the contents of a listbox on the display.  This is
//! called in response to a \b #WIDGET_MSG_PAINT message.
//!
//! \return None.
//
//*****************************************************************************
static void
ListBoxPaint(tWidget *pWidget)
{
    tListBoxWidget *pListBox;
    tContext sCtx;
    tRectangle sWidgetRect, sLineRect;
    short sHeight;
    long lWidth;
    unsigned short usCount, usString;

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

    //
    // Convert the generic widget pointer into a listbox widget pointer.
    //
    pListBox = (tListBoxWidget *)pWidget;

    //
    // Initialize a drawing context.
    //
    GrContextInit(&sCtx, pWidget->pDisplay);
    GrContextFontSet(&sCtx, pListBox->pFont);

    //
    // Initialize the clipping region based on the extents of this listbox.
    //
    sWidgetRect = pWidget->sPosition;
    GrContextClipRegionSet(&sCtx, &sWidgetRect);

    //
    // See if the listbox outline style is selected.
    //
    if(pListBox->ulStyle & LISTBOX_STYLE_OUTLINE)
    {
        //
        // Outline the listbox with the outline color.
        //
        GrContextForegroundSet(&sCtx, pListBox->ulOutlineColor);
        GrRectDraw(&sCtx, &(pWidget->sPosition));

        //
        // Shrink the widget region by one pixel on each side and draw another
        // rectangle, this time in the background color.  This ensures that the
        // text will not interfere with the colored border.
        //
        sWidgetRect.sXMin++;
        sWidgetRect.sYMin++;
        sWidgetRect.sXMax--;
        sWidgetRect.sYMax--;
        GrContextForegroundSet(&sCtx, pListBox->ulBackgroundColor);
        GrRectDraw(&sCtx, &sWidgetRect);

        //
        // Reduce the size of the rectangle by another pixel to get the final
        // area into which we will put the text.
        //
        sWidgetRect.sXMin++;
        sWidgetRect.sYMin++;
        sWidgetRect.sXMax--;
        sWidgetRect.sYMax--;
        GrContextClipRegionSet(&sCtx, &sWidgetRect);
    }

    //
    // Start drawing at the top of the widget.
    //
    sLineRect = sWidgetRect;
    usCount = 0;
    usString = pListBox->usStartEntry;
    sHeight = GrFontHeightGet(pListBox->pFont);

    //
    // Keep drawing until we reach the bottom of the listbox or run out of
    // strings to draw.
    //
    while((sLineRect.sYMin < sWidgetRect.sYMax) &&
          (usCount < pListBox->usPopulated))
    {
        //
        // Calculate the rectangle that will enclose this line of text.
        //
        sLineRect.sYMax = sLineRect.sYMin + sHeight - 1;

        //
        // Set foreground and background colors appropriately.
        //
        GrContextBackgroundSet(&sCtx, ((usString == pListBox->sSelected) ?
                               pListBox->ulSelectedBackgroundColor :
                               pListBox->ulBackgroundColor));
        GrContextForegroundSet(&sCtx, ((usString == pListBox->sSelected) ?
                               pListBox->ulSelectedTextColor :
                               pListBox->ulTextColor));

        //
        // Draw the text.
        //用绘制背景颜色的方式进行绘制
        GrStringDraw(&sCtx, pListBox->ppcText[usString], -1, sLineRect.sXMin,
                     sLineRect.sYMin, 1);

        //
        // Determine the width of the string we just rendered.
        //
        lWidth = GrStringWidthGet(&sCtx, pListBox->ppcText[usString], -1);

        //
        // Do we need to clear the area to the right of the string?
        //将没有绘图的部分填充
        if(lWidth < (sLineRect.sXMax - sLineRect.sXMin + 1))
        {
            //
            // Yes - we need to fill the right side of this string with
            // background color.
            //
            GrContextForegroundSet(&sCtx, ((usString == pListBox->sSelected) ?
                                   pListBox->ulSelectedBackgroundColor :
                                   pListBox->ulBackgroundColor));
            sLineRect.sXMin += lWidth;
            GrRectFill(&sCtx, &sLineRect);
            sLineRect.sXMin = sWidgetRect.sXMin;
        }

        //
        // Move on to the next string.
        //
        usCount++;
        usString++;
        if(usString == pListBox->usMaxEntries)
        {
            usString = 0;
        }
        sLineRect.sYMin += sHeight;
    }

    //
    // Fill the remainder of the listbox area with the background color.
    //
    if(sLineRect.sYMin < sWidgetRect.sYMax)
    {
        //
        // Determine the rectangle to be filled.
        //
        sLineRect.sYMax = sWidgetRect.sYMax;

        //
        // Fill the rectangle with the background color.
        //
        GrContextForegroundSet(&sCtx, pListBox->ulBackgroundColor);
        GrRectFill(&sCtx, &sLineRect);
    }
}

//*****************************************************************************
//
// Handles pointer messages for a listbox widget.
//
// \param pListBox is a pointer to the listbox widget.
// \param ulMsg is the message.
// \param lX is the X coordinate of the pointer.
// \param lY is the Y coordinate of the pointer.
//
// This function receives pointer messages intended for this listbox widget
// and processes them accordingly.
//
// \return Returns a value appropriate to the supplied message.
//
//*****************************************************************************
static long
ListBoxPointer(tListBoxWidget *pListBox, unsigned long ulMsg, long lX, long lY)
{
    long lLineNum, lEntry, lVisible, lMaxUp, lMaxDown, lScroll;

    switch(ulMsg)
    {
        //
        // The touchscreen has been pressed.
        //
        case WIDGET_MSG_PTR_DOWN:
        {
            //
            // Is the pointer press within the bounds of this widget?
            //
            if(!GrRectContainsPoint(&(pListBox->sBase.sPosition), lX, lY))
            {
                //
                // This is not a message for us so return 0 to indicate that
                // we did not process it.
                //
                return(0);
            }
            else
            {
                //
                // The pointer was pressed within this control.  Remember the Y
                // coordinate and reset or scrolling flag.
                //
                pListBox->usScrolled = 0;
                pListBox->lPointerY = lY;

                //
                // Return 1 to indicate to the widget manager that we processed
                // the message.  This widget will now receive all pointer move
                // messages until the pointer is released.
                //
                return(1);
            }
        }

        //
        // The touchscreen has been released.
        //
        case WIDGET_MSG_PTR_UP:
        {
            //
            // If the pointer is still within the bounds of the control and
            // we have not scrolled the contents since the last time the
            // pointer was pressed, we assume that this is a tap rather than
            // a drag and select the element that falls beneath the current
            // pointer position.  If the pointer is outside our control, if
            // we have scrolled already or if the control is locked, don't
            // change the selection.
            //
            if((pListBox->usScrolled == 0) &&
               !(pListBox->ulStyle & LISTBOX_STYLE_LOCKED) &&
                GrRectContainsPoint(&(pListBox->sBase.sPosition), lX, lY))
            {
                //
                // It seems we need to change the selected element. What is
                // the display line number that has been clicked on?
                //
                lLineNum = (lY - (long)pListBox->sBase.sPosition.sYMin) /
                           GrFontHeightGet(pListBox->pFont);

                //
                // We now know the location of the click as a number of text
                // lines from the top of the list box.  Now determine what
                // entry is shown there, remembering that the index may wrap.
                //
                lEntry = ((long)pListBox->usStartEntry + lLineNum) %
                         pListBox->usMaxEntries;

                //
                // If this is an unpopulated entry or the current selection,
                // clear the selection.
                //
                if((lEntry >= (long)pListBox->usPopulated) ||
                   (lEntry == (long)pListBox->sSelected))
                {
                    //
                    // Yes - update the selection and force a repaint.
                    //
                    pListBox->sSelected = (short)0xFFFF;
                }
                else
                {
                    //
                    // The pointer was tapped on a valid entry other than the
                    // current selection so change the selection.
                    //
                    pListBox->sSelected = (short)lEntry;
                }

                //
                // Force a repaint of the widget.
                //
                WidgetPaint((tWidget *)pListBox);

                //
                // Tell the client that the selection changed.
                //
                if(pListBox->pfnOnChange)
                {
                    (pListBox->pfnOnChange)((tWidget *)pListBox,
                                            pListBox->sSelected);
                }
            }

            //
            // We process all pointer up messages so return 1 to tell the
            // widget manager this.
            //
            return(1);
        }

        //
        // The pointer is moving while pressed.
        //
        case WIDGET_MSG_PTR_MOVE:
        {
            //
            // How far has the pointer moved vertically from the point where it

⌨️ 快捷键说明

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