Page 1 of 1

Manually resizing/placing gadgets inside a Dialog

Posted: Mon Feb 06, 2017 11:11 pm
by Andre
The following code I created using an earlier posted code example, but I extended the 'GeneralContainer' with a toggle button 'Hide settings'. While I can easily hide the first container with the frame 'Settings' I can't resize the second container with the listicon. While I think the pos/height are calculated right (see Debug output), the ResizeGadget() command has only an effect to the y position, but not to the height of the container/listicon.

Anyone knows a solution?

(Note: While I regularly use the 'DynamicDialogs' framework, with which I have the same problem, I'm here using/posting a code with native PB commands only for testing...)

Beside that this probably leads to a feature: RefreshDialog() should be able to recalculate a GUI after hiding one or several gadgets or complete containers. Currently it can't be used for such purposes, as it will always display the initial GUI layout in the dialog... (even better would be, if there would be a native solution for adding/removing gadgets to/from dialogs 'on the fly' without recreating the complete XML code and recreating the dialog based on it.)

Code: Select all

; Original: http://www.purebasic.fr/english/viewtopic.php?f=13&t=56422&hilit=multibox

; Modified by Andre / PureBasic team on 6th Feb. 2017:
; This is a modified version, with a button 'Toggle' included, with which a part of the displayed 
; area can be expanded while another one is hidden. See the 'GeneralContainer' and click the
; Toggle button 'Hide settings'!


  ; As we embedded xml directly in the source, the encoding of special characters will vary if we are in unicode mode or not.
  ; So ensure to use the correct one. This is not needed if the XML is read from an external file or directly included
  ; with IncludeBinary.
  ;
  CompilerIf #PB_Compiler_Unicode
    #XmlEncoding = #PB_UTF8
  CompilerElse
    #XmlEncoding = #PB_Ascii
  CompilerEndIf

  #Dialog = 0
  #Xml = 0
 
  Runtime Enumeration Gadget
    #ListView
    #GeneralContainer
    #EditorContainer
    #BackupContainer
    #GeneralSubContainer1
    #ToggleButton
    #GeneralSubContainer2
    #ListIcon
  EndEnumeration
 
  Procedure ShowPanels()
   
    HideGadget(#GeneralContainer, #True)
    HideGadget(#EditorContainer, #True)
    HideGadget(#BackupContainer, #True)
   
    Select GetGadgetState(#ListView)
      Case 0
        HideGadget(#GeneralContainer, #False)
       
      Case 1
        HideGadget(#EditorContainer, #False)
       
      Case 2
        HideGadget(#BackupContainer, #False)
    EndSelect
  EndProcedure
 
 
  Runtime Procedure OnListViewEvent()
    ShowPanels()
  EndProcedure
  
  Runtime Procedure OnToggleButtonEvent()
    Protected ToggleState, y, height
    Protected HeightSubContainer1, yPosSubContainer1
    Protected HeightSubContainer2, HeightContainerTotal
    
    ; Will be used for resized the SubContainer2 below:
    HeightContainerTotal = GadgetHeight(#GeneralContainer)
    HeightSubContainer1 = GadgetHeight(#GeneralSubContainer1)
    yPosSubContainer1 = GadgetY(#GeneralSubContainer1)
    HeightSubContainer2 = GadgetHeight(#GeneralSubContainer2)
    Debug "total height = " + HeightContainerTotal + " / Pos+Height container1 = " + yPosSubContainer1 + " with " + HeightSubContainer1 + " / Height container2 = " + HeightSubContainer2
    ToggleState = GetGadgetState(#ToggleButton)
    If ToggleState = 1
      HideGadget(#GeneralSubContainer1, #True)
      y = yPosSubContainer1
      height = HeightContainerTotal - 20
    Else
      HideGadget(#GeneralSubContainer1, #False)
      y = yPosSubContainer1 + HeightSubContainer1 + 10
      height = HeightContainerTotal - HeightSubContainer1 - 20
    EndIf
    Debug "Calculated height/pos = " + height + " at " + y
    ResizeGadget(#GeneralSubContainer2, #PB_Ignore, y, #PB_Ignore, height)
    ; RefreshDialog(#Dialog)  ; can't be used, as it will place/resize all gadgets according to their initial pos/size!
  EndProcedure
 
  XML$ = "<window id='#PB_Any' name='test' text='Preferences' minwidth='auto' minheight='auto' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget '>" +
         "  <hbox expand='item:2'>" +
         "    <listview id='#ListView' width='100' onEvent='OnListViewEvent()'/>" +
         "    <multibox>" +
         "" +
         "      <container id='#GeneralContainer' expand='yes' invisible='yes'>" +
       ; "        <multibox>" +
         "          <vbox expand='yes'>" +
         "            <container id='#GeneralSubContainer1' invisible='no'>" +
         "              <vbox expand='no'>" +
         "                <frame text='Settings'>" +
         "                  <vbox expand='no'>" +
         "                    <checkbox text='Enable red light'/>" +
         "                    <checkbox text='Enable green light'/>" +
         "                  </vbox>" +
         "                </frame>" +
         "              </vbox>" +
         "            </container>" +
         "            <container id='#GeneralSubContainer2' expand='yes' invisible='no'>" +
         "              <vbox expand='yes'>" +
         "                <singlebox expand='no' align='right' margin='0'>" +
         "                  <button id='#ToggleButton' text='Hide settings' flags='#PB_Button_Toggle' onEvent='OnToggleButtonEvent()' />" +
         "                </singlebox>" +
         "                <frame text='Results'>" +
         "                  <vbox expand='yes'>" +
         "                    <listicon id='#ListIcon' width='100' height='200'/>" +
         "                  </vbox>" +
         "                </frame>" +
         "              </vbox>" +
         "            </container>" +
         "          </vbox>" +
       ; "        </multibox>" +
         "      </container>" +
         "" +
         "      <container id='#EditorContainer' invisible='yes'>" +
         "        <frame text='Editor'>" +
         "          <vbox expand='no'>" +
         "            <checkbox text='Set read only mode'/>" +
         "            <checkbox text='Duplicate line automatically'/>" +
         "            <checkbox text='Enable monospace font'/>" +
         "          </vbox>" +
         "        </frame>" +
         "      </container>" +
         "" +
         "      <container  id='#BackupContainer' invisible='yes'>" +
         "      <frame text='Backup'>" +
         "        <vbox expand='no'>" +
         "          <checkbox text='Activate backup'/>" +
         "        </vbox>" +
         "      </frame>" +
         "      </container>" +
         "" +
         "    </multibox>" +
         "  </hbox>" +
         "</window>"
 
  If CatchXML(#Xml, @XML$, StringByteLength(XML$), 0, #XmlEncoding) And XMLStatus(#Xml) = #PB_XML_Success
   
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #Xml, "test")
     
      AddGadgetItem(#ListView, -1, "General")
      AddGadgetItem(#ListView, -1, "Editor")
      AddGadgetItem(#ListView, -1, "Backup")
     
      SetGadgetState(#ListView, 0)
     
      ShowPanels()
     
      Repeat
        Event = WaitWindowEvent()
      Until Event = #PB_Event_CloseWindow
     
    Else 
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#Xml) + " (Line: " + XMLErrorLine(#Xml) + ")"
  EndIf