1999-01-27 05:01:14 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: client.cpp
|
|
|
|
// Purpose: DDE sample: client
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 25/01/99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 05:01:14 -05:00
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
2000-01-15 17:34:29 -05:00
|
|
|
#pragma hdrstop
|
1999-01-27 05:01:14 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2000-01-15 17:34:29 -05:00
|
|
|
#include "wx/wx.h"
|
1999-01-27 05:01:14 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Settings common to both executables: determines whether
|
|
|
|
// we're using TCP/IP or real DDE.
|
|
|
|
|
|
|
|
#include "ddesetup.h"
|
|
|
|
|
2001-11-24 04:28:33 -05:00
|
|
|
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
|
1999-01-27 05:01:14 -05:00
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxWin macros
|
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-27 05:01:14 -05:00
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU(CLIENT_QUIT, MyFrame::OnExit)
|
|
|
|
EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute)
|
|
|
|
EVT_MENU(CLIENT_POKE, MyFrame::OnPoke)
|
|
|
|
EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// globals
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 05:01:14 -05:00
|
|
|
char ipc_buffer[4000];
|
|
|
|
wxListBox *the_list = NULL;
|
|
|
|
|
|
|
|
MyConnection *the_connection = NULL;
|
|
|
|
MyClient *my_client ;
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 05:01:14 -05:00
|
|
|
// The `main program' equivalent, creating the windows and returning the
|
|
|
|
// main frame
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2000-07-15 15:51:35 -04:00
|
|
|
// service name (DDE classes) or port number (TCP/IP based classes)
|
|
|
|
wxString service = "4242";
|
|
|
|
|
|
|
|
// ignored under DDE, host name in TCP/IP based classes
|
|
|
|
wxString hostName = "localhost";
|
1999-01-27 05:01:14 -05:00
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
if (argc > 1)
|
2000-07-15 15:51:35 -04:00
|
|
|
service = argv[1];
|
2000-01-15 17:34:29 -05:00
|
|
|
if (argc > 2)
|
|
|
|
hostName = argv[2];
|
1999-01-27 05:01:14 -05:00
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
// Create a new client
|
|
|
|
my_client = new MyClient;
|
1999-01-27 05:01:14 -05:00
|
|
|
|
2000-03-10 19:47:45 -05:00
|
|
|
// suppress the log messages from MakeConnection()
|
2000-01-15 17:34:29 -05:00
|
|
|
{
|
2000-03-10 19:47:45 -05:00
|
|
|
wxLogNull nolog;
|
2000-07-15 15:51:35 -04:00
|
|
|
the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
|
2000-03-10 19:47:45 -05:00
|
|
|
|
|
|
|
while ( !the_connection )
|
2000-01-15 17:34:29 -05:00
|
|
|
{
|
2000-03-10 19:47:45 -05:00
|
|
|
if ( wxMessageBox("Failed to make connection to server.\nRetry?",
|
|
|
|
"Client Demo Error",
|
|
|
|
wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
|
|
|
|
{
|
|
|
|
// no server
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, "IPC TEST");
|
2000-01-15 17:34:29 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-27 05:01:14 -05:00
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
if (!the_connection->StartAdvise("Item"))
|
|
|
|
wxMessageBox("StartAdvise failed", "Client Demo Error");
|
1999-01-27 05:01:14 -05:00
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
// Create the main frame window
|
|
|
|
(new MyFrame(NULL, "Client"))->Show(TRUE);
|
2000-01-05 20:21:26 -05:00
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
return TRUE;
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
if (the_connection)
|
|
|
|
{
|
|
|
|
the_connection->Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// will delete the connection too
|
|
|
|
delete my_client;
|
1999-01-27 05:01:14 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define my frame constructor
|
2000-01-15 17:34:29 -05:00
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title)
|
|
|
|
: wxFrame(frame, -1, title)
|
1999-01-27 05:01:14 -05:00
|
|
|
{
|
|
|
|
panel = NULL;
|
2000-01-15 17:34:29 -05:00
|
|
|
|
|
|
|
// Give it an icon
|
|
|
|
SetIcon(wxICON(mondrian));
|
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
|
|
file_menu->Append(CLIENT_EXECUTE, "Execute");
|
|
|
|
file_menu->Append(CLIENT_REQUEST, "Request");
|
|
|
|
file_menu->Append(CLIENT_POKE, "Poke");
|
|
|
|
file_menu->Append(CLIENT_QUIT, "Quit");
|
|
|
|
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
|
|
|
|
menu_bar->Append(file_menu, "File");
|
|
|
|
|
|
|
|
// Associate the menu bar with the frame
|
|
|
|
SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
// Make a panel
|
|
|
|
panel = new wxPanel(this);
|
|
|
|
the_list = new wxListBox(panel, CLIENT_LISTBOX, wxPoint(5, 5));
|
|
|
|
the_list->Append("Apple");
|
|
|
|
the_list->Append("Pear");
|
|
|
|
the_list->Append("Orange");
|
|
|
|
the_list->Append("Banana");
|
|
|
|
the_list->Append("Fruit");
|
|
|
|
|
|
|
|
panel->Fit();
|
|
|
|
Fit();
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExecute(wxCommandEvent& event)
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
if (the_connection)
|
2000-01-05 20:21:26 -05:00
|
|
|
if (!the_connection->Execute("Hello from the client!"))
|
2000-01-15 17:34:29 -05:00
|
|
|
wxMessageBox("Execute failed", "Client Demo Error");
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPoke(wxCommandEvent& event)
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
if (the_connection)
|
2000-01-05 20:21:26 -05:00
|
|
|
if (!the_connection->Poke("An item", "Some data to poke at the server!"))
|
2000-01-15 17:34:29 -05:00
|
|
|
wxMessageBox("Poke failed", "Client Demo Error");
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnRequest(wxCommandEvent& event)
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
if (the_connection)
|
|
|
|
{
|
1999-01-27 05:01:14 -05:00
|
|
|
char *data = the_connection->Request("An item");
|
|
|
|
if (data)
|
2000-01-15 17:34:29 -05:00
|
|
|
wxMessageBox(data, "Client: Request", wxOK);
|
2000-01-05 20:21:26 -05:00
|
|
|
else
|
2000-01-15 17:34:29 -05:00
|
|
|
wxMessageBox("Request failed", "Client Demo Error");
|
|
|
|
}
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnExit(wxCommandEvent& event)
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
Close();
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
wxConnectionBase *MyClient::OnMakeConnection()
|
1999-01-27 05:01:14 -05:00
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
return new MyConnection;
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
MyConnection::MyConnection()
|
|
|
|
: wxConnection(ipc_buffer, WXSIZEOF(ipc_buffer))
|
1999-01-27 05:01:14 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-01-15 17:34:29 -05:00
|
|
|
MyConnection::~MyConnection()
|
1999-01-27 05:01:14 -05:00
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
the_connection = NULL;
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
if (the_list)
|
|
|
|
{
|
|
|
|
int n = the_list->FindString(data);
|
|
|
|
if (n > -1)
|
|
|
|
the_list->SetSelection(n);
|
|
|
|
}
|
|
|
|
return TRUE;
|
1999-01-27 05:01:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::OnDisconnect()
|
|
|
|
{
|
2000-01-15 17:34:29 -05:00
|
|
|
wxWindow *win = wxTheApp->GetTopWindow();
|
|
|
|
if ( win )
|
|
|
|
win->Destroy();
|
1999-01-27 05:01:14 -05:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|