Home Forums General XMetaL Discussion Add new button to toolbar Reply To: Add new button to toolbar

Derek Read

Reply to: Add new button to toolbar

Users are free to make their own manual changes, so you'll probably want to start with the XMetaL Author help for information on these types of toolbar changes, starting with the main topic “Customizing the user interface” to familiarize yourself with what can be done there. That also means any scripts you write should not assume an out of the box installation as toolbars or buttons may have been deleted, moved around, added, etc.

As a developer this feature allows you to reset the toolbars on your test machine if your script has messed them up. To do that right click in the toolbar area, select “Customize”, then in the dialog that appears select the toolbar (or menu) your script messed up and click the “Reset” button.

Before you begin scripting toolbar and menu changes a good topic to read in the Programmers Guide is this one: “Command Bar interfaces”. If you are familiar with customizing MS Word toolbars and menus with script then you will see that our APIs for this are very similar.

Here's a sample of a basic function that will add a new button to an existing toolbar. Most of the time you will want to place such script into the event macro called On_Default_CommandBars_Complete.

function addButtonToToolbar(strToolbarName,strOnAction,strDescriptionText,strTooltipText,intFaceId,boolBeginGroup) {
var tbr = Application.CommandBars.item(strToolbarName);
var boolButtonExists = false;

for (i=1;i<=tbr.Controls.count;i++) {
if (tbr.Controls.item(i).OnAction == strOnAction) {
boolButtonExists = true;

if (!boolButtonExists) {
var newButton = tbr.Controls.Add(1);
newButton.DescriptionText = strDescriptionText;
newButton.OnAction        = strOnAction;
newButton.TooltipText     = strTooltipText;
newButton.BeginGroup      = boolBeginGroup;
newButton.FaceId          = intFaceId;
var faceId = Application.MakeFaceId(“Misc 1 (Custom)”,5,2);
addButtonToToolbar(“Standard”,”My Macro Name”,”Hello World”,”My Tooltip”,faceId,false);[/code]

Notice that the code checks to see that the button does not already exist. If you do not do this you will end up with duplicate buttons (one for each time the event fires).

For information on any of the methods or properties above refer to the Programmer's Guide.

You can probably use this function as it is (test it first), but at a minimum you will probably need to read the help topic for the “MakeFaceId” property so you understand how toolbar icons (face ids) are referenced. In this example I've externalized the call to MakeFaceId. You could obviously put it into any function you end up writing if you feel that might be cleaner.