X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b21348b405d443f8bb150087cd9502b6a3397ac4..8a31648287be0ef976f133de2786b137f1e98340:/samples/ipc/client.cpp diff --git a/samples/ipc/client.cpp b/samples/ipc/client.cpp index e0380dd666..24132a36c2 100644 --- a/samples/ipc/client.cpp +++ b/samples/ipc/client.cpp @@ -32,13 +32,13 @@ // we're using TCP/IP or real DDE. #include "ipcsetup.h" -#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) -#include "mondrian.xpm" -#endif - #include "wx/datetime.h" #include "client.h" +#if !defined(__WXMSW__) && !defined(__WXPM__) + #include "../sample.xpm" +#endif + // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -76,8 +76,11 @@ END_EVENT_TABLE() // main frame bool MyApp::OnInit() { + if ( !wxApp::OnInit() ) + return false; + // Create the main frame window - m_frame = new MyFrame(NULL, _T("Client")); + m_frame = new MyFrame(NULL, wxT("Client")); m_frame->Show(true); return true; @@ -94,16 +97,16 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title) : wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(400, 300)) { // Give it an icon - SetIcon(wxICON(mondrian)); + SetIcon(wxICON(sample)); // Make a menubar wxMenu *file_menu = new wxMenu; - file_menu->Append(wxID_EXIT, _T("&Quit\tCtrl-Q")); + file_menu->Append(wxID_EXIT, wxT("&Quit\tCtrl-Q")); wxMenuBar *menu_bar = new wxMenuBar; - menu_bar->Append(file_menu, _T("&File")); + menu_bar->Append(file_menu, wxT("&File")); // Associate the menu bar with the frame SetMenuBar(menu_bar); @@ -112,17 +115,17 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title) SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)); // add the controls to the frame - wxString strs4[] = + wxString strs4[] = { - IPC_SERVICE, _T("...") + IPC_SERVICE, wxT("...") }; - wxString strs5[] = + wxString strs5[] = { - IPC_HOST, _T("...") + IPC_HOST, wxT("...") }; - wxString strs6[] = + wxString strs6[] = { - IPC_TOPIC, _T("...") + IPC_TOPIC, wxT("...") }; wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL ); @@ -192,7 +195,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title) wxStaticBox *item14 = new wxStaticBox( this, -1, wxT("Client log") ); wxStaticBoxSizer *item13 = new wxStaticBoxSizer( item14, wxVERTICAL ); - wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxT(""), wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE ); + wxTextCtrl *item15 = new wxTextCtrl( this, ID_LOG, wxEmptyString, wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE ); item13->Add( item15, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); item0->Add( item13, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 ); @@ -207,18 +210,18 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title) GetTopic()->SetSelection(0); wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog()); delete wxLog::SetActiveTarget(logWindow); - wxLogMessage(_T("Click on Connect to connect to the server")); - Enable(); + wxLogMessage(wxT("Click on Connect to connect to the server")); + EnableControls(); } -void MyFrame::Enable() +void MyFrame::EnableControls() { GetStart()->Enable(m_client == NULL); GetServername()->Enable(m_client == NULL); GetHostname()->Enable(m_client == NULL); GetTopic()->Enable(m_client == NULL); - const bool isConnected = m_client->IsConnected(); + const bool isConnected = (m_client != NULL && m_client->IsConnected()); GetDisconnect()->Enable(m_client != NULL && isConnected); GetStartAdvise()->Enable(m_client != NULL && isConnected); GetStopAdvise()->Enable(m_client != NULL && isConnected); @@ -229,11 +232,8 @@ void MyFrame::Enable() void MyFrame::OnClose(wxCloseEvent& event) { - if (m_client) - { - delete m_client; - m_client = NULL; - } + wxDELETE(m_client); + event.Skip(); } @@ -252,24 +252,23 @@ void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event)) m_client = new MyClient; bool retval = m_client->Connect(hostname, servername, topic); - wxLogMessage(_T("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"), + wxLogMessage(wxT("Client host=\"%s\" port=\"%s\" topic=\"%s\" %s"), hostname.c_str(), servername.c_str(), topic.c_str(), - retval ? _T("connected") : _T("failed to connect")); + retval ? wxT("connected") : wxT("failed to connect")); if (!retval) { - delete m_client; - m_client = NULL; + wxDELETE(m_client); } - Enable(); + EnableControls(); } void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) ) { - if (GetServername()->GetStringSelection() == _T("...")) + if (GetServername()->GetStringSelection() == wxT("...")) { - wxString s = wxGetTextFromUser(_T("Specify the name of the server"), - _T("Server Name"), _(""), this); + wxString s = wxGetTextFromUser(wxT("Specify the name of the server"), + wxT("Server Name"), wxEmptyString, this); if (!s.IsEmpty() && s != IPC_SERVICE) { GetServername()->Insert(s, 0); @@ -280,10 +279,10 @@ void MyFrame::OnServername( wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) ) { - if (GetHostname()->GetStringSelection() == _T("...")) + if (GetHostname()->GetStringSelection() == wxT("...")) { - wxString s = wxGetTextFromUser(_T("Specify the name of the host (ignored under DDE)"), - _T("Host Name"), _(""), this); + wxString s = wxGetTextFromUser(wxT("Specify the name of the host (ignored under DDE)"), + wxT("Host Name"), wxEmptyString, this); if (!s.IsEmpty() && s != IPC_HOST) { GetHostname()->Insert(s, 0); @@ -294,10 +293,10 @@ void MyFrame::OnHostname( wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) ) { - if (GetTopic()->GetStringSelection() == _T("...")) + if (GetTopic()->GetStringSelection() == wxT("...")) { - wxString s = wxGetTextFromUser(_T("Specify the name of the topic"), - _T("Topic Name"), _(""), this); + wxString s = wxGetTextFromUser(wxT("Specify the name of the topic"), + wxT("Topic Name"), wxEmptyString, this); if (!s.IsEmpty() && s != IPC_TOPIC) { GetTopic()->Insert(s, 0); @@ -308,40 +307,38 @@ void MyFrame::OnTopic( wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event)) { - Disconnect(); + Disconnect(); } void MyFrame::Disconnect() { - delete m_client; - m_client = NULL; - Enable(); + wxDELETE(m_client); + EnableControls(); } void MyFrame::OnStartAdvise(wxCommandEvent& WXUNUSED(event)) { - m_client->GetConnection()->StartAdvise(_T("something")); + m_client->GetConnection()->StartAdvise(wxT("something")); } void MyFrame::OnStopAdvise(wxCommandEvent& WXUNUSED(event)) { - m_client->GetConnection()->StopAdvise(_T("something")); + m_client->GetConnection()->StopAdvise(wxT("something")); } void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event)) { if (m_client->IsConnected()) { - wxString s = _T("Date"); + wxString s = wxT("Date"); - m_client->GetConnection()->Execute((wxChar *)s.c_str()); - m_client->GetConnection()->Execute((wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar)); -#if wxUSE_DDE_FOR_IPC - wxLogMessage(_T("DDE Execute can only be used to send text strings, not arbitrary data.\nThe type argument will be ignored, text truncated, converted to Unicode and null terminated.")); -#endif + m_client->GetConnection()->Execute(s); + m_client->GetConnection()->Execute((const char *)s.c_str(), s.length() + 1); char bytes[3]; - bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; - m_client->GetConnection()->Execute((wxChar *)bytes, 3, wxIPC_PRIVATE); + bytes[0] = '1'; + bytes[1] = '2'; + bytes[2] = '3'; + m_client->GetConnection()->Execute(bytes, WXSIZEOF(bytes)); } } @@ -350,12 +347,12 @@ void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event)) if (m_client->IsConnected()) { wxString s = wxDateTime::Now().Format(); - m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str()); - s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); - m_client->GetConnection()->Poke(_T("Date"), (wxChar *)s.c_str(), (s.Length() + 1) * sizeof(wxChar)); + m_client->GetConnection()->Poke(wxT("Date"), s); + s = wxDateTime::Now().FormatTime() + wxT(" ") + wxDateTime::Now().FormatDate(); + m_client->GetConnection()->Poke(wxT("Date"), (const char *)s.c_str(), s.length() + 1); char bytes[3]; bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; - m_client->GetConnection()->Poke(_T("bytes[3]"), (wxChar *)bytes, 3, wxIPC_PRIVATE); + m_client->GetConnection()->Poke(wxT("bytes[3]"), bytes, 3, wxIPC_PRIVATE); } } @@ -363,10 +360,10 @@ void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event)) { if (m_client->IsConnected()) { - int size; - m_client->GetConnection()->Request(_T("Date")); - m_client->GetConnection()->Request(_T("Date+len"), &size); - m_client->GetConnection()->Request(_T("bytes[3]"), &size, wxIPC_PRIVATE); + size_t size; + m_client->GetConnection()->Request(wxT("Date")); + m_client->GetConnection()->Request(wxT("Date+len"), &size); + m_client->GetConnection()->Request(wxT("bytes[3]"), &size, wxIPC_PRIVATE); } } @@ -382,7 +379,7 @@ bool MyClient::Connect(const wxString& sHost, const wxString& sService, const wx { // suppress the log messages from MakeConnection() wxLogNull nolog; - + m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic); return m_connection != NULL; } @@ -397,10 +394,9 @@ void MyClient::Disconnect() if (m_connection) { m_connection->Disconnect(); - delete m_connection; - m_connection = NULL; - wxGetApp().GetFrame()->Enable(); - wxLogMessage(_T("Client disconnected from server")); + wxDELETE(m_connection); + wxGetApp().GetFrame()->EnableControls(); + wxLogMessage(wxT("Client disconnected from server")); } } @@ -413,68 +409,40 @@ MyClient::~MyClient() // MyConnection // ---------------------------------------------------------------------------- -void MyConnection::Log(const wxString& command, const wxString& topic, - const wxString& item, wxChar *data, int size, wxIPCFormat format) +bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data, + size_t size, wxIPCFormat format) { - wxString s; - if (topic.IsEmpty() && item.IsEmpty()) - s.Printf(_T("%s("), command.c_str()); - else if (topic.IsEmpty()) - s.Printf(_T("%s(item=\"%s\","), command.c_str(), item.c_str()); - else if (item.IsEmpty()) - s.Printf(_T("%s(topic=\"%s\","), command.c_str(), topic.c_str()); - else - s.Printf(_T("%s(topic=\"%s\",item=\"%s\","), command.c_str(), topic.c_str(), item.c_str()); - - if (format == wxIPC_TEXT || format == wxIPC_UNICODETEXT) - wxLogMessage(_T("%s\"%s\",%d)"), s.c_str(), data, size); - else if (format == wxIPC_PRIVATE) - { - if (size == 3) - { - char *bytes = (char *)data; - wxLogMessage(_T("%s'%c%c%c',%d)"), s.c_str(), bytes[0], bytes[1], bytes[2], size); - } - else - wxLogMessage(_T("%s...,%d)"), s.c_str(), size); - } - else if (format == wxIPC_INVALID) - wxLogMessage(_T("%s[invalid data],%d)"), s.c_str(), size); -} - -bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, wxChar *data, - int size, wxIPCFormat format) -{ - Log(_T("OnAdvise"), topic, item, data, size, format); + Log(wxT("OnAdvise"), topic, item, data, size, format); return true; } bool MyConnection::OnDisconnect() { - wxLogMessage(_T("OnDisconnect()")); + wxLogMessage(wxT("OnDisconnect()")); wxGetApp().GetFrame()->Disconnect(); return true; } -bool MyConnection::Execute(const wxChar *data, int size, wxIPCFormat format) +bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format) { - Log(_T("Execute"), _T(""), _T(""), (wxChar *)data, size, format); - bool retval = wxConnection::Execute(data, size, format); + Log(wxT("Execute"), wxEmptyString, wxEmptyString, data, size, format); + bool retval = wxConnection::DoExecute(data, size, format); if (!retval) - wxLogMessage(_T("Execute failed!")); + { + wxLogMessage(wxT("Execute failed!")); + } return retval; } -wxChar *MyConnection::Request(const wxString& item, int *size, wxIPCFormat format) +const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format) { - wxChar *data = wxConnection::Request(item, size, format); - Log(_T("Request"), _T(""), item, data, size ? *size : -1, format); + const void *data = wxConnection::Request(item, size, format); + Log(wxT("Request"), wxEmptyString, item, data, size ? *size : wxNO_LEN, format); return data; } -bool MyConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format) +bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format) { - Log(_T("Poke"), _T(""), item, data, size, format); - return wxConnection::Poke(item, data, size, format); + Log(wxT("Poke"), wxEmptyString, item, data, size, format); + return wxConnection::DoPoke(item, data, size, format); } -