GUI: the save and print menu does not work

I use ROOT 5.6/00 with Windows XP.

I followed the example given in Guitest (ROOTSYS/tutorials/gui/guitest.C) to form my tool bar with two menus File and Help. Under the sub menu File I have Save and Print.

When I click on save, the save window of ROOT opens. what enables me to introduce the name of the file to be saved.
After having clicked on the button save, the window is closed, but the file is not saved.

I beg who can help me to check my code for to see why I can’t save the file on the disc.

This is my code . and in attachement there is the photo of the widget.

[code]#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGLayout.h>
#include <TGWindow.h>
#include <TGLabel.h>
#include <TGNumberEntry.h>
#include <TString.h>

#include “Riostream.h”

enum EMenuIds {
M_FILE_OPEN,
M_FILE_SAVE,
M_FILE_SAVEAS,
M_FILE_PRINT,
M_FILE_PRINTSETUP,
M_FILE_EXIT,
M_FILE_CLOSE,

M_HELP_CONTENTS,
};

const char filetypes1[] = { “All files”, "",
“ROOT files”, “.root",
“ROOT macros”, "
.C”,
“Text files”, “*.[tT][xX][tT]”,
0, 0 };

const char filetypes2[] = { “All files”, "",
“ROOT files”, “.root",
“ROOT macros”, "
.C”,
“Text files”, “*.[tT][xX][tT]”,
0, 0 };

const char *editortxt1 =“This is the text Editor\n”;

class Editor {

private:
TGTransientFrame *fMain; // main frame of this widget
TGTextEdit *fEdit; // text edit widget
TGTextButton *fOK; // OK button
TGLayoutHints *fL1; // layout of TGTextEdit
TGLayoutHints *fL2; // layout of OK button

public:
Editor(const TGWindow *main, UInt_t w, UInt_t h);
virtual ~Editor();

void LoadFile(const char *file);
void LoadBuffer(const char *buffer);
void AddBuffer(const char *buffer);

TGTextEdit *GetEditor() const { return fEdit; }

void SetTitle();
void Popup();

// slots
void CloseWindow();
void DoOK();
void DoOpen();
void DoSave();
void DoClose();
};

class MyMainFrame : public TGMainFrame {

private:

TGGroupFrame *fGroupFrame515 ;
TGPopupMenu *fMenuFile;
TGPopupMenu * fMenuHelp;
TGMenuBar *fMenuBar;

TRootEmbeddedCanvas *fRootEmbeddedCanvas528;
TCanvas *c123;
TGStatusBar *fStatusBar;

public:
MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyMainFrame();

ClassDef(MyMainFrame, 0)
};

MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h)
: TGMainFrame(p, w, h)
{

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

// adding help menu entries
fMenuHelp->AddEntry("&Editor…",M_HELP_CONTENTS);

// adding file menu entries
fMenuFile->AddEntry("&Open…",M_FILE_OPEN);
fMenuFile->AddEntry("&Save",M_FILE_SAVE);
fMenuFile->AddEntry(“S&ave as…”,M_FILE_SAVEAS);
fMenuFile->AddEntry("&Close", M_FILE_CLOSE);

// adding separator
fMenuFile->AddSeparator();

// next group of menu entries
fMenuFile->AddEntry("&Print",M_FILE_PRINT);
fMenuFile->AddEntry(“P&rint setup…”,M_FILE_PRINTSETUP);

fMenuFile->AddSeparator();
fMenuFile->AddEntry(“E&xit”,M_FILE_EXIT);

// menu message are handled by the class’ HandleMenu() method
fMenuFile->Connect(“Activated(Int_t)”, “MyMainFrame”, this,“HandleMenuFile(Int_t)”);
fMenuHelp->Connect(“Activated(Int_t)”, “MyMainFrame”, this,“HandleMenuHelp(Int_t)”);

// menu bar item layout hints
fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop | kLHintsRight);

// menu bar
fMenuBar = new TGMenuBar(this,1,1,kHorizontalFrame);

// adding popup menus
fMenuBar->AddPopup("&File",fMenuFile,fMenuBarItemLayout);
fMenuBar->AddPopup("&Help",fMenuHelp,fMenuBarHelpLayout);

this->AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));

// “” group frame
fGroupFrame515 = new TGGroupFrame(this,"");
fGroupFrame515->SetLayoutBroken(kTRUE);

// embedded canvas
fRootEmbeddedCanvas528 = new TRootEmbeddedCanvas(0,fGroupFrame515,280,126);
Int_t wfRootEmbeddedCanvas528 = fRootEmbeddedCanvas528->GetCanvasWindowId();
c123 = new TCanvas(“c123”, 10, 10, wfRootEmbeddedCanvas528);
fRootEmbeddedCanvas528->AdoptCanvas(c123);
fGroupFrame515->AddFrame(fRootEmbeddedCanvas528, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
fRootEmbeddedCanvas528->MoveResize(18,18,280,126);

fGroupFrame515->SetLayoutManager(new TGVerticalLayout(fGroupFrame515));
fGroupFrame515->Resize(316,162);
this->AddFrame(fGroupFrame515, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
fGroupFrame515->MoveResize(8,24,316,162);

this->MapSubwindows();

this->Resize(fGroupFrame515->GetDefaultSize());
this->MapWindow();
this->Resize(343,191);
}

void MyMainFrame::HandleMenuHelp(Int_t id)
{
// Handle menuu items.

switch (id) {

 case M_HELP_CONTENTS:
     {
 Editor *ed = new Editor(this, 600, 400);

ed->LoadBuffer(editortxt1);
ed->Popup();
}
break;
}
}

void MyMainFrame::HandleMenuFile(Int_t id)
{
// Handle menu items.

switch (id) {

 case M_FILE_OPEN:
     {
        static TString dir(".");

        TGFileInfo fi;
        fi.fFileTypes = filetypes1;
        fi.fIniDir    = StrDup(dir);
        //printf("fIniDir = %s\n", fi.fIniDir);
        
        new TGFileDialog(gClient->GetRoot(),this, kFDOpen, &fi);
        //printf("Open file: %s (dir: %s)\n", fi.fFilename, fi.fIniDir);
        dir = fi.fIniDir;
     }
     break;

  case M_FILE_SAVE:

        static TString dir(".");
    
        TGFileInfo fi;
        fi.fFileTypes = filetypes2;
        fi.fIniDir    = StrDup(dir);
        //printf("fIniDir = %s\n", fi.fIniDir);
       
        new TGFileDialog(gClient->GetRoot(),this, kFDSave, &fi);
        printf("Save file: %s (dir: %s)\n", fi.fFilename, fi.fIniDir);
        dir = fi.fIniDir;

        //printf("M_FILE_SAVE\n");
        break;

  case M_FILE_PRINT:
     printf("M_FILE_PRINT\n");
     printf("Hiding itself, select \"Print Setup...\" to enable again\n");
     fMenuFile->HideEntry(M_FILE_PRINT);
     break;

  case M_FILE_PRINTSETUP:
     printf("M_FILE_PRINTSETUP\n");
     printf("Enabling \"Print\"\n");
     fMenuFile->EnableEntry(M_FILE_PRINT);
     break;

  case M_FILE_EXIT:
     CloseWindow();   // terminate theApp no need to use SendCloseMessage()
     break;

default:

     break;

}
}

Editor::Editor(const TGWindow *main, UInt_t w, UInt_t h)
{
// Create an editor in a dialog.

fMain = new TGTransientFrame(gClient->GetRoot(), main, w, h);
fMain->Connect(“CloseWindow()”, “Editor”, this, “CloseWindow()”);
fMain->DontCallClose(); // to avoid double deletions.

fMain->SetWindowName(“HELP”);

// use hierarchical cleaning
fMain->SetCleanup(kDeepCleanup);

fEdit = new TGTextEdit(fMain, w, h, kSunkenFrame | kDoubleBorder);
fL1 = new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3);
fMain->AddFrame(fEdit, fL1);

// set selected text colors
Pixel_t pxl;
gClient->GetColorByName("#ccccff", pxl);
fEdit->SetSelectBack(pxl);
fEdit->SetSelectFore(TGFrame::GetBlackPixel());

fOK = new TGTextButton(fMain, " &OK ");
fOK->Connect(“Clicked()”, “Editor”, this, “CloseWindow()”);
fL2 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
fMain->AddFrame(fOK, fL2);

fMain->MapSubwindows();

fMain->Resize();

// editor covers right half of parent window
fMain->CenterOnParent(kTRUE, TGTransientFrame::kRight);
}

Editor::~Editor()
{
// Delete editor dialog.

fMain->DeleteWindow(); // deletes fMain
}

void Editor::Popup()
{
// Show editor.

fMain->MapWindow();
}

void Editor::LoadBuffer(const char *buffer)
{
// Load a text buffer in the editor.

fEdit->LoadBuffer(buffer);
}

void Editor::CloseWindow()
{
// Called when closed via window manager action.

delete this;
}

void Editor::DoOpen()
{
SetTitle();
}

void Editor::DoSave()
{
SetTitle();
}

void Editor::DoClose()
{
// Handle close button.

CloseWindow();
}

void test()
{
new MyMainFrame(0,10,10);
}
[/code]
test.doc (740 KB)

My first guess is that you forgot to implement a method to actually save something :open_mouth: (your data, in whatever format you want to save it…)
FYI, the file dialog allows you to specify the file name and the location, but doesn’t save the data for you…
i.e:

[code]
[…]

  case M_FILE_SAVE: 
        static TString dir("."); 
        TGFileInfo fi; 
        fi.fFileTypes = filetypes2; 
        fi.fIniDir    = StrDup(dir); 
        new TGFileDialog(gClient->GetRoot(),this, kFDSave, &fi); 
        printf("Save file: %s (dir: %s)\n", fi.fFilename, fi.fIniDir); 
        dir = fi.fIniDir; 
        if (fi.fFilename) 
           SaveFile(fi.fFilename);
        break; 

[…]

//______________________________________________________________________________
void MyMainFrame::SaveFile(const char *filename)
{
// Saves your data in the file “filename”

}
[/code] Now up to you to adapt the code to your needs.

BTW, please try to attach picture file (i.e. GIF, jpg, png) instead of doc files…

Cheers,
Bertrand.

Hello Bertrand. and Thank you for your assistance.
Excuse to ask you three other questions. Be not annoyed if you find them tedious. For me who is at the beginning of the training of ROOT it is not always easy to retain all.
Here the first question: I do not manage the extensions. Which instruction I must used so that the extension I choose in << file dialog box>> is taken into account. For the moment all file is saved in the form of PS file.

[code][…]
case M_FILE_SAVE:
{

        static TString dir(".");
    
        TGFileInfo fi;
        fi.fFileTypes = filetypes2;
        fi.fIniDir    = StrDup(dir);
        new TGFileDialog(gClient->GetRoot(),this, kFDSave, &fi);
       dir = fi.fIniDir;
       if (fi.fFilename)
      SaveFile(fi.fFilename);
   	}
     break;

[…]

void MyMainFrame::SaveFile(const char *filename)
{
c123->Print(filename);
}[/code]

Here the second question: How to manage the impression with a printer ? For example to call << file dialog box>> one uses construction << new TGFileDialog (gClient->GetRoot (), this, kFDSave, &fi) >> which construction is necessary to call box for the impression.

Here the third question: When I execute my program, some time, the << root command window>> arrives at the last position and recovers my graph it is possible to make so that at the end the << root command window>> is in background of my graph?.

once more Thank you for your Help !

Hi,

Here is the way of setting file types (used as type filter in TGFileDialog):

const char *filetypes2[] = {
   "ROOT Macros",  "*.C",
   "Source files", "*.cxx",
   "Text files",   "*.txt",
   "ROOT files",   "*.root",
   "PS files",     "*.ps",
   "PDF files",    "*.pdf",
   "GIF files",    "*.gif",
   "jpg files",    "*.jpg",
   "All files",    "*",
   0, 0
};

Then use “filetypes2” as in your code:

   TGFileInfo fi; 
   fi.fFileTypes = filetypes2;

But you have to explicitely type the file extension (i.e. ‘filename.ext’ and not just ‘filename’). The file format will be recognized by Root via its extension.
For the Print dialog, please take a look at how it is used in TRootCanvas::PrintCanvas():

//______________________________________________________________________________
void TRootCanvas::PrintCanvas()
{
   // Print the canvas.

   Int_t ret = 0;
   Bool_t pname = kTRUE;
   char *printer, *printCmd;
   static TString sprinter, sprintCmd;

   if (sprinter == "")
      printer = StrDup(gEnv->GetValue("Print.Printer", ""));
   else
      printer = StrDup(sprinter);
   if (sprintCmd == "")
#ifndef WIN32
      printCmd = StrDup(gEnv->GetValue("Print.Command", ""));
#else
      printCmd = StrDup(gEnv->GetValue("Print.Command", "start AcroRd32.exe /p"));
#endif
   else
      printCmd = StrDup(sprintCmd);

   new TGPrintDialog(fClient->GetDefaultRoot(), this, 400, 150,
                     &printer, &printCmd, &ret);
   if (ret) {
      sprinter  = printer;
      sprintCmd = printCmd;

      if (sprinter == "")
         pname = kFALSE;

      TString fn = "rootprint";
      FILE *f = gSystem->TempFileName(fn, gEnv->GetValue("Print.Directory", gSystem->TempDirectory()));
      fclose(f);
      fn += Form(".%s",gEnv->GetValue("Print.FileType", "pdf"));
      fCanvas->Print(fn);

      TString cmd = sprintCmd;
      if (cmd.Contains("%p"))
         cmd.ReplaceAll("%p", sprinter);
      else if (pname) {
         cmd += " "; cmd += sprinter; cmd += " ";
      }

      if (cmd.Contains("%f"))
         cmd.ReplaceAll("%f", fn);
      else {
         cmd += " "; cmd += fn; cmd += " ";
      }

      gSystem->Exec(cmd);
#ifndef WIN32
      gSystem->Unlink(fn);
#endif
   }
   delete [] printer;
   delete [] printCmd;
}

And about the third question, I have no idea…

Cheers,
Bertrand.

[size=150]Thank you very much now all goes well.
I was very happy to receive your assistance.[/size]