Made wxLayoutAlgorithm more general (copes with nested sash windows)
[wxWidgets.git] / src / generic / laywin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: laywin.cpp
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
8 // Modified by:
9 // Created: 04/01/98
10 // RCS-ID: $Id$
11 // Copyright: (c) Julian Smart
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
14
15 #ifdef __GNUG__
16 #pragma implementation "laywin.h"
17 #endif
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #include "wx/mdi.h"
29 #endif
30
31 #include "wx/laywin.h"
32
33 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
34 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
35
36 IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
37
38 BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
39 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
40 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
41 END_EVENT_TABLE()
42
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)
46 {
47 m_orientation = wxLAYOUT_HORIZONTAL;
48 m_alignment = wxLAYOUT_TOP;
49 }
50
51 // This is the function that wxLayoutAlgorithm calls to ascertain the window
52 // dimensions.
53 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
54 {
55 // int flags = event.GetFlags();
56 int requestedLength = event.GetRequestedLength();
57
58 event.SetOrientation(m_orientation);
59 event.SetAlignment(m_alignment);
60
61 if (m_orientation == wxLAYOUT_HORIZONTAL)
62 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
63 else
64 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
65 }
66
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.
69
70 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
71 {
72 wxRect clientSize(event.GetRect());
73
74 int flags = event.GetFlags();
75
76 if (!IsShown())
77 return;
78
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).
85
86 wxRect thisRect;
87
88 // Try to stretch
89 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
90 wxLayoutOrientation orient = GetOrientation();
91
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;
96
97 wxQueryLayoutInfoEvent infoEvent(GetId());
98 infoEvent.SetEventObject(this);
99 infoEvent.SetRequestedLength(length);
100 infoEvent.SetFlags(orient | whichDimension);
101
102 if (!GetEventHandler()->ProcessEvent(infoEvent))
103 return;
104
105 wxSize sz = infoEvent.GetSize();
106
107 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
108 return;
109
110 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
111 // how it's aligned.
112 switch (GetAlignment())
113 {
114 case wxLAYOUT_TOP:
115 {
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;
120 break;
121 }
122 case wxLAYOUT_LEFT:
123 {
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;
128 break;
129 }
130 case wxLAYOUT_RIGHT:
131 {
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;
135 break;
136 }
137 case wxLAYOUT_BOTTOM:
138 {
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;
142 break;
143 }
144 case wxLAYOUT_NONE:
145 {
146 break;
147 }
148
149 }
150
151 if ((flags & wxLAYOUT_QUERY) == 0)
152 {
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);
156 }
157
158 event.SetRect(clientSize);
159 }
160
161 /*
162 * wxLayoutAlgorithm
163 */
164
165 // Lays out windows for an MDI frame. The MDI client area gets what's left
166 // over.
167 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
168 {
169 int cw, ch;
170 frame->GetClientSize(& cw, & ch);
171
172 wxRect rect(0, 0, cw, ch);
173 if (r)
174 rect = * r;
175
176 wxCalculateLayoutEvent event;
177 event.SetRect(rect);
178
179 wxNode* node = frame->GetChildren().First();
180 while (node)
181 {
182 wxWindow* win = (wxWindow*) node->Data();
183
184 event.SetId(win->GetId());
185 event.SetEventObject(win);
186 event.SetFlags(0); // ??
187
188 win->GetEventHandler()->ProcessEvent(event);
189
190 node = node->Next();
191 }
192
193 wxWindow* clientWindow = frame->GetClientWindow();
194
195 rect = event.GetRect();
196
197 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
198
199 return TRUE;
200 }
201
202 // Layout algorithm for any window. mainWindow gets what's left over.
203 bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
204 {
205 // Test if the parent is a sash window, and if so,
206 // reduce the available space to allow space for any active edges.
207
208 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
209 if (parent->IsKindOf(CLASSINFO(wxSashWindow)))
210 {
211 wxSashWindow* sashWindow = (wxSashWindow*) parent;
212
213 leftMargin = sashWindow->GetExtraBorderSize();
214 rightMargin = sashWindow->GetExtraBorderSize();
215 topMargin = sashWindow->GetExtraBorderSize();
216 bottomMargin = sashWindow->GetExtraBorderSize();
217
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();
226 }
227
228 int cw, ch;
229 parent->GetClientSize(& cw, & ch);
230
231 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
232
233 wxCalculateLayoutEvent event;
234 event.SetRect(rect);
235
236 wxNode* node = parent->GetChildren().First();
237 while (node)
238 {
239 wxWindow* win = (wxWindow*) node->Data();
240
241 event.SetId(win->GetId());
242 event.SetEventObject(win);
243 event.SetFlags(0); // ??
244
245 win->GetEventHandler()->ProcessEvent(event);
246
247 node = node->Next();
248 }
249
250 rect = event.GetRect();
251
252 if (mainWindow)
253 mainWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
254
255 return TRUE;
256 }
257