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.Dialog; 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.HeaderBar; 33 private import gtk.Widget; 34 private import gtk.Window; 35 private import gtk.c.functions; 36 public import gtk.c.types; 37 private import std.algorithm; 38 39 40 /** 41 * Dialogs are a convenient way to prompt the user for a small amount 42 * of input. 43 * 44 * ![An example GtkDialog](dialog.png) 45 * 46 * Typical uses are to display a message, ask a question, or anything else 47 * that does not require extensive effort on the user’s part. 48 * 49 * The main area of a `GtkDialog` is called the "content area", and is yours 50 * to populate with widgets such a `GtkLabel` or `GtkEntry`, to present 51 * your information, questions, or tasks to the user. 52 * 53 * In addition, dialogs allow you to add "action widgets". Most commonly, 54 * action widgets are buttons. Depending on the platform, action widgets may 55 * be presented in the header bar at the top of the window, or at the bottom 56 * of the window. To add action widgets, create your `GtkDialog` using 57 * [ctor@Gtk.Dialog.new_with_buttons], or use 58 * [method@Gtk.Dialog.add_button], [method@Gtk.Dialog.add_buttons], 59 * or [method@Gtk.Dialog.add_action_widget]. 60 * 61 * `GtkDialogs` uses some heuristics to decide whether to add a close 62 * button to the window decorations. If any of the action buttons use 63 * the response ID %GTK_RESPONSE_CLOSE or %GTK_RESPONSE_CANCEL, the 64 * close button is omitted. 65 * 66 * Clicking a button that was added as an action widget will emit the 67 * [signal@Gtk.Dialog::response] signal with a response ID that you specified. 68 * GTK will never assign a meaning to positive response IDs; these are 69 * entirely user-defined. But for convenience, you can use the response 70 * IDs in the [enum@Gtk.ResponseType] enumeration (these all have values 71 * less than zero). If a dialog receives a delete event, the 72 * [signal@Gtk.Dialog::response] signal will be emitted with the 73 * %GTK_RESPONSE_DELETE_EVENT response ID. 74 * 75 * Dialogs are created with a call to [ctor@Gtk.Dialog.new] or 76 * [ctor@Gtk.Dialog.new_with_buttons]. The latter is recommended; it allows 77 * you to set the dialog title, some convenient flags, and add buttons. 78 * 79 * A “modal” dialog (that is, one which freezes the rest of the application 80 * from user input), can be created by calling [method@Gtk.Window.set_modal] 81 * on the dialog. When using [ctor@Gtk.Dialog.new_with_buttons], you can also 82 * pass the %GTK_DIALOG_MODAL flag to make a dialog modal. 83 * 84 * For the simple dialog in the following example, a [class@Gtk.MessageDialog] 85 * would save some effort. But you’d need to create the dialog contents manually 86 * if you had more than a simple message in the dialog. 87 * 88 * An example for simple `GtkDialog` usage: 89 * ```c 90 * // Function to open a dialog box with a message 91 * void 92 * quick_message (GtkWindow *parent, char *message) 93 * { 94 * GtkWidget *dialog, *label, *content_area; 95 * GtkDialogFlags flags; 96 * 97 * // Create the widgets 98 * flags = GTK_DIALOG_DESTROY_WITH_PARENT; 99 * dialog = gtk_dialog_new_with_buttons ("Message", 100 * parent, 101 * flags, 102 * _("_OK"), 103 * GTK_RESPONSE_NONE, 104 * NULL); 105 * content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); 106 * label = gtk_label_new (message); 107 * 108 * // Ensure that the dialog box is destroyed when the user responds 109 * 110 * g_signal_connect_swapped (dialog, 111 * "response", 112 * G_CALLBACK (gtk_window_destroy), 113 * dialog); 114 * 115 * // Add the label, and show everything we’ve added 116 * 117 * gtk_box_append (GTK_BOX (content_area), label); 118 * gtk_widget_show (dialog); 119 * } 120 * ``` 121 * 122 * # GtkDialog as GtkBuildable 123 * 124 * The `GtkDialog` implementation of the `GtkBuildable` interface exposes the 125 * @content_area as an internal child with the name “content_area”. 126 * 127 * `GtkDialog` supports a custom <action-widgets> element, which can contain 128 * multiple <action-widget> elements. The “response” attribute specifies a 129 * numeric response, and the content of the element is the id of widget 130 * (which should be a child of the dialogs @action_area). To mark a response 131 * as default, set the “default“ attribute of the <action-widget> element 132 * to true. 133 * 134 * `GtkDialog` supports adding action widgets by specifying “action“ as 135 * the “type“ attribute of a <child> element. The widget will be added 136 * either to the action area or the headerbar of the dialog, depending 137 * on the “use-header-bar“ property. The response id has to be associated 138 * with the action widget using the <action-widgets> element. 139 * 140 * An example of a #GtkDialog UI definition fragment: 141 * ```xml 142 * <object class="GtkDialog" id="dialog1"> 143 * <child type="action"> 144 * <object class="GtkButton" id="button_cancel"/> 145 * </child> 146 * <child type="action"> 147 * <object class="GtkButton" id="button_ok"> 148 * </object> 149 * </child> 150 * <action-widgets> 151 * <action-widget response="cancel">button_cancel</action-widget> 152 * <action-widget response="ok" default="true">button_ok</action-widget> 153 * </action-widgets> 154 * </object> 155 * ``` 156 * 157 * # Accessibility 158 * 159 * `GtkDialog` uses the %GTK_ACCESSIBLE_ROLE_DIALOG role. 160 */ 161 public class Dialog : Window 162 { 163 /** the main Gtk struct */ 164 protected GtkDialog* gtkDialog; 165 166 /** Get the main Gtk struct */ 167 public GtkDialog* getDialogStruct(bool transferOwnership = false) 168 { 169 if (transferOwnership) 170 ownedRef = false; 171 return gtkDialog; 172 } 173 174 /** the main Gtk struct as a void* */ 175 protected override void* getStruct() 176 { 177 return cast(void*)gtkDialog; 178 } 179 180 /** 181 * Sets our main struct and passes it to the parent class. 182 */ 183 public this (GtkDialog* gtkDialog, bool ownedRef = false) 184 { 185 this.gtkDialog = gtkDialog; 186 super(cast(GtkWindow*)gtkDialog, ownedRef); 187 } 188 189 /** */ 190 public void addButtons(string[] buttonsText, ResponseType[] responses) 191 { 192 for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++) 193 { 194 addButton(buttonsText[i], responses[i]); 195 } 196 } 197 198 /** 199 */ 200 201 /** */ 202 public static GType getType() 203 { 204 return gtk_dialog_get_type(); 205 } 206 207 /** 208 * Creates a new dialog box. 209 * 210 * Widgets should not be packed into the `GtkWindow` 211 * directly, but into the @content_area and @action_area, 212 * as described above. 213 * 214 * Returns: the new dialog as a `GtkWidget` 215 * 216 * Throws: ConstructionException GTK+ fails to create the object. 217 */ 218 public this() 219 { 220 auto __p = gtk_dialog_new(); 221 222 if(__p is null) 223 { 224 throw new ConstructionException("null returned by new"); 225 } 226 227 this(cast(GtkDialog*) __p); 228 } 229 230 /** 231 * Adds an activatable widget to the action area of a `GtkDialog`. 232 * 233 * GTK connects a signal handler that will emit the 234 * [signal@Gtk.Dialog::response] signal on the dialog when the widget 235 * is activated. The widget is appended to the end of the dialog’s action 236 * area. 237 * 238 * If you want to add a non-activatable widget, simply pack it into 239 * the @action_area field of the `GtkDialog` struct. 240 * 241 * Params: 242 * child = an activatable widget 243 * responseId = response ID for @child 244 */ 245 public void addActionWidget(Widget child, int responseId) 246 { 247 gtk_dialog_add_action_widget(gtkDialog, (child is null) ? null : child.getWidgetStruct(), responseId); 248 } 249 250 /** 251 * Adds a button with the given text. 252 * 253 * GTK arranges things so that clicking the button will emit the 254 * [signal@Gtk.Dialog::response] signal with the given @response_id. 255 * The button is appended to the end of the dialog’s action area. 256 * The button widget is returned, but usually you don’t need it. 257 * 258 * Params: 259 * buttonText = text of button 260 * responseId = response ID for the button 261 * 262 * Returns: the `GtkButton` widget that was added 263 */ 264 public Widget addButton(string buttonText, int responseId) 265 { 266 auto __p = gtk_dialog_add_button(gtkDialog, Str.toStringz(buttonText), responseId); 267 268 if(__p is null) 269 { 270 return null; 271 } 272 273 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) __p); 274 } 275 276 /** 277 * Returns the content area of @dialog. 278 * 279 * Returns: the content area #GtkBox. 280 */ 281 public Box getContentArea() 282 { 283 auto __p = gtk_dialog_get_content_area(gtkDialog); 284 285 if(__p is null) 286 { 287 return null; 288 } 289 290 return ObjectG.getDObject!(Box)(cast(GtkBox*) __p); 291 } 292 293 /** 294 * Returns the header bar of @dialog. 295 * 296 * Note that the headerbar is only used by the dialog if the 297 * [property@Gtk.Dialog:use-header-bar] property is %TRUE. 298 * 299 * Returns: the header bar 300 */ 301 public HeaderBar getHeaderBar() 302 { 303 auto __p = gtk_dialog_get_header_bar(gtkDialog); 304 305 if(__p is null) 306 { 307 return null; 308 } 309 310 return ObjectG.getDObject!(HeaderBar)(cast(GtkHeaderBar*) __p); 311 } 312 313 /** 314 * Gets the response id of a widget in the action area 315 * of a dialog. 316 * 317 * Params: 318 * widget = a widget in the action area of @dialog 319 * 320 * Returns: the response id of @widget, or %GTK_RESPONSE_NONE 321 * if @widget doesn’t have a response id set. 322 */ 323 public int getResponseForWidget(Widget widget) 324 { 325 return gtk_dialog_get_response_for_widget(gtkDialog, (widget is null) ? null : widget.getWidgetStruct()); 326 } 327 328 /** 329 * Gets the widget button that uses the given response ID in the action area 330 * of a dialog. 331 * 332 * Params: 333 * responseId = the response ID used by the @dialog widget 334 * 335 * Returns: the @widget button that uses the given 336 * @response_id, or %NULL. 337 */ 338 public Widget getWidgetForResponse(int responseId) 339 { 340 auto __p = gtk_dialog_get_widget_for_response(gtkDialog, responseId); 341 342 if(__p is null) 343 { 344 return null; 345 } 346 347 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) __p); 348 } 349 350 /** 351 * Emits the ::response signal with the given response ID. 352 * 353 * Used to indicate that the user has responded to the dialog in some way. 354 * 355 * Params: 356 * responseId = response ID 357 */ 358 public void response(int responseId) 359 { 360 gtk_dialog_response(gtkDialog, responseId); 361 } 362 363 /** 364 * Sets the default widget for the dialog based on the response ID. 365 * 366 * Pressing “Enter” normally activates the default widget. 367 * 368 * Params: 369 * responseId = a response ID 370 */ 371 public void setDefaultResponse(int responseId) 372 { 373 gtk_dialog_set_default_response(gtkDialog, responseId); 374 } 375 376 /** 377 * A convenient way to sensitize/desensitize dialog buttons. 378 * 379 * Calls `gtk_widget_set_sensitive (widget, @setting)` 380 * for each widget in the dialog’s action area with the given @response_id. 381 * 382 * Params: 383 * responseId = a response ID 384 * setting = %TRUE for sensitive 385 */ 386 public void setResponseSensitive(int responseId, bool setting) 387 { 388 gtk_dialog_set_response_sensitive(gtkDialog, responseId, setting); 389 } 390 391 /** 392 * Emitted when the user uses a keybinding to close the dialog. 393 * 394 * This is a [keybinding signal](class.SignalAction.html). 395 * 396 * The default binding for this signal is the Escape key. 397 */ 398 gulong addOnClose(void delegate(Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 399 { 400 return Signals.connect(this, "close", dlg, connectFlags ^ ConnectFlags.SWAPPED); 401 } 402 403 /** 404 * Emitted when an action widget is clicked. 405 * 406 * The signal is also emitted when the dialog receives a 407 * delete event, and when [method@Gtk.Dialog.response] is called. 408 * On a delete event, the response ID is %GTK_RESPONSE_DELETE_EVENT. 409 * Otherwise, it depends on which action widget was clicked. 410 * 411 * Params: 412 * responseId = the response ID 413 */ 414 gulong addOnResponse(void delegate(int, Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 415 { 416 return Signals.connect(this, "response", dlg, connectFlags ^ ConnectFlags.SWAPPED); 417 } 418 }