Added wxSashWindow, wxSashLayoutWindow, wxLayoutAlgorithm classes and sample
[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 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #include "wx/mdi.h"
25 #endif
26
27 #include "wx/laywin.h"
28
29 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
30 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
31
32 IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
33
34 BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
35 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
36 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
37 END_EVENT_TABLE()
38
39 wxSashLayoutWindow::wxSashLayoutWindow(wxWindow *parent, wxWindowID id, const wxPoint& pos,
40 const wxSize& size, long style, const wxString& name):
41 wxSashWindow(parent, id, pos, size, style, name)
42 {
43 m_orientation = wxLAYOUT_HORIZONTAL;
44 m_alignment = wxLAYOUT_TOP;
45 }
46
47 // These are the functions that wxWin will call to ascertain the window
48 // dimensions.
49 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
50 {
51 int flags = event.GetFlags();
52 int requestedLength = event.GetRequestedLength();
53
54 // This code won't be in the final thing, it's just so we don't have to give it
55 // real windows: mock up some dimensions.
56
57 event.SetOrientation(m_orientation);
58 event.SetAlignment(m_alignment);
59
60 if (m_orientation == wxLAYOUT_HORIZONTAL)
61 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
62 else
63 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
64 }
65
66 // Called by parent to allow window to take a bit out of the
67 // client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
68 // Will eventually be an event.
69
70 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
71 {
72 wxRect clientSize(event.GetRect());
73
74 int flags = event.GetFlags();
75
76 if (!IsShown())
77 return;
78
79 // Let's assume that all windows stretch the full extent of the window in
80 // the direction of that window orientation. This will work for non-docking toolbars,
81 // and the status bar. Note that the windows have to have been created in a certain
82 // order to work, else you might get a left-aligned window going to the bottom
83 // of the window, and the status bar appearing to the right of it. The
84 // status bar would have to be created after or before the toolbar(s).
85
86 wxRect thisRect;
87
88 // Try to stretch
89 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
90 wxLayoutOrientation orient = GetOrientation();
91
92 // We assume that a window that says it's horizontal, wants to be stretched in that
93 // direction. Is this distinction too fine? Do we assume that any horizontal
94 // window needs to be stretched in that direction? Possibly.
95 int whichDimension = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? wxLAYOUT_LENGTH_X : wxLAYOUT_LENGTH_Y;
96
97 wxQueryLayoutInfoEvent infoEvent(GetId());
98 infoEvent.SetEventObject(this);
99 infoEvent.SetRequestedLength(length);
100 infoEvent.SetFlags(orient | whichDimension);
101
102 if (!GetEventHandler()->ProcessEvent(infoEvent))
103 return;
104
105 wxSize sz = infoEvent.GetSize();
106
107 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
108 return;
109
110 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
111 // how it's aligned.
112 switch (GetAlignment())
113 {
114 case wxLAYOUT_TOP:
115 {
116 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
117 thisRect.width = sz.x; thisRect.height = sz.y;
118 clientSize.y += thisRect.height;
119 clientSize.height -= thisRect.height;
120 break;
121 }
122 case wxLAYOUT_LEFT:
123 {
124 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
125 thisRect.width = sz.x; thisRect.height = sz.y;
126 clientSize.x += thisRect.width;
127 clientSize.width -= thisRect.width;
128 break;
129 }
130 case wxLAYOUT_RIGHT:
131 {
132 thisRect.x = clientSize.x + (clientSize.width - sz.x); thisRect.y = clientSize.y;
133 thisRect.width = sz.x; thisRect.height = sz.y;
134 clientSize.width -= thisRect.width;
135 break;
136 }
137 case wxLAYOUT_BOTTOM:
138 {
139 thisRect.x = clientSize.x; thisRect.y = clientSize.y + (clientSize.height - sz.y);
140 thisRect.width = sz.x; thisRect.height = sz.y;
141 clientSize.height -= thisRect.height;
142 break;
143 }
144 }
145
146 if ((flags & wxLAYOUT_QUERY) == 0)
147 {
148 // If not in query mode, resize the window.
149 // TODO: add wxRect& form to wxWindow::SetSize
150 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
151 }
152
153 event.SetRect(clientSize);
154 }
155
156 /*
157 * wxLayoutAlgorithm
158 */
159
160 // Lays out windows for an MDI frame. The MDI client area gets what's left
161 // over.
162 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame)
163 {
164 int cw, ch;
165 frame->GetClientSize(& cw, & ch);
166
167 wxRect rect(0, 0, cw, ch);
168
169 wxCalculateLayoutEvent event;
170 event.SetRect(rect);
171
172 wxNode* node = frame->GetChildren()->First();
173 while (node)
174 {
175 wxWindow* win = (wxWindow*) node->Data();
176
177 event.SetId(win->GetId());
178 event.SetEventObject(win);
179 event.SetFlags(0); // ??
180
181 win->GetEventHandler()->ProcessEvent(event);
182
183 node = node->Next();
184 }
185
186 wxWindow* clientWindow = frame->GetClientWindow();
187
188 rect = event.GetRect();
189
190 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
191
192 return TRUE;
193 }
194
195 // Layout algorithm for normal frame. mainWindow gets what's left over.
196 bool wxLayoutAlgorithm::LayoutFrame(wxFrame* frame, wxWindow* mainWindow)
197 {
198 int cw, ch;
199 frame->GetClientSize(& cw, & ch);
200
201 wxRect rect(0, 0, cw, ch);
202
203 wxCalculateLayoutEvent event;
204 event.SetRect(rect);
205
206 wxNode* node = frame->GetChildren()->First();
207 while (node)
208 {
209 wxWindow* win = (wxWindow*) node->Data();
210
211 event.SetId(win->GetId());
212 event.SetEventObject(win);
213 event.SetFlags(0); // ??
214
215 win->GetEventHandler()->ProcessEvent(event);
216
217 node = node->Next();
218 }
219
220 rect = event.GetRect();
221
222 mainWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
223
224 return TRUE;
225 }
226