Pages

Tuesday 25 March 2014

Set Layout Window Title from Right Click Menu using Mapbasic

This post demonstrates how to modify right click menus within MapInfo. In this example I demonstrate how to alter the Layout window right click menu, add a new menu item which calls a sub routine to alter the Layout window title.

I find this tool very useful when creating maps as you can quickly set the layout window title with a simple right click on the layout.

You can download the source code and compiled mbx from here SetLayoutWindowTitle.mbx
Note: Compiled in Mapbasic 11.5 







Include "Mapbasic.def"
Include "Menu.def"

'Defines-------------------------------------------
Define SET_ALIAS_TXTBOX 1Define SET_TITLE_TXTBOX 1

'Sub Routines---------------------------------------
Declare Sub Main
Declare Sub
SetLayoutWindowTitleDialog
Declare Sub ValidateSetLayoutWindowTitleDialog
Declare Sub SetWindowTitle(ByVal strVal as String)


'-------------------------------------------------------------------------------------------------
Sub Main
OnError Goto
CatchEx
 
   'Layout Window Right Click Menu
   Alter Menu ID M_SHORTCUT_LAYOUT Add
   "(-",   "Set Window Title..."

   HelpMsg "\nSet Window Title"
   Calling SetLayoutWindowTitleDialogDone:
   Exit Sub

CatchEx:
   Note Error$()
   Resume Done
End Sub
'-------------------------------------------------------------------------------------------------
Sub SetLayoutWindowTitleDialog
OnError Goto CatchEx  

   Dim strWindowTitle as String

   strWindowTitle = WindowInfo(FrontWindow(), WIN_INFO_NAME)
   Dialog

      Title "Set Layout Window Title"
      Width 250      Height 30
     Control EditText
        Position 10,10       
        Width 150
        Value strWindowTitle
        ID SET_TITLE_TXTBOX
        Into strWindowTitle

     Control OKButton
        Position 165, 8.5       
        Title "Set"
        Calling ValidateSetLayoutWindowTitleDialog
     
     Control CancelButton
        Position 205, 8.5  

   If CommandInfo(CMD_INFO_DLG_OK) then
     Call SetWindowTitle(strWindowTitle)
   End If

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'-------------------------------------------------------------------------------------------------
Sub ValidateSetLayoutWindowTitleDialog
OnError Goto CatchEx
  
   If ReadControlValue(SET_ALIAS_TXTBOX) = "" then
      Note "You must enter a Window Title"
      Dialog Preserve     
      Goto Done  
   End If

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub
'-------------------------------------------------------------------------------------------------
Sub SetWindowTitle(ByVal strVal as String)
OnError Goto CatchEx
  
   Set window FrontWindow() Title strVal

Done:
   Exit Sub
CatchEx:
   Note Error$()
   Resume Done
End Sub