]> git.saurik.com Git - wxWidgets.git/blame - src/generic/laywin.cpp
check for NULL pointer in OnMeasureItem() as well as in OnDrawItem()
[wxWidgets.git] / src / generic / laywin.cpp
CommitLineData
a6d70308
JS
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
65571936 12// Licence: wxWindows licence
a6d70308
JS
13/////////////////////////////////////////////////////////////////////////////
14
14f355c2 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27529614
JS
16#pragma implementation "laywin.h"
17#endif
18
a6d70308
JS
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
04dbb646
VZ
27 #include "wx/frame.h"
28 #include "wx/mdi.h"
a6d70308
JS
29#endif
30
31#include "wx/laywin.h"
32
33IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
34IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
35
2e4df4bf
VZ
36DEFINE_EVENT_TYPE(wxEVT_QUERY_LAYOUT_INFO)
37DEFINE_EVENT_TYPE(wxEVT_CALCULATE_LAYOUT)
82a5f02c 38
88ac883a 39#if wxUSE_SASH
a6d70308
JS
40IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
41
42BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
43 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
44 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
45END_EVENT_TABLE()
46
f6bcfd97
BP
47bool wxSashLayoutWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
48 const wxSize& size, long style, const wxString& name)
49{
50 return wxSashWindow::Create(parent, id, pos, size, style, name);
51}
52
53void wxSashLayoutWindow::Init()
a6d70308
JS
54{
55 m_orientation = wxLAYOUT_HORIZONTAL;
56 m_alignment = wxLAYOUT_TOP;
8adc196b
SC
57#ifdef __WXMAC__
58 MacSetClipChildren( true ) ;
59#endif
a6d70308
JS
60}
61
2243eed5 62// This is the function that wxLayoutAlgorithm calls to ascertain the window
a6d70308
JS
63// dimensions.
64void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
65{
341287bf 66 // int flags = event.GetFlags();
a6d70308
JS
67 int requestedLength = event.GetRequestedLength();
68
a6d70308
JS
69 event.SetOrientation(m_orientation);
70 event.SetAlignment(m_alignment);
71
72 if (m_orientation == wxLAYOUT_HORIZONTAL)
73 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
74 else
75 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
76}
77
78// Called by parent to allow window to take a bit out of the
79// client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
a6d70308
JS
80
81void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
82{
83 wxRect clientSize(event.GetRect());
84
85 int flags = event.GetFlags();
86
87 if (!IsShown())
88 return;
89
90 // Let's assume that all windows stretch the full extent of the window in
91 // the direction of that window orientation. This will work for non-docking toolbars,
92 // and the status bar. Note that the windows have to have been created in a certain
93 // order to work, else you might get a left-aligned window going to the bottom
94 // of the window, and the status bar appearing to the right of it. The
95 // status bar would have to be created after or before the toolbar(s).
96
97 wxRect thisRect;
98
99 // Try to stretch
100 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
101 wxLayoutOrientation orient = GetOrientation();
102
103 // We assume that a window that says it's horizontal, wants to be stretched in that
104 // direction. Is this distinction too fine? Do we assume that any horizontal
105 // window needs to be stretched in that direction? Possibly.
106 int whichDimension = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? wxLAYOUT_LENGTH_X : wxLAYOUT_LENGTH_Y;
107
108 wxQueryLayoutInfoEvent infoEvent(GetId());
109 infoEvent.SetEventObject(this);
110 infoEvent.SetRequestedLength(length);
111 infoEvent.SetFlags(orient | whichDimension);
112
113 if (!GetEventHandler()->ProcessEvent(infoEvent))
114 return;
115
116 wxSize sz = infoEvent.GetSize();
117
118 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
119 return;
120
121 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
122 // how it's aligned.
123 switch (GetAlignment())
124 {
125 case wxLAYOUT_TOP:
126 {
127 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
128 thisRect.width = sz.x; thisRect.height = sz.y;
129 clientSize.y += thisRect.height;
130 clientSize.height -= thisRect.height;
131 break;
132 }
133 case wxLAYOUT_LEFT:
134 {
135 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
136 thisRect.width = sz.x; thisRect.height = sz.y;
137 clientSize.x += thisRect.width;
138 clientSize.width -= thisRect.width;
139 break;
140 }
141 case wxLAYOUT_RIGHT:
142 {
143 thisRect.x = clientSize.x + (clientSize.width - sz.x); thisRect.y = clientSize.y;
144 thisRect.width = sz.x; thisRect.height = sz.y;
145 clientSize.width -= thisRect.width;
146 break;
147 }
148 case wxLAYOUT_BOTTOM:
149 {
150 thisRect.x = clientSize.x; thisRect.y = clientSize.y + (clientSize.height - sz.y);
151 thisRect.width = sz.x; thisRect.height = sz.y;
152 clientSize.height -= thisRect.height;
153 break;
154 }
341287bf
JS
155 case wxLAYOUT_NONE:
156 {
88ac883a 157 break;
341287bf
JS
158 }
159
a6d70308
JS
160 }
161
162 if ((flags & wxLAYOUT_QUERY) == 0)
163 {
164 // If not in query mode, resize the window.
165 // TODO: add wxRect& form to wxWindow::SetSize
f6bcfd97
BP
166 wxSize sz = GetSize();
167 wxPoint pos = GetPosition();
a6d70308 168 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
f6bcfd97
BP
169
170 // Make sure the sash is erased when the window is resized
171 if ((pos.x != thisRect.x || pos.y != thisRect.y || sz.x != thisRect.width || sz.y != thisRect.height) &&
172 (GetSashVisible(wxSASH_TOP) || GetSashVisible(wxSASH_RIGHT) || GetSashVisible(wxSASH_BOTTOM) || GetSashVisible(wxSASH_LEFT)))
ca65c044 173 Refresh(true);
f6bcfd97 174
a6d70308
JS
175 }
176
177 event.SetRect(clientSize);
178}
88ac883a 179#endif // wxUSE_SASH
a6d70308
JS
180
181/*
182 * wxLayoutAlgorithm
183 */
184
1e6feb95
VZ
185#if wxUSE_MDI_ARCHITECTURE
186
a6d70308
JS
187// Lays out windows for an MDI frame. The MDI client area gets what's left
188// over.
2243eed5 189bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
a6d70308
JS
190{
191 int cw, ch;
192 frame->GetClientSize(& cw, & ch);
193
194 wxRect rect(0, 0, cw, ch);
2243eed5
JS
195 if (r)
196 rect = * r;
a6d70308
JS
197
198 wxCalculateLayoutEvent event;
199 event.SetRect(rect);
200
222ed1d6 201 wxWindowList::compatibility_iterator node = frame->GetChildren().GetFirst();
a6d70308
JS
202 while (node)
203 {
b1d4dd7a 204 wxWindow* win = node->GetData();
a6d70308
JS
205
206 event.SetId(win->GetId());
207 event.SetEventObject(win);
208 event.SetFlags(0); // ??
209
210 win->GetEventHandler()->ProcessEvent(event);
211
b1d4dd7a 212 node = node->GetNext();
a6d70308
JS
213 }
214
215 wxWindow* clientWindow = frame->GetClientWindow();
216
217 rect = event.GetRect();
218
219 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
220
ca65c044 221 return true;
a6d70308
JS
222}
223
1e6feb95
VZ
224#endif // wxUSE_MDI_ARCHITECTURE
225
04dbb646
VZ
226bool wxLayoutAlgorithm::LayoutFrame(wxFrame* frame, wxWindow* mainWindow)
227{
228 return LayoutWindow(frame, mainWindow);
229}
230
f9b1708c
JS
231// Layout algorithm for any window. mainWindow gets what's left over.
232bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
a6d70308 233{
f9b1708c
JS
234 // Test if the parent is a sash window, and if so,
235 // reduce the available space to allow space for any active edges.
236
237 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
88ac883a 238#if wxUSE_SASH
f9b1708c
JS
239 if (parent->IsKindOf(CLASSINFO(wxSashWindow)))
240 {
241 wxSashWindow* sashWindow = (wxSashWindow*) parent;
242
243 leftMargin = sashWindow->GetExtraBorderSize();
244 rightMargin = sashWindow->GetExtraBorderSize();
245 topMargin = sashWindow->GetExtraBorderSize();
246 bottomMargin = sashWindow->GetExtraBorderSize();
247
248 if (sashWindow->GetSashVisible(wxSASH_LEFT))
249 leftMargin += sashWindow->GetDefaultBorderSize();
250 if (sashWindow->GetSashVisible(wxSASH_RIGHT))
251 rightMargin += sashWindow->GetDefaultBorderSize();
252 if (sashWindow->GetSashVisible(wxSASH_TOP))
253 topMargin += sashWindow->GetDefaultBorderSize();
254 if (sashWindow->GetSashVisible(wxSASH_BOTTOM))
255 bottomMargin += sashWindow->GetDefaultBorderSize();
256 }
88ac883a 257#endif // wxUSE_SASH
f9b1708c 258
a6d70308 259 int cw, ch;
f9b1708c 260 parent->GetClientSize(& cw, & ch);
a6d70308 261
f9b1708c 262 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
a6d70308
JS
263
264 wxCalculateLayoutEvent event;
265 event.SetRect(rect);
266
2f82899b
JS
267 // Find the last layout-aware window, so we can make it fill all remaining
268 // space.
ca65c044 269 wxWindow *lastAwareWindow = NULL;
222ed1d6 270 wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
b1d4dd7a 271
a6d70308
JS
272 while (node)
273 {
b1d4dd7a 274 wxWindow* win = node->GetData();
a6d70308 275
2f82899b
JS
276 if (win->IsShown())
277 {
278 wxCalculateLayoutEvent tempEvent(win->GetId());
279 tempEvent.SetEventObject(win);
280 tempEvent.SetFlags(wxLAYOUT_QUERY);
281 tempEvent.SetRect(event.GetRect());
282 if (win->GetEventHandler()->ProcessEvent(tempEvent))
283 lastAwareWindow = win;
284 }
285
b1d4dd7a 286 node = node->GetNext();
2f82899b
JS
287 }
288
8c023dee 289 // Now do a dummy run to see if we have any space left for the final window (fail if not)
b1d4dd7a 290 node = parent->GetChildren().GetFirst();
2f82899b
JS
291 while (node)
292 {
b1d4dd7a 293 wxWindow* win = node->GetData();
2f82899b
JS
294
295 // If mainWindow is NULL and we're at the last window,
296 // skip this, because we'll simply make it fit the remaining space.
8c023dee
JS
297 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
298 {
299 event.SetId(win->GetId());
300 event.SetEventObject(win);
301 event.SetFlags(wxLAYOUT_QUERY);
302
303 win->GetEventHandler()->ProcessEvent(event);
304 }
305
b1d4dd7a 306 node = node->GetNext();
8c023dee
JS
307 }
308
309 if (event.GetRect().GetWidth() < 0 || event.GetRect().GetHeight() < 0)
ca65c044 310 return false;
8c023dee
JS
311
312 event.SetRect(rect);
313
b1d4dd7a 314 node = parent->GetChildren().GetFirst();
8c023dee
JS
315 while (node)
316 {
b1d4dd7a 317 wxWindow* win = node->GetData();
8c023dee
JS
318
319 // If mainWindow is NULL and we're at the last window,
320 // skip this, because we'll simply make it fit the remaining space.
321 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
f6bcfd97
BP
322 {
323 event.SetId(win->GetId());
324 event.SetEventObject(win);
325 event.SetFlags(0); // ??
a6d70308 326
f6bcfd97
BP
327 win->GetEventHandler()->ProcessEvent(event);
328 }
a6d70308 329
b1d4dd7a 330 node = node->GetNext();
a6d70308
JS
331 }
332
333 rect = event.GetRect();
334
44c4a334 335 if (mainWindow)
d11bb14f 336 mainWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
2f82899b
JS
337 else if (lastAwareWindow)
338 {
339 // Fit the remaining space
d11bb14f 340 lastAwareWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
2f82899b 341 }
a6d70308 342
ca65c044 343 return true;
a6d70308
JS
344}
345