Dear Rooters
Below I enclose a simple macro displaying the outline for a dialogbox.
I would like to use this macro (gui1.C) to ask some questions related
to the use of the GUI classes:
1, Memory leaks:
In line 84 I use the following code, similar to code found in guitest.C:
fFrameInfo->AddFrame(new TGLabel(fFrameInfo,new TGString("Last
Name")),
new TGLayoutHints(kLHintsTop | kLHintsLeft));
Here TGLabel, TGString and TGLayoutHints are created within a function,
but never explicitely deleted. Does this result in memory leaks when the
dialogbox will be closed, or will these objects be deleted
automatically?
2, TGGroupFrame "Author Info":
Here I use TGMatrixLayout which results in a large distance between
TGLabel
and TGTextEntry. Is there a better way to implement it?
Could I use TGTableLayout, and how do I need to implement this? (line 71
results in segmentation error)
How can I make sure that TGTextEntry extends to the right end of a frame
without using Resize()?
3, Tabulator key:
Usually, the tabulator key is used to move from one Textentry field to
the
next. Is there a way to implement this behavior in this case?
4, Choose Directory:
Sometimes it is necessary to select a directory. For these purposes, the
Open-Dialogboxes often have an extra button "Choose Directory".
In the current macro I select a directory using TGFileDialog, however,
this is not completely satisfactorily: When I select a directory, then
the selected directory is stored in TGFileInfo.fFilename instead of
TGFileInfo.fIniDir.
Is there a way to easily extend TGFileDialog, so that it displays a
button "Choose Directory"?
5, Size of "OK"-button:
What is the best way to ensure that "Cancel" and "OK" button have the
same size? (fOkBtn->Resize(150,20) on line 116 has no effect)
6, Return-key for "OK"-button:
Usually, pressing the Return-key is analogous to clicking the
"OK"-button.
How can I implement this in the macro?
(Maybe, this could be implemented in class TGTextButton?)
7, Position of main window:
Why does "fMain->SetWMPosition(140,120)" on line 127 place the main
window at top-left instead of the position (140,120)?
Thank you very much in advance.
Best regards
Christian
----------------------------------
C.h.r.i.s.t.i.a.n S.t.r.a.t.o.w.a
V.i.e.n.n.a, A.u.s.t.r.i.a
//--------------------------------------//
// macro gui1.C //
//--------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <TROOT.h>
#include <TApplication.h>
#include <TVirtualX.h>
#include <RQ_OBJECT.h>
#include <TGFrame.h>
#include <TGMenu.h>
#include <TGTextEntry.h>
#include <TGTextBuffer.h>
#include <TGListBox.h>
#include <TGFileDialog.h>
#include <TSystem.h>
#include <TEnv.h>
const char *filetypes[] = { "All files", "*",
"ROOT files", "*.root",
"ROOT macros", "*.C",
0, 0 };
class TNewDlog {
RQ_OBJECT()
private:
TGMainFrame *fMain;
TGCompositeFrame *fFrameLT;
TGGroupFrame *fFrameInfo;
TGTextEntry *fNameF;
TGTextBuffer *fBufF;
TGTextEntry *fNameL;
TGTextBuffer *fBufL;
TGGroupFrame *fFrameDir;
// TGListBox *fDir;
TGTextEntry *fDir;
TGTextBuffer *fBufDir;
TGButton *fBrowseBtn;
TGCompositeFrame *fFrameLB;
TGButton *fOkBtn;
TGButton *fCancelBtn;
public:
TNewDlog(const TGWindow *vWindow,
UInt_t vWidth, UInt_t vHeight, UInt_t vOptions =
kVerticalFrame);
virtual ~TNewDlog();
// slots
void FCloseWindow();
void FBrowse();
void FPressOK();
void FCancel();
};
TNewDlog::TNewDlog(const TGWindow *vWindow,
UInt_t vWidth, UInt_t vHeight, UInt_t
vOptions)
{
fMain = new TGMainFrame(vWindow, vWidth, vHeight, vOptions);
fMain->Connect("FCloseWindow()", "TNewDlog", this, "FCloseWindow()");
// FrameLT for subframes
fFrameLT = new TGCompositeFrame(fMain, 200, 20, kVerticalFrame);
fMain->AddFrame(fFrameLT, new TGLayoutHints(kLHintsLeft |
kLHintsTop));
// Subframe Project Info
fFrameInfo = new TGGroupFrame(fFrameLT, "Author Info",
kVerticalFrame);
// fFrameInfo->SetLayoutManager(new TGTableLayout(fFrameInfo,2,2));
//error
fFrameInfo->SetLayoutManager(new TGMatrixLayout(fFrameInfo,0,2,10));
fFrameLT->AddFrame(fFrameInfo,
new TGLayoutHints(kLHintsTop | kLHintsLeft |
kLHintsExpandX,5,5,5,5));
fFrameInfo->AddFrame(new TGLabel(fFrameInfo,new TGString("First
Name")),
new TGLayoutHints(kLHintsTop | kLHintsLeft));
fNameF = new TGTextEntry(fFrameInfo, fBufF = new TGTextBuffer(100),
-1);
fFrameInfo->AddFrame(fNameF,
new TGLayoutHints(kLHintsTop | kLHintsLeft, 2, 2, 2, 2));
fNameF->Resize(250, fNameF->GetDefaultHeight());
fFrameInfo->AddFrame(new TGLabel(fFrameInfo,new TGString("Last
Name")),
new TGLayoutHints(kLHintsTop | kLHintsLeft));
fNameL = new TGTextEntry(fFrameInfo, fBufL = new TGTextBuffer(100),
-1);
fFrameInfo->AddFrame(fNameL,
new TGLayoutHints(kLHintsTop | kLHintsLeft, 2, 2, 2, 2));
fNameL->Resize(200, fNameL->GetDefaultHeight());
// Subframe Directory
fFrameDir = new TGGroupFrame(fFrameLT, "Directory",
kHorizontalFrame);
fFrameLT->AddFrame(fFrameDir,
new TGLayoutHints(kLHintsTop | kLHintsLeft |
kLHintsExpandX,5, 5, 5, 5));
fDir = new TGTextEntry(fFrameDir, fBufDir = new TGTextBuffer(255),
-1);
// fDir = new TGListBox(fFrameDir,-1);
fDir->Resize(400,20);
char tmp[255];
sprintf(tmp,"%s",gSystem->WorkingDirectory());
fDir->SetText(tmp);
// fDir->AddEntry(tmp,1);
fFrameDir->AddFrame(fDir, new
TGLayoutHints(kLHintsLeft|kLHintsTop,2,2,2,2));
fBrowseBtn = new TGTextButton(fFrameDir, "&Browse...", 1);
fBrowseBtn->Connect("Clicked()", "TNewDlog", this, "FBrowse()");
fFrameDir->AddFrame(fBrowseBtn, new TGLayoutHints(kLHintsRight |
kLHintsTop));
// FrameLB for OK, Cancel
fFrameLB = new TGCompositeFrame(fMain, 200, 20, kHorizontalFrame);
fMain->AddFrame(fFrameLB, new TGLayoutHints(kLHintsLeft |
kLHintsBottom));
fOkBtn = new TGTextButton(fFrameLB, "&OK", 1);
fOkBtn->Connect("Clicked()", "TNewDlog", this, "FPressOK()");
fOkBtn->Resize(150,20); // does not work
fFrameLB->AddFrame(fOkBtn,
new TGLayoutHints(kLHintsRight | kLHintsBottom, 5, 5, 5,
5));
fCancelBtn = new TGTextButton(fFrameLB, "&Cancel", 1);
fCancelBtn->Connect("Clicked()", "TNewDlog", this, "FCancel()");
fFrameLB->AddFrame(fCancelBtn,
new TGLayoutHints(kLHintsLeft | kLHintsBottom, 5, 5, 5,
5));
fMain->MapSubwindows();
fMain->Resize(fMain->GetDefaultSize());
fMain->SetWMPosition(140,120);
// fMain->Resize(400,220);
fMain->SetWindowName("New Dialog");
fMain->MapWindow();
}//Constructor
//----------------------------------------------------------------------//
TNewDlog::~TNewDlog()
{
delete fOkBtn; delete fCancelBtn; delete fFrameLB;
delete fBrowseBtn; delete fDir; delete fFrameDir;
delete fNameL; delete fNameF; delete fFrameInfo;
delete fFrameLT;
delete fMain;
}//Destructor
//----------------------------------------------------------------------//
void TNewDlog::FCloseWindow()
{
delete this;
}//FCloseWindow
//----------------------------------------------------------------------//
void TNewDlog::FBrowse()
{
printf("Browse\n");
TGFileInfo fi;
fi.fFileTypes = filetypes;
fi.fIniDir = fBufDir->GetString();
printf("fIniDir = %s\n", fi.fIniDir);
new TGFileDialog(gClient->GetRoot(), fMain, kFDOpen, &fi);
printf("Open file: %s (dir: %s)\n", fi.fFilename, fi.fIniDir);
fDir->SetText(fi.fIniDir);
}//FBrowseProject
//----------------------------------------------------------------------//
void TNewDlog::FCancel()
{
// Clear entries
fBufF->Clear();
fBufL->Clear();
printf("Cancel\n");
}//FCancel
//----------------------------------------------------------------------//
void TNewDlog::FPressOK()
{
cout << "First Name: " << fBufF->GetString() << endl;
cout << "Last Name: " << fBufL->GetString() << endl;
cout << "Directory: " << fBufDir->GetString() << endl;
fMain->SendCloseMessage();
}//FPressOK
//----------------------------------------------------------------------//
void gui1()
{
new TNewDlog(gClient->GetRoot(), 400, 220);
}
//-------------------------END-------------------
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:57 MET