]> git.saurik.com Git - wxWidgets.git/blob - src/generic/laywin.cpp
restored "#ifdef __WXMSW__" around SetFont(wxNullFont) - this leads to SIGSEGV
[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 case wxLAYOUT_NONE:
149 {
150 break;
151 }
152
153 }
154
155 if ((flags & wxLAYOUT_QUERY) == 0)
156 {
157 // If not in query mode, resize the window.
158 // TODO: add wxRect& form to wxWindow::SetSize
159 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
160 }
161
162 event.SetRect(clientSize);
163 }
164
165 /*
166 * wxLayoutAlgorithm
167 */
168
169 // Lays out windows for an MDI frame. The MDI client area gets what's left
170 // over.
171 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame)
172 {
173 int cw, ch;
174 frame->GetClientSize(& cw, & ch);
175
176 wxRect rect(0, 0, cw, ch);
177
178 wxCalculateLayoutEvent event;
179 event.SetRect(rect);
180
181 wxNode* node = frame->GetChildren()->First();
182 while (node)
183 {
184 wxWindow* win = (wxWindow*) node->Data();
185
186 event.SetId(win->GetId());
187 event.SetEventObject(win);
188 event.SetFlags(0); // ??
189
190 win->GetEventHandler()->ProcessEvent(event);
191
192 node = node->Next();
193 }
194
195 wxWindow* clientWindow = frame->GetClientWindow();
196
197 rect = event.GetRect();
198
199 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
200
201 return TRUE;
202 }
203
204 // Layout algorithm for normal frame. mainWindow gets what's left over.
205 bool wxLayoutAlgorithm::LayoutFrame(wxFrame* frame, wxWindow* mainWindow)
206 {
207 int cw, ch;
208 frame->GetClientSize(& cw, & ch);
209
210 wxRect rect(0, 0, cw, ch);
211
212 wxCalculateLayoutEvent event;
213 event.SetRect(rect);
214
215 wxNode* node = frame->GetChildren()->First();
216 while (node)
217 {
218 wxWindow* win = (wxWindow*) node->Data();
219
220 event.SetId(win->GetId());
221 event.SetEventObject(win);
222 event.SetFlags(0); // ??
223
224 win->GetEventHandler()->ProcessEvent(event);
225
226 node = node->Next();
227 }
228
229 rect = event.GetRect();
230
231 mainWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
232
233 return TRUE;
234 }
235