I created a global panel and called a method that creates a sizer and a button. The button clears the sizer (i.e., also the panel), and then deletes is. Then, another method is called, using the same logic, it creates another sizer and another button. This time they don't work.
my code(windows, vs studio):
#include "MainFrame.h"
#include <wx/wx.h>s
MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title)
{
panel = new wxPanel(this);
StartParty(panel);
}
void MainFrame::ClearButtonClicked(wxCommandEvent& evt)
{
panel->GetSizer()->Clear(true);
panel->SetSizerAndFit(nullptr);
ChooseMode(panel);
}
void MainFrame::StartParty(wxPanel* parent)
{
wxButton* start_button = new wxButton(parent, wxID_ANY, "Start the Party!", wxDefaultPosition, wxSize(200, 70));
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->AddStretchSpacer(1);
sizer->Add(
start_button,
0,
wxALL | wxALIGN_CENTER,
0);
sizer->AddStretchSpacer(1);
parent->SetSizerAndFit(sizer);
start_button->Bind(wxEVT_BUTTON, &MainFrame::ClearButtonClicked, this);
}
void MainFrame::ChooseMode(wxPanel* parent)
{
wxButton* select_button = new wxButton(parent, wxID_ANY, "Choose", wxDefaultPosition, wxSize(200, 70));
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
sizer->AddStretchSpacer(1);
sizer->Add(
select_button,
0,
wxALL | wxALIGN_CENTER,
0);
sizer->AddStretchSpacer(1);
parent->SetSizer(sizer);
}
You do need Layout(), as mentioned in the comments, as this is what actually repositions the windows -- SetSizer() just specifies the sizer to use for doing it, but doesn't do anything on its own immediately (it will when the window is resized the next time, as this results in a call to Layout()).
However, even if it's somewhat unrelated to the question itself, I think you shouldn't be doing this at all and use wxSimplebook instead. This simple (sic) class allows you to add a few pages to it and then easily switch between them.
Related
I have a frame which has 4 textboxes and one link(change no of textboxes). when user clicks on link I am hiding 4th textbox. but what happens is after hiding 4th textbox, it's space remains blank. I don't want to keep its space blank. That space should disappear and frame should get resized. Is there any way that I can achieve this? when user again clicks on the link(change no of textboxes I am again displaying 4th textbox.
code:
m_ComputerNameText1 = new wxTextCtrl(m_panel, wxID_ANY, "computerName", wxDefaultPosition, wxSize(250, wxDefaultSize.GetHeight()));
m_ComputerNameText2 = new wxTextCtrl(m_panel, wxID_ANY, "computerName1", wxDefaultPosition, wxSize(250, wxDefaultSize.GetHeight()));
m_ComputerNameText3 = new wxTextCtrl(m_panel, wxID_ANY, "computerName3", wxDefaultPosition, wxSize(250, wxDefaultSize.GetHeight()));
m_ComputerNameText4 = new wxTextCtrl(m_panel, wxID_ANY, "computerName4", wxDefaultPosition, wxSize(250, wxDefaultSize.GetHeight()));
m_hyperLinkOption = new wxHyperlinkCtrl(m_panel, wxID_ANY, "Change no of textboxes", wxT(""), wxDefaultPosition, wxSize(135, wxDefaultSize.GetHeight()));
m_userPassSizer->Add(m_ComputerNameText1, 0, wxALIGN_LEFT | wxALL, 0);
m_userPassSizer->Add(m_ComputerNameText2, 0, wxALIGN_LEFT | wxALL, 0);
m_userPassSizer->Add(m_ComputerNameText3, 0, wxALIGN_LEFT | wxALL, 0);
m_userPassSizer->Add(m_ComputerNameText4, 0, wxALIGN_LEFT | wxALL, 0);
m_userPassSizer->Add(m_hyperLinkOption , 0, wxALIGN_LEFT | wxALL, 0);
Following function executes when hyperlink is clicked.
void OnAuthCodeOptionLinkClicked(wxHyperlinkEvent& event)
{
if (!m_hyperlinkOptionSelected)
{
m_hyperlinkOptionSelected= true;
m_ComputerNameText4->Hide();
m_hyperLinkOption->SetLabel("Go back");
}
else
{
m_hyperlinkOptionSelected= false;
m_ComputerNameText4->Show();
m_hyperLinkOption->SetLabel("change no of textboxes");
}
}
in this way I have to hide and show 4th textbox. After hiding some blank space remains. How can I avaoid that blank space and take link in the place of 4th textbox ?
Whenever I need to dynamically update the size of a control, I always call Layout() and Refresh().
You can find more info about Layout here:
http://docs.wxwidgets.org/3.1/classwx_window.html#a1b143c3e72bd0af533b76db4830a6113
As well as some good info on window sizing in general here:
http://docs.wxwidgets.org/3.1/overview_windowsizing.html
Calling Layout assumes that your class where you construct and manage your textboxes, etc. is a wxWindow derived control itself (like a panel, frame, whatever). If not, like you are making the changes on a view-model, you will need to call Layout and Refresh on the owning window:
void UpdateView()
{
view->Layout();
view->Refresh();
}
You may need to play around with your sizers, minimum size of windows, etc. to get the exact behavior you want.
I have 2 wxDialogs with the button which makes each of them top most dialog(wxSTAY_ON_TOP) after button click.
Workflow:
I make one of them top most, checked - dialog become top most.
I clicked once more and dialog become not top most, checked - ok.
Made both of them top most, checked - ok, both top most.
Made one of them not top most after step 3, checked - accordinly to logs 1 - top most, another is not top most, but if I click somewhere both behaves like not top most. I have tried with wxFrame, with more than 2 wxDialogs - result same. Maybe I missing something or threre is a bug. Please help.
wxWidgets 3.0.2, Windows
Simple frame which owns dialogs
class Frame : public wxFrame
{
public:
Frame()
: wxFrame(nullptr, wxID_ANY, wxEmptyString, wxPoint(1100, 300), wxSize(200, 200))
{
wxBoxSizer* sz = new wxBoxSizer(wxVERTICAL);
wxStaticText* text = new wxStaticText(this, wxID_ANY, "Parent Frame");
sz->Add(text, 0, wxALL, 5);
SetSizer(sz);
Layout();
}
};
Dialog class
class Dialog : public wxDialog
{
public:
Dialog(wxWindow* parent, wxPoint pos)
: wxDialog(parent, wxID_ANY, "", pos, wxSize(200, 200),
wxFRAME_NO_TASKBAR | wxTAB_TRAVERSAL | wxBORDER_NONE)
{
wxBoxSizer* mainS = new wxBoxSizer(wxVERTICAL);
wxButton* button = new wxButton(this, wxID_ANY, "Top Most");
mainS->Add(button, 0, wxALL, 5);
SetSizer(mainS);
Layout();
button->Bind(wxEVT_BUTTON, &Dialog::onButton, this);
}
void onButton(wxCommandEvent&)
{
bool isTopMost = (GetWindowStyle() & wxSTAY_ON_TOP) != 0;
if (isTopMost) {
// Makes not top most dynamically
SetWindowStyle(GetWindowStyle() & ~wxSTAY_ON_TOP);
}
else {
// Makes top most dynamically
SetWindowStyle(GetWindowStyle() | wxSTAY_ON_TOP);
}
}
};
Calling sample
class WxguiApp : public wxApp
{
public:
bool OnInit() override
{
Frame* mainWnd = new Frame();
mainWnd->Show();
SetTopWindow(mainWnd);
Dialog* dlg1 = new Dialog(mainWnd, wxPoint(500, 300));
dlg1->Show();
Dialog* dlg2 = new Dialog(mainWnd, wxPoint(800, 300));
dlg2->Show();
return true;
}
};
IMPLEMENT_APP(WxguiApp);
I have an issue with spacer inside of the inner wxBoxSizer.
Here is the sample code.
class Frame : public wxFrame
{
public:
Frame()
: wxFrame(nullptr,
wxID_ANY,
wxEmptyString,
wxDefaultPosition,
wxSize(600, 200))
{
wxPanel* panel = new wxPanel(this);
// Some OUTER SIZER
wxBoxSizer* mainS = new wxBoxSizer(wxVERTICAL);
// INNER HORIZONTAL SIZER
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
wxStaticText* text = new wxStaticText(panel, wxID_ANY, wxT("Some Text"));
sizer->Add(text, 0, wxALL, 5);
wxComboBox* comboBox = new wxComboBox(panel, wxID_ANY, wxT("Combo"));
sizer->Add(comboBox, 0, wxALL, 5);
// SPACER
sizer->Add(0, 0, 1, wxEXPAND, 5);
wxButton* button = new wxButton(panel, wxID_ANY, wxT("Some button"));
sizer->Add(button, 0, wxALL, 5);
mainS->Add(sizer, 0, wxALL, 5);
panel->SetSizer(mainS);
// PANEL SIZER
wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
panelSizer->Add(panel, 1, wxEXPAND, 5);
SetSizer(panelSizer);
Layout();
Centre(wxBOTH);
}
};
class WxguiApp
: public wxApp
{
public:
bool OnInit() override
{
Frame* w = new Frame();
w->Show();
SetTopWindow(w);
return true;
}
};
IMPLEMENT_APP(WxguiApp);
If I remove outer sizer spacer starts working fine. Is it bug in my code? Or maybe some issue with wxWidgets?
I couldn't find answer, maybe someone help? wxWidgets 3.0.2
Your inner sizer (sizer) won't expand in the horizontal direction because you didn't tell its parent sizer (mainS) to do it when adding it, so your spacer will always be 0 pixels wide, which is its initial and minimal width.
To fix this, you just need to change the mainS->Add(sizer, 0, wxALL, 5) line to use wxALL | wxEXPAND.
Additionally, I strongly encourage you to look at using wxSizerFlags instead of the cryptic syntax shown above. And, last and really least, but still worth mentioning, prefer using much more readable AddStretchSpacer() to the confusing Add(0, 0, 1, wxEXPAND, 5) call (it's confusing because expanding your spacer doesn't do anything and border size is not taken into account without any border flags).
The wxWidgets hello world tutorial exhibits a strange behavior. As soon as I add a panel to the application using the following line to the MyFrame constructor:
wxPanel *panel = new wxPanel(this);
The [Return] and [Keypad Enter] keys cause the program to exit (Clean Close event detected).
Why is this? Without the wxPanel, the keys do nothing.
Here is the code with the added line:
// hworld.cpp
// Version using dynamic event routing
#include <wx/wx.h>
class MyApp : public wxApp
{
virtual bool OnInit();
};
IMPLEMENT_APP(MyApp)
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
};
enum
{
ID_Quit=1,
ID_About
};
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450, 350));
frame->Connect( ID_Quit, wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) &MyFrame::OnQuit );
frame->Connect( ID_About, wxEVT_COMMAND_MENU_SELECTED,
(wxObjectEventFunction) &MyFrame::OnAbout );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenuBar *menuBar = new wxMenuBar;
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
menuBar->Append(menuFile, _("&File") );
// Added line causing failure.
wxPanel *panel = new wxPanel(this);
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("wxWidgets Hello World example."),
_("About Hello World"),
wxOK|wxICON_INFORMATION, this );
}
It's probably crashing....
I know this already but check here: http://docs.wxwidgets.org/trunk/classwx_panel.html
It'd mention it is panels did that on purpose.
Any wxWindow can use a wxSizer, this is a thing that lays stuff out (like box sizers and stuff).
While the parent of a wxWindow is the thing it is in (unless it is top-level), it must go in a sizer inside the parent.
you want something like:
wxSizer* mySizer = new wxBoxSizer(WX_VERTICAL);
setSizer(mySizer);
wxWindow newWindow = new wxPanel(this);
mySizer->Add(newWindow,1,WX_EXPAND);
the 1 is the ratio of extra space this window will get of the total (in this case 1), in the direction of the sizer (vertically)
the expand flag means "fill up your space in the axis the sizer isn't" in this case horizontally. So it will fill up all the space the sizer gives it.
If you have a horizontal sizer (puts things across) with 2 buttons, if one has weight 1 and the other 2, the one with the 2 weight will get 2/3rds of whatever space the sizer has available but doesn't use.
Each window has a desired size, a size it wants to be at least, if we have 300px and the buttons only want 100 each, the weight 2 one will get 66px extra.
You get the idea.
My names here (WX_EXPAND) could be wrong, I used wxPython first (wx.EXPAND) then came to wxWidgets, I mainly use the IDE to get the names right.
If you are looking to learn wx, do consider wxPython.
It's behaving now, but it's a big of a guess as to why. The program was never crashing. I traced it in debug and the close event was being called.
This closes when [Enter] is pressed:
wxPanel *panel = new wxPanel(this);
As do all of these:
wxPanel *panel = new wxPanel(this, wxID_ANY);
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition);
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
This however doesn't close with [Enter] is pressed:
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND);
Nor do these close:
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND);
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND | wxCLIP_CHILDREN);
But this closes again:
wxPanel *panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxEXPAND | wxCLIP_CHILDREN | wxTAB_TRAVERSAL);
Apparently if wxTAB_TRAVERSAL is involved in the style (it's the default for the constructor BTW) it will propagate your [Enter] keypress to the wxFrame and click the [X] close button.
The only thing even close to a hint is a cryptic and seemingly irrelevant note in the documentation:
Tab traversal is implemented through an otherwise undocumented intermediate wxControlContainer class from which any class can derive in addition to the normal wxWindow base class. Please see wx/containr.h and wx/panel.h to find out how this is achieved.
if not all characters are being intercepted by your OnKeyDown or OnChar handler, it may be because you are using the wxTAB_TRAVERSAL style, which grabs some keypresses for use by child controls.
This is my best guess. I don't know if this is intentional behavior or a bug.
I've spent the past day figuring out how to get a wxRibbon bar. It took some work, but I finally got a ribbon bar working. Except that the ribbon bar takes up the entire window, except for a tiny space on the right side of my application.
The code I'm using to test in my main window is as follow:
Win_app::Win_app(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(640, 480))
{
wxRibbonBar* mainMenu = new wxRibbonBar(this,-1,wxDefaultPosition,wxSize(20,40));
wxRibbonPage* home = new wxRibbonPage(mainMenu, wxID_ANY, wxT("Home"));
wxRibbonPanel *test_panel1 = new wxRibbonPanel(home, wxID_ANY, wxT("Panel 1"),
wxNullBitmap, wxDefaultPosition, wxSize(320,60));
wxRibbonPanel *test_panel2 = new wxRibbonPanel(home, wxID_ANY, wxT("Panel 2"),
wxNullBitmap, wxDefaultPosition, wxSize(320,60));
wxRibbonPage* page = new wxRibbonPage(mainMenu, wxID_ANY, wxT("Another Page"));
wxRibbonPanel *test_panel3 = new wxRibbonPanel(page, wxID_ANY, wxT("Panel 3"),
wxNullBitmap, wxDefaultPosition, wxSize(640,60));
mainMenu->Realize();
Centre();
}
I'm using wxWidgets 2.9.4. Any help would be much appreciated! Thanks for reading.
I would suggest taking a look at the ribbon sample in your wxWidgets install located at wxdir\samples\ribbon. It looks to be doing roughly the same as your code with a key difference in that it adds the wxRibbonBar to a sizer:
wxSizer *s = new wxBoxSizer(wxVERTICAL);
s->Add(m_ribbon, 0, wxEXPAND);
s->Add(m_logwindow, 1, wxEXPAND);
s->Add(m_togglePanels, wxSizerFlags().Border());
SetSizer(s);
You need to do this because if there is a single control on a dialog it automatically fills the dialog, which in your case is unwanted behaviour.