Hi,
I have a dynamic picture button which changes the pictures displayed on it with every mouse click. So I want to store the status of that picture button in a variable, that is suppose I have Char_t a = "0";
So I want to have something like this depending on the picture displayed on the button:
Char_t a = "1"; //when picture 1 is displayed
Char_t a = "2"; //when picture 2 is displayed
.
.
.
Any suggestions about how I can do that?
Cheers
Hi,
Well, I’m not sure to really understand what you are asking for… You can use any kind of variable to store the information (note that in case of a char, you should use ‘0’, ‘1’, …)
Cheers, Bertrand.
Well, what I am trying to say is how do I actually get the status of the picture button and store it, i mean the functions involved.
Cheers
One simple solution would be be to use a TString to store the picture name. Something like this:
Cheers, Bertrand.
I tried to do it but I got the following error: illegal pointer to class object butn, where butn is the variable for the picture button (which has replaced “YourButton” in the above suggestion).
Soo…!
Cheers
Hi,
Well, I’m afraid to not be able to debug your code without seeing it… If you need help, please post a running macro illustrating the problem or at least showing what you’re trying to achieve. This will be much faster than trying to guess…
Cheers, Bertrand.
Hi
OK, I have created a simple window with one picture button and one single-line text entry. So the following code is used to change the pictures when the button is clicked(the variables and pointers are already defined in the MainFrame class)
fTextBuffer = new TGTextBuffer(5);//creating a text entry which displays a number
fTextEntry = new TGTextEntry(fFrame,fTextBuffer,Id_1); //depending on the picture
fFrame->AddFrame(fTextEntry,fL2); //on the picture button
//***********************************
fFrame->AddFrame(butn= new TGPictureButton(fFrame,gClient->GetPicture("arrow_up.xpm")));
butn->Connect("Clicked()","MainFrame",this,"ChangeButtons()");//change pictures
butn->SetUserData((void*)1); //with every click
butn->Resize(40, 30);
//********************************
void MainFrame::ChangeButtons()
{
TGPictureButton *a = (TGPictureButton *) gTQSender;
if (((void*) a->GetUserData())==0)
{
butn->SetPicture(gClient->GetPicture("arrow_up.xpm")); //display up arrow
butn->SetUserData((void*)1);
}
else if(((void*) a->GetUserData())==1)
{
butn->SetPicture(gClient->GetPicture("arrow_down.xpm"));//display down arrow
butn->SetUserData((void*)2);
}
else if(((void*) a->GetUserData()==2))
{
butn->SetPicture(gClient->GetPicture("arrow.xpm")); //display an arrow
butn->SetUserData((void*)0);
}
}
So what I want is to display a number on the text entry when a picture button is clicked, like so:
[ul]number -----------> picture on the picture button
1 --------------------------> arrow_up.xpm
2 --------------------------> arrow_down.xpm
3 --------------------------> arrow.xpm[/ul]
I tried to do the following code, but i got the error i mentioned in the last post:
const char*b = "0";
TString data = butn->GetPicture()->GetName(); //get the name of the picture
//display the number on the text entry as the button is clicked
if (data=="arrow_up.xpm")
{
b = "1";
}
else if (data=="arrow_down.xpm")
{
b = "2";
}
else if (data=="arrow_up.xpm")
{
b = "3";
}
fTextBuffer->AddText(0,"b"); //add number to be displayed to the text buffer
Cheers
Hi,
OK, thanks for the code, but please, next time send a running macro, this will save our time.
Here is a possible solution:
#include "TGClient.h"
#include "TGFrame.h"
#include "TGButton.h"
#include "TGTextEntry.h"
#include "TGPicture.h"
class MainFrame : public TGMainFrame {
protected:
TGHorizontalFrame *fFrame;
TGTextBuffer *fTextBuffer;
TGTextEntry *fTextEntry;
TGLayoutHints *fL2;
public:
MainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MainFrame();
void ChangeButtons();
ClassDef(MainFrame, 0)
};
//______________________________________________________________________
MainFrame::~MainFrame()
{
// Clean up main frame...
Cleanup();
}
//______________________________________________________________________
MainFrame::MainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
TGMainFrame(p, w, h)
{
SetCleanup(kDeepCleanup);
fFrame = new TGHorizontalFrame(this, 100, 100);
TGPictureButton *butn = new TGPictureButton(fFrame, gClient->GetPicture("arrow_up.xpm"));
fFrame->AddFrame(butn, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 5, 5, 5, 5));
butn->Connect("Clicked()", "MainFrame", this, "ChangeButtons()");//change pictures
butn->Resize(40, 30);
//***********************************
// creating a text entry which displays a number
// depending on the picture on the picture button
fTextBuffer = new TGTextBuffer(5);
fTextEntry = new TGTextEntry(fFrame, fTextBuffer, 1);
fFrame->AddFrame(fTextEntry, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 5, 5, 5, 5));
AddFrame(fFrame, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
fTextEntry->Clear();
fTextEntry->Insert("0");
//********************************
MapSubwindows();
MapWindow();
Layout();
}
//______________________________________________________________________
void MainFrame::ChangeButtons()
{
TGPictureButton *a = (TGPictureButton *) gTQSender;
char b[5];
sprintf(b, "0");
TString data = a->GetPicture()->GetName(); //get the name of the picture
//display the number on the text entry as the button is clicked
if (data == "arrow_up.xpm") {
sprintf(b, "1");
a->SetPicture(gClient->GetPicture("arrow_down.xpm"));
}
else if (data == "arrow_down.xpm") {
sprintf(b, "2");
a->SetPicture(gClient->GetPicture("arrow_up.xpm"));
}
fTextEntry->Clear();
fTextEntry->Insert(b);
}
//______________________________________________________________________
void changebutton()
{
new MainFrame(gClient->GetRoot(), 150, 100);
}
Cheers, Bertrand.
Thank you very much for your help, bellenot.
Well, since I am new to ROOT I am getting interested, I mean I wish to ask about the best way to learn ROOT. As of now I was using the user guide and the reference guide, but then I am getting confused as to how will I know which function I have to use when dealing with this and that, I mean maybe I am unable to use the reference guide correctly.
Cheers
Hi,
You’re welcome. And I would advise to first look at macros in $ROOTSYS/tutorials (and subdirectories), they are simple and can be used as a good starting point.
Cheers, Bertrand.