source: trunk/MagicSoft/Mars/mbase/MStatusDisplay.cc@ 9345

Last change on this file since 9345 was 9302, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 96.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 4/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2003-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MStatusDisplay
28//
29// This status display can be used (and is used) to display results in
30// a tabbed window. The window can be written to and read from a root file
31// (see Read and Write) or printed as a postscript file (see SaveAsPS).
32//
33// To write gif files of C-Macros use SaveAsGif()/SaveAsPNG() or SaveAsC().
34// Direct printing to the default printer (via lpr) can be done by
35// PrintPS().
36//
37// It has also to half status lines which can be used to display the status
38// or something going on. Together with the status lines it has a progress
39// bar which can display the progress of a job or loop.
40// Access the progress bar by GetProgressBar()
41//
42// To add a new tab and get a pointer to the newly created TCanvas
43// use AddTab.
44//
45// If you have a MStatusDisplay and you are not sure whether it was
46// destroyed by the user meanwhile use:
47// gROOT->GetListOfSpecials()->FindObject(pointer);
48// Each MStatusDisplay is added to list list by its constructor and
49// removed from the list by the destructor.
50//
51// You can redirect an output to a MLog-logstream by calling SetLogStream().
52// To disable redirction call SetLogStream(NULL)
53//
54// Because updates to the tabs are only done/displayed if a tab is active
55// using the gui doesn't make things slower (<1%) if the first (legend
56// tab) is displayed. This gives you the possibility to look into
57// the current progress of a loop without loosing more time than the
58// single update of the tab.
59//
60/////////////////////////////////////////////////////////////////////////////
61#include "MStatusDisplay.h"
62
63#include <errno.h>
64#include <stdlib.h> // mktemp (Ubunto 8.10)
65
66#include <fstream> // fstream
67
68#include <TH1.h> // TH1::AddDirectory
69#include <TPDF.h> // TPDF
70#include <TSVG.h> // TSVG
71#include <TEnv.h> // TEnv
72#include <TLine.h> // TLine
73#include <TMath.h>
74#include <TText.h> // TText
75#include <TFile.h> // gFile
76#include <TFrame.h> // TFrame
77#include <TStyle.h> // gStyle
78#include <TCanvas.h> // TCanvas
79#include <TSystem.h> // gSystem
80#include <TDatime.h> // TDatime
81#include <TRandom.h> // TRandom
82#include <TRegexp.h> // TRegexp
83#include <TThread.h> // TThread::Self()
84#include <TBrowser.h> // TBrowser
85#include <TObjArray.h> // TObjArray
86#include <TPostScript.h> // TPostScript
87#include <TMethodCall.h> // TMethodCall
88
89#include <TInterpreter.h> // gInterpreter
90
91#include <TGTab.h> // TGTab
92#include <TGLabel.h> // TGLabel
93#include <TG3DLine.h> // TGHorizontal3DLine
94#include <TGButton.h> // TGPictureButton
95#include <TGTextView.h> // TGTextView
96#include <TGComboBox.h> // TGComboBox
97#include <TGStatusBar.h> // TGStatusBar
98#include <TGFileDialog.h> // TGFileDialog
99#include <TGProgressBar.h> // TGHProgressBar
100#include <TGTextEditDialogs.h> // TGPrintDialog
101#include <TRootEmbeddedCanvas.h> // TRootEmbeddedCanvas
102
103#include "MString.h"
104
105#include "MLog.h" // MLog
106#include "MLogManip.h" // inf, warn, err
107
108#include "MGList.h" // MGList
109#include "MGMenu.h" // MGMenu, TGMenu
110#include "MSearch.h" // MSearch
111#include "MParContainer.h" // MParContainer::GetDescriptor
112#include "MStatusArray.h" // MStatusArray
113
114#if ROOT_VERSION_CODE <= ROOT_VERSION(5,22,00)
115#include "../mhbase/MH.h"
116#endif
117
118#undef DEBUG
119//#define DEBUG
120
121ClassImp(MStatusDisplay);
122
123using namespace std;
124
125// ------------ Workaround for a non working TGTextView::Search -------------
126#if ROOT_VERSION_CODE < ROOT_VERSION(3,02,05)
127class MGTextView : public TGTextView
128{
129public:
130 MGTextView(const TGWindow *parent, UInt_t w, UInt_t h, Int_t id = -1,
131 UInt_t sboptions = 0, ULong_t back = GetWhitePixel()) :
132 TGTextView(parent, w, h, id, sboptions, back) {}
133 MGTextView(const TGWindow *parent, UInt_t w, UInt_t h, TGText *text,
134 Int_t id = -1, UInt_t sboptions = 0, ULong_t back = GetWhitePixel()) :
135 TGTextView(parent, w, h, text, id, sboptions, back) {}
136 MGTextView(const TGWindow *parent, UInt_t w, UInt_t h, const char *string,
137 Int_t id = -1, UInt_t sboptions = 0, ULong_t back = GetWhitePixel()) :
138 TGTextView(parent, w, h, string, id, sboptions, back) {}
139
140 void Mark(Long_t xPos, Long_t yPos) { TGTextView::Mark(xPos, yPos); }
141 void UnMark() { TGTextView::UnMark(); }
142
143 Bool_t Search(const char *string, Bool_t direction, Bool_t caseSensitive)
144 {
145 // Taken from TGTextView::Search and modified.
146
147 TGLongPosition pos, pos2;
148 pos2.fX = pos2.fY = 0;
149 if (fIsMarked) {
150 if (!direction)
151 {
152 pos2.fX = fMarkedStart.fX;
153 pos2.fY = fMarkedStart.fY;
154 }
155 else
156 {
157 pos2.fX = fMarkedEnd.fX + 1;
158 pos2.fY = fMarkedEnd.fY;
159 }
160 }
161 if (!fText->Search(&pos, pos2, string, direction, caseSensitive))
162 return kFALSE;
163 UnMark();
164 fIsMarked = kTRUE;
165 fMarkedStart.fY = fMarkedEnd.fY = pos.fY;
166 fMarkedStart.fX = pos.fX;
167 fMarkedEnd.fX = fMarkedStart.fX + strlen(string);
168 pos.fY = ToObjYCoord(fVisible.fY);
169 if ((fMarkedStart.fY < pos.fY) ||
170 (ToScrYCoord(fMarkedStart.fY) >= (Int_t)fCanvas->GetHeight()))
171 pos.fY = fMarkedStart.fY;
172 pos.fX = ToObjXCoord(fVisible.fX, pos.fY);
173 if ((fMarkedStart.fX < pos.fX) ||
174 (ToScrXCoord(fMarkedStart.fX, pos.fY) >= (Int_t)fCanvas->GetWidth()))
175 pos.fX = fMarkedStart.fX;
176
177 SetVsbPosition((ToScrYCoord(pos.fY) + fVisible.fY)/fScrollVal.fY);
178 SetHsbPosition((ToScrXCoord(pos.fX, pos.fY) + fVisible.fX)/fScrollVal.fX);
179 DrawRegion(0, (Int_t)ToScrYCoord(fMarkedStart.fY), fCanvas->GetWidth(),
180 UInt_t(ToScrYCoord(fMarkedEnd.fY+1) - ToScrYCoord(fMarkedEnd.fY)));
181
182 return kTRUE;
183 }
184};
185#else
186#define MGTextView TGTextView
187#endif
188
189// --------------------------------------------------------------------------
190
191TGCompositeFrame *MStatusDisplay::GetTabContainer(const char *name) const
192{
193#if ROOT_VERSION_CODE < ROOT_VERSION(4,03,05)
194 if (!fTab)
195 return 0;
196
197 TGFrameElement *el;
198 TGTabElement *tab = 0;
199 TGCompositeFrame *comp = 0;
200
201 TIter next(fTab->GetList());
202 next(); // skip first container
203
204 while ((el = (TGFrameElement *) next())) {
205 el = (TGFrameElement *) next();
206 comp = (TGCompositeFrame *) el->fFrame;
207 next();
208 tab = (TGTabElement *)el->fFrame;
209 if (name == tab->GetText()->GetString()) {
210 return comp;
211 }
212 }
213
214 return 0;
215#else
216 return fTab ? fTab->GetTabContainer(name) : 0;
217#endif
218}
219
220TGTabElement *MStatusDisplay::GetTabTab(const char *name) const
221{
222#if ROOT_VERSION_CODE < ROOT_VERSION(4,03,05)
223 if (!fTab)
224 return 0;
225
226 TGFrameElement *el;
227 TGTabElement *tab = 0;
228
229 TIter next(fTab->GetList());
230 next(); // skip first container
231
232 while ((el = (TGFrameElement *) next())) {
233 next();
234 tab = (TGTabElement *)el->fFrame;
235 if (name == tab->GetText()->GetString()) {
236 return tab;
237 }
238 }
239
240 return 0;
241#else
242 return fTab ? fTab->GetTabTab(name) : 0;
243#endif
244}
245// --------------------------------------------------------------------------
246
247
248// --------------------------------------------------------------------------
249//
250// Add menu bar to the GUI
251//
252void MStatusDisplay::AddMenuBar()
253{
254 //
255 // File Menu
256 //
257 MGPopupMenu *filemenu = new MGPopupMenu(gClient->GetRoot());
258 filemenu->AddEntry("New &Canvas", kFileCanvas);
259 filemenu->AddEntry("New &Browser", kFileBrowser);
260 filemenu->AddEntry("New &Tab", kFileTab);
261 filemenu->AddSeparator();
262
263 const TString fname(MString::Format("Save %s.", gROOT->GetName()));
264 MGPopupMenu *savemenu = new MGPopupMenu(gClient->GetRoot());
265 savemenu->AddEntry(MString::Format("%s&ps", fname.Data()), kFileSaveAsPS);
266 savemenu->AddEntry(MString::Format("%sp&df", fname.Data()), kFileSaveAsPDF);
267 savemenu->AddEntry(MString::Format("%s&svg", fname.Data()), kFileSaveAsSVG);
268 savemenu->AddSeparator();
269 savemenu->AddEntry(MString::Format("%sp&ng", fname.Data()), kFileSaveAsPNG);
270 savemenu->AddEntry(MString::Format("%s&gif", fname.Data()), kFileSaveAsGIF);
271 savemenu->AddEntry(MString::Format("%s&jpg", fname.Data()), kFileSaveAsJPG);
272 savemenu->AddEntry(MString::Format("%s&xpm", fname.Data()), kFileSaveAsXPM);
273 savemenu->AddEntry(MString::Format("%s&tiff",fname.Data()), kFileSaveAsTIFF);
274 savemenu->AddEntry(MString::Format("%s&bmp", fname.Data()), kFileSaveAsBMP);
275 savemenu->AddEntry(MString::Format("%sx&ml", fname.Data()), kFileSaveAsXML);
276 savemenu->AddEntry(MString::Format("%scs&v", fname.Data()), kFileSaveAsCSV);
277 savemenu->AddSeparator();
278 savemenu->AddEntry(MString::Format("%s&C", fname.Data()), kFileSaveAsC);
279 savemenu->AddEntry(MString::Format("%s&root", fname.Data()), kFileSaveAsRoot);
280 savemenu->Associate(this);
281
282 filemenu->AddEntry("&Open...", kFileOpen);
283 filemenu->AddPopup("&Save", savemenu);
284 filemenu->AddEntry("Save &As...", kFileSaveAs);
285 filemenu->AddSeparator();
286 filemenu->AddEntry("&Reset", kFileReset);
287 filemenu->AddSeparator();
288 filemenu->AddEntry("&Print", kFilePrint);
289 filemenu->AddSeparator();
290 filemenu->AddEntry("C&lose", kFileClose);
291 filemenu->AddEntry("E&xit", kFileExit);
292 filemenu->Associate(this);
293
294 //
295 // Tab Menu
296 //
297 MGPopupMenu *tabmenu = new MGPopupMenu(gClient->GetRoot());
298 tabmenu->AddEntry("Next [&+]", kTabNext);
299 tabmenu->AddEntry("Previous [&-]", kTabPrevious);
300 tabmenu->AddSeparator();
301
302 const TString fname2(MString::Format("Save %s-i.", gROOT->GetName()));
303 MGPopupMenu *savemenu2 = new MGPopupMenu(gClient->GetRoot());
304 savemenu2->AddEntry(MString::Format("%s&ps", fname2.Data()), kTabSaveAsPS);
305 savemenu2->AddEntry(MString::Format("%sp&df", fname2.Data()), kTabSaveAsPDF);
306 savemenu2->AddEntry(MString::Format("%s&svg", fname2.Data()), kTabSaveAsSVG);
307 savemenu2->AddSeparator();
308 savemenu2->AddEntry(MString::Format("%sp&ng", fname2.Data()), kTabSaveAsPNG);
309 savemenu2->AddEntry(MString::Format("%s&gif", fname2.Data()), kTabSaveAsGIF);
310 savemenu2->AddEntry(MString::Format("%s&jpg", fname2.Data()), kTabSaveAsJPG);
311 savemenu2->AddEntry(MString::Format("%s&xpm", fname2.Data()), kTabSaveAsXPM);
312 savemenu2->AddEntry(MString::Format("%s&tiff",fname2.Data()), kTabSaveAsTIFF);
313 savemenu2->AddEntry(MString::Format("%s&bmp", fname2.Data()), kTabSaveAsBMP);
314 savemenu2->AddEntry(MString::Format("%sx&ml", fname2.Data()), kTabSaveAsXML);
315 savemenu2->AddEntry(MString::Format("%scs&v", fname2.Data()), kTabSaveAsCSV);
316 savemenu2->AddSeparator();
317 savemenu2->AddEntry(MString::Format("%s&C", fname2.Data()), kTabSaveAsC);
318 savemenu2->AddEntry(MString::Format("%s&root", fname2.Data()), kTabSaveAsRoot);
319 savemenu2->Associate(this);
320
321 tabmenu->AddPopup("&Save", savemenu2);
322 tabmenu->AddEntry("Save tab &As...", kTabSaveAs);
323 tabmenu->AddSeparator();
324 tabmenu->AddEntry("&Remove", kTabRemove);
325 tabmenu->AddSeparator();
326 tabmenu->AddEntry("&Print", kTabPrint);
327 tabmenu->Associate(this);
328
329 //
330 // Loop Menu
331 //
332 MGPopupMenu *loopmenu = new MGPopupMenu(gClient->GetRoot());
333 loopmenu->AddEntry("&Pause", kLoopPause);
334 loopmenu->AddEntry("Single S&tep", kLoopStep);
335 loopmenu->AddSeparator();
336 loopmenu->AddEntry("&Stop", kLoopStop);
337 loopmenu->Associate(this);
338
339 loopmenu->DisableEntry(kLoopStep);
340
341 //
342 // Loop Menu
343 //
344 MGPopupMenu *sizemenu = new MGPopupMenu(gClient->GetRoot());
345 sizemenu->AddEntry("Fit to 640x&480", kSize640);
346 sizemenu->AddEntry("Fit to 768x&576", kSize768);
347 sizemenu->AddEntry("Fit to 800x&600", kSize800);
348 sizemenu->AddEntry("Fit to 960x7&20", kSize960);
349 sizemenu->AddEntry("Fit to 1024x&768", kSize1024);
350 sizemenu->AddEntry("Fit to 1152x&864", kSize1152);
351 sizemenu->AddEntry("Fit to 1280x&1024", kSize1280);
352 sizemenu->AddEntry("Fit to 1400x1050", kSize1400);
353 sizemenu->AddEntry("Fit to 1600x1200", kSize1600);
354 sizemenu->AddEntry("Fit to &Desktop", kSizeOptimum);
355 sizemenu->Associate(this);
356
357 //
358 // Log Menu
359 //
360 MGPopupMenu *logmenu = new MGPopupMenu(gClient->GetRoot());
361 logmenu->AddEntry("&Copy Selected", kLogCopy);
362 logmenu->AddEntry("Cl&ear all", kLogClear);
363 logmenu->AddSeparator();
364 logmenu->AddEntry("Select &All", kLogSelect);
365 logmenu->AddSeparator();
366 logmenu->AddEntry("&Find...", kLogFind);
367 logmenu->AddSeparator();
368 logmenu->AddEntry("&Save", kLogSave);
369 logmenu->AddEntry("Save &append", kLogAppend);
370 logmenu->AddSeparator();
371 logmenu->AddEntry("&Print", kLogPrint);
372 logmenu->Associate(this);
373
374 //
375 // Menu Bar
376 //
377 TGLayoutHints *layitem = new TGLayoutHints(kLHintsNormal, 0, 4, 0, 0);
378 fList->Add(layitem);
379
380 fMenuBar = new MGMenuBar(this, 1, 1, kHorizontalFrame);
381 fMenuBar->AddPopup("&File", filemenu, layitem);
382 fMenuBar->AddPopup("Lo&g", logmenu, layitem);
383 fMenuBar->AddPopup("&Size", sizemenu, layitem);
384 fMenuBar->AddPopup("&Tab", tabmenu, layitem);
385 fMenuBar->AddPopup("&Loop", loopmenu, layitem);
386 fMenuBar->BindKeys(this);
387 AddFrame(fMenuBar);
388
389 //
390 // Line below menu bar
391 //
392 TGLayoutHints *laylinesep = new TGLayoutHints(kLHintsTop|kLHintsExpandX);
393 fList->Add(laylinesep);
394
395 TGHorizontal3DLine *linesep = new TGHorizontal3DLine(this);
396 AddFrame(linesep, laylinesep);
397
398 //
399 // Add everything to autodel list
400 //
401 fList->Add(savemenu);
402 fList->Add(savemenu2);
403 fList->Add(filemenu);
404 fList->Add(loopmenu);
405 fList->Add(sizemenu);
406 fList->Add(fMenuBar);
407 fList->Add(tabmenu);
408 fList->Add(logmenu);
409 fList->Add(linesep);
410}
411
412// --------------------------------------------------------------------------
413//
414// Adds an empty TGCompositeFrame which might be filled by the user
415//
416void MStatusDisplay::AddUserFrame()
417{
418 TGLayoutHints *lay=new TGLayoutHints(kLHintsExpandX);
419 fList->Add(lay);
420
421 fUserFrame = new TGCompositeFrame(this, 1, 1);
422 AddFrame(fUserFrame, lay);
423 fList->Add(fUserFrame);
424}
425
426// --------------------------------------------------------------------------
427//
428// Add the title tab
429//
430void MStatusDisplay::AddMarsTab()
431{
432 // Create Tab1
433 TGCompositeFrame *f = fTab->AddTab("-=MARS=-");
434
435 // Add list of tabs
436
437 TGComboBox *filter = new TGComboBox(f, kTabs);
438 fList->Add(filter);
439 filter->Associate(this);
440 filter->AddEntry("-=MARS=-", 0);
441 filter->Select(0);
442
443
444 TGLayoutHints *lay3 = new TGLayoutHints(kLHintsCenterX|kLHintsTop, 10, 10, 10, 5);
445 fList->Add(lay3);
446 f->AddFrame(filter, lay3);
447
448 // Add MARS version
449 TGLabel *l = new TGLabel(f, MString::Format("Official Release: V%s", MARSVER));
450 fList->Add(l);
451
452 filter->SetWidth(5*l->GetWidth()/4);
453 filter->SetHeight(4*l->GetHeight()/3);
454 filter->GetListBox()->SetHeight(l->GetHeight()*16);
455
456 TGLayoutHints *layb = new TGLayoutHints(kLHintsCenterX|kLHintsTop, 10, 10, 5, 5);
457 fList->Add(layb);
458 f->AddFrame(l, layb);
459
460 // Add root version
461 l = new TGLabel(f, MString::Format("Using ROOT v%s", ROOT_RELEASE));
462 fList->Add(l);
463
464 TGLayoutHints *lay = new TGLayoutHints(kLHintsCenterX|kLHintsTop);
465 fList->Add(lay);
466 f->AddFrame(l, lay);
467
468 // Add Mars logo picture
469 const TGPicture *pic2 = fList->GetPicture("marslogo.xpm");
470 if (pic2)
471 {
472 TGPictureButton *mars = new TGPictureButton(f, pic2, kPicMars);
473 fList->Add(mars);
474 mars->Associate(this);
475
476 TGLayoutHints *lay2 = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY, 10, 10, 5, 5);
477 fList->Add(lay2);
478 f->AddFrame(mars, lay2);
479 }
480
481 // Add date and time
482 l = new TGLabel(f, TDatime().AsString());
483 fList->Add(l);
484 f->AddFrame(l, lay);
485
486 // Add copyright notice
487 l = new TGLabel(f, MString::Format("(c) MARS Software Development, 2000-%d", TDatime().GetYear()));
488 fList->Add(l);
489 f->AddFrame(l, layb);
490
491 TGLayoutHints *layc = new TGLayoutHints(kLHintsCenterX|kLHintsTop, 10, 10, 0, 5);
492 fList->Add(layc);
493
494 const char *txt = "<< Thomas Bretz >>";
495 l = new TGLabel(f, txt);
496 fList->Add(l);
497 f->AddFrame(l, layc);
498}
499
500// --------------------------------------------------------------------------
501//
502// Adds the logbook tab to the GUI if it was not added previously.
503//
504// The logbook is updated four times a second only if the tab is visible.
505//
506// You can redirect an output to a MLog-logstream by calling SetLogStream().
507// To disable redirction call SetLogStream(NULL)
508//
509// if enable==kFALSE the stdout is disabled/enabled. Otherwise stdout
510// is ignored.
511//
512void MStatusDisplay::SetLogStream(MLog *log, Bool_t enable)
513{
514 if (gROOT->IsBatch())
515 return;
516
517 if (log && fLogBox==NULL)
518 {
519 fLogIdx = fTab->GetNumberOfTabs();
520
521 // Create Tab1
522 TGCompositeFrame *f = AddRawTab("-Logbook-");//fTab->AddTab("-Logbook-");
523
524 // Create Text View
525 fLogBox = new MGTextView(f, 1, 1); // , -1, 0, TGFrame::GetDefaultFrameBackground());
526 if (fFont)
527 fLogBox->SetFont(fFont);
528 //fLogBox->Associate(this);
529
530 // Add List box to the tab
531 TGLayoutHints *lay = new TGLayoutHints(kLHintsNormal|kLHintsExpandX|kLHintsExpandY,2,2,2,2);
532 f->AddFrame(fLogBox, lay);
533
534 // layout and map tab
535 Layout();
536 MapSubwindows();
537
538 // make it visible
539 // FIXME: This is a workaround, because TApplication::Run is not
540 // thread safe against ProcessEvents. We assume, that if
541 // we are not in the Main-Thread ProcessEvents() is
542 // called by the TApplication Event Loop...
543 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
544 gClient->ProcessEventsFor(fTab);
545 }
546
547 if (log)
548 {
549 fLog = log;
550
551 log->SetOutputGui(fLogBox, kTRUE);
552 log->EnableOutputDevice(MLog::eGui);
553 if (!enable)
554 log->DisableOutputDevice(MLog::eStdout);
555
556 fLogTimer.Start();
557 }
558 else
559 {
560 fLogTimer.Stop();
561
562 fLog->DisableOutputDevice(MLog::eGui);
563 fLog->SetOutputGui(NULL);
564 if (!enable)
565 fLog->EnableOutputDevice(MLog::eStdout);
566
567 fLog = &gLog;
568 }
569}
570
571// --------------------------------------------------------------------------
572//
573// Add the Tabs and the predifined Tabs to the GUI
574//
575void MStatusDisplay::AddTabs()
576{
577 fTab = new TGTab(this, 300, 300);
578
579 AddMarsTab();
580
581 // Add fTab to Frame
582 TGLayoutHints *laytabs = new TGLayoutHints(kLHintsNormal|kLHintsExpandX|kLHintsExpandY, 5, 5, 5);
583 AddFrame(fTab, laytabs);
584
585 fList->Add(fTab);
586 fList->Add(laytabs);
587}
588
589// --------------------------------------------------------------------------
590//
591// Add the progress bar to the GUI. The Progress Bar range is set to
592// (0,1) as default.
593//
594void MStatusDisplay::AddProgressBar()
595{
596 TGLayoutHints *laybar=new TGLayoutHints(kLHintsExpandX, 5, 5, 5, 5);
597 fList->Add(laybar);
598
599 fBar=new TGHProgressBar(this);
600 fBar->SetRange(0, 1);
601 fBar->ShowPosition();
602 AddFrame(fBar, laybar);
603 fList->Add(fBar);
604}
605
606// --------------------------------------------------------------------------
607//
608// Set the progress bar position between 0 and 1. The Progress bar range
609// is assumed to be (0,1)
610//
611void MStatusDisplay::SetProgressBarPosition(Float_t p, Bool_t upd)
612{
613 if (!gClient || gROOT->IsBatch())
614 return;
615
616 fBar->SetPosition(p);
617 if (upd)
618 gClient->ProcessEventsFor(fBar);
619}
620
621// --------------------------------------------------------------------------
622//
623// Adds the status bar to the GUI
624//
625void MStatusDisplay::AddStatusBar()
626{
627 fStatusBar = new TGStatusBar(this, 1, 1);
628
629 //
630 // Divide it like the 'Golden Cut' (goldener Schnitt)
631 //
632 // 1-a a
633 // 1: ------|----
634 //
635 // a/(1-a) = (1-a)/1
636 // a^2+a-1 = 0
637 // a = (-1+-sqrt(1+4))/2 = sqrt(5)/2-1/2 = 0.618
638 //
639 Int_t p[] = {38-2, 62-8, 10};
640
641 fStatusBar->SetParts(p, 3);
642
643 TGLayoutHints *layb = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 5, 4, 0, 3);
644 AddFrame(fStatusBar, layb);
645
646 fList->Add(fStatusBar);
647 fList->Add(layb);
648}
649
650// --------------------------------------------------------------------------
651//
652// Change the text in the status line 1
653//
654void MStatusDisplay::SetStatusLine(const char *txt, Int_t i)
655{
656 if (gROOT->IsBatch())
657 return;
658 fStatusBar->SetText(txt, i);
659
660 // FIXME: This is a workaround, because TApplication::Run is not
661 // thread safe against ProcessEvents. We assume, that if
662 // we are not in the Main-Thread ProcessEvents() is
663 // called by the TApplication Event Loop...
664 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
665 gClient->ProcessEventsFor(fStatusBar);
666}
667
668// --------------------------------------------------------------------------
669//
670// Display information about the name of a container
671//
672void MStatusDisplay::SetStatusLine2(const MParContainer &cont)
673{
674 SetStatusLine2(MString::Format("%s: %s", cont.GetDescriptor().Data(), cont.GetTitle()));
675}
676
677// --------------------------------------------------------------------------
678//
679// Get TGPopupMenu as defined by name from fMenuBar
680//
681TGPopupMenu *MStatusDisplay::GetPopup(const char *name)
682{
683 if (!fMenuBar)
684 return 0;
685
686 TGPopupMenu *m = fMenuBar->GetPopup(name);
687 if (!m)
688 {
689 *fLog << warn << name << " doesn't exist in menu bar." << endl;
690 return 0;
691 }
692
693 return m;
694}
695
696// --------------------------------------------------------------------------
697//
698// Default constructor. Opens a window with a progress bar. Get a pointer
699// to the bar by calling GetBar. This pointer can be used for the
700// eventloop.
701//
702// Be carefull: killing or closing the window while the progress meter
703// is still in use may cause segmentation faults. Please kill the window
704// always by deleting the corresponding object.
705//
706// You can give either width or height. (Set the value not given to -1)
707// The other value is calculated accordingly. If width and height are
708// given height is ignored. If width=height=0 an optimum size from
709// the desktop size is calculated.
710//
711// Update time default: 10s
712//
713MStatusDisplay::MStatusDisplay(Int_t w, Int_t h, Long_t t)
714: TGMainFrame((TGWindow*)((gClient?gClient:new TGClient),NULL), 1, 1), fName("MStatusDisplay"), fLog(&gLog), fBar(NULL), fTab(NULL), fTimer(this, t, kTRUE), fStatus(kLoopNone), fLogIdx(-1), fLogTimer(this, 250, kTRUE), fLogBox(NULL), fIsLocked(0)
715{
716 // p==NULL means: Take gClient->GetRoot() if not in batch mode
717 // see TGWindow::TGWindow()
718
719 // Make sure that the display is removed via RecursiveRemove
720 // from whereever possible.
721 SetBit(kMustCleanup);
722
723 //
724 // This is a possibility for the user to check whether this
725 // object has already been deleted. It will be removed
726 // from the list in the destructor.
727 //
728 gROOT->GetListOfSpecials()->Add(this);
729
730 fFont = gVirtualX->LoadQueryFont("7x13bold");
731 fMutex = new TMutex;
732
733 //
734 // In case we are in batch mode use a list of canvases
735 // instead of the Root Embedded Canvases in the TGTab
736 //
737 fBatch = new TList;
738 fBatch->SetOwner();
739
740 //
741 // Create a list handling GUI widgets
742 //
743 fList = new MGList;
744 fList->SetOwner();
745
746 //
747 // Create the layout hint for the root embedded canavses
748 //
749 fLayCanvas = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY);
750 fList->Add(fLayCanvas);
751
752 //
753 // Add Widgets (from top to bottom)
754 //
755 // In newer root versions gClient!=NULL in batch mode!
756 if (!gClient || !gClient->GetRoot() || gROOT->IsBatch()) // BATCH MODE
757 {
758 Resize(644, 484);
759 return;
760 }
761
762 AddMenuBar();
763 AddUserFrame();
764 AddTabs();
765 AddProgressBar();
766 AddStatusBar();
767
768 //
769 // set the smallest and biggest size of the Main frame
770 // and move it to its appearance position
771 SetWMSizeHints(566, 476, 2048, 1536, 1, 1);
772 MoveResize(rand()%100+566, rand()%100+476, 566, 476);
773 if (h>0)
774 SetDisplayHeight(h);
775 if (w>0)
776 SetDisplayWidth(w);
777 if (w==0 && h==0)
778 SetOptimumSize();
779
780 //
781 // Now do an automatic layout of the widgets and display the window
782 //
783 Layout();
784 MapSubwindows();
785
786 SetWindowName("Status Display");
787 SetIconName("Status Display");
788
789 MapWindow();
790
791 // FIXME: This is a workaround, because TApplication::Run is not
792 // thread safe against ProcessEvents. We assume, that if
793 // we are not in the Main-Thread ProcessEvents() is
794 // called by the TApplication Event Loop...
795 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
796 gSystem->ProcessEvents();
797}
798
799// --------------------------------------------------------------------------
800//
801// Destruct the window with all its tiles. Also the Progress Bar object
802// is deleted.
803//
804MStatusDisplay::~MStatusDisplay()
805{
806 fTimer.Stop();
807
808#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,01)
809 fTab = NULL; // See HandleEvent
810#endif
811
812 //
813 // Delete object from global object table so it cannot
814 // be deleted by chance a second time
815 //
816 gInterpreter->DeleteGlobal(this);
817
818 //
819 // This is a possibility for the user to check whether this
820 // object has already been deleted. It has been added
821 // to the list in the constructor.
822 //
823 gROOT->GetListOfSpecials()->Remove(this);
824
825 SetLogStream(NULL);
826
827 //
828 // Delete the list of objects corresponding to this object
829 //
830 delete fList;
831
832 //
833 // Delete the list of canvases used in batch mode
834 // instead of the Root Embedded Canvases in the TGTab
835 //
836 delete fBatch;
837
838 //
839 // Delete the font used for the logging window
840 //
841 if (fFont)
842 gVirtualX->DeleteFont(fFont);
843
844 //
845 // Delete mutex
846 //
847 delete fMutex;
848}
849
850// --------------------------------------------------------------------------
851//
852// Takes a TGCompositeFrame as argument. Searches for the first
853// TRootEmbeddedCanvas which is contained by it and returns a pointer
854// to the corresponding TCanvas. If it isn't found NULL is returned.
855//
856TRootEmbeddedCanvas *MStatusDisplay::GetEmbeddedCanvas(TGCompositeFrame &cf)
857{
858 TIter Next(cf.GetList());
859
860 TGFrameElement *f;
861 while ((f=(TGFrameElement*)Next()))
862 if (f->fFrame->InheritsFrom(TRootEmbeddedCanvas::Class()))
863 return (TRootEmbeddedCanvas*)f->fFrame;
864
865 return NULL;
866}
867
868// --------------------------------------------------------------------------
869//
870// Takes a TGCompositeFrame as argument. Searches for the first
871// TRootEmbeddedCanvas which is contained by it and returns a pointer
872// to the corresponding TCanvas. If it isn't found NULL is returned.
873//
874TCanvas *MStatusDisplay::GetCanvas(TGCompositeFrame &cf)
875{
876 TRootEmbeddedCanvas *ec = GetEmbeddedCanvas(cf);
877 return ec ? ec->GetCanvas() : NULL;
878}
879
880// --------------------------------------------------------------------------
881//
882// Returns the range of tabs containing valid canvases for the condition
883// num.
884//
885void MStatusDisplay::GetCanvasRange(Int_t &from, Int_t &to, Int_t num) const
886{
887 const Int_t max = gROOT->IsBatch() ? fBatch->GetSize()+1 : fTab->GetNumberOfTabs();
888
889 from = num<0 ? 1 : num;
890 to = num<0 ? max : num+1;
891}
892
893// --------------------------------------------------------------------------
894//
895// Returns GetCanvas of the i-th Tab.
896//
897TCanvas *MStatusDisplay::GetCanvas(int i) const
898{
899 if (gROOT->IsBatch())
900 return (TCanvas*)fBatch->At(i-1);
901
902 if (i<0 || i>=fTab->GetNumberOfTabs())
903 {
904 *fLog << warn << "MStatusDisplay::GetCanvas: Out of range." << endl;
905 return NULL;
906 }
907
908 return GetCanvas(*fTab->GetTabContainer(i));
909}
910
911// --------------------------------------------------------------------------
912//
913// Returns j-th pad of the i-th Tab.
914// Sets the pad to fill an entire window.
915//
916// This function can be used if single pad's out of an MStatusDisplay
917// have to be stored to file.
918//
919// ATTENTION: This function modifies the requested tab in MStatusDisplay itself!
920//
921TVirtualPad *MStatusDisplay::GetFullPad(const Int_t i, const Int_t j)
922{
923 if (!GetCanvas(i))
924 {
925 *fLog << warn << "MStatusDisplay::GetFullPad: i-th canvas not dound." << endl;
926 return NULL;
927 }
928
929 TVirtualPad *vpad = GetCanvas(i)->GetPad(j);
930 if (!vpad)
931 {
932 *fLog << warn << "MStatusDisplay::GetFullPad: Pad is out of range." << endl;
933 return NULL;
934 }
935
936 vpad->SetPad(0.,0.,1.,1.);
937 return vpad;
938}
939
940// --------------------------------------------------------------------------
941//
942// Searches for a TRootEmbeddedCanvas in the TGCompositeFramme of the
943// Tab with the name 'name'. Returns the corresponding TCanvas or
944// NULL if something isn't found.
945//
946TCanvas *MStatusDisplay::GetCanvas(const TString &name) const
947{
948 if (gROOT->IsBatch())
949 return (TCanvas*)fBatch->FindObject(name);
950
951 TGFrameElement *f;
952 TIter Next(fTab->GetList());
953 while ((f=(TGFrameElement*)Next()))
954 {
955 TObject *frame = f->fFrame;
956 if (!frame->InheritsFrom(TGTabElement::Class()))
957 continue;
958
959 TGTabElement *tab = (TGTabElement*)frame;
960 if (tab->GetString()==name)
961 break;
962 }
963
964 // Search for the next TGCompositeFrame in the list
965 while ((f=(TGFrameElement*)Next()))
966 {
967 TObject *frame = f->fFrame;
968 if (frame->InheritsFrom(TGCompositeFrame::Class()))
969 return GetCanvas(*(TGCompositeFrame*)frame);
970 }
971
972 return NULL;
973}
974
975// --------------------------------------------------------------------------
976//
977// Calls TCanvas::cd(), for the canvas returned by GetCanvas.
978//
979Bool_t MStatusDisplay::CdCanvas(const TString &name)
980{
981 TCanvas *c = GetCanvas(name);
982 if (!c)
983 return kFALSE;
984
985 c->cd();
986 return kTRUE;
987}
988
989// --------------------------------------------------------------------------
990//
991// Return the number of user added tabs (not that in batch mode this
992// exclude tabs without a canvas)
993//
994Int_t MStatusDisplay::GetNumTabs() const
995{
996 return gROOT->IsBatch() ? fBatch->GetEntries() : fTab->GetNumberOfTabs()-1;
997}
998
999TGCompositeFrame *MStatusDisplay::AddRawTab(const char *name)
1000{
1001 // Add new tab
1002 TGCompositeFrame *f = fTab->AddTab(name);
1003
1004 TGComboBox *box = (TGComboBox*)fList->FindWidget(kTabs);
1005 box->AddEntry(name, box->GetListBox()->GetNumberOfEntries());
1006
1007 // layout and map new tab
1008 Layout();
1009 MapSubwindows();
1010 Layout();
1011
1012 // display new tab in the main frame
1013 // FIXME: This is a workaround, because TApplication::Run is not
1014 // thread safe against ProcessEvents. We assume, that if
1015 // we are not in the Main-Thread ProcessEvents() is
1016 // called by the TApplication Event Loop...
1017 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
1018 gClient->ProcessEventsFor(fTab);
1019
1020 *fLog << inf3 << "Adding Raw Tab '" << name << "' (" << f->GetWidth() << "x";
1021 *fLog << f->GetHeight() << ")" << endl;
1022
1023 // return pointer to new canvas
1024 return f;
1025}
1026
1027// --------------------------------------------------------------------------
1028//
1029// This function was connected to all created canvases. It is used
1030// to redirect GetObjectInfo into our own status bar.
1031//
1032// The 'connection' is done in AddTab
1033//
1034void MStatusDisplay::EventInfo(Int_t /*event*/, Int_t px, Int_t py, TObject *selected)
1035{
1036 // Writes the event status in the status bar parts
1037 if (!selected)
1038 return;
1039
1040 TCanvas *c = (TCanvas*)gTQSender;
1041
1042 TVirtualPad* save=gPad;
1043
1044 gPad = c ? c->GetSelectedPad() : NULL;
1045
1046 if (gPad)
1047 {
1048 // Find the object which will get picked by the GetObjectInfo
1049 // due to buffer overflows in many root-versions
1050 // in TH1 and TProfile we have to work around and implement
1051 // our own GetObjectInfo which make everything a bit more
1052 // complicated.
1053#if ROOT_VERSION_CODE > ROOT_VERSION(5,22,00)
1054 SetStatusLine2(selected->GetObjectInfo(px,py));
1055#else
1056 TObjLink *link=0;
1057 static_cast<TPad*>(gPad)->Pick(px, py, link);
1058
1059 const TObject *o = link ? link->GetObject() : 0;
1060 if (o)
1061 SetStatusLine2(MH::GetObjectInfo(px, py, *o));
1062#endif
1063 }
1064
1065 gPad=save;
1066}
1067
1068// --------------------------------------------------------------------------
1069//
1070// Adds a new tab with the name 'name'. Adds a TRootEmbeddedCanvas to the
1071// tab and returns a reference to the corresponding TCanvas.
1072//
1073TCanvas &MStatusDisplay::AddTab(const char *name, const char *title)
1074{
1075 /*
1076 if (GetCanvas(name))
1077 {
1078 *fLog << warn;
1079 *fLog << "WARNING - A canvas '" << name << "' is already existing in the Status Display." << endl;
1080 *fLog << " This can cause unexpected crahes!" << endl;
1081 }*/
1082
1083 if (gROOT->IsBatch())
1084 {
1085 // 4 = 2*default border width of a canvas
1086 const UInt_t cw = GetWidth();
1087 const UInt_t ch = 2*cw/3 + 25; // 25: Menu, etc
1088
1089 // The constructor of TCanvas adds the canvas to the global list
1090 // of canvases gROOT->GetListOfCanvases(). If a canvas with an
1091 // identical name exists already in this list, the canvas is
1092 // deleted. In normal operation this might make sense and doesn't harm
1093 // because the embedded canvases behave different.
1094 // By creating the canvas without a name it is made sure that no
1095 // older canvas/tab vanished silently from the system (deleted from
1096 // the construtor). To make the handling of our canvases nevertheless
1097 // work well the name is set later. The list of canvases is also
1098 // part of the list of cleanups, thus fBatch need not to be added
1099 // to the list of cleanups.
1100 TCanvas *c = new TCanvas("", title?title:"", -cw, ch);
1101 c->SetName(name);
1102 c->SetFillColor(10); // White
1103 c->SetFrameBorderMode(0);
1104 c->SetBorderMode(0);
1105 fBatch->Add(c);
1106
1107 *fLog << inf3 << "Adding Canvas '" << name << "' (" << c->GetWw() << "x";
1108 *fLog << c->GetWh() << ", TCanvas=" << c << ")" << endl;
1109
1110 // Remove the canvas from the global list to make sure it is
1111 // not found by gROOT->FindObject
1112 //gROOT->GetListOfCanvases()->Remove(c);
1113 //gROOT->GetListOfCleanups()->Add(c);
1114
1115 return *c;
1116 }
1117
1118 // Add new tab
1119 TGCompositeFrame *f = fTab->AddTab(name);
1120
1121 // create root embedded canvas and add it to the tab
1122 TRootEmbeddedCanvas *ec = new TRootEmbeddedCanvas(name, f, f->GetWidth(), f->GetHeight(), kSunkenFrame);
1123 f->AddFrame(ec, fLayCanvas);
1124 fList->Add(ec);
1125
1126 // set background and border mode of the canvas
1127 TCanvas &c = *ec->GetCanvas();
1128
1129 if (title)
1130 c.SetTitle(title);
1131
1132 c.SetFillColor(10); // White
1133 c.SetFrameBorderMode(0);
1134 c.SetBorderMode(0);
1135
1136 // If kNoContextMenu set set kNoContextMenu of the canvas
1137 if (TestBit(kNoContextMenu))
1138 c.SetBit(kNoContextMenu);
1139
1140 // Connect all TCanvas::ProcessedEvent to this->EventInfo
1141 // This means, that after TCanvas has processed an event
1142 // EventInfo of this class is called, see TCanvas::HandleInput
1143 c.Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
1144 "MStatusDisplay", this, "EventInfo(Int_t,Int_t,Int_t,TObject*)");
1145
1146 // Make sure that root itself doesn't try to call GetObjectInfo
1147 // This is now handled from EventsInfo. This is necessary
1148 // due to the buffer overflow bug in GetObjectInfo of
1149 // TProfile and TH1
1150 c.ResetBit(TCanvas::kShowEventStatus);
1151
1152 // Remove the canvas from the global list to make sure it is
1153 // not found by gROOT->FindObject
1154 //gROOT->GetListOfCanvases()->Remove(&c);
1155 //gROOT->GetListOfCleanups()->Add(&c);
1156
1157 TGComboBox *box = (TGComboBox*)fList->FindWidget(kTabs);
1158 box->AddEntry(name, box->GetListBox()->GetNumberOfEntries());
1159
1160 // layout and map new tab
1161 Layout(); // seems to layout the TGCompositeFrame
1162 MapSubwindows(); // maps the TGCompositeFrame
1163 Layout(); // layout the embedded canvas in the frame
1164
1165 // display new tab in the main frame
1166 // FIXME: This is a workaround, because TApplication::Run is not
1167 // thread safe against ProcessEvents. We assume, that if
1168 // we are not in the Main-Thread ProcessEvents() is
1169 // called by the TApplication Event Loop...
1170 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
1171 gClient->ProcessEventsFor(fTab);
1172
1173 *fLog << inf3 << "Adding Tab '" << name << "' (" << f->GetWidth() << "x";
1174 *fLog << f->GetHeight() << ", TCanvas=" << &c << ")" << endl;
1175
1176 // return pointer to new canvas
1177 return c;
1178}
1179
1180// --------------------------------------------------------------------------
1181//
1182// Update a canvas in a tab, takes the corresponding TGCompositeFrame
1183// as an argument. This is necessary, because not all functions
1184// changing the contents of a canvas or pad can call SetModified()
1185// for the corresponding tab. If this is not called correctly the
1186// tab won't be updated calling TCanvas::Update(). So we simply
1187// redraw it by our own (instead we could recursively call
1188// TPad::Modified() for everything contained by the TCanvas and
1189// call TCanvas::Update() afterwards)
1190//
1191void MStatusDisplay::UpdateTab(TGCompositeFrame *f)
1192{
1193 if (!f)
1194 return;
1195
1196 TCanvas *c=GetCanvas(*f);
1197 if (!c)
1198 return;
1199
1200 //
1201 // If we are in a multithreaded environment (gThreadXAR) we
1202 // have to make sure, that thus function is called from
1203 // the main thread.
1204 //
1205 if (gThreadXAR)
1206 {
1207 // Tell the X-Requester how to call this method
1208 TString str = MString::Format("%d", (ULong_t)f);
1209
1210 TMethodCall call(IsA(), "UpdateTab", "NULL");
1211 void *arr[4] = { NULL, &call, this, (void*)(const char*)str };
1212
1213 // If this is not the main thread return
1214 if (((*gThreadXAR)("METH", 4, arr, NULL)))
1215 return;
1216 }
1217
1218 //
1219 // Secure calls to update the tabs against itself, at least
1220 // c->Paint() or c->Flush() may crash X (bad drawable).
1221 // This makes sure, that a X call is not interuppted by
1222 // another X-call which was started from an gui interrrupt
1223 // in the same thread
1224 //
1225 if (fMutex->TryLock())
1226 return;
1227
1228#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,02)
1229 TPad *padsav = (TPad*)gPad;
1230 if (!gPad)
1231 c->cd();
1232#endif
1233
1234 if (!c->IsBatch())
1235 c->FeedbackMode(kFALSE); // Goto double buffer mode
1236
1237 //
1238 // Doing this ourself gives us the possibility to repaint
1239 // the canvas in any case (Paint() instead of PaintModified())
1240 //
1241 c->Paint(); // Repaint all pads
1242 c->Flush(); // Copy all pad pixmaps to the screen
1243
1244#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,02)
1245 if (padsav)
1246 padsav->cd();
1247 else
1248 gPad=NULL;
1249#endif
1250
1251 //c->SetCursor(kCross);
1252
1253 // Old version
1254 //c->Modified();
1255 //c->Update();
1256 //c->Paint();
1257
1258 fMutex->UnLock();
1259}
1260
1261TString MStatusDisplay::PrintDialog(TString &p, TString &c, TString &t, const char *ext)
1262{
1263 // If not in batch mode open a user dialog
1264 if (!gROOT->IsBatch())
1265 {
1266 char *cprinter = StrDup(p);
1267 char *ccmd = StrDup(c);
1268
1269 Int_t rc=0;
1270 new TGPrintDialog(fClient->GetRoot(), this, 400, 150, &cprinter, &ccmd, &rc);
1271 if (rc)
1272 {
1273 p = cprinter; // default has been changed
1274 c = ccmd;
1275 }
1276
1277 delete [] cprinter;
1278 delete [] ccmd;
1279
1280 if (!rc)
1281 return "";
1282 }
1283
1284 if (c.Contains("%f") && ext)
1285 {
1286 // Get temporary file name
1287 TString name = "mars";
1288
1289 FILE *f = gSystem->TempFileName(name, t);
1290 if (!f)
1291 {
1292 *fLog << warn << "MStatusDisplay::PrintDialog: Couldn't create temporary file in " << t << endl;
1293 SetStatusLine2("failed!");
1294 return "";
1295 }
1296 fclose(f);
1297
1298 // remove temp file
1299 gSystem->Unlink(name);
1300 name += ".";
1301 name += ext;
1302
1303 t = name;
1304 }
1305
1306 // compile command
1307 TString cmd(c);
1308
1309 // if sprinter.IsNull we assume that everything around %p can
1310 // be omitted and the program uses some kind of default
1311 if (p.IsNull())
1312 {
1313 TString sub;
1314 while (1)
1315 {
1316 sub = TString(cmd(TRegexp(" .*%p.* "))).Strip(TString::kBoth);
1317 if (sub.IsNull())
1318 break;
1319
1320 cmd.ReplaceAll(sub, "");
1321 }
1322 }
1323
1324 cmd.ReplaceAll("%p", p);
1325 cmd.ReplaceAll("%f", t);
1326
1327 return cmd;
1328}
1329
1330// --------------------------------------------------------------------------
1331//
1332// Saves the given canvas (pad) or all pads (num<0) as a temporary
1333// postscript file and prints it.
1334//
1335// The default command line c is: lpr -P%p %f
1336// %p: printer name
1337// %f: temporary file name
1338//
1339// The default printer name p is: <empty>
1340//
1341// Both can be changed in .rootrc by:
1342// PrintPS.Printer
1343// PrintPS.Command
1344//
1345// Ant the location of the temporary file t can by changed by
1346// Print.Directory
1347// the default is the system default directory (normally /tmp)
1348//
1349Int_t MStatusDisplay::PrintPS(Int_t num, const char *p, const char *c, const char *t)
1350{
1351 static TString sprinter = gEnv->GetValue("PrintPS.Printer", p&&*p?p:"");
1352 static TString scmd = gEnv->GetValue("PrintPS.Command", c&&*c?c:"lpr -P%p %f");
1353
1354 TString tmp = gEnv->GetValue("Print.Directory", t&&*t?t:gSystem->TempDirectory());
1355
1356 TString cmd = PrintDialog(sprinter, scmd, tmp, "ps");
1357 if (cmd.IsNull())
1358 return 0;
1359
1360 // set status lines
1361 SetStatusLine1("Printing...");
1362 SetStatusLine2("");
1363
1364 // print to temporary file
1365 const Int_t pages = SaveAsPS(num, tmp);
1366
1367 // check
1368 if (!pages)
1369 {
1370 *fLog << warn << "MStatusDisplay::Print: Sorry, couldn't save file as temporary postscript!" << endl;
1371 SetStatusLine2("Failed!");
1372 return 0;
1373 }
1374
1375 // execute command
1376 *fLog << dbg << "Executing: " << cmd << endl;
1377 gSystem->Exec(cmd);
1378
1379 // remove temporary file
1380 gSystem->Unlink(tmp);
1381
1382 SetStatusLine2(MString::Format("Done (%dpage(s))", pages));
1383
1384 return pages;
1385}
1386
1387// --------------------------------------------------------------------------
1388//
1389// Remove tab no i if this tab contains a TRootEmbeddedCanvas
1390//
1391void MStatusDisplay::RemoveTab(int i)
1392{
1393 TGCompositeFrame *f = fTab->GetTabContainer(i);
1394 if (!f)
1395 return;
1396
1397 TRootEmbeddedCanvas *ec = GetEmbeddedCanvas(*f);
1398 if (!ec)
1399 return;
1400
1401 TCanvas *c = ec->GetCanvas();
1402 if (!c)
1403 return;
1404
1405 // Repair the "Workaround" being in the RecursiveRemove list
1406 // but not in a list checked by gROOT->FindObject
1407 //gROOT->GetListOfCleanups()->Remove(c);
1408 //gROOT->GetListOfCanvases()->Add(c);
1409
1410 // FIXME: Due to our workaround this is necessary for a successfull deletion
1411 //c->cd();
1412
1413 const TString name(c->GetName());
1414
1415 f->RemoveFrame(ec);
1416 delete fList->Remove(ec);
1417
1418 fTab->RemoveTab(i);
1419 fTab->SetTab(0);
1420
1421 TGComboBox *box = (TGComboBox*)fList->FindWidget(kTabs);
1422 box->RemoveEntry(i);
1423 for (int j=i; j<box->GetListBox()->GetNumberOfEntries(); j++)
1424 {
1425 TGTextLBEntry *entry = (TGTextLBEntry *)box->GetListBox()->Select(j+1, kFALSE);
1426 box->AddEntry(entry->GetText()->GetString(), j);
1427 box->RemoveEntry(j+1);
1428 }
1429 box->GetListBox()->Select(0);
1430
1431 // Looks strange...
1432 // const Int_t n = fTab->GetNumberOfTabs();
1433 // fTab->SetTab(i<=n-1 ? i : i-1);
1434
1435 // layout and map new tab
1436 Layout(); // seems to layout the TGCompositeFrame
1437 MapSubwindows(); // maps the TGCompositeFrame
1438 Layout(); // layout the embedded canvas in the frame
1439
1440 // display new tab in the main frame
1441 // FIXME: This is a workaround, because TApplication::Run is not
1442 // thread safe against ProcessEvents. We assume, that if
1443 // we are not in the Main-Thread ProcessEvents() is
1444 // called by the TApplication Event Loop...
1445 if (!TThread::Self()/*gApplication->InheritsFrom(TRint::Class())*/)
1446 gClient->ProcessEventsFor(fTab);
1447
1448 *fLog << inf << "Removed Tab #" << i << " '" << name << "'" << endl;
1449}
1450
1451// --------------------------------------------------------------------------
1452//
1453// Use this to check whether the MStatusDisplay still contains the
1454// TCanvas c. It could be removed meanwhile by menu usage.
1455//
1456Bool_t MStatusDisplay::HasCanvas(const TCanvas *c) const
1457{
1458 if (!c)
1459 return kFALSE;
1460
1461 // If you encounter unexpected crashes here, check if
1462 // a canvas is existing twice in the list or has been
1463 // deleted by accident. (Check AddTab)
1464
1465 if (gROOT->IsBatch())
1466 return (Bool_t)fBatch->FindObject(c);
1467
1468 for (int i=1; i<fTab->GetNumberOfTabs(); i++)
1469 if (c==GetCanvas(i))
1470 return kTRUE;
1471 return kFALSE;
1472}
1473
1474/*
1475 if (...)
1476 fMenu->AddPopup("&CaOs", fCaOs, NULL);
1477 else
1478 fMenu->RemovePopup("CaOs");
1479 fMenu->Resize(fMenu->GetDefaultSize());
1480 MapSubwindows();
1481 MapWindow();
1482 */
1483
1484void MStatusDisplay::Reset()
1485{
1486 if (gROOT->IsBatch())
1487 {
1488 fBatch->Delete();
1489 return;
1490 }
1491
1492 for (int i=fTab->GetNumberOfTabs()-1; i>0; i--)
1493 RemoveTab(i);
1494}
1495
1496Bool_t MStatusDisplay::SaveLogAsPS(const char *n) const
1497{
1498 TString name(n);
1499 AddExtension(name, "ps");
1500
1501 // Code taken from TGTextEdit::Print
1502 const TString pipe = MString::Format("a2ps -o%s", name.Data());
1503 FILE *p = gSystem->OpenPipe(pipe, "w");
1504 if (!p)
1505 {
1506 *fLog << err << "ERROR - Couldn't open pipe " << pipe << endl;
1507 return kFALSE;
1508 }
1509
1510 TGText *text = fLogBox->GetText();
1511
1512 char *buf1, *buf2;
1513 Long_t len;
1514 ULong_t i = 0;
1515 TGLongPosition pos;
1516
1517 pos.fX = pos.fY = 0;
1518 while (pos.fY < text->RowCount())
1519 {
1520 len = text->GetLineLength(pos.fY);
1521 buf1 = text->GetLine(pos, len);
1522 buf2 = new char[len + 2];
1523 strncpy(buf2, buf1, (UInt_t)len);
1524 buf2[len] = '\n';
1525 buf2[len+1] = '\0';
1526 while (buf2[i] != '\0') {
1527 if (buf2[i] == '\t') {
1528 ULong_t j = i+1;
1529 while (buf2[j] == 16 && buf2[j] != '\0')
1530 j++;
1531 strcpy(buf2+i+1, buf2+j);
1532 }
1533 i++;
1534 }
1535 fwrite(buf2, sizeof(char), strlen(buf2)+1, p);
1536
1537 delete [] buf1;
1538 delete [] buf2;
1539 pos.fY++;
1540 }
1541 gSystem->ClosePipe(p);
1542 return kTRUE;
1543}
1544
1545// --------------------------------------------------------------------------
1546//
1547// Print the log text.
1548//
1549// The default command line c is: a2ps -P%p
1550// %p: printer name
1551//
1552// The default printer name p is: <empty>
1553//
1554// Both can be changed in .rootrc by:
1555// PrintText.Printer
1556// PrintText.Command
1557//
1558Bool_t MStatusDisplay::PrintLog(const char *p, const char *c)
1559{
1560 static TString sprinter = gEnv->GetValue("PrintText.Printer", p&&*p?p:"");
1561 static TString scmd = gEnv->GetValue("PrintText.Command", c&&*c?c:"a2ps -P%p");
1562
1563 TString tmp;
1564 TString cmd = PrintDialog(sprinter, scmd, tmp);
1565 if (cmd.IsNull())
1566 return kFALSE;
1567
1568 // set status lines
1569 SetStatusLine1("Printing...");
1570 SetStatusLine2("");
1571
1572 // print to temporary file
1573 if (!SaveLogAsPS(cmd))
1574 {
1575 *fLog << warn << "MStatusDisplay::PrintLog: Sorry, couldn't create postscript!" << endl;
1576 SetStatusLine2("Failed!");
1577 return kFALSE;
1578 }
1579
1580 // execute command
1581 *fLog << dbg << "Executing: " << cmd << endl;
1582 gSystem->Exec(cmd);
1583
1584 SetStatusLine2("Done.");
1585
1586 return kTRUE;
1587}
1588
1589// --------------------------------------------------------------------------
1590//
1591// Process the kC_COMMAND, kCM_MENU messages
1592//
1593Bool_t MStatusDisplay::ProcessMessageCommandMenu(Long_t id)
1594{
1595 switch (id)
1596 {
1597 case kLoopPause:
1598 {
1599 TGPopupMenu *m = GetPopup("Loop");
1600 if (!m)
1601 return kTRUE;
1602
1603 if (fStatus==kLoopNone)
1604 {
1605 fStatus = (Status_t)kLoopPause;
1606 m->CheckEntry(kLoopPause);
1607 m->EnableEntry(kLoopStep);
1608 return kTRUE;
1609 }
1610 if (fStatus==kLoopPause)
1611 {
1612 fStatus = (Status_t)kLoopNone;
1613 m->UnCheckEntry(kLoopPause);
1614 m->DisableEntry(kLoopStep);
1615 return kTRUE;
1616 }
1617 }
1618 return kTRUE;
1619
1620 case kLoopStep:
1621 fStatus = (Status_t)kLoopStep;
1622 return kTRUE;
1623
1624 case kLoopStop:
1625 case kFileClose:
1626 case kFileExit:
1627 if (id==kFileExit || id==kFileClose)
1628 if (Close())
1629 delete this;
1630 fStatus = (Status_t)id;
1631 return kTRUE;
1632
1633 case kFileCanvas:
1634 new TCanvas;
1635 return kTRUE;
1636
1637 case kFileBrowser:
1638 new TBrowser;
1639 return kTRUE;
1640
1641 case kFileTab:
1642 AddTab(MString::Format("%d", fTab->GetNumberOfTabs()));
1643 return kTRUE;
1644
1645 case kFileReset:
1646 Reset();
1647 return kTRUE;
1648
1649 case kFileOpen:
1650 Open();
1651 return kTRUE;
1652
1653 case kFileSaveAs:
1654 SaveAs();
1655 return kTRUE;
1656
1657 case kFileSaveAsPS:
1658 SaveAsPS();
1659 return kTRUE;
1660
1661 case kFileSaveAsPDF:
1662 SaveAsPDF();
1663 return kTRUE;
1664
1665 case kFileSaveAsSVG:
1666 SaveAsSVG();
1667 return kTRUE;
1668
1669 case kFileSaveAsPNG:
1670 SaveAsPNG();
1671 return kTRUE;
1672
1673 case kFileSaveAsGIF:
1674 SaveAsGIF();
1675 return kTRUE;
1676
1677 case kFileSaveAsXPM:
1678 SaveAsXPM();
1679 return kTRUE;
1680
1681 case kFileSaveAsJPG:
1682 SaveAsJPG();
1683 return kTRUE;
1684
1685 case kFileSaveAsTIFF:
1686 SaveAsTIFF();
1687 return kTRUE;
1688
1689 case kFileSaveAsBMP:
1690 SaveAsBMP();
1691 return kTRUE;
1692
1693 case kFileSaveAsXML:
1694 SaveAsXML();
1695 return kTRUE;
1696
1697 case kFileSaveAsCSV:
1698 SaveAsCSV();
1699 return kTRUE;
1700
1701 case kFileSaveAsC:
1702 SaveAsC();
1703 return kTRUE;
1704
1705 case kFileSaveAsRoot:
1706 SaveAsRoot();
1707 return kTRUE;
1708
1709 case kFilePrint:
1710 PrintPS();
1711 return kTRUE;
1712
1713 case kTabSaveAs:
1714 SaveAs(fTab->GetCurrent());
1715 return kTRUE;
1716
1717 case kTabSaveAsPS:
1718 SaveAsPS(fTab->GetCurrent());
1719 return kTRUE;
1720
1721 case kTabSaveAsPDF:
1722 SaveAsPDF(fTab->GetCurrent());
1723 return kTRUE;
1724
1725 case kTabSaveAsSVG:
1726 SaveAsSVG(fTab->GetCurrent());
1727 return kTRUE;
1728
1729 case kTabSaveAsPNG:
1730 SaveAsPNG(fTab->GetCurrent());
1731 return kTRUE;
1732
1733 case kTabSaveAsGIF:
1734 SaveAsGIF(fTab->GetCurrent());
1735 return kTRUE;
1736
1737 case kTabSaveAsXPM:
1738 SaveAsXPM(fTab->GetCurrent());
1739 return kTRUE;
1740
1741 case kTabSaveAsJPG:
1742 SaveAsJPG(fTab->GetCurrent());
1743 return kTRUE;
1744
1745 case kTabSaveAsTIFF:
1746 SaveAsTIFF(fTab->GetCurrent());
1747 return kTRUE;
1748
1749 case kTabSaveAsBMP:
1750 SaveAsBMP(fTab->GetCurrent());
1751 return kTRUE;
1752
1753 case kTabSaveAsXML:
1754 SaveAsXML(fTab->GetCurrent());
1755 return kTRUE;
1756
1757 case kTabSaveAsCSV:
1758 SaveAsCSV(fTab->GetCurrent());
1759 return kTRUE;
1760
1761 case kTabSaveAsC:
1762 SaveAsC(fTab->GetCurrent());
1763 return kTRUE;
1764
1765 case kTabSaveAsRoot:
1766 SaveAsRoot(fTab->GetCurrent());
1767 return kTRUE;
1768
1769 case kTabPrint:
1770 PrintPS(fTab->GetCurrent());
1771 return kTRUE;
1772
1773 case kTabNext:
1774 fTab->SetTab(fTab->GetCurrent()+1);
1775 return kTRUE;
1776
1777 case kTabPrevious:
1778 fTab->SetTab(fTab->GetCurrent()-1);
1779 return kTRUE;
1780
1781 case kTabRemove:
1782 RemoveTab(fTab->GetCurrent());
1783 return kTRUE;
1784
1785 case kSize640:
1786 SetDisplaySize(640, 480);
1787 return kTRUE;
1788 case kSize768:
1789 SetDisplaySize(768, 576);
1790 return kTRUE;
1791 case kSize800:
1792 SetDisplaySize(800, 600);
1793 return kTRUE;
1794 case kSize960:
1795 SetDisplaySize(960, 720);
1796 return kTRUE;
1797 case kSize1024:
1798 SetDisplaySize(1024, 768);
1799 return kTRUE;
1800 case kSize1152:
1801 SetDisplaySize(1152, 864);
1802 return kTRUE;
1803 case kSize1280:
1804 SetDisplaySize(1280, 1024);
1805 return kTRUE;
1806 case kSize1400:
1807 SetDisplaySize(1400, 1050);
1808 return kTRUE;
1809 case kSize1600:
1810 SetDisplaySize(1600, 1200);
1811 return kTRUE;
1812 case kSizeOptimum:
1813 SetOptimumSize();
1814 return kTRUE;
1815
1816 case kLogClear:
1817 fLogBox->Clear();
1818 return kTRUE;
1819 case kLogCopy:
1820 fLogBox->Copy();
1821 return kTRUE;
1822 case kLogSelect:
1823 fLogBox->SelectAll();
1824 return kTRUE;
1825 case kLogFind:
1826 new MSearch(this);
1827 return kTRUE;
1828 case kLogSave:
1829 SetStatusLine1("Saving log...");
1830 SetStatusLine2("");
1831 *fLog << inf << "Saving log... " << flush;
1832 if (fLogBox->GetText()->Save(MString::Format("%s.log", gROOT->GetName())))
1833 {
1834 *fLog << "done." << endl;
1835 SetStatusLine2("done.");
1836 }
1837 else
1838 {
1839 *fLog << "failed!" << endl;
1840 SetStatusLine2("Failed!");
1841 }
1842 return kTRUE;
1843
1844 case kLogAppend:
1845 SetStatusLine1("Appending log...");
1846 SetStatusLine2("");
1847 *fLog << inf << "Appending log... " << flush;
1848 if (fLogBox->GetText()->Append(MString::Format("%s.log", gROOT->GetName())))
1849 {
1850 *fLog << "done." << endl;
1851 SetStatusLine2("done.");
1852 }
1853 else
1854 {
1855 *fLog << "failed!" << endl;
1856 SetStatusLine2("Failed!");
1857 }
1858 return kTRUE;
1859
1860 case kLogPrint:
1861 PrintLog();
1862 return kTRUE;
1863#ifdef DEBUG
1864 default:
1865 cout << "Command-Menu #" << id << endl;
1866#endif
1867 }
1868 return kTRUE;
1869
1870}
1871
1872// --------------------------------------------------------------------------
1873//
1874// Process the kC_COMMAND messages
1875//
1876Bool_t MStatusDisplay::ProcessMessageCommand(Long_t submsg, Long_t mp1, Long_t mp2)
1877{
1878 switch (submsg)
1879 {
1880 case kCM_MENU: // 1
1881 return ProcessMessageCommandMenu(mp1); // mp2=userdata
1882 case kCM_TAB: // 8
1883 /*
1884 for (int i=0; i<fTab->GetNumberOfTabs(); i++)
1885 fTab->GetTabContainer(i)->UnmapWindow();
1886 */
1887 UpdateTab(fTab->GetTabContainer(mp1));
1888 //fTab->GetTabContainer(mp1)->MapWindow();
1889
1890 /*
1891 if (mp1>0)
1892 fMenu->AddPopup("&CaOs", fCaOs, NULL);
1893 else
1894 fMenu->RemovePopup("CaOs");
1895 fMenu->Resize(fMenu->GetDefaultSize());
1896 MapSubwindows();
1897 MapWindow();
1898 */
1899 return kTRUE;
1900 case kCM_COMBOBOX: // 7
1901 if (mp1==kTabs)
1902 fTab->SetTab(mp2);
1903 return kTRUE;
1904#ifdef DEBUG
1905 case kCM_MENUSELECT: // 2
1906 cout << "Command-Menuselect #" << mp1 << " (UserData=" << (void*)mp2 << ")" << endl;
1907 return kTRUE;
1908
1909 case kCM_BUTTON: // 3
1910 cout << "Command-Button." << endl;
1911 return kTRUE;
1912
1913 case kCM_CHECKBUTTON: // 4
1914 cout << "Command-CheckButton." << endl;
1915 return kTRUE;
1916
1917 case kCM_RADIOBUTTON: // 5
1918 cout << "Command-RadioButton." << endl;
1919 return kTRUE;
1920
1921 case kCM_LISTBOX: // 6
1922 cout << "Command-Listbox #" << mp1 << " (LineId #" << mp2 << ")" << endl;
1923 return kTRUE;
1924 default:
1925 cout << "Command: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
1926#endif
1927 }
1928 return kTRUE;
1929}
1930
1931// --------------------------------------------------------------------------
1932//
1933// Process the kC_TEXTVIEW messages
1934//
1935Bool_t MStatusDisplay::ProcessMessageTextview(Long_t /*submsg*/, Long_t /*mp1*/, Long_t /*mp2*/)
1936{
1937 // kC_TEXTVIEW, kTXT_ISMARKED, widget id, [true|false] //
1938 // kC_TEXTVIEW, kTXT_DATACHANGE, widget id, 0 //
1939 // kC_TEXTVIEW, kTXT_CLICK2, widget id, position (y << 16) | x) //
1940 // kC_TEXTVIEW, kTXT_CLICK3, widget id, position (y << 16) | x) //
1941 // kC_TEXTVIEW, kTXT_F3, widget id, true //
1942 // kC_TEXTVIEW, kTXT_OPEN, widget id, 0 //
1943 // kC_TEXTVIEW, kTXT_CLOSE, widget id, 0 //
1944 // kC_TEXTVIEW, kTXT_SAVE, widget id, 0 //
1945#ifdef DEBUG
1946 switch (submsg)
1947 {
1948 case kTXT_ISMARKED:
1949 cout << "Textview-IsMarked #" << mp1 << " " << (mp2?"yes":"no") << endl;
1950 return kTRUE;
1951
1952 case kTXT_DATACHANGE:
1953 cout << "Textview-DataChange #" << mp1 << endl;
1954 return kTRUE;
1955
1956 case kTXT_CLICK2:
1957 cout << "Textview-Click2 #" << mp1 << " x=" << (mp2&0xffff) << " y= " << (mp2>>16) << endl;
1958 return kTRUE;
1959
1960 case kTXT_CLICK3:
1961 cout << "Textview-Click3 #" << mp1 << " x=" << (mp2&0xffff) << " y= " << (mp2>>16) << endl;
1962 return kTRUE;
1963
1964 case kTXT_F3:
1965 cout << "Textview-F3 #" << mp1 << endl;
1966 return kTRUE;
1967
1968 case kTXT_OPEN:
1969 cout << "Textview-Open #" << mp1 << endl;
1970 return kTRUE;
1971
1972 case kTXT_CLOSE:
1973 cout << "Textview-Close #" << mp1 << endl;
1974 return kTRUE;
1975
1976 case kTXT_SAVE:
1977 cout << "Textview-Save #" << mp1 << endl;
1978 return kTRUE;
1979
1980 default:
1981 cout << "Textview: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
1982 }
1983#endif
1984 return kTRUE;
1985}
1986
1987// --------------------------------------------------------------------------
1988//
1989// Process the kC_USER messages
1990//
1991Bool_t MStatusDisplay::ProcessMessageUser(Long_t submsg, Long_t mp1, Long_t mp2)
1992{
1993 // kS_START, case sensitive | backward<<1, char *txt
1994 switch (submsg)
1995 {
1996 case kS_START:
1997 fLogBox->Search((char*)mp2, !(mp1&2>>1), mp1&1);
1998 return kTRUE;
1999#ifdef DEBUG
2000 default:
2001 cout << "User: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2002#endif
2003 }
2004 return kTRUE;
2005}
2006
2007// --------------------------------------------------------------------------
2008//
2009// Process the messages from the GUI
2010//
2011Bool_t MStatusDisplay::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
2012{
2013 // Can be found in WidgetMessageTypes.h
2014#ifdef DEBUG
2015 cout << "Msg: " << GET_MSG(msg) << " Submsg:" << GET_SUBMSG(msg);
2016 cout << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2017#endif
2018 switch (GET_MSG(msg))
2019 {
2020 case kC_COMMAND: // 1
2021 return ProcessMessageCommand(GET_SUBMSG(msg), mp1, mp2);
2022
2023 case kC_TEXTVIEW: // 9
2024 return ProcessMessageTextview(GET_SUBMSG(msg), mp1, mp2);
2025
2026 case kC_USER: // 1001
2027 return ProcessMessageUser(GET_SUBMSG(msg), mp1, mp2);
2028 }
2029#ifdef DEBUG
2030 cout << "Msg: " << GET_MSG(msg) << " Submsg:" << GET_SUBMSG(msg);
2031 cout << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2032#endif
2033 return kTRUE;
2034}
2035
2036Bool_t MStatusDisplay::Close()
2037{
2038 // Got close message for this MainFrame. Calls parent CloseWindow()
2039 // (which destroys the window) and terminate the application.
2040 // The close message is generated by the window manager when its close
2041 // window menu item is selected.
2042
2043 // CloseWindow must be overwritten because otherwise CloseWindow
2044 // and the destructor are calling DestroyWindow which seems to be
2045 // in conflict with the TRootEmbeddedCanvas.
2046
2047 // FIXME: Make sure that the Status Display is deleted from every
2048 // where (eg Eventloop) first!
2049
2050 //gLog << dbg << fName << " is on heap: " << (int)IsOnHeap() << endl;
2051
2052 if (TestBit(kExitLoopOnExit) || TestBit(kExitLoopOnClose))
2053 {
2054 //gLog << dbg << "CloseWindow() calling ExitLoop." << endl;
2055 gSystem->ExitLoop();
2056 }
2057
2058 if (fIsLocked<=0 && IsOnHeap())
2059 return kTRUE;
2060
2061 fStatus = kFileExit;
2062 return kFALSE;
2063}
2064
2065void MStatusDisplay::CloseWindow()
2066{
2067 if (Close())
2068 delete this;
2069}
2070
2071// --------------------------------------------------------------------------
2072//
2073// Calls SetBit(kNoContextMenu) for all TCanvas objects found in the
2074// Tabs.
2075//
2076void MStatusDisplay::SetNoContextMenu(Bool_t flag)
2077{
2078 if (fIsLocked>1 || gROOT->IsBatch())
2079 return;
2080
2081 flag ? SetBit(kNoContextMenu) : ResetBit(kNoContextMenu);
2082
2083 for (int i=1; i<fTab->GetNumberOfTabs(); i++)
2084 {
2085 TCanvas *c = GetCanvas(i);
2086 if (c)
2087 flag ? c->SetBit(kNoContextMenu) : c->ResetBit(kNoContextMenu);
2088 }
2089}
2090
2091// --------------------------------------------------------------------------
2092//
2093// Update the memory display in the status bar
2094//
2095void MStatusDisplay::UpdateMemory() const
2096{
2097 const TString path = MString::Format("/proc/%d/status", gSystem->GetPid());
2098 if (gSystem->AccessPathName(path, kFileExists))
2099 return;
2100
2101 TEnv env(path);
2102 const UInt_t kb = env.GetValue("VmSize", 0);
2103 if (kb==0)
2104 return;
2105
2106 char type = 'k';
2107 Float_t val = kb;
2108
2109 if (val>999)
2110 {
2111 type = 'M';
2112 val /= 1000;
2113 }
2114 if (val>999)
2115 {
2116 type = 'G';
2117 val /= 1000;
2118 }
2119 const TString txt = MString::Format("%.1f%c", val, type);
2120 fStatusBar->SetText(txt, 2);
2121}
2122
2123// --------------------------------------------------------------------------
2124//
2125// Updates the canvas (if existing) in the currenly displayed Tab.
2126// The update intervall is controlled by StartUpdate and StopUpdate
2127//
2128Bool_t MStatusDisplay::HandleTimer(TTimer *timer)
2129{
2130 if (gROOT->IsBatch())
2131 return kTRUE;
2132
2133 UpdateMemory();
2134
2135 const Int_t c = fTab->GetCurrent();
2136
2137 // Skip Legend Tab
2138 if (c==0)
2139 return kTRUE;
2140
2141 // Update a canvas tab (if visible)
2142 if (timer==&fTimer && c!=fLogIdx)
2143 {
2144 UpdateTab(fTab->GetCurrentContainer());
2145 return kTRUE;
2146 }
2147
2148 // update the logbook tab (if visible)
2149 if (timer==&fLogTimer && c==fLogIdx)
2150 {
2151 fLog->UpdateGui();
2152
2153 /*
2154 if (!fLogBox->TestBit(kHasChanged))
2155 return kTRUE;
2156
2157 fLogBox->ResetBit(kHasChanged);
2158 */
2159 return kTRUE;
2160 }
2161
2162 return kTRUE;
2163}
2164
2165// --------------------------------------------------------------------------
2166//
2167// Find an object in a canvas (uses MStatusArray as helper)
2168//
2169void MStatusDisplay::PrintContent(Option_t *o) const
2170{
2171 MStatusArray(*this).Print(o);
2172}
2173
2174// --------------------------------------------------------------------------
2175//
2176// Find an object in a canvas (uses MStatusArray as helper)
2177//
2178TObject *MStatusDisplay::FindObjectInCanvas(const char *obj, const char *base, const char *canv) const
2179{
2180 return MStatusArray(*this).FindObjectInCanvas(obj, base, canv);
2181}
2182
2183// --------------------------------------------------------------------------
2184//
2185// Draws a clone of a canvas into a new canvas. Taken from TCanvas.
2186//
2187void MStatusDisplay::DrawClonePad(TCanvas &newc, TCanvas &oldc) const
2188{
2189 //copy pad attributes
2190 newc.Range(oldc.GetX1(),oldc.GetY1(),oldc.GetX2(),oldc.GetY2());
2191 newc.SetTickx(oldc.GetTickx());
2192 newc.SetTicky(oldc.GetTicky());
2193 newc.SetGridx(oldc.GetGridx());
2194 newc.SetGridy(oldc.GetGridy());
2195 newc.SetLogx(oldc.GetLogx());
2196 newc.SetLogy(oldc.GetLogy());
2197 newc.SetLogz(oldc.GetLogz());
2198 newc.SetBorderSize(oldc.GetBorderSize());
2199 newc.SetBorderMode(oldc.GetBorderMode());
2200 ((TAttLine&)oldc).Copy((TAttLine&)newc);
2201 ((TAttFill&)oldc).Copy((TAttFill&)newc);
2202 ((TAttPad&)oldc).Copy((TAttPad&)newc);
2203
2204 // This must be there: Otherwise GetDrawOption() won't work
2205 TVirtualPad *padsav = gPad;
2206 oldc.cd();
2207
2208 const Bool_t store = TH1::AddDirectoryStatus();
2209 TH1::AddDirectory(kFALSE);
2210
2211 //copy primitives
2212 TObject *obj;
2213 TIter next(oldc.GetListOfPrimitives());
2214 while ((obj=next()))
2215 {
2216 // Old line - I think it is not necessary anymore because of the cd()
2217 //gROOT->SetSelectedPad(&newc);
2218
2219 // Now make a clone of the object
2220 TObject *clone = obj->Clone();
2221
2222 // Clone also important bits (FIXME: Is this correct)
2223 clone->SetBit(obj->TestBits(kCannotPick|kNoContextMenu));
2224
2225 // Now make sure that the clones are deleted at a later time
2226 clone->SetBit(kCanDelete|kMustCleanup);
2227
2228 // FIXME: This is a workaround for the problem with the MAstroCatalog in
2229 // MHFalseSource. It doesn't harm. We'll still try to find the reason
2230 if (clone->IsA()==TPad::Class())
2231 gROOT->GetListOfCleanups()->Add(clone);
2232
2233 // Add the clone and its draw-option to the current pad
2234 TVirtualPad *save2 = gPad;
2235 gPad = &oldc; // Don't do this before Clone()!
2236 newc.GetListOfPrimitives()->Add(clone, obj->GetDrawOption());
2237 gPad = save2;
2238 }
2239 newc.Modified();
2240 newc.Update();
2241
2242 TH1::AddDirectory(store);
2243
2244 padsav->cd();
2245}
2246
2247// --------------------------------------------------------------------------
2248//
2249// Display the contexts of a TObjArray in the display (all canvases)
2250//
2251Bool_t MStatusDisplay::Display(const TObjArray &list, const char *tab)
2252{
2253 TIter Next(&list);
2254
2255 TObject *o=Next();
2256 if (!o)
2257 {
2258 *fLog << err << "MStatusDisplay::Display: No entry in TObjArray." << endl;
2259 return kFALSE;
2260 }
2261
2262 fTitle = o->GetTitle();
2263
2264 TCanvas *c;
2265 while ((c=(TCanvas*)Next()))
2266 //if (!GetCanvas(c->GetName()))
2267 if (c->InheritsFrom(TCanvas::Class()))
2268 if (!tab || c->GetName()==(TString)tab)
2269 DrawClonePad(AddTab(c->GetName(), c->GetTitle()), *c);
2270
2271 return kTRUE;
2272}
2273
2274// --------------------------------------------------------------------------
2275//
2276// Reads the contents of a saved MStatusDisplay from a file.
2277//
2278Int_t MStatusDisplay::Read(const char *name, const char *tab)
2279{
2280 if (!gFile)
2281 {
2282 *fLog << warn << "MStatusDisplay::Read: No file found. Please create a TFile first." << endl;
2283 return 0;
2284 }
2285
2286 if (!gFile->IsOpen())
2287 {
2288 *fLog << warn << "MStatusDisplay::Read: File not open. Please open the TFile first." << endl;
2289 return 0;
2290 }
2291
2292 MStatusArray list;
2293
2294 const Int_t n = list.Read(name);
2295
2296 //
2297 // If no status display was found with this name try to find canvases
2298 // in the file and s´display them instead.
2299 //
2300 if (n==0)
2301 {
2302 const Bool_t store = TH1::AddDirectoryStatus();
2303 TH1::AddDirectory(kFALSE);
2304
2305 TIter Next(gFile->GetListOfKeys());
2306 TObject *key = 0;
2307 while ((key=Next()))
2308 {
2309 TCanvas *c=0;
2310 gFile->GetObject(key->GetName(), c);
2311 if (!c)
2312 break;
2313
2314 if (list.GetEntries()==0)
2315 list.Add(new TNamed(GetName(), GetTitle()));
2316
2317 c->SetTitle(gFile->GetName());
2318 list.Add(c);
2319 }
2320
2321 TH1::AddDirectory(store);
2322
2323 if (list.GetEntries()==0)
2324 {
2325 *fLog << warn << "MStatusDisplay::Read: No objects read from " << gFile->GetName() << endl;
2326 return 0;
2327 }
2328
2329 *fLog << inf << "MStatusDisplay: " << list.GetEntries() << " canvases directly read from file." << endl;
2330 }
2331
2332
2333 if (!Display(list, tab))
2334 {
2335 *fLog << err << "MStatusDisplay::Display: No entries found." << endl;
2336 return 0;
2337 }
2338
2339
2340 if (n==0)
2341 return list.GetEntries();
2342
2343 *fLog << inf << "MStatusDisplay: Key " << name << " with " << n << " keys read from file." << endl;
2344 return n;
2345}
2346
2347// --------------------------------------------------------------------------
2348//
2349// Add all canvases to the MStatusArray
2350//
2351void MStatusDisplay::FillArray(MStatusArray &list, Int_t num) const
2352{
2353 Int_t from, to;
2354 GetCanvasRange(from, to, num);
2355
2356 TCanvas *c;
2357 for (int i=from; i<to; i++)
2358 if ((c = GetCanvas(i)))
2359 list.Add(c);
2360}
2361
2362// --------------------------------------------------------------------------
2363//
2364// Writes the contents of a MStatusDisplay to a file.
2365//
2366Int_t MStatusDisplay::Write(Int_t num, const char *name, Int_t /*option*/, Int_t /*bufsize*/) const
2367{
2368 if (!gFile)
2369 {
2370 *fLog << warn << "MStatusDisplay::Write: No file found. Please create a TFile first." << endl;
2371 return 0;
2372 }
2373
2374 if (!gFile->IsOpen())
2375 {
2376 *fLog << warn << "MStatusDisplay::Write: File not open. Please open the TFile first." << endl;
2377 return 0;
2378 }
2379
2380 if (!gFile->IsWritable())
2381 {
2382 *fLog << warn << "MStatusDisplay::Write: File not writable." << endl;
2383 return 0;
2384 }
2385
2386 if (num==0)
2387 {
2388 *fLog << warn << "MStatusDisplay::Write: Tab doesn't contain an embedded Canvas... skipped." << endl;
2389 return 0;
2390 }
2391
2392 if (!gROOT->IsBatch() && num>=fTab->GetNumberOfTabs())
2393 {
2394 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
2395 return 0;
2396 }
2397 if (gROOT->IsBatch() && num>fBatch->GetSize())
2398 {
2399 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
2400 return 0;
2401 }
2402
2403 MStatusArray list;
2404
2405 // Be careful: So far Display() assumes that it is the first entry in the list
2406 TNamed named;
2407 named.SetTitle(fTitle);
2408 list.Add(&named);
2409
2410 FillArray(list, num);
2411
2412 const Int_t n = list.Write(name, kSingleKey);
2413
2414 //*fLog << inf << "MStatusDisplay: " << n << " keys written to file as key " << name << "." << endl;
2415
2416 return n;
2417}
2418
2419// --------------------------------------------------------------------------
2420//
2421// Use this to start the synchronous (GUI eventloop driven) tab update.
2422// Can also be used to change the update intervall. If millisec<0
2423// the intervall given in SetUpdateTime is used. If the intervall in
2424// SetUpdateTime is <0 nothing is done. (Call SetUpdateTime(-1) to
2425// disable the automatic update in a MEventloop.
2426//
2427void MStatusDisplay::StartUpdate(Int_t millisec)
2428{
2429 if (fIsLocked>1)
2430 return;
2431
2432 if (fTimer.GetTime()<TTime(0))
2433 return;
2434 fTimer.Start(millisec);
2435}
2436
2437// --------------------------------------------------------------------------
2438//
2439// Stops the automatic GUI update
2440//
2441void MStatusDisplay::StopUpdate()
2442{
2443 if (fIsLocked>1)
2444 return;
2445
2446 fTimer.Stop();
2447}
2448
2449// --------------------------------------------------------------------------
2450//
2451// Set the update interval for the GUI update, see StartUpdate.
2452//
2453void MStatusDisplay::SetUpdateTime(Long_t t)
2454{
2455 fTimer.SetTime(t);
2456}
2457
2458// --------------------------------------------------------------------------
2459//
2460// If the filename name doesn't end with ext, ext is added to the end.
2461// If name.IsNull() "status" is assumed and the a number (>0) is added
2462// as "status-6".
2463// The extension is returned.
2464//
2465const TString &MStatusDisplay::AddExtension(TString &name, const TString &ext, Int_t num) const
2466{
2467 if (name.IsNull())
2468 {
2469 name = gROOT->GetName();
2470 if (num>0)
2471 {
2472 name += "-";
2473 name += num;
2474 }
2475 }
2476
2477 if (name.EndsWith("."+ext))
2478 return ext;
2479
2480 name += ".";
2481 name += ext;
2482
2483 return ext;
2484}
2485
2486Bool_t MStatusDisplay::CheckTabForCanvas(int num) const
2487{
2488 if (gROOT->IsBatch())
2489 return num>0 && num<=fBatch->GetSize() || num<0;
2490
2491 if (num>=fTab->GetNumberOfTabs())
2492 {
2493 *fLog << warn << "Tab #" << num << " doesn't exist..." << endl;
2494 return kFALSE;
2495 }
2496 if (num==0)
2497 {
2498 *fLog << warn << "Tab #" << num << " doesn't contain an embedded canvas..." << endl;
2499 return kFALSE;
2500 }
2501 if (fTab->GetNumberOfTabs()<2 || !gPad)
2502 {
2503 *fLog << warn << "Sorry, you must have at least one existing canvas (gPad!=NULL)" << endl;
2504 return kFALSE;
2505 }
2506 return kTRUE;
2507}
2508
2509// --------------------------------------------------------------------------
2510//
2511// Insert the following two lines into the postscript header:
2512//
2513// %%DocumentPaperSizes: a4
2514// %%Orientation: Landscape
2515//
2516void MStatusDisplay::UpdatePSHeader(const TString &name) const
2517{
2518 const TString newstr("%%DocumentPaperSizes: a4\n%%Orientation: Landscape\n");
2519
2520 TString tmp(name+"XXXXXX");
2521
2522 // FIXME: Use mkstemp instead
2523 if (!mktemp(const_cast<char*>(tmp.Data())))
2524 {
2525 *fLog << err << "ERROR - MStatusDisplay::UpdatePSHeader: mktemp failed." << endl;
2526 return;
2527 }
2528
2529 ifstream fin(name);
2530 ofstream fout(tmp);
2531 if (!fout)
2532 {
2533 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
2534 return;
2535 }
2536
2537 char c;
2538
2539 TString str;
2540 fin >> str >> c; // Read "%!PS-Adobe-2.0\n"
2541 fout << str << endl << newstr;
2542
2543 // Doing it in blocks seems not to gain much for small (MB) files
2544 while (fin)
2545 {
2546 fin.read(&c, 1);
2547 fout.write(&c, 1);
2548 }
2549
2550 gSystem->Unlink(name);
2551 gSystem->Rename(tmp, name);
2552}
2553
2554// --------------------------------------------------------------------------
2555//
2556void MStatusDisplay::PSToolsRange(TVirtualPS &vps, Float_t psw, Float_t psh) const
2557{
2558 if (vps.InheritsFrom(TPostScript::Class()))
2559 static_cast<TPostScript&>(vps).Range(psw, psh);
2560 // if (vps.InheritsFrom(TPDF::Class()))
2561 // static_cast<TPDF&>(vps).Range(psw*0.69, psh*0.69);
2562 // if (vps.InheritsFrom(TSVG::Class()))
2563 // static_cast<TSVG&>(vps).Range(psw, psh);
2564}
2565
2566// --------------------------------------------------------------------------
2567//
2568void MStatusDisplay::PSToolsTextNDC(TVirtualPS &vps, Double_t u, Double_t v, const char *string) const
2569{
2570 if (vps.InheritsFrom(TPostScript::Class()))
2571 static_cast<TPostScript&>(vps).TextNDC(u, v, string);
2572 if (vps.InheritsFrom(TPDF::Class()))
2573 static_cast<TPDF&>(vps).TextNDC(u, v, string);
2574 // if (vps.InheritsFrom(TSVG::Class()))
2575 // static_cast<TSVG&>(vps).TextNDC(u, v, string);
2576}
2577
2578// --------------------------------------------------------------------------
2579//
2580Int_t MStatusDisplay::InitWriteDisplay(Int_t num, TString &name, const TString &ext)
2581{
2582 SetStatusLine1(MString::Format("Writing %s file...",ext.Data()));
2583 SetStatusLine2("Please be patient!");
2584
2585 if (!CheckTabForCanvas(num))
2586 {
2587 SetStatusLine2("Failed!");
2588 return 0;
2589 }
2590
2591 AddExtension(name, ext, num);
2592
2593 if (num<0)
2594 *fLog << inf << "Open " << ext << "-File: " << name << endl;
2595
2596 return num;
2597}
2598
2599// --------------------------------------------------------------------------
2600//
2601TCanvas *MStatusDisplay::InitWriteTab(Int_t num, TString &name)
2602{
2603 const Int_t i = TMath::Abs(num);
2604
2605 TCanvas *c = GetCanvas(i);
2606 if (!c)
2607 {
2608 if (num<0)
2609 *fLog << inf << " - ";
2610 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
2611 return 0;
2612 }
2613
2614 SetStatusLine2(MString::Format("Tab #%d", i));
2615
2616 //
2617 // Paint canvas into root file
2618 //
2619 if (num<0 && !name.IsNull())
2620 {
2621 Bool_t found = kFALSE;
2622 if (name.Index("%%%%name%%%%"))
2623 {
2624 name.ReplaceAll("%%name%%", c->GetName());
2625 found = kTRUE;
2626 }
2627
2628 if (name.Index("%%%%title%%%%"))
2629 {
2630 name.ReplaceAll("%%title%%", c->GetTitle());
2631 found = kTRUE;
2632 }
2633
2634 if (name.Index("%%%%tab%%%%"))
2635 {
2636 name.ReplaceAll("%%tab%%", MString::Format("%d", i));
2637 found = kTRUE;
2638 }
2639
2640 if (!found)
2641 name.Insert(name.Last('.'), MString::Format("-%d", i));
2642 }
2643
2644 if (num<0)
2645 *fLog << inf << " - ";
2646 *fLog << inf << "Writing Tab #" << i;
2647
2648 if (!name.IsNull())
2649 *fLog << " to " << name;
2650
2651 *fLog << ": " << c->GetName() << "... " << flush;
2652
2653 return c;
2654}
2655
2656// This is a stupid workaround to get rid of the damned clipping
2657// of the text. Who the hell needs clipping?
2658class MyCanvas : public TCanvas
2659{
2660public:
2661 void Scale(Double_t val)
2662 {
2663 fAbsXlowNDC = -val;
2664 fAbsYlowNDC = -val;
2665 fAbsWNDC = 1+2*val;
2666 fAbsHNDC = 1+2*val;
2667 }
2668};
2669
2670// --------------------------------------------------------------------------
2671//
2672// Write some VGF (vector graphics format). Currently PS, PDF and SVG
2673// is available. Specified by ext.
2674//
2675// In case of num<0 all tabs are written into the VGF file. If num>0
2676// the canvas in the corresponding tab is written to the file.
2677// Name is the name of the file (with or without extension).
2678//
2679// Returns the number of pages written.
2680//
2681// To write all tabs you can also use SaveAsVGF(name, ext)
2682//
2683// If the third argument is given a bottom line is drawn with the text
2684// under it. If no argument is given a bottom line is drawn if
2685// fTitle (SetTitle) is not empty.
2686//
2687Int_t MStatusDisplay::SaveAsVGF(Int_t num, TString name, const TString addon, const TString ext)
2688{
2689 num = InitWriteDisplay(num, name, ext);
2690 if (num==0)
2691 return 0;
2692
2693 TPad *padsav = (TPad*)gPad;
2694 TVirtualPS *psave = gVirtualPS;
2695
2696 TDatime d;
2697
2698 Int_t type = -1;
2699
2700 TVirtualPS *ps =0;
2701 if (!ext.CompareTo("ps", TString::kIgnoreCase))
2702 {
2703 gStyle->SetColorModelPS(1);
2704 ps = new TPostScript(name, 112);
2705 type = 1;
2706 }
2707 if (!ext.CompareTo("pdf", TString::kIgnoreCase))
2708 {
2709 ps = new TPDF(name, 112);
2710 type = 2;
2711 }
2712 if (!ext.CompareTo("svg", TString::kIgnoreCase))
2713 {
2714 ps = new TSVG(name, 112);
2715 type = 3;
2716 }
2717
2718 if (!ps)
2719 {
2720 *fLog << err << "Extension " << ext << " unknown..." << endl;
2721 SetStatusLine2("Failed!");
2722 return 0;
2723 }
2724
2725 ps->SetBit(TPad::kPrintingPS);
2726 if (type==1)
2727 ps->PrintFast(13, "/nan {1} def ");
2728
2729 gVirtualPS = ps;
2730
2731 //
2732 // Create some GUI elements for a page legend
2733 //
2734 TLine line;
2735
2736 int page = 1;
2737
2738 //
2739 // Maintain tab numbers
2740 //
2741 Int_t from, to;
2742 GetCanvasRange(from, to, num);
2743
2744 for (int i=from; i<to; i++)
2745 {
2746 TCanvas *c = InitWriteTab(num<0?-i:i);
2747 if (c==0)
2748 continue;
2749
2750 //
2751 // Init page and page size, make sure, that the canvas in the file
2752 // has the same Aspect Ratio than on the screen.
2753 //
2754 if (type==1 || i>from)
2755 ps->NewPage();
2756
2757 //
2758 // 28 is used here to scale the canvas into a height of 28,
2759 // such that the page title can be set above the canvas...
2760 //
2761 Float_t psw = 28.0; // A4 - width (29.7)
2762 Float_t psh = 21.0; // A4 - height (21.0)
2763
2764 const Float_t cw = c->GetWw();
2765 const Float_t ch = c->GetWh();
2766
2767 if (psw/psh>cw/ch)
2768 psw = cw/ch*psh;
2769 else
2770 psh = ch/cw*psw;
2771
2772 PSToolsRange(*ps, psw, psh);
2773
2774 //
2775 // Clone canvas and change background color and schedule for
2776 // deletion
2777 //
2778
2779 //const Bool_t store = c->IsBatch();
2780 //c->SetBatch(kTRUE);
2781 c->Paint();
2782 //c->SetBatch(store);
2783
2784 //
2785 // Change/fix the canvas coordinate system for the overlaying text.
2786 // This is necessary because root clip everything away which is
2787 // outside a predefined area, which is (0,0)/(1,1)
2788 //
2789 const Double_t height = 0.015; // Text height
2790 const Double_t off = 0.005; // Line offset from text
2791
2792 const Double_t bot = height+off;
2793 const Double_t top = 1-bot;
2794
2795 static_cast<MyCanvas*>(c)->Scale(bot);
2796
2797 // If gPad is not set to c all follwing commands will
2798 // get the wrong numbers for alignment
2799 gPad = c;
2800
2801 // Separator Lines
2802 line.PaintLineNDC(0.01, top, 0.99, top);
2803 line.PaintLineNDC(0.01, bot, 0.99, bot);
2804
2805 //
2806 // Print overlaying text (NDC = %)
2807 //
2808 // align phi col font size (11=left top)
2809 const TString txt(addon.IsNull() ? fTitle : addon);
2810
2811 // Text Attributes
2812 TAttText(11, 0, kBlack, 22, height).Copy(*ps);
2813
2814 // Text on top
2815 ps->SetTextAlign(11); // left bottom
2816 PSToolsTextNDC(*ps, 0.01, top+off, c->GetName());
2817
2818 ps->SetTextAlign(21); // cent bottom
2819 PSToolsTextNDC(*ps, 0.50, top+off, TString("MARS V"MARSVER" - Modular Analysis and Reconstruction Software - ")+d.AsString());
2820
2821 ps->SetTextAlign(31); // right bottom
2822 PSToolsTextNDC(*ps, 0.99, top+off, MString::Format("Page No.%i (%i)", page++, i));
2823
2824 // Text on bottom
2825 ps->SetTextAlign(13); // left top
2826 PSToolsTextNDC(*ps, 0.01, bot-off, c->GetTitle());
2827
2828 ps->SetTextAlign(23); // cent top
2829 PSToolsTextNDC(*ps, 0.50, bot-off, txt);
2830
2831 ps->SetTextAlign(33); // right top
2832 PSToolsTextNDC(*ps, 0.99, bot-off, MString::Format("(c) 2000-%d, Thomas Bretz", TDatime().GetYear()));
2833
2834 static_cast<MyCanvas*>(c)->Scale(0);
2835
2836 //
2837 // Finish drawing page
2838 //
2839 *fLog << "done." << endl;
2840 }
2841
2842 gPad = NULL; // Important!
2843
2844 ps->Close();
2845 delete ps;
2846
2847#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
2848 if (type==1)
2849 {
2850 SetStatusLine2("Updating header of PS file...");
2851
2852 if (num<0)
2853 *fLog << inf3 << " - Updating header of PS file... " << flush;
2854 UpdatePSHeader(name);
2855 if (num<0)
2856 *fLog << inf3 << "done." << endl;
2857 }
2858#endif
2859
2860 gVirtualPS = psave;
2861 if (padsav)
2862 padsav->cd();
2863
2864 if (num<0)
2865 *fLog << inf << "done." << endl;
2866
2867 SetStatusLine2(MString::Format("Done (%dpages)", page-1));
2868
2869 return page-1;
2870}
2871
2872// --------------------------------------------------------------------------
2873//
2874Bool_t MStatusDisplay::SaveAsImage(Int_t num, TString name, TImage::EImageFileTypes type)
2875{
2876#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
2877 if (gROOT->IsBatch())
2878 {
2879 *fLog << warn << "Sorry, writing image-files is not available in batch mode." << endl;
2880 return 0;
2881 }
2882#endif
2883
2884 TString ext;
2885 switch (type)
2886 {
2887 case TImage::kXpm:
2888 case TImage::kZCompressedXpm: ext = "xpm"; break;
2889 case TImage::kPng: ext = "png"; break;
2890 case TImage::kJpeg: ext = "jpg"; break;
2891 case TImage::kGif: ext = "gif"; break;
2892 case TImage::kTiff: ext = "tiff"; break;
2893 case TImage::kBmp: ext = "bmp"; break;
2894 case TImage::kXml: ext = "xml"; break;
2895 //case TImage::kGZCompressedXpm: ext = "xpm.gz"; break;
2896 //case TImage::kPpm: ext = "ppm"; break;
2897 //case TImage::kPnm: ext = "pnm"; break;
2898 //case TImage::kIco: ext = "ico"; break;
2899 //case TImage::kCur: ext = "cur"; break;
2900 //case TImage::kXcf: ext = "xcf"; break;
2901 //case TImage::kXbm: ext = "xbm"; break;
2902 //case TImage::kFits: ext = "fits"; break;
2903 //case TImage::kTga: ext = "tga"; break;
2904 default:
2905 *fLog << warn << "Sorry, unknown or unsupported file type..." << endl;
2906 return 0;
2907 }
2908
2909 num = InitWriteDisplay(num, name, ext);
2910 if (num==0)
2911 return 0;
2912
2913 TPad *padsav = (TPad*)gPad;
2914
2915 Int_t counter = 0;
2916
2917 //
2918 // Maintain tab numbers
2919 //
2920 Int_t from, to;
2921 GetCanvasRange(from, to, num);
2922
2923 for (int i=from; i<to; i++)
2924 {
2925 TString writename(name);
2926
2927 TCanvas *c = InitWriteTab(num<0 ? -i : i, writename);
2928 if (!c)
2929 continue;
2930
2931 //
2932 // Paint canvas into root file
2933 //
2934
2935 // TImage *img = TImage::Create();
2936 // img->FromPad(c);
2937 // img->WriteImage(writename, type);
2938 // delete img;
2939
2940 // FIXME: Not all file types are supported by Print()
2941 c->Print(writename);
2942
2943 if (num<0)
2944 *fLog << "done." << endl;
2945
2946 counter++;
2947 }
2948
2949 if (padsav)
2950 padsav->cd();
2951
2952 *fLog << inf << "done." << endl;
2953
2954 SetStatusLine2("Done.");
2955
2956 return counter>0;
2957}
2958
2959// --------------------------------------------------------------------------
2960//
2961Bool_t MStatusDisplay::SaveAsC(Int_t num, TString name)
2962{
2963 num = InitWriteDisplay(num, name, "C");
2964 if (num==0)
2965 return kFALSE;
2966
2967 TPad *padsav = (TPad*)gPad;
2968
2969 Int_t counter = 0;
2970
2971 //
2972 // Maintain tab numbers
2973 //
2974 Int_t from, to;
2975 GetCanvasRange(from, to, num);
2976
2977 for (int i=from; i<to; i++)
2978 {
2979 TString writename(name);
2980
2981 TCanvas *c = InitWriteTab(num<0 ? -i : i, writename);
2982 if (!c)
2983 continue;
2984
2985 //
2986 // Clone canvas and change background color and schedule for
2987 // deletion
2988 //
2989 c->SaveSource(writename, "");
2990
2991 if (num<0)
2992 *fLog << "done." << endl;
2993
2994 counter++;
2995 }
2996
2997 if (padsav)
2998 padsav->cd();
2999
3000 *fLog << inf << "done." << endl;
3001
3002 SetStatusLine2("Done.");
3003
3004 return counter>0;
3005}
3006
3007// --------------------------------------------------------------------------
3008//
3009// In case of num<0 all tabs are written into the PS file. If num>0
3010// the canvas in the corresponding tab is written to the file.
3011// Name is the name of the file (with or without extension).
3012//
3013// Returns the number of keys written.
3014//
3015// To write all tabs you can also use SaveAsPS(name)
3016//
3017Int_t MStatusDisplay::SaveAsRoot(Int_t num, TString name)
3018{
3019 num = InitWriteDisplay(num, name, "root");
3020 if (num==0)
3021 return -1;
3022
3023 TFile *fsave = gFile;
3024 TFile file(name, "RECREATE", GetTitle(), 9);
3025 const Int_t keys = Write(num);
3026 gFile = fsave;
3027
3028 SetStatusLine2("Done.");
3029
3030 return keys;
3031}
3032
3033// --------------------------------------------------------------------------
3034//
3035Bool_t MStatusDisplay::SaveAsCSV(Int_t num, TString name, Char_t delim)
3036{
3037 num = InitWriteDisplay(num, name, "csv");
3038 if (num==0)
3039 return kFALSE;
3040
3041 gSystem->ExpandPathName(name);
3042
3043 ofstream fout(name);
3044 if (!fout)
3045 {
3046 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
3047 return kFALSE;
3048 }
3049
3050 fout << 0 << delim << GetName() << delim << GetTitle() << endl;
3051
3052 Int_t from, to;
3053 GetCanvasRange(from, to, num);
3054
3055 for (int i=from; i<to; i++)
3056 {
3057 TCanvas *c;
3058 if (!(c = GetCanvas(i)))
3059 {
3060 if (num<0)
3061 *fLog << inf << " - ";
3062 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
3063 continue;
3064 }
3065
3066 fout << i << delim << c->GetName() << delim << c->GetTitle() << endl;
3067 }
3068
3069 SetStatusLine2("Done.");
3070
3071 return kTRUE;
3072}
3073
3074/*
3075Bool_t MStatusDisplay::SaveAsCSV(Int_t num, TString name)
3076{
3077 num = InitWriteDisplay(num, name, "csv");
3078 if (num==0)
3079 return kFALSE;
3080
3081 gSystem->ExpandPathName(name);
3082
3083 ofstream fout(name);
3084 if (!fout)
3085 {
3086 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
3087 return kFALSE;
3088 }
3089
3090 fout << "<?xml version=\"1.0\"?>" << endl;
3091 fout << "<display name='" << GetName() << "'>" << endl;
3092 fout << " <file>" << name << "</file>" << endl;
3093 fout << " <status>" << endl;
3094 fout << " <name>" << GetName() << "</name>" << endl;
3095 fout << " <title>" << GetTitle() << "</title>" << endl;
3096 fout << " </status>" << endl;
3097 fout << " <tabs>" << endl;
3098
3099 fout << 0 << delim << GetName() << delim << GetTitle() << endl;
3100
3101 Int_t from, to;
3102 GetCanvasRange(from, to, num);
3103
3104 for (int i=from; i<to; i++)
3105 {
3106 TCanvas *c;
3107 if (!(c = GetCanvas(i)))
3108 {
3109 if (num<0)
3110 *fLog << inf << " - ";
3111 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
3112 continue;
3113 }
3114
3115 fout << " <tab index='" << i << "'>" << endl;
3116 fout << " <index>" << i << "</index>" << endl;
3117 fout << " <name>" << c->GetName() << "</name>" << endl;
3118 fout << " <title>" << c->GetName() << "</title>" << endl;
3119 fout << " </tab>" << endl;
3120 }
3121
3122 fout << " </tabs>" << endl;
3123 fout << "</display>" << endl;
3124
3125 SetStatusLine2("Done.");
3126
3127 return kTRUE;
3128}
3129*/
3130
3131// --------------------------------------------------------------------------
3132//
3133void MStatusDisplay::SaveAs(const char *c, const Option_t *o) const
3134{
3135#if ROOT_VERSION_CODE >= ROOT_VERSION(5,18,00)
3136 TGObject::SaveAs(c, o);
3137#endif
3138}
3139
3140// --------------------------------------------------------------------------
3141//
3142// Determin File type to save file as by extension. Allowed extensions are:
3143// root, ps, pdf, svg, gif, png, jpg, xpm, C
3144//
3145// returns -1 if file type is unknown. Otherwise return value of SaveAs*
3146//
3147Int_t MStatusDisplay::SaveAs(Int_t num, TString name)
3148{
3149 if (name.EndsWith(".root")) return SaveAsRoot(num, name); // kFileSaveAsRoot
3150 if (name.EndsWith(".ps")) return SaveAsPS(num, name); // kFileSaveAsPS
3151 if (name.EndsWith(".pdf")) return SaveAsPDF(num, name); // kFileSaveAsPDF
3152 if (name.EndsWith(".svg")) return SaveAsSVG(num, name); // kFileSaveAsSVG
3153 if (name.EndsWith(".gif")) return SaveAsGIF(num, name); // kFileSaveAsGIF
3154 if (name.EndsWith(".png")) return SaveAsPNG(num, name); // kFileSaveAsPNG
3155 if (name.EndsWith(".bmp")) return SaveAsBMP(num, name); // kFileSaveAsBMP
3156 if (name.EndsWith(".xml")) return SaveAsXML(num, name); // kFileSaveAsXML
3157 if (name.EndsWith(".jpg")) return SaveAsJPG(num, name); // kFileSaveAsJPG
3158 if (name.EndsWith(".xpm")) return SaveAsXPM(num, name); // kFileSaveAsXPM
3159 if (name.EndsWith(".csv")) return SaveAsCSV(num, name); // kFileSaveAsCSV
3160 if (name.EndsWith(".tiff")) return SaveAsTIFF(num, name); // kFileSaveAsTIFF
3161 if (name.EndsWith(".C")) return SaveAsC(num, name); // kFileSaveAsC
3162 return -1;
3163}
3164
3165// --------------------------------------------------------------------------
3166//
3167// Opens a save as dialog
3168//
3169Int_t MStatusDisplay::SaveAs(Int_t num)
3170{
3171 static const char *gSaveAsTypes[] =
3172 {
3173 "PostScript", "*.ps",
3174 "Acrobat pdf", "*.pdf",
3175 "SVG vector", "*.svg",
3176 "Gif files", "*.gif",
3177 "Png files", "*.png",
3178 "Gif files", "*.gif",
3179 "Jpeg files", "*.jpeg",
3180 "Xpm files", "*.xpm",
3181 "Bmp files", "*.bmp",
3182 "Xml files", "*.xml",
3183 "Tiff files", "*.tiff",
3184 "Csv files", "*.csv",
3185 "Macro files", "*.C",
3186 "ROOT files", "*.root",
3187 "All files", "*",
3188 NULL, NULL
3189 };
3190
3191 static TString dir(".");
3192
3193 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
3194
3195 fi.fFileTypes = (const char**)gSaveAsTypes;
3196 fi.fIniDir = StrDup(dir);
3197
3198 new TGFileDialog(fClient->GetRoot(), this, kFDSave, &fi);
3199
3200 if (!fi.fFilename)
3201 return 0;
3202
3203 dir = fi.fIniDir;
3204
3205 const Int_t rc = SaveAs(num, fi.fFilename);
3206 if (rc>=0)
3207 return rc;
3208
3209 Warning("MStatusDisplay::SaveAs", "Unknown Extension: %s", fi.fFilename);
3210 return 0;
3211}
3212
3213// --------------------------------------------------------------------------
3214//
3215// Open contents of a MStatusDisplay with key name from file fname.
3216//
3217Int_t MStatusDisplay::Open(TString fname, const char *name)
3218{
3219 TFile file(fname, "READ");
3220 if (file.IsZombie())
3221 {
3222 gLog << warn << "WARNING - Cannot open file " << fname << endl;
3223 return 0;
3224 }
3225
3226 return Read(name);
3227}
3228
3229// --------------------------------------------------------------------------
3230//
3231// Opens an open dialog
3232//
3233Int_t MStatusDisplay::Open()
3234{
3235 static const char *gOpenTypes[] =
3236 {
3237 "ROOT files", "*.root",
3238 "All files", "*",
3239 NULL, NULL
3240 };
3241
3242 static TString dir(".");
3243
3244 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
3245
3246 fi.fFileTypes = (const char**)gOpenTypes;
3247 fi.fIniDir = StrDup(dir);
3248
3249 new TGFileDialog(fClient->GetRoot(), this, kFDOpen, &fi);
3250
3251 if (!fi.fFilename)
3252 return 0;
3253
3254 dir = fi.fIniDir;
3255
3256 return Open(fi.fFilename);
3257}
3258
3259// --------------------------------------------------------------------------
3260//
3261// Change width of display. The height is calculated accordingly.
3262//
3263void MStatusDisplay::SetDisplayWidth(UInt_t dw)
3264{
3265 if (gROOT->IsBatch())
3266 {
3267 SetCanvasWidth(dw);
3268 return;
3269 }
3270
3271 // 4 == 2*default border with of canvas
3272 dw -= 4;
3273
3274 // Difference between canvas size and display size
3275 const UInt_t cw = GetWidth() -fTab->GetWidth();
3276 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3277
3278 const UInt_t dh = TMath::Nint((dw - cw)/1.5 + ch);
3279
3280 Resize(dw, dh); // Set display size
3281}
3282
3283// --------------------------------------------------------------------------
3284//
3285// Change height of display. The width is calculated accordingly.
3286//
3287void MStatusDisplay::SetDisplayHeight(UInt_t dh)
3288{
3289 if (gROOT->IsBatch())
3290 {
3291 SetCanvasHeight(dh);
3292 return;
3293 }
3294
3295 // 4 == 2*default border with of canvas
3296 dh -= 4;
3297
3298 // Difference between canvas size and display size
3299 const UInt_t cw = GetWidth() -fTab->GetWidth();
3300 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3301
3302 const UInt_t dw = TMath::Nint((dh - ch)*1.5 + cw);
3303
3304 Resize(dw, dh); // Set display size
3305}
3306
3307// --------------------------------------------------------------------------
3308//
3309// Change width of canvas. The height is calculated accordingly.
3310//
3311void MStatusDisplay::SetCanvasWidth(UInt_t w)
3312{
3313 // 4 == 2*default border with of canvas
3314 w += 4;
3315
3316 if (gROOT->IsBatch())
3317 {
3318 Resize(w, 3*w/2);
3319 return;
3320 }
3321
3322 // Difference between canvas size and display size
3323 const UInt_t cw = GetWidth() -fTab->GetWidth();
3324 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3325
3326 const UInt_t h = TMath::Nint(w/1.5 + ch);
3327
3328 Resize(w + cw, h); // Set display size
3329}
3330
3331// --------------------------------------------------------------------------
3332//
3333// Change height of canvas. The width is calculated accordingly.
3334//
3335void MStatusDisplay::SetCanvasHeight(UInt_t h)
3336{
3337 // 4 == 2*default border with of canvas
3338 h += 4;
3339
3340 if (gROOT->IsBatch())
3341 {
3342 Resize(2*h/3, h);
3343 return;
3344 }
3345
3346 // Difference between canvas size and display size
3347 const UInt_t cw = GetWidth() -fTab->GetWidth();
3348 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3349
3350 // 4 == 2*default border with of canvas
3351 const UInt_t dw = TMath::Nint((h+4)*1.5 + cw);
3352
3353 Resize(dw, h + ch); // Set display size
3354}
3355
3356// --------------------------------------------------------------------------
3357//
3358// Calculate width and height of the display such that it fits into the
3359// defined box.
3360//
3361void MStatusDisplay::SetDisplaySize(UInt_t w, UInt_t h)
3362{
3363 if (gROOT->IsBatch())
3364 return;
3365
3366 SetDisplayHeight(h);
3367
3368 if (GetWidth()>w)
3369 SetDisplayWidth(w);
3370}
3371
3372// --------------------------------------------------------------------------
3373//
3374// Calculate an optimum size for the display from the desktop size
3375//
3376void MStatusDisplay::SetOptimumSize()
3377{
3378 if (gROOT->IsBatch())
3379 return;
3380
3381 const UInt_t w = TMath::Nint(0.95*gClient->GetDisplayWidth());
3382 const UInt_t h = TMath::Nint(0.95*gClient->GetDisplayHeight());
3383
3384 SetDisplaySize(w, h);
3385}
3386
3387
3388Bool_t MStatusDisplay::HandleConfigureNotify(Event_t *evt)
3389{
3390 //
3391 // The initialization of the GUI is not yet enough finished...
3392 //
3393 if (!fTab)
3394 return kTRUE;
3395
3396 UInt_t w = evt->fWidth;
3397 UInt_t h = evt->fHeight;
3398
3399 const Bool_t wchanged = w!=GetWidth()-fTab->GetWidth();
3400 const Bool_t hchanged = h!=GetHeight()-fTab->GetHeight();
3401
3402 if (!wchanged && !hchanged)
3403 {
3404 Layout();
3405 // FIXME: Make sure that this doesn't result in endless loops.
3406 return kTRUE;
3407 }
3408
3409 if (GetWidth()==1 && GetHeight()==1)
3410 return kTRUE;
3411
3412 // calculate the constant part of the window
3413 const UInt_t cw = GetWidth() -fTab->GetWidth();
3414 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3415
3416 // calculate new size of frame (canvas @ 2:3)
3417 if (hchanged)
3418 w = TMath::Nint((h-ch)*1.5+cw);
3419 else
3420 h = TMath::Nint((w-cw)/1.5+ch);
3421
3422 // resize frame
3423 Resize(w, h);
3424
3425 return kTRUE;
3426}
3427
3428Bool_t MStatusDisplay::HandleEvent(Event_t *event)
3429{
3430 // Instead of doing this in CloseWindow (called from HandleEvent)
3431 // we do it here. This makes sure, that handle event doesn't
3432 // execute code after deleting this.
3433 if (event->fType==kDestroyNotify)
3434 {
3435 if (Close())
3436 delete this;
3437// Close();
3438 return kTRUE;
3439 }
3440
3441 const Bool_t rc = TGMainFrame::HandleEvent(event);
3442
3443 //
3444 // This fixes a bug in older root versions which makes
3445 // TCanvas crash if gPad==NULL. So we make sure, that
3446 // gPad!=NULL -- be carfull, this may have other side
3447 // effects.
3448 //
3449#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,01)
3450 if (!gPad && fTab)
3451 for (int i=0; i<fTab->GetNumberOfTabs(); i++)
3452 {
3453 TCanvas *c = GetCanvas(i);
3454 if (c)
3455 {
3456 c->cd();
3457 gLog << dbg << "MStatusDisplay::HandleEvent - Workaround: gPad=" << gPad << "." << endl;
3458 break;
3459 }
3460 }
3461#endif
3462
3463 return rc;
3464}
Note: See TracBrowser for help on using the repository browser.