General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Add new button to toolbar

  • biswajitsr

    Add new button to toolbar

    Participants 1
    Replies 2
    Last Activity 12 years, 6 months ago

    In XMetaL author how to add toolbar button. If it is possible then can anyone please give some sample code. Thanks in advance.


    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.



    Reply to: Add new button to toolbar

    I'd started on some xslts to generate the add/remove toolbar changes a while back. I believe I had it working for the menus but hadn't added the toolbars yet. The idea is to set up your menus manually using the XMetaL GUI, then close XMetaL so it saves all that to a tbr file. Then you run this over your tbr file and it gives you the js you need to add to your mcr file. Why do this? Long ago I thought the right way to do it was to use xmetal to create the tbr file and push stuff out that way. Rather than manually code all the menu changes (I have a bunch) I figured I'd write an xslt (note this is an xslt 2.0 stylesheet, so you should use Saxon 9):



    // Get the CommandBars object
    var cmdBars = Application.CommandBars;
    // Get a single command bar (the main menu bar)
    var menuBar = cmdBars.item(“Menu bar”);
    // Get all of the menu bar's controls (all of the menus)
    var menuBarCtrls = menuBar.Controls;

    var currentTopMenu;
    var currentTopMenuCtrls;



    currentTopMenu = menuBarCtrls.item();
    currentTopMenuCtrls = currentTopMenu.Controls;




    for (i=1;i<=MenuCtrls.count; i++) {
    if(MenuCtrls.item(i).Caption == ''){ MenuCtrls.item(i).Delete(); }
    newSubItem = MenuCtrls.Add(5,1,);
    newSubItem.Caption = ““;
    newSubItem.BeginGroup = ;
    newSubItem.OnAction = ““;
    newSubItem.Enabled = true;

    for (i=1;i<=MenuCtrls.count; i++) {
    if(MenuCtrls.item(i).Caption == ''){ MenuCtrls.item(i).Delete(); }
    newSubItem = MenuCtrls.Add(5,1,);
    newSubItem.Caption = '';

    newSubItem.BeginGroup = ;
    newSubItem.OnAction = ““;
    newSubItemMenuCtrls = newSubItem.Controls;





  • You must be logged in to reply to this topic.

Lost Your Password?