1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.InfoBar;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.Box;
32 private import gtk.Button;
33 private import gtk.HBox;
34 private import gtk.VButtonBox;
35 private import gtk.Widget;
36 private import gtk.c.functions;
37 public  import gtk.c.types;
38 public  import gtkc.gtktypes;
39 private import std.algorithm;
40 
41 
42 /**
43  * #GtkInfoBar is a widget that can be used to show messages to
44  * the user without showing a dialog. It is often temporarily shown
45  * at the top or bottom of a document. In contrast to #GtkDialog, which
46  * has a action area at the bottom, #GtkInfoBar has an action area
47  * at the side.
48  * 
49  * The API of #GtkInfoBar is very similar to #GtkDialog, allowing you
50  * to add buttons to the action area with gtk_info_bar_add_button() or
51  * gtk_info_bar_new_with_buttons(). The sensitivity of action widgets
52  * can be controlled with gtk_info_bar_set_response_sensitive().
53  * To add widgets to the main content area of a #GtkInfoBar, use
54  * gtk_info_bar_get_content_area() and add your widgets to the container.
55  * 
56  * Similar to #GtkMessageDialog, the contents of a #GtkInfoBar can by
57  * classified as error message, warning, informational message, etc,
58  * by using gtk_info_bar_set_message_type(). GTK+ may use the message type
59  * to determine how the message is displayed.
60  * 
61  * A simple example for using a GtkInfoBar:
62  * |[<!-- language="C" -->
63  * // set up info bar
64  * GtkWidget *widget;
65  * GtkInfoBar *bar;
66  * 
67  * widget = gtk_info_bar_new ();
68  * bar = GTK_INFO_BAR (widget);
69  * 
70  * gtk_widget_set_no_show_all (widget, TRUE);
71  * message_label = gtk_label_new ("");
72  * gtk_widget_show (message_label);
73  * content_area = gtk_info_bar_get_content_area (bar);
74  * gtk_container_add (GTK_CONTAINER (content_area),
75  * message_label);
76  * gtk_info_bar_add_button (bar,
77  * _("_OK"),
78  * GTK_RESPONSE_OK);
79  * g_signal_connect (bar,
80  * "response",
81  * G_CALLBACK (gtk_widget_hide),
82  * NULL);
83  * gtk_grid_attach (GTK_GRID (grid),
84  * widget,
85  * 0, 2, 1, 1);
86  * 
87  * ...
88  * 
89  * // show an error message
90  * gtk_label_set_text (GTK_LABEL (message_label), message);
91  * gtk_info_bar_set_message_type (bar,
92  * GTK_MESSAGE_ERROR);
93  * gtk_widget_show (bar);
94  * ]|
95  * 
96  * # GtkInfoBar as GtkBuildable
97  * 
98  * The GtkInfoBar implementation of the GtkBuildable interface exposes
99  * the content area and action area as internal children with the names
100  * “content_area” and “action_area”.
101  * 
102  * GtkInfoBar supports a custom <action-widgets> element, which can contain
103  * multiple <action-widget> elements. The “response” attribute specifies a
104  * numeric response, and the content of the element is the id of widget
105  * (which should be a child of the dialogs @action_area).
106  * 
107  * # CSS nodes
108  * 
109  * GtkInfoBar has a single CSS node with name infobar. The node may get
110  * one of the style classes .info, .warning, .error or .question, depending
111  * on the message type.
112  */
113 public class InfoBar : Box
114 {
115 	/** the main Gtk struct */
116 	protected GtkInfoBar* gtkInfoBar;
117 
118 	/** Get the main Gtk struct */
119 	public GtkInfoBar* getInfoBarStruct(bool transferOwnership = false)
120 	{
121 		if (transferOwnership)
122 			ownedRef = false;
123 		return gtkInfoBar;
124 	}
125 
126 	/** the main Gtk struct as a void* */
127 	protected override void* getStruct()
128 	{
129 		return cast(void*)gtkInfoBar;
130 	}
131 
132 	/**
133 	 * Sets our main struct and passes it to the parent class.
134 	 */
135 	public this (GtkInfoBar* gtkInfoBar, bool ownedRef = false)
136 	{
137 		this.gtkInfoBar = gtkInfoBar;
138 		super(cast(GtkBox*)gtkInfoBar, ownedRef);
139 	}
140 
141 	/** */
142 	public this(string[] buttonsText, ResponseType[] responses)
143 	{
144 		this();
145 
146 		for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++)
147 		{
148 			addButton(buttonsText[i], responses[i]);
149 		}
150 	}
151 
152 	/** */
153 	public this(StockID[] stockIDs, ResponseType[] responses)
154 	{
155 		this();
156 
157 		for ( int i=0 ; i<stockIDs.length && i<responses.length ; i++)
158 		{
159 			addButton(stockIDs[i], responses[i]);
160 		}
161 	}
162 
163 	/** */
164 	public Button addButton(StockID stockID, int responseId)
165 	{
166 		auto p = gtk_info_bar_add_button(gtkInfoBar, Str.toStringz(stockID), responseId);
167 
168 		if ( p is null )
169 		{
170 			return null;
171 		}
172 
173 		return new Button(cast(GtkButton*)p);
174 	}
175 
176 	/** */
177 	public void addButtons(string[] buttonsText, ResponseType[] responses)
178 	{
179 		for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++)
180 		{
181 			addButton(buttonsText[i], responses[i]);
182 		}
183 	}
184 
185 	/** */
186 	public void addButtons(StockID[] stockIDs, ResponseType[] responses)
187 	{
188 		for ( int i=0 ; i<stockIDs.length && i<responses.length ; i++)
189 		{
190 			addButton(stockIDs[i], responses[i]);
191 		}
192 	}
193 
194 	/**
195 	 * Returns the action area of info_bar.
196 	 * Since 2.18
197 	 * Returns: the action area.
198 	 */
199 	public VButtonBox getActionArea()
200 	{
201 		// GtkWidget * gtk_info_bar_get_action_area (GtkInfoBar *info_bar);
202 		auto p = gtk_info_bar_get_action_area(gtkInfoBar);
203 		if(p is null)
204 		{
205 			return null;
206 		}
207 		return ObjectG.getDObject!(VButtonBox)(cast(GtkVButtonBox*) p);
208 	}
209 
210 	/**
211 	 * Returns the content area of info_bar.
212 	 * Since 2.18
213 	 * Returns: the content area.
214 	 */
215 	public HBox getContentArea()
216 	{
217 		// GtkWidget * gtk_info_bar_get_content_area (GtkInfoBar *info_bar);
218 		auto p = gtk_info_bar_get_content_area(gtkInfoBar);
219 		if(p is null)
220 		{
221 			return null;
222 		}
223 		return ObjectG.getDObject!(HBox)(cast(GtkHBox*) p);
224 	}
225 
226 	/**
227 	 */
228 
229 	/** */
230 	public static GType getType()
231 	{
232 		return gtk_info_bar_get_type();
233 	}
234 
235 	/**
236 	 * Creates a new #GtkInfoBar object.
237 	 *
238 	 * Returns: a new #GtkInfoBar object
239 	 *
240 	 * Since: 2.18
241 	 *
242 	 * Throws: ConstructionException GTK+ fails to create the object.
243 	 */
244 	public this()
245 	{
246 		auto p = gtk_info_bar_new();
247 
248 		if(p is null)
249 		{
250 			throw new ConstructionException("null returned by new");
251 		}
252 
253 		this(cast(GtkInfoBar*) p);
254 	}
255 
256 	/**
257 	 * Add an activatable widget to the action area of a #GtkInfoBar,
258 	 * connecting a signal handler that will emit the #GtkInfoBar::response
259 	 * signal on the message area when the widget is activated. The widget
260 	 * is appended to the end of the message areas action area.
261 	 *
262 	 * Params:
263 	 *     child = an activatable widget
264 	 *     responseId = response ID for @child
265 	 *
266 	 * Since: 2.18
267 	 */
268 	public void addActionWidget(Widget child, int responseId)
269 	{
270 		gtk_info_bar_add_action_widget(gtkInfoBar, (child is null) ? null : child.getWidgetStruct(), responseId);
271 	}
272 
273 	/**
274 	 * Adds a button with the given text and sets things up so that
275 	 * clicking the button will emit the “response” signal with the given
276 	 * response_id. The button is appended to the end of the info bars's
277 	 * action area. The button widget is returned, but usually you don't
278 	 * need it.
279 	 *
280 	 * Params:
281 	 *     buttonText = text of button
282 	 *     responseId = response ID for the button
283 	 *
284 	 * Returns: the #GtkButton widget
285 	 *     that was added
286 	 *
287 	 * Since: 2.18
288 	 */
289 	public Button addButton(string buttonText, int responseId)
290 	{
291 		auto p = gtk_info_bar_add_button(gtkInfoBar, Str.toStringz(buttonText), responseId);
292 
293 		if(p is null)
294 		{
295 			return null;
296 		}
297 
298 		return ObjectG.getDObject!(Button)(cast(GtkButton*) p);
299 	}
300 
301 	/**
302 	 * Returns the message type of the message area.
303 	 *
304 	 * Returns: the message type of the message area.
305 	 *
306 	 * Since: 2.18
307 	 */
308 	public GtkMessageType getMessageType()
309 	{
310 		return gtk_info_bar_get_message_type(gtkInfoBar);
311 	}
312 
313 	/**
314 	 * Returns whether the widget will display a standard close button.
315 	 *
316 	 * Returns: %TRUE if the widget displays standard close button
317 	 *
318 	 * Since: 3.10
319 	 */
320 	public bool getShowCloseButton()
321 	{
322 		return gtk_info_bar_get_show_close_button(gtkInfoBar) != 0;
323 	}
324 
325 	/**
326 	 * Emits the “response” signal with the given @response_id.
327 	 *
328 	 * Params:
329 	 *     responseId = a response ID
330 	 *
331 	 * Since: 2.18
332 	 */
333 	public void response(int responseId)
334 	{
335 		gtk_info_bar_response(gtkInfoBar, responseId);
336 	}
337 
338 	/**
339 	 * Sets the last widget in the info bar’s action area with
340 	 * the given response_id as the default widget for the dialog.
341 	 * Pressing “Enter” normally activates the default widget.
342 	 *
343 	 * Note that this function currently requires @info_bar to
344 	 * be added to a widget hierarchy.
345 	 *
346 	 * Params:
347 	 *     responseId = a response ID
348 	 *
349 	 * Since: 2.18
350 	 */
351 	public void setDefaultResponse(int responseId)
352 	{
353 		gtk_info_bar_set_default_response(gtkInfoBar, responseId);
354 	}
355 
356 	/**
357 	 * Sets the message type of the message area.
358 	 *
359 	 * GTK+ uses this type to determine how the message is displayed.
360 	 *
361 	 * Params:
362 	 *     messageType = a #GtkMessageType
363 	 *
364 	 * Since: 2.18
365 	 */
366 	public void setMessageType(GtkMessageType messageType)
367 	{
368 		gtk_info_bar_set_message_type(gtkInfoBar, messageType);
369 	}
370 
371 	/**
372 	 * Calls gtk_widget_set_sensitive (widget, setting) for each
373 	 * widget in the info bars’s action area with the given response_id.
374 	 * A convenient way to sensitize/desensitize dialog buttons.
375 	 *
376 	 * Params:
377 	 *     responseId = a response ID
378 	 *     setting = TRUE for sensitive
379 	 *
380 	 * Since: 2.18
381 	 */
382 	public void setResponseSensitive(int responseId, bool setting)
383 	{
384 		gtk_info_bar_set_response_sensitive(gtkInfoBar, responseId, setting);
385 	}
386 
387 	/**
388 	 * If true, a standard close button is shown. When clicked it emits
389 	 * the response %GTK_RESPONSE_CLOSE.
390 	 *
391 	 * Params:
392 	 *     setting = %TRUE to include a close button
393 	 *
394 	 * Since: 3.10
395 	 */
396 	public void setShowCloseButton(bool setting)
397 	{
398 		gtk_info_bar_set_show_close_button(gtkInfoBar, setting);
399 	}
400 
401 	protected class OnCloseDelegateWrapper
402 	{
403 		void delegate(InfoBar) dlg;
404 		gulong handlerId;
405 
406 		this(void delegate(InfoBar) dlg)
407 		{
408 			this.dlg = dlg;
409 			onCloseListeners ~= this;
410 		}
411 
412 		void remove(OnCloseDelegateWrapper source)
413 		{
414 			foreach(index, wrapper; onCloseListeners)
415 			{
416 				if (wrapper.handlerId == source.handlerId)
417 				{
418 					onCloseListeners[index] = null;
419 					onCloseListeners = std.algorithm.remove(onCloseListeners, index);
420 					break;
421 				}
422 			}
423 		}
424 	}
425 	OnCloseDelegateWrapper[] onCloseListeners;
426 
427 	/**
428 	 * The ::close signal is a
429 	 * [keybinding signal][GtkBindingSignal]
430 	 * which gets emitted when the user uses a keybinding to dismiss
431 	 * the info bar.
432 	 *
433 	 * The default binding for this signal is the Escape key.
434 	 *
435 	 * Since: 2.18
436 	 */
437 	gulong addOnClose(void delegate(InfoBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
438 	{
439 		auto wrapper = new OnCloseDelegateWrapper(dlg);
440 		wrapper.handlerId = Signals.connectData(
441 			this,
442 			"close",
443 			cast(GCallback)&callBackClose,
444 			cast(void*)wrapper,
445 			cast(GClosureNotify)&callBackCloseDestroy,
446 			connectFlags);
447 		return wrapper.handlerId;
448 	}
449 
450 	extern(C) static void callBackClose(GtkInfoBar* infobarStruct, OnCloseDelegateWrapper wrapper)
451 	{
452 		wrapper.dlg(wrapper.outer);
453 	}
454 
455 	extern(C) static void callBackCloseDestroy(OnCloseDelegateWrapper wrapper, GClosure* closure)
456 	{
457 		wrapper.remove(wrapper);
458 	}
459 
460 	protected class OnResponseDelegateWrapper
461 	{
462 		void delegate(int, InfoBar) dlg;
463 		gulong handlerId;
464 
465 		this(void delegate(int, InfoBar) dlg)
466 		{
467 			this.dlg = dlg;
468 			onResponseListeners ~= this;
469 		}
470 
471 		void remove(OnResponseDelegateWrapper source)
472 		{
473 			foreach(index, wrapper; onResponseListeners)
474 			{
475 				if (wrapper.handlerId == source.handlerId)
476 				{
477 					onResponseListeners[index] = null;
478 					onResponseListeners = std.algorithm.remove(onResponseListeners, index);
479 					break;
480 				}
481 			}
482 		}
483 	}
484 	OnResponseDelegateWrapper[] onResponseListeners;
485 
486 	/**
487 	 * Emitted when an action widget is clicked or the application programmer
488 	 * calls gtk_dialog_response(). The @response_id depends on which action
489 	 * widget was clicked.
490 	 *
491 	 * Params:
492 	 *     responseId = the response ID
493 	 *
494 	 * Since: 2.18
495 	 */
496 	gulong addOnResponse(void delegate(int, InfoBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
497 	{
498 		auto wrapper = new OnResponseDelegateWrapper(dlg);
499 		wrapper.handlerId = Signals.connectData(
500 			this,
501 			"response",
502 			cast(GCallback)&callBackResponse,
503 			cast(void*)wrapper,
504 			cast(GClosureNotify)&callBackResponseDestroy,
505 			connectFlags);
506 		return wrapper.handlerId;
507 	}
508 
509 	extern(C) static void callBackResponse(GtkInfoBar* infobarStruct, int responseId, OnResponseDelegateWrapper wrapper)
510 	{
511 		wrapper.dlg(responseId, wrapper.outer);
512 	}
513 
514 	extern(C) static void callBackResponseDestroy(OnResponseDelegateWrapper wrapper, GClosure* closure)
515 	{
516 		wrapper.remove(wrapper);
517 	}
518 }