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

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