Geant4 Cross Reference |
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 // 28 // G.Barrand 29 30 #include "G4Xt.hh" 31 32 #include "G4ios.hh" 33 34 #include <X11/Intrinsic.h> 35 #include <X11/Shell.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #define NewString(str) \ 40 ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : NULL) 41 42 // static void XWidgetIconify (Widget); 43 // static void XWidgetUniconify (Widget); 44 // static void XDisplaySetWindowToNormalState (Display*,Window); 45 46 G4Xt* G4Xt::instance = NULL; 47 48 static G4bool XtInited = false; 49 static int argn = 0; 50 static char** args = NULL; 51 static XtAppContext appContext = NULL; 52 static Widget topWidget = NULL; 53 /***************************************************************************/ 54 G4Xt* G4Xt::getInstance() 55 /***************************************************************************/ 56 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 57 { 58 return G4Xt::getInstance(0, NULL, (char*)"Geant4"); 59 } 60 /***************************************************************************/ 61 G4Xt* G4Xt::getInstance(int a_argn, char** a_args, char* a_class) 62 /***************************************************************************/ 63 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 64 { 65 if (instance == NULL) { 66 instance = new G4Xt(a_argn, a_args, a_class); 67 } 68 return instance; 69 } 70 /***************************************************************************/ 71 G4Xt::G4Xt(int a_argn, char** a_args, char* a_class) 72 /***************************************************************************/ 73 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 74 { 75 if (XtInited == false) { // Xt should be Inited once ! 76 if (a_argn != 0) { // Save args. 77 args = (char**)malloc(a_argn * sizeof(char*)); 78 if (args != NULL) { 79 argn = a_argn; 80 for (int argi = 0; argi < a_argn; argi++) { 81 args[argi] = (char*)NewString(a_args[argi]); 82 } 83 } 84 } 85 #if XtSpecificationRelease == 4 86 Cardinal narg; 87 narg = (Cardinal)a_argn; 88 #else 89 int narg; 90 narg = a_argn; 91 #endif 92 Arg xargs[1]; 93 XtSetArg(xargs[0], XtNgeometry, "100x100"); 94 topWidget = 95 XtAppInitialize(&appContext, a_class, NULL, (Cardinal)0, &narg, a_args, NULL, xargs, 1); 96 if (topWidget == NULL) { 97 G4cout << "G4Xt : Unable to init Xt." << G4endl; 98 } 99 // Restore a_args. XtAppInitialize corrupts the given ones !!! 100 if ((a_argn != 0) && (args != NULL)) { 101 for (int argi = 0; argi < a_argn; argi++) { 102 if (args[argi] != NULL) 103 strcpy(a_args[argi], args[argi]); 104 else 105 a_args[argi] = NULL; 106 } 107 } 108 // If topWidget not realized, pbs with Inventor shells. 109 XtSetMappedWhenManaged(topWidget, False); 110 XtRealizeWidget(topWidget); 111 XtInited = true; 112 } 113 SetArguments(argn, args); 114 SetMainInteractor(topWidget); 115 AddDispatcher(xt_dispatch_event); 116 // Coverity gcc8 cast warning 117 // AddDispatcher ((G4DispatchFunction)XtDispatchEvent); 118 } 119 /***************************************************************************/ 120 G4Xt::~G4Xt() 121 /***************************************************************************/ 122 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 123 { 124 if (this == instance) { 125 instance = NULL; 126 } 127 } 128 /***************************************************************************/ 129 G4bool G4Xt::Inited() 130 /***************************************************************************/ 131 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 132 { 133 return XtInited; 134 } 135 /***************************************************************************/ 136 void* G4Xt::GetEvent() 137 /***************************************************************************/ 138 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 139 { 140 static XEvent event; 141 if (appContext == NULL) return NULL; 142 if (topWidget == NULL) return NULL; 143 XtAppNextEvent(appContext, &event); 144 return &event; 145 } 146 /***************************************************************************/ 147 void G4Xt::PutStringInResourceDatabase(char* a_string) 148 /***************************************************************************/ 149 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 150 { 151 if (topWidget == NULL) return; 152 if (a_string == NULL) return; 153 Display* dpy = XtDisplay(topWidget); 154 XrmDatabase dbres = XrmGetStringDatabase(a_string); 155 if (dbres == NULL) return; 156 XrmDatabase database = XrmGetDatabase(dpy); 157 if (database != NULL) { 158 XrmMergeDatabases(dbres, &database); 159 } 160 else { 161 XrmSetDatabase(dpy, dbres); 162 } 163 } 164 /***************************************************************************/ 165 void G4Xt::FlushAndWaitExecution() 166 /***************************************************************************/ 167 /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/ 168 { 169 if (topWidget == NULL) return; 170 XSync(XtDisplay(topWidget), False); 171 } 172