]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/infobar.cpp
Add some version checks to help compiling on OSX.
[wxWidgets.git] / src / gtk / infobar.cpp
index 237b99a9f0d13787e916b9522be0d5cfe449f024..f6ceee09dbb2fab582506309be14e7c90c700cc6 100644 (file)
@@ -3,7 +3,7 @@
 // Purpose:     wxInfoBar implementation for GTK
 // Author:      Vadim Zeitlin
 // Created:     2009-09-27
 // Purpose:     wxInfoBar implementation for GTK
 // Author:      Vadim Zeitlin
 // Created:     2009-09-27
-// RCS-ID:      $Id: wxhead.cpp,v 1.10 2009-06-29 10:23:04 zeitlin Exp $
+// RCS-ID:      $Id$
 // Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 // Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
@@ -31,6 +31,7 @@
 #endif // WX_PRECOMP
 
 #include "wx/vector.h"
 #endif // WX_PRECOMP
 
 #include "wx/vector.h"
+#include "wx/stockitem.h"
 
 #include "wx/gtk/private.h"
 #include "wx/gtk/private/messagetype.h"
 
 #include "wx/gtk/private.h"
 #include "wx/gtk/private/messagetype.h"
@@ -45,10 +46,17 @@ public:
     wxInfoBarGTKImpl()
     {
         m_label = NULL;
     wxInfoBarGTKImpl()
     {
         m_label = NULL;
+        m_close = NULL;
     }
 
     }
 
+    // label for the text shown in the bar
     GtkWidget *m_label;
 
     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_)
     struct Button
     {
         Button(GtkWidget *button_, int id_)
@@ -136,7 +144,7 @@ bool wxInfoBar::Create(wxWindow *parent, wxWindowID winid)
     GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response));
     GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close));
 
     GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response));
     GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close));
 
-    return false;
+    return true;
 }
 
 wxInfoBar::~wxInfoBar()
 }
 
 wxInfoBar::~wxInfoBar()
@@ -152,6 +160,13 @@ void wxInfoBar::ShowMessage(const wxString& msg, int flags)
         return;
     }
 
         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);
     GtkMessageType type;
     if ( wxGTKImpl::ConvertMessageTypeFromWX(flags, &type) )
         gtk_info_bar_set_message_type(GTK_INFO_BAR(m_widget), type);
@@ -163,17 +178,46 @@ void wxInfoBar::ShowMessage(const wxString& msg, int flags)
     UpdateParent();
 }
 
     UpdateParent();
 }
 
+void wxInfoBar::Dismiss()
+{
+    if ( !UseNative() )
+    {
+        wxInfoBarGeneric::Dismiss();
+        return;
+    }
+
+    Hide();
+
+    UpdateParent();
+}
+
 void wxInfoBar::GTKResponse(int btnid)
 {
     wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, btnid);
     event.SetEventObject(this);
 
     if ( !HandleWindowEvent(event) )
 void wxInfoBar::GTKResponse(int btnid)
 {
     wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, btnid);
     event.SetEventObject(this);
 
     if ( !HandleWindowEvent(event) )
-    {
-        Hide();
+        Dismiss();
+}
 
 
-        UpdateParent();
-    }
+GtkWidget *wxInfoBar::GTKAddButton(wxWindowID btnid, const wxString& label)
+{
+    // 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
+                        (
+                            GTK_INFO_BAR(m_widget),
+                            (label.empty()
+                                ? GTKConvertMnemonics(wxGetStockGtkID(btnid))
+                                : label).utf8_str(),
+                            btnid
+                        );
+
+    wxASSERT_MSG( button, "unexpectedly failed to add button to info bar" );
+
+    return button;
 }
 
 void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
 }
 
 void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
@@ -184,18 +228,17 @@ void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
         return;
     }
 
         return;
     }
 
-    GtkWidget *button = gtk_info_bar_add_button
-                        (
-                            GTK_INFO_BAR(m_widget),
-                            label.empty()
-                                ? GTKConvertMnemonics(wxGetStockGtkID(btnid))
-                                : label,
-                            btnid
-                        );
-    wxCHECK_RET( button, "unexpectedly failed to add button to info bar" );
+    // 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;
+    }
 
 
-    g_object_ref(button);
-    m_impl->m_buttons.push_back(wxInfoBarGTKImpl::Button(button, btnid));
+    GtkWidget * const button = GTKAddButton(btnid, label);
+    if ( button )
+        m_impl->m_buttons.push_back(wxInfoBarGTKImpl::Button(button, btnid));
 }
 
 void wxInfoBar::RemoveButton(wxWindowID btnid)
 }
 
 void wxInfoBar::RemoveButton(wxWindowID btnid)
@@ -212,14 +255,27 @@ void wxInfoBar::RemoveButton(wxWindowID btnid)
           i != buttons.rend();
           ++i )
     {
           i != buttons.rend();
           ++i )
     {
-        GtkWidget * const button = i->button;
-        buttons.erase(i.base());
-        gtk_widget_destroy(button);
-        g_object_unref(button);
-        return;
+        if (i->id == btnid)
+        {
+            gtk_widget_destroy(i->button);
+            buttons.erase(i.base());
+
+            // see comment in GTKAddButton()
+            InvalidateBestSize();
+
+            return;
+        }
     }
 
     wxFAIL_MSG( wxString::Format("button with id %d not found", btnid) );
 }
 
     }
 
     wxFAIL_MSG( wxString::Format("button with id %d not found", btnid) );
 }
 
+void wxInfoBar::DoApplyWidgetStyle(GtkRcStyle *style)
+{
+    wxInfoBarGeneric::DoApplyWidgetStyle(style);
+
+    if ( UseNative() )
+        gtk_widget_modify_style(m_impl->m_label, style);
+}
+
 #endif // wxUSE_INFOBAR
 #endif // wxUSE_INFOBAR