Geant4_10
G4OpenGLXmConvenienceRoutines.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4OpenGLXmConvenienceRoutines.cc 68043 2013-03-13 14:27:49Z gcosmo $
28 //
29 //
30 // Andrew Walkden 16th April 1997
31 // G4OpenGLXmConvenienceRoutines :
32 // Collection of routines to facilitate
33 // the addition of simple push button boxes,
34 // and slider bars to the control panel.
35 //
36 // See G4OpenGLXmConvenienceRoutines.hh for more information.
37 
38 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
39 
40 #include "G4OpenGLXmViewer.hh"
41 
42 #include <Xm/Form.h>
43 #include <Xm/ToggleB.h>
44 #include <Xm/ArrowBG.h>
45 #include <Xm/RowColumn.h>
46 #include <Xm/TextF.h>
47 #include <Xm/Separator.h>
48 #include <Xm/Scale.h>
49 
50 #include <sstream>
51 
52 void G4OpenGLXmViewer::Add_four_arrow_buttons (G4OpenGLXmViewer* pView,
53  XtCallbackRec** arrow_callbacks,
54  Widget* parent_widget) {
55 
56  Widget arrow_form = XtVaCreateWidget
57  ("arrow_form",
58  xmFormWidgetClass,
59  *parent_widget,
60  XmNfractionBase, 3,
61  XtNvisual, pView->vi->visual,
62  XtNdepth, pView->vi->depth,
63  XtNcolormap, pView->cmap,
64  XtNborderColor, pView->borcol,
65  XtNbackground, pView->bgnd,
66  NULL);
67 
68  Widget arrow = XtVaCreateManagedWidget
69  ("up_arrow",
70  xmArrowButtonGadgetClass,
71  arrow_form,
72  XmNtopAttachment, XmATTACH_POSITION,
73  XmNtopPosition, 0,
74  XmNbottomAttachment, XmATTACH_POSITION,
75  XmNbottomPosition, 1,
76  XmNleftAttachment, XmATTACH_POSITION,
77  XmNleftPosition, 1,
78  XmNrightAttachment, XmATTACH_POSITION,
79  XmNrightPosition, 2,
80  XmNarrowDirection, XmARROW_UP,
81  NULL);
82 
83  XtVaSetValues (arrow,
84  XmNuserData, True,
85  NULL);
86 
87  XtAddCallbacks (arrow,
88  XmNactivateCallback,
89  arrow_callbacks[0]);
90 
91  XtAddCallbacks (arrow,
92  XmNarmCallback,
93  arrow_callbacks[0]);
94 
95  XtAddCallbacks (arrow,
96  XmNdisarmCallback,
97  arrow_callbacks[0]);
98 
99  arrow = XtVaCreateManagedWidget
100  ("down_arrow",
101  xmArrowButtonGadgetClass,
102  arrow_form,
103  XmNtopAttachment, XmATTACH_POSITION,
104  XmNtopPosition, 2,
105  XmNbottomAttachment, XmATTACH_POSITION,
106  XmNbottomPosition, 3,
107  XmNleftAttachment, XmATTACH_POSITION,
108  XmNleftPosition, 1,
109  XmNrightAttachment, XmATTACH_POSITION,
110  XmNrightPosition, 2,
111  XmNarrowDirection, XmARROW_DOWN,
112  NULL);
113 
114  XtVaSetValues (arrow,
115  XmNuserData, False,
116  NULL);
117 
118  XtAddCallbacks (arrow,
119  XmNactivateCallback,
120  arrow_callbacks[1]);
121 
122  XtAddCallbacks (arrow,
123  XmNarmCallback,
124  arrow_callbacks[1]);
125 
126  XtAddCallbacks (arrow,
127  XmNdisarmCallback,
128  arrow_callbacks[1]);
129 
130  arrow = XtVaCreateManagedWidget
131  ("left_arrow",
132  xmArrowButtonGadgetClass,
133  arrow_form,
134  XmNtopAttachment, XmATTACH_POSITION,
135  XmNtopPosition, 1,
136  XmNbottomAttachment, XmATTACH_POSITION,
137  XmNbottomPosition, 2,
138  XmNleftAttachment, XmATTACH_POSITION,
139  XmNleftPosition, 0,
140  XmNrightAttachment, XmATTACH_POSITION,
141  XmNrightPosition, 1,
142  XmNarrowDirection, XmARROW_LEFT,
143  NULL);
144 
145  XtVaSetValues (arrow,
146  XmNuserData, False,
147  NULL);
148 
149  XtAddCallbacks (arrow,
150  XmNactivateCallback,
151  arrow_callbacks[2]);
152 
153  XtAddCallbacks (arrow,
154  XmNarmCallback,
155  arrow_callbacks[2]);
156 
157  XtAddCallbacks (arrow,
158  XmNdisarmCallback,
159  arrow_callbacks[2]);
160 
161  arrow = XtVaCreateManagedWidget
162  ("right_arrow",
163  xmArrowButtonGadgetClass,
164  arrow_form,
165  XmNtopAttachment, XmATTACH_POSITION,
166  XmNtopPosition, 1,
167  XmNbottomAttachment, XmATTACH_POSITION,
168  XmNbottomPosition, 2,
169  XmNleftAttachment, XmATTACH_POSITION,
170  XmNleftPosition, 2,
171  XmNrightAttachment, XmATTACH_POSITION,
172  XmNrightPosition, 3,
173  XmNarrowDirection, XmARROW_RIGHT,
174  NULL);
175 
176  XtVaSetValues (arrow,
177  XmNuserData, True,
178  NULL);
179 
180  XtAddCallbacks (arrow,
181  XmNactivateCallback,
182  arrow_callbacks[3]);
183 
184  XtAddCallbacks (arrow,
185  XmNarmCallback,
186  arrow_callbacks[3]);
187 
188  XtAddCallbacks (arrow,
189  XmNdisarmCallback,
190  arrow_callbacks[3]);
191 
192  XtManageChild (arrow_form);
193 
194 }
195 
196 void G4OpenGLXmViewer::Add_radio_box (char* label_string,
197  Widget* parent_widget,
198  XtCallbackRec* radio_box_callback,
199  G4int num_buttons,
200  G4int default_button,
201  char* radio_box_name,
202  char** button_names,
203  G4OpenGLXmViewer* pView)
204 {
205  XmString button_str = XmStringCreateLocalized((char*) ""); // ...to
206  // initialise to something to avoid pedantic warning.
207  Arg** args;
208  args = new Arg* [num_buttons];
209  Widget button;
210 
211  G4int i;
212  for (i = 0; i < num_buttons; i++) {
213 
214  args[i] = new Arg[7];
215  button_str = XmStringCreateLocalized (button_names[i]);
216 
217  XtSetArg (args[i][0], XtNvisual, pView->vi->visual);
218  XtSetArg (args[i][1], XtNdepth, pView->vi->depth);
219  XtSetArg (args[i][2], XtNcolormap, pView->cmap);
220  XtSetArg (args[i][3], XtNborderColor, pView->borcol);
221  XtSetArg (args[i][4], XtNbackground, pView->bgnd);
222  XtSetArg (args[i][5], XmNlabelString, button_str);
223 
224  if (i == default_button) {
225  XtSetArg (args[i][6], XmNset, True);
226  } else {
227  XtSetArg (args[i][6], XmNset, False);
228  }
229  }
230 
231  Widget radio_box = XtVaCreateWidget (radio_box_name,
232  xmRowColumnWidgetClass,
233  *parent_widget,
234  XmNisHomogeneous, False,
235  XmNradioBehavior, True,
236  XmNradioAlwaysOne, True,
237  XmNuserData, pView,
238  XtNvisual, pView->vi->visual,
239  XtNdepth, pView->vi->depth,
240  XtNcolormap, pView->cmap,
241  XtNborderColor, pView->borcol,
242  XtNbackground, pView->bgnd,
243  NULL);
244 
245  XmString lab = XmStringCreateLocalized (label_string);
246 
247  // Unused!
248  //Widget label = XtVaCreateManagedWidget ("radio_label",
249  // xmLabelWidgetClass,
250  // radio_box,
251  // XmNalignment, XmALIGNMENT_CENTER,
252  // XmNlabelString, lab,
253  // XtNvisual, pView->vi->visual,
254  // XtNdepth, pView->vi->depth,
255  // XtNcolormap, pView->cmap,
256  // XtNborderColor, pView->borcol,
257  // XtNbackground, pView->bgnd,
258  // NULL);
259 
260  XmStringFree (lab);
261 
262  for (i = 0; i < num_buttons; i++) {
263  button = XtCreateManagedWidget (button_names[i],
264  xmToggleButtonWidgetClass,
265  radio_box,
266  args[i],
267  7);
268  XtVaSetValues (button,
269  XmNuserData, i,
270  NULL);
271 
272  XtAddCallbacks (button,
273  XmNarmCallback,
274  radio_box_callback);
275  }
276 
277  XtManageChild (radio_box);
278 
279  XmStringFree (button_str);
280 
281  for (i = 0; i < num_buttons; i++) {
282 
283  delete[] args[i];
284 
285  }
286 
287  delete[] args;
288 }
289 
290 void G4OpenGLXmViewer::Add_set_field (char* w_name,
291  char* w_text,
292  Widget* row_col_box,
293  Widget* wid,
294  G4double* val,
295  G4OpenGLXmViewer* pView)
296 {
297 
298  char local_w_text[50];
299  strcpy (local_w_text, w_text);
300 
301  char label_name[50];
302  strcpy (label_name, w_name);
303  strcat (label_name, "_label");
304 
305  char text_field_name[50];
306  strcpy (text_field_name, w_name);
307  strcat (text_field_name, "_text_field");
308 
309  XmString local_text = XmStringCreateLocalized (local_w_text);
310 
311  // Unused!
312  // Widget label = XtVaCreateManagedWidget (label_name,
313  // xmLabelWidgetClass,
314  // *row_col_box,
315  // XmNlabelString, local_text,
316  // XtNvisual, pView->vi->visual,
317  // XtNdepth, pView->vi->depth,
318  // XtNcolormap, pView->cmap,
319  // XtNborderColor, pView->borcol,
320  // XtNbackground, pView->bgnd,
321  // NULL);
322 
323  XmStringFree (local_text);
324 
325  char initial[50];
326  sprintf (initial, "%6.2f", *val);
327 
328  *wid = XtVaCreateManagedWidget (text_field_name,
329  xmTextFieldWidgetClass,
330  *row_col_box,
331  XmNvalue, (String)initial,
332  XtNvisual, pView->vi->visual,
333  XtNdepth, pView->vi->depth,
334  XtNcolormap, pView->cmap,
335  XtNborderColor, pView->borcol,
336  XtNbackground, pView->bgnd,
337  NULL);
338 
339  XtAddCallback (*wid,
340  XmNvalueChangedCallback,
341  get_double_value_callback,
342  val);
343 
344  /* Not actually used - comment out to prevent compiler warnings.
345  Instead, just in case it matters, just invoke
346  XtVaCreateManagedWidget (JA)
347  Widget sep = XtVaCreateManagedWidget ("sep",
348  xmSeparatorWidgetClass,
349  *row_col_box,
350  XmNorientation, XmHORIZONTAL,
351  XtNvisual, pView->vi->visual,
352  XtNdepth, pView->vi->depth,
353  XtNcolormap, pView->cmap,
354  XtNborderColor, pView->borcol,
355  XtNbackground, pView->bgnd,
356  NULL);
357  sep = XtVaCreateManagedWidget ("sep",
358  xmSeparatorWidgetClass,
359  *row_col_box,
360  XmNseparatorType, XmNO_LINE,
361  XmNmargin, 1,
362  XmNorientation, XmHORIZONTAL,
363  XtNvisual, pView->vi->visual,
364  XtNdepth, pView->vi->depth,
365  XtNcolormap, pView->cmap,
366  XtNborderColor, pView->borcol,
367  XtNbackground, pView->bgnd,
368  NULL);
369  */
370  XtVaCreateManagedWidget ("sep",
371  xmSeparatorWidgetClass,
372  *row_col_box,
373  XmNseparatorType, XmNO_LINE,
374  XmNmargin, 1,
375  XmNorientation, XmHORIZONTAL,
376  XtNvisual, pView->vi->visual,
377  XtNdepth, pView->vi->depth,
378  XtNcolormap, pView->cmap,
379  XtNborderColor, pView->borcol,
380  XtNbackground, pView->bgnd,
381  NULL);
382 }
383 
384 void G4OpenGLXmViewer::Add_slider_box (char* label_string,
385  G4int num_sliders,
386  char** slider_names,
387  G4OpenGLXmViewer* pView,
388  G4double* min_array,
389  G4double* max_array,
390  G4double* value_array,
391  G4bool* show,
392  short* decimals,
393  unsigned char* orientation,
394  unsigned char* direction,
395  XtCallbackRec** slider_box_callbacks,
396  Widget* parent_widget)
397 {
398  XmString slider_name_str = XmStringCreateLocalized((char*) ""); // ...to
399  // initialise to something to avoid pedantic warning.
400  Arg** slider_args;
401  slider_args = new Arg*[num_sliders];
402  Widget slider;
403  G4int j = 0;
404 
405  G4int i;
406  for (i = 0; i < num_sliders; i++) {
407  j = 0;
408  slider_args[i] = new Arg[13];
409  slider_name_str = XmStringCreateLtoR (slider_names[i],
410  XmFONTLIST_DEFAULT_TAG);
411 
412  XtSetArg (slider_args[i][j],
413  XtNvisual, pView->vi->visual); j++;
414  XtSetArg (slider_args[i][j],
415  XtNdepth, pView->vi->depth); j++;
416  XtSetArg (slider_args[i][j],
417  XtNcolormap, pView->cmap); j++;
418  XtSetArg (slider_args[i][j],
419  XtNborderColor, pView->borcol); j++;
420  XtSetArg (slider_args[i][j],
421  XtNbackground, pView->bgnd); j++;
422 
423  XtSetArg (slider_args[i][j],
424  XmNtitleString, slider_name_str); j++;
425 
426  XtSetArg (slider_args[i][j],
427  XmNmaximum, G4int(max_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
428  XtSetArg (slider_args[i][j],
429  XmNminimum, G4int(min_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
430  XtSetArg (slider_args[i][j],
431  XmNvalue, G4int(value_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
432  XtSetArg (slider_args[i][j],
433  XmNshowValue, show[i]); j++;
434  XtSetArg (slider_args[i][j],
435  XmNdecimalPoints, decimals[i]); j++;
436 
437  XtSetArg (slider_args[i][j],
438  XmNorientation, orientation[i]); j++;
439  XtSetArg (slider_args[i][j],
440  XmNprocessingDirection, direction[i]); j++;
441 
442  }
443 
444  Widget slider_box = XtVaCreateWidget ("slider_box",
445  xmRowColumnWidgetClass,
446  *parent_widget,
447  XmNisHomogeneous, False,
448  XtNvisual, pView->vi->visual,
449  XtNdepth, pView->vi->depth,
450  XtNcolormap, pView->cmap,
451  XtNborderColor, pView->borcol,
452  XtNbackground, pView->bgnd,
453  NULL);
454 
455  XmString lab = XmStringCreateLocalized (label_string);
456 
457  // Unused!
458  //Widget label = XtVaCreateManagedWidget ("slider_label",
459  // xmLabelWidgetClass,
460  // slider_box,
461  // XmNlabelString, lab,
462  // XmNalignment, XmALIGNMENT_CENTER,
463  // XtNvisual, pView->vi->visual,
464  // XtNdepth, pView->vi->depth,
465  // XtNcolormap, pView->cmap,
466  // XtNborderColor, pView->borcol,
467  // XtNbackground, pView->bgnd,
468  // NULL);
469 
470  XmStringFree (lab);
471 
472  for (i = 0; i < num_sliders; i++) {
473 
474  slider = XtCreateManagedWidget (slider_names[i],
475  xmScaleWidgetClass,
476  slider_box,
477  slider_args[i],
478  j);
479 
480  XtAddCallbacks (slider,
481  XmNvalueChangedCallback,
482  slider_box_callbacks[i]);
483 
484  XtAddCallbacks (slider,
485  XmNdragCallback,
486  slider_box_callbacks[i]);
487 
488  }
489 
490  XtManageChild (slider_box);
491  XmStringFree (slider_name_str);
492 
493  for (i = 0; i < num_sliders; i++) {
494 
495  delete[] slider_args[i];
496 
497  }
498 
499  delete[] slider_args;
500 
501 }
502 
503 void G4OpenGLXmViewer::get_double_value_callback (Widget w,
504  XtPointer clientData,
505  XtPointer)
506 {
507  G4double* val = (G4double*) clientData;
508  String string;
509 
510  XtVaGetValues (w,
511  XmNvalue, &string,
512  NULL);
513 
514 // sscanf (string, "%lg", val);
515  std::istringstream iss(string);
516  iss >> *val;
517 }
518 
519 void G4OpenGLXmViewer::get_text_callback (Widget w,
520  XtPointer clientData,
521  XtPointer)
522 {
523  char* txt = (char*)clientData;
524  String string;
525 
526  XtVaGetValues (w,
527  XmNvalue, &string,
528  NULL);
529 
530 // sscanf (string, "%s", txt);
531  std::istringstream iss(string);
532  iss >> txt;
533 }
534 
535 G4bool G4OpenGLXmViewer::get_boolean_userData (Widget w)
536 {
537  XtPointer userData;
538  XtVaGetValues (w,XmNuserData,&userData,NULL);
539  return (G4bool)(((unsigned long)userData)&0xffff);
540 }
541 
542 G4int G4OpenGLXmViewer::get_int_userData (Widget w)
543 {
544  XtPointer userData;
545  XtVaGetValues (w,XmNuserData,&userData,NULL);
546  return (G4int)(unsigned long)userData;
547 }
548 
549 #endif
550 
551 
552 
553 
554 
#define userData
Definition: xmlparse.cc:555
int G4int
Definition: G4Types.hh:78
bool G4bool
Definition: G4Types.hh:79
double G4double
Definition: G4Types.hh:76