removed automatically generated code for frame layout with human-readable version; some other cleanup

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@54611 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-07-14 01:20:33 +00:00
parent d7ad5a0289
commit 589c1174f4
2 changed files with 189 additions and 175 deletions

View File

@@ -32,8 +32,8 @@
// we're using TCP/IP or real DDE. // we're using TCP/IP or real DDE.
#include "ipcsetup.h" #include "ipcsetup.h"
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) #if !defined(__WXMSW__) && !defined(__WXPM__)
#include "mondrian.xpm" #include "../sample.xpm"
#endif #endif
#include "server.h" #include "server.h"
@@ -47,10 +47,10 @@
IMPLEMENT_APP(MyApp) IMPLEMENT_APP(MyApp)
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU (wxID_EXIT, MyFrame::OnExit)
EVT_CLOSE( MyFrame::OnClose ) EVT_CLOSE( MyFrame::OnClose )
EVT_BUTTON( ID_START, MyFrame::OnStart )
EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServerName ) EVT_BUTTON( ID_START, MyFrame::OnStart )
EVT_CHOICE( ID_SERVERNAME, MyFrame::OnServerName )
EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect ) EVT_BUTTON( ID_DISCONNECT, MyFrame::OnDisconnect )
EVT_BUTTON( ID_ADVISE, MyFrame::OnAdvise ) EVT_BUTTON( ID_ADVISE, MyFrame::OnAdvise )
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -70,17 +70,12 @@ bool MyApp::OnInit()
return false; return false;
// Create the main frame window // Create the main frame window
m_frame = new MyFrame(NULL, _T("Server")); m_frame = new MyFrame(NULL, "Server");
m_frame->Show(true); m_frame->Show(true);
return true; return true;
} }
int MyApp::OnExit()
{
return 0;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// MyFrame // MyFrame
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -93,77 +88,69 @@ MyFrame::MyFrame(wxFrame *frame, const wxString& title)
CreateStatusBar(); CreateStatusBar();
#endif // wxUSE_STATUSBAR #endif // wxUSE_STATUSBAR
// Give it an icon SetIcon(wxICON(sample));
SetIcon(wxICON(mondrian));
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(wxID_EXIT, _T("&Quit\tCtrl-Q"));
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, _T("&File"));
// Associate the menu bar with the frame
SetMenuBar(menu_bar);
// set a dialog background
SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
// add the controls to the frame
wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
wxFlexGridSizer *item2 = new wxFlexGridSizer( 2, 0, 0 );
item2->AddGrowableCol( 1 );
wxButton *item3 = new wxButton( this, ID_START, wxT("Start Server"), wxDefaultPosition, wxDefaultSize, 0 );
item2->Add( item3, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
wxString strs4[] =
{
IPC_SERVICE, _T("...")
};
wxChoice *item4 = new wxChoice( this, ID_SERVERNAME, wxDefaultPosition, wxSize(100,-1), 2, strs4, 0 );
item2->Add( item4, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
wxButton *item5 = new wxButton( this, ID_DISCONNECT, wxT("Disconnect Client"), wxDefaultPosition, wxDefaultSize, 0 );
item2->Add( item5, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
wxButton *item6 = new wxButton( this, ID_ADVISE, wxT("Advise"), wxDefaultPosition, wxDefaultSize, 0 );
item2->Add( item6, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
item2->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
item1->Add( item2, 1, wxALIGN_CENTER|wxALL, 5 );
item0->Add( item1, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
wxStaticBox *item8 = new wxStaticBox( this, -1, wxT("Server log") );
wxStaticBoxSizer *item7 = new wxStaticBoxSizer( item8, wxVERTICAL );
wxTextCtrl *item9 = new wxTextCtrl( this, ID_LOG, wxT(""), wxDefaultPosition, wxSize(500,140), wxTE_MULTILINE );
item7->Add( item9, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
item0->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
SetSizer( item0 );
item0->SetSizeHints( this );
// status
m_server = NULL; m_server = NULL;
wxPanel * const panel = new wxPanel(this);
wxBoxSizer * const sizerMain = new wxBoxSizer( wxVERTICAL );
wxFlexGridSizer * const sizerCmds = new wxFlexGridSizer( 2, 0, 0 );
sizerCmds->AddGrowableCol( 1 );
wxButton *btn;
btn = new wxButton(panel, ID_START, "&Start Server");
sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
const wxString choices[] = { IPC_SERVICE, "..." };
wxChoice * const choice = new wxChoice
(
panel,
ID_SERVERNAME,
wxDefaultPosition, wxSize(100, -1),
WXSIZEOF(choices), choices
);
sizerCmds->Add(choice, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
btn = new wxButton(panel, ID_DISCONNECT, "&Disconnect Client");
sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
sizerCmds->AddSpacer(20);
btn = new wxButton( panel, ID_ADVISE, "&Advise");
sizerCmds->Add(btn, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
sizerCmds->AddSpacer(20);
sizerMain->Add(sizerCmds, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
wxStaticBoxSizer * const
sizerLog = new wxStaticBoxSizer(wxVERTICAL, panel, "Server &log");
wxTextCtrl * const textLog = new wxTextCtrl
(
panel,
wxID_ANY,
"",
wxDefaultPosition, wxSize(500, 140),
wxTE_MULTILINE
);
sizerLog->Add(textLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
sizerMain->Add(sizerLog, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
panel->SetSizer(sizerMain);
sizerMain->SetSizeHints(panel);
SetClientSize(panel->GetSize());
GetServername()->SetSelection(0); GetServername()->SetSelection(0);
wxLogTextCtrl *logWindow = new wxLogTextCtrl(GetLog()); wxLogTextCtrl *logWindow = new wxLogTextCtrl(textLog);
delete wxLog::SetActiveTarget(logWindow); delete wxLog::SetActiveTarget(logWindow);
wxLogMessage(_T("Click on Start to start the server")); wxLogMessage("Click on Start to start the server");
Enable(); UpdateUI();
} }
void MyFrame::Enable() void MyFrame::UpdateUI()
{ {
GetStart()->Enable(m_server == NULL); GetStart()->Enable(m_server == NULL);
GetServername()->Enable(m_server == NULL); GetServername()->Enable(m_server == NULL);
@@ -181,11 +168,6 @@ void MyFrame::OnClose(wxCloseEvent& event)
event.Skip(); event.Skip();
} }
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
{ {
// Create a new server // Create a new server
@@ -193,29 +175,35 @@ void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
wxString servername = GetServername()->GetStringSelection(); wxString servername = GetServername()->GetStringSelection();
if (m_server->Create(servername)) if (m_server->Create(servername))
{ {
wxLogMessage(_T("Server %s started"), servername.c_str()); wxLogMessage("Server %s started", servername);
#if wxUSE_DDE_FOR_IPC #if wxUSE_DDE_FOR_IPC
wxLogMessage(_T("Server uses DDE")); wxLogMessage("Server uses DDE");
#else // !wxUSE_DDE_FOR_IPC #else // !wxUSE_DDE_FOR_IPC
wxLogMessage(_T("Server uses TCP")); wxLogMessage("Server uses TCP");
#endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC #endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC
} }
else else
{ {
wxLogMessage(_T("Server %s failed to start"), servername.c_str()); wxLogMessage("Server %s failed to start", servername);
delete m_server; delete m_server;
m_server = NULL; m_server = NULL;
} }
Enable(); UpdateUI();
} }
void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) )
{ {
if (GetServername()->GetStringSelection() == _T("...")) if ( GetServername()->GetStringSelection() == "..." )
{ {
wxString s = wxGetTextFromUser(_T("Specify the name of the server"), wxString s = wxGetTextFromUser
_T("Server Name"), _(""), this); (
if (!s.IsEmpty() && s != IPC_SERVICE) "Specify the name of the server",
"Server Name",
"",
this
);
if ( !s.empty() && s != IPC_SERVICE )
{ {
GetServername()->Insert(s, 0); GetServername()->Insert(s, 0);
GetServername()->SetSelection(0); GetServername()->SetSelection(0);
@@ -226,7 +214,7 @@ void MyFrame::OnServerName( wxCommandEvent& WXUNUSED(event) )
void MyFrame::Disconnect() void MyFrame::Disconnect()
{ {
m_server->Disconnect(); m_server->Disconnect();
Enable(); UpdateUI();
} }
void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDisconnect(wxCommandEvent& WXUNUSED(event))
@@ -255,48 +243,51 @@ MyServer::~MyServer()
wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
{ {
wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic.c_str()); wxLogMessage("OnAcceptConnection(\"%s\")", topic);
if ( topic == IPC_TOPIC ) if ( topic == IPC_TOPIC )
{ {
m_connection = new MyConnection(); m_connection = new MyConnection();
wxGetApp().GetFrame()->Enable(); wxGetApp().GetFrame()->UpdateUI();
wxLogMessage(_T("Connection accepted")); wxLogMessage("Connection accepted");
return m_connection; return m_connection;
} }
// unknown topic //else: unknown topic
wxLogMessage("Unknown topic, connection refused");
return NULL; return NULL;
} }
void MyServer::Disconnect() void MyServer::Disconnect()
{ {
if (m_connection) if ( m_connection )
{ {
m_connection->Disconnect();
delete m_connection; delete m_connection;
m_connection = NULL; m_connection = NULL;
wxGetApp().GetFrame()->Enable(); wxGetApp().GetFrame()->UpdateUI();
wxLogMessage(_T("Disconnected client")); wxLogMessage("Disconnected client");
} }
} }
void MyServer::Advise() void MyServer::Advise()
{ {
if (CanAdvise()) if ( CanAdvise() )
{ {
wxString s = wxDateTime::Now().Format(); const wxDateTime now = wxDateTime::Now();
m_connection->Advise(m_connection->m_sAdvise, s);
s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); m_connection->Advise(m_connection->m_advise, now.Format());
m_connection->Advise(m_connection->m_sAdvise, (const char *)s.c_str(), s.Length() + 1);
const wxString s = now.FormatTime() + " " + now.FormatDate();
m_connection->Advise(m_connection->m_advise, s.mb_str(), wxNO_LEN);
#if wxUSE_DDE_FOR_IPC #if wxUSE_DDE_FOR_IPC
wxLogMessage(_T("DDE Advise type argument cannot be wxIPC_PRIVATE. The client will receive it as wxIPC_TEXT, and receive the correct no of bytes, but not print a correct log entry.")); wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
"The client will receive it as wxIPC_TEXT, "
" and receive the correct no of bytes, "
"but not print a correct log entry.");
#endif #endif
char bytes[3]; char bytes[3] = { '1', '2', '3' };
bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; m_connection->Advise(m_connection->m_advise, bytes, 3, wxIPC_PRIVATE);
m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_PRIVATE);
// this works, but the log treats it as a string now
// m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
} }
} }
@@ -304,80 +295,99 @@ void MyServer::Advise()
// MyConnection // MyConnection
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool MyConnection::OnExecute(const wxString& topic, bool
const void *data, size_t size, wxIPCFormat format) MyConnection::OnExecute(const wxString& topic,
const void *data,
size_t size,
wxIPCFormat format)
{ {
Log(_T("OnExecute"), topic, _T(""), data, size, format); Log("OnExecute", topic, "", data, size, format);
return true; return true;
} }
bool MyConnection::OnPoke(const wxString& topic, bool
const wxString& item, const void *data, size_t size, wxIPCFormat format) MyConnection::OnPoke(const wxString& topic,
const wxString& item,
const void *data,
size_t size,
wxIPCFormat format)
{ {
Log(_T("OnPoke"), topic, item, data, size, format); Log("OnPoke", topic, item, data, size, format);
return wxConnection::OnPoke(topic, item, data, size, format); return wxConnection::OnPoke(topic, item, data, size, format);
} }
const void *MyConnection::OnRequest(const wxString& topic, const void *
const wxString& item, size_t *size, wxIPCFormat format) MyConnection::OnRequest(const wxString& topic,
const wxString& item,
size_t *size,
wxIPCFormat format)
{ {
const void *data; *size = 0;
if (item == _T("Date"))
wxString afterDate;
if ( item.StartsWith("Date", &afterDate) )
{ {
m_sRequestDate = wxDateTime::Now().Format(); const wxDateTime now = wxDateTime::Now();
data = m_sRequestDate.c_str();
*size = wxNO_LEN; if ( afterDate.empty() )
{
m_requestData = now.Format();
*size = wxNO_LEN;
}
else if ( afterDate == "+len" )
{
m_requestData = now.FormatTime() + " " + now.FormatDate();
*size = strlen(m_requestData.mb_str()) + 1;
}
} }
else if (item == _T("Date+len")) else if ( item == "bytes[3]" )
{ {
m_sRequestDate = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); m_requestData = "123";
data = m_sRequestDate.c_str();
*size = m_sRequestDate.Length() + 1;
}
else if (item == _T("bytes[3]"))
{
data = m_achRequestBytes;
m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3';
*size = 3; *size = 3;
} }
else
if ( !*size )
{ {
data = NULL; wxLogMessage("Unknown request for \"%s\"", item);
*size = 0; return NULL;
} }
Log(_T("OnRequest"), topic, item, data, *size, format);
const void * const data = m_requestData.mb_str();
Log("OnRequest", topic, item, data, *size, format);
return data; return data;
} }
bool MyConnection::OnStartAdvise(const wxString& topic, bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
const wxString& item)
{ {
wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); wxLogMessage("OnStartAdvise(\"%s\", \"%s\")", topic, item);
wxLogMessage(_T("Returning true")); wxLogMessage("Returning true");
m_sAdvise = item; m_advise = item;
wxGetApp().GetFrame()->Enable(); wxGetApp().GetFrame()->UpdateUI();
return true; return true;
} }
bool MyConnection::OnStopAdvise(const wxString& topic, bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
const wxString& item)
{ {
wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item);
wxLogMessage(_T("Returning true")); wxLogMessage("Returning true");
m_sAdvise.Empty(); m_advise.clear();
wxGetApp().GetFrame()->Enable(); wxGetApp().GetFrame()->UpdateUI();
return true; return true;
} }
bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format) bool
MyConnection::DoAdvise(const wxString& item,
const void *data,
size_t size,
wxIPCFormat format)
{ {
Log(_T("Advise"), _T(""), item, data, size, format); Log("Advise", "", item, data, size, format);
return wxConnection::DoAdvise(item, data, size, format); return wxConnection::DoAdvise(item, data, size, format);
} }
bool MyConnection::OnDisconnect() bool MyConnection::OnDisconnect()
{ {
wxLogMessage(_T("OnDisconnect()")); wxLogMessage("OnDisconnect()");
wxGetApp().GetFrame()->Disconnect(); wxGetApp().GetFrame()->Disconnect();
return true; return true;
} }

View File

@@ -11,11 +11,13 @@
#include "connection.h" #include "connection.h"
#define ID_START 10000 enum
#define ID_DISCONNECT 10001 {
#define ID_ADVISE 10002 ID_START = 10000,
#define ID_LOG 10003 ID_DISCONNECT,
#define ID_SERVERNAME 10004 ID_ADVISE,
ID_SERVERNAME,
};
// Define a new application // Define a new application
class MyServer; class MyServer;
@@ -25,11 +27,10 @@ class MyApp : public wxApp
{ {
public: public:
virtual bool OnInit(); virtual bool OnInit();
virtual int OnExit(); MyFrame *GetFrame() { return m_frame; }
MyFrame *GetFrame() { return m_frame; };
protected: protected:
MyFrame *m_frame; MyFrame *m_frame;
}; };
DECLARE_APP(MyApp) DECLARE_APP(MyApp)
@@ -40,10 +41,9 @@ class MyFrame : public wxFrame
public: public:
MyFrame(wxFrame *frame, const wxString& title); MyFrame(wxFrame *frame, const wxString& title);
void OnExit(wxCommandEvent& event);
void OnClose(wxCloseEvent& event); void OnClose(wxCloseEvent& event);
void Enable(); void UpdateUI();
void Disconnect(); void Disconnect();
protected: protected:
@@ -51,10 +51,9 @@ protected:
wxChoice* GetServername() { return (wxChoice*) FindWindow( ID_SERVERNAME ); } wxChoice* GetServername() { return (wxChoice*) FindWindow( ID_SERVERNAME ); }
wxButton* GetDisconnect() { return (wxButton*) FindWindow( ID_DISCONNECT ); } wxButton* GetDisconnect() { return (wxButton*) FindWindow( ID_DISCONNECT ); }
wxButton* GetAdvise() { return (wxButton*) FindWindow( ID_ADVISE ); } wxButton* GetAdvise() { return (wxButton*) FindWindow( ID_ADVISE ); }
wxTextCtrl* GetLog() { return (wxTextCtrl*) FindWindow( ID_LOG ); }
MyServer *m_server; MyServer *m_server;
void OnStart( wxCommandEvent &event ); void OnStart( wxCommandEvent &event );
void OnServerName( wxCommandEvent &event ); void OnServerName( wxCommandEvent &event );
@@ -75,26 +74,31 @@ public:
virtual bool DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format); virtual bool DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format);
virtual bool OnDisconnect(); virtual bool OnDisconnect();
wxString m_sAdvise; // topic for which we advise the client or empty if none
wxString m_advise;
protected: protected:
wxString m_sRequestDate; // the data returned by last OnRequest(): we keep it in this wxString to
char m_achRequestBytes[3]; // ensure that the pointer we return from OnRequest() stays valid
wxString m_requestData;
}; };
class MyServer: public wxServer class MyServer : public wxServer
{ {
public: public:
MyServer(); MyServer();
~MyServer(); virtual ~MyServer();
void Disconnect(); void Disconnect();
bool IsConnected() { return m_connection != NULL; }; bool IsConnected() { return m_connection != NULL; }
MyConnection *GetConnection() { return m_connection; }; MyConnection *GetConnection() { return m_connection; }
void Advise(); void Advise();
bool CanAdvise() { return m_connection != NULL && !m_connection->m_sAdvise.IsEmpty(); }; bool CanAdvise() { return m_connection && !m_connection->m_advise.empty(); }
wxConnectionBase *OnAcceptConnection(const wxString& topic);
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
protected: protected:
MyConnection *m_connection; MyConnection *m_connection;
}; };