1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     XML resource for wxBoxSizer 
   4 // Author:      Vaclav Slavik 
   7 // Copyright:   (c) 2000 Vaclav Slavik 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  12 #pragma implementation "xh_sizer.h" 
  15 // For compilers that support precompilation, includes "wx.h". 
  16 #include "wx/wxprec.h" 
  22 #include "wx/xml/xh_sizer.h" 
  25 #include "wx/statbox.h" 
  26 #include "wx/notebook.h" 
  28 wxSizerXmlHandler::wxSizerXmlHandler()  
  29 : wxXmlResourceHandler(), m_IsInside(FALSE
), m_ParentSizer(NULL
) 
  31     ADD_STYLE(wxHORIZONTAL
); 
  32     ADD_STYLE(wxVERTICAL
); 
  48     ADD_STYLE(wxSTRETCH_NOT
); 
  50     ADD_STYLE(wxALIGN_CENTER
); 
  51     ADD_STYLE(wxALIGN_CENTRE
); 
  52     ADD_STYLE(wxALIGN_LEFT
); 
  53     ADD_STYLE(wxALIGN_TOP
); 
  54     ADD_STYLE(wxALIGN_RIGHT
); 
  55     ADD_STYLE(wxALIGN_BOTTOM
); 
  56     ADD_STYLE(wxALIGN_CENTER_HORIZONTAL
); 
  57     ADD_STYLE(wxALIGN_CENTRE_HORIZONTAL
); 
  58     ADD_STYLE(wxALIGN_CENTER_VERTICAL
); 
  59     ADD_STYLE(wxALIGN_CENTRE_VERTICAL
); 
  64 wxObject 
*wxSizerXmlHandler::DoCreateResource() 
  66     if (m_Node
->GetName() == _T("sizeritem")) 
  68         wxXmlNode 
*n 
= GetParamNode(_T("window"))->GetChildren(); 
  72             if (n
->GetType() == wxXML_ELEMENT_NODE
) 
  74                 bool old_ins 
= m_IsInside
; 
  76                 wxObject 
*item 
= CreateResFromNode(n
, m_Parent
, NULL
); 
  78                 wxSizer 
*sizer 
= wxDynamicCast(item
, wxSizer
); 
  79                 wxWindow 
*wnd 
= wxDynamicCast(item
, wxWindow
); 
  82                     m_ParentSizer
->Add(sizer
, GetLong(_T("option")),  
  83                                        GetStyle(_T("flag")), GetLong(_T("border"))); 
  85                     m_ParentSizer
->Add(wnd
, GetLong(_T("option")),  
  86                                        GetStyle(_T("flag")), GetLong(_T("border"))); 
  88                     wxLogError(_T("Error in resource.")); 
  94         wxLogError(_T("Error in resource: no control/sizer within sizer's <item> tag.")); 
  98     else if (m_Node
->GetName() == _T("spacer")) 
 100         wxCHECK_MSG(m_ParentSizer
, NULL
, _T("Incorrect syntax of XML resource: spacer not within sizer!")); 
 101         wxSize sz 
= GetSize(); 
 102         m_ParentSizer
->Add(sz
.x
, sz
.y
, 
 103             GetLong(_T("option")), GetStyle(_T("flag")), GetLong(_T("border"))); 
 108     else if (m_Node
->GetName() == _T("notebooksizer")) 
 110         wxCHECK_MSG(m_ParentSizer
, NULL
, _T("Incorrect syntax of XML resource: notebooksizer not within sizer!"));         
 112         wxSizer 
*old_par 
= m_ParentSizer
; 
 113         m_ParentSizer 
= NULL
; 
 115         wxNotebook 
*nb 
= NULL
; 
 117         wxXmlNode 
*n 
= GetParamNode(_T("window"))->GetChildren(); 
 120             if (n
->GetType() == wxXML_ELEMENT_NODE
) 
 122                 item 
= CreateResFromNode(n
, m_Parent
, NULL
); 
 123                 nb 
= wxDynamicCast(item
, wxNotebook
); 
 129         m_ParentSizer 
= old_par
; 
 131         wxCHECK_MSG(nb
, NULL
, _T("Incorrect syntax of XML resource: notebooksizer must contain a notebook!")); 
 132         return new wxNotebookSizer(nb
); 
 137         wxSizer 
*sizer 
= NULL
; 
 139         wxXmlNode 
*parentNode 
= m_Node
->GetParent()->GetParent(); 
 141         wxCHECK_MSG(m_ParentSizer 
!= NULL 
|| 
 142                 ((parentNode
->GetName() == _T("panel") || 
 143                   parentNode
->GetName() == _T("dialog")) && 
 144                  parentNode
->GetType() == wxXML_ELEMENT_NODE
), NULL
, 
 145                 _T("Incorrect use of sizer: parent is not 'dialog' or 'panel'.")); 
 147         if (m_Node
->GetName() == _T("boxsizer")) 
 148             sizer 
= new wxBoxSizer(GetStyle(_T("orient"), wxHORIZONTAL
)); 
 150         else if (m_Node
->GetName() == _T("staticboxsizer")) 
 152             sizer 
= new wxStaticBoxSizer( 
 153                          new wxStaticBox(m_ParentAsWindow
, -1, GetText(_T("label"))), 
 154                          GetStyle(_T("orient"), wxHORIZONTAL
)); 
 157         else if (m_Node
->GetName() == _T("gridsizer")) 
 158             sizer 
= new wxGridSizer(GetLong(_T("rows")), GetLong(_T("cols")), 
 159                                     GetLong(_T("vgap")), GetLong(_T("hgap"))); 
 161         else if (m_Node
->GetName() == _T("flexgridsizer")) 
 162             sizer 
= new wxFlexGridSizer(GetLong(_T("rows")), GetLong(_T("cols")), 
 163                                     GetLong(_T("vgap")), GetLong(_T("hgap"))); 
 165         wxSizer 
*old_par 
= m_ParentSizer
; 
 166         m_ParentSizer 
= sizer
; 
 167         bool old_ins 
= m_IsInside
; 
 169         CreateChildren(m_Parent
, TRUE
/*only this handler*/); 
 170         m_IsInside 
= old_ins
; 
 171         m_ParentSizer 
= old_par
; 
 173         if (m_ParentSizer 
== NULL
) // setup window: 
 175             m_ParentAsWindow
->SetAutoLayout(TRUE
); 
 176             m_ParentAsWindow
->SetSizer(sizer
); 
 178             wxXmlNode 
*nd 
= m_Node
; 
 180             if (GetSize() == wxDefaultSize
) 
 181                 sizer
->Fit(m_ParentAsWindow
); 
 184             if (m_ParentAsWindow
->GetWindowStyle() & (wxRESIZE_BOX 
| wxRESIZE_BORDER
)) 
 185                 sizer
->SetSizeHints(m_ParentAsWindow
); 
 194 bool wxSizerXmlHandler::CanHandle(wxXmlNode 
*node
) 
 196     return ((!m_IsInside 
&& node
->GetName() == _T("boxsizer")) || 
 197             (!m_IsInside 
&& node
->GetName() == _T("staticboxsizer")) || 
 198             (!m_IsInside 
&& node
->GetName() == _T("gridsizer")) || 
 199             (!m_IsInside 
&& node
->GetName() == _T("flexgridsizer")) || 
 201             (!m_IsInside 
&& node
->GetName() == _T("notebooksizer")) || 
 203             (m_IsInside 
&& node
->GetName() == _T("sizeritem")) || 
 204             (m_IsInside 
&& node
->GetName() == _T("spacer")));