dialog.c 6.16 KB
Newer Older
1
/* Dialogs and Message Boxes
2
 *
3
 * Dialog widgets are used to pop up a transient window for user feedback.
4 5
 */

6
#include <glib/gi18n.h>
7 8
#include <gtk/gtk.h>

9 10 11
static GtkWidget *window = NULL;
static GtkWidget *entry1 = NULL;
static GtkWidget *entry2 = NULL;
12 13

static void
14
message_dialog_clicked (GtkButton *button,
Matthias Clasen's avatar
Matthias Clasen committed
15
                        gpointer   user_data)
16 17 18 19 20
{
  GtkWidget *dialog;
  static gint i = 1;

  dialog = gtk_message_dialog_new (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
21 22
                                   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                   GTK_MESSAGE_INFO,
23
                                   GTK_BUTTONS_OK_CANCEL,
Matthias Clasen's avatar
Matthias Clasen committed
24 25
                                   "This message box has been popped up the following\n"
                                   "number of times:");
26 27
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                            "%d", i);
28 29 30 31 32 33
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
  i++;
}

static void
34
interactive_dialog_clicked (GtkButton *button,
Matthias Clasen's avatar
Matthias Clasen committed
35
                            gpointer   user_data)
36
{
37
  GtkWidget *content_area;
38 39
  GtkWidget *dialog;
  GtkWidget *hbox;
40
  GtkWidget *image;
41 42 43
  GtkWidget *table;
  GtkWidget *local_entry1;
  GtkWidget *local_entry2;
44
  GtkWidget *label;
45 46 47
  gint response;

  dialog = gtk_dialog_new_with_buttons ("Interactive Dialog",
Matthias Clasen's avatar
Matthias Clasen committed
48 49
                                        GTK_WINDOW (window),
                                        GTK_DIALOG_MODAL| GTK_DIALOG_DESTROY_WITH_PARENT,
50
                                        _("_OK"),
Matthias Clasen's avatar
Matthias Clasen committed
51
                                        GTK_RESPONSE_OK,
52
                                        "_Cancel",
53
                                        GTK_RESPONSE_CANCEL,
Matthias Clasen's avatar
Matthias Clasen committed
54
                                        NULL);
55

56 57
  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

58
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
59
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 8);
60
  gtk_box_pack_start (GTK_BOX (content_area), hbox, FALSE, FALSE, 0);
61

62 63
  image = gtk_image_new_from_icon_name ("dialog-question", GTK_ICON_SIZE_DIALOG);
  gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
64

65 66 67
  table = gtk_grid_new ();
  gtk_grid_set_row_spacing (GTK_GRID (table), 4);
  gtk_grid_set_column_spacing (GTK_GRID (table), 4);
68
  gtk_box_pack_start (GTK_BOX (hbox), table, TRUE, TRUE, 0);
69
  label = gtk_label_new_with_mnemonic ("_Entry 1");
70
  gtk_grid_attach (GTK_GRID (table), label, 0, 0, 1, 1);
71 72
  local_entry1 = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (local_entry1), gtk_entry_get_text (GTK_ENTRY (entry1)));
73
  gtk_grid_attach (GTK_GRID (table), local_entry1, 1, 0, 1, 1);
74 75 76
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), local_entry1);

  label = gtk_label_new_with_mnemonic ("E_ntry 2");
77
  gtk_grid_attach (GTK_GRID (table), label, 0, 1, 1, 1);
78

79 80
  local_entry2 = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (local_entry2), gtk_entry_get_text (GTK_ENTRY (entry2)));
81
  gtk_grid_attach (GTK_GRID (table), local_entry2, 1, 1, 1, 1);
82
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), local_entry2);
83

84 85 86 87 88 89 90 91 92 93 94 95 96
  gtk_widget_show_all (hbox);
  response = gtk_dialog_run (GTK_DIALOG (dialog));

  if (response == GTK_RESPONSE_OK)
    {
      gtk_entry_set_text (GTK_ENTRY (entry1), gtk_entry_get_text (GTK_ENTRY (local_entry1)));
      gtk_entry_set_text (GTK_ENTRY (entry2), gtk_entry_get_text (GTK_ENTRY (local_entry2)));
    }

  gtk_widget_destroy (dialog);
}

GtkWidget *
97
do_dialog (GtkWidget *do_widget)
98 99 100 101 102 103 104
{
  GtkWidget *frame;
  GtkWidget *vbox;
  GtkWidget *vbox2;
  GtkWidget *hbox;
  GtkWidget *button;
  GtkWidget *table;
105
  GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
106

107 108 109
  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
110
      gtk_window_set_screen (GTK_WINDOW (window),
Matthias Clasen's avatar
Matthias Clasen committed
111
                             gtk_widget_get_screen (do_widget));
112
      gtk_window_set_title (GTK_WINDOW (window), "Dialogs and Message Boxes");
113

114 115 116
      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed), &window);

117 118 119 120 121
      gtk_container_set_border_width (GTK_CONTAINER (window), 8);

      frame = gtk_frame_new ("Dialogs");
      gtk_container_add (GTK_CONTAINER (window), frame);

122
      vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
123 124 125 126
      gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
      gtk_container_add (GTK_CONTAINER (frame), vbox);

      /* Standard message dialog */
127
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
128
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
129
      button = gtk_button_new_with_mnemonic ("_Message Dialog");
130
      g_signal_connect (button, "clicked",
Matthias Clasen's avatar
Matthias Clasen committed
131
                        G_CALLBACK (message_dialog_clicked), NULL);
132 133
      gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

134 135
      gtk_box_pack_start (GTK_BOX (vbox), gtk_separator_new (GTK_ORIENTATION_HORIZONTAL),
                          FALSE, FALSE, 0);
136 137

      /* Interactive dialog*/
138
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
139
      gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
140
      vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
141

142
      button = gtk_button_new_with_mnemonic ("_Interactive Dialog");
143
      g_signal_connect (button, "clicked",
Matthias Clasen's avatar
Matthias Clasen committed
144
                        G_CALLBACK (interactive_dialog_clicked), NULL);
145 146 147
      gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, 0);
      gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);

148 149 150
      table = gtk_grid_new ();
      gtk_grid_set_row_spacing (GTK_GRID (table), 4);
      gtk_grid_set_column_spacing (GTK_GRID (table), 4);
151
      gtk_box_pack_start (GTK_BOX (hbox), table, FALSE, FALSE, 0);
152 153

      label = gtk_label_new_with_mnemonic ("_Entry 1");
154
      gtk_grid_attach (GTK_GRID (table), label, 0, 0, 1, 1);
155 156

      entry1 = gtk_entry_new ();
157
      gtk_grid_attach (GTK_GRID (table), entry1, 1, 0, 1, 1);
158 159 160
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry1);

      label = gtk_label_new_with_mnemonic ("E_ntry 2");
161
      gtk_grid_attach (GTK_GRID (table), label, 0, 1, 1, 1);
162

163
      entry2 = gtk_entry_new ();
164
      gtk_grid_attach (GTK_GRID (table), entry2, 1, 1, 1, 1);
165 166
    }

167
  if (!gtk_widget_get_visible (window))
168
    gtk_widget_show_all (window);
169
  else
170
    gtk_widget_destroy (window);
171 172 173

  return window;
}