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

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