Added native wxInfoBar implementation for wxGTK.
[wxWidgets.git] / src / gtk / infobar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/infobar.cpp
3 // Purpose: wxInfoBar implementation for GTK
4 // Author: Vadim Zeitlin
5 // Created: 2009-09-27
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 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/infobar.h"
27
28 #if wxUSE_INFOBAR && defined(wxHAS_NATIVE_INFOBAR)
29
30 #ifndef WX_PRECOMP
31 #endif // WX_PRECOMP
32
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/private/messagetype.h"
35
36 // ----------------------------------------------------------------------------
37 // local functions
38 // ----------------------------------------------------------------------------
39
40 namespace
41 {
42
43 inline bool UseNative()
44 {
45 // native GtkInfoBar widget is only available in GTK+ 2.18 and later
46 return gtk_check_version(2, 18, 0) == 0;
47 }
48
49 } // anonymous namespace
50
51 extern "C"
52 {
53
54 static void wxgtk_infobar_response(GtkInfoBar * WXUNUSED(infobar),
55 gint btnid,
56 wxInfoBar *win)
57 {
58 win->GTKResponse(btnid);
59 }
60
61 static void wxgtk_infobar_close(GtkInfoBar * WXUNUSED(infobar),
62 wxInfoBar *win)
63 {
64 win->GTKResponse(wxID_CANCEL);
65 }
66
67 } // extern "C" section with GTK+ callbacks
68
69 // ============================================================================
70 // wxInfoBar implementation
71 // ============================================================================
72
73 bool wxInfoBar::Create(wxWindow *parent, wxWindowID winid)
74 {
75 if ( !UseNative() )
76 return wxInfoBarGeneric::Create(parent, winid);
77
78 // this control is created initially hidden
79 Hide();
80 if ( !CreateBase(parent, winid) )
81 return false;
82
83 // create the info bar widget itself
84 m_widget = gtk_info_bar_new();
85 wxCHECK_MSG( m_widget, false, "failed to create GtkInfoBar" );
86 g_object_ref(m_widget);
87
88 // also create a label which will be used to show our message
89 m_label = gtk_label_new("");
90 gtk_widget_show(m_label);
91
92 GtkWidget * const
93 contentArea = gtk_info_bar_get_content_area(GTK_INFO_BAR(m_widget));
94 wxCHECK_MSG( contentArea, false, "failed to get GtkInfoBar content area" );
95 gtk_container_add(GTK_CONTAINER(contentArea), m_label);
96
97 // finish creation and connect to all the signals we're interested in
98 m_parent->DoAddChild(this);
99
100 PostCreation(wxDefaultSize);
101
102 GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response));
103 GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close));
104
105 return false;
106 }
107
108 void wxInfoBar::ShowMessage(const wxString& msg, int flags)
109 {
110 if ( !UseNative() )
111 {
112 wxInfoBarGeneric::ShowMessage(msg, flags);
113 return;
114 }
115
116 GtkMessageType type;
117 if ( wxGTKImpl::ConvertMessageTypeFromWX(flags, &type) )
118 gtk_info_bar_set_message_type(GTK_INFO_BAR(m_widget), type);
119 gtk_label_set_text(GTK_LABEL(m_label), wxGTK_CONV(msg));
120
121 if ( !IsShown() )
122 Show();
123
124 UpdateParent();
125 }
126
127 void wxInfoBar::GTKResponse(int WXUNUSED(btnid))
128 {
129 Hide();
130
131 UpdateParent();
132 }
133
134 void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label)
135 {
136 if ( !UseNative() )
137 {
138 wxInfoBarGeneric::AddButton(btnid, label);
139 return;
140 }
141
142 gtk_info_bar_add_button
143 (
144 GTK_INFO_BAR(m_widget),
145 label.empty() ? GTKConvertMnemonics(wxGetStockGtkID(btnid)) : label,
146 btnid
147 );
148 }
149
150 #endif // wxUSE_INFOBAR