In early December 2024, I got a question from our English partner CDR Group:
In MapBasic, is there a way to access the native tables in the Explorer Window rather than the map layers?
I would use LayerControlInfo()
and LayerControlSelectionInfo()
to access the chosen map layer, but if my table has no graphical objects, and is still 'down' in the Native section, how do I access that programmatically?
With the adoption of the ribbon interface, MapInfo Pro also became more context-aware. This means that MapInfo Pro can react to changes in the interface such as the user selecting a layer or a table in the Explorer Window. Below is the context menu of the tables in the Table List.
You can also take advantage of this from within your MapBasic application.
Let me show you how.
Happy #MapInfoMonday!
Inspecting Selected Tables in the Table List
In the context menu above, you can also find some menu items added by a MapBasic application, WindowHelper. We will look at the Open Tab File in UEStudio menu item.
You can find the source code for the WindowHelper tool on GitHub.
The source code shows how the menu item is added to the context menu for the Table List. I have simplified the code a bit.
I use the custom function SYSFindTextEditor()
to get the full path and file name of the default text editor. I use some custom functions from the RIBBONLib to add the menu item to the context menu of the Table List. The RIBBONLib is a custom MapBasic module that also can be found on GitHub.
sTextEditorExe = SYSFindTextEditor()
If sTextEditorExe <> "" Then
'**Extracting file name from full path
sTextEditor = PathToFileName$(sTextEditorExe)
sTextEditor = Left$(sTextEditor, Len(sTextEditor) - 4)
'**With versions of MapInfo Pro newer than v2100 context menu items have been given a static name
nCtrlIdx = RBNCntxtMenuInsertMenuItemAfter(MenuId_TableListTablesShortcut, "tlCntxtOpenInTextEditor", "Open Tab File in " & sTextEditor, "", "MenuItem_TlvOpenInFolder")
If nCtrlIdx > 0 Then
Call RBNControlSetToolTipIdx(nCtrlIdx, PRGIGetApplicationName(), "Open the selected Tab File in your favorite Text Editor.", "")
Call RBNControlSetIconsIdx(nCtrlIdx, CONTROL_SIZE_SMALL, PATH_IMAGES & "Notepad 16x16.png", "")
Call RBNControlSetLeftMarginIdx(nCtrlIdx, 0)
Call RBNControlSetCustomMBXHandlerIdx(nCtrlIdx, "TABHOpenTabFileTL")
End If
End If
The procedure below will be called when the user right-clicks on a table in the Table List and clicks on the Open Tab File in Text Editor menu item.
Sub TABHOpenTabFileTL
Dim sTab, sFile As String
OnError GoTo ErrorOccured
If TableListInfo(TL_INFO_SEL_COUNT) <> 1 Then
Note "Please select one table in the TableList window!"
Exit Sub
End If
sTab = TableListSelectionInfo (1, TL_SEL_INFO_NAME)
sFile = TableInfo(sTab, TAB_INFO_TABFILE)
If sFile <> "" Then
Print "Run Program " & msTextEditor + Chr$(32) + sFile
Run Program msTextEditor + Chr$(32) + sFile
Else
Note "Please select a non-temporal table!"
End If
Exit Sub
'-------------------------
ErrorOccured:
Call ERRCreate(Err(), Error$(), "TABHOpenTabFileTL")
Call ERRShow()
End Sub
I am using the MapBasic functions TableListInfo()
and TableListSelectionInfo()
to determine what the user has selected.
TableListInfo()
is only used to determine the number of selected tables - as seen in the source code above. This procedure only works with a single selected table.
TableListSelectionInfo()
can give you details about the selected tables. It can return the name or the ID of the table. In my case, I ask for the table name. Notice that I ask for the name of the specific selected table, 1
. That's because my procedure only supports a single selected table. If your procedure would work for multiple selected tables, you would have to loop over the number of selected tables.
Once I have retrieved the table name, I can execute my command to open the Tab File in my favorite text editor.
This is just one basic example of integrating the business logic of your MapBasic application into the Table List. A similar approach can be taken to integrate your logic into the Layer List.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------