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

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