How to size wxWindows properly - c++

I have a wxWidgets C++ program made in Code::blocks. On the desktop PC it looks fine, but not on windows tablets.
The following picture is what it should look like (at least all of the text should be visible):
I already tried several ways, at the moment my code looks like this:
TrackDialog::TrackDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
const wxSize& size)
{
wxBoxSizer* BoxSizer4;
wxBoxSizer* BoxSizer5;
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
wxBoxSizer* BoxSizer3;
wxBoxSizer* pPanelSizer;
Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSTAY_ON_TOP|wxSUNKEN_BORDER, _T("wxID_ANY"));
SetMinSize(wxSize(500,400));
Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(184,136), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
pPanelSizer = new wxBoxSizer(wxVERTICAL);
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
StaticSelectText = new wxStaticText(Panel1, ID_STATICTEXT1, _("Select a *.xml file."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer1->Add(StaticSelectText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
XmlFilePickerCtrl = new wxFilePickerCtrl(Panel1, ID_FILEPICKERCTRL1, _T("./resources/"), wxEmptyString, _T("*.xml"), wxDefaultPosition, wxDefaultSize, wxFLP_FILE_MUST_EXIST|wxFLP_OPEN|wxFLP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_FILEPICKERCTRL1"));
BoxSizer2->Add(XmlFilePickerCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
StaticNumberText = new wxStaticText(Panel1, ID_STATICTEXT2, _("Tracknumber"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
StaticNumberText->Disable();
BoxSizer3->Add(StaticNumberText, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
ChoiceTrack = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
ChoiceTrack->Disable();
BoxSizer4->Add(ChoiceTrack, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
ButtonOk = new wxButton(Panel1, ID_BUTTON1, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer5->Add(ButtonOk, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel1->SetSizer(pPanelSizer);
pPanelSizer->Fit(Panel1);
pPanelSizer->SetSizeHints(Panel1);
Center();
Connect(ID_FILEPICKERCTRL1,wxEVT_COMMAND_FILEPICKER_CHANGED,(wxObjectEventFunction)&TrackDialog::OnXmlFilePickerCtrlFileChanged);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TrackDialog::OnButtonOkClick);
//*)
Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&TrackDialog::OnChoiceTrackSelect1);
SetSize(DoGetBestClientSize());
Fit();
}
Additionally, another dialog is way too big:
with this code:
PortsDialog::PortsDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
const wxSize& size)
{
//(*Initialize(PortsDialog)
wxBoxSizer* BoxSizer4;
wxBoxSizer* BoxSizer5;
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
wxBoxSizer* BoxSizer3;
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("id"));
SetClientSize(wxDefaultSize);
Move(wxDefaultPosition);
Panel1 = new wxPanel(this, ID_PANEL1, wxPoint(200,320), wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
BoxSizer1 = new wxBoxSizer(wxVERTICAL);
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
StaticComText = new wxStaticText(Panel1, ID_STATICTEXT1, _("COM Ports"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer2->Add(StaticComText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
TextCtrlVibroTacPort = new wxTextCtrl(Panel1, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
BoxSizer3->Add(TextCtrlVibroTacPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
ButtonVibroTacPort = new wxButton(Panel1, ID_BUTTON1, _("Set VibroTac Port"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer3->Add(ButtonVibroTacPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
TextCtrlGnssPort = new wxTextCtrl(Panel1, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
BoxSizer4->Add(TextCtrlGnssPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
ButtonGnssPort = new wxButton(Panel1, ID_BUTTON2, _("Set GNSS Port"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
BoxSizer4->Add(ButtonGnssPort, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
ButtonClose = new wxButton(Panel1, ID_BUTTON3, _("Close"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
BoxSizer5->Add(ButtonClose, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer1->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel1->SetSizer(BoxSizer1);
BoxSizer1->Fit(Panel1);
BoxSizer1->SetSizeHints(Panel1);
Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&PortsDialog::OnButtonVibroTacPortClick);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonVibroTacPortClick);
Connect(ID_TEXTCTRL2,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&PortsDialog::OnButtonGnssPortClick);
Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonGnssPortClick);
Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&PortsDialog::OnButtonCloseClick);
//*)
SetSize(DoGetBestClientSize());
Fit();
}
I've seen many different methods to set the size, but until now I could not figure out a way that sets the size so that everything looks fine.
Thank you for your help.
Edit:
SetSizerAndFit(BoxSizer1); with the PortsDialog as Tibo suggested leads to the dialog being empty:
With SetSizerAndFit(pPanelSizer); in TrackDialog, it looks fine on start, but if I reload the text half of it is out of the window again and the program crashes if the window is closed... This is so confusing.
Concerning the version: I have Windows 10 Pro 64-bit version on all of my devices (tablet and PC) and I use wxWidgets v3.0.2 and Code::Blocks v13.12 (with GCC compiler following C++11 ISO)

It's really not clear what this question is about, there seem to be several completely unrelated things here and I'd advise closing/deleting this one and asking each of them in separate new submissions.
But to try to answer the first question, the text is not fully visible because there is not enough space for it on the tablet. It won't wrap on its own, you need to use a control with support for wrapping or call Wrap() yourself on wxStaticText manually.

Related

Scaling font as window is resized

I'm writing a C++ calculator application in wxWidgets.
I want the font of all the buttons and the two wxTextCtrl's to be scaled as I resize the window. How is that done? I'm posting some code if that can help.
text_controls.cpp
#include "main.h"
void Main::AddTextControls()
{
//creazione font
wxFont Expression(10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_LIGHT, false, "Lato");
wxFont Main(30, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_LIGHT, false, "Lato");
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
ExpText = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxNO_BORDER);
ExpText->SetFont(Expression);
ExpText->SetForegroundColour(wxColour(125, 125, 125));
sizer->Add(ExpText, 1, wxEXPAND);
MainText = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxNO_BORDER);
MainText->SetFont(Main);
MainText->SetForegroundColour(wxColour(55, 55, 55));
MainText->Bind(wxEVT_TEXT, &Main::OnTextChange, this);
sizer->Add(MainText, 2, wxEXPAND);
MainSizer->Add(sizer, 4, wxEXPAND);
}
You could use wxFont::SetPixelSize() to set the font size to, say, one third of the text control height. E.g.:
MainText->Bind(wxEVT_SIZE, [this](wxSizeEvent& e) {
e.Skip();
MainText->SetFont(wxFontInfo(wxSize(0, MainText->GetSize().y / 3)).Family(wxFONTFAMILY_SWISS).FaceName("Lato").Light());
});
(which also shows using a more readable way of creating a wxFont than the ctor taking a zillion arguments).

How to get text entered in wxTextCtrl and assign it to a string variable?

I have the following GUI created in wxFormBuilder:
gui.h:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#pragma once
#include <wx/artprov.h
#include <wx/xrc/xmlres.h
#include <wx/intl.h
#include <wx/string.h
#include <wx/stattext.h
#include <wx/gdicmn.h
#include <wx/font.h
#include <wx/colour.h
#include <wx/settings.h
#include <wx/textctrl.h
#include <wx/sizer.h
#include <wx/statline.h
#include <wx/bitmap.h
#include <wx/image.h
#include <wx/icon.h
#include <wx/button.h
#include <wx/dialog.h
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/// Class MainDialogBase
///////////////////////////////////////////////////////////////////////////////
class MainDialogBase : public wxDialog
{
private:
protected:
wxStaticText* m_staticText3;
wxStaticText* m_staticText1;
wxTextCtrl* m_textCtrl1;
wxStaticText* m_staticText4;
wxTextCtrl* m_textCtrl2;
wxStaticText* m_staticText6;
wxStaticText* m_staticText5;
wxTextCtrl* m_textCtrl3;
wxStaticText* m_staticText7;
wxTextCtrl* m_textCtrl4;
wxStaticLine* m_staticLine;
wxButton* m_button1;
wxButton* m_button2;
// Virtual event handlers, overide them in your derived class
virtual void OnCloseDialog( wxCloseEvent& event ) { event.Skip(); }
public:
MainDialogBase( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 400,300 ), long style = wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE );
~MainDialogBase();
};
gui.cpp:
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#include "gui.h"
///////////////////////////////////////////////////////////////////////////
MainDialogBase::MainDialogBase( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, >pos, size, style )
{
this->SetSizeHints( wxSize( -1,-1 ), wxDefaultSize );
wxBoxSizer* mainSizer;
mainSizer = new wxBoxSizer( wxVERTICAL );
wxBoxSizer* bSizer8;
bSizer8 = new wxBoxSizer( wxHORIZONTAL );
wxBoxSizer* bSizer2;
bSizer2 = new wxBoxSizer( wxVERTICAL );
m_staticText3 = new wxStaticText( this, wxID_ANY, _("Patient's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
m_staticText3->Wrap( -1 );
m_staticText3->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );
bSizer2->Add( m_staticText3, 0, wxALL, 5 );
wxBoxSizer* bSizer3;
bSizer3 = new wxBoxSizer( wxHORIZONTAL );
m_staticText1 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText1->Wrap( -1 );
bSizer3->Add( m_staticText1, 0, wxALL, 5 );
m_textCtrl1 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer3->Add( m_textCtrl1, 0, wxALL, 5 );
bSizer2->Add( bSizer3, 1, wxEXPAND, 5 );
wxBoxSizer* bSizer4;
bSizer4 = new wxBoxSizer( wxHORIZONTAL );
m_staticText4 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText4->Wrap( -1 );
bSizer4->Add( m_staticText4, 0, wxALL, 5 );
m_textCtrl2 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer4->Add( m_textCtrl2, 0, wxALL, 5 );
bSizer2->Add( bSizer4, 1, wxEXPAND, 5 );
bSizer8->Add( bSizer2, 1, 0, 5 );
wxBoxSizer* bSizer5;
bSizer5 = new wxBoxSizer( wxVERTICAL );
m_staticText6 = new wxStaticText( this, wxID_ANY, _("Doctor's data"), wxDefaultPosition, wxSize( 105,-1 ), 0 );
m_staticText6->Wrap( -1 );
m_staticText6->SetFont( wxFont( 10, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Sans") ) );
bSizer5->Add( m_staticText6, 0, wxALL, 5 );
wxBoxSizer* bSizer6;
bSizer6 = new wxBoxSizer( wxHORIZONTAL );
m_staticText5 = new wxStaticText( this, wxID_ANY, _("First Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText5->Wrap( -1 );
bSizer6->Add( m_staticText5, 0, wxALL, 5 );
m_textCtrl3 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer6->Add( m_textCtrl3, 0, wxALL, 5 );
bSizer5->Add( bSizer6, 1, wxEXPAND, 5 );
wxBoxSizer* bSizer7;
bSizer7 = new wxBoxSizer( wxHORIZONTAL );
m_staticText7 = new wxStaticText( this, wxID_ANY, _("Last Name:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText7->Wrap( -1 );
bSizer7->Add( m_staticText7, 0, wxALL, 5 );
m_textCtrl4 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
bSizer7->Add( m_textCtrl4, 0, wxALL, 5 );
bSizer5->Add( bSizer7, 1, wxEXPAND, 5 );
bSizer8->Add( bSizer5, 1, 0, 5 );
mainSizer->Add( bSizer8, 1, wxEXPAND, 5 );
mainSizer->Add( 0, 0, 1, wxEXPAND, 5 );
m_staticLine = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
mainSizer->Add( m_staticLine, 0, wxEXPAND | wxALL, 5 );
m_button1 = new wxButton( this, wxID_ANY, _("Record Data"), wxDefaultPosition, wxDefaultSize, 0 );
mainSizer->Add( m_button1, 0, wxALL|wxEXPAND, 5 );
m_button2 = new wxButton( this, wxID_ANY, _("List"), wxDefaultPosition, wxDefaultSize, 0 );
mainSizer->Add( m_button2, 0, wxALL|wxEXPAND, 5 );
wxBoxSizer* bSizer9;
bSizer9 = new wxBoxSizer( wxHORIZONTAL );
mainSizer->Add( bSizer9, 1, wxEXPAND, 5 );
this->SetSizer( mainSizer );
this->Layout();
this->Centre( wxBOTH );
// Connect Events
this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
}
MainDialogBase::~MainDialogBase()
{
// Disconnect Events
this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( MainDialogBase::OnCloseDialog ) );
}
I also have the following inherited class:
inheritedgui.h:
#ifndef __inheritedgui__
#define __inheritedgui__
/**
#file
Subclass of MainDialogBase, which is generated by wxFormBuilder.
*/
#include "gui.h"
//// end generated include
/** Implementing MainDialogBase */
class inheritedgui : public MainDialogBase
{
protected:
// Handlers for MainDialogBase events.
void OnCloseDialog( wxCloseEvent& event );
public:
/** Constructor */
inheritedgui( wxWindow* parent );
//// end generated class members
};
#endif // __inheritedgui__
inheritedgui.cpp:
#include "inheritedgui.h"
inheritedgui::inheritedgui( wxWindow* parent )
:
MainDialogBase( parent )
{
}
void inheritedgui::OnCloseDialog( wxCloseEvent& event )
{
// TODO: Implement OnCloseDialog
}
Could you please tell me how to get text entered in m_textCtrl1 and assign it to a string variable when m_button1 is pressed?
I'm trying to port a simple program from JavaFX to C++, the program mainly takes user input (from wxTextCtrl controls) and assigns the input to string variables, so then I can show the values stored in the variables in a messagebox when m_button2 is pressed.
I've been doing some google searches, but C++ is getting a bit difficult for me.
m_textCtrl1 is a wxTextCtrl, which has a GetValue() method that returns a wxString, which in turn has a ToStdString() for converting to a std::string:
std::string s = m_textCtrl1->GetValue().ToStdString();
#etcCoder,
If you pan to use messagebox function from wxWidgets you dont need to convert the string to std::string - wxMessageBox() accepts wxString.
Thank you.

Size wxDialogs so all of the text is inside the Dialog

My wxWidgets program should work on both desktop PC and Windows tablet. Both use Windows 10 Pro 64-bit version. How can I make it, that a text with a changing size is inside a dialog no matter where it is displayed?
This is what I do now:
TrackDialog::TrackDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
const wxSize& size)
{
//(*Initialize(TrackDialog)
wxBoxSizer* BoxSizer4;
wxBoxSizer* BoxSizer5;
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
wxBoxSizer* BoxSizer3;
wxBoxSizer* pPanelSizer;
Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSTAY_ON_TOP|wxSUNKEN_BORDER, _T("wxID_ANY"));
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
pPanelSizer = new wxBoxSizer(wxVERTICAL);
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
StaticSelectText = new wxStaticText(Panel1, ID_STATICTEXT1, _("Select a *.xml file."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer1->Add(StaticSelectText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
XmlFilePickerCtrl = new wxFilePickerCtrl(Panel1, ID_FILEPICKERCTRL1, _T("./resources/"), wxEmptyString, _T("*.xml"), wxDefaultPosition, wxDefaultSize, wxFLP_FILE_MUST_EXIST|wxFLP_OPEN|wxFLP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_FILEPICKERCTRL1"));
BoxSizer2->Add(XmlFilePickerCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
StaticNumberText = new wxStaticText(Panel1, ID_STATICTEXT2, _("Tracknumber \n"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
StaticNumberText->Disable();
wxFont StaticNumberTextFont(10,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("Arial"),wxFONTENCODING_DEFAULT);
StaticNumberText->SetFont(StaticNumberTextFont);
BoxSizer3->Add(StaticNumberText, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
ChoiceTrack = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
ChoiceTrack->Disable();
BoxSizer4->Add(ChoiceTrack, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
ButtonOk = new wxButton(Panel1, ID_BUTTON1, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer5->Add(ButtonOk, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel1->SetSizer(pPanelSizer);
pPanelSizer->Fit(Panel1);
pPanelSizer->SetSizeHints(Panel1);
Center();
Connect(ID_FILEPICKERCTRL1,wxEVT_COMMAND_FILEPICKER_CHANGED,(wxObjectEventFunction)&TrackDialog::OnXmlFilePickerCtrlFileChanged);
Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&TrackDialog::OnChoiceTrackSelect1);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TrackDialog::OnButtonOkClick);
//*)
SetSize(DoGetBestClientSize());
Fit();
}
The text is changed from "Track number" to one of the options below:
void TrackDialog::setOutputAndChoice()
{
std::vector<wxString> storedTracks = m_xmlReader.getNamesOfTracks(m_path);
int numberOfTracks = storedTracks.size();
StaticNumberText->Enable();
wxString text;
// Make sure there are tracks stored in the file.
if(numberOfTracks > 0){
text = "There are ";
text += wxString::Format(wxT("%i"), numberOfTracks);
text += " tracks stored in the selected file. Select one."
"\nTrack 1 is the inner track.";
}
else{
text = "Opening file failed. Please chose another"
"file of the right format.";
}
StaticNumberText->SetLabel(text);
}
And this is what it looks like on desktop PC (on the tablet it looks the same, thanks to SetSize(DoGetBestClientSize());)
What can I do so that all of the text is displayed? I already tried to set it in the constructor before I call SetSizerAndFit() but then my program crashes if I want to close it...
Thank you for your help.
Edit: I put a panel in the sizer for the StaticNumberText and the text in it and I am able to display all of the text now, though now there is some nasty space underneath the StaticText:
There has to be a better way to properly layout dialogs with changing text...
Sizers need an action to get into game. User changing the size of the main (or dialog) window is definitely an action.
You can also trigger the action asking the dialog window to fit into its children. Call Fit() at the end of your TrackDialog::setOutputAndChoice()
Maybe the sizer for the StaticText there be inside the sizer for both TextCtrl and Button. Then the sizer for the StaticText will take the width of the TextCtrl.
Better, put the sizer for the StaticText, below and independently of the previous sizer.
The proportion of the StaticText, and the Panel in where is inside, must be set at 0.

wxFrame does not process Tab button

I have a simple wxFrame with 3 buttons. After I press Tab nothing happens. In the forum I found that wxFrame should process Tab button events normally and switch focus between controls. I tried with wxTAB_TRAVERSAL and without it, but look like no result.
Here is my code. wxWidgets 3.0.2. Please, help.
class TabWnd
: public wxFrame
{
public:
TabWnd()
: wxFrame(nullptr,
wxID_ANY,
wxEmptyString,
wxDefaultPosition,
wxDefaultSize,
wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL)
{
wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
wxButton* b1 = new wxButton(this, wxID_ANY, wxT("First"));
sz->Add(b1, 0, wxALL, 5);
wxButton* b2 = new wxButton(this, wxID_ANY, wxT("Second"));
sz->Add(b2, 0, wxALL, 5);
wxButton* b3 = new wxButton(this, wxID_ANY, wxT("Third"));
sz->Add(b3, 0, wxALL, 5);
SetSizer(sz);
Layout();
Centre(wxBOTH);
}
};
class WxguiApp
: public wxApp
{
public:
bool OnInit() override
{
TabWnd* mainWnd = new TabWnd();
mainWnd->Show();
SetTopWindow(mainWnd);
return true;
}
};
IMPLEMENT_APP(WxguiApp);
Try adding a panel between the frame and the buttons like this:
wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
wxPanel* pnl = new wxPanel( this, wxID_ANY );
wxBoxSizer* sz2 = new wxBoxSizer( wxVERTICAL );
wxButton* b1 = new wxButton(pnl, wxID_ANY, wxT("First"));
sz2->Add(b1, 0, wxALL, 5);
wxButton* b2 = new wxButton(pnl, wxID_ANY, wxT("Second"));
sz2->Add(b2, 0, wxALL, 5);
wxButton* b3 = new wxButton(pnl, wxID_ANY, wxT("Third"));
sz2->Add(b3, 0, wxALL, 5);
pnl->SetSizer( sz2 );
sz->Add( pnl, 1, wxEXPAND );
SetSizer(sz);
Layout();
Centre(wxBOTH);

Custom Dialog Event Handling with Event Tables

I pretend to "grab" and process the events: EVT_TEXT from wxTextCtrl and EVT_BUTTON from wxID_APPLY.
I try handling the events with Event Tables and I can't? Why?
The source code:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class Dialog : public wxDialog
{
public:
Dialog(wxWindow *parent, const wxString &title);
private:
void OnNameChange(wxCommandEvent &event);
void OnApply(wxCommandEvent &event);
wxDECLARE_EVENT_TABLE();
wxTextCtrl *m_name;
};
enum
{
ID_NAME = 1
};
Dialog::Dialog(wxWindow *parent, const wxString &title) :
wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
SetSizer(vbox);
wxFlexGridSizer *flexGrid = new wxFlexGridSizer(2, 2, 5, 5);
wxStaticText *label = new wxStaticText(this, wxID_ANY, "&Name:");
flexGrid->Add(label);
m_name = new wxTextCtrl(this, ID_NAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
flexGrid->Add(m_name, 1, wxEXPAND);
label = new wxStaticText(this, wxID_ANY, "&Description:");
flexGrid->Add(label);
m_description = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
flexGrid->Add(m_description, 1, wxEXPAND);
flexGrid->AddGrowableRow(1, 1);
flexGrid->AddGrowableCol(1, 1);
vbox->Add(flexGrid, 1, wxALL | wxEXPAND, 15);
vbox->Add(CreateSeparatedButtonSizer(wxAPPLY | wxCANCEL), 0, wxEXPAND | wxALL, 5);
}
void Dialog::OnNameChange(wxCommandEvent &event)
{
m_description->AppendText("Hello\n");
}
void Dialog::OnApply(wxCommandEvent &event)
{
m_description->AppendText("Apply\n");
}
wxBEGIN_EVENT_TABLE(Dialog, wxDialog)
EVT_TEXT(ID_NAME, Dialog::OnNameChange)
EVT_BUTTON(wxID_APPLY, Dialog::OnApply)
wxEND_EVENT_TABLE()
It's only possible to handle the events dynamically?
Bind(wxEVT_TEXT, &Dialog::OnNameChange, this, m_name->GetId());
The code as shown absolutely should work. It's not self-contained, so I can't test it, but there must be something else not shown here preventing it from working. Please try to make a SSCCE if you still can't isolate the problem yourself.