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

Last change on this file since 9441 was 9410, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 97.1 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 << ": " << strerror(errno) << 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 Bool_t rc = kTRUE;
1518
1519 pos.fX = pos.fY = 0;
1520 while (pos.fY < text->RowCount())
1521 {
1522 len = text->GetLineLength(pos.fY);
1523 buf1 = text->GetLine(pos, len);
1524 buf2 = new char[len + 2];
1525 strncpy(buf2, buf1, (UInt_t)len);
1526 buf2[len] = '\n';
1527 buf2[len+1] = '\0';
1528 while (buf2[i] != '\0') {
1529 if (buf2[i] == '\t') {
1530 ULong_t j = i+1;
1531 while (buf2[j] == 16 && buf2[j] != '\0')
1532 j++;
1533 strcpy(buf2+i+1, buf2+j);
1534 }
1535 i++;
1536 }
1537
1538 const UInt_t len = sizeof(char)*(strlen(buf2)+1);
1539
1540 const size_t ret = fwrite(buf2, len, 1, p);
1541 delete [] buf1;
1542 delete [] buf2;
1543
1544 if (ret!=1)
1545 {
1546 *fLog << err << "ERROR - fwrite to pipe " << pipe << " failed: " << strerror(errno) << endl;
1547 rc = kFALSE;
1548 break;
1549 }
1550
1551 pos.fY++;
1552 }
1553 gSystem->ClosePipe(p);
1554 return kTRUE;
1555}
1556
1557// --------------------------------------------------------------------------
1558//
1559// Print the log text.
1560//
1561// The default command line c is: a2ps -P%p
1562// %p: printer name
1563//
1564// The default printer name p is: <empty>
1565//
1566// Both can be changed in .rootrc by:
1567// PrintText.Printer
1568// PrintText.Command
1569//
1570Bool_t MStatusDisplay::PrintLog(const char *p, const char *c)
1571{
1572 static TString sprinter = gEnv->GetValue("PrintText.Printer", p&&*p?p:"");
1573 static TString scmd = gEnv->GetValue("PrintText.Command", c&&*c?c:"a2ps -P%p");
1574
1575 TString tmp;
1576 TString cmd = PrintDialog(sprinter, scmd, tmp);
1577 if (cmd.IsNull())
1578 return kFALSE;
1579
1580 // set status lines
1581 SetStatusLine1("Printing...");
1582 SetStatusLine2("");
1583
1584 // print to temporary file
1585 if (!SaveLogAsPS(cmd))
1586 {
1587 *fLog << warn << "MStatusDisplay::PrintLog: Sorry, couldn't create postscript!" << endl;
1588 SetStatusLine2("Failed!");
1589 return kFALSE;
1590 }
1591
1592 // execute command
1593 *fLog << dbg << "Executing: " << cmd << endl;
1594 gSystem->Exec(cmd);
1595
1596 SetStatusLine2("Done.");
1597
1598 return kTRUE;
1599}
1600
1601// --------------------------------------------------------------------------
1602//
1603// Process the kC_COMMAND, kCM_MENU messages
1604//
1605Bool_t MStatusDisplay::ProcessMessageCommandMenu(Long_t id)
1606{
1607 switch (id)
1608 {
1609 case kLoopPause:
1610 {
1611 TGPopupMenu *m = GetPopup("Loop");
1612 if (!m)
1613 return kTRUE;
1614
1615 if (fStatus==kLoopNone)
1616 {
1617 fStatus = (Status_t)kLoopPause;
1618 m->CheckEntry(kLoopPause);
1619 m->EnableEntry(kLoopStep);
1620 return kTRUE;
1621 }
1622 if (fStatus==kLoopPause)
1623 {
1624 fStatus = (Status_t)kLoopNone;
1625 m->UnCheckEntry(kLoopPause);
1626 m->DisableEntry(kLoopStep);
1627 return kTRUE;
1628 }
1629 }
1630 return kTRUE;
1631
1632 case kLoopStep:
1633 fStatus = (Status_t)kLoopStep;
1634 return kTRUE;
1635
1636 case kLoopStop:
1637 case kFileClose:
1638 case kFileExit:
1639 if (id==kFileExit || id==kFileClose)
1640 if (Close())
1641 delete this;
1642 fStatus = (Status_t)id;
1643 return kTRUE;
1644
1645 case kFileCanvas:
1646 new TCanvas;
1647 return kTRUE;
1648
1649 case kFileBrowser:
1650 new TBrowser;
1651 return kTRUE;
1652
1653 case kFileTab:
1654 AddTab(MString::Format("%d", fTab->GetNumberOfTabs()));
1655 return kTRUE;
1656
1657 case kFileReset:
1658 Reset();
1659 return kTRUE;
1660
1661 case kFileOpen:
1662 Open();
1663 return kTRUE;
1664
1665 case kFileSaveAs:
1666 SaveAs();
1667 return kTRUE;
1668
1669 case kFileSaveAsPS:
1670 SaveAsPS();
1671 return kTRUE;
1672
1673 case kFileSaveAsPDF:
1674 SaveAsPDF();
1675 return kTRUE;
1676
1677 case kFileSaveAsSVG:
1678 SaveAsSVG();
1679 return kTRUE;
1680
1681 case kFileSaveAsPNG:
1682 SaveAsPNG();
1683 return kTRUE;
1684
1685 case kFileSaveAsGIF:
1686 SaveAsGIF();
1687 return kTRUE;
1688
1689 case kFileSaveAsXPM:
1690 SaveAsXPM();
1691 return kTRUE;
1692
1693 case kFileSaveAsJPG:
1694 SaveAsJPG();
1695 return kTRUE;
1696
1697 case kFileSaveAsTIFF:
1698 SaveAsTIFF();
1699 return kTRUE;
1700
1701 case kFileSaveAsBMP:
1702 SaveAsBMP();
1703 return kTRUE;
1704
1705 case kFileSaveAsXML:
1706 SaveAsXML();
1707 return kTRUE;
1708
1709 case kFileSaveAsCSV:
1710 SaveAsCSV();
1711 return kTRUE;
1712
1713 case kFileSaveAsC:
1714 SaveAsC();
1715 return kTRUE;
1716
1717 case kFileSaveAsRoot:
1718 SaveAsRoot();
1719 return kTRUE;
1720
1721 case kFilePrint:
1722 PrintPS();
1723 return kTRUE;
1724
1725 case kTabSaveAs:
1726 SaveAs(fTab->GetCurrent());
1727 return kTRUE;
1728
1729 case kTabSaveAsPS:
1730 SaveAsPS(fTab->GetCurrent());
1731 return kTRUE;
1732
1733 case kTabSaveAsPDF:
1734 SaveAsPDF(fTab->GetCurrent());
1735 return kTRUE;
1736
1737 case kTabSaveAsSVG:
1738 SaveAsSVG(fTab->GetCurrent());
1739 return kTRUE;
1740
1741 case kTabSaveAsPNG:
1742 SaveAsPNG(fTab->GetCurrent());
1743 return kTRUE;
1744
1745 case kTabSaveAsGIF:
1746 SaveAsGIF(fTab->GetCurrent());
1747 return kTRUE;
1748
1749 case kTabSaveAsXPM:
1750 SaveAsXPM(fTab->GetCurrent());
1751 return kTRUE;
1752
1753 case kTabSaveAsJPG:
1754 SaveAsJPG(fTab->GetCurrent());
1755 return kTRUE;
1756
1757 case kTabSaveAsTIFF:
1758 SaveAsTIFF(fTab->GetCurrent());
1759 return kTRUE;
1760
1761 case kTabSaveAsBMP:
1762 SaveAsBMP(fTab->GetCurrent());
1763 return kTRUE;
1764
1765 case kTabSaveAsXML:
1766 SaveAsXML(fTab->GetCurrent());
1767 return kTRUE;
1768
1769 case kTabSaveAsCSV:
1770 SaveAsCSV(fTab->GetCurrent());
1771 return kTRUE;
1772
1773 case kTabSaveAsC:
1774 SaveAsC(fTab->GetCurrent());
1775 return kTRUE;
1776
1777 case kTabSaveAsRoot:
1778 SaveAsRoot(fTab->GetCurrent());
1779 return kTRUE;
1780
1781 case kTabPrint:
1782 PrintPS(fTab->GetCurrent());
1783 return kTRUE;
1784
1785 case kTabNext:
1786 fTab->SetTab(fTab->GetCurrent()+1);
1787 return kTRUE;
1788
1789 case kTabPrevious:
1790 fTab->SetTab(fTab->GetCurrent()-1);
1791 return kTRUE;
1792
1793 case kTabRemove:
1794 RemoveTab(fTab->GetCurrent());
1795 return kTRUE;
1796
1797 case kSize640:
1798 SetDisplaySize(640, 480);
1799 return kTRUE;
1800 case kSize768:
1801 SetDisplaySize(768, 576);
1802 return kTRUE;
1803 case kSize800:
1804 SetDisplaySize(800, 600);
1805 return kTRUE;
1806 case kSize960:
1807 SetDisplaySize(960, 720);
1808 return kTRUE;
1809 case kSize1024:
1810 SetDisplaySize(1024, 768);
1811 return kTRUE;
1812 case kSize1152:
1813 SetDisplaySize(1152, 864);
1814 return kTRUE;
1815 case kSize1280:
1816 SetDisplaySize(1280, 1024);
1817 return kTRUE;
1818 case kSize1400:
1819 SetDisplaySize(1400, 1050);
1820 return kTRUE;
1821 case kSize1600:
1822 SetDisplaySize(1600, 1200);
1823 return kTRUE;
1824 case kSizeOptimum:
1825 SetOptimumSize();
1826 return kTRUE;
1827
1828 case kLogClear:
1829 fLogBox->Clear();
1830 return kTRUE;
1831 case kLogCopy:
1832 fLogBox->Copy();
1833 return kTRUE;
1834 case kLogSelect:
1835 fLogBox->SelectAll();
1836 return kTRUE;
1837 case kLogFind:
1838 new MSearch(this);
1839 return kTRUE;
1840 case kLogSave:
1841 SetStatusLine1("Saving log...");
1842 SetStatusLine2("");
1843 *fLog << inf << "Saving log... " << flush;
1844 if (fLogBox->GetText()->Save(MString::Format("%s.log", gROOT->GetName())))
1845 {
1846 *fLog << "done." << endl;
1847 SetStatusLine2("done.");
1848 }
1849 else
1850 {
1851 *fLog << "failed!" << endl;
1852 SetStatusLine2("Failed!");
1853 }
1854 return kTRUE;
1855
1856 case kLogAppend:
1857 SetStatusLine1("Appending log...");
1858 SetStatusLine2("");
1859 *fLog << inf << "Appending log... " << flush;
1860 if (fLogBox->GetText()->Append(MString::Format("%s.log", gROOT->GetName())))
1861 {
1862 *fLog << "done." << endl;
1863 SetStatusLine2("done.");
1864 }
1865 else
1866 {
1867 *fLog << "failed!" << endl;
1868 SetStatusLine2("Failed!");
1869 }
1870 return kTRUE;
1871
1872 case kLogPrint:
1873 PrintLog();
1874 return kTRUE;
1875#ifdef DEBUG
1876 default:
1877 cout << "Command-Menu #" << id << endl;
1878#endif
1879 }
1880 return kTRUE;
1881
1882}
1883
1884// --------------------------------------------------------------------------
1885//
1886// Process the kC_COMMAND messages
1887//
1888Bool_t MStatusDisplay::ProcessMessageCommand(Long_t submsg, Long_t mp1, Long_t mp2)
1889{
1890 switch (submsg)
1891 {
1892 case kCM_MENU: // 1
1893 return ProcessMessageCommandMenu(mp1); // mp2=userdata
1894 case kCM_TAB: // 8
1895 /*
1896 for (int i=0; i<fTab->GetNumberOfTabs(); i++)
1897 fTab->GetTabContainer(i)->UnmapWindow();
1898 */
1899 UpdateTab(fTab->GetTabContainer(mp1));
1900 //fTab->GetTabContainer(mp1)->MapWindow();
1901
1902 /*
1903 if (mp1>0)
1904 fMenu->AddPopup("&CaOs", fCaOs, NULL);
1905 else
1906 fMenu->RemovePopup("CaOs");
1907 fMenu->Resize(fMenu->GetDefaultSize());
1908 MapSubwindows();
1909 MapWindow();
1910 */
1911 return kTRUE;
1912 case kCM_COMBOBOX: // 7
1913 if (mp1==kTabs)
1914 fTab->SetTab(mp2);
1915 return kTRUE;
1916#ifdef DEBUG
1917 case kCM_MENUSELECT: // 2
1918 cout << "Command-Menuselect #" << mp1 << " (UserData=" << (void*)mp2 << ")" << endl;
1919 return kTRUE;
1920
1921 case kCM_BUTTON: // 3
1922 cout << "Command-Button." << endl;
1923 return kTRUE;
1924
1925 case kCM_CHECKBUTTON: // 4
1926 cout << "Command-CheckButton." << endl;
1927 return kTRUE;
1928
1929 case kCM_RADIOBUTTON: // 5
1930 cout << "Command-RadioButton." << endl;
1931 return kTRUE;
1932
1933 case kCM_LISTBOX: // 6
1934 cout << "Command-Listbox #" << mp1 << " (LineId #" << mp2 << ")" << endl;
1935 return kTRUE;
1936 default:
1937 cout << "Command: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
1938#endif
1939 }
1940 return kTRUE;
1941}
1942
1943// --------------------------------------------------------------------------
1944//
1945// Process the kC_TEXTVIEW messages
1946//
1947Bool_t MStatusDisplay::ProcessMessageTextview(Long_t /*submsg*/, Long_t /*mp1*/, Long_t /*mp2*/)
1948{
1949 // kC_TEXTVIEW, kTXT_ISMARKED, widget id, [true|false] //
1950 // kC_TEXTVIEW, kTXT_DATACHANGE, widget id, 0 //
1951 // kC_TEXTVIEW, kTXT_CLICK2, widget id, position (y << 16) | x) //
1952 // kC_TEXTVIEW, kTXT_CLICK3, widget id, position (y << 16) | x) //
1953 // kC_TEXTVIEW, kTXT_F3, widget id, true //
1954 // kC_TEXTVIEW, kTXT_OPEN, widget id, 0 //
1955 // kC_TEXTVIEW, kTXT_CLOSE, widget id, 0 //
1956 // kC_TEXTVIEW, kTXT_SAVE, widget id, 0 //
1957#ifdef DEBUG
1958 switch (submsg)
1959 {
1960 case kTXT_ISMARKED:
1961 cout << "Textview-IsMarked #" << mp1 << " " << (mp2?"yes":"no") << endl;
1962 return kTRUE;
1963
1964 case kTXT_DATACHANGE:
1965 cout << "Textview-DataChange #" << mp1 << endl;
1966 return kTRUE;
1967
1968 case kTXT_CLICK2:
1969 cout << "Textview-Click2 #" << mp1 << " x=" << (mp2&0xffff) << " y= " << (mp2>>16) << endl;
1970 return kTRUE;
1971
1972 case kTXT_CLICK3:
1973 cout << "Textview-Click3 #" << mp1 << " x=" << (mp2&0xffff) << " y= " << (mp2>>16) << endl;
1974 return kTRUE;
1975
1976 case kTXT_F3:
1977 cout << "Textview-F3 #" << mp1 << endl;
1978 return kTRUE;
1979
1980 case kTXT_OPEN:
1981 cout << "Textview-Open #" << mp1 << endl;
1982 return kTRUE;
1983
1984 case kTXT_CLOSE:
1985 cout << "Textview-Close #" << mp1 << endl;
1986 return kTRUE;
1987
1988 case kTXT_SAVE:
1989 cout << "Textview-Save #" << mp1 << endl;
1990 return kTRUE;
1991
1992 default:
1993 cout << "Textview: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
1994 }
1995#endif
1996 return kTRUE;
1997}
1998
1999// --------------------------------------------------------------------------
2000//
2001// Process the kC_USER messages
2002//
2003Bool_t MStatusDisplay::ProcessMessageUser(Long_t submsg, Long_t mp1, Long_t mp2)
2004{
2005 // kS_START, case sensitive | backward<<1, char *txt
2006 switch (submsg)
2007 {
2008 case kS_START:
2009 fLogBox->Search((char*)mp2, !(mp1&2>>1), mp1&1);
2010 return kTRUE;
2011#ifdef DEBUG
2012 default:
2013 cout << "User: " << "Submsg:" << submsg << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2014#endif
2015 }
2016 return kTRUE;
2017}
2018
2019// --------------------------------------------------------------------------
2020//
2021// Process the messages from the GUI
2022//
2023Bool_t MStatusDisplay::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
2024{
2025 // Can be found in WidgetMessageTypes.h
2026#ifdef DEBUG
2027 cout << "Msg: " << GET_MSG(msg) << " Submsg:" << GET_SUBMSG(msg);
2028 cout << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2029#endif
2030 switch (GET_MSG(msg))
2031 {
2032 case kC_COMMAND: // 1
2033 return ProcessMessageCommand(GET_SUBMSG(msg), mp1, mp2);
2034
2035 case kC_TEXTVIEW: // 9
2036 return ProcessMessageTextview(GET_SUBMSG(msg), mp1, mp2);
2037
2038 case kC_USER: // 1001
2039 return ProcessMessageUser(GET_SUBMSG(msg), mp1, mp2);
2040 }
2041#ifdef DEBUG
2042 cout << "Msg: " << GET_MSG(msg) << " Submsg:" << GET_SUBMSG(msg);
2043 cout << " Mp1=" << mp1 << " Mp2=" << mp2 << endl;
2044#endif
2045 return kTRUE;
2046}
2047
2048Bool_t MStatusDisplay::Close()
2049{
2050 // Got close message for this MainFrame. Calls parent CloseWindow()
2051 // (which destroys the window) and terminate the application.
2052 // The close message is generated by the window manager when its close
2053 // window menu item is selected.
2054
2055 // CloseWindow must be overwritten because otherwise CloseWindow
2056 // and the destructor are calling DestroyWindow which seems to be
2057 // in conflict with the TRootEmbeddedCanvas.
2058
2059 // FIXME: Make sure that the Status Display is deleted from every
2060 // where (eg Eventloop) first!
2061
2062 //gLog << dbg << fName << " is on heap: " << (int)IsOnHeap() << endl;
2063
2064 if (TestBit(kExitLoopOnExit) || TestBit(kExitLoopOnClose))
2065 {
2066 //gLog << dbg << "CloseWindow() calling ExitLoop." << endl;
2067 gSystem->ExitLoop();
2068 }
2069
2070 if (fIsLocked<=0 && IsOnHeap())
2071 return kTRUE;
2072
2073 fStatus = kFileExit;
2074 return kFALSE;
2075}
2076
2077void MStatusDisplay::CloseWindow()
2078{
2079 if (Close())
2080 delete this;
2081}
2082
2083// --------------------------------------------------------------------------
2084//
2085// Calls SetBit(kNoContextMenu) for all TCanvas objects found in the
2086// Tabs.
2087//
2088void MStatusDisplay::SetNoContextMenu(Bool_t flag)
2089{
2090 if (fIsLocked>1 || gROOT->IsBatch())
2091 return;
2092
2093 flag ? SetBit(kNoContextMenu) : ResetBit(kNoContextMenu);
2094
2095 for (int i=1; i<fTab->GetNumberOfTabs(); i++)
2096 {
2097 TCanvas *c = GetCanvas(i);
2098 if (c)
2099 flag ? c->SetBit(kNoContextMenu) : c->ResetBit(kNoContextMenu);
2100 }
2101}
2102
2103// --------------------------------------------------------------------------
2104//
2105// Update the memory display in the status bar
2106//
2107void MStatusDisplay::UpdateMemory() const
2108{
2109 const TString path = MString::Format("/proc/%d/status", gSystem->GetPid());
2110 if (gSystem->AccessPathName(path, kFileExists))
2111 return;
2112
2113 TEnv env(path);
2114 const UInt_t kb = env.GetValue("VmSize", 0);
2115 if (kb==0)
2116 return;
2117
2118 char type = 'k';
2119 Float_t val = kb;
2120
2121 if (val>999)
2122 {
2123 type = 'M';
2124 val /= 1000;
2125 }
2126 if (val>999)
2127 {
2128 type = 'G';
2129 val /= 1000;
2130 }
2131 const TString txt = MString::Format("%.1f%c", val, type);
2132 fStatusBar->SetText(txt, 2);
2133}
2134
2135// --------------------------------------------------------------------------
2136//
2137// Updates the canvas (if existing) in the currenly displayed Tab.
2138// The update intervall is controlled by StartUpdate and StopUpdate
2139//
2140Bool_t MStatusDisplay::HandleTimer(TTimer *timer)
2141{
2142 if (gROOT->IsBatch())
2143 return kTRUE;
2144
2145 UpdateMemory();
2146
2147 const Int_t c = fTab->GetCurrent();
2148
2149 // Skip Legend Tab
2150 if (c==0)
2151 return kTRUE;
2152
2153 // Update a canvas tab (if visible)
2154 if (timer==&fTimer && c!=fLogIdx)
2155 {
2156 UpdateTab(fTab->GetCurrentContainer());
2157 return kTRUE;
2158 }
2159
2160 // update the logbook tab (if visible)
2161 if (timer==&fLogTimer && c==fLogIdx)
2162 {
2163 fLog->UpdateGui();
2164
2165 /*
2166 if (!fLogBox->TestBit(kHasChanged))
2167 return kTRUE;
2168
2169 fLogBox->ResetBit(kHasChanged);
2170 */
2171 return kTRUE;
2172 }
2173
2174 return kTRUE;
2175}
2176
2177// --------------------------------------------------------------------------
2178//
2179// Find an object in a canvas (uses MStatusArray as helper)
2180//
2181void MStatusDisplay::PrintContent(Option_t *o) const
2182{
2183 MStatusArray(*this).Print(o);
2184}
2185
2186// --------------------------------------------------------------------------
2187//
2188// Find an object in a canvas (uses MStatusArray as helper)
2189//
2190TObject *MStatusDisplay::FindObjectInCanvas(const char *obj, const char *base, const char *canv) const
2191{
2192 return MStatusArray(*this).FindObjectInCanvas(obj, base, canv);
2193}
2194
2195// --------------------------------------------------------------------------
2196//
2197// Draws a clone of a canvas into a new canvas. Taken from TCanvas.
2198//
2199void MStatusDisplay::DrawClonePad(TCanvas &newc, TCanvas &oldc) const
2200{
2201 //copy pad attributes
2202 newc.Range(oldc.GetX1(),oldc.GetY1(),oldc.GetX2(),oldc.GetY2());
2203 newc.SetTickx(oldc.GetTickx());
2204 newc.SetTicky(oldc.GetTicky());
2205 newc.SetGridx(oldc.GetGridx());
2206 newc.SetGridy(oldc.GetGridy());
2207 newc.SetLogx(oldc.GetLogx());
2208 newc.SetLogy(oldc.GetLogy());
2209 newc.SetLogz(oldc.GetLogz());
2210 newc.SetBorderSize(oldc.GetBorderSize());
2211 newc.SetBorderMode(oldc.GetBorderMode());
2212 ((TAttLine&)oldc).Copy((TAttLine&)newc);
2213 ((TAttFill&)oldc).Copy((TAttFill&)newc);
2214 ((TAttPad&)oldc).Copy((TAttPad&)newc);
2215
2216 // This must be there: Otherwise GetDrawOption() won't work
2217 TVirtualPad *padsav = gPad;
2218 oldc.cd();
2219
2220 const Bool_t store = TH1::AddDirectoryStatus();
2221 TH1::AddDirectory(kFALSE);
2222
2223 //copy primitives
2224 TObject *obj;
2225 TIter next(oldc.GetListOfPrimitives());
2226 while ((obj=next()))
2227 {
2228 // Old line - I think it is not necessary anymore because of the cd()
2229 //gROOT->SetSelectedPad(&newc);
2230
2231 // Now make a clone of the object
2232 TObject *clone = obj->Clone();
2233
2234 // Clone also important bits (FIXME: Is this correct)
2235 clone->SetBit(obj->TestBits(kCannotPick|kNoContextMenu));
2236
2237 // Now make sure that the clones are deleted at a later time
2238 clone->SetBit(kCanDelete|kMustCleanup);
2239
2240 // FIXME: This is a workaround for the problem with the MAstroCatalog in
2241 // MHFalseSource. It doesn't harm. We'll still try to find the reason
2242 if (clone->IsA()==TPad::Class())
2243 gROOT->GetListOfCleanups()->Add(clone);
2244
2245 // Add the clone and its draw-option to the current pad
2246 TVirtualPad *save2 = gPad;
2247 gPad = &oldc; // Don't do this before Clone()!
2248 newc.GetListOfPrimitives()->Add(clone, obj->GetDrawOption());
2249 gPad = save2;
2250 }
2251 newc.Modified();
2252 newc.Update();
2253
2254 TH1::AddDirectory(store);
2255
2256 padsav->cd();
2257}
2258
2259// --------------------------------------------------------------------------
2260//
2261// Display the contexts of a TObjArray in the display (all canvases)
2262//
2263Bool_t MStatusDisplay::Display(const TObjArray &list, const char *tab)
2264{
2265 TIter Next(&list);
2266
2267 TObject *o=Next();
2268 if (!o)
2269 {
2270 *fLog << err << "MStatusDisplay::Display: No entry in TObjArray." << endl;
2271 return kFALSE;
2272 }
2273
2274 fTitle = o->GetTitle();
2275
2276 TCanvas *c;
2277 while ((c=(TCanvas*)Next()))
2278 //if (!GetCanvas(c->GetName()))
2279 if (c->InheritsFrom(TCanvas::Class()))
2280 if (!tab || c->GetName()==(TString)tab)
2281 DrawClonePad(AddTab(c->GetName(), c->GetTitle()), *c);
2282
2283 return kTRUE;
2284}
2285
2286// --------------------------------------------------------------------------
2287//
2288// Reads the contents of a saved MStatusDisplay from a file.
2289//
2290Int_t MStatusDisplay::Read(const char *name, const char *tab)
2291{
2292 if (!gFile)
2293 {
2294 *fLog << warn << "MStatusDisplay::Read: No file found. Please create a TFile first." << endl;
2295 return 0;
2296 }
2297
2298 if (!gFile->IsOpen())
2299 {
2300 *fLog << warn << "MStatusDisplay::Read: File not open. Please open the TFile first." << endl;
2301 return 0;
2302 }
2303
2304 MStatusArray list;
2305
2306 const Int_t n = list.Read(name);
2307
2308 //
2309 // If no status display was found with this name try to find canvases
2310 // in the file and s´display them instead.
2311 //
2312 if (n==0)
2313 {
2314 const Bool_t store = TH1::AddDirectoryStatus();
2315 TH1::AddDirectory(kFALSE);
2316
2317 TIter Next(gFile->GetListOfKeys());
2318 TObject *key = 0;
2319 while ((key=Next()))
2320 {
2321 TCanvas *c=0;
2322 gFile->GetObject(key->GetName(), c);
2323 if (!c)
2324 {
2325 AddTab(key->GetName(), key->GetTitle());
2326 TObject *obj = gFile->Get(key->GetName());
2327 obj->SetBit(kCanDelete);
2328 obj->Draw();
2329 continue;
2330 }
2331
2332 if (list.GetEntries()==0)
2333 list.Add(new TNamed(GetName(), GetTitle()));
2334
2335 c->SetTitle(gFile->GetName());
2336 list.Add(c);
2337 }
2338
2339 TH1::AddDirectory(store);
2340
2341 if (list.GetEntries()==0)
2342 {
2343 *fLog << warn << "MStatusDisplay::Read: No objects read from " << gFile->GetName() << endl;
2344 return 0;
2345 }
2346
2347 *fLog << inf << "MStatusDisplay: " << list.GetEntries() << " canvases directly read from file." << endl;
2348 }
2349
2350
2351 if (!Display(list, tab))
2352 {
2353 *fLog << err << "MStatusDisplay::Display: No entries found." << endl;
2354 return 0;
2355 }
2356
2357
2358 if (n==0)
2359 return list.GetEntries();
2360
2361 *fLog << inf << "MStatusDisplay: Key " << name << " with " << n << " keys read from file." << endl;
2362 return n;
2363}
2364
2365// --------------------------------------------------------------------------
2366//
2367// Add all canvases to the MStatusArray
2368//
2369void MStatusDisplay::FillArray(MStatusArray &list, Int_t num) const
2370{
2371 Int_t from, to;
2372 GetCanvasRange(from, to, num);
2373
2374 TCanvas *c;
2375 for (int i=from; i<to; i++)
2376 if ((c = GetCanvas(i)))
2377 list.Add(c);
2378}
2379
2380// --------------------------------------------------------------------------
2381//
2382// Writes the contents of a MStatusDisplay to a file.
2383//
2384Int_t MStatusDisplay::Write(Int_t num, const char *name, Int_t /*option*/, Int_t /*bufsize*/) const
2385{
2386 if (!gFile)
2387 {
2388 *fLog << warn << "MStatusDisplay::Write: No file found. Please create a TFile first." << endl;
2389 return 0;
2390 }
2391
2392 if (!gFile->IsOpen())
2393 {
2394 *fLog << warn << "MStatusDisplay::Write: File not open. Please open the TFile first." << endl;
2395 return 0;
2396 }
2397
2398 if (!gFile->IsWritable())
2399 {
2400 *fLog << warn << "MStatusDisplay::Write: File not writable." << endl;
2401 return 0;
2402 }
2403
2404 if (num==0)
2405 {
2406 *fLog << warn << "MStatusDisplay::Write: Tab doesn't contain an embedded Canvas... skipped." << endl;
2407 return 0;
2408 }
2409
2410 if (!gROOT->IsBatch() && num>=fTab->GetNumberOfTabs())
2411 {
2412 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
2413 return 0;
2414 }
2415 if (gROOT->IsBatch() && num>fBatch->GetSize())
2416 {
2417 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
2418 return 0;
2419 }
2420
2421 MStatusArray list;
2422
2423 // Be careful: So far Display() assumes that it is the first entry in the list
2424 TNamed named;
2425 named.SetTitle(fTitle);
2426 list.Add(&named);
2427
2428 FillArray(list, num);
2429
2430 const Int_t n = list.Write(name, kSingleKey);
2431
2432 //*fLog << inf << "MStatusDisplay: " << n << " keys written to file as key " << name << "." << endl;
2433
2434 return n;
2435}
2436
2437// --------------------------------------------------------------------------
2438//
2439// Use this to start the synchronous (GUI eventloop driven) tab update.
2440// Can also be used to change the update intervall. If millisec<0
2441// the intervall given in SetUpdateTime is used. If the intervall in
2442// SetUpdateTime is <0 nothing is done. (Call SetUpdateTime(-1) to
2443// disable the automatic update in a MEventloop.
2444//
2445void MStatusDisplay::StartUpdate(Int_t millisec)
2446{
2447 if (fIsLocked>1)
2448 return;
2449
2450 if (fTimer.GetTime()<TTime(0))
2451 return;
2452 fTimer.Start(millisec);
2453}
2454
2455// --------------------------------------------------------------------------
2456//
2457// Stops the automatic GUI update
2458//
2459void MStatusDisplay::StopUpdate()
2460{
2461 if (fIsLocked>1)
2462 return;
2463
2464 fTimer.Stop();
2465}
2466
2467// --------------------------------------------------------------------------
2468//
2469// Set the update interval for the GUI update, see StartUpdate.
2470//
2471void MStatusDisplay::SetUpdateTime(Long_t t)
2472{
2473 fTimer.SetTime(t);
2474}
2475
2476// --------------------------------------------------------------------------
2477//
2478// If the filename name doesn't end with ext, ext is added to the end.
2479// If name.IsNull() "status" is assumed and the a number (>0) is added
2480// as "status-6".
2481// The extension is returned.
2482//
2483const TString &MStatusDisplay::AddExtension(TString &name, const TString &ext, Int_t num) const
2484{
2485 if (name.IsNull())
2486 {
2487 name = gROOT->GetName();
2488 if (num>0)
2489 {
2490 name += "-";
2491 name += num;
2492 }
2493 }
2494
2495 if (name.EndsWith("."+ext))
2496 return ext;
2497
2498 name += ".";
2499 name += ext;
2500
2501 return ext;
2502}
2503
2504Bool_t MStatusDisplay::CheckTabForCanvas(int num) const
2505{
2506 if (gROOT->IsBatch())
2507 return (num>0 && num<=fBatch->GetSize()) || num<0;
2508
2509 if (num>=fTab->GetNumberOfTabs())
2510 {
2511 *fLog << warn << "Tab #" << num << " doesn't exist..." << endl;
2512 return kFALSE;
2513 }
2514 if (num==0)
2515 {
2516 *fLog << warn << "Tab #" << num << " doesn't contain an embedded canvas..." << endl;
2517 return kFALSE;
2518 }
2519 if (fTab->GetNumberOfTabs()<2 || !gPad)
2520 {
2521 *fLog << warn << "Sorry, you must have at least one existing canvas (gPad!=NULL)" << endl;
2522 return kFALSE;
2523 }
2524 return kTRUE;
2525}
2526
2527// --------------------------------------------------------------------------
2528//
2529// Insert the following two lines into the postscript header:
2530//
2531// %%DocumentPaperSizes: a4
2532// %%Orientation: Landscape
2533//
2534void MStatusDisplay::UpdatePSHeader(const TString &name) const
2535{
2536 const TString newstr("%%DocumentPaperSizes: a4\n%%Orientation: Landscape\n");
2537
2538 TString tmp(name+"XXXXXX");
2539
2540 // FIXME: Use mkstemp instead
2541 if (!mktemp(const_cast<char*>(tmp.Data())))
2542 {
2543 *fLog << err << "ERROR - MStatusDisplay::UpdatePSHeader: mktemp failed." << endl;
2544 return;
2545 }
2546
2547 ifstream fin(name);
2548 ofstream fout(tmp);
2549 if (!fout)
2550 {
2551 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
2552 return;
2553 }
2554
2555 char c;
2556
2557 TString str;
2558 fin >> str >> c; // Read "%!PS-Adobe-2.0\n"
2559 fout << str << endl << newstr;
2560
2561 // Doing it in blocks seems not to gain much for small (MB) files
2562 while (fin)
2563 {
2564 fin.read(&c, 1);
2565 fout.write(&c, 1);
2566 }
2567
2568 gSystem->Unlink(name);
2569 gSystem->Rename(tmp, name);
2570}
2571
2572// --------------------------------------------------------------------------
2573//
2574void MStatusDisplay::PSToolsRange(TVirtualPS &vps, Float_t psw, Float_t psh) const
2575{
2576 if (vps.InheritsFrom(TPostScript::Class()))
2577 static_cast<TPostScript&>(vps).Range(psw, psh);
2578 // if (vps.InheritsFrom(TPDF::Class()))
2579 // static_cast<TPDF&>(vps).Range(psw*0.69, psh*0.69);
2580 // if (vps.InheritsFrom(TSVG::Class()))
2581 // static_cast<TSVG&>(vps).Range(psw, psh);
2582}
2583
2584// --------------------------------------------------------------------------
2585//
2586void MStatusDisplay::PSToolsTextNDC(TVirtualPS &vps, Double_t u, Double_t v, const char *string) const
2587{
2588 if (vps.InheritsFrom(TPostScript::Class()))
2589 static_cast<TPostScript&>(vps).TextNDC(u, v, string);
2590 if (vps.InheritsFrom(TPDF::Class()))
2591 static_cast<TPDF&>(vps).TextNDC(u, v, string);
2592 // if (vps.InheritsFrom(TSVG::Class()))
2593 // static_cast<TSVG&>(vps).TextNDC(u, v, string);
2594}
2595
2596// --------------------------------------------------------------------------
2597//
2598Int_t MStatusDisplay::InitWriteDisplay(Int_t num, TString &name, const TString &ext)
2599{
2600 SetStatusLine1(MString::Format("Writing %s file...",ext.Data()));
2601 SetStatusLine2("Please be patient!");
2602
2603 if (!CheckTabForCanvas(num))
2604 {
2605 SetStatusLine2("Failed!");
2606 return 0;
2607 }
2608
2609 AddExtension(name, ext, num);
2610
2611 if (num<0)
2612 *fLog << inf << "Open " << ext << "-File: " << name << endl;
2613
2614 return num;
2615}
2616
2617// --------------------------------------------------------------------------
2618//
2619TCanvas *MStatusDisplay::InitWriteTab(Int_t num, TString &name)
2620{
2621 const Int_t i = TMath::Abs(num);
2622
2623 TCanvas *c = GetCanvas(i);
2624 if (!c)
2625 {
2626 if (num<0)
2627 *fLog << inf << " - ";
2628 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
2629 return 0;
2630 }
2631
2632 SetStatusLine2(MString::Format("Tab #%d", i));
2633
2634 //
2635 // Paint canvas into root file
2636 //
2637 if (num<0 && !name.IsNull())
2638 {
2639 Bool_t found = kFALSE;
2640 if (name.Index("%%%%name%%%%"))
2641 {
2642 name.ReplaceAll("%%name%%", c->GetName());
2643 found = kTRUE;
2644 }
2645
2646 if (name.Index("%%%%title%%%%"))
2647 {
2648 name.ReplaceAll("%%title%%", c->GetTitle());
2649 found = kTRUE;
2650 }
2651
2652 if (name.Index("%%%%tab%%%%"))
2653 {
2654 name.ReplaceAll("%%tab%%", MString::Format("%d", i));
2655 found = kTRUE;
2656 }
2657
2658 if (!found)
2659 name.Insert(name.Last('.'), MString::Format("-%d", i));
2660 }
2661
2662 if (num<0)
2663 *fLog << inf << " - ";
2664 *fLog << inf << "Writing Tab #" << i;
2665
2666 if (!name.IsNull())
2667 *fLog << " to " << name;
2668
2669 *fLog << ": " << c->GetName() << "... " << flush;
2670
2671 return c;
2672}
2673
2674// This is a stupid workaround to get rid of the damned clipping
2675// of the text. Who the hell needs clipping?
2676class MyCanvas : public TCanvas
2677{
2678public:
2679 void Scale(Double_t val)
2680 {
2681 fAbsXlowNDC = -val;
2682 fAbsYlowNDC = -val;
2683 fAbsWNDC = 1+2*val;
2684 fAbsHNDC = 1+2*val;
2685 }
2686};
2687
2688// --------------------------------------------------------------------------
2689//
2690// Write some VGF (vector graphics format). Currently PS, PDF and SVG
2691// is available. Specified by ext.
2692//
2693// In case of num<0 all tabs are written into the VGF file. If num>0
2694// the canvas in the corresponding tab is written to the file.
2695// Name is the name of the file (with or without extension).
2696//
2697// Returns the number of pages written.
2698//
2699// To write all tabs you can also use SaveAsVGF(name, ext)
2700//
2701// If the third argument is given a bottom line is drawn with the text
2702// under it. If no argument is given a bottom line is drawn if
2703// fTitle (SetTitle) is not empty.
2704//
2705Int_t MStatusDisplay::SaveAsVGF(Int_t num, TString name, const TString addon, const TString ext)
2706{
2707 num = InitWriteDisplay(num, name, ext);
2708 if (num==0)
2709 return 0;
2710
2711 TPad *padsav = (TPad*)gPad;
2712 TVirtualPS *psave = gVirtualPS;
2713
2714 TDatime d;
2715
2716 Int_t type = -1;
2717
2718 TVirtualPS *ps =0;
2719 if (!ext.CompareTo("ps", TString::kIgnoreCase))
2720 {
2721 gStyle->SetColorModelPS(1);
2722 ps = new TPostScript(name, 112);
2723 type = 1;
2724 }
2725 if (!ext.CompareTo("pdf", TString::kIgnoreCase))
2726 {
2727 ps = new TPDF(name, 112);
2728 type = 2;
2729 }
2730 if (!ext.CompareTo("svg", TString::kIgnoreCase))
2731 {
2732 ps = new TSVG(name, 112);
2733 type = 3;
2734 }
2735
2736 if (!ps)
2737 {
2738 *fLog << err << "Extension " << ext << " unknown..." << endl;
2739 SetStatusLine2("Failed!");
2740 return 0;
2741 }
2742
2743 ps->SetBit(TPad::kPrintingPS);
2744 if (type==1)
2745 ps->PrintFast(13, "/nan {1} def ");
2746
2747 gVirtualPS = ps;
2748
2749 //
2750 // Create some GUI elements for a page legend
2751 //
2752 TLine line;
2753
2754 int page = 1;
2755
2756 //
2757 // Maintain tab numbers
2758 //
2759 Int_t from, to;
2760 GetCanvasRange(from, to, num);
2761
2762 for (int i=from; i<to; i++)
2763 {
2764 TCanvas *c = InitWriteTab(num<0?-i:i);
2765 if (c==0)
2766 continue;
2767
2768 //
2769 // Init page and page size, make sure, that the canvas in the file
2770 // has the same Aspect Ratio than on the screen.
2771 //
2772 if (type==1 || i>from)
2773 ps->NewPage();
2774
2775 //
2776 // 28 is used here to scale the canvas into a height of 28,
2777 // such that the page title can be set above the canvas...
2778 //
2779 Float_t psw = 28.0; // A4 - width (29.7)
2780 Float_t psh = 21.0; // A4 - height (21.0)
2781
2782 const Float_t cw = c->GetWw();
2783 const Float_t ch = c->GetWh();
2784
2785 if (psw/psh>cw/ch)
2786 psw = cw/ch*psh;
2787 else
2788 psh = ch/cw*psw;
2789
2790 PSToolsRange(*ps, psw, psh);
2791
2792 //
2793 // Clone canvas and change background color and schedule for
2794 // deletion
2795 //
2796
2797 //const Bool_t store = c->IsBatch();
2798 //c->SetBatch(kTRUE);
2799 c->Paint();
2800 //c->SetBatch(store);
2801
2802 //
2803 // Change/fix the canvas coordinate system for the overlaying text.
2804 // This is necessary because root clip everything away which is
2805 // outside a predefined area, which is (0,0)/(1,1)
2806 //
2807 const Double_t height = 0.015; // Text height
2808 const Double_t off = 0.005; // Line offset from text
2809
2810 const Double_t bot = height+off;
2811 const Double_t top = 1-bot;
2812
2813 static_cast<MyCanvas*>(c)->Scale(bot);
2814
2815 // If gPad is not set to c all follwing commands will
2816 // get the wrong numbers for alignment
2817 gPad = c;
2818
2819 // Separator Lines
2820 line.PaintLineNDC(0.01, top, 0.99, top);
2821 line.PaintLineNDC(0.01, bot, 0.99, bot);
2822
2823 //
2824 // Print overlaying text (NDC = %)
2825 //
2826 // align phi col font size (11=left top)
2827 const TString txt(addon.IsNull() ? fTitle : addon);
2828
2829 // Text Attributes
2830 TAttText(11, 0, kBlack, 22, height).Copy(*ps);
2831
2832 // Text on top
2833 ps->SetTextAlign(11); // left bottom
2834 PSToolsTextNDC(*ps, 0.01, top+off, c->GetName());
2835
2836 ps->SetTextAlign(21); // cent bottom
2837 PSToolsTextNDC(*ps, 0.50, top+off, TString("MARS V"MARSVER" - Modular Analysis and Reconstruction Software - ")+d.AsString());
2838
2839 ps->SetTextAlign(31); // right bottom
2840 PSToolsTextNDC(*ps, 0.99, top+off, MString::Format("Page No.%i (%i)", page++, i));
2841
2842 // Text on bottom
2843 ps->SetTextAlign(13); // left top
2844 PSToolsTextNDC(*ps, 0.01, bot-off, c->GetTitle());
2845
2846 ps->SetTextAlign(23); // cent top
2847 PSToolsTextNDC(*ps, 0.50, bot-off, txt);
2848
2849 ps->SetTextAlign(33); // right top
2850 PSToolsTextNDC(*ps, 0.99, bot-off, MString::Format("(c) 2000-%d, Thomas Bretz", TDatime().GetYear()));
2851
2852 static_cast<MyCanvas*>(c)->Scale(0);
2853
2854 //
2855 // Finish drawing page
2856 //
2857 *fLog << "done." << endl;
2858 }
2859
2860 gPad = NULL; // Important!
2861
2862 ps->Close();
2863 delete ps;
2864
2865#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
2866 if (type==1)
2867 {
2868 SetStatusLine2("Updating header of PS file...");
2869
2870 if (num<0)
2871 *fLog << inf3 << " - Updating header of PS file... " << flush;
2872 UpdatePSHeader(name);
2873 if (num<0)
2874 *fLog << inf3 << "done." << endl;
2875 }
2876#endif
2877
2878 gVirtualPS = psave;
2879 if (padsav)
2880 padsav->cd();
2881
2882 if (num<0)
2883 *fLog << inf << "done." << endl;
2884
2885 SetStatusLine2(MString::Format("Done (%dpages)", page-1));
2886
2887 return page-1;
2888}
2889
2890// --------------------------------------------------------------------------
2891//
2892Bool_t MStatusDisplay::SaveAsImage(Int_t num, TString name, TImage::EImageFileTypes type)
2893{
2894#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
2895 if (gROOT->IsBatch())
2896 {
2897 *fLog << warn << "Sorry, writing image-files is not available in batch mode." << endl;
2898 return 0;
2899 }
2900#endif
2901
2902 TString ext;
2903 switch (type)
2904 {
2905 case TImage::kXpm:
2906 case TImage::kZCompressedXpm: ext = "xpm"; break;
2907 case TImage::kPng: ext = "png"; break;
2908 case TImage::kJpeg: ext = "jpg"; break;
2909 case TImage::kGif: ext = "gif"; break;
2910 case TImage::kTiff: ext = "tiff"; break;
2911 case TImage::kBmp: ext = "bmp"; break;
2912 case TImage::kXml: ext = "xml"; break;
2913 //case TImage::kGZCompressedXpm: ext = "xpm.gz"; break;
2914 //case TImage::kPpm: ext = "ppm"; break;
2915 //case TImage::kPnm: ext = "pnm"; break;
2916 //case TImage::kIco: ext = "ico"; break;
2917 //case TImage::kCur: ext = "cur"; break;
2918 //case TImage::kXcf: ext = "xcf"; break;
2919 //case TImage::kXbm: ext = "xbm"; break;
2920 //case TImage::kFits: ext = "fits"; break;
2921 //case TImage::kTga: ext = "tga"; break;
2922 default:
2923 *fLog << warn << "Sorry, unknown or unsupported file type..." << endl;
2924 return 0;
2925 }
2926
2927 num = InitWriteDisplay(num, name, ext);
2928 if (num==0)
2929 return 0;
2930
2931 TPad *padsav = (TPad*)gPad;
2932
2933 Int_t counter = 0;
2934
2935 //
2936 // Maintain tab numbers
2937 //
2938 Int_t from, to;
2939 GetCanvasRange(from, to, num);
2940
2941 for (int i=from; i<to; i++)
2942 {
2943 TString writename(name);
2944
2945 TCanvas *c = InitWriteTab(num<0 ? -i : i, writename);
2946 if (!c)
2947 continue;
2948
2949 //
2950 // Paint canvas into root file
2951 //
2952
2953 // TImage *img = TImage::Create();
2954 // img->FromPad(c);
2955 // img->WriteImage(writename, type);
2956 // delete img;
2957
2958 // FIXME: Not all file types are supported by Print()
2959 c->Print(writename);
2960
2961 if (num<0)
2962 *fLog << "done." << endl;
2963
2964 counter++;
2965 }
2966
2967 if (padsav)
2968 padsav->cd();
2969
2970 *fLog << inf << "done." << endl;
2971
2972 SetStatusLine2("Done.");
2973
2974 return counter>0;
2975}
2976
2977// --------------------------------------------------------------------------
2978//
2979Bool_t MStatusDisplay::SaveAsC(Int_t num, TString name)
2980{
2981 num = InitWriteDisplay(num, name, "C");
2982 if (num==0)
2983 return kFALSE;
2984
2985 TPad *padsav = (TPad*)gPad;
2986
2987 Int_t counter = 0;
2988
2989 //
2990 // Maintain tab numbers
2991 //
2992 Int_t from, to;
2993 GetCanvasRange(from, to, num);
2994
2995 for (int i=from; i<to; i++)
2996 {
2997 TString writename(name);
2998
2999 TCanvas *c = InitWriteTab(num<0 ? -i : i, writename);
3000 if (!c)
3001 continue;
3002
3003 //
3004 // Clone canvas and change background color and schedule for
3005 // deletion
3006 //
3007 c->SaveSource(writename, "");
3008
3009 if (num<0)
3010 *fLog << "done." << endl;
3011
3012 counter++;
3013 }
3014
3015 if (padsav)
3016 padsav->cd();
3017
3018 *fLog << inf << "done." << endl;
3019
3020 SetStatusLine2("Done.");
3021
3022 return counter>0;
3023}
3024
3025// --------------------------------------------------------------------------
3026//
3027// In case of num<0 all tabs are written into the PS file. If num>0
3028// the canvas in the corresponding tab is written to the file.
3029// Name is the name of the file (with or without extension).
3030//
3031// Returns the number of keys written.
3032//
3033// To write all tabs you can also use SaveAsPS(name)
3034//
3035Int_t MStatusDisplay::SaveAsRoot(Int_t num, TString name)
3036{
3037 num = InitWriteDisplay(num, name, "root");
3038 if (num==0)
3039 return -1;
3040
3041 TFile *fsave = gFile;
3042 TFile file(name, "RECREATE", GetTitle(), 9);
3043 const Int_t keys = Write(num);
3044 gFile = fsave;
3045
3046 SetStatusLine2("Done.");
3047
3048 return keys;
3049}
3050
3051// --------------------------------------------------------------------------
3052//
3053Bool_t MStatusDisplay::SaveAsCSV(Int_t num, TString name, Char_t delim)
3054{
3055 num = InitWriteDisplay(num, name, "csv");
3056 if (num==0)
3057 return kFALSE;
3058
3059 gSystem->ExpandPathName(name);
3060
3061 ofstream fout(name);
3062 if (!fout)
3063 {
3064 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
3065 return kFALSE;
3066 }
3067
3068 fout << 0 << delim << GetName() << delim << GetTitle() << endl;
3069
3070 Int_t from, to;
3071 GetCanvasRange(from, to, num);
3072
3073 for (int i=from; i<to; i++)
3074 {
3075 TCanvas *c;
3076 if (!(c = GetCanvas(i)))
3077 {
3078 if (num<0)
3079 *fLog << inf << " - ";
3080 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
3081 continue;
3082 }
3083
3084 fout << i << delim << c->GetName() << delim << c->GetTitle() << endl;
3085 }
3086
3087 SetStatusLine2("Done.");
3088
3089 return kTRUE;
3090}
3091
3092/*
3093Bool_t MStatusDisplay::SaveAsCSV(Int_t num, TString name)
3094{
3095 num = InitWriteDisplay(num, name, "csv");
3096 if (num==0)
3097 return kFALSE;
3098
3099 gSystem->ExpandPathName(name);
3100
3101 ofstream fout(name);
3102 if (!fout)
3103 {
3104 *fLog << err << "Cannot open file " << name << ": " << strerror(errno) << endl;
3105 return kFALSE;
3106 }
3107
3108 fout << "<?xml version=\"1.0\"?>" << endl;
3109 fout << "<display name='" << GetName() << "'>" << endl;
3110 fout << " <file>" << name << "</file>" << endl;
3111 fout << " <status>" << endl;
3112 fout << " <name>" << GetName() << "</name>" << endl;
3113 fout << " <title>" << GetTitle() << "</title>" << endl;
3114 fout << " </status>" << endl;
3115 fout << " <tabs>" << endl;
3116
3117 fout << 0 << delim << GetName() << delim << GetTitle() << endl;
3118
3119 Int_t from, to;
3120 GetCanvasRange(from, to, num);
3121
3122 for (int i=from; i<to; i++)
3123 {
3124 TCanvas *c;
3125 if (!(c = GetCanvas(i)))
3126 {
3127 if (num<0)
3128 *fLog << inf << " - ";
3129 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
3130 continue;
3131 }
3132
3133 fout << " <tab index='" << i << "'>" << endl;
3134 fout << " <index>" << i << "</index>" << endl;
3135 fout << " <name>" << c->GetName() << "</name>" << endl;
3136 fout << " <title>" << c->GetName() << "</title>" << endl;
3137 fout << " </tab>" << endl;
3138 }
3139
3140 fout << " </tabs>" << endl;
3141 fout << "</display>" << endl;
3142
3143 SetStatusLine2("Done.");
3144
3145 return kTRUE;
3146}
3147*/
3148
3149// --------------------------------------------------------------------------
3150//
3151void MStatusDisplay::SaveAs(const char *c, const Option_t *o) const
3152{
3153#if ROOT_VERSION_CODE >= ROOT_VERSION(5,18,00)
3154 TGObject::SaveAs(c, o);
3155#endif
3156}
3157
3158// --------------------------------------------------------------------------
3159//
3160// Determin File type to save file as by extension. Allowed extensions are:
3161// root, ps, pdf, svg, gif, png, jpg, xpm, C
3162//
3163// returns -1 if file type is unknown. Otherwise return value of SaveAs*
3164//
3165Int_t MStatusDisplay::SaveAs(Int_t num, TString name)
3166{
3167 if (name.EndsWith(".root")) return SaveAsRoot(num, name); // kFileSaveAsRoot
3168 if (name.EndsWith(".ps")) return SaveAsPS(num, name); // kFileSaveAsPS
3169 if (name.EndsWith(".pdf")) return SaveAsPDF(num, name); // kFileSaveAsPDF
3170 if (name.EndsWith(".svg")) return SaveAsSVG(num, name); // kFileSaveAsSVG
3171 if (name.EndsWith(".gif")) return SaveAsGIF(num, name); // kFileSaveAsGIF
3172 if (name.EndsWith(".png")) return SaveAsPNG(num, name); // kFileSaveAsPNG
3173 if (name.EndsWith(".bmp")) return SaveAsBMP(num, name); // kFileSaveAsBMP
3174 if (name.EndsWith(".xml")) return SaveAsXML(num, name); // kFileSaveAsXML
3175 if (name.EndsWith(".jpg")) return SaveAsJPG(num, name); // kFileSaveAsJPG
3176 if (name.EndsWith(".xpm")) return SaveAsXPM(num, name); // kFileSaveAsXPM
3177 if (name.EndsWith(".csv")) return SaveAsCSV(num, name); // kFileSaveAsCSV
3178 if (name.EndsWith(".tiff")) return SaveAsTIFF(num, name); // kFileSaveAsTIFF
3179 if (name.EndsWith(".C")) return SaveAsC(num, name); // kFileSaveAsC
3180 return -1;
3181}
3182
3183// --------------------------------------------------------------------------
3184//
3185// Opens a save as dialog
3186//
3187Int_t MStatusDisplay::SaveAs(Int_t num)
3188{
3189 static const char *gSaveAsTypes[] =
3190 {
3191 "PostScript", "*.ps",
3192 "Acrobat pdf", "*.pdf",
3193 "SVG vector", "*.svg",
3194 "Gif files", "*.gif",
3195 "Png files", "*.png",
3196 "Gif files", "*.gif",
3197 "Jpeg files", "*.jpeg",
3198 "Xpm files", "*.xpm",
3199 "Bmp files", "*.bmp",
3200 "Xml files", "*.xml",
3201 "Tiff files", "*.tiff",
3202 "Csv files", "*.csv",
3203 "Macro files", "*.C",
3204 "ROOT files", "*.root",
3205 "All files", "*",
3206 NULL, NULL
3207 };
3208
3209 static TString dir(".");
3210
3211 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
3212
3213 fi.fFileTypes = (const char**)gSaveAsTypes;
3214 fi.fIniDir = StrDup(dir);
3215
3216 new TGFileDialog(fClient->GetRoot(), this, kFDSave, &fi);
3217
3218 if (!fi.fFilename)
3219 return 0;
3220
3221 dir = fi.fIniDir;
3222
3223 const Int_t rc = SaveAs(num, fi.fFilename);
3224 if (rc>=0)
3225 return rc;
3226
3227 Warning("MStatusDisplay::SaveAs", "Unknown Extension: %s", fi.fFilename);
3228 return 0;
3229}
3230
3231// --------------------------------------------------------------------------
3232//
3233// Open contents of a MStatusDisplay with key name from file fname.
3234//
3235Int_t MStatusDisplay::Open(TString fname, const char *name)
3236{
3237 TFile file(fname, "READ");
3238 if (file.IsZombie())
3239 {
3240 gLog << warn << "WARNING - Cannot open file " << fname << endl;
3241 return 0;
3242 }
3243
3244 return Read(name);
3245}
3246
3247// --------------------------------------------------------------------------
3248//
3249// Opens an open dialog
3250//
3251Int_t MStatusDisplay::Open()
3252{
3253 static const char *gOpenTypes[] =
3254 {
3255 "ROOT files", "*.root",
3256 "All files", "*",
3257 NULL, NULL
3258 };
3259
3260 static TString dir(".");
3261
3262 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
3263
3264 fi.fFileTypes = (const char**)gOpenTypes;
3265 fi.fIniDir = StrDup(dir);
3266
3267 new TGFileDialog(fClient->GetRoot(), this, kFDOpen, &fi);
3268
3269 if (!fi.fFilename)
3270 return 0;
3271
3272 dir = fi.fIniDir;
3273
3274 return Open(fi.fFilename);
3275}
3276
3277// --------------------------------------------------------------------------
3278//
3279// Change width of display. The height is calculated accordingly.
3280//
3281void MStatusDisplay::SetDisplayWidth(UInt_t dw)
3282{
3283 if (gROOT->IsBatch())
3284 {
3285 SetCanvasWidth(dw);
3286 return;
3287 }
3288
3289 // 4 == 2*default border with of canvas
3290 dw -= 4;
3291
3292 // Difference between canvas size and display size
3293 const UInt_t cw = GetWidth() -fTab->GetWidth();
3294 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3295
3296 const UInt_t dh = TMath::Nint((dw - cw)/1.5 + ch);
3297
3298 Resize(dw, dh); // Set display size
3299}
3300
3301// --------------------------------------------------------------------------
3302//
3303// Change height of display. The width is calculated accordingly.
3304//
3305void MStatusDisplay::SetDisplayHeight(UInt_t dh)
3306{
3307 if (gROOT->IsBatch())
3308 {
3309 SetCanvasHeight(dh);
3310 return;
3311 }
3312
3313 // 4 == 2*default border with of canvas
3314 dh -= 4;
3315
3316 // Difference between canvas size and display size
3317 const UInt_t cw = GetWidth() -fTab->GetWidth();
3318 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3319
3320 const UInt_t dw = TMath::Nint((dh - ch)*1.5 + cw);
3321
3322 Resize(dw, dh); // Set display size
3323}
3324
3325// --------------------------------------------------------------------------
3326//
3327// Change width of canvas. The height is calculated accordingly.
3328//
3329void MStatusDisplay::SetCanvasWidth(UInt_t w)
3330{
3331 // 4 == 2*default border with of canvas
3332 w += 4;
3333
3334 if (gROOT->IsBatch())
3335 {
3336 Resize(w, 3*w/2);
3337 return;
3338 }
3339
3340 // Difference between canvas size and display size
3341 const UInt_t cw = GetWidth() -fTab->GetWidth();
3342 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3343
3344 const UInt_t h = TMath::Nint(w/1.5 + ch);
3345
3346 Resize(w + cw, h); // Set display size
3347}
3348
3349// --------------------------------------------------------------------------
3350//
3351// Change height of canvas. The width is calculated accordingly.
3352//
3353void MStatusDisplay::SetCanvasHeight(UInt_t h)
3354{
3355 // 4 == 2*default border with of canvas
3356 h += 4;
3357
3358 if (gROOT->IsBatch())
3359 {
3360 Resize(2*h/3, h);
3361 return;
3362 }
3363
3364 // Difference between canvas size and display size
3365 const UInt_t cw = GetWidth() -fTab->GetWidth();
3366 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3367
3368 // 4 == 2*default border with of canvas
3369 const UInt_t dw = TMath::Nint((h+4)*1.5 + cw);
3370
3371 Resize(dw, h + ch); // Set display size
3372}
3373
3374// --------------------------------------------------------------------------
3375//
3376// Calculate width and height of the display such that it fits into the
3377// defined box.
3378//
3379void MStatusDisplay::SetDisplaySize(UInt_t w, UInt_t h)
3380{
3381 if (gROOT->IsBatch())
3382 return;
3383
3384 SetDisplayHeight(h);
3385
3386 if (GetWidth()>w)
3387 SetDisplayWidth(w);
3388}
3389
3390// --------------------------------------------------------------------------
3391//
3392// Calculate an optimum size for the display from the desktop size
3393//
3394void MStatusDisplay::SetOptimumSize()
3395{
3396 if (gROOT->IsBatch())
3397 return;
3398
3399 const UInt_t w = TMath::Nint(0.95*gClient->GetDisplayWidth());
3400 const UInt_t h = TMath::Nint(0.95*gClient->GetDisplayHeight());
3401
3402 SetDisplaySize(w, h);
3403}
3404
3405
3406Bool_t MStatusDisplay::HandleConfigureNotify(Event_t *evt)
3407{
3408 //
3409 // The initialization of the GUI is not yet enough finished...
3410 //
3411 if (!fTab)
3412 return kTRUE;
3413
3414 UInt_t w = evt->fWidth;
3415 UInt_t h = evt->fHeight;
3416
3417 const Bool_t wchanged = w!=GetWidth()-fTab->GetWidth();
3418 const Bool_t hchanged = h!=GetHeight()-fTab->GetHeight();
3419
3420 if (!wchanged && !hchanged)
3421 {
3422 Layout();
3423 // FIXME: Make sure that this doesn't result in endless loops.
3424 return kTRUE;
3425 }
3426
3427 if (GetWidth()==1 && GetHeight()==1)
3428 return kTRUE;
3429
3430 // calculate the constant part of the window
3431 const UInt_t cw = GetWidth() -fTab->GetWidth();
3432 const UInt_t ch = GetHeight()-fTab->GetHeight()+fTab->GetTabHeight();
3433
3434 // calculate new size of frame (canvas @ 2:3)
3435 if (hchanged)
3436 w = TMath::Nint((h-ch)*1.5+cw);
3437 else
3438 h = TMath::Nint((w-cw)/1.5+ch);
3439
3440 // resize frame
3441 Resize(w, h);
3442
3443 return kTRUE;
3444}
3445
3446Bool_t MStatusDisplay::HandleEvent(Event_t *event)
3447{
3448 // Instead of doing this in CloseWindow (called from HandleEvent)
3449 // we do it here. This makes sure, that handle event doesn't
3450 // execute code after deleting this.
3451 if (event->fType==kDestroyNotify)
3452 {
3453 if (Close())
3454 delete this;
3455// Close();
3456 return kTRUE;
3457 }
3458
3459 const Bool_t rc = TGMainFrame::HandleEvent(event);
3460
3461 //
3462 // This fixes a bug in older root versions which makes
3463 // TCanvas crash if gPad==NULL. So we make sure, that
3464 // gPad!=NULL -- be carfull, this may have other side
3465 // effects.
3466 //
3467#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,01)
3468 if (!gPad && fTab)
3469 for (int i=0; i<fTab->GetNumberOfTabs(); i++)
3470 {
3471 TCanvas *c = GetCanvas(i);
3472 if (c)
3473 {
3474 c->cd();
3475 gLog << dbg << "MStatusDisplay::HandleEvent - Workaround: gPad=" << gPad << "." << endl;
3476 break;
3477 }
3478 }
3479#endif
3480
3481 return rc;
3482}
Note: See TracBrowser for help on using the repository browser.