Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Splitter Bars - 6 layout options

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin Splitter Bars - 6 layout options

    At the request of a couple of members, I've updated my
    splitter bar code to support up to 6 different layouts. Just
    select a menu option to change the laout.

    It allows fixed splitter bar positions or percentage-based splitter
    bar positions. Resizing the dialog also resizes the controls.

    The basic layout is with 3 controls - 2 on top, 1 on bottom.
    By selecting a menu choice you can change the layout,
    including two options which hide one of the controls.

    This version shows how to handle the presence of a menu,
    toolbar and statusbar.



    The code is provided below, and is also available in the
    gbSnippets PowerBASIC source code library (snippet# gbs_00392).
    You can get the entire library by downloading gbSnippets or you
    can view individual snippets online.

    gbSnippets home page: http://www.garybeene.com/sw/gbsnippets.htm
    Online source code listings: http://www.garybeene.com/power/code/

    If you've already installed gbSnippets, you can ensure that your local
    library is synchronized with the latest snippets on the gbSnippets server
    by using the "Actions/Synchronize with gbSnippets Server" menu.

    Finally, here's a direct link to the gbsnippets.pbr resource file use in this code:



    Code:
    Splitter Bars II
    
    'Allowing a user to change an application's layout, or to change the size of various
    'sections of a layout is a very common feature of GUI applications.  This snippet
    'shows how it can be done and includes the following features.
    
       '1. Horizontal and vertical splitter bars, displaying up to controls.
       '2. 6 different layout options (some with 2 controls, others with 3 controls).
       '3. Splitter bars can have fixed position or be positioned on a percentage basis
    
    'Primary Code; 
    'The code sections/functions used in the compilable example below are:
    '1. %WM_SetCursor - detects mouse clicks and movement
    '2. Sub MoveBarUpDown - moves horizontal bar in response to mouse action
    '3. Sub MoveBarLeftRight - moves vertical bar in response to mouse action
    '4. Sub ResizeWindow - sets splitterbar location/size, and control visiblity/location/size
    
    'A mouse is used to drag/move either splitter bar, which resets the
    'fixed/percentage position of the splitter bar.  When the dialog is resized, the
    'current splitter bar positions (fixed/percentage) are maintained.  Label controls
    'are used as the splitter bars - one for vertical splitting and one for horizontal splitting.
    
    'Due to the length of this example, on a description will be covered in this section.
    'See the compilable example below for the source code.
    
    
    'Compilable Example:
    'In this example, up to 6 layout options may be selected. The splitter bars (label controls)
    'are colored red/blue for visiblity, but an application typically uses colors which match the 
    'background color of the dialog in order to render the splitter bars invisible - except as the
    'mouse cursor changes when it passes over the splitter bars.
    #Compile Exe
    #Dim All
    #Debug Error On     'catch array/pointer errors - OFF in production
    #Debug Display On   'display untrapped errors   - OFF in production
    #Include "Win32api.inc"
    #Resource "gbsnippets.pbr"
    
    ' Equates =============================================================
    %IDC_Left = 500 : %IDC_Right = 501 : %IDC_Bottom = 502
    %IDC_LabelH = 503 : %IDC_LabelV = 504 : %IDC_StatusBar = 505
    %IDC_Toolbar = 506 : %IDM_Fixed = 507 : %IDT_New = 508 : %ID_ImageList = 509
    %IDT_X = 701 : %IDT_Y = 702 : %IDT_Z = 703
    %IDM_1 = 601 : %IDM_2 = 602 : %IDM_3 = 603
    %IDM_4 = 604 : %IDM_5 = 605 : %IDM_6 = 606 : %IDM_Sep = 607
    
    ' Global Variables =============================================================
    Global hDlg As Dword, hTree As Dword, hList As Dword, hMenu As Dword
    Global SplitVInWork As Long, SplitHInWork As Long, hMenuOptions As Dword
    Global XSplit As Single, YSplit As Single, hLst As Dword
    Global HBarLeft As Long, VBarTop As Long, Layout&, FixedSplitterBar&
    
                 
    ' Main Function =======================================================================
    Function PBMain()
        Dialog New Pixels, 0, "Splitter Text",300,300,410,340, %WS_OverlappedWindow Or %WS_ClipChildren To hDlg
        Dialog Set Icon hDlg, "aainfo"
        Control Add StatusBar, hDlg, %IDC_StatusBar, "",0,0,0,0     'xy, xxyy ignored, %ccs_bottom by default
        Control Add Toolbar, hDlg, %IDC_Toolbar, "Toolbar",0,0,0,0                    'xy, xxyy, txt$ ignored, %ccs_top by default
        'create imagelist for Toolbar
        ImageList New Icon 16,16,32,3 To hLst
        ImageList Add Icon hLst, "x"              '1
        ImageList Add Icon hLst, "y"              '2
        ImageList Add Icon hLst, "z"              '3
        'attach imagelist
        Toolbar Set ImageList hDlg, %IDC_Toolbar, hLst, 0
        'create buttons
        Toolbar Add Button  hDlg, %IDC_Toolbar, 1, %IDT_X, %TbStyle_Check, "X"
        Toolbar Add Button  hDlg, %IDC_Toolbar, 2, %IDT_Y, %TbStyle_Check, "Y"
        Toolbar Add Button  hDlg, %IDC_Toolbar, 3, %IDT_Y, %TbStyle_Check, "Z"
        
        'create menu
        Menu New Bar To hMenu
        Menu New Popup To hMenuOptions
        Menu Add Popup, hMenu, "&Options", hMenuOptions, %MF_Enabled
        Menu Add String, hMenuOptions, "1 Left Beside Right over Bottom, Bottom Left-to-Right", %IDM_1, %MF_Enabled
        Menu Add String, hMenuOptions, "2 Left over Bottom - Right Top-to-Bottom", %IDM_2, %MF_Enabled
        Menu Add String, hMenuOptions, "3 Left Beside Right, No Bottom", %IDM_3, %MF_Enabled
        Menu Add String, hMenuOptions, "4 Left Beside Right under Bottom, Bottom Left-to-Right", %IDM_4, %MF_Enabled
        Menu Add String, hMenuOptions, "5 Right over Bottom - Left Top-to-Bottom", %IDM_5, %MF_Enabled
        Menu Add String, hMenuOptions, "6 Left over Right, No Bottom", %IDM_6, %MF_Enabled
        Menu Add String, hMenuOptions, "-", %IDM_Sep, %MF_Enabled
        Menu Add String, hMenuOptions, "Fixed Position Splitter Bars", %IDM_Fixed, %MF_Enabled
        Menu Attach hMenu, hDlg   
    
        'add the controls + splitter bars (labels)
        Control Add TextBox, hDlg, %IDC_Left, "TopLeft",  20, 80, 160, 80
        Control Add TextBox, hDlg, %IDC_Bottom, "Bottom",  20, 200, 370, 90
        Control Add TextBox, hDlg, %IDC_Right, "TopRight", 220, 55, 170, 100
        Control Add Label, hDlg, %IDC_LabelH, "",  200, 45, 6, 125, %SS_Notify , %WS_Ex_ClientEdge  ' up/down - does horizontal split
        Control Add Label, hDlg, %IDC_LabelV, "",  0,170, 410, 6, %SS_Notify , %WS_Ex_ClientEdge   ' left/right - does vertical split
        Control Set Color hDlg, %IDC_LabelV, %Black, %Red
        Control Set Color hDlg, %IDC_LabelH, %Black, %Blue
    
        'initial values
        XSplit = 0.5 : YSplit = 0.5 : HBarLeft = 100 : VBarTop = 100 : Layout& = 1
    
        Dialog Show Modal hDlg Call DlgProc()
    End Function
    
    'monitor mouse movement=================================
    CallBack Function DlgProc() As Long
    Local iReturn As Long, x as Long, y as Long, w as Long, h as Long
       Select Case CB.Msg
          Case %WM_Command
             Select Case CB.Ctl
                Case %IDM_1 : Layout& = 1 : ResizeWindow
                Case %IDM_2 : Layout& = 2 : ResizeWindow
                Case %IDM_3 : Layout& = 3 : ResizeWindow
                Case %IDM_4 : Layout& = 4 : ResizeWindow
                Case %IDM_5 : Layout& = 5 : ResizeWindow
                Case %IDM_6 : Layout& = 6 : ResizeWindow
                Case %IDM_Fixed
                   FixedSplitterBar& = FixedSplitterBar&  XOR 1     'flip between 0 and 1
                   Menu Set State hMenuOptions, ByCmd %IDM_Fixed, FixedSplitterBar& * 8            
                   Dialog Get Client hDlg To w,h
                   'adjusts VBarTop/HBarLeft or xSplit/ySplit when option is changed
                   'both are not kept current at all times - only the pair in use
                   If FixedSplitterBar& Then
                      VBarTop = h - ySplit * h
                      HBarLeft = xSplit * w
                   Else
                      ySplit = (h-VBarTop) / h
                      xSplit = HBarLeft / w
                   End If
                   ResizeWindow
                Case %IDT_X, %IDT_Y, %IDT_Z
                   MsgBox "No action. Toolbar included just to show how splitter bars work with toolbar in place.", %MB_OK + %MB_IconInformation, "Toolbar Button Pressed"
             End Select
          Case %WM_Size
             ResizeWindow
          Case %WM_SetCursor
                iReturn = GetDlgCtrlID (CB.wParam)
                'identify over which label control the mouse action took place
                If iReturn = %IDC_LabelH Then MousePTR 9 : Function = 1    '9 = horizontal cursor
                If iReturn = %IDC_LabelV Then MousePTR 7 : Function = 1    '7 = vertical cursor
    
                'monitors the 3 basic splitter bar mouse actions, lbuttondown, mousemose, lbuttonup
                Select Case Hi(Word, CB.lParam) 
                   Case %WM_LButtonDown
                      'sets flags to say splitter action has started (no actual movement yet)
                      If iReturn = %IDC_LabelV Then SplitVInWork = 1
                      If iReturn = %IDC_LabelH Then SplitHInWork = 1
                   Case %WM_MouseMove
                      'Repositions splitter bars to match mouse position (if position has changed)
                      If SplitVInWork Then If MoveBarUpDown Then ResizeWindow
                      If SplitHInWork Then If MoveBarLeftRight Then ResizeWindow
                   Case %WM_LButtonUp      
                      'sets flags to say splitter action has ended
                      SplitHInWork = 0 : SplitVInWork = 0
                End Select
       End Select
    End Function
    
    Function MoveBarUpDown As Long
        Local pt As Point, h As Long, w As Long
        Static oldY As Long
        Dialog Get Client hDlg To w,h
        GetCursorPos pt               'pt has xy screen coordinates
        ScreenToClient hDlg, pt       'pt now has client coordinates
    '    If h-pt.y < 50 Then Exit Function   'limit right motion   'optional
    '    If pt.y < 50 Then Exit Function                            'optional
        YSplit = (pt.y-3) / h         'actually, should only do one or the other
        VBarTop = h - (Pt.y-3)        'actually, should only do one or the other
        If pt.y <> oldY Then Function = %True : oldY = pt.y
    End Function
    
    Function MoveBarLeftRight As Long
        Local pt As Point, h As Long, w As Long
        Static oldX As Long
        Dialog Get Client hDlg To w,h
        GetCursorPos pt               'pt has xy screen coordinates
        ScreenToClient hDlg, pt       'pt now has client coordinates
    '    If w-pt.x < 50 Then Exit Function   'limit right motion       'optional
    '    If pt.x < 50 Then Exit Function                                 'optional
        XSplit = (pt.x-3) / w        'actually, should only do one or the other
        HBarLeft = pt.x-3            'actually, should only do one or the other
        If pt.x <> oldX Then Function = %True : oldX = pt.x
    End Function
    
    Sub ResizeWindow
       Local vx As Long, vy As Long, hx As Long, hy As Long, h As Long, w As Long, HLeft&, VTop&
       Local yToolbar as Long, yStatusBar as Long, M as Long, T as Long
    
       Local yStatus as Long, yTool as Long
       Control Get Size hDlg, %IDC_StatusBar To w,yStatus
       Control Get Size hDlg, %IDC_Toolbar To w,yTool
       Dialog Get Client hDlg To w,h
       M = 5   'margin around controls
       T = 6    'splitter thickness (smallest dimension)
    
       'get the splitter bar positions - top of vertical splitter (VTop) and
       'left of horizontal splitter (HLeft).  These will be used in positioning
       'controls on the dialog.  Result depends on whether fixed or percentage
       'positioning is selected by the user.
       If FixedSplitterBar& Then
          VTop = h - VBarTop
          HLeft = HBarLeft
       Else
          VTop = ySplit*h
          HLeft = xSplit*w
       End If
     
       Select Case Layout&         'Function Layout returns a value 1-6
          Case 1    'left beside right, both above bottom
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Show
             Control Show State hDlg, %IDC_LabelV, %SW_Show
             Control Show State hDlg, %IDC_Bottom, %SW_Show
    
             'H splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
             Control Set Size hDlg, %IDC_LabelH, T, VTop - yTool - M
    
             'V splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelV, M, VTop
             Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
    
             'Left size/loc
             Control Set Loc  hDlg, %IDC_Left, M, yTool + M
             Control Set Size hDlg, %IDC_Left, HLeft - 2*M, VTop - yTool - 2*M
    
             'Right size/loc
             Control Set Loc  hDlg, %IDC_Right, HLeft + T + M, yTool + M
             Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, VTop - yTool - 2*M
    
             'Bottom size/loc
             Control Set Loc  hDlg, %IDC_Bottom, M, VTop + T + M
             Control Set Size hDlg, %IDC_Bottom, w - 2*M, h - VTop - yStatus - 2*M - T
    
    
          Case 2    'left over bottom, right top-to-bottom
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Show
             Control Show State hDlg, %IDC_LabelV, %SW_Show
             Control Show State hDlg, %IDC_Bottom, %SW_Show
    
             'H splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + 5
             Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
    
             'V splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelV, M, VTop
             Control Set Size hDlg, %IDC_LabelV, HLeft - M, T
    
             'Left size/loc
             Control Set Loc  hDlg, %IDC_Left, M, yTool + M
             Control Set Size hDlg, %IDC_Left, HLeft - 2*M, VTop - yTool - 2*M
    
             'Right size/loc
             Control Set Loc  hDlg, %IDC_Right, HLeft + T + M, yTool + M
             Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - yTool - yStatus - 2*M
    
             'Bottom size/loc
             Control Set Loc  hDlg, %IDC_Bottom, M, VTop + T + M
             Control Set Size hDlg, %IDC_Bottom, HLeft - 2*M, h - VTop - yStatus - 2*M - T
    
          Case 3    'left beside right, bottom not visible
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Show
             Control Show State hDlg, %IDC_LabelV, %SW_Hide
             Control Show State hDlg, %IDC_Bottom, %SW_Hide
    
             'H splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
             Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
    
             'V splitter bar loc/size
             '   none - not visible
    
             'Left loc/size
             Control Set Loc  hDlg, %IDC_Left, M, yTool + M
             Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - yTool - yStatus - 2*M
    
             'Right loc/size
             Control Set Loc  hDlg, %IDC_Right, HLeft + T + M, yTool + M
             Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - yTool - yStatus - 2*M
    
             'Bottom loc/size
             '   none - not visible
    
          Case 4    'left beside right, both below bottom
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Show
             Control Show State hDlg, %IDC_LabelV, %SW_Show
             Control Show State hDlg, %IDC_Bottom, %SW_Show
    
             'H splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelH, HLeft, VTop + T
             Control Set Size hDlg, %IDc_LabelH, T, h - VTop - yStatus - M - T
    
             'V splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelV, M, VTop
             Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
    
             'Left loc/size
             Control Set Loc  hDlg, %IDC_Left, M, VTop + T + M
             Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - VTop - yStatus - 2*M - T
    
             'Right loc/size
             Control Set Loc  hDlg, %IDC_Right, HLeft + T + M, VTop + T + M
             Control Set Size hDlg, %IDC_Right, w - HLeft - 2*M - T, h - VTop - yStatus - 2*M - T
    
             'Bottom loc/size
             Control Set Loc  hDlg, %IDC_Bottom, M, yTool + M
             Control Set Size hDlg, %IDC_Bottom, w - 2*M, VTop - yTool - 2*M
    
    
          Case 5    'left top-to-bottom, right over bottom
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Show
             Control Show State hDlg, %IDC_LabelV, %SW_Show
             Control Show State hDlg, %IDC_Bottom, %SW_Show
    
             'H splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelH, HLeft, yTool + M
             Control Set Size hDlg, %IDC_LabelH, T, h - yTool - yStatus - 2*M
    
             'V splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelV, HLEFT + T, VTop
             Control Set Size hDlg, %IDC_LabelV, w - HLeft - T - M, T
    
             'Left loc/size
             Control Set Loc  hDlg, %IDC_Left, M, yTool + M
             Control Set Size hDlg, %IDC_Left, HLeft - 2*M, h - yTool - yStatus - 2*M
    
             'Right loc/size
             Control Set Loc  hDlg, %IDC_Right, HLeft + T + M, yTool + M
             Control Set Size hDlg, %IDC_Right, w - HLeft - T - 2*M, VTop - yTool - 2*M
    
             'Bottom loc/size
             Control Set Loc  hDlg, %IDC_Bottom, HLeft + T + M, VTop + T + M
             Control Set Size hDlg, %IDC_Bottom, w - HLeft - T - 2*M, h - VTop - yStatus - 2*M - T
    
          Case 6    'left over right, bottom not visible
             'visibility
             Control Show State hDlg, %IDC_LabelH, %SW_Hide
             Control Show State hDlg, %IDC_LabelV, %SW_Show
             Control Show State hDlg, %IDC_Bottom, %SW_Hide
    
             'H splitter bar loc/size
             '  none - not visible
    
             'V splitter bar loc/size
             Control Set Loc hDlg, %IDC_LabelV, M, VTop
             Control Set Size hDlg, %IDC_LabelV, w - 2*M, T
    
             'Left loc/size
             Control Set Loc  hDlg, %IDC_Left, M, yTool + M
             Control Set Size hDlg, %IDC_Left, w - 2*M, VTop - yTool - 2*M
    
             'Right loc/size
             Control Set Loc  hDlg, %IDC_Right, M, VTop + T + M
             Control Set Size hDlg, %IDC_Right, w - 2*M, h - VTop - yStatus - 2*M - T
    
             'Bottom loc/size
             '   none - not visible
       End Select
       StatusBar Set Parts hDlg, %IDC_StatusBar, w/3, w/3, w/3
       Dialog Redraw hDlg
    End Sub
                     
    'gbs_00392
    Last edited by Gary Beene; 7 Oct 2009, 06:59 AM.
Working...
X