WxDialog creating different size

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.
Post Reply
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

WxDialog creating different size

Post by philm »

Hello everyone,

I have been noticing that alot of my wxDialog objects are somehow adjusting their sizes that I set for them. Sometimes, the size is too long, other times, the size is too short and cuts off parts of my Ok and Cancel buttons (or whatever button I have at the bottom)

I was wondering if someone else ran into a similiar issue before and found a fix? I have been looking online to see if there is something similiar but I have not found anything so far. For those wondering, here is a code snippet of one of my dialogs that seem to be changing sizes:

Code: Select all

boundaryDialog::boundaryDialog(std::vector<magneticBoundary> boundaryList) : wxDialog(NULL, wxID_ANY, "Boundary Definition", wxDefaultPosition, wxSize(233, 148))
{
    wxFont *font = new wxFont(8.5, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    
    _magneticBoundaryList = boundaryList;
    
    for(std::vector<magneticBoundary>::iterator boundaryIterator = _magneticBoundaryList.begin(); boundaryIterator != _magneticBoundaryList.end(); ++boundaryIterator)
    {
        magneticBoundaryNameArray->Add(wxString(boundaryIterator->getBoundaryName()));
    }
    
    wxButton *addPropertyButton = new wxButton(this, propertiesDialogEnum::ID_ButtonAdd, "Add Property", wxPoint(12, 49), wxSize(125, 26));
    
    wxButton *deletePropertyButton = new wxButton(this, propertiesDialogEnum::ID_ButtonDelete, "Delete Property", wxPoint(12, 78), wxSize(125, 26));
    
    wxButton *modifyPropertyButton = new wxButton(this, propertiesDialogEnum::ID_ButtonModify, "Modify Property", wxPoint(12, 107), wxSize(125, 26));
    
    wxButton *okButton = new wxButton(this, wxID_OK, "OK", wxPoint(146, 107), wxSize(75, 23));
    
    wxStaticText *name = new wxStaticText(this, wxID_ANY, "Name: ", wxPoint(12, 9), wxSize(38, 13));
    name->SetFont(*font);
    
    selection->Create(this, wxID_ANY, wxEmptyString, wxPoint(56, 6), wxSize(165, 21), *magneticBoundaryNameArray);
    selection->SetFont(*font);
    
    this->FitInside();
    this->SetMinSize(this->GetSize());
    this->SetMaxSize(this->GetSize());
}
This one typically is created with a size about 20 pixels short of what it is suppose to be.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: WxDialog creating different size

Post by catalin »

There is no perfect solution for using fixed sizes like you do. You need to use sizers to cope with differences in layout on different systems.

p.s. When the dialog final size is determined by its children, you should not give it an explicit size when it is created, because that will also set its minimum size (you set it again at some point but that is too late for the layout).
Go with Fit() instead of FitInside().

..again, do use sizers, you'll be a lot happier in the end.
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: WxDialog creating different size

Post by philm »

Oh so the sizers are used to prevent random resizing of the window? I thought that their main purpose was for assisting in resizing windows.

I have been developing on ubuntu 14.04 and I haven't tried my application in another environment.

My dialog, I make sure that the user is unable to resize it.

What do you mean in your ps? I would have to initilize the dialog to some eplicit size?

Also, there is another wxDialog that I used Fit() and the results are the same. Here is another dialog that I have created that does random resizing:

Code: Select all

blockPropertyMagnetic::blockPropertyMagnetic() : wxDialog(NULL, wxID_ANY, "Magnetic Block Property", wxDefaultPosition, wxSize(416, 465))
{
    wxFont *font = new wxFont(8.5, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
    
    _magneticMaterial.setCoercivity(0);
    _magneticMaterial.setName("New Material");
    _magneticMaterial.setCurrentDensity(0);
    _magneticMaterial.setLaminationFillFactor(1);
    _magneticMaterial.setLaminationThickness(0);
    _magneticMaterial.setMUrX(1);
    _magneticMaterial.setMUrY(1);
    _magneticMaterial.setNumberStrands(1);
    _magneticMaterial.setPhiX(0);
    _magneticMaterial.setPhiY(0);
    _magneticMaterial.setSigma(0);
    _magneticMaterial.setSpecialAttribute(lamWireEnum::NOT_LAMINATED_OR_STRANDED);
    _magneticMaterial.setStrandDiameter(0);
    
    BHSettingsArray->Add("Linear B-H Curve");
    BHSettingsArray->Add("Nonlinear B-H Curve");
    
    LamWireSettingsArray->Add("Not Laminated or Stranded");
    LamWireSettingsArray->Add("Laminated In-Plane");
    LamWireSettingsArray->Add("Laminated Parallel to x (Planar) or r (Axisymmetric)");
    LamWireSettingsArray->Add("Laminated Parallel to y (Planar) or z (Axisymmetric)");
    LamWireSettingsArray->Add("Magnet Wire");
    LamWireSettingsArray->Add("Plain Stranded Wire");
    LamWireSettingsArray->Add("Litz Wire");
    LamWireSettingsArray->Add("Square Wire");
    LamWireSettingsArray->Add("10% CCA");
    LamWireSettingsArray->Add("15% CCA");
    
    /* This section is for the header */
    wxStaticText *name = new wxStaticText(this, wxID_ANY, "Name:", wxPoint(21, 15), wxSize(38, 13));
    name->SetFont(*font);
    wxStaticText *BHCurve = new wxStaticText(this, wxID_ANY, "B-H Curve", wxPoint(21, 45), wxSize(56, 13));
    BHCurve->SetFont(*font);
    BHCurveComboBox->Create(this, generalFrameButton::ID_ComboBox1, wxEmptyString, wxPoint(95, 42), wxSize(165, 21), *BHSettingsArray);
    BHCurveComboBox->SetFont(*font);
    nameTextCtrl->Create(this, magneticBlockPropertyDiag::ID_TextControl1, _magneticMaterial.getName(), wxPoint(95, 12), wxSize(251, 20));
    nameTextCtrl->SetFont(*font);
    
    /* This first part is for the Linear Properties */
    linearMatPropGroupBox->SetFont(*font);
    linearMatPropGroupBox->Create(this, wxID_ANY, "Linear Material Properties", wxPoint(5, 69), wxSize(409, 74));
    wxStaticText *relativeUx = new wxStaticText(linearMatPropGroupBox, wxID_ANY, wxT("Relative μx:"), wxPoint(6, 4), wxSize(63,13));
    relativeUx->SetFont(*font);
    wxStaticText *relativeUy = new wxStaticText(linearMatPropGroupBox, wxID_ANY, wxT("Relative μy:"), wxPoint(214, 4), wxSize(63, 13));
    relativeUy->SetFont(*font);
    wxStaticText *phiX = new wxStaticText(linearMatPropGroupBox, wxID_ANY, wxT("ϕ hx:"), wxPoint(7, 30), wxSize(38, 13));
    phiX->SetFont(*font);
    wxStaticText *phiY = new wxStaticText(linearMatPropGroupBox, wxID_ANY, wxT("ϕ hy:"), wxPoint(214, 30), wxSize(38, 13));
    phiY->SetFont(*font);
    wxStaticText *deg1 = new wxStaticText(linearMatPropGroupBox, wxID_ANY, "deg", wxPoint(158, 30), wxSize(25, 13));
    deg1->SetFont(*font);
    wxStaticText *deg2 = new wxStaticText(linearMatPropGroupBox, wxID_ANY, "deg", wxPoint(376, 30), wxSize(25, 13));
    deg2->SetFont(*font);
    relativeUxTextCtrl->Create(linearMatPropGroupBox, magneticBlockPropertyDiag::ID_TextControl2, std::to_string(_magneticMaterial.getMUrX()), wxPoint(75, 0), wxSize(77, 20));
    relativeUxTextCtrl->SetFont(*font);
    phiXTextCtrl->Create(linearMatPropGroupBox, magneticBlockPropertyDiag::ID_TextControl3, std::to_string(_magneticMaterial.getPhiX()), wxPoint(75, 27), wxSize(77, 20));
    phiXTextCtrl->SetFont(*font);
    relativeUyTextCtrl->Create(linearMatPropGroupBox, magneticBlockPropertyDiag::ID_TextControl4, std::to_string(_magneticMaterial.getMUrY()), wxPoint(293, 0), wxSize(77, 20));
    relativeUyTextCtrl->SetFont(*font);
    phiYTextCtrl->Create(linearMatPropGroupBox, magneticBlockPropertyDiag::ID_TextControl5, std::to_string(_magneticMaterial.getPhiY()), wxPoint(293, 27), wxSize(77, 20));
    phiYTextCtrl->SetFont(*font);
    
    /* This second part is for the nonlinear properties */
    nonlinearGroupBox->SetFont(*font);
    nonlinearGroupBox->Create(this, wxID_ANY, "Nonlinear Material Properties", wxPoint(5, 149), wxSize(409, 49));
    wxStaticText *phiMaxLabel = new wxStaticText(nonlinearGroupBox, wxID_ANY, wxT("ϕ hmax:"), wxPoint(214, 5), wxSize(52, 13));
    phiMaxLabel->SetFont(*font);
    wxStaticText *deg3 = new wxStaticText(nonlinearGroupBox, wxID_ANY, "deg", wxPoint(376, 5), wxSize(25, 13));
    deg3->SetFont(*font);
    editBHCurve->Create(nonlinearGroupBox, wxID_EDIT, "Edit B-H Curve", wxPoint(6, 0), wxSize(157, 23));
    editBHCurve->SetFont(*font);
    phiMaxTextCtrl->Create(nonlinearGroupBox, magneticBlockPropertyDiag::ID_TextControl6, std::to_string(_magneticMaterial.getPhiMax()), wxPoint(293, 0), wxSize(77, 20));
    phiMaxTextCtrl->SetFont(*font);
    
    /* This part is for the coercivity section */
    coercivityGroupBox->SetFont(*font);
    coercivityGroupBox->Create(this, wxID_ANY, "Coercivity", wxPoint(5, 204), wxSize(176, 40));
    coercivityTextCtrl->Create(coercivityGroupBox, magneticBlockPropertyDiag::ID_TextControl7, std::to_string(_magneticMaterial.getCoercivity()), wxPoint(75, 0), wxSize(77, 20));
    coercivityTextCtrl->SetFont(*font);
    wxStaticText *HcLabel = new wxStaticText(coercivityGroupBox, wxID_ANY, "Hc, A/m2", wxPoint(6, 4), wxSize(56, 13));
    HcLabel->SetFont(*font);
    
    /* This part is for the electrical conductivity */
    eConductivityGroupBox->SetFont(*font);
    eConductivityGroupBox->Create(this, wxID_ANY, "Electrical Conductivity", wxPoint(204, 204), wxSize(210, 40));
    wxStaticText *sigLabel = new wxStaticText(eConductivityGroupBox, wxID_ANY, wxT("σ MS/m:"), wxPoint(15, 4), wxSize(55, 13));
    sigLabel->SetFont(*font);
    eConductivityTextCtrl->Create(eConductivityGroupBox, magneticBlockPropertyDiag::ID_TextControl8, std::to_string(_magneticMaterial.getSigma()), wxPoint(94, 0), wxSize(77, 20));
    eConductivityTextCtrl->SetFont(*font);
    
    /* This part is for the source current density */
    sourceCurrentGroupBox->SetFont(*font);
    sourceCurrentGroupBox->Create(this, wxID_ANY, "Source Current Density", wxPoint(5, 250), wxSize(409, 45));
    wxStaticText *currentDesnityLabel = new wxStaticText(sourceCurrentGroupBox, wxID_ANY, "J MA/m2:", wxPoint(7, 4), wxSize(56, 13));
    currentDesnityLabel->SetFont(*font);
    currentDesnityTextCtrl->Create(sourceCurrentGroupBox, magneticBlockPropertyDiag::ID_TextControl9, std::to_string(_magneticMaterial.getCurrentDensity()), wxPoint(75, 0), wxSize(295, 20));
    currentDesnityTextCtrl->SetFont(*font);
    
    /* This part is for the Special Attributes */
    specialAttriGroupBox->SetFont(*font);
    specialAttriGroupBox->Create(this, wxID_ANY, "Special Attributes: Lamination and Wire Type", wxPoint(5, 301), wxSize(410, 100));
    specialAttriComboBox->Create(specialAttriGroupBox,  generalFrameButton::ID_ComboBox2, wxEmptyString, wxPoint(10, 0), wxSize(361, 21), *LamWireSettingsArray);
    specialAttriComboBox->SetFont(*font);
    wxStaticText *lamThickLabel = new wxStaticText(specialAttriGroupBox, wxID_ANY, "Lam. Thickness:", wxPoint(6, 34), wxSize(82, 13));
    lamThickLabel->SetFont(*font);
    wxStaticText *numTurnsLabel = new wxStaticText(specialAttriGroupBox, wxID_ANY, "Number of Turns:", wxPoint(6, 60), wxSize(89, 13));
    numTurnsLabel->SetFont(*font);
    wxStaticText *lamFillLabel = new wxStaticText(specialAttriGroupBox, wxID_ANY, "Lam. Fill Factor:", wxPoint(209, 34), wxSize(78, 13));
    lamFillLabel->SetFont(*font);
    wxStaticText *strandDiaLabel = new wxStaticText(specialAttriGroupBox, wxID_ANY, "Strand Dia:", wxPoint(210, 60), wxSize(60,13));
    strandDiaLabel->SetFont(*font);
    wxStaticText *mmLabel1 = new wxStaticText(specialAttriGroupBox, wxID_ANY, "mm", wxPoint(184, 34), wxSize(23, 13));
    mmLabel1->SetFont(*font);
    wxStaticText *mmLabel2 = new wxStaticText(specialAttriGroupBox, wxID_ANY, "mm", wxPoint(374, 60), wxSize(23, 13));
    mmLabel2->SetFont(*font);
    lamThickTextCtrl->Create(specialAttriGroupBox, magneticBlockPropertyDiag::ID_TextControl10, std::to_string(_magneticMaterial.getLaminationThickness()), wxPoint(101, 31), wxSize(77, 20));
    lamThickTextCtrl->SetFont(*font);
    numStrandsTextCtrl->Create(specialAttriGroupBox, magneticBlockPropertyDiag::ID_TextControl11, std::to_string(_magneticMaterial.getNumberStrands()), wxPoint(101, 56), wxSize(77, 20));
    numStrandsTextCtrl->SetFont(*font);
    lamFFTextCtrl->Create(specialAttriGroupBox, magneticBlockPropertyDiag::ID_TextControl12, std::to_string(_magneticMaterial.getLaminationFillFactor()), wxPoint(293, 31), wxSize(77, 20));
    lamFFTextCtrl->SetFont(*font);
    strandDiaTextCtrl->Create(specialAttriGroupBox, magneticBlockPropertyDiag::ID_TextControl13, std::to_string(_magneticMaterial.getStrandDiameter()), wxPoint(293, 56), wxSize(77, 20));
    strandDiaTextCtrl->SetFont(*font);

    wxButton *okButton = new wxButton(this, wxID_OK, "Ok", wxPoint(258, 407), wxSize(75, 23));
    okButton->SetFont(*font);
    
    wxButton *cancelButton = new wxButton(this, wxID_CANCEL, "Cancel", wxPoint(339, 407), wxSize(75, 23));
    cancelButton->SetFont(*font);

    this->Fit();
    this->SetMinSize(this->GetSize());
    this->SetMaxSize(this->GetSize());
}
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: WxDialog creating different size

Post by philm »

So for those wondering, I measured the size of the dialog and it comes out to be about 280 pixels (I used Kruler). That is not counting the top border/header. It would be much longer. This measurement is the height of the window. It should be what i have posted in the constructor.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: WxDialog creating different size

Post by catalin »

philm wrote:Oh so the sizers are used to prevent random resizing of the window?
No. They are used "for laying out subwindows in a window". You should read Sizers Overview and wxSizer docs. Many of the samples use sizers as well.
philm wrote:My dialog, I make sure that the user is unable to resize it.
That is completely irrelevant for the initial layout.
philm wrote:What do you mean in your ps? I would have to initilize the dialog to some eplicit size?
I mean, use wxDefaultSize.

After you will have built the layout using sizers you will no longer need to count pixels and measure sizes. The sizers will do it for you.
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: WxDialog creating different size

Post by coderrc »

philm wrote: Also, there is another wxDialog that I used Fit() and the results are the same.
I have had this problem in the past. *generally* it is solved by using SetInitialSizeAndFit(), otherwise it is because i goof'd up an expansion setting somewhere in the xrc.
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: WxDialog creating different size

Post by philm »

Hello coderrc,

Your tip seems to be working on one of my windows, thank you.

For another window, it doesn't seem to be working. Could you explain a little bit what you mean by the xrc? I have not come across this.
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: WxDialog creating different size

Post by coderrc »

philm wrote:Hello coderrc,
Could you explain a little bit what you mean by the xrc? I have not come across this.
The xrc is the special wxWidegets xml file that holds all the GUI information so you can focus entirely on logic and implementation in your c++ code rather than wading through thousands of lines of GUI layout. Plus you can change the UI without having to recompile which is great.

The mistake i made (with embarrasing frequency) was to assign the "stretch factor" to the containing sizer rather than the contained elements.
EG if my layout was something like

Code: Select all

-frame
   |___sizer1
         |___panel1
	           |___sizer2
I might accidently set sizer1 to have a stretchfactor of 1 rather than assigning that stretchfactor to panel1. #-o The result of course is a layout that never quite has the right size, no matter what you do.

ETA:
on ubuntu anyway. Windows always seems to know what I meant to do, even when I was doing it wrong lol.
philm
Experienced Solver
Experienced Solver
Posts: 88
Joined: Fri Dec 18, 2015 4:46 am

Re: WxDialog creating different size

Post by philm »

catalin wrote:
philm wrote:Oh so the sizers are used to prevent random resizing of the window?
No. They are used "for laying out subwindows in a window". You should read Sizers Overview and wxSizer docs. Many of the samples use sizers as well.
philm wrote:My dialog, I make sure that the user is unable to resize it.
That is completely irrelevant for the initial layout.
philm wrote:What do you mean in your ps? I would have to initilize the dialog to some eplicit size?
I mean, use wxDefaultSize.

After you will have built the layout using sizers you will no longer need to count pixels and measure sizes. The sizers will do it for you.
Hello Catalin,

I followed your advice and I looked up about sizers. I as able to correctly implement them on one of my dialogs which seems to be giving me what I am looking for. I am attempting to implement sizers on some of my other dialogs to see if the issue goes away, thank you for the tip.
Post Reply