PIPS
xv_mchoose.c
Go to the documentation of this file.
1 /*
2 
3  $Id: xv_mchoose.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 
28 /* Multiple choices handling */
29 
30 #include <stdio.h>
31 
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <xview/xview.h>
35 #include <xview/panel.h>
36 #include <xview/notice.h>
37 #include <xview/notify.h>
38 
39 #include "genC.h"
40 #include "misc.h"
41 
42 #include "wpips.h"
43 
44 static Panel_item mchoices, choices, ok, cancel, help;
45 
46 static void (*apply_on_mchoices)(gen_array_t) = NULL;
47 static void (*cancel_on_mchoices)(void) = NULL;
48 
49 static void mchoose_help_notify(item, event)
50 Panel_item item;
51 Event *event;
52 {
53  display_help("MultipleChoice");
54 }
55 
56 void static
58  Panel_item item,
59  Event * event)
60 {
61  gen_array_t mchoices_args = gen_array_make(0);
62  char * buffer, * mchoices_notify_buffer;
63  int mchoices_length = 0;
64  int i, nchoices, len;
65  int item_is_in_the_list = FALSE;
66  char * p;
67 
68  nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, NULL);
69  mchoices_length = 0;
70 
71  mchoices_notify_buffer = strdup((char *) xv_get(mchoices, PANEL_VALUE));
72  /* Upperbound size for the scanf buffer: */
73  buffer = (char *) malloc(strlen(mchoices_notify_buffer) + 1);
74 
75  p = mchoices_notify_buffer;
76  while(sscanf(p, "%s%n", buffer, &len) == 1) {
77  gen_array_dupaddto(mchoices_args, mchoices_length++, buffer);
78  item_is_in_the_list = FALSE;
79  for(i = 0; i < nchoices; i++)
80  if (strcmp((char *)
81  xv_get(choices, PANEL_LIST_STRING, i), buffer) == 0) {
82  item_is_in_the_list = TRUE;
83  break;
84  }
85  if (item_is_in_the_list == FALSE)
86  break;
87  p += len;
88  }
89 
90  free(mchoices_notify_buffer);
91  free(buffer);
92 
93  /* At least on item selected, and in the list.
94  RK, 21/05/1993.
95  */
96  if (mchoices_length == 0 || item_is_in_the_list == FALSE) {
97  char *s;
98  s = mchoices_length == 0 ? "You have to select at least 1 item!" :
99  "You have selected an item not in the choice list!";
100  gen_array_full_free(mchoices_args);
101  prompt_user(s);
102  return;
103  }
104 
106 
107  /* The OK button becomes inactive through RETURN: */
108  xv_set(mchoose_panel, PANEL_DEFAULT_ITEM, NULL, NULL);
109  xv_set(mchoices, PANEL_NOTIFY_PROC, NULL);
110 
111  (*apply_on_mchoices)(mchoices_args);
112  gen_array_full_free(mchoices_args);
113 
114  /* Delay the graphics transformations. RK, 21/05/1993. */
115 
116  /* Delete all the rows, ie nchoices rows from row 0: */
117  xv_set(choices,
118  PANEL_LIST_DELETE_ROWS, 0, nchoices,
119  NULL);
120 
121  xv_set(mchoices, PANEL_VALUE, "", NULL);
122 }
123 
124 
125 void static
126 mchoose_cancel_notify(Panel_item item,
127  Event * event)
128 {
130 
131  /* The OK button becomes inactive through RETURN: */
132  xv_set(mchoose_panel, PANEL_DEFAULT_ITEM, NULL, NULL);
133  xv_set(mchoices, PANEL_NOTIFY_PROC, NULL);
134 
135  (*cancel_on_mchoices)();
136 
137  /* Delete all the rows, ie nchoices rows from row 0: */
138  xv_set(choices,
139  PANEL_LIST_DELETE_ROWS, 0, (int) xv_get(choices,
140  PANEL_LIST_NROWS,
141  NULL),
142  NULL);
143 
144  xv_set(mchoices, PANEL_VALUE, "", NULL);
145 }
146 
147 
148 /* Avoid the mchoose_frame destruction and act as cancel: */
149 void static
151 {
152  mchoose_cancel_notify((Panel_item)NULL, (Event*)NULL);
153 }
154 
155 
156 /*
157 Notify_value
158 try_to_avoid_mchoose_destruction(Notify_client client,
159  Destroy_status status)
160 {
161  fprintf(stderr, "%x, %x\n", client, status);
162 
163  if (status == DESTROY_CHECKING) {
164  notify_veto_destroy(client);
165  prompt_user("You are not allowed to destroy the Mchoose frame !");
166  }
167  else if (status == DESTROY_CLEANUP)
168  return notify_next_destroy_func(client, status);
169 
170  return NOTIFY_DONE;
171 }
172 */
173 
174 
175 /* Function used to update the text panel according to the list panel: */
176 int static
177 mchoose_notify(Panel_item item,
178  char * item_string,
179  Xv_opaque client_data,
180  Panel_list_op op,
181  Event * event,
182  int row)
183 {
184  switch (op) {
185  case PANEL_LIST_OP_SELECT:
186  case PANEL_LIST_OP_DESELECT:
187  {
188  int i;
189  int nchoices = (int) xv_get(choices, PANEL_LIST_NROWS);
190 
191  /* Now it is mchoices_notify_buffer which is used for the selection.
192  No size verification implemented yet... :-)
193  RK, 19/05/1993. */
194  /* Make the PANEL_VALUE of mchoices a string that is all the
195  names of the selected files: */
196  xv_set(mchoices, PANEL_VALUE, "", NULL);
197 
198  for(i = 0; i < nchoices; i++) {
199  if ((int) xv_get(choices, PANEL_LIST_SELECTED, i) == TRUE) {
200  xv_set(mchoices, PANEL_VALUE,
201  concatenate((char *) xv_get(mchoices, PANEL_VALUE),
202  (char *) xv_get(choices,
203  PANEL_LIST_STRING, i),
204  " ",
205  NULL),
206  NULL);
207  }
208  }
209  break;
210  }
211 
212  /* Avoid deletion and insertion with the edit menu of button 3: */
213  case PANEL_LIST_OP_DELETE:
214  case PANEL_LIST_OP_VALIDATE:
215  return XV_ERROR;
216 
217  default:
218  pips_assert("schoose_choice_notify: unknown operation !", 0);
219  }
220 
221  /* Accept the operation by default: */
222  return XV_OK;
223 }
224 
225 
226 /* When we press on the "(De)Select" all button, select or deselect
227  all the items. */
228 void static
230  Event * event)
231 {
232  int i;
233  static bool select_all_when_press_this_button = TRUE;
234 
235  int nchoices = (int) xv_get(choices, PANEL_LIST_NROWS);
236 
237  for(i = nchoices - 1; i >= 0; i--)
238  xv_set (choices,
239  PANEL_LIST_SELECT, i, select_all_when_press_this_button,
240  NULL);
241 
242  /* Update the "Current choices": */
243  (void) mchoose_notify((Panel_item)NULL, NULL,
244  (Xv_opaque)NULL, PANEL_LIST_OP_SELECT,
245  (Event*) NULL, 0);
246 
247  /* Next time we press this button, do the opposite: */
248  select_all_when_press_this_button = !select_all_when_press_this_button;
249 }
250 
251 
252 void
253 mchoose(char * title,
255  void (*function_ok)(gen_array_t),
256  void (*function_cancel)(void))
257 {
258  int i, nchoices, argc = gen_array_nitems(array);
259 
260  apply_on_mchoices = function_ok;
261  cancel_on_mchoices = function_cancel;
262 
263  xv_set(mchoose_frame, FRAME_LABEL, title, NULL);
264 
265  /* reset the choice set to empty */
266  nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, 0);
267 
268  /* Delete all the rows, ie nchoices rows from row 0: */
269  xv_set(choices,
270  PANEL_LIST_DELETE_ROWS, 0, nchoices,
271  NULL);
272 
273  for (i = 0; i < argc; i++) {
274  string mn = gen_array_item(array, i);
275  xv_set(choices, PANEL_LIST_STRING, i, mn, NULL);
276  }
277 
279 
280  /* move the pointer to the center of the query window */
282 
283  /* The OK button becomes active through RETURN: */
284  xv_set(mchoose_panel, PANEL_DEFAULT_ITEM, ok, NULL);
285  xv_set(mchoices, PANEL_NOTIFY_PROC, mchoose_ok_notify, NULL);
286 }
287 
288 
289 void
291 {
292  mchoose_panel = xv_create(mchoose_frame, PANEL, NULL);
293 
294 /* Carriage return enable for "OK". RK, 19/05/1993. */
295 
296  mchoices = xv_create(mchoose_panel, PANEL_TEXT,
297  PANEL_LABEL_STRING, "Current choices",
298  PANEL_VALUE_DISPLAY_LENGTH, 30,
299  PANEL_VALUE_STORED_LENGTH, 12800,
300  /* PANEL_NOTIFY_PROC, mchoose_ok_notify, */
301  XV_X, xv_col(mchoose_panel, 0),
302  NULL);
303 
304  choices = xv_create(mchoose_panel, PANEL_LIST,
305  PANEL_LABEL_STRING, "Available choices",
306  PANEL_LIST_DISPLAY_ROWS, 5,
307  PANEL_NOTIFY_PROC, mchoose_notify,
308  PANEL_CHOOSE_ONE, FALSE,
309  XV_X, xv_col(mchoose_panel, 0),
310  XV_Y, xv_rows(mchoose_panel, 1),
311  NULL);
312 
313  ok = xv_create(mchoose_panel, PANEL_BUTTON,
314  PANEL_LABEL_STRING, "OK",
315  PANEL_NOTIFY_PROC, mchoose_ok_notify,
316  XV_X, xv_col(mchoose_panel, 5),
317  XV_Y, xv_rows(mchoose_panel, 5),
318  NULL);
319 
320  (void) xv_create(mchoose_panel, PANEL_BUTTON,
321  PANEL_LABEL_STRING, "(De)Select all",
322  PANEL_NOTIFY_PROC, mchoose_de_select_all_notify,
323  NULL);
324 
325  cancel = xv_create(mchoose_panel, PANEL_BUTTON,
326  PANEL_LABEL_STRING, "Cancel",
327  PANEL_NOTIFY_PROC, mchoose_cancel_notify,
328  NULL);
329 
330  help = xv_create(mchoose_panel, PANEL_BUTTON,
331  PANEL_LABEL_STRING, "Help",
332  PANEL_NOTIFY_PROC, mchoose_help_notify,
333  NULL);
334 
335  window_fit(mchoose_panel);
336  window_fit(mchoose_frame);
337 
338  /*
339  Just to veto the xv_destroy():
340  (void) notify_interpose_destroy_func(mchoose_frame,
341  try_to_avoid_mchoose_destruction);
342  xv_set((Window) xv_get(mchoose_frame, XV_XID)
343  */
344  /* Avoid the mchoose_frame destruction: */
345  xv_set(mchoose_frame,
346  FRAME_DONE_PROC, mchoose_frame_done_proc,
347  NULL);
348 }
void const char const char const int
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void gen_array_full_free(gen_array_t a)
Definition: array.c:77
gen_array_t gen_array_make(size_t size)
declarations...
Definition: array.c:40
void gen_array_dupaddto(gen_array_t a, size_t i, void *what)
Definition: array.c:111
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
void * malloc(YYSIZE_T)
void free(void *)
GtkWidget * mchoose_frame
Definition: gpips-local.h:42
void display_help(char *topic)
Definition: gtk_help.c:50
void prompt_user(string a_printf_format,...)
Definition: gtk_log.c:66
gint hide_window(GtkWidget *window, GdkEvent *ev __attribute__((unused)), gpointer data __attribute__((unused)))
Definition: gtk_utils.c:89
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
struct _gen_array_chunk_t * gen_array_t
Definition: newgen_array.h:24
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
char * strdup()
static entity array
static string buffer
Definition: string.c:113
Panel mchoose_panel
Definition: wpips-local.h:49
static void mchoose_cancel_notify(Panel_item item, Event *event)
Definition: xv_mchoose.c:126
static void mchoose_help_notify(Panel_item item, Event *event)
Definition: xv_mchoose.c:49
static Panel_item cancel
Definition: xv_mchoose.c:44
static Panel_item help
Definition: xv_mchoose.c:44
static Panel_item choices
Definition: xv_mchoose.c:44
static void(* apply_on_mchoices)(gen_array_t)
Definition: xv_mchoose.c:46
static void mchoose_frame_done_proc(Frame frame)
Avoid the mchoose_frame destruction and act as cancel:
Definition: xv_mchoose.c:150
static void mchoose_ok_notify(Panel_item item, Event *event)
Definition: xv_mchoose.c:57
static void mchoose_de_select_all_notify(Panel_item item, Event *event)
When we press on the "(De)Select" all button, select or deselect all the items.
Definition: xv_mchoose.c:229
static void(* cancel_on_mchoices)(void)
Definition: xv_mchoose.c:47
void create_mchoose_window()
Definition: xv_mchoose.c:290
static Panel_item ok
Definition: xv_mchoose.c:44
static int mchoose_notify(Panel_item item, char *item_string, Xv_opaque client_data, Panel_list_op op, Event *event, int row)
Function used to update the text panel according to the list panel:
Definition: xv_mchoose.c:177
static Panel_item mchoices
Multiple choices handling.
Definition: xv_mchoose.c:44
void mchoose(char *title, gen_array_t array, void(*function_ok)(gen_array_t), void(*function_cancel)(void))
Definition: xv_mchoose.c:253
void unhide_window(Frame frame)
map a frame on the screen
Definition: xv_utils.c:55
void pointer_in_center_of_frame(Frame frame)
Centre la souris sur une fene^tre :
Definition: xv_utils.c:64