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

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