]> git.saurik.com Git - wxWidgets.git/blob - src/generic/laywin.cpp
Committing in .
[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/mdi.h"
28 #endif
29
30 #include "wx/laywin.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
33 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
34
35 DEFINE_EVENT_TYPE(wxEVT_QUERY_LAYOUT_INFO)
36 DEFINE_EVENT_TYPE(wxEVT_CALCULATE_LAYOUT)
37
38 #if wxUSE_SASH
39 IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
40
41 BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
42 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
43 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
44 END_EVENT_TABLE()
45
46 bool wxSashLayoutWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
47 const wxSize& size, long style, const wxString& name)
48 {
49 return wxSashWindow::Create(parent, id, pos, size, style, name);
50 }
51
52 void wxSashLayoutWindow::Init()
53 {
54 m_orientation = wxLAYOUT_HORIZONTAL;
55 m_alignment = wxLAYOUT_TOP;
56 }
57
58 // This is the function that wxLayoutAlgorithm calls to ascertain the window
59 // dimensions.
60 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
61 {
62 // int flags = event.GetFlags();
63 int requestedLength = event.GetRequestedLength();
64
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.
76
77 void 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 }
151 case wxLAYOUT_NONE:
152 {
153 break;
154 }
155
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
162 wxSize sz = GetSize();
163 wxPoint pos = GetPosition();
164 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
165
166 // Make sure the sash is erased when the window is resized
167 if ((pos.x != thisRect.x || pos.y != thisRect.y || sz.x != thisRect.width || sz.y != thisRect.height) &&
168 (GetSashVisible(wxSASH_TOP) || GetSashVisible(wxSASH_RIGHT) || GetSashVisible(wxSASH_BOTTOM) || GetSashVisible(wxSASH_LEFT)))
169 Refresh(TRUE);
170
171 }
172
173 event.SetRect(clientSize);
174 }
175 #endif // wxUSE_SASH
176
177 /*
178 * wxLayoutAlgorithm
179 */
180
181 #if wxUSE_MDI_ARCHITECTURE
182
183 // Lays out windows for an MDI frame. The MDI client area gets what's left
184 // over.
185 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
186 {
187 int cw, ch;
188 frame->GetClientSize(& cw, & ch);
189
190 wxRect rect(0, 0, cw, ch);
191 if (r)
192 rect = * r;
193
194 wxCalculateLayoutEvent event;
195 event.SetRect(rect);
196
197 wxNode* node = frame->GetChildren().First();
198 while (node)
199 {
200 wxWindow* win = (wxWindow*) node->Data();
201
202 event.SetId(win->GetId());
203 event.SetEventObject(win);
204 event.SetFlags(0); // ??
205
206 win->GetEventHandler()->ProcessEvent(event);
207
208 node = node->Next();
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
217 return TRUE;
218 }
219
220 #endif // wxUSE_MDI_ARCHITECTURE
221
222 // Layout algorithm for any window. mainWindow gets what's left over.
223 bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
224 {
225 // Test if the parent is a sash window, and if so,
226 // reduce the available space to allow space for any active edges.
227
228 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
229 #if wxUSE_SASH
230 if (parent->IsKindOf(CLASSINFO(wxSashWindow)))
231 {
232 wxSashWindow* sashWindow = (wxSashWindow*) parent;
233
234 leftMargin = sashWindow->GetExtraBorderSize();
235 rightMargin = sashWindow->GetExtraBorderSize();
236 topMargin = sashWindow->GetExtraBorderSize();
237 bottomMargin = sashWindow->GetExtraBorderSize();
238
239 if (sashWindow->GetSashVisible(wxSASH_LEFT))
240 leftMargin += sashWindow->GetDefaultBorderSize();
241 if (sashWindow->GetSashVisible(wxSASH_RIGHT))
242 rightMargin += sashWindow->GetDefaultBorderSize();
243 if (sashWindow->GetSashVisible(wxSASH_TOP))
244 topMargin += sashWindow->GetDefaultBorderSize();
245 if (sashWindow->GetSashVisible(wxSASH_BOTTOM))
246 bottomMargin += sashWindow->GetDefaultBorderSize();
247 }
248 #endif // wxUSE_SASH
249
250 int cw, ch;
251 parent->GetClientSize(& cw, & ch);
252
253 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
254
255 wxCalculateLayoutEvent event;
256 event.SetRect(rect);
257
258 // Find the last layout-aware window, so we can make it fill all remaining
259 // space.
260 wxWindow* lastAwareWindow = NULL;
261 wxNode* node = parent->GetChildren().First();
262 while (node)
263 {
264 wxWindow* win = (wxWindow*) node->Data();
265
266 if (win->IsShown())
267 {
268 wxCalculateLayoutEvent tempEvent(win->GetId());
269 tempEvent.SetEventObject(win);
270 tempEvent.SetFlags(wxLAYOUT_QUERY);
271 tempEvent.SetRect(event.GetRect());
272 if (win->GetEventHandler()->ProcessEvent(tempEvent))
273 lastAwareWindow = win;
274 }
275
276 node = node->Next();
277 }
278
279 // Now do a dummy run to see if we have any space left for the final window (fail if not)
280 node = parent->GetChildren().First();
281 while (node)
282 {
283 wxWindow* win = (wxWindow*) node->Data();
284
285 // If mainWindow is NULL and we're at the last window,
286 // skip this, because we'll simply make it fit the remaining space.
287 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
288 {
289 event.SetId(win->GetId());
290 event.SetEventObject(win);
291 event.SetFlags(wxLAYOUT_QUERY);
292
293 win->GetEventHandler()->ProcessEvent(event);
294 }
295
296 node = node->Next();
297 }
298
299 if (event.GetRect().GetWidth() < 0 || event.GetRect().GetHeight() < 0)
300 return FALSE;
301
302 event.SetRect(rect);
303
304 node = parent->GetChildren().First();
305 while (node)
306 {
307 wxWindow* win = (wxWindow*) node->Data();
308
309 // If mainWindow is NULL and we're at the last window,
310 // skip this, because we'll simply make it fit the remaining space.
311 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
312 {
313 event.SetId(win->GetId());
314 event.SetEventObject(win);
315 event.SetFlags(0); // ??
316
317 win->GetEventHandler()->ProcessEvent(event);
318 }
319
320 node = node->Next();
321 }
322
323 rect = event.GetRect();
324
325 if (mainWindow)
326 mainWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
327 else if (lastAwareWindow)
328 {
329 // Fit the remaining space
330 lastAwareWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
331 }
332
333 return TRUE;
334 }
335