1 /////////////////////////////////////////////////////////////////////////////
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
11 // Copyright: (c) Julian Smart
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/laywin.h"
29 IMPLEMENT_DYNAMIC_CLASS(wxQueryLayoutInfoEvent
, wxEvent
)
30 IMPLEMENT_DYNAMIC_CLASS(wxCalculateLayoutEvent
, wxEvent
)
32 IMPLEMENT_CLASS(wxSashLayoutWindow
, wxSashWindow
)
34 BEGIN_EVENT_TABLE(wxSashLayoutWindow
, wxSashWindow
)
35 EVT_CALCULATE_LAYOUT(wxSashLayoutWindow::OnCalculateLayout
)
36 EVT_QUERY_LAYOUT_INFO(wxSashLayoutWindow::OnQueryLayoutInfo
)
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
)
43 m_orientation
= wxLAYOUT_HORIZONTAL
;
44 m_alignment
= wxLAYOUT_TOP
;
47 // These are the functions that wxWin will call to ascertain the window
49 void wxSashLayoutWindow::OnQueryLayoutInfo(wxQueryLayoutInfoEvent
& event
)
51 int flags
= event
.GetFlags();
52 int requestedLength
= event
.GetRequestedLength();
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.
57 event
.SetOrientation(m_orientation
);
58 event
.SetAlignment(m_alignment
);
60 if (m_orientation
== wxLAYOUT_HORIZONTAL
)
61 event
.SetSize(wxSize(requestedLength
, m_defaultSize
.y
));
63 event
.SetSize(wxSize(m_defaultSize
.x
, requestedLength
));
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.
70 void wxSashLayoutWindow::OnCalculateLayout(wxCalculateLayoutEvent
& event
)
72 wxRect
clientSize(event
.GetRect());
74 int flags
= event
.GetFlags();
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).
89 int length
= (GetOrientation() == wxLAYOUT_HORIZONTAL
) ? clientSize
.width
: clientSize
.height
;
90 wxLayoutOrientation orient
= GetOrientation();
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
;
97 wxQueryLayoutInfoEvent
infoEvent(GetId());
98 infoEvent
.SetEventObject(this);
99 infoEvent
.SetRequestedLength(length
);
100 infoEvent
.SetFlags(orient
| whichDimension
);
102 if (!GetEventHandler()->ProcessEvent(infoEvent
))
105 wxSize sz
= infoEvent
.GetSize();
107 if (sz
.x
== 0 && sz
.y
== 0) // Assume it's invisible
110 // Now we know the size it wants to be. We wish to decide where to place it, i.e.
112 switch (GetAlignment())
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
;
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
;
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
;
137 case wxLAYOUT_BOTTOM
:
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
;
146 if ((flags
& wxLAYOUT_QUERY
) == 0)
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
);
153 event
.SetRect(clientSize
);
160 // Lays out windows for an MDI frame. The MDI client area gets what's left
162 bool wxLayoutAlgorithm::LayoutMDIFrame(wxMDIParentFrame
* frame
)
165 frame
->GetClientSize(& cw
, & ch
);
167 wxRect
rect(0, 0, cw
, ch
);
169 wxCalculateLayoutEvent event
;
172 wxNode
* node
= frame
->GetChildren()->First();
175 wxWindow
* win
= (wxWindow
*) node
->Data();
177 event
.SetId(win
->GetId());
178 event
.SetEventObject(win
);
179 event
.SetFlags(0); // ??
181 win
->GetEventHandler()->ProcessEvent(event
);
186 wxWindow
* clientWindow
= frame
->GetClientWindow();
188 rect
= event
.GetRect();
190 clientWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
195 // Layout algorithm for normal frame. mainWindow gets what's left over.
196 bool wxLayoutAlgorithm::LayoutFrame(wxFrame
* frame
, wxWindow
* mainWindow
)
199 frame
->GetClientSize(& cw
, & ch
);
201 wxRect
rect(0, 0, cw
, ch
);
203 wxCalculateLayoutEvent event
;
206 wxNode
* node
= frame
->GetChildren()->First();
209 wxWindow
* win
= (wxWindow
*) node
->Data();
211 event
.SetId(win
->GetId());
212 event
.SetEventObject(win
);
213 event
.SetFlags(0); // ??
215 win
->GetEventHandler()->ProcessEvent(event
);
220 rect
= event
.GetRect();
222 mainWindow
->SetSize(rect
.x
, rect
.y
, rect
.width
, rect
.height
);