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