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 public  import gtkc.gdktypes;
37 private import gtkc.gtk;
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 (bar);
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()
120 	{
121 		return gtkInfoBar;
122 	}
123 
124 	/** the main Gtk struct as a void* */
125 	protected override void* getStruct()
126 	{
127 		return cast(void*)gtkInfoBar;
128 	}
129 
130 	protected override void setStruct(GObject* obj)
131 	{
132 		gtkInfoBar = cast(GtkInfoBar*)obj;
133 		super.setStruct(obj);
134 	}
135 
136 	/**
137 	 * Sets our main struct and passes it to the parent class.
138 	 */
139 	public this (GtkInfoBar* gtkInfoBar, bool ownedRef = false)
140 	{
141 		this.gtkInfoBar = gtkInfoBar;
142 		super(cast(GtkBox*)gtkInfoBar, ownedRef);
143 	}
144 
145 	/** */
146 	public this(string[] buttonsText, ResponseType[] responses)
147 	{
148 		this();
149 		
150 		for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++)
151 		{
152 			addButton(buttonsText[i], responses[i]);
153 		}
154 	}
155 	
156 	/** */
157 	public this(StockID[] stockIDs, ResponseType[] responses)
158 	{
159 		this();
160 		
161 		for ( int i=0 ; i<stockIDs.length && i<responses.length ; i++)
162 		{
163 			addButton(stockIDs[i], responses[i]);
164 		}
165 	}
166 	
167 	/** */
168 	public Button addButton(StockID stockID, int responseId)
169 	{
170 		auto p = gtk_info_bar_add_button(gtkInfoBar, Str.toStringz(stockID), responseId);
171 		
172 		if ( p is null )
173 		{
174 			return null;
175 		}
176 		
177 		return new Button(cast(GtkButton*)p);
178 	}
179 	
180 	/** */
181 	public void addButtons(string[] buttonsText, ResponseType[] responses)
182 	{
183 		for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++)
184 		{
185 			addButton(buttonsText[i], responses[i]);
186 		}
187 	}
188 	
189 	/** */
190 	public void addButtons(StockID[] stockIDs, ResponseType[] responses)
191 	{
192 		for ( int i=0 ; i<stockIDs.length && i<responses.length ; i++)
193 		{
194 			addButton(stockIDs[i], responses[i]);
195 		}
196 	}
197 	
198 	/**
199 	 * Returns the action area of info_bar.
200 	 * Since 2.18
201 	 * Returns: the action area.
202 	 */
203 	public VButtonBox getActionArea()
204 	{
205 		// GtkWidget * gtk_info_bar_get_action_area (GtkInfoBar *info_bar);
206 		auto p = gtk_info_bar_get_action_area(gtkInfoBar);
207 		if(p is null)
208 		{
209 			return null;
210 		}
211 		return new VButtonBox(cast(GtkVButtonBox*) p);
212 	}
213 	
214 	/**
215 	 * Returns the content area of info_bar.
216 	 * Since 2.18
217 	 * Returns: the content area.
218 	 */
219 	public HBox getContentArea()
220 	{
221 		// GtkWidget * gtk_info_bar_get_content_area (GtkInfoBar *info_bar);
222 		auto p = gtk_info_bar_get_content_area(gtkInfoBar);
223 		if(p is null)
224 		{
225 			return null;
226 		}
227 		return new HBox(cast(GtkHBox*) p);
228 	}
229 
230 	/**
231 	 */
232 
233 	/** */
234 	public static GType getType()
235 	{
236 		return gtk_info_bar_get_type();
237 	}
238 
239 	/**
240 	 * Creates a new #GtkInfoBar object.
241 	 *
242 	 * Return: a new #GtkInfoBar object
243 	 *
244 	 * Since: 2.18
245 	 *
246 	 * Throws: ConstructionException GTK+ fails to create the object.
247 	 */
248 	public this()
249 	{
250 		auto p = gtk_info_bar_new();
251 		
252 		if(p is null)
253 		{
254 			throw new ConstructionException("null returned by new");
255 		}
256 		
257 		this(cast(GtkInfoBar*) p);
258 	}
259 
260 	/**
261 	 * Add an activatable widget to the action area of a #GtkInfoBar,
262 	 * connecting a signal handler that will emit the #GtkInfoBar::response
263 	 * signal on the message area when the widget is activated. The widget
264 	 * is appended to the end of the message areas action area.
265 	 *
266 	 * Params:
267 	 *     child = an activatable widget
268 	 *     responseId = response ID for @child
269 	 *
270 	 * Since: 2.18
271 	 */
272 	public void addActionWidget(Widget child, int responseId)
273 	{
274 		gtk_info_bar_add_action_widget(gtkInfoBar, (child is null) ? null : child.getWidgetStruct(), responseId);
275 	}
276 
277 	/**
278 	 * Adds a button with the given text and sets things up so that
279 	 * clicking the button will emit the “response” signal with the given
280 	 * response_id. The button is appended to the end of the info bars's
281 	 * action area. The button widget is returned, but usually you don't
282 	 * need it.
283 	 *
284 	 * Params:
285 	 *     buttonText = text of button
286 	 *     responseId = response ID for the button
287 	 *
288 	 * Return: the #GtkButton widget
289 	 *     that was added
290 	 *
291 	 * Since: 2.18
292 	 */
293 	public Button addButton(string buttonText, int responseId)
294 	{
295 		auto p = gtk_info_bar_add_button(gtkInfoBar, Str.toStringz(buttonText), responseId);
296 		
297 		if(p is null)
298 		{
299 			return null;
300 		}
301 		
302 		return ObjectG.getDObject!(Button)(cast(GtkButton*) p);
303 	}
304 
305 	/**
306 	 * Returns the message type of the message area.
307 	 *
308 	 * Return: the message type of the message area.
309 	 *
310 	 * Since: 2.18
311 	 */
312 	public GtkMessageType getMessageType()
313 	{
314 		return gtk_info_bar_get_message_type(gtkInfoBar);
315 	}
316 
317 	/**
318 	 * Returns whether the widget will display a standard close button.
319 	 *
320 	 * Return: %TRUE if the widget displays standard close button
321 	 *
322 	 * Since: 3.10
323 	 */
324 	public bool getShowCloseButton()
325 	{
326 		return gtk_info_bar_get_show_close_button(gtkInfoBar) != 0;
327 	}
328 
329 	/**
330 	 * Emits the “response” signal with the given @response_id.
331 	 *
332 	 * Params:
333 	 *     responseId = a response ID
334 	 *
335 	 * Since: 2.18
336 	 */
337 	public void response(int responseId)
338 	{
339 		gtk_info_bar_response(gtkInfoBar, responseId);
340 	}
341 
342 	/**
343 	 * Sets the last widget in the info bar’s action area with
344 	 * the given response_id as the default widget for the dialog.
345 	 * Pressing “Enter” normally activates the default widget.
346 	 *
347 	 * Note that this function currently requires @info_bar to
348 	 * be added to a widget hierarchy.
349 	 *
350 	 * Params:
351 	 *     responseId = a response ID
352 	 *
353 	 * Since: 2.18
354 	 */
355 	public void setDefaultResponse(int responseId)
356 	{
357 		gtk_info_bar_set_default_response(gtkInfoBar, responseId);
358 	}
359 
360 	/**
361 	 * Sets the message type of the message area.
362 	 *
363 	 * GTK+ uses this type to determine how the message is displayed.
364 	 *
365 	 * Params:
366 	 *     messageType = a #GtkMessageType
367 	 *
368 	 * Since: 2.18
369 	 */
370 	public void setMessageType(GtkMessageType messageType)
371 	{
372 		gtk_info_bar_set_message_type(gtkInfoBar, messageType);
373 	}
374 
375 	/**
376 	 * Calls gtk_widget_set_sensitive (widget, setting) for each
377 	 * widget in the info bars’s action area with the given response_id.
378 	 * A convenient way to sensitize/desensitize dialog buttons.
379 	 *
380 	 * Params:
381 	 *     responseId = a response ID
382 	 *     setting = TRUE for sensitive
383 	 *
384 	 * Since: 2.18
385 	 */
386 	public void setResponseSensitive(int responseId, bool setting)
387 	{
388 		gtk_info_bar_set_response_sensitive(gtkInfoBar, responseId, setting);
389 	}
390 
391 	/**
392 	 * If true, a standard close button is shown. When clicked it emits
393 	 * the response %GTK_RESPONSE_CLOSE.
394 	 *
395 	 * Params:
396 	 *     setting = %TRUE to include a close button
397 	 *
398 	 * Since: 3.10
399 	 */
400 	public void setShowCloseButton(bool setting)
401 	{
402 		gtk_info_bar_set_show_close_button(gtkInfoBar, setting);
403 	}
404 
405 	protected class OnCloseDelegateWrapper
406 	{
407 		void delegate(InfoBar) dlg;
408 		gulong handlerId;
409 		ConnectFlags flags;
410 		this(void delegate(InfoBar) dlg, gulong handlerId, ConnectFlags flags)
411 		{
412 			this.dlg = dlg;
413 			this.handlerId = handlerId;
414 			this.flags = flags;
415 		}
416 	}
417 	protected OnCloseDelegateWrapper[] onCloseListeners;
418 
419 	/**
420 	 * The ::close signal is a
421 	 * [keybinding signal][GtkBindingSignal]
422 	 * which gets emitted when the user uses a keybinding to dismiss
423 	 * the info bar.
424 	 *
425 	 * The default binding for this signal is the Escape key.
426 	 *
427 	 * Since: 2.18
428 	 */
429 	gulong addOnClose(void delegate(InfoBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
430 	{
431 		onCloseListeners ~= new OnCloseDelegateWrapper(dlg, 0, connectFlags);
432 		onCloseListeners[onCloseListeners.length - 1].handlerId = Signals.connectData(
433 			this,
434 			"close",
435 			cast(GCallback)&callBackClose,
436 			cast(void*)onCloseListeners[onCloseListeners.length - 1],
437 			cast(GClosureNotify)&callBackCloseDestroy,
438 			connectFlags);
439 		return onCloseListeners[onCloseListeners.length - 1].handlerId;
440 	}
441 	
442 	extern(C) static void callBackClose(GtkInfoBar* infobarStruct,OnCloseDelegateWrapper wrapper)
443 	{
444 		wrapper.dlg(wrapper.outer);
445 	}
446 	
447 	extern(C) static void callBackCloseDestroy(OnCloseDelegateWrapper wrapper, GClosure* closure)
448 	{
449 		wrapper.outer.internalRemoveOnClose(wrapper);
450 	}
451 
452 	protected void internalRemoveOnClose(OnCloseDelegateWrapper source)
453 	{
454 		foreach(index, wrapper; onCloseListeners)
455 		{
456 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
457 			{
458 				onCloseListeners[index] = null;
459 				onCloseListeners = std.algorithm.remove(onCloseListeners, index);
460 				break;
461 			}
462 		}
463 	}
464 	
465 
466 	protected class OnResponseDelegateWrapper
467 	{
468 		void delegate(int, InfoBar) dlg;
469 		gulong handlerId;
470 		ConnectFlags flags;
471 		this(void delegate(int, InfoBar) dlg, gulong handlerId, ConnectFlags flags)
472 		{
473 			this.dlg = dlg;
474 			this.handlerId = handlerId;
475 			this.flags = flags;
476 		}
477 	}
478 	protected OnResponseDelegateWrapper[] onResponseListeners;
479 
480 	/**
481 	 * Emitted when an action widget is clicked or the application programmer
482 	 * calls gtk_dialog_response(). The @response_id depends on which action
483 	 * widget was clicked.
484 	 *
485 	 * Params:
486 	 *     responseId = the response ID
487 	 *
488 	 * Since: 2.18
489 	 */
490 	gulong addOnResponse(void delegate(int, InfoBar) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
491 	{
492 		onResponseListeners ~= new OnResponseDelegateWrapper(dlg, 0, connectFlags);
493 		onResponseListeners[onResponseListeners.length - 1].handlerId = Signals.connectData(
494 			this,
495 			"response",
496 			cast(GCallback)&callBackResponse,
497 			cast(void*)onResponseListeners[onResponseListeners.length - 1],
498 			cast(GClosureNotify)&callBackResponseDestroy,
499 			connectFlags);
500 		return onResponseListeners[onResponseListeners.length - 1].handlerId;
501 	}
502 	
503 	extern(C) static void callBackResponse(GtkInfoBar* infobarStruct, int responseId,OnResponseDelegateWrapper wrapper)
504 	{
505 		wrapper.dlg(responseId, wrapper.outer);
506 	}
507 	
508 	extern(C) static void callBackResponseDestroy(OnResponseDelegateWrapper wrapper, GClosure* closure)
509 	{
510 		wrapper.outer.internalRemoveOnResponse(wrapper);
511 	}
512 
513 	protected void internalRemoveOnResponse(OnResponseDelegateWrapper source)
514 	{
515 		foreach(index, wrapper; onResponseListeners)
516 		{
517 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
518 			{
519 				onResponseListeners[index] = null;
520 				onResponseListeners = std.algorithm.remove(onResponseListeners, index);
521 				break;
522 			}
523 		}
524 	}
525 	
526 }