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

📄 toolbar.c

📁 labwindows编程环境下
💻 C
📖 第 1 页 / 共 3 页
字号:
        ListGetItem (newToolbar->items, &currentItem, i);
        if (!currentItem)
            continue;

        currentItem->toolbar = newToolbar;
        if (currentItem->type != kSeparator) {
            errChk (SetCtrlAttribute (newToolbar->panelId, currentItem->controlId,
                                      ATTR_CALLBACK_DATA, (void *)currentItem));
        }
    }

    errChk (InstallPanelCallback (newToolbar->panelId, ToolbarPanelCallback,
                                  (void *)newToolbar));

    *destToolbar = newToolbar;

Error :
    if (error < 0) {
        if (newToolbar)
            Toolbar_Discard (newToolbar);
    }

    return error;
}


static int SizeToolbarToItems (ToolbarType toolbar)
{
    int error = -UIENoError;
    int top = 0, left = 0, maxHeight = 0;
    int brTop, brLeft, brHeight, brWidth;
    int numItems, i;
    ToolbarItemPtr currentItem;

    if (!toolbar)
        return UIEHandleInvalid;

    numItems = (toolbar->items) ? (ListNumItems (toolbar->items)) : 0;
    for (i = 1 ; i <= numItems ; i++) {
        ListGetItem (toolbar->items, &currentItem, i);
        if (!currentItem)
            continue;

        switch (currentItem->type) {
            case kCommandButton:
            case kToggleButton:
                errChk (SetCtrlAttribute (toolbar->panelId, currentItem->controlId,
                                          ATTR_VISIBLE, currentItem->active));
                if (currentItem->active) {
                    errChk (SetCtrlAttribute (toolbar->panelId, currentItem->controlId,
                                                ATTR_TOP, top));
                    errChk (SetCtrlAttribute (toolbar->panelId, currentItem->controlId,
                                                ATTR_LEFT, left));
                    errChk (GetCtrlBoundingRect (toolbar->panelId, currentItem->controlId, &brTop,
                                                 &brLeft, &brHeight, &brWidth));
                    toolbar->itemWidth = brWidth;       /* record item width (ASSUME : all buttons are the same size) */

                    if (brHeight > maxHeight)
                        maxHeight = brHeight;

                    left += brWidth;
                    if (NextItemIsButton (toolbar->items, i))
                        left += kButtonSeparation;
                }
                break;
            case kSeparator:
                if (currentItem->active)
                    left += kSeparatorWidth;
                break;
        }
        
        currentItem->currActive = currentItem->active;
    }

    errChk (SetPanelAttribute (toolbar->panelId, ATTR_HEIGHT, maxHeight));
    errChk (SetPanelAttribute (toolbar->panelId, ATTR_WIDTH, kToolbarWidth));

    toolbar->sizeValid = 1;

Error :
    return error;
}


/*************************************************************/
/* enabled means the timer is on                             */
/* active means the help is on so the poll rate is different */
/*************************************************************/

static int SetPollTimer (ToolbarType toolbar, int enabled, int active,
                         int resetTimer)
{
    int error = -UIENoError;

    if (enabled != toolbar->helpEnabled) {
        errChk (SetCtrlAttribute (toolbar->helpPanel, toolbar->pollTimer,
                                    ATTR_ENABLED, enabled));
        toolbar->helpEnabled = enabled;
    }
    if (enabled && (toolbar->helpActive != active)) {
        errChk (SetCtrlAttribute (toolbar->helpPanel, toolbar->pollTimer,
                                  ATTR_INTERVAL,
                                  active ? kToolbarHelpActivePollPeriod
                                         : kToolbarHelpInactivePollPeriod));
        toolbar->helpActive = active;
    }

    if (resetTimer) {
        errChk (ResetTimer (toolbar->helpPanel, toolbar->pollTimer));
    }

Error :
    return error;
}


int Toolbar_Display (ToolbarType toolbar)
{
    int error = -UIENoError;

    if (!toolbar)
        return UIEHandleInvalid;

    errChk (SetPanelAttribute (toolbar->panelId, ATTR_VISIBLE, FALSE));
    if (!toolbar->sizeValid) {
        errChk (SizeToolbarToItems (toolbar));
    }
    errChk (SetPanelAttribute (toolbar->panelId, ATTR_VISIBLE, TRUE));
    errChk (SetPanelAttribute (toolbar->panelId, ATTR_ZPLANE_POSITION, 0));
    errChk (SetPollTimer (toolbar, TRUE, FALSE, TRUE));

Error :
    return error;
}


int ToolbarPanel (ToolbarType toolbar)
{
    return (toolbar) ? toolbar->panelId : UIEHandleInvalid;
}


int Toolbar_Hide (ToolbarType toolbar)
{
    int error = -UIENoError;

    if (!toolbar)
        return UIEHandleInvalid;

    HideToolbarHelp (toolbar, FALSE, FALSE);
    errChk (SetPollTimer (toolbar, FALSE, FALSE, FALSE));
    errChk (SetPanelAttribute (toolbar->panelId, ATTR_VISIBLE, 0));

Error :
    return error;
}



int Toolbar_SetAttribute (ToolbarType toolbar, int attribute, ...)
{
    int error = -UIENoError;

Error :
    return error;
}


int Toolbar_GetAttribute (ToolbarType toolbar, int attribute, ...)
{
    int error = -UIENoError;

Error :
    return error;
}



static ToolbarItemPtr gCurrentHelpItem = NULL;


static void SetCurrentHelpItem (ToolbarItemPtr itemPtr, ToolbarType toolbar)
{
    if ((!itemPtr) && (gCurrentHelpItem) &&
        (gCurrentHelpItem->toolbar != toolbar))
        return;

    gCurrentHelpItem = itemPtr;
}


static ToolbarItemPtr CurrentHelpItem (void)
{
    return gCurrentHelpItem;
}


/**********************************************************************/
/* Hidden UI Library function -- returns true if there is currently a */
/* menu open.                                                         */
/**********************************************************************/

extern int MenuIsOpen (void);


static int PollTimerCallback (int panel, int control, int event,
                              void *callbackData, int eventData1,
                              int eventData2)
{
    int error = -UIENoError;
    int x, y, panelHeight, parentWidth;
    int i, visible;
    ToolbarItemPtr itemPtr = NULL;
    ToolbarItemPtr currentHelpItem = NULL;
    ToolbarType toolbar;

    toolbar = (ToolbarType)callbackData;

    /* Ignore anything besides a tick (eg EVENT_DISCARD) */
    if (event != EVENT_TIMER_TICK)
        return (FALSE);

    /* If toolbar is not visible, do not respond to this event */
    GetPanelAttribute (toolbar->panelId, ATTR_VISIBLE, &visible);
    if (!visible)
        return (FALSE);

    if (MenuIsOpen ()) {
        HideToolbarHelp (toolbar, FALSE, FALSE);
        SetCurrentHelpItem (NULL, toolbar);

        return (FALSE);
    }

    GetRelativeMouseState (toolbar->panelId, 0, &x, &y, NULL, NULL, NULL);
    GetPanelAttribute (toolbar->panelId, ATTR_HEIGHT, &panelHeight);
    if ((x < 0) || (y < 0) || (y > panelHeight)) {
        /*****************************************************/
        /* Mouse is to the left, above, or below the toolbar */
        /*****************************************************/
        HideToolbarHelp (toolbar, FALSE, FALSE);
        SetCurrentHelpItem (NULL, toolbar);

        return (FALSE);
    }

    /***********************************************************************/
    /* We must use the toolbar panel's parent to determine if the cursor   */
    /* is over the toolbar, since the toolbar itself is ~4K pixels wide    */
    /*                                                                     */
    /* NOTE : If the parent panel is also clipped inside its parent panel, */
    /* this will not be any more useful than checking the width of the     */
    /* toolbar.                                                            */
    /***********************************************************************/
    GetPanelAttribute (toolbar->parentPanel, ATTR_WIDTH, &parentWidth);
    if (x > parentWidth) {
        HideToolbarHelp (toolbar, FALSE, FALSE);
        SetCurrentHelpItem (NULL, toolbar);

        return (FALSE);
    }

    /*******************************************************/
    /* At this point we know the mouse is over the toolbar */
    /*******************************************************/
    i = GetItemIndexFromPosition (toolbar, x);
    if (i == -1)
        HideToolbarHelp (toolbar, TRUE, FALSE);
    else {
        ListGetItem (toolbar->items, &itemPtr, i);
        currentHelpItem = CurrentHelpItem();
        if (itemPtr != currentHelpItem) {
            currentHelpItem = itemPtr;
            if (currentHelpItem->type == kSeparator)
                HideToolbarHelp (toolbar, TRUE, FALSE);
            else {
                DisplayToolbarHelp (itemPtr, y);
                SetPollTimer (toolbar, TRUE, TRUE, FALSE);
            }
        }
        SetCurrentHelpItem (currentHelpItem, toolbar);

    }

    return (FALSE);
}


static int HelpTimerCallback (int panel, int control, int event,
                                   void *callbackData, int eventData1,
                                   int eventData2)
{
    ToolbarType toolbar = (ToolbarType)callbackData;

    if (event == EVENT_TIMER_TICK)
        HideToolbarHelp (toolbar, FALSE, TRUE);

    return (0);
}


static int HideToolbarHelp (ToolbarType toolbar, int keepTracking,
                            int rememberCurrentItem)
{
    int error = -UIENoError;
    int resetTimer;

    if (!toolbar)
        goto Error;

    errChk (HidePanel (toolbar->helpPanel));

    errChk (SetPollTimer (toolbar, TRUE, keepTracking, !keepTracking));
    if (!keepTracking) {
        errChk (SetCtrlAttribute (toolbar->helpPanel, toolbar->helpTimer,
                                  ATTR_ENABLED, FALSE));
    }
    if (!rememberCurrentItem)
        SetCurrentHelpItem (NULL, toolbar);

Error :
    return error;
}


static int DisplayToolbarHelp (ToolbarItemPtr toolbarItem, int yPosition)
{
    int error = -UIENoError;
    int textMsg, panelTop, panelLeft, panelHeight, panelWidth;
    int ctrlTop, ctrlLeft, ctrlHeight, ctrlWidth, ctrlCenter;
    int rightBorder, childLeft, childTop;
    int panel, helpPanel;
    ToolbarType toolbar;

    /* No help for this control */
    if (!toolbarItem->description || !(toolbarItem->description[0]))
        goto Error;

    toolbar = toolbarItem->toolbar;
    panel = toolbar->panelId;
    if (!toolbar->helpPanel) {      /* help panel not created yet */
        errChk (CreateToolbarHelpPanel (toolbar));
    }
    helpPanel = toolbar->helpPanel;

    errChk (GetPanelAttribute (panel, ATTR_TOP, &childTop));
    errChk (GetPanelAttribute (panel, ATTR_LEFT, &childLeft));
    errChk (GetCtrlBoundingRect (panel, toolbarItem->controlId, &ctrlTop, &ctrlLeft,
                                 &ctrlHeight, &ctrlWidth));
    ctrlCenter = ctrlLeft + (ctrlWidth / 2);
    panelTop = childTop + ctrlTop + ctrlHeight + kToolbarHelpVerticalOffset;

    errChk (SetCtrlVal (helpPanel, toolbar->helpText, toolbarItem->description));
    errChk (GetCtrlBoundingRect (helpPanel, toolbar->helpText, &ctrlTop, &ctrlLeft,
                                 &ctrlHeight, &ctrlWidth));
    panelHeight = ctrlHeight;
    panelWidth = ctrlWidth;

    errChk (GetPanelAttribute (toolbar->parentPanel, ATTR_WIDTH, &rightBorder));
    panelLeft = childLeft + ctrlCenter - (panelWidth / 2);
    if (panelLeft <= 0)
        panelLeft = 1;
    if (panelLeft + panelWidth >= rightBorder)
        panelLeft = rightBorder - panelWidth - 1;

    errChk (SetPanelAttribute (helpPanel, ATTR_TOP, panelTop));
    errChk (SetPanelAttribute (helpPanel, ATTR_LEFT, panelLeft));
    errChk (SetPanelAttribute (helpPanel, ATTR_HEIGHT, panelHeight));
    errChk (SetPanelAttribute (helpPanel, ATTR_WIDTH, panelWidth));

    errChk (SetPanelAttribute (helpPanel, ATTR_ZPLANE_POSITION, 0));
    errChk (SetPanelAttribute (helpPanel, ATTR_VISIBLE, TRUE));

    errChk (ResetTimer (toolbar->helpPanel, toolbar->helpTimer));
    errChk (SetCtrlAttribute (toolbar->helpPanel, toolbar->helpTimer,
                              ATTR_ENABLED, TRUE));

Error :
    return error;
}


/***************************************************************/
/* Returns TRUE iff the next item in the item list after item i */
/* is a button.                                                 */
/***************************************************************/

static int NextItemIsButton (ListType itemList, int i)
{
    ToolbarItemPtr nextItem;
    int isButton = FALSE;
    int done = FALSE;
    int numItems;

    if (!itemList)
        return FALSE;

    numItems = ListNumItems (itemList);

    while (!done) {
        if (i >= numItems)
            return FALSE;

        ListGetItem (itemList, &nextItem, i+1);
        if (!nextItem->active) {
            i++;
            continue;
        }

        switch (nextItem->type) {
            case kCommandButton:
            case kToggleButton:

⌨️ 快捷键说明

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