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

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