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