]> git.saurik.com Git - wxWidgets.git/blob - src/generic/laywin.cpp
defer queue-resize until after size-allocate processing, to keep it from being ignore...
[wxWidgets.git] / src / generic / laywin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/frame.h"
24 #endif
25
26 #include "wx/laywin.h"
27 #include "wx/mdi.h"
28
29
30 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
31 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
32
33 wxDEFINE_EVENT( wxEVT_QUERY_LAYOUT_INFO, wxQueryLayoutInfoEvent );
34 wxDEFINE_EVENT( wxEVT_CALCULATE_LAYOUT, wxCalculateLayoutEvent );
35
36
37 // ----------------------------------------------------------------------------
38 // wxSashLayoutWindow
39 // ----------------------------------------------------------------------------
40
41 #if wxUSE_SASH
42 IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
43 BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
44 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
45 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
46 END_EVENT_TABLE()
47
48 bool wxSashLayoutWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
49 const wxSize& size, long style, const wxString& name)
50 {
51 return wxSashWindow::Create(parent, id, pos, size, style, name);
52 }
53
54 void wxSashLayoutWindow::Init()
55 {
56 m_orientation = wxLAYOUT_HORIZONTAL;
57 m_alignment = wxLAYOUT_TOP;
58 #ifdef __WXMAC__
59 MacSetClipChildren( true ) ;
60 #endif
61 }
62
63 // This is the function that wxLayoutAlgorithm calls to ascertain the window
64 // dimensions.
65 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
66 {
67 // int flags = event.GetFlags();
68 int requestedLength = event.GetRequestedLength();
69
70 event.SetOrientation(m_orientation);
71 event.SetAlignment(m_alignment);
72
73 if (m_orientation == wxLAYOUT_HORIZONTAL)
74 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
75 else
76 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
77 }
78
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.
81
82 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
83 {
84 wxRect clientSize(event.GetRect());
85
86 int flags = event.GetFlags();
87
88 if (!IsShown())
89 return;
90
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).
97
98 wxRect thisRect;
99
100 // Try to stretch
101 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
102 wxLayoutOrientation orient = GetOrientation();
103
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;
108
109 wxQueryLayoutInfoEvent infoEvent(GetId());
110 infoEvent.SetEventObject(this);
111 infoEvent.SetRequestedLength(length);
112 infoEvent.SetFlags(orient | whichDimension);
113
114 if (!GetEventHandler()->ProcessEvent(infoEvent))
115 return;
116
117 wxSize sz = infoEvent.GetSize();
118
119 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
120 return;
121
122 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
123 // how it's aligned.
124 switch (GetAlignment())
125 {
126 case wxLAYOUT_TOP:
127 {
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;
132 break;
133 }
134 case wxLAYOUT_LEFT:
135 {
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;
140 break;
141 }
142 case wxLAYOUT_RIGHT:
143 {
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;
147 break;
148 }
149 case wxLAYOUT_BOTTOM:
150 {
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;
154 break;
155 }
156 case wxLAYOUT_NONE:
157 {
158 break;
159 }
160
161 }
162
163 if ((flags & wxLAYOUT_QUERY) == 0)
164 {
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);
170
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)))
174 Refresh(true);
175
176 }
177
178 event.SetRect(clientSize);
179 }
180 #endif // wxUSE_SASH
181
182
183 // ----------------------------------------------------------------------------
184 // wxLayoutAlgorithm
185 // ----------------------------------------------------------------------------
186
187 #if wxUSE_MDI_ARCHITECTURE
188
189 // Lays out windows for an MDI frame. The MDI client area gets what's left
190 // over.
191 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
192 {
193 int cw, ch;
194 frame->GetClientSize(& cw, & ch);
195
196 wxRect rect(0, 0, cw, ch);
197 if (r)
198 rect = * r;
199
200 wxCalculateLayoutEvent event;
201 event.SetRect(rect);
202
203 wxWindowList::compatibility_iterator node = frame->GetChildren().GetFirst();
204 while (node)
205 {
206 wxWindow* win = node->GetData();
207
208 event.SetId(win->GetId());
209 event.SetEventObject(win);
210 event.SetFlags(0); // ??
211
212 win->GetEventHandler()->ProcessEvent(event);
213
214 node = node->GetNext();
215 }
216
217 wxWindow* clientWindow = frame->GetClientWindow();
218
219 rect = event.GetRect();
220
221 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
222
223 return true;
224 }
225
226 #endif // wxUSE_MDI_ARCHITECTURE
227
228 bool wxLayoutAlgorithm::LayoutFrame(wxFrame* frame, wxWindow* mainWindow)
229 {
230 return LayoutWindow(frame, mainWindow);
231 }
232
233 // Layout algorithm for any window. mainWindow gets what's left over.
234 bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
235 {
236 // Test if the parent is a sash window, and if so,
237 // reduce the available space to allow space for any active edges.
238
239 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
240 #if wxUSE_SASH
241 if (parent->IsKindOf(CLASSINFO(wxSashWindow)))
242 {
243 wxSashWindow* sashWindow = (wxSashWindow*) parent;
244
245 leftMargin = sashWindow->GetExtraBorderSize();
246 rightMargin = sashWindow->GetExtraBorderSize();
247 topMargin = sashWindow->GetExtraBorderSize();
248 bottomMargin = sashWindow->GetExtraBorderSize();
249
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();
258 }
259 #endif // wxUSE_SASH
260
261 int cw, ch;
262 parent->GetClientSize(& cw, & ch);
263
264 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
265
266 wxCalculateLayoutEvent event;
267 event.SetRect(rect);
268
269 // Find the last layout-aware window, so we can make it fill all remaining
270 // space.
271 wxWindow *lastAwareWindow = NULL;
272 wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
273
274 while (node)
275 {
276 wxWindow* win = node->GetData();
277
278 if (win->IsShown())
279 {
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;
286 }
287
288 node = node->GetNext();
289 }
290
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();
293 while (node)
294 {
295 wxWindow* win = node->GetData();
296
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))
300 {
301 event.SetId(win->GetId());
302 event.SetEventObject(win);
303 event.SetFlags(wxLAYOUT_QUERY);
304
305 win->GetEventHandler()->ProcessEvent(event);
306 }
307
308 node = node->GetNext();
309 }
310
311 if (event.GetRect().GetWidth() < 0 || event.GetRect().GetHeight() < 0)
312 return false;
313
314 event.SetRect(rect);
315
316 node = parent->GetChildren().GetFirst();
317 while (node)
318 {
319 wxWindow* win = node->GetData();
320
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))
324 {
325 event.SetId(win->GetId());
326 event.SetEventObject(win);
327 event.SetFlags(0); // ??
328
329 win->GetEventHandler()->ProcessEvent(event);
330 }
331
332 node = node->GetNext();
333 }
334
335 rect = event.GetRect();
336
337 if (mainWindow)
338 mainWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
339 else if (lastAwareWindow)
340 {
341 // Fit the remaining space
342 lastAwareWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
343 }
344
345 return true;
346 }
347