Getting Component Sizes/Locations? Topic is solved
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
Getting Component Sizes/Locations?
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
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
Re: Getting Component Sizes/Locations?
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.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?
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
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
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:
I haven't done any cleanup on it either, so variables are being thrown around haphazardly.
FlyingIsFun1217
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();
}
FlyingIsFun1217
No laughing, a couple quick comments.
You might want to make these protected not private
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?
-Max
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();
}
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
As in: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?
Code: Select all
KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : public wxWindow
If so, I still end up with (generally speaking) the same compile errors as before (with all changes mentioned):
Thanks again! I'm REALLY thankful!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"
FlyingIsFun1217
Missed that in the first post.
This is wrong
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.
in the actual definition of the constructor you can have a : wxWindow() to call the base constructor but no public.
So
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
This initializes the i member variable with the foo argument.
-Max
This is wrong
Code: Select all
KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : public wxWindow
So in the header.
Code: Select all
KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size);
So
Code: Select all
KeyCatchCtrl::KeyCatchCtrl(wxWindow *parent, wxPoint &position, wxSize &size) : wxWindow(parent, position, size){
// Your 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) {
}
-Max
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
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
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
Constructors don't have inheritance, classes have inheritance.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.
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
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm
Alright, I'll just post my basic class implementation, and you can show me the errs of my ways
This alone gives me a plethora of errors:
FlyingIsFun1217
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)
{
//
}
Thanks again!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'
FlyingIsFun1217
: 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
Not sure if I explained any clearer this time. Hopefully so.
-Max
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)
{
//
}
-Max
-
- Super wx Problem Solver
- Posts: 497
- Joined: Mon Nov 06, 2006 9:58 pm