ea3019c03760abbf73ddab47d11ba8f0b556dea1
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: provide wxNewSizer class for layounting
4 // Author: Robert Roebling and Robin Dunn
8 // Copyright: (c) Robin Dunn, Dirk Holtwick and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef __WXSIZERS_H__
13 #define __WXSIZERS_H__
16 #pragma implementation "sizer.h"
22 //---------------------------------------------------------------------------
24 //---------------------------------------------------------------------------
26 wxNewSizerItem::wxNewSizerItem( int width
, int height
, int option
)
28 m_window
= (wxWindow
*) NULL
;
29 m_sizer
= (wxNewSizer
*) NULL
;
35 wxNewSizerItem::wxNewSizerItem( wxWindow
*window
, int option
)
38 m_sizer
= (wxNewSizer
*) NULL
;
39 m_minSize
= window
->GetSize();
43 wxNewSizerItem::wxNewSizerItem( wxNewSizer
*sizer
, int option
)
45 m_window
= (wxWindow
*) NULL
;
47 m_minSize
= sizer
->GetSize();
51 wxSize
wxNewSizerItem::GetSize()
54 return m_sizer
->GetSize();
57 return m_window
->GetSize();
62 wxSize
wxNewSizerItem::CalcMin()
65 return m_sizer
->CalcMin();
68 return m_window
->GetSize();
73 void wxNewSizerItem::SetDimension( wxPoint pos
, wxSize size
)
76 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
79 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
);
84 bool wxNewSizerItem::IsWindow()
86 return (m_window
!= NULL
);
89 bool wxNewSizerItem::IsNewSizer()
91 return (m_sizer
!= NULL
);
94 bool wxNewSizerItem::IsSpacer()
96 return (m_window
== NULL
) && (m_sizer
== NULL
);
99 //---------------------------------------------------------------------------
101 //---------------------------------------------------------------------------
103 wxNewSizer::wxNewSizer()
105 m_children
.DeleteContents( TRUE
);
108 wxNewSizer::~wxNewSizer()
112 void wxNewSizer::Add( wxWindow
*window
, int option
)
114 m_children
.Append( new wxNewSizerItem( window
, option
) );
117 void wxNewSizer::Add( wxNewSizer
*sizer
, int option
)
119 m_children
.Append( new wxNewSizerItem( sizer
, option
) );
122 void wxNewSizer::Add( int width
, int height
, int option
)
124 m_children
.Append( new wxNewSizerItem( width
, height
, option
) );
127 void wxNewSizer::Fit( wxWindow
*window
)
129 window
->SetSize( GetMinWindowSize( window
) );
132 void wxNewSizer::Layout()
138 void wxNewSizer::SetSizeHints( wxWindow
*window
)
140 wxSize
size( GetMinWindowSize( window
) );
141 window
->SetSizeHints( size
.x
, size
.y
);
144 wxSize
wxNewSizer::GetMinWindowSize( wxWindow
*window
)
146 wxSize
min( GetMinSize() );
147 wxSize
size( window
->GetSize() );
148 wxSize
client_size( window
->GetClientSize() );
149 return wxSize( min
.x
+size
.x
-client_size
.x
, min
.y
+size
.y
-client_size
.y
);
152 void wxNewSizer::SetDimension( int x
, int y
, int width
, int height
)
161 //---------------------------------------------------------------------------
163 //---------------------------------------------------------------------------
165 wxBorderNewSizer::wxBorderNewSizer( int sides
)
170 void wxBorderNewSizer::Add( wxWindow
*window
, int option
)
172 wxCHECK_RET( m_children
.GetCount() == 0, _T("border sizer can only hold one child") );
174 wxNewSizer::Add( window
, option
);
177 void wxBorderNewSizer::Add( wxNewSizer
*sizer
, int option
)
179 wxCHECK_RET( m_children
.GetCount() == 0, _T("border sizer can only hold one child") );
181 wxNewSizer::Add( sizer
, option
);
184 void wxBorderNewSizer::Add( int width
, int height
, int option
)
186 wxCHECK_RET( m_children
.GetCount() == 0, _T("border sizer can only hold one child") );
188 wxNewSizer::Add( width
, height
, option
);
191 void wxBorderNewSizer::RecalcSizes()
193 wxNode
*node
= m_children
.GetFirst();
197 SetDimension( m_position
.x
, m_position
.y
, 2, 2 );
201 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
203 wxSize
size( m_size
);
204 wxPoint
pt( m_position
);
205 int borderSize
= item
->GetOption();
207 if (m_sides
& wxWEST
)
209 size
.x
-= borderSize
;
212 if (m_sides
& wxEAST
)
214 size
.x
-= borderSize
;
216 if (m_sides
& wxNORTH
)
218 size
.y
-= borderSize
;
221 if (m_sides
& wxSOUTH
)
223 size
.y
-= borderSize
;
226 item
->SetDimension( pt
, size
);
229 wxSize
wxBorderNewSizer::CalcMin()
231 wxNode
*node
= m_children
.GetFirst();
236 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
238 wxSize
size( item
->CalcMin() );
240 int borderSize
= item
->GetOption();
242 if (m_sides
& wxWEST
)
243 size
.x
+= borderSize
;
244 if (m_sides
& wxEAST
)
245 size
.x
+= borderSize
;
246 if (m_sides
& wxNORTH
)
247 size
.y
+= borderSize
;
248 if (m_sides
& wxSOUTH
)
249 size
.y
+= borderSize
;
254 //---------------------------------------------------------------------------
256 //---------------------------------------------------------------------------
258 wxBoxNewSizer::wxBoxNewSizer( int orient
)
263 void wxBoxNewSizer::RecalcSizes()
265 if (m_children
.GetCount() == 0)
267 SetDimension( m_position
.x
, m_position
.y
, 2, 2 );
275 if (m_orient
== wxHORIZONTAL
)
277 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
278 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
282 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
283 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
287 wxPoint
pt( m_position
);
289 wxNode
*node
= m_children
.GetFirst();
292 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
295 if (item
->GetOption())
296 weight
= item
->GetOption();
298 wxSize
size( item
->CalcMin() );
300 if (m_orient
== wxVERTICAL
)
302 long height
= size
.y
;
303 if (item
->GetOption())
305 height
= (delta
* weight
) + extra
;
306 extra
= 0; // only the first item will get the remainder as extra size
308 item
->SetDimension( pt
, wxSize( size
.x
, height
) );
314 if (item
->GetOption())
316 width
= (delta
* weight
) + extra
;
317 extra
= 0; // only the first item will get the remainder as extra size
319 item
->SetDimension( pt
, wxSize(width
, size
.y
) );
327 wxSize
wxBoxNewSizer::CalcMin()
329 if (m_children
.GetCount() == 0)
338 wxNode
*node
= m_children
.GetFirst();
341 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
344 if (item
->GetOption())
345 weight
= item
->GetOption();
347 wxSize
size( item
->CalcMin() );
349 if (m_orient
== wxHORIZONTAL
)
351 m_minWidth
+= (size
.x
* weight
);
352 m_minHeight
= wxMax( m_minHeight
, size
.y
);
356 m_minHeight
+= (size
.y
* weight
);
357 m_minWidth
= wxMax( m_minWidth
, size
.x
);
360 if (item
->GetOption())
362 m_stretchable
+= weight
;
366 if (m_orient
== wxVERTICAL
)
368 m_fixedHeight
+= size
.y
;
369 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
373 m_fixedWidth
+= size
.x
;
374 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
381 return wxSize( m_minWidth
, m_minHeight
);