]> git.saurik.com Git - wxWidgets.git/commitdiff
Only show the default close button in wxInfoBar if there are no others.
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 5 Oct 2009 22:55:40 +0000 (22:55 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 5 Oct 2009 22:55:40 +0000 (22:55 +0000)
Assume that user-added buttons can be already used to close the message so
don't show the default close button if any were added.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62280 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/gtk/infobar.h
include/wx/infobar.h
interface/wx/infobar.h
src/generic/infobar.cpp
src/gtk/infobar.cpp

index 1789ea17694487b25d9063e75a03cafcfa93424e..ed4b0a08d7b0499f2b032da0d8b98c943e6a43d3 100644 (file)
@@ -57,6 +57,10 @@ protected:
 private:
     void Init() { m_impl = NULL; }
 
+    // add a button with the given id/label and return its widget
+    GtkWidget *GTKAddButton(wxWindowID btnid,
+                            const wxString& label = wxString());
+
 
     // only used when the native implementation is really being used
     class wxInfoBarGTKImpl *m_impl;
index ec02fb33e84231a4b47fac56581bd76bd2dec792..7d661283e0492bc034d74042e4b48b47720adb52 100644 (file)
@@ -36,7 +36,8 @@ public:
     virtual void ShowMessage(const wxString& msg,
                              int flags = wxICON_INFORMATION) = 0;
 
-    // add an extra button to the bar, near the message
+    // add an extra button to the bar, near the message (replacing the default
+    // close button which is only shown if no extra buttons are used)
     virtual void AddButton(wxWindowID btnid,
                            const wxString& label = wxString()) = 0;
 
index a59e1edea7d92072186b42adb7e10bddc9bfbcc4..91360cf2d1de163537cc3dd042ad5e82af8c1749 100644 (file)
@@ -114,7 +114,9 @@ public:
 
         The button added by this method will be shown to the right of the text
         (in LTR layout), with each successive button being added to the right
-        of the previous one.
+        of the previous one. If any buttons are added to the info bar using
+        this method, the default "Close" button is not shown as it is assumed
+        that the extra buttons already allow the user to close it.
 
         Clicking the button will generate a normal EVT_COMMAND_BUTTON_CLICKED
         event which can be handled as usual. The default handler in wxInfoBar
index 3b9c6b64cb4beeb8a5e874ac28a25db97a0535f7..5e5a3432768492f3b47630557fc29549a382b0d2 100644 (file)
@@ -245,9 +245,15 @@ void wxInfoBarGeneric::AddButton(wxWindowID btnid, const wxString& label)
     wxSizer * const sizer = GetSizer();
     wxCHECK_RET( sizer, "must be created first" );
 
-    sizer->Insert(sizer->GetItemCount() - 1,
-                  new wxButton(this, btnid, label),
-                  wxSizerFlags().Centre().DoubleBorder());
+    // user-added buttons replace the standard close button so remove it if we
+    // hadn't done it yet
+    if ( sizer->Detach(m_button) )
+    {
+        m_button->Hide();
+    }
+
+    sizer->Add(new wxButton(this, btnid, label),
+               wxSizerFlags().Centre().DoubleBorder());
 }
 
 void wxInfoBarGeneric::RemoveButton(wxWindowID btnid)
@@ -263,7 +269,6 @@ void wxInfoBarGeneric::RemoveButton(wxWindowID btnid)
           node != items.GetFirst();
           node = node->GetPrevious() )
     {
-        node = node->GetPrevious();
         const wxSizerItem * const item = node->GetData();
 
         // if we reached the spacer separating the buttons from the text
@@ -282,6 +287,15 @@ void wxInfoBarGeneric::RemoveButton(wxWindowID btnid)
             break;
         }
     }
+
+    // check if there are any custom buttons left
+    if ( sizer->GetChildren().GetLast()->GetData()->IsSpacer() )
+    {
+        // if the last item is the spacer, none are left so restore the
+        // standard close button
+        sizer->Add(m_button, wxSizerFlags().Centre().DoubleBorder());
+        m_button->Show();
+    }
 }
 
 void wxInfoBarGeneric::OnButton(wxCommandEvent& WXUNUSED(event))
index 237b99a9f0d13787e916b9522be0d5cfe449f024..275f0199a8e3e5e210b7b2c415e9cdf431fb1ca3 100644 (file)
@@ -45,10 +45,17 @@ public:
     wxInfoBarGTKImpl()
     {
         m_label = NULL;
+        m_close = NULL;
     }
 
+    // label for the text shown in the bar
     GtkWidget *m_label;
 
+    // the default close button, NULL if not needed (m_buttons is not empty) or
+    // not created yet
+    GtkWidget *m_close;
+
+    // information about the buttons added using AddButton()
     struct Button
     {
         Button(GtkWidget *button_, int id_)
@@ -152,6 +159,13 @@ void wxInfoBar::ShowMessage(const wxString& msg, int flags)
         return;
     }
 
+    // if we don't have any buttons, create a standard close one to give the
+    // user at least some way to close the bar
+    if ( m_impl->m_buttons.empty() && !m_impl->m_close )
+    {
+        m_impl->m_close = GTKAddButton(wxID_CLOSE);
+    }
+
     GtkMessageType type;
     if ( wxGTKImpl::ConvertMessageTypeFromWX(flags, &type) )
         gtk_info_bar_set_message_type(GTK_INFO_BAR(m_widget), type);
@@ -176,13 +190,11 @@ void wxInfoBar::GTKResponse(int btnid)
     }
 }
 
-void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
+GtkWidget *wxInfoBar::GTKAddButton(wxWindowID btnid, const wxString& label)
 {
-    if ( !UseNative() )
-    {
-        wxInfoBarGeneric::AddButton(btnid, label);
-        return;
-    }
+    // as GTK+ lays out the buttons vertically, adding another button changes
+    // our best size (at least in vertical direction)
+    InvalidateBestSize();
 
     GtkWidget *button = gtk_info_bar_add_button
                         (
@@ -192,10 +204,31 @@ void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
                                 : label,
                             btnid
                         );
-    wxCHECK_RET( button, "unexpectedly failed to add button to info bar" );
 
-    g_object_ref(button);
-    m_impl->m_buttons.push_back(wxInfoBarGTKImpl::Button(button, btnid));
+    wxASSERT_MSG( button, "unexpectedly failed to add button to info bar" );
+
+    return button;
+}
+
+void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
+{
+    if ( !UseNative() )
+    {
+        wxInfoBarGeneric::AddButton(btnid, label);
+        return;
+    }
+
+    // if we had created the default close button before, remove it now that we
+    // have some user-defined button
+    if ( m_impl->m_close )
+    {
+        gtk_widget_destroy(m_impl->m_close);
+        m_impl->m_close = NULL;
+    }
+
+    GtkWidget * const button = GTKAddButton(btnid, label);
+    if ( button )
+        m_impl->m_buttons.push_back(wxInfoBarGTKImpl::Button(button, btnid));
 }
 
 void wxInfoBar::RemoveButton(wxWindowID btnid)
@@ -212,10 +245,12 @@ void wxInfoBar::RemoveButton(wxWindowID btnid)
           i != buttons.rend();
           ++i )
     {
-        GtkWidget * const button = i->button;
+        gtk_widget_destroy(i->button);
         buttons.erase(i.base());
-        gtk_widget_destroy(button);
-        g_object_unref(button);
+
+        // see comment in GTKAddButton()
+        InvalidateBestSize();
+
         return;
     }