]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sizer.cpp
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sizer.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 //---------------------------------------------------------------------------
28 //---------------------------------------------------------------------------
30 wxNewSizerItem::wxNewSizerItem( int width
, int height
, int option
, int flag
, int border
)
32 m_window
= (wxWindow
*) NULL
;
33 m_sizer
= (wxNewSizer
*) NULL
;
38 // minimal size is the initial size
42 // size is set directly
46 wxNewSizerItem::wxNewSizerItem( wxWindow
*window
, int option
, int flag
, int border
)
49 m_sizer
= (wxNewSizer
*) NULL
;
54 // minimal size is the initial size
55 m_minSize
= window
->GetSize();
57 // size is calculated later
61 wxNewSizerItem::wxNewSizerItem( wxNewSizer
*sizer
, int option
, int flag
, int border
)
63 m_window
= (wxWindow
*) NULL
;
69 // minimal size is calculated later
72 // size is calculated later
76 wxSize
wxNewSizerItem::GetSize()
80 ret
= m_sizer
->GetSize();
83 ret
= m_window
->GetSize();
98 wxSize
wxNewSizerItem::CalcMin()
102 ret
= m_sizer
->CalcMin();
104 The minimum size of a window should be the
105 initial size, as saved in m_minSize, not the
110 ret = m_window->GetSize();
112 else ret
= m_minSize
;
118 if (m_flag
& wxNORTH
)
120 if (m_flag
& wxSOUTH
)
126 void wxNewSizerItem::SetDimension( wxPoint pos
, wxSize size
)
137 if (m_flag
& wxNORTH
)
142 if (m_flag
& wxSOUTH
)
148 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
151 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
);
156 bool wxNewSizerItem::IsWindow()
158 return (m_window
!= NULL
);
161 bool wxNewSizerItem::IsNewSizer()
163 return (m_sizer
!= NULL
);
166 bool wxNewSizerItem::IsSpacer()
168 return (m_window
== NULL
) && (m_sizer
== NULL
);
171 //---------------------------------------------------------------------------
173 //---------------------------------------------------------------------------
175 wxNewSizer::wxNewSizer()
177 m_children
.DeleteContents( TRUE
);
180 wxNewSizer::~wxNewSizer()
184 void wxNewSizer::Add( wxWindow
*window
, int option
, int flag
, int border
)
186 m_children
.Append( new wxNewSizerItem( window
, option
, flag
, border
) );
189 void wxNewSizer::Add( wxNewSizer
*sizer
, int option
, int flag
, int border
)
191 m_children
.Append( new wxNewSizerItem( sizer
, option
, flag
, border
) );
194 void wxNewSizer::Add( int width
, int height
, int option
, int flag
, int border
)
196 m_children
.Append( new wxNewSizerItem( width
, height
, option
, flag
, border
) );
199 void wxNewSizer::Fit( wxWindow
*window
)
201 window
->SetSize( GetMinWindowSize( window
) );
204 void wxNewSizer::Layout()
210 void wxNewSizer::SetSizeHints( wxWindow
*window
)
212 wxSize
size( GetMinWindowSize( window
) );
213 window
->SetSizeHints( size
.x
, size
.y
);
216 wxSize
wxNewSizer::GetMinWindowSize( wxWindow
*window
)
218 wxSize
minSize( GetMinSize() );
219 wxSize
size( window
->GetSize() );
220 wxSize
client_size( window
->GetClientSize() );
221 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
222 minSize
.y
+size
.y
-client_size
.y
);
225 void wxNewSizer::SetDimension( int x
, int y
, int width
, int height
)
234 //---------------------------------------------------------------------------
236 //---------------------------------------------------------------------------
238 wxBox::wxBox( int orient
)
243 void wxBox::RecalcSizes()
245 if (m_children
.GetCount() == 0)
247 SetDimension( m_position
.x
, m_position
.y
, 2, 2 );
255 if (m_orient
== wxHORIZONTAL
)
257 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
258 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
262 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
263 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
267 wxPoint
pt( m_position
);
269 wxNode
*node
= m_children
.GetFirst();
272 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
275 if (item
->GetOption())
276 weight
= item
->GetOption();
278 wxSize
size( item
->CalcMin() );
280 if (m_orient
== wxVERTICAL
)
282 long height
= size
.y
;
283 if (item
->GetOption())
285 height
= (delta
* weight
) + extra
;
286 extra
= 0; // only the first item will get the remainder as extra size
289 wxPoint
child_pos( pt
);
290 wxSize
child_size( wxSize( size
.x
, height
) );
292 if (item
->GetFlag() & wxALIGN_RIGHT
)
293 child_pos
.x
+= m_size
.x
- size
.x
;
294 else if (item
->GetFlag() & wxCENTER
)
295 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
296 else if (item
->GetFlag() & wxEXPAND
)
297 child_size
.x
= m_size
.x
;
299 item
->SetDimension( child_pos
, child_size
);
306 if (item
->GetOption())
308 width
= (delta
* weight
) + extra
;
309 extra
= 0; // only the first item will get the remainder as extra size
312 wxPoint
child_pos( pt
);
313 wxSize
child_size( wxSize(width
, size
.y
) );
315 if (item
->GetFlag() & wxALIGN_BOTTOM
)
316 child_pos
.y
+= m_size
.y
- size
.y
;
317 else if (item
->GetFlag() & wxCENTER
)
318 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
319 else if (item
->GetFlag() & wxEXPAND
)
320 child_size
.y
= m_size
.y
;
322 item
->SetDimension( child_pos
, child_size
);
331 wxSize
wxBox::CalcMin()
333 if (m_children
.GetCount() == 0)
342 wxNode
*node
= m_children
.GetFirst();
345 wxNewSizerItem
*item
= (wxNewSizerItem
*) node
->Data();
348 if (item
->GetOption())
349 weight
= item
->GetOption();
351 wxSize
size( item
->CalcMin() );
353 if (m_orient
== wxHORIZONTAL
)
355 m_minWidth
+= (size
.x
* weight
);
356 m_minHeight
= wxMax( m_minHeight
, size
.y
);
360 m_minHeight
+= (size
.y
* weight
);
361 m_minWidth
= wxMax( m_minWidth
, size
.x
);
364 if (item
->GetOption())
366 m_stretchable
+= weight
;
370 if (m_orient
== wxVERTICAL
)
372 m_fixedHeight
+= size
.y
;
373 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
377 m_fixedWidth
+= size
.x
;
378 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
385 return wxSize( m_minWidth
, m_minHeight
);