gtkdialog.c 41.8 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
24
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 26
 */

Johan Dahlin's avatar
Johan Dahlin committed
27 28
#include <stdlib.h>
#include <string.h>
29
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
30 31
#include "gtkbutton.h"
#include "gtkdialog.h"
32
#include "gtkhbbox.h"
33
#include "gtklabel.h"
34
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
35
#include "gtkvbox.h"
36 37
#include "gdkkeysyms.h"
#include "gtkmain.h"
38
#include "gtkintl.h"
39
#include "gtkbindings.h"
40
#include "gtkprivate.h"
Johan Dahlin's avatar
Johan Dahlin committed
41
#include "gtkbuildable.h"
Elliot Lee's avatar
Elliot Lee committed
42

Federico Mena Quintero's avatar
Federico Mena Quintero committed
43

44
struct _GtkDialogPrivate
45 46 47 48
{
  GtkWidget *vbox;
  GtkWidget *action_area;
};
Federico Mena Quintero's avatar
Federico Mena Quintero committed
49

50 51 52 53 54 55 56
typedef struct _ResponseData ResponseData;

struct _ResponseData
{
  gint response_id;
};

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static void      gtk_dialog_add_buttons_valist   (GtkDialog    *dialog,
                                                  const gchar  *first_button_text,
                                                  va_list       args);

static gboolean  gtk_dialog_delete_event_handler (GtkWidget    *widget,
                                                  GdkEventAny  *event,
                                                  gpointer      user_data);
static void      gtk_dialog_style_set            (GtkWidget    *widget,
                                                  GtkStyle     *prev_style);
static void      gtk_dialog_map                  (GtkWidget    *widget);

static void      gtk_dialog_close                (GtkDialog    *dialog);

static ResponseData * get_response_data          (GtkWidget    *widget,
                                                  gboolean      create);

static void      gtk_dialog_buildable_interface_init     (GtkBuildableIface *iface);
static GObject * gtk_dialog_buildable_get_internal_child (GtkBuildable  *buildable,
                                                          GtkBuilder    *builder,
                                                          const gchar   *childname);
static gboolean  gtk_dialog_buildable_custom_tag_start   (GtkBuildable  *buildable,
                                                          GtkBuilder    *builder,
                                                          GObject       *child,
                                                          const gchar   *tagname,
                                                          GMarkupParser *parser,
                                                          gpointer      *data);
static void      gtk_dialog_buildable_custom_finished    (GtkBuildable  *buildable,
                                                          GtkBuilder    *builder,
                                                          GObject       *child,
                                                          const gchar   *tagname,
                                                          gpointer       user_data);
Johan Dahlin's avatar
Johan Dahlin committed
88

89

90 91 92 93
enum {
  PROP_0,
  PROP_HAS_SEPARATOR
};
94 95 96

enum {
  RESPONSE,
97
  CLOSE,
98 99
  LAST_SIGNAL
};
Elliot Lee's avatar
Elliot Lee committed
100

101
static guint dialog_signals[LAST_SIGNAL];
Elliot Lee's avatar
Elliot Lee committed
102

Johan Dahlin's avatar
Johan Dahlin committed
103 104 105
G_DEFINE_TYPE_WITH_CODE (GtkDialog, gtk_dialog, GTK_TYPE_WINDOW,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_dialog_buildable_interface_init))
Elliot Lee's avatar
Elliot Lee committed
106 107 108 109

static void
gtk_dialog_class_init (GtkDialogClass *class)
{
110
  GObjectClass *gobject_class;
111
  GtkWidgetClass *widget_class;
112
  GtkBindingSet *binding_set;
113

114 115
  gobject_class = G_OBJECT_CLASS (class);
  widget_class = GTK_WIDGET_CLASS (class);
116

117
  widget_class->map = gtk_dialog_map;
118
  widget_class->style_set = gtk_dialog_style_set;
119 120

  class->close = gtk_dialog_close;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
121

122
  g_type_class_add_private (gobject_class, sizeof (GtkDialogPrivate));
123 124 125 126 127

  /**
   * GtkDialog::response:
   * @dialog: the object on which the signal is emitted
   * @response_id: the response ID
128 129 130 131
   *
   * Emitted when an action widget is clicked, the dialog receives a
   * delete event, or the application programmer calls gtk_dialog_response().
   * On a delete event, the response ID is #GTK_RESPONSE_DELETE_EVENT.
132 133
   * Otherwise, it depends on which action widget was clicked.
   */
134
  dialog_signals[RESPONSE] =
Matthias Clasen's avatar
Matthias Clasen committed
135
    g_signal_new (I_("response"),
Manish Singh's avatar
Manish Singh committed
136 137 138 139
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkDialogClass, response),
		  NULL, NULL,
140
		  _gtk_marshal_VOID__INT,
Manish Singh's avatar
Manish Singh committed
141 142
		  G_TYPE_NONE, 1,
		  G_TYPE_INT);
143

144 145 146
  /**
   * GtkDialog::close:
   *
147
   * The ::close signal is a
148
   * <link linkend="keybinding-signals">keybinding signal</link>
Paul Bolle's avatar
Paul Bolle committed
149
   * which gets emitted when the user uses a keybinding to close
150 151 152
   * the dialog.
   *
   * The default binding for this signal is the Escape key.
153
   */
154
  dialog_signals[CLOSE] =
Matthias Clasen's avatar
Matthias Clasen committed
155
    g_signal_new (I_("close"),
Manish Singh's avatar
Manish Singh committed
156 157 158 159
		  G_OBJECT_CLASS_TYPE (class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkDialogClass, close),
		  NULL, NULL,
160
		  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
161
		  G_TYPE_NONE, 0);
162

163
  gtk_widget_class_install_style_property (widget_class,
164
					   g_param_spec_int ("content-area-border",
165 166
                                                             P_("Content area border"),
                                                             P_("Width of border around the main dialog area"),
167 168 169
                                                             0,
                                                             G_MAXINT,
                                                             2,
170
                                                             GTK_PARAM_READABLE));
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  /**
   * GtkDialog:content-area-spacing:
   *
   * The default spacing used between elements of the
   * content area of the dialog, as returned by
   * gtk_dialog_get_content_area(), unless gtk_box_set_spacing()
   * was called on that widget directly.
   *
   * Since: 2.16
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("content-area-spacing",
                                                             P_("Content area spacing"),
                                                             P_("Spacing between elements of the main dialog area"),
                                                             0,
                                                             G_MAXINT,
                                                             0,
                                                             GTK_PARAM_READABLE));
189
  gtk_widget_class_install_style_property (widget_class,
190
                                           g_param_spec_int ("button-spacing",
191 192
                                                             P_("Button spacing"),
                                                             P_("Spacing between buttons"),
193 194
                                                             0,
                                                             G_MAXINT,
195
                                                             6,
196
                                                             GTK_PARAM_READABLE));
197

198
  gtk_widget_class_install_style_property (widget_class,
199
                                           g_param_spec_int ("action-area-border",
200 201
                                                             P_("Action area border"),
                                                             P_("Width of border around the button area at the bottom of the dialog"),
202 203
                                                             0,
                                                             G_MAXINT,
204
                                                             5,
205
                                                             GTK_PARAM_READABLE));
206 207

  binding_set = gtk_binding_set_by_class (class);
208

209
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0, "close", 0);
210 211 212 213 214
}

static void
update_spacings (GtkDialog *dialog)
{
215
  GtkDialogPrivate *priv = dialog->priv;
216
  gint content_area_border;
217
  gint content_area_spacing;
218 219 220
  gint button_spacing;
  gint action_area_border;

221
  gtk_widget_style_get (GTK_WIDGET (dialog),
222
                        "content-area-border", &content_area_border,
223
                        "content-area-spacing", &content_area_spacing,
224 225
                        "button-spacing", &button_spacing,
                        "action-area-border", &action_area_border,
226 227
                        NULL);

228
  gtk_container_set_border_width (GTK_CONTAINER (priv->vbox),
229
                                  content_area_border);
230
  if (!_gtk_box_get_spacing_set (GTK_BOX (priv->vbox)))
231
    {
232 233
      gtk_box_set_spacing (GTK_BOX (priv->vbox), content_area_spacing);
      _gtk_box_set_spacing_set (GTK_BOX (priv->vbox), FALSE);
234
    }
235
  gtk_box_set_spacing (GTK_BOX (priv->action_area),
236
                       button_spacing);
237
  gtk_container_set_border_width (GTK_CONTAINER (priv->action_area),
238
                                  action_area_border);
Elliot Lee's avatar
Elliot Lee committed
239 240 241 242 243
}

static void
gtk_dialog_init (GtkDialog *dialog)
{
244
  GtkDialogPrivate *priv;
245 246 247

  dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
                                              GTK_TYPE_DIALOG,
248
                                              GtkDialogPrivate);
249
  priv = dialog->priv;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
250

251 252 253 254
  /* To avoid breaking old code that prevents destroy on delete event
   * by connecting a handler, we have to have the FIRST signal
   * connection on the dialog.
   */
Manish Singh's avatar
Manish Singh committed
255
  g_signal_connect (dialog,
256
                    "delete-event",
Manish Singh's avatar
Manish Singh committed
257 258
                    G_CALLBACK (gtk_dialog_delete_event_handler),
                    NULL);
259

260
  priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
261 262
  gtk_container_add (GTK_CONTAINER (dialog), priv->vbox);
  gtk_widget_show (priv->vbox);
Elliot Lee's avatar
Elliot Lee committed
263

264
  priv->action_area = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
265

266
  gtk_button_box_set_layout (GTK_BUTTON_BOX (priv->action_area),
267
                             GTK_BUTTONBOX_END);
268

269
  gtk_box_pack_end (GTK_BOX (priv->vbox), priv->action_area,
270
                    FALSE, TRUE, 0);
271
  gtk_widget_show (priv->action_area);
Elliot Lee's avatar
Elliot Lee committed
272

273 274
  gtk_window_set_type_hint (GTK_WINDOW (dialog),
			    GDK_WINDOW_TYPE_HINT_DIALOG);
275
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
Elliot Lee's avatar
Elliot Lee committed
276 277
}

278 279
static GtkBuildableIface *parent_buildable_iface;

Johan Dahlin's avatar
Johan Dahlin committed
280 281 282
static void
gtk_dialog_buildable_interface_init (GtkBuildableIface *iface)
{
283
  parent_buildable_iface = g_type_interface_peek_parent (iface);
Johan Dahlin's avatar
Johan Dahlin committed
284 285 286 287 288 289 290 291 292 293
  iface->get_internal_child = gtk_dialog_buildable_get_internal_child;
  iface->custom_tag_start = gtk_dialog_buildable_custom_tag_start;
  iface->custom_finished = gtk_dialog_buildable_custom_finished;
}

static GObject *
gtk_dialog_buildable_get_internal_child (GtkBuildable *buildable,
					 GtkBuilder   *builder,
					 const gchar  *childname)
{
294
  GtkDialogPrivate *priv = GTK_DIALOG (buildable)->priv;
295 296 297 298 299 300 301 302 303

  if (strcmp (childname, "vbox") == 0)
    return G_OBJECT (priv->vbox);
  else if (strcmp (childname, "action_area") == 0)
    return G_OBJECT (priv->action_area);

  return parent_buildable_iface->get_internal_child (buildable,
                                                     builder,
                                                     childname);
Johan Dahlin's avatar
Johan Dahlin committed
304
}
305

306
static gboolean
307 308 309 310 311
gtk_dialog_delete_event_handler (GtkWidget   *widget,
                                 GdkEventAny *event,
                                 gpointer     user_data)
{
  /* emit response signal */
312
  gtk_dialog_response (GTK_DIALOG (widget), GTK_RESPONSE_DELETE_EVENT);
313 314 315 316 317

  /* Do the destroy by default */
  return FALSE;
}

318 319 320 321 322
/* A far too tricky heuristic for getting the right initial
 * focus widget if none was set. What we do is we focus the first
 * widget in the tab chain, but if this results in the focus
 * ending up on one of the response widgets _other_ than the
 * default response, we focus the default response instead.
323 324 325
 *
 * Additionally, skip selectable labels when looking for the
 * right initial focus widget.
326 327 328 329
 */
static void
gtk_dialog_map (GtkWidget *widget)
{
330
  GtkWidget *default_widget, *focus;
331 332
  GtkWindow *window = GTK_WINDOW (widget);
  GtkDialog *dialog = GTK_DIALOG (widget);
333
  GtkDialogPrivate *priv = dialog->priv;
334

Matthias Clasen's avatar
Matthias Clasen committed
335
  GTK_WIDGET_CLASS (gtk_dialog_parent_class)->map (widget);
336

337 338
  focus = gtk_window_get_focus (window);
  if (!focus)
339 340
    {
      GList *children, *tmp_list;
341
      GtkWidget *first_focus = NULL;
342 343 344 345

      do
        {
          g_signal_emit_by_name (window, "move_focus", GTK_DIR_TAB_FORWARD);
346

347
          focus = gtk_window_get_focus (window);
348 349 350 351
          if (GTK_IS_LABEL (focus) &&
              !gtk_label_get_current_uri (GTK_LABEL (focus)))
            gtk_label_select_region (GTK_LABEL (focus), 0, 0);

352
          if (first_focus == NULL)
353
            first_focus = focus;
354
          else if (first_focus == focus)
355 356
            break;

357
          if (!GTK_IS_LABEL (focus))
358
            break;
359
        }
360
      while (TRUE);
361

362
      tmp_list = children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
363

364 365 366
      while (tmp_list)
	{
	  GtkWidget *child = tmp_list->data;
367 368 369 370 371

          default_widget = gtk_window_get_default_widget (window);
	  if ((focus == NULL || child == focus) &&
	      child != default_widget &&
	      default_widget)
372
	    {
373
	      gtk_widget_grab_focus (default_widget);
374 375
	      break;
	    }
376

377 378
	  tmp_list = tmp_list->next;
	}
379

380 381 382 383
      g_list_free (children);
    }
}

384 385 386 387 388 389 390
static void
gtk_dialog_style_set (GtkWidget *widget,
                      GtkStyle  *prev_style)
{
  update_spacings (GTK_DIALOG (widget));
}

391 392 393
static GtkWidget *
dialog_find_button (GtkDialog *dialog,
		    gint       response_id)
394
{
395
  GtkDialogPrivate *priv = dialog->priv;
396
  GtkWidget *child = NULL;
397
  GList *children, *tmp_list;
398

399
  children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
400 401 402

  for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
    {
403
      ResponseData *rd = get_response_data (tmp_list->data, FALSE);
404

405
      if (rd && rd->response_id == response_id)
406
	{
407
	  child = tmp_list->data;
408 409 410 411 412 413
	  break;
	}
    }

  g_list_free (children);

414
  return child;
415 416
}

417 418
static void
gtk_dialog_close (GtkDialog *dialog)
419
{
420
  /* Synthesize delete_event to close dialog. */
421

422 423
  GtkWidget *widget = GTK_WIDGET (dialog);
  GdkEvent *event;
424

425
  event = gdk_event_new (GDK_DELETE);
426 427

  event->any.window = g_object_ref (gtk_widget_get_window (widget));
428
  event->any.send_event = TRUE;
429

430 431
  gtk_main_do_event (event);
  gdk_event_free (event);
432 433
}

Elliot Lee's avatar
Elliot Lee committed
434
GtkWidget*
435
gtk_dialog_new (void)
Elliot Lee's avatar
Elliot Lee committed
436
{
Manish Singh's avatar
Manish Singh committed
437
  return g_object_new (GTK_TYPE_DIALOG, NULL);
Elliot Lee's avatar
Elliot Lee committed
438
}
439 440 441 442 443 444 445 446

static GtkWidget*
gtk_dialog_new_empty (const gchar     *title,
                      GtkWindow       *parent,
                      GtkDialogFlags   flags)
{
  GtkDialog *dialog;

Manish Singh's avatar
Manish Singh committed
447
  dialog = g_object_new (GTK_TYPE_DIALOG, NULL);
448 449 450 451 452 453 454 455 456

  if (title)
    gtk_window_set_title (GTK_WINDOW (dialog), title);

  if (parent)
    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

  if (flags & GTK_DIALOG_MODAL)
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
457

458 459 460 461 462 463 464 465
  if (flags & GTK_DIALOG_DESTROY_WITH_PARENT)
    gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);

  return GTK_WIDGET (dialog);
}

/**
 * gtk_dialog_new_with_buttons:
466 467
 * @title: (allow-none): Title of the dialog, or %NULL
 * @parent: (allow-none): Transient parent of the dialog, or %NULL
468
 * @flags: from #GtkDialogFlags
469
 * @first_button_text: (allow-none): stock ID or text to go in first button, or %NULL
470
 * @Varargs: response ID for first button, then additional buttons, ending with %NULL
471
 *
472
 * Creates a new #GtkDialog with title @title (or %NULL for the default
473
 * title; see gtk_window_set_title()) and transient parent @parent (or
474 475
 * %NULL for none; see gtk_window_set_transient_for()). The @flags
 * argument can be used to make the dialog modal (#GTK_DIALOG_MODAL)
476
 * and/or to have it destroyed along with its transient parent
477 478
 * (#GTK_DIALOG_DESTROY_WITH_PARENT). After @flags, button
 * text/response ID pairs should be listed, with a %NULL pointer ending
479
 * the list. Button text can be either a stock ID such as
480
 * #GTK_STOCK_OK, or some arbitrary text. A response ID can be
481 482
 * any positive number, or one of the values in the #GtkResponseType
 * enumeration. If the user clicks one of these dialog buttons,
483
 * #GtkDialog will emit the #GtkDialog::response signal with the corresponding
484
 * response ID. If a #GtkDialog receives the #GtkWidget::delete-event signal,
485 486
 * it will emit ::response with a response ID of #GTK_RESPONSE_DELETE_EVENT.
 * However, destroying a dialog does not emit the ::response signal;
487
 * so be careful relying on ::response when using the
488
 * #GTK_DIALOG_DESTROY_WITH_PARENT flag. Buttons are from left to right,
489 490 491
 * so the first button in the list will be the leftmost button in the dialog.
 *
 * Here's a simple example:
Matthias Clasen's avatar
Matthias Clasen committed
492
 * |[
493 494 495 496 497 498 499 500
 *  GtkWidget *dialog = gtk_dialog_new_with_buttons ("My dialog",
 *                                                   main_app_window,
 *                                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 *                                                   GTK_STOCK_OK,
 *                                                   GTK_RESPONSE_ACCEPT,
 *                                                   GTK_STOCK_CANCEL,
 *                                                   GTK_RESPONSE_REJECT,
 *                                                   NULL);
Matthias Clasen's avatar
Matthias Clasen committed
501
 * ]|
502
 *
503 504 505 506 507 508 509 510 511 512 513
 * Return value: a new #GtkDialog
 **/
GtkWidget*
gtk_dialog_new_with_buttons (const gchar    *title,
                             GtkWindow      *parent,
                             GtkDialogFlags  flags,
                             const gchar    *first_button_text,
                             ...)
{
  GtkDialog *dialog;
  va_list args;
514

515 516 517 518 519 520 521
  dialog = GTK_DIALOG (gtk_dialog_new_empty (title, parent, flags));

  va_start (args, first_button_text);

  gtk_dialog_add_buttons_valist (dialog,
                                 first_button_text,
                                 args);
522

523 524 525 526 527
  va_end (args);

  return GTK_WIDGET (dialog);
}

528
static void
529 530 531 532 533
response_data_free (gpointer data)
{
  g_slice_free (ResponseData, data);
}

534
static ResponseData*
535 536
get_response_data (GtkWidget *widget,
		   gboolean   create)
537
{
Manish Singh's avatar
Manish Singh committed
538 539
  ResponseData *ad = g_object_get_data (G_OBJECT (widget),
                                        "gtk-dialog-response-data");
540

541
  if (ad == NULL && create)
542
    {
543
      ad = g_slice_new (ResponseData);
544

Manish Singh's avatar
Manish Singh committed
545
      g_object_set_data_full (G_OBJECT (widget),
Matthias Clasen's avatar
Matthias Clasen committed
546
                              I_("gtk-dialog-response-data"),
Manish Singh's avatar
Manish Singh committed
547
                              ad,
548
			      response_data_free);
549 550 551 552 553 554 555 556 557
    }

  return ad;
}

static void
action_widget_activated (GtkWidget *widget, GtkDialog *dialog)
{
  gint response_id;
558

559
  response_id = gtk_dialog_get_response_for_widget (dialog, widget);
560 561 562

  gtk_dialog_response (dialog, response_id);
}
Manish Singh's avatar
Manish Singh committed
563

564 565 566 567 568
/**
 * gtk_dialog_add_action_widget:
 * @dialog: a #GtkDialog
 * @child: an activatable widget
 * @response_id: response ID for @child
569
 *
570
 * Adds an activatable widget to the action area of a #GtkDialog,
571 572
 * connecting a signal handler that will emit the #GtkDialog::response
 * signal on the dialog when the widget is activated. The widget is
573
 * appended to the end of the dialog's action area. If you want to add a
574
 * non-activatable widget, simply pack it into the @action_area field
575
 * of the #GtkDialog struct.
576 577
 **/
void
Manish Singh's avatar
Manish Singh committed
578 579 580
gtk_dialog_add_action_widget (GtkDialog *dialog,
                              GtkWidget *child,
                              gint       response_id)
581
{
582
  GtkDialogPrivate *priv;
583
  ResponseData *ad;
584
  guint signal_id;
585

586 587 588
  g_return_if_fail (GTK_IS_DIALOG (dialog));
  g_return_if_fail (GTK_IS_WIDGET (child));

589 590
  priv = dialog->priv;

591
  ad = get_response_data (child, TRUE);
592 593 594

  ad->response_id = response_id;

595
  if (GTK_IS_BUTTON (child))
Manish Singh's avatar
Manish Singh committed
596
    signal_id = g_signal_lookup ("clicked", GTK_TYPE_BUTTON);
597
  else
598
    signal_id = GTK_WIDGET_GET_CLASS (child)->activate_signal;
599 600 601

  if (signal_id)
    {
Manish Singh's avatar
Manish Singh committed
602 603 604 605 606 607 608 609 610
      GClosure *closure;

      closure = g_cclosure_new_object (G_CALLBACK (action_widget_activated),
				       G_OBJECT (dialog));
      g_signal_connect_closure_by_id (child,
				      signal_id,
				      0,
				      closure,
				      FALSE);
611 612 613 614
    }
  else
    g_warning ("Only 'activatable' widgets can be packed into the action area of a GtkDialog");

615
  gtk_box_pack_end (GTK_BOX (priv->action_area),
616
                    child,
617
                    FALSE, TRUE, 0);
618

619
  if (response_id == GTK_RESPONSE_HELP)
620
    gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (priv->action_area), child, TRUE);
621 622 623 624 625 626 627
}

/**
 * gtk_dialog_add_button:
 * @dialog: a #GtkDialog
 * @button_text: text of button, or stock ID
 * @response_id: response ID for the button
628
 *
629 630
 * Adds a button with the given text (or a stock button, if @button_text is a
 * stock ID) and sets things up so that clicking the button will emit the
631 632
 * #GtkDialog::response signal with the given @response_id. The button is
 * appended to the end of the dialog's action area. The button widget is
633
 * returned, but usually you don't need it.
634
 *
635
 * Return value: (transfer full): the button widget that was added
636
 **/
637
GtkWidget*
638 639 640 641 642
gtk_dialog_add_button (GtkDialog   *dialog,
                       const gchar *button_text,
                       gint         response_id)
{
  GtkWidget *button;
643

644 645
  g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
  g_return_val_if_fail (button_text != NULL, NULL);
646

647
  button = gtk_button_new_from_stock (button_text);
648

649
  gtk_widget_set_can_default (button, TRUE);
650

651
  gtk_widget_show (button);
652

653 654 655
  gtk_dialog_add_action_widget (dialog,
                                button,
                                response_id);
656 657

  return button;
658 659 660
}

static void
Chema Celorio's avatar
Chema Celorio committed
661 662 663
gtk_dialog_add_buttons_valist (GtkDialog      *dialog,
                               const gchar    *first_button_text,
                               va_list         args)
664 665 666 667
{
  const gchar* text;
  gint response_id;

668
  g_return_if_fail (GTK_IS_DIALOG (dialog));
669

670 671
  if (first_button_text == NULL)
    return;
672

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  text = first_button_text;
  response_id = va_arg (args, gint);

  while (text != NULL)
    {
      gtk_dialog_add_button (dialog, text, response_id);

      text = va_arg (args, gchar*);
      if (text == NULL)
        break;
      response_id = va_arg (args, int);
    }
}

/**
 * gtk_dialog_add_buttons:
 * @dialog: a #GtkDialog
 * @first_button_text: button text or stock ID
 * @Varargs: response ID for first button, then more text-response_id pairs
692
 *
693
 * Adds more buttons, same as calling gtk_dialog_add_button()
694
 * repeatedly.  The variable argument list should be %NULL-terminated
695 696 697 698 699 700 701
 * as with gtk_dialog_new_with_buttons(). Each button must have both
 * text and response ID.
 **/
void
gtk_dialog_add_buttons (GtkDialog   *dialog,
                        const gchar *first_button_text,
                        ...)
702
{
703 704 705 706 707 708 709
  va_list args;

  va_start (args, first_button_text);

  gtk_dialog_add_buttons_valist (dialog,
                                 first_button_text,
                                 args);
710

711 712 713
  va_end (args);
}

714 715 716 717 718 719
/**
 * gtk_dialog_set_response_sensitive:
 * @dialog: a #GtkDialog
 * @response_id: a response ID
 * @setting: %TRUE for sensitive
 *
720
 * Calls <literal>gtk_widget_set_sensitive (widget, @setting)</literal>
Matthias Clasen's avatar
Matthias Clasen committed
721
 * for each widget in the dialog's action area with the given @response_id.
722 723 724 725 726 727 728
 * A convenient way to sensitize/desensitize dialog buttons.
 **/
void
gtk_dialog_set_response_sensitive (GtkDialog *dialog,
                                   gint       response_id,
                                   gboolean   setting)
{
729
  GtkDialogPrivate *priv;
730 731 732
  GList *children;
  GList *tmp_list;

Darin Adler's avatar
Darin Adler committed
733 734
  g_return_if_fail (GTK_IS_DIALOG (dialog));

735 736 737
  priv = dialog->priv;

  children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
738 739 740 741 742

  tmp_list = children;
  while (tmp_list != NULL)
    {
      GtkWidget *widget = tmp_list->data;
743
      ResponseData *rd = get_response_data (widget, FALSE);
744 745 746 747 748 749 750 751 752 753 754 755 756 757

      if (rd && rd->response_id == response_id)
        gtk_widget_set_sensitive (widget, setting);

      tmp_list = g_list_next (tmp_list);
    }

  g_list_free (children);
}

/**
 * gtk_dialog_set_default_response:
 * @dialog: a #GtkDialog
 * @response_id: a response ID
758
 *
759 760 761 762 763 764 765 766
 * Sets the last widget in the dialog's action area with the given @response_id
 * as the default widget for the dialog. Pressing "Enter" normally activates
 * the default widget.
 **/
void
gtk_dialog_set_default_response (GtkDialog *dialog,
                                 gint       response_id)
{
767
  GtkDialogPrivate *priv;
768 769 770
  GList *children;
  GList *tmp_list;

Darin Adler's avatar
Darin Adler committed
771 772
  g_return_if_fail (GTK_IS_DIALOG (dialog));

773 774 775
  priv = dialog->priv;

  children = gtk_container_get_children (GTK_CONTAINER (priv->action_area));
776 777 778 779 780

  tmp_list = children;
  while (tmp_list != NULL)
    {
      GtkWidget *widget = tmp_list->data;
781
      ResponseData *rd = get_response_data (widget, FALSE);
782 783

      if (rd && rd->response_id == response_id)
784
	gtk_widget_grab_default (widget);
785

786 787 788 789 790 791
      tmp_list = g_list_next (tmp_list);
    }

  g_list_free (children);
}

792 793 794
/**
 * gtk_dialog_response:
 * @dialog: a #GtkDialog
795 796 797
 * @response_id: response ID
 *
 * Emits the #GtkDialog::response signal with the given response ID.
798
 * Used to indicate that the user has responded to the dialog in some way;
799
 * typically either you or gtk_dialog_run() will be monitoring the
800
 * ::response signal and take appropriate action.
801 802 803 804 805 806 807
 **/
void
gtk_dialog_response (GtkDialog *dialog,
                     gint       response_id)
{
  g_return_if_fail (GTK_IS_DIALOG (dialog));

Manish Singh's avatar
Manish Singh committed
808 809 810 811
  g_signal_emit (dialog,
		 dialog_signals[RESPONSE],
		 0,
		 response_id);
812 813 814 815 816 817 818
}

typedef struct
{
  GtkDialog *dialog;
  gint response_id;
  GMainLoop *loop;
819
  gboolean destroyed;
820 821 822 823 824
} RunInfo;

static void
shutdown_loop (RunInfo *ri)
{
825 826
  if (g_main_loop_is_running (ri->loop))
    g_main_loop_quit (ri->loop);
827 828 829
}

static void
830
run_unmap_handler (GtkDialog *dialog, gpointer data)
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
{
  RunInfo *ri = data;

  shutdown_loop (ri);
}

static void
run_response_handler (GtkDialog *dialog,
                      gint response_id,
                      gpointer data)
{
  RunInfo *ri;

  ri = data;

  ri->response_id = response_id;

  shutdown_loop (ri);
}

static gint
run_delete_handler (GtkDialog *dialog,
                    GdkEventAny *event,
                    gpointer data)
{
  RunInfo *ri = data;
857

858
  shutdown_loop (ri);
859

860 861 862
  return TRUE; /* Do not destroy */
}

863 864 865 866 867 868
static void
run_destroy_handler (GtkDialog *dialog, gpointer data)
{
  RunInfo *ri = data;

  /* shutdown_loop will be called by run_unmap_handler */
869

870 871 872
  ri->destroyed = TRUE;
}

873 874 875
/**
 * gtk_dialog_run:
 * @dialog: a #GtkDialog
876
 *
877
 * Blocks in a recursive main loop until the @dialog either emits the
878 879 880
 * #GtkDialog::response signal, or is destroyed. If the dialog is
 * destroyed during the call to gtk_dialog_run(), gtk_dialog_run() returns
 * #GTK_RESPONSE_NONE. Otherwise, it returns the response ID from the
881 882
 * ::response signal emission.
 *
Soeren Sandmann's avatar
Soeren Sandmann committed
883
 * Before entering the recursive main loop, gtk_dialog_run() calls
884 885 886
 * gtk_widget_show() on the dialog for you. Note that you still
 * need to show any children of the dialog yourself.
 *
887
 * During gtk_dialog_run(), the default behavior of #GtkWidget::delete-event
888
 * is disabled; if the dialog receives ::delete_event, it will not be
889
 * destroyed as windows usually are, and gtk_dialog_run() will return
890
 * #GTK_RESPONSE_DELETE_EVENT. Also, during gtk_dialog_run() the dialog
891
 * will be modal. You can force gtk_dialog_run() to return at any time by
892 893
 * calling gtk_dialog_response() to emit the ::response signal. Destroying
 * the dialog during gtk_dialog_run() is a very bad idea, because your
894
 * post-run code won't know whether the dialog was destroyed or not.
895 896 897 898 899
 *
 * After gtk_dialog_run() returns, you are responsible for hiding or
 * destroying the dialog if you wish to do so.
 *
 * Typical usage of this function might be:
Matthias Clasen's avatar
Matthias Clasen committed
900
 * |[
901 902 903 904
 *   gint result = gtk_dialog_run (GTK_DIALOG (dialog));
 *   switch (result)
 *     {
 *       case GTK_RESPONSE_ACCEPT:
Matthias Clasen's avatar
Matthias Clasen committed
905
 *          do_application_specific_something ();
906 907
 *          break;
 *       default:
Matthias Clasen's avatar
Matthias Clasen committed
908
 *          do_nothing_since_dialog_was_cancelled ();
909 910 911
 *          break;
 *     }
 *   gtk_widget_destroy (dialog);
Matthias Clasen's avatar
Matthias Clasen committed
912
 * ]|
913
 *
914
 * Note that even though the recursive main loop gives the effect of a
915 916 917
 * modal dialog (it prevents the user from interacting with other
 * windows in the same window group while the dialog is run), callbacks
 * such as timeouts, IO channel watches, DND drops, etc, <emphasis>will</emphasis>
918
 * be triggered during a gtk_dialog_run() call.
919
 *
920 921 922 923 924
 * Return value: response ID
 **/
gint
gtk_dialog_run (GtkDialog *dialog)
{
925
  RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
926
  gboolean was_modal;
927 928 929 930
  gulong response_handler;
  gulong unmap_handler;
  gulong destroy_handler;
  gulong delete_handler;
931

932 933
  g_return_val_if_fail (GTK_IS_DIALOG (dialog), -1);

Manish Singh's avatar
Manish Singh committed
934
  g_object_ref (dialog);
935

936
  was_modal = gtk_window_get_modal (GTK_WINDOW (dialog));
937 938 939
  if (!was_modal)
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

940
  if (!gtk_widget_get_visible (GTK_WIDGET (dialog)))
941
    gtk_widget_show (GTK_WIDGET (dialog));
942

943
  response_handler =
Manish Singh's avatar
Manish Singh committed
944 945 946 947
    g_signal_connect (dialog,
                      "response",
                      G_CALLBACK (run_response_handler),
                      &ri);
948

949
  unmap_handler =
Manish Singh's avatar
Manish Singh committed
950 951 952 953
    g_signal_connect (dialog,
                      "unmap",
                      G_CALLBACK (run_unmap_handler),
                      &ri);
954

955
  delete_handler =
Manish Singh's avatar
Manish Singh committed
956
    g_signal_connect (dialog,
957
                      "delete-event",
Manish Singh's avatar
Manish Singh committed
958 959
                      G_CALLBACK (run_delete_handler),
                      &ri);
960