source: branches/Mars_IncreaseNsb/mmain/MBrowser.cc@ 18726

Last change on this file since 18726 was 7169, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 16.0 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 if (pic0)
140 {
141 dir->GetListBox()->Resize(100, 150);
142 TGTreeLBEntry *entry = new TGTreeLBEntry(dir->GetListBox()->GetContainer(),
143 new TGString("/magic/data"), pic0, 6000,
144 new TGString("/magic/data"));
145 TGLayoutHints *laylb = new TGLayoutHints(kLHintsLeft|kLHintsTop, 14, 0, 0, 0);
146 dir->AddEntry(entry, laylb);
147 // Not necessary - deleted in ~TGLBContainer: fList->Add(laylb);
148 // fList->Add(laylb);
149 }
150
151 //
152 // Get the three picturs from the system (must be deleted by FreePicture)
153 //
154 const TGPicture *pic1 = fList->GetPicture("tb_list.xpm");
155 if (pic1)
156 {
157 TGPictureButton *list = new TGPictureButton(frame, pic1, kButListMode);
158 list->SetToolTipText("List Mode");
159 list->SetState(kButtonUp);
160 list->AllowStayDown(kTRUE);
161 list->Associate(this);
162 fList->Add(list);
163 frame->AddFrame(list, layout);
164 }
165
166 const TGPicture *pic2 = fList->GetPicture("tb_details.xpm");
167 if (pic2)
168 {
169 TGPictureButton *detail = new TGPictureButton(frame, pic2, kButDetailMode);
170 detail->SetToolTipText("Details Mode");
171 detail->SetState(kButtonEngaged);
172 detail->AllowStayDown(kTRUE);
173 detail->Associate(this);
174 fList->Add(detail);
175 frame->AddFrame(detail, layout);
176 }
177
178 const TGPicture *pic3 = fList->GetPicture("tb_uplevel.xpm");
179 if (pic3)
180 {
181 TGPictureButton *cdup = new TGPictureButton(frame, pic3, kButDirUp);
182 cdup->SetToolTipText("One Level up!");
183 cdup->Associate(this);
184 fList->Add(cdup);
185 frame->AddFrame(cdup, layout);
186 }
187}
188
189void MBrowser::CreateDirListBox(TGCompositeFrame *frame)
190{
191 //
192 // Create file viewer (browser)
193 //
194 fFileView = new TGListView(frame, 540, 380);
195 fList->Add(fFileView);
196
197 TGViewPort *port = fFileView->GetViewPort();
198 port->SetBackgroundColor(fgWhitePixel);
199
200 fFileCont = new TGFileContainer(port, 100, 100, kVerticalFrame, fgWhitePixel);
201 fList->Add(fFileCont);
202
203 fFileView->SetContainer(fFileCont);
204 fFileView->SetViewMode(kLVDetails);
205
206 fFileCont->SetFilter("*.root");
207 fFileCont->Associate(this);
208 fFileCont->Sort(kSortByName);
209
210 TGLayoutHints *layview = new TGLayoutHints(kLHintsTop|kLHintsExpandX|kLHintsExpandY); //, 5, 5, 5, 5);
211 fList->Add(layview);
212
213 frame->AddFrame(fFileView, layview);
214}
215
216void MBrowser::CreateTab1()
217{
218 static const char *filters[] =
219 {
220 "*.root <root-files>",
221 "* <All Files>",
222 NULL
223 };
224
225 TGCompositeFrame *frame = CreateNewTab("Input File");
226
227 //
228 // Create three frames for the first tab
229 //
230 TGHorizontalFrame *tab1 = new TGHorizontalFrame(frame, 100, 100);
231 TGVerticalFrame *tab2 = new TGVerticalFrame (frame, 100, 100);
232
233 TGLayoutHints *laytab1 = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 10);
234 TGLayoutHints *laytab2 = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 10, 10, 10, 10);
235 TGLayoutHints *layfilter = new TGLayoutHints(kLHintsNormal|kLHintsExpandX, 10, 10, 10);
236
237 fEntry = new TGTextEntry(frame, "", kTEFileName);
238 fEntry->Associate(this);
239
240 TGComboBox *filter = new TGComboBox(frame, kCBFilter);
241 filter->SetHeight(fEntry->GetHeight());
242 filter->Associate(this);
243 for (int i=0; filters[i]; i++)
244 filter->AddEntry(filters[i], i);
245 filter->Select(0);
246
247 frame->AddFrame(fEntry, laytab1);
248 frame->AddFrame(tab1, laytab1);
249 frame->AddFrame(filter, layfilter);
250 frame->AddFrame(tab2, laytab2);
251
252 CreateDirListMenu(tab1);
253 CreateDirListBox(tab2);
254
255 fList->Add(laytab1);
256 fList->Add(laytab2);
257 fList->Add(tab1);
258 fList->Add(tab2);
259 fList->Add(layfilter);
260 fList->Add(fEntry);
261 fList->Add(filter);
262}
263
264TGCompositeFrame *MBrowser::CreateNewTab(const char *name)
265{
266 return fTabs->AddTab(name);
267}
268
269void MBrowser::CreateLowerFrame(TGCompositeFrame *framelow)
270{
271 //
272 // *********** Create Contents of frame low (downer part) **********
273 //
274
275 //
276 // ----- Create Object holding the Tabs -----
277 //
278 fTabs = new TGTab(framelow, 400, 400);
279 fList->Add(fTabs);
280
281 TGLayoutHints *laytabs = new TGLayoutHints(kLHintsBottom|kLHintsExpandX|kLHintsExpandY, 5, 5, 5, 5);
282 fList->Add(laytabs);
283
284 framelow->AddFrame(fTabs, laytabs);
285
286 //
287 // --- Create the first tab of the tabs ---
288 //
289 CreateTab1();
290}
291
292MBrowser::MBrowser(/*const TGWindow *main,*/ const TGWindow *p,
293 const UInt_t w, const UInt_t h)
294//: TGTransientFrame(p?p:gClient->GetRoot(),
295// main?main:gClient->GetRoot(), w, h)
296: TGMainFrame(p?p:gClient->GetRoot(), w, h)
297{
298 fList = new MGList;
299 fList->SetOwner();
300
301 CreateMenuBar();
302
303 TGLayoutHints *laylinesep = new TGLayoutHints(kLHintsTop|kLHintsExpandX);
304 fList->Add(laylinesep);
305
306 TGHorizontal3DLine *linesep = new TGHorizontal3DLine(this);
307 fList->Add(linesep);
308 AddFrame(linesep, laylinesep);
309
310 //
311 // ---- Create the top window with a lot of buttons ----
312 //
313 TGLayoutHints *layframetop = new TGLayoutHints(kLHintsExpandX);
314 fList->Add(layframetop);
315
316 TGCompositeFrame *frametop = new TGCompositeFrame(this, 300, 100, kVerticalFrame);
317 fList->Add(frametop);
318 AddFrame(frametop, layframetop);
319
320 linesep = new TGHorizontal3DLine(this);
321 fList->Add(linesep);
322 AddFrame(linesep, laylinesep);
323
324 //
325 // ---- Create the low window with a tabs in it ----
326 //
327 TGLayoutHints *layframelow = new TGLayoutHints(kLHintsExpandX|kLHintsExpandY);
328 fList->Add(layframelow);
329
330 TGCompositeFrame *framelow = new TGCompositeFrame(this, 300, 100, kHorizontalFrame);
331 fList->Add(framelow);
332
333 AddFrame(framelow, layframelow);
334
335 CreateUpperFrame(frametop);
336 CreateLowerFrame(framelow);
337
338 //
339 // Map the window, set up the layout, etc.
340 //
341 ChangeDir();
342 SetWMSizeHints(400, 350, 1000, 1000, 10, 10); // set the smallest and biggest size of the Main frame
343 Move(rand()%100+50, rand()%100+50);
344}
345
346
347MBrowser::~MBrowser()
348{
349 delete fList;
350}
351
352TGProgressBar *MBrowser::CreateProgressBar(TGHorizontalFrame *frame)
353{
354 static TGLayoutHints laybar(kLHintsCenterY|kLHintsRight/*|kLHintsExpandX*/,
355 10, 10);
356
357 TGHProgressBar *bar=new TGHProgressBar(frame);
358
359 bar->SetWidth(150);
360 bar->ShowPosition();
361
362 frame->AddFrame(bar, &laybar);
363
364 Layout();
365 MapSubwindows();
366
367 return bar;
368}
369
370void MBrowser::DestroyProgressBar(TGProgressBar *bar)
371{
372 TGHorizontalFrame *frame = (TGHorizontalFrame*)bar->GetParent();
373
374 frame->RemoveFrame(bar);
375
376 Layout();
377 MapSubwindows();
378
379 delete bar;
380}
381
382void MBrowser::CloseWindow()
383{
384 // Got close message for this MainFrame. Calls parent CloseWindow()
385 // (which destroys the window) and terminate the application.
386 // The close message is generated by the window manager when its close
387 // window menu item is selected.
388
389 delete this;
390}
391
392Bool_t MBrowser::InputFileSelected()
393{
394 //
395 // Checks if there is a selected input root file
396 //
397 return !fInputFile.IsNull(); //[0]!='\0';
398}
399
400void MBrowser::DisplError(const char *txt)
401{
402 Int_t retval;
403 new TGMsgBox(fClient->GetRoot(), this, "Error!", txt,
404 kMBIconExclamation, 4, &retval);
405}
406
407void MBrowser::DisplWarning(const char *txt)
408{
409 Int_t retval;
410 new TGMsgBox(fClient->GetRoot(), this, "Warning!", txt,
411 kMBIconExclamation, 4, &retval);
412}
413
414void MBrowser::DisplInfo(const char *txt)
415{
416 Int_t retval;
417 new TGMsgBox(fClient->GetRoot(), this, "Information!", txt,
418 kMBIconExclamation, 4, &retval);
419}
420
421void MBrowser::ChangeDir(const char *txt)
422{
423 fFileCont->ChangeDirectory(txt?txt:gSystem->WorkingDirectory());
424
425 const char *dir = fFileCont->GetDirectory();
426
427 TGFSComboBox *cbox = (TGFSComboBox*)fList->FindWidget(kCBDirectory);
428 if (cbox)
429 cbox->Update(dir);
430}
431
432void MBrowser::SetFileName(TString name)
433{
434 if (name[0]!='/')
435 {
436 name.Insert(0, "/");
437 name.Insert(0, fFileCont->GetDirectory());
438 }
439
440 if (gSystem->AccessPathName(name, kFileExists))
441 return;
442
443 if (name.EndsWith(".root", TString::kIgnoreCase))
444 fInputFile = name;
445
446 fEntry->SetText(gSystem->BaseName(name));
447
448 ChangeDir(gSystem->DirName(name));
449}
450
451void MBrowser::SetDir()
452{
453 TGFSComboBox *cbox = (TGFSComboBox*)fList->FindWidget(kCBDirectory);
454 if(!cbox)
455 return;
456
457 const TGTreeLBEntry *entry = (TGTreeLBEntry*)cbox->GetSelectedEntry();
458
459 ChangeDir(entry->GetPath()->GetString());
460}
461
462void MBrowser::SetFilter()
463{
464 //
465 // Try to get widget from list
466 //
467 TGComboBox *filter = (TGComboBox*)fList->FindWidget(kCBFilter);
468 if (!filter)
469 return;
470
471 //
472 // Get the selected text from the list box
473 //
474 const TGTextLBEntry *selected = (TGTextLBEntry*)filter->GetListBox()->GetSelectedEntry();
475
476 //
477 // find filter and remove leading spaces
478 //
479 TString txt = selected->GetText()->GetString();
480 if (txt.First('<') < 0)
481 return;
482
483 //
484 // Set new filter and refresh the file container
485 //
486 txt.Remove(txt.First('<'));
487
488 TString ftxt = txt.Strip();
489 fFileCont->SetFilter(ftxt);
490 fFileCont->DisplayDirectory();
491}
492
493void MBrowser::SetViewMode(const Int_t mode)
494{
495 fFileView->SetViewMode(mode ? kLVList : kLVDetails);
496
497 TGButton *but = (TGButton*)fList->FindWidget(mode);
498 if(!but)
499 return;
500
501 but->SetState(kButtonUp);
502}
503
504// --------------------------------------------------------------------------
505//
506// Process events generated by the gui elements in the frame.
507//
508Bool_t MBrowser::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
509{
510 switch (GET_MSG(msg))
511 {
512 case kC_TEXTENTRY:
513 if (GET_SUBMSG(msg)!=kTE_ENTER)
514 return kTRUE;
515
516 SetFileName(fEntry->GetText());
517 return kTRUE;
518
519 case kC_COMMAND:
520 switch (GET_SUBMSG(msg))
521 {
522 case kCM_MENU:
523 switch (parm1)
524 {
525 case kFileClose:
526 CloseWindow();
527 return kTRUE;
528
529 case kFileTBrowser:
530 new TBrowser();
531 return kTRUE;
532 }
533 return kTRUE;
534
535 case kCM_BUTTON:
536 switch (parm1)
537 {
538 case kButDirUp:
539 //
540 // goto the parent directory
541 //
542 ChangeDir("..");
543 return kTRUE;
544
545 case kButListMode:
546 case kButDetailMode:
547 SetViewMode(parm1);
548 return kTRUE;
549 }
550 return kTRUE;
551
552 case kCM_COMBOBOX:
553 switch (parm1)
554 {
555 case kCBDirectory:
556 SetDir();
557 return kTRUE;
558 case kCBFilter:
559 SetFilter();
560 return kTRUE;
561 }
562 return kTRUE;
563 }
564 return kTRUE;
565
566 case kC_CONTAINER:
567 switch (GET_SUBMSG(msg))
568 {
569
570 // case kCT_ITEMCLICK:
571 // printf ("itemclick\n");
572 // break;
573
574 case kCT_ITEMDBLCLICK:
575 //
576 // process the double click in the file view container
577 //
578 if (parm1 != kButton1 || fFileCont->NumSelected() != 1)
579 return kTRUE;
580
581 //
582 // one file selected
583 //
584 void *dummy = NULL;
585 const TGFileItem *item = (TGFileItem *)fFileCont->GetNextSelected(&dummy);
586
587 const char *str = item->GetItemName()->GetString();
588
589 //
590 // if the user choose a directory
591 // change to this directory
592 //
593 if (S_ISDIR(item->GetType()))
594 {
595 ChangeDir(str);
596 return kTRUE;
597 }
598
599 SetFileName(str);
600 return kTRUE;
601 }
602 }
603 return kTRUE;
604}
Note: See TracBrowser for help on using the repository browser.