PIPS
xv_schoose2.c
Go to the documentation of this file.
1 /*
2 
3  $Id: xv_schoose2.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 /* Single choice handling */
28 
29 /* Difference with previous release:
30  1/ schoose_close() must be called in order to close the schoose window.
31  2/ schoose() has one more argument, because cancel button is created.
32  bb 04.06.91
33  */
34 
35 #include <stdio.h>
36 
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <xview/xview.h>
40 #include <xview/panel.h>
41 #include <xview/text.h>
42 
43 #include "genC.h"
44 #include "misc.h"
45 
46 #include "wpips.h"
47 
48 enum
49 {
52 };
53 
54 static Panel_item choice, choices, ok, help, cancel;
55 
56 static void (* apply_on_choice)(char *);
57 static void (* apply_on_cancel)(void);
58 
59 void static
60 schoose_help_notify(Panel_item item,
61  Event * event)
62 {
63  display_help("SingleChoice");
64 }
65 
66 /* Cette routine est appelé d'une part lorsqu'on a cliqué sur OK pour
67  valider un nom tapé textuellement, d'autre part lorsqu'on clique sur
68  un choix. */
69 void static
70 schoose_ok_notify(Panel_item item,
71  Event * event)
72 {
73  /* Suppose qu'item et event sont folklo car on peut être appelé par
74  schoose_choice_notify */
75  char *curchoice;
76  int i, nchoices;
77  int item_is_in_the_list;
78 
79  curchoice = strdup((char *) xv_get(choice, PANEL_VALUE, 0));
80  if (strlen(curchoice)==0)
81  prompt_user("Choose one item or cancel");
82  else {
83  /* Modified to verify that an correct item is selected.
84  RK, 21/05/1993. */
85  nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, 0);
86  item_is_in_the_list = FALSE;
87  for(i = 0; i < nchoices; i++)
88  if (strcmp((char *)xv_get(choices, PANEL_LIST_STRING, i),
89  curchoice) == 0) {
90  item_is_in_the_list = TRUE;
91  break;
92  }
93  if (item_is_in_the_list == FALSE)
94  prompt_user("You have to choose one item of the list!");
95  else {
96  /* Normal case : */
97  (*apply_on_choice)(curchoice);
98  schoose_close();
99  }
100  }
101 
102  free(curchoice);
103 }
104 
105 /* schoose_close() can be called even when schoose window is already closed.
106  */
107 void
109 {
110  int nchoices;
111 
113 
114  nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, NULL);
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(choice, PANEL_VALUE, "", NULL);
122 }
123 
124 
125 void
126 schoose_cancel_notify(Panel_item item,
127  Event * event)
128 {
129  schoose_close();
130 
131  (*apply_on_cancel)();
132 }
133 
134 /* Function used to update the text panel according to the list panel: */
135 int static
136 schoose_choice_notify(Panel_item item,
137  char * item_string,
138  Xv_opaque client_data,
139  Panel_list_op op,
140  Event * event,
141  int row)
142 {
143  switch (op) {
144  case PANEL_LIST_OP_SELECT:
145  xv_set(choice,
146  PANEL_VALUE, item_string,
147  NULL);
148  break;
149 
150  /* Avoid deletion and insertion with the edit menu of button 3: */
151  case PANEL_LIST_OP_DELETE:
152  case PANEL_LIST_OP_VALIDATE:
153  return XV_ERROR;
154 
155  case PANEL_LIST_OP_DESELECT:
156  break;
157 
158  default:
159  pips_assert("schoose_choice_notify: unknown operation !", 0);
160  }
161 
162  /* Accept the operation by default: */
163  return XV_OK;
164 }
165 
166 
167 /* Avoid the schoose_frame destruction and act as cancel: */
168 void static
170 {
171  (*apply_on_cancel)();
172  hide_window(frame);
173 }
174 
175 
176 void
177 schoose(char * title,
179  char * initial_choice,
180  void (*function_for_ok)(char *),
181  void (*function_for_cancel)(void))
182 {
183  int i;
184  int nchoices;
185  int argc = gen_array_nitems(array);
186 
187  apply_on_choice = function_for_ok;
188  apply_on_cancel = function_for_cancel;
189 
190  xv_set(schoose_frame, FRAME_LABEL, title, NULL);
191 
192  /* reset the choice set to empty */
193  nchoices = (int) xv_get(choices, PANEL_LIST_NROWS, 0);
194 
195  /* Delete all the rows, ie nchoices rows from row 0: */
196  xv_set(choices,
197  PANEL_LIST_DELETE_ROWS, 0, nchoices,
198  NULL);
199 
200  for (i = 0; i < argc; i++) {
201  string name = gen_array_item(array, i);
202  xv_set(choices, PANEL_LIST_STRING, i, name, NULL);
203  }
204 
205  /* Initialise au choix initial ou à défaut le premier : */
206  xv_set(choice, PANEL_VALUE, gen_array_item(array, 0), NULL);
207  if (initial_choice != NULL)
208  {
209  for (i = 0; i < argc; i++)
210  {
211  string name = gen_array_item(array, i);
212  if (strcmp(initial_choice, name) == 0) {
213  xv_set(choice, PANEL_VALUE, name, NULL);
214  xv_set(choices, PANEL_LIST_SELECT, i, TRUE, NULL);
215  break;
216  }
217  }
218  }
219 
221  /* move the pointer to the center of the query window */
223 }
224 
225 
226 /* Accept only typed text in the menu list: */
227 static void
229  /* int Value, ? */
230  Event * event)
231 {
232  void (* real_user_notify_function)(char *);
233 
234  char * text = (char *) xv_get(text_item, PANEL_VALUE);
235 
236  debug_on("WPIPS_DEBUG_LEVEL");
237  debug(9, "schoose_abbrev_menu_with_text_text_notify", "Entering ...\n");
238 
239  real_user_notify_function =
240  (void (*)(char *)) xv_get(text_item, XV_KEY_DATA,
242  real_user_notify_function(text);
243 
244  debug_off();
245 }
246 
247 
248 /* The function that calls the real user notifying function: */
249 static void
250 abbrev_menu_with_text_menu_notify(Menu menu, Menu_item menu_item)
251 {
252  void (* real_user_notify_function)(char *);
253  char * menu_choice = (char *) xv_get(menu_item, MENU_STRING);
254 
255  debug_on("WPIPS_DEBUG_LEVEL");
256  debug(9, "abbrev_menu_with_text_menu_notify", "Entering...\n");
257 
258  real_user_notify_function =
259  (void (*)(char *)) xv_get(menu, MENU_CLIENT_DATA);
260 
261  real_user_notify_function(menu_choice);
262 
263  debug_off();
264 }
265 
266 
267 static Notify_value
269  Event * event,
270  Notify_arg arg,
271  Notify_event_type type)
272 {
273  Panel_item item;
274  Rect * rect;
275  Menu (* generate_menu)(void);
276 
277  debug_on("WPIPS_DEBUG_LEVEL");
278  debug(9, "abbrev_menu_event_filter_proc", "Entering ...\n");
279 
280  /* See example p. 675 in the XView Programming Manual: */
281  if (event_is_down(event)) {
282  /* Find the Panel_item */
283  PANEL_EACH_ITEM(panel, item)
284  {
285  rect = (Rect *) xv_get(item, XV_RECT);
286  if (rect_includespoint(rect,
287  event->ie_locx,
288  event->ie_locy)) {
289  generate_menu =
290  (Menu (* )()) xv_get(item,
291  XV_KEY_DATA,
293 
294  if (generate_menu != NULL) {
295  /* OK, we clicked on a abbrev_menu_with_text menu: */
296  Menu new_menu;
297  void (* a_menu_notify_procedure)(Menu, Menu_item);
298  /* If there is an old menu, remove it: */
299  Menu old_menu = (Menu) xv_get(item, PANEL_ITEM_MENU);
300 
301  debug(9, "abbrev_menu_event_filter_proc",
302  "OK, we clicked on a abbrev_menu_with_text menu.\n");
303 
304  if (old_menu != NULL)
305  xv_destroy(old_menu);
306 
307  /* Create the new menu: */
308  new_menu = generate_menu();
309 
310  a_menu_notify_procedure = (void (*)(Menu, Menu_item)) xv_get(new_menu, MENU_NOTIFY_PROC);
311  /* menu_return_value() seems to be the default
312  MENU_NOTIFY_PROC in XView... Hum, internal
313  details... */
314  /* Quite strange: with gcc without -static on
315  SunOS4.1.4, this test is never true... :-( Well,
316  remove this micro-optimization and always
317  reinstall the MENU_NOTIFY_PROC: */
318  /*
319  if (a_menu_notify_procedure == menu_return_value) {
320  */
321  /* The new_menu has not attached a notify
322  procedure. Get the one given at creation time
323  of the panel: */
324  xv_set(new_menu, MENU_NOTIFY_PROC,
326  NULL);
327  debug(9, "abbrev_menu_event_filter_proc",
328  "Attaching abbrev_menu_with_text_menu_notify...\n");
329  /*
330  }
331  */
332 
333 
334  {
335  /* Associate the real notify function to the menu too: */
336  void (* after_selection)(char *);
337 
338  after_selection = (void (*)(char *))
339  xv_get(item,
340  XV_KEY_DATA,
342 
343  xv_set(new_menu,
344  MENU_CLIENT_DATA,
345  after_selection,
346  NULL);
347  }
348  xv_set(item, PANEL_ITEM_MENU, new_menu);
349  }
350  }
351  }
352  PANEL_END_EACH
353  }
354  debug_off();
355 
356  /* Now call the normal event procedure: */
357  return notify_next_event_func(panel, (Notify_event) event, arg, type);
358 }
359 
360 
361 /* Create an abbreviation menu attached with a text item.
362  after_selection() is called when a selection is done or a text has been
363  entered. It can be seen as a new widget. */
364 Panel_item
366  char * label_string,
367  int value_display_length,
368  int x,
369  int y,
370  Menu (* generate_menu)(void),
371  void (* after_selection)(char *))
372 {
373  Panel_item item, text;
374 
375  item = xv_create(main_panel, PANEL_ABBREV_MENU_BUTTON,
376  PANEL_LABEL_STRING, label_string,
377  PANEL_VALUE_DISPLAY_LENGTH, value_display_length,
378  PANEL_LAYOUT, PANEL_HORIZONTAL,
379  PANEL_VALUE_X, x,
380  PANEL_VALUE_Y, y,
381  /* No real menu yet: */
382  PANEL_ITEM_MENU, xv_create(NULL, MENU,
383  MENU_STRINGS, "* none *", NULL,
384  NULL),
385  XV_KEY_DATA, ABBREV_MENU_WITH_TEXT_GENERATE_MENU, generate_menu,
386  XV_KEY_DATA, ABBREV_MENU_WITH_TEXT_AFTER_SELECTION, after_selection,
387 
388  NULL);
389 
390  notify_interpose_event_func(main_panel,
392  NOTIFY_SAFE);
393 
394  text = xv_create(main_panel, PANEL_TEXT,
395  PANEL_VALUE_DISPLAY_LENGTH, value_display_length,
396  PANEL_VALUE_STORED_LENGTH, 128,
397  PANEL_READ_ONLY, FALSE,
400  after_selection,
401  /* Strange, 21 does a
402  Program received signal SIGSEGV, Segmentation fault.
403  0x1cf988 in attr_check_use_custom () */
404  PANEL_VALUE_X, x + 25,
405  PANEL_VALUE_Y, y,
406  /* PANEL_ITEM_X_GAP, 22,*/
407  NULL);
408 
409  return text;
410 }
411 
412 
413 void
415 {
416  schoose_frame = xv_create(main_frame, FRAME,
417  XV_SHOW, FALSE,
418  FRAME_DONE_PROC, hide_window,
419  NULL);
420 
421  schoose_panel = xv_create(schoose_frame, PANEL, NULL);
422 
423  choice = xv_create(schoose_panel, PANEL_TEXT,
424  PANEL_LABEL_STRING, "Current choice",
425  PANEL_VALUE_DISPLAY_LENGTH, 30,
426  PANEL_NOTIFY_PROC, schoose_ok_notify,
427  XV_X, xv_col(schoose_panel, 0),
428  NULL);
429 
430  choices = xv_create(schoose_panel, PANEL_LIST,
431  PANEL_LABEL_STRING, "Available choices",
432  PANEL_LIST_DISPLAY_ROWS, 5,
433  PANEL_NOTIFY_PROC, schoose_choice_notify,
434  PANEL_CHOOSE_ONE, TRUE,
435  XV_X, xv_col(schoose_panel, 0),
436  XV_Y, xv_rows(schoose_panel, 1),
437  NULL);
438 
439  ok = xv_create(schoose_panel, PANEL_BUTTON,
440  PANEL_LABEL_STRING, "OK",
441  PANEL_NOTIFY_PROC, schoose_ok_notify,
442  XV_X, xv_col(schoose_panel, 5),
443  XV_Y, xv_rows(schoose_panel, 5),
444  NULL);
445 
446  cancel = xv_create(schoose_panel, PANEL_BUTTON,
447  PANEL_LABEL_STRING, "Cancel",
448  PANEL_NOTIFY_PROC, schoose_cancel_notify,
449  NULL);
450 
451  help = xv_create(schoose_panel, PANEL_BUTTON,
452  PANEL_LABEL_STRING, "Help",
453  PANEL_NOTIFY_PROC, schoose_help_notify,
454  NULL);
455 
456  (void) xv_set(schoose_panel, PANEL_DEFAULT_ITEM, ok, NULL);
457 
458  window_fit(schoose_panel);
459  window_fit(schoose_frame);
460 
461  /* Avoid the schoose_frame destruction: */
462  xv_set(schoose_frame,
463  FRAME_DONE_PROC, schoose_frame_done_proc,
464  NULL);
465 }
void const char const char const int
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
void free(void *)
GtkWidget * main_frame
If we are in the Emacs mode, the log_frame is no longer really used:
Definition: gpips.c:60
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 debug_on(env)
Definition: misc-local.h:157
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define debug_off()
Definition: misc-local.h:160
void debug(const int the_expected_debug_level, const char *calling_function_name, const char *a_message_format,...)
ARARGS0.
Definition: debug.c:189
char * strdup()
static entity array
static char * x
Definition: split_file.c:159
struct _newgen_struct_text_ * text
Definition: text.h:23
Frame schoose_frame
Definition: wpips-local.h:38
Panel main_panel
Definition: wpips.c:63
Panel schoose_panel
Definition: wpips-local.h:50
static void schoose_abbrev_menu_with_text_text_notify(Panel_item text_item, Event *event)
Accept only typed text in the menu list:
Definition: xv_schoose2.c:228
void schoose_cancel_notify(Panel_item item, Event *event)
Definition: xv_schoose2.c:126
static Panel_item cancel
Definition: xv_schoose2.c:54
@ ABBREV_MENU_WITH_TEXT_GENERATE_MENU
Definition: xv_schoose2.c:51
@ ABBREV_MENU_WITH_TEXT_AFTER_SELECTION
Definition: xv_schoose2.c:50
static Panel_item help
Definition: xv_schoose2.c:54
static Panel_item choices
Definition: xv_schoose2.c:54
void schoose_close()
schoose_close() can be called even when schoose window is already closed.
Definition: xv_schoose2.c:108
static void(* apply_on_cancel)(void)
Definition: xv_schoose2.c:57
static void abbrev_menu_with_text_menu_notify(Menu menu, Menu_item menu_item)
The function that calls the real user notifying function:
Definition: xv_schoose2.c:250
static void(* apply_on_choice)(char *)
Definition: xv_schoose2.c:56
static Panel_item ok
Definition: xv_schoose2.c:54
static Panel_item choice
Definition: xv_schoose2.c:54
void schoose(char *title, gen_array_t array, char *initial_choice, void(*function_for_ok)(char *), void(*function_for_cancel)(void))
Definition: xv_schoose2.c:177
static int schoose_choice_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_schoose2.c:136
static void schoose_ok_notify(Panel_item item, Event *event)
Cette routine est appelé d'une part lorsqu'on a cliqué sur OK pour valider un nom tapé textuellement,...
Definition: xv_schoose2.c:70
static void schoose_help_notify(Panel_item item, Event *event)
Definition: xv_schoose2.c:60
Panel_item schoose_create_abbrev_menu_with_text(Panel main_panel, char *label_string, int value_display_length, int x, int y, Menu(*generate_menu)(void), void(*after_selection)(char *))
Create an abbreviation menu attached with a text item.
Definition: xv_schoose2.c:365
void create_schoose_window()
Definition: xv_schoose2.c:414
static Notify_value abbrev_menu_event_filter_proc(Panel panel, Event *event, Notify_arg arg, Notify_event_type type)
Definition: xv_schoose2.c:268
static void schoose_frame_done_proc(Frame frame)
Avoid the schoose_frame destruction and act as cancel:
Definition: xv_schoose2.c:169
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