Getting Component Sizes/Locations? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Getting Component Sizes/Locations?

Post by FlyingIsFun1217 »

Hey!

I'm trying to make a custom component that can have a set width/height. Problem is, in it, there are a couple of widgets that should be laid out side-by-side, next to each other. For me to be able to do this, I would like to get the size of one component, subtract that from the size of the total component, and finally give the other component a set width based on this result.

Most components (all?) don't allow the user to get their width's/heights though. Is there ANY way I can do this?

Thanks!
FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: Getting Component Sizes/Locations?

Post by mc2r »

FlyingIsFun1217 wrote:Most components (all?) don't allow the user to get their width's/heights though. Is there ANY way I can do this?
I'm not sure what you count as a component, but must of the gui objects if not all in wxWidgets have accessors for their size. It just may not seem that way because of how the docs are laid out. The documentation only shows the member functions of a class that have been changed/added in that class. They don't show the member functions of the base class.

wxWindow is the base of most gui classes in wxWidgets. Some of the size accessors in wxWindow.

wxWindow::GetAdjustedBestSize
wxWindow::GetEffectiveMinSize
wxWindow::GetBestSize
wxWindow::GetClientSize
wxWindow::GetMaxSize
wxWindow::GetMinSize
wxWindow::GetSize
wxWindow::GetVirtualSize

These should be accessible from any of the gui controls.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Exactly what I was looking for!

Thanks! ;)
FlyingIsFun1217
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

however, sizers were made for that kind of task. No need to reinvent the wheel...
Use the source, Luke!
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

doublemax wrote:however, sizers were made for that kind of task. No need to reinvent the wheel...
Very true, forgot to add that note.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

mc2r wrote:
doublemax wrote:however, sizers were made for that kind of task. No need to reinvent the wheel...
Very true, forgot to add that note.
Yes, I know that sizers are the easy way of going about this.

FlyingIsFun1217
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Alright, please don't laugh at me. I've never done much with class inheritance, so I'm sure (and know that) it's riddled with errors, but I imagine you will be able to understand what I'm trying to do:

Code: Select all

class KeyCatchCtrl : public wxWindow
{
	public:
		KeyCatchCtrl(wxWindow *parent, wxPoint *position, wxSize *size) : public wxWindow;

		wxString GetValue();

	private:

		void OnCaptureClick();

		wxString newKeyCombo;
		int width, height, xPos, yPos;

		wxSize *buttonSize;
};

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint *position, wxSize *size) : public wxWindow
{
	wxButton *button = new wxButton(parent, wxID_ANY, _("Capture..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
	button.Disable();
	buttonSize = button.GetSize();
	boxWidth = size.GetWidth() - buttonSize.GetWidth();

	wxTextCtrl *captureBox = new wxTextCtrl(parent, -1, _(""), position, wxSize(boxWidth, size.GetHeight()), wxTE_READONLY | wxTE_CENTRE);
	button.Move((position.x + boxWidth + 5), position.y);
	button.Enable();
}
I haven't done any cleanup on it either, so variables are being thrown around haphazardly.

FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

No laughing, a couple quick comments.

You might want to make these protected not private

Code: Select all

private:

		void OnCaptureClick();

		wxString newKeyCombo;
		int width, height, xPos, yPos;

		wxSize *buttonSize;
};

button and captureBox will lose their scope when the constructor exits. You should make them class members as you do with buttonSize. And remember to get rid of the wxButton* and wxTextCtrl* stuff in the constructor because this will declare 2 pointers shadowing your class members still go out of scope when the constructor is finished.

Also, button is a pointer to a wxButton but you are accessing its member functions with . and not ->

Similiar problem with buttonSize you use . and not -> also, GetSize() returns a wxSize not a wxSize*.

last, the constructor for wxWindow is expecting a wxPoint& and wxSize& and not a wxPoint* and wxSize*. Did you mean to change these?

Code: Select all

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint *position, wxSize *size) : public wxWindow
{
	wxButton *button = new wxButton(parent, wxID_ANY, _("Capture..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
	button.Disable();
	buttonSize = button.GetSize();
	boxWidth = size.GetWidth() - buttonSize.GetWidth();

	wxTextCtrl *captureBox = new wxTextCtrl(parent, -1, _(""), position, wxSize(boxWidth, size.GetHeight()), wxTE_READONLY | wxTE_CENTRE);
	button.Move((position.x + boxWidth + 5), position.y);
	button.Enable();
}
-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

mc2r wrote: last, the constructor for wxWindow is expecting a wxPoint& and wxSize& and not a wxPoint* and wxSize*. Did you mean to change these?
As in:

Code: Select all

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : public wxWindow
Right?

If so, I still end up with (generally speaking) the same compile errors as before (with all changes mentioned):
In constructor `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint&, wxSize&)':
9|error: expected identifier before "public"
9|error: expected `(' before "public"
9|error: expected `{' before "public"
6|error: prototype for `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint*, wxSize*)' does not match any in class `KeyCatchCtrl'
7|error: candidates are: KeyCatchCtrl::KeyCatchCtrl(const KeyCatchCtrl&
9|error: KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint&, wxSize&)
In constructor `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint*, wxSize*)':
6|error: expected identifier before "public"
6|error: expected `(' before "public"
6|error: expected `{' before "public"
6|error: expected unqualified-id before "public"
Thanks again! I'm REALLY thankful!

FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

Missed that in the first post.

This is wrong

Code: Select all

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : public wxWindow
In your header you tell the KeyCatchCtrl class it inherits publicly from wxWindow but not the constructor or any of the other member functions for that matter.

So in the header.

Code: Select all

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size);
in the actual definition of the constructor you can have a : wxWindow() to call the base constructor but no public.

So

Code: Select all

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : wxWindow(parent, position, size){
// Your code
}
This will call the base constructor passing the arguments. and then run your bit of code.

you can do this as well to initialize member variables such as

bogus.h

Code: Select all

class BogusClass {
    public:
        Bogus(int foo);
    protected:
        int i;
};

Code: Select all

Bogus::Bogus(int foo) : i(foo) {
}
This initializes the i member variable with the foo argument.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

I'm still confused. Of course, like I said, I've never really done any inheritance work like this.

Are we saying that none of the constructors have inheritance (including the class, and it's functions), and the actual function declarations get it? Again, this still gives errors.

Looks like I need to read through my C++ book again!

FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

FlyingIsFun1217 wrote:Are we saying that none of the constructors have inheritance (including the class, and it's functions), and the actual function declarations get it? Again, this still gives errors.
Constructors don't have inheritance, classes have inheritance.

the : wxWindow() thing in the previous example is not inheritance. It might look it because of the : but it isn't. It is a special syntax for constructors only to all you to call the constructors of the base classes and constructors of member variables before the classes own constructor is runs.

Hitting the books on this might not be a bad idea, as I am rather poor at explaining these things. You'll more likely than not end up more confused listening to me ;)

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Alright, I'll just post my basic class implementation, and you can show me the errs of my ways :)

Code: Select all

class KeyCatchCtrl
{
    public:
        KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : wxWindow(parent, position, size);
};

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : wxWindow(parent, position, size)
{
    //
}
This alone gives me a plethora of errors:
In constructor `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint&, wxSize&)':
9|error: type `class wxWindow' is not a direct base of `KeyCatchCtrl'
9|error: expected `{' at end of input
6|error: prototype for `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint*, wxSize*)' does not match any in class `KeyCatchCtrl'
7|error: candidates are: KeyCatchCtrl::KeyCatchCtrl(const KeyCatchCtrl&)
9|error: KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint&, wxSize&)|
In constructor `KeyCatchCtrl::KeyCatchCtrl(wxWindow*, wxPoint*, wxSize*)':
6|error: type `class wxWindow' is not a direct base of `KeyCatchCtrl'
Thanks again!
FlyingIsFun1217
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

: wxWindow goes only in the .cpp not the .h. It is effecticly code and part of the function definition and not declaration. Also, missing from the declaration for the KeyCatchCtrl is : public wxWindow

I may not have explained this well in the previous posts. These two (: wxWindow(...) and : public wxWindow) are NOT the same or related.

: public wxWindow declares that class KeyCatchCtrl IS A wxWindow.

: wxWindow(...) in the constructor definition tells the compiler to call the constructor as part of the construction of KeyCatchCtrl

Code: Select all

class KeyCatchCtrl : public wxWindow
{
    public:
        KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size);
};

KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : wxWindow(parent, position, size)
{
    //
}
Not sure if I explained any clearer this time. Hopefully so.

-Max
FlyingIsFun1217
Super wx Problem Solver
Super wx Problem Solver
Posts: 497
Joined: Mon Nov 06, 2006 9:58 pm

Post by FlyingIsFun1217 »

Ok, NOW I've got what you're saying! The class inherits to become a wxWindow, and the function is inheriting wxWindow properties.

And yeah, I'm still gonna take a better look through that book of mine ;)

FlyingIsFun1217
Post Reply