[ROOT] Tabbing between text elements

From: Chris Milne (phycm@phy.hw.ac.uk)
Date: Thu Sep 13 2001 - 15:38:39 MEST


Hi there,

I have a class (see below), which creates a dialog box with 5 text entry boxes, 
however, I was wondering if there is a way to allow the user to Tab between 
these elements, and also allow them to simply hit return to answer OK. I know 
that dialogs.C allows the user to simply hit return, but there is what I based 
my code on!

Any ideas

Thanks in advance
Chris Milne


class MainDialog {

///////////////////////////////////////
// this class brings up the dialog that
// requests all the calculation params
///////////////////////////////////////

private:
  TGTextEntry      *fTE,*fTE2,*fTE3,*fTE4,*fTE5;      // text entry widget 
containing
  TGTransientFrame *fDialog;  // transient frame, main dialog window
  TGCompositeFrame     *f1, *f2, *f3, *f4, *f5;
  TList            *fWidgets; // keep track of widgets to be deleted in dtor
  char             *fRetStr,*fRetStr2,*fRetStr3,*fRetStr4,*fRetStr5;  // address 
to store return string

public:
   MainDialog(          const char *prompt, const char *defval, char &date[],
			 const char *prompt2, const char *defval2, Int_t 
&reference,
			 const char *prompt3, const char *defval3, Float_t 
&filter,
			 const char *prompt4, const char *defval4, Double_t 
&roughLH,
			 const char *prompt5, const char *defval5, Double_t 
&roughHH);
			 
   ~MainDialog();
   void ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);
};

MainDialog::~MainDialog()
{
   // Cleanup dialog.

   fWidgets->Delete();
   delete fWidgets;

   delete fTE;
   delete fTE2;
   delete fTE3;
   delete fTE4;
   delete fTE5;
   delete fDialog;
}

MainDialog::MainDialog(const char *prompt, const char *defval, char &date[],
			 const char *prompt2, const char *defval2, Int_t 
&reference,
			 const char *prompt3, const char *defval3, Float_t 
&filter,
			 const char *prompt4, const char *defval4, Double_t 
&roughLH,
			 const char *prompt5, const char *defval5, Double_t 
&roughHH)
{
   char *retstr;

   // Create simple input dialog.
   fWidgets = new TList;

   TGWindow *main = gClient->GetRoot();
   fDialog = new TGTransientFrame(main, main, 10, 10);

   // command to be executed by buttons and text entry widget
   char cmd[128];
   sprintf(cmd, "{long r__ptr=0x%lx; 
((MainDialog*)r__ptr)->ProcessMessage($MSG,$PARM1,$PARM2);}", (Long_t)this);

 
   f1 = new TGCompositeFrame(fDialog, 10, 20, kHorizontalFrame);
   f2 = new TGCompositeFrame(fDialog, 10, 20, kHorizontalFrame);
   f3 = new TGCompositeFrame(fDialog, 10, 20, kHorizontalFrame);
   f4 = new TGCompositeFrame(fDialog, 10, 20, kHorizontalFrame);
   f5 = new TGCompositeFrame(fDialog, 10, 20, kHorizontalFrame);

   // create prompt label and textentry widget
   TGLabel *label = new TGLabel(f1, prompt);
   fWidgets->Add(label);
   TGLabel *label2 = new TGLabel(f2, prompt2);
   fWidgets->Add(label2);
   TGLabel *label3 = new TGLabel(f3, prompt3);
   fWidgets->Add(label3);
   TGLabel *label4 = new TGLabel(f4, prompt4);
   fWidgets->Add(label4);
   TGLabel *label5 = new TGLabel(f5, prompt5);
   fWidgets->Add(label5);

   TGTextBuffer *tbuf = new TGTextBuffer(4);  //will be deleted by TGtextEntry
   tbuf->AddText(0, defval);
   TGTextBuffer *tbuf2 = new TGTextBuffer(10);  //will be deleted by TGtextEntry
   tbuf2->AddText(0, defval2);
   TGTextBuffer *tbuf3 = new TGTextBuffer(10);  //will be deleted by TGtextEntry
   tbuf3->AddText(0, defval3);
   TGTextBuffer *tbuf4 = new TGTextBuffer(10);  //will be deleted by TGtextEntry
   tbuf4->AddText(0, defval4);
   TGTextBuffer *tbuf5 = new TGTextBuffer(10);  //will be deleted by TGtextEntry
   tbuf5->AddText(0, defval5);

   fTE = new TGTextEntry(f1, tbuf);
   fTE->Resize(60, fTE->GetDefaultHeight());
   fTE->SetCommand(cmd);
   fTE2 = new TGTextEntry(f2, tbuf2);
   fTE2->Resize(60, fTE2->GetDefaultHeight());
   fTE2->SetCommand(cmd);
   fTE3 = new TGTextEntry(f3, tbuf3);
   fTE3->Resize(60, fTE3->GetDefaultHeight());
   fTE3->SetCommand(cmd);
   fTE4 = new TGTextEntry(f4, tbuf4);
   fTE4->Resize(60, fTE4->GetDefaultHeight());
   fTE4->SetCommand(cmd);
   fTE5 = new TGTextEntry(f5, tbuf5);
   fTE5->Resize(60, fTE5->GetDefaultHeight());
   fTE5->SetCommand(cmd);

   TGLayoutHints *l1 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 0);
   TGLayoutHints *l2 = new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
   fWidgets->Add(l1);
   fWidgets->Add(l2);

   fDialog->AddFrame(f1, l1);
   f1->AddFrame(label, l1);
   f1->AddFrame(fTE, l2);
   fDialog->AddFrame(f2, l1);
   f2->AddFrame(label2, l1);
   f2->AddFrame(fTE2, l2);
   fDialog->AddFrame(f3, l1);
   f3->AddFrame(label3, l1);
   f3->AddFrame(fTE3, l2);
   fDialog->AddFrame(f4, l1);
   f4->AddFrame(label4, l1);
   f4->AddFrame(fTE4, l2);
   fDialog->AddFrame(f5, l1);
   f5->AddFrame(label5, l1);
   f5->AddFrame(fTE5, l2);

   // create frame and layout hints for Ok and Cancel buttons
   TGHorizontalFrame *hf = new TGHorizontalFrame(fDialog, 60, 20, kFixedWidth);
   TGLayoutHints     *l3 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 5, 
5, 0, 0);

   // put hf as last in list to be deleted
   fWidgets->Add(l3);

   // create OK and Cancel buttons in their own frame (hf)
   UInt_t  nb = 0, width = 0, height = 0;
   TGTextButton *b;

   b = new TGTextButton(hf, "Ok", cmd, 1);
   fWidgets->Add(b);
   b->Associate(fDialog);
   hf->AddFrame(b, l3);
   height = b->GetDefaultHeight();
   width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;

   b = new TGTextButton(hf, "Cancel", ".q", 2);
   fWidgets->Add(b);
   b->Associate(fDialog);
   hf->AddFrame(b, l3);
   height = b->GetDefaultHeight();
   width  = TMath::Max(width, b->GetDefaultWidth()); ++nb;

   // place button frame (hf) at the bottom
   TGLayoutHints *l4 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 
5, 5);
   fWidgets->Add(l4);
   fWidgets->Add(hf);

   fDialog->AddFrame(hf, l4);

   // keep buttons centered and with the same width
   hf->Resize((width + 20) * nb, height);

   // set dialog title

   fDialog->SetWindowName("Please Enter The Following Parameters");

   // map all widgets and calculate size of dialog
   fDialog->MapSubwindows();

   width  = fDialog->GetDefaultWidth();
   height = fDialog->GetDefaultHeight();

   fDialog->Resize(width, height);

   // position relative to the parent window (which is the root window)
   Window_t wdum;
   int      ax, ay;

   gVirtualX->TranslateCoordinates(main->GetId(), main->GetId(),
                          (((TGFrame *) main)->GetWidth() - width) >> 1,
                          (((TGFrame *) main)->GetHeight() - height) >> 1,
                          ax, ay, wdum);
   fDialog->Move(ax, ay);
   fDialog->SetWMPosition(ax, ay);

   // make the message box non-resizable
   fDialog->SetWMSize(width, height);
   fDialog->SetWMSizeHints(width, height, width, height, 0, 0);

   fDialog->SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
                                       kMWMDecorMinimize | kMWMDecorMenu,
                        kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
                                       kMWMFuncMinimize,
                        kMWMInputModeless);

   // popup dialog and wait till user replies
   fDialog->MapWindow();

   gClient->WaitFor(fDialog);
}

void MainDialog::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
{
   // Handle button and text enter events

   switch (GET_MSG(msg)) {
      case kC_COMMAND:
         switch (GET_SUBMSG(msg)) {
             case kCM_BUTTON:
                switch (parm1) {
                   case 1:
                      // here copy the string from text buffer to return 
variable
		      sprintf(date,"%s",fTE->GetBuffer()->GetString());
		      reference=atoi(fTE2->GetBuffer()->GetString());
		      filter=atof(fTE3->GetBuffer()->GetString());
		      roughLH=atof(fTE4->GetBuffer()->GetString());
		      roughHH=atof(fTE5->GetBuffer()->GetString());		      
                      delete this;
                      break;

                   case 2:
                      fRetStr[0] = 0;
                      delete this;
                      break;
                 }
              default:
                 break;
          }
          break;

       case kC_TEXTENTRY:
         switch (GET_SUBMSG(msg)) {
             case kTE_ENTER:
                // here copy the string from text buffer to return variable
		sprintf(date,"%s",fTE->GetBuffer()->GetString());
	        reference=atoi(fTE2->GetBuffer()->GetString());
	 	filter=atof(fTE3->GetBuffer()->GetString());
		roughLH=atof(fTE4->GetBuffer()->GetString());
		roughHH=atof(fTE5->GetBuffer()->GetString());			
                delete this;
                break;
             default:
                break;
          }
          break;

       default:
          break;
   }
}



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:59 MET