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