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
)
36 IMPLEMENT_CLASS(wxSashLayoutWindow
, wxSashWindow
)
38 BEGIN_EVENT_TABLE(wxSashLayoutWindow
, wxSashWindow
)
39 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout
)
40 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo
)
43 wxSashLayoutWindow::wxSashLayoutWindow(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
,
44 const wxSize
& size
, long style
, const wxString
& name
):
45 wxSashWindow(parent
, id
, pos
, size
, style
, name
)
47 m_orientation
= wxLAYOUT_HORIZONTAL
;
48 m_alignment
= wxLAYOUT_TOP
;
51 // This is the function that wxLayoutAlgorithm calls to ascertain the window
53 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent
& event
)
55 // int flags = event.GetFlags();
56 int requestedLength
= event
.GetRequestedLength();
58 event
.SetOrientation(m_orientation
);
59 event
.SetAlignment(m_alignment
);
61 if (m_orientation
== wxLAYOUT_HORIZONTAL
)
62 event
.SetSize(wxSize(requestedLength
, m_defaultSize
.y
));
64 event
.SetSize(wxSize(m_defaultSize
.x
, requestedLength
));
67 // Called by parent to allow window to take a bit out of the
68 // client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
70 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent
& event
)
72 wxRect
clientSize(event
.GetRect());
74 int flags
= event
.GetFlags();
79 // Let's assume that all windows stretch the full extent of the window in
80 // the direction of that window orientation. This will work for non-docking toolbars,
81 // and the status bar. Note that the windows have to have been created in a certain
82 // order to work, else you might get a left-aligned window going to the bottom
83 // of the window, and the status bar appearing to the right of it. The
84 // status bar would have to be created after or before the toolbar(s).
89 int length
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? clientSize
.width
: clientSize
.height
;
90 wxLayoutOrientation orient
= GetOrientation();
92 // We assume that a window that says it's horizontal, wants to be stretched in that
93 // direction. Is this distinction too fine? Do we assume that any horizontal
94 // window needs to be stretched in that direction? Possibly.
95 int whichDimension
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? wxLAYOUT_LENGTH_X
: wxLAYOUT_LENGTH_Y
;
97 wxQueryLayoutInfoEvent
infoEvent(GetId());
98 infoEvent
.SetEventObject(this);
99 infoEvent
.SetRequestedLength(length
);
100 infoEvent
.SetFlags(orient
| whichDimension
);
102 if (!GetEventHandler()->ProcessEvent(infoEvent
))
105 wxSize sz
= infoEvent
.GetSize();
107 if (sz
.x
== 0 && sz
.y
== 0) // Assume it's invisible
110 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
112 switch (GetAlignment())
116 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
117 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
118 clientSize
.y
+= thisRect
.height
;
119 clientSize
.height
-= thisRect
.height
;
124 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
;
125 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
126 clientSize
.x
+= thisRect
.width
;
127 clientSize
.width
-= thisRect
.width
;
132 thisRect
.x
= clientSize
.x
+ (clientSize
.width
- sz
.x
); thisRect
.y
= clientSize
.y
;
133 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
134 clientSize
.width
-= thisRect
.width
;
137 case wxLAYOUT_BOTTOM
:
139 thisRect
.x
= clientSize
.x
; thisRect
.y
= clientSize
.y
+ (clientSize
.height
- sz
.y
);
140 thisRect
.width
= sz
.x
; thisRect
.height
= sz
.y
;
141 clientSize
.height
-= thisRect
.height
;
151 if ((flags
& wxLAYOUT_QUERY
) == 0)
153 // If not in query mode, resize the window.
154 // TODO: add wxRect& form to wxWindow::SetSize
155 SetSize(thisRect
.x
, thisRect
.y
, thisRect
.width
, thisRect
.height
);
158 event
.SetRect(clientSize
);
165 // Lays out windows for an MDI frame. The MDI client area gets what's left
167 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame
* frame
, wxRect
* r
)
170 frame
->GetClientSize(& cw
, & ch
);
172 wxRect
rect(0, 0, cw
, ch
);
176 wxCalculateLayoutEvent event
;
179 wxNode
* node
= frame
->GetChildren().First();
182 wxWindow
* win
= (wxWindow
*) node
->Data();
184 event
.SetId(win
->GetId());
185 event
.SetEventObject(win
);
186 event
.SetFlags(0); // ??
188 win
->GetEventHandler()->ProcessEvent(event
);
193 wxWindow
* clientWindow
= frame
->GetClientWindow();
195 rect
= event
.GetRect();
197 clientWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
202 // Layout algorithm for any window. mainWindow gets what's left over.
203 bool wxLayoutAlgorithm::LayoutWindow(wxWindow
* parent
, wxWindow
* mainWindow
)
205 // Test if the parent is a sash window, and if so,
206 // reduce the available space to allow space for any active edges.
208 int leftMargin
= 0, rightMargin
= 0, topMargin
= 0, bottomMargin
= 0;
209 if (parent
->IsKindOf(CLASSINFO(wxSashWindow
)))
211 wxSashWindow
* sashWindow
= (wxSashWindow
*) parent
;
213 leftMargin
= sashWindow
->GetExtraBorderSize();
214 rightMargin
= sashWindow
->GetExtraBorderSize();
215 topMargin
= sashWindow
->GetExtraBorderSize();
216 bottomMargin
= sashWindow
->GetExtraBorderSize();
218 if (sashWindow
->GetSashVisible(wxSASH_LEFT
))
219 leftMargin
+= sashWindow
->GetDefaultBorderSize();
220 if (sashWindow
->GetSashVisible(wxSASH_RIGHT
))
221 rightMargin
+= sashWindow
->GetDefaultBorderSize();
222 if (sashWindow
->GetSashVisible(wxSASH_TOP
))
223 topMargin
+= sashWindow
->GetDefaultBorderSize();
224 if (sashWindow
->GetSashVisible(wxSASH_BOTTOM
))
225 bottomMargin
+= sashWindow
->GetDefaultBorderSize();
229 parent
->GetClientSize(& cw
, & ch
);
231 wxRect
rect(leftMargin
, topMargin
, cw
- leftMargin
- rightMargin
, ch
- topMargin
- bottomMargin
);
233 wxCalculateLayoutEvent event
;
236 wxNode
* node
= parent
->GetChildren().First();
239 wxWindow
* win
= (wxWindow
*) node
->Data();
241 event
.SetId(win
->GetId());
242 event
.SetEventObject(win
);
243 event
.SetFlags(0); // ??
245 win
->GetEventHandler()->ProcessEvent(event
);
250 rect
= event
.GetRect();
253 mainWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);