MapInfo Pro Developers User Group

How to find and select specific frames in a Layout

  • 1.  How to find and select specific frames in a Layout

    Pitney Bowes
    Posted 11-26-2018 17:36
    Here's an overview of the MapBasic selection support for frames in a Layout with some examples of creating and modifying a selection.  The statement to change the selection of frames in a Layout is:

     

    Set Layout
           [ Window layout_win_id ]
           Selection [ All | Clear | [Add | Remove] Frame Id frame_id, frame_id, frame_id,…]

     

    The Window clause is optional. If left out, the statement is applied to the front-most Layout window.  The Selection clause also applies to the current page only. You can select all frames on the current page, or select a subset. You can change the current selection by adding or removing specific frames. And of course, you can clear the selection.

    To modify the selection of frames on a different page you need to set that page as the current page. This is done using the following statement:


    Set Layout [ Window layout_win_id ] Page page_no

     

    where page_no is a number from 1 to the total number of pages in the Layout.


    Selecting All Frames

    To select all frames on the current page use:


    Set Layout Window layoutWinId Selection All


    Clearing the Selection

    To clear the page's current selection use:

     

    Set Layout Window layoutWinId Selection Clear

     

    Creating a Selection

    Example showing frame selection

    Let's say we have a multi-page Layout window as the front window, and we want to select all the text frames on page 2, then open the Text Style dialog to let the user change the text style for all the selected text.  We start out with the following:

    Dim layoutWinId as Integer
    layoutWinId = FrontWindow()
    Set Layout Window layoutWinId Page 2


    Page 2 is now currently displayed in the Layout window. We'll iterate over the frames on page two, looking for text frames so we need to know the number of frames.

    Dim curFrame, numFrames as Integer
    numFrames = LayoutItemInfo(layoutWinId, -1, LAYOUT_ITEM_INFO_NUM_ITEMS_ON_PAGE)

     

     The 2rd argument to LayouttemInfo is ignored when the attribute is LAYOUT_ITEM_INFO_NUM_ITEMS_ON_PAGE, so passing -1 is OK. 

    Make sure the current selection is empty by clearing it. Then iterate over the frames, adding each text frame to build the collection of selected text frames.

    Set Layout Window layoutWinId Selection Clear
    
    For curFrame = 1 to numFrames
        If (LayoutItemInfo(layoutWinId,curFrame,LAYOUT_ITEM_INFO_TYPE)=LAYOUT_ITEM_TYPE_TEXT) Then
            ' add to selection
            Set Layout Selection Add Frame Id curFrame
        End If
    Next
    
    ' now all text frames are selected
    ' open the Text Style dialog which will operate on the current selection
    Run Menu Command ID M_LAYOUT_TEXT_STYLE

      

    Example modifying the selection

    You can remove individual frames from the current selection. For example, let's say the title frame on page 2 is named "Title".  (You can assign a name to any frame in a Layout using the Name clause as part of its Create statement, or adding it later using Alter Designer Frame.) If you wanted to let the user apply one set of styles to all text frames except the title frame on the current page, you could do the following:

    Set Layout Window layoutWinId Selection Clear
    
    For curFrame = 1 to numFrames
        If (LayoutItemInfo(layoutWinId,curFrame,LAYOUT_ITEM_INFO_TYPE)=LAYOUT_ITEM_TYPE_TEXT) Then
            ' add to selection
            Set Layout Selection Add Frame Id curFrame
        End If
    Next
    
    Dim titleFrame as Integer
    titleFrame = LayoutItemID(layoutWinId, "Title", LAYOUT_ITEM_TYPE_TEXT)
    
    If titleFrame <> -1 Then
        Set Layout Selection Remove Frame Id titleFrame
    End If
    
    ' now all text frames are selected except the one named "Title"
    ' open the Text Style dialog  which will operate on the current selection
    Run Menu Command ID M_LAYOUT_TEXT_STYLE

      

    Selecting a known set of IDs

    You can also completely change the current selection by specifying a specific list of frame ids. 

     

    Set Layout Window layoutWinId Selection Frame ID 2, 4, 5

     

    This will leave only frames with the ids 2, 4, and 5 selected, regardless of what their type is, clearing any previously selected frames.



    ------------------------------
    Michele Buselli
    Senior Principal Software Engineer
    Pitney Bowes Software
    ------------------------------