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

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