1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements a simple layout algorithm, plus
4 // wxSashLayoutWindow which is an example of a window with
5 // layout-awareness (via event handlers). This is suited to
6 // IDE-style window layout.
7 // Author: Julian Smart
11 // Copyright: (c) Julian Smart
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/laywin.h"
30 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent
, wxEvent
)
31 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent
, wxEvent
)
33 wxDEFINE_EVENT( wxEVT_QUERY_LAYOUT_INFO
, wxQueryLayoutInfoEvent
);
34 wxDEFINE_EVENT( wxEVT_CALCULATE_LAYOUT
, wxCalculateLayoutEvent
);
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
42 IMPLEMENT_CLASS(wxSashLayoutWindow
, wxSashWindow
)
43 BEGIN_EVENT_TABLE(wxSashLayoutWindow
, wxSashWindow
)
44 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout
)
45 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo
)
48 bool wxSashLayoutWindow::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
49 const wxSize
& size
, long style
, const wxString
& name
)
51 return wxSashWindow::Create(parent
, id
, pos
, size
, style
, name
);
54 void wxSashLayoutWindow::Init()
56 m_orientation
= wxLAYOUT_HORIZONTAL
;
57 m_alignment
= wxLAYOUT_TOP
;
59 MacSetClipChildren( true ) ;
63 // This is the function that wxLayoutAlgorithm calls to ascertain the window
65 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent
& event
)
67 // int flags = event.GetFlags();
68 int requestedLength
= event
.GetRequestedLength();
70 event
.SetOrientation(m_orientation
);
71 event
.SetAlignment(m_alignment
);
73 if (m_orientation
== wxLAYOUT_HORIZONTAL
)
74 event
.SetSize(wxSize(requestedLength
, m_defaultSize
.y
));
76 event
.SetSize(wxSize(m_defaultSize
.x
, requestedLength
));
79 // Called by parent to allow window to take a bit out of the
80 // client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
82 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent
& event
)
84 wxRect
clientSize(event
.GetRect());
86 int flags
= event
.GetFlags();
91 // Let's assume that all windows stretch the full extent of the window in
92 // the direction of that window orientation. This will work for non-docking toolbars,
93 // and the status bar. Note that the windows have to have been created in a certain
94 // order to work, else you might get a left-aligned window going to the bottom
95 // of the window, and the status bar appearing to the right of it. The
96 // status bar would have to be created after or before the toolbar(s).
101 int length
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? clientSize
.width
: clientSize
.height
;
102 wxLayoutOrientation orient
= GetOrientation();
104 // We assume that a window that says it's horizontal, wants to be stretched in that
105 // direction. Is this distinction too fine? Do we assume that any horizontal
106 // window needs to be stretched in that direction? Possibly.
107 int whichDimension
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? wxLAYOUT_LENGTH_X
: wxLAYOUT_LENGTH_Y
;
109 wxQueryLayoutInfoEvent
infoEvent(GetId());
110 infoEvent
.SetEventObject(this);
111 infoEvent
.SetRequestedLength(length
);
112 infoEvent
.SetFlags(orient
| whichDimension
);
114 if (!GetEventHandler()->ProcessEvent(infoEvent
))
117 wxSize sz
= infoEvent
.GetSize();
119 if (sz
.x
== 0 && sz
.y
== 0) // Assume it's invisible
122 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
124 switch (GetAlignment())
128 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
129 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
130 clientSize
.y
+= thisRect
.height
;
131 clientSize
.height
-= thisRect
.height
;
136 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
137 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
138 clientSize
.x
+= thisRect
.width
;
139 clientSize
.width
-= thisRect
.width
;
144 thisRect
.x
= clientSize
.x
+ (clientSize
.width
- sz
.x
); thisRect
.y
= clientSize
.y
;
145 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
146 clientSize
.width
-= thisRect
.width
;
149 case wxLAYOUT_BOTTOM
:
151 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
+ (clientSize
.height
- sz
.y
);
152 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
153 clientSize
.height
-= thisRect
.height
;
163 if ((flags
& wxLAYOUT_QUERY
) == 0)
165 // If not in query mode, resize the window.
166 // TODO: add wxRect& form to wxWindow::SetSize
167 wxSize sz2
= GetSize();
168 wxPoint pos
= GetPosition();
169 SetSize(thisRect
.x
, thisRect
.y
, thisRect
.width
, thisRect
.height
);
171 // Make sure the sash is erased when the window is resized
172 if ((pos
.x
!= thisRect
.x
|| pos
.y
!= thisRect
.y
|| sz2
.x
!= thisRect
.width
|| sz2
.y
!= thisRect
.height
) &&
173 (GetSashVisible(wxSASH_TOP
) || GetSashVisible(wxSASH_RIGHT
) || GetSashVisible(wxSASH_BOTTOM
) || GetSashVisible(wxSASH_LEFT
)))
178 event
.SetRect(clientSize
);
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 #if wxUSE_MDI_ARCHITECTURE
189 // Lays out windows for an MDI frame. The MDI client area gets what's left
191 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame
* frame
, wxRect
* r
)
194 frame
->GetClientSize(& cw
, & ch
);
196 wxRect
rect(0, 0, cw
, ch
);
200 wxCalculateLayoutEvent event
;
203 wxWindowList::compatibility_iterator node
= frame
->GetChildren().GetFirst();
206 wxWindow
* win
= node
->GetData();
208 event
.SetId(win
->GetId());
209 event
.SetEventObject(win
);
210 event
.SetFlags(0); // ??
212 win
->GetEventHandler()->ProcessEvent(event
);
214 node
= node
->GetNext();
217 wxWindow
* clientWindow
= frame
->GetClientWindow();
219 rect
= event
.GetRect();
221 clientWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
226 #endif // wxUSE_MDI_ARCHITECTURE
228 bool wxLayoutAlgorithm::LayoutFrame(wxFrame
* frame
, wxWindow
* mainWindow
)
230 return LayoutWindow(frame
, mainWindow
);
233 // Layout algorithm for any window. mainWindow gets what's left over.
234 bool wxLayoutAlgorithm::LayoutWindow(wxWindow
* parent
, wxWindow
* mainWindow
)
236 // Test if the parent is a sash window, and if so,
237 // reduce the available space to allow space for any active edges.
239 int leftMargin
= 0, rightMargin
= 0, topMargin
= 0, bottomMargin
= 0;
241 if (parent
->IsKindOf(CLASSINFO(wxSashWindow
)))
243 wxSashWindow
* sashWindow
= (wxSashWindow
*) parent
;
245 leftMargin
= sashWindow
->GetExtraBorderSize();
246 rightMargin
= sashWindow
->GetExtraBorderSize();
247 topMargin
= sashWindow
->GetExtraBorderSize();
248 bottomMargin
= sashWindow
->GetExtraBorderSize();
250 if (sashWindow
->GetSashVisible(wxSASH_LEFT
))
251 leftMargin
+= sashWindow
->GetDefaultBorderSize();
252 if (sashWindow
->GetSashVisible(wxSASH_RIGHT
))
253 rightMargin
+= sashWindow
->GetDefaultBorderSize();
254 if (sashWindow
->GetSashVisible(wxSASH_TOP
))
255 topMargin
+= sashWindow
->GetDefaultBorderSize();
256 if (sashWindow
->GetSashVisible(wxSASH_BOTTOM
))
257 bottomMargin
+= sashWindow
->GetDefaultBorderSize();
262 parent
->GetClientSize(& cw
, & ch
);
264 wxRect
rect(leftMargin
, topMargin
, cw
- leftMargin
- rightMargin
, ch
- topMargin
- bottomMargin
);
266 wxCalculateLayoutEvent event
;
269 // Find the last layout-aware window, so we can make it fill all remaining
271 wxWindow
*lastAwareWindow
= NULL
;
272 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
276 wxWindow
* win
= node
->GetData();
280 wxCalculateLayoutEvent
tempEvent(win
->GetId());
281 tempEvent
.SetEventObject(win
);
282 tempEvent
.SetFlags(wxLAYOUT_QUERY
);
283 tempEvent
.SetRect(event
.GetRect());
284 if (win
->GetEventHandler()->ProcessEvent(tempEvent
))
285 lastAwareWindow
= win
;
288 node
= node
->GetNext();
291 // Now do a dummy run to see if we have any space left for the final window (fail if not)
292 node
= parent
->GetChildren().GetFirst();
295 wxWindow
* win
= node
->GetData();
297 // If mainWindow is NULL and we're at the last window,
298 // skip this, because we'll simply make it fit the remaining space.
299 if (win
->IsShown() && (win
!= mainWindow
) && (mainWindow
!= NULL
|| win
!= lastAwareWindow
))
301 event
.SetId(win
->GetId());
302 event
.SetEventObject(win
);
303 event
.SetFlags(wxLAYOUT_QUERY
);
305 win
->GetEventHandler()->ProcessEvent(event
);
308 node
= node
->GetNext();
311 if (event
.GetRect().GetWidth() < 0 || event
.GetRect().GetHeight() < 0)
316 node
= parent
->GetChildren().GetFirst();
319 wxWindow
* win
= node
->GetData();
321 // If mainWindow is NULL and we're at the last window,
322 // skip this, because we'll simply make it fit the remaining space.
323 if (win
->IsShown() && (win
!= mainWindow
) && (mainWindow
!= NULL
|| win
!= lastAwareWindow
))
325 event
.SetId(win
->GetId());
326 event
.SetEventObject(win
);
327 event
.SetFlags(0); // ??
329 win
->GetEventHandler()->ProcessEvent(event
);
332 node
= node
->GetNext();
335 rect
= event
.GetRect();
338 mainWindow
->SetSize(rect
.x
, rect
.y
, wxMax(0, rect
.width
), wxMax(0, rect
.height
));
339 else if (lastAwareWindow
)
341 // Fit the remaining space
342 lastAwareWindow
->SetSize(rect
.x
, rect
.y
, wxMax(0, rect
.width
), wxMax(0, rect
.height
));