]> git.saurik.com Git - wxWidgets.git/blame - src/generic/laywin.cpp
Added chapter on collection and container classes to contents
[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
88ac883a 12// Licence: wxWindows licence
a6d70308
JS
13/////////////////////////////////////////////////////////////////////////////
14
27529614
JS
15#ifdef __GNUG__
16#pragma implementation "laywin.h"
17#endif
18
a6d70308
JS
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
33IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
34IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
35
88ac883a 36#if wxUSE_SASH
a6d70308
JS
37IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
38
39BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
40 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
41 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
42END_EVENT_TABLE()
43
f6bcfd97
BP
44bool 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
50void wxSashLayoutWindow::Init()
a6d70308
JS
51{
52 m_orientation = wxLAYOUT_HORIZONTAL;
53 m_alignment = wxLAYOUT_TOP;
54}
55
2243eed5 56// This is the function that wxLayoutAlgorithm calls to ascertain the window
a6d70308
JS
57// dimensions.
58void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
59{
341287bf 60 // int flags = event.GetFlags();
a6d70308
JS
61 int requestedLength = event.GetRequestedLength();
62
a6d70308
JS
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.
a6d70308
JS
74
75void 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 }
341287bf
JS
149 case wxLAYOUT_NONE:
150 {
88ac883a 151 break;
341287bf
JS
152 }
153
a6d70308
JS
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
f6bcfd97
BP
160 wxSize sz = GetSize();
161 wxPoint pos = GetPosition();
a6d70308 162 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
f6bcfd97
BP
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
a6d70308
JS
169 }
170
171 event.SetRect(clientSize);
172}
88ac883a 173#endif // wxUSE_SASH
a6d70308
JS
174
175/*
176 * wxLayoutAlgorithm
177 */
178
179// Lays out windows for an MDI frame. The MDI client area gets what's left
180// over.
2243eed5 181bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
a6d70308
JS
182{
183 int cw, ch;
184 frame->GetClientSize(& cw, & ch);
185
186 wxRect rect(0, 0, cw, ch);
2243eed5
JS
187 if (r)
188 rect = * r;
a6d70308
JS
189
190 wxCalculateLayoutEvent event;
191 event.SetRect(rect);
192
c0ed460c 193 wxNode* node = frame->GetChildren().First();
a6d70308
JS
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
f9b1708c
JS
216// Layout algorithm for any window. mainWindow gets what's left over.
217bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
a6d70308 218{
f9b1708c
JS
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;
88ac883a 223#if wxUSE_SASH
f9b1708c
JS
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 }
88ac883a 242#endif // wxUSE_SASH
f9b1708c 243
a6d70308 244 int cw, ch;
f9b1708c 245 parent->GetClientSize(& cw, & ch);
a6d70308 246
f9b1708c 247 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
a6d70308
JS
248
249 wxCalculateLayoutEvent event;
250 event.SetRect(rect);
251
f9b1708c 252 wxNode* node = parent->GetChildren().First();
a6d70308
JS
253 while (node)
254 {
255 wxWindow* win = (wxWindow*) node->Data();
256
f6bcfd97
BP
257 if (win != mainWindow)
258 {
259 event.SetId(win->GetId());
260 event.SetEventObject(win);
261 event.SetFlags(0); // ??
a6d70308 262
f6bcfd97
BP
263 win->GetEventHandler()->ProcessEvent(event);
264 }
a6d70308
JS
265
266 node = node->Next();
267 }
268
269 rect = event.GetRect();
270
44c4a334
JS
271 if (mainWindow)
272 mainWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
a6d70308
JS
273
274 return TRUE;
275}
276