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.
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 need unpack an archive which contains unicode characters for filenames in archives.
My code equivalent this:
#include "wx/wx.h"
#include <wx/archive.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/fs_arc.h>
#include "wx/fs_zip.h"
#include <memory>
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title) :
wxFrame( NULL, -1, title, wxDefaultPosition, wxSize(350,180 ),
wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxMAXIMIZE_BOX))
{
wxBoxSizer* Sizer = new wxBoxSizer( wxVERTICAL );
TextBox = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
Sizer->Add( TextBox, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5 );
auto Button = new wxButton( this, wxID_ANY, wxT("Show entries"), wxDefaultPosition, wxDefaultSize, 0 );
Button->Bind(wxEVT_BUTTON, [this](wxCommandEvent&)
{
auto filename = "test.zip";
auto factory = wxArchiveClassFactory::Find(filename, wxSTREAM_FILEEXT);
if(!factory)
return;
std::unique_ptr<wxArchiveInputStream> inarc(factory->NewStream(new wxFFileInputStream(filename)));
std::unique_ptr<wxArchiveEntry> entry(factory->NewEntry());
while (entry.reset(inarc->GetNextEntry()), entry.get() != NULL)
TextBox->AppendText("Entry : "+entry->GetName()+"\n");
});
Sizer->Add( Button, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
this->SetSizer( Sizer );
this->Layout();
}
private:
wxTextCtrl* TextBox;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE
{
MyFrame *frame = new MyFrame(wxT("Test"));
frame->Show(true);
return true;
};
};
wxIMPLEMENT_APP(MyApp);
Is it possible to get the correct names for these files using wxWidgets?
Edit: I on Windows7 use wxWidgets 3.1.0, compiler - cl( 17.00.50727.1) VS11.
It seems you suffer this bug: http://trac.wxwidgets.org/ticket/17244 which has been fixed after wx3.1 release.
You need to download the current head code (available with git).
I am starting to practise my skills with wxWidgets and I wanted to add some panel with two different texts above this slider, but when I tried, the result is invisible:
MainWindow::MainWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition) {
wxMenuBar *menubar;
wxMenu *file;
menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_OPEN, wxT("&Open"));
menubar->Append(file, wxT("&File"));
SetMenuBar(menubar);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
this->SetSizer(vbox);
player_widget = new wxWindow(this, wxID_ANY);
player_widget->SetBackgroundColour(wxColour(wxT("black")));
vbox->Add(player_widget, 1, wxEXPAND | wxALIGN_TOP);
wxBoxSizer* bs = new wxBoxSizer(wxHORIZONTAL);
wxPanel* p1 = new wxPanel(this,wxID_ANY,wxDefaultPosition,wxSize(0,20));
p1->SetSizer(bs);
p1->Enable(true);
p1->Show(true);
vbox->Add(p1,0,wxEXPAND);
wxStaticText* text1 = new wxStaticText(this,11, "text 1");
wxStaticText* text2 = new wxStaticText(this,12, "text 2");
bs->Add(text1);
bs->Add(text2);
timeline = new wxSlider(this, myID_TIMELINE, 0, 0, TIMELINE_MAX);
vbox->Add(timeline, 0, wxEXPAND);
wxPanel *controlPanel = new wxPanel(this, wxID_ANY);
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
controlPanel->SetSizer(hbox);
vbox->Add(controlPanel, 0, wxEXPAND);
playpause_button = new wxButton(controlPanel, myID_PLAYPAUSE, wxT("Play"));
stop_button = new wxButton(controlPanel, myID_STOP, wxT("Stop"));
volume_slider = new wxSlider(controlPanel, myID_VOLUME, VOLUME_MAX, 0, VOLUME_MAX, wxDefaultPosition, wxSize(100, -1));
hbox->Add(playpause_button);
hbox->Add(stop_button);
hbox->AddStretchSpacer();
hbox->Add(volume_slider);
}
Any ideas what I did wrong?
Change parent of text1 and text2 to p1.
It is simpler to use just one panel that holds everything.
Something like this ( a complete program )
#include <wx/wx.h>
#include <wx/app.h>
class cApp : public wxApp
{
public:
virtual bool OnInit();
};
#define TIMELINE_MAX 100
#define VOLUME_MAX 100
enum {
myID_TIMELINE,
myID_PLAYPAUSE,
myID_STOP,
myID_VOLUME
};
class cFrame: public wxFrame
{
wxWindow * player_widget;
wxSlider * timeline;
wxSlider * volume_slider;
wxButton * playpause_button;
wxButton * stop_button;
public:
cFrame(wxFrame *frame, const wxString& title)
: wxFrame(frame, -1, title, wxPoint(-1,-1),wxSize(600,600))
{
wxMenuBar *menubar;
wxMenu *file;
menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_OPEN, wxT("&Open"));
menubar->Append(file, wxT("&File"));
SetMenuBar(menubar);
// define one panel to hold everything
// make it big enough to fill the frame
wxPanel* p1 = new wxPanel(this,wxID_ANY, wxPoint(-1,-1),wxSize(600,600) );
// top level sizer to hold everything
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
// add window widget at top
player_widget = new wxWindow(p1, wxID_ANY);
player_widget->SetBackgroundColour(wxColour(wxT("black")));
vbox->Add(player_widget, 1, wxEXPAND | wxALIGN_TOP);
// add some texts in a horizontal row
wxBoxSizer* bs = new wxBoxSizer(wxHORIZONTAL);
wxStaticText* text1 = new wxStaticText(p1,11, "text 1");
wxStaticText* text2 = new wxStaticText(p1,12, "text 2");
bs->Add(text1);
bs->Add(text2);
// add texts just below window widget
vbox->Add( bs );
// add slider below texts
timeline = new wxSlider(this, myID_TIMELINE, 0, 0, TIMELINE_MAX);
vbox->Add(timeline, 0, wxEXPAND);
// some more controls in a row
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
playpause_button = new wxButton(p1, myID_PLAYPAUSE, wxT("Play"));
stop_button = new wxButton(p1, myID_STOP, wxT("Stop"));
volume_slider = new wxSlider(p1, myID_VOLUME, VOLUME_MAX,
0, VOLUME_MAX, wxDefaultPosition, wxSize(100, -1));
hbox->Add(playpause_button);
hbox->Add(stop_button);
hbox->AddStretchSpacer();
hbox->Add(volume_slider);
// add controls below big slider
vbox->Add( hbox );
// make everything happen
SetSizer(vbox);
}
};
IMPLEMENT_APP(cApp);
bool cApp::OnInit()
{
cFrame* frame = new cFrame(0L, _("wx Starter"));
frame->Show();
return true;
}
This gives:
Here's the thing about computer programming: at some time you always have to settle down and write some code. All those applications, like wxFormBuilder, that promise to write code for you have to be abandoned at some point and you actually have to do some work ( Here is a link to more about this )
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);
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.