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

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