wxAUI: Send cancel event on floating panes closing too (last log was misleading ...
[wxWidgets.git] / src / aui / floatpane.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/floatpane.cpp
3 // Purpose: wxaui: wx advanced user interface - docking window manager
4 // Author: Benjamin I. Williams
5 // Modified by:
6 // Created: 2005-05-17
7 // RCS-ID: $Id$
8 // Copyright: (C) Copyright 2005-2006, Kirix Corporation, All Rights Reserved
9 // Licence: wxWindows Library Licence, Version 3.1
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_AUI
27
28 #include "wx/aui/framemanager.h"
29 #include "wx/aui/floatpane.h"
30 #include "wx/aui/dockart.h"
31
32 #ifndef WX_PRECOMP
33 #endif
34
35 IMPLEMENT_CLASS( wxFloatingPane, wxFloatingPaneBaseClass )
36
37 wxFloatingPane::wxFloatingPane(wxWindow* parent,
38 wxFrameManager* owner_mgr,
39 const wxPaneInfo& pane,
40 wxWindowID id /*= wxID_ANY*/)
41 : wxFloatingPaneBaseClass(parent, id, wxEmptyString,
42 pane.floating_pos, pane.floating_size,
43 wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION |
44 wxCLOSE_BOX | wxFRAME_NO_TASKBAR |
45 wxFRAME_FLOAT_ON_PARENT | wxCLIP_CHILDREN |
46 (pane.IsFixed()?0:wxRESIZE_BORDER)
47 )
48 {
49 m_owner_mgr = owner_mgr;
50 m_moving = false;
51 m_last_rect = wxRect();
52 m_mgr.SetFrame(this);
53 SetExtraStyle(wxWS_EX_PROCESS_IDLE);
54 }
55
56 wxFloatingPane::~wxFloatingPane()
57 {
58 m_mgr.UnInit();
59 }
60
61 void wxFloatingPane::SetPaneWindow(const wxPaneInfo& pane)
62 {
63 m_pane_window = pane.window;
64 m_pane_window->Reparent(this);
65
66 wxPaneInfo contained_pane = pane;
67 contained_pane.Dock().Center().Show().
68 CaptionVisible(false).
69 PaneBorder(false).
70 Layer(0).Row(0).Position(0);
71
72 m_mgr.AddPane(m_pane_window, contained_pane);
73 m_mgr.Update();
74
75 if (pane.min_size.IsFullySpecified())
76 {
77 // because SetSizeHints() calls Fit() too (which sets the window
78 // size to its minimum allowed), we keep the size before calling
79 // SetSizeHints() and reset it afterwards...
80 wxSize tmp = GetSize();
81 GetSizer()->SetSizeHints(this);
82 SetSize(tmp);
83 }
84
85 SetTitle(pane.caption);
86
87 if (pane.floating_size != wxDefaultSize)
88 {
89 SetSize(pane.floating_size);
90 }
91 else
92 {
93 wxSize size = pane.best_size;
94 if (size == wxDefaultSize)
95 size = pane.min_size;
96 if (size == wxDefaultSize)
97 size = m_pane_window->GetSize();
98 if (pane.HasGripper())
99 {
100 if (pane.HasGripperTop())
101 size.y += m_owner_mgr->m_art->GetMetric(wxAUI_ART_GRIPPER_SIZE);
102 else
103 size.x += m_owner_mgr->m_art->GetMetric(wxAUI_ART_GRIPPER_SIZE);
104 }
105
106 SetClientSize(size);
107 }
108 }
109
110 void wxFloatingPane::OnSize(wxSizeEvent& event)
111 {
112 m_owner_mgr->OnFloatingPaneResized(m_pane_window, event.GetSize());
113 }
114
115 void wxFloatingPane::OnClose(wxCloseEvent& WXUNUSED(event))
116 {
117 static wxList s_closing;
118
119 if (!s_closing.Member(this))
120 {
121 s_closing.Append(this);
122
123 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
124 cancelEvent.SetEventObject( m_pane_window );
125 m_pane_window->GetEventHandler()->ProcessEvent(cancelEvent);
126 s_closing.DeleteObject(this);
127 // we should really return here without doing anything if the close was vetoed
128 }
129
130 // The problem here is that the above can cause the window itself to be destroyed
131 if (!IsBeingDeleted() && m_pane_window && !m_pane_window->IsBeingDeleted()
132 && (m_pane_window->GetParent()==this))
133 {
134 m_owner_mgr->OnFloatingPaneClosed(m_pane_window);
135 Destroy();
136 }
137 }
138
139 void wxFloatingPane::OnMoveEvent(wxMoveEvent& event)
140 {
141 wxRect win_rect = GetRect();
142
143 // skip the first move event
144 if (m_last_rect.IsEmpty())
145 {
146 m_last_rect = win_rect;
147 return;
148 }
149
150 // prevent frame redocking during resize
151 if (m_last_rect.GetSize() != win_rect.GetSize())
152 {
153 m_last_rect = win_rect;
154 return;
155 }
156
157 m_last_rect = win_rect;
158
159 if (!isMouseDown())
160 return;
161
162 if (!m_moving)
163 {
164 OnMoveStart();
165 m_moving = true;
166 }
167
168 OnMoving(event.GetRect());
169 }
170
171 void wxFloatingPane::OnIdle(wxIdleEvent& event)
172 {
173 if (m_moving)
174 {
175 if (!isMouseDown())
176 {
177 m_moving = false;
178 OnMoveFinished();
179 }
180 else
181 {
182 event.RequestMore();
183 }
184 }
185 }
186
187 void wxFloatingPane::OnMoveStart()
188 {
189 // notify the owner manager that the pane has started to move
190 m_owner_mgr->OnFloatingPaneMoveStart(m_pane_window);
191 }
192
193 void wxFloatingPane::OnMoving(const wxRect& WXUNUSED(window_rect))
194 {
195 // notify the owner manager that the pane is moving
196 m_owner_mgr->OnFloatingPaneMoving(m_pane_window);
197 }
198
199 void wxFloatingPane::OnMoveFinished()
200 {
201 // notify the owner manager that the pane has finished moving
202 m_owner_mgr->OnFloatingPaneMoved(m_pane_window);
203 }
204
205 void wxFloatingPane::OnActivate(wxActivateEvent& event)
206 {
207 if (event.GetActive())
208 {
209 m_owner_mgr->OnFloatingPaneActivated(m_pane_window);
210 }
211 }
212
213 // utility function which determines the state of the mouse button
214 // (independant of having a wxMouseEvent handy) - utimately a better
215 // mechanism for this should be found (possibly by adding the
216 // functionality to wxWidgets itself)
217 bool wxFloatingPane::isMouseDown()
218 {
219 return wxGetMouseState().LeftDown();
220 }
221
222
223 BEGIN_EVENT_TABLE(wxFloatingPane, wxFloatingPaneBaseClass)
224 EVT_SIZE(wxFloatingPane::OnSize)
225 EVT_MOVE(wxFloatingPane::OnMoveEvent)
226 EVT_MOVING(wxFloatingPane::OnMoveEvent)
227 EVT_CLOSE(wxFloatingPane::OnClose)
228 EVT_IDLE(wxFloatingPane::OnIdle)
229 EVT_ACTIVATE(wxFloatingPane::OnActivate)
230 END_EVENT_TABLE()
231
232
233 #endif // wxUSE_AUI