]> git.saurik.com Git - wxWidgets.git/blob - src/generic/laywin.cpp
bug fix for m_parent == NULL
[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 #if wxUSE_SASH
37 IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
38
39 BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
40 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
41 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
42 END_EVENT_TABLE()
43
44 wxSashLayoutWindow::wxSashLayoutWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos,
45 const wxSize& size, long style, const wxString& name):
46 wxSashWindow(parent, id, pos, size, style, name)
47 {
48 m_orientation = wxLAYOUT_HORIZONTAL;
49 m_alignment = wxLAYOUT_TOP;
50 }
51
52 // This is the function that wxLayoutAlgorithm calls to ascertain the window
53 // dimensions.
54 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
55 {
56 // int flags = event.GetFlags();
57 int requestedLength = event.GetRequestedLength();
58
59 event.SetOrientation(m_orientation);
60 event.SetAlignment(m_alignment);
61
62 if (m_orientation == wxLAYOUT_HORIZONTAL)
63 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
64 else
65 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
66 }
67
68 // Called by parent to allow window to take a bit out of the
69 // client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
70
71 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
72 {
73 wxRect clientSize(event.GetRect());
74
75 int flags = event.GetFlags();
76
77 if (!IsShown())
78 return;
79
80 // Let's assume that all windows stretch the full extent of the window in
81 // the direction of that window orientation. This will work for non-docking toolbars,
82 // and the status bar. Note that the windows have to have been created in a certain
83 // order to work, else you might get a left-aligned window going to the bottom
84 // of the window, and the status bar appearing to the right of it. The
85 // status bar would have to be created after or before the toolbar(s).
86
87 wxRect thisRect;
88
89 // Try to stretch
90 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
91 wxLayoutOrientation orient = GetOrientation();
92
93 // We assume that a window that says it's horizontal, wants to be stretched in that
94 // direction. Is this distinction too fine? Do we assume that any horizontal
95 // window needs to be stretched in that direction? Possibly.
96 int whichDimension = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? wxLAYOUT_LENGTH_X : wxLAYOUT_LENGTH_Y;
97
98 wxQueryLayoutInfoEvent infoEvent(GetId());
99 infoEvent.SetEventObject(this);
100 infoEvent.SetRequestedLength(length);
101 infoEvent.SetFlags(orient | whichDimension);
102
103 if (!GetEventHandler()->ProcessEvent(infoEvent))
104 return;
105
106 wxSize sz = infoEvent.GetSize();
107
108 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
109 return;
110
111 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
112 // how it's aligned.
113 switch (GetAlignment())
114 {
115 case wxLAYOUT_TOP:
116 {
117 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
118 thisRect.width = sz.x; thisRect.height = sz.y;
119 clientSize.y += thisRect.height;
120 clientSize.height -= thisRect.height;
121 break;
122 }
123 case wxLAYOUT_LEFT:
124 {
125 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
126 thisRect.width = sz.x; thisRect.height = sz.y;
127 clientSize.x += thisRect.width;
128 clientSize.width -= thisRect.width;
129 break;
130 }
131 case wxLAYOUT_RIGHT:
132 {
133 thisRect.x = clientSize.x + (clientSize.width - sz.x); thisRect.y = clientSize.y;
134 thisRect.width = sz.x; thisRect.height = sz.y;
135 clientSize.width -= thisRect.width;
136 break;
137 }
138 case wxLAYOUT_BOTTOM:
139 {
140 thisRect.x = clientSize.x; thisRect.y = clientSize.y + (clientSize.height - sz.y);
141 thisRect.width = sz.x; thisRect.height = sz.y;
142 clientSize.height -= thisRect.height;
143 break;
144 }
145 case wxLAYOUT_NONE:
146 {
147 break;
148 }
149
150 }
151
152 if ((flags & wxLAYOUT_QUERY) == 0)
153 {
154 // If not in query mode, resize the window.
155 // TODO: add wxRect& form to wxWindow::SetSize
156 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
157 }
158
159 event.SetRect(clientSize);
160 }
161 #endif // wxUSE_SASH
162
163 /*
164 * wxLayoutAlgorithm
165 */
166
167 // Lays out windows for an MDI frame. The MDI client area gets what's left
168 // over.
169 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
170 {
171 int cw, ch;
172 frame->GetClientSize(& cw, & ch);
173
174 wxRect rect(0, 0, cw, ch);
175 if (r)
176 rect = * r;
177
178 wxCalculateLayoutEvent event;
179 event.SetRect(rect);
180
181 wxNode* node = frame->GetChildren().First();
182 while (node)
183 {
184 wxWindow* win = (wxWindow*) node->Data();
185
186 event.SetId(win->GetId());
187 event.SetEventObject(win);
188 event.SetFlags(0); // ??
189
190 win->GetEventHandler()->ProcessEvent(event);
191
192 node = node->Next();
193 }
194
195 wxWindow* clientWindow = frame->GetClientWindow();
196
197 rect = event.GetRect();
198
199 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
200
201 return TRUE;
202 }
203
204 // Layout algorithm for any window. mainWindow gets what's left over.
205 bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
206 {
207 // Test if the parent is a sash window, and if so,
208 // reduce the available space to allow space for any active edges.
209
210 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
211 #if wxUSE_SASH
212 if (parent->IsKindOf(CLASSINFO(wxSashWindow)))
213 {
214 wxSashWindow* sashWindow = (wxSashWindow*) parent;
215
216 leftMargin = sashWindow->GetExtraBorderSize();
217 rightMargin = sashWindow->GetExtraBorderSize();
218 topMargin = sashWindow->GetExtraBorderSize();
219 bottomMargin = sashWindow->GetExtraBorderSize();
220
221 if (sashWindow->GetSashVisible(wxSASH_LEFT))
222 leftMargin += sashWindow->GetDefaultBorderSize();
223 if (sashWindow->GetSashVisible(wxSASH_RIGHT))
224 rightMargin += sashWindow->GetDefaultBorderSize();
225 if (sashWindow->GetSashVisible(wxSASH_TOP))
226 topMargin += sashWindow->GetDefaultBorderSize();
227 if (sashWindow->GetSashVisible(wxSASH_BOTTOM))
228 bottomMargin += sashWindow->GetDefaultBorderSize();
229 }
230 #endif // wxUSE_SASH
231
232 int cw, ch;
233 parent->GetClientSize(& cw, & ch);
234
235 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
236
237 wxCalculateLayoutEvent event;
238 event.SetRect(rect);
239
240 wxNode* node = parent->GetChildren().First();
241 while (node)
242 {
243 wxWindow* win = (wxWindow*) node->Data();
244
245 event.SetId(win->GetId());
246 event.SetEventObject(win);
247 event.SetFlags(0); // ??
248
249 win->GetEventHandler()->ProcessEvent(event);
250
251 node = node->Next();
252 }
253
254 rect = event.GetRect();
255
256 if (mainWindow)
257 mainWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
258
259 return TRUE;
260 }
261