]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/infobar.cpp | |
3 | // Purpose: wxInfoBar implementation for GTK | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2009-09-27 | |
6 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | // for compilers that support precompilation, includes "wx.h". | |
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #include "wx/infobar.h" | |
26 | ||
27 | #if wxUSE_INFOBAR && defined(wxHAS_NATIVE_INFOBAR) | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #endif // WX_PRECOMP | |
31 | ||
32 | #include "wx/vector.h" | |
33 | #include "wx/stockitem.h" | |
34 | ||
35 | #include "wx/gtk/private.h" | |
36 | #include "wx/gtk/private/messagetype.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // local classes | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | class wxInfoBarGTKImpl | |
43 | { | |
44 | public: | |
45 | wxInfoBarGTKImpl() | |
46 | { | |
47 | m_label = NULL; | |
48 | m_close = NULL; | |
49 | } | |
50 | ||
51 | // label for the text shown in the bar | |
52 | GtkWidget *m_label; | |
53 | ||
54 | // the default close button, NULL if not needed (m_buttons is not empty) or | |
55 | // not created yet | |
56 | GtkWidget *m_close; | |
57 | ||
58 | // information about the buttons added using AddButton() | |
59 | struct Button | |
60 | { | |
61 | Button(GtkWidget *button_, int id_) | |
62 | : button(button_), | |
63 | id(id_) | |
64 | { | |
65 | } | |
66 | ||
67 | GtkWidget *button; | |
68 | int id; | |
69 | }; | |
70 | typedef wxVector<Button> Buttons; | |
71 | ||
72 | Buttons m_buttons; | |
73 | }; | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // local functions | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | namespace | |
80 | { | |
81 | ||
82 | inline bool UseNative() | |
83 | { | |
84 | #ifdef __WXGTK3__ | |
85 | return true; | |
86 | #else | |
87 | // native GtkInfoBar widget is only available in GTK+ 2.18 and later | |
88 | return gtk_check_version(2, 18, 0) == 0; | |
89 | #endif | |
90 | } | |
91 | ||
92 | } // anonymous namespace | |
93 | ||
94 | extern "C" | |
95 | { | |
96 | ||
97 | static void wxgtk_infobar_response(GtkInfoBar * WXUNUSED(infobar), | |
98 | gint btnid, | |
99 | wxInfoBar *win) | |
100 | { | |
101 | win->GTKResponse(btnid); | |
102 | } | |
103 | ||
104 | static void wxgtk_infobar_close(GtkInfoBar * WXUNUSED(infobar), | |
105 | wxInfoBar *win) | |
106 | { | |
107 | win->GTKResponse(wxID_CANCEL); | |
108 | } | |
109 | ||
110 | } // extern "C" section with GTK+ callbacks | |
111 | ||
112 | // ============================================================================ | |
113 | // wxInfoBar implementation | |
114 | // ============================================================================ | |
115 | ||
116 | bool wxInfoBar::Create(wxWindow *parent, wxWindowID winid) | |
117 | { | |
118 | if ( !UseNative() ) | |
119 | return wxInfoBarGeneric::Create(parent, winid); | |
120 | ||
121 | m_impl = new wxInfoBarGTKImpl; | |
122 | ||
123 | // this control is created initially hidden | |
124 | Hide(); | |
125 | if ( !CreateBase(parent, winid) ) | |
126 | return false; | |
127 | ||
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); | |
132 | ||
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); | |
136 | ||
137 | GtkWidget * const | |
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); | |
141 | ||
142 | // finish creation and connect to all the signals we're interested in | |
143 | m_parent->DoAddChild(this); | |
144 | ||
145 | PostCreation(wxDefaultSize); | |
146 | ||
147 | GTKConnectWidget("response", G_CALLBACK(wxgtk_infobar_response)); | |
148 | GTKConnectWidget("close", G_CALLBACK(wxgtk_infobar_close)); | |
149 | ||
150 | return true; | |
151 | } | |
152 | ||
153 | wxInfoBar::~wxInfoBar() | |
154 | { | |
155 | delete m_impl; | |
156 | } | |
157 | ||
158 | void wxInfoBar::ShowMessage(const wxString& msg, int flags) | |
159 | { | |
160 | if ( !UseNative() ) | |
161 | { | |
162 | wxInfoBarGeneric::ShowMessage(msg, flags); | |
163 | return; | |
164 | } | |
165 | ||
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 ) | |
169 | { | |
170 | m_impl->m_close = GTKAddButton(wxID_CLOSE); | |
171 | } | |
172 | ||
173 | GtkMessageType type; | |
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)); | |
177 | ||
178 | if ( !IsShown() ) | |
179 | Show(); | |
180 | ||
181 | UpdateParent(); | |
182 | } | |
183 | ||
184 | void wxInfoBar::Dismiss() | |
185 | { | |
186 | if ( !UseNative() ) | |
187 | { | |
188 | wxInfoBarGeneric::Dismiss(); | |
189 | return; | |
190 | } | |
191 | ||
192 | Hide(); | |
193 | ||
194 | UpdateParent(); | |
195 | } | |
196 | ||
197 | void wxInfoBar::GTKResponse(int btnid) | |
198 | { | |
199 | wxCommandEvent event(wxEVT_BUTTON, btnid); | |
200 | event.SetEventObject(this); | |
201 | ||
202 | if ( !HandleWindowEvent(event) ) | |
203 | Dismiss(); | |
204 | } | |
205 | ||
206 | GtkWidget *wxInfoBar::GTKAddButton(wxWindowID btnid, const wxString& label) | |
207 | { | |
208 | // as GTK+ lays out the buttons vertically, adding another button changes | |
209 | // our best size (at least in vertical direction) | |
210 | InvalidateBestSize(); | |
211 | ||
212 | GtkWidget *button = gtk_info_bar_add_button | |
213 | ( | |
214 | GTK_INFO_BAR(m_widget), | |
215 | (label.empty() | |
216 | ? GTKConvertMnemonics(wxGetStockGtkID(btnid)) | |
217 | : label).utf8_str(), | |
218 | btnid | |
219 | ); | |
220 | ||
221 | wxASSERT_MSG( button, "unexpectedly failed to add button to info bar" ); | |
222 | ||
223 | return button; | |
224 | } | |
225 | ||
226 | void wxInfoBar::AddButton(wxWindowID btnid, const wxString& label) | |
227 | { | |
228 | if ( !UseNative() ) | |
229 | { | |
230 | wxInfoBarGeneric::AddButton(btnid, label); | |
231 | return; | |
232 | } | |
233 | ||
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 ) | |
237 | { | |
238 | gtk_widget_destroy(m_impl->m_close); | |
239 | m_impl->m_close = NULL; | |
240 | } | |
241 | ||
242 | GtkWidget * const button = GTKAddButton(btnid, label); | |
243 | if ( button ) | |
244 | m_impl->m_buttons.push_back(wxInfoBarGTKImpl::Button(button, btnid)); | |
245 | } | |
246 | ||
247 | void wxInfoBar::RemoveButton(wxWindowID btnid) | |
248 | { | |
249 | if ( !UseNative() ) | |
250 | { | |
251 | wxInfoBarGeneric::RemoveButton(btnid); | |
252 | return; | |
253 | } | |
254 | ||
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(); | |
258 | i != buttons.rend(); | |
259 | ++i ) | |
260 | { | |
261 | if (i->id == btnid) | |
262 | { | |
263 | gtk_widget_destroy(i->button); | |
264 | buttons.erase(i.base()); | |
265 | ||
266 | // see comment in GTKAddButton() | |
267 | InvalidateBestSize(); | |
268 | ||
269 | return; | |
270 | } | |
271 | } | |
272 | ||
273 | wxFAIL_MSG( wxString::Format("button with id %d not found", btnid) ); | |
274 | } | |
275 | ||
276 | void wxInfoBar::DoApplyWidgetStyle(GtkRcStyle *style) | |
277 | { | |
278 | wxInfoBarGeneric::DoApplyWidgetStyle(style); | |
279 | ||
280 | if ( UseNative() ) | |
281 | GTKApplyStyle(m_impl->m_label, style); | |
282 | } | |
283 | ||
284 | #endif // wxUSE_INFOBAR |