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