wxWidgets: callback-Frage
-
Hallo,
Ich habe ein selbst geschriebenes Control (wxMyOwnTestCtrl), welches ich meinem Frame (myFrame) mehrere male aufrufe.
Dann möchte ich verschiedene nicht GUI-Funktionen aufrufen, und zwar dann, wenn der Event LeftDown()in wxMyOwnTestCtrl::OnMouseEvent(wxMouseEvent& event) ausgelöst wird.
Zum Bespiel:
1. Mit der Instance myOwnTestCtrl soll die Funktion
int myFunc1(int x, in y)
aufgerufen werden
2. Mit der Instance myOwnTestCtrl2 soll die Funktion
vector myFunc2(string z)
aufgerufen werden.
Die jeweiligen Aufrufe der verschiedenen Funktionen möchte ich in meinem Frame (myFrame) aufbereiten und dann mit testTextCtrl anzeigen.
Ich denke, so etwas wird mit dem Callback-Mechanismus gelöst, ich bin aber ein c++/wxWigets newbie, so dass ich für Lösungsmöglichkeiten dankbar bin.
Viele Grüsse Steffen.Test1.h:
#pragma once #include "wx/wx.h" enum { ID_Quit = 1, ID_About, }; class MyApp: public wxApp { virtual bool OnInit(); }; class MyFrame: public wxFrame { public: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); ~MyFrame(); void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); DECLARE_EVENT_TABLE() };
Test1.cpp:
#include <winsock2.h> #include "Test1.h" // #include "Ping.h" #include "wxMyOwnTestCtrl.h" #include <string> #define TEST_ID 4711 BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(ID_Quit, MyFrame::OnQuit) EVT_MENU(ID_About, MyFrame::OnAbout) END_EVENT_TABLE() IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { MyFrame *frame = new MyFrame( _("Test"), wxPoint(50, 50), wxSize(450,340) ); frame->Show(true); SetTopWindow(frame); return true; } MyFrame::~MyFrame() { } MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame( NULL, -1, title, pos, size ) { wxMenu *menuFile = new wxMenu; menuFile->Append( ID_About, _("&About...") ); menuFile->AppendSeparator(); menuFile->Append( ID_Quit, _("E&xit") ); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append( menuFile, _("&File") ); SetMenuBar( menuBar ); CreateStatusBar(); SetStatusText( _("Welcome to wxWidgets!") ); wxBoxSizer *testSizer = new wxBoxSizer(wxVERTICAL); wxTextCtrl *testTextCtrl = new wxTextCtrl(this, 99, _("test output"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_PROCESS_TAB|wxTE_RICH); testSizer->Add(testTextCtrl, 0, wxALL, 20); wxMyOwnTestCtrl *myOwnTestCtrl = new wxMyOwnTestCtrl(); myOwnTestCtrl->Create(this, TEST_ID, wxDefaultPosition, wxSize(120,30), wxBORDER_NONE, wxDefaultValidator); testSizer->Add(myOwnTestCtrl,0, wxALL, 20); wxMyOwnTestCtrl *myOwnTestCtrl2 = new wxMyOwnTestCtrl(); myOwnTestCtrl2->Create(this, TEST_ID+1, wxDefaultPosition, wxSize(120,30), wxBORDER_NONE, wxDefaultValidator); testSizer->Add(myOwnTestCtrl2,0, wxALL, 20); testSizer->SetSizeHints(this); SetSizer(testSizer); Refresh(); } void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(TRUE); } void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageBox( _("This is a wxWidgets test"), _("test"), wxOK | wxICON_INFORMATION, this); }
myOwnTestCtrl.h:
#pragma once #include "wx/wx.h" #include "wx/msw/control.h" class wxMyOwnTestCtrl : public wxControl { DECLARE_DYNAMIC_CLASS(wxMyOwnTestCtrl) DECLARE_EVENT_TABLE() public: wxMyOwnTestCtrl(void); ~wxMyOwnTestCtrl(void); wxMyOwnTestCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxSUNKEN_BORDER, const wxValidator& validator = wxDefaultValidator) { Create(parent, id, pos, size, style, validator); } bool Create(wxWindow *parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxSUNKEN_BORDER, const wxValidator& validator = wxDefaultValidator); void OnPaint(wxPaintEvent& event); void OnMouseEvent(wxMouseEvent& event); int OnClick(wxMouseEvent& event); };
myOwnTestCtrl.cpp:
#include "wxMyOwnTestCtrl.h" BEGIN_EVENT_TABLE(wxMyOwnTestCtrl, wxControl) EVT_PAINT(wxMyOwnTestCtrl::OnPaint) EVT_MOUSE_EVENTS(wxMyOwnTestCtrl::OnMouseEvent) END_EVENT_TABLE() IMPLEMENT_DYNAMIC_CLASS(wxMyOwnTestCtrl, wxControl) wxMyOwnTestCtrl::wxMyOwnTestCtrl(void) {} wxMyOwnTestCtrl::~wxMyOwnTestCtrl(void){} bool wxMyOwnTestCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator) { if (!wxControl::Create(parent, id, pos, size, style, validator)) return false; SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); Connect(wxEVT_PAINT, wxPaintEventHandler(wxMyOwnTestCtrl::OnPaint)); return true; } void wxMyOwnTestCtrl::OnPaint(wxPaintEvent& event) { wxString testString = _("test string"); wxPaintDC dc(this); wxRect rect = GetClientRect(); int topMargin = 2; int leftMargin = 2; wxCoord width, height; dc.GetTextExtent(testString, &width, &height); int x = wxMax(leftMargin, ((rect.GetWidth() - width)/2)); int y = wxMax(topMargin, ((rect.GetHeight() - height)/2)); dc.DrawText(testString, x, y); dc.SetFont(wxNullFont); } void wxMyOwnTestCtrl::OnMouseEvent(wxMouseEvent& event) { if (event.LeftDown()) { this->m_backgroundColour = *wxBLUE; Refresh(); /*************************************/ /**** here call of my function(s) ****/ /*************************************/ } else if (event.Entering()) { this->m_backgroundColour = *wxRED; Refresh(); } else if (event.Leaving()) { this->m_backgroundColour = *wxGREEN; Refresh(); } }
-
Die Entscheidung, welcher der Funktionen (myFunc1, myFunc2, ... oder myFunc<n>) mit den entsprechenden Parametern aufgerufen wird, soll im Frame (myFrame) entschieden werden.
Die Implementierung im Control (wxMyOwnTestCtrl) soll unabhängig vom Inhalt der jewiligen Funktionen sein.
Ich suche nach einer Implementierungsmöglichkeit zum Aufruf der Funktionen mit dem Control (wxMyOwnTestCtrl) und die Möglichkeit der Auswertung der jeweiligen Return Werte.
-
Hallo mit "Using existing event classes" von http://docs.wxwidgets.org/2.8/wx_eventhandlingoverview.html#customevents konnte ich mein Problem lösen.
Aller Anfang ist schwer, Steffen