MapInfo Pro Developers User Group

Creating a Toggle Button using the RibbonLib

  • 1.  Creating a Toggle Button using the RibbonLib

    Posted 10-22-2019 08:15
    I regularly get questions related to creating some basic control on the ribbon. In this short post, I'll show how you can create a Toggle Button using the RibbonLib that is part of the Common MapBasic Library.

    I have a few defines and also some modular level variables in this tool.
    Here are a few defines for my icons
    Define PATH_IMAGES ApplicationDirectory$() & "Images\"
    Define ICON_INFOSELHANDLER_32 PATH_IMAGES & "InfoSelHandler_32x32.png"

    And a few variables to keep track of the state. These are defined at the top of my module and so accessible inside all my procedures in the specific module. They are not global variables but only modular variables. So they can only be used from within the module.
    Dim mbInfoSelChangedHandlerEnabled As Logical,
       mnbtnSelInfoHandlerIdx As Integer,

    And here is how I create the control, the button. It's the last statement (RBNControlToggleIdx) before End If that makes my control "toggable". I use the internal control index (mnbtnSelInfoHandlerIdx) from the RibbonLib when referring to the control. That the index I get back from the functions used to adding or inserting controls.
    '**Adding to the Ribbon, MAP Tab
    mnbtnSelInfoHandlerIdx = RBNGroupAddButton("btnSelInfoHandler", "Show Info", "", sTabName, sGroupName)
    If mnbtnSelInfoHandlerIdx > 0 Then
       'Create & Set the button tooltip
       Call RBNControlSetToolTipIdx(mnbtnSelInfoHandlerIdx, PRGIGetApplicationName(), "Show info on selection", "")
       'Set the button icon
       Call RBNControlSetIconsIdx(mnbtnSelInfoHandlerIdx, CONTROL_SIZE_LARGE, "", ICON_INFOSELHANDLER_32)
       '**Set the handler to match the handler of the first button
       Call RBNControlSetCustomMBXHandlerIdx(mnbtnSelInfoHandlerIdx, "MENUSwitchDisplaySelInfoRBN")

       'Specify that this is a toggle button
       Call RBNControlToggleIdx(mnbtnSelInfoHandlerIdx, TRUE)
    End If


    Finally, I need to modify the procedure called by my control to make the control toggle when used.

    '****************************************'****************************************
    Sub MENUSwitchDisplaySelInfoMENU

       '**Revert the value of the logical variable

       mbInfoSelChangedHandlerEnabled = (NOT mbInfoSelChangedHandlerEnabled)

       '**Check/Uncheck (toggle) the control
       Call RBNControlSelectedIdx(mnbtnSelInfoHandlerIdx, mbInfoSelChangedHandlerEnabled)

       '**Change the tooltip of the control. You can also change the icon, if you want to.
       If mbInfoSelChangedHandlerEnabled Then
          Call RBNControlSetToolTipIdx(mnbtnSelInfoHandlerIdx, PRGIGetApplicationName(), "Turn off Info on Selection", "")
       Else
          Call RBNControlSetToolTipIdx(mnbtnSelInfoHandlerIdx, PRGIGetApplicationName(), "Turn off Info on Selection", "")
       End If


    End Sub


    The above example sets a variable that controls whether the SelChangedhandler should do an action when the user selected a new record or not. So the procedure connected to the ToggleButton doesn't do any action directly. It only controls if an action should be done or not.

    You can of course also call another procedure from the handler/procedure of the ToggleButton if you want to.

    The example above comes from the InfoSelHandler tool.

    ------------------------------
    Peter Horsbøll Møller
    Pitney Bowes
    ------------------------------