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 gdk.Screen; 28 private import glib.ConstructionException; 29 private import glib.Str; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gtk.Button; 33 private import gtk.HButtonBox; 34 private import gtk.VBox; 35 private import gtk.Widget; 36 private import gtk.Window; 37 public import gtkc.gdktypes; 38 private import gtkc.gtk; 39 public import gtkc.gtktypes; 40 41 42 /** 43 * Dialog boxes are a convenient way to prompt the user for a small amount 44 * of input, e.g. to display a message, ask a question, or anything else 45 * that does not require extensive effort on the user’s part. 46 * 47 * GTK+ treats a dialog as a window split vertically. The top section is a 48 * #GtkVBox, and is where widgets such as a #GtkLabel or a #GtkEntry should 49 * be packed. The bottom area is known as the 50 * “action area”. This is generally used for 51 * packing buttons into the dialog which may perform functions such as 52 * cancel, ok, or apply. 53 * 54 * #GtkDialog boxes are created with a call to gtk_dialog_new() or 55 * gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is 56 * recommended; it allows you to set the dialog title, some convenient 57 * flags, and add simple buttons. 58 * 59 * If “dialog” is a newly created dialog, the two primary areas of the 60 * window can be accessed through gtk_dialog_get_content_area() and 61 * gtk_dialog_get_action_area(), as can be seen from the example below. 62 * 63 * A “modal” dialog (that is, one which freezes the rest of the application 64 * from user input), can be created by calling gtk_window_set_modal() on the 65 * dialog. Use the GTK_WINDOW() macro to cast the widget returned from 66 * gtk_dialog_new() into a #GtkWindow. When using gtk_dialog_new_with_buttons() 67 * you can also pass the #GTK_DIALOG_MODAL flag to make a dialog modal. 68 * 69 * If you add buttons to #GtkDialog using gtk_dialog_new_with_buttons(), 70 * gtk_dialog_add_button(), gtk_dialog_add_buttons(), or 71 * gtk_dialog_add_action_widget(), clicking the button will emit a signal 72 * called #GtkDialog::response with a response ID that you specified. GTK+ 73 * will never assign a meaning to positive response IDs; these are entirely 74 * user-defined. But for convenience, you can use the response IDs in the 75 * #GtkResponseType enumeration (these all have values less than zero). If 76 * a dialog receives a delete event, the #GtkDialog::response signal will 77 * be emitted with a response ID of #GTK_RESPONSE_DELETE_EVENT. 78 * 79 * If you want to block waiting for a dialog to return before returning 80 * control flow to your code, you can call gtk_dialog_run(). This function 81 * enters a recursive main loop and waits for the user to respond to the 82 * dialog, returning the response ID corresponding to the button the user 83 * clicked. 84 * 85 * For the simple dialog in the following example, in reality you’d probably 86 * use #GtkMessageDialog to save yourself some effort. But you’d need to 87 * create the dialog contents manually if you had more than a simple message 88 * in the dialog. 89 * 90 * An example for simple GtkDialog usage: 91 * |[<!-- language="C" --> 92 * // Function to open a dialog box with a message 93 * void 94 * quick_message (GtkWindow *parent, gchar *message) 95 * { 96 * GtkWidget *dialog, *label, *content_area; 97 * GtkDialogFlags flags; 98 * 99 * // Create the widgets 100 * flags = GTK_DIALOG_DESTROY_WITH_PARENT; 101 * dialog = gtk_dialog_new_with_buttons ("Message", 102 * parent, 103 * flags, 104 * _("_OK"), 105 * GTK_RESPONSE_NONE, 106 * NULL); 107 * content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog)); 108 * label = gtk_label_new (message); 109 * 110 * // Ensure that the dialog box is destroyed when the user responds 111 * 112 * g_signal_connect_swapped (dialog, 113 * "response", 114 * G_CALLBACK (gtk_widget_destroy), 115 * dialog); 116 * 117 * // Add the label, and show everything we’ve added 118 * 119 * gtk_container_add (GTK_CONTAINER (content_area), label); 120 * gtk_widget_show_all (dialog); 121 * } 122 * ]| 123 * 124 * # GtkDialog as GtkBuildable 125 * 126 * The GtkDialog implementation of the #GtkBuildable interface exposes the 127 * @vbox and @action_area as internal children with the names “vbox” and 128 * “action_area”. 129 * 130 * GtkDialog supports a custom <action-widgets> element, which can contain 131 * multiple <action-widget> elements. The “response” attribute specifies a 132 * numeric response, and the content of the element is the id of widget 133 * (which should be a child of the dialogs @action_area). To mark a response 134 * as default, set the “default“ attribute of the <action-widget> element 135 * to true. 136 * 137 * GtkDialog supports adding action widgets by specifying “action“ as 138 * the “type“ attribute of a <child> element. The widget will be added 139 * either to the action area or the headerbar of the dialog, depending 140 * on the “use-header-bar“ property. The response id has to be associated 141 * with the action widget using the <action-widgets> element. 142 * 143 * An example of a #GtkDialog UI definition fragment: 144 * |[ 145 * <object class="GtkDialog" id="dialog1"> 146 * <child type="action"> 147 * <object class="GtkButton" id="button_cancel"/> 148 * </child> 149 * <child type="action"> 150 * <object class="GtkButton" id="button_ok"/> 151 * </child> 152 * <action-widgets> 153 * <action-widget response="cancel">button_cancel</action-widget> 154 * <action-widget response="ok" default="true">button_ok</action-widget> 155 * </action-widgets> 156 * </object> 157 * ]| 158 */ 159 public class Dialog : Window 160 { 161 /** the main Gtk struct */ 162 protected GtkDialog* gtkDialog; 163 164 /** Get the main Gtk struct */ 165 public GtkDialog* getDialogStruct() 166 { 167 return gtkDialog; 168 } 169 170 /** the main Gtk struct as a void* */ 171 protected override void* getStruct() 172 { 173 return cast(void*)gtkDialog; 174 } 175 176 protected override void setStruct(GObject* obj) 177 { 178 gtkDialog = cast(GtkDialog*)obj; 179 super.setStruct(obj); 180 } 181 182 /** 183 * Sets our main struct and passes it to the parent class. 184 */ 185 public this (GtkDialog* gtkDialog, bool ownedRef = false) 186 { 187 this.gtkDialog = gtkDialog; 188 super(cast(GtkWindow*)gtkDialog, ownedRef); 189 } 190 191 /** 192 * Both title and parent can be null. 193 */ 194 this(string title, Window parent, GtkDialogFlags flags, string[] buttonsText, ResponseType[] responses) 195 { 196 auto p = gtk_dialog_new_with_buttons(Str.toStringz(title), (parent is null) ? null : parent.getWindowStruct(), flags, Str.toStringz(buttonsText[0]), responses[0], null); 197 if(p is null) 198 { 199 throw new ConstructionException("null returned by gtk_dialog_new_with_buttons"); 200 } 201 202 this(cast(GtkDialog*)p); 203 204 addButtons(buttonsText[1 .. $], responses[1 .. $]); 205 } 206 207 /** ditto */ 208 this(string title, Window parent, GtkDialogFlags flags, StockID[] stockIDs, ResponseType[] responses) 209 { 210 auto p = gtk_dialog_new_with_buttons(Str.toStringz(title), (parent is null) ? null : parent.getWindowStruct(), flags, Str.toStringz(stockIDs[0]), responses[0], null); 211 if(p is null) 212 { 213 throw new ConstructionException("null returned by gtk_dialog_new_with_buttons"); 214 } 215 216 this(cast(GtkDialog*)p); 217 218 addButtons(stockIDs[1 .. $], responses[1 .. $]); 219 } 220 221 /** */ 222 public Button addButton(StockID stockID, int responseId) 223 { 224 auto p = gtk_dialog_add_button(gtkDialog, Str.toStringz(stockID), responseId); 225 226 if ( p is null ) 227 { 228 return null; 229 } 230 231 return new Button(cast(GtkButton*)p); 232 } 233 234 /** */ 235 public void addButtons(string[] buttonsText, ResponseType[] responses) 236 { 237 for ( int i=0 ; i<buttonsText.length && i<responses.length ; i++) 238 { 239 addButton(buttonsText[i], responses[i]); 240 } 241 } 242 243 /** */ 244 public void addButtons(StockID[] stockIDs, ResponseType[] responses) 245 { 246 for ( int i=0 ; i<stockIDs.length && i<responses.length ; i++) 247 { 248 addButton(stockIDs[i], responses[i]); 249 } 250 } 251 252 //Return the corect class instead of Widget 253 /** 254 * Returns the action area of dialog. 255 * Since: 2.14 256 * Returns: the action area. 257 */ 258 public HButtonBox getActionArea() 259 { 260 auto p = gtk_dialog_get_action_area(gtkDialog); 261 if(p is null) 262 { 263 return null; 264 } 265 return new HButtonBox(cast(GtkHButtonBox*) p); 266 } 267 268 //Return the corect class instead of Widget 269 /** 270 * Returns the content area of dialog. 271 * Since: 2.14 272 * Returns: the content area GtkVBox. 273 */ 274 public VBox getContentArea() 275 { 276 auto p = gtk_dialog_get_content_area(gtkDialog); 277 if(p is null) 278 { 279 return null; 280 } 281 return new VBox(cast(GtkVBox*) p); 282 } 283 284 /** 285 */ 286 287 public static GType getType() 288 { 289 return gtk_dialog_get_type(); 290 } 291 292 /** 293 * Creates a new dialog box. 294 * 295 * Widgets should not be packed into this #GtkWindow 296 * directly, but into the @vbox and @action_area, as described above. 297 * 298 * Return: the new dialog as a #GtkWidget 299 * 300 * Throws: ConstructionException GTK+ fails to create the object. 301 */ 302 public this() 303 { 304 auto p = gtk_dialog_new(); 305 306 if(p is null) 307 { 308 throw new ConstructionException("null returned by new"); 309 } 310 311 this(cast(GtkDialog*) p); 312 } 313 314 /** 315 * Adds an activatable widget to the action area of a #GtkDialog, 316 * connecting a signal handler that will emit the #GtkDialog::response 317 * signal on the dialog when the widget is activated. The widget is 318 * appended to the end of the dialog’s action area. If you want to add a 319 * non-activatable widget, simply pack it into the @action_area field 320 * of the #GtkDialog struct. 321 * 322 * Params: 323 * child = an activatable widget 324 * responseId = response ID for @child 325 */ 326 public void addActionWidget(Widget child, int responseId) 327 { 328 gtk_dialog_add_action_widget(gtkDialog, (child is null) ? null : child.getWidgetStruct(), responseId); 329 } 330 331 /** 332 * Adds a button with the given text and sets things up so that 333 * clicking the button will emit the #GtkDialog::response signal with 334 * the given @response_id. The button is appended to the end of the 335 * dialog’s action area. The button widget is returned, but usually 336 * you don’t need it. 337 * 338 * Params: 339 * buttonText = text of button 340 * responseId = response ID for the button 341 * 342 * Return: the #GtkButton widget that was added 343 */ 344 public Widget addButton(string buttonText, int responseId) 345 { 346 auto p = gtk_dialog_add_button(gtkDialog, Str.toStringz(buttonText), responseId); 347 348 if(p is null) 349 { 350 return null; 351 } 352 353 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 354 } 355 356 /** 357 * Returns the header bar of @dialog. Note that the 358 * headerbar is only used by the dialog if the 359 * #GtkDialog:use-header-bar property is %TRUE. 360 * 361 * Return: the header bar 362 * 363 * Since: 3.12 364 */ 365 public Widget getHeaderBar() 366 { 367 auto p = gtk_dialog_get_header_bar(gtkDialog); 368 369 if(p is null) 370 { 371 return null; 372 } 373 374 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 375 } 376 377 /** 378 * Gets the response id of a widget in the action area 379 * of a dialog. 380 * 381 * Params: 382 * widget = a widget in the action area of @dialog 383 * 384 * Return: the response id of @widget, or %GTK_RESPONSE_NONE 385 * if @widget doesn’t have a response id set. 386 * 387 * Since: 2.8 388 */ 389 public int getResponseForWidget(Widget widget) 390 { 391 return gtk_dialog_get_response_for_widget(gtkDialog, (widget is null) ? null : widget.getWidgetStruct()); 392 } 393 394 /** 395 * Gets the widget button that uses the given response ID in the action area 396 * of a dialog. 397 * 398 * Params: 399 * responseId = the response ID used by the @dialog widget 400 * 401 * Return: the @widget button that uses the given 402 * @response_id, or %NULL. 403 * 404 * Since: 2.20 405 */ 406 public Widget getWidgetForResponse(int responseId) 407 { 408 auto p = gtk_dialog_get_widget_for_response(gtkDialog, responseId); 409 410 if(p is null) 411 { 412 return null; 413 } 414 415 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 416 } 417 418 /** 419 * Emits the #GtkDialog::response signal with the given response ID. 420 * Used to indicate that the user has responded to the dialog in some way; 421 * typically either you or gtk_dialog_run() will be monitoring the 422 * ::response signal and take appropriate action. 423 * 424 * Params: 425 * responseId = response ID 426 */ 427 public void response(int responseId) 428 { 429 gtk_dialog_response(gtkDialog, responseId); 430 } 431 432 /** 433 * Blocks in a recursive main loop until the @dialog either emits the 434 * #GtkDialog::response signal, or is destroyed. If the dialog is 435 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns 436 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the 437 * ::response signal emission. 438 * 439 * Before entering the recursive main loop, gtk_dialog_run() calls 440 * gtk_widget_show() on the dialog for you. Note that you still 441 * need to show any children of the dialog yourself. 442 * 443 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event 444 * is disabled; if the dialog receives ::delete_event, it will not be 445 * destroyed as windows usually are, and gtk_dialog_run() will return 446 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog 447 * will be modal. You can force gtk_dialog_run() to return at any time by 448 * calling gtk_dialog_response() to emit the ::response signal. Destroying 449 * the dialog during gtk_dialog_run() is a very bad idea, because your 450 * post-run code won’t know whether the dialog was destroyed or not. 451 * 452 * After gtk_dialog_run() returns, you are responsible for hiding or 453 * destroying the dialog if you wish to do so. 454 * 455 * Typical usage of this function might be: 456 * |[<!-- language="C" --> 457 * gint result = gtk_dialog_run (GTK_DIALOG (dialog)); 458 * switch (result) 459 * { 460 * case GTK_RESPONSE_ACCEPT: 461 * do_application_specific_something (); 462 * break; 463 * default: 464 * do_nothing_since_dialog_was_cancelled (); 465 * break; 466 * } 467 * gtk_widget_destroy (dialog); 468 * ]| 469 * 470 * Note that even though the recursive main loop gives the effect of a 471 * modal dialog (it prevents the user from interacting with other 472 * windows in the same window group while the dialog is run), callbacks 473 * such as timeouts, IO channel watches, DND drops, etc, will 474 * be triggered during a gtk_dialog_run() call. 475 * 476 * Return: response ID 477 */ 478 public int run() 479 { 480 return gtk_dialog_run(gtkDialog); 481 } 482 483 /** 484 * Sets an alternative button order. If the 485 * #GtkSettings:gtk-alternative-button-order setting is set to %TRUE, 486 * the dialog buttons are reordered according to the order of the 487 * response ids in @new_order. 488 * 489 * See gtk_dialog_set_alternative_button_order() for more information. 490 * 491 * This function is for use by language bindings. 492 * 493 * Deprecated: Deprecated 494 * 495 * Params: 496 * nParams = the number of response ids in @new_order 497 * newOrder = an array of response ids of 498 * @dialog’s buttons 499 * 500 * Since: 2.6 501 */ 502 public void setAlternativeButtonOrder(int[] newOrder) 503 { 504 gtk_dialog_set_alternative_button_order_from_array(gtkDialog, cast(int)newOrder.length, newOrder.ptr); 505 } 506 507 /** 508 * Sets the last widget in the dialog’s action area with the given @response_id 509 * as the default widget for the dialog. Pressing “Enter” normally activates 510 * the default widget. 511 * 512 * Params: 513 * responseId = a response ID 514 */ 515 public void setDefaultResponse(int responseId) 516 { 517 gtk_dialog_set_default_response(gtkDialog, responseId); 518 } 519 520 /** 521 * Calls `gtk_widget_set_sensitive (widget, @setting)` 522 * for each widget in the dialog’s action area with the given @response_id. 523 * A convenient way to sensitize/desensitize dialog buttons. 524 * 525 * Params: 526 * responseId = a response ID 527 * setting = %TRUE for sensitive 528 */ 529 public void setResponseSensitive(int responseId, bool setting) 530 { 531 gtk_dialog_set_response_sensitive(gtkDialog, responseId, setting); 532 } 533 534 int[string] connectedSignals; 535 536 void delegate(Dialog)[] onCloseListeners; 537 /** 538 * The ::close signal is a 539 * [keybinding signal][GtkBindingSignal] 540 * which gets emitted when the user uses a keybinding to close 541 * the dialog. 542 * 543 * The default binding for this signal is the Escape key. 544 */ 545 void addOnClose(void delegate(Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 546 { 547 if ( "close" !in connectedSignals ) 548 { 549 Signals.connectData( 550 this, 551 "close", 552 cast(GCallback)&callBackClose, 553 cast(void*)this, 554 null, 555 connectFlags); 556 connectedSignals["close"] = 1; 557 } 558 onCloseListeners ~= dlg; 559 } 560 extern(C) static void callBackClose(GtkDialog* dialogStruct, Dialog _dialog) 561 { 562 foreach ( void delegate(Dialog) dlg; _dialog.onCloseListeners ) 563 { 564 dlg(_dialog); 565 } 566 } 567 568 void delegate(int, Dialog)[] onResponseListeners; 569 /** 570 * Emitted when an action widget is clicked, the dialog receives a 571 * delete event, or the application programmer calls gtk_dialog_response(). 572 * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT. 573 * Otherwise, it depends on which action widget was clicked. 574 * 575 * Params: 576 * responseId = the response ID 577 */ 578 void addOnResponse(void delegate(int, Dialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 579 { 580 if ( "response" !in connectedSignals ) 581 { 582 Signals.connectData( 583 this, 584 "response", 585 cast(GCallback)&callBackResponse, 586 cast(void*)this, 587 null, 588 connectFlags); 589 connectedSignals["response"] = 1; 590 } 591 onResponseListeners ~= dlg; 592 } 593 extern(C) static void callBackResponse(GtkDialog* dialogStruct, int responseId, Dialog _dialog) 594 { 595 foreach ( void delegate(int, Dialog) dlg; _dialog.onResponseListeners ) 596 { 597 dlg(responseId, _dialog); 598 } 599 } 600 601 /** 602 * Returns %TRUE if dialogs are expected to use an alternative 603 * button order on the screen @screen. See 604 * gtk_dialog_set_alternative_button_order() for more details 605 * about alternative button order. 606 * 607 * If you need to use this function, you should probably connect 608 * to the ::notify:gtk-alternative-button-order signal on the 609 * #GtkSettings object associated to @screen, in order to be 610 * notified if the button order setting changes. 611 * 612 * Deprecated: Deprecated 613 * 614 * Params: 615 * screen = a #GdkScreen, or %NULL to use the default screen 616 * 617 * Return: Whether the alternative button order should be used 618 * 619 * Since: 2.6 620 */ 621 public static bool alternativeDialogButtonOrder(Screen screen) 622 { 623 return gtk_alternative_dialog_button_order((screen is null) ? null : screen.getScreenStruct()) != 0; 624 } 625 }