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