New file: builder-wrapper.h and builder-wrapper.c
[pspp-builds.git] / src / ui / gui / entry-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011 Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/entry-dialog.h"
20
21 #include "ui/gui/builder-wrapper.h"
22 #include "ui/gui/psppire-dialog.h"
23
24 #include "gl/xalloc.h"
25
26 /* Creates a modal dialog with PARENT as its parent (this should be the
27    application window that the dialog is associated with), with TITLE as its
28    title, that prompts for a text string with PROMPT as the explanation and
29    DEFAULT_VALUE as the default value.
30
31    Returns a malloc()'d string owned by the caller if the user clicks on OK or
32    otherwise accepts a value, or NULL if the user cancels. */
33 char *
34 entry_dialog_run (GtkWindow *parent,
35                   const char *title,
36                   const char *prompt,
37                   const char *default_value)
38 {
39   GtkBuilder *xml = builder_new ("entry-dialog.ui");
40   GtkWidget *dialog;
41   GtkWidget *label;
42   GtkWidget *entry;
43   char *result;
44
45   dialog = get_widget_assert (xml, "entry-dialog");
46   gtk_window_set_title (GTK_WINDOW (dialog), title);
47   gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
48
49   label = get_widget_assert (xml, "label");
50   gtk_label_set_text (GTK_LABEL (label), prompt);
51
52   entry = get_widget_assert (xml, "entry");
53   gtk_entry_set_text (GTK_ENTRY (entry), default_value);
54
55   result = (psppire_dialog_run (PSPPIRE_DIALOG (dialog)) == GTK_RESPONSE_OK
56             ? xstrdup (gtk_entry_get_text (GTK_ENTRY (entry)))
57             : NULL);
58
59   g_object_unref (xml);
60
61   return result;
62 }