Page 1 of 1

Creating a wxStaticBitmap within a class constructor

Posted: Fri Mar 20, 2009 5:05 pm
by oarzu
I have a class myframe that has a wxPanel* called panel. "panel" is where i put all my controls and such. I currently have many wxStaticBitmaps created on "panel" that represent LEDs. Instead of manually creating every LED i want to create a class within myframe called sLED that will create a wxStaticBitmap for every instance of the sLED.
Here's my constructor:
saviLED(int x_pos, int y_pos){
state = 0;
xcord = x_pos;
ycord = y_pos;
//parent = new wxPanel;
bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
my_bitmap = new wxStaticBitmap(panel, wxID_ANY, bmp, wxPoint(xcord,ycord),
wxSize(14,14), wxBU_AUTODRAW);
}
I need to pass the parent window -> myframme::panel to the wxStaticBitmap constructor but i haven't been able to. I've tried [this] and [myframe::panel] or just [panel]. Can anyone tell me how to do this? Thank you.

Re: Creating a wxStaticBitmap within a class constructor

Posted: Fri Mar 20, 2009 5:16 pm
by mc2r
Hello,

If you could, please use the code tags when posting code. It makes it much easier to read.
oarzu wrote: saviLED(int x_pos, int y_pos){
state = 0;
xcord = x_pos;
ycord = y_pos;
//parent = new wxPanel;
bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
my_bitmap = new wxStaticBitmap(panel, wxID_ANY, bmp, wxPoint(xcord,ycord),
wxSize(14,14), wxBU_AUTODRAW);
}
as you have this declared saviLED is not a class constructor. It is function called saviLED.

Code: Select all

saviLED::saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(this, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}

-Max

Posted: Fri Mar 20, 2009 7:00 pm
by oarzu
The code i posted is housed inside the saviLED class definition. A class constructor is a function inside the class with the same name as the class. This particular constructor takes two int arguments. Here's the entire class difinition.

Code: Select all

class saviLED{
		public:
			int GetState();
			void SetState(int new_state);
			saviLED(){
				state = 0;
				xcord = 0;
				ycord = 0;
				//wxString str = "RED";
				//wxBitmap *r_bit = MyFrame::GetBitmap(str);

				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14),wxBU_AUTODRAW);
			}
			saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}
			~saviLED(){
				delete my_bitmap;
			}
		private:
			wxStaticBitmap *my_bitmap;
			wxBitmap bmp;
			wxPanel *parent;
			int state, xcord, ycord;

	};

Posted: Fri Mar 20, 2009 7:19 pm
by mc2r
oarzu wrote:A class constructor is a function inside the class with the same name as the class.
True but, it needs to be either inside the class definition(which you didn't show) or have the class name and :: prepended. People can only comment on what code is posted.

oarzu wrote:

Code: Select all

class saviLED{
		public:
			int GetState();
			void SetState(int new_state);
			saviLED(){
				state = 0;
				xcord = 0;
				ycord = 0;
				//wxString str = "RED";
				//wxBitmap *r_bit = MyFrame::GetBitmap(str);

				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14),wxBU_AUTODRAW);
			}
			saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}
			~saviLED(){
				delete my_bitmap;
			}
		private:
			wxStaticBitmap *my_bitmap;
			wxBitmap bmp;
			wxPanel *parent;
			int state, xcord, ycord;

	};
Again, I can only comment on the actual code you've posted as I'm not a psychic.

But in both the default constructor and the 2 int argument constructor you have commented out the line where parent is initialized to something. As it is parent is an unitialized pointer and using it for anything is going to result in undefined behavior. Set it to NULL or a new wxPanel();

The reason the using this in the wxStaticBitmap doesn't work is that saviLED is NOT derived from anything. To be used as the parent it needs to be derived from wxWindow or a class derived from wxWindow.

-Max