source: trunk/Mars/mmain/MBrowser.cc@ 19813

Last change on this file since 19813 was 19302, checked in by tbretz, 6 years ago
Fixed a compiler warning in a root header - nothing we can do about it.
File size: 16.1 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@uni-sw.gwdg.de>
19! Author(s): Harald Kornmayer 1/2001
20!
21! Copyright: MAGIC Software Development, 2000-2001
22!
23!
24\* ======================================================================== */
25
26#include "MBrowser.h"
27
28#include <iostream>
29#include <sys/stat.h> // S_ISDIR
30
31#include <TSystem.h> // gSystem
32
33#include <TGTab.h> // TGTab
34#include <TGMenu.h> // TGPopupMenu
35#include <TG3DLine.h> // TGHorizontal3DLine
36#include <TGButton.h> // TGTextButton
37#include <TGMsgBox.h> // TGMsgBox
38#include <TBrowser.h> // TBrowser
39#include <TGTextEntry.h> // TGTextEntry
40#include <TGFSComboBox.h> // TGFSComboBox, TGFSLboxEntry
41#include <TGProgressBar.h> // TGHProgressBar
42
43#pragma GCC diagnostic push
44#pragma GCC diagnostic ignored "-Wcast-align"
45#include <TGFSContainer.h> // TGFileContainer
46#pragma GCC diagnostic pop
47
48#include "MGList.h"
49
50ClassImp(MBrowser);
51
52using namespace std;
53
54enum {
55 kFileTBrowser,
56 kFileClose,
57 kButDirUp,
58 kButListMode,
59 kButDetailMode,
60 kCBDirectory,
61 kCBFilter,
62 kTEFileName
63};
64
65void MBrowser::CreateMenuBar()
66{
67 //
68 // crate the menu bar
69 //
70 TGPopupMenu *filemenu = new TGPopupMenu(gClient->GetRoot());
71 filemenu->AddEntry("Open &TBrowser", kFileTBrowser);
72 filemenu->AddSeparator();
73 filemenu->AddEntry("&Close", kFileClose);
74 filemenu->Associate(this);
75 fList->Add(filemenu);
76
77 //
78 // the button messages are handled by main frame (this)
79 //
80 TGLayoutHints *laymenubar = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandX, 2, 2, 2, 2);
81 TGLayoutHints *laymenuitem = new TGLayoutHints(kLHintsTop|kLHintsLeft, 0, 4, 0, 0);
82
83 fList->Add(laymenubar);
84 fList->Add(laymenuitem);
85
86 TGMenuBar *menubar = new TGMenuBar(this, 1, 1, kHorizontalFrame);
87 menubar->AddPopup("&File", filemenu, laymenuitem);
88 AddFrame(menubar, laymenubar);
89 fList->Add(menubar);
90}
91
92void MBrowser::CreateUpperFrame(TGCompositeFrame *frametop)
93{
94 TGLayoutHints *lay1 = new TGLayoutHints(kLHintsTop |kLHintsExpandX, 2, 2, 2, 0);
95 TGLayoutHints *lay2 = new TGLayoutHints(kLHintsCenterY|kLHintsExpandX, 2, 2, 2, 2);
96 TGLayoutHints *lay3 = new TGLayoutHints(kLHintsBottom |kLHintsExpandX, 2, 2, 0, 2);
97 fList->Add(lay1);
98 fList->Add(lay2);
99 fList->Add(lay3);
100
101 //
102 // *********** Create Contents of frame top (upper part) **********
103 //
104 fTop1 = new TGHorizontalFrame(frametop, 500, 50);
105 fTop2 = new TGHorizontalFrame(frametop, 500, 50);
106 fTop3 = new TGHorizontalFrame(frametop, 500, 50);
107
108 // FIXME: If I use TGLayoutHints the Progress Bar doesn't disappear!
109 frametop->AddFrame(fTop1);//, lay1);
110 frametop->AddFrame(fTop2);//, lay2);
111 frametop->AddFrame(fTop3);//, lay3);
112
113 fList->Add(fTop1);
114 fList->Add(fTop2);
115 fList->Add(fTop3);
116}
117
118void MBrowser::CreateDirListMenu(TGCompositeFrame *frame)
119{
120 //
121 // Layout Dir-Listbox and buttons in one row (frame)
122 //
123 // - layout:
124 // alignment: top, left
125 // padding: 5, 5, 5, 5
126 //
127 TGLayoutHints *laydir = new TGLayoutHints(kLHintsExpandX|kLHintsLeft|kLHintsCenterY); //, 5, 5, 5, 5);
128 TGLayoutHints *layout = new TGLayoutHints(kLHintsRight|kLHintsCenterY, 10); //, 5, 5, 5);
129
130 fList->Add(laydir);
131 fList->Add(layout);
132
133 //
134 // Create Dir-Listbox and buttons in first frame
135 //
136 TGFSComboBox *dir = new TGFSComboBox(frame, kCBDirectory);
137 dir->SetHeight(fEntry->GetHeight());
138 dir->Associate(this);
139 fList->Add(dir);
140 frame->AddFrame(dir, laydir);
141
142 const TGPicture *pic0 = fList->GetPicture("magic_t.xpm");
143 if (pic0)
144 {
145 dir->GetListBox()->Resize(100, 150);
146 TGTreeLBEntry *entry = new TGTreeLBEntry(dir->GetListBox()->GetContainer(),
147 new TGString("/magic/data"), pic0, 6000,
148 new TGString("/magic/data"));
149 TGLayoutHints *laylb = new TGLayoutHints(kLHintsLeft|kLHintsTop, 14, 0, 0, 0);
150 dir->AddEntry(entry, laylb);
151 // Not necessary - deleted in ~TGLBContainer: fList->Add(laylb);
152 // fList->Add(laylb);
153 }
154
155 //
156 // Get the three picturs from the system (must be deleted by FreePicture)
157 //
158 const TGPicture *pic1 = fList->GetPicture("tb_list.xpm");
159 if (pic1)
160 {
161 TGPictureButton *list = new TGPictureButton(frame, pic1, kButListMode);
162 list->SetToolTipText("List Mode");
163 list->SetState(kButtonUp);
164 list->AllowStayDown(kTRUE);
165 list->Associate(this);
166 fList->Add(list);
167 frame->AddFrame(list, layout);
168 }
169
170 const TGPicture *pic2 = fList->GetPicture("tb_details.xpm");
171 if (pic2)
172 {
173 TGPictureButton *detail = new TGPictureButton(frame, pic2, kButDetailMode);
174 detail->SetToolTipText("Details Mode");
175 detail->SetState(kButtonEngaged);
176 detail->AllowStayDown(kTRUE);
177 detail->Associate(this);
178 fList->Add(detail);
179 frame->AddFrame(detail, layout);
180 }
181
182 const TGPicture *pic3 = fList->GetPicture("tb_uplevel.xpm");
183 if (pic3)
184 {
185 TGPictureButton *cdup = new TGPictureButton(frame, pic3, kButDirUp);
186 cdup->SetToolTipText("One Level up!");
187 cdup->Associate(this);
188 fList->Add(cdup);
189 frame->AddFrame(cdup, layout);
190 }
191}
192
193void MBrowser::CreateDirListBox(TGCompositeFrame *frame)
194{
195 //
196 // Create file viewer (browser)
197 //
198 fFileView = new TGListView(frame, 540, 380);
199 fList->Add(fFileView);
200
201 TGViewPort *port = fFileView->GetViewPort();
202 port->SetBackgroundColor(fgWhitePixel);
203
204 fFileCont = new TGFileContainer(port, 100, 100, kVerticalFrame, fgWhitePixel);
205 fList->Add(fFileCont);
206
207 fFileView->SetContainer(fFileCont);
208 fFileView->SetViewMode(kLVDetails);
209
210 fFileCont->SetFilter("*.root");
211 fFileCont->Associate(this);
212 fFileCont->Sort(kSortByName);
213
214 TGLayoutHints *layview = new TGLayoutHints(kLHintsTop|kLHintsExpandX|kLHintsExpandY); //, 5, 5, 5, 5);
215 fList->Add(layview);
216
217 frame->AddFrame(fFileView, layview);
218}
219
220void MBrowser::CreateTab1()
221{
222 static const char *filters[] =
223 {
224 "*.root <root-files>",
225 "* <All Files>",
226 NULL
227 };
228
229 TGCompositeFrame *frame = CreateNewTab("Input File");
230
231 //
232 // Create three frames for the first tab
233 //
234 TGHorizontalFrame *tab1 = new TGHorizontalFrame(frame, 100, 100);
235 TGVerticalFrame *tab2 = new TGVerticalFrame (frame, 100, 100);
236
237 TGLayoutHints *laytab1 = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 10);
238 TGLayoutHints *laytab2 = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 10, 10, 10, 10);
239 TGLayoutHints *layfilter = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 10);
240
241 fEntry = new TGTextEntry(frame, "", kTEFileName);
242 fEntry->Associate(this);
243
244 TGComboBox *filter = new TGComboBox(frame, kCBFilter);
245 filter->SetHeight(fEntry->GetHeight());
246 filter->Associate(this);
247 for (int i=0; filters[i]; i++)
248 filter->AddEntry(filters[i], i);
249 filter->Select(0);
250
251 frame->AddFrame(fEntry, laytab1);
252 frame->AddFrame(tab1, laytab1);
253 frame->AddFrame(filter, layfilter);
254 frame->AddFrame(tab2, laytab2);
255
256 CreateDirListMenu(tab1);
257 CreateDirListBox(tab2);
258
259 fList->Add(laytab1);
260 fList->Add(laytab2);
261 fList->Add(tab1);
262 fList->Add(tab2);
263 fList->Add(layfilter);
264 fList->Add(fEntry);
265 fList->Add(filter);
266}
267
268TGCompositeFrame *MBrowser::CreateNewTab(const char *name)
269{
270 return fTabs->AddTab(name);
271}
272
273void MBrowser::CreateLowerFrame(TGCompositeFrame *framelow)
274{
275 //
276 // *********** Create Contents of frame low (downer part) **********
277 //
278
279 //
280 // ----- Create Object holding the Tabs -----
281 //
282 fTabs = new TGTab(framelow, 400, 400);
283 fList->Add(fTabs);
284
285 TGLayoutHints *laytabs = new TGLayoutHints(kLHintsBottom|kLHintsExpandX|kLHintsExpandY, 5, 5, 5, 5);
286 fList->Add(laytabs);
287
288 framelow->AddFrame(fTabs, laytabs);
289
290 //
291 // --- Create the first tab of the tabs ---
292 //
293 CreateTab1();
294}
295
296MBrowser::MBrowser(/*const TGWindow *main,*/ const TGWindow *p,
297 const UInt_t w, const UInt_t h)
298//: TGTransientFrame(p?p:gClient->GetRoot(),
299// main?main:gClient->GetRoot(), w, h)
300: TGMainFrame(p?p:gClient->GetRoot(), w, h)
301{
302 fList = new MGList;
303 fList->SetOwner();
304
305 CreateMenuBar();
306
307 TGLayoutHints *laylinesep = new TGLayoutHints(kLHintsTop|kLHintsExpandX);
308 fList->Add(laylinesep);
309
310 TGHorizontal3DLine *linesep = new TGHorizontal3DLine(this);
311 fList->Add(linesep);
312 AddFrame(linesep, laylinesep);
313
314 //
315 // ---- Create the top window with a lot of buttons ----
316 //
317 TGLayoutHints *layframetop = new TGLayoutHints(kLHintsExpandX);
318 fList->Add(layframetop);
319
320 TGCompositeFrame *frametop = new TGCompositeFrame(this, 300, 100, kVerticalFrame);
321 fList->Add(frametop);
322 AddFrame(frametop, layframetop);
323
324 linesep = new TGHorizontal3DLine(this);
325 fList->Add(linesep);
326 AddFrame(linesep, laylinesep);
327
328 //
329 // ---- Create the low window with a tabs in it ----
330 //
331 TGLayoutHints *layframelow = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY);
332 fList->Add(layframelow);
333
334 TGCompositeFrame *framelow = new TGCompositeFrame(this, 300, 100, kHorizontalFrame);
335 fList->Add(framelow);
336
337 AddFrame(framelow, layframelow);
338
339 CreateUpperFrame(frametop);
340 CreateLowerFrame(framelow);
341
342 //
343 // Map the window, set up the layout, etc.
344 //
345 ChangeDir();
346 SetWMSizeHints(400, 350, 1000, 1000, 10, 10); // set the smallest and biggest size of the Main frame
347 Move(rand()%100+50, rand()%100+50);
348}
349
350
351MBrowser::~MBrowser()
352{
353 delete fList;
354}
355
356TGProgressBar *MBrowser::CreateProgressBar(TGHorizontalFrame *frame)
357{
358 static TGLayoutHints laybar(kLHintsCenterY|kLHintsRight/*|kLHintsExpandX*/,
359 10, 10);
360
361 TGHProgressBar *bar=new TGHProgressBar(frame);
362
363 bar->SetWidth(150);
364 bar->ShowPosition();
365
366 frame->AddFrame(bar, &laybar);
367
368 Layout();
369 MapSubwindows();
370
371 return bar;
372}
373
374void MBrowser::DestroyProgressBar(TGProgressBar *bar)
375{
376 TGHorizontalFrame *frame = (TGHorizontalFrame*)bar->GetParent();
377
378 frame->RemoveFrame(bar);
379
380 Layout();
381 MapSubwindows();
382
383 delete bar;
384}
385
386void MBrowser::CloseWindow()
387{
388 // Got close message for this MainFrame. Calls parent CloseWindow()
389 // (which destroys the window) and terminate the application.
390 // The close message is generated by the window manager when its close
391 // window menu item is selected.
392
393 delete this;
394}
395
396Bool_t MBrowser::InputFileSelected()
397{
398 //
399 // Checks if there is a selected input root file
400 //
401 return !fInputFile.IsNull(); //[0]!='\0';
402}
403
404void MBrowser::DisplError(const char *txt)
405{
406 Int_t retval;
407 new TGMsgBox(fClient->GetRoot(), this, "Error!", txt,
408 kMBIconExclamation, 4, &retval);
409}
410
411void MBrowser::DisplWarning(const char *txt)
412{
413 Int_t retval;
414 new TGMsgBox(fClient->GetRoot(), this, "Warning!", txt,
415 kMBIconExclamation, 4, &retval);
416}
417
418void MBrowser::DisplInfo(const char *txt)
419{
420 Int_t retval;
421 new TGMsgBox(fClient->GetRoot(), this, "Information!", txt,
422 kMBIconExclamation, 4, &retval);
423}
424
425void MBrowser::ChangeDir(const char *txt)
426{
427 fFileCont->ChangeDirectory(txt?txt:gSystem->WorkingDirectory());
428
429 const char *dir = fFileCont->GetDirectory();
430
431 TGFSComboBox *cbox = (TGFSComboBox*)fList->FindWidget(kCBDirectory);
432 if (cbox)
433 cbox->Update(dir);
434}
435
436void MBrowser::SetFileName(TString name)
437{
438 if (name[0]!='/')
439 {
440 name.Insert(0, "/");
441 name.Insert(0, fFileCont->GetDirectory());
442 }
443
444 if (gSystem->AccessPathName(name, kFileExists))
445 return;
446
447 if (name.EndsWith(".root", TString::kIgnoreCase))
448 fInputFile = name;
449
450 fEntry->SetText(gSystem->BaseName(name));
451
452 ChangeDir(gSystem->DirName(name));
453}
454
455void MBrowser::SetDir()
456{
457 TGFSComboBox *cbox = (TGFSComboBox*)fList->FindWidget(kCBDirectory);
458 if(!cbox)
459 return;
460
461 const TGTreeLBEntry *entry = (TGTreeLBEntry*)cbox->GetSelectedEntry();
462
463 ChangeDir(entry->GetPath()->GetString());
464}
465
466void MBrowser::SetFilter()
467{
468 //
469 // Try to get widget from list
470 //
471 TGComboBox *filter = (TGComboBox*)fList->FindWidget(kCBFilter);
472 if (!filter)
473 return;
474
475 //
476 // Get the selected text from the list box
477 //
478 const TGTextLBEntry *selected = (TGTextLBEntry*)filter->GetListBox()->GetSelectedEntry();
479
480 //
481 // find filter and remove leading spaces
482 //
483 TString txt = selected->GetText()->GetString();
484 if (txt.First('<') < 0)
485 return;
486
487 //
488 // Set new filter and refresh the file container
489 //
490 txt.Remove(txt.First('<'));
491
492 TString ftxt = txt.Strip();
493 fFileCont->SetFilter(ftxt);
494 fFileCont->DisplayDirectory();
495}
496
497void MBrowser::SetViewMode(const Int_t mode)
498{
499 fFileView->SetViewMode(mode ? kLVList : kLVDetails);
500
501 TGButton *but = (TGButton*)fList->FindWidget(mode);
502 if(!but)
503 return;
504
505 but->SetState(kButtonUp);
506}
507
508// --------------------------------------------------------------------------
509//
510// Process events generated by the gui elements in the frame.
511//
512Bool_t MBrowser::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
513{
514 switch (GET_MSG(msg))
515 {
516 case kC_TEXTENTRY:
517 if (GET_SUBMSG(msg)!=kTE_ENTER)
518 return kTRUE;
519
520 SetFileName(fEntry->GetText());
521 return kTRUE;
522
523 case kC_COMMAND:
524 switch (GET_SUBMSG(msg))
525 {
526 case kCM_MENU:
527 switch (parm1)
528 {
529 case kFileClose:
530 CloseWindow();
531 return kTRUE;
532
533 case kFileTBrowser:
534 new TBrowser();
535 return kTRUE;
536 }
537 return kTRUE;
538
539 case kCM_BUTTON:
540 switch (parm1)
541 {
542 case kButDirUp:
543 //
544 // goto the parent directory
545 //
546 ChangeDir("..");
547 return kTRUE;
548
549 case kButListMode:
550 case kButDetailMode:
551 SetViewMode(parm1);
552 return kTRUE;
553 }
554 return kTRUE;
555
556 case kCM_COMBOBOX:
557 switch (parm1)
558 {
559 case kCBDirectory:
560 SetDir();
561 return kTRUE;
562 case kCBFilter:
563 SetFilter();
564 return kTRUE;
565 }
566 return kTRUE;
567 }
568 return kTRUE;
569
570 case kC_CONTAINER:
571 switch (GET_SUBMSG(msg))
572 {
573
574 // case kCT_ITEMCLICK:
575 // printf ("itemclick\n");
576 // break;
577
578 case kCT_ITEMDBLCLICK:
579 //
580 // process the double click in the file view container
581 //
582 if (parm1 != kButton1 || fFileCont->NumSelected() != 1)
583 return kTRUE;
584
585 //
586 // one file selected
587 //
588 void *dummy = NULL;
589 const TGFileItem *item = (TGFileItem *)fFileCont->GetNextSelected(&dummy);
590
591 const char *str = item->GetItemName()->GetString();
592
593 //
594 // if the user choose a directory
595 // change to this directory
596 //
597 if (S_ISDIR(item->GetType()))
598 {
599 ChangeDir(str);
600 return kTRUE;
601 }
602
603 SetFileName(str);
604 return kTRUE;
605 }
606 }
607 return kTRUE;
608}
Note: See TracBrowser for help on using the repository browser.