I followed the wxCrafter Hello World tutorial: https://wiki.codelite.org/pmwiki.php/Main/WxCrafterHelloWorld
Building process shows no errors, however no dialog shows up when I click "Build->Run" in CodeLite 13, for some reason nothing happens :/
main.cpp:
#include <wx/app.h>
#include <wx/event.h>
#include "MainDialog.h"
#include <wx/image.h>
// Define the MainApp
class MainApp : public wxApp
{
public:
MainApp() {}
virtual ~MainApp() {}
virtual bool OnInit() {
// Add the common image handlers
wxImage::AddHandler( new wxPNGHandler );
wxImage::AddHandler( new wxJPEGHandler );
MainDialog mainDialog(NULL);
mainDialog.ShowModal();
return false;
}
};
DECLARE_APP(MainApp)
IMPLEMENT_APP(MainApp)
MainDialog.h:
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include "wxcrafter.h"
class MainDialog : public MainDialogBaseClass
{
public:
MainDialog(wxWindow* parent);
virtual ~MainDialog();
protected:
virtual void OnButtonokButtonClicked(wxCommandEvent& event);
};
#endif // MAINDIALOG_H
MainDialog.cpp:
#include "MainDialog.h"
#include <wx/msgdlg.h>
MainDialog::MainDialog(wxWindow* parent)
: MainDialogBaseClass(parent)
{
}
MainDialog::~MainDialog()
{
}
void MainDialog::OnButtonokButtonClicked(wxCommandEvent& event)
{
::wxMessageBox(_("Hello World"));
}
wxcrafter.h:
//////////////////////////////////////////////////////////////////////
// This file was auto-generated by codelite's wxCrafter Plugin
// wxCrafter project file: wxcrafter.wxcp
// Do not modify this file by hand!
//////////////////////////////////////////////////////////////////////
#ifndef _WORKSPACE_MEDCEN_PLUS_WXCRAFTER_BASE_CLASSES_H
#define _WORKSPACE_MEDCEN_PLUS_WXCRAFTER_BASE_CLASSES_H
#include <wx/artprov.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/iconbndl.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/xrc/xh_bmp.h>
#include <wx/xrc/xmlres.h>
#if wxVERSION_NUMBER >= 2900
#include <wx/persist.h>
#include <wx/persist/bookctrl.h>
#include <wx/persist/toplevel.h>
#include <wx/persist/treebook.h>
#endif
#ifdef WXC_FROM_DIP
#undef WXC_FROM_DIP
#endif
#if wxVERSION_NUMBER >= 3100
#define WXC_FROM_DIP(x) wxWindow::FromDIP(x, NULL)
#else
#define WXC_FROM_DIP(x) x
#endif
class MainDialogBaseClass : public wxDialog
{
protected:
wxStaticText* m_staticText21;
wxTextCtrl* m_textCtrl23;
wxStaticLine* m_staticLine15;
wxButton* m_buttonOK;
wxButton* m_buttonCancel;
protected:
virtual void OnButtonokButtonClicked(wxCommandEvent& event)
{
event.Skip();
}
public:
wxStaticText* GetStaticText21()
{
return m_staticText21;
}
wxTextCtrl* GetTextCtrl23()
{
return m_textCtrl23;
}
wxStaticLine* GetStaticLine15()
{
return m_staticLine15;
}
wxButton* GetButtonOK()
{
return m_buttonOK;
}
wxButton* GetButtonCancel()
{
return m_buttonCancel;
}
MainDialogBaseClass(wxWindow* parent,
wxWindowID id = wxID_ANY,
const wxString& title = _("My Dialog"),
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxSize(500, 300),
long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
virtual ~MainDialogBaseClass();
};
#endif
wxcrafter.cpp:
//////////////////////////////////////////////////////////////////////
// This file was auto-generated by codelite's wxCrafter Plugin
// wxCrafter project file: wxcrafter.wxcp
// Do not modify this file by hand!
//////////////////////////////////////////////////////////////////////
#include "wxcrafter.h"
// Declare the bitmap loading function
extern void wxC9ED9InitBitmapResources();
static bool bBitmapLoaded = false;
MainDialogBaseClass::MainDialogBaseClass(wxWindow* parent,
wxWindowID id,
const wxString& title,
const wxPoint& pos,
const wxSize& size,
long style)
: wxDialog(parent, id, title, pos, size, style)
{
if(!bBitmapLoaded) {
// We need to initialise the default bitmap handler
wxXmlResource::Get()->AddHandler(new wxBitmapXmlHandler);
wxC9ED9InitBitmapResources();
bBitmapLoaded = true;
}
this->SetFocus();
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
this->SetSizer(mainSizer);
wxBoxSizer* boxSizer19 = new wxBoxSizer(wxHORIZONTAL);
mainSizer->Add(boxSizer19, 1, wxALL | wxEXPAND, WXC_FROM_DIP(5));
m_staticText21 = new wxStaticText(
this, wxID_ANY, _("Static Text Label"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
boxSizer19->Add(m_staticText21, 0, wxALL, WXC_FROM_DIP(5));
m_textCtrl23 = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
#if wxVERSION_NUMBER >= 3000
m_textCtrl23->SetHint(wxT(""));
#endif
boxSizer19->Add(m_textCtrl23, 0, wxALL, WXC_FROM_DIP(5));
mainSizer->Add(0, 0, 1, wxALL | wxEXPAND, WXC_FROM_DIP(5));
m_staticLine15 =
new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), wxLI_HORIZONTAL);
mainSizer->Add(m_staticLine15, 0, wxALL | wxEXPAND, WXC_FROM_DIP(5));
wxBoxSizer* boxSizer12 = new wxBoxSizer(wxHORIZONTAL);
mainSizer->Add(boxSizer12, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, WXC_FROM_DIP(5));
m_buttonOK = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
m_buttonOK->SetDefault();
boxSizer12->Add(m_buttonOK, 0, wxALL, WXC_FROM_DIP(5));
m_buttonCancel =
new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDLG_UNIT(this, wxSize(-1, -1)), 0);
boxSizer12->Add(m_buttonCancel, 0, wxALL, WXC_FROM_DIP(5));
SetName(wxT("MainDialogBaseClass"));
SetSize(wxDLG_UNIT(this, wxSize(500, 300)));
if(GetSizer()) {
GetSizer()->Fit(this);
}
if(GetParent()) {
CentreOnParent(wxBOTH);
} else {
CentreOnScreen(wxBOTH);
}
#if wxVERSION_NUMBER >= 2900
if(!wxPersistenceManager::Get().Find(this)) {
wxPersistenceManager::Get().RegisterAndRestore(this);
} else {
wxPersistenceManager::Get().Restore(this);
}
#endif
// Connect events
m_buttonOK->Connect(
wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainDialogBaseClass::OnButtonokButtonClicked), NULL, this);
}
MainDialogBaseClass::~MainDialogBaseClass()
{
m_buttonOK->Disconnect(
wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainDialogBaseClass::OnButtonokButtonClicked), NULL, this);
}
Something is wrong with either the C::L installation or the wxCrafter plugin.
If you did follow the referenced tutorial, it should create a wxButton object as a member of the dialog and bind the event handler to it.
Can you compile and run the minimal sample from the wxWidgets install?
Related
I am a novice in C++ and wxWidgets, and use wxFormBuilder to help me create the GUI.
I like the idea to put similar functions together in 1 module, which means that my apps have multiple modules.
One of the modules is for the GUI (it now contains code for the main Frame (with only a menu and taskbar) and
a Panel (with 2 StaticText controls and a button).
The button increments a counter and the value is shown in one of the StaticTexts on the Panel.
Sofar so good. (compiles without errors)
I can make the Panel show/Hide, but I seem to miss an essential piece of knowledge to make the Panel button work.
The ways I tried are all similar to the way the Main module is coded, but that is not working.
I understand how it works with GUI class elements in the same file.
However, I like to keep all GUI code in one module (GUIFrame.h/.cpp),
and all the 'function' code in e.g. the Panel module (MyPanel.h/.cpp).
Just because I am not sure where I make my mistake, I present all code in this post.
My aplologies if it is too much.
Hopefully someone can help me bridge my gap in knowledge about this way of working.
Regards,
Ruud
=== GUIFrame.h
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/string.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/menu.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/statusbr.h>
#include <wx/frame.h>
#include <wx/stattext.h>
#include <wx/button.h>
#include <wx/sizer.h>
#include <wx/panel.h>
///////////////////////////////////////////////////////////////////////////
#define idMenuQuit 1000
///////////////////////////////////////////////////////////////////////////////
/// Class GUIFrame
///////////////////////////////////////////////////////////////////////////////
class GUIFrame : public wxFrame
{
private:
protected:
wxMenuBar* mbar;
wxMenu* mainMenu;
wxStatusBar* statusBar;
// Virtual event handlers, overide them in your derived class
virtual void OnClose( wxCloseEvent& event ) { event.Skip(); }
virtual void m_menuItemPanelMainOnMenuSelection( wxCommandEvent& event ) { event.Skip(); }
virtual void OnQuit( wxCommandEvent& event ) { event.Skip(); }
public:
GUIFrame( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Test application"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 500,500 ), long style = wxDEFAULT_FRAME_STYLE );
~GUIFrame();
};
///////////////////////////////////////////////////////////////////////////////
/// Class PanelMAIN
///////////////////////////////////////////////////////////////////////////////
class PanelMAIN : public wxPanel
{
private:
protected:
wxStaticText* m_staticText3;
// Virtual event handlers, overide them in your derived class
virtual void m_btn_Counter_OnButtonClick( wxCommandEvent& event ) { event.Skip(); }
public:
wxStaticText* m_staticTextPMain;
wxButton* m_buttonPMain;
PanelMAIN( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxPoint( 3,3 ), const wxSize& size = wxSize( 350,300 ), long style = wxBORDER_RAISED, const wxString& name = wxEmptyString );
~PanelMAIN();
};
==== GUIFrame.cpp
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif //WX_PRECOMP
#include "GUIFrame.h"
///////////////////////////////////////////////////////////////////////////
GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
mbar = new wxMenuBar( 0 );
mainMenu = new wxMenu();
wxMenuItem* m_menuItemPanelMain;
m_menuItemPanelMain = new wxMenuItem( mainMenu, wxID_ANY, wxString( wxT("To Main Panel") ) , wxEmptyString, wxITEM_NORMAL );
mainMenu->Append( m_menuItemPanelMain );
wxMenuItem* menuFileQuit;
menuFileQuit = new wxMenuItem( mainMenu, idMenuQuit, wxString( wxT("&Quit") ) + wxT('\t') + wxT("Alt+F4"), wxT("Quit the application"), wxITEM_NORMAL );
mainMenu->Append( menuFileQuit );
mbar->Append( mainMenu, wxT("&Menu") );
this->SetMenuBar( mbar );
statusBar = this->CreateStatusBar( 2, wxSTB_SIZEGRIP, wxID_ANY );
// Connect Events
this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
mainMenu->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::m_menuItemPanelMainOnMenuSelection ), this, m_menuItemPanelMain->GetId());
mainMenu->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnQuit ), this, menuFileQuit->GetId());
}
GUIFrame::~GUIFrame()
{
// Disconnect Events
this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
}
PanelMAIN::PanelMAIN( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name ) : wxPanel( parent, id, pos, size, style, name )
{
this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_APPWORKSPACE ) );
wxBoxSizer* bSizer1;
bSizer1 = new wxBoxSizer( wxVERTICAL );
m_staticTextPMain = new wxStaticText( this, wxID_ANY, wxT("We are on PanelMAIN now"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticTextPMain->Wrap( -1 );
bSizer1->Add( m_staticTextPMain, 0, wxALL, 5 );
m_buttonPMain = new wxButton( this, wxID_ANY, wxT("Counter++"), wxDefaultPosition, wxDefaultSize, 0 );
bSizer1->Add( m_buttonPMain, 0, wxALL, 5 );
m_staticText3 = new wxStaticText( this, wxID_ANY, wxT("0"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticText3->Wrap( -1 );
bSizer1->Add( m_staticText3, 0, wxALL, 5 );
this->SetSizer( bSizer1 );
this->Layout();
// Connect Events
m_buttonPMain->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PanelMAIN::m_btn_Counter_OnButtonClick ), NULL, this );
}
PanelMAIN::~PanelMAIN()
{
// Disconnect Events
m_buttonPMain->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PanelMAIN::m_btn_Counter_OnButtonClick ), NULL, this );
}
==== wxPanelAPP.h
#ifndef WXPANELAPP_H
#define WXPANELAPP_H
#include <wx/app.h>
class wxPanelApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // WXPANELAPP_H
==== wxPanelAPP.cpp
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "wxPanelApp.h"
#include "wxPanelMain.h"
IMPLEMENT_APP(wxPanelApp);
bool wxPanelApp::OnInit()
{
wxPanelFrame* frame = new wxPanelFrame(0L);
frame->SetIcon(wxICON(aaaa)); // To Set App Icon
frame->Show();
return true;
}
==== wxPanelMain.h
#ifndef WXPANELMAIN_H
#define WXPANELMAIN_H
#include "wxPanelApp.h"
#include "GUIFrame.h"
#include "MyPanel.h"
#include <iostream>
class wxPanelFrame: public GUIFrame
{
public:
wxPanelFrame(wxFrame *frame);
~wxPanelFrame();
wxPanel *m_wxpMain = new PanelMAIN(this, wxID_ANY);
private:
virtual void OnClose(wxCloseEvent& event);
virtual void OnQuit(wxCommandEvent& event);
void m_menuItemPanelMainOnMenuSelection( wxCommandEvent& event );
};
#endif // WXPANELMAIN_H
==== wxPanelMain.cpp
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "wxPanelMain.h"
wxPanelFrame::wxPanelFrame(wxFrame *frame) : GUIFrame(frame)
{
#if wxUSE_STATUSBAR
statusBar->SetStatusText(_("Simple Panel Test"), 0);
#endif
// Hide the panel
m_wxpMain->Show(false);
}
wxPanelFrame::~wxPanelFrame()
{
}
void wxPanelFrame::OnClose(wxCloseEvent &event)
{
Destroy();
}
void wxPanelFrame::OnQuit(wxCommandEvent &event)
{
Destroy();
}
void wxPanelFrame::m_menuItemPanelMainOnMenuSelection( wxCommandEvent& event )
{
// Show the panel
m_wxpMain->Show(true);
}
==== MyPanel.h
#ifndef MYPANEL_H
#define MYPANEL_H
#include "GUIFrame.h"
#include <iostream>
class MyPanel : public PanelMAIN
{
public:
MyPanel(wxPanel *panel);
virtual ~MyPanel();
int i{0};
protected:
private:
void m_btn_Counter_OnButtonClick( wxCommandEvent& event );
};
#endif // MYPANEL_H
==== MyPanel.cpp
#include "MyPanel.h"
MyPanel::MyPanel(wxPanel *panel) : PanelMAIN(panel)
{
std::cout << "MyPanel::MyPanel /*CONSTRUCTOR*/\n";
}
MyPanel::~MyPanel()
{
std::cout << "MyPanel::~MyPanel /*DESTRUCTOR*/\n";
}
void MyPanel::m_btn_Counter_OnButtonClick( wxCommandEvent& event )
{
wxString wxs_Count{};
i++;
wxs_Count << i;
m_staticText3->SetLabelText(wxs_Count);
}
You define your event handler in your derived class MyPanel, but you never create and instance of it. The line
wxPanel *m_wxpMain = new PanelMAIN(this, wxID_ANY);
only creates on object of the base class.
You should change this to
MyPanel *m_wxpMain = new MyPanel(this);
You also have an problem with the constructor for MyPanel. The parent needs to be a wxWindow not wxPanel. So the declaration should be something like
MyPanel(wxWindow *panel);
and the body should look something like
MyPanel::MyPanel(wxWindow *panel) : PanelMAIN(panel)
{
std::cout << "MyPanel::MyPanel /*CONSTRUCTOR*/\n";
}
I have a simple frame which is containing some buttons. My aim is that, after clicking the GetMousePosition button, getting the first mouse click position. I try to capture mouse click, even if I click outside of the running application.
This is a desktop application running on Windows. I tried some mouse events that wxwidgets provides, but I couldn't handle the next click event. I tried to find a solution with following code, but if there is some different solution, I can throw that code in the trash.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(BUTTON_GetPos, MyFrame::OnButtonClick)
EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent)
EVT_MOUSE_CAPTURE_LOST(MyFrame::OnMouseCaptureLost)
END_EVENT_TABLE()
//some more code
void MyFrame::OnButtonClick(wxCommandEvent & WXUNUSED(event))
{
//Start Capturing for next mouse left-click
if (!HasCapture())
CaptureMouse();
}
void MyFrame::OnMouseEvent(wxMouseEvent &event)
{
if (event.LeftDown()) {
//GetMousePosition
if (HasCapture())
ReleaseMouse();
}
}
void MyFrame::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
{
}
I expect to get the mouse position, in the first left click after the button is pressed.
The code you posted looks like it should work. If there's a problem, it might be in the code you omitted. Anyway, here's a small example application showing the behavior you want. The underlying logic of this example is the same as the code you posted, except this example uses dynamic binding instead of event tables.
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
class MyFrame : public wxFrame
{
public:
MyFrame(wxWindow* parent);
protected:
void OnButtonClick(wxCommandEvent& event);
void OnMouseCapLost(wxMouseCaptureLostEvent& event);
void OnLeftDown(wxMouseEvent&);
void CleanUp();
private:
wxTextCtrl* m_textCtrl;
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
MyFrame::MyFrame(wxWindow* parent)
:wxFrame(parent, wxID_ANY, "Demo", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
{
wxPanel* panel = new wxPanel(this, wxID_ANY );
wxButton* button = new wxButton(panel, wxID_ANY, "Click Me");
m_textCtrl = new wxTextCtrl(panel, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_DONTWRAP|wxTE_MULTILINE|wxTE_READONLY);
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
bSizer->Add(button, 0, wxALL, 5);
bSizer->Add(m_textCtrl, 1, wxALL|wxEXPAND, 5);
panel->SetSizer(bSizer);
Layout();
button->Bind(wxEVT_BUTTON,&MyFrame::OnButtonClick,this);
}
void MyFrame::OnButtonClick(wxCommandEvent& event)
{
if ( !HasCapture() )
{
CaptureMouse();
m_textCtrl->AppendText("Mouse captured.\n");
Bind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
Bind(wxEVT_MOUSE_CAPTURE_LOST, &MyFrame::OnMouseCapLost, this);
}
}
void MyFrame::CleanUp()
{
if ( HasCapture() )
ReleaseMouse();
Unbind(wxEVT_LEFT_DOWN, &MyFrame::OnLeftDown, this);
Unbind(wxEVT_MOUSE_CAPTURE_LOST, &MyFrame::OnMouseCapLost, this);
}
void MyFrame::OnMouseCapLost(wxMouseCaptureLostEvent& event)
{
m_textCtrl->AppendText("Mouse cap lost.\n");
CleanUp();
}
void MyFrame::OnLeftDown(wxMouseEvent& event)
{
m_textCtrl->AppendText("Click recorded.\n");
CleanUp();
}
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
I hope this helps.
edit: Here's a version using event tables as well:
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#define BUTTON_ID 101
class MyFrame : public wxFrame
{
public:
MyFrame(wxWindow* parent);
protected:
void OnButtonClick(wxCommandEvent& event);
void OnMouseCapLost(wxMouseCaptureLostEvent& event);
void OnMouseEvent(wxMouseEvent&);
void CleanUp();
private:
wxTextCtrl* m_textCtrl;
wxDECLARE_EVENT_TABLE();
};
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_BUTTON(BUTTON_ID, MyFrame::OnButtonClick)
EVT_MOUSE_EVENTS(MyFrame::OnMouseEvent)
EVT_MOUSE_CAPTURE_LOST(MyFrame::OnMouseCapLost)
wxEND_EVENT_TABLE()
MyFrame::MyFrame(wxWindow* parent)
:wxFrame(parent, wxID_ANY, "Demo", wxDefaultPosition, wxDefaultSize,
wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
{
wxPanel* panel = new wxPanel(this, wxID_ANY );
wxButton* button = new wxButton(panel, BUTTON_ID, "Click Me");
m_textCtrl = new wxTextCtrl(panel, wxID_ANY, wxEmptyString,
wxDefaultPosition, wxDefaultSize,
wxTE_DONTWRAP|wxTE_MULTILINE|wxTE_READONLY);
wxBoxSizer* bSizer = new wxBoxSizer(wxVERTICAL);
bSizer->Add(button, 0, wxALL, 5);
bSizer->Add(m_textCtrl, 1, wxALL|wxEXPAND, 5);
panel->SetSizer(bSizer);
Layout();
}
void MyFrame::OnButtonClick(wxCommandEvent& event)
{
if ( !HasCapture() )
{
CaptureMouse();
m_textCtrl->AppendText("Mouse captured.\n");
}
}
void MyFrame::CleanUp()
{
if ( HasCapture() )
ReleaseMouse();
}
void MyFrame::OnMouseCapLost(wxMouseCaptureLostEvent& event)
{
m_textCtrl->AppendText("Mouse cap lost.\n");
CleanUp();
}
void MyFrame::OnMouseEvent(wxMouseEvent& event)
{
if( HasCapture() && event.LeftIsDown() )
{
m_textCtrl->AppendText("Click recorded.\n");
CleanUp();
}
}
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame(NULL);
frame->Show();
return true;
}
wxIMPLEMENT_APP(MyApp);
I am trying to follow this tutorial. http://www.remy.org.uk/tech.php?tech=1407951209
the test compiles and runs apart from not getting the correct result. for some reason the test gui is not getting focus.
I tried to step through the code it appends the text on the current window i have focus which is the text editor.
What am i missing from the example?
#pragma once
#include <wx/wx.h>
#include <wx/uiaction.h>
#include "gtest/gtest.h"
class TestFrame : public wxFrame
{
wxTextCtrl *textIn;
wxButton *button;
wxTextCtrl *textOut;
FRIEND_TEST(GuiTest, CopyTest);
public:
TestFrame() : wxFrame(NULL, wxID_ANY, "wxUnitTest", wxPoint(50, 50), wxSize(450, 340))
{
textIn = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
button = new wxButton(this, wxID_ANY, wxT(" => "), wxDefaultPosition, wxDefaultSize, 0);
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &TestFrame::OnButton, this);
textOut = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
wxBoxSizer *boxSizer = new wxBoxSizer(wxHORIZONTAL); boxSizer->Add(textIn, 1, wxALL, 5);
boxSizer->Add(button, 0, wxALL, 5); boxSizer->Add(textOut, 1, wxALL, 5);
this->SetSizer(boxSizer); this->Layout(); this->Centre(wxBOTH); }
void OnButton(wxCommandEvent &WXUNUSED(event)) { this->textOut->SetValue(this->textIn->GetValue()); }
};
class TestApp : public wxApp
{
public: TestFrame *frame; virtual bool OnInit()
{
frame = new TestFrame();
frame->Show(true);
return true;
}
};
class GuiTest : public testing::Test
{
protected:
TestApp *app;
virtual void SetUp()
{
char appname[] = "wxUnitTest.exe";
int argc = 1;
char *argv[1] = { appname };
app = new TestApp();
wxApp::SetInstance(app);
wxEntryStart(argc, argv);
app->OnInit();
}
virtual void TearDown()
{
//wxTheApp->OnRun();
app->OnExit();
wxEntryCleanup();
}
};
TEST_F(GuiTest, CopyTest)
{
wxUIActionSimulator acts;
app->frame->textIn->SetFocus();
wxYield();
acts.Text("Text");
wxYield();
app->frame->button->SetFocus();
wxYield();
acts.Char(WXK_RETURN);
wxYield();
ASSERT_EQ("Text", app->frame->textOut->GetValue());
}
// Main.cpp
#include "stdafx.h"
#include "Main.h"
#include "tst_guitest.h"
#include "wx/app.h"
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
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 know that our questions put on evidence our knowledge, so be gentle please :). This is one of mine in C++. Well, here we go.
I'm trying to change from codeblocks the font of a StaticBoxSizer. I can change the font of the wxStaticBox in the properties section but I'm having a hard time finding a way to access the same thing for the wxStaticBoxSizer FROM wxSmith in codeBlocks.
I tried without success to acces the staticBox within the sizer. What's worst, i dont know how to access any of the wxSmith's elements from the code editor.
All hints are welcomed and thank you in advance.
Edit:
I know for sure that there's nothing wrong with the IDE but here's the header and here the cpp.
I noticed that the sizers are not declared in the headers but in the constructor's implementation of the dialog. I try to access the sizer in the constructor but to no avail.
The links works to me but here are the contents of the files directly. I simplified the code to include the minimum of elements.
header:
/***************************************************************
* Name: sizerTestMain.h
* Purpose: Defines Application Frame
* Author: RainMaker ()
* Created: 2015-11-11
* Copyright: RainMaker ()
* License:
**************************************************************/
#ifndef SIZERTESTMAIN_H
#define SIZERTESTMAIN_H
//(*Headers(sizerTestFrame)
#include <wx/sizer.h>
#include <wx/menu.h>
#include <wx/button.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
//*)
class sizerTestFrame: public wxFrame
{
public:
sizerTestFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~sizerTestFrame();
private:
//(*Handlers(sizerTestFrame)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnButton1Click(wxCommandEvent& event);
void OnButton3Click(wxCommandEvent& event);
//*)
//(*Identifiers(sizerTestFrame)
static const long ID_BUTTON1;
static const long ID_BUTTON2;
static const long ID_BUTTON3;
static const long idMenuQuit;
static const long idMenuAbout;
static const long ID_STATUSBAR1;
//*)
//(*Declarations(sizerTestFrame)
wxButton* Button1;
wxButton* Button2;
wxButton* Button3;
wxStatusBar* StatusBar1;
//*)
DECLARE_EVENT_TABLE()
};
#endif // SIZERTESTMAIN_H
and this is the cpp
/***************************************************************
* Name: sizerTestMain.cpp
* Purpose: Code for Application Frame
* Author: RainMaker ()
* Created: 2015-11-11
* Copyright: RainMaker ()
* License:
**************************************************************/
#include "wx_pch.h"
#include "sizerTestMain.h"
#include <wx/msgdlg.h>
//(*InternalHeaders(sizerTestFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)
//helper functions
enum wxbuildinfoformat {
short_f, long_f };
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
//(*IdInit(sizerTestFrame)
const long sizerTestFrame::ID_BUTTON1 = wxNewId();
const long sizerTestFrame::ID_BUTTON2 = wxNewId();
const long sizerTestFrame::ID_BUTTON3 = wxNewId();
const long sizerTestFrame::idMenuQuit = wxNewId();
const long sizerTestFrame::idMenuAbout = wxNewId();
const long sizerTestFrame::ID_STATUSBAR1 = wxNewId();
//*)
BEGIN_EVENT_TABLE(sizerTestFrame,wxFrame)
//(*EventTable(sizerTestFrame)
//*)
END_EVENT_TABLE()
sizerTestFrame::sizerTestFrame(wxWindow* parent,wxWindowID id)
{
//(*Initialize(sizerTestFrame)
wxMenuItem* MenuItem2;
wxMenuItem* MenuItem1;
wxMenu* Menu1;
wxStaticBoxSizer* theSizer;
wxMenuBar* MenuBar1;
wxFlexGridSizer* FlexGridSizer1;
wxMenu* Menu2;
Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
theSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("I want to change this title"));
Button1 = new wxButton(this, ID_BUTTON1, _("with this"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
theSizer->Add(Button1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Button2 = new wxButton(this, ID_BUTTON2, _("or this"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
theSizer->Add(Button2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
FlexGridSizer1->Add(theSizer, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Button3 = new wxButton(this, ID_BUTTON3, _("Or this"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON3"));
FlexGridSizer1->Add(Button3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
SetSizer(FlexGridSizer1);
MenuBar1 = new wxMenuBar();
Menu1 = new wxMenu();
MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
Menu1->Append(MenuItem1);
MenuBar1->Append(Menu1, _("&File"));
Menu2 = new wxMenu();
MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
Menu2->Append(MenuItem2);
MenuBar1->Append(Menu2, _("Help"));
SetMenuBar(MenuBar1);
StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
int __wxStatusBarWidths_1[1] = { -1 };
int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
SetStatusBar(StatusBar1);
FlexGridSizer1->Fit(this);
FlexGridSizer1->SetSizeHints(this);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&sizerTestFrame::OnButton1Click);
Connect(ID_BUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&sizerTestFrame::OnButton3Click);
Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&sizerTestFrame::OnQuit);
Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&sizerTestFrame::OnAbout);
//*)
}
sizerTestFrame::~sizerTestFrame()
{
//(*Destroy(sizerTestFrame)
//*)
}
void sizerTestFrame::OnQuit(wxCommandEvent& event)
{
Close();
}
void sizerTestFrame::OnAbout(wxCommandEvent& event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
void sizerTestFrame::OnButton1Click(wxCommandEvent& event)
{
}
void sizerTestFrame::OnButton3Click(wxCommandEvent& event)
{
}
I solved my problem ! !
This is what did.
1 - I noticed that the sizer was not included in the header so I looked for the ways to include it from wxSmith. The magic came from the "isMember" checkBox in the properties table. It seems that this is the way of including it to the header and then accessing it.
2 - I created a member function at the end of the constructor:
void IbDialogue::setFonts();
3 - In the function implementation, I accessed the wxStaticBox of the wxStaticBoxSizer and set it like this:
wxFont* nf = new wxFont;
StaticBoxSizer7->GetStaticBox()->SetFont(nf->Bold());
And VOILĂ€ the font is mastered ! ! !
for (int i=0; i<1000000; i++)
cout<<(HA HA HA HA);