📄 toolbar.c
字号:
isButton = TRUE;
done = TRUE;
break;
default:
done = TRUE;
break;
}
}
return isButton;
}
/************************************************************************/
/* Return the index associated with the given x position in the toolbar */
/* panel. This is used for the toolbar help so, given the mouse */
/* coordinates relative to the panel, we can compute which item the */
/* cursor is currently over. (return -1 if no item matches) */
/************************************************************************/
static int GetItemIndexFromPosition (ToolbarType toolbar, int xPosition)
{
int numItems, i;
ToolbarItemPtr itemPtr;
int left = 0;
if (!toolbar || (xPosition < 0))
return -1;
numItems = (toolbar->items) ? (ListNumItems (toolbar->items)) : 0;
for (i = 1 ; i <= numItems ; i++) {
ListGetItem (toolbar->items, &itemPtr, i);
if (itemPtr->currActive) {
switch (itemPtr->type) {
case kCommandButton:
case kToggleButton:
left += toolbar->itemWidth;
if (NextItemIsButton (toolbar->items, i))
left += kButtonSeparation;
break;
case kSeparator:
left += kSeparatorWidth;
break;
}
}
if (xPosition < left)
return i;
}
return -1;
}
static int ToolbarPanelCallback (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
ToolbarItemPtr itemPtr = NULL;
ToolbarType toolbar = NULL;
int index;
toolbar = (ToolbarType)callbackData;
switch (event) {
case EVENT_RIGHT_CLICK:
case EVENT_RIGHT_DOUBLE_CLICK:
SetCurrentHelpItem (NULL, toolbar);
SetPollTimer (toolbar, TRUE, TRUE, FALSE);
break;
case EVENT_DISCARD:
DiscardToolbarResources (toolbar);
break;
}
return (FALSE);
}
static int GenericToolbarItemCallbackFunc (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
ToolbarItemPtr toolbarItem = NULL;
int swallow = FALSE;
toolbarItem = (ToolbarItemPtr)callbackData;
switch (event) {
case EVENT_COMMIT:
HideToolbarHelp (toolbarItem->toolbar, FALSE, TRUE);
if (toolbarItem->callbackFunc) {
switch (toolbarItem->callbackType) {
case kMenuCallback:
((MenuCallbackPtr)toolbarItem->callbackFunc)
(toolbarItem->toolbar->menuBar,
toolbarItem->menuItem,
toolbarItem->callbackData,
toolbarItem->toolbar->parentPanel);
break;
case kControlCallback:
swallow = ((CtrlCallbackPtr)toolbarItem->callbackFunc)
(panel, control, event,
toolbarItem->callbackData,
eventData1, eventData2);
break;
}
}
break;
}
return (0);
}
/**********************************************************************/
/* Assumes that the toolbar and the type have already been validated. */
/**********************************************************************/
static int CreateNewToolbarItemCtrl (ToolbarItemPtr toolbarItem, char imageFile[])
{
int error = -UIENoError;
int ctrlType;
if (!toolbarItem)
return UIEHandleInvalid;
if (toolbarItem->type == kSeparator) /* Don't need a control */
goto Error;
switch (toolbarItem->type) {
case kCommandButton:
ctrlType = CTRL_PICTURE_COMMAND_BUTTON;
break;
case kToggleButton:
ctrlType = CTRL_PICTURE_TOGGLE_BUTTON;
break;
}
errChk (toolbarItem->controlId = NewCtrl (toolbarItem->toolbar->panelId,
ctrlType, "", 0, 0));
errChk (SetCtrlAttribute (toolbarItem->toolbar->panelId,
toolbarItem->controlId, ATTR_HEIGHT,
kToolbarButtonSize));
errChk (SetCtrlAttribute (toolbarItem->toolbar->panelId,
toolbarItem->controlId, ATTR_WIDTH,
kToolbarButtonSize));
errChk (SetCtrlAttribute (toolbarItem->toolbar->panelId,
toolbarItem->controlId, ATTR_FAST_DRAW_BUTTON,
TRUE));
errChk (SetCtrlAttribute (toolbarItem->toolbar->panelId,
toolbarItem->controlId, ATTR_IMAGE_FILE,
imageFile));
errChk (InstallCtrlCallback (toolbarItem->toolbar->panelId,
toolbarItem->controlId,
GenericToolbarItemCallbackFunc,
(void *)toolbarItem));
Error :
return error;
}
int Toolbar_InsertItem (ToolbarType toolbar, int position, int type, int active,
char description[], int callbackType, int menuItem,
CtrlCallbackPtr callbackFunc, void *callbackData,
char imageFile[])
{
int error = -UIENoError;
ToolbarItemPtr newItem = NULL;
MenuCallbackPtr menuCallback = NULL;
if (!toolbar)
return UIEHandleInvalid;
nullChk (newItem = malloc (sizeof (ToolbarItem)));
newItem->type = type;
newItem->active = active;
newItem->currActive = FALSE;
newItem->toolbar = toolbar;
if (description) {
nullChk (newItem->description = StrDup (description));
}
else
newItem->description = NULL;
switch (callbackType) {
case kMenuCallback:
newItem->callbackType = callbackType;
newItem->menuItem = menuItem;
errChk (GetMenuBarAttribute (toolbar->menuBar, menuItem,
ATTR_CALLBACK_FUNCTION_POINTER, &menuCallback));
newItem->callbackFunc = (void *)menuCallback;
errChk (GetMenuBarAttribute (toolbar->menuBar, menuItem,
ATTR_CALLBACK_DATA, &newItem->callbackData));
break;
case kControlCallback:
newItem->callbackType = callbackType;
newItem->callbackFunc = (void *)callbackFunc;
newItem->callbackData = callbackData;
newItem->menuItem = menuItem;
break;
default:
newItem->callbackType = kNoCallback;
newItem->callbackFunc = NULL;
newItem->callbackData = NULL;
newItem->menuItem = 0;
break;
}
errChk (CreateNewToolbarItemCtrl (newItem, imageFile));
errChk (ListInsertItem (toolbar->items, &newItem, position));
if (active)
{
toolbar->sizeValid = 0;
if (ToolbarIsVisible (toolbar))
{
errChk (Toolbar_Display (toolbar));
}
}
Error :
if (error < 0) {
if (newItem) {
if (newItem->controlId)
DiscardCtrl (toolbar->panelId, newItem->controlId);
DisposeToolbarItem (newItem);
}
}
return error;
}
int Toolbar_RemoveItem (ToolbarType toolbar, int position)
{
int error = -UIENoError;
ToolbarItemPtr itemPtr;
if (!toolbar)
return UIEHandleInvalid;
if ((position < 1) || (position > ListNumItems (toolbar->items)))
return UIEIndexOutOfRange;
ListRemoveItem (toolbar->items, &itemPtr, position);
if (itemPtr->type != kSeparator)
{
errChk (DiscardCtrl (toolbar->panelId, itemPtr->controlId));
}
DisposeToolbarItem(itemPtr);
toolbar->sizeValid = 0;
if (ToolbarIsVisible (toolbar))
{
errChk (Toolbar_Display (toolbar));
}
Error :
return error;
}
int Toolbar_SetItemVal (ToolbarType toolbar, int position, int value)
{
int error = -UIENoError;
ToolbarItemPtr toolbarItem;
if (!toolbar)
return UIEHandleInvalid;
if ((position < 1) || (position > ListNumItems (toolbar->items)))
return UIEIndexOutOfRange;
ListGetItem (toolbar->items, &toolbarItem, position);
if (toolbarItem->type != kSeparator) {
errChk (SetCtrlVal (toolbarItem->toolbar->panelId,
toolbarItem->controlId, value));
}
Error :
return error;
}
int Toolbar_DimItem (ToolbarType toolbar, int position, int dimmed)
{
int error = -UIENoError;
ToolbarItemPtr toolbarItem;
if (!toolbar)
return UIEHandleInvalid;
if ((position < 1) || (position > ListNumItems (toolbar->items)))
return UIEIndexOutOfRange;
ListGetItem (toolbar->items, &toolbarItem, position);
if (toolbarItem->type != kSeparator) {
errChk (SetCtrlAttribute (toolbarItem->toolbar->panelId,
toolbarItem->controlId,
ATTR_DIMMED, dimmed));
}
Error :
return error;
}
int Toolbar_ActivateItem (ToolbarType toolbar, int position, int active, int refresh)
{
int error = -UIENoError;
ToolbarItemPtr toolbarItem;
if (!toolbar)
return UIEHandleInvalid;
if ((position < 1) || (position > ListNumItems (toolbar->items)))
return UIEIndexOutOfRange;
ListGetItem (toolbar->items, &toolbarItem, position);
active = (active) ? 1 : 0;
if (toolbarItem->active != active) {
toolbarItem->active = active;
toolbarItem->toolbar->sizeValid = 0;
}
if (refresh && ToolbarIsVisible (toolbarItem->toolbar)) {
errChk (Toolbar_Display (toolbarItem->toolbar));
}
Error :
return error;
}
int Toolbar_GetNumItems (ToolbarType toolbar, int *count)
{
int error = -UIENoError;
if (!toolbar)
return UIEHandleInvalid;
if (!count)
goto Error;
*count = (toolbar->items) ? ListNumItems(toolbar->items) : 0;
Error :
return error;
}
int Toolbar_GetIndexFromDescription (ToolbarType toolbar, char label[], int *index)
{
int error = -UIENoError;
int i, numItems;
ToolbarItemPtr toolbarItem;
if (!index)
goto Error;
*index = -1;
numItems = ListNumItems (toolbar->items);
for (i = 1 ; i <= numItems ; i++) {
ListGetItem (toolbar->items, &toolbarItem, i);
if (!strcmp (label, toolbarItem->description)) {
*index = i;
break;
}
}
Error :
return error;
}
int Toolbar_GetIndexFromMenu (ToolbarType toolbar, int menuItem, int *index)
{
int error = -UIENoError;
int i, numItems;
ToolbarItemPtr toolbarItem;
if (!toolbar)
return UIEHandleInvalid;
if (!index)
goto Error;
*index = -1;
numItems = ListNumItems (toolbar->items);
for (i = 1 ; i <= numItems ; i++) {
ListGetItem (toolbar->items, &toolbarItem, i);
if (toolbarItem->menuItem == menuItem) {
*index = i;
break;
}
}
Error :
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -