source: trunk/MagicSoft/Mars/mmain/MBrowser.cc@ 3032

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