Expanding and collapsing information in a dialog, wxMSW vs. wxLIN, wxMAC

Volker Bartheld dr_versaeg at freenet.de
Fri Aug 18 03:25:46 PDT 2006


Hi!

[Update]

On Mon, 31 Jul 2006 15:40:36 +0200, Volker Bartheld
<dr_versaeg at freenet.de> wrote :
>I want to expand and collapse a dialog according to information that is
>added/removed when the user presses a button

After a little tweaking, some offline chat with Vadim and peeking into
the "Dialogs" sample (the wxLog class, to be precise), I've come up with
the following codesnippet that will horizontally or vertically resize a
dialog to its contents. Vadim's original comments have been stripped to
reduce the size of this post a little and there's also no wxCE
implementation because I wasn't able to test the functionality there.

 void wxDialog::vSizeToContent()
 {
   m_minHeight=m_maxHeight=-1;
   wxSize
     sizeTotal=GetSize(),
     sizeClient=GetClientSize(),
     size=GetSizer()->GetMinSize();
   size.x=sizeTotal.x; // originally: size.x+=sizeTotal.x-sizeClient.x; // why??
   size.y+=sizeTotal.y-sizeClient.y;
   SetSizeHints(size.x, size.y, m_maxWidth, m_maxHeight);
   SetSize(wxDefaultCoord, size.y); // don't change the width when resizing vertically
 # ifdef __WXGTK__
   Show();
 # endif // wxGTK
 } // void wxDialog::vSizeToContent()
 void wxDialog::hSizeToContent()
 {
   m_minWidth=m_maxWidth=-1;
   wxSize
     sizeTotal=GetSize(),
     sizeClient=GetClientSize(),
     size=GetSizer()->GetMinSize();
   size.x+=sizeTotal.x-sizeClient.x; // originally: size.y+=sizeTotal.y-sizeClient.y; // why??
   size.y=sizeTotal.y;
   SetSizeHints(size.x, size.y, m_maxWidth, m_maxHeight);
   SetSize(size.x, wxDefaultCoord); // don't change the height when resizing horizontally
 # ifdef __WXGTK__
   Show();
 # endif // wxGTK
 } // void wxDialog::hSizeToContent()

I've tested the code above on wxMSW, wxLin and wxMac with the following
test dialog

 enum tMYDIALOGRES {ID_EXPANDBUTTON=wxID_HIGHEST+2000};
 class MyDialog : public wxDialog
 {
   // ...
 private:
   wxPanel* m_helpBox;
   wxButton* m_helpButton;
   bool m_bHelp;
 public:
   MyDialog::MyDialog(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE, const wxString& name = "dialogBox")
     : wxDialog(parent, id, title, pos, size, style, name ),
     m_helpButton(NULL), m_helpBox(NULL), m_bHelp(false)
   {
     m_helpButton=new wxButton(this, ID_EXPANDBUTTON, wxT("Show"));
     SetSizer(new wxBoxSizer(wxVERTICAL));
     GetSizer()->Add(m_helpButton);
     SetHelp(wxT("Some help text..."), m_bHelp);
   }
   void MyDialog::OnExpandButton(wxCommandEvent& event)
   {
     m_bHelp=!m_bHelp;
     SetHelp(wxT("Some help text..."), m_bHelp);
   }
 protected:
   void MyDialog::SetHelp(const wxString& help, bool bShow)
   {
     if(m_helpBox)
     {
       GetSizer()->Detach(m_helpBox);
       m_helpBox->Destroy();
       m_helpBox=NULL;
     }
     if(help.IsEmpty())
     {
       m_helpButton->Enable(false);
       vSizeToContent();
       return;
     } // if(help.IsEmpty())
     if(bShow)
     {
       m_helpButton->SetLabel(wxT("Hide"))
         m_helpBox=new wxPanel(this);
       m_helpBox->SetSizer(new wxBoxSizer(wxHORIZONTAL));
       m_helpBox->GetSizer()->Add(new wxStaticText(m_helpBox, wxID_ANY, help), 1, wxEXPAND|wxCENTER);
       GetSizer()->Add(m_helpBox, 0, wxEXPAND);
     } // if(bShow)
     else
     {
       m_helpButton->SetLabel(wxT("Show"))
     } // if(bShow)... else
     vSizeToContent();
   } // void SetHelp(const wxString& help)
   // ...
 }; // class MyDialog : public wxDialog
 
 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
 EVT_BUTTON(ID_EXPANDBUTTON, ModalDialog::OnExpandButton)
 END_EVENT_TABLE()

and it seems to work quite well.

Maybe it's of some use to one of you.


Happy coding,

Volker
__
Mail replies to/an V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list