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 /////////////////////////////////////////////////////////////////////////////
16 #pragma implementation "laywin.h"
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/laywin.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent
, wxEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent
, wxEvent
)
37 IMPLEMENT_CLASS(wxSashLayoutWindow
, wxSashWindow
)
39 BEGIN_EVENT_TABLE(wxSashLayoutWindow
, wxSashWindow
)
40 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout
)
41 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo
)
44 wxSashLayoutWindow::wxSashLayoutWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
45 const wxSize
& size
, long style
, const wxString
& name
):
46 wxSashWindow(parent
, id
, pos
, size
, style
, name
)
48 m_orientation
= wxLAYOUT_HORIZONTAL
;
49 m_alignment
= wxLAYOUT_TOP
;
52 // This is the function that wxLayoutAlgorithm calls to ascertain the window
54 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent
& event
)
56 // int flags = event.GetFlags();
57 int requestedLength
= event
.GetRequestedLength();
59 event
.SetOrientation(m_orientation
);
60 event
.SetAlignment(m_alignment
);
62 if (m_orientation
== wxLAYOUT_HORIZONTAL
)
63 event
.SetSize(wxSize(requestedLength
, m_defaultSize
.y
));
65 event
.SetSize(wxSize(m_defaultSize
.x
, requestedLength
));
68 // Called by parent to allow window to take a bit out of the
69 // client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
71 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent
& event
)
73 wxRect
clientSize(event
.GetRect());
75 int flags
= event
.GetFlags();
80 // Let's assume that all windows stretch the full extent of the window in
81 // the direction of that window orientation. This will work for non-docking toolbars,
82 // and the status bar. Note that the windows have to have been created in a certain
83 // order to work, else you might get a left-aligned window going to the bottom
84 // of the window, and the status bar appearing to the right of it. The
85 // status bar would have to be created after or before the toolbar(s).
90 int length
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? clientSize
.width
: clientSize
.height
;
91 wxLayoutOrientation orient
= GetOrientation();
93 // We assume that a window that says it's horizontal, wants to be stretched in that
94 // direction. Is this distinction too fine? Do we assume that any horizontal
95 // window needs to be stretched in that direction? Possibly.
96 int whichDimension
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? wxLAYOUT_LENGTH_X
: wxLAYOUT_LENGTH_Y
;
98 wxQueryLayoutInfoEvent
infoEvent(GetId());
99 infoEvent
.SetEventObject(this);
100 infoEvent
.SetRequestedLength(length
);
101 infoEvent
.SetFlags(orient
| whichDimension
);
103 if (!GetEventHandler()->ProcessEvent(infoEvent
))
106 wxSize sz
= infoEvent
.GetSize();
108 if (sz
.x
== 0 && sz
.y
== 0) // Assume it's invisible
111 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
113 switch (GetAlignment())
117 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
118 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
119 clientSize
.y
+= thisRect
.height
;
120 clientSize
.height
-= thisRect
.height
;
125 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
126 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
127 clientSize
.x
+= thisRect
.width
;
128 clientSize
.width
-= thisRect
.width
;
133 thisRect
.x
= clientSize
.x
+ (clientSize
.width
- sz
.x
); thisRect
.y
= clientSize
.y
;
134 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
135 clientSize
.width
-= thisRect
.width
;
138 case wxLAYOUT_BOTTOM
:
140 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
+ (clientSize
.height
- sz
.y
);
141 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
142 clientSize
.height
-= thisRect
.height
;
152 if ((flags
& wxLAYOUT_QUERY
) == 0)
154 // If not in query mode, resize the window.
155 // TODO: add wxRect& form to wxWindow::SetSize
156 SetSize(thisRect
.x
, thisRect
.y
, thisRect
.width
, thisRect
.height
);
159 event
.SetRect(clientSize
);
167 // Lays out windows for an MDI frame. The MDI client area gets what's left
169 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame
* frame
, wxRect
* r
)
172 frame
->GetClientSize(& cw
, & ch
);
174 wxRect
rect(0, 0, cw
, ch
);
178 wxCalculateLayoutEvent event
;
181 wxNode
* node
= frame
->GetChildren().First();
184 wxWindow
* win
= (wxWindow
*) node
->Data();
186 event
.SetId(win
->GetId());
187 event
.SetEventObject(win
);
188 event
.SetFlags(0); // ??
190 win
->GetEventHandler()->ProcessEvent(event
);
195 wxWindow
* clientWindow
= frame
->GetClientWindow();
197 rect
= event
.GetRect();
199 clientWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
204 // Layout algorithm for any window. mainWindow gets what's left over.
205 bool wxLayoutAlgorithm::LayoutWindow(wxWindow
* parent
, wxWindow
* mainWindow
)
207 // Test if the parent is a sash window, and if so,
208 // reduce the available space to allow space for any active edges.
210 int leftMargin
= 0, rightMargin
= 0, topMargin
= 0, bottomMargin
= 0;
212 if (parent
->IsKindOf(CLASSINFO(wxSashWindow
)))
214 wxSashWindow
* sashWindow
= (wxSashWindow
*) parent
;
216 leftMargin
= sashWindow
->GetExtraBorderSize();
217 rightMargin
= sashWindow
->GetExtraBorderSize();
218 topMargin
= sashWindow
->GetExtraBorderSize();
219 bottomMargin
= sashWindow
->GetExtraBorderSize();
221 if (sashWindow
->GetSashVisible(wxSASH_LEFT
))
222 leftMargin
+= sashWindow
->GetDefaultBorderSize();
223 if (sashWindow
->GetSashVisible(wxSASH_RIGHT
))
224 rightMargin
+= sashWindow
->GetDefaultBorderSize();
225 if (sashWindow
->GetSashVisible(wxSASH_TOP
))
226 topMargin
+= sashWindow
->GetDefaultBorderSize();
227 if (sashWindow
->GetSashVisible(wxSASH_BOTTOM
))
228 bottomMargin
+= sashWindow
->GetDefaultBorderSize();
233 parent
->GetClientSize(& cw
, & ch
);
235 wxRect
rect(leftMargin
, topMargin
, cw
- leftMargin
- rightMargin
, ch
- topMargin
- bottomMargin
);
237 wxCalculateLayoutEvent event
;
240 wxNode
* node
= parent
->GetChildren().First();
243 wxWindow
* win
= (wxWindow
*) node
->Data();
245 event
.SetId(win
->GetId());
246 event
.SetEventObject(win
);
247 event
.SetFlags(0); // ??
249 win
->GetEventHandler()->ProcessEvent(event
);
254 rect
= event
.GetRect();
257 mainWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);