G4OpenGLXmConvenienceRoutines.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // Andrew Walkden  16th April 1997
00031 // G4OpenGLXmConvenienceRoutines : 
00032 //                       Collection of routines to facilitate
00033 //                       the addition of simple push button boxes,
00034 //                       and slider bars to the control panel.
00035 //
00036 // See G4OpenGLXmConvenienceRoutines.hh for more information.
00037 
00038 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
00039 
00040 #include "G4OpenGLXmViewer.hh"
00041 
00042 #include <Xm/Form.h>
00043 #include <Xm/ToggleB.h>
00044 #include <Xm/ArrowBG.h>
00045 #include <Xm/RowColumn.h>
00046 #include <Xm/TextF.h>
00047 #include <Xm/Separator.h>
00048 #include <Xm/Scale.h>
00049 
00050 void G4OpenGLXmViewer::Add_four_arrow_buttons (G4OpenGLXmViewer* pView,
00051                                              XtCallbackRec** arrow_callbacks,
00052                                              Widget* parent_widget) {
00053   
00054   Widget arrow_form = XtVaCreateWidget 
00055     ("arrow_form",
00056      xmFormWidgetClass,
00057      *parent_widget,
00058      XmNfractionBase, 3,
00059      XtNvisual, pView->vi->visual, 
00060      XtNdepth, pView->vi->depth, 
00061      XtNcolormap, pView->cmap, 
00062      XtNborderColor, pView->borcol,
00063      XtNbackground, pView->bgnd,
00064      NULL);
00065   
00066   Widget arrow = XtVaCreateManagedWidget 
00067     ("up_arrow",
00068      xmArrowButtonGadgetClass, 
00069      arrow_form,
00070      XmNtopAttachment, XmATTACH_POSITION,
00071      XmNtopPosition, 0,
00072      XmNbottomAttachment, XmATTACH_POSITION,
00073      XmNbottomPosition, 1,
00074      XmNleftAttachment, XmATTACH_POSITION,
00075      XmNleftPosition, 1,
00076      XmNrightAttachment, XmATTACH_POSITION,
00077      XmNrightPosition, 2,
00078      XmNarrowDirection, XmARROW_UP,
00079      NULL);
00080   
00081   XtVaSetValues (arrow,
00082                  XmNuserData, True,
00083                  NULL);
00084   
00085   XtAddCallbacks (arrow, 
00086                  XmNactivateCallback, 
00087                  arrow_callbacks[0]);
00088   
00089   XtAddCallbacks (arrow, 
00090                  XmNarmCallback, 
00091                  arrow_callbacks[0]);
00092   
00093   XtAddCallbacks (arrow, 
00094                  XmNdisarmCallback, 
00095                  arrow_callbacks[0]);
00096   
00097   arrow = XtVaCreateManagedWidget 
00098     ("down_arrow",
00099      xmArrowButtonGadgetClass, 
00100      arrow_form,
00101      XmNtopAttachment, XmATTACH_POSITION,
00102      XmNtopPosition, 2,
00103      XmNbottomAttachment, XmATTACH_POSITION,
00104      XmNbottomPosition, 3,
00105      XmNleftAttachment, XmATTACH_POSITION,
00106      XmNleftPosition, 1,
00107      XmNrightAttachment, XmATTACH_POSITION,
00108      XmNrightPosition, 2,
00109      XmNarrowDirection, XmARROW_DOWN,
00110      NULL);
00111   
00112   XtVaSetValues (arrow,
00113                  XmNuserData, False,
00114                  NULL);
00115   
00116   XtAddCallbacks (arrow, 
00117                  XmNactivateCallback, 
00118                  arrow_callbacks[1]);
00119   
00120   XtAddCallbacks (arrow, 
00121                  XmNarmCallback,
00122                  arrow_callbacks[1]);
00123   
00124   XtAddCallbacks (arrow, 
00125                  XmNdisarmCallback, 
00126                  arrow_callbacks[1]);
00127   
00128   arrow = XtVaCreateManagedWidget 
00129     ("left_arrow",
00130      xmArrowButtonGadgetClass, 
00131      arrow_form,
00132      XmNtopAttachment, XmATTACH_POSITION,
00133      XmNtopPosition, 1,
00134      XmNbottomAttachment, XmATTACH_POSITION,
00135      XmNbottomPosition, 2,
00136      XmNleftAttachment, XmATTACH_POSITION,
00137      XmNleftPosition, 0,
00138      XmNrightAttachment, XmATTACH_POSITION,
00139      XmNrightPosition, 1,
00140      XmNarrowDirection, XmARROW_LEFT,
00141      NULL);
00142   
00143   XtVaSetValues (arrow,
00144                  XmNuserData, False,
00145                  NULL);
00146 
00147   XtAddCallbacks (arrow, 
00148                  XmNactivateCallback, 
00149                  arrow_callbacks[2]);
00150 
00151   XtAddCallbacks (arrow, 
00152                  XmNarmCallback, 
00153                  arrow_callbacks[2]);
00154   
00155   XtAddCallbacks (arrow, 
00156                  XmNdisarmCallback, 
00157                  arrow_callbacks[2]);
00158       
00159   arrow = XtVaCreateManagedWidget 
00160     ("right_arrow",
00161      xmArrowButtonGadgetClass, 
00162      arrow_form,
00163      XmNtopAttachment, XmATTACH_POSITION,
00164      XmNtopPosition, 1,
00165      XmNbottomAttachment, XmATTACH_POSITION,
00166      XmNbottomPosition, 2,
00167      XmNleftAttachment, XmATTACH_POSITION,
00168      XmNleftPosition, 2,
00169      XmNrightAttachment, XmATTACH_POSITION,
00170      XmNrightPosition, 3,
00171      XmNarrowDirection, XmARROW_RIGHT,
00172      NULL);
00173   
00174   XtVaSetValues (arrow,
00175                  XmNuserData, True,
00176                  NULL);
00177   
00178   XtAddCallbacks (arrow, 
00179                  XmNactivateCallback, 
00180                  arrow_callbacks[3]);
00181   
00182   XtAddCallbacks (arrow, 
00183                  XmNarmCallback, 
00184                  arrow_callbacks[3]);
00185 
00186   XtAddCallbacks (arrow, 
00187                  XmNdisarmCallback, 
00188                  arrow_callbacks[3]);
00189   
00190   XtManageChild (arrow_form);
00191   
00192 }
00193 
00194 void G4OpenGLXmViewer::Add_radio_box (char* label_string,
00195                                     Widget* parent_widget,
00196                                     XtCallbackRec* radio_box_callback,
00197                                     G4int num_buttons,
00198                                     G4int default_button,
00199                                     char* radio_box_name,
00200                                     char** button_names,
00201                                     G4OpenGLXmViewer* pView)
00202 {
00203   XmString button_str = XmStringCreateLocalized((char*) ""); // ...to
00204   // initialise to something to avoid pedantic warning.
00205   Arg** args;
00206   args = new Arg* [num_buttons];
00207   Widget button;
00208 
00209   G4int i;
00210   for (i = 0; i < num_buttons; i++) {
00211 
00212     args[i] = new Arg[7];
00213     button_str = XmStringCreateLocalized (button_names[i]);
00214 
00215     XtSetArg (args[i][0], XtNvisual, pView->vi->visual);
00216     XtSetArg (args[i][1], XtNdepth, pView->vi->depth);
00217     XtSetArg (args[i][2], XtNcolormap, pView->cmap);
00218     XtSetArg (args[i][3], XtNborderColor, pView->borcol);
00219     XtSetArg (args[i][4], XtNbackground, pView->bgnd);
00220     XtSetArg (args[i][5], XmNlabelString, button_str); 
00221 
00222     if (i == default_button) {
00223       XtSetArg (args[i][6], XmNset, True);
00224     } else {
00225       XtSetArg (args[i][6], XmNset, False);
00226     }
00227   }
00228   
00229   Widget radio_box = XtVaCreateWidget (radio_box_name,
00230                                        xmRowColumnWidgetClass,
00231                                        *parent_widget,
00232                                        XmNisHomogeneous, False,
00233                                        XmNradioBehavior, True,
00234                                        XmNradioAlwaysOne, True,
00235                                        XmNuserData, pView,
00236                                        XtNvisual, pView->vi->visual,
00237                                        XtNdepth, pView->vi->depth,
00238                                        XtNcolormap, pView->cmap,
00239                                        XtNborderColor, pView->borcol,
00240                                        XtNbackground, pView->bgnd,
00241                                        NULL);
00242 
00243   XmString lab = XmStringCreateLocalized (label_string);
00244 
00245   // Unused!
00246   //Widget label = XtVaCreateManagedWidget ("radio_label",
00247   //                              xmLabelWidgetClass,
00248   //                              radio_box,
00249   //                              XmNalignment, XmALIGNMENT_CENTER,
00250   //                              XmNlabelString, lab,
00251   //                              XtNvisual, pView->vi->visual,
00252   //                              XtNdepth, pView->vi->depth,
00253   //                              XtNcolormap, pView->cmap,
00254   //                              XtNborderColor, pView->borcol,
00255   //                              XtNbackground, pView->bgnd,
00256   //                              NULL);
00257 
00258   XmStringFree (lab);
00259 
00260   for (i = 0; i < num_buttons; i++) {
00261     button = XtCreateManagedWidget (button_names[i],
00262                                     xmToggleButtonWidgetClass,
00263                                     radio_box,
00264                                     args[i],
00265                                     7);
00266     XtVaSetValues (button,
00267                    XmNuserData, i,
00268                    NULL);
00269     
00270     XtAddCallbacks (button,
00271                    XmNarmCallback,
00272                    radio_box_callback);
00273   }
00274 
00275   XtManageChild (radio_box);
00276 
00277   XmStringFree (button_str);
00278   
00279   for (i = 0; i < num_buttons; i++) {
00280 
00281     delete[] args[i];
00282 
00283   }
00284 
00285   delete[] args;
00286 }  
00287 
00288 void G4OpenGLXmViewer::Add_set_field (char* w_name, 
00289                                     char* w_text,
00290                                     Widget* row_col_box,
00291                                     Widget* wid,
00292                                     G4double* val,
00293                                     G4OpenGLXmViewer* pView)
00294 {
00295 
00296   char local_w_text[50];
00297   strcpy (local_w_text, w_text);
00298 
00299   char label_name[50];
00300   strcpy (label_name, w_name);
00301   strcat (label_name, "_label");
00302   
00303   char text_field_name[50];
00304   strcpy (text_field_name, w_name);
00305   strcat (text_field_name, "_text_field");
00306   
00307   XmString local_text = XmStringCreateLocalized (local_w_text);
00308 
00309   // Unused!
00310   //  Widget label = XtVaCreateManagedWidget (label_name, 
00311   //                              xmLabelWidgetClass,
00312   //                              *row_col_box,
00313   //                              XmNlabelString, local_text,
00314   //                              XtNvisual, pView->vi->visual, 
00315   //                              XtNdepth, pView->vi->depth, 
00316   //                              XtNcolormap, pView->cmap, 
00317   //                              XtNborderColor, pView->borcol,
00318   //                              XtNbackground, pView->bgnd,
00319   //                              NULL);
00320 
00321   XmStringFree (local_text);
00322 
00323   char initial[50];
00324   sprintf (initial, "%6.2f", *val);
00325   
00326   *wid = XtVaCreateManagedWidget (text_field_name,
00327                                   xmTextFieldWidgetClass,
00328                                   *row_col_box,
00329                                   XmNvalue, (String)initial,
00330                                   XtNvisual, pView->vi->visual, 
00331                                   XtNdepth, pView->vi->depth, 
00332                                   XtNcolormap, pView->cmap, 
00333                                   XtNborderColor, pView->borcol,
00334                                   XtNbackground, pView->bgnd,
00335                                   NULL);
00336 
00337   XtAddCallback (*wid, 
00338                  XmNvalueChangedCallback,
00339                  get_double_value_callback,
00340                  val);
00341 
00342   /* Not actually used - comment out to prevent compiler warnings.
00343      Instead, just in case it matters, just invoke
00344      XtVaCreateManagedWidget (JA)
00345   Widget sep = XtVaCreateManagedWidget ("sep",
00346                                         xmSeparatorWidgetClass,
00347                                         *row_col_box,
00348                                         XmNorientation, XmHORIZONTAL,
00349                                         XtNvisual, pView->vi->visual, 
00350                                         XtNdepth, pView->vi->depth, 
00351                                         XtNcolormap, pView->cmap, 
00352                                         XtNborderColor, pView->borcol,
00353                                         XtNbackground, pView->bgnd,
00354                                         NULL);
00355   sep = XtVaCreateManagedWidget ("sep",
00356                                  xmSeparatorWidgetClass,
00357                                  *row_col_box,
00358                                  XmNseparatorType, XmNO_LINE,
00359                                  XmNmargin, 1,
00360                                  XmNorientation, XmHORIZONTAL,
00361                                  XtNvisual, pView->vi->visual, 
00362                                  XtNdepth, pView->vi->depth, 
00363                                  XtNcolormap, pView->cmap, 
00364                                  XtNborderColor, pView->borcol,
00365                                  XtNbackground, pView->bgnd,
00366                                  NULL);
00367   */
00368   XtVaCreateManagedWidget ("sep",
00369                                  xmSeparatorWidgetClass,
00370                                  *row_col_box,
00371                                  XmNseparatorType, XmNO_LINE,
00372                                  XmNmargin, 1,
00373                                  XmNorientation, XmHORIZONTAL,
00374                                  XtNvisual, pView->vi->visual, 
00375                                  XtNdepth, pView->vi->depth, 
00376                                  XtNcolormap, pView->cmap, 
00377                                  XtNborderColor, pView->borcol,
00378                                  XtNbackground, pView->bgnd,
00379                                  NULL);
00380 }
00381 
00382 void G4OpenGLXmViewer::Add_slider_box (char* label_string,
00383                                      G4int num_sliders,
00384                                      char** slider_names,
00385                                      G4OpenGLXmViewer* pView,
00386                                      G4double* min_array,
00387                                      G4double* max_array,
00388                                      G4double* value_array,
00389                                      G4bool* show,
00390                                      short* decimals,
00391                                      unsigned char* orientation,
00392                                      unsigned char* direction,
00393                                      XtCallbackRec** slider_box_callbacks,
00394                                      Widget* parent_widget)
00395 {
00396   XmString slider_name_str = XmStringCreateLocalized((char*) ""); // ...to
00397   // initialise to something to avoid pedantic warning.
00398   Arg** slider_args;
00399   slider_args = new Arg*[num_sliders];
00400   Widget slider;
00401   G4int j = 0;
00402 
00403   G4int i;
00404   for (i = 0; i < num_sliders; i++) {
00405     j = 0; 
00406     slider_args[i] = new Arg[13];
00407     slider_name_str = XmStringCreateLtoR (slider_names[i], 
00408                                           XmFONTLIST_DEFAULT_TAG);
00409     
00410     XtSetArg (slider_args[i][j], 
00411               XtNvisual, pView->vi->visual); j++;
00412     XtSetArg (slider_args[i][j], 
00413               XtNdepth, pView->vi->depth); j++;
00414     XtSetArg (slider_args[i][j], 
00415               XtNcolormap, pView->cmap); j++;
00416     XtSetArg (slider_args[i][j], 
00417               XtNborderColor, pView->borcol); j++;
00418     XtSetArg (slider_args[i][j], 
00419               XtNbackground, pView->bgnd); j++;
00420     
00421     XtSetArg (slider_args[i][j], 
00422               XmNtitleString, slider_name_str);  j++;
00423     
00424     XtSetArg (slider_args[i][j], 
00425               XmNmaximum, G4int(max_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
00426     XtSetArg (slider_args[i][j], 
00427               XmNminimum, G4int(min_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
00428     XtSetArg (slider_args[i][j], 
00429               XmNvalue, G4int(value_array[i] * std::pow(10.0, (G4double)decimals[i]))); j++;
00430     XtSetArg (slider_args[i][j], 
00431               XmNshowValue, show[i]); j++;
00432     XtSetArg (slider_args[i][j], 
00433               XmNdecimalPoints, decimals[i]); j++;
00434     
00435     XtSetArg (slider_args[i][j], 
00436               XmNorientation, orientation[i]);  j++;
00437     XtSetArg (slider_args[i][j], 
00438               XmNprocessingDirection, direction[i]); j++;
00439 
00440   }
00441 
00442   Widget slider_box = XtVaCreateWidget ("slider_box",
00443                                         xmRowColumnWidgetClass,
00444                                         *parent_widget,
00445                                         XmNisHomogeneous, False,
00446                                         XtNvisual, pView->vi->visual,
00447                                         XtNdepth, pView->vi->depth,
00448                                         XtNcolormap, pView->cmap,
00449                                         XtNborderColor, pView->borcol,
00450                                         XtNbackground, pView->bgnd,
00451                                         NULL);
00452 
00453   XmString lab = XmStringCreateLocalized (label_string);
00454 
00455   // Unused!
00456   //Widget label = XtVaCreateManagedWidget ("slider_label",
00457   //                              xmLabelWidgetClass,
00458   //                              slider_box,
00459   //                              XmNlabelString, lab,
00460   //                              XmNalignment, XmALIGNMENT_CENTER,
00461   //                              XtNvisual, pView->vi->visual,
00462   //                              XtNdepth, pView->vi->depth,
00463   //                              XtNcolormap, pView->cmap,
00464   //                              XtNborderColor, pView->borcol,
00465   //                              XtNbackground, pView->bgnd,
00466   //                              NULL);
00467 
00468   XmStringFree (lab);
00469   
00470   for (i = 0; i < num_sliders; i++) {
00471     
00472     slider = XtCreateManagedWidget (slider_names[i],
00473                                     xmScaleWidgetClass,
00474                                     slider_box,
00475                                     slider_args[i],
00476                                     j);
00477 
00478     XtAddCallbacks (slider,
00479                    XmNvalueChangedCallback,
00480                    slider_box_callbacks[i]);
00481     
00482     XtAddCallbacks (slider,
00483                    XmNdragCallback,
00484                    slider_box_callbacks[i]);
00485     
00486   }
00487 
00488   XtManageChild (slider_box);
00489   XmStringFree (slider_name_str);
00490 
00491   for (i = 0; i < num_sliders; i++) {
00492 
00493     delete[] slider_args[i];
00494 
00495   }
00496 
00497   delete[] slider_args;
00498 
00499 }
00500 
00501 void G4OpenGLXmViewer::get_double_value_callback (Widget w, 
00502                                                 XtPointer clientData, 
00503                                                 XtPointer) 
00504 {
00505   G4double* val = (G4double*) clientData;
00506   String string;
00507 
00508   XtVaGetValues (w,
00509                  XmNvalue, &string,
00510                  NULL);
00511 
00512   sscanf (string, "%lg", val);
00513 
00514 }
00515 
00516 void G4OpenGLXmViewer::get_text_callback (Widget w, 
00517                                         XtPointer clientData, 
00518                                         XtPointer) 
00519 {
00520   char* txt = (char*)clientData;
00521   String string;
00522 
00523   XtVaGetValues (w,
00524                  XmNvalue, &string,
00525                  NULL);
00526 
00527   sscanf (string, "%s", txt);
00528 }
00529 
00530 G4bool G4OpenGLXmViewer::get_boolean_userData (Widget w)
00531 {
00532   XtPointer userData;
00533   XtVaGetValues (w,XmNuserData,&userData,NULL);
00534   return (G4bool)(((unsigned long)userData)&0xffff);
00535 }
00536 
00537 G4int G4OpenGLXmViewer::get_int_userData (Widget w)
00538 {
00539   XtPointer userData;
00540   XtVaGetValues (w,XmNuserData,&userData,NULL);
00541   return (G4int)(unsigned long)userData;
00542 }
00543 
00544 #endif
00545 
00546 
00547 
00548 
00549 

Generated on Mon May 27 17:49:11 2013 for Geant4 by  doxygen 1.4.7