]> git.saurik.com Git - wxWidgets.git/blame - src/generic/laywin.cpp
Get DC size from GDK window if possible, client size can unfortunately be wrong
[wxWidgets.git] / src / generic / laywin.cpp
CommitLineData
a6d70308 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/generic/laywin.cpp
a6d70308
JS
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
65571936 12// Licence: wxWindows licence
a6d70308
JS
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
04dbb646 23 #include "wx/frame.h"
a6d70308
JS
24#endif
25
26#include "wx/laywin.h"
e041a73d
FM
27#include "wx/mdi.h"
28
a6d70308
JS
29
30IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent, wxEvent)
31IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent, wxEvent)
32
9b11752c
VZ
33wxDEFINE_EVENT( wxEVT_QUERY_LAYOUT_INFO, wxQueryLayoutInfoEvent );
34wxDEFINE_EVENT( wxEVT_CALCULATE_LAYOUT, wxCalculateLayoutEvent );
82a5f02c 35
e041a73d
FM
36
37// ----------------------------------------------------------------------------
38// wxSashLayoutWindow
39// ----------------------------------------------------------------------------
40
88ac883a 41#if wxUSE_SASH
a6d70308 42IMPLEMENT_CLASS(wxSashLayoutWindow, wxSashWindow)
a6d70308
JS
43BEGIN_EVENT_TABLE(wxSashLayoutWindow, wxSashWindow)
44 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout)
45 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo)
46END_EVENT_TABLE()
47
f6bcfd97
BP
48bool wxSashLayoutWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
49 const wxSize& size, long style, const wxString& name)
50{
51 return wxSashWindow::Create(parent, id, pos, size, style, name);
52}
53
54void wxSashLayoutWindow::Init()
a6d70308
JS
55{
56 m_orientation = wxLAYOUT_HORIZONTAL;
57 m_alignment = wxLAYOUT_TOP;
8adc196b
SC
58#ifdef __WXMAC__
59 MacSetClipChildren( true ) ;
60#endif
a6d70308
JS
61}
62
2243eed5 63// This is the function that wxLayoutAlgorithm calls to ascertain the window
a6d70308
JS
64// dimensions.
65void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent& event)
66{
341287bf 67 // int flags = event.GetFlags();
a6d70308
JS
68 int requestedLength = event.GetRequestedLength();
69
a6d70308
JS
70 event.SetOrientation(m_orientation);
71 event.SetAlignment(m_alignment);
72
73 if (m_orientation == wxLAYOUT_HORIZONTAL)
74 event.SetSize(wxSize(requestedLength, m_defaultSize.y));
75 else
76 event.SetSize(wxSize(m_defaultSize.x, requestedLength));
77}
78
79// Called by parent to allow window to take a bit out of the
80// client rectangle, and size itself if not in wxLAYOUT_QUERY mode.
a6d70308
JS
81
82void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent& event)
83{
84 wxRect clientSize(event.GetRect());
85
86 int flags = event.GetFlags();
87
88 if (!IsShown())
89 return;
90
91 // Let's assume that all windows stretch the full extent of the window in
92 // the direction of that window orientation. This will work for non-docking toolbars,
93 // and the status bar. Note that the windows have to have been created in a certain
94 // order to work, else you might get a left-aligned window going to the bottom
95 // of the window, and the status bar appearing to the right of it. The
96 // status bar would have to be created after or before the toolbar(s).
97
98 wxRect thisRect;
99
100 // Try to stretch
101 int length = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? clientSize.width : clientSize.height;
102 wxLayoutOrientation orient = GetOrientation();
103
104 // We assume that a window that says it's horizontal, wants to be stretched in that
105 // direction. Is this distinction too fine? Do we assume that any horizontal
106 // window needs to be stretched in that direction? Possibly.
107 int whichDimension = (GetOrientation() == wxLAYOUT_HORIZONTAL) ? wxLAYOUT_LENGTH_X : wxLAYOUT_LENGTH_Y;
108
109 wxQueryLayoutInfoEvent infoEvent(GetId());
110 infoEvent.SetEventObject(this);
111 infoEvent.SetRequestedLength(length);
112 infoEvent.SetFlags(orient | whichDimension);
113
114 if (!GetEventHandler()->ProcessEvent(infoEvent))
115 return;
116
117 wxSize sz = infoEvent.GetSize();
118
119 if (sz.x == 0 && sz.y == 0) // Assume it's invisible
120 return;
121
122 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
123 // how it's aligned.
124 switch (GetAlignment())
125 {
126 case wxLAYOUT_TOP:
127 {
128 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
129 thisRect.width = sz.x; thisRect.height = sz.y;
130 clientSize.y += thisRect.height;
131 clientSize.height -= thisRect.height;
132 break;
133 }
134 case wxLAYOUT_LEFT:
135 {
136 thisRect.x = clientSize.x; thisRect.y = clientSize.y;
137 thisRect.width = sz.x; thisRect.height = sz.y;
138 clientSize.x += thisRect.width;
139 clientSize.width -= thisRect.width;
140 break;
141 }
142 case wxLAYOUT_RIGHT:
143 {
144 thisRect.x = clientSize.x + (clientSize.width - sz.x); thisRect.y = clientSize.y;
145 thisRect.width = sz.x; thisRect.height = sz.y;
146 clientSize.width -= thisRect.width;
147 break;
148 }
149 case wxLAYOUT_BOTTOM:
150 {
151 thisRect.x = clientSize.x; thisRect.y = clientSize.y + (clientSize.height - sz.y);
152 thisRect.width = sz.x; thisRect.height = sz.y;
153 clientSize.height -= thisRect.height;
154 break;
155 }
341287bf
JS
156 case wxLAYOUT_NONE:
157 {
88ac883a 158 break;
341287bf
JS
159 }
160
a6d70308
JS
161 }
162
163 if ((flags & wxLAYOUT_QUERY) == 0)
164 {
165 // If not in query mode, resize the window.
166 // TODO: add wxRect& form to wxWindow::SetSize
17a1ebd1 167 wxSize sz2 = GetSize();
f6bcfd97 168 wxPoint pos = GetPosition();
a6d70308 169 SetSize(thisRect.x, thisRect.y, thisRect.width, thisRect.height);
f6bcfd97
BP
170
171 // Make sure the sash is erased when the window is resized
17a1ebd1 172 if ((pos.x != thisRect.x || pos.y != thisRect.y || sz2.x != thisRect.width || sz2.y != thisRect.height) &&
f6bcfd97 173 (GetSashVisible(wxSASH_TOP) || GetSashVisible(wxSASH_RIGHT) || GetSashVisible(wxSASH_BOTTOM) || GetSashVisible(wxSASH_LEFT)))
ca65c044 174 Refresh(true);
f6bcfd97 175
a6d70308
JS
176 }
177
178 event.SetRect(clientSize);
179}
88ac883a 180#endif // wxUSE_SASH
a6d70308 181
e041a73d
FM
182
183// ----------------------------------------------------------------------------
184// wxLayoutAlgorithm
185// ----------------------------------------------------------------------------
a6d70308 186
1e6feb95
VZ
187#if wxUSE_MDI_ARCHITECTURE
188
a6d70308
JS
189// Lays out windows for an MDI frame. The MDI client area gets what's left
190// over.
2243eed5 191bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame* frame, wxRect* r)
a6d70308
JS
192{
193 int cw, ch;
194 frame->GetClientSize(& cw, & ch);
195
196 wxRect rect(0, 0, cw, ch);
2243eed5
JS
197 if (r)
198 rect = * r;
a6d70308
JS
199
200 wxCalculateLayoutEvent event;
201 event.SetRect(rect);
202
222ed1d6 203 wxWindowList::compatibility_iterator node = frame->GetChildren().GetFirst();
a6d70308
JS
204 while (node)
205 {
b1d4dd7a 206 wxWindow* win = node->GetData();
a6d70308
JS
207
208 event.SetId(win->GetId());
209 event.SetEventObject(win);
210 event.SetFlags(0); // ??
211
212 win->GetEventHandler()->ProcessEvent(event);
213
b1d4dd7a 214 node = node->GetNext();
a6d70308
JS
215 }
216
217 wxWindow* clientWindow = frame->GetClientWindow();
218
219 rect = event.GetRect();
220
221 clientWindow->SetSize(rect.x, rect.y, rect.width, rect.height);
222
ca65c044 223 return true;
a6d70308
JS
224}
225
1e6feb95
VZ
226#endif // wxUSE_MDI_ARCHITECTURE
227
04dbb646
VZ
228bool wxLayoutAlgorithm::LayoutFrame(wxFrame* frame, wxWindow* mainWindow)
229{
230 return LayoutWindow(frame, mainWindow);
231}
232
f9b1708c
JS
233// Layout algorithm for any window. mainWindow gets what's left over.
234bool wxLayoutAlgorithm::LayoutWindow(wxWindow* parent, wxWindow* mainWindow)
a6d70308 235{
f9b1708c
JS
236 // Test if the parent is a sash window, and if so,
237 // reduce the available space to allow space for any active edges.
238
239 int leftMargin = 0, rightMargin = 0, topMargin = 0, bottomMargin = 0;
88ac883a 240#if wxUSE_SASH
345c78ca 241 if (wxDynamicCast(parent, wxSashWindow))
f9b1708c
JS
242 {
243 wxSashWindow* sashWindow = (wxSashWindow*) parent;
244
245 leftMargin = sashWindow->GetExtraBorderSize();
246 rightMargin = sashWindow->GetExtraBorderSize();
247 topMargin = sashWindow->GetExtraBorderSize();
248 bottomMargin = sashWindow->GetExtraBorderSize();
249
250 if (sashWindow->GetSashVisible(wxSASH_LEFT))
251 leftMargin += sashWindow->GetDefaultBorderSize();
252 if (sashWindow->GetSashVisible(wxSASH_RIGHT))
253 rightMargin += sashWindow->GetDefaultBorderSize();
254 if (sashWindow->GetSashVisible(wxSASH_TOP))
255 topMargin += sashWindow->GetDefaultBorderSize();
256 if (sashWindow->GetSashVisible(wxSASH_BOTTOM))
257 bottomMargin += sashWindow->GetDefaultBorderSize();
258 }
88ac883a 259#endif // wxUSE_SASH
f9b1708c 260
a6d70308 261 int cw, ch;
f9b1708c 262 parent->GetClientSize(& cw, & ch);
a6d70308 263
f9b1708c 264 wxRect rect(leftMargin, topMargin, cw - leftMargin - rightMargin, ch - topMargin - bottomMargin);
a6d70308
JS
265
266 wxCalculateLayoutEvent event;
267 event.SetRect(rect);
268
2f82899b
JS
269 // Find the last layout-aware window, so we can make it fill all remaining
270 // space.
ca65c044 271 wxWindow *lastAwareWindow = NULL;
222ed1d6 272 wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
b1d4dd7a 273
a6d70308
JS
274 while (node)
275 {
b1d4dd7a 276 wxWindow* win = node->GetData();
a6d70308 277
2f82899b
JS
278 if (win->IsShown())
279 {
280 wxCalculateLayoutEvent tempEvent(win->GetId());
281 tempEvent.SetEventObject(win);
282 tempEvent.SetFlags(wxLAYOUT_QUERY);
283 tempEvent.SetRect(event.GetRect());
284 if (win->GetEventHandler()->ProcessEvent(tempEvent))
285 lastAwareWindow = win;
286 }
287
b1d4dd7a 288 node = node->GetNext();
2f82899b
JS
289 }
290
8c023dee 291 // Now do a dummy run to see if we have any space left for the final window (fail if not)
b1d4dd7a 292 node = parent->GetChildren().GetFirst();
2f82899b
JS
293 while (node)
294 {
b1d4dd7a 295 wxWindow* win = node->GetData();
2f82899b
JS
296
297 // If mainWindow is NULL and we're at the last window,
298 // skip this, because we'll simply make it fit the remaining space.
8c023dee
JS
299 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
300 {
301 event.SetId(win->GetId());
302 event.SetEventObject(win);
303 event.SetFlags(wxLAYOUT_QUERY);
304
305 win->GetEventHandler()->ProcessEvent(event);
306 }
307
b1d4dd7a 308 node = node->GetNext();
8c023dee
JS
309 }
310
311 if (event.GetRect().GetWidth() < 0 || event.GetRect().GetHeight() < 0)
ca65c044 312 return false;
8c023dee
JS
313
314 event.SetRect(rect);
315
b1d4dd7a 316 node = parent->GetChildren().GetFirst();
8c023dee
JS
317 while (node)
318 {
b1d4dd7a 319 wxWindow* win = node->GetData();
8c023dee
JS
320
321 // If mainWindow is NULL and we're at the last window,
322 // skip this, because we'll simply make it fit the remaining space.
323 if (win->IsShown() && (win != mainWindow) && (mainWindow != NULL || win != lastAwareWindow))
f6bcfd97
BP
324 {
325 event.SetId(win->GetId());
326 event.SetEventObject(win);
327 event.SetFlags(0); // ??
a6d70308 328
f6bcfd97
BP
329 win->GetEventHandler()->ProcessEvent(event);
330 }
a6d70308 331
b1d4dd7a 332 node = node->GetNext();
a6d70308
JS
333 }
334
335 rect = event.GetRect();
336
44c4a334 337 if (mainWindow)
d11bb14f 338 mainWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
2f82899b
JS
339 else if (lastAwareWindow)
340 {
341 // Fit the remaining space
d11bb14f 342 lastAwareWindow->SetSize(rect.x, rect.y, wxMax(0, rect.width), wxMax(0, rect.height));
2f82899b 343 }
a6d70308 344
ca65c044 345 return true;
a6d70308
JS
346}
347