PIPS
gtk_query.c
Go to the documentation of this file.
1 /*
2 
3  $Id: gtk_query.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 
34 #include "genC.h"
35 #include "misc.h"
36 
37 #undef test_undefined // also defined in glib included from gtk
38 #include <gtk/gtk.h>
39 #include "gpips.h"
40 
41 static GtkWidget * query_entry, * query_entry_label;
42 static GtkWidget * query_cancel_button;
43 
44 static char *query_help_topic;
45 static success (*apply_on_query)(const char *);
46 
47 void start_query(char * window_title, char * query_title, char * help_topic,
48  success(* ok_func)(const char * ), void(* cancel_func)( GtkWidget*, gpointer)) {
49 
50  gtk_window_set_title(GTK_WINDOW(query_dialog), window_title);
51 
52  /* PANEL_NOTIFY_PROC, cancel_query_notify, */
53  gtk_widget_set_sensitive(GTK_WIDGET(query_cancel_button), TRUE);
54  if (cancel_func == NULL)
55  /* No cancel button requested: */
56  gtk_widget_set_sensitive(GTK_WIDGET(query_cancel_button), FALSE);
57  else
58  gtk_signal_connect(GTK_OBJECT(query_cancel_button), "clicked",
59  GTK_SIGNAL_FUNC(cancel_func), NULL);
60 
61  gtk_label_set_text(GTK_LABEL(query_entry_label), query_title);
62  gtk_entry_set_text(GTK_ENTRY(query_entry), "");
63 
64  query_help_topic = help_topic;
65  apply_on_query = ok_func;
66 
67  gtk_widget_show_all(query_dialog);
68 }
69 
70 // don't know the purpose of this code...
71 
72 //void query_canvas_event_proc(window, event)
73 // Xv_Window window;Event *event; {
74 // debug_on("WPIPS_EVENT_DEBUG_LEVEL");
75 // debug(2, "query_canvas_event_proc", "Event_id %d, event_action %d\n",
76 // event_id(event), event_action(event));
77 // debug_off();
78 // switch (event_id(event)) {
79 // case LOC_WINENTER:
80 // /* enter_window(window); */
81 // break;
82 // case '\r':
83 // /* ie. return key pressed */
84 // if (event_is_up(event))
85 // /* ie. key is released. It is necessary to use this event
86 // because notice_prompt() (in prompt_user() (in
87 // end_query_notify() )) also returns on up RETURN.
88 // This can cause the notice to return immediately when it is
89 // called on down RETURN.
90 // There schould be another possibility: put a mask to ignore
91 // key release events on the window which owns notice_prompt().
92 // This was done in create_main_window() but seems without
93 // effect.
94 // */
95 // end_query_notify(NULL, event);
96 // break;
97 // default:
98 // ;
99 // }
100 //}
101 
102 void end_query_notify(GtkWidget * widget, gpointer data) {
103  const char * s = gtk_entry_get_text(GTK_ENTRY(query_entry));
104  if (s == NULL)
105  s = strdup("");
106  else
107  s = strdup(s);
108 
109  if (apply_on_query(s))
110  hide_window(query_dialog, NULL, NULL);
111 }
112 
113 void help_query_notify(GtkWidget * widget, gpointer data) {
115 }
116 
117 /* hides a window... */
118 void cancel_query_notify(GtkWidget * widget, gpointer data) {
119  hide_window(query_dialog, NULL, NULL);
120 }
121 
122 /* Cancel clear the string value and return: */
123 void cancel_user_request_notify(GtkWidget * widget, gpointer data) {
124  gtk_entry_set_text(GTK_ENTRY(query_entry), "");
125  hide_window(query_dialog, NULL, NULL);
126  /* Just return the "": */
127  gtk_dialog_response(GTK_DIALOG(query_dialog), 1);
128 }
129 
130 success end_user_request_notify(const char * the_answer) {
131  hide_window(query_dialog, NULL, NULL);
132  gtk_dialog_response(GTK_DIALOG(query_dialog), 1);
133  return TRUE;
134 }
135 
136 string gpips_user_request(const char * a_printf_format, va_list * args) {
137 
138  /* char * the_answer; */
139 
140  static char message_buffer[SMALL_BUFFER_LENGTH];
141 
142  va_list acpy;
143  va_copy(acpy, *args);
144  vsprintf(message_buffer, a_printf_format, acpy);
145  va_end(acpy);
146 
147  start_query("User Query", message_buffer, "UserQuery",
149 
150  user_log("User Request...\n");
151 
152  gtk_widget_show(query_dialog);
153  gtk_window_set_modal(GTK_WINDOW(query_dialog), TRUE);
154  gtk_dialog_run(GTK_DIALOG(query_dialog)); // On force l'attente de la réponse
155  gtk_window_set_modal(GTK_WINDOW(query_dialog), FALSE);
156 
157  /* Log the answer for possible rerun through tpips: */
158  user_log("%s\n\"%s\"\nEnd User Request\n", message_buffer, "" /*the_answer*/);
159 
160  return strdup(gtk_entry_get_text(GTK_ENTRY(query_entry)));
161 }
162 
164  GtkWidget *help_button, *ok_button;
165  //GtkWidget *action_area;
166  GtkWidget *content_area;
167 
168  /* seems it has no use. RK, 9/11/93. */
169  // xv_set(canvas_paint_window(query_panel), WIN_CONSUME_EVENT, LOC_WINENTER,
170  // NULL,
171  // /* WIN_IGNORE_X_EVENT_MASK, KeyReleaseMask, */
172  // WIN_EVENT_PROC, query_canvas_event_proc, NULL);
173 
174  //action_area = gtk_dialog_get_action_area(query_dialog);
175  content_area = gpips_gtk_dialog_get_content_area(GTK_DIALOG(query_dialog));
176 
177  GtkWidget * hbox = gtk_hbox_new(FALSE,0);
178  query_entry_label = gtk_label_new(NULL);
179  query_entry = gtk_entry_new_with_max_length(128);
180  gtk_box_pack_start(GTK_BOX(hbox), query_entry_label, FALSE, FALSE, 5);
181  gtk_box_pack_start(GTK_BOX(hbox), query_entry, FALSE, FALSE, 5);
182  gtk_container_add(GTK_CONTAINER(content_area), hbox);
183  gtk_widget_show_all(hbox);
184 
185  ok_button = gtk_button_new_with_label("OK");
186  gtk_dialog_add_action_widget(GTK_DIALOG(query_dialog), ok_button, GTK_RESPONSE_OK);
187  gtk_signal_connect(GTK_OBJECT(ok_button), "clicked", GTK_SIGNAL_FUNC(
188  end_query_notify), NULL);
189  gtk_window_set_default(GTK_WINDOW(query_dialog), ok_button);
190  //gtk_container_add(GTK_CONTAINER(action_area), ok_button);
191 
192  help_button = gtk_button_new_with_label("Help");
193  gtk_dialog_add_action_widget(GTK_DIALOG(query_dialog), help_button, GTK_RESPONSE_HELP);
194  gtk_signal_connect(GTK_OBJECT(help_button), "clicked", GTK_SIGNAL_FUNC(
195  help_query_notify), NULL);
196  //gtk_container_add(GTK_CONTAINER(action_area), help_button);
197 
198  query_cancel_button = gtk_button_new_with_label("Cancel");
199  gtk_dialog_add_action_widget(GTK_DIALOG(query_dialog), query_cancel_button, GTK_RESPONSE_CANCEL);
200  //gtk_container_add(GTK_CONTAINER(action_area), query_cancel_button);
201 
202  gtk_widget_show_all(query_dialog);
203  gtk_widget_hide(query_dialog);
204 }
void user_log(const char *format,...)
Definition: message.c:234
GtkWidget * query_dialog
Definition: gpips-local.h:37
#define SMALL_BUFFER_LENGTH
Definition: gpips-local.h:24
bool success
Definition: gpips-local.h:59
void display_help(char *topic)
Definition: gtk_help.c:50
static GtkWidget * ok_button
Definition: gtk_mchoose.c:43
static GtkWidget * help_button
Definition: gtk_mchoose.c:44
static char * query_help_topic
Definition: gtk_query.c:44
void create_query_window()
Definition: gtk_query.c:163
static GtkWidget * query_entry
Definition: gtk_query.c:41
void cancel_user_request_notify(GtkWidget *widget, gpointer data)
Cancel clear the string value and return:
Definition: gtk_query.c:123
void help_query_notify(GtkWidget *widget, gpointer data)
Definition: gtk_query.c:113
void end_query_notify(GtkWidget *widget, gpointer data)
Definition: gtk_query.c:102
void start_query(char *window_title, char *query_title, char *help_topic, success(*ok_func)(const char *), void(*cancel_func)(GtkWidget *, gpointer))
Definition: gtk_query.c:47
static GtkWidget * query_cancel_button
Definition: gtk_query.c:42
static success(* apply_on_query)(const char *)
Definition: gtk_query.c:45
success end_user_request_notify(const char *the_answer)
Definition: gtk_query.c:130
void cancel_query_notify(GtkWidget *widget, gpointer data)
hides a window...
Definition: gtk_query.c:118
string gpips_user_request(const char *a_printf_format, va_list *args)
Definition: gtk_query.c:136
static GtkWidget * query_entry_label
Definition: gtk_query.c:41
gint hide_window(GtkWidget *window, GdkEvent *ev __attribute__((unused)), gpointer data __attribute__((unused)))
Definition: gtk_utils.c:89
GtkWidget * gpips_gtk_dialog_get_content_area(GtkDialog *dialog)
Definition: gtk_utils.c:79
char * strdup()