1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/infobar.cpp
3 // Purpose: generic wxInfoBar implementation
4 // Author: Vadim Zeitlin
6 // RCS-ID: $Id: wxhead.cpp,v 1.10 2009-06-29 10:23:04 zeitlin Exp $
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/bmpbuttn.h"
30 #include "wx/button.h"
31 #include "wx/settings.h"
32 #include "wx/statbmp.h"
33 #include "wx/stattext.h"
36 #include "wx/infobar.h"
38 #include "wx/artprov.h"
39 #include "wx/scopeguard.h"
42 BEGIN_EVENT_TABLE(wxInfoBarGeneric
, wxInfoBarBase
)
43 EVT_BUTTON(wxID_ANY
, wxInfoBarGeneric::OnButton
)
46 // ============================================================================
48 // ============================================================================
50 void wxInfoBarGeneric::Init()
56 m_showEffect
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
;
57 m_hideEffect
= wxSHOW_EFFECT_SLIDE_TO_TOP
;
59 // use default effect duration
63 bool wxInfoBarGeneric::Create(wxWindow
*parent
, wxWindowID winid
)
65 // calling Hide() before Create() ensures that we're created initially
68 if ( !wxWindow::Create(parent
, winid
) )
71 // use special, easy to notice, colours
72 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
73 SetOwnForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
));
75 // create the controls: icon, text and the button to dismiss the
78 // the icon is not shown unless it's assigned a valid bitmap
79 m_icon
= new wxStaticBitmap(this, wxID_ANY
, wxNullBitmap
);
81 m_text
= new wxStaticText(this, wxID_ANY
, "");
83 m_button
= new wxBitmapButton
87 wxArtProvider::GetBitmap(wxART_CLOSE
, wxART_MENU
),
92 m_button
->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
));
93 m_button
->SetToolTip(_("Hide this notification message."));
95 // center the text inside the sizer with an icon to the left of it and a
96 // button at the very right
98 // NB: AddButton() relies on the button being the last control in the sizer
99 // and being preceded by a spacer
100 wxSizer
* const sizer
= new wxBoxSizer(wxHORIZONTAL
);
101 sizer
->Add(m_icon
, wxSizerFlags().Centre().Border());
102 sizer
->Add(m_text
, wxSizerFlags().Centre());
103 sizer
->AddStretchSpacer();
104 sizer
->Add(m_button
, wxSizerFlags().Centre().Border());
110 bool wxInfoBarGeneric::SetFont(const wxFont
& font
)
112 if ( !wxInfoBarBase::SetFont(font
) )
115 // check that we're not called before Create()
117 m_text
->SetFont(font
);
122 void wxInfoBarGeneric::UpdateParent()
124 wxWindow
* const parent
= wxGetTopLevelParent(GetParent());
128 void wxInfoBarGeneric::ChangeParentBackground()
130 wxWindow
* const parent
= GetParent();
131 m_origParentBgCol
= parent
->GetBackgroundColour();
133 wxSizer
* const sizer
= GetContainingSizer();
137 wxWindow
*sibling
= NULL
;
138 for ( wxSizerItemList::compatibility_iterator
139 node
= sizer
->GetChildren().GetFirst();
141 node
= node
->GetNext() )
143 if ( node
->GetData()->GetWindow() == this )
145 // find the next window following us
146 for ( node
= node
->GetNext();
148 node
= node
->GetNext() )
150 wxSizerItem
* const item
= node
->GetData();
151 if ( item
->IsWindow() )
153 sibling
= item
->GetWindow();
163 parent
->SetOwnBackgroundColour(sibling
->GetBackgroundColour());
166 void wxInfoBarGeneric::RestoreParentBackground()
168 GetParent()->SetOwnBackgroundColour(m_origParentBgCol
);
171 void wxInfoBarGeneric::DoHide()
173 ChangeParentBackground();
174 wxON_BLOCK_EXIT_THIS0( wxInfoBarGeneric::RestoreParentBackground
);
176 HideWithEffect(m_hideEffect
, m_effectDuration
);
180 void wxInfoBarGeneric::DoShow()
182 // re-layout the parent first so that the window expands into an already
183 // unoccupied by the other controls area: for this we need to change our
184 // internal visibility flag to force Layout() to take us into account (an
185 // alternative solution to this hack would be to temporarily set
186 // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag but it's not really batter)
188 // just change the internal flag indicating that the window is visible,
189 // without really showing it
190 wxWindowBase::Show();
192 // an extra hack: we want the temporarily uncovered area in which we're
193 // going to expand to look like part of this sibling for a better effect so
194 // temporarily change the background of our parent to the same colour
195 ChangeParentBackground();
196 wxON_BLOCK_EXIT_THIS0( wxInfoBarGeneric::RestoreParentBackground
);
198 // adjust the parent layout to account for us
201 // reset the flag back before really showing the window or it wouldn't be
202 // shown at all because it would believe itself already visible
203 wxWindowBase::Show(false);
206 // finally do really show the window.
207 ShowWithEffect(m_showEffect
, m_effectDuration
);
210 void wxInfoBarGeneric::ShowMessage(const wxString
& msg
, int flags
)
212 // first update the controls
213 const int icon
= flags
& wxICON_MASK
;
214 if ( !icon
|| (icon
== wxICON_NONE
) )
218 else // do show an icon
220 m_icon
->SetBitmap(wxArtProvider::GetBitmap(
221 wxArtProvider::GetMessageBoxIconId(flags
),
226 // notice the use of EscapeMnemonics() to ensure that "&" come through
228 m_text
->SetLabel(wxControl::EscapeMnemonics(msg
));
231 // then show this entire window if not done yet
236 else // we're already shown
238 // just update the layout to correspond to the new message
243 void wxInfoBarGeneric::Dismiss()
248 void wxInfoBarGeneric::AddButton(wxWindowID btnid
, const wxString
& label
)
250 wxSizer
* const sizer
= GetSizer();
251 wxCHECK_RET( sizer
, "must be created first" );
253 // user-added buttons replace the standard close button so remove it if we
254 // hadn't done it yet
255 if ( sizer
->Detach(m_button
) )
260 sizer
->Add(new wxButton(this, btnid
, label
),
261 wxSizerFlags().Centre().DoubleBorder());
264 void wxInfoBarGeneric::RemoveButton(wxWindowID btnid
)
266 wxSizer
* const sizer
= GetSizer();
267 wxCHECK_RET( sizer
, "must be created first" );
269 // iterate over the sizer items in reverse order to find the last added
270 // button with this id (ids of all buttons should be unique anyhow but if
271 // they are repeated removing the last added one probably makes more sense)
272 const wxSizerItemList
& items
= sizer
->GetChildren();
273 for ( wxSizerItemList::compatibility_iterator node
= items
.GetLast();
274 node
!= items
.GetFirst();
275 node
= node
->GetPrevious() )
277 const wxSizerItem
* const item
= node
->GetData();
279 // if we reached the spacer separating the buttons from the text
280 // preceding them without finding our button, it must mean it's not
282 if ( item
->IsSpacer() )
284 wxFAIL_MSG( wxString::Format("button with id %d not found", btnid
) );
288 // check if we found our button
289 if ( item
->GetWindow()->GetId() == btnid
)
291 delete item
->GetWindow();
296 // check if there are any custom buttons left
297 if ( sizer
->GetChildren().GetLast()->GetData()->IsSpacer() )
299 // if the last item is the spacer, none are left so restore the
300 // standard close button
301 sizer
->Add(m_button
, wxSizerFlags().Centre().DoubleBorder());
306 void wxInfoBarGeneric::OnButton(wxCommandEvent
& WXUNUSED(event
))
311 #endif // wxUSE_INFOBAR