]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/infobar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/infobar.cpp
3 // Purpose: wxInfoBar implementation for GTK
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #include "wx/infobar.h"
27 #if wxUSE_INFOBAR && defined(wxHAS_NATIVE_INFOBAR)
32 #include "wx/vector.h"
33 #include "wx/stockitem.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()
87 // native GtkInfoBar widget is only available in GTK+ 2.18 and later
88 return gtk_check_version(2, 18, 0) == 0;
92 } // anonymous namespace
97 static void wxgtk_infobar_response(GtkInfoBar
* WXUNUSED(infobar
),
101 win
->GTKResponse(btnid
);
104 static void wxgtk_infobar_close(GtkInfoBar
* WXUNUSED(infobar
),
107 win
->GTKResponse(wxID_CANCEL
);
110 } // extern "C" section with GTK+ callbacks
112 // ============================================================================
113 // wxInfoBar implementation
114 // ============================================================================
116 bool wxInfoBar::Create(wxWindow
*parent
, wxWindowID winid
)
119 return wxInfoBarGeneric::Create(parent
, winid
);
121 m_impl
= new wxInfoBarGTKImpl
;
123 // this control is created initially hidden
125 if ( !CreateBase(parent
, winid
) )
128 // create the info bar widget itself
129 m_widget
= gtk_info_bar_new();
130 wxCHECK_MSG( m_widget
, false, "failed to create GtkInfoBar" );
131 g_object_ref(m_widget
);
133 // also create a label which will be used to show our message
134 m_impl
->m_label
= gtk_label_new("");
135 gtk_widget_show(m_impl
->m_label
);
138 contentArea
= gtk_info_bar_get_content_area(GTK_INFO_BAR(m_widget
));
139 wxCHECK_MSG( contentArea
, false, "failed to get GtkInfoBar content area" );
140 gtk_container_add(GTK_CONTAINER(contentArea
), m_impl
->m_label
);
142 // finish creation and connect to all the signals we're interested in
143 m_parent
->DoAddChild(this);
145 PostCreation(wxDefaultSize
);
147 GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response
));
148 GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close
));
153 wxInfoBar::~wxInfoBar()
158 void wxInfoBar::ShowMessage(const wxString
& msg
, int flags
)
162 wxInfoBarGeneric::ShowMessage(msg
, flags
);
166 // if we don't have any buttons, create a standard close one to give the
167 // user at least some way to close the bar
168 if ( m_impl
->m_buttons
.empty() && !m_impl
->m_close
)
170 m_impl
->m_close
= GTKAddButton(wxID_CLOSE
);
174 if ( wxGTKImpl::ConvertMessageTypeFromWX(flags
, &type
) )
175 gtk_info_bar_set_message_type(GTK_INFO_BAR(m_widget
), type
);
176 gtk_label_set_text(GTK_LABEL(m_impl
->m_label
), wxGTK_CONV(msg
));
184 void wxInfoBar::Dismiss()
188 wxInfoBarGeneric::Dismiss();
197 void wxInfoBar::GTKResponse(int btnid
)
199 wxCommandEvent
event(wxEVT_BUTTON
, btnid
);
200 event
.SetEventObject(this);
202 if ( !HandleWindowEvent(event
) )
206 GtkWidget
*wxInfoBar::GTKAddButton(wxWindowID btnid
, const wxString
& label
)
208 // as GTK+ lays out the buttons vertically, adding another button changes
209 // our best size (at least in vertical direction)
210 InvalidateBestSize();
212 GtkWidget
*button
= gtk_info_bar_add_button
214 GTK_INFO_BAR(m_widget
),
216 ? GTKConvertMnemonics(wxGetStockGtkID(btnid
))
221 wxASSERT_MSG( button
, "unexpectedly failed to add button to info bar" );
226 void wxInfoBar::AddButton(wxWindowID btnid
, const wxString
& label
)
230 wxInfoBarGeneric::AddButton(btnid
, label
);
234 // if we had created the default close button before, remove it now that we
235 // have some user-defined button
236 if ( m_impl
->m_close
)
238 gtk_widget_destroy(m_impl
->m_close
);
239 m_impl
->m_close
= NULL
;
242 GtkWidget
* const button
= GTKAddButton(btnid
, label
);
244 m_impl
->m_buttons
.push_back(wxInfoBarGTKImpl::Button(button
, btnid
));
247 void wxInfoBar::RemoveButton(wxWindowID btnid
)
251 wxInfoBarGeneric::RemoveButton(btnid
);
255 // as in the generic version, look for the button starting from the end
256 wxInfoBarGTKImpl::Buttons
& buttons
= m_impl
->m_buttons
;
257 for ( wxInfoBarGTKImpl::Buttons::reverse_iterator i
= buttons
.rbegin();
263 gtk_widget_destroy(i
->button
);
264 buttons
.erase(i
.base());
266 // see comment in GTKAddButton()
267 InvalidateBestSize();
273 wxFAIL_MSG( wxString::Format("button with id %d not found", btnid
) );
276 void wxInfoBar::DoApplyWidgetStyle(GtkRcStyle
*style
)
278 wxInfoBarGeneric::DoApplyWidgetStyle(style
);
281 GTKApplyStyle(m_impl
->m_label
, style
);
284 #endif // wxUSE_INFOBAR