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);
Related
I am trying to set an image as the background but I have no idea how to do it.
It has been hours. The first lines of code I copied from somebody and they seemed alright but it still doesn't work.
The error that I have appears in a separate window and says it cannot load/find the image.
#include "MainFrame.h"
#include <wx/wx.h>
#include "FrameTwo.h"
#include "wx/custombgwin.h"
#include <wx/dcclient.h>
enum IDs {
BUTTON_ID=2
};
wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_BUTTON(BUTTON_ID, MainFrame::OnLoginClicked)
wxEND_EVENT_TABLE()
MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
wxPanel* panel = new wxPanel(this);
wxClientDC dc(panel);
wxMemoryDC mdc;
int w, h;
w = 600;
h = 600;
dc.GetSize(&w, &h);
wxImage img(wxT("C:\\Users\\ALEX\\Desktop\\background_uno.jpeg"), wxBITMAP_TYPE_JPEG);
wxBitmap cat(img.Scale(w, h, wxIMAGE_QUALITY_HIGH));
mdc.SelectObject(cat);
dc.Blit(0, 0, cat.GetWidth(), cat.GetHeight(), &mdc, 0, 0, wxCOPY, 0);
mdc.SelectObject(wxNullBitmap);
//panel->SetBackgroundColour(wxColour(255, 102, 102)); //light orange-red
//panel->SetBackgroundColour(wxColour(155, 202, 62)); //fresh green
wxButton* login = new wxButton(panel, BUTTON_ID, "Log in", wxPoint(340, 350), wxSize(100, 35));
CreateStatusBar(); //creates a bar in the bottom of the frame
//wxBoxSizer* sizer = new wxBoxSizer(wxALIGN_CENTER_VERTICAL);
//sizer -> Add(login, 1, wxEXPAND | wxALL, 10);
wxButton* exit = new wxButton(panel, wxID_OK, "Exit", wxPoint(340, 390), wxSize(100, 35));
exit->Bind(wxEVT_BUTTON, &MainFrame::OnExitClicked, this);
panel->SetFont(panel->GetFont().Scale(2.5));
wxStaticText* staticText = new wxStaticText(panel, wxID_ANY, "UNO DELUXE", wxPoint(300, 40));
panel->SetFont(panel->GetFont().Scale(0.5));
wxStaticText* username = new wxStaticText(panel, wxID_ANY, "Enter username: ", wxPoint(180, 250));
wxTextCtrl* textCtrl = new wxTextCtrl(panel, wxID_ANY, " ", wxPoint(300, 250), wxSize(250, -1));
wxStaticText* password = new wxStaticText(panel, wxID_ANY, "Enter password: ", wxPoint(180, 290));
wxTextCtrl* textCtrl2 = new wxTextCtrl(panel, wxID_ANY, "", wxPoint(300, 290), wxSize(250, -1), wxTE_PASSWORD);
}
I can't help you with the program not being able to find the image - the image needs to exist either in the application's working folder or on an absolute path.
You can get around this by including the image in the executable. Unfortunately there is no consistent cross-platform way to do this - windows and mac have separate ways of doing this but GTK has no way at all. Here are some suggestions from the wiki for some possible workarounds.
Regardless of the method used to get the image into the application, you should not draw it with a client DC. Instead you should handle the paint event for the panel and draw the image in the handler for that event. Here's an example:
#include <wx/wx.h>
class ImageBgFrame: public wxFrame
{
public:
ImageBgFrame(wxFrame *frame, const wxString& title);
private:
void OnImagePanelPaint(wxPaintEvent&);
void OnExit(wxCommandEvent&);
void CreateScaledBg();
wxPanel* m_imagePanel;
wxImage m_image;
wxBitmap m_scaledBg;
};
ImageBgFrame::ImageBgFrame(wxFrame *frame, const wxString& title)
:wxFrame(frame, wxID_ANY, title)
{
::wxInitAllImageHandlers();
// Try to load the image.
m_image = wxImage("Thinking-of-getting-a-cat.png", wxBITMAP_TYPE_PNG);
if ( !m_image.IsOk() )
{
return;
}
// Create the controls.
m_imagePanel = new wxPanel(this, wxID_ANY);
CreateScaledBg();
wxButton* exit = new wxButton(m_imagePanel, wxID_OK, "Exit");
// Arrang the controls with a sizer.
wxBoxSizer* szr = new wxBoxSizer(wxVERTICAL);
szr->Add(exit,wxSizerFlags().Border(wxALL));
m_imagePanel->SetSizer(szr);
Layout();
// Bind Event Handlers
m_imagePanel->Bind(wxEVT_PAINT, &ImageBgFrame::OnImagePanelPaint, this);
exit->Bind(wxEVT_BUTTON, &ImageBgFrame::OnExit, this);
}
void ImageBgFrame::OnImagePanelPaint(wxPaintEvent&)
{
if ( m_imagePanel->GetSize() != m_scaledBg.GetSize() )
{
CreateScaledBg();
}
wxPaintDC dc(m_imagePanel);
dc.DrawBitmap(m_scaledBg,0,0);
}
void ImageBgFrame::OnExit(wxCommandEvent&)
{
Close();
}
void ImageBgFrame::CreateScaledBg()
{
wxSize sz = m_imagePanel->GetSize();
m_scaledBg = wxBitmap(m_image.Scale(sz.GetWidth(), sz.GetHeight(),
wxIMAGE_QUALITY_NORMAL));
}
class ImageBgApp : public wxApp
{
public:
bool OnInit()
{
ImageBgFrame* frame = new ImageBgFrame(NULL, "Image BG");
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(ImageBgApp);
This assumes that the image "Thinking-of-getting-a-cat.png" is in the application's working folder. On GTK it looks like this:
I've also made a few other changes to the code you gave:
I'm using Bind to connect the event handlers instead of static event tables.
I'm using default sizes for the controls instead of setting the size explicitly and laying out the controls in a sizer instead of setting explicit location.
Both of these changes are a more modern way of doing things.
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.
I am having trouble capturing and handling mouse events within a panel.
Binding mouse events to my main window frame works as expected. However, when I bind events to a child panel, they successfully don't go my frame, but are not correctly handled by my panel. Any help would be appreciated.
I am using wxWidgets v3.1.5
Below is my simplest example: a single panel inside a parent frame.
Clicking the panel should turn itself yellow. Clicking the surrounding frame area should turn the panel green.
// wxWidgets in full of strcpy
#pragma warning(disable : 4996)
#include <wx/wx.h>
class cPanel : public wxPanel {
public:
cPanel(wxWindow* parent, wxSize size)
: wxPanel(parent, wxID_ANY, wxDefaultPosition, size) {
this->Bind(wxEVT_LEFT_DOWN, &cPanel::OnLeftClick, this);
}
void OnLeftClick(wxMouseEvent& event) {
SetBackgroundColour(wxColour("yellow"));
Refresh();
};
};
class cFrame : public wxFrame {
public:
wxPanel* child;
cFrame()
: wxFrame(nullptr, wxID_ANY, "Example Title", wxPoint(200, 200),
wxSize(800, 500)) {
child = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(300, 300));
auto top = new wxBoxSizer(wxHORIZONTAL);
top->Add(child, 0, wxALL, 20);
SetSizer(top);
this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
};
void OnLeftClick(wxMouseEvent& event) {
child->SetBackgroundColour(wxColour("green"));
child->Refresh();
}
};
class cApp : public wxApp {
public:
cFrame* frame = nullptr;
cApp(){};
~cApp(){};
virtual bool OnInit() {
frame = new cFrame();
frame->Show();
return true;
}
};
wxIMPLEMENT_APP(cApp);
Only wxCommandEvents (and classes derived from it) will filter up to parent windows. wxMouseEvent does not derive from wxCommandEvent, so a mouse event on the panel that is not handled by the panel will not filter up to the frame.
Consequently, you'll need to Bind the mouse event handler to the panel instead of the frame. Here's an example of how to change the last few lines of your cFrame class to do that.
this->Bind(wxEVT_LEFT_DOWN, &cFrame::OnLeftClick, this);
child->Bind(wxEVT_LEFT_DOWN, &cFrame::OnChildLeftClick, this);
};
void OnLeftClick(wxMouseEvent& event) {
child->SetBackgroundColour(wxColour("green"));
child->Refresh();
}
void OnChildLeftClick(wxMouseEvent& event) {
child->SetBackgroundColour(wxColour("yellow"));
child->Refresh();
}
};
There are many ways of accomplishing the same thing, but I think something like this is probably the simplest.
I have created a popup using "wxPopupTransientWindow" and added some text and a button. Now I want to destroy this popup only on Clicking this particular button. Right now, Popup will get destroyed if click anywhere outside it.
Here is what I have tried so far.
SimpleTransientPopup::SimpleTransientPopup(wxWindow *parent, bool scrolled) : wxPopupTransientWindow(parent, wxFRAME_SHAPED)
{
m_bGotItClick = false;
m_panel = new wxScrolledWindow(this,-1,wxDefaultPosition,wxDefaultSize);
m_panel->SetBackgroundColour(*wxYELLOW);
wxStaticText *text = new wxStaticText( m_panel, wxID_ANY,
"This panel will guide you stepwise through the workflow\n");
wxPanel *pBottomPanel = new wxPanel(m_panel, -1);
m_button = new wxButton(pBottomPanel, Minimal_PopupButton, "Got it!",wxDefaultPosition, wxDefaultSize);
m_link = new wxHyperlinkCtrl(pBottomPanel, Minimal_PopupLink, _("Hide these tips"), _(""), wxDefaultPosition, wxDefaultSize, wxHL_ALIGN_LEFT);
m_link->SetFont(wxFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_NORMAL, true));
m_link->SetNormalColour(*wxLIGHT_GREY);
m_link->SetForegroundColour(*wxLIGHT_GREY);
wxBoxSizer *topSizer = new wxBoxSizer( wxVERTICAL );
wxBoxSizer *bottomSizer = new wxBoxSizer(wxHORIZONTAL);
bottomSizer->Add(m_link,0,wxALL,20);
bottomSizer->AddStretchSpacer();
bottomSizer->Add(m_button, 0, wxALL, 20);
pBottomPanel->SetAutoLayout(true);
pBottomPanel->SetSizer(bottomSizer);
bottomSizer->SetSizeHints(pBottomPanel);
bottomSizer->Fit(pBottomPanel);
topSizer->Add(text, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 20);
topSizer->Add(pBottomPanel,0,wxEXPAND);
m_panel->SetSizer( topSizer );
// Use the fitting size for the panel if we don't need scrollbars.
topSizer->Fit(m_panel);
SetClientSize(m_panel->GetSize());
wxTipKind tipKind = wxTipKind_Auto;
const int offsetY = SetTipShapeAndSize(tipKind, GetBestSize());
if (offsetY > 0)
{
// Offset our contents by the tip height to make it appear in the
// main rectangle.
topSizer->PrependSpacer(offsetY);
}
m_panel->Fit();
}
I am calling this OnDismiss function on clicking the button.
void SimpleTransientPopup::OnDismiss()
{
if (m_bGotItClick) {
wxLogMessage("%p SimpleTransientPopup::OnDismiss", this);
wxPopupTransientWindow::OnDismiss();
m_bGotItClick = false;
}
}
PS: I am new to wxWidgets
By overriding "wxPopupTransientWindow::Dismiss()" I was able to close the popup when button is clicked.
void SimpleTransientPopup::Dismiss()
{
if (m_bGotItClick) {
wxLogMessage("%p SimpleTransientPopup::Dismiss", this);
wxPopupTransientWindow::Dismiss();
m_bGotItClick = false;
}
}
how to show another frame after button click event?
like this
My code here show window OnInit. but what to do next?
I did not find how to do this. little experience with this.
I comment the window that should be.
enum
{
wxID_First_Load = 5000,
wxID_First_Frame,
wxID_First_Panel
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void fileLoad(wxCommandEvent& event);
private:
int file_count = 0;
wxDECLARE_EVENT_TABLE();
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(wxID_First_Load, MyFrame::fileLoad)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame("Hello World", wxDefaultPosition, wxSize(450, 250));
frame->SetWindowStyle(wxCAPTION | wxSYSTEM_MENU );
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_First_Frame, title, pos, size)
{
wxBoxSizer *first_sizer = new wxBoxSizer(wxVERTICAL);
wxPanel *firstPanel = new wxPanel(this, wxID_First_Panel);
wxButton *firstButton_Load = new wxButton(firstPanel, wxID_First_Load, "Choose file");
firstPanel->SetSizer(first_sizer);
first_sizer->Add(firstButton_Load, 1, wxEXPAND | wxALL, 10);
firstPanel->SetSizer(first_sizer);
}
void MyFrame::fileLoad(wxCommandEvent& WXUNUSED(event))
{
file_count = 2;
}
Second Frame or window:
wxPanel *firstPanel = new wxPanel(this, wxID_First_Panel);
wxBoxSizer *second_sizer = new wxBoxSizer(wxVERTICAL);
for (int i = 0; i < file_count; i++)
{
second_sizer->Add(new wxTextCtrl(firstPanel, wxWindowID(i), "Hello", wxDefaultPosition, wxSize(235, 60)), wxSizerFlags(0).Border(wxALL, 5));
}
firstPanel->SetSizer(second_sizer);
To create a new frame you need to create a new object of wxFrame class or a class deriving from it. Typically, you want to put some data and logic into your new frame, so you would create some class, e.g. MySecondaryFrame (but hopefully with a better name) , inheriting from wxFrame in a similar way to your existing MyFrame class.
Then to show it you would do the same thing that you do in MyApp::OnInit(), i.e. create a new object of this class and call Show() to actually show it.
P.S. Note that your SetWindowStyle(wxCAPTION | wxSYSTEM_MENU ) call is unnecessary, these styles are already on by default. Also, hard coding the frame size in pixels is a bad idea, consider using sizers to determine the best size suitable for the frame contents or just leave it unspecified if it really doesn't matter.