Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGApplication.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Guy Barrand 30/05/2001
3 
4 /*************************************************************************
5  * Copyright (C) 2001, Guy Barrand. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TGApplication //
15 // //
16 // This class initialize the ROOT GUI toolkit. //
17 // This class must be instantiated exactly once in any given //
18 // application. //
19 // //
20 //////////////////////////////////////////////////////////////////////////
21 
22 #include "RConfigure.h"
23 
24 #include "TGApplication.h"
25 #include "TROOT.h"
26 #include "TSystem.h"
27 #include "TGClient.h"
28 #include "TPluginManager.h"
29 #include "TError.h"
30 #include "TEnv.h"
31 #include "TVirtualX.h"
32 #include "TStyle.h"
33 #include "TInterpreter.h"
34 #include "TColor.h"
35 
36 ClassImp(TGApplication);
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// Create a GUI application environment. Use this class if you only
40 /// want to use the ROOT GUI and no other services. In all other cases
41 /// use either TApplication or TRint.
42 
43 TGApplication::TGApplication(const char *appClassName,
44  int *argc, char **argv, void*, int)
45  : TApplication(), fDisplay(0), fClient(0)
46 {
47  if (gApplication) {
48  Error("TGApplication", "only one instance of TGApplication allowed");
49  return;
50  }
51 
52  if (!gROOT)
53  ::Fatal("TGApplication::TGApplication", "ROOT system not initialized");
54 
55  if (!gSystem)
56  ::Fatal("TGApplication::TGApplication", "gSystem not initialized");
57 
58  gApplication = this;
59  gROOT->SetApplication(this);
60  gROOT->SetName(appClassName);
61 
62  GetOptions(argc, argv);
63  if (argv && argv[0])
64  gSystem->SetProgname(argv[0]);
65 
66  // Tell TSystem the TApplication has been created
67  gSystem->NotifyApplicationCreated();
68 
69  LoadGraphicsLibs();
70 
71  if (!fDisplay) gSystem->SetDisplay();
72  fClient = new TGClient(fDisplay);
73 
74  if (fClient->IsZombie()) {
75  Error("TGApplication", "cannot switch to batch mode, exiting...");
76  gSystem->Exit(1);
77  }
78 
79  // a GUI application is never run in batch mode
80  gROOT->SetBatch(kFALSE);
81 
82  if (strcmp(appClassName, "proofserv")) {
83  const char *ttpath = gEnv->GetValue("Root.TTFontPath",
84  TROOT::GetTTFFontDir());
85  char *ttfont = gSystem->Which(ttpath, "arialbd.ttf", kReadPermission);
86  // Added by cholm for use of DFSG - fonts - based on fix by Kevin
87  if (!ttfont)
88  ttfont = gSystem->Which(ttpath, "FreeSansBold.ttf", kReadPermission);
89  if (ttfont && gEnv->GetValue("Root.UseTTFonts", 1)) {
90  TPluginHandler *h;
91  if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualX", "x11ttf")))
92  if (h->LoadPlugin() == -1)
93  Info("TGApplication", "no TTF support");
94  }
95 
96  delete [] ttfont;
97  }
98 
99  // Create the canvas colors early so they are allocated before
100  // any color table expensive bitmaps get allocated in GUI routines (like
101  // creation of XPM bitmaps).
102  TColor::InitializeColors();
103 
104  // Set default screen factor (if not disabled in rc file)
105  if (gEnv->GetValue("Canvas.UseScreenFactor", 1)) {
106  Int_t x, y;
107  UInt_t w, h;
108  if (gVirtualX) {
109  gVirtualX->GetGeometry(-1, x, y, w, h);
110  if (h > 0 && h < 1000) gStyle->SetScreenFactor(0.0011*h);
111  }
112  }
113 
114  // Save current interpreter context
115  gInterpreter->SaveContext();
116  gInterpreter->SaveGlobalsContext();
117 
118  // to allow user to interact with TCanvas's under WIN32
119  gROOT->SetLineHasBeenProcessed();
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// TGApplication dtor.
124 
125 TGApplication::~TGApplication()
126 {
127  delete fDisplay;
128  delete fClient;
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Load shared libs necessary for GUI.
133 
134 void TGApplication::LoadGraphicsLibs()
135 {
136  TString name;
137  TString title1 = "ROOT interface to ";
138  TString nativex, title;
139 #ifndef R__WIN32
140  nativex = "x11";
141  name = "X11";
142  title = title1 + "X11";
143 #else
144  nativex = "win32gdk";
145  name = "Win32gdk";
146  title = title1 + "Win32gdk";
147 #endif
148 
149  TString guiBackend(gEnv->GetValue("Gui.Backend", "native"));
150  guiBackend.ToLower();
151  if (guiBackend == "native") {
152  guiBackend = nativex;
153  } else {
154  name = guiBackend;
155  title = title1 + guiBackend;
156  }
157 
158  TPluginHandler *h;
159  if ((h = gROOT->GetPluginManager()->FindHandler("TVirtualX", guiBackend))) {
160  if (h->LoadPlugin() == -1)
161  return;
162  gVirtualX = (TVirtualX *) h->ExecPlugin(2, name.Data(), title.Data());
163  }
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Handle command line arguments. Arguments handled are removed from the
168 /// argument array. Currently only option "-display xserver" is considered.
169 
170 void TGApplication::GetOptions(Int_t *argc, char **argv)
171 {
172  if (!argc) return;
173 
174  int i, j;
175  for (i = 0; i < *argc; i++) {
176  if (!strcmp(argv[i], "-display")) {
177  if (argv[i+1] && strlen(argv[i+1]) && argv[i+1][0] != '-') {
178  fDisplay = StrDup(argv[i+1]);
179  argv[i] = 0;
180  argv[i+1] = 0;
181  i++;
182  }
183  }
184  }
185 
186  j = 0;
187  for (i = 0; i < *argc; i++) {
188  if (argv[i]) {
189  argv[j] = argv[i];
190  j++;
191  }
192  }
193 
194  *argc = j;
195 }
196