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/artprov.h" 
  30     #include "wx/bmpbuttn.h" 
  31     #include "wx/settings.h" 
  32     #include "wx/statbmp.h" 
  33     #include "wx/stattext.h" 
  36 #include "wx/infobar.h" 
  38 #include "wx/scopeguard.h" 
  41 // ============================================================================ 
  43 // ============================================================================ 
  45 void wxInfoBar::Init() 
  51     m_showEffect 
= wxSHOW_EFFECT_SLIDE_TO_BOTTOM
; 
  52     m_hideEffect 
= wxSHOW_EFFECT_SLIDE_TO_TOP
; 
  54     // use default effect duration 
  58 bool wxInfoBar::Create(wxWindow 
*parent
, wxWindowID winid
) 
  60     // calling Hide() before Create() ensures that we're created initially 
  63     if ( !wxWindow::Create(parent
, winid
) ) 
  66     // use special, easy to notice, colours 
  67     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK
)); 
  68     SetOwnForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT
)); 
  70     // create the controls: icon, text and the button to dismiss the 
  73     // the icon is not shown unless it's assigned a valid bitmap 
  74     m_icon 
= new wxStaticBitmap(this, wxID_ANY
, wxNullBitmap
); 
  76     // by default, the text uses a larger, more noticeable, font 
  77     m_text 
= new wxStaticText(this, wxID_ANY
, ""); 
  78     m_text
->SetFont(m_text
->GetFont().Bold().Larger()); 
  80     m_button 
= new wxBitmapButton
 
  84                     wxArtProvider::GetBitmap(wxART_CROSS_MARK
), 
  89     m_button
->SetToolTip(_("Hide this notification message.")); 
  93         wxEVT_COMMAND_BUTTON_CLICKED
, 
  94         wxCommandEventHandler(wxInfoBar::OnButton
), 
  99     // Center the text inside the sizer. 
 100     wxSizer 
* const sizer 
= new wxBoxSizer(wxHORIZONTAL
); 
 101     sizer
->AddStretchSpacer(); 
 102     sizer
->Add(m_icon
, wxSizerFlags().Centre().DoubleBorder()); 
 103     sizer
->Add(m_text
, wxSizerFlags().Centre().DoubleBorder()); 
 104     sizer
->AddStretchSpacer(); 
 105     sizer
->Add(m_button
, wxSizerFlags().Centre().DoubleBorder()); 
 111 void wxInfoBar::UpdateParent() 
 113     wxWindow 
* const parent 
= wxGetTopLevelParent(GetParent()); 
 117 void wxInfoBar::ChangeParentBackground() 
 119     wxWindow 
* const parent 
= GetParent(); 
 120     m_origParentBgCol 
= parent
->GetBackgroundColour(); 
 122     wxSizer 
* const sizer 
= GetContainingSizer(); 
 126     wxWindow 
*sibling 
= NULL
; 
 127     for ( wxSizerItemList::compatibility_iterator
 
 128             node 
= sizer
->GetChildren().GetFirst(); 
 130             node 
= node
->GetNext() ) 
 132         if ( node
->GetData()->GetWindow() == this ) 
 134             // find the next window following us 
 135             for ( node 
= node
->GetNext(); 
 137                   node 
= node
->GetNext() ) 
 139                 wxSizerItem 
* const item 
= node
->GetData(); 
 140                 if ( item
->IsWindow() ) 
 142                     sibling 
= item
->GetWindow(); 
 152         parent
->SetOwnBackgroundColour(sibling
->GetBackgroundColour()); 
 155 void wxInfoBar::RestoreParentBackground() 
 157     GetParent()->SetOwnBackgroundColour(m_origParentBgCol
); 
 160 void wxInfoBar::DoHide() 
 162     ChangeParentBackground(); 
 163     wxON_BLOCK_EXIT_THIS0( wxInfoBar::RestoreParentBackground 
); 
 165     HideWithEffect(m_hideEffect
, m_effectDuration
); 
 169 void wxInfoBar::DoShow() 
 171     // re-layout the parent first so that the window expands into an already 
 172     // unoccupied by the other controls area: for this we need to change our 
 173     // internal visibility flag to force Layout() to take us into account (an 
 174     // alternative solution to this hack would be to temporarily set 
 175     // wxRESERVE_SPACE_EVEN_IF_HIDDEN flag but it's not really batter) 
 177     // just change the internal flag indicating that the window is visible, 
 178     // without really showing it 
 179     wxWindowBase::Show(); 
 181     // an extra hack: we want the temporarily uncovered area in which we're 
 182     // going to expand to look like part of this sibling for a better effect so 
 183     // temporarily change the background of our parent to the same colour 
 184     ChangeParentBackground(); 
 185     wxON_BLOCK_EXIT_THIS0( wxInfoBar::RestoreParentBackground 
); 
 187     // adjust the parent layout to account for us 
 190     // reset the flag back before really showing the window or it wouldn't be 
 191     // shown at all because it would believe itself already visible 
 192     wxWindowBase::Show(false); 
 195     // finally do really show the window. 
 196     ShowWithEffect(m_showEffect
, m_effectDuration
); 
 199 void wxInfoBar::ShowMessage(const wxString
& msg
, int flags
) 
 201     // first update the controls 
 202     const int icon 
= flags 
& wxICON_MASK
; 
 203     if ( !icon 
|| (icon 
== wxICON_NONE
) ) 
 207     else // do show an icon 
 209         m_icon
->SetBitmap(wxArtProvider::GetMessageBoxIcon(icon
)); 
 213     // notice the use of EscapeMnemonics() to ensure that "&" come through 
 215     m_text
->SetLabel(wxControl::EscapeMnemonics(msg
)); 
 218     // then show this entire window if not done yet 
 223     else // we're already shown 
 225         // just update the layout to correspond to the new message 
 230 void wxInfoBar::OnButton(wxCommandEvent
& WXUNUSED(event
)) 
 235 #endif // wxUSE_INFOBAR