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

Last change on this file since 6000 was 5974, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 68.4 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
1613 //copy primitives
1614 TObject *obj;
1615 TIter next(oldc.GetListOfPrimitives());
1616 while ((obj=next()))
1617 {
1618 // Old line - I think it is not necessary anymore because of the cd()
1619 //gROOT->SetSelectedPad(&newc);
1620
1621 // Now make a clone of the object
1622 TObject *clone = obj->Clone();
1623
1624 // Clone also important bits (FIXME: Is this correct)
1625 clone->SetBit(obj->TestBits(kCannotPick|kNoContextMenu));
1626
1627 // Now make sure that the clones are deleted at a later time
1628 clone->SetBit(kCanDelete|kMustCleanup);
1629
1630 // FIXME: This is a workaround for the problem with the MAstroCatalog in
1631 // MHFalseSource. It doesn't harm. We'll still try to find the reason
1632 if (clone->IsA()==TPad::Class())
1633 gROOT->GetListOfCleanups()->Add(clone);
1634
1635 // Add the clone and its draw-option to the current pad
1636 TVirtualPad *save2 = gPad;
1637 gPad = &oldc; // Don't do this before Clone()!
1638 newc.GetListOfPrimitives()->Add(clone, obj->GetDrawOption());
1639 gPad = save2;
1640 }
1641 newc.Modified();
1642 newc.Update();
1643
1644 padsav->cd();
1645}
1646
1647Bool_t MStatusDisplay::Display(const TObjArray &list)
1648{
1649 TIter Next(&list);
1650
1651 TObject *o=Next();
1652 if (!o)
1653 {
1654 *fLog << err << "MStatusDisplay::Display: No entry in TObjArray." << endl;
1655 return kFALSE;
1656 }
1657
1658 fTitle = o->GetTitle();
1659
1660 TCanvas *c;
1661 while ((c=(TCanvas*)Next()))
1662 if (!GetCanvas(c->GetName()))
1663 DrawClonePad(AddTab(c->GetName()), *c);
1664
1665 return kTRUE;
1666}
1667
1668// --------------------------------------------------------------------------
1669//
1670// Reads the contents of a saved MStatusDisplay from a file.
1671//
1672Int_t MStatusDisplay::Read(const char *name)
1673{
1674 if (!gFile)
1675 {
1676 *fLog << warn << "MStatusDisplay::Read: No file found. Please create a TFile first." << endl;
1677 return 0;
1678 }
1679
1680 if (!gFile->IsOpen())
1681 {
1682 *fLog << warn << "MStatusDisplay::Read: File not open. Please open the TFile first." << endl;
1683 return 0;
1684 }
1685
1686 MStatusArray list;
1687
1688 const Int_t n = list.Read(name);
1689 if (n==0)
1690 {
1691 *fLog << warn << "MStatusDisplay::Read: No objects read." << endl;
1692 return 0;
1693 }
1694
1695 if (!Display(list))
1696 {
1697 *fLog << err << "MStatusDisplay::Display: No entry in " << name << "." << endl;
1698 return 0;
1699 }
1700
1701 *fLog << inf << "MStatusDisplay: Key " << name << " with " << n << " keys read from file." << endl;
1702
1703 return n;
1704}
1705
1706// --------------------------------------------------------------------------
1707//
1708// Writes the contents of a MStatusDisplay to a file.
1709//
1710Int_t MStatusDisplay::Write(Int_t num, const char *name, Int_t option, Int_t bufsize) const
1711{
1712 if (!gFile)
1713 {
1714 *fLog << warn << "MStatusDisplay::Write: No file found. Please create a TFile first." << endl;
1715 return 0;
1716 }
1717
1718 if (!gFile->IsOpen())
1719 {
1720 *fLog << warn << "MStatusDisplay::Write: File not open. Please open the TFile first." << endl;
1721 return 0;
1722 }
1723
1724 if (!gFile->IsWritable())
1725 {
1726 *fLog << warn << "MStatusDisplay::Write: File not writable." << endl;
1727 return 0;
1728 }
1729
1730 if (num==0)
1731 {
1732 *fLog << warn << "MStatusDisplay::Write: Tab doesn't contain an embedded Canvas... skipped." << endl;
1733 return 0;
1734 }
1735
1736 if (!gROOT->IsBatch() && num>=fTab->GetNumberOfTabs())
1737 {
1738 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
1739 return 0;
1740 }
1741 if (gROOT->IsBatch() && num>fBatch->GetSize())
1742 {
1743 *fLog << warn << "MStatusDisplay::Write: Tab doesn't exist... skipped." << endl;
1744 return 0;
1745 }
1746
1747 MStatusArray list;
1748
1749 TNamed named;
1750 named.SetTitle(fTitle);
1751 list.Add(&named);
1752
1753 const Int_t max = gROOT->IsBatch() ? fBatch->GetSize()+1 : fTab->GetNumberOfTabs();
1754 const Int_t from = num<0 ? 1 : num;
1755 const Int_t to = num<0 ? max : num+1;
1756
1757 TCanvas *c;
1758 for (int i=from; i<to; i++)
1759 if ((c = GetCanvas(i)))
1760 list.Add(c);
1761
1762 const Int_t n = list.Write(name, kSingleKey);
1763
1764 //*fLog << inf << "MStatusDisplay: " << n << " keys written to file as key " << name << "." << endl;
1765
1766 return n;
1767}
1768
1769// --------------------------------------------------------------------------
1770//
1771// Use this to start the synchronous (GUI eventloop driven) tab update.
1772// Can also be used to change the update intervall. If millisec<0
1773// the intervall given in SetUpdateTime is used. If the intervall in
1774// SetUpdateTime is <0 nothing is done. (Call SetUpdateTime(-1) to
1775// disable the automatic update in a MEventloop.
1776//
1777void MStatusDisplay::StartUpdate(Int_t millisec)
1778{
1779 if (fIsLocked>1)
1780 return;
1781
1782 if (fTimer.GetTime()<TTime(0))
1783 return;
1784 fTimer.Start(millisec);
1785}
1786
1787// --------------------------------------------------------------------------
1788//
1789// Stops the automatic GUI update
1790//
1791void MStatusDisplay::StopUpdate()
1792{
1793 if (fIsLocked>1)
1794 return;
1795
1796 fTimer.Stop();
1797}
1798
1799// --------------------------------------------------------------------------
1800//
1801// Set the update interval for the GUI update, see StartUpdate.
1802//
1803void MStatusDisplay::SetUpdateTime(Long_t t)
1804{
1805 fTimer.SetTime(t);
1806}
1807
1808// --------------------------------------------------------------------------
1809//
1810// Set the background color in a canvas
1811//
1812void MStatusDisplay::CanvasSetFillColor(TPad &p, Int_t col) const
1813{
1814 TObject *obj;
1815
1816 // See also TPad::UseCurrentStyle
1817 TIter Next(p.GetListOfPrimitives());
1818 while ((obj=Next()))
1819 {
1820 if (obj->InheritsFrom(TPad::Class()))
1821 CanvasSetFillColor(*(TPad*)obj, col);
1822 if (obj->InheritsFrom(TFrame::Class()))
1823 ((TFrame*)obj)->SetFillColor(col);
1824 }
1825
1826 p.SetFillColor(col);
1827}
1828
1829void MStatusDisplay::AddExtension(TString &name, const TString &ext, Int_t num) const
1830{
1831 if (name.IsNull())
1832 {
1833 name = "status";
1834 if (num>0)
1835 {
1836 name += "-";
1837 name += num;
1838 }
1839 }
1840
1841 if (name.EndsWith("."+ext))
1842 return;
1843
1844 name += ".";
1845 name += ext;
1846}
1847
1848Bool_t MStatusDisplay::CheckTabForCanvas(int num) const
1849{
1850 if (gROOT->IsBatch())
1851 return num>0 && num<=fBatch->GetSize() || num<0;
1852
1853 if (num>=fTab->GetNumberOfTabs())
1854 {
1855 *fLog << warn << "Tab #" << num << " doesn't exist..." << endl;
1856 return kFALSE;
1857 }
1858 if (num==0)
1859 {
1860 *fLog << warn << "Tab #" << num << " doesn't contain an embedded canvas..." << endl;
1861 return kFALSE;
1862 }
1863 if (fTab->GetNumberOfTabs()<2 || !gPad)
1864 {
1865 *fLog << warn << "Sorry, you must have at least one existing canvas (gPad!=NULL)" << endl;
1866 return kFALSE;
1867 }
1868 return kTRUE;
1869}
1870
1871// --------------------------------------------------------------------------
1872//
1873// Insert the following two lines into the postscript header:
1874//
1875// %%DocumentPaperSizes: a4
1876// %%Orientation: Landscape
1877//
1878void MStatusDisplay::UpdatePSHeader(const TString &name) const
1879{
1880 const TString newstr("%%DocumentPaperSizes: a4\n%%Orientation: Landscape\n");
1881
1882 ifstream fin(name);
1883 ofstream fout(name+".$$$");
1884
1885 char c;
1886
1887 TString str;
1888 fin >> str >> c; // Read "%!PS-Adobe-2.0\n"
1889 fout << str << endl << newstr;
1890
1891 // Doing it in blocks seems not to gain much for small (MB) files
1892 while (fin)
1893 {
1894 fin.read(&c, 1);
1895 fout.write(&c, 1);
1896 }
1897
1898 gSystem->Unlink(name);
1899 gSystem->Rename(name+".$$$", name);
1900/*
1901 //
1902 // Old style algorithm. Shifts blocks inside a single file --- SLOW!
1903 //
1904 const Int_t l = newstr.Length();
1905
1906 Long_t t[4]; // { id, size, flags, modtime }
1907 gSystem->GetPathInfo(name, t, t+1, t+2, t+3);
1908
1909 char *c[2] = { new char[l], new char[l] };
1910
1911 fstream f(name, ios::in|ios::out);
1912
1913 TString str;
1914 f >> str >> c[0][0]; // Read "%!PS-Adobe-2.0\n" (Mini Header)
1915 f.read(c[0], l);
1916 f.seekp(-l, ios::cur);
1917 f.write(newstr, l);
1918
1919 int i=0;
1920 while (1)
1921 {
1922 f.read(c[(i+1)%2], l);
1923 f.seekp(-l, ios::cur);
1924
1925 if (f)
1926 {
1927 f.write(c[i%2],l);
1928 i++;
1929 i%=2;
1930 continue;
1931 }
1932
1933 const Int_t ssz = str.Length()+1; // Length of Mini-Header
1934 const Int_t block = t[1]-ssz; // Length of block to be shifted
1935 const Int_t size = block%l; // Reminder
1936 const Int_t pos = (block/l)*l + ssz + 1; // Position to start writing
1937
1938 f.clear();
1939 f.seekp(pos);
1940 f.write(c[i%2], l);
1941 f.write(c[(i+1)%2], size);
1942 break;
1943 }
1944
1945 delete c[1];
1946 delete c[0];
1947*/
1948}
1949
1950// --------------------------------------------------------------------------
1951//
1952// In case of num<0 all tabs are written into the PS file. If num>0
1953// the canvas in the corresponding tab is written to the file.
1954// Name is the name of the file (with or without extension).
1955//
1956// Returns the number of pages written.
1957//
1958// To write all tabs you can also use SaveAsPS(name)
1959//
1960// If the third argument is given a bottom line is drawn with the text
1961// under it. If no argument is given a bottom line is drawn if
1962// fTitle (SetTitle) is not empty.
1963//
1964Int_t MStatusDisplay::SaveAsPS(Int_t num, TString name, const TString addon)
1965{
1966 SetStatusLine1("Writing Postscript file...");
1967 SetStatusLine2("");
1968
1969 if (!CheckTabForCanvas(num))
1970 {
1971 SetStatusLine2("Failed!");
1972 return 0;
1973 }
1974
1975 AddExtension(name, "ps", num);
1976
1977 if (num<0)
1978 *fLog << inf << "Open ps-File: " << name << endl;
1979
1980 TPad *padsav = (TPad*)gPad;
1981 TVirtualPS *psave = gVirtualPS;
1982
1983 TDatime d;
1984
1985 TPostScript ps(name, 112);
1986 ps.SetBit(TPad::kPrintingPS);
1987 ps.PrintFast(13, "/nan {1} def ");
1988
1989 gVirtualPS = &ps;
1990
1991 //
1992 // Create a list to delete the canvas clones
1993 //
1994 TList l;
1995 l.SetOwner();
1996
1997 //
1998 // Create some GUI elements for a page legend
1999 //
2000 TLine line;
2001
2002 int page = 1;
2003
2004 //
2005 // Maintain tab numbers
2006 //
2007 const Int_t max = gROOT->IsBatch() ? fBatch->GetSize()+1 : fTab->GetNumberOfTabs();
2008 const Int_t from = num<0 ? 1 : num;
2009 const Int_t to = num<0 ? max : num+1;
2010
2011 for (int i=from; i<to; i++)
2012 {
2013 TCanvas *c;
2014 if (!(c = GetCanvas(i)))
2015 {
2016 if (num<0)
2017 *fLog << inf << " - ";
2018 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
2019 continue;
2020 }
2021
2022 SetStatusLine2(MString::Form("Tab #%d", i));
2023
2024 //
2025 // Init page and page size, make sure, that the canvas in the file
2026 // has the same Aspect Ratio than on the screen.
2027 //
2028 ps.NewPage();
2029
2030 //
2031 // 28 is used here to scale the canvas into a height of 28,
2032 // such that the page title can be set above the canvas...
2033 //
2034 Float_t psw = 28.0; // A4 - width (29.7)
2035 Float_t psh = 21.0; // A4 - height (21.0)
2036
2037 const Float_t cw = c->GetWw();
2038 const Float_t ch = c->GetWh();
2039
2040 if (psw/psh>cw/ch)
2041 psw = cw/ch*psh;
2042 else
2043 psh = ch/cw*psw;
2044
2045 ps.Range(psw, psh); // A4
2046
2047 //
2048 // Clone canvas and change background color and schedule for
2049 // deletion
2050 //
2051 TCanvas *n = (TCanvas*)c->Clone();
2052 CanvasSetFillColor(*n, kWhite);
2053 l.Add(n);
2054
2055 //
2056 // Paint canvas into root file
2057 //
2058 if (num<0)
2059 *fLog << inf << " - ";
2060 *fLog << inf << "Writing Tab #" << i << ": " << c->GetName() << " (" << c << ") ";
2061 if (num>0)
2062 *fLog << "to " << name;
2063 *fLog << "... " << flush;
2064
2065 n->SetBatch(kTRUE);
2066 n->Paint();
2067
2068 //
2069 // Use the canvas as coordinate system for the overlaying text
2070 //
2071 gPad = n;
2072 //n->cd();
2073
2074 //
2075 // Print overlaying text (NDC = %)
2076 //
2077 ps.SetTextColor(kBlack);
2078 ps.SetTextSize(0.015);
2079 ps.SetTextFont(22);
2080 ps.SetTextAlign(11); // left top
2081 ps.TextNDC(0, 1.015, TString(" ")+n->GetName());
2082 ps.SetTextAlign(21); // cent top
2083 ps.TextNDC(0.5, 1.015, TString("MARS - Magic Analysis and Reconstruction Software - ")+d.AsString());
2084 ps.SetTextAlign(31); // right top
2085 ps.TextNDC(1, 1.015, MString::Form("Page No.%i (%i) ", page++, i));
2086 line.PaintLineNDC(0, 1.01, 1, 1.01);
2087
2088 TString txt(addon.IsNull() ? fTitle : addon);
2089 if (!txt.IsNull())
2090 {
2091 line.PaintLineNDC(0, -0.00, 1, -0.00);
2092 ps.SetTextAlign(11); // left top
2093 ps.TextNDC(0, -0.015, TString(" ")+txt);
2094 ps.SetTextAlign(31); // right top
2095 ps.TextNDC(1, -0.015, "(c) 2000-2004, Thomas Bretz ");
2096 }
2097
2098 //
2099 // Finish drawing page
2100 //
2101 n->SetBatch(kFALSE);
2102 *fLog << "done." << endl;
2103 }
2104
2105 gPad = NULL; // Important!
2106 l.Delete();
2107
2108 ps.Close();
2109
2110 SetStatusLine2("Updating header of PS file...");
2111
2112 if (num<0)
2113 *fLog << " - Updating header of PS file... " << flush;
2114 UpdatePSHeader(name);
2115 if (num<0)
2116 *fLog << inf << "done." << endl;
2117
2118 gVirtualPS = psave;
2119 if (padsav)
2120 padsav->cd();
2121
2122 if (num<0)
2123 *fLog << inf << "done." << endl;
2124
2125 SetStatusLine2(MString::Form("Done (%dpages)", page-1));
2126
2127 return page-1;
2128}
2129
2130Bool_t MStatusDisplay::SaveAsGIF(Int_t num, TString name)
2131{
2132 if (gROOT->IsBatch())
2133 {
2134 *fLog << warn << "Sorry, writing gif-files is not available in batch mode." << endl;
2135 return 0;
2136 }
2137 SetStatusLine1("Writing GIF file...");
2138 SetStatusLine2("");
2139
2140 if (!CheckTabForCanvas(num))
2141 {
2142 SetStatusLine2("Failed!");
2143 return 0;
2144 }
2145
2146 AddExtension(name, "gif", num);
2147
2148 if (num<0)
2149 *fLog << inf << "Writing gif-Files..." << endl;
2150
2151 TPad *padsav = (TPad*)gPad;
2152
2153 int page = 1;
2154
2155 //
2156 // Maintain tab numbers
2157 //
2158 const Int_t from = num<0 ? 1 : num;
2159 const Int_t to = num<0 ? fTab->GetNumberOfTabs() : num+1;
2160
2161 for (int i=from; i<to; i++)
2162 {
2163 TCanvas *c;
2164 if (!(c = GetCanvas(i)))
2165 {
2166 if (num<0)
2167 *fLog << inf << " - ";
2168 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
2169 continue;
2170 }
2171
2172 SetStatusLine2(MString::Form("Tab #%d", i));
2173
2174 //
2175 // Clone canvas and change background color and schedule for
2176 // deletion
2177 //
2178 //TCanvas *n = (TCanvas*)c->Clone();
2179 //CanvasSetFillColor(*n, kWhite);
2180
2181 //
2182 // Paint canvas into root file
2183 //
2184 TString writename = name;
2185 if (num<0)
2186 {
2187 TString numname = "-";
2188 numname += i;
2189 writename.Insert(name.Last('.'), numname);
2190 }
2191 if (num<0)
2192 *fLog << inf << " - ";
2193 *fLog << inf << "Writing Tab #" << i << " to " << writename << ": " << c->GetName() << " (" << c << ") ";
2194 if (num>0)
2195 *fLog << "to " << name;
2196 *fLog << "..." << flush;
2197
2198 c->Draw();
2199 c->SaveAs(writename);
2200 /*
2201 n->Draw();
2202 n->SaveAs(writename);
2203 delete n;
2204 */
2205
2206 if (num<0)
2207 *fLog << "done." << endl;
2208 }
2209
2210 padsav->cd();
2211
2212 *fLog << inf << "done." << endl;
2213
2214 SetStatusLine2("Done.");
2215
2216 return page-1;
2217}
2218
2219Bool_t MStatusDisplay::SaveAsC(Int_t num, TString name)
2220{
2221 SetStatusLine1("Writing C++ file...");
2222 SetStatusLine2("");
2223
2224 if (!CheckTabForCanvas(num))
2225 {
2226 SetStatusLine2("Failed!");
2227 return 0;
2228 }
2229
2230 AddExtension(name, "C", num);
2231
2232 if (num<0)
2233 *fLog << inf << "Writing C-Files..." << endl;
2234
2235 TPad *padsav = (TPad*)gPad;
2236
2237 int page = 1;
2238
2239 //
2240 // Maintain tab numbers
2241 //
2242 const Int_t from = num<0 ? 1 : num;
2243 const Int_t to = num<0 ? fTab->GetNumberOfTabs() : num+1;
2244
2245 for (int i=from; i<to; i++)
2246 {
2247 TCanvas *c;
2248 if (!(c = GetCanvas(i)))
2249 {
2250 if (num<0)
2251 *fLog << inf << " - ";
2252 *fLog << "Tab #" << i << " doesn't contain an embedded Canvas... skipped." << endl;
2253 continue;
2254 }
2255
2256 SetStatusLine2(MString::Form("Tab #%d", i));
2257
2258 //
2259 // Clone canvas and change background color and schedule for
2260 // deletion
2261 //
2262 TCanvas *n = (TCanvas*)c->Clone();
2263 CanvasSetFillColor(*n, kWhite);
2264
2265 //
2266 // Paint canvas into root file
2267 //
2268 TString writename = name;
2269 if (num<0)
2270 {
2271 TString numname = "-";
2272 numname += i;
2273 writename.Insert(name.Last('.'), numname);
2274 }
2275 if (num<0)
2276 *fLog << inf << " - ";
2277 *fLog << inf << "Writing Tab #" << i << " to " << writename << ": " << c->GetName() << " (" << n << ") ";
2278 if (num>0)
2279 *fLog << "to " << name;
2280 *fLog << "..." << flush;
2281
2282 n->SaveSource(writename, "");
2283 delete n;
2284
2285 if (num<0)
2286 *fLog << "done." << endl;
2287 }
2288
2289 padsav->cd();
2290
2291 *fLog << inf << "done." << endl;
2292
2293 SetStatusLine2("Done.");
2294
2295 return page-1;
2296}
2297
2298// --------------------------------------------------------------------------
2299//
2300// In case of num<0 all tabs are written into the PS file. If num>0
2301// the canvas in the corresponding tab is written to the file.
2302// Name is the name of the file (with or without extension).
2303//
2304// Returns the number of keys written.
2305//
2306// To write all tabs you can also use SaveAsPS(name)
2307//
2308Int_t MStatusDisplay::SaveAsRoot(Int_t num, TString name)
2309{
2310 SetStatusLine1("Writing root file...");
2311 SetStatusLine2("");
2312
2313 if (!CheckTabForCanvas(num))
2314 {
2315 SetStatusLine2("Failed!");
2316 return 0;
2317 }
2318
2319 AddExtension(name, "root", num);
2320
2321 TFile *fsave = gFile;
2322 TFile file(name, "RECREATE", "MARS - Status Window Contents", 9);
2323 const Int_t keys = Write(num);
2324 gFile = fsave;
2325
2326 SetStatusLine2("Done.");
2327
2328 return keys;
2329}
2330
2331// --------------------------------------------------------------------------
2332//
2333// Opens a save as dialog
2334//
2335Int_t MStatusDisplay::SaveAs(Int_t num)
2336{
2337 static const char *gSaveAsTypes[] =
2338 {
2339 "PostScript", "*.ps",
2340 "Gif files", "*.gif",
2341 "Macro files", "*.C",
2342 "ROOT files", "*.root",
2343 "All files", "*",
2344 NULL, NULL
2345 };
2346
2347 static TString dir(".");
2348
2349 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
2350
2351 fi.fFileTypes = (const char**)gSaveAsTypes;
2352 fi.fIniDir = StrDup(dir);
2353
2354 new TGFileDialog(fClient->GetRoot(), this, kFDSave, &fi);
2355
2356 if (!fi.fFilename)
2357 return 0;
2358
2359 dir = fi.fIniDir;
2360
2361 const TString name(fi.fFilename);
2362
2363 if (name.EndsWith(".root")) return SaveAsRoot(num, name);
2364 if (name.EndsWith(".ps")) return SaveAsPS(num, name);
2365 if (name.EndsWith(".gif")) return SaveAsGIF(num, name);
2366 if (name.EndsWith(".C")) return SaveAsC(num, name);
2367
2368 Warning("MStatusDisplay::SaveAs", "Unknown Extension: %s", fi.fFilename);
2369 return 0;
2370}
2371
2372// --------------------------------------------------------------------------
2373//
2374// Open contents of a MStatusDisplay with key name from file fname.
2375//
2376Int_t MStatusDisplay::Open(TString fname, const char *name)
2377{
2378 TFile file(fname, "READ");
2379 if (file.IsZombie())
2380 {
2381 gLog << warn << "WARNING - Cannot open file " << fname << endl;
2382 return 0;
2383 }
2384
2385 return Read(name);
2386}
2387
2388// --------------------------------------------------------------------------
2389//
2390// Opens an open dialog
2391//
2392Int_t MStatusDisplay::Open()
2393{
2394 static const char *gOpenTypes[] =
2395 {
2396 "ROOT files", "*.root",
2397 "All files", "*",
2398 NULL, NULL
2399 };
2400
2401 static TString dir(".");
2402
2403 TGFileInfo fi; // fFileName and fIniDir deleted in ~TGFileInfo
2404
2405 fi.fFileTypes = (const char**)gOpenTypes;
2406 fi.fIniDir = StrDup(dir);
2407
2408 new TGFileDialog(fClient->GetRoot(), this, kFDSave, &fi);
2409
2410 if (!fi.fFilename)
2411 return 0;
2412
2413 dir = fi.fIniDir;
2414
2415 return Open(fi.fFilename);
2416}
2417
2418Bool_t MStatusDisplay::HandleConfigureNotify(Event_t *evt)
2419{
2420 //
2421 // The initialization of the GUI is not yet enough finished...
2422 //
2423 if (!fTab)
2424 return kTRUE;
2425
2426 UInt_t w = evt->fWidth;
2427 UInt_t h = evt->fHeight;
2428
2429 /*
2430 cout << "Old: " << GetWidth() << " " << GetHeight() << " " << GetBorderWidth() << endl;
2431 cout << "New: " << w << " " << h << " ";
2432 cout << "New: " << GetDefaultWidth() << " " << GetDefaultHeight() << " " << endl;
2433 */
2434
2435 Bool_t wchanged = w!=GetWidth();
2436 Bool_t hchanged = h!=GetHeight();
2437
2438 if (!wchanged && !hchanged)
2439 {
2440 Layout();
2441 // FIXME: Make sure that this doesn't result in endless loops.
2442 return kTRUE;
2443 }
2444
2445
2446 if (GetWidth()==1 && GetHeight()==1)
2447 return kTRUE;
2448
2449 // calculate the constant part of the window
2450 const UInt_t cw = GetWidth() -fTab->GetWidth();
2451 const UInt_t ch = GetHeight()-fTab->GetHeight();
2452
2453 // calculate new size of frame (canvas @ 1:sqrt(2))
2454 if (hchanged)
2455 w = (UInt_t)((h-ch)*sqrt(2.)+.5)+cw;
2456 else
2457 h = (UInt_t)((w-cw)/sqrt(2.)+.5)+ch;
2458
2459 // resize frame
2460 Resize(w, h);
2461
2462 return kTRUE;
2463}
2464
2465Bool_t MStatusDisplay::HandleEvent(Event_t *event)
2466{
2467 // Instead of doing this in CloseWindow (called from HandleEvent)
2468 // we do it here. This makes sure, that handle event doesn't
2469 // execute code after deleting this.
2470 if (event->fType==kDestroyNotify)
2471 {
2472 if (Close())
2473 delete this;
2474// Close();
2475 return kTRUE;
2476 }
2477
2478 const Bool_t rc = TGMainFrame::HandleEvent(event);
2479
2480 //
2481 // This fixes a bug in older root versions which makes
2482 // TCanvas crash if gPad==NULL. So we make sure, that
2483 // gPad!=NULL -- be carfull, this may have other side
2484 // effects.
2485 //
2486#if ROOT_VERSION_CODE < ROOT_VERSION(3,10,01)
2487 if (!gPad && fTab)
2488 for (int i=0; i<fTab->GetNumberOfTabs(); i++)
2489 {
2490 TCanvas *c = GetCanvas(i);
2491 if (c)
2492 {
2493 c->cd();
2494 gLog << dbg << "MStatusDisplay::HandleEvent - Workaround: gPad=" << gPad << "." << endl;
2495 break;
2496 }
2497 }
2498#endif
2499
2500 return rc;
2501}
Note: See TracBrowser for help on using the repository browser.