Logo ROOT   master
Reference Guide
RWebDisplayArgs.cxx
Go to the documentation of this file.
1 /// \file RWebDisplayArgs.cxx
2 /// \ingroup WebGui ROOT7
3 /// \author Sergey Linev <s.linev@gsi.de>
4 /// \date 2018-10-24
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #include <ROOT/RWebDisplayArgs.hxx>
17 #include <ROOT/RWebWindow.hxx>
18 #include <ROOT/RConfig.hxx>
19 
20 #include "TROOT.h"
21 
22 /** \class ROOT::Experimental::RWebDisplayArgs
23  * \ingroup webdisplay
24  *
25  * Holds different arguments for starting browser with RWebDisplayHandle::Display() method
26  */
27 
28 ///////////////////////////////////////////////////////////////////////////////////////////
29 /// Default constructor - browser kind configured from gROOT->GetWebDisplay()
30 
32 {
33  SetBrowserKind("");
34 }
35 
36 ///////////////////////////////////////////////////////////////////////////////////////////
37 /// Constructor - browser kind specified as std::string
38 /// See SetBrowserKind() method for description of allowed parameters
39 
41 {
42  SetBrowserKind(browser);
43 }
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////
46 /// Constructor - browser kind specified as const char *
47 /// See SetBrowserKind() method for description of allowed parameters
48 
50 {
51  SetBrowserKind(browser);
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////////////////
55 /// Constructor - specify window width and height
56 
57 ROOT::Experimental::RWebDisplayArgs::RWebDisplayArgs(int width, int height, int x, int y, const std::string &browser)
58 {
59  SetSize(width, height);
60  SetPos(x, y);
61  SetBrowserKind(browser);
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 /// Constructor - specify master window and channel (if reserved already)
66 
67 ROOT::Experimental::RWebDisplayArgs::RWebDisplayArgs(std::shared_ptr<RWebWindow> master, int channel)
68 {
69  SetMasterWindow(master, channel);
70 }
71 
72 
73 ///////////////////////////////////////////////////////////////////////////////////////////
74 /// Destructor
75 
77 {
78  // must be defined here to correctly call RWebWindow destructor
79 }
80 
81 ///////////////////////////////////////////////////////////////////////////////////////////
82 /// Set size of web browser window as string like "800x600"
83 
85 {
86  auto separ = str.find("x");
87  if ((separ == std::string::npos) || (separ == 0) || (separ == str.length()-1)) return false;
88 
89  int width = 0, height = 0;
90 
91  try {
92  width = std::stoi(str.substr(0,separ));
93  height = std::stoi(str.substr(separ+1));
94  } catch(...) {
95  return false;
96  }
97 
98  if ((width<=0) || (height<=0))
99  return false;
100 
101  SetSize(width, height);
102  return true;
103 }
104 
105 ///////////////////////////////////////////////////////////////////////////////////////////
106 /// Set position of web browser window as string like "100,100"
107 
109 {
110  auto separ = str.find(",");
111  if ((separ == std::string::npos) || (separ == 0) || (separ == str.length()-1)) return false;
112 
113  int x = 0, y = 0;
114 
115  try {
116  x = std::stoi(str.substr(0,separ));
117  y = std::stoi(str.substr(separ+1));
118  } catch(...) {
119  return false;
120  }
121 
122  if ((x<0) || (y<0))
123  return false;
124 
125  SetPos(x, y);
126  return true;
127 }
128 
129 ///////////////////////////////////////////////////////////////////////////////////////////
130 /// Set browser kind as string argument
131 /// Recognized values:
132 /// chrome - use Google Chrome web browser, supports headless mode from v60, default
133 /// firefox - use Mozilla Firefox browser, supports headless mode from v57
134 /// native - (or empty string) either chrome or firefox, only these browsers support batch (headless) mode
135 /// browser - default system web-browser, no batch mode
136 /// safari - Safari browser on Mac
137 /// cef - Chromium Embeded Framework, local display, local communication
138 /// qt5 - Qt5 WebEngine, local display, local communication
139 /// local - either cef or qt5
140 /// <prog> - any program name which will be started instead of default browser, like /usr/bin/opera
141 
143 {
144  std::string kind = _kind;
145 
146  auto pos = kind.find("?");
147  if (pos == 0) {
148  SetUrlOpt(kind.substr(1));
149  kind.clear();
150  }
151 
152  pos = kind.find("size:");
153  if (pos != std::string::npos) {
154  auto epos = kind.find_first_of(" ;", pos+5);
155  if (epos == std::string::npos) epos = kind.length();
156  SetSizeAsStr(kind.substr(pos+5, epos-pos-5));
157  kind.erase(pos, epos-pos);
158  }
159 
160  pos = kind.find("pos:");
161  if (pos != std::string::npos) {
162  auto epos = kind.find_first_of(" ;", pos+4);
163  if (epos == std::string::npos) epos = kind.length();
164  SetPosAsStr(kind.substr(pos+4, epos-pos-4));
165  kind.erase(pos, epos-pos);
166  }
167 
168  // remove all trailing spaces
169  while ((kind.length() > 0) && (kind[kind.length()-1] == ' '))
170  kind.resize(kind.length()-1);
171 
172  // remove any remaining spaces?
173  // kind.erase(remove_if(kind.begin(), kind.end(), std::isspace), kind.end());
174 
175  if (kind.empty())
176  kind = gROOT->GetWebDisplay().Data();
177 
178  if (kind == "local")
179  SetBrowserKind(kLocal);
180  else if (kind.empty() || (kind == "native"))
181  SetBrowserKind(kNative);
182  else if (kind == "firefox")
183  SetBrowserKind(kFirefox);
184  else if ((kind == "chrome") || (kind == "chromium"))
185  SetBrowserKind(kChrome);
186  else if ((kind == "cef") || (kind == "cef3"))
187  SetBrowserKind(kCEF);
188  else if ((kind == "qt") || (kind == "qt5"))
189  SetBrowserKind(kQt5);
190  else if ((kind == "embed") || (kind == "embedded"))
191  SetBrowserKind(kEmbedded);
192  else if (!SetSizeAsStr(kind))
193  SetCustomExec(kind);
194 
195  return *this;
196 }
197 
198 /////////////////////////////////////////////////////////////////////
199 /// Returns configured browser name
200 
202 {
203  switch (GetBrowserKind()) {
204  case kChrome: return "chrome";
205  case kFirefox: return "firefox";
206  case kNative: return "native";
207  case kCEF: return "cef";
208  case kQt5: return "qt5";
209  case kLocal: return "local";
210  case kStandard: return "default";
211  case kEmbedded: return "embed";
212  case kCustom:
213  auto pos = fExec.find(" ");
214  return (pos == std::string::npos) ? fExec : fExec.substr(0,pos);
215  }
216 
217  return "";
218 }
219 
220 ///////////////////////////////////////////////////////////////////////////////////////////
221 /// Assign window and channel id where other window will be embed
222 
223 void ROOT::Experimental::RWebDisplayArgs::SetMasterWindow(std::shared_ptr<RWebWindow> master, int channel)
224 {
225  SetBrowserKind(kEmbedded);
226  fMaster = master;
227  fMasterChannel = channel;
228 }
229 
230 ///////////////////////////////////////////////////////////////////////////////////////////
231 /// Append string to url options
232 /// Add "&" as separator if any options already exists
233 
235 {
236  if (opt.empty()) return;
237 
238  if (!fUrlOpt.empty())
239  fUrlOpt.append("&");
240 
241  fUrlOpt.append(opt);
242 }
243 
244 ///////////////////////////////////////////////////////////////////////////////////////////
245 /// Returns full url, which is combined from URL and extra URL options
246 /// Takes into account "#" symbol in url - options are inserted before that symbol
247 
249 {
250  std::string url = GetUrl(), urlopt = GetUrlOpt();
251  if (url.empty() || urlopt.empty()) return url;
252 
253  auto rpos = url.find("#");
254  if (rpos == std::string::npos) rpos = url.length();
255 
256  if (url.find("?") != std::string::npos)
257  url.insert(rpos, "&");
258  else
259  url.insert(rpos, "?");
260  url.insert(rpos+1, urlopt);
261 
262  return url;
263 }
264 
265 ///////////////////////////////////////////////////////////////////////////////////////////
266 /// Configure custom web browser
267 /// Either just name of browser which can be used like "opera"
268 /// or full execution string which must includes $url like "/usr/bin/opera $url"
269 
271 {
272  SetBrowserKind(kCustom);
273  fExec = exec;
274 }
275 
276 ///////////////////////////////////////////////////////////////////////////////////////////
277 /// returns custom executable to start web browser
278 
280 {
281  if (GetBrowserKind() != kCustom)
282  return "";
283 
284 #ifdef R__MACOSX
285  if ((fExec == "safari") || (fExec == "Safari"))
286  return "open -a Safari";
287 #endif
288 
289  return fExec;
290 }
RWebDisplayArgs & SetBrowserKind(const std::string &kind)
Set browser kind as string argument Recognized values: chrome - use Google Chrome web browser...
Holds different arguments for starting browser with RWebDisplayHandle::Display() method.
#define gROOT
Definition: TROOT.h:415
virtual ~RWebDisplayArgs()
Destructor.
std::string GetCustomExec() const
returns custom executable to start web browser
void SetMasterWindow(std::shared_ptr< RWebWindow > master, int channel=-1)
Assign window and channel id where other window will be embed.
Double_t x[n]
Definition: legend1.C:17
RWebDisplayArgs()
Default constructor - browser kind configured from gROOT->GetWebDisplay()
bool SetPosAsStr(const std::string &str)
Set position of web browser window as string like "100,100".
void AppendUrlOpt(const std::string &opt)
append extra url options, add "&" as separator if required
void SetCustomExec(const std::string &exec)
set custom executable to start web browser
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:120
std::string GetFullUrl() const
returns window url with append options
Double_t y[n]
Definition: legend1.C:17
bool SetSizeAsStr(const std::string &str)
Set size of web browser window as string like "800x600".
std::string GetBrowserName() const
Returns configured browser name.