1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/infobar.cpp
3 // Purpose: wxInfoBar implementation for GTK
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"
26 #include "wx/infobar.h"
28 #if wxUSE_INFOBAR && defined(wxHAS_NATIVE_INFOBAR)
33 #include "wx/vector.h"
35 #include "wx/gtk/private.h"
36 #include "wx/gtk/private/messagetype.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class wxInfoBarGTKImpl
51 // label for the text shown in the bar
54 // the default close button, NULL if not needed (m_buttons is not empty) or
58 // information about the buttons added using AddButton()
61 Button(GtkWidget
*button_
, int id_
)
70 typedef wxVector
<Button
> Buttons
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
82 inline bool UseNative()
84 // native GtkInfoBar widget is only available in GTK+ 2.18 and later
85 return gtk_check_version(2, 18, 0) == 0;
88 } // anonymous namespace
93 static void wxgtk_infobar_response(GtkInfoBar
* WXUNUSED(infobar
),
97 win
->GTKResponse(btnid
);
100 static void wxgtk_infobar_close(GtkInfoBar
* WXUNUSED(infobar
),
103 win
->GTKResponse(wxID_CANCEL
);
106 } // extern "C" section with GTK+ callbacks
108 // ============================================================================
109 // wxInfoBar implementation
110 // ============================================================================
112 bool wxInfoBar::Create(wxWindow
*parent
, wxWindowID winid
)
115 return wxInfoBarGeneric::Create(parent
, winid
);
117 m_impl
= new wxInfoBarGTKImpl
;
119 // this control is created initially hidden
121 if ( !CreateBase(parent
, winid
) )
124 // create the info bar widget itself
125 m_widget
= gtk_info_bar_new();
126 wxCHECK_MSG( m_widget
, false, "failed to create GtkInfoBar" );
127 g_object_ref(m_widget
);
129 // also create a label which will be used to show our message
130 m_impl
->m_label
= gtk_label_new("");
131 gtk_widget_show(m_impl
->m_label
);
134 contentArea
= gtk_info_bar_get_content_area(GTK_INFO_BAR(m_widget
));
135 wxCHECK_MSG( contentArea
, false, "failed to get GtkInfoBar content area" );
136 gtk_container_add(GTK_CONTAINER(contentArea
), m_impl
->m_label
);
138 // finish creation and connect to all the signals we're interested in
139 m_parent
->DoAddChild(this);
141 PostCreation(wxDefaultSize
);
143 GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response
));
144 GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close
));
149 wxInfoBar::~wxInfoBar()
154 void wxInfoBar::ShowMessage(const wxString
& msg
, int flags
)
158 wxInfoBarGeneric::ShowMessage(msg
, flags
);
162 // if we don't have any buttons, create a standard close one to give the
163 // user at least some way to close the bar
164 if ( m_impl
->m_buttons
.empty() && !m_impl
->m_close
)
166 m_impl
->m_close
= GTKAddButton(wxID_CLOSE
);
170 if ( wxGTKImpl::ConvertMessageTypeFromWX(flags
, &type
) )
171 gtk_info_bar_set_message_type(GTK_INFO_BAR(m_widget
), type
);
172 gtk_label_set_text(GTK_LABEL(m_impl
->m_label
), wxGTK_CONV(msg
));
180 void wxInfoBar::Dismiss()
184 wxInfoBarGeneric::Dismiss();
193 void wxInfoBar::GTKResponse(int btnid
)
195 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, btnid
);
196 event
.SetEventObject(this);
198 if ( !HandleWindowEvent(event
) )
202 GtkWidget
*wxInfoBar::GTKAddButton(wxWindowID btnid
, const wxString
& label
)
204 // as GTK+ lays out the buttons vertically, adding another button changes
205 // our best size (at least in vertical direction)
206 InvalidateBestSize();
208 GtkWidget
*button
= gtk_info_bar_add_button
210 GTK_INFO_BAR(m_widget
),
212 ? GTKConvertMnemonics(wxGetStockGtkID(btnid
))
217 wxASSERT_MSG( button
, "unexpectedly failed to add button to info bar" );
222 void wxInfoBar::AddButton(wxWindowID btnid
, const wxString
& label
)
226 wxInfoBarGeneric::AddButton(btnid
, label
);
230 // if we had created the default close button before, remove it now that we
231 // have some user-defined button
232 if ( m_impl
->m_close
)
234 gtk_widget_destroy(m_impl
->m_close
);
235 m_impl
->m_close
= NULL
;
238 GtkWidget
* const button
= GTKAddButton(btnid
, label
);
240 m_impl
->m_buttons
.push_back(wxInfoBarGTKImpl::Button(button
, btnid
));
243 void wxInfoBar::RemoveButton(wxWindowID btnid
)
247 wxInfoBarGeneric::RemoveButton(btnid
);
251 // as in the generic version, look for the button starting from the end
252 wxInfoBarGTKImpl::Buttons
& buttons
= m_impl
->m_buttons
;
253 for ( wxInfoBarGTKImpl::Buttons::reverse_iterator i
= buttons
.rbegin();
257 gtk_widget_destroy(i
->button
);
258 buttons
.erase(i
.base());
260 // see comment in GTKAddButton()
261 InvalidateBestSize();
266 wxFAIL_MSG( wxString::Format("button with id %d not found", btnid
) );
269 void wxInfoBar::DoApplyWidgetStyle(GtkRcStyle
*style
)
271 wxInfoBarGeneric::DoApplyWidgetStyle(style
);
274 gtk_widget_modify_style(m_impl
->m_label
, style
);
277 #endif // wxUSE_INFOBAR