1 /***********************************************************************
2  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 ***********************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
17 
18 #include <stdio.h>
19 
20 #include <gtk/gtk.h>
21 
22 /* utility */
23 #include "fcintl.h"
24 #include "log.h"
25 #include "mem.h"
26 
27 /* client/gui-gtk-3.22 */
28 #include "gui_main.h"
29 #include "gui_stuff.h"
30 
31 #include "inputdlg.h"
32 
33 struct input_dialog_data {
34   input_dialog_callback_t response_callback;
35   gpointer response_cli_data;
36 };
37 
38 /**************************************************************************
39   Called when user dismisses dialog -- either to accept or to cancel.
40 **************************************************************************/
input_dialog_response(GtkDialog * shell,gint response,gpointer data)41 static void input_dialog_response(GtkDialog *shell, gint response,
42                                   gpointer data)
43 {
44   GtkWidget *winput = g_object_get_data(G_OBJECT(shell), "iinput");
45   struct input_dialog_data *cb = data;
46 
47   cb->response_callback(cb->response_cli_data,
48                         response, gtk_entry_get_text(GTK_ENTRY(winput)));
49 
50   /* Any response is final */
51   gtk_widget_destroy(GTK_WIDGET(shell));
52   FC_FREE(cb);
53 }
54 
55 /**************************************************************************
56   Called when user closes dialog with key (Esc).
57 **************************************************************************/
input_dialog_close(GtkDialog * shell,gpointer data)58 static void input_dialog_close(GtkDialog *shell, gpointer data)
59 {
60   input_dialog_response(shell, GTK_RESPONSE_CANCEL, data);
61 }
62 
63 /**************************************************************************
64   Create a popup with a text entry box and "OK" and "Cancel" buttons.
65 **************************************************************************/
input_dialog_create(GtkWindow * parent,const char * dialogname,const char * text,const char * postinputtest,input_dialog_callback_t response_callback,gpointer response_cli_data)66 GtkWidget *input_dialog_create(GtkWindow *parent, const char *dialogname,
67                                const char *text, const char *postinputtest,
68                                input_dialog_callback_t response_callback,
69                                gpointer response_cli_data)
70 {
71   GtkWidget *shell, *label, *input;
72   struct input_dialog_data *cb = fc_malloc(sizeof(struct input_dialog_data));
73 
74   cb->response_callback = response_callback;
75   cb->response_cli_data = response_cli_data;
76 
77   shell = gtk_dialog_new_with_buttons(dialogname,
78                                       parent,
79                                       GTK_DIALOG_DESTROY_WITH_PARENT,
80                                       _("_Cancel"), GTK_RESPONSE_CANCEL,
81                                       _("_OK"), GTK_RESPONSE_OK,
82                                       NULL);
83   gtk_dialog_set_default_response(GTK_DIALOG(shell), GTK_RESPONSE_OK);
84   setup_dialog(shell, GTK_WIDGET(parent));
85   g_signal_connect(shell, "response", G_CALLBACK(input_dialog_response), cb);
86   g_signal_connect(shell, "close", G_CALLBACK(input_dialog_close), cb);
87   gtk_window_set_position(GTK_WINDOW(shell), GTK_WIN_POS_CENTER_ON_PARENT);
88 
89   label = gtk_frame_new(text);
90   /* Should use gtk_dialog_get_content_area() instead of ->vbox, but that
91    * requires at least gtk+-2.14.0 */
92   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(shell))), label, TRUE, TRUE, 0);
93 
94   input = gtk_entry_new();
95   gtk_container_add(GTK_CONTAINER(label), input);
96   gtk_entry_set_text(GTK_ENTRY(input), postinputtest);
97   gtk_entry_set_activates_default(GTK_ENTRY(input), TRUE);
98   g_object_set_data(G_OBJECT(shell), "iinput", input);
99 
100   gtk_widget_show_all(GTK_WIDGET(shell));
101   gtk_window_present(GTK_WINDOW(shell));
102 
103   return shell;
104 }
105