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

Last change on this file since 1122 was 1108, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 15.8 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 fInputFile[0] = '\0';
283
284 fList = new MGList;
285 fList->SetOwner();
286
287 CreateMenuBar();
288
289 TGLayoutHints *laylinesep = new TGLayoutHints(kLHintsTop|kLHintsExpandX);
290 fList->Add(laylinesep);
291
292 TGHorizontal3DLine *linesep = new TGHorizontal3DLine(this);
293 fList->Add(linesep);
294 AddFrame(linesep, laylinesep);
295
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[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(const char *name)
420{
421 //
422 // determine the file type by extensions
423 //
424 const char *ext=strrchr(name, '.');
425
426 Bool_t failed = kFALSE;
427
428 if (!ext)
429 failed = kTRUE;
430 else
431 if (strcasecmp(ext, ".root"))
432 failed = kTRUE;
433
434 char *fname=NULL;
435 if (!failed)
436 {
437 const char *dir = fFileCont->GetDirectory();
438
439 fname = Form("%s/%s", dir, name);
440
441 failed = gSystem->AccessPathName(fname, kFileExists);
442 }
443
444 if (!failed)
445 strcpy(fInputFile, fname);
446 else
447 fname = Form(fInputFile);
448
449 char *slash = strrchr(fname, '/');
450
451 fEntry->SetText(slash ? slash+1 : "");
452
453 if (!slash)
454 return;
455
456 *slash = '\0';
457 ChangeDir(fname);
458}
459
460void MBrowser::SetDir()
461{
462 TGFSComboBox *cbox = (TGFSComboBox*)fList->FindWidget(kCBDirectory);
463 if(!cbox)
464 return;
465
466 const TGTreeLBEntry *entry = (TGTreeLBEntry*)cbox->GetSelectedEntry();
467
468 ChangeDir(entry->GetPath()->GetString());
469}
470
471void MBrowser::SetFilter()
472{
473 //
474 // Try to get widget from list
475 //
476 TGComboBox *filter = (TGComboBox*)fList->FindWidget(kCBFilter);
477 if (!filter)
478 return;
479
480 //
481 // Get the selected text from the list box
482 //
483 const TGTextLBEntry *selected = (TGTextLBEntry*)filter->GetListBox()->GetSelectedEntry();
484
485 const char *txt = StrDup(selected->GetText()->GetString());
486
487 //
488 // find filter and remove leading spaces
489 //
490 char *lt = strchr(txt, '<');
491 if (lt)
492 {
493 do *lt-- = '\0';
494 while (lt>txt && *lt==' ');
495
496 //
497 // Set new filter and refresh the file container
498 //
499 fFileCont->SetFilter(txt);
500 fFileCont->DisplayDirectory();
501 }
502
503 delete [] txt;
504}
505
506void MBrowser::SetViewMode(const Int_t mode)
507{
508 fFileView->SetViewMode(mode ? kLVList : kLVDetails);
509
510 TGButton *but = (TGButton*)fList->FindWidget(mode);
511 if(!but)
512 return;
513
514 but->SetState(kButtonUp);
515}
516
517// --------------------------------------------------------------------------
518//
519// Process events generated by the gui elements in the frame.
520//
521Bool_t MBrowser::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
522{
523 switch (GET_MSG(msg))
524 {
525 case kC_TEXTENTRY:
526 if (GET_SUBMSG(msg)!=kTE_ENTER)
527 return kTRUE;
528
529 SetFileName(fEntry->GetText());
530 return kTRUE;
531
532 case kC_COMMAND:
533 switch (GET_SUBMSG(msg))
534 {
535 case kCM_MENU:
536 switch (parm1)
537 {
538 case kFileClose:
539 CloseWindow();
540 return kTRUE;
541
542 case kFileTBrowser:
543 new TBrowser();
544 return kTRUE;
545 }
546 return kTRUE;
547
548 case kCM_BUTTON:
549 switch (parm1)
550 {
551 case kButDirUp:
552 //
553 // goto the parent directory
554 //
555 ChangeDir("..");
556 return kTRUE;
557
558 case kButListMode:
559 case kButDetailMode:
560 SetViewMode(parm1);
561 return kTRUE;
562 }
563 return kTRUE;
564
565 case kCM_COMBOBOX:
566 switch (parm1)
567 {
568 case kCBDirectory:
569 SetDir();
570 return kTRUE;
571 case kCBFilter:
572 SetFilter();
573 return kTRUE;
574 }
575 return kTRUE;
576 }
577 return kTRUE;
578
579 case kC_CONTAINER:
580 switch (GET_SUBMSG(msg))
581 {
582
583 // case kCT_ITEMCLICK:
584 // printf ("itemclick\n");
585 // break;
586
587 case kCT_ITEMDBLCLICK:
588 //
589 // process the double click in the file view container
590 //
591 if (parm1 != kButton1 || fFileCont->NumSelected() != 1)
592 return kTRUE;
593
594 //
595 // one file selected
596 //
597 void *dummy = NULL;
598 const TGFileItem *item = (TGFileItem *)fFileCont->GetNextSelected(&dummy);
599
600 const char *str = item->GetItemName()->GetString();
601
602 //
603 // if the user choose a directory
604 // change to this directory
605 //
606 if (S_ISDIR(item->GetType()))
607 {
608 ChangeDir(str);
609 return kTRUE;
610 }
611
612 SetFileName(str);
613 return kTRUE;
614 }
615 }
616 return kTRUE;
617}
Note: See TracBrowser for help on using the repository browser.