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

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