GUI: how to extend menus title one the menu bar?

Dear Rooters, Good morning !
I am tring to add the menu bar on my frame containing a title (File, Test,Help)
I followed all the instructions in the users guide, but there is only one title (File) which is posted. Can somebody help me to look where is the error in my program?
I maked thesame with the toolbar and I got thesame problem.
this is the macro and in attachement the resut I obtained

[code]// Mainframe macro generated from application: C:\root\bin\root.exe
// By ROOT version 5.17/02 on 2008-01-25 04:09:24

#include “Riostream.h”

void menubar()
{

// main frame
TGMainFrame *fMainFrame = new TGMainFrame(0,10,10,kMainFrame | kVerticalFrame);
fMainFrame->SetLayoutBroken(kTRUE);

// embedded canvas
TRootEmbeddedCanvas *fRootEmbeddedCanvas = new TRootEmbeddedCanvas(0,fMainFrame,280,136);
Int_t wfRootEmbeddedCanvas = fRootEmbeddedCanvas->GetCanvasWindowId();
TCanvas *c = new TCanvas(“c”, 10, 10, wfRootEmbeddedCanvas);
fRootEmbeddedCanvas->AdoptCanvas©;
fMainFrame->AddFrame(fRootEmbeddedCanvas, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
fRootEmbeddedCanvas->MoveResize(32,32,280,136);

// a popup menu
fMenuFile = new TGPopupMenu(gClient->GetRoot());
fMenuTest = new TGPopupMenu(gClient->GetRoot());
fMenuHelp = new TGPopupMenu(gClient->GetRoot());

// adding menu entries
fMenuFile->AddEntry("&Open…",1);
fMenuFile->AddEntry("&Save",1);
fMenuFile->AddEntry(“S&ave as…”,1);
fMenuFile->AddEntry("&Close", -1);
// adding separator
fMenuFile->AddSeparator();
// next group of menu entries
fMenuFile->AddEntry("&Print",1);
fMenuFile->AddEntry(“P&rint setup…”,1);
fMenuFile->AddSeparator();
fMenuFile->AddEntry(“E&xit”,1);

// menu bar item layout hints
fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop | kLHintsRight);
// menu bar
fMenuBar = new TGMenuBar(fMainFrame,100,20,kHorizontalFrame);
// adding popup menus
fMenuBar->AddPopup("&File",fMenuFile,fMenuBarItemLayout);
fMenuBar->AddPopup("&Test",fMenuTest,fMenuBarItemLayout);
fMenuBar->AddPopup("&Help",fMenuHelp,fMenuBarHelpLayout);

fMainFrame->SetMWMHints(kMWMDecorAll,
kMWMFuncAll,
kMWMInputModeless);
fMainFrame->MapSubwindows();

fMainFrame->Resize(fMainFrame->GetDefaultSize());
fMainFrame->MapWindow();
fMainFrame->Resize(336,188);
} [/code]
Doc1.doc (28 KB)

Hi,

Several issues in your code:

  1. remove the line: fMainFrame->SetLayoutBroken(kTRUE);
  2. add this line: fMainFrame->AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));

But then, you will see that the menu is below your embedded canvas…
So to solve this, two options:

  1. move the line fMainFrame->AddFrame(fRootEmbeddedCanvas, …) below fMainFrame->AddFrame(fMenuBar, …) (ordering is important)
  2. move the creation of the menu on top of your macro, before the creation of your canvas (cleaner solution when you look at the code)

At the end, your code should look like this:


#include "Riostream.h" 

void menubar() 
{ 
   // main frame 
   TGMainFrame *fMainFrame = new TGMainFrame(0,10,10,kMainFrame | kVerticalFrame); 

//   fMainFrame->SetLayoutBroken(kTRUE); <---- Remove this line

   // a popup menu 
   fMenuFile = new TGPopupMenu(gClient->GetRoot()); 
   fMenuTest = new TGPopupMenu(gClient->GetRoot()); 
   fMenuHelp = new TGPopupMenu(gClient->GetRoot()); 

   // adding menu entries 
   fMenuFile->AddEntry("&Open...",1); 
   fMenuFile->AddEntry("&Save",2); 
   fMenuFile->AddEntry("S&ave as...",3); 
   fMenuFile->AddEntry("&Close", 4); 
   // adding separator 
   fMenuFile->AddSeparator(); 
   // next group of menu entries 
   fMenuFile->AddEntry("&Print",5); 
   fMenuFile->AddEntry("P&rint setup...",6); 
   fMenuFile->AddSeparator(); 
   fMenuFile->AddEntry("E&xit",7); 

   // menu bar item layout hints 
   fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0); 
   fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop | kLHintsRight); 
   // menu bar 
   fMenuBar = new TGMenuBar(fMainFrame,1,1,kHorizontalFrame); 
   // adding popup menus 
   fMenuBar->AddPopup("&File",fMenuFile,fMenuBarItemLayout); 
   fMenuBar->AddPopup("&Test",fMenuTest,fMenuBarItemLayout); 
   fMenuBar->AddPopup("&Help",fMenuHelp,fMenuBarHelpLayout); 

   // Don't forget this line !!!!
   fMainFrame->AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));

   // embedded canvas 
   TRootEmbeddedCanvas *fRootEmbeddedCanvas = new TRootEmbeddedCanvas(0,fMainFrame,280,136); 
   Int_t wfRootEmbeddedCanvas = fRootEmbeddedCanvas->GetCanvasWindowId(); 
   TCanvas *c = new TCanvas("c", 10, 10, wfRootEmbeddedCanvas); 
   fRootEmbeddedCanvas->AdoptCanvas(c); 
   fMainFrame->AddFrame(fRootEmbeddedCanvas, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2)); 
   fRootEmbeddedCanvas->MoveResize(32,32,280,136); 

   fMainFrame->SetMWMHints(kMWMDecorAll, 
                           kMWMFuncAll, 
                           kMWMInputModeless); 
   fMainFrame->MapSubwindows(); 

   fMainFrame->Resize(fMainFrame->GetDefaultSize()); 
   fMainFrame->MapWindow(); 
   fMainFrame->Resize(336,188); 
}

Cheers,
Bertrand.

[size=134]Thank you Bertran,
I obtained what I wanted but the final result is catastrophic.[/size]
By eliminating the comand fMainFrame->SetLayoutBroken(kTRUE);, [size=134]all my widgets dispersed on the frame. How can I put my widgets in order without using that comand ?
I thought of creating horizontal frame to place the menu bar there. But the result was not concluding either[/size].

Hi,

Please read the chapter “Writing a Graphical User Interface” of the user guide (available there: root.cern.ch/root/doc/RootDoc.html), and especially the “Layout Management” section.

Bertrand.